4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 1992-2001 by Sun Microsystems, Inc.
24 * All rights reserved.
27 #pragma ident "%Z%%M% %I% %E% SMI"
32 * g723_init_state(), g723_encode(), g723_decode()
34 * These routines comprise an implementation of the CCITT G.723 ADPCM coding
35 * algorithm. Essentially, this implementation is identical to
36 * the bit level description except for a few deviations which
37 * take advantage of work station attributes, such as hardware 2's
38 * complement arithmetic and large memory. Specifically, certain time
39 * consuming operations such as multiplications are replaced
40 * with look up tables and software 2's complement operations are
41 * replaced with hardware 2's complement.
43 * The deviation (look up tables) from the bit level
44 * specification, preserves the bit level performance specifications.
46 * As outlined in the G.723 Recommendation, the algorithm is broken
47 * down into modules. Each section of code below is preceded by
48 * the name of the module which it is implementing.
59 * This file contains statically defined lookup tables for
60 * use with the G.723 coding routines.
64 * Maps G.723 code word to reconstructed scale factor normalized log
67 static short _dqlntab
[8] = {-2048, 135, 273, 373, 373, 273, 135, -2048};
69 /* Maps G.723 code word to log of scale factor multiplier. */
70 static short _witab
[8] = {-128, 960, 4384, 18624, 18624, 4384, 960, -128};
73 * Maps G.723 code words to a set of values whose long and short
74 * term averages are computed and then compared to give an indication
75 * how stationary (steady state) the signal is.
77 static short _fitab
[8] = {0, 0x200, 0x400, 0xE00, 0xE00, 0x400, 0x200, 0};
84 * This routine initializes and/or resets the audio_encode_state structure
85 * pointed to by 'state_ptr'.
86 * All the state initial values are specified in the G.723 standard specs.
90 struct audio_g72x_state
*state_ptr
)
94 state_ptr
->yl
= 34816;
99 for (cnta
= 0; cnta
< 2; cnta
++) {
100 state_ptr
->a
[cnta
] = 0;
101 state_ptr
->pk
[cnta
] = 0;
102 state_ptr
->sr
[cnta
] = 32;
104 for (cnta
= 0; cnta
< 6; cnta
++) {
105 state_ptr
->b
[cnta
] = 0;
106 state_ptr
->dq
[cnta
] = 32;
109 state_ptr
->leftover_cnt
= 0; /* no left over codes */
115 * returns the integer product of the "floating point" an and srn
116 * by the lookup table _fmultwanmant[].
124 short anmag
, anexp
, anmant
;
129 ((srn
& 077) + 1) >> (18 - (srn
>> 6)) :
130 -(((srn
& 077) + 1) >> (2 - (srn
>> 6))));
132 anexp
= _fmultanexp
[an
] - 12;
133 anmant
= ((anexp
>= 0) ? an
>> anexp
: an
<< -anexp
) & 07700;
135 wanexp
= anexp
+ (srn
>> 6) - 7;
136 return ((wanexp
>= 0) ?
137 (_fmultwanmant
[(srn
& 077) + anmant
] << wanexp
)
139 _fmultwanmant
[(srn
& 077) + anmant
] >> -wanexp
);
141 wanexp
= anexp
+ (srn
>> 6) - 0xFFF7;
142 return ((wanexp
>= 0) ?
143 -((_fmultwanmant
[(srn
& 077) + anmant
] << wanexp
)
145 -(_fmultwanmant
[(srn
& 077) + anmant
] >> -wanexp
));
148 anmag
= (-an
) & 0x1FFF;
149 anexp
= _fmultanexp
[anmag
] - 12;
150 anmant
= ((anexp
>= 0) ? anmag
>> anexp
: anmag
<< -anexp
)
153 wanexp
= anexp
+ (srn
>> 6) - 7;
154 return ((wanexp
>= 0) ?
155 -((_fmultwanmant
[(srn
& 077) + anmant
] << wanexp
)
157 -(_fmultwanmant
[(srn
& 077) + anmant
] >> -wanexp
));
159 wanexp
= anexp
+ (srn
>> 6) - 0xFFF7;
160 return ((wanexp
>= 0) ?
161 (_fmultwanmant
[(srn
& 077) + anmant
] << wanexp
)
163 _fmultwanmant
[(srn
& 077) + anmant
] >> -wanexp
);
172 * updates the state variables for each output code
182 struct audio_g72x_state
*state_ptr
,
186 long fi
; /* Adaptation speed control, FUNCTF */
187 short mag
, exp
; /* Adaptive predictor, FLOAT A */
188 short a2p
; /* LIMC */
189 short a1ul
; /* UPA1 */
190 short pks1
, fa1
; /* UPA2 */
191 char tr
; /* tone/transition detector */
196 if (state_ptr
->td
== 0)
198 else if (state_ptr
->yl
> 0x40000)
199 tr
= (mag
<= 0x2F80) ? 0 : 1;
201 thr2
= (0x20 + ((state_ptr
->yl
>> 10) & 0x1F)) <<
202 (state_ptr
->yl
>> 15);
206 tr
= (mag
<= (thr2
- (thr2
>> 2))) ? 0 : 1;
210 * Quantizer scale factor adaptation.
213 /* FUNCTW & FILTD & DELAY */
214 state_ptr
->yu
= y
+ ((_witab
[i
] - y
) >> 5);
217 if (state_ptr
->yu
< 544)
219 else if (state_ptr
->yu
> 5120)
220 state_ptr
->yu
= 5120;
223 state_ptr
->yl
+= state_ptr
->yu
+ ((-state_ptr
->yl
) >> 6);
226 * Adaptive predictor coefficients.
240 pks1
= pk0
^ state_ptr
->pk
[0];
242 a2p
= state_ptr
->a
[1] - (state_ptr
->a
[1] >> 7);
244 fa1
= (pks1
) ? state_ptr
->a
[0] : -state_ptr
->a
[0];
252 if (pk0
^ state_ptr
->pk
[1])
256 else if (a2p
>= 12416)
260 else if (a2p
<= -12416)
262 else if (a2p
>= 12160)
269 state_ptr
->a
[1] = a2p
;
272 state_ptr
->a
[0] -= state_ptr
->a
[0] >> 8;
275 state_ptr
->a
[0] += 192;
277 state_ptr
->a
[0] -= 192;
281 if (state_ptr
->a
[0] < -a1ul
)
282 state_ptr
->a
[0] = -a1ul
;
283 else if (state_ptr
->a
[0] > a1ul
)
284 state_ptr
->a
[0] = a1ul
;
286 /* UPB : update of b's */
287 for (cnt
= 0; cnt
< 6; cnt
++) {
288 state_ptr
->b
[cnt
] -= state_ptr
->b
[cnt
] >> 8;
291 if ((dq
^ state_ptr
->dq
[cnt
]) >= 0)
292 state_ptr
->b
[cnt
] += 128;
294 state_ptr
->b
[cnt
] -= 128;
299 for (cnt
= 5; cnt
> 0; cnt
--)
300 state_ptr
->dq
[cnt
] = state_ptr
->dq
[cnt
-1];
303 state_ptr
->dq
[0] = (dq
>= 0) ? 0x20 : 0xFC20;
305 exp
= _fmultanexp
[mag
];
306 state_ptr
->dq
[0] = (dq
>= 0) ?
307 (exp
<< 6) + ((mag
<< 6) >> exp
) :
308 (exp
<< 6) + ((mag
<< 6) >> exp
) - 0x400;
311 state_ptr
->sr
[1] = state_ptr
->sr
[0];
314 state_ptr
->sr
[0] = 0x20;
316 exp
= _fmultanexp
[sr
];
317 state_ptr
->sr
[0] = (exp
<< 6) + ((sr
<< 6) >> exp
);
320 exp
= _fmultanexp
[mag
];
321 state_ptr
->sr
[0] = (exp
<< 6) + ((mag
<< 6) >> exp
) - 0x400;
325 state_ptr
->pk
[1] = state_ptr
->pk
[0];
326 state_ptr
->pk
[0] = pk0
;
331 else if (a2p
< -11776)
337 * Adaptation speed control.
339 fi
= _fitab
[i
]; /* FUNCTF */
340 state_ptr
->dms
+= (fi
- state_ptr
->dms
) >> 5; /* FILTA */
341 state_ptr
->dml
+= (((fi
<< 2) - state_ptr
->dml
) >> 7); /* FILTB */
345 else if (y
< 1536) /* SUBTC */
346 state_ptr
->ap
+= (0x200 - state_ptr
->ap
) >> 4;
347 else if (state_ptr
->td
== 1)
348 state_ptr
->ap
+= (0x200 - state_ptr
->ap
) >> 4;
349 else if (abs((state_ptr
->dms
<< 2) - state_ptr
->dml
) >=
350 (state_ptr
->dml
>> 3))
351 state_ptr
->ap
+= (0x200 - state_ptr
->ap
) >> 4;
353 state_ptr
->ap
+= (-state_ptr
->ap
) >> 4;
361 * Given a raw sample, 'd', of the difference signal and a
362 * quantization step size scale factor, 'y', this routine returns the
363 * G.723 codeword to which that sample gets quantized. The step
364 * size scale factor division operation is done in the log base 2 domain
369 int d
, /* Raw difference signal sample. */
370 int y
) /* Step size multiplier. */
373 short dqm
; /* Magnitude of 'd'. */
374 short exp
; /* Integer part of base 2 log of magnitude of 'd'. */
375 short mant
; /* Fractional part of base 2 log. */
376 short dl
; /* Log of magnitude of 'd'. */
379 short dln
; /* Step size scale factor normalized log. */
382 unsigned char i
; /* G.723 codeword. */
387 * Compute base 2 log of 'd', and store in 'dln'.
391 exp
= _fmultanexp
[dqm
>> 1];
392 mant
= ((dqm
<< 7) >> exp
) & 0x7F; /* Fractional portion. */
393 dl
= (exp
<< 7) + mant
;
398 * "Divide" by step size multiplier.
405 * Obtain codword for 'd'.
407 i
= _g723quani
[dln
& 0xFFF];
409 i
^= 7; /* Stuff in sign of 'd'. */
411 i
= 7; /* New in 1988 revision */
421 * Returns reconstructed difference signal 'dq' obtained from
422 * G.723 codeword 'i' and quantization step size scale factor 'y'.
423 * Multiplication is performed in log base 2 domain as addition.
427 int i
, /* G.723 codeword. */
428 unsigned long y
) /* Step size multiplier. */
431 short dql
; /* Log of 'dq' magnitude. */
434 short dex
; /* Integer part of log. */
436 short dq
; /* Reconstructed difference signal sample. */
439 dql
= _dqlntab
[i
] + (y
>> 2); /* ADDA */
444 dex
= (dql
>> 7) & 15;
445 dqt
= 128 + (dql
& 127);
446 dq
= (dqt
<< 7) >> (14 - dex
);
455 * _tandem_adjust(sr, se, y, i)
459 * At the end of ADPCM decoding, it simulates an encoder which may be receiving
460 * the output of this decoder as a tandem process. If the output of the
461 * simulated encoder differs from the input to this decoder, the decoder output
462 * is adjusted by one level of A-law or Mu-law codes.
465 * sr decoder output linear PCM sample,
466 * se predictor estimate sample,
467 * y quantizer step size,
468 * i decoder input code
471 * adjusted A-law or Mu-law compressed sample.
475 int sr
, /* decoder output linear PCM sample */
476 int se
, /* predictor estimate sample */
477 int y
, /* quantizer step size */
478 int i
) /* decoder input code */
480 unsigned char sp
; /* A-law compressed 8-bit code */
481 short dx
; /* prediction error */
482 char id
; /* quantized prediction error */
483 int sd
; /* adjusted A-law decoded sample value */
484 int im
; /* biased magnitude of i */
485 int imx
; /* biased magnitude of id */
487 sp
= audio_s2a((sr
<= -0x2000)? -0x8000 :
488 (sr
< 0x1FFF)? sr
<< 2 : 0x7FFF); /* short to A-law compression */
489 dx
= (audio_a2s(sp
) >> 2) - se
; /* 16-bit prediction error */
490 id
= _g723_quantize(dx
, y
);
492 if (id
== i
) /* no adjustment on sp */
494 else { /* sp adjustment needed */
495 im
= i
^ 4; /* 2's complement to biased unsigned */
498 if (imx
> im
) { /* sp adjusted to next lower value */
500 sd
= (sp
== 0xD5)? 0x55 :
501 ((sp
^ 0x55) - 1) ^ 0x55;
503 sd
= (sp
== 0x2A)? 0x2A :
504 ((sp
^ 0x55) + 1) ^ 0x55;
505 } else { /* sp adjusted to next higher value */
507 sd
= (sp
== 0xAA)? 0xAA :
508 ((sp
^ 0x55) + 1) ^ 0x55;
510 sd
= (sp
== 0x55)? 0xD5 :
511 ((sp
^ 0x55) - 1) ^ 0x55;
519 int sr
, /* decoder output linear PCM sample */
520 int se
, /* predictor estimate sample */
521 int y
, /* quantizer step size */
522 int i
) /* decoder input code */
524 unsigned char sp
; /* A-law compressed 8-bit code */
525 short dx
; /* prediction error */
526 char id
; /* quantized prediction error */
527 int sd
; /* adjusted A-law decoded sample value */
528 int im
; /* biased magnitude of i */
529 int imx
; /* biased magnitude of id */
531 sp
= audio_s2u((sr
<= -0x2000)? -0x8000 :
532 (sr
>= 0x1FFF)? 0x7FFF : sr
<< 2); /* short to u-law compression */
533 dx
= (audio_u2s(sp
) >> 2) - se
; /* 16-bit prediction error */
534 id
= _g723_quantize(dx
, y
);
538 /* ADPCM codes : 8, 9, ... F, 0, 1, ... , 6, 7 */
539 im
= i
^ 4; /* 2's complement to biased unsigned */
542 /* u-law codes : 0, 1, ... 7E, 7F, FF, FE, ... 81, 80 */
543 if (imx
> im
) { /* sp adjusted to next lower value */
545 sd
= (sp
== 0xFF)? 0x7E : sp
+ 1;
547 sd
= (sp
== 0)? 0 : sp
- 1;
549 } else { /* sp adjusted to next higher value */
551 sd
= (sp
== 0x80)? 0x80 : sp
- 1;
553 sd
= (sp
== 0x7F)? 0xFE : sp
+ 1;
562 struct audio_g72x_state
*state_ptr
)
564 short sei
, sezi
, se
, sez
; /* ACCUM */
566 float al
; /* use floating point for faster multiply */
567 short y
, dif
; /* MIX */
569 short pk0
, sigpk
, dqsez
; /* ADDC */
574 sezi
= _g723_fmult(state_ptr
->b
[0] >> 2, state_ptr
->dq
[0]);
575 for (cnt
= 1; cnt
< 6; cnt
++)
576 sezi
= sezi
+ _g723_fmult(state_ptr
->b
[cnt
] >> 2,
579 for (cnt
= 1; cnt
> -1; cnt
--)
580 sei
= sei
+ _g723_fmult(state_ptr
->a
[cnt
] >> 2,
585 d
= sl
- se
; /* SUBTA */
587 if (state_ptr
->ap
>= 256)
590 y
= state_ptr
->yl
>> 6;
591 dif
= state_ptr
->yu
- y
;
592 al
= state_ptr
->ap
>> 2;
594 y
+= ((int)(dif
* al
)) >> 6;
596 y
+= ((int)(dif
* al
) + 0x3F) >> 6;
599 i
= _g723_quantize(d
, y
);
600 dq
= _g723_reconstr(i
, y
);
602 sr
= (dq
< 0) ? se
- (dq
& 0x3FFF) : se
+ dq
; /* ADDB */
604 dqsez
= sr
+ sez
- se
; /* ADDC */
609 pk0
= (dqsez
< 0) ? 1 : 0;
613 _g723_update(y
, i
, dq
, sr
, pk0
, state_ptr
, sigpk
);
623 * Encodes a buffer of linear PCM, A-law or Mu-law data pointed to by 'in_buf'
624 * according the G.723 encoding algorithm and packs the resulting code words
625 * into bytes. The bytes of codewords are written to a buffer
626 * pointed to by 'out_buf'.
630 * In the event that the number packed codes is shorter than a sample unit,
631 * the remainder is saved in the state stucture till next call. It is then
632 * packed into the new buffer on the next call.
633 * The number of valid bytes in 'out_buf' is returned in *out_size. Note that
634 * this will not always be equal to 3/8 of 'data_size' on input. On the
635 * final call to 'g723_encode()' the calling program might want to
636 * check if any code bits was left over. This can be
637 * done by calling 'g723_encode()' with data_size = 0, which returns in
638 * *out_size a* 0 if nothing was leftover and the number of bits left over in
639 * the state structure which now is in out_buf[0].
641 * The 3 lower significant bits of an individual byte in the output byte
642 * stream is packed with a G.723 code first. Then the 3 higher order
643 * bits are packed with the next code.
649 Audio_hdr
*in_header
,
650 unsigned char *out_buf
,
652 struct audio_g72x_state
*state_ptr
)
655 unsigned char *out_ptr
;
656 unsigned char *leftover
;
661 unsigned char *char_ptr
;
663 /* Dereference the array pointer for faster access */
664 leftover
= &state_ptr
->leftover
[0];
666 /* Return all cached leftovers */
667 if (data_size
== 0) {
668 for (i
= 0; state_ptr
->leftover_cnt
> 0; i
++) {
669 *out_buf
++ = leftover
[i
];
670 state_ptr
->leftover_cnt
-= 8;
673 /* Round up to a complete sample unit */
678 state_ptr
->leftover_cnt
= 0;
679 return (AUDIO_SUCCESS
);
682 /* XXX - if linear, it had better be 16-bit! */
683 if (in_header
->encoding
== AUDIO_ENCODING_LINEAR
) {
685 return (AUDIO_ERR_BADFRAME
);
688 short_ptr
= (short *)in_buf
;
691 char_ptr
= (unsigned char *)in_buf
;
693 out_ptr
= (unsigned char *)out_buf
;
695 offset
= state_ptr
->leftover_cnt
/ 8;
696 bits
= state_ptr
->leftover_cnt
% 8;
697 codes
= (bits
> 0) ? leftover
[offset
] : 0;
699 while (data_size
--) {
700 switch (in_header
->encoding
) {
701 case AUDIO_ENCODING_LINEAR
:
702 i
= _encoder(*short_ptr
++ >> 2, state_ptr
);
704 case AUDIO_ENCODING_ALAW
:
705 i
= _encoder(audio_a2s(*char_ptr
++) >> 2, state_ptr
);
707 case AUDIO_ENCODING_ULAW
:
708 i
= _encoder(audio_u2s(*char_ptr
++) >> 2, state_ptr
);
711 return (AUDIO_ERR_ENCODING
);
713 /* pack the resulting code into leftover buffer */
717 leftover
[offset
] = codes
& 0xff;
722 state_ptr
->leftover_cnt
+= 3;
724 /* got a whole sample unit so copy it out and reset */
726 *out_ptr
++ = leftover
[0];
727 *out_ptr
++ = leftover
[1];
728 *out_ptr
++ = leftover
[2];
730 state_ptr
->leftover_cnt
= 0;
734 /* If any residual bits, save them for the next call */
736 leftover
[offset
] = codes
& 0xff;
737 state_ptr
->leftover_cnt
+= bits
;
739 *out_size
= (out_ptr
- (unsigned char *)out_buf
);
740 return (AUDIO_SUCCESS
);
748 * Decodes a buffer of G.723 encoded data pointed to by 'in_buf' and
749 * writes the resulting linear PCM, A-law or Mu-law words into a buffer
750 * pointed to by 'out_buf'.
755 unsigned char *in_buf
, /* Buffer of g723 encoded data. */
756 int data_size
, /* Size in bytes of in_buf. */
757 Audio_hdr
*out_header
,
758 void *out_buf
, /* Decoded data buffer. */
760 struct audio_g72x_state
*state_ptr
) /* the decoder's state structure. */
762 unsigned char *inbuf_end
;
763 unsigned char *in_ptr
, *out_ptr
;
769 short sezi
, sei
, sez
, se
; /* ACCUM */
770 float al
; /* use floating point for faster multiply */
771 short y
, dif
; /* MIX */
780 inbuf_end
= in_buf
+ data_size
;
781 out_ptr
= (unsigned char *)out_buf
;
782 linear_ptr
= (short *)out_buf
;
784 /* Leftovers in decoding are only up to 8 bits */
785 bits
= state_ptr
->leftover_cnt
;
786 codes
= (bits
> 0) ? state_ptr
->leftover
[0] : 0;
788 while ((bits
>= 3) || (in_ptr
< (unsigned char *)inbuf_end
)) {
790 codes
+= *in_ptr
++ << bits
;
795 sezi
= _g723_fmult(state_ptr
->b
[0] >> 2, state_ptr
->dq
[0]);
796 for (cnt
= 1; cnt
< 6; cnt
++)
797 sezi
= sezi
+ _g723_fmult(state_ptr
->b
[cnt
] >> 2,
800 for (cnt
= 1; cnt
>= 0; cnt
--)
801 sei
= sei
+ _g723_fmult(state_ptr
->a
[cnt
] >> 2,
806 if (state_ptr
->ap
>= 256)
809 y
= state_ptr
->yl
>> 6;
810 dif
= state_ptr
->yu
- y
;
811 al
= state_ptr
->ap
>> 2;
813 y
+= ((int)(dif
* al
)) >> 6;
815 y
+= ((int)(dif
* al
) + 0x3F) >> 6;
819 dq
= _g723_reconstr(i
, y
);
822 sr
= se
- (dq
& 0x3FFF);
827 dqsez
= sr
- se
+ sez
; /* ADDC */
828 pk0
= (dqsez
< 0) ? 1 : 0;
829 sigpk
= (dqsez
) ? 0 : 1;
831 _g723_update(y
, i
, dq
, sr
, pk0
, state_ptr
, sigpk
);
833 switch (out_header
->encoding
) {
834 case AUDIO_ENCODING_LINEAR
:
835 *linear_ptr
++ = ((sr
<= -0x2000) ? -0x8000 :
836 (sr
>= 0x1FFF) ? 0x7FFF : sr
<< 2);
838 case AUDIO_ENCODING_ALAW
:
839 *out_ptr
++ = _tandem_adjust_alaw(sr
, se
, y
, i
);
841 case AUDIO_ENCODING_ULAW
:
842 *out_ptr
++ = _tandem_adjust_ulaw(sr
, se
, y
, i
);
845 return (AUDIO_ERR_ENCODING
);
850 state_ptr
->leftover_cnt
= bits
;
852 state_ptr
->leftover
[0] = codes
;
854 /* Calculate number of samples returned */
855 if (out_header
->encoding
== AUDIO_ENCODING_LINEAR
)
856 *out_size
= linear_ptr
- (short *)out_buf
;
858 *out_size
= out_ptr
- (unsigned char *)out_buf
;
860 return (AUDIO_SUCCESS
);