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

libavcodec/binkaudio.c

Go to the documentation of this file.
00001 /*
00002  * Bink Audio decoder
00003  * Copyright (c) 2007-2011 Peter Ross (pross@xvid.org)
00004  * Copyright (c) 2009 Daniel Verkamp (daniel@drv.nu)
00005  *
00006  * This file is part of FFmpeg.
00007  *
00008  * FFmpeg is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * FFmpeg is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with FFmpeg; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00031 #include "avcodec.h"
00032 #define ALT_BITSTREAM_READER_LE
00033 #include "get_bits.h"
00034 #include "dsputil.h"
00035 #include "dct.h"
00036 #include "rdft.h"
00037 #include "fmtconvert.h"
00038 #include "libavutil/intfloat_readwrite.h"
00039 
00040 extern const uint16_t ff_wma_critical_freqs[25];
00041 
00042 #define MAX_CHANNELS 2
00043 #define BINK_BLOCK_MAX_SIZE (MAX_CHANNELS << 11)
00044 
00045 typedef struct {
00046     GetBitContext gb;
00047     DSPContext dsp;
00048     FmtConvertContext fmt_conv;
00049     int version_b;          
00050     int first;
00051     int channels;
00052     int frame_len;          
00053     int overlap_len;        
00054     int block_size;
00055     int num_bands;
00056     unsigned int *bands;
00057     float root;
00058     DECLARE_ALIGNED(32, FFTSample, coeffs)[BINK_BLOCK_MAX_SIZE];
00059     DECLARE_ALIGNED(16, short, previous)[BINK_BLOCK_MAX_SIZE / 16];  
00060     float *coeffs_ptr[MAX_CHANNELS]; 
00061     union {
00062         RDFTContext rdft;
00063         DCTContext dct;
00064     } trans;
00065 } BinkAudioContext;
00066 
00067 
00068 static av_cold int decode_init(AVCodecContext *avctx)
00069 {
00070     BinkAudioContext *s = avctx->priv_data;
00071     int sample_rate = avctx->sample_rate;
00072     int sample_rate_half;
00073     int i;
00074     int frame_len_bits;
00075 
00076     dsputil_init(&s->dsp, avctx);
00077     ff_fmt_convert_init(&s->fmt_conv, avctx);
00078 
00079     /* determine frame length */
00080     if (avctx->sample_rate < 22050) {
00081         frame_len_bits = 9;
00082     } else if (avctx->sample_rate < 44100) {
00083         frame_len_bits = 10;
00084     } else {
00085         frame_len_bits = 11;
00086     }
00087 
00088     if (avctx->channels > MAX_CHANNELS) {
00089         av_log(avctx, AV_LOG_ERROR, "too many channels: %d\n", avctx->channels);
00090         return -1;
00091     }
00092 
00093     if (avctx->extradata && avctx->extradata_size > 0)
00094         s->version_b = avctx->extradata[0];
00095 
00096     if (avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT) {
00097         // audio is already interleaved for the RDFT format variant
00098         sample_rate  *= avctx->channels;
00099         s->channels = 1;
00100         if (!s->version_b)
00101             frame_len_bits += av_log2(avctx->channels);
00102     } else {
00103         s->channels = avctx->channels;
00104     }
00105 
00106     s->frame_len     = 1 << frame_len_bits;
00107     s->overlap_len   = s->frame_len / 16;
00108     s->block_size    = (s->frame_len - s->overlap_len) * s->channels;
00109     sample_rate_half = (sample_rate + 1) / 2;
00110     s->root          = 2.0 / sqrt(s->frame_len);
00111 
00112     /* calculate number of bands */
00113     for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
00114         if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1])
00115             break;
00116 
00117     s->bands = av_malloc((s->num_bands + 1) * sizeof(*s->bands));
00118     if (!s->bands)
00119         return AVERROR(ENOMEM);
00120 
00121     /* populate bands data */
00122     s->bands[0] = 2;
00123     for (i = 1; i < s->num_bands; i++)
00124         s->bands[i] = (ff_wma_critical_freqs[i - 1] * s->frame_len / sample_rate_half) & ~1;
00125     s->bands[s->num_bands] = s->frame_len;
00126 
00127     s->first = 1;
00128     avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00129 
00130     for (i = 0; i < s->channels; i++)
00131         s->coeffs_ptr[i] = s->coeffs + i * s->frame_len;
00132 
00133     if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
00134         ff_rdft_init(&s->trans.rdft, frame_len_bits, DFT_C2R);
00135     else if (CONFIG_BINKAUDIO_DCT_DECODER)
00136         ff_dct_init(&s->trans.dct, frame_len_bits, DCT_III);
00137     else
00138         return -1;
00139 
00140     return 0;
00141 }
00142 
00143 static float get_float(GetBitContext *gb)
00144 {
00145     int power = get_bits(gb, 5);
00146     float f = ldexpf(get_bits_long(gb, 23), power - 23);
00147     if (get_bits1(gb))
00148         f = -f;
00149     return f;
00150 }
00151 
00152 static const uint8_t rle_length_tab[16] = {
00153     2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
00154 };
00155 
00156 #define GET_BITS_SAFE(out, nbits) do {  \
00157     if (get_bits_left(gb) < nbits)      \
00158         return AVERROR_INVALIDDATA;     \
00159     out = get_bits(gb, nbits);          \
00160 } while (0)
00161 
00167 static int decode_block(BinkAudioContext *s, short *out, int use_dct)
00168 {
00169     int ch, i, j, k;
00170     float q, quant[25];
00171     int width, coeff;
00172     GetBitContext *gb = &s->gb;
00173 
00174     if (use_dct)
00175         skip_bits(gb, 2);
00176 
00177     for (ch = 0; ch < s->channels; ch++) {
00178         FFTSample *coeffs = s->coeffs_ptr[ch];
00179         if (s->version_b) {
00180             if (get_bits_left(gb) < 64)
00181                 return AVERROR_INVALIDDATA;
00182             coeffs[0] = av_int2flt(get_bits(gb, 32)) * s->root;
00183             coeffs[1] = av_int2flt(get_bits(gb, 32)) * s->root;
00184         } else {
00185             if (get_bits_left(gb) < 58)
00186                 return AVERROR_INVALIDDATA;
00187             coeffs[0] = get_float(gb) * s->root;
00188             coeffs[1] = get_float(gb) * s->root;
00189         }
00190 
00191         if (get_bits_left(gb) < s->num_bands * 8)
00192             return AVERROR_INVALIDDATA;
00193         for (i = 0; i < s->num_bands; i++) {
00194             /* constant is result of 0.066399999/log10(M_E) */
00195             int value = get_bits(gb, 8);
00196             quant[i] = expf(FFMIN(value, 95) * 0.15289164787221953823f) * s->root;
00197         }
00198 
00199         k = 0;
00200         q = quant[0];
00201 
00202         // parse coefficients
00203         i = 2;
00204         while (i < s->frame_len) {
00205             if (s->version_b) {
00206                 j = i + 16;
00207             } else {
00208                 int v;
00209                 GET_BITS_SAFE(v, 1);
00210                 if (v) {
00211                     GET_BITS_SAFE(v, 4);
00212                     j = i + rle_length_tab[v] * 8;
00213                 } else {
00214                     j = i + 8;
00215                 }
00216             }
00217 
00218             j = FFMIN(j, s->frame_len);
00219 
00220             GET_BITS_SAFE(width, 4);
00221             if (width == 0) {
00222                 memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
00223                 i = j;
00224                 while (s->bands[k] < i)
00225                     q = quant[k++];
00226             } else {
00227                 while (i < j) {
00228                     if (s->bands[k] == i)
00229                         q = quant[k++];
00230                     GET_BITS_SAFE(coeff, width);
00231                     if (coeff) {
00232                         int v;
00233                         GET_BITS_SAFE(v, 1);
00234                         if (v)
00235                             coeffs[i] = -q * coeff;
00236                         else
00237                             coeffs[i] =  q * coeff;
00238                     } else {
00239                         coeffs[i] = 0.0f;
00240                     }
00241                     i++;
00242                 }
00243             }
00244         }
00245 
00246         if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
00247             coeffs[0] /= 0.5;
00248             s->trans.dct.dct_calc(&s->trans.dct,  coeffs);
00249             s->dsp.vector_fmul_scalar(coeffs, coeffs, s->frame_len / 2, s->frame_len);
00250         }
00251         else if (CONFIG_BINKAUDIO_RDFT_DECODER)
00252             s->trans.rdft.rdft_calc(&s->trans.rdft, coeffs);
00253     }
00254 
00255     s->fmt_conv.float_to_int16_interleave(out, (const float **)s->coeffs_ptr,
00256                                           s->frame_len, s->channels);
00257 
00258     if (!s->first) {
00259         int count = s->overlap_len * s->channels;
00260         int shift = av_log2(count);
00261         for (i = 0; i < count; i++) {
00262             out[i] = (s->previous[i] * (count - i) + out[i] * i) >> shift;
00263         }
00264     }
00265 
00266     memcpy(s->previous, out + s->block_size,
00267            s->overlap_len * s->channels * sizeof(*out));
00268 
00269     s->first = 0;
00270 
00271     return 0;
00272 }
00273 
00274 static av_cold int decode_end(AVCodecContext *avctx)
00275 {
00276     BinkAudioContext * s = avctx->priv_data;
00277     av_freep(&s->bands);
00278     if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
00279         ff_rdft_end(&s->trans.rdft);
00280     else if (CONFIG_BINKAUDIO_DCT_DECODER)
00281         ff_dct_end(&s->trans.dct);
00282     return 0;
00283 }
00284 
00285 static void get_bits_align32(GetBitContext *s)
00286 {
00287     int n = (-get_bits_count(s)) & 31;
00288     if (n) skip_bits(s, n);
00289 }
00290 
00291 static int decode_frame(AVCodecContext *avctx,
00292                         void *data, int *data_size,
00293                         AVPacket *avpkt)
00294 {
00295     BinkAudioContext *s = avctx->priv_data;
00296     const uint8_t *buf  = avpkt->data;
00297     int buf_size        = avpkt->size;
00298     short *samples      = data;
00299     short *samples_end  = (short*)((uint8_t*)data + *data_size);
00300     int reported_size;
00301     GetBitContext *gb = &s->gb;
00302 
00303     if (buf_size < 4) {
00304         av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
00305         return AVERROR_INVALIDDATA;
00306     }
00307 
00308     init_get_bits(gb, buf, buf_size * 8);
00309 
00310     reported_size = get_bits_long(gb, 32);
00311     while (samples + s->block_size <= samples_end) {
00312         if (decode_block(s, samples, avctx->codec->id == CODEC_ID_BINKAUDIO_DCT))
00313             break;
00314         samples += s->block_size;
00315         get_bits_align32(gb);
00316     }
00317 
00318     *data_size = FFMIN(reported_size, (uint8_t*)samples - (uint8_t*)data);
00319     return buf_size;
00320 }
00321 
00322 AVCodec ff_binkaudio_rdft_decoder = {
00323     "binkaudio_rdft",
00324     AVMEDIA_TYPE_AUDIO,
00325     CODEC_ID_BINKAUDIO_RDFT,
00326     sizeof(BinkAudioContext),
00327     decode_init,
00328     NULL,
00329     decode_end,
00330     decode_frame,
00331     .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)")
00332 };
00333 
00334 AVCodec ff_binkaudio_dct_decoder = {
00335     "binkaudio_dct",
00336     AVMEDIA_TYPE_AUDIO,
00337     CODEC_ID_BINKAUDIO_DCT,
00338     sizeof(BinkAudioContext),
00339     decode_init,
00340     NULL,
00341     decode_end,
00342     decode_frame,
00343     .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)")
00344 };

Generated on Wed Apr 11 2012 07:31:32 for FFmpeg by  doxygen 1.7.1