• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/g726.c

Go to the documentation of this file.
00001 /*
00002  * G.726 ADPCM audio codec
00003  * Copyright (c) 2004 Roman Shaposhnik
00004  *
00005  * This is a very straightforward rendition of the G.726
00006  * Section 4 "Computational Details".
00007  *
00008  * This file is part of FFmpeg.
00009  *
00010  * FFmpeg is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or (at your option) any later version.
00014  *
00015  * FFmpeg is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with FFmpeg; if not, write to the Free Software
00022  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00023  */
00024 #include <limits.h>
00025 #include "avcodec.h"
00026 #include "get_bits.h"
00027 #include "put_bits.h"
00028 
00035 typedef struct Float11 {
00036     uint8_t sign;   
00037     uint8_t exp;    
00038     uint8_t mant;   
00039 } Float11;
00040 
00041 static inline Float11* i2f(int i, Float11* f)
00042 {
00043     f->sign = (i < 0);
00044     if (f->sign)
00045         i = -i;
00046     f->exp = av_log2_16bit(i) + !!i;
00047     f->mant = i? (i<<6) >> f->exp : 1<<5;
00048     return f;
00049 }
00050 
00051 static inline int16_t mult(Float11* f1, Float11* f2)
00052 {
00053         int res, exp;
00054 
00055         exp = f1->exp + f2->exp;
00056         res = (((f1->mant * f2->mant) + 0x30) >> 4);
00057         res = exp > 19 ? res << (exp - 19) : res >> (19 - exp);
00058         return (f1->sign ^ f2->sign) ? -res : res;
00059 }
00060 
00061 static inline int sgn(int value)
00062 {
00063     return (value < 0) ? -1 : 1;
00064 }
00065 
00066 typedef struct G726Tables {
00067     const int* quant;         
00068     const int16_t* iquant;    
00069     const int16_t* W;         
00070     const uint8_t* F;         
00071 } G726Tables;
00072 
00073 typedef struct G726Context {
00074     G726Tables tbls;    
00076     Float11 sr[2];      
00077     Float11 dq[6];      
00078     int a[2];           
00079     int b[6];           
00080     int pk[2];          
00082     int ap;             
00083     int yu;             
00084     int yl;             
00085     int dms;            
00086     int dml;            
00087     int td;             
00089     int se;             
00090     int sez;            
00091     int y;              
00092     int code_size;
00093 } G726Context;
00094 
00095 static const int quant_tbl16[] =                  
00096            { 260, INT_MAX };
00097 static const int16_t iquant_tbl16[] =
00098            { 116, 365, 365, 116 };
00099 static const int16_t W_tbl16[] =
00100            { -22, 439, 439, -22 };
00101 static const uint8_t F_tbl16[] =
00102            { 0, 7, 7, 0 };
00103 
00104 static const int quant_tbl24[] =                  
00105            {  7, 217, 330, INT_MAX };
00106 static const int16_t iquant_tbl24[] =
00107            { INT16_MIN, 135, 273, 373, 373, 273, 135, INT16_MIN };
00108 static const int16_t W_tbl24[] =
00109            { -4,  30, 137, 582, 582, 137,  30, -4 };
00110 static const uint8_t F_tbl24[] =
00111            { 0, 1, 2, 7, 7, 2, 1, 0 };
00112 
00113 static const int quant_tbl32[] =                  
00114            { -125,  79, 177, 245, 299, 348, 399, INT_MAX };
00115 static const int16_t iquant_tbl32[] =
00116          { INT16_MIN,   4, 135, 213, 273, 323, 373, 425,
00117                  425, 373, 323, 273, 213, 135,   4, INT16_MIN };
00118 static const int16_t W_tbl32[] =
00119            { -12,  18,  41,  64, 112, 198, 355, 1122,
00120             1122, 355, 198, 112,  64,  41,  18, -12};
00121 static const uint8_t F_tbl32[] =
00122            { 0, 0, 0, 1, 1, 1, 3, 7, 7, 3, 1, 1, 1, 0, 0, 0 };
00123 
00124 static const int quant_tbl40[] =                  
00125            { -122, -16,  67, 138, 197, 249, 297, 338,
00126               377, 412, 444, 474, 501, 527, 552, INT_MAX };
00127 static const int16_t iquant_tbl40[] =
00128          { INT16_MIN, -66,  28, 104, 169, 224, 274, 318,
00129                  358, 395, 429, 459, 488, 514, 539, 566,
00130                  566, 539, 514, 488, 459, 429, 395, 358,
00131                  318, 274, 224, 169, 104,  28, -66, INT16_MIN };
00132 static const int16_t W_tbl40[] =
00133            {   14,  14,  24,  39,  40,  41,   58,  100,
00134               141, 179, 219, 280, 358, 440,  529,  696,
00135               696, 529, 440, 358, 280, 219,  179,  141,
00136               100,  58,  41,  40,  39,  24,   14,   14 };
00137 static const uint8_t F_tbl40[] =
00138            { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 6,
00139              6, 6, 5, 4, 3, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
00140 
00141 static const G726Tables G726Tables_pool[] =
00142            {{ quant_tbl16, iquant_tbl16, W_tbl16, F_tbl16 },
00143             { quant_tbl24, iquant_tbl24, W_tbl24, F_tbl24 },
00144             { quant_tbl32, iquant_tbl32, W_tbl32, F_tbl32 },
00145             { quant_tbl40, iquant_tbl40, W_tbl40, F_tbl40 }};
00146 
00147 
00151 static inline uint8_t quant(G726Context* c, int d)
00152 {
00153     int sign, exp, i, dln;
00154 
00155     sign = i = 0;
00156     if (d < 0) {
00157         sign = 1;
00158         d = -d;
00159     }
00160     exp = av_log2_16bit(d);
00161     dln = ((exp<<7) + (((d<<7)>>exp)&0x7f)) - (c->y>>2);
00162 
00163     while (c->tbls.quant[i] < INT_MAX && c->tbls.quant[i] < dln)
00164         ++i;
00165 
00166     if (sign)
00167         i = ~i;
00168     if (c->code_size != 2 && i == 0) /* I'm not sure this is a good idea */
00169         i = 0xff;
00170 
00171     return i;
00172 }
00173 
00177 static inline int16_t inverse_quant(G726Context* c, int i)
00178 {
00179     int dql, dex, dqt;
00180 
00181     dql = c->tbls.iquant[i] + (c->y >> 2);
00182     dex = (dql>>7) & 0xf;        /* 4bit exponent */
00183     dqt = (1<<7) + (dql & 0x7f); /* log2 -> linear */
00184     return (dql < 0) ? 0 : ((dqt<<dex) >> 7);
00185 }
00186 
00187 static int16_t g726_decode(G726Context* c, int I)
00188 {
00189     int dq, re_signal, pk0, fa1, i, tr, ylint, ylfrac, thr2, al, dq0;
00190     Float11 f;
00191     int I_sig= I >> (c->code_size - 1);
00192 
00193     dq = inverse_quant(c, I);
00194 
00195     /* Transition detect */
00196     ylint = (c->yl >> 15);
00197     ylfrac = (c->yl >> 10) & 0x1f;
00198     thr2 = (ylint > 9) ? 0x1f << 10 : (0x20 + ylfrac) << ylint;
00199     tr= (c->td == 1 && dq > ((3*thr2)>>2));
00200 
00201     if (I_sig)  /* get the sign */
00202         dq = -dq;
00203     re_signal = c->se + dq;
00204 
00205     /* Update second order predictor coefficient A2 and A1 */
00206     pk0 = (c->sez + dq) ? sgn(c->sez + dq) : 0;
00207     dq0 = dq ? sgn(dq) : 0;
00208     if (tr) {
00209         c->a[0] = 0;
00210         c->a[1] = 0;
00211         for (i=0; i<6; i++)
00212             c->b[i] = 0;
00213     } else {
00214         /* This is a bit crazy, but it really is +255 not +256 */
00215         fa1 = av_clip((-c->a[0]*c->pk[0]*pk0)>>5, -256, 255);
00216 
00217         c->a[1] += 128*pk0*c->pk[1] + fa1 - (c->a[1]>>7);
00218         c->a[1] = av_clip(c->a[1], -12288, 12288);
00219         c->a[0] += 64*3*pk0*c->pk[0] - (c->a[0] >> 8);
00220         c->a[0] = av_clip(c->a[0], -(15360 - c->a[1]), 15360 - c->a[1]);
00221 
00222         for (i=0; i<6; i++)
00223             c->b[i] += 128*dq0*sgn(-c->dq[i].sign) - (c->b[i]>>8);
00224     }
00225 
00226     /* Update Dq and Sr and Pk */
00227     c->pk[1] = c->pk[0];
00228     c->pk[0] = pk0 ? pk0 : 1;
00229     c->sr[1] = c->sr[0];
00230     i2f(re_signal, &c->sr[0]);
00231     for (i=5; i>0; i--)
00232         c->dq[i] = c->dq[i-1];
00233     i2f(dq, &c->dq[0]);
00234     c->dq[0].sign = I_sig; /* Isn't it crazy ?!?! */
00235 
00236     c->td = c->a[1] < -11776;
00237 
00238     /* Update Ap */
00239     c->dms += (c->tbls.F[I]<<4) + ((- c->dms) >> 5);
00240     c->dml += (c->tbls.F[I]<<4) + ((- c->dml) >> 7);
00241     if (tr)
00242         c->ap = 256;
00243     else {
00244         c->ap += (-c->ap) >> 4;
00245         if (c->y <= 1535 || c->td || abs((c->dms << 2) - c->dml) >= (c->dml >> 3))
00246             c->ap += 0x20;
00247     }
00248 
00249     /* Update Yu and Yl */
00250     c->yu = av_clip(c->y + c->tbls.W[I] + ((-c->y)>>5), 544, 5120);
00251     c->yl += c->yu + ((-c->yl)>>6);
00252 
00253     /* Next iteration for Y */
00254     al = (c->ap >= 256) ? 1<<6 : c->ap >> 2;
00255     c->y = (c->yl + (c->yu - (c->yl>>6))*al) >> 6;
00256 
00257     /* Next iteration for SE and SEZ */
00258     c->se = 0;
00259     for (i=0; i<6; i++)
00260         c->se += mult(i2f(c->b[i] >> 2, &f), &c->dq[i]);
00261     c->sez = c->se >> 1;
00262     for (i=0; i<2; i++)
00263         c->se += mult(i2f(c->a[i] >> 2, &f), &c->sr[i]);
00264     c->se >>= 1;
00265 
00266     return av_clip(re_signal << 2, -0xffff, 0xffff);
00267 }
00268 
00269 static av_cold int g726_reset(G726Context* c, int index)
00270 {
00271     int i;
00272 
00273     c->tbls = G726Tables_pool[index];
00274     for (i=0; i<2; i++) {
00275         c->sr[i].mant = 1<<5;
00276         c->pk[i] = 1;
00277     }
00278     for (i=0; i<6; i++) {
00279         c->dq[i].mant = 1<<5;
00280     }
00281     c->yu = 544;
00282     c->yl = 34816;
00283 
00284     c->y = 544;
00285 
00286     return 0;
00287 }
00288 
00289 #if CONFIG_ADPCM_G726_ENCODER
00290 static int16_t g726_encode(G726Context* c, int16_t sig)
00291 {
00292     uint8_t i;
00293 
00294     i = quant(c, sig/4 - c->se) & ((1<<c->code_size) - 1);
00295     g726_decode(c, i);
00296     return i;
00297 }
00298 #endif
00299 
00300 /* Interfacing to the libavcodec */
00301 
00302 static av_cold int g726_init(AVCodecContext * avctx)
00303 {
00304     G726Context* c = avctx->priv_data;
00305     unsigned int index;
00306 
00307     if (avctx->sample_rate <= 0) {
00308         av_log(avctx, AV_LOG_ERROR, "Samplerate is invalid\n");
00309         return -1;
00310     }
00311 
00312     index = (avctx->bit_rate + avctx->sample_rate/2) / avctx->sample_rate - 2;
00313 
00314     if (avctx->bit_rate % avctx->sample_rate && avctx->codec->encode) {
00315         av_log(avctx, AV_LOG_ERROR, "Bitrate - Samplerate combination is invalid\n");
00316         return -1;
00317     }
00318     if(avctx->channels != 1){
00319         av_log(avctx, AV_LOG_ERROR, "Only mono is supported\n");
00320         return -1;
00321     }
00322     if(index>3){
00323         av_log(avctx, AV_LOG_ERROR, "Unsupported number of bits %d\n", index+2);
00324         return -1;
00325     }
00326     g726_reset(c, index);
00327     c->code_size = index+2;
00328 
00329     avctx->coded_frame = avcodec_alloc_frame();
00330     if (!avctx->coded_frame)
00331         return AVERROR(ENOMEM);
00332     avctx->coded_frame->key_frame = 1;
00333 
00334     if (avctx->codec->decode)
00335         avctx->sample_fmt = SAMPLE_FMT_S16;
00336 
00337     return 0;
00338 }
00339 
00340 static av_cold int g726_close(AVCodecContext *avctx)
00341 {
00342     av_freep(&avctx->coded_frame);
00343     return 0;
00344 }
00345 
00346 #if CONFIG_ADPCM_G726_ENCODER
00347 static int g726_encode_frame(AVCodecContext *avctx,
00348                             uint8_t *dst, int buf_size, void *data)
00349 {
00350     G726Context *c = avctx->priv_data;
00351     short *samples = data;
00352     PutBitContext pb;
00353 
00354     init_put_bits(&pb, dst, 1024*1024);
00355 
00356     for (; buf_size; buf_size--)
00357         put_bits(&pb, c->code_size, g726_encode(c, *samples++));
00358 
00359     flush_put_bits(&pb);
00360 
00361     return put_bits_count(&pb)>>3;
00362 }
00363 #endif
00364 
00365 static int g726_decode_frame(AVCodecContext *avctx,
00366                              void *data, int *data_size,
00367                              AVPacket *avpkt)
00368 {
00369     const uint8_t *buf = avpkt->data;
00370     int buf_size = avpkt->size;
00371     G726Context *c = avctx->priv_data;
00372     short *samples = data;
00373     GetBitContext gb;
00374 
00375     init_get_bits(&gb, buf, buf_size * 8);
00376 
00377     while (get_bits_count(&gb) + c->code_size <= buf_size*8)
00378         *samples++ = g726_decode(c, get_bits(&gb, c->code_size));
00379 
00380     if(buf_size*8 != get_bits_count(&gb))
00381         av_log(avctx, AV_LOG_ERROR, "Frame invalidly split, missing parser?\n");
00382 
00383     *data_size = (uint8_t*)samples - (uint8_t*)data;
00384     return buf_size;
00385 }
00386 
00387 #if CONFIG_ADPCM_G726_ENCODER
00388 AVCodec adpcm_g726_encoder = {
00389     "g726",
00390     AVMEDIA_TYPE_AUDIO,
00391     CODEC_ID_ADPCM_G726,
00392     sizeof(G726Context),
00393     g726_init,
00394     g726_encode_frame,
00395     g726_close,
00396     NULL,
00397     .sample_fmts = (const enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
00398     .long_name = NULL_IF_CONFIG_SMALL("G.726 ADPCM"),
00399 };
00400 #endif
00401 
00402 AVCodec adpcm_g726_decoder = {
00403     "g726",
00404     AVMEDIA_TYPE_AUDIO,
00405     CODEC_ID_ADPCM_G726,
00406     sizeof(G726Context),
00407     g726_init,
00408     NULL,
00409     g726_close,
00410     g726_decode_frame,
00411     .long_name = NULL_IF_CONFIG_SMALL("G.726 ADPCM"),
00412 };

Generated on Fri Sep 16 2011 17:17:36 for FFmpeg by  doxygen 1.7.1