Debian Codegraph @

adpcm/g72x.c

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