00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <limits.h>
00025 #include "libavutil/avassert.h"
00026 #include "libavutil/opt.h"
00027 #include "avcodec.h"
00028 #include "internal.h"
00029 #include "get_bits.h"
00030 #include "put_bits.h"
00031
00038 typedef struct Float11 {
00039 uint8_t sign;
00040 uint8_t exp;
00041 uint8_t mant;
00042 } Float11;
00043
00044 static inline Float11* i2f(int i, Float11* f)
00045 {
00046 f->sign = (i < 0);
00047 if (f->sign)
00048 i = -i;
00049 f->exp = av_log2_16bit(i) + !!i;
00050 f->mant = i? (i<<6) >> f->exp : 1<<5;
00051 return f;
00052 }
00053
00054 static inline int16_t mult(Float11* f1, Float11* f2)
00055 {
00056 int res, exp;
00057
00058 exp = f1->exp + f2->exp;
00059 res = (((f1->mant * f2->mant) + 0x30) >> 4);
00060 res = exp > 19 ? res << (exp - 19) : res >> (19 - exp);
00061 return (f1->sign ^ f2->sign) ? -res : res;
00062 }
00063
00064 static inline int sgn(int value)
00065 {
00066 return (value < 0) ? -1 : 1;
00067 }
00068
00069 typedef struct G726Tables {
00070 const int* quant;
00071 const int16_t* iquant;
00072 const int16_t* W;
00073 const uint8_t* F;
00074 } G726Tables;
00075
00076 typedef struct G726Context {
00077 AVClass *class;
00078 AVFrame frame;
00079 G726Tables tbls;
00081 Float11 sr[2];
00082 Float11 dq[6];
00083 int a[2];
00084 int b[6];
00085 int pk[2];
00087 int ap;
00088 int yu;
00089 int yl;
00090 int dms;
00091 int dml;
00092 int td;
00094 int se;
00095 int sez;
00096 int y;
00097 int code_size;
00098 } G726Context;
00099
00100 static const int quant_tbl16[] =
00101 { 260, INT_MAX };
00102 static const int16_t iquant_tbl16[] =
00103 { 116, 365, 365, 116 };
00104 static const int16_t W_tbl16[] =
00105 { -22, 439, 439, -22 };
00106 static const uint8_t F_tbl16[] =
00107 { 0, 7, 7, 0 };
00108
00109 static const int quant_tbl24[] =
00110 { 7, 217, 330, INT_MAX };
00111 static const int16_t iquant_tbl24[] =
00112 { INT16_MIN, 135, 273, 373, 373, 273, 135, INT16_MIN };
00113 static const int16_t W_tbl24[] =
00114 { -4, 30, 137, 582, 582, 137, 30, -4 };
00115 static const uint8_t F_tbl24[] =
00116 { 0, 1, 2, 7, 7, 2, 1, 0 };
00117
00118 static const int quant_tbl32[] =
00119 { -125, 79, 177, 245, 299, 348, 399, INT_MAX };
00120 static const int16_t iquant_tbl32[] =
00121 { INT16_MIN, 4, 135, 213, 273, 323, 373, 425,
00122 425, 373, 323, 273, 213, 135, 4, INT16_MIN };
00123 static const int16_t W_tbl32[] =
00124 { -12, 18, 41, 64, 112, 198, 355, 1122,
00125 1122, 355, 198, 112, 64, 41, 18, -12};
00126 static const uint8_t F_tbl32[] =
00127 { 0, 0, 0, 1, 1, 1, 3, 7, 7, 3, 1, 1, 1, 0, 0, 0 };
00128
00129 static const int quant_tbl40[] =
00130 { -122, -16, 67, 138, 197, 249, 297, 338,
00131 377, 412, 444, 474, 501, 527, 552, INT_MAX };
00132 static const int16_t iquant_tbl40[] =
00133 { INT16_MIN, -66, 28, 104, 169, 224, 274, 318,
00134 358, 395, 429, 459, 488, 514, 539, 566,
00135 566, 539, 514, 488, 459, 429, 395, 358,
00136 318, 274, 224, 169, 104, 28, -66, INT16_MIN };
00137 static const int16_t W_tbl40[] =
00138 { 14, 14, 24, 39, 40, 41, 58, 100,
00139 141, 179, 219, 280, 358, 440, 529, 696,
00140 696, 529, 440, 358, 280, 219, 179, 141,
00141 100, 58, 41, 40, 39, 24, 14, 14 };
00142 static const uint8_t F_tbl40[] =
00143 { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 6,
00144 6, 6, 5, 4, 3, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
00145
00146 static const G726Tables G726Tables_pool[] =
00147 {{ quant_tbl16, iquant_tbl16, W_tbl16, F_tbl16 },
00148 { quant_tbl24, iquant_tbl24, W_tbl24, F_tbl24 },
00149 { quant_tbl32, iquant_tbl32, W_tbl32, F_tbl32 },
00150 { quant_tbl40, iquant_tbl40, W_tbl40, F_tbl40 }};
00151
00152
00156 static inline uint8_t quant(G726Context* c, int d)
00157 {
00158 int sign, exp, i, dln;
00159
00160 sign = i = 0;
00161 if (d < 0) {
00162 sign = 1;
00163 d = -d;
00164 }
00165 exp = av_log2_16bit(d);
00166 dln = ((exp<<7) + (((d<<7)>>exp)&0x7f)) - (c->y>>2);
00167
00168 while (c->tbls.quant[i] < INT_MAX && c->tbls.quant[i] < dln)
00169 ++i;
00170
00171 if (sign)
00172 i = ~i;
00173 if (c->code_size != 2 && i == 0)
00174 i = 0xff;
00175
00176 return i;
00177 }
00178
00182 static inline int16_t inverse_quant(G726Context* c, int i)
00183 {
00184 int dql, dex, dqt;
00185
00186 dql = c->tbls.iquant[i] + (c->y >> 2);
00187 dex = (dql>>7) & 0xf;
00188 dqt = (1<<7) + (dql & 0x7f);
00189 return (dql < 0) ? 0 : ((dqt<<dex) >> 7);
00190 }
00191
00192 static int16_t g726_decode(G726Context* c, int I)
00193 {
00194 int dq, re_signal, pk0, fa1, i, tr, ylint, ylfrac, thr2, al, dq0;
00195 Float11 f;
00196 int I_sig= I >> (c->code_size - 1);
00197
00198 dq = inverse_quant(c, I);
00199
00200
00201 ylint = (c->yl >> 15);
00202 ylfrac = (c->yl >> 10) & 0x1f;
00203 thr2 = (ylint > 9) ? 0x1f << 10 : (0x20 + ylfrac) << ylint;
00204 tr= (c->td == 1 && dq > ((3*thr2)>>2));
00205
00206 if (I_sig)
00207 dq = -dq;
00208 re_signal = c->se + dq;
00209
00210
00211 pk0 = (c->sez + dq) ? sgn(c->sez + dq) : 0;
00212 dq0 = dq ? sgn(dq) : 0;
00213 if (tr) {
00214 c->a[0] = 0;
00215 c->a[1] = 0;
00216 for (i=0; i<6; i++)
00217 c->b[i] = 0;
00218 } else {
00219
00220 fa1 = av_clip((-c->a[0]*c->pk[0]*pk0)>>5, -256, 255);
00221
00222 c->a[1] += 128*pk0*c->pk[1] + fa1 - (c->a[1]>>7);
00223 c->a[1] = av_clip(c->a[1], -12288, 12288);
00224 c->a[0] += 64*3*pk0*c->pk[0] - (c->a[0] >> 8);
00225 c->a[0] = av_clip(c->a[0], -(15360 - c->a[1]), 15360 - c->a[1]);
00226
00227 for (i=0; i<6; i++)
00228 c->b[i] += 128*dq0*sgn(-c->dq[i].sign) - (c->b[i]>>8);
00229 }
00230
00231
00232 c->pk[1] = c->pk[0];
00233 c->pk[0] = pk0 ? pk0 : 1;
00234 c->sr[1] = c->sr[0];
00235 i2f(re_signal, &c->sr[0]);
00236 for (i=5; i>0; i--)
00237 c->dq[i] = c->dq[i-1];
00238 i2f(dq, &c->dq[0]);
00239 c->dq[0].sign = I_sig;
00240
00241 c->td = c->a[1] < -11776;
00242
00243
00244 c->dms += (c->tbls.F[I]<<4) + ((- c->dms) >> 5);
00245 c->dml += (c->tbls.F[I]<<4) + ((- c->dml) >> 7);
00246 if (tr)
00247 c->ap = 256;
00248 else {
00249 c->ap += (-c->ap) >> 4;
00250 if (c->y <= 1535 || c->td || abs((c->dms << 2) - c->dml) >= (c->dml >> 3))
00251 c->ap += 0x20;
00252 }
00253
00254
00255 c->yu = av_clip(c->y + c->tbls.W[I] + ((-c->y)>>5), 544, 5120);
00256 c->yl += c->yu + ((-c->yl)>>6);
00257
00258
00259 al = (c->ap >= 256) ? 1<<6 : c->ap >> 2;
00260 c->y = (c->yl + (c->yu - (c->yl>>6))*al) >> 6;
00261
00262
00263 c->se = 0;
00264 for (i=0; i<6; i++)
00265 c->se += mult(i2f(c->b[i] >> 2, &f), &c->dq[i]);
00266 c->sez = c->se >> 1;
00267 for (i=0; i<2; i++)
00268 c->se += mult(i2f(c->a[i] >> 2, &f), &c->sr[i]);
00269 c->se >>= 1;
00270
00271 return av_clip(re_signal << 2, -0xffff, 0xffff);
00272 }
00273
00274 static av_cold int g726_reset(G726Context *c)
00275 {
00276 int i;
00277
00278 c->tbls = G726Tables_pool[c->code_size - 2];
00279 for (i=0; i<2; i++) {
00280 c->sr[i].mant = 1<<5;
00281 c->pk[i] = 1;
00282 }
00283 for (i=0; i<6; i++) {
00284 c->dq[i].mant = 1<<5;
00285 }
00286 c->yu = 544;
00287 c->yl = 34816;
00288
00289 c->y = 544;
00290
00291 return 0;
00292 }
00293
00294 #if CONFIG_ADPCM_G726_ENCODER
00295 static int16_t g726_encode(G726Context* c, int16_t sig)
00296 {
00297 uint8_t i;
00298
00299 i = quant(c, sig/4 - c->se) & ((1<<c->code_size) - 1);
00300 g726_decode(c, i);
00301 return i;
00302 }
00303
00304
00305
00306 static av_cold int g726_encode_init(AVCodecContext *avctx)
00307 {
00308 G726Context* c = avctx->priv_data;
00309
00310 if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL &&
00311 avctx->sample_rate != 8000) {
00312 av_log(avctx, AV_LOG_ERROR, "Sample rates other than 8kHz are not "
00313 "allowed when the compliance level is higher than unofficial. "
00314 "Resample or reduce the compliance level.\n");
00315 return AVERROR(EINVAL);
00316 }
00317 av_assert0(avctx->sample_rate > 0);
00318
00319 if(avctx->channels != 1){
00320 av_log(avctx, AV_LOG_ERROR, "Only mono is supported\n");
00321 return AVERROR(EINVAL);
00322 }
00323
00324 if (avctx->bit_rate)
00325 c->code_size = (avctx->bit_rate + avctx->sample_rate/2) / avctx->sample_rate;
00326
00327 c->code_size = av_clip(c->code_size, 2, 5);
00328 avctx->bit_rate = c->code_size * avctx->sample_rate;
00329 avctx->bits_per_coded_sample = c->code_size;
00330
00331 g726_reset(c);
00332
00333 avctx->coded_frame = avcodec_alloc_frame();
00334 if (!avctx->coded_frame)
00335 return AVERROR(ENOMEM);
00336 avctx->coded_frame->key_frame = 1;
00337
00338
00339
00340 avctx->frame_size = ((int[]){ 4096, 2736, 2048, 1640 })[c->code_size - 2];
00341
00342 return 0;
00343 }
00344
00345 static av_cold int g726_encode_close(AVCodecContext *avctx)
00346 {
00347 av_freep(&avctx->coded_frame);
00348 return 0;
00349 }
00350
00351 static int g726_encode_frame(AVCodecContext *avctx,
00352 uint8_t *dst, int buf_size, void *data)
00353 {
00354 G726Context *c = avctx->priv_data;
00355 const int16_t *samples = data;
00356 PutBitContext pb;
00357 int i;
00358
00359 init_put_bits(&pb, dst, 1024*1024);
00360
00361 for (i = 0; i < avctx->frame_size; i++)
00362 put_bits(&pb, c->code_size, g726_encode(c, *samples++));
00363
00364 flush_put_bits(&pb);
00365
00366 return put_bits_count(&pb)>>3;
00367 }
00368
00369 #define OFFSET(x) offsetof(G726Context, x)
00370 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
00371 static const AVOption options[] = {
00372 { "code_size", "Bits per code", OFFSET(code_size), AV_OPT_TYPE_INT, { 4 }, 2, 5, AE },
00373 { NULL },
00374 };
00375
00376 static const AVClass class = {
00377 .class_name = "g726",
00378 .item_name = av_default_item_name,
00379 .option = options,
00380 .version = LIBAVUTIL_VERSION_INT,
00381 };
00382
00383 static const AVCodecDefault defaults[] = {
00384 { "b", "0" },
00385 { NULL },
00386 };
00387
00388 AVCodec ff_adpcm_g726_encoder = {
00389 .name = "g726",
00390 .type = AVMEDIA_TYPE_AUDIO,
00391 .id = CODEC_ID_ADPCM_G726,
00392 .priv_data_size = sizeof(G726Context),
00393 .init = g726_encode_init,
00394 .encode = g726_encode_frame,
00395 .close = g726_encode_close,
00396 .capabilities = CODEC_CAP_SMALL_LAST_FRAME,
00397 .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
00398 .long_name = NULL_IF_CONFIG_SMALL("G.726 ADPCM"),
00399 .priv_class = &class,
00400 .defaults = defaults,
00401 };
00402 #endif
00403
00404 #if CONFIG_ADPCM_G726_DECODER
00405 static av_cold int g726_decode_init(AVCodecContext *avctx)
00406 {
00407 G726Context* c = avctx->priv_data;
00408
00409 if (avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT &&
00410 avctx->sample_rate != 8000) {
00411 av_log(avctx, AV_LOG_ERROR, "Only 8kHz sample rate is allowed when "
00412 "the compliance level is strict. Reduce the compliance level "
00413 "if you wish to decode the stream anyway.\n");
00414 return AVERROR(EINVAL);
00415 }
00416
00417 if(avctx->channels != 1){
00418 av_log(avctx, AV_LOG_ERROR, "Only mono is supported\n");
00419 return AVERROR(EINVAL);
00420 }
00421
00422 c->code_size = avctx->bits_per_coded_sample;
00423 if (c->code_size < 2 || c->code_size > 5) {
00424 av_log(avctx, AV_LOG_ERROR, "Invalid number of bits %d\n", c->code_size);
00425 return AVERROR(EINVAL);
00426 }
00427 g726_reset(c);
00428
00429 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00430
00431 avcodec_get_frame_defaults(&c->frame);
00432 avctx->coded_frame = &c->frame;
00433
00434 return 0;
00435 }
00436
00437 static int g726_decode_frame(AVCodecContext *avctx, void *data,
00438 int *got_frame_ptr, AVPacket *avpkt)
00439 {
00440 const uint8_t *buf = avpkt->data;
00441 int buf_size = avpkt->size;
00442 G726Context *c = avctx->priv_data;
00443 int16_t *samples;
00444 GetBitContext gb;
00445 int out_samples, ret;
00446
00447 out_samples = buf_size * 8 / c->code_size;
00448
00449
00450 c->frame.nb_samples = out_samples;
00451 if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0) {
00452 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00453 return ret;
00454 }
00455 samples = (int16_t *)c->frame.data[0];
00456
00457 init_get_bits(&gb, buf, buf_size * 8);
00458
00459 while (out_samples--)
00460 *samples++ = g726_decode(c, get_bits(&gb, c->code_size));
00461
00462 if (get_bits_left(&gb) > 0)
00463 av_log(avctx, AV_LOG_ERROR, "Frame invalidly split, missing parser?\n");
00464
00465 *got_frame_ptr = 1;
00466 *(AVFrame *)data = c->frame;
00467
00468 return buf_size;
00469 }
00470
00471 static void g726_decode_flush(AVCodecContext *avctx)
00472 {
00473 G726Context *c = avctx->priv_data;
00474 g726_reset(c);
00475 }
00476
00477 AVCodec ff_adpcm_g726_decoder = {
00478 .name = "g726",
00479 .type = AVMEDIA_TYPE_AUDIO,
00480 .id = CODEC_ID_ADPCM_G726,
00481 .priv_data_size = sizeof(G726Context),
00482 .init = g726_decode_init,
00483 .decode = g726_decode_frame,
00484 .flush = g726_decode_flush,
00485 .capabilities = CODEC_CAP_DR1,
00486 .long_name = NULL_IF_CONFIG_SMALL("G.726 ADPCM"),
00487 };
00488 #endif