Debian Codegraph @

adpcm/g72x.c

raw symbols ◀ ▶
Source reconstructed at patch state 1 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 /*
2 * This source code is a product of Sun Microsystems, Inc. and is provided
3 * for unrestricted use. Users may copy or modify this source code without
4 * charge.
5 *
6 * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
7 * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
8 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
9 *
10 * Sun source code is provided with no support and without any obligation on
11 * the part of Sun Microsystems, Inc. to assist in its use, correction,
12 * modification or enhancement.
13 *
14 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
15 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
16 * OR ANY PART THEREOF.
17 *
18 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
19 * or profits or other special, indirect and consequential damages, even if
20 * Sun has been advised of the possibility of such damages.
21 *
22 * Sun Microsystems, Inc.
23 * 2550 Garcia Avenue
24 * Mountain View, California 94043
25 */
26
27 /*
28 * g72x.c
29 *
30 * Common routines for G.721 and G.723 conversions.
31 */
32
33 #include "g72x.h"
34 #include "avoid-implicit.h"
35 #include <stdlib.h>
36
37 static short power2[15] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80,
38 0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000};
39
40 /*
41 * quan()
42 *
43 * quantizes the input val against the table of size short integers.
44 * It returns i if table[i - 1] <= val < table[i].
45 *
46 * Using linear search for simple coding.
47 */
48 static int
49 quan(val, table, size)
50 int val;
51 short *table;
52 int size;
53 {
54 int i;
55
56 for (i = 0; i < size; i++)
57 if (val < *table++)
58 break;
59 return (i);
60 }
61
62 /*
63 * fmult()
64 *
65 * returns the integer product of the 14-bit integer "an" and
66 * "floating point" representation (4-bit exponent, 6-bit mantessa) "srn".
67 */
68 static int
69 fmult(an, srn)
70 int an;
71 int srn;
72 {
73 short anmag, anexp, anmant;
74 short wanexp, wanmag, wanmant;
75 short retval;
76
77 anmag = (an > 0) ? an : ((-an) & 0x1FFF);
78 anexp = quan(anmag, power2, 15) - 6;
79 anmant = (anmag == 0) ? 32 :
80 (anexp >= 0) ? anmag >> anexp : anmag << -anexp;
81 wanexp = anexp + ((srn >> 6) & 0xF) - 13;
82
83 wanmant = (anmant * (srn & 077) + 0x30) >> 4;
84 retval = (wanexp >= 0) ? ((wanmant << wanexp) & 0x7FFF) :
85 (wanmant >> -wanexp);
86
87 return (((an ^ srn) < 0) ? -retval : retval);
88 }
89
90 /*
91 * g72x_init_state()
92 *
93 * This routine initializes and/or resets the g72x_state structure
94 * pointed to by 'state_ptr'.
95 * All the initial state values are specified in the CCITT G.721 document.
96 */
97 void
98 g72x_init_state(state_ptr)
99 struct g72x_state *state_ptr;
100 {
101 int cnta;
102
103 state_ptr->yl = 34816;
104 state_ptr->yu = 544;
105 state_ptr->dms = 0;
106 state_ptr->dml = 0;
107 state_ptr->ap = 0;
108 for (cnta = 0; cnta < 2; cnta++) {
109 state_ptr->a[cnta] = 0;
110 state_ptr->pk[cnta] = 0;
111 state_ptr->sr[cnta] = 32;
112 }
113 for (cnta = 0; cnta < 6; cnta++) {
114 state_ptr->b[cnta] = 0;
115 state_ptr->dq[cnta] = 32;
116 }
117 state_ptr->td = 0;
118 }
119
120 /*
121 * predictor_zero()
122 *
123 * computes the estimated signal from 6-zero predictor.
124 *
125 */
126 int
127 predictor_zero(state_ptr)
128 struct g72x_state *state_ptr;
129 {
130 int i;
131 int sezi;
132
133 sezi = fmult(state_ptr->b[0] >> 2, state_ptr->dq[0]);
134 for (i = 1; i < 6; i++) /* ACCUM */
135 sezi += fmult(state_ptr->b[i] >> 2, state_ptr->dq[i]);
136 return (sezi);
137 }
138 /*
139 * predictor_pole()
140 *
141 * computes the estimated signal from 2-pole predictor.
142 *
143 */
144 int
145 predictor_pole(state_ptr)
146 struct g72x_state *state_ptr;
147 {
148 return (fmult(state_ptr->a[1] >> 2, state_ptr->sr[1]) +
149 fmult(state_ptr->a[0] >> 2, state_ptr->sr[0]));
150 }
151 /*
152 * step_size()
153 *
154 * computes the quantization step size of the adaptive quantizer.
155 *
156 */
157 int
158 step_size(state_ptr)
159 struct g72x_state *state_ptr;
160 {
161 int y;
162 int dif;
163 int al;
164
165 if (state_ptr->ap >= 256)
166 return (state_ptr->yu);
167 else {
168 y = state_ptr->yl >> 6;
169 dif = state_ptr->yu - y;
170 al = state_ptr->ap >> 2;
171 if (dif > 0)
172 y += (dif * al) >> 6;
173 else if (dif < 0)
174 y += (dif * al + 0x3F) >> 6;
175 return (y);
176 }
177 }
178
179 /*
180 * quantize()
181 *
182 * Given a raw sample, 'd', of the difference signal and a
183 * quantization step size scale factor, 'y', this routine returns the
184 * ADPCM codeword to which that sample gets quantized. The step
185 * size scale factor division operation is done in the log base 2 domain
186 * as a subtraction.
187 */
188 int
189 quantize(d, y, table, size)
190 int d; /* Raw difference signal sample */
191 int y; /* Step size multiplier */
192 short *table; /* quantization table */
193 int size; /* table size of short integers */
194 {
195 short dqm; /* Magnitude of 'd' */
196 short exp; /* Integer part of base 2 log of 'd' */
197 short mant; /* Fractional part of base 2 log */
198 short dl; /* Log of magnitude of 'd' */
199 short dln; /* Step size scale factor normalized log */
200 int i;
201
202 /*
203 * LOG
204 *
205 * Compute base 2 log of 'd', and store in 'dl'.
206 */
207 dqm = abs(d);
208 exp = quan(dqm >> 1, power2, 15);
209 mant = ((dqm << 7) >> exp) & 0x7F; /* Fractional portion. */
210 dl = (exp << 7) + mant;
211
212 /*
213 * SUBTB
214 *
215 * "Divide" by step size multiplier.
216 */
217 dln = dl - (y >> 2);
218
219 /*
220 * QUAN
221 *
222 * Obtain codword i for 'd'.
223 */
224 i = quan(dln, table, size);
225 if (d < 0) /* take 1's complement of i */
226 return ((size << 1) + 1 - i);
227 else if (i == 0) /* take 1's complement of 0 */
228 return ((size << 1) + 1); /* new in 1988 */
229 else
230 return (i);
231 }
232 /*
233 * reconstruct()
234 *
235 * Returns reconstructed difference signal 'dq' obtained from
236 * codeword 'i' and quantization step size scale factor 'y'.
237 * Multiplication is performed in log base 2 domain as addition.
238 */
239 int
240 reconstruct(sign, dqln, y)
241 int sign; /* 0 for non-negative value */
242 int dqln; /* G.72x codeword */
243 int y; /* Step size multiplier */
244 {
245 short dql; /* Log of 'dq' magnitude */
246 short dex; /* Integer part of log */
247 short dqt;
248 short dq; /* Reconstructed difference signal sample */
249
250 dql = dqln + (y >> 2); /* ADDA */
251
252 if (dql < 0) {
253 return ((sign) ? -0x8000 : 0);
254 } else { /* ANTILOG */
255 dex = (dql >> 7) & 15;
256 dqt = 128 + (dql & 127);
257 dq = (dqt << 7) >> (14 - dex);
258 return ((sign) ? (dq - 0x8000) : dq);
259 }
260 }
261
262
263 /*
264 * update()
265 *
266 * updates the state variables for each output code
267 */
268 void
269 update(code_size, y, wi, fi, dq, sr, dqsez, state_ptr)
270 int code_size; /* distinguish 723_40 with others */
271 int y; /* quantizer step size */
272 int wi; /* scale factor multiplier */
273 int fi; /* for long/short term energies */
274 int dq; /* quantized prediction difference */
275 int sr; /* reconstructed signal */
276 int dqsez; /* difference from 2-pole predictor */
277 struct g72x_state *state_ptr; /* coder state pointer */
278 {
279 int cnt;
280 short mag, exp, mant; /* Adaptive predictor, FLOAT A */
281 short a2p; /* LIMC */
282 short a1ul; /* UPA1 */
283 short ua2, pks1; /* UPA2 */
284 short uga2a, fa1;
285 short uga2b;
286 char tr; /* tone/transition detector */
287 short ylint, thr2, dqthr;
288 short ylfrac, thr1;
289 short pk0;
290
291 pk0 = (dqsez < 0) ? 1 : 0; /* needed in updating predictor poles */
292
293 mag = dq & 0x7FFF; /* prediction difference magnitude */
294 /* TRANS */
295 ylint = state_ptr->yl >> 15; /* exponent part of yl */
296 ylfrac = (state_ptr->yl >> 10) & 0x1F; /* fractional part of yl */
297 thr1 = (32 + ylfrac) << ylint; /* threshold */
298 thr2 = (ylint > 9) ? 31 << 10 : thr1; /* limit thr2 to 31 << 10 */
299 dqthr = (thr2 + (thr2 >> 1)) >> 1; /* dqthr = 0.75 * thr2 */
300 if (state_ptr->td == 0) /* signal supposed voice */
301 tr = 0;
302 else if (mag <= dqthr) /* supposed data, but small mag */
303 tr = 0; /* treated as voice */
304 else /* signal is data (modem) */
305 tr = 1;
306
307 /*
308 * Quantizer scale factor adaptation.
309 */
310
311 /* FUNCTW & FILTD & DELAY */
312 /* update non-steady state step size multiplier */
313 state_ptr->yu = y + ((wi - y) >> 5);
314
315 /* LIMB */
316 if (state_ptr->yu < 544) /* 544 <= yu <= 5120 */
317 state_ptr->yu = 544;
318 else if (state_ptr->yu > 5120)
319 state_ptr->yu = 5120;
320
321 /* FILTE & DELAY */
322 /* update steady state step size multiplier */
323 state_ptr->yl += state_ptr->yu + ((-state_ptr->yl) >> 6);
324
325 /*
326 * Adaptive predictor coefficients.
327 */
328 if (tr == 1) { /* reset a's and b's for modem signal */
329 state_ptr->a[0] = 0;
330 state_ptr->a[1] = 0;
331 state_ptr->b[0] = 0;
332 state_ptr->b[1] = 0;
333 state_ptr->b[2] = 0;
334 state_ptr->b[3] = 0;
335 state_ptr->b[4] = 0;
336 state_ptr->b[5] = 0;
337 } else { /* update a's and b's */
338 pks1 = pk0 ^ state_ptr->pk[0]; /* UPA2 */
339
340 /* update predictor pole a[1] */
341 a2p = state_ptr->a[1] - (state_ptr->a[1] >> 7);
342 if (dqsez != 0) {
343 fa1 = (pks1) ? state_ptr->a[0] : -state_ptr->a[0];
344 if (fa1 < -8191) /* a2p = function of fa1 */
345 a2p -= 0x100;
346 else if (fa1 > 8191)
347 a2p += 0xFF;
348 else
349 a2p += fa1 >> 5;
350
351 if (pk0 ^ state_ptr->pk[1])
352 /* LIMC */
353 if (a2p <= -12160)
354 a2p = -12288;
355 else if (a2p >= 12416)
356 a2p = 12288;
357 else
358 a2p -= 0x80;
359 else if (a2p <= -12416)
360 a2p = -12288;
361 else if (a2p >= 12160)
362 a2p = 12288;
363 else
364 a2p += 0x80;
365 }
366
367 /* TRIGB & DELAY */
368 state_ptr->a[1] = a2p;
369
370 /* UPA1 */
371 /* update predictor pole a[0] */
372 state_ptr->a[0] -= state_ptr->a[0] >> 8;
373 if (dqsez != 0)
374 if (pks1 == 0)
375 state_ptr->a[0] += 192;
376 else
377 state_ptr->a[0] -= 192;
378
379 /* LIMD */
380 a1ul = 15360 - a2p;
381 if (state_ptr->a[0] < -a1ul)
382 state_ptr->a[0] = -a1ul;
383 else if (state_ptr->a[0] > a1ul)
384 state_ptr->a[0] = a1ul;
385
386 /* UPB : update predictor zeros b[6] */
387 for (cnt = 0; cnt < 6; cnt++) {
388 if (code_size == 5) /* for 40Kbps G.723 */
389 state_ptr->b[cnt] -= state_ptr->b[cnt] >> 9;
390 else /* for G.721 and 24Kbps G.723 */
391 state_ptr->b[cnt] -= state_ptr->b[cnt] >> 8;
392 if (dq & 0x7FFF) { /* XOR */
393 if ((dq ^ state_ptr->dq[cnt]) >= 0)
394 state_ptr->b[cnt] += 128;
395 else
396 state_ptr->b[cnt] -= 128;
397 }
398 }
399 }
400
401 for (cnt = 5; cnt > 0; cnt--)
402 state_ptr->dq[cnt] = state_ptr->dq[cnt-1];
403 /* FLOAT A : convert dq[0] to 4-bit exp, 6-bit mantissa f.p. */
404 if (mag == 0) {
405 state_ptr->dq[0] = (dq >= 0) ? 0x20 : 0xFC20;
406 } else {
407 exp = quan(mag, power2, 15);
408 state_ptr->dq[0] = (dq >= 0) ?
409 (exp << 6) + ((mag << 6) >> exp) :
410 (exp << 6) + ((mag << 6) >> exp) - 0x400;
411 }
412
413 state_ptr->sr[1] = state_ptr->sr[0];
414 /* FLOAT B : convert sr to 4-bit exp., 6-bit mantissa f.p. */
415 if (sr == 0) {
416 state_ptr->sr[0] = 0x20;
417 } else if (sr > 0) {
418 exp = quan(sr, power2, 15);
419 state_ptr->sr[0] = (exp << 6) + ((sr << 6) >> exp);
420 } else if (sr > -32768) {
421 mag = -sr;
422 exp = quan(mag, power2, 15);
423 state_ptr->sr[0] = (exp << 6) + ((mag << 6) >> exp) - 0x400;
424 } else
425 state_ptr->sr[0] = 0xFC20;
426
427 /* DELAY A */
428 state_ptr->pk[1] = state_ptr->pk[0];
429 state_ptr->pk[0] = pk0;
430
431 /* TONE */
432 if (tr == 1) /* this sample has been treated as data */
433 state_ptr->td = 0; /* next one will be treated as voice */
434 else if (a2p < -11776) /* small sample-to-sample correlation */
435 state_ptr->td = 1; /* signal may be data */
436 else /* signal is voice */
437 state_ptr->td = 0;
438
439 /*
440 * Adaptation speed control.
441 */
442 state_ptr->dms += (fi - state_ptr->dms) >> 5; /* FILTA */
443 state_ptr->dml += (((fi << 2) - state_ptr->dml) >> 7); /* FILTB */
444
445 if (tr == 1)
446 state_ptr->ap = 256;
447 else if (y < 1536) /* SUBTC */
448 state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
449 else if (state_ptr->td == 1)
450 state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
451 else if (abs((state_ptr->dms << 2) - state_ptr->dml) >=
452 (state_ptr->dml >> 3))
453 state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
454 else
455 state_ptr->ap += (-state_ptr->ap) >> 4;
456 }
457
458 /*
459 * tandem_adjust(sr, se, y, i, sign)
460 *
461 * At the end of ADPCM decoding, it simulates an encoder which may be receiving
462 * the output of this decoder as a tandem process. If the output of the
463 * simulated encoder differs from the input to this decoder, the decoder output
464 * is adjusted by one level of A-law or u-law codes.
465 *
466 * Input:
467 * sr decoder output linear PCM sample,
468 * se predictor estimate sample,
469 * y quantizer step size,
470 * i decoder input code,
471 * sign sign bit of code i
472 *
473 * Return:
474 * adjusted A-law or u-law compressed sample.
475 */
476 int
477 tandem_adjust_alaw(sr, se, y, i, sign, qtab)
478 int sr; /* decoder output linear PCM sample */
479 int se; /* predictor estimate sample */
480 int y; /* quantizer step size */
481 int i; /* decoder input code */
482 int sign;
483 short *qtab;
484 {
485 unsigned char sp; /* A-law compressed 8-bit code */
486 short dx; /* prediction error */
487 char id; /* quantized prediction error */
488 int sd; /* adjusted A-law decoded sample value */
489 int im; /* biased magnitude of i */
490 int imx; /* biased magnitude of id */
491
492 if (sr <= -32768)
493 sr = -1;
494 sp = linear2alaw((sr >> 1) << 3); /* short to A-law compression */
495 dx = (alaw2linear(sp) >> 2) - se; /* 16-bit prediction error */
496 id = quantize(dx, y, qtab, sign - 1);
497
498 if (id == i) { /* no adjustment on sp */
499 return (sp);
500 } else { /* sp adjustment needed */
501 /* ADPCM codes : 8, 9, ... F, 0, 1, ... , 6, 7 */
502 im = i ^ sign; /* 2's complement to biased unsigned */
503 imx = id ^ sign;
504
505 if (imx > im) { /* sp adjusted to next lower value */
506 if (sp & 0x80) {
507 sd = (sp == 0xD5) ? 0x55 :
508 ((sp ^ 0x55) - 1) ^ 0x55;
509 } else {
510 sd = (sp == 0x2A) ? 0x2A :
511 ((sp ^ 0x55) + 1) ^ 0x55;
512 }
513 } else { /* sp adjusted to next higher value */
514 if (sp & 0x80)
515 sd = (sp == 0xAA) ? 0xAA :
516 ((sp ^ 0x55) + 1) ^ 0x55;
517 else
518 sd = (sp == 0x55) ? 0xD5 :
519 ((sp ^ 0x55) - 1) ^ 0x55;
520 }
521 return (sd);
522 }
523 }
524
525 int
526 tandem_adjust_ulaw(sr, se, y, i, sign, qtab)
527 int sr; /* decoder output linear PCM sample */
528 int se; /* predictor estimate sample */
529 int y; /* quantizer step size */
530 int i; /* decoder input code */
531 int sign;
532 short *qtab;
533 {
534 unsigned char sp; /* u-law compressed 8-bit code */
535 short dx; /* prediction error */
536 char id; /* quantized prediction error */
537 int sd; /* adjusted u-law decoded sample value */
538 int im; /* biased magnitude of i */
539 int imx; /* biased magnitude of id */
540
541 if (sr <= -32768)
542 sr = 0;
543 sp = linear2ulaw(sr << 2); /* short to u-law compression */
544 dx = (ulaw2linear(sp) >> 2) - se; /* 16-bit prediction error */
545 id = quantize(dx, y, qtab, sign - 1);
546 if (id == i) {
547 return (sp);
548 } else {
549 /* ADPCM codes : 8, 9, ... F, 0, 1, ... , 6, 7 */
550 im = i ^ sign; /* 2's complement to biased unsigned */
551 imx = id ^ sign;
552 if (imx > im) { /* sp adjusted to next lower value */
553 if (sp & 0x80)
554 sd = (sp == 0xFF) ? 0x7E : sp + 1;
555 else
556 sd = (sp == 0) ? 0 : sp - 1;
557
558 } else { /* sp adjusted to next higher value */
559 if (sp & 0x80)
560 sd = (sp == 0x80) ? 0x80 : sp - 1;
561 else
562 sd = (sp == 0x7F) ? 0xFE : sp + 1;
563 }
564 return (sd);
565 }
566 }