Utilise new MergeSym feature to no longer overwrite the source .DEF file when buildin...
[openh323.git] / src / g726 / g726_40.c
blob55b55dd3db0e27019640e3fb3bcfedc5ed1efbec
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 * g726_40.c
30 * Description:
32 * g723_40_encoder(), g723_40_decoder()
34 * These routines comprise an implementation of the CCITT G.723 40Kbps
35 * ADPCM coding algorithm. Essentially, this implementation is identical to
36 * the bit level description except for a few deviations which
37 * take advantage of workstation attributes, such as hardware 2's
38 * complement arithmetic.
40 * The deviation from the bit level specification (lookup tables),
41 * preserves the bit level performance specifications.
43 * As outlined in the G.723 Recommendation, the algorithm is broken
44 * down into modules. Each section of code below is preceded by
45 * the name of the module which it is implementing.
47 * The ITU-T G.726 coder is an adaptive differential pulse code modulation
48 * (ADPCM) waveform coding algorithm, suitable for coding of digitized
49 * telephone bandwidth (0.3-3.4 kHz) speech or audio signals sampled at 8 kHz.
50 * This coder operates on a sample-by-sample basis. Input samples may be
51 * represented in linear PCM or companded 8-bit G.711 (m-law/A-law) formats
52 * (i.e., 64 kbps). For 32 kbps operation, each sample is converted into a
53 * 4-bit quantized difference signal resulting in a compression ratio of
54 * 2:1 over the G.711 format. For 24 kbps 40 kbps operation, the quantized
55 * difference signal is 3 bits and 5 bits, respectively.
57 * $Log$
58 * Revision 1.4 2002/11/20 04:29:13 robertj
59 * Included optimisations for G.711 and G.726 codecs, thanks Ted Szoczei
61 * Revision 1.1 2002/02/11 23:24:23 robertj
62 * Updated to openH323 v1.8.0
64 * Revision 1.2 2002/02/10 21:14:54 dereks
65 * Add cvs log history to head of the file.
66 * Ensure file is terminated by a newline.
72 #include "g72x.h"
73 #include "private.h"
76 * Maps G.723_40 code word to ructeconstructed scale factor normalized log
77 * magnitude values.
79 static short _dqlntab[32] = {-2048, -66, 28, 104, 169, 224, 274, 318,
80 358, 395, 429, 459, 488, 514, 539, 566,
81 566, 539, 514, 488, 459, 429, 395, 358,
82 318, 274, 224, 169, 104, 28, -66, -2048};
84 /* Maps G.723_40 code word to log of scale factor multiplier. */
85 static short _witab[32] = {448, 448, 768, 1248, 1280, 1312, 1856, 3200,
86 4512, 5728, 7008, 8960, 11456, 14080, 16928, 22272,
87 22272, 16928, 14080, 11456, 8960, 7008, 5728, 4512,
88 3200, 1856, 1312, 1280, 1248, 768, 448, 448};
91 * Maps G.723_40 code words to a set of values whose long and short
92 * term averages are computed and then compared to give an indication
93 * how stationary (steady state) the signal is.
95 static short _fitab[32] = {0, 0, 0, 0, 0, 0x200, 0x200, 0x200,
96 0x200, 0x200, 0x400, 0x600, 0x800, 0xA00, 0xC00, 0xC00,
97 0xC00, 0xC00, 0xA00, 0x800, 0x600, 0x400, 0x200, 0x200,
98 0x200, 0x200, 0x200, 0, 0, 0, 0, 0};
100 static int qtab_723_40[15] = {-122, -16, 68, 139, 198, 250, 298, 339,
101 378, 413, 445, 475, 502, 528, 553};
104 * g723_40_encoder()
106 * Encodes a 16-bit linear PCM, A-law or u-law input sample and returns
107 * the resulting 5-bit CCITT G.723 40Kbps code.
108 * Returns -1 if the input coding value is invalid.
111 g726_40_encoder(
112 int sl,
113 int in_coding,
114 g726_state *state_ptr)
116 int sezi;
117 int sez; /* ACCUM */
118 int sei;
119 int se;
120 int d; /* SUBTA */
121 int y; /* MIX */
122 int i;
123 int dq;
124 int sr; /* ADDB */
125 int dqsez; /* ADDC */
127 switch (in_coding) { /* linearize input sample to 14-bit PCM */
128 case AUDIO_ENCODING_ALAW:
129 sl = alaw2linear(sl) >> 2;
130 break;
131 case AUDIO_ENCODING_ULAW:
132 sl = ulaw2linear(sl) >> 2;
133 break;
134 case AUDIO_ENCODING_LINEAR:
135 sl >>= 2; /* sl of 14-bit dynamic range */
136 break;
137 default:
138 return (-1);
141 sezi = predictor_zero(state_ptr);
142 sez = sezi >> 1;
143 sei = sezi + predictor_pole(state_ptr);
144 se = sei >> 1; /* se = estimated signal */
146 d = sl - se; /* d = estimation difference */
148 /* quantize prediction difference */
149 y = step_size(state_ptr); /* adaptive quantizer step size */
150 i = quantize(d, y, qtab_723_40, 15); /* i = ADPCM code */
152 dq = reconstruct(i & 0x10, _dqlntab[i], y); /* quantized diff */
154 sr = (dq < 0) ? se - (dq & 0x7FFF) : se + dq; /* reconstructed signal */
156 dqsez = sr + sez - se; /* dqsez = pole prediction diff. */
158 update(5, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
160 return (i);
164 * g723_40_decoder()
166 * Decodes a 5-bit CCITT G.723 40Kbps code and returns
167 * the resulting 16-bit linear PCM, A-law or u-law sample value.
168 * -1 is returned if the output coding is unknown.
171 g726_40_decoder(
172 int i,
173 int out_coding,
174 g726_state *state_ptr)
176 int sezi;
177 int sez; /* ACCUM */
178 int sei;
179 int se;
180 int y; /* MIX */
181 int dq;
182 int sr; /* ADDB */
183 int dqsez;
185 i &= 0x1f; /* mask to get proper bits */
186 sezi = predictor_zero(state_ptr);
187 sez = sezi >> 1;
188 sei = sezi + predictor_pole(state_ptr);
189 se = sei >> 1; /* se = estimated signal */
191 y = step_size(state_ptr); /* adaptive quantizer step size */
192 dq = reconstruct(i & 0x10, _dqlntab[i], y); /* estimation diff. */
194 sr = (dq < 0) ? (se - (dq & 0x7FFF)) : (se + dq); /* reconst. signal */
196 dqsez = sr - se + sez; /* pole prediction diff. */
198 update(5, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
200 switch (out_coding) {
201 case AUDIO_ENCODING_ALAW:
202 return (tandem_adjust_alaw(sr, se, y, i, 0x10, qtab_723_40));
203 case AUDIO_ENCODING_ULAW:
204 return (tandem_adjust_ulaw(sr, se, y, i, 0x10, qtab_723_40));
205 case AUDIO_ENCODING_LINEAR:
206 return (sr << 2); /* sr was of 14-bit dynamic range */
207 default:
208 return (-1);