SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
audiog722.cpp
Go to the documentation of this file.
1 // Copyright (C) 2019 F4EXB //
3 // written by Edouard Griffiths //
4 // //
5 // This program is free software; you can redistribute it and/or modify //
6 // it under the terms of the GNU General Public License as published by //
7 // the Free Software Foundation as version 3 of the License, or //
8 // (at your option) any later version. //
9 // //
10 // This program is distributed in the hope that it will be useful, //
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
13 // GNU General Public License V3 for more details. //
14 // //
15 // You should have received a copy of the GNU General Public License //
16 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
18 
19 #include <algorithm>
20 #include "audiog722.h"
21 
22 #define G722_SAMPLE_RATE_8000 1
23 #define G722_PACKED 2
24 
25 const int AudioG722::q6[32] =
26 {
27  0, 35, 72, 110, 150, 190, 233, 276,
28  323, 370, 422, 473, 530, 587, 650, 714,
29  786, 858, 940, 1023, 1121, 1219, 1339, 1458,
30  1612, 1765, 1980, 2195, 2557, 2919, 0, 0
31 };
32 const int AudioG722::iln[32] =
33 {
34  0, 63, 62, 31, 30, 29, 28, 27,
35  26, 25, 24, 23, 22, 21, 20, 19,
36  18, 17, 16, 15, 14, 13, 12, 11,
37  10, 9, 8, 7, 6, 5, 4, 0
38 };
39 const int AudioG722::ilp[32] =
40 {
41  0, 61, 60, 59, 58, 57, 56, 55,
42  54, 53, 52, 51, 50, 49, 48, 47,
43  46, 45, 44, 43, 42, 41, 40, 39,
44  38, 37, 36, 35, 34, 33, 32, 0
45 };
46 const int AudioG722::wl[8] =
47 {
48  -60, -30, 58, 172, 334, 538, 1198, 3042
49 };
50 const int AudioG722::rl42[16] =
51 {
52  0, 7, 6, 5, 4, 3, 2, 1, 7, 6, 5, 4, 3, 2, 1, 0
53 };
54 const int AudioG722::ilb[32] =
55 {
56  2048, 2093, 2139, 2186, 2233, 2282, 2332,
57  2383, 2435, 2489, 2543, 2599, 2656, 2714,
58  2774, 2834, 2896, 2960, 3025, 3091, 3158,
59  3228, 3298, 3371, 3444, 3520, 3597, 3676,
60  3756, 3838, 3922, 4008
61 };
62 const int AudioG722::qm4[16] =
63 {
64  0, -20456, -12896, -8968,
65  -6288, -4240, -2584, -1200,
66  20456, 12896, 8968, 6288,
67  4240, 2584, 1200, 0
68 };
69 const int AudioG722::qm2[4] =
70 {
71  -7408, -1616, 7408, 1616
72 };
73 const int AudioG722::qmf_coeffs[12] =
74 {
75  3, -11, 12, 32, -210, 951, 3876, -805, 362, -156, 53, -11,
76 };
77 const int AudioG722::ihn[3] = {0, 1, 0};
78 const int AudioG722::ihp[3] = {0, 3, 2};
79 const int AudioG722::wh[3] = {0, -214, 798};
80 const int AudioG722::rh2[4] = {2, 1, 2, 1};
81 
82 
84 {
85  init(64000, 0);
86 }
87 
88 void AudioG722::g722_encode_state::init(int rate, int options)
89 {
90  itu_test_mode = 0;
91  std::fill(x, x+24, 0);
92  in_buffer = 0;
93  in_bits = 0;
94  out_buffer = 0;
95  out_bits = 0;
96 
97  char *pBand = (char *) band;
98  std::fill(pBand, pBand + sizeof(band), 0);
99 
100  if (rate == 48000) {
101  bits_per_sample = 6;
102  } else if (rate == 56000) {
103  bits_per_sample = 7;
104  } else {
105  bits_per_sample = 8;
106  }
107 
108 
109  if ((options & G722_SAMPLE_RATE_8000)) {
110  eight_k = 1;
111  } else {
112  eight_k = 0;
113  }
114 
115  if ((options & G722_PACKED) && bits_per_sample != 8) {
116  packed = 1;
117  } else {
118  packed = 0;
119  }
120 
121  band[0].det = 32;
122  band[1].det = 8;
123 }
124 
126 {}
127 
129 {}
130 
131 void AudioG722::init(int rate, int options)
132 {
133  state.init(rate, options);
134 }
135 
136 void AudioG722::block4(int band, int d)
137 {
138  int wd1;
139  int wd2;
140  int wd3;
141  int i;
142 
143  /* Block 4, RECONS */
144  state.band[band].d[0] = d;
145  state.band[band].r[0] = saturate(state.band[band].s + d);
146 
147  /* Block 4, PARREC */
148  state.band[band].p[0] = saturate(state.band[band].sz + d);
149 
150  /* Block 4, UPPOL2 */
151  for (i = 0; i < 3; i++) {
152  state.band[band].sg[i] = state.band[band].p[i] >> 15;
153  }
154 
155  wd1 = saturate(state.band[band].a[1] << 2);
156  wd2 = (state.band[band].sg[0] == state.band[band].sg[1]) ? -wd1 : wd1;
157 
158  if (wd2 > 32767) {
159  wd2 = 32767;
160  }
161 
162  wd3 = (wd2 >> 7) + ((state.band[band].sg[0] == state.band[band].sg[2]) ? 128 : -128);
163  wd3 += (state.band[band].a[2]*32512) >> 15;
164 
165  if (wd3 > 12288) {
166  wd3 = 12288;
167  } else if (wd3 < -12288) {
168  wd3 = -12288;
169  }
170 
171  state.band[band].ap[2] = wd3;
172 
173  /* Block 4, UPPOL1 */
174  state.band[band].sg[0] = state.band[band].p[0] >> 15;
175  state.band[band].sg[1] = state.band[band].p[1] >> 15;
176  wd1 = (state.band[band].sg[0] == state.band[band].sg[1]) ? 192 : -192;
177  wd2 = (state.band[band].a[1]*32640) >> 15;
178 
179  state.band[band].ap[1] = saturate(wd1 + wd2);
180  wd3 = saturate(15360 - state.band[band].ap[2]);
181 
182  if (state.band[band].ap[1] > wd3) {
183  state.band[band].ap[1] = wd3;
184  } else if (state.band[band].ap[1] < -wd3) {
185  state.band[band].ap[1] = -wd3;
186  }
187 
188  /* Block 4, UPZERO */
189  wd1 = (d == 0) ? 0 : 128;
190  state.band[band].sg[0] = d >> 15;
191 
192  for (i = 1; i < 7; i++)
193  {
194  state.band[band].sg[i] = state.band[band].d[i] >> 15;
195  wd2 = (state.band[band].sg[i] == state.band[band].sg[0]) ? wd1 : -wd1;
196  wd3 = (state.band[band].b[i]*32640) >> 15;
197  state.band[band].bp[i] = saturate(wd2 + wd3);
198  }
199 
200  /* Block 4, DELAYA */
201  for (i = 6; i > 0; i--)
202  {
203  state.band[band].d[i] = state.band[band].d[i - 1];
204  state.band[band].b[i] = state.band[band].bp[i];
205  }
206 
207  for (i = 2; i > 0; i--)
208  {
209  state.band[band].r[i] = state.band[band].r[i - 1];
210  state.band[band].p[i] = state.band[band].p[i - 1];
211  state.band[band].a[i] = state.band[band].ap[i];
212  }
213 
214  /* Block 4, FILTEP */
215  wd1 = saturate(state.band[band].r[1] + state.band[band].r[1]);
216  wd1 = (state.band[band].a[1]*wd1) >> 15;
217  wd2 = saturate(state.band[band].r[2] + state.band[band].r[2]);
218  wd2 = (state.band[band].a[2]*wd2) >> 15;
219  state.band[band].sp = saturate(wd1 + wd2);
220 
221  /* Block 4, FILTEZ */
222  state.band[band].sz = 0;
223 
224  for (i = 6; i > 0; i--)
225  {
226  wd1 = saturate(state.band[band].d[i] + state.band[band].d[i]);
227  state.band[band].sz += (state.band[band].b[i]*wd1) >> 15;
228  }
229 
230  state.band[band].sz = saturate(state.band[band].sz);
231 
232  /* Block 4, PREDIC */
233  state.band[band].s = saturate(state.band[band].sp + state.band[band].sz);
234 }
235 
236 int AudioG722::encode(uint8_t g722_data[], const int16_t amp[], int len)
237 {
238  int dlow;
239  int dhigh;
240  int el;
241  int wd;
242  int wd1;
243  int ril;
244  int wd2;
245  int il4;
246  int ih2;
247  int wd3;
248  int eh;
249  int mih;
250  int i;
251  int j;
252  /* Low and high band PCM from the QMF */
253  int xlow;
254  int xhigh;
255  int g722_bytes;
256  /* Even and odd tap accumulators */
257  int sumeven;
258  int sumodd;
259  int ihigh;
260  int ilow;
261  int code;
262 
263  g722_bytes = 0;
264  xhigh = 0;
265 
266  for (j = 0; j < len; )
267  {
268  if (state.itu_test_mode)
269  {
270  xhigh = amp[j++] >> 1;
271  xlow = xhigh;
272  }
273  else
274  {
275  if (state.eight_k)
276  {
277  xlow = amp[j++] >> 1;
278  }
279  else
280  {
281  /* Apply the transmit QMF */
282  /* Shuffle the buffer down */
283  for (i = 0; i < 22; i++) {
284  state.x[i] = state.x[i + 2];
285  }
286 
287  state.x[22] = amp[j++];
288  state.x[23] = amp[j++];
289 
290  /* Discard every other QMF output */
291  sumeven = 0;
292  sumodd = 0;
293 
294  for (i = 0; i < 12; i++)
295  {
296  sumodd += state.x[2*i]*qmf_coeffs[i];
297  sumeven += state.x[2*i + 1]*qmf_coeffs[11 - i];
298  }
299 
300  xlow = (sumeven + sumodd) >> 14;
301  xhigh = (sumeven - sumodd) >> 14;
302  }
303  }
304  /* Block 1L, SUBTRA */
305  el = saturate(xlow - state.band[0].s);
306 
307  /* Block 1L, QUANTL */
308  wd = (el >= 0) ? el : -(el + 1);
309 
310  for (i = 1; i < 30; i++)
311  {
312  wd1 = (q6[i]*state.band[0].det) >> 12;
313 
314  if (wd < wd1) {
315  break;
316  }
317  }
318 
319  ilow = (el < 0) ? iln[i] : ilp[i];
320 
321  /* Block 2L, INVQAL */
322  ril = ilow >> 2;
323  wd2 = qm4[ril];
324  dlow = (state.band[0].det*wd2) >> 15;
325 
326  /* Block 3L, LOGSCL */
327  il4 = rl42[ril];
328  wd = (state.band[0].nb*127) >> 7;
329  state.band[0].nb = wd + wl[il4];
330 
331  if (state.band[0].nb < 0) {
332  state.band[0].nb = 0;
333  } else if (state.band[0].nb > 18432) {
334  state.band[0].nb = 18432;
335  }
336 
337  /* Block 3L, SCALEL */
338  wd1 = (state.band[0].nb >> 6) & 31;
339  wd2 = 8 - (state.band[0].nb >> 11);
340  wd3 = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2);
341  state.band[0].det = wd3 << 2;
342 
343  block4(0, dlow);
344 
345  if (state.eight_k)
346  {
347  /* Just leave the high bits as zero */
348  code = (0xC0 | ilow) >> (8 - state.bits_per_sample);
349  }
350  else
351  {
352  /* Block 1H, SUBTRA */
353  eh = saturate(xhigh - state.band[1].s);
354 
355  /* Block 1H, QUANTH */
356  wd = (eh >= 0) ? eh : -(eh + 1);
357  wd1 = (564*state.band[1].det) >> 12;
358  mih = (wd >= wd1) ? 2 : 1;
359  ihigh = (eh < 0) ? ihn[mih] : ihp[mih];
360 
361  /* Block 2H, INVQAH */
362  wd2 = qm2[ihigh];
363  dhigh = (state.band[1].det*wd2) >> 15;
364 
365  /* Block 3H, LOGSCH */
366  ih2 = rh2[ihigh];
367  wd = (state.band[1].nb*127) >> 7;
368  state.band[1].nb = wd + wh[ih2];
369 
370  if (state.band[1].nb < 0) {
371  state.band[1].nb = 0;
372  } else if (state.band[1].nb > 22528) {
373  state.band[1].nb = 22528;
374  }
375 
376  /* Block 3H, SCALEH */
377  wd1 = (state.band[1].nb >> 6) & 31;
378  wd2 = 10 - (state.band[1].nb >> 11);
379  wd3 = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2);
380  state.band[1].det = wd3 << 2;
381 
382  block4(1, dhigh);
383  code = ((ihigh << 6) | ilow) >> (8 - state.bits_per_sample);
384  }
385 
386  if (state.packed)
387  {
388  /* Pack the code bits */
389  state.out_buffer |= (code << state.out_bits);
391 
392  if (state.out_bits >= 8)
393  {
394  g722_data[g722_bytes++] = (uint8_t) (state.out_buffer & 0xFF);
395  state.out_bits -= 8;
396  state.out_buffer >>= 8;
397  }
398  }
399  else
400  {
401  g722_data[g722_bytes++] = (uint8_t) code;
402  }
403  }
404 
405  return g722_bytes;
406 }
short int16_t
Definition: rtptypes_win.h:43
void block4(int band, int d)
Definition: audiog722.cpp:136
static const int ihn[3]
Definition: audiog722.h:105
static const int rh2[4]
Definition: audiog722.h:108
void init(int rate, int options)
Definition: audiog722.cpp:88
static const int qm2[4]
Definition: audiog722.h:103
static const int ilb[32]
Definition: audiog722.h:101
static const int qm4[16]
Definition: audiog722.h:102
unsigned char uint8_t
Definition: rtptypes_win.h:42
static const int qmf_coeffs[12]
Definition: audiog722.h:104
static const int q6[32]
Definition: audiog722.h:96
int32_t i
Definition: decimators.h:244
void init(int rate, int options)
Definition: audiog722.cpp:131
static const int wh[3]
Definition: audiog722.h:107
#define G722_PACKED
Definition: audiog722.cpp:23
static const int iln[32]
Definition: audiog722.h:97
struct AudioG722::g722_encode_state::@10 band[2]
g722_encode_state state
Definition: audiog722.h:94
#define G722_SAMPLE_RATE_8000
Definition: audiog722.cpp:22
static const int rl42[16]
Definition: audiog722.h:100
int encode(uint8_t g722_data[], const int16_t amp[], int len)
Definition: audiog722.cpp:236
int16_t saturate(int32_t amp)
Definition: audiog722.h:75
static const int wl[8]
Definition: audiog722.h:99
static const int ihp[3]
Definition: audiog722.h:106
static const int ilp[32]
Definition: audiog722.h:98