Changeset 192

Add full bitrate G726 support (still needs audio core modifications)

Committed by:  file
Date:  Sep 30 2005 * 20:51 (over 3 years ago)

Affected files:

openpbx/branches/josh/codecs/Makefile (unified diff)

r8r192
5757 LIBGSM=gsm/lib/libgsm.a
5858 LIBGSMT=gsm/lib/libgsm.a
5959 LIBLPC10=lpc10/liblpc10.a
60 LIBG726=g726/libg726.a
6061
6162 ifeq ($(findstring BSD,${OSARCH}),BSD)
6263 CFLAGS+=-I$(CROSS_COMPILE_TARGET)/usr/local/include -L$(CROSS_COMPILE_TARGET)/usr/local/lib
------
8889 $(LIBLPC10):
8990 $(MAKE) -C lpc10 all
9091
92 $(LIBG726):
93 $(MAKE) -C g726 all
94
9195 $(LIBILBC):
9296 $(MAKE) -C ilbc all
9397
------
112116 codec_lpc10.so: codec_lpc10.o $(LIBLPC10)
113117 $(CC) $(SOLINK) -o $@ $< $(LIBLPC10) -lm
114118
119 codec_g726.so: codec_g726.o $(LIBG726)
120 $(CC) $(SOLINK) -o $@ $< $(LIBG726) -lm
121
115122 %.so : %.o
116123 $(CC) $(SOLINK) -o $@ $<
117124

openpbx/branches/josh/codecs/codec_g726.c (unified diff)

