Initial port of RIFF-WAVE (.wav) sound datatype (v50.3) by Fredrik Wikstrom <fredrik...
[AROS.git] / workbench / classes / datatypes / wav / g72x / g723_24.c
blob389a79c75137656f8f4c846890188127ca129790
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 * g723_24.c
30 * Description:
32 * g723_24_encoder(), g723_24_decoder()
34 * These routines comprise an implementation of the CCITT G.723 24 Kbps
35 * ADPCM coding algorithm. Essentially, this implementation is identical to
36 * the bit level description except for a few deviations which take advantage
37 * of workstation attributes, such as hardware 2's complement arithmetic.
41 #include "g72x.h"
44 * Maps G.723_24 code word to reconstructed scale factor normalized log
45 * magnitude values.
47 static short _dqlntab[8] = {-2048, 135, 273, 373, 373, 273, 135, -2048};
49 /* Maps G.723_24 code word to log of scale factor multiplier. */
50 static short _witab[8] = {-128, 960, 4384, 18624, 18624, 4384, 960, -128};
53 * Maps G.723_24 code words to a set of values whose long and short
54 * term averages are computed and then compared to give an indication
55 * how stationary (steady state) the signal is.
57 static short _fitab[8] = {0, 0x200, 0x400, 0xE00, 0xE00, 0x400, 0x200, 0};
59 #ifndef NO_G72X_ENCODERS
60 static short qtab_723_24[3] = {8, 218, 331};
61 #endif /* NO_G72X_ENCODERS */
63 #ifndef NO_G72X_ENCODERS
65 * g723_24_encoder()
67 * Encodes a linear PCM, A-law or u-law input sample and returns its 3-bit code.
68 * Returns -1 if invalid input coding value.
70 int g723_24_encoder(int sl, struct g72x_state *state_ptr)
72 short sei, sezi, se, sez; /* ACCUM */
73 short d; /* SUBTA */
74 short y; /* MIX */
75 short sr; /* ADDB */
76 short dqsez; /* ADDC */
77 short dq, i;
79 sl >>= 2; /* sl of 14-bit dynamic range */
81 sezi = predictor_zero(state_ptr);
82 sez = sezi >> 1;
83 sei = sezi + predictor_pole(state_ptr);
84 se = sei >> 1; /* se = estimated signal */
86 d = sl - se; /* d = estimation diff. */
88 /* quantize prediction difference d */
89 y = step_size(state_ptr); /* quantizer step size */
90 i = quantize(d, y, qtab_723_24, 3); /* i = ADPCM code */
91 dq = reconstruct(i & 4, _dqlntab[i], y); /* quantized diff. */
93 sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconstructed signal */
95 dqsez = sr + sez - se; /* pole prediction diff. */
97 update(3, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
99 return (i);
101 #endif /* NO_G72X_ENCODERS */
104 * g723_24_decoder()
106 * Decodes a 3-bit CCITT G.723_24 ADPCM code and returns
107 * the resulting 16-bit linear PCM, A-law or u-law sample value.
108 * -1 is returned if the output coding is unknown.
110 int g723_24_decoder(int i, struct g72x_state *state_ptr)
112 short sezi, sei, sez, se; /* ACCUM */
113 short y; /* MIX */
114 short sr; /* ADDB */
115 short dq;
116 short dqsez;
118 i &= 0x07; /* mask to get proper bits */
119 sezi = predictor_zero(state_ptr);
120 sez = sezi >> 1;
121 sei = sezi + predictor_pole(state_ptr);
122 se = sei >> 1; /* se = estimated signal */
124 y = step_size(state_ptr); /* adaptive quantizer step size */
125 dq = reconstruct(i & 0x04, _dqlntab[i], y); /* unquantize pred diff */
127 sr = (dq < 0) ? (se - (dq & 0x3FFF)) : (se + dq); /* reconst. signal */
129 dqsez = sr - se + sez; /* pole prediction diff. */
131 update(3, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
133 return (sr << 2); /* sr was of 14-bit dynamic range */