1 /* Common routines for G.721 and G.723 conversions.
5 * This library is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation; either version 2.1 of the License, or (at
8 * your option) any later version.
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
13 * General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 * This code is based on code from Sun, which came with the following
22 * -----------------------------------------------------------------------
23 * This source code is a product of Sun Microsystems, Inc. and is provided
24 * for unrestricted use. Users may copy or modify this source code without
27 * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
28 * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
31 * Sun source code is provided with no support and without any obligation on
32 * the part of Sun Microsystems, Inc. to assist in its use, correction,
33 * modification or enhancement.
35 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
36 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
37 * OR ANY PART THEREOF.
39 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
40 * or profits or other special, indirect and consequential damages, even if
41 * Sun has been advised of the possibility of such damages.
43 * Sun Microsystems, Inc.
45 * Mountain View, California 94043
46 * -----------------------------------------------------------------------
53 static const char LogTable256
[] =
55 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
56 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
57 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
58 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
59 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
60 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
61 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
62 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
63 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
64 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
65 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
66 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
67 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
68 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
69 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
70 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
73 static inline int log2plus1(int val
)
75 /* From http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogLookup */
76 unsigned int v
= (unsigned int)val
; /* 32-bit word to find the log of */
77 unsigned r
; /* r will be lg(v) */
78 register unsigned int t
, tt
; /* temporaries */
82 r
= (t
= tt
>> 8) ? 24 + LogTable256
[t
] : 16 + LogTable256
[tt
];
86 r
= (t
= v
>> 8) ? 8 + LogTable256
[t
] : LogTable256
[v
];
95 * quantizes the input val against the table of size short integers.
96 * It returns i if table[i - 1] <= val < table[i].
98 * Using linear search for simple coding.
100 static int quan(int val
, short const *table
, int size
)
104 for (i
= 0; i
< size
; i
++)
113 * returns the integer product of the 14-bit integer "an" and
114 * "floating point" representation (4-bit exponent, 6-bit mantessa) "srn".
116 static int fmult(int an
, int srn
)
118 short anmag
, anexp
, anmant
;
119 short wanexp
, wanmant
;
122 anmag
= (an
> 0) ? an
: ((-an
) & 0x1FFF);
123 anexp
= log2plus1(anmag
) - 6;
124 anmant
= (anmag
== 0) ? 32 :
125 (anexp
>= 0) ? anmag
>> anexp
: anmag
<< -anexp
;
126 wanexp
= anexp
+ ((srn
>> 6) & 0xF) - 13;
128 wanmant
= (anmant
* (srn
& 077) + 0x30) >> 4;
129 retval
= (wanexp
>= 0) ? ((wanmant
<< wanexp
) & 0x7FFF) :
130 (wanmant
>> -wanexp
);
132 return (((an
^ srn
) < 0) ? -retval
: retval
);
138 * This routine initializes and/or resets the g72x_state structure
139 * pointed to by 'state_ptr'.
140 * All the initial state values are specified in the CCITT G.721 document.
142 void g72x_init_state(struct g72x_state
*state_ptr
)
146 state_ptr
->yl
= 34816;
151 for (cnta
= 0; cnta
< 2; cnta
++) {
152 state_ptr
->a
[cnta
] = 0;
153 state_ptr
->pk
[cnta
] = 0;
154 state_ptr
->sr
[cnta
] = 32;
156 for (cnta
= 0; cnta
< 6; cnta
++) {
157 state_ptr
->b
[cnta
] = 0;
158 state_ptr
->dq
[cnta
] = 32;
166 * computes the estimated signal from 6-zero predictor.
169 int predictor_zero(struct g72x_state
*state_ptr
)
174 sezi
= fmult(state_ptr
->b
[0] >> 2, state_ptr
->dq
[0]);
175 for (i
= 1; i
< 6; i
++) /* ACCUM */
176 sezi
+= fmult(state_ptr
->b
[i
] >> 2, state_ptr
->dq
[i
]);
182 * computes the estimated signal from 2-pole predictor.
185 int predictor_pole(struct g72x_state
*state_ptr
)
187 return (fmult(state_ptr
->a
[1] >> 2, state_ptr
->sr
[1]) +
188 fmult(state_ptr
->a
[0] >> 2, state_ptr
->sr
[0]));
193 * computes the quantization step size of the adaptive quantizer.
196 int step_size(struct g72x_state
*state_ptr
)
202 if (state_ptr
->ap
>= 256)
203 return (state_ptr
->yu
);
205 y
= state_ptr
->yl
>> 6;
206 dif
= state_ptr
->yu
- y
;
207 al
= state_ptr
->ap
>> 2;
209 y
+= (dif
* al
) >> 6;
211 y
+= (dif
* al
+ 0x3F) >> 6;
219 * Given a raw sample, 'd', of the difference signal and a
220 * quantization step size scale factor, 'y', this routine returns the
221 * ADPCM codeword to which that sample gets quantized. The step
222 * size scale factor division operation is done in the log base 2 domain
225 int quantize(int d
, int y
, short const *table
, int size
)
227 short dqm
; /* Magnitude of 'd' */
228 short exp
; /* Integer part of base 2 log of 'd' */
229 short mant
; /* Fractional part of base 2 log */
230 short dl
; /* Log of magnitude of 'd' */
231 short dln
; /* Step size scale factor normalized log */
237 * Compute base 2 log of 'd', and store in 'dl'.
240 exp
= log2plus1(dqm
>> 1);
241 mant
= ((dqm
<< 7) >> exp
) & 0x7F; /* Fractional portion. */
242 dl
= (exp
<< 7) + mant
;
247 * "Divide" by step size multiplier.
254 * Obtain codword i for 'd'.
256 i
= quan(dln
, table
, size
);
257 if (d
< 0) /* take 1's complement of i */
258 return ((size
<< 1) + 1 - i
);
259 else if (i
== 0) /* take 1's complement of 0 */
260 return ((size
<< 1) + 1); /* new in 1988 */
267 * Returns reconstructed difference signal 'dq' obtained from
268 * codeword 'i' and quantization step size scale factor 'y'.
269 * Multiplication is performed in log base 2 domain as addition.
271 int reconstruct(int sign
, int dqln
, int y
)
273 short dql
; /* Log of 'dq' magnitude */
274 short dex
; /* Integer part of log */
276 short dq
; /* Reconstructed difference signal sample */
278 dql
= dqln
+ (y
>> 2); /* ADDA */
281 return ((sign
) ? -0x8000 : 0);
282 } else { /* ANTILOG */
283 dex
= (dql
>> 7) & 15;
284 dqt
= 128 + (dql
& 127);
285 dq
= (dqt
<< 7) >> (14 - dex
);
286 return ((sign
) ? (dq
- 0x8000) : dq
);
294 * updates the state variables for each output code
296 void update(int code_size
, int y
, int wi
, int fi
, int dq
, int sr
,
297 int dqsez
, struct g72x_state
*state_ptr
)
300 short mag
, exp
; /* Adaptive predictor, FLOAT A */
301 short a2p
=0; /* LIMC */
302 short a1ul
; /* UPA1 */
303 short pks1
; /* UPA2 */
305 char tr
; /* tone/transition detector */
306 short ylint
, thr2
, dqthr
;
310 pk0
= (dqsez
< 0) ? 1 : 0; /* needed in updating predictor poles */
312 mag
= dq
& 0x7FFF; /* prediction difference magnitude */
314 ylint
= state_ptr
->yl
>> 15; /* exponent part of yl */
315 ylfrac
= (state_ptr
->yl
>> 10) & 0x1F; /* fractional part of yl */
316 thr1
= (32 + ylfrac
) << ylint
; /* threshold */
317 thr2
= (ylint
> 9) ? 31 << 10 : thr1
; /* limit thr2 to 31 << 10 */
318 dqthr
= (thr2
+ (thr2
>> 1)) >> 1; /* dqthr = 0.75 * thr2 */
319 if (state_ptr
->td
== 0) /* signal supposed voice */
321 else if (mag
<= dqthr
) /* supposed data, but small mag */
322 tr
= 0; /* treated as voice */
323 else /* signal is data (modem) */
327 * Quantizer scale factor adaptation.
330 /* FUNCTW & FILTD & DELAY */
331 /* update non-steady state step size multiplier */
332 state_ptr
->yu
= y
+ ((wi
- y
) >> 5);
335 if (state_ptr
->yu
< 544) /* 544 <= yu <= 5120 */
337 else if (state_ptr
->yu
> 5120)
338 state_ptr
->yu
= 5120;
341 /* update steady state step size multiplier */
342 state_ptr
->yl
+= state_ptr
->yu
+ ((-state_ptr
->yl
) >> 6);
345 * Adaptive predictor coefficients.
347 if (tr
== 1) { /* reset a's and b's for modem signal */
356 } else { /* update a's and b's */
357 pks1
= pk0
^ state_ptr
->pk
[0]; /* UPA2 */
359 /* update predictor pole a[1] */
360 a2p
= state_ptr
->a
[1] - (state_ptr
->a
[1] >> 7);
362 fa1
= (pks1
) ? state_ptr
->a
[0] : -state_ptr
->a
[0];
363 if (fa1
< -8191) /* a2p = function of fa1 */
370 if (pk0
^ state_ptr
->pk
[1])
375 else if (a2p
>= 12416)
380 else if (a2p
<= -12416)
382 else if (a2p
>= 12160)
388 /* Possible bug: a2p not initialized if dqsez == 0) */
390 state_ptr
->a
[1] = a2p
;
393 /* update predictor pole a[0] */
394 state_ptr
->a
[0] -= state_ptr
->a
[0] >> 8;
398 state_ptr
->a
[0] += 192;
400 state_ptr
->a
[0] -= 192;
404 if (state_ptr
->a
[0] < -a1ul
)
405 state_ptr
->a
[0] = -a1ul
;
406 else if (state_ptr
->a
[0] > a1ul
)
407 state_ptr
->a
[0] = a1ul
;
409 /* UPB : update predictor zeros b[6] */
410 for (cnt
= 0; cnt
< 6; cnt
++) {
411 if (code_size
== 5) /* for 40Kbps G.723 */
412 state_ptr
->b
[cnt
] -= state_ptr
->b
[cnt
] >> 9;
413 else /* for G.721 and 24Kbps G.723 */
414 state_ptr
->b
[cnt
] -= state_ptr
->b
[cnt
] >> 8;
415 if (dq
& 0x7FFF) { /* XOR */
416 if ((dq
^ state_ptr
->dq
[cnt
]) >= 0)
417 state_ptr
->b
[cnt
] += 128;
419 state_ptr
->b
[cnt
] -= 128;
424 for (cnt
= 5; cnt
> 0; cnt
--)
425 state_ptr
->dq
[cnt
] = state_ptr
->dq
[cnt
-1];
426 /* FLOAT A : convert dq[0] to 4-bit exp, 6-bit mantissa f.p. */
428 state_ptr
->dq
[0] = (dq
>= 0) ? 0x20 : (short)(unsigned short)0xFC20;
430 exp
= log2plus1(mag
);
431 state_ptr
->dq
[0] = (dq
>= 0) ?
432 (exp
<< 6) + ((mag
<< 6) >> exp
) :
433 (exp
<< 6) + ((mag
<< 6) >> exp
) - 0x400;
436 state_ptr
->sr
[1] = state_ptr
->sr
[0];
437 /* FLOAT B : convert sr to 4-bit exp., 6-bit mantissa f.p. */
439 state_ptr
->sr
[0] = 0x20;
442 state_ptr
->sr
[0] = (exp
<< 6) + ((sr
<< 6) >> exp
);
443 } else if (sr
> -32768) {
445 exp
= log2plus1(mag
);
446 state_ptr
->sr
[0] = (exp
<< 6) + ((mag
<< 6) >> exp
) - 0x400;
448 state_ptr
->sr
[0] = (short)(unsigned short)0xFC20;
451 state_ptr
->pk
[1] = state_ptr
->pk
[0];
452 state_ptr
->pk
[0] = pk0
;
455 if (tr
== 1) /* this sample has been treated as data */
456 state_ptr
->td
= 0; /* next one will be treated as voice */
457 else if (a2p
< -11776) /* small sample-to-sample correlation */
458 state_ptr
->td
= 1; /* signal may be data */
459 else /* signal is voice */
463 * Adaptation speed control.
465 state_ptr
->dms
+= (fi
- state_ptr
->dms
) >> 5; /* FILTA */
466 state_ptr
->dml
+= (((fi
<< 2) - state_ptr
->dml
) >> 7); /* FILTB */
470 else if (y
< 1536) /* SUBTC */
471 state_ptr
->ap
+= (0x200 - state_ptr
->ap
) >> 4;
472 else if (state_ptr
->td
== 1)
473 state_ptr
->ap
+= (0x200 - state_ptr
->ap
) >> 4;
474 else if (abs((state_ptr
->dms
<< 2) - state_ptr
->dml
) >=
475 (state_ptr
->dml
>> 3))
476 state_ptr
->ap
+= (0x200 - state_ptr
->ap
) >> 4;
478 state_ptr
->ap
+= (-state_ptr
->ap
) >> 4;
482 * tandem_adjust(sr, se, y, i, sign)
484 * At the end of ADPCM decoding, it simulates an encoder which may be receiving
485 * the output of this decoder as a tandem process. If the output of the
486 * simulated encoder differs from the input to this decoder, the decoder output
487 * is adjusted by one level of A-law or u-law codes.
490 * sr decoder output linear PCM sample,
491 * se predictor estimate sample,
492 * y quantizer step size,
493 * i decoder input code,
494 * sign sign bit of code i
497 * adjusted A-law or u-law compressed sample.
499 int tandem_adjust_alaw(int sr
, int se
, int y
, int i
, int sign
, short const *qtab
)
501 unsigned char sp
; /* A-law compressed 8-bit code */
502 short dx
; /* prediction error */
503 char id
; /* quantized prediction error */
504 int sd
; /* adjusted A-law decoded sample value */
505 int im
; /* biased magnitude of i */
506 int imx
; /* biased magnitude of id */
510 sp
= sox_13linear2alaw(((sr
>> 1) << 3));/* short to A-law compression */
511 dx
= (sox_alaw2linear16(sp
) >> 2) - se
; /* 16-bit prediction error */
512 id
= quantize(dx
, y
, qtab
, sign
- 1);
514 if (id
== i
) { /* no adjustment on sp */
516 } else { /* sp adjustment needed */
517 /* ADPCM codes : 8, 9, ... F, 0, 1, ... , 6, 7 */
518 im
= i
^ sign
; /* 2's complement to biased unsigned */
521 if (imx
> im
) { /* sp adjusted to next lower value */
523 sd
= (sp
== 0xD5) ? 0x55 :
524 ((sp
^ 0x55) - 1) ^ 0x55;
526 sd
= (sp
== 0x2A) ? 0x2A :
527 ((sp
^ 0x55) + 1) ^ 0x55;
529 } else { /* sp adjusted to next higher value */
531 sd
= (sp
== 0xAA) ? 0xAA :
532 ((sp
^ 0x55) + 1) ^ 0x55;
534 sd
= (sp
== 0x55) ? 0xD5 :
535 ((sp
^ 0x55) - 1) ^ 0x55;
541 int tandem_adjust_ulaw(int sr
, int se
, int y
, int i
, int sign
, short const *qtab
)
543 unsigned char sp
; /* u-law compressed 8-bit code */
544 short dx
; /* prediction error */
545 char id
; /* quantized prediction error */
546 int sd
; /* adjusted u-law decoded sample value */
547 int im
; /* biased magnitude of i */
548 int imx
; /* biased magnitude of id */
552 sp
= sox_14linear2ulaw((sr
<< 2));/* short to u-law compression */
553 dx
= (sox_ulaw2linear16(sp
) >> 2) - se
; /* 16-bit prediction error */
554 id
= quantize(dx
, y
, qtab
, sign
- 1);
558 /* ADPCM codes : 8, 9, ... F, 0, 1, ... , 6, 7 */
559 im
= i
^ sign
; /* 2's complement to biased unsigned */
561 if (imx
> im
) { /* sp adjusted to next lower value */
563 sd
= (sp
== 0xFF) ? 0x7E : sp
+ 1;
565 sd
= (sp
== 0) ? 0 : sp
- 1;
567 } else { /* sp adjusted to next higher value */
569 sd
= (sp
== 0x80) ? 0x80 : sp
- 1;
571 sd
= (sp
== 0x7F) ? 0xFE : sp
+ 1;