r663: This commit was generated by cvs2svn to compensate for changes in r662,
[cinelerra_cv.git] / libsndfile / src / G72x / g72x.c
blob33d28aac91f131bc1751c4c2790188a4dadd1d9d
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.
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.
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.
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.
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.
22 * Sun Microsystems, Inc.
23 * 2550 Garcia Avenue
24 * Mountain View, California 94043
28 * g72x.c
30 * Common routines for G.721 and G.723 conversions.
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
37 #include "g72x.h"
38 #include "g72x_priv.h"
40 static
41 short power2 [15] =
42 { 1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80,
43 0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000
44 } ;
47 * quan()
49 * quantizes the input val against the table of size short integers.
50 * It returns i if table[i - 1] <= val < table[i].
52 * Using linear search for simple coding.
54 static
55 int quan (int val, short *table, int size)
57 int i;
59 for (i = 0; i < size; i++)
60 if (val < *table++)
61 break;
62 return (i);
66 * fmult()
68 * returns the integer product of the 14-bit integer "an" and
69 * "floating point" representation (4-bit exponent, 6-bit mantessa) "srn".
71 static
72 int fmult (int an, int srn)
74 short anmag, anexp, anmant;
75 short wanexp, wanmant;
76 short retval;
78 anmag = (an > 0) ? an : ((-an) & 0x1FFF);
79 anexp = quan(anmag, power2, 15) - 6;
80 anmant = (anmag == 0) ? 32 :
81 (anexp >= 0) ? anmag >> anexp : anmag << -anexp;
82 wanexp = anexp + ((srn >> 6) & 0xF) - 13;
85 ** The original was :
86 ** wanmant = (anmant * (srn & 0x37) + 0x30) >> 4 ;
87 ** but could see no valid reason for the + 0x30.
88 ** Removed it and it improved the SNR of the codec.
91 wanmant = (anmant * (srn & 0x37)) >> 4 ;
93 retval = (wanexp >= 0) ? ((wanmant << wanexp) & 0x7FFF) :
94 (wanmant >> -wanexp);
96 return (((an ^ srn) < 0) ? -retval : retval);
100 * private_init_state()
102 * This routine initializes and/or resets the G72x_PRIVATE structure
103 * pointed to by 'state_ptr'.
104 * All the initial state values are specified in the CCITT G.721 document.
106 void private_init_state (G72x_STATE *state_ptr)
108 int cnta;
110 state_ptr->yl = 34816;
111 state_ptr->yu = 544;
112 state_ptr->dms = 0;
113 state_ptr->dml = 0;
114 state_ptr->ap = 0;
115 for (cnta = 0; cnta < 2; cnta++) {
116 state_ptr->a[cnta] = 0;
117 state_ptr->pk[cnta] = 0;
118 state_ptr->sr[cnta] = 32;
120 for (cnta = 0; cnta < 6; cnta++) {
121 state_ptr->b[cnta] = 0;
122 state_ptr->dq[cnta] = 32;
124 state_ptr->td = 0;
125 } /* private_init_state */
127 int g72x_reader_init (G72x_DATA *data, int codec)
128 { G72x_STATE *pstate ;
130 if (sizeof (data->private) < sizeof (G72x_STATE))
131 { /* This is for safety only. */
132 return 1 ;
135 memset (data, 0, sizeof (G72x_DATA)) ;
137 pstate = (G72x_STATE*) data->private ;
138 private_init_state (pstate) ;
140 pstate->encoder = NULL ;
142 switch (codec)
143 { case G723_16_BITS_PER_SAMPLE : /* 2 bits per sample. */
144 pstate->decoder = g723_16_decoder ;
145 data->blocksize = G723_16_BYTES_PER_BLOCK ;
146 data->samplesperblock = G723_16_SAMPLES_PER_BLOCK ;
147 pstate->codec_bits = 2 ;
148 break ;
150 case G723_24_BITS_PER_SAMPLE : /* 3 bits per sample. */
151 pstate->decoder = g723_24_decoder ;
152 data->blocksize = G723_24_BYTES_PER_BLOCK ;
153 data->samplesperblock = G723_24_SAMPLES_PER_BLOCK ;
154 pstate->codec_bits = 3 ;
155 break ;
157 case G721_32_BITS_PER_SAMPLE : /* 4 bits per sample. */
158 pstate->decoder = g721_decoder ;
159 data->blocksize = G721_32_BYTES_PER_BLOCK ;
160 data->samplesperblock = G721_32_SAMPLES_PER_BLOCK ;
161 pstate->codec_bits = 4 ;
162 break ;
164 case G721_40_BITS_PER_SAMPLE : /* 5 bits per sample. */
165 pstate->decoder = g723_40_decoder ;
166 data->blocksize = G721_40_BYTES_PER_BLOCK ;
167 data->samplesperblock = G721_40_SAMPLES_PER_BLOCK ;
168 pstate->codec_bits = 5 ;
169 break ;
171 default : return 1 ;
174 return 0 ;
175 } /* g72x_reader_init */
177 int g72x_writer_init (G72x_DATA *data, int codec)
178 { G72x_STATE *pstate ;
180 if (sizeof (data->private) < sizeof (G72x_STATE))
181 { /* This is for safety only. Gets optimised out. */
182 return 1 ;
185 memset (data, 0, sizeof (G72x_DATA)) ;
187 pstate = (G72x_STATE*) data->private ;
188 private_init_state (pstate) ;
190 pstate->decoder = NULL ;
192 switch (codec)
193 { case G723_16_BITS_PER_SAMPLE : /* 2 bits per sample. */
194 pstate->encoder = g723_16_encoder ;
195 data->blocksize = G723_16_BYTES_PER_BLOCK ;
196 data->samplesperblock = G723_16_SAMPLES_PER_BLOCK ;
197 pstate->codec_bits = 2 ;
198 break ;
200 case G723_24_BITS_PER_SAMPLE : /* 3 bits per sample. */
201 pstate->encoder = g723_24_encoder ;
202 data->blocksize = G723_24_BYTES_PER_BLOCK ;
203 data->samplesperblock = G723_24_SAMPLES_PER_BLOCK ;
204 pstate->codec_bits = 3 ;
205 break ;
207 case G721_32_BITS_PER_SAMPLE : /* 4 bits per sample. */
208 pstate->encoder = g721_encoder ;
209 data->blocksize = G721_32_BYTES_PER_BLOCK ;
210 data->samplesperblock = G721_32_SAMPLES_PER_BLOCK ;
211 pstate->codec_bits = 4 ;
212 break ;
214 case G721_40_BITS_PER_SAMPLE : /* 5 bits per sample. */
215 pstate->encoder = g723_40_encoder ;
216 data->blocksize = G721_40_BYTES_PER_BLOCK ;
217 data->samplesperblock = G721_40_SAMPLES_PER_BLOCK ;
218 pstate->codec_bits = 5 ;
219 break ;
221 default : return 1 ;
224 return 0 ;
225 } /* g72x_writer_init */
227 int unpack_bytes (G72x_DATA *data, int bits)
228 { unsigned int in_buffer = 0 ;
229 unsigned char in_byte ;
230 int k, in_bits = 0, bindex = 0 ;
232 for (k = 0 ; bindex <= data->blocksize && k < G72x_BLOCK_SIZE ; k++)
233 { if (in_bits < bits)
234 { in_byte = data->block [bindex++] ;
236 in_buffer |= (in_byte << in_bits);
237 in_bits += 8;
239 data->samples [k] = in_buffer & ((1 << bits) - 1);
240 in_buffer >>= bits;
241 in_bits -= bits;
244 return k ;
245 } /* unpack_bytes */
247 int g72x_decode_block (G72x_DATA *data)
248 { G72x_STATE *pstate ;
249 int k, count ;
251 pstate = (G72x_STATE*) data->private ;
253 count = unpack_bytes (data, pstate->codec_bits) ;
255 for (k = 0 ; k < count ; k++)
256 data->samples [k] = pstate->decoder (data->samples [k], pstate) ;
258 return 0 ;
259 } /* g72x_decode_block */
261 int pack_bytes (G72x_DATA *data, int bits)
263 unsigned int out_buffer = 0 ;
264 int k, bindex = 0, out_bits = 0 ;
265 unsigned char out_byte ;
267 for (k = 0 ; k < G72x_BLOCK_SIZE ; k++)
268 { out_buffer |= (data->samples [k] << out_bits) ;
269 out_bits += bits ;
270 if (out_bits >= 8)
271 { out_byte = out_buffer & 0xFF ;
272 out_bits -= 8 ;
273 out_buffer >>= 8 ;
274 data->block [bindex++] = out_byte ;
278 return bindex ;
279 } /* pack_bytes */
281 int g72x_encode_block (G72x_DATA *data)
282 { G72x_STATE *pstate ;
283 int k, count ;
285 pstate = (G72x_STATE*) data->private ;
287 for (k = 0 ; k < data->samplesperblock ; k++)
288 data->samples [k] = pstate->encoder (data->samples [k], pstate) ;
290 count = pack_bytes (data, pstate->codec_bits) ;
292 return count ;
293 } /* g72x_encode_block */
296 * predictor_zero()
298 * computes the estimated signal from 6-zero predictor.
301 int predictor_zero (G72x_STATE *state_ptr)
303 int i;
304 int sezi;
306 sezi = fmult(state_ptr->b[0] >> 2, state_ptr->dq[0]);
307 for (i = 1; i < 6; i++) /* ACCUM */
308 sezi += fmult(state_ptr->b[i] >> 2, state_ptr->dq[i]);
309 return (sezi);
312 * predictor_pole()
314 * computes the estimated signal from 2-pole predictor.
317 int predictor_pole(G72x_STATE *state_ptr)
319 return (fmult(state_ptr->a[1] >> 2, state_ptr->sr[1]) +
320 fmult(state_ptr->a[0] >> 2, state_ptr->sr[0]));
323 * step_size()
325 * computes the quantization step size of the adaptive quantizer.
328 int step_size (G72x_STATE *state_ptr)
330 int y;
331 int dif;
332 int al;
334 if (state_ptr->ap >= 256)
335 return (state_ptr->yu);
336 else {
337 y = state_ptr->yl >> 6;
338 dif = state_ptr->yu - y;
339 al = state_ptr->ap >> 2;
340 if (dif > 0)
341 y += (dif * al) >> 6;
342 else if (dif < 0)
343 y += (dif * al + 0x3F) >> 6;
344 return (y);
349 * quantize()
351 * Given a raw sample, 'd', of the difference signal and a
352 * quantization step size scale factor, 'y', this routine returns the
353 * ADPCM codeword to which that sample gets quantized. The step
354 * size scale factor division operation is done in the log base 2 domain
355 * as a subtraction.
357 int quantize(
358 int d, /* Raw difference signal sample */
359 int y, /* Step size multiplier */
360 short *table, /* quantization table */
361 int size) /* table size of short integers */
363 short dqm; /* Magnitude of 'd' */
364 short exp; /* Integer part of base 2 log of 'd' */
365 short mant; /* Fractional part of base 2 log */
366 short dl; /* Log of magnitude of 'd' */
367 short dln; /* Step size scale factor normalized log */
368 int i;
371 * LOG
373 * Compute base 2 log of 'd', and store in 'dl'.
375 dqm = abs(d);
376 exp = quan(dqm >> 1, power2, 15);
377 mant = ((dqm << 7) >> exp) & 0x7F; /* Fractional portion. */
378 dl = (exp << 7) + mant;
381 * SUBTB
383 * "Divide" by step size multiplier.
385 dln = dl - (y >> 2);
388 * QUAN
390 * Obtain codword i for 'd'.
392 i = quan(dln, table, size);
393 if (d < 0) /* take 1's complement of i */
394 return ((size << 1) + 1 - i);
395 else if (i == 0) /* take 1's complement of 0 */
396 return ((size << 1) + 1); /* new in 1988 */
397 else
398 return (i);
401 * reconstruct()
403 * Returns reconstructed difference signal 'dq' obtained from
404 * codeword 'i' and quantization step size scale factor 'y'.
405 * Multiplication is performed in log base 2 domain as addition.
408 reconstruct(
409 int sign, /* 0 for non-negative value */
410 int dqln, /* G.72x codeword */
411 int y) /* Step size multiplier */
413 short dql; /* Log of 'dq' magnitude */
414 short dex; /* Integer part of log */
415 short dqt;
416 short dq; /* Reconstructed difference signal sample */
418 dql = dqln + (y >> 2); /* ADDA */
420 if (dql < 0) {
421 return ((sign) ? -0x8000 : 0);
422 } else { /* ANTILOG */
423 dex = (dql >> 7) & 15;
424 dqt = 128 + (dql & 127);
425 dq = (dqt << 7) >> (14 - dex);
426 return ((sign) ? (dq - 0x8000) : dq);
432 * update()
434 * updates the state variables for each output code
436 void
437 update(
438 int code_size, /* distinguish 723_40 with others */
439 int y, /* quantizer step size */
440 int wi, /* scale factor multiplier */
441 int fi, /* for long/short term energies */
442 int dq, /* quantized prediction difference */
443 int sr, /* reconstructed signal */
444 int dqsez, /* difference from 2-pole predictor */
445 G72x_STATE *state_ptr) /* coder state pointer */
447 int cnt;
448 short mag, exp; /* Adaptive predictor, FLOAT A */
449 short a2p = 0; /* LIMC */
450 short a1ul; /* UPA1 */
451 short pks1; /* UPA2 */
452 short fa1;
453 char tr; /* tone/transition detector */
454 short ylint, thr2, dqthr;
455 short ylfrac, thr1;
456 short pk0;
458 pk0 = (dqsez < 0) ? 1 : 0; /* needed in updating predictor poles */
460 mag = dq & 0x7FFF; /* prediction difference magnitude */
461 /* TRANS */
462 ylint = state_ptr->yl >> 15; /* exponent part of yl */
463 ylfrac = (state_ptr->yl >> 10) & 0x1F; /* fractional part of yl */
464 thr1 = (32 + ylfrac) << ylint; /* threshold */
465 thr2 = (ylint > 9) ? 31 << 10 : thr1; /* limit thr2 to 31 << 10 */
466 dqthr = (thr2 + (thr2 >> 1)) >> 1; /* dqthr = 0.75 * thr2 */
467 if (state_ptr->td == 0) /* signal supposed voice */
468 tr = 0;
469 else if (mag <= dqthr) /* supposed data, but small mag */
470 tr = 0; /* treated as voice */
471 else /* signal is data (modem) */
472 tr = 1;
475 * Quantizer scale factor adaptation.
478 /* FUNCTW & FILTD & DELAY */
479 /* update non-steady state step size multiplier */
480 state_ptr->yu = y + ((wi - y) >> 5);
482 /* LIMB */
483 if (state_ptr->yu < 544) /* 544 <= yu <= 5120 */
484 state_ptr->yu = 544;
485 else if (state_ptr->yu > 5120)
486 state_ptr->yu = 5120;
488 /* FILTE & DELAY */
489 /* update steady state step size multiplier */
490 state_ptr->yl += state_ptr->yu + ((-state_ptr->yl) >> 6);
493 * Adaptive predictor coefficients.
495 if (tr == 1) { /* reset a's and b's for modem signal */
496 state_ptr->a[0] = 0;
497 state_ptr->a[1] = 0;
498 state_ptr->b[0] = 0;
499 state_ptr->b[1] = 0;
500 state_ptr->b[2] = 0;
501 state_ptr->b[3] = 0;
502 state_ptr->b[4] = 0;
503 state_ptr->b[5] = 0;
504 } else { /* update a's and b's */
505 pks1 = pk0 ^ state_ptr->pk[0]; /* UPA2 */
507 /* update predictor pole a[1] */
508 a2p = state_ptr->a[1] - (state_ptr->a[1] >> 7);
509 if (dqsez != 0) {
510 fa1 = (pks1) ? state_ptr->a[0] : -state_ptr->a[0];
511 if (fa1 < -8191) /* a2p = function of fa1 */
512 a2p -= 0x100;
513 else if (fa1 > 8191)
514 a2p += 0xFF;
515 else
516 a2p += fa1 >> 5;
518 if (pk0 ^ state_ptr->pk[1])
519 { /* LIMC */
520 if (a2p <= -12160)
521 a2p = -12288;
522 else if (a2p >= 12416)
523 a2p = 12288;
524 else
525 a2p -= 0x80;
527 else if (a2p <= -12416)
528 a2p = -12288;
529 else if (a2p >= 12160)
530 a2p = 12288;
531 else
532 a2p += 0x80;
535 /* TRIGB & DELAY */
536 state_ptr->a[1] = a2p;
538 /* UPA1 */
539 /* update predictor pole a[0] */
540 state_ptr->a[0] -= state_ptr->a[0] >> 8;
541 if (dqsez != 0)
542 { if (pks1 == 0)
543 state_ptr->a[0] += 192;
544 else
545 state_ptr->a[0] -= 192;
548 /* LIMD */
549 a1ul = 15360 - a2p;
550 if (state_ptr->a[0] < -a1ul)
551 state_ptr->a[0] = -a1ul;
552 else if (state_ptr->a[0] > a1ul)
553 state_ptr->a[0] = a1ul;
555 /* UPB : update predictor zeros b[6] */
556 for (cnt = 0; cnt < 6; cnt++) {
557 if (code_size == 5) /* for 40Kbps G.723 */
558 state_ptr->b[cnt] -= state_ptr->b[cnt] >> 9;
559 else /* for G.721 and 24Kbps G.723 */
560 state_ptr->b[cnt] -= state_ptr->b[cnt] >> 8;
561 if (dq & 0x7FFF) { /* XOR */
562 if ((dq ^ state_ptr->dq[cnt]) >= 0)
563 state_ptr->b[cnt] += 128;
564 else
565 state_ptr->b[cnt] -= 128;
570 for (cnt = 5; cnt > 0; cnt--)
571 state_ptr->dq[cnt] = state_ptr->dq[cnt-1];
572 /* FLOAT A : convert dq[0] to 4-bit exp, 6-bit mantissa f.p. */
573 if (mag == 0) {
574 state_ptr->dq[0] = (dq >= 0) ? 0x20 : 0xFC20;
575 } else {
576 exp = quan(mag, power2, 15);
577 state_ptr->dq[0] = (dq >= 0) ?
578 (exp << 6) + ((mag << 6) >> exp) :
579 (exp << 6) + ((mag << 6) >> exp) - 0x400;
582 state_ptr->sr[1] = state_ptr->sr[0];
583 /* FLOAT B : convert sr to 4-bit exp., 6-bit mantissa f.p. */
584 if (sr == 0) {
585 state_ptr->sr[0] = 0x20;
586 } else if (sr > 0) {
587 exp = quan(sr, power2, 15);
588 state_ptr->sr[0] = (exp << 6) + ((sr << 6) >> exp);
589 } else if (sr > -32768) {
590 mag = -sr;
591 exp = quan(mag, power2, 15);
592 state_ptr->sr[0] = (exp << 6) + ((mag << 6) >> exp) - 0x400;
593 } else
594 state_ptr->sr[0] = (short) 0xFC20;
596 /* DELAY A */
597 state_ptr->pk[1] = state_ptr->pk[0];
598 state_ptr->pk[0] = pk0;
600 /* TONE */
601 if (tr == 1) /* this sample has been treated as data */
602 state_ptr->td = 0; /* next one will be treated as voice */
603 else if (a2p < -11776) /* small sample-to-sample correlation */
604 state_ptr->td = 1; /* signal may be data */
605 else /* signal is voice */
606 state_ptr->td = 0;
609 * Adaptation speed control.
611 state_ptr->dms += (fi - state_ptr->dms) >> 5; /* FILTA */
612 state_ptr->dml += (((fi << 2) - state_ptr->dml) >> 7); /* FILTB */
614 if (tr == 1)
615 state_ptr->ap = 256;
616 else if (y < 1536) /* SUBTC */
617 state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
618 else if (state_ptr->td == 1)
619 state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
620 else if (abs((state_ptr->dms << 2) - state_ptr->dml) >=
621 (state_ptr->dml >> 3))
622 state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
623 else
624 state_ptr->ap += (-state_ptr->ap) >> 4;
626 return ;
627 } /* update */