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 <[email protected]>
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 
26 /*! \file */
27 
28 #include <stdlib.h>
29 #include <string.h>
30 
31 #include "g722_enc_dec.h"
32 #include "g722_typedefs.h"
33 
34 #if !defined(FALSE)
35 #define FALSE 0
36 #endif
37 #if !defined(TRUE)
38 #define TRUE (!FALSE)
39 #endif
40 
41 #define PACKED_OUTPUT (0)
42 #define BITS_PER_SAMPLE (8)
43 
44 #ifndef BUILD_FEATURE_G722_USE_INTRINSIC_SAT
saturate(int32_t amp)45 static __inline int16_t saturate(int32_t amp) {
46   int16_t amp16;
47 
48   /* Hopefully this is optimised for the common case - not clipping */
49   amp16 = (int16_t)amp;
50   if (amp == amp16) {
51     return amp16;
52   }
53   if (amp > 0x7FFF) {
54     return 0x7FFF;
55   }
56   return 0x8000;
57 }
58 #else
saturate(int32_t val)59 static __inline int16_t saturate(int32_t val) {
60   register int32_t res;
61   __asm volatile("SSAT %0, #16, %1\n\t" : "=r"(res) : "r"(val) :);
62   return (int16_t)res;
63 }
64 #endif
65 /*- End of function --------------------------------------------------------*/
66 
block4(g722_band_t * band,int d)67 static void block4(g722_band_t *band, int d) {
68   int wd1;
69   int wd2;
70   int wd3;
71   int i;
72   int sg[7];
73   int ap1, ap2;
74   int sg0, sgi;
75   int sz;
76 
77   /* Block 4, RECONS */
78   band->d[0] = d;
79   band->r[0] = saturate(band->s + d);
80 
81   /* Block 4, PARREC */
82   band->p[0] = saturate(band->sz + d);
83 
84   /* Block 4, UPPOL2 */
85   for (i = 0; i < 3; i++) {
86     sg[i] = band->p[i] >> 15;
87   }
88   wd1 = saturate(band->a[1] << 2);
89 
90   wd2 = (sg[0] == sg[1]) ? -wd1 : wd1;
91   if (wd2 > 32767) {
92     wd2 = 32767;
93   }
94 
95   ap2 = (wd2 >> 7) + ((sg[0] == sg[2]) ? 128 : -128);
96   ap2 += (band->a[2] * 32512) >> 15;
97   if (ap2 > 12288) {
98     ap2 = 12288;
99   } else if (ap2 < -12288) {
100     ap2 = -12288;
101   }
102   band->ap[2] = ap2;
103 
104   /* Block 4, UPPOL1 */
105   sg[0] = band->p[0] >> 15;
106   sg[1] = band->p[1] >> 15;
107   wd1 = (sg[0] == sg[1]) ? 192 : -192;
108   wd2 = (band->a[1] * 32640) >> 15;
109 
110   ap1 = saturate(wd1 + wd2);
111   wd3 = saturate(15360 - band->ap[2]);
112   if (ap1 > wd3) {
113     ap1 = wd3;
114   } else if (ap1 < -wd3) {
115     ap1 = -wd3;
116   }
117   band->ap[1] = ap1;
118 
119   /* Block 4, UPZERO */
120   /* Block 4, FILTEZ */
121   wd1 = (d == 0) ? 0 : 128;
122 
123   sg0 = sg[0] = d >> 15;
124   for (i = 1; i < 7; i++) {
125     sgi = band->d[i] >> 15;
126     wd2 = (sgi == sg0) ? wd1 : -wd1;
127     wd3 = (band->b[i] * 32640) >> 15;
128     band->bp[i] = saturate(wd2 + wd3);
129   }
130 
131   /* Block 4, DELAYA */
132   sz = 0;
133   for (i = 6; i > 0; i--) {
134     int bi;
135 
136     band->d[i] = band->d[i - 1];
137     bi = band->b[i] = band->bp[i];
138     wd1 = saturate(band->d[i] + band->d[i]);
139     sz += (bi * wd1) >> 15;
140   }
141   band->sz = sz;
142 
143   for (i = 2; i > 0; i--) {
144     band->r[i] = band->r[i - 1];
145     band->p[i] = band->p[i - 1];
146     band->a[i] = band->ap[i];
147   }
148 
149   /* Block 4, FILTEP */
150   wd1 = saturate(band->r[1] + band->r[1]);
151   wd1 = (band->a[1] * wd1) >> 15;
152   wd2 = saturate(band->r[2] + band->r[2]);
153   wd2 = (band->a[2] * wd2) >> 15;
154   band->sp = saturate(wd1 + wd2);
155 
156   /* Block 4, PREDIC */
157   band->s = saturate(band->sp + band->sz);
158 }
159 /*- End of function --------------------------------------------------------*/
160 
g722_encode_init(g722_encode_state_t * s,unsigned int rate,int options)161 g722_encode_state_t *g722_encode_init(g722_encode_state_t *s, unsigned int rate, int options) {
162   if (s == NULL) {
163 #ifdef G722_SUPPORT_MALLOC
164     if ((s = (g722_encode_state_t *)malloc(sizeof(*s))) == NULL)
165 #endif
166       return NULL;
167   }
168   memset(s, 0, sizeof(*s));
169   if (rate == 48000) {
170     s->bits_per_sample = 6;
171   } else if (rate == 56000) {
172     s->bits_per_sample = 7;
173   } else {
174     s->bits_per_sample = 8;
175   }
176   s->band[0].det = 32;
177   s->band[1].det = 8;
178   return s;
179 }
180 /*- End of function --------------------------------------------------------*/
181 
g722_encode_release(g722_encode_state_t * s)182 int g722_encode_release(g722_encode_state_t *s) {
183   free(s);
184   return 0;
185 }
186 /*- End of function --------------------------------------------------------*/
187 
188 /* WebRtc, tlegrand:
189  * Only define the following if bit-exactness with reference implementation
190  * is needed. Will only have any effect if input signal is saturated.
191  */
192 // #define RUN_LIKE_REFERENCE_G722
193 #ifdef RUN_LIKE_REFERENCE_G722
limitValues(int16_t rl)194 int16_t limitValues(int16_t rl) {
195   int16_t yl;
196 
197   yl = (rl > 16383) ? 16383 : ((rl < -16384) ? -16384 : rl);
198 
199   return yl;
200 }
201 /*- End of function --------------------------------------------------------*/
202 #endif
203 
204 static int16_t q6[32] = {0,    35,   72,   110,  150,  190,  233,  276,  323,  370,  422,
205                          473,  530,  587,  650,  714,  786,  858,  940,  1023, 1121, 1219,
206                          1339, 1458, 1612, 1765, 1980, 2195, 2557, 2919, 0,    0};
207 static int16_t iln[32] = {0,  63, 62, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19,
208                           18, 17, 16, 15, 14, 13, 12, 11, 10, 9,  8,  7,  6,  5,  4,  0};
209 static int16_t ilp[32] = {0,  61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47,
210                           46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 0};
211 static int16_t wl[8] = {-60, -30, 58, 172, 334, 538, 1198, 3042};
212 static int16_t rl42[16] = {0, 7, 6, 5, 4, 3, 2, 1, 7, 6, 5, 4, 3, 2, 1, 0};
213 static int16_t ilb[32] = {2048, 2093, 2139, 2186, 2233, 2282, 2332, 2383, 2435, 2489, 2543,
214                           2599, 2656, 2714, 2774, 2834, 2896, 2960, 3025, 3091, 3158, 3228,
215                           3298, 3371, 3444, 3520, 3597, 3676, 3756, 3838, 3922, 4008};
216 static int16_t qm4[16] = {0,     -20456, -12896, -8968, -6288, -4240, -2584, -1200,
217                           20456, 12896,  8968,   6288,  4240,  2584,  1200,  0};
218 static int16_t qm2[4] = {-7408, -1616, 7408, 1616};
219 static int16_t qmf_coeffs[12] = {
220         3, -11, 12, 32, -210, 951, 3876, -805, 362, -156, 53, -11,
221 };
222 static int16_t ihn[3] = {0, 1, 0};
223 static int16_t ihp[3] = {0, 3, 2};
224 static int16_t wh[3] = {0, -214, 798};
225 static int16_t rh2[4] = {2, 1, 2, 1};
226 
g722_encode(g722_encode_state_t * s,uint8_t g722_data[],const int16_t amp[],int len)227 int g722_encode(g722_encode_state_t *s, uint8_t g722_data[], const int16_t amp[], int len) {
228   int dlow;
229   int dhigh;
230   int el;
231   int wd;
232   int wd1;
233   int ril;
234   int wd2;
235   int il4;
236   int ih2;
237   int wd3;
238   int eh;
239   int mih;
240   int i;
241   int j;
242   /* Low and high band PCM from the QMF */
243   int xlow;
244   int xhigh;
245   int g722_bytes;
246   /* Even and odd tap accumulators */
247   int sumeven;
248   int sumodd;
249   int ihigh;
250   int ilow;
251   int code;
252 
253   g722_bytes = 0;
254   xhigh = 0;
255   for (j = 0; j < len;) {
256     if (s->itu_test_mode) {
257       xlow = xhigh = amp[j++] >> 1;
258     } else {
259       {
260         /* Apply the transmit QMF */
261         /* Shuffle the buffer down */
262         for (i = 0; i < 22; i++) {
263           s->x[i] = s->x[i + 2];
264         }
265         // TODO: if len is odd, then this can be a buffer overrun
266         s->x[22] = amp[j++];
267         s->x[23] = amp[j++];
268 
269         /* Discard every other QMF output */
270         sumeven = 0;
271         sumodd = 0;
272         for (i = 0; i < 12; i++) {
273           sumodd += s->x[2 * i] * qmf_coeffs[i];
274           sumeven += s->x[2 * i + 1] * qmf_coeffs[11 - i];
275         }
276         /* We shift by 12 to allow for the QMF filters (DC gain = 4096), plus 1
277            to allow for us summing two filters, plus 1 to allow for the 15 bit
278            input to the G.722 algorithm. */
279         xlow = (sumeven + sumodd) >> 14;
280         xhigh = (sumeven - sumodd) >> 14;
281 
282 #ifdef RUN_LIKE_REFERENCE_G722
283         /* The following lines are only used to verify bit-exactness
284          * with reference implementation of G.722. Higher precision
285          * is achieved without limiting the values.
286          */
287         xlow = limitValues(xlow);
288         xhigh = limitValues(xhigh);
289 #endif
290       }
291     }
292     /* Block 1L, SUBTRA */
293     el = saturate(xlow - s->band[0].s);
294 
295     /* Block 1L, QUANTL */
296     wd = (el >= 0) ? el : -(el + 1);
297 
298     for (i = 1; i < 30; i++) {
299       wd1 = (q6[i] * s->band[0].det) >> 12;
300       if (wd < wd1) {
301         break;
302       }
303     }
304     ilow = (el < 0) ? iln[i] : ilp[i];
305 
306     /* Block 2L, INVQAL */
307     ril = ilow >> 2;
308     wd2 = qm4[ril];
309     dlow = (s->band[0].det * wd2) >> 15;
310 
311     /* Block 3L, LOGSCL */
312     il4 = rl42[ril];
313     wd = (s->band[0].nb * 127) >> 7;
314     s->band[0].nb = wd + wl[il4];
315     if (s->band[0].nb < 0) {
316       s->band[0].nb = 0;
317     } else if (s->band[0].nb > 18432) {
318       s->band[0].nb = 18432;
319     }
320 
321     /* Block 3L, SCALEL */
322     wd1 = (s->band[0].nb >> 6) & 31;
323     wd2 = 8 - (s->band[0].nb >> 11);
324     wd3 = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2);
325     s->band[0].det = wd3 << 2;
326 
327     block4(&s->band[0], dlow);
328     {
329       int nb;
330 
331       /* Block 1H, SUBTRA */
332       eh = saturate(xhigh - s->band[1].s);
333 
334       /* Block 1H, QUANTH */
335       wd = (eh >= 0) ? eh : -(eh + 1);
336       wd1 = (564 * s->band[1].det) >> 12;
337       mih = (wd >= wd1) ? 2 : 1;
338       ihigh = (eh < 0) ? ihn[mih] : ihp[mih];
339 
340       /* Block 2H, INVQAH */
341       wd2 = qm2[ihigh];
342       dhigh = (s->band[1].det * wd2) >> 15;
343 
344       /* Block 3H, LOGSCH */
345       ih2 = rh2[ihigh];
346       wd = (s->band[1].nb * 127) >> 7;
347 
348       nb = wd + wh[ih2];
349       if (nb < 0) {
350         nb = 0;
351       } else if (nb > 22528) {
352         nb = 22528;
353       }
354       s->band[1].nb = nb;
355 
356       /* Block 3H, SCALEH */
357       wd1 = (s->band[1].nb >> 6) & 31;
358       wd2 = 10 - (s->band[1].nb >> 11);
359       wd3 = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2);
360       s->band[1].det = wd3 << 2;
361 
362       block4(&s->band[1], dhigh);
363 #if BITS_PER_SAMPLE == 8
364       code = ((ihigh << 6) | ilow);
365 #elif BITS_PER_SAMPLE == 7
366       code = ((ihigh << 6) | ilow) >> 1;
367 #elif BITS_PER_SAMPLE == 6
368       code = ((ihigh << 6) | ilow) >> 2;
369 #endif
370     }
371 
372 #if PACKED_OUTPUT == 1
373     /* Pack the code bits */
374     s->out_buffer |= (code << s->out_bits);
375     s->out_bits += s->bits_per_sample;
376     if (s->out_bits >= 8) {
377       g722_data[g722_bytes++] = (uint8_t)(s->out_buffer & 0xFF);
378       s->out_bits -= 8;
379       s->out_buffer >>= 8;
380     }
381 #else
382     g722_data[g722_bytes++] = (uint8_t)code;
383 #endif
384   }
385   return g722_bytes;
386 }
387 /*- End of function --------------------------------------------------------*/
388 /*- End of file ------------------------------------------------------------*/
389