30
30
* as that of the covered work.
36
#include "../common.h"
37
33
#include "audiocodec.h"
34
#include "sfl_types.h"
39
#include "noncopyable.h"
45
40
class G722 : public sfl::AudioCodec {
48
G722(int payload=9) : sfl::AudioCodec(payload, "G722"),
49
decode_s(new g722_decode_state_t),
50
encode_s(new g722_encode_state_t) {
52
frameSize_ = 320; // samples, 20 ms at 16kHz
43
G722() : sfl::AudioCodec(9, "G722", 16000, 320, 1), decode_state_(),
55
46
hasDynamicPayload_ = false;
62
g722_decode_release();
63
g722_encode_release();
66
virtual int decode(short *dst, unsigned char *src, size_t buf_size) {
67
assert(buf_size == frameSize_ / sizeof(SFLDataFormat) * encode_s->bits_per_sample / 8);
68
return g722_decode((int16_t*) dst, (const uint8_t*) src, buf_size);
71
virtual int encode(unsigned char *dst, short *src, size_t buf_size) {
72
int out = g722_encode((uint8_t*) dst, (const int16_t*) src, frameSize_);
73
assert((size_t)out <= buf_size);
48
g722_state_init(decode_state_);
49
g722_state_init(encode_state_);
53
virtual int decode(SFLDataFormat *dst, unsigned char *src, size_t buf_size)
55
return g722_decode(dst, src, buf_size);
58
virtual int encode(unsigned char *dst, SFLDataFormat *src, size_t /*buf_size*/)
60
int out = g722_encode(dst, src, frameSize_);
78
void g722_encode_init() {
79
encode_s->itu_test_mode = false;
81
// 8 => 64 kbps; 7 => 56 kbps; 6 => 48 kbps
82
encode_s->bits_per_sample = 8;
84
// Enable 8khz mode, encode using lower subband only
85
encode_s->eight_k = false;
87
// Never set packed true when using 64 kbps
88
encode_s->packed = false;
90
memset(encode_s->band, 0, sizeof(decode_s->band));
91
encode_s->band[0].det = 32;
92
encode_s->band[1].det = 8;
94
memset(encode_s->x, 0, sizeof(encode_s->x));
96
decode_s->in_buffer = 0;
97
decode_s->in_bits = 0;
98
decode_s->out_buffer = 0;
99
decode_s->out_bits = 0;
102
void g722_decode_init() {
104
decode_s->itu_test_mode = false;
106
// 8 => 64 kbps; 7 => 56 kbps; 6 => 48 kbps
107
decode_s->bits_per_sample = 8;
109
// Enable 8khz mode, encode using lower subband only
110
decode_s->eight_k = false;
112
// Never set packed true when using 64 kbps
113
decode_s->packed = false;
115
memset(decode_s->band, 0, sizeof(decode_s->band));
116
decode_s->band[0].det = 32;
117
decode_s->band[1].det = 8;
119
decode_s->in_bits = 0;
121
memset(decode_s->x, 0, sizeof(decode_s->x));
123
decode_s->in_buffer = 0;
124
decode_s->in_bits = 0;
125
decode_s->out_buffer = 0;
126
decode_s->out_bits = 0;
129
int16_t saturate(int32_t amp) {
64
static void g722_state_init(g722_state_t &state)
66
state.itu_test_mode = false;
68
// 8 => 64 kbps; 7 => 56 kbps; 6 => 48 kbps
69
state.bits_per_sample = 8;
71
// Enable 8khz mode, encode using lower subband only
72
state.eight_k = false;
74
// Never set packed true when using 64 kbps
77
memset(state.band, 0, sizeof(state.band));
78
state.band[0].det = 32;
79
state.band[1].det = 8;
81
memset(state.x, 0, sizeof(state.x));
89
SFLDataFormat saturate(int32_t amp)
91
SFLDataFormat amp16 = 0;
132
93
/* Hopefully this is optimised for the common case - not clipping */
133
amp16 = (int16_t) amp;
94
amp16 = (SFLDataFormat) amp;
138
99
if (amp > INT16_MAX)
145
void block4_encode(int band, int d) {
105
void block4_encode(int band, int d)
151
112
/* Block 4, RECONS */
152
encode_s->band[band].d[0] = d;
153
encode_s->band[band].r[0] = saturate(encode_s->band[band].s + d);
113
encode_state_.band[band].d[0] = d;
114
encode_state_.band[band].r[0] = saturate(encode_state_.band[band].s + d);
155
116
/* Block 4, PARREC */
156
encode_s->band[band].p[0] = saturate(encode_s->band[band].sz + d);
117
encode_state_.band[band].p[0] = saturate(encode_state_.band[band].sz + d);
158
119
/* Block 4, UPPOL2 */
160
121
for (i = 0; i < 3; i++)
161
encode_s->band[band].sg[i] = encode_s->band[band].p[i] >> 15;
163
wd1 = saturate(encode_s->band[band].a[1] << 2);
165
wd2 = (encode_s->band[band].sg[0] == encode_s->band[band].sg[1]) ? -wd1 : wd1;
122
encode_state_.band[band].sg[i] = encode_state_.band[band].p[i] >> 15;
124
wd1 = saturate(encode_state_.band[band].a[1] << 2);
126
wd2 = (encode_state_.band[band].sg[0] == encode_state_.band[band].sg[1]) ? -wd1 : wd1;
170
wd3 = (wd2 >> 7) + ((encode_s->band[band].sg[0] == encode_s->band[band].sg[2]) ? 128 : -128);
131
wd3 = (wd2 >> 7) + ((encode_state_.band[band].sg[0] == encode_state_.band[band].sg[2]) ? 128 : -128);
172
wd3 += (encode_s->band[band].a[2]*32512) >> 15;
133
wd3 += (encode_state_.band[band].a[2]*32512) >> 15;
176
137
else if (wd3 < -12288)
179
encode_s->band[band].ap[2] = wd3;
140
encode_state_.band[band].ap[2] = wd3;
181
142
/* Block 4, UPPOL1 */
182
encode_s->band[band].sg[0] = encode_s->band[band].p[0] >> 15;
184
encode_s->band[band].sg[1] = encode_s->band[band].p[1] >> 15;
186
wd1 = (encode_s->band[band].sg[0] == encode_s->band[band].sg[1]) ? 192 : -192;
188
wd2 = (encode_s->band[band].a[1]*32640) >> 15;
190
encode_s->band[band].ap[1] = saturate(wd1 + wd2);
192
wd3 = saturate(15360 - encode_s->band[band].ap[2]);
194
if (encode_s->band[band].ap[1] > wd3)
195
encode_s->band[band].ap[1] = wd3;
196
else if (encode_s->band[band].ap[1] < -wd3)
197
encode_s->band[band].ap[1] = -wd3;
143
encode_state_.band[band].sg[0] = encode_state_.band[band].p[0] >> 15;
145
encode_state_.band[band].sg[1] = encode_state_.band[band].p[1] >> 15;
147
wd1 = (encode_state_.band[band].sg[0] == encode_state_.band[band].sg[1]) ? 192 : -192;
149
wd2 = (encode_state_.band[band].a[1]*32640) >> 15;
151
encode_state_.band[band].ap[1] = saturate(wd1 + wd2);
153
wd3 = saturate(15360 - encode_state_.band[band].ap[2]);
155
if (encode_state_.band[band].ap[1] > wd3)
156
encode_state_.band[band].ap[1] = wd3;
157
else if (encode_state_.band[band].ap[1] < -wd3)
158
encode_state_.band[band].ap[1] = -wd3;
199
160
/* Block 4, UPZERO */
200
161
wd1 = (d == 0) ? 0 : 128;
202
encode_s->band[band].sg[0] = d >> 15;
163
encode_state_.band[band].sg[0] = d >> 15;
204
165
for (i = 1; i < 7; i++) {
205
encode_s->band[band].sg[i] = encode_s->band[band].d[i] >> 15;
206
wd2 = (encode_s->band[band].sg[i] == encode_s->band[band].sg[0]) ? wd1 : -wd1;
207
wd3 = (encode_s->band[band].b[i]*32640) >> 15;
208
encode_s->band[band].bp[i] = saturate(wd2 + wd3);
166
encode_state_.band[band].sg[i] = encode_state_.band[band].d[i] >> 15;
167
wd2 = (encode_state_.band[band].sg[i] == encode_state_.band[band].sg[0]) ? wd1 : -wd1;
168
wd3 = (encode_state_.band[band].b[i]*32640) >> 15;
169
encode_state_.band[band].bp[i] = saturate(wd2 + wd3);
211
172
/* Block 4, DELAYA */
212
173
for (i = 6; i > 0; i--) {
213
encode_s->band[band].d[i] = encode_s->band[band].d[i - 1];
214
encode_s->band[band].b[i] = encode_s->band[band].bp[i];
174
encode_state_.band[band].d[i] = encode_state_.band[band].d[i - 1];
175
encode_state_.band[band].b[i] = encode_state_.band[band].bp[i];
217
178
for (i = 2; i > 0; i--) {
218
encode_s->band[band].r[i] = encode_s->band[band].r[i - 1];
219
encode_s->band[band].p[i] = encode_s->band[band].p[i - 1];
220
encode_s->band[band].a[i] = encode_s->band[band].ap[i];
179
encode_state_.band[band].r[i] = encode_state_.band[band].r[i - 1];
180
encode_state_.band[band].p[i] = encode_state_.band[band].p[i - 1];
181
encode_state_.band[band].a[i] = encode_state_.band[band].ap[i];
223
184
/* Block 4, FILTEP */
224
wd1 = saturate(encode_s->band[band].r[1] + encode_s->band[band].r[1]);
226
wd1 = (encode_s->band[band].a[1]*wd1) >> 15;
228
wd2 = saturate(encode_s->band[band].r[2] + encode_s->band[band].r[2]);
230
wd2 = (encode_s->band[band].a[2]*wd2) >> 15;
232
encode_s->band[band].sp = saturate(wd1 + wd2);
185
wd1 = saturate(encode_state_.band[band].r[1] + encode_state_.band[band].r[1]);
187
wd1 = (encode_state_.band[band].a[1]*wd1) >> 15;
189
wd2 = saturate(encode_state_.band[band].r[2] + encode_state_.band[band].r[2]);
191
wd2 = (encode_state_.band[band].a[2]*wd2) >> 15;
193
encode_state_.band[band].sp = saturate(wd1 + wd2);
234
195
/* Block 4, FILTEZ */
235
encode_s->band[band].sz = 0;
196
encode_state_.band[band].sz = 0;
237
198
for (i = 6; i > 0; i--) {
238
wd1 = saturate(encode_s->band[band].d[i] + encode_s->band[band].d[i]);
239
encode_s->band[band].sz += (encode_s->band[band].b[i]*wd1) >> 15;
199
wd1 = saturate(encode_state_.band[band].d[i] + encode_state_.band[band].d[i]);
200
encode_state_.band[band].sz += (encode_state_.band[band].b[i]*wd1) >> 15;
242
encode_s->band[band].sz = saturate(encode_s->band[band].sz);
203
encode_state_.band[band].sz = saturate(encode_state_.band[band].sz);
244
205
/* Block 4, PREDIC */
245
encode_s->band[band].s = saturate(encode_s->band[band].sp + encode_s->band[band].sz);
206
encode_state_.band[band].s = saturate(encode_state_.band[band].sp + encode_state_.band[band].sz);
249
void block4_decode(int band, int d) {
210
void block4_decode(int band, int d)
255
217
/* Block 4, RECONS */
256
decode_s->band[band].d[0] = d;
257
decode_s->band[band].r[0] = saturate(decode_s->band[band].s + d);
218
decode_state_.band[band].d[0] = d;
219
decode_state_.band[band].r[0] = saturate(decode_state_.band[band].s + d);
259
221
/* Block 4, PARREC */
260
decode_s->band[band].p[0] = saturate(decode_s->band[band].sz + d);
222
decode_state_.band[band].p[0] = saturate(decode_state_.band[band].sz + d);
262
224
/* Block 4, UPPOL2 */
264
226
for (i = 0; i < 3; i++)
265
decode_s->band[band].sg[i] = decode_s->band[band].p[i] >> 15;
267
wd1 = saturate(decode_s->band[band].a[1] << 2);
269
wd2 = (decode_s->band[band].sg[0] == decode_s->band[band].sg[1]) ? -wd1 : wd1;
227
decode_state_.band[band].sg[i] = decode_state_.band[band].p[i] >> 15;
229
wd1 = saturate(decode_state_.band[band].a[1] << 2);
231
wd2 = (decode_state_.band[band].sg[0] == decode_state_.band[band].sg[1]) ? -wd1 : wd1;
274
wd3 = (decode_s->band[band].sg[0] == decode_s->band[band].sg[2]) ? 128 : -128;
236
wd3 = (decode_state_.band[band].sg[0] == decode_state_.band[band].sg[2]) ? 128 : -128;
276
238
wd3 += (wd2 >> 7);
278
wd3 += (decode_s->band[band].a[2]*32512) >> 15;
240
wd3 += (decode_state_.band[band].a[2]*32512) >> 15;
282
244
else if (wd3 < -12288)
285
decode_s->band[band].ap[2] = wd3;
247
decode_state_.band[band].ap[2] = wd3;
287
249
/* Block 4, UPPOL1 */
288
decode_s->band[band].sg[0] = decode_s->band[band].p[0] >> 15;
290
decode_s->band[band].sg[1] = decode_s->band[band].p[1] >> 15;
292
wd1 = (decode_s->band[band].sg[0] == decode_s->band[band].sg[1]) ? 192 : -192;
294
wd2 = (decode_s->band[band].a[1]*32640) >> 15;
296
decode_s->band[band].ap[1] = saturate(wd1 + wd2);
298
wd3 = saturate(15360 - decode_s->band[band].ap[2]);
300
if (decode_s->band[band].ap[1] > wd3)
301
decode_s->band[band].ap[1] = wd3;
302
else if (decode_s->band[band].ap[1] < -wd3)
303
decode_s->band[band].ap[1] = -wd3;
250
decode_state_.band[band].sg[0] = decode_state_.band[band].p[0] >> 15;
252
decode_state_.band[band].sg[1] = decode_state_.band[band].p[1] >> 15;
254
wd1 = (decode_state_.band[band].sg[0] == decode_state_.band[band].sg[1]) ? 192 : -192;
256
wd2 = (decode_state_.band[band].a[1]*32640) >> 15;
258
decode_state_.band[band].ap[1] = saturate(wd1 + wd2);
260
wd3 = saturate(15360 - decode_state_.band[band].ap[2]);
262
if (decode_state_.band[band].ap[1] > wd3)
263
decode_state_.band[band].ap[1] = wd3;
264
else if (decode_state_.band[band].ap[1] < -wd3)
265
decode_state_.band[band].ap[1] = -wd3;
305
267
/* Block 4, UPZERO */
306
268
wd1 = (d == 0) ? 0 : 128;
308
decode_s->band[band].sg[0] = d >> 15;
270
decode_state_.band[band].sg[0] = d >> 15;
310
272
for (i = 1; i < 7; i++) {
311
decode_s->band[band].sg[i] = decode_s->band[band].d[i] >> 15;
312
wd2 = (decode_s->band[band].sg[i] == decode_s->band[band].sg[0]) ? wd1 : -wd1;
313
wd3 = (decode_s->band[band].b[i]*32640) >> 15;
314
decode_s->band[band].bp[i] = saturate(wd2 + wd3);
273
decode_state_.band[band].sg[i] = decode_state_.band[band].d[i] >> 15;
274
wd2 = (decode_state_.band[band].sg[i] == decode_state_.band[band].sg[0]) ? wd1 : -wd1;
275
wd3 = (decode_state_.band[band].b[i]*32640) >> 15;
276
decode_state_.band[band].bp[i] = saturate(wd2 + wd3);
317
279
/* Block 4, DELAYA */
318
280
for (i = 6; i > 0; i--) {
319
decode_s->band[band].d[i] = decode_s->band[band].d[i - 1];
320
decode_s->band[band].b[i] = decode_s->band[band].bp[i];
281
decode_state_.band[band].d[i] = decode_state_.band[band].d[i - 1];
282
decode_state_.band[band].b[i] = decode_state_.band[band].bp[i];
323
285
for (i = 2; i > 0; i--) {
324
decode_s->band[band].r[i] = decode_s->band[band].r[i - 1];
325
decode_s->band[band].p[i] = decode_s->band[band].p[i - 1];
326
decode_s->band[band].a[i] = decode_s->band[band].ap[i];
286
decode_state_.band[band].r[i] = decode_state_.band[band].r[i - 1];
287
decode_state_.band[band].p[i] = decode_state_.band[band].p[i - 1];
288
decode_state_.band[band].a[i] = decode_state_.band[band].ap[i];
329
291
/* Block 4, FILTEP */
330
wd1 = saturate(decode_s->band[band].r[1] + decode_s->band[band].r[1]);
332
wd1 = (decode_s->band[band].a[1]*wd1) >> 15;
334
wd2 = saturate(decode_s->band[band].r[2] + decode_s->band[band].r[2]);
336
wd2 = (decode_s->band[band].a[2]*wd2) >> 15;
338
decode_s->band[band].sp = saturate(wd1 + wd2);
292
wd1 = saturate(decode_state_.band[band].r[1] + decode_state_.band[band].r[1]);
294
wd1 = (decode_state_.band[band].a[1]*wd1) >> 15;
296
wd2 = saturate(decode_state_.band[band].r[2] + decode_state_.band[band].r[2]);
298
wd2 = (decode_state_.band[band].a[2]*wd2) >> 15;
300
decode_state_.band[band].sp = saturate(wd1 + wd2);
340
302
/* Block 4, FILTEZ */
341
decode_s->band[band].sz = 0;
303
decode_state_.band[band].sz = 0;
343
305
for (i = 6; i > 0; i--) {
344
wd1 = saturate(decode_s->band[band].d[i] + decode_s->band[band].d[i]);
345
decode_s->band[band].sz += (decode_s->band[band].b[i]*wd1) >> 15;
306
wd1 = saturate(decode_state_.band[band].d[i] + decode_state_.band[band].d[i]);
307
decode_state_.band[band].sz += (decode_state_.band[band].b[i]*wd1) >> 15;
348
decode_s->band[band].sz = saturate(decode_s->band[band].sz);
310
decode_state_.band[band].sz = saturate(decode_state_.band[band].sz);
350
312
/* Block 4, PREDIC */
351
decode_s->band[band].s = saturate(decode_s->band[band].sp + decode_s->band[band].sz);
354
int g722_encode_release() {
361
int g722_decode_release() {
367
int g722_decode(int16_t amp[], const uint8_t g722_data[], int len) {
313
decode_state_.band[band].s = saturate(decode_state_.band[band].sp + decode_state_.band[band].sz);
316
int g722_decode(SFLDataFormat amp[], const uint8_t g722_data[], int len)
368
318
static const int wl[8] = {-60, -30, 58, 172, 334, 538, 1198, 3042 };
369
319
static const int rl42[16] = {0, 7, 6, 5, 4, 3, 2, 1, 7, 6, 5, 4, 3, 2, 1, 0 };
370
320
static const int ilb[32] = {
719
670
/* Block 2L, INVQAL */
722
dlow = (encode_s->band[0].det*wd2) >> 15;
673
dlow = (encode_state_.band[0].det*wd2) >> 15;
724
675
/* Block 3L, LOGSCL */
726
wd = (encode_s->band[0].nb*127) >> 7;
727
encode_s->band[0].nb = wd + wl[il4];
677
wd = (encode_state_.band[0].nb*127) >> 7;
678
encode_state_.band[0].nb = wd + wl[il4];
729
if (encode_s->band[0].nb < 0)
730
encode_s->band[0].nb = 0;
731
else if (encode_s->band[0].nb > 18432)
732
encode_s->band[0].nb = 18432;
680
if (encode_state_.band[0].nb < 0)
681
encode_state_.band[0].nb = 0;
682
else if (encode_state_.band[0].nb > 18432)
683
encode_state_.band[0].nb = 18432;
734
685
/* Block 3L, SCALEL */
735
wd1 = (encode_s->band[0].nb >> 6) & 31;
686
wd1 = (encode_state_.band[0].nb >> 6) & 31;
737
wd2 = 8 - (encode_s->band[0].nb >> 11);
688
wd2 = 8 - (encode_state_.band[0].nb >> 11);
739
690
wd3 = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2);
741
encode_s->band[0].det = wd3 << 2;
692
encode_state_.band[0].det = wd3 << 2;
743
694
block4_encode(0, dlow);
745
if (encode_s->eight_k) {
696
if (encode_state_.eight_k) {
746
697
/* Just leave the high bits as zero */
747
code = (0xC0 | ilow) >> (8 - encode_s->bits_per_sample);
698
code = (0xC0 | ilow) >> (8 - encode_state_.bits_per_sample);
749
700
/* Block 1H, SUBTRA */
750
eh = saturate(xhigh - encode_s->band[1].s);
701
eh = saturate(xhigh - encode_state_.band[1].s);
752
703
/* Block 1H, QUANTH */
753
704
wd = (eh >= 0) ? eh : - (eh + 1);
754
wd1 = (564*encode_s->band[1].det) >> 12;
705
wd1 = (564*encode_state_.band[1].det) >> 12;
755
706
mih = (wd >= wd1) ? 2 : 1;
756
707
ihigh = (eh < 0) ? ihn[mih] : ihp[mih];
758
709
/* Block 2H, INVQAH */
759
710
wd2 = qm2[ihigh];
760
dhigh = (encode_s->band[1].det*wd2) >> 15;
711
dhigh = (encode_state_.band[1].det*wd2) >> 15;
762
713
/* Block 3H, LOGSCH */
763
714
ih2 = rh2[ihigh];
764
wd = (encode_s->band[1].nb*127) >> 7;
765
encode_s->band[1].nb = wd + wh[ih2];
715
wd = (encode_state_.band[1].nb*127) >> 7;
716
encode_state_.band[1].nb = wd + wh[ih2];
767
if (encode_s->band[1].nb < 0)
768
encode_s->band[1].nb = 0;
769
else if (encode_s->band[1].nb > 22528)
770
encode_s->band[1].nb = 22528;
718
if (encode_state_.band[1].nb < 0)
719
encode_state_.band[1].nb = 0;
720
else if (encode_state_.band[1].nb > 22528)
721
encode_state_.band[1].nb = 22528;
772
723
/* Block 3H, SCALEH */
773
wd1 = (encode_s->band[1].nb >> 6) & 31;
724
wd1 = (encode_state_.band[1].nb >> 6) & 31;
775
wd2 = 10 - (encode_s->band[1].nb >> 11);
726
wd2 = 10 - (encode_state_.band[1].nb >> 11);
777
728
wd3 = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2);
779
encode_s->band[1].det = wd3 << 2;
730
encode_state_.band[1].det = wd3 << 2;
781
732
block4_encode(1, dhigh);
783
code = ((ihigh << 6) | ilow) >> (8 - encode_s->bits_per_sample);
734
code = ((ihigh << 6) | ilow) >> (8 - encode_state_.bits_per_sample);
786
if (encode_s->packed) {
737
if (encode_state_.packed) {
787
738
/* Pack the code bits */
788
encode_s->out_buffer |= (code << encode_s->out_bits);
789
encode_s->out_bits += encode_s->bits_per_sample;
739
encode_state_.out_buffer |= (code << encode_state_.out_bits);
740
encode_state_.out_bits += encode_state_.bits_per_sample;
791
if (encode_s->out_bits >= 8) {
792
g722_data[g722_bytes++] = (uint8_t)(encode_s->out_buffer & 0xFF);
793
encode_s->out_bits -= 8;
794
encode_s->out_buffer >>= 8;
742
if (encode_state_.out_bits >= 8) {
743
g722_data[g722_bytes++] = (uint8_t)(encode_state_.out_buffer & 0xFF);
744
encode_state_.out_bits -= 8;
745
encode_state_.out_buffer >>= 8;
797
748
g722_data[g722_bytes++] = (uint8_t) code;