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.
Classes | Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
AudioG722 Class Reference

#include <audiog722.h>

+ Collaboration diagram for AudioG722:

Classes

struct  g722_encode_state
 

Public Member Functions

 AudioG722 ()
 
 ~AudioG722 ()
 
void init (int rate, int options)
 
int encode (uint8_t g722_data[], const int16_t amp[], int len)
 

Private Member Functions

int16_t saturate (int32_t amp)
 
void block4 (int band, int d)
 

Private Attributes

g722_encode_state state
 

Static Private Attributes

static const int q6 [32]
 
static const int iln [32]
 
static const int ilp [32]
 
static const int wl [8]
 
static const int rl42 [16]
 
static const int ilb [32]
 
static const int qm4 [16]
 
static const int qm2 [4]
 
static const int qmf_coeffs [12]
 
static const int ihn [3] = {0, 1, 0}
 
static const int ihp [3] = {0, 3, 2}
 
static const int wh [3] = {0, -214, 798}
 
static const int rh2 [4] = {2, 1, 2, 1}
 

Detailed Description

Definition at line 25 of file audiog722.h.

Constructor & Destructor Documentation

◆ AudioG722()

AudioG722::AudioG722 ( )

Definition at line 125 of file audiog722.cpp.

126 {}

◆ ~AudioG722()

AudioG722::~AudioG722 ( )

Definition at line 128 of file audiog722.cpp.

129 {}

Member Function Documentation

◆ block4()

void AudioG722::block4 ( int  band,
int  d 
)
private

Definition at line 136 of file audiog722.cpp.

References AudioG722::g722_encode_state::a, AudioG722::g722_encode_state::ap, AudioG722::g722_encode_state::b, AudioG722::g722_encode_state::band, AudioG722::g722_encode_state::bp, AudioG722::g722_encode_state::d, i, AudioG722::g722_encode_state::p, AudioG722::g722_encode_state::r, AudioG722::g722_encode_state::s, saturate(), AudioG722::g722_encode_state::sg, AudioG722::g722_encode_state::sp, state, and AudioG722::g722_encode_state::sz.

Referenced by encode().

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 }
int32_t i
Definition: decimators.h:244
struct AudioG722::g722_encode_state::@10 band[2]
g722_encode_state state
Definition: audiog722.h:94
int16_t saturate(int32_t amp)
Definition: audiog722.h:75
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ encode()

int AudioG722::encode ( uint8_t  g722_data[],
const int16_t  amp[],
int  len 
)

Definition at line 236 of file audiog722.cpp.

References AudioG722::g722_encode_state::band, AudioG722::g722_encode_state::bits_per_sample, block4(), AudioG722::g722_encode_state::det, AudioG722::g722_encode_state::eight_k, i, ihn, ihp, ilb, iln, ilp, AudioG722::g722_encode_state::itu_test_mode, AudioG722::g722_encode_state::nb, AudioG722::g722_encode_state::out_bits, AudioG722::g722_encode_state::out_buffer, AudioG722::g722_encode_state::packed, q6, qm2, qm4, qmf_coeffs, rh2, rl42, AudioG722::g722_encode_state::s, saturate(), state, wh, wl, and AudioG722::g722_encode_state::x.

Referenced by AudioNetSink::write().

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 }
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
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
static const int wh[3]
Definition: audiog722.h:107
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
static const int rl42[16]
Definition: audiog722.h:100
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
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ init()

void AudioG722::init ( int  rate,
int  options 
)

Definition at line 131 of file audiog722.cpp.

References AudioG722::g722_encode_state::init(), and state.

132 {
133  state.init(rate, options);
134 }
void init(int rate, int options)
Definition: audiog722.cpp:88
g722_encode_state state
Definition: audiog722.h:94
+ Here is the call graph for this function:

◆ saturate()

int16_t AudioG722::saturate ( int32_t  amp)
inlineprivate

Definition at line 75 of file audiog722.h.

Referenced by block4(), and encode().