r47r192
11 /*
2 * OpenPBX -- An open source telephony toolkit.
32 *
4 * Copyright (C) 1999 - 2005, Digium, Inc.
3 * codec_g726.c - translate between signed linear and ITU G.726 (all bitrates)
54 *
6 * Mark Spencer <markster@digium.com>
7 *
8 * Based on frompcm.c and topcm.c from the Emiliano MIPL browser/
9 * interpreter. See http://www.bsdtelephony.com.mx
10 *
11 * See http://www.openpbx.org for more information about
12 * the OpenPBX project. Please do not directly contact
13 * any of the maintainers of this project for assistance;
14 * the project provides a web site, mailing lists and IRC
15 * channels for your use.
16 *
17 * This program is free software, distributed under the terms of
18 * the GNU General Public License Version 2. See the LICENSE file
19 * at the top of the source tree.
205 */
216
22
23 /*
24 *
25 * codec_g726.c - translate between signed linear and ITU G.726-32kbps
26 *
27 */
28
297 #include <fcntl.h>
308 #include <netinet/in.h>
319 #include <stdio.h>
------
3513
3614 #include "openpbx.h"
3715
38 OPENPBX_FILE_VERSION(__FILE__, "$Revision$")
39
4016 #include "openpbx/lock.h"
4117 #include "openpbx/logger.h"
4218 #include "openpbx/module.h"
------
4521 #include "openpbx/translate.h"
4622 #include "openpbx/channel.h"
4723
48 #define WANT_ASM
49 #include "log2comp.h"
24 #include "g726/g72x.h"
5025
51 /* define NOT_BLI to use a faster but not bit-level identical version */
52 /* #define NOT_BLI */
53
54 #if defined(NOT_BLI)
55 # if defined(_MSC_VER)
56 typedef __int64 sint64;
57 # elif defined(__GNUC__)
58 typedef long long sint64;
59 # else
60 # error 64-bit integer type is not defined for your compiler/platform
61 # endif
62 #endif
63
6426 #define BUFFER_SIZE 8096 /* size for the translation buffers */
6527 #define BUF_SHIFT 5
6628
6729 OPBX_MUTEX_DEFINE_STATIC(localuser_lock);
6830 static int localusecnt = 0;
6931
70 static char *tdesc = "ITU G.726-32kbps G726 Transcoder";
32 static char *tdesc = "ITU G.726 G726 Transcoder (Full Bitrate Support)";
7133
7234 static int useplc = 0;
7335
------
7638 #include "slin_g726_ex.h"
7739 #include "g726_slin_ex.h"
7840
79 /*
80 * The following is the definition of the state structure
81 * used by the G.721/G.723 encoder and decoder to preserve their internal
82 * state between successive calls. The meanings of the majority
83 * of the state structure fields are explained in detail in the
84 * CCITT Recommendation G.721. The field names are essentially indentical
85 * to variable names in the bit level description of the coding algorithm
86 * included in this Recommendation.
87 */
88 struct g726_state {
89 long yl; /* Locked or steady state step size multiplier. */
90 int yu; /* Unlocked or non-steady state step size multiplier. */
91 int dms; /* Short term energy estimate. */
92 int dml; /* Long term energy estimate. */
93 int ap; /* Linear weighting coefficient of 'yl' and 'yu'. */
94
95 int a[2]; /* Coefficients of pole portion of prediction filter.
96 * stored as fixed-point 1==2^14 */
97 int b[6]; /* Coefficients of zero portion of prediction filter.
98 * stored as fixed-point 1==2^14 */
99 int pk[2]; /* Signs of previous two samples of a partially
100 * reconstructed signal.
101 */
102 int dq[6]; /* Previous 6 samples of the quantized difference signal
103 * stored as fixed point 1==2^12,
104 * or in internal floating point format */
105 int sr[2]; /* Previous 2 samples of the quantized difference signal
106 * stored as fixed point 1==2^12,
107 * or in internal floating point format */
108 int td; /* delayed tone detect, new in 1988 version */
109 };
110
111
112
113 static int qtab_721[7] = {-124, 80, 178, 246, 300, 349, 400};
114 /*
115 * Maps G.721 code word to reconstructed scale factor normalized log
116 * magnitude values.
117 */
118 static int _dqlntab[16] = {-2048, 4, 135, 213, 273, 323, 373, 425,
119 425, 373, 323, 273, 213, 135, 4, -2048};
120
121 /* Maps G.721 code word to log of scale factor multiplier. */
122 static int _witab[16] = {-12, 18, 41, 64, 112, 198, 355, 1122,
123 1122, 355, 198, 112, 64, 41, 18, -12};
124 /*
125 * Maps G.721 code words to a set of values whose long and short
126 * term averages are computed and then compared to give an indication
127 * how stationary (steady state) the signal is.
128 */
129 static int _fitab[16] = {0, 0, 0, 0x200, 0x200, 0x200, 0x600, 0xE00,
130 0xE00, 0x600, 0x200, 0x200, 0x200, 0, 0, 0};
131
132 /* Deprecated
133 static int power2[15] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80,
134 0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000};
135 */
136
137 /*
138 * g72x_init_state()
139 *
140 * This routine initializes and/or resets the g726_state structure
141 * pointed to by 'state_ptr'.
142 * All the initial state values are specified in the CCITT G.721 document.
143 */
144 static void g726_init_state(struct g726_state *state_ptr)
145 {
146 int cnta;
147
148 state_ptr->yl = 34816;
149 state_ptr->yu = 544;
150 state_ptr->dms = 0;
151 state_ptr->dml = 0;
152 state_ptr->ap = 0;
153 for (cnta = 0; cnta < 2; cnta++)
154 {
155 state_ptr->a[cnta] = 0;
156 state_ptr->pk[cnta] = 0;
157 #ifdef NOT_BLI
158 state_ptr->sr[cnta] = 1;
159 #else
160 state_ptr->sr[cnta] = 32;
161 #endif
162 }
163 for (cnta = 0; cnta < 6; cnta++)
164 {
165 state_ptr->b[cnta] = 0;
166 #ifdef NOT_BLI
167 state_ptr->dq[cnta] = 1;
168 #else
169 state_ptr->dq[cnta] = 32;
170 #endif
171 }
172 state_ptr->td = 0;
173 }
174
175 /*
176 * quan()
177 *
178 * quantizes the input val against the table of integers.
179 * It returns i if table[i - 1] <= val < table[i].
180 *
181 * Using linear search for simple coding.
182 */
183 static int quan(int val, int *table, int size)
184 {
185 int i;
186
187 for (i = 0; i < size && val >= *table; ++i, ++table)
188 ;
189 return (i);
190 }
191
192 #ifdef NOT_BLI /* faster non-identical version */
193
194 /*
195 * predictor_zero()
196 *
197 * computes the estimated signal from 6-zero predictor.
198 *
199 */
200 static int predictor_zero(struct g726_state *state_ptr)
201 { /* divide by 2 is necessary here to handle negative numbers correctly */
202 int i;
203 sint64 sezi;
204 for (sezi = 0, i = 0; i < 6; i++) /* ACCUM */
205 sezi += (sint64)state_ptr->b[i] * state_ptr->dq[i];
206 return (int)(sezi >> 13) / 2 /* 2^14 */;
207 }
208
209 /*
210 * predictor_pole()
211 *
212 * computes the estimated signal from 2-pole predictor.
213 *
214 */
215 static int predictor_pole(struct g726_state *state_ptr)
216 { /* divide by 2 is necessary here to handle negative numbers correctly */
217 return (int)(((sint64)state_ptr->a[1] * state_ptr->sr[1] +
218 (sint64)state_ptr->a[0] * state_ptr->sr[0]) >> 13) / 2 /* 2^14 */;
219 }
220
221 #else /* NOT_BLI - identical version */
222 /*
223 * fmult()
224 *
225 * returns the integer product of the fixed-point number "an" (1==2^12) and
226 * "floating point" representation (4-bit exponent, 6-bit mantessa) "srn".
227 */
228 static int fmult(int an, int srn)
229 {
230 int anmag, anexp, anmant;
231 int wanexp, wanmant;
232 int retval;
233
234 anmag = (an > 0) ? an : ((-an) & 0x1FFF);
235 anexp = ilog2(anmag) - 5;
236 anmant = (anmag == 0) ? 32 :
237 (anexp >= 0) ? anmag >> anexp : anmag << -anexp;
238 wanexp = anexp + ((srn >> 6) & 0xF) - 13;
239
240 wanmant = (anmant * (srn & 077) + 0x30) >> 4;
241 retval = (wanexp >= 0) ? ((wanmant << wanexp) & 0x7FFF) :
242 (wanmant >> -wanexp);
243
244 return (((an ^ srn) < 0) ? -retval : retval);
245 }
246
247 static int predictor_zero(struct g726_state *state_ptr)
248 {
249 int i;
250 int sezi;
251 for (sezi = 0, i = 0; i < 6; i++) /* ACCUM */
252 sezi += fmult(state_ptr->b[i] >> 2, state_ptr->dq[i]);
253 return sezi;
254 }
255
256 static int predictor_pole(struct g726_state *state_ptr)
257 {
258 return (fmult(state_ptr->a[1] >> 2, state_ptr->sr[1]) +
259 fmult(state_ptr->a[0] >> 2, state_ptr->sr[0]));
260 }
261
262 #endif /* NOT_BLI */
263
264 /*
265 * step_size()
266 *
267 * computes the quantization step size of the adaptive quantizer.
268 *
269 */
270 static int step_size(struct g726_state *state_ptr)
271 {
272 int y;
273 int dif;
274 int al;
275
276 if (state_ptr->ap >= 256)
277 return (state_ptr->yu);
278 else {
279 y = state_ptr->yl >> 6;
280 dif = state_ptr->yu - y;
281 al = state_ptr->ap >> 2;
282 if (dif > 0)
283 y += (dif * al) >> 6;
284 else if (dif < 0)
285 y += (dif * al + 0x3F) >> 6;
286 return (y);
287 }
288 }
289
290 /*
291 * quantize()
292 *
293 * Given a raw sample, 'd', of the difference signal and a
294 * quantization step size scale factor, 'y', this routine returns the
295 * ADPCM codeword to which that sample gets quantized. The step
296 * size scale factor division operation is done in the log base 2 domain
297 * as a subtraction.
298 */
299 static int quantize(
300 int d, /* Raw difference signal sample */
301 int y, /* Step size multiplier */
302 int *table, /* quantization table */
303 int size) /* table size of integers */
304 {
305 int dqm; /* Magnitude of 'd' */
306 int exp; /* Integer part of base 2 log of 'd' */
307 int mant; /* Fractional part of base 2 log */
308 int dl; /* Log of magnitude of 'd' */
309 int dln; /* Step size scale factor normalized log */
310 int i;
311
312 /*
313 * LOG
314 *
315 * Compute base 2 log of 'd', and store in 'dl'.
316 */
317 dqm = abs(d);
318 exp = ilog2(dqm);
319 if (exp < 0)
320 exp = 0;
321 mant = ((dqm << 7) >> exp) & 0x7F; /* Fractional portion. */
322 dl = (exp << 7) | mant;
323
324 /*
325 * SUBTB
326 *
327 * "Divide" by step size multiplier.
328 */
329 dln = dl - (y >> 2);
330
331 /*
332 * QUAN
333 *
334 * Obtain codword i for 'd'.
335 */
336 i = quan(dln, table, size);
337 if (d < 0) /* take 1's complement of i */
338 return ((size << 1) + 1 - i);
339 else if (i == 0) /* take 1's complement of 0 */
340 return ((size << 1) + 1); /* new in 1988 */
341 else
342 return (i);
343 }
344
345 /*
346 * reconstruct()
347 *
348 * Returns reconstructed difference signal 'dq' obtained from
349 * codeword 'i' and quantization step size scale factor 'y'.
350 * Multiplication is performed in log base 2 domain as addition.
351 */
352 static int reconstruct(
353 int sign, /* 0 for non-negative value */
354 int dqln, /* G.72x codeword */
355 int y) /* Step size multiplier */
356 {
357 int dql; /* Log of 'dq' magnitude */
358 int dex; /* Integer part of log */
359 int dqt;
360 int dq; /* Reconstructed difference signal sample */
361
362 dql = dqln + (y >> 2); /* ADDA */
363
364 if (dql < 0) {
365 #ifdef NOT_BLI
366 return (sign) ? -1 : 1;
367 #else
368 return (sign) ? -0x8000 : 0;
369 #endif
370 } else { /* ANTILOG */
371 dex = (dql >> 7) & 15;
372 dqt = 128 + (dql & 127);
373 #ifdef NOT_BLI
374 dq = ((dqt << 19) >> (14 - dex));
375 return (sign) ? -dq : dq;
376 #else
377 dq = (dqt << 7) >> (14 - dex);
378 return (sign) ? (dq - 0x8000) : dq;
379 #endif
380 }
381 }
382
383 /*
384 * update()
385 *
386 * updates the state variables for each output code
387 */
388 static void update(
389 int code_size, /* distinguish 723_40 with others */
390 int y, /* quantizer step size */
391 int wi, /* scale factor multiplier */
392 int fi, /* for long/short term energies */
393 int dq, /* quantized prediction difference */
394 int sr, /* reconstructed signal */
395 int dqsez, /* difference from 2-pole predictor */
396 struct g726_state *state_ptr) /* coder state pointer */
397 {
398 int cnt;
399 int mag; /* Adaptive predictor, FLOAT A */
400 #ifndef NOT_BLI
401 int exp;
402 #endif
403 int a2p=0; /* LIMC */
404 int a1ul; /* UPA1 */
405 int pks1; /* UPA2 */
406 int fa1;
407 int tr; /* tone/transition detector */
408 int ylint, thr2, dqthr;
409 int ylfrac, thr1;
410 int pk0;
411
412 pk0 = (dqsez < 0) ? 1 : 0; /* needed in updating predictor poles */
413
414 #ifdef NOT_BLI
415 mag = abs(dq / 0x1000); /* prediction difference magnitude */
416 #else
417 mag = dq & 0x7FFF; /* prediction difference magnitude */
418 #endif
419 /* TRANS */
420 ylint = state_ptr->yl >> 15; /* exponent part of yl */
421 ylfrac = (state_ptr->yl >> 10) & 0x1F; /* fractional part of yl */
422 thr1 = (32 + ylfrac) << ylint; /* threshold */
423 thr2 = (ylint > 9) ? 31 << 10 : thr1; /* limit thr2 to 31 << 10 */
424 dqthr = (thr2 + (thr2 >> 1)) >> 1; /* dqthr = 0.75 * thr2 */
425 if (state_ptr->td == 0) /* signal supposed voice */
426 tr = 0;
427 else if (mag <= dqthr) /* supposed data, but small mag */
428 tr = 0; /* treated as voice */
429 else /* signal is data (modem) */
430 tr = 1;
431
432 /*
433 * Quantizer scale factor adaptation.
434 */
435
436 /* FUNCTW & FILTD & DELAY */
437 /* update non-steady state step size multiplier */
438 state_ptr->yu = y + ((wi - y) >> 5);
439
440 /* LIMB */
441 if (state_ptr->yu < 544) /* 544 <= yu <= 5120 */
442 state_ptr->yu = 544;
443 else if (state_ptr->yu > 5120)
444 state_ptr->yu = 5120;
445
446 /* FILTE & DELAY */
447 /* update steady state step size multiplier */
448 state_ptr->yl += state_ptr->yu + ((-state_ptr->yl) >> 6);
449
450 /*
451 * Adaptive predictor coefficients.
452 */
453 if (tr == 1) { /* reset a's and b's for modem signal */
454 state_ptr->a[0] = 0;
455 state_ptr->a[1] = 0;
456 state_ptr->b[0] = 0;
457 state_ptr->b[1] = 0;
458 state_ptr->b[2] = 0;
459 state_ptr->b[3] = 0;
460 state_ptr->b[4] = 0;
461 state_ptr->b[5] = 0;
462 } else { /* update a's and b's */
463 pks1 = pk0 ^ state_ptr->pk[0]; /* UPA2 */
464
465 /* update predictor pole a[1] */
466 a2p = state_ptr->a[1] - (state_ptr->a[1] >> 7);
467 if (dqsez != 0) {
468 fa1 = (pks1) ? state_ptr->a[0] : -state_ptr->a[0];
469 if (fa1 < -8191) /* a2p = function of fa1 */
470 a2p -= 0x100;
471 else if (fa1 > 8191)
472 a2p += 0xFF;
473 else
474 a2p += fa1 >> 5;
475
476 if (pk0 ^ state_ptr->pk[1])
477 /* LIMC */
478 if (a2p <= -12160)
479 a2p = -12288;
480 else if (a2p >= 12416)
481 a2p = 12288;
482 else
483 a2p -= 0x80;
484 else if (a2p <= -12416)
485 a2p = -12288;
486 else if (a2p >= 12160)
487 a2p = 12288;
488 else
489 a2p += 0x80;
490 }
491
492 /* TRIGB & DELAY */
493 state_ptr->a[1] = a2p;
494
495 /* UPA1 */
496 /* update predictor pole a[0] */
497 state_ptr->a[0] -= state_ptr->a[0] >> 8;
498 if (dqsez != 0) {
499 if (pks1 == 0)
500 state_ptr->a[0] += 192;
501 else
502 state_ptr->a[0] -= 192;
503 }
504 /* LIMD */
505 a1ul = 15360 - a2p;
506 if (state_ptr->a[0] < -a1ul)
507 state_ptr->a[0] = -a1ul;
508 else if (state_ptr->a[0] > a1ul)
509 state_ptr->a[0] = a1ul;
510
511 /* UPB : update predictor zeros b[6] */
512 for (cnt = 0; cnt < 6; cnt++) {
513 if (code_size == 5) /* for 40Kbps G.723 */
514 state_ptr->b[cnt] -= state_ptr->b[cnt] >> 9;
515 else /* for G.721 and 24Kbps G.723 */
516 state_ptr->b[cnt] -= state_ptr->b[cnt] >> 8;
517 if (mag)
518 { /* XOR */
519 if ((dq ^ state_ptr->dq[cnt]) >= 0)
520 state_ptr->b[cnt] += 128;
521 else
522 state_ptr->b[cnt] -= 128;
523 }
524 }
525 }
526
527 for (cnt = 5; cnt > 0; cnt--)
528 state_ptr->dq[cnt] = state_ptr->dq[cnt-1];
529 #ifdef NOT_BLI
530 state_ptr->dq[0] = dq;
531 #else
532 /* FLOAT A : convert dq[0] to 4-bit exp, 6-bit mantissa f.p. */
533 if (mag == 0) {
534 state_ptr->dq[0] = (dq >= 0) ? 0x20 : 0x20 - 0x400;
535 } else {
536 exp = ilog2(mag) + 1;
537 state_ptr->dq[0] = (dq >= 0) ?
538 (exp << 6) + ((mag << 6) >> exp) :
539 (exp << 6) + ((mag << 6) >> exp) - 0x400;
540 }
541 #endif
542
543 state_ptr->sr[1] = state_ptr->sr[0];
544 #ifdef NOT_BLI
545 state_ptr->sr[0] = sr;
546 #else
547 /* FLOAT B : convert sr to 4-bit exp., 6-bit mantissa f.p. */
548 if (sr == 0) {
549 state_ptr->sr[0] = 0x20;
550 } else if (sr > 0) {
551 exp = ilog2(sr) + 1;
552 state_ptr->sr[0] = (exp << 6) + ((sr << 6) >> exp);
553 } else if (sr > -0x8000) {
554 mag = -sr;
555 exp = ilog2(mag) + 1;
556 state_ptr->sr[0] = (exp << 6) + ((mag << 6) >> exp) - 0x400;
557 } else
558 state_ptr->sr[0] = 0x20 - 0x400;
559 #endif
560
561 /* DELAY A */
562 state_ptr->pk[1] = state_ptr->pk[0];
563 state_ptr->pk[0] = pk0;
564
565 /* TONE */
566 if (tr == 1) /* this sample has been treated as data */
567 state_ptr->td = 0; /* next one will be treated as voice */
568 else if (a2p < -11776) /* small sample-to-sample correlation */
569 state_ptr->td = 1; /* signal may be data */
570 else /* signal is voice */
571 state_ptr->td = 0;
572
573 /*
574 * Adaptation speed control.
575 */
576 state_ptr->dms += (fi - state_ptr->dms) >> 5; /* FILTA */
577 state_ptr->dml += (((fi << 2) - state_ptr->dml) >> 7); /* FILTB */
578
579 if (tr == 1)
580 state_ptr->ap = 256;
581 else if (y < 1536) /* SUBTC */
582 state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
583 else if (state_ptr->td == 1)
584 state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
585 else if (abs((state_ptr->dms << 2) - state_ptr->dml) >=
586 (state_ptr->dml >> 3))
587 state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
588 else
589 state_ptr->ap += (-state_ptr->ap) >> 4;
590 }
591
592 /*
593 * g726_decode()
594 *
595 * Description:
596 *
597 * Decodes a 4-bit code of G.726-32 encoded data of i and
598 * returns the resulting linear PCM, A-law or u-law value.
599 * return -1 for unknown out_coding value.
600 */
601 static int g726_decode(int i, struct g726_state *state_ptr)
602 {
603 int sezi, sez, se; /* ACCUM */
604 int y; /* MIX */
605 int sr; /* ADDB */
606 int dq;
607 int dqsez;
608
609 i &= 0x0f; /* mask to get proper bits */
610 #ifdef NOT_BLI
611 sezi = predictor_zero(state_ptr);
612 sez = sezi;
613 se = sezi + predictor_pole(state_ptr); /* estimated signal */
614 #else
615 sezi = predictor_zero(state_ptr);
616 sez = sezi >> 1;
617 se = (sezi + predictor_pole(state_ptr)) >> 1; /* estimated signal */
618 #endif
619
620 y = step_size(state_ptr); /* dynamic quantizer step size */
621
622 dq = reconstruct(i & 8, _dqlntab[i], y); /* quantized diff. */
623
624 #ifdef NOT_BLI
625 sr = se + dq; /* reconst. signal */
626 dqsez = dq + sez; /* pole prediction diff. */
627 #else
628 sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconst. signal */
629 dqsez = sr - se + sez; /* pole prediction diff. */
630 #endif
631
632 update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
633
634 #ifdef NOT_BLI
635 return (sr >> 10); /* sr was 26-bit dynamic range */
636 #else
637 return (sr << 2); /* sr was 14-bit dynamic range */
638 #endif
639 }
640
641 /*
642 * g726_encode()
643 *
644 * Encodes the input vale of linear PCM, A-law or u-law data sl and returns
645 * the resulting code. -1 is returned for unknown input coding value.
646 */
647 static int g726_encode(int sl, struct g726_state *state_ptr)
648 {
649 int sezi, se, sez; /* ACCUM */
650 int d; /* SUBTA */
651 int sr; /* ADDB */
652 int y; /* MIX */
653 int dqsez; /* ADDC */
654 int dq, i;
655
656 #ifdef NOT_BLI
657 sl <<= 10; /* 26-bit dynamic range */
658
659 sezi = predictor_zero(state_ptr);
660 sez = sezi;
661 se = sezi + predictor_pole(state_ptr); /* estimated signal */
662 #else
663 sl >>= 2; /* 14-bit dynamic range */
664
665 sezi = predictor_zero(state_ptr);
666 sez = sezi >> 1;
667 se = (sezi + predictor_pole(state_ptr)) >> 1; /* estimated signal */
668 #endif
669
670 d = sl - se; /* estimation difference */
671
672 /* quantize the prediction difference */
673 y = step_size(state_ptr); /* quantizer step size */
674 #ifdef NOT_BLI
675 d /= 0x1000;
676 #endif
677 i = quantize(d, y, qtab_721, 7); /* i = G726 code */
678
679 dq = reconstruct(i & 8, _dqlntab[i], y); /* quantized est diff */
680
681 #ifdef NOT_BLI
682 sr = se + dq; /* reconst. signal */
683 dqsez = dq + sez; /* pole prediction diff. */
684 #else
685 sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconst. signal */
686 dqsez = sr - se + sez; /* pole prediction diff. */
687 #endif
688
689 update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
690
691 return (i);
692 }
693
694 /*
695 * Private workspace for translating signed linear signals to G726.
696 */
697
69841 struct g726_encoder_pvt
69942 {
70043 struct opbx_frame f;
70144 char offset[OPBX_FRIENDLY_OFFSET]; /* Space to build offset */
70245 unsigned char outbuf[BUFFER_SIZE]; /* Encoded G726, two nibbles to a word */
70346 unsigned char next_flag;
704 struct g726_state g726;
47 struct g726_state_s g726;
70548 int tail;
49 int size;
50 int bits;
51 unsigned int out_buffer;
52 int out_bits;
70653 };
70754
708 /*
709 * Private workspace for translating G726 signals to signed linear.
710 */
711
71255 struct g726_decoder_pvt
71356 {
71457 struct opbx_frame f;
71558 char offset[OPBX_FRIENDLY_OFFSET]; /* Space to build offset */
71659 short outbuf[BUFFER_SIZE]; /* Decoded signed linear values */
717 struct g726_state g726;
60 struct g726_state_s g726;
71861 int tail;
62 int size;
63 int bits;
64 unsigned int in_buffer;
65 int in_bits;
71966 plc_state_t plc;
72067 };
72168
722 /*
723 * G726ToLin_New
724 * Create a new instance of g726_decoder_pvt.
725 *
726 * Results:
727 * Returns a pointer to the new instance.
728 *
729 * Side effects:
730 * None.
731 */
732
73369 static struct opbx_translator_pvt *
73470 g726tolin_new (void)
73571 {
------
73773 tmp = malloc (sizeof (struct g726_decoder_pvt));
73874 if (tmp)
73975 {
740 memset(tmp, 0, sizeof(*tmp));
76 memset(tmp, 0, sizeof(*tmp));
74177 tmp->tail = 0;
74278 plc_init(&tmp->plc);
74379 localusecnt++;
744 g726_init_state(&tmp->g726);
80 g726_init_state(&tmp->g726);
81 /* Reset all common variables */
82 tmp->size = 32;
83 tmp->bits = 4;
84 tmp->in_buffer = 0;
85 tmp->in_bits = 0;
74586 opbx_update_use_count ();
74687 }
74788 return (struct opbx_translator_pvt *) tmp;
74889 }
74990
750 /*
751 * LinToG726_New
752 * Create a new instance of g726_encoder_pvt.
753 *
754 * Results:
755 * Returns a pointer to the new instance.
756 *
757 * Side effects:
758 * None.
759 */
760
76191 static struct opbx_translator_pvt *
76292 lintog726_new (void)
76393 {
------
76595 tmp = malloc (sizeof (struct g726_encoder_pvt));
76696 if (tmp)
76797 {
768 memset(tmp, 0, sizeof(*tmp));
98 memset(tmp, 0, sizeof(*tmp));
76999 localusecnt++;
770100 tmp->tail = 0;
771 g726_init_state(&tmp->g726);
101 g726_init_state(&tmp->g726);
102 /* Reset all common variables */
103 tmp->size = 32;
104 tmp->bits = 4;
105 tmp->out_buffer = 0;
106 tmp->out_bits = 0;
772107 opbx_update_use_count ();
773108 }
774109 return (struct opbx_translator_pvt *) tmp;
775110 }
776111
777 /*
778 * G726ToLin_FrameIn
779 * Fill an input buffer with packed 4-bit G726 values if there is room
780 * left.
781 *
782 * Results:
783 * Foo
784 *
785 * Side effects:
786 * tmp->tail is the number of packed values in the buffer.
787 */
112 static int g726_encoder(int size, int sample, g726_state *state_ptr)
113 {
114 if (size == 16)
115 return g726_16_encoder(sample, AUDIO_ENCODING_LINEAR, state_ptr);
116 else if (size == 24)
117 return g726_24_encoder(sample, AUDIO_ENCODING_LINEAR, state_ptr);
118 else if (size == 32)
119 return g726_32_encoder(sample, AUDIO_ENCODING_LINEAR, state_ptr);
120 else if (size == 40)
121 return g726_40_encoder(sample, AUDIO_ENCODING_LINEAR, state_ptr);
122 return 0;
123 }
788124
125 static int g726_decoder(int size, int code, g726_state *state_ptr)
126 {
127 if (size == 16)
128 return g726_16_decoder(code, AUDIO_ENCODING_LINEAR, state_ptr);
129 else if (size == 24)
130 return g726_24_decoder(code, AUDIO_ENCODING_LINEAR, state_ptr);
131 else if (size == 32)
132 return g726_32_decoder(code, AUDIO_ENCODING_LINEAR, state_ptr);
133 else if (size == 40)
134 return g726_40_decoder(code, AUDIO_ENCODING_LINEAR, state_ptr);
135 return 0;
136 }
137
789138 static int
790139 g726tolin_framein (struct opbx_translator_pvt *pvt, struct opbx_frame *f)
791140 {
792141 struct g726_decoder_pvt *tmp = (struct g726_decoder_pvt *) pvt;
793 unsigned char *b;
794 int x;
142 unsigned char *b = NULL;
143 int x = 0;
795144
796145 if(f->datalen == 0) { /* perform PLC with nominal framesize of 20ms/160 samples */
797146 if((tmp->tail + 160) > BUFFER_SIZE) {
------
806155 }
807156
808157 b = f->data;
809 for (x=0;x<f->datalen;x++) {
810 if (tmp->tail >= BUFFER_SIZE) {
811 opbx_log(LOG_WARNING, "Out of buffer space!\n");
812 return -1;
813 }
814 tmp->outbuf[tmp->tail++] = g726_decode((b[x] >> 4) & 0xf, &tmp->g726);
815 if (tmp->tail >= BUFFER_SIZE) {
816 opbx_log(LOG_WARNING, "Out of buffer space!\n");
817 return -1;
818 }
819 tmp->outbuf[tmp->tail++] = g726_decode(b[x] & 0x0f, &tmp->g726);
158
159 while (x < f->datalen) {
160 if (tmp->in_bits < tmp->bits) {
161 tmp->in_buffer |= (b[x++] << tmp->in_bits);
162 tmp->in_bits += 8;
163 }
164 tmp->outbuf[tmp->tail++] = g726_decoder(tmp->size, tmp->in_buffer & ((1 << tmp->bits) - 1), &tmp->g726);
165 tmp->in_buffer >>= tmp->bits;
166 tmp->in_bits -= tmp->bits;
820167 }
821168
822169 if(useplc) plc_rx(&tmp->plc, tmp->outbuf+tmp->tail-f->datalen*2, f->datalen*2);
------
824171 return 0;
825172 }
826173
827 /*
828 * G726ToLin_FrameOut
829 * Convert 4-bit G726 encoded signals to 16-bit signed linear.
830 *
831 * Results:
832 * Converted signals are placed in tmp->f.data, tmp->f.datalen
833 * and tmp->f.samples are calculated.
834 *
835 * Side effects:
836 * None.
837 */
838
839174 static struct opbx_frame *
840175 g726tolin_frameout (struct opbx_translator_pvt *pvt)
841176 {
------
856191 return &tmp->f;
857192 }
858193
859 /*
860 * LinToG726_FrameIn
861 * Fill an input buffer with 16-bit signed linear PCM values.
862 *
863 * Results:
864 * None.
865 *
866 * Side effects:
867 * tmp->tail is number of signal values in the input buffer.
868 */
869
870194 static int
871195 lintog726_framein (struct opbx_translator_pvt *pvt, struct opbx_frame *f)
872196 {
873197 struct g726_encoder_pvt *tmp = (struct g726_encoder_pvt *) pvt;
874198 short *s = f->data;
875199 int samples = f->datalen / 2;
876 int x;
877 for (x=0;x<samples;x++) {
878 if (tmp->next_flag & 0x80) {
879 if (tmp->tail >= BUFFER_SIZE) {
880 opbx_log(LOG_WARNING, "Out of buffer space\n");
881 return -1;
882 }
883 tmp->outbuf[tmp->tail++] = ((tmp->next_flag & 0xf)<< 4) | g726_encode(s[x], &tmp->g726);
884 tmp->next_flag = 0;
885 } else {
886 tmp->next_flag = 0x80 | g726_encode(s[x], &tmp->g726);
887 }
200 int x = 0;
201
202 for (x=0; x<samples; x++) {
203 tmp->out_buffer |= ((unsigned char)g726_encoder(tmp->size, s[x], &tmp->g726) << tmp->out_bits);
204 tmp->out_bits += tmp->bits;
205 if (tmp->out_bits >= 8) {
206 tmp->outbuf[tmp->tail++] = tmp->out_buffer & 0xff;
207 tmp->out_bits -= 8;
208 tmp->out_buffer >>= 8;
209 }
888210 }
211
889212 return 0;
890213 }
891214
892 /*
893 * LinToG726_FrameOut
894 * Convert a buffer of raw 16-bit signed linear PCM to a buffer
895 * of 4-bit G726 packed two to a byte (Big Endian).
896 *
897 * Results:
898 * Foo
899 *
900 * Side effects:
901 * Leftover inbuf data gets packed, tail gets updated.
902 */
903
904215 static struct opbx_frame *
905216 lintog726_frameout (struct opbx_translator_pvt *pvt)
906217 {
------
921232 return &tmp->f;
922233 }
923234
924
925 /*
926 * G726ToLin_Sample
927 */
928
929235 static struct opbx_frame *
930236 g726tolin_sample (void)
931237 {
------
941247 return &f;
942248 }
943249
944 /*
945 * LinToG726_Sample
946 */
947
948250 static struct opbx_frame *
949251 lintog726_sample (void)
950252 {
------
961263 return &f;
962264 }
963265
964 /*
965 * G726_Destroy
966 * Destroys a private workspace.
967 *
968 * Results:
969 * It's gone!
970 *
971 * Side effects:
972 * None.
973 */
974
975266 static void
976267 g726_destroy (struct opbx_translator_pvt *pvt)
977268 {
------
980271 opbx_update_use_count ();
981272 }
982273
983 /*
984 * The complete translator for G726ToLin.
985 */
986
987274 static struct opbx_translator g726tolin = {
988275 "g726tolin",
989276 OPBX_FORMAT_G726,
------
996283 g726tolin_sample
997284 };
998285
999 /*
1000 * The complete translator for LinToG726.
1001 */
1002
1003286 static struct opbx_translator lintog726 = {
1004287 "lintog726",
1005288 OPBX_FORMAT_SLINEAR,
------
1083366 STANDARD_USECOUNT (res);
1084367 return res;
1085368 }
369