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