Bug Summary

File:g726.c
Warning:line 612, column 35
The result of the left shift is undefined because the left operand is negative

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name g726.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-eagerly-assume -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -menable-no-infs -menable-no-nans -menable-unsafe-fp-math -fno-signed-zeros -mreassociate -freciprocal-math -fno-trapping-math -ffp-contract=fast -ffast-math -ffinite-math-only -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -target-feature +sse2 -dwarf-column-info -debugger-tuning=gdb -resource-dir /usr/lib/llvm-7/lib/clang/7.0.1 -D HAVE_CONFIG_H -I . -I .. -I /usr/include/libxml2 -D NDEBUG -D HAVE_VISIBILITY=1 -D PIC -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.0.1/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wwrite-strings -std=gnu99 -fconst-strings -fdebug-compilation-dir /drone/src/src -ferror-limit 19 -fmessage-length 0 -fvisibility hidden -fobjc-runtime=gcc -fdiagnostics-show-option -analyzer-output=html -o /drone/src/scan-build/2021-05-27-190700-749-1 -x c g726.c -faddrsig
1/*
2 * SpanDSP - a series of DSP components for telephony
3 *
4 * g726.c - The ITU G.726 codec.
5 *
6 * Written by Steve Underwood <steveu@coppice.org>
7 *
8 * Copyright (C) 2006 Steve Underwood
9 *
10 * All rights reserved.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 2.1,
14 * as published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 * Based on G.721/G.723 code which is:
26 *
27 * This source code is a product of Sun Microsystems, Inc. and is provided
28 * for unrestricted use. Users may copy or modify this source code without
29 * charge.
30 *
31 * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
32 * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
33 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
34 *
35 * Sun source code is provided with no support and without any obligation on
36 * the part of Sun Microsystems, Inc. to assist in its use, correction,
37 * modification or enhancement.
38 *
39 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
40 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
41 * OR ANY PART THEREOF.
42 *
43 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
44 * or profits or other special, indirect and consequential damages, even if
45 * Sun has been advised of the possibility of such damages.
46 *
47 * Sun Microsystems, Inc.
48 * 2550 Garcia Avenue
49 * Mountain View, California 94043
50 */
51
52/*! \file */
53
54#if defined(HAVE_CONFIG_H1)
55#include "config.h"
56#endif
57
58#include <inttypes.h>
59#include <memory.h>
60#include <stdlib.h>
61#if defined(HAVE_TGMATH_H1)
62#include <tgmath.h>
63#endif
64#if defined(HAVE_MATH_H1)
65#include <math.h>
66#endif
67#if defined(HAVE_STDBOOL_H1)
68#include <stdbool.h>
69#else
70#include "spandsp/stdbool.h"
71#endif
72#include "floating_fudge.h"
73
74#include "spandsp/telephony.h"
75#include "spandsp/alloc.h"
76#include "spandsp/bitstream.h"
77#include "spandsp/bit_operations.h"
78#include "spandsp/g711.h"
79#include "spandsp/g726.h"
80
81#include "spandsp/private/bitstream.h"
82#include "spandsp/private/g726.h"
83
84/*
85 * Maps G.726_16 code word to reconstructed scale factor normalized log
86 * magnitude values.
87 */
88static const int g726_16_dqlntab[4] =
89{
90 116, 365, 365, 116
91};
92
93/* Maps G.726_16 code word to log of scale factor multiplier. */
94static const int g726_16_witab[4] =
95{
96 -704, 14048, 14048, -704
97};
98
99/*
100 * Maps G.726_16 code words to a set of values whose long and short
101 * term averages are computed and then compared to give an indication
102 * how stationary (steady state) the signal is.
103 */
104static const int g726_16_fitab[4] =
105{
106 0x000, 0xE00, 0xE00, 0x000
107};
108
109static const int qtab_726_16[1] =
110{
111 261
112};
113
114/*
115 * Maps G.726_24 code word to reconstructed scale factor normalized log
116 * magnitude values.
117 */
118static const int g726_24_dqlntab[8] =
119{
120 -2048, 135, 273, 373, 373, 273, 135, -2048
121};
122
123/* Maps G.726_24 code word to log of scale factor multiplier. */
124static const int g726_24_witab[8] =
125{
126 -128, 960, 4384, 18624, 18624, 4384, 960, -128
127};
128
129/*
130 * Maps G.726_24 code words to a set of values whose long and short
131 * term averages are computed and then compared to give an indication
132 * how stationary (steady state) the signal is.
133 */
134static const int g726_24_fitab[8] =
135{
136 0x000, 0x200, 0x400, 0xE00, 0xE00, 0x400, 0x200, 0x000
137};
138
139static const int qtab_726_24[3] =
140{
141 8, 218, 331
142};
143
144/*
145 * Maps G.726_32 code word to reconstructed scale factor normalized log
146 * magnitude values.
147 */
148static const int g726_32_dqlntab[16] =
149{
150 -2048, 4, 135, 213, 273, 323, 373, 425,
151 425, 373, 323, 273, 213, 135, 4, -2048
152};
153
154/* Maps G.726_32 code word to log of scale factor multiplier. */
155static const int g726_32_witab[16] =
156{
157 -384, 576, 1312, 2048, 3584, 6336, 11360, 35904,
158 35904, 11360, 6336, 3584, 2048, 1312, 576, -384
159};
160
161/*
162 * Maps G.726_32 code words to a set of values whose long and short
163 * term averages are computed and then compared to give an indication
164 * how stationary (steady state) the signal is.
165 */
166static const int g726_32_fitab[16] =
167{
168 0x000, 0x000, 0x000, 0x200, 0x200, 0x200, 0x600, 0xE00,
169 0xE00, 0x600, 0x200, 0x200, 0x200, 0x000, 0x000, 0x000
170};
171
172static const int qtab_726_32[7] =
173{
174 -124, 80, 178, 246, 300, 349, 400
175};
176
177/*
178 * Maps G.726_40 code word to ructeconstructed scale factor normalized log
179 * magnitude values.
180 */
181static const int g726_40_dqlntab[32] =
182{
183 -2048, -66, 28, 104, 169, 224, 274, 318,
184 358, 395, 429, 459, 488, 514, 539, 566,
185 566, 539, 514, 488, 459, 429, 395, 358,
186 318, 274, 224, 169, 104, 28, -66, -2048
187};
188
189/* Maps G.726_40 code word to log of scale factor multiplier. */
190static const int g726_40_witab[32] =
191{
192 448, 448, 768, 1248, 1280, 1312, 1856, 3200,
193 4512, 5728, 7008, 8960, 11456, 14080, 16928, 22272,
194 22272, 16928, 14080, 11456, 8960, 7008, 5728, 4512,
195 3200, 1856, 1312, 1280, 1248, 768, 448, 448
196};
197
198/*
199 * Maps G.726_40 code words to a set of values whose long and short
200 * term averages are computed and then compared to give an indication
201 * how stationary (steady state) the signal is.
202 */
203static const int g726_40_fitab[32] =
204{
205 0x000, 0x000, 0x000, 0x000, 0x000, 0x200, 0x200, 0x200,
206 0x200, 0x200, 0x400, 0x600, 0x800, 0xA00, 0xC00, 0xC00,
207 0xC00, 0xC00, 0xA00, 0x800, 0x600, 0x400, 0x200, 0x200,
208 0x200, 0x200, 0x200, 0x000, 0x000, 0x000, 0x000, 0x000
209};
210
211static const int qtab_726_40[15] =
212{
213 -122, -16, 68, 139, 198, 250, 298, 339,
214 378, 413, 445, 475, 502, 528, 553
215};
216
217/*
218 * returns the integer product of the 14-bit integer "an" and
219 * "floating point" representation (4-bit exponent, 6-bit mantessa) "srn".
220 */
221static int16_t fmult(int16_t an, int16_t srn)
222{
223 int16_t anmag;
224 int16_t anexp;
225 int16_t anmant;
226 int16_t wanexp;
227 int16_t wanmant;
228 int16_t retval;
229
230 anmag = (an > 0) ? an : ((-an) & 0x1FFF);
231 anexp = (int16_t) (top_bit(anmag) - 5);
232 anmant = (anmag == 0) ? 32 : (anexp >= 0) ? (anmag >> anexp) : (anmag << -anexp);
233 wanexp = anexp + ((srn >> 6) & 0xF) - 13;
234
235 wanmant = (anmant*(srn & 0x3F) + 0x30) >> 4;
236 retval = (wanexp >= 0) ? ((wanmant << wanexp) & 0x7FFF) : (wanmant >> -wanexp);
237
238 return (((an ^ srn) < 0) ? -retval : retval);
239}
240/*- End of function --------------------------------------------------------*/
241
242/*
243 * Compute the estimated signal from the 6-zero predictor.
244 */
245static __inline__ int16_t predictor_zero(g726_state_t *s)
246{
247 int i;
248 int sezi;
249
250 sezi = fmult(s->b[0] >> 2, s->dq[0]);
251 /* ACCUM */
252 for (i = 1; i < 6; i++)
253 sezi += fmult(s->b[i] >> 2, s->dq[i]);
254 return (int16_t) sezi;
255}
256/*- End of function --------------------------------------------------------*/
257
258/*
259 * Computes the estimated signal from the 2-pole predictor.
260 */
261static __inline__ int16_t predictor_pole(g726_state_t *s)
262{
263 return (fmult(s->a[1] >> 2, s->sr[1]) + fmult(s->a[0] >> 2, s->sr[0]));
264}
265/*- End of function --------------------------------------------------------*/
266
267/*
268 * Computes the quantization step size of the adaptive quantizer.
269 */
270static int step_size(g726_state_t *s)
271{
272 int y;
273 int dif;
274 int al;
275
276 if (s->ap >= 256)
277 return s->yu;
278 y = s->yl >> 6;
279 dif = s->yu - y;
280 al = s->ap >> 2;
281 if (dif > 0)
282 y += (dif*al) >> 6;
283 else if (dif < 0)
284 y += (dif*al + 0x3F) >> 6;
285 return y;
286}
287/*- End of function --------------------------------------------------------*/
288
289/*
290 * Given a raw sample, 'd', of the difference signal and a
291 * quantization step size scale factor, 'y', this routine returns the
292 * ADPCM codeword to which that sample gets quantized. The step
293 * size scale factor division operation is done in the log base 2 domain
294 * as a subtraction.
295 */
296static int16_t quantize(int d, /* Raw difference signal sample */
297 int y, /* Step size multiplier */
298 const int table[], /* quantization table */
299 int quantizer_states) /* table size of int16_t integers */
300{
301 int16_t dqm; /* Magnitude of 'd' */
302 int16_t exp; /* Integer part of base 2 log of 'd' */
303 int16_t mant; /* Fractional part of base 2 log */
304 int16_t dl; /* Log of magnitude of 'd' */
305 int16_t dln; /* Step size scale factor normalized log */
306 int i;
307 int size;
308
309 /*
310 * LOG
311 *
312 * Compute base 2 log of 'd', and store in 'dl'.
313 */
314 dqm = (int16_t) abs(d);
315 exp = (int16_t) (top_bit(dqm >> 1) + 1);
316 /* Fractional portion. */
317 mant = ((dqm << 7) >> exp) & 0x7F;
318 dl = (exp << 7) + mant;
319
320 /*
321 * SUBTB
322 *
323 * "Divide" by step size multiplier.
324 */
325 dln = dl - (int16_t) (y >> 2);
326
327 /*
328 * QUAN
329 *
330 * Search for codword i for 'dln'.
331 */
332 size = (quantizer_states - 1) >> 1;
333 for (i = 0; i < size; i++)
334 {
335 if (dln < table[i])
336 break;
337 }
338 if (d < 0)
339 {
340 /* Take 1's complement of i */
341 return (int16_t) ((size << 1) + 1 - i);
342 }
343 if (i == 0 && (quantizer_states & 1))
344 {
345 /* Zero is only valid if there are an even number of states, so
346 take the 1's complement if the code is zero. */
347 return (int16_t) quantizer_states;
348 }
349 return (int16_t) i;
350}
351/*- End of function --------------------------------------------------------*/
352
353/*
354 * Returns reconstructed difference signal 'dq' obtained from
355 * codeword 'i' and quantization step size scale factor 'y'.
356 * Multiplication is performed in log base 2 domain as addition.
357 */
358static int16_t reconstruct(int sign, /* 0 for non-negative value */
359 int dqln, /* G.72x codeword */
360 int y) /* Step size multiplier */
361{
362 int16_t dql; /* Log of 'dq' magnitude */
363 int16_t dex; /* Integer part of log */
364 int16_t dqt;
365 int16_t dq; /* Reconstructed difference signal sample */
366
367 dql = (int16_t) (dqln + (y >> 2)); /* ADDA */
368
369 if (dql < 0)
370 return ((sign) ? -0x8000 : 0);
371 /* ANTILOG */
372 dex = (dql >> 7) & 15;
373 dqt = 128 + (dql & 127);
374 dq = (dqt << 7) >> (14 - dex);
375 return ((sign) ? (dq - 0x8000) : dq);
376}
377/*- End of function --------------------------------------------------------*/
378
379/*
380 * updates the state variables for each output code
381 */
382static void update(g726_state_t *s,
383 int y, /* quantizer step size */
384 int wi, /* scale factor multiplier */
385 int fi, /* for long/short term energies */
386 int dq, /* quantized prediction difference */
387 int sr, /* reconstructed signal */
388 int dqsez) /* difference from 2-pole predictor */
389{
390 int16_t mag;
391 int16_t exp;
392 int16_t a2p; /* LIMC */
393 int16_t a1ul; /* UPA1 */
394 int16_t pks1; /* UPA2 */
395 int16_t fa1;
396 int16_t ylint;
397 int16_t dqthr;
398 int16_t ylfrac;
399 int16_t thr;
400 int16_t pk0;
401 int i;
402 bool_Bool tr;
403
404 a2p = 0;
405 /* Needed in updating predictor poles */
406 pk0 = (dqsez < 0) ? 1 : 0;
407
408 /* prediction difference magnitude */
409 mag = (int16_t) (dq & 0x7FFF);
410 /* TRANS */
411 ylint = (int16_t) (s->yl >> 15); /* exponent part of yl */
412 ylfrac = (int16_t) ((s->yl >> 10) & 0x1F); /* fractional part of yl */
413 /* Limit threshold to 31 << 10 */
414 thr = (ylint > 9) ? (31 << 10) : ((32 + ylfrac) << ylint);
415 dqthr = (thr + (thr >> 1)) >> 1; /* dqthr = 0.75 * thr */
416 if (!s->td) /* signal supposed voice */
417 tr = false0;
418 else if (mag <= dqthr) /* supposed data, but small mag */
419 tr = false0; /* treated as voice */
420 else /* signal is data (modem) */
421 tr = true1;
422
423 /*
424 * Quantizer scale factor adaptation.
425 */
426
427 /* FUNCTW & FILTD & DELAY */
428 /* update non-steady state step size multiplier */
429 s->yu = (int16_t) (y + ((wi - y) >> 5));
430
431 /* LIMB */
432 if (s->yu < 544)
433 s->yu = 544;
434 else if (s->yu > 5120)
435 s->yu = 5120;
436
437 /* FILTE & DELAY */
438 /* update steady state step size multiplier */
439 s->yl += s->yu + ((-s->yl) >> 6);
440
441 /*
442 * Adaptive predictor coefficients.
443 */
444 if (tr)
445 {
446 /* Reset the a's and b's for a modem signal */
447 s->a[0] = 0;
448 s->a[1] = 0;
449 s->b[0] = 0;
450 s->b[1] = 0;
451 s->b[2] = 0;
452 s->b[3] = 0;
453 s->b[4] = 0;
454 s->b[5] = 0;
455 }
456 else
457 {
458 /* Update the a's and b's */
459 /* UPA2 */
460 pks1 = pk0 ^ s->pk[0];
461
462 /* Update predictor pole a[1] */
463 a2p = s->a[1] - (s->a[1] >> 7);
464 if (dqsez != 0)
465 {
466 fa1 = (pks1) ? s->a[0] : -s->a[0];
467 /* a2p = function of fa1 */
468 if (fa1 < -8191)
469 a2p -= 0x100;
470 else if (fa1 > 8191)
471 a2p += 0xFF;
472 else
473 a2p += fa1 >> 5;
474
475 if (pk0 ^ s->pk[1])
476 {
477 /* LIMC */
478 if (a2p <= -12160)
479 a2p = -12288;
480 else if (a2p >= 12416)
481 a2p = 12288;
482 else
483 a2p -= 0x80;
484 }
485 else if (a2p <= -12416)
486 a2p = -12288;
487 else if (a2p >= 12160)
488 a2p = 12288;
489 else
490 a2p += 0x80;
491 }
492
493 /* TRIGB & DELAY */
494 s->a[1] = a2p;
495
496 /* UPA1 */
497 /* Update predictor pole a[0] */
498 s->a[0] -= s->a[0] >> 8;
499 if (dqsez != 0)
500 {
501 if (pks1 == 0)
502 s->a[0] += 192;
503 else
504 s->a[0] -= 192;
505 }
506 /* LIMD */
507 a1ul = 15360 - a2p;
508 if (s->a[0] < -a1ul)
509 s->a[0] = -a1ul;
510 else if (s->a[0] > a1ul)
511 s->a[0] = a1ul;
512
513 /* UPB : update predictor zeros b[6] */
514 for (i = 0; i < 6; i++)
515 {
516 /* Distinguish 40Kbps mode from the others */
517 s->b[i] -= s->b[i] >> ((s->bits_per_sample == 5) ? 9 : 8);
518 if (dq & 0x7FFF)
519 {
520 /* XOR */
521 if ((dq ^ s->dq[i]) >= 0)
522 s->b[i] += 128;
523 else
524 s->b[i] -= 128;
525 }
526 }
527 }
528
529 for (i = 5; i > 0; i--)
530 s->dq[i] = s->dq[i - 1];
531 /* FLOAT A : convert dq[0] to 4-bit exp, 6-bit mantissa f.p. */
532 if (mag == 0)
533 {
534 s->dq[0] = (dq >= 0) ? 0x20 : 0xFC20;
535 }
536 else
537 {
538 exp = (int16_t) (top_bit(mag) + 1);
539 s->dq[0] = (dq >= 0)
540 ? ((exp << 6) + ((mag << 6) >> exp))
541 : ((exp << 6) + ((mag << 6) >> exp) - 0x400);
542 }
543
544 s->sr[1] = s->sr[0];
545 /* FLOAT B : convert sr to 4-bit exp., 6-bit mantissa f.p. */
546 if (sr == 0)
547 {
548 s->sr[0] = 0x20;
549 }
550 else if (sr > 0)
551 {
552 exp = (int16_t) (top_bit(sr) + 1);
553 s->sr[0] = (int16_t) ((exp << 6) + ((sr << 6) >> exp));
554 }
555 else if (sr > -32768)
556 {
557 mag = (int16_t) -sr;
558 exp = (int16_t) (top_bit(mag) + 1);
559 s->sr[0] = (exp << 6) + ((mag << 6) >> exp) - 0x400;
560 }
561 else
562 {
563 s->sr[0] = (uint16_t) 0xFC20;
564 }
565
566 /* DELAY A */
567 s->pk[1] = s->pk[0];
568 s->pk[0] = pk0;
569
570 /* TONE */
571 if (tr) /* this sample has been treated as data */
572 s->td = false0; /* next one will be treated as voice */
573 else if (a2p < -11776) /* small sample-to-sample correlation */
574 s->td = true1; /* signal may be data */
575 else /* signal is voice */
576 s->td = false0;
577
578 /* Adaptation speed control. */
579 /* FILTA */
580 s->dms += ((int16_t) fi - s->dms) >> 5;
581 /* FILTB */
582 s->dml += (((int16_t) (fi << 2) - s->dml) >> 7);
583
584 if (tr)
585 s->ap = 256;
586 else if (y < 1536) /* SUBTC */
587 s->ap += (0x200 - s->ap) >> 4;
588 else if (s->td)
589 s->ap += (0x200 - s->ap) >> 4;
590 else if (abs((s->dms << 2) - s->dml) >= (s->dml >> 3))
591 s->ap += (0x200 - s->ap) >> 4;
592 else
593 s->ap += (-s->ap) >> 4;
594}
595/*- End of function --------------------------------------------------------*/
596
597static int16_t tandem_adjust_alaw(int16_t sr, /* decoder output linear PCM sample */
598 int se, /* predictor estimate sample */
599 int y, /* quantizer step size */
600 int i, /* decoder input code */
601 int sign,
602 const int qtab[],
603 int quantizer_states)
604{
605 uint8_t sp; /* A-law compressed 8-bit code */
606 int16_t dx; /* prediction error */
607 int id; /* quantized prediction error */
608 int sd; /* adjusted A-law decoded sample value */
609
610 if (sr <= -32768)
4
Assuming the condition is true
5
Taking true branch
611 sr = -1;
612 sp = linear_to_alaw((sr >> 1) << 3);
6
The result of the left shift is undefined because the left operand is negative
613 /* 16-bit prediction error */
614 dx = (int16_t) ((alaw_to_linear(sp) >> 2) - se);
615 id = quantize(dx, y, qtab, quantizer_states);
616 if (id == i)
617 {
618 /* No adjustment of sp required */
619 return (int16_t) sp;
620 }
621 /* sp adjustment needed */
622 /* ADPCM codes : 8, 9, ... F, 0, 1, ... , 6, 7 */
623 /* 2's complement to biased unsigned */
624 if ((id ^ sign) > (i ^ sign))
625 {
626 /* sp adjusted to next lower value */
627 if (sp & 0x80)
628 sd = (sp == 0xD5) ? 0x55 : (((sp ^ 0x55) - 1) ^ 0x55);
629 else
630 sd = (sp == 0x2A) ? 0x2A : (((sp ^ 0x55) + 1) ^ 0x55);
631 }
632 else
633 {
634 /* sp adjusted to next higher value */
635 if (sp & 0x80)
636 sd = (sp == 0xAA) ? 0xAA : (((sp ^ 0x55) + 1) ^ 0x55);
637 else
638 sd = (sp == 0x55) ? 0xD5 : (((sp ^ 0x55) - 1) ^ 0x55);
639 }
640 return (int16_t) sd;
641}
642/*- End of function --------------------------------------------------------*/
643
644static int16_t tandem_adjust_ulaw(int16_t sr, /* decoder output linear PCM sample */
645 int se, /* predictor estimate sample */
646 int y, /* quantizer step size */
647 int i, /* decoder input code */
648 int sign,
649 const int qtab[],
650 int quantizer_states)
651{
652 uint8_t sp; /* u-law compressed 8-bit code */
653 int16_t dx; /* prediction error */
654 int id; /* quantized prediction error */
655 int sd; /* adjusted u-law decoded sample value */
656
657 if (sr <= -32768)
658 sr = 0;
659 sp = linear_to_ulaw(sr << 2);
660 /* 16-bit prediction error */
661 dx = (int16_t) ((ulaw_to_linear(sp) >> 2) - se);
662 id = quantize(dx, y, qtab, quantizer_states);
663 if (id == i)
664 {
665 /* No adjustment of sp required. */
666 return (int16_t) sp;
667 }
668 /* ADPCM codes : 8, 9, ... F, 0, 1, ... , 6, 7 */
669 /* 2's complement to biased unsigned */
670 if ((id ^ sign) > (i ^ sign))
671 {
672 /* sp adjusted to next lower value */
673 if (sp & 0x80)
674 sd = (sp == 0xFF) ? 0x7E : (sp + 1);
675 else
676 sd = (sp == 0x00) ? 0x00 : (sp - 1);
677 }
678 else
679 {
680 /* sp adjusted to next higher value */
681 if (sp & 0x80)
682 sd = (sp == 0x80) ? 0x80 : (sp - 1);
683 else
684 sd = (sp == 0x7F) ? 0xFE : (sp + 1);
685 }
686 return (int16_t) sd;
687}
688/*- End of function --------------------------------------------------------*/
689
690/*
691 * Encodes a linear PCM, A-law or u-law input sample and returns its 3-bit code.
692 */
693static uint8_t g726_16_encoder(g726_state_t *s, int16_t amp)
694{
695 int y;
696 int16_t sei;
697 int16_t sezi;
698 int16_t se;
699 int16_t d;
700 int16_t sr;
701 int16_t dqsez;
702 int16_t dq;
703 int16_t i;
704
705 sezi = predictor_zero(s);
706 sei = sezi + predictor_pole(s);
707 se = sei >> 1;
708 d = amp - se;
709
710 /* Quantize prediction difference */
711 y = step_size(s);
712 i = quantize(d, y, qtab_726_16, 4);
713 dq = reconstruct(i & 2, g726_16_dqlntab[i], y);
714
715 /* Reconstruct the signal */
716 sr = (dq < 0) ? (se - (dq & 0x3FFF)) : (se + dq);
717
718 /* Pole prediction difference */
719 dqsez = sr + (sezi >> 1) - se;
720
721 update(s, y, g726_16_witab[i], g726_16_fitab[i], dq, sr, dqsez);
722 return (uint8_t) i;
723}
724/*- End of function --------------------------------------------------------*/
725
726/*
727 * Decodes a 2-bit CCITT G.726_16 ADPCM code and returns
728 * the resulting 16-bit linear PCM, A-law or u-law sample value.
729 */
730static int16_t g726_16_decoder(g726_state_t *s, uint8_t code)
731{
732 int16_t sezi;
733 int16_t sei;
734 int16_t se;
735 int16_t sr;
736 int16_t dq;
737 int16_t dqsez;
738 int y;
739
740 /* Mask to get proper bits */
741 code &= 0x03;
742 sezi = predictor_zero(s);
743 sei = sezi + predictor_pole(s);
744
745 y = step_size(s);
746 dq = reconstruct(code & 2, g726_16_dqlntab[code], y);
747
748 /* Reconstruct the signal */
749 se = sei >> 1;
750 sr = (dq < 0) ? (se - (dq & 0x3FFF)) : (se + dq);
751
752 /* Pole prediction difference */
753 dqsez = sr + (sezi >> 1) - se;
754
755 update(s, y, g726_16_witab[code], g726_16_fitab[code], dq, sr, dqsez);
756
757 switch (s->ext_coding)
758 {
759 case G726_ENCODING_ALAW:
760 return tandem_adjust_alaw(sr, se, y, code, 2, qtab_726_16, 4);
761 case G726_ENCODING_ULAW:
762 return tandem_adjust_ulaw(sr, se, y, code, 2, qtab_726_16, 4);
763 }
764 return (sr << 2);
765}
766/*- End of function --------------------------------------------------------*/
767
768/*
769 * Encodes a linear PCM, A-law or u-law input sample and returns its 3-bit code.
770 */
771static uint8_t g726_24_encoder(g726_state_t *s, int16_t amp)
772{
773 int16_t sei;
774 int16_t sezi;
775 int16_t se;
776 int16_t d;
777 int16_t sr;
778 int16_t dqsez;
779 int16_t dq;
780 int16_t i;
781 int y;
782
783 sezi = predictor_zero(s);
784 sei = sezi + predictor_pole(s);
785 se = sei >> 1;
786 d = amp - se;
787
788 /* Quantize prediction difference */
789 y = step_size(s);
790 i = quantize(d, y, qtab_726_24, 7);
791 dq = reconstruct(i & 4, g726_24_dqlntab[i], y);
792
793 /* Reconstruct the signal */
794 sr = (dq < 0) ? (se - (dq & 0x3FFF)) : (se + dq);
795
796 /* Pole prediction difference */
797 dqsez = sr + (sezi >> 1) - se;
798
799 update(s, y, g726_24_witab[i], g726_24_fitab[i], dq, sr, dqsez);
800 return (uint8_t) i;
801}
802/*- End of function --------------------------------------------------------*/
803
804/*
805 * Decodes a 3-bit CCITT G.726_24 ADPCM code and returns
806 * the resulting 16-bit linear PCM, A-law or u-law sample value.
807 */
808static int16_t g726_24_decoder(g726_state_t *s, uint8_t code)
809{
810 int16_t sezi;
811 int16_t sei;
812 int16_t se;
813 int16_t sr;
814 int16_t dq;
815 int16_t dqsez;
816 int y;
817
818 /* Mask to get proper bits */
819 code &= 0x07;
820 sezi = predictor_zero(s);
821 sei = sezi + predictor_pole(s);
822
823 y = step_size(s);
824 dq = reconstruct(code & 4, g726_24_dqlntab[code], y);
825
826 /* Reconstruct the signal */
827 se = sei >> 1;
828 sr = (dq < 0) ? (se - (dq & 0x3FFF)) : (se + dq);
829
830 /* Pole prediction difference */
831 dqsez = sr + (sezi >> 1) - se;
832
833 update(s, y, g726_24_witab[code], g726_24_fitab[code], dq, sr, dqsez);
834
835 switch (s->ext_coding)
836 {
837 case G726_ENCODING_ALAW:
838 return tandem_adjust_alaw(sr, se, y, code, 4, qtab_726_24, 7);
839 case G726_ENCODING_ULAW:
840 return tandem_adjust_ulaw(sr, se, y, code, 4, qtab_726_24, 7);
841 }
842 return (sr << 2);
843}
844/*- End of function --------------------------------------------------------*/
845
846/*
847 * Encodes a linear input sample and returns its 4-bit code.
848 */
849static uint8_t g726_32_encoder(g726_state_t *s, int16_t amp)
850{
851 int16_t sei;
852 int16_t sezi;
853 int16_t se;
854 int16_t d;
855 int16_t sr;
856 int16_t dqsez;
857 int16_t dq;
858 int16_t i;
859 int y;
860
861 sezi = predictor_zero(s);
862 sei = sezi + predictor_pole(s);
863 se = sei >> 1;
864 d = amp - se;
865
866 /* Quantize the prediction difference */
867 y = step_size(s);
868 i = quantize(d, y, qtab_726_32, 15);
869 dq = reconstruct(i & 8, g726_32_dqlntab[i], y);
870
871 /* Reconstruct the signal */
872 sr = (dq < 0) ? (se - (dq & 0x3FFF)) : (se + dq);
873
874 /* Pole prediction difference */
875 dqsez = sr + (sezi >> 1) - se;
876
877 update(s, y, g726_32_witab[i], g726_32_fitab[i], dq, sr, dqsez);
878 return (uint8_t) i;
879}
880/*- End of function --------------------------------------------------------*/
881
882/*
883 * Decodes a 4-bit CCITT G.726_32 ADPCM code and returns
884 * the resulting 16-bit linear PCM, A-law or u-law sample value.
885 */
886static int16_t g726_32_decoder(g726_state_t *s, uint8_t code)
887{
888 int16_t sezi;
889 int16_t sei;
890 int16_t se;
891 int16_t sr;
892 int16_t dq;
893 int16_t dqsez;
894 int y;
895
896 /* Mask to get proper bits */
897 code &= 0x0F;
898 sezi = predictor_zero(s);
899 sei = sezi + predictor_pole(s);
900
901 y = step_size(s);
902 dq = reconstruct(code & 8, g726_32_dqlntab[code], y);
903
904 /* Reconstruct the signal */
905 se = sei >> 1;
906 sr = (dq < 0) ? (se - (dq & 0x3FFF)) : (se + dq);
907
908 /* Pole prediction difference */
909 dqsez = sr + (sezi >> 1) - se;
910
911 update(s, y, g726_32_witab[code], g726_32_fitab[code], dq, sr, dqsez);
912
913 switch (s->ext_coding)
914 {
915 case G726_ENCODING_ALAW:
916 return tandem_adjust_alaw(sr, se, y, code, 8, qtab_726_32, 15);
917 case G726_ENCODING_ULAW:
918 return tandem_adjust_ulaw(sr, se, y, code, 8, qtab_726_32, 15);
919 }
920 return (sr << 2);
921}
922/*- End of function --------------------------------------------------------*/
923
924/*
925 * Encodes a 16-bit linear PCM, A-law or u-law input sample and retuens
926 * the resulting 5-bit CCITT G.726 40Kbps code.
927 */
928static uint8_t g726_40_encoder(g726_state_t *s, int16_t amp)
929{
930 int16_t sei;
931 int16_t sezi;
932 int16_t se;
933 int16_t d;
934 int16_t sr;
935 int16_t dqsez;
936 int16_t dq;
937 int16_t i;
938 int y;
939
940 sezi = predictor_zero(s);
941 sei = sezi + predictor_pole(s);
942 se = sei >> 1;
943 d = amp - se;
944
945 /* Quantize prediction difference */
946 y = step_size(s);
947 i = quantize(d, y, qtab_726_40, 31);
948 dq = reconstruct(i & 0x10, g726_40_dqlntab[i], y);
949
950 /* Reconstruct the signal */
951 sr = (dq < 0) ? (se - (dq & 0x7FFF)) : (se + dq);
952
953 /* Pole prediction difference */
954 dqsez = sr + (sezi >> 1) - se;
955
956 update(s, y, g726_40_witab[i], g726_40_fitab[i], dq, sr, dqsez);
957 return (uint8_t) i;
958}
959/*- End of function --------------------------------------------------------*/
960
961/*
962 * Decodes a 5-bit CCITT G.726 40Kbps code and returns
963 * the resulting 16-bit linear PCM, A-law or u-law sample value.
964 */
965static int16_t g726_40_decoder(g726_state_t *s, uint8_t code)
966{
967 int16_t sezi;
968 int16_t sei;
969 int16_t se;
970 int16_t sr;
971 int16_t dq;
972 int16_t dqsez;
973 int y;
974
975 /* Mask to get proper bits */
976 code &= 0x1F;
977 sezi = predictor_zero(s);
978 sei = sezi + predictor_pole(s);
979
980 y = step_size(s);
981 dq = reconstruct(code & 0x10, g726_40_dqlntab[code], y);
982
983 /* Reconstruct the signal */
984 se = sei >> 1;
985 sr = (dq < 0) ? (se - (dq & 0x7FFF)) : (se + dq);
1
'?' condition is false
986
987 /* Pole prediction difference */
988 dqsez = sr + (sezi >> 1) - se;
989
990 update(s, y, g726_40_witab[code], g726_40_fitab[code], dq, sr, dqsez);
991
992 switch (s->ext_coding)
2
Control jumps to 'case G726_ENCODING_ALAW:' at line 994
993 {
994 case G726_ENCODING_ALAW:
995 return tandem_adjust_alaw(sr, se, y, code, 0x10, qtab_726_40, 31);
3
Calling 'tandem_adjust_alaw'
996 case G726_ENCODING_ULAW:
997 return tandem_adjust_ulaw(sr, se, y, code, 0x10, qtab_726_40, 31);
998 }
999 return (sr << 2);
1000}
1001/*- End of function --------------------------------------------------------*/
1002
1003SPAN_DECLARE(g726_state_t *)__attribute__((visibility("default"))) g726_state_t * g726_init(g726_state_t *s, int bit_rate, int ext_coding, int packing)
1004{
1005 int i;
1006
1007 if (bit_rate != 16000 && bit_rate != 24000 && bit_rate != 32000 && bit_rate != 40000)
1008 return NULL((void*)0);
1009 if (s == NULL((void*)0))
1010 {
1011 if ((s = (g726_state_t *) span_alloc(sizeof(*s))) == NULL((void*)0))
1012 return NULL((void*)0);
1013 }
1014 s->yl = 34816;
1015 s->yu = 544;
1016 s->dms = 0;
1017 s->dml = 0;
1018 s->ap = 0;
1019 s->rate = bit_rate;
1020 s->ext_coding = ext_coding;
1021 s->packing = packing;
1022 for (i = 0; i < 2; i++)
1023 {
1024 s->a[i] = 0;
1025 s->pk[i] = 0;
1026 s->sr[i] = 32;
1027 }
1028 for (i = 0; i < 6; i++)
1029 {
1030 s->b[i] = 0;
1031 s->dq[i] = 32;
1032 }
1033 s->td = false0;
1034 switch (bit_rate)
1035 {
1036 case 16000:
1037 s->enc_func = g726_16_encoder;
1038 s->dec_func = g726_16_decoder;
1039 s->bits_per_sample = 2;
1040 break;
1041 case 24000:
1042 s->enc_func = g726_24_encoder;
1043 s->dec_func = g726_24_decoder;
1044 s->bits_per_sample = 3;
1045 break;
1046 case 32000:
1047 default:
1048 s->enc_func = g726_32_encoder;
1049 s->dec_func = g726_32_decoder;
1050 s->bits_per_sample = 4;
1051 break;
1052 case 40000:
1053 s->enc_func = g726_40_encoder;
1054 s->dec_func = g726_40_decoder;
1055 s->bits_per_sample = 5;
1056 break;
1057 }
1058 bitstream_init(&s->bs, (s->packing != G726_PACKING_LEFT));
1059 return s;
1060}
1061/*- End of function --------------------------------------------------------*/
1062
1063SPAN_DECLARE(int)__attribute__((visibility("default"))) int g726_release(g726_state_t *s)
1064{
1065 return 0;
1066}
1067/*- End of function --------------------------------------------------------*/
1068
1069SPAN_DECLARE(int)__attribute__((visibility("default"))) int g726_free(g726_state_t *s)
1070{
1071 span_free(s);
1072 return 0;
1073}
1074/*- End of function --------------------------------------------------------*/
1075
1076SPAN_DECLARE(int)__attribute__((visibility("default"))) int g726_decode(g726_state_t *s,
1077 int16_t amp[],
1078 const uint8_t g726_data[],
1079 int g726_bytes)
1080{
1081 int i;
1082 int samples;
1083 uint8_t code;
1084 int sl;
1085
1086 for (samples = i = 0; ; )
1087 {
1088 if (s->packing != G726_PACKING_NONE)
1089 {
1090 /* Unpack the code bits */
1091 if (s->packing != G726_PACKING_LEFT)
1092 {
1093 if (s->bs.residue < s->bits_per_sample)
1094 {
1095 if (i >= g726_bytes)
1096 break;
1097 s->bs.bitstream |= (g726_data[i++] << s->bs.residue);
1098 s->bs.residue += 8;
1099 }
1100 code = (uint8_t) (s->bs.bitstream & ((1 << s->bits_per_sample) - 1));
1101 s->bs.bitstream >>= s->bits_per_sample;
1102 }
1103 else
1104 {
1105 if (s->bs.residue < s->bits_per_sample)
1106 {
1107 if (i >= g726_bytes)
1108 break;
1109 s->bs.bitstream = (s->bs.bitstream << 8) | g726_data[i++];
1110 s->bs.residue += 8;
1111 }
1112 code = (uint8_t) ((s->bs.bitstream >> (s->bs.residue - s->bits_per_sample)) & ((1 << s->bits_per_sample) - 1));
1113 }
1114 s->bs.residue -= s->bits_per_sample;
1115 }
1116 else
1117 {
1118 if (i >= g726_bytes)
1119 break;
1120 code = g726_data[i++];
1121 }
1122 sl = s->dec_func(s, code);
1123 if (s->ext_coding != G726_ENCODING_LINEAR)
1124 ((uint8_t *) amp)[samples++] = (uint8_t) sl;
1125 else
1126 amp[samples++] = (int16_t) sl;
1127 }
1128 return samples;
1129}
1130/*- End of function --------------------------------------------------------*/
1131
1132SPAN_DECLARE(int)__attribute__((visibility("default"))) int g726_encode(g726_state_t *s,
1133 uint8_t g726_data[],
1134 const int16_t amp[],
1135 int len)
1136{
1137 int i;
1138 int g726_bytes;
1139 int16_t sl;
1140 uint8_t code;
1141
1142 for (g726_bytes = i = 0; i < len; i++)
1143 {
1144 /* Linearize the input sample to 14-bit PCM */
1145 switch (s->ext_coding)
1146 {
1147 case G726_ENCODING_ALAW:
1148 sl = alaw_to_linear(((const uint8_t *) amp)[i]) >> 2;
1149 break;
1150 case G726_ENCODING_ULAW:
1151 sl = ulaw_to_linear(((const uint8_t *) amp)[i]) >> 2;
1152 break;
1153 default:
1154 sl = amp[i] >> 2;
1155 break;
1156 }
1157 code = s->enc_func(s, sl);
1158 if (s->packing != G726_PACKING_NONE)
1159 {
1160 /* Pack the code bits */
1161 if (s->packing != G726_PACKING_LEFT)
1162 {
1163 s->bs.bitstream |= (code << s->bs.residue);
1164 s->bs.residue += s->bits_per_sample;
1165 if (s->bs.residue >= 8)
1166 {
1167 g726_data[g726_bytes++] = (uint8_t) (s->bs.bitstream & 0xFF);
1168 s->bs.bitstream >>= 8;
1169 s->bs.residue -= 8;
1170 }
1171 }
1172 else
1173 {
1174 s->bs.bitstream = (s->bs.bitstream << s->bits_per_sample) | code;
1175 s->bs.residue += s->bits_per_sample;
1176 if (s->bs.residue >= 8)
1177 {
1178 g726_data[g726_bytes++] = (uint8_t) ((s->bs.bitstream >> (s->bs.residue - 8)) & 0xFF);
1179 s->bs.residue -= 8;
1180 }
1181 }
1182 }
1183 else
1184 {
1185 g726_data[g726_bytes++] = (uint8_t) code;
1186 }
1187 }
1188 return g726_bytes;
1189}
1190/*- End of function --------------------------------------------------------*/
1191/*- End of file ------------------------------------------------------------*/