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