1 /*
2 * SpanDSP - a series of DSP components for telephony
3 *
4 * g722_decode.c - The ITU G.722 codec, decode part.
5 *
6 * Written by Steve Underwood <steveu@coppice.org>
7 *
8 * Copyright (C) 2005 Steve Underwood
9 *
10 * Despite my general liking of the GPL, I place my own contributions
11 * to this code in the public domain for the benefit of all mankind -
12 * even the slimy ones who might try to proprietize my work and use it
13 * to my detriment.
14 *
15 * Based in part on a single channel G.722 codec which is:
16 *
17 * Copyright (c) CMU 1993
18 * Computer Science, Speech Group
19 * Chengxiang Lu and Alex Hauptmann
20 *
21 * $Id: g722_decode.c,v 1.15 2006/07/07 16:37:49 steveu Exp $
22 *
23 * Modifications for WebRtc, 2011/04/28, by tlegrand:
24 * -Removed usage of inttypes.h and tgmath.h
25 * -Changed to use WebRtc types
26 * -Changed __inline__ to __inline
27 * -Added saturation check on output
28 */
29
30 /*! \file */
31
32
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
36
37 #include <stdio.h>
38 #include <memory.h>
39 #include <stdlib.h>
40
41 #include "typedefs.h"
42 #include "g722_enc_dec.h"
43
44
45 #if !defined(FALSE)
46 #define FALSE 0
47 #endif
48 #if !defined(TRUE)
49 #define TRUE (!FALSE)
50 #endif
51
saturate(int32_t amp)52 static __inline int16_t saturate(int32_t amp)
53 {
54 int16_t amp16;
55
56 /* Hopefully this is optimised for the common case - not clipping */
57 amp16 = (int16_t) amp;
58 if (amp == amp16)
59 return amp16;
60 if (amp > WEBRTC_INT16_MAX)
61 return WEBRTC_INT16_MAX;
62 return WEBRTC_INT16_MIN;
63 }
64 /*- End of function --------------------------------------------------------*/
65
66 static void block4(g722_decode_state_t *s, int band, int d);
67
block4(g722_decode_state_t * s,int band,int d)68 static void block4(g722_decode_state_t *s, int band, int d)
69 {
70 int wd1;
71 int wd2;
72 int wd3;
73 int i;
74
75 /* Block 4, RECONS */
76 s->band[band].d[0] = d;
77 s->band[band].r[0] = saturate(s->band[band].s + d);
78
79 /* Block 4, PARREC */
80 s->band[band].p[0] = saturate(s->band[band].sz + d);
81
82 /* Block 4, UPPOL2 */
83 for (i = 0; i < 3; i++)
84 s->band[band].sg[i] = s->band[band].p[i] >> 15;
85 wd1 = saturate(s->band[band].a[1] << 2);
86
87 wd2 = (s->band[band].sg[0] == s->band[band].sg[1]) ? -wd1 : wd1;
88 if (wd2 > 32767)
89 wd2 = 32767;
90 wd3 = (s->band[band].sg[0] == s->band[band].sg[2]) ? 128 : -128;
91 wd3 += (wd2 >> 7);
92 wd3 += (s->band[band].a[2]*32512) >> 15;
93 if (wd3 > 12288)
94 wd3 = 12288;
95 else if (wd3 < -12288)
96 wd3 = -12288;
97 s->band[band].ap[2] = wd3;
98
99 /* Block 4, UPPOL1 */
100 s->band[band].sg[0] = s->band[band].p[0] >> 15;
101 s->band[band].sg[1] = s->band[band].p[1] >> 15;
102 wd1 = (s->band[band].sg[0] == s->band[band].sg[1]) ? 192 : -192;
103 wd2 = (s->band[band].a[1]*32640) >> 15;
104
105 s->band[band].ap[1] = saturate(wd1 + wd2);
106 wd3 = saturate(15360 - s->band[band].ap[2]);
107 if (s->band[band].ap[1] > wd3)
108 s->band[band].ap[1] = wd3;
109 else if (s->band[band].ap[1] < -wd3)
110 s->band[band].ap[1] = -wd3;
111
112 /* Block 4, UPZERO */
113 wd1 = (d == 0) ? 0 : 128;
114 s->band[band].sg[0] = d >> 15;
115 for (i = 1; i < 7; i++)
116 {
117 s->band[band].sg[i] = s->band[band].d[i] >> 15;
118 wd2 = (s->band[band].sg[i] == s->band[band].sg[0]) ? wd1 : -wd1;
119 wd3 = (s->band[band].b[i]*32640) >> 15;
120 s->band[band].bp[i] = saturate(wd2 + wd3);
121 }
122
123 /* Block 4, DELAYA */
124 for (i = 6; i > 0; i--)
125 {
126 s->band[band].d[i] = s->band[band].d[i - 1];
127 s->band[band].b[i] = s->band[band].bp[i];
128 }
129
130 for (i = 2; i > 0; i--)
131 {
132 s->band[band].r[i] = s->band[band].r[i - 1];
133 s->band[band].p[i] = s->band[band].p[i - 1];
134 s->band[band].a[i] = s->band[band].ap[i];
135 }
136
137 /* Block 4, FILTEP */
138 wd1 = saturate(s->band[band].r[1] + s->band[band].r[1]);
139 wd1 = (s->band[band].a[1]*wd1) >> 15;
140 wd2 = saturate(s->band[band].r[2] + s->band[band].r[2]);
141 wd2 = (s->band[band].a[2]*wd2) >> 15;
142 s->band[band].sp = saturate(wd1 + wd2);
143
144 /* Block 4, FILTEZ */
145 s->band[band].sz = 0;
146 for (i = 6; i > 0; i--)
147 {
148 wd1 = saturate(s->band[band].d[i] + s->band[band].d[i]);
149 s->band[band].sz += (s->band[band].b[i]*wd1) >> 15;
150 }
151 s->band[band].sz = saturate(s->band[band].sz);
152
153 /* Block 4, PREDIC */
154 s->band[band].s = saturate(s->band[band].sp + s->band[band].sz);
155 }
156 /*- End of function --------------------------------------------------------*/
157
WebRtc_g722_decode_init(g722_decode_state_t * s,int rate,int options)158 g722_decode_state_t *WebRtc_g722_decode_init(g722_decode_state_t *s,
159 int rate,
160 int options)
161 {
162 if (s == NULL)
163 {
164 if ((s = (g722_decode_state_t *) malloc(sizeof(*s))) == NULL)
165 return NULL;
166 }
167 memset(s, 0, sizeof(*s));
168 if (rate == 48000)
169 s->bits_per_sample = 6;
170 else if (rate == 56000)
171 s->bits_per_sample = 7;
172 else
173 s->bits_per_sample = 8;
174 if ((options & G722_SAMPLE_RATE_8000))
175 s->eight_k = TRUE;
176 if ((options & G722_PACKED) && s->bits_per_sample != 8)
177 s->packed = TRUE;
178 else
179 s->packed = FALSE;
180 s->band[0].det = 32;
181 s->band[1].det = 8;
182 return s;
183 }
184 /*- End of function --------------------------------------------------------*/
185
WebRtc_g722_decode_release(g722_decode_state_t * s)186 int WebRtc_g722_decode_release(g722_decode_state_t *s)
187 {
188 free(s);
189 return 0;
190 }
191 /*- End of function --------------------------------------------------------*/
192
WebRtc_g722_decode(g722_decode_state_t * s,int16_t amp[],const uint8_t g722_data[],int len)193 int WebRtc_g722_decode(g722_decode_state_t *s, int16_t amp[],
194 const uint8_t g722_data[], int len)
195 {
196 static const int wl[8] = {-60, -30, 58, 172, 334, 538, 1198, 3042 };
197 static const int rl42[16] = {0, 7, 6, 5, 4, 3, 2, 1,
198 7, 6, 5, 4, 3, 2, 1, 0 };
199 static const int ilb[32] =
200 {
201 2048, 2093, 2139, 2186, 2233, 2282, 2332,
202 2383, 2435, 2489, 2543, 2599, 2656, 2714,
203 2774, 2834, 2896, 2960, 3025, 3091, 3158,
204 3228, 3298, 3371, 3444, 3520, 3597, 3676,
205 3756, 3838, 3922, 4008
206 };
207 static const int wh[3] = {0, -214, 798};
208 static const int rh2[4] = {2, 1, 2, 1};
209 static const int qm2[4] = {-7408, -1616, 7408, 1616};
210 static const int qm4[16] =
211 {
212 0, -20456, -12896, -8968,
213 -6288, -4240, -2584, -1200,
214 20456, 12896, 8968, 6288,
215 4240, 2584, 1200, 0
216 };
217 static const int qm5[32] =
218 {
219 -280, -280, -23352, -17560,
220 -14120, -11664, -9752, -8184,
221 -6864, -5712, -4696, -3784,
222 -2960, -2208, -1520, -880,
223 23352, 17560, 14120, 11664,
224 9752, 8184, 6864, 5712,
225 4696, 3784, 2960, 2208,
226 1520, 880, 280, -280
227 };
228 static const int qm6[64] =
229 {
230 -136, -136, -136, -136,
231 -24808, -21904, -19008, -16704,
232 -14984, -13512, -12280, -11192,
233 -10232, -9360, -8576, -7856,
234 -7192, -6576, -6000, -5456,
235 -4944, -4464, -4008, -3576,
236 -3168, -2776, -2400, -2032,
237 -1688, -1360, -1040, -728,
238 24808, 21904, 19008, 16704,
239 14984, 13512, 12280, 11192,
240 10232, 9360, 8576, 7856,
241 7192, 6576, 6000, 5456,
242 4944, 4464, 4008, 3576,
243 3168, 2776, 2400, 2032,
244 1688, 1360, 1040, 728,
245 432, 136, -432, -136
246 };
247 static const int qmf_coeffs[12] =
248 {
249 3, -11, 12, 32, -210, 951, 3876, -805, 362, -156, 53, -11,
250 };
251
252 int dlowt;
253 int rlow;
254 int ihigh;
255 int dhigh;
256 int rhigh;
257 int xout1;
258 int xout2;
259 int wd1;
260 int wd2;
261 int wd3;
262 int code;
263 int outlen;
264 int i;
265 int j;
266
267 outlen = 0;
268 rhigh = 0;
269 for (j = 0; j < len; )
270 {
271 if (s->packed)
272 {
273 /* Unpack the code bits */
274 if (s->in_bits < s->bits_per_sample)
275 {
276 s->in_buffer |= (g722_data[j++] << s->in_bits);
277 s->in_bits += 8;
278 }
279 code = s->in_buffer & ((1 << s->bits_per_sample) - 1);
280 s->in_buffer >>= s->bits_per_sample;
281 s->in_bits -= s->bits_per_sample;
282 }
283 else
284 {
285 code = g722_data[j++];
286 }
287
288 switch (s->bits_per_sample)
289 {
290 default:
291 case 8:
292 wd1 = code & 0x3F;
293 ihigh = (code >> 6) & 0x03;
294 wd2 = qm6[wd1];
295 wd1 >>= 2;
296 break;
297 case 7:
298 wd1 = code & 0x1F;
299 ihigh = (code >> 5) & 0x03;
300 wd2 = qm5[wd1];
301 wd1 >>= 1;
302 break;
303 case 6:
304 wd1 = code & 0x0F;
305 ihigh = (code >> 4) & 0x03;
306 wd2 = qm4[wd1];
307 break;
308 }
309 /* Block 5L, LOW BAND INVQBL */
310 wd2 = (s->band[0].det*wd2) >> 15;
311 /* Block 5L, RECONS */
312 rlow = s->band[0].s + wd2;
313 /* Block 6L, LIMIT */
314 if (rlow > 16383)
315 rlow = 16383;
316 else if (rlow < -16384)
317 rlow = -16384;
318
319 /* Block 2L, INVQAL */
320 wd2 = qm4[wd1];
321 dlowt = (s->band[0].det*wd2) >> 15;
322
323 /* Block 3L, LOGSCL */
324 wd2 = rl42[wd1];
325 wd1 = (s->band[0].nb*127) >> 7;
326 wd1 += wl[wd2];
327 if (wd1 < 0)
328 wd1 = 0;
329 else if (wd1 > 18432)
330 wd1 = 18432;
331 s->band[0].nb = wd1;
332
333 /* Block 3L, SCALEL */
334 wd1 = (s->band[0].nb >> 6) & 31;
335 wd2 = 8 - (s->band[0].nb >> 11);
336 wd3 = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2);
337 s->band[0].det = wd3 << 2;
338
339 block4(s, 0, dlowt);
340
341 if (!s->eight_k)
342 {
343 /* Block 2H, INVQAH */
344 wd2 = qm2[ihigh];
345 dhigh = (s->band[1].det*wd2) >> 15;
346 /* Block 5H, RECONS */
347 rhigh = dhigh + s->band[1].s;
348 /* Block 6H, LIMIT */
349 if (rhigh > 16383)
350 rhigh = 16383;
351 else if (rhigh < -16384)
352 rhigh = -16384;
353
354 /* Block 2H, INVQAH */
355 wd2 = rh2[ihigh];
356 wd1 = (s->band[1].nb*127) >> 7;
357 wd1 += wh[wd2];
358 if (wd1 < 0)
359 wd1 = 0;
360 else if (wd1 > 22528)
361 wd1 = 22528;
362 s->band[1].nb = wd1;
363
364 /* Block 3H, SCALEH */
365 wd1 = (s->band[1].nb >> 6) & 31;
366 wd2 = 10 - (s->band[1].nb >> 11);
367 wd3 = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2);
368 s->band[1].det = wd3 << 2;
369
370 block4(s, 1, dhigh);
371 }
372
373 if (s->itu_test_mode)
374 {
375 amp[outlen++] = (int16_t) (rlow << 1);
376 amp[outlen++] = (int16_t) (rhigh << 1);
377 }
378 else
379 {
380 if (s->eight_k)
381 {
382 amp[outlen++] = (int16_t) (rlow << 1);
383 }
384 else
385 {
386 /* Apply the receive QMF */
387 for (i = 0; i < 22; i++)
388 s->x[i] = s->x[i + 2];
389 s->x[22] = rlow + rhigh;
390 s->x[23] = rlow - rhigh;
391
392 xout1 = 0;
393 xout2 = 0;
394 for (i = 0; i < 12; i++)
395 {
396 xout2 += s->x[2*i]*qmf_coeffs[i];
397 xout1 += s->x[2*i + 1]*qmf_coeffs[11 - i];
398 }
399 /* We shift by 12 to allow for the QMF filters (DC gain = 4096), less 1
400 to allow for the 15 bit input to the G.722 algorithm. */
401 /* WebRtc, tlegrand: added saturation */
402 amp[outlen++] = saturate(xout1 >> 11);
403 amp[outlen++] = saturate(xout2 >> 11);
404 }
405 }
406 }
407 return outlen;
408 }
409 /*- End of function --------------------------------------------------------*/
410 /*- End of file ------------------------------------------------------------*/
411