Initial port of RIFF-WAVE (.wav) sound datatype (v50.3) by Fredrik Wikstrom <fredrik...
[AROS.git] / workbench / classes / datatypes / wav / g72x / g723_16.c
blob0974e61666dadf358e8f8148377d1acafd27e55c
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
26 /* 16kbps version created, used 24kbps code and changing as little as possible.
27 * G.726 specs are available from ITU's gopher or WWW site (http://www.itu.ch)
28 * If any errors are found, please contact me at mrand@tamu.edu
29 * -Marc Randolph
33 * g723_16.c
35 * Description:
37 * g723_16_encoder(), g723_16_decoder()
39 * These routines comprise an implementation of the CCITT G.726 16 Kbps
40 * ADPCM coding algorithm. Essentially, this implementation is identical to
41 * the bit level description except for a few deviations which take advantage
42 * of workstation attributes, such as hardware 2's complement arithmetic.
46 #include "g72x.h"
49 * Maps G.723_16 code word to reconstructed scale factor normalized log
50 * magnitude values. Comes from Table 11/G.726
52 static short _dqlntab[4] = { 116, 365, 365, 116};
54 /* Maps G.723_16 code word to log of scale factor multiplier.
56 * _witab[4] is actually {-22 , 439, 439, -22}, but FILTD wants it
57 * as WI << 5 (multiplied by 32), so we'll do that here
59 static short _witab[4] = {-704, 14048, 14048, -704};
62 * Maps G.723_16 code words to a set of values whose long and short
63 * term averages are computed and then compared to give an indication
64 * how stationary (steady state) the signal is.
67 /* Comes from FUNCTF */
68 static short _fitab[4] = {0, 0xE00, 0xE00, 0};
70 #ifndef NO_G72X_ENCODERS
71 /* Comes from quantizer decision level tables (Table 7/G.726)
73 static short qtab_723_16[1] = {261};
74 #endif /* NO_G72X_ENCODERS */
77 #ifndef NO_G72X_ENCODERS
79 * g723_16_encoder()
81 * Encodes a linear PCM, A-law or u-law input sample and returns its 2-bit code.
82 * Returns -1 if invalid input coding value.
84 int
85 g723_16_encoder(
86 int sl,
87 struct g72x_state *state_ptr)
89 short sei, sezi, se, sez; /* ACCUM */
90 short d; /* SUBTA */
91 short y; /* MIX */
92 short sr; /* ADDB */
93 short dqsez; /* ADDC */
94 short dq, i;
96 sl >>= 2; /* sl of 14-bit dynamic range */
98 sezi = predictor_zero(state_ptr);
99 sez = sezi >> 1;
100 sei = sezi + predictor_pole(state_ptr);
101 se = sei >> 1; /* se = estimated signal */
103 d = sl - se; /* d = estimation diff. */
105 /* quantize prediction difference d */
106 y = step_size(state_ptr); /* quantizer step size */
107 i = quantize(d, y, qtab_723_16, 1); /* i = ADPCM code */
109 /* Since quantize() only produces a three level output
110 * (1, 2, or 3), we must create the fourth one on our own
112 if (i == 3) /* i code for the zero region */
113 if ((d & 0x8000) == 0) /* If d > 0, i=3 isn't right... */
114 i = 0;
116 dq = reconstruct(i & 2, _dqlntab[i], y); /* quantized diff. */
118 sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconstructed signal */
120 dqsez = sr + sez - se; /* pole prediction diff. */
122 update(2, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
124 return (i);
126 #endif /* NO_G72X_ENCODERS */
129 * g723_16_decoder()
131 * Decodes a 2-bit CCITT G.723_16 ADPCM code and returns
132 * the resulting 16-bit linear PCM, A-law or u-law sample value.
133 * -1 is returned if the output coding is unknown.
136 g723_16_decoder(
137 int i,
138 struct g72x_state *state_ptr)
140 short sezi, sei, sez, se; /* ACCUM */
141 short y; /* MIX */
142 short sr; /* ADDB */
143 short dq;
144 short dqsez;
146 i &= 0x03; /* mask to get proper bits */
147 sezi = predictor_zero(state_ptr);
148 sez = sezi >> 1;
149 sei = sezi + predictor_pole(state_ptr);
150 se = sei >> 1; /* se = estimated signal */
152 y = step_size(state_ptr); /* adaptive quantizer step size */
153 dq = reconstruct(i & 0x02, _dqlntab[i], y); /* unquantize pred diff */
155 sr = (dq < 0) ? (se - (dq & 0x3FFF)) : (se + dq); /* reconst. signal */
157 dqsez = sr - se + sez; /* pole prediction diff. */
159 update(2, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
161 return (sr << 2); /* sr was of 14-bit dynamic range */