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 "avcodec.h"
00026 #include "bitstream.h"
00027
00034 typedef struct Float11 {
00035 uint8_t sign;
00036 uint8_t exp;
00037 uint8_t mant;
00038 } Float11;
00039
00040 static inline Float11* i2f(int i, Float11* f)
00041 {
00042 f->sign = (i < 0);
00043 if (f->sign)
00044 i = -i;
00045 f->exp = av_log2_16bit(i) + !!i;
00046 f->mant = i? (i<<6) >> f->exp : 1<<5;
00047 return f;
00048 }
00049
00050 static inline int16_t mult(Float11* f1, Float11* f2)
00051 {
00052 int res, exp;
00053
00054 exp = f1->exp + f2->exp;
00055 res = (((f1->mant * f2->mant) + 0x30) >> 4);
00056 res = exp > 19 ? res << (exp - 19) : res >> (19 - exp);
00057 return (f1->sign ^ f2->sign) ? -res : res;
00058 }
00059
00060 static inline int sgn(int value)
00061 {
00062 return (value < 0) ? -1 : 1;
00063 }
00064
00065 typedef struct G726Tables {
00066 const int* quant;
00067 const int16_t* iquant;
00068 const int16_t* W;
00069 const uint8_t* F;
00070 } G726Tables;
00071
00072 typedef struct G726Context {
00073 G726Tables tbls;
00075 Float11 sr[2];
00076 Float11 dq[6];
00077 int a[2];
00078 int b[6];
00079 int pk[2];
00081 int ap;
00082 int yu;
00083 int yl;
00084 int dms;
00085 int dml;
00086 int td;
00088 int se;
00089 int sez;
00090 int y;
00091 int code_size;
00092 } G726Context;
00093
00094 static const int quant_tbl16[] =
00095 { 260, INT_MAX };
00096 static const int16_t iquant_tbl16[] =
00097 { 116, 365, 365, 116 };
00098 static const int16_t W_tbl16[] =
00099 { -22, 439, 439, -22 };
00100 static const uint8_t F_tbl16[] =
00101 { 0, 7, 7, 0 };
00102
00103 static const int quant_tbl24[] =
00104 { 7, 217, 330, INT_MAX };
00105 static const int16_t iquant_tbl24[] =
00106 { INT16_MIN, 135, 273, 373, 373, 273, 135, INT16_MIN };
00107 static const int16_t W_tbl24[] =
00108 { -4, 30, 137, 582, 582, 137, 30, -4 };
00109 static const uint8_t F_tbl24[] =
00110 { 0, 1, 2, 7, 7, 2, 1, 0 };
00111
00112 static const int quant_tbl32[] =
00113 { -125, 79, 177, 245, 299, 348, 399, INT_MAX };
00114 static const int16_t iquant_tbl32[] =
00115 { INT16_MIN, 4, 135, 213, 273, 323, 373, 425,
00116 425, 373, 323, 273, 213, 135, 4, INT16_MIN };
00117 static const int16_t W_tbl32[] =
00118 { -12, 18, 41, 64, 112, 198, 355, 1122,
00119 1122, 355, 198, 112, 64, 41, 18, -12};
00120 static const uint8_t F_tbl32[] =
00121 { 0, 0, 0, 1, 1, 1, 3, 7, 7, 3, 1, 1, 1, 0, 0, 0 };
00122
00123 static const int quant_tbl40[] =
00124 { -122, -16, 67, 138, 197, 249, 297, 338,
00125 377, 412, 444, 474, 501, 527, 552, INT_MAX };
00126 static const int16_t iquant_tbl40[] =
00127 { INT16_MIN, -66, 28, 104, 169, 224, 274, 318,
00128 358, 395, 429, 459, 488, 514, 539, 566,
00129 566, 539, 514, 488, 459, 429, 395, 358,
00130 318, 274, 224, 169, 104, 28, -66, INT16_MIN };
00131 static const int16_t W_tbl40[] =
00132 { 14, 14, 24, 39, 40, 41, 58, 100,
00133 141, 179, 219, 280, 358, 440, 529, 696,
00134 696, 529, 440, 358, 280, 219, 179, 141,
00135 100, 58, 41, 40, 39, 24, 14, 14 };
00136 static const uint8_t F_tbl40[] =
00137 { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 6,
00138 6, 6, 5, 4, 3, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
00139
00140 static const G726Tables G726Tables_pool[] =
00141 {{ quant_tbl16, iquant_tbl16, W_tbl16, F_tbl16 },
00142 { quant_tbl24, iquant_tbl24, W_tbl24, F_tbl24 },
00143 { quant_tbl32, iquant_tbl32, W_tbl32, F_tbl32 },
00144 { quant_tbl40, iquant_tbl40, W_tbl40, F_tbl40 }};
00145
00146
00150 static inline uint8_t quant(G726Context* c, int d)
00151 {
00152 int sign, exp, i, dln;
00153
00154 sign = i = 0;
00155 if (d < 0) {
00156 sign = 1;
00157 d = -d;
00158 }
00159 exp = av_log2_16bit(d);
00160 dln = ((exp<<7) + (((d<<7)>>exp)&0x7f)) - (c->y>>2);
00161
00162 while (c->tbls.quant[i] < INT_MAX && c->tbls.quant[i] < dln)
00163 ++i;
00164
00165 if (sign)
00166 i = ~i;
00167 if (c->code_size != 2 && i == 0)
00168 i = 0xff;
00169
00170 return i;
00171 }
00172
00176 static inline int16_t inverse_quant(G726Context* c, int i)
00177 {
00178 int dql, dex, dqt;
00179
00180 dql = c->tbls.iquant[i] + (c->y >> 2);
00181 dex = (dql>>7) & 0xf;
00182 dqt = (1<<7) + (dql & 0x7f);
00183 return (dql < 0) ? 0 : ((dqt<<dex) >> 7);
00184 }
00185
00186 static int16_t g726_decode(G726Context* c, int I)
00187 {
00188 int dq, re_signal, pk0, fa1, i, tr, ylint, ylfrac, thr2, al, dq0;
00189 Float11 f;
00190 int I_sig= I >> (c->code_size - 1);
00191
00192 dq = inverse_quant(c, I);
00193
00194
00195 ylint = (c->yl >> 15);
00196 ylfrac = (c->yl >> 10) & 0x1f;
00197 thr2 = (ylint > 9) ? 0x1f << 10 : (0x20 + ylfrac) << ylint;
00198 tr= (c->td == 1 && dq > ((3*thr2)>>2));
00199
00200 if (I_sig)
00201 dq = -dq;
00202 re_signal = c->se + dq;
00203
00204
00205 pk0 = (c->sez + dq) ? sgn(c->sez + dq) : 0;
00206 dq0 = dq ? sgn(dq) : 0;
00207 if (tr) {
00208 c->a[0] = 0;
00209 c->a[1] = 0;
00210 for (i=0; i<6; i++)
00211 c->b[i] = 0;
00212 } else {
00213
00214 fa1 = av_clip((-c->a[0]*c->pk[0]*pk0)>>5, -256, 255);
00215
00216 c->a[1] += 128*pk0*c->pk[1] + fa1 - (c->a[1]>>7);
00217 c->a[1] = av_clip(c->a[1], -12288, 12288);
00218 c->a[0] += 64*3*pk0*c->pk[0] - (c->a[0] >> 8);
00219 c->a[0] = av_clip(c->a[0], -(15360 - c->a[1]), 15360 - c->a[1]);
00220
00221 for (i=0; i<6; i++)
00222 c->b[i] += 128*dq0*sgn(-c->dq[i].sign) - (c->b[i]>>8);
00223 }
00224
00225
00226 c->pk[1] = c->pk[0];
00227 c->pk[0] = pk0 ? pk0 : 1;
00228 c->sr[1] = c->sr[0];
00229 i2f(re_signal, &c->sr[0]);
00230 for (i=5; i>0; i--)
00231 c->dq[i] = c->dq[i-1];
00232 i2f(dq, &c->dq[0]);
00233 c->dq[0].sign = I_sig;
00234
00235 c->td = c->a[1] < -11776;
00236
00237
00238 c->dms += (c->tbls.F[I]<<4) + ((- c->dms) >> 5);
00239 c->dml += (c->tbls.F[I]<<4) + ((- c->dml) >> 7);
00240 if (tr)
00241 c->ap = 256;
00242 else {
00243 c->ap += (-c->ap) >> 4;
00244 if (c->y <= 1535 || c->td || abs((c->dms << 2) - c->dml) >= (c->dml >> 3))
00245 c->ap += 0x20;
00246 }
00247
00248
00249 c->yu = av_clip(c->y + c->tbls.W[I] + ((-c->y)>>5), 544, 5120);
00250 c->yl += c->yu + ((-c->yl)>>6);
00251
00252
00253 al = (c->ap >= 256) ? 1<<6 : c->ap >> 2;
00254 c->y = (c->yl + (c->yu - (c->yl>>6))*al) >> 6;
00255
00256
00257 c->se = 0;
00258 for (i=0; i<6; i++)
00259 c->se += mult(i2f(c->b[i] >> 2, &f), &c->dq[i]);
00260 c->sez = c->se >> 1;
00261 for (i=0; i<2; i++)
00262 c->se += mult(i2f(c->a[i] >> 2, &f), &c->sr[i]);
00263 c->se >>= 1;
00264
00265 return av_clip(re_signal << 2, -0xffff, 0xffff);
00266 }
00267
00268 static av_cold int g726_reset(G726Context* c, int index)
00269 {
00270 int i;
00271
00272 c->tbls = G726Tables_pool[index];
00273 for (i=0; i<2; i++) {
00274 c->sr[i].mant = 1<<5;
00275 c->pk[i] = 1;
00276 }
00277 for (i=0; i<6; i++) {
00278 c->dq[i].mant = 1<<5;
00279 }
00280 c->yu = 544;
00281 c->yl = 34816;
00282
00283 c->y = 544;
00284
00285 return 0;
00286 }
00287
00288 #if CONFIG_ADPCM_G726_ENCODER
00289 static int16_t g726_encode(G726Context* c, int16_t sig)
00290 {
00291 uint8_t i;
00292
00293 i = quant(c, sig/4 - c->se) & ((1<<c->code_size) - 1);
00294 g726_decode(c, i);
00295 return i;
00296 }
00297 #endif
00298
00299
00300
00301 static av_cold int g726_init(AVCodecContext * avctx)
00302 {
00303 G726Context* c = avctx->priv_data;
00304 unsigned int index;
00305
00306 if (avctx->sample_rate <= 0) {
00307 av_log(avctx, AV_LOG_ERROR, "Samplerate is invalid\n");
00308 return -1;
00309 }
00310
00311 index = (avctx->bit_rate + avctx->sample_rate/2) / avctx->sample_rate - 2;
00312
00313 if (avctx->bit_rate % avctx->sample_rate && avctx->codec->encode) {
00314 av_log(avctx, AV_LOG_ERROR, "Bitrate - Samplerate combination is invalid\n");
00315 return -1;
00316 }
00317 if(avctx->channels != 1){
00318 av_log(avctx, AV_LOG_ERROR, "Only mono is supported\n");
00319 return -1;
00320 }
00321 if(index>3){
00322 av_log(avctx, AV_LOG_ERROR, "Unsupported number of bits %d\n", index+2);
00323 return -1;
00324 }
00325 g726_reset(c, index);
00326 c->code_size = index+2;
00327
00328 avctx->coded_frame = avcodec_alloc_frame();
00329 if (!avctx->coded_frame)
00330 return AVERROR(ENOMEM);
00331 avctx->coded_frame->key_frame = 1;
00332
00333 if (avctx->codec->decode)
00334 avctx->sample_fmt = SAMPLE_FMT_S16;
00335
00336 return 0;
00337 }
00338
00339 static av_cold int g726_close(AVCodecContext *avctx)
00340 {
00341 av_freep(&avctx->coded_frame);
00342 return 0;
00343 }
00344
00345 #if CONFIG_ADPCM_G726_ENCODER
00346 static int g726_encode_frame(AVCodecContext *avctx,
00347 uint8_t *dst, int buf_size, void *data)
00348 {
00349 G726Context *c = avctx->priv_data;
00350 short *samples = data;
00351 PutBitContext pb;
00352
00353 init_put_bits(&pb, dst, 1024*1024);
00354
00355 for (; buf_size; buf_size--)
00356 put_bits(&pb, c->code_size, g726_encode(c, *samples++));
00357
00358 flush_put_bits(&pb);
00359
00360 return put_bits_count(&pb)>>3;
00361 }
00362 #endif
00363
00364 static int g726_decode_frame(AVCodecContext *avctx,
00365 void *data, int *data_size,
00366 const uint8_t *buf, int buf_size)
00367 {
00368 G726Context *c = avctx->priv_data;
00369 short *samples = data;
00370 GetBitContext gb;
00371
00372 init_get_bits(&gb, buf, buf_size * 8);
00373
00374 while (get_bits_count(&gb) + c->code_size <= buf_size*8)
00375 *samples++ = g726_decode(c, get_bits(&gb, c->code_size));
00376
00377 if(buf_size*8 != get_bits_count(&gb))
00378 av_log(avctx, AV_LOG_ERROR, "Frame invalidly split, missing parser?\n");
00379
00380 *data_size = (uint8_t*)samples - (uint8_t*)data;
00381 return buf_size;
00382 }
00383
00384 #if CONFIG_ADPCM_G726_ENCODER
00385 AVCodec adpcm_g726_encoder = {
00386 "g726",
00387 CODEC_TYPE_AUDIO,
00388 CODEC_ID_ADPCM_G726,
00389 sizeof(G726Context),
00390 g726_init,
00391 g726_encode_frame,
00392 g726_close,
00393 NULL,
00394 .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
00395 .long_name = NULL_IF_CONFIG_SMALL("G.726 ADPCM"),
00396 };
00397 #endif
00398
00399 AVCodec adpcm_g726_decoder = {
00400 "g726",
00401 CODEC_TYPE_AUDIO,
00402 CODEC_ID_ADPCM_G726,
00403 sizeof(G726Context),
00404 g726_init,
00405 NULL,
00406 g726_close,
00407 g726_decode_frame,
00408 .long_name = NULL_IF_CONFIG_SMALL("G.726 ADPCM"),
00409 };