1 /*
2 * SpanDSP - a series of DSP components for telephony
3 *
4 * g722_encode.c - The ITU G.722 codec, encode part.
5 *
6 * Written by Steve Underwood <steveu@coppice.org>
7 *
8 * Copyright (C) 2005 Steve Underwood
9 *
10 * All rights reserved.
11 *
12 * Despite my general liking of the GPL, I place my own contributions
13 * to this code in the public domain for the benefit of all mankind -
14 * even the slimy ones who might try to proprietize my work and use it
15 * to my detriment.
16 *
17 * Based on a single channel 64kbps only G.722 codec which is:
18 *
19 ***** Copyright (c) CMU 1993 *****
20 * Computer Science, Speech Group
21 * Chengxiang Lu and Alex Hauptmann
22 *
23 * $Id: g722_encode.c,v 1.14 2006/07/07 16:37:49 steveu Exp $
24 *
25 * Modifications for WebRtc, 2011/04/28, by tlegrand:
26 * -Removed usage of inttypes.h and tgmath.h
27 * -Changed to use WebRtc types
28 * -Added option to run encoder bitexact with ITU-T reference implementation
29 */
30
31 /*! \file */
32
33 #include <memory.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36
37 #include "modules/third_party/g722/g722_enc_dec.h"
38
39 #if !defined(FALSE)
40 #define FALSE 0
41 #endif
42 #if !defined(TRUE)
43 #define TRUE (!FALSE)
44 #endif
45
saturate(int32_t amp)46 static __inline int16_t saturate(int32_t amp)
47 {
48 int16_t amp16;
49
50 /* Hopefully this is optimised for the common case - not clipping */
51 amp16 = (int16_t) amp;
52 if (amp == amp16)
53 return amp16;
54 if (amp > WEBRTC_INT16_MAX)
55 return WEBRTC_INT16_MAX;
56 return WEBRTC_INT16_MIN;
57 }
58 /*- End of function --------------------------------------------------------*/
59
block4(G722EncoderState * s,int band,int d)60 static void block4(G722EncoderState *s, int band, int d)
61 {
62 int wd1;
63 int wd2;
64 int wd3;
65 int i;
66
67 /* Block 4, RECONS */
68 s->band[band].d[0] = d;
69 s->band[band].r[0] = saturate(s->band[band].s + d);
70
71 /* Block 4, PARREC */
72 s->band[band].p[0] = saturate(s->band[band].sz + d);
73
74 /* Block 4, UPPOL2 */
75 for (i = 0; i < 3; i++)
76 s->band[band].sg[i] = s->band[band].p[i] >> 15;
77 wd1 = saturate(s->band[band].a[1] << 2);
78
79 wd2 = (s->band[band].sg[0] == s->band[band].sg[1]) ? -wd1 : wd1;
80 if (wd2 > 32767)
81 wd2 = 32767;
82 wd3 = (wd2 >> 7) + ((s->band[band].sg[0] == s->band[band].sg[2]) ? 128 : -128);
83 wd3 += (s->band[band].a[2]*32512) >> 15;
84 if (wd3 > 12288)
85 wd3 = 12288;
86 else if (wd3 < -12288)
87 wd3 = -12288;
88 s->band[band].ap[2] = wd3;
89
90 /* Block 4, UPPOL1 */
91 s->band[band].sg[0] = s->band[band].p[0] >> 15;
92 s->band[band].sg[1] = s->band[band].p[1] >> 15;
93 wd1 = (s->band[band].sg[0] == s->band[band].sg[1]) ? 192 : -192;
94 wd2 = (s->band[band].a[1]*32640) >> 15;
95
96 s->band[band].ap[1] = saturate(wd1 + wd2);
97 wd3 = saturate(15360 - s->band[band].ap[2]);
98 if (s->band[band].ap[1] > wd3)
99 s->band[band].ap[1] = wd3;
100 else if (s->band[band].ap[1] < -wd3)
101 s->band[band].ap[1] = -wd3;
102
103 /* Block 4, UPZERO */
104 wd1 = (d == 0) ? 0 : 128;
105 s->band[band].sg[0] = d >> 15;
106 for (i = 1; i < 7; i++)
107 {
108 s->band[band].sg[i] = s->band[band].d[i] >> 15;
109 wd2 = (s->band[band].sg[i] == s->band[band].sg[0]) ? wd1 : -wd1;
110 wd3 = (s->band[band].b[i]*32640) >> 15;
111 s->band[band].bp[i] = saturate(wd2 + wd3);
112 }
113
114 /* Block 4, DELAYA */
115 for (i = 6; i > 0; i--)
116 {
117 s->band[band].d[i] = s->band[band].d[i - 1];
118 s->band[band].b[i] = s->band[band].bp[i];
119 }
120
121 for (i = 2; i > 0; i--)
122 {
123 s->band[band].r[i] = s->band[band].r[i - 1];
124 s->band[band].p[i] = s->band[band].p[i - 1];
125 s->band[band].a[i] = s->band[band].ap[i];
126 }
127
128 /* Block 4, FILTEP */
129 wd1 = saturate(s->band[band].r[1] + s->band[band].r[1]);
130 wd1 = (s->band[band].a[1]*wd1) >> 15;
131 wd2 = saturate(s->band[band].r[2] + s->band[band].r[2]);
132 wd2 = (s->band[band].a[2]*wd2) >> 15;
133 s->band[band].sp = saturate(wd1 + wd2);
134
135 /* Block 4, FILTEZ */
136 s->band[band].sz = 0;
137 for (i = 6; i > 0; i--)
138 {
139 wd1 = saturate(s->band[band].d[i] + s->band[band].d[i]);
140 s->band[band].sz += (s->band[band].b[i]*wd1) >> 15;
141 }
142 s->band[band].sz = saturate(s->band[band].sz);
143
144 /* Block 4, PREDIC */
145 s->band[band].s = saturate(s->band[band].sp + s->band[band].sz);
146 }
147 /*- End of function --------------------------------------------------------*/
148
WebRtc_g722_encode_init(G722EncoderState * s,int rate,int options)149 G722EncoderState* WebRtc_g722_encode_init(G722EncoderState* s,
150 int rate,
151 int options) {
152 if (s == NULL)
153 {
154 if ((s = (G722EncoderState *) malloc(sizeof(*s))) == NULL)
155 return NULL;
156 }
157 memset(s, 0, sizeof(*s));
158 if (rate == 48000)
159 s->bits_per_sample = 6;
160 else if (rate == 56000)
161 s->bits_per_sample = 7;
162 else
163 s->bits_per_sample = 8;
164 if ((options & G722_SAMPLE_RATE_8000))
165 s->eight_k = TRUE;
166 if ((options & G722_PACKED) && s->bits_per_sample != 8)
167 s->packed = TRUE;
168 else
169 s->packed = FALSE;
170 s->band[0].det = 32;
171 s->band[1].det = 8;
172 return s;
173 }
174 /*- End of function --------------------------------------------------------*/
175
WebRtc_g722_encode_release(G722EncoderState * s)176 int WebRtc_g722_encode_release(G722EncoderState *s)
177 {
178 free(s);
179 return 0;
180 }
181 /*- End of function --------------------------------------------------------*/
182
183 /* WebRtc, tlegrand:
184 * Only define the following if bit-exactness with reference implementation
185 * is needed. Will only have any effect if input signal is saturated.
186 */
187 //#define RUN_LIKE_REFERENCE_G722
188 #ifdef RUN_LIKE_REFERENCE_G722
limitValues(int16_t rl)189 int16_t limitValues (int16_t rl)
190 {
191
192 int16_t yl;
193
194 yl = (rl > 16383) ? 16383 : ((rl < -16384) ? -16384 : rl);
195
196 return (yl);
197 }
198 #endif
199
WebRtc_g722_encode(G722EncoderState * s,uint8_t g722_data[],const int16_t amp[],size_t len)200 size_t WebRtc_g722_encode(G722EncoderState *s, uint8_t g722_data[],
201 const int16_t amp[], size_t len)
202 {
203 static const int q6[32] =
204 {
205 0, 35, 72, 110, 150, 190, 233, 276,
206 323, 370, 422, 473, 530, 587, 650, 714,
207 786, 858, 940, 1023, 1121, 1219, 1339, 1458,
208 1612, 1765, 1980, 2195, 2557, 2919, 0, 0
209 };
210 static const int iln[32] =
211 {
212 0, 63, 62, 31, 30, 29, 28, 27,
213 26, 25, 24, 23, 22, 21, 20, 19,
214 18, 17, 16, 15, 14, 13, 12, 11,
215 10, 9, 8, 7, 6, 5, 4, 0
216 };
217 static const int ilp[32] =
218 {
219 0, 61, 60, 59, 58, 57, 56, 55,
220 54, 53, 52, 51, 50, 49, 48, 47,
221 46, 45, 44, 43, 42, 41, 40, 39,
222 38, 37, 36, 35, 34, 33, 32, 0
223 };
224 static const int wl[8] =
225 {
226 -60, -30, 58, 172, 334, 538, 1198, 3042
227 };
228 static const int rl42[16] =
229 {
230 0, 7, 6, 5, 4, 3, 2, 1, 7, 6, 5, 4, 3, 2, 1, 0
231 };
232 static const int ilb[32] =
233 {
234 2048, 2093, 2139, 2186, 2233, 2282, 2332,
235 2383, 2435, 2489, 2543, 2599, 2656, 2714,
236 2774, 2834, 2896, 2960, 3025, 3091, 3158,
237 3228, 3298, 3371, 3444, 3520, 3597, 3676,
238 3756, 3838, 3922, 4008
239 };
240 static const int qm4[16] =
241 {
242 0, -20456, -12896, -8968,
243 -6288, -4240, -2584, -1200,
244 20456, 12896, 8968, 6288,
245 4240, 2584, 1200, 0
246 };
247 static const int qm2[4] =
248 {
249 -7408, -1616, 7408, 1616
250 };
251 static const int qmf_coeffs[12] =
252 {
253 3, -11, 12, 32, -210, 951, 3876, -805, 362, -156, 53, -11,
254 };
255 static const int ihn[3] = {0, 1, 0};
256 static const int ihp[3] = {0, 3, 2};
257 static const int wh[3] = {0, -214, 798};
258 static const int rh2[4] = {2, 1, 2, 1};
259
260 int dlow;
261 int dhigh;
262 int el;
263 int wd;
264 int wd1;
265 int ril;
266 int wd2;
267 int il4;
268 int ih2;
269 int wd3;
270 int eh;
271 int mih;
272 int i;
273 size_t j;
274 /* Low and high band PCM from the QMF */
275 int xlow;
276 int xhigh;
277 size_t g722_bytes;
278 /* Even and odd tap accumulators */
279 int sumeven;
280 int sumodd;
281 int ihigh;
282 int ilow;
283 int code;
284
285 g722_bytes = 0;
286 xhigh = 0;
287 for (j = 0; j < len; )
288 {
289 if (s->itu_test_mode)
290 {
291 xlow =
292 xhigh = amp[j++] >> 1;
293 }
294 else
295 {
296 if (s->eight_k)
297 {
298 /* We shift by 1 to allow for the 15 bit input to the G.722 algorithm. */
299 xlow = amp[j++] >> 1;
300 }
301 else
302 {
303 /* Apply the transmit QMF */
304 /* Shuffle the buffer down */
305 for (i = 0; i < 22; i++)
306 s->x[i] = s->x[i + 2];
307 s->x[22] = amp[j++];
308 s->x[23] = amp[j++];
309
310 /* Discard every other QMF output */
311 sumeven = 0;
312 sumodd = 0;
313 for (i = 0; i < 12; i++)
314 {
315 sumodd += s->x[2*i]*qmf_coeffs[i];
316 sumeven += s->x[2*i + 1]*qmf_coeffs[11 - i];
317 }
318 /* We shift by 12 to allow for the QMF filters (DC gain = 4096), plus 1
319 to allow for us summing two filters, plus 1 to allow for the 15 bit
320 input to the G.722 algorithm. */
321 xlow = (sumeven + sumodd) >> 14;
322 xhigh = (sumeven - sumodd) >> 14;
323
324 #ifdef RUN_LIKE_REFERENCE_G722
325 /* The following lines are only used to verify bit-exactness
326 * with reference implementation of G.722. Higher precision
327 * is achieved without limiting the values.
328 */
329 xlow = limitValues(xlow);
330 xhigh = limitValues(xhigh);
331 #endif
332 }
333 }
334 /* Block 1L, SUBTRA */
335 el = saturate(xlow - s->band[0].s);
336
337 /* Block 1L, QUANTL */
338 wd = (el >= 0) ? el : -(el + 1);
339
340 for (i = 1; i < 30; i++)
341 {
342 wd1 = (q6[i]*s->band[0].det) >> 12;
343 if (wd < wd1)
344 break;
345 }
346 ilow = (el < 0) ? iln[i] : ilp[i];
347
348 /* Block 2L, INVQAL */
349 ril = ilow >> 2;
350 wd2 = qm4[ril];
351 dlow = (s->band[0].det*wd2) >> 15;
352
353 /* Block 3L, LOGSCL */
354 il4 = rl42[ril];
355 wd = (s->band[0].nb*127) >> 7;
356 s->band[0].nb = wd + wl[il4];
357 if (s->band[0].nb < 0)
358 s->band[0].nb = 0;
359 else if (s->band[0].nb > 18432)
360 s->band[0].nb = 18432;
361
362 /* Block 3L, SCALEL */
363 wd1 = (s->band[0].nb >> 6) & 31;
364 wd2 = 8 - (s->band[0].nb >> 11);
365 wd3 = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2);
366 s->band[0].det = wd3 << 2;
367
368 block4(s, 0, dlow);
369
370 if (s->eight_k)
371 {
372 /* Just leave the high bits as zero */
373 code = (0xC0 | ilow) >> (8 - s->bits_per_sample);
374 }
375 else
376 {
377 /* Block 1H, SUBTRA */
378 eh = saturate(xhigh - s->band[1].s);
379
380 /* Block 1H, QUANTH */
381 wd = (eh >= 0) ? eh : -(eh + 1);
382 wd1 = (564*s->band[1].det) >> 12;
383 mih = (wd >= wd1) ? 2 : 1;
384 ihigh = (eh < 0) ? ihn[mih] : ihp[mih];
385
386 /* Block 2H, INVQAH */
387 wd2 = qm2[ihigh];
388 dhigh = (s->band[1].det*wd2) >> 15;
389
390 /* Block 3H, LOGSCH */
391 ih2 = rh2[ihigh];
392 wd = (s->band[1].nb*127) >> 7;
393 s->band[1].nb = wd + wh[ih2];
394 if (s->band[1].nb < 0)
395 s->band[1].nb = 0;
396 else if (s->band[1].nb > 22528)
397 s->band[1].nb = 22528;
398
399 /* Block 3H, SCALEH */
400 wd1 = (s->band[1].nb >> 6) & 31;
401 wd2 = 10 - (s->band[1].nb >> 11);
402 wd3 = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2);
403 s->band[1].det = wd3 << 2;
404
405 block4(s, 1, dhigh);
406 code = ((ihigh << 6) | ilow) >> (8 - s->bits_per_sample);
407 }
408
409 if (s->packed)
410 {
411 /* Pack the code bits */
412 s->out_buffer |= (code << s->out_bits);
413 s->out_bits += s->bits_per_sample;
414 if (s->out_bits >= 8)
415 {
416 g722_data[g722_bytes++] = (uint8_t) (s->out_buffer & 0xFF);
417 s->out_bits -= 8;
418 s->out_buffer >>= 8;
419 }
420 }
421 else
422 {
423 g722_data[g722_bytes++] = (uint8_t) code;
424 }
425 }
426 return g722_bytes;
427 }
428 /*- End of function --------------------------------------------------------*/
429 /*- End of file ------------------------------------------------------------*/
430