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
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.
24 * Mountain View, California 94043
30 * Common routines for G.721 and G.723 conversions.
38 #include "g72x_priv.h"
42 { 1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80,
43 0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000
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.
55 int quan (int val
, short *table
, int size
)
59 for (i
= 0; i
< size
; i
++)
68 * returns the integer product of the 14-bit integer "an" and
69 * "floating point" representation (4-bit exponent, 6-bit mantessa) "srn".
72 int fmult (int an
, int srn
)
74 short anmag
, anexp
, anmant
;
75 short wanexp
, wanmant
;
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;
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) :
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
)
110 state_ptr
->yl
= 34816;
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;
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. */
135 memset (data
, 0, sizeof (G72x_DATA
)) ;
137 pstate
= (G72x_STATE
*) data
->private ;
138 private_init_state (pstate
) ;
140 pstate
->encoder
= NULL
;
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 ;
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 ;
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 ;
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 ;
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. */
185 memset (data
, 0, sizeof (G72x_DATA
)) ;
187 pstate
= (G72x_STATE
*) data
->private ;
188 private_init_state (pstate
) ;
190 pstate
->decoder
= NULL
;
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 ;
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 ;
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 ;
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 ;
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
);
239 data
->samples
[k
] = in_buffer
& ((1 << bits
) - 1);
247 int g72x_decode_block (G72x_DATA
*data
)
248 { G72x_STATE
*pstate
;
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
) ;
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
) ;
271 { out_byte
= out_buffer
& 0xFF ;
274 data
->block
[bindex
++] = out_byte
;
281 int g72x_encode_block (G72x_DATA
*data
)
282 { G72x_STATE
*pstate
;
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
) ;
293 } /* g72x_encode_block */
298 * computes the estimated signal from 6-zero predictor.
301 int predictor_zero (G72x_STATE
*state_ptr
)
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
]);
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]));
325 * computes the quantization step size of the adaptive quantizer.
328 int step_size (G72x_STATE
*state_ptr
)
334 if (state_ptr
->ap
>= 256)
335 return (state_ptr
->yu
);
337 y
= state_ptr
->yl
>> 6;
338 dif
= state_ptr
->yu
- y
;
339 al
= state_ptr
->ap
>> 2;
341 y
+= (dif
* al
) >> 6;
343 y
+= (dif
* al
+ 0x3F) >> 6;
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
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 */
373 * Compute base 2 log of 'd', and store in 'dl'.
376 exp
= quan(dqm
>> 1, power2
, 15);
377 mant
= ((dqm
<< 7) >> exp
) & 0x7F; /* Fractional portion. */
378 dl
= (exp
<< 7) + mant
;
383 * "Divide" by step size multiplier.
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 */
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.
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 */
416 short dq
; /* Reconstructed difference signal sample */
418 dql
= dqln
+ (y
>> 2); /* ADDA */
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
);
434 * updates the state variables for each output code
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 */
448 short mag
, exp
; /* Adaptive predictor, FLOAT A */
449 short a2p
= 0; /* LIMC */
450 short a1ul
; /* UPA1 */
451 short pks1
; /* UPA2 */
453 char tr
; /* tone/transition detector */
454 short ylint
, thr2
, dqthr
;
458 pk0
= (dqsez
< 0) ? 1 : 0; /* needed in updating predictor poles */
460 mag
= dq
& 0x7FFF; /* prediction difference magnitude */
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 */
469 else if (mag
<= dqthr
) /* supposed data, but small mag */
470 tr
= 0; /* treated as voice */
471 else /* signal is data (modem) */
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);
483 if (state_ptr
->yu
< 544) /* 544 <= yu <= 5120 */
485 else if (state_ptr
->yu
> 5120)
486 state_ptr
->yu
= 5120;
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 */
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);
510 fa1
= (pks1
) ? state_ptr
->a
[0] : -state_ptr
->a
[0];
511 if (fa1
< -8191) /* a2p = function of fa1 */
518 if (pk0
^ state_ptr
->pk
[1])
522 else if (a2p
>= 12416)
527 else if (a2p
<= -12416)
529 else if (a2p
>= 12160)
536 state_ptr
->a
[1] = a2p
;
539 /* update predictor pole a[0] */
540 state_ptr
->a
[0] -= state_ptr
->a
[0] >> 8;
543 state_ptr
->a
[0] += 192;
545 state_ptr
->a
[0] -= 192;
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;
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. */
574 state_ptr
->dq
[0] = (dq
>= 0) ? 0x20 : 0xFC20;
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. */
585 state_ptr
->sr
[0] = 0x20;
587 exp
= quan(sr
, power2
, 15);
588 state_ptr
->sr
[0] = (exp
<< 6) + ((sr
<< 6) >> exp
);
589 } else if (sr
> -32768) {
591 exp
= quan(mag
, power2
, 15);
592 state_ptr
->sr
[0] = (exp
<< 6) + ((mag
<< 6) >> exp
) - 0x400;
594 state_ptr
->sr
[0] = (short) 0xFC20;
597 state_ptr
->pk
[1] = state_ptr
->pk
[0];
598 state_ptr
->pk
[0] = pk0
;
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 */
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 */
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;
624 state_ptr
->ap
+= (-state_ptr
->ap
) >> 4;