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 Libav.
00007  *
00008  * Libav 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  * Libav 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 Libav; 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 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.h"
00039 
00040 extern const uint16_t ff_wma_critical_freqs[25];
00041 
00042 static float quant_table[96];
00043 
00044 #define MAX_CHANNELS 2
00045 #define BINK_BLOCK_MAX_SIZE (MAX_CHANNELS << 11)
00046 
00047 typedef struct {
00048     AVFrame frame;
00049     GetBitContext gb;
00050     DSPContext dsp;
00051     FmtConvertContext fmt_conv;
00052     int version_b;          
00053     int first;
00054     int channels;
00055     int frame_len;          
00056     int overlap_len;        
00057     int block_size;
00058     int num_bands;
00059     unsigned int *bands;
00060     float root;
00061     DECLARE_ALIGNED(32, FFTSample, coeffs)[BINK_BLOCK_MAX_SIZE];
00062     DECLARE_ALIGNED(16, int16_t, previous)[BINK_BLOCK_MAX_SIZE / 16];  
00063     DECLARE_ALIGNED(16, int16_t, current)[BINK_BLOCK_MAX_SIZE / 16];
00064     float *coeffs_ptr[MAX_CHANNELS]; 
00065     float *prev_ptr[MAX_CHANNELS];   
00066     uint8_t *packet_buffer;
00067     union {
00068         RDFTContext rdft;
00069         DCTContext dct;
00070     } trans;
00071 } BinkAudioContext;
00072 
00073 
00074 static av_cold int decode_init(AVCodecContext *avctx)
00075 {
00076     BinkAudioContext *s = avctx->priv_data;
00077     int sample_rate = avctx->sample_rate;
00078     int sample_rate_half;
00079     int i;
00080     int frame_len_bits;
00081 
00082     dsputil_init(&s->dsp, avctx);
00083     ff_fmt_convert_init(&s->fmt_conv, avctx);
00084 
00085     /* determine frame length */
00086     if (avctx->sample_rate < 22050) {
00087         frame_len_bits = 9;
00088     } else if (avctx->sample_rate < 44100) {
00089         frame_len_bits = 10;
00090     } else {
00091         frame_len_bits = 11;
00092     }
00093 
00094     if (avctx->channels > MAX_CHANNELS) {
00095         av_log(avctx, AV_LOG_ERROR, "too many channels: %d\n", avctx->channels);
00096         return -1;
00097     }
00098 
00099     s->version_b = avctx->extradata && avctx->extradata[3] == 'b';
00100 
00101     if (avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT) {
00102         // audio is already interleaved for the RDFT format variant
00103         sample_rate  *= avctx->channels;
00104         s->channels = 1;
00105         if (!s->version_b)
00106             frame_len_bits += av_log2(avctx->channels);
00107     } else {
00108         s->channels = avctx->channels;
00109     }
00110 
00111     s->frame_len     = 1 << frame_len_bits;
00112     s->overlap_len   = s->frame_len / 16;
00113     s->block_size    = (s->frame_len - s->overlap_len) * s->channels;
00114     sample_rate_half = (sample_rate + 1) / 2;
00115     s->root          = 2.0 / sqrt(s->frame_len);
00116     for (i = 0; i < 96; i++) {
00117         /* constant is result of 0.066399999/log10(M_E) */
00118         quant_table[i] = expf(i * 0.15289164787221953823f) * s->root;
00119     }
00120 
00121     /* calculate number of bands */
00122     for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
00123         if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1])
00124             break;
00125 
00126     s->bands = av_malloc((s->num_bands + 1) * sizeof(*s->bands));
00127     if (!s->bands)
00128         return AVERROR(ENOMEM);
00129 
00130     /* populate bands data */
00131     s->bands[0] = 2;
00132     for (i = 1; i < s->num_bands; i++)
00133         s->bands[i] = (ff_wma_critical_freqs[i - 1] * s->frame_len / sample_rate_half) & ~1;
00134     s->bands[s->num_bands] = s->frame_len;
00135 
00136     s->first = 1;
00137     avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00138 
00139     for (i = 0; i < s->channels; i++) {
00140         s->coeffs_ptr[i] = s->coeffs + i * s->frame_len;
00141         s->prev_ptr[i]   = s->coeffs_ptr[i] + s->frame_len - s->overlap_len;
00142     }
00143 
00144     if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
00145         ff_rdft_init(&s->trans.rdft, frame_len_bits, DFT_C2R);
00146     else if (CONFIG_BINKAUDIO_DCT_DECODER)
00147         ff_dct_init(&s->trans.dct, frame_len_bits, DCT_III);
00148     else
00149         return -1;
00150 
00151     avcodec_get_frame_defaults(&s->frame);
00152     avctx->coded_frame = &s->frame;
00153 
00154     return 0;
00155 }
00156 
00157 static float get_float(GetBitContext *gb)
00158 {
00159     int power = get_bits(gb, 5);
00160     float f = ldexpf(get_bits_long(gb, 23), power - 23);
00161     if (get_bits1(gb))
00162         f = -f;
00163     return f;
00164 }
00165 
00166 static const uint8_t rle_length_tab[16] = {
00167     2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
00168 };
00169 
00170 #define GET_BITS_SAFE(out, nbits) do {  \
00171     if (get_bits_left(gb) < nbits)      \
00172         return AVERROR_INVALIDDATA;     \
00173     out = get_bits(gb, nbits);          \
00174 } while (0)
00175 
00181 static int decode_block(BinkAudioContext *s, int16_t *out, int use_dct)
00182 {
00183     int ch, i, j, k;
00184     float q, quant[25];
00185     int width, coeff;
00186     GetBitContext *gb = &s->gb;
00187 
00188     if (use_dct)
00189         skip_bits(gb, 2);
00190 
00191     for (ch = 0; ch < s->channels; ch++) {
00192         FFTSample *coeffs = s->coeffs_ptr[ch];
00193         if (s->version_b) {
00194             if (get_bits_left(gb) < 64)
00195                 return AVERROR_INVALIDDATA;
00196             coeffs[0] = av_int2float(get_bits_long(gb, 32)) * s->root;
00197             coeffs[1] = av_int2float(get_bits_long(gb, 32)) * s->root;
00198         } else {
00199             if (get_bits_left(gb) < 58)
00200                 return AVERROR_INVALIDDATA;
00201             coeffs[0] = get_float(gb) * s->root;
00202             coeffs[1] = get_float(gb) * s->root;
00203         }
00204 
00205         if (get_bits_left(gb) < s->num_bands * 8)
00206             return AVERROR_INVALIDDATA;
00207         for (i = 0; i < s->num_bands; i++) {
00208             int value = get_bits(gb, 8);
00209             quant[i]  = quant_table[FFMIN(value, 95)];
00210         }
00211 
00212         k = 0;
00213         q = quant[0];
00214 
00215         // parse coefficients
00216         i = 2;
00217         while (i < s->frame_len) {
00218             if (s->version_b) {
00219                 j = i + 16;
00220             } else {
00221                 int v;
00222                 GET_BITS_SAFE(v, 1);
00223                 if (v) {
00224                     GET_BITS_SAFE(v, 4);
00225                     j = i + rle_length_tab[v] * 8;
00226                 } else {
00227                     j = i + 8;
00228                 }
00229             }
00230 
00231             j = FFMIN(j, s->frame_len);
00232 
00233             GET_BITS_SAFE(width, 4);
00234             if (width == 0) {
00235                 memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
00236                 i = j;
00237                 while (s->bands[k] < i)
00238                     q = quant[k++];
00239             } else {
00240                 while (i < j) {
00241                     if (s->bands[k] == i)
00242                         q = quant[k++];
00243                     GET_BITS_SAFE(coeff, width);
00244                     if (coeff) {
00245                         int v;
00246                         GET_BITS_SAFE(v, 1);
00247                         if (v)
00248                             coeffs[i] = -q * coeff;
00249                         else
00250                             coeffs[i] =  q * coeff;
00251                     } else {
00252                         coeffs[i] = 0.0f;
00253                     }
00254                     i++;
00255                 }
00256             }
00257         }
00258 
00259         if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
00260             coeffs[0] /= 0.5;
00261             s->trans.dct.dct_calc(&s->trans.dct,  coeffs);
00262             s->dsp.vector_fmul_scalar(coeffs, coeffs, s->frame_len / 2, s->frame_len);
00263         }
00264         else if (CONFIG_BINKAUDIO_RDFT_DECODER)
00265             s->trans.rdft.rdft_calc(&s->trans.rdft, coeffs);
00266     }
00267 
00268     s->fmt_conv.float_to_int16_interleave(s->current,
00269                                           (const float **)s->prev_ptr,
00270                                           s->overlap_len, s->channels);
00271     s->fmt_conv.float_to_int16_interleave(out, (const float **)s->coeffs_ptr,
00272                                           s->frame_len - s->overlap_len,
00273                                           s->channels);
00274 
00275     if (!s->first) {
00276         int count = s->overlap_len * s->channels;
00277         int shift = av_log2(count);
00278         for (i = 0; i < count; i++) {
00279             out[i] = (s->previous[i] * (count - i) + out[i] * i) >> shift;
00280         }
00281     }
00282 
00283     memcpy(s->previous, s->current,
00284            s->overlap_len * s->channels * sizeof(*s->previous));
00285 
00286     s->first = 0;
00287 
00288     return 0;
00289 }
00290 
00291 static av_cold int decode_end(AVCodecContext *avctx)
00292 {
00293     BinkAudioContext * s = avctx->priv_data;
00294     av_freep(&s->bands);
00295     av_freep(&s->packet_buffer);
00296     if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
00297         ff_rdft_end(&s->trans.rdft);
00298     else if (CONFIG_BINKAUDIO_DCT_DECODER)
00299         ff_dct_end(&s->trans.dct);
00300 
00301     return 0;
00302 }
00303 
00304 static void get_bits_align32(GetBitContext *s)
00305 {
00306     int n = (-get_bits_count(s)) & 31;
00307     if (n) skip_bits(s, n);
00308 }
00309 
00310 static int decode_frame(AVCodecContext *avctx, void *data,
00311                         int *got_frame_ptr, AVPacket *avpkt)
00312 {
00313     BinkAudioContext *s = avctx->priv_data;
00314     int16_t *samples;
00315     GetBitContext *gb = &s->gb;
00316     int ret, consumed = 0;
00317 
00318     if (!get_bits_left(gb)) {
00319         uint8_t *buf;
00320         /* handle end-of-stream */
00321         if (!avpkt->size) {
00322             *got_frame_ptr = 0;
00323             return 0;
00324         }
00325         if (avpkt->size < 4) {
00326             av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
00327             return AVERROR_INVALIDDATA;
00328         }
00329         buf = av_realloc(s->packet_buffer, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
00330         if (!buf)
00331             return AVERROR(ENOMEM);
00332         s->packet_buffer = buf;
00333         memcpy(s->packet_buffer, avpkt->data, avpkt->size);
00334         init_get_bits(gb, s->packet_buffer, avpkt->size * 8);
00335         consumed = avpkt->size;
00336 
00337         /* skip reported size */
00338         skip_bits_long(gb, 32);
00339     }
00340 
00341     /* get output buffer */
00342     s->frame.nb_samples = s->block_size / avctx->channels;
00343     if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
00344         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00345         return ret;
00346     }
00347     samples = (int16_t *)s->frame.data[0];
00348 
00349     if (decode_block(s, samples, avctx->codec->id == CODEC_ID_BINKAUDIO_DCT)) {
00350         av_log(avctx, AV_LOG_ERROR, "Incomplete packet\n");
00351         return AVERROR_INVALIDDATA;
00352     }
00353     get_bits_align32(gb);
00354 
00355     *got_frame_ptr   = 1;
00356     *(AVFrame *)data = s->frame;
00357 
00358     return consumed;
00359 }
00360 
00361 AVCodec ff_binkaudio_rdft_decoder = {
00362     .name           = "binkaudio_rdft",
00363     .type           = AVMEDIA_TYPE_AUDIO,
00364     .id             = CODEC_ID_BINKAUDIO_RDFT,
00365     .priv_data_size = sizeof(BinkAudioContext),
00366     .init           = decode_init,
00367     .close          = decode_end,
00368     .decode         = decode_frame,
00369     .capabilities   = CODEC_CAP_DELAY | CODEC_CAP_DR1,
00370     .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)")
00371 };
00372 
00373 AVCodec ff_binkaudio_dct_decoder = {
00374     .name           = "binkaudio_dct",
00375     .type           = AVMEDIA_TYPE_AUDIO,
00376     .id             = CODEC_ID_BINKAUDIO_DCT,
00377     .priv_data_size = sizeof(BinkAudioContext),
00378     .init           = decode_init,
00379     .close          = decode_end,
00380     .decode         = decode_frame,
00381     .capabilities   = CODEC_CAP_DELAY | CODEC_CAP_DR1,
00382     .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)")
00383 };