76  {
77  int16_t amp16;
78 
79  /* Hopefully this is optimized for the common case - not clipping */
80  amp16 = (int16_t) amp;
81  if (amp == amp16) {
82  return amp16;
83  }
84 
85  if (amp > INT16_MAX) {
86  return INT16_MAX;
87  }
88 
89  return INT16_MIN;
90  }
short int16_t
Definition: rtptypes_win.h:43
+ Here is the caller graph for this function:

Member Data Documentation

◆ ihn

const int AudioG722::ihn = {0, 1, 0}
staticprivate

Definition at line 105 of file audiog722.h.

Referenced by encode().

◆ ihp

const int AudioG722::ihp = {0, 3, 2}
staticprivate

Definition at line 106 of file audiog722.h.

Referenced by encode().

◆ ilb

const int AudioG722::ilb
staticprivate
Initial value:
=
{
2048, 2093, 2139, 2186, 2233, 2282, 2332,
2383, 2435, 2489, 2543, 2599, 2656, 2714,
2774, 2834, 2896, 2960, 3025, 3091, 3158,
3228, 3298, 3371, 3444, 3520, 3597, 3676,
3756, 3838, 3922, 4008
}

Definition at line 101 of file audiog722.h.

Referenced by encode().

◆ iln

const int AudioG722::iln
staticprivate
Initial value:
=
{
0, 63, 62, 31, 30, 29, 28, 27,
26, 25, 24, 23, 22, 21, 20, 19,
18, 17, 16, 15, 14, 13, 12, 11,
10, 9, 8, 7, 6, 5, 4, 0
}

Definition at line 97 of file audiog722.h.

Referenced by encode().

◆ ilp

const int AudioG722::ilp
staticprivate
Initial value:
=
{
0, 61, 60, 59, 58, 57, 56, 55,
54, 53, 52, 51, 50, 49, 48, 47,
46, 45, 44, 43, 42, 41, 40, 39,
38, 37, 36, 35, 34, 33, 32, 0
}

Definition at line 98 of file audiog722.h.

Referenced by encode().

◆ q6

const int AudioG722::q6
staticprivate
Initial value:
=
{
0, 35, 72, 110, 150, 190, 233, 276,
323, 370, 422, 473, 530, 587, 650, 714,
786, 858, 940, 1023, 1121, 1219, 1339, 1458,
1612, 1765, 1980, 2195, 2557, 2919, 0, 0
}

Definition at line 96 of file audiog722.h.

Referenced by encode().

◆ qm2

const int AudioG722::qm2
staticprivate
Initial value:
=
{
-7408, -1616, 7408, 1616
}

Definition at line 103 of file audiog722.h.

Referenced by encode().

◆ qm4

const int AudioG722::qm4
staticprivate
Initial value:
=
{
0, -20456, -12896, -8968,
-6288, -4240, -2584, -1200,
20456, 12896, 8968, 6288,
4240, 2584, 1200, 0
}

Definition at line 102 of file audiog722.h.

Referenced by encode().

◆ qmf_coeffs

const int AudioG722::qmf_coeffs
staticprivate
Initial value:
=
{
3, -11, 12, 32, -210, 951, 3876, -805, 362, -156, 53, -11,
}

Definition at line 104 of file audiog722.h.

Referenced by encode().

◆ rh2

const int AudioG722::rh2 = {2, 1, 2, 1}
staticprivate

Definition at line 108 of file audiog722.h.

Referenced by encode().

◆ rl42

const int AudioG722::rl42
staticprivate
Initial value:
=
{
0, 7, 6, 5, 4, 3, 2, 1, 7, 6, 5, 4, 3, 2, 1, 0
}

Definition at line 100 of file audiog722.h.

Referenced by encode().

◆ state

g722_encode_state AudioG722::state
private

Definition at line 94 of file audiog722.h.

Referenced by block4(), encode(), and init().

◆ wh

const int AudioG722::wh = {0, -214, 798}
staticprivate

Definition at line 107 of file audiog722.h.

Referenced by encode().

◆ wl

const int AudioG722::wl
staticprivate
Initial value:
=
{
-60, -30, 58, 172, 334, 538, 1198, 3042
}

Definition at line 99 of file audiog722.h.

Referenced by encode().


The documentation for this class was generated from the following files: