1 /*
2 * G.726 ADPCM audio codec
3 * Copyright (c) 2004 Roman Shaposhnik
4 *
5 * This is a very straightforward rendition of the G.726
6 * Section 4 "Computational Details".
7 *
8 * This file is part of FFmpeg.
9 *
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 #include "config_components.h"
26
28
37
38 /**
39 * G.726 11-bit float.
40 * G.726 Standard uses rather odd 11-bit floating point arithmetic for
41 * numerous occasions. It's a mystery to me why they did it this way
42 * instead of simply using 32-bit integer arithmetic.
43 */
45 uint8_t
sign;
/**< 1 bit sign */
46 uint8_t
exp;
/**< 4 bits exponent */
47 uint8_t
mant;
/**< 6 bits mantissa */
49
51 {
56 f->mant =
i? (i<<6) >>
f->exp : 1<<5;
58 }
59
61 {
63
65 res = (((f1->
mant * f2->
mant) + 0x30) >> 4);
66 res =
exp > 19 ? res << (
exp - 19) : res >> (19 -
exp);
67 return (f1->
sign ^ f2->
sign) ? -res : res;
68 }
69
71 {
72 return (
value < 0) ? -1 : 1;
73 }
74
76 const int*
quant;
/**< quantization table */
77 const int16_t*
iquant;
/**< inverse quantization table */
78 const int16_t*
W;
/**< special table #1 ;-) */
79 const uint8_t*
F;
/**< special table #2 */
81
85
88 int a[2];
/**< second order predictor coeffs */
89 int b[6];
/**< sixth order predictor coeffs */
90 int pk[2];
/**< signs of prev. 2 sez + dq */
91
92 int ap;
/**< scale factor control */
93 int yu;
/**< fast scale factor */
94 int yl;
/**< slow scale factor */
95 int dms;
/**< short average magnitude of F[i] */
96 int dml;
/**< long average magnitude of F[i] */
97 int td;
/**< tone detect */
98
99 int se;
/**< estimated signal for the next iteration */
100 int sez;
/**< estimated second order prediction */
101 int y;
/**< quantizer scaling factor for the next iteration */
103 int little_endian;
/**< little-endian bitstream as used in aiff and Sun AU */
105
106 static const int quant_tbl16[] =
/**< 16kbit/s 2 bits per sample */
107 { 260, INT_MAX };
109 { 116, 365, 365, 116 };
111 { -22, 439, 439, -22 };
113 { 0, 7, 7, 0 };
114
115 static const int quant_tbl24[] =
/**< 24kbit/s 3 bits per sample */
116 { 7, 217, 330, INT_MAX };
118 { INT16_MIN, 135, 273, 373, 373, 273, 135, INT16_MIN };
120 { -4, 30, 137, 582, 582, 137, 30, -4 };
122 { 0, 1, 2, 7, 7, 2, 1, 0 };
123
124 static const int quant_tbl32[] =
/**< 32kbit/s 4 bits per sample */
125 { -125, 79, 177, 245, 299, 348, 399, INT_MAX };
127 { INT16_MIN, 4, 135, 213, 273, 323, 373, 425,
128 425, 373, 323, 273, 213, 135, 4, INT16_MIN };
130 { -12, 18, 41, 64, 112, 198, 355, 1122,
131 1122, 355, 198, 112, 64, 41, 18, -12};
133 { 0, 0, 0, 1, 1, 1, 3, 7, 7, 3, 1, 1, 1, 0, 0, 0 };
134
135 static const int quant_tbl40[] =
/**< 40kbit/s 5 bits per sample */
136 { -122, -16, 67, 138, 197, 249, 297, 338,
137 377, 412, 444, 474, 501, 527, 552, INT_MAX };
139 { INT16_MIN, -66, 28, 104, 169, 224, 274, 318,
140 358, 395, 429, 459, 488, 514, 539, 566,
141 566, 539, 514, 488, 459, 429, 395, 358,
142 318, 274, 224, 169, 104, 28, -66, INT16_MIN };
144 { 14, 14, 24, 39, 40, 41, 58, 100,
145 141, 179, 219, 280, 358, 440, 529, 696,
146 696, 529, 440, 358, 280, 219, 179, 141,
147 100, 58, 41, 40, 39, 24, 14, 14 };
149 { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 6,
150 6, 6, 5, 4, 3, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
151
157
158
159 /**
160 * Paragraph 4.2.2 page 18: Adaptive quantizer.
161 */
163 {
164 int sign,
exp,
i, dln;
165
168 sign = 1;
170 }
172 dln = ((
exp<<7) + (((
d<<7)>>
exp)&0x7f)) - (
c->y>>2);
173
174 while (
c->tbls.quant[
i] < INT_MAX &&
c->tbls.quant[
i] < dln)
176
177 if (sign)
179 if (
c->code_size != 2 &&
i == 0)
/* I'm not sure this is a good idea */
181
183 }
184
185 /**
186 * Paragraph 4.2.3 page 22: Inverse adaptive quantizer.
187 */
189 {
191
192 dql =
c->tbls.iquant[
i] + (
c->y >> 2);
193 dex = (dql>>7) & 0
xf;
/* 4-bit exponent */
194 dqt = (1<<7) + (dql & 0x7f);
/* log2 -> linear */
195 return (dql < 0) ? 0 : ((dqt<<dex) >> 7);
196 }
197
199 {
200 int dq, re_signal, pk0, fa1,
i, tr, ylint, ylfrac, thr2, al, dq0;
202 int I_sig= I >> (
c->code_size - 1);
203
205
206 /* Transition detect */
207 ylint = (
c->yl >> 15);
208 ylfrac = (
c->yl >> 10) & 0x1f;
209 thr2 = (ylint > 9) ? 0x1f << 10 : (0x20 + ylfrac) << ylint;
210 tr= (
c->td == 1 && dq > ((3*thr2)>>2));
211
212 if (I_sig) /* get the sign */
213 dq = -dq;
214 re_signal = (int16_t)(
c->se + dq);
215
216 /* Update second order predictor coefficient A2 and A1 */
217 pk0 = (
c->sez + dq) ?
sgn(
c->sez + dq) : 0;
218 dq0 = dq ?
sgn(dq) : 0;
219 if (tr) {
224 } else {
225 /* This is a bit crazy, but it really is +255 not +256 */
227
228 c->a[1] += 128*pk0*
c->pk[1] + fa1 - (
c->a[1]>>7);
229 c->a[1] =
av_clip(
c->a[1], -12288, 12288);
230 c->a[0] += 64*3*pk0*
c->pk[0] - (
c->a[0] >> 8);
231 c->a[0] =
av_clip(
c->a[0], -(15360 -
c->a[1]), 15360 -
c->a[1]);
232
234 c->b[
i] += 128*dq0*
sgn(-
c->dq[
i].sign) - (
c->b[
i]>>8);
235 }
236
237 /* Update Dq and Sr and Pk */
239 c->pk[0] = pk0 ? pk0 : 1;
241 i2f(re_signal, &
c->sr[0]);
243 c->dq[
i] =
c->dq[
i-1];
245 c->dq[0].sign = I_sig;
/* Isn't it crazy ?!?! */
246
247 c->td =
c->a[1] < -11776;
248
249 /* Update Ap */
250 c->dms += (
c->tbls.F[I]<<4) + ((-
c->dms) >> 5);
251 c->dml += (
c->tbls.F[I]<<4) + ((-
c->dml) >> 7);
252 if (tr)
254 else {
255 c->ap += (-
c->ap) >> 4;
256 if (
c->y <= 1535 ||
c->td ||
abs((
c->dms << 2) -
c->dml) >= (
c->dml >> 3))
258 }
259
260 /* Update Yu and Yl */
261 c->yu =
av_clip(
c->y +
c->tbls.W[I] + ((-
c->y)>>5), 544, 5120);
262 c->yl +=
c->yu + ((-
c->yl)>>6);
263
264 /* Next iteration for Y */
265 al = (
c->ap >= 256) ? 1<<6 :
c->ap >> 2;
266 c->y = (
c->yl + (
c->yu - (
c->yl>>6))*al) >> 6;
267
268 /* Next iteration for SE and SEZ */
276
277 return av_clip(re_signal * 4, -0xffff, 0xffff);
278 }
279
281 {
283
285 for (
i=0;
i<2;
i++) {
286 c->sr[
i].mant = 1<<5;
288 }
289 for (
i=0;
i<6;
i++) {
290 c->dq[
i].mant = 1<<5;
291 }
294
296
297 return 0;
298 }
299
300 #if CONFIG_ADPCM_G726_ENCODER || CONFIG_ADPCM_G726LE_ENCODER
302 {
304
308 }
309
310 /* Interfacing to the libavcodec */
311
313 {
315
316 c->little_endian = !strcmp(avctx->
codec->
name,
"g726le");
317
321 "allowed when the compliance level is higher than unofficial. "
322 "Resample or reduce the compliance level.\n");
324 }
329 }
330
334 }
335
338
339 c->code_size =
av_clip(
c->code_size, 2, 5);
342
344
345 /* select a frame size that will end on a byte boundary and have a size of
346 approximately 1024 bytes */
347 avctx->
frame_size = ((
int[]){ 4096, 2736, 2048, 1640 })[
c->code_size - 2];
348
349 return 0;
350 }
351
354 {
356 const int16_t *
samples = (
const int16_t *)
frame->data[0];
359
364
365 for (
i = 0;
i <
frame->nb_samples;
i++)
366 if (
c->little_endian) {
368 } else {
370 }
371
372 if (
c->little_endian) {
374 } else {
376 }
377
378 *got_packet_ptr = 1;
379 return 0;
380 }
381
382 #define OFFSET(x) offsetof(G726Context, x)
383 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
387 };
388
389 static const AVClass g726_class = {
394 };
395
397 { "b", "0" },
399 };
400 #endif
401
402 #if CONFIG_ADPCM_G726_ENCODER
410 .
init = g726_encode_init,
414 .p.priv_class = &g726_class,
417 };
418 #endif
419
420 #if CONFIG_ADPCM_G726LE_ENCODER
428 .
init = g726_encode_init,
432 .p.priv_class = &g726_class,
435 };
436 #endif
437
438 #if CONFIG_ADPCM_G726_DECODER || CONFIG_ADPCM_G726LE_DECODER
440 {
442
446 }
449
450 c->little_endian = !strcmp(avctx->
codec->
name,
"g726le");
451
453 if (
c->code_size < 2 ||
c->code_size > 5) {
456 }
458
460
461 return 0;
462 }
463
465 int *got_frame_ptr,
AVPacket *avpkt)
466 {
467 const uint8_t *buf = avpkt->
data;
468 int buf_size = avpkt->
size;
472 int out_samples,
ret;
473
474 out_samples = buf_size * 8 /
c->code_size;
475
476 /* get output buffer */
477 frame->nb_samples = out_samples;
481
483
484 while (out_samples--)
488
491
492 *got_frame_ptr = 1;
493
494 return buf_size;
495 }
496
498 {
501 }
502 #endif
503
504 #if CONFIG_ADPCM_G726_DECODER
511 .
init = g726_decode_init,
513 .flush = g726_decode_flush,
516 };
517 #endif
518
519 #if CONFIG_ADPCM_G726LE_DECODER
525 .
init = g726_decode_init,
527 .flush = g726_decode_flush,
531 };
532 #endif