LCOV - code coverage report
Current view: top level - src/libavcodec - binkaudio.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 141 160 88.1 %
Date: 2017-03-25 17:02:41 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Bink Audio decoder
       3             :  * Copyright (c) 2007-2011 Peter Ross (pross@xvid.org)
       4             :  * Copyright (c) 2009 Daniel Verkamp (daniel@drv.nu)
       5             :  *
       6             :  * This file is part of FFmpeg.
       7             :  *
       8             :  * FFmpeg is free software; you can redistribute it and/or
       9             :  * modify it under the terms of the GNU Lesser General Public
      10             :  * License as published by the Free Software Foundation; either
      11             :  * version 2.1 of the License, or (at your option) any later version.
      12             :  *
      13             :  * FFmpeg is distributed in the hope that it will be useful,
      14             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      16             :  * Lesser General Public License for more details.
      17             :  *
      18             :  * You should have received a copy of the GNU Lesser General Public
      19             :  * License along with FFmpeg; if not, write to the Free Software
      20             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      21             :  */
      22             : 
      23             : /**
      24             :  * @file
      25             :  * Bink Audio decoder
      26             :  *
      27             :  * Technical details here:
      28             :  *  http://wiki.multimedia.cx/index.php?title=Bink_Audio
      29             :  */
      30             : 
      31             : #include "libavutil/channel_layout.h"
      32             : #include "libavutil/intfloat.h"
      33             : 
      34             : #define BITSTREAM_READER_LE
      35             : #include "avcodec.h"
      36             : #include "dct.h"
      37             : #include "get_bits.h"
      38             : #include "internal.h"
      39             : #include "rdft.h"
      40             : #include "wma_freqs.h"
      41             : 
      42             : static float quant_table[96];
      43             : 
      44             : #define MAX_CHANNELS 2
      45             : #define BINK_BLOCK_MAX_SIZE (MAX_CHANNELS << 11)
      46             : 
      47             : typedef struct BinkAudioContext {
      48             :     GetBitContext gb;
      49             :     int version_b;          ///< Bink version 'b'
      50             :     int first;
      51             :     int channels;
      52             :     int frame_len;          ///< transform size (samples)
      53             :     int overlap_len;        ///< overlap size (samples)
      54             :     int block_size;
      55             :     int num_bands;
      56             :     unsigned int *bands;
      57             :     float root;
      58             :     DECLARE_ALIGNED(32, FFTSample, coeffs)[BINK_BLOCK_MAX_SIZE];
      59             :     float previous[MAX_CHANNELS][BINK_BLOCK_MAX_SIZE / 16];  ///< coeffs from previous audio block
      60             :     uint8_t *packet_buffer;
      61             :     union {
      62             :         RDFTContext rdft;
      63             :         DCTContext dct;
      64             :     } trans;
      65             : } BinkAudioContext;
      66             : 
      67             : 
      68           6 : static av_cold int decode_init(AVCodecContext *avctx)
      69             : {
      70           6 :     BinkAudioContext *s = avctx->priv_data;
      71           6 :     int sample_rate = avctx->sample_rate;
      72             :     int sample_rate_half;
      73             :     int i;
      74             :     int frame_len_bits;
      75             : 
      76             :     /* determine frame length */
      77           6 :     if (avctx->sample_rate < 22050) {
      78           0 :         frame_len_bits = 9;
      79           6 :     } else if (avctx->sample_rate < 44100) {
      80           2 :         frame_len_bits = 10;
      81             :     } else {
      82           4 :         frame_len_bits = 11;
      83             :     }
      84             : 
      85           6 :     if (avctx->channels < 1 || avctx->channels > MAX_CHANNELS) {
      86           0 :         av_log(avctx, AV_LOG_ERROR, "invalid number of channels: %d\n", avctx->channels);
      87           0 :         return AVERROR_INVALIDDATA;
      88             :     }
      89           6 :     avctx->channel_layout = avctx->channels == 1 ? AV_CH_LAYOUT_MONO :
      90             :                                                    AV_CH_LAYOUT_STEREO;
      91             : 
      92           6 :     s->version_b = avctx->extradata_size >= 4 && avctx->extradata[3] == 'b';
      93             : 
      94           6 :     if (avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT) {
      95             :         // audio is already interleaved for the RDFT format variant
      96           2 :         avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
      97           2 :         sample_rate  *= avctx->channels;
      98           2 :         s->channels = 1;
      99           2 :         if (!s->version_b)
     100           2 :             frame_len_bits += av_log2(avctx->channels);
     101             :     } else {
     102           4 :         s->channels = avctx->channels;
     103           4 :         avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
     104             :     }
     105             : 
     106           6 :     s->frame_len     = 1 << frame_len_bits;
     107           6 :     s->overlap_len   = s->frame_len / 16;
     108           6 :     s->block_size    = (s->frame_len - s->overlap_len) * s->channels;
     109           6 :     sample_rate_half = (sample_rate + 1) / 2;
     110           6 :     if (avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
     111           2 :         s->root = 2.0 / (sqrt(s->frame_len) * 32768.0);
     112             :     else
     113           4 :         s->root = s->frame_len / (sqrt(s->frame_len) * 32768.0);
     114         582 :     for (i = 0; i < 96; i++) {
     115             :         /* constant is result of 0.066399999/log10(M_E) */
     116         576 :         quant_table[i] = expf(i * 0.15289164787221953823f) * s->root;
     117             :     }
     118             : 
     119             :     /* calculate number of bands */
     120         150 :     for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
     121         144 :         if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1])
     122           0 :             break;
     123             : 
     124           6 :     s->bands = av_malloc((s->num_bands + 1) * sizeof(*s->bands));
     125           6 :     if (!s->bands)
     126           0 :         return AVERROR(ENOMEM);
     127             : 
     128             :     /* populate bands data */
     129           6 :     s->bands[0] = 2;
     130         150 :     for (i = 1; i < s->num_bands; i++)
     131         144 :         s->bands[i] = (ff_wma_critical_freqs[i - 1] * s->frame_len / sample_rate_half) & ~1;
     132           6 :     s->bands[s->num_bands] = s->frame_len;
     133             : 
     134           6 :     s->first = 1;
     135             : 
     136           6 :     if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
     137           2 :         ff_rdft_init(&s->trans.rdft, frame_len_bits, DFT_C2R);
     138             :     else if (CONFIG_BINKAUDIO_DCT_DECODER)
     139           4 :         ff_dct_init(&s->trans.dct, frame_len_bits, DCT_III);
     140             :     else
     141             :         return -1;
     142             : 
     143           6 :     return 0;
     144             : }
     145             : 
     146         582 : static float get_float(GetBitContext *gb)
     147             : {
     148         582 :     int power = get_bits(gb, 5);
     149         582 :     float f = ldexpf(get_bits_long(gb, 23), power - 23);
     150         582 :     if (get_bits1(gb))
     151         314 :         f = -f;
     152         582 :     return f;
     153             : }
     154             : 
     155             : static const uint8_t rle_length_tab[16] = {
     156             :     2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
     157             : };
     158             : 
     159             : /**
     160             :  * Decode Bink Audio block
     161             :  * @param[out] out Output buffer (must contain s->block_size elements)
     162             :  * @return 0 on success, negative error code on failure
     163             :  */
     164         172 : static int decode_block(BinkAudioContext *s, float **out, int use_dct)
     165             : {
     166             :     int ch, i, j, k;
     167             :     float q, quant[25];
     168             :     int width, coeff;
     169         172 :     GetBitContext *gb = &s->gb;
     170             : 
     171         172 :     if (use_dct)
     172         119 :         skip_bits(gb, 2);
     173             : 
     174         463 :     for (ch = 0; ch < s->channels; ch++) {
     175         291 :         FFTSample *coeffs = out[ch];
     176             : 
     177         291 :         if (s->version_b) {
     178           0 :             if (get_bits_left(gb) < 64)
     179           0 :                 return AVERROR_INVALIDDATA;
     180           0 :             coeffs[0] = av_int2float(get_bits_long(gb, 32)) * s->root;
     181           0 :             coeffs[1] = av_int2float(get_bits_long(gb, 32)) * s->root;
     182             :         } else {
     183         291 :             if (get_bits_left(gb) < 58)
     184           0 :                 return AVERROR_INVALIDDATA;
     185         291 :             coeffs[0] = get_float(gb) * s->root;
     186         291 :             coeffs[1] = get_float(gb) * s->root;
     187             :         }
     188             : 
     189         291 :         if (get_bits_left(gb) < s->num_bands * 8)
     190           0 :             return AVERROR_INVALIDDATA;
     191        7566 :         for (i = 0; i < s->num_bands; i++) {
     192        7275 :             int value = get_bits(gb, 8);
     193        7275 :             quant[i]  = quant_table[FFMIN(value, 95)];
     194             :         }
     195             : 
     196         291 :         k = 0;
     197         291 :         q = quant[0];
     198             : 
     199             :         // parse coefficients
     200         291 :         i = 2;
     201       21903 :         while (i < s->frame_len) {
     202       21321 :             if (s->version_b) {
     203           0 :                 j = i + 16;
     204             :             } else {
     205       21321 :                 int v = get_bits1(gb);
     206       21321 :                 if (v) {
     207        7766 :                     v = get_bits(gb, 4);
     208        7766 :                     j = i + rle_length_tab[v] * 8;
     209             :                 } else {
     210       13555 :                     j = i + 8;
     211             :                 }
     212             :             }
     213             : 
     214       21321 :             j = FFMIN(j, s->frame_len);
     215             : 
     216       21321 :             width = get_bits(gb, 4);
     217       21321 :             if (width == 0) {
     218         827 :                 memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
     219         827 :                 i = j;
     220        1916 :                 while (s->bands[k] < i)
     221         262 :                     q = quant[k++];
     222             :             } else {
     223      391998 :                 while (i < j) {
     224      351010 :                     if (s->bands[k] == i)
     225        7013 :                         q = quant[k++];
     226      351010 :                     coeff = get_bits(gb, width);
     227      351010 :                     if (coeff) {
     228             :                         int v;
     229      286706 :                         v = get_bits1(gb);
     230      286706 :                         if (v)
     231      138673 :                             coeffs[i] = -q * coeff;
     232             :                         else
     233      148033 :                             coeffs[i] =  q * coeff;
     234             :                     } else {
     235       64304 :                         coeffs[i] = 0.0f;
     236             :                     }
     237      351010 :                     i++;
     238             :                 }
     239             :             }
     240             :         }
     241             : 
     242         291 :         if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
     243         238 :             coeffs[0] /= 0.5;
     244         238 :             s->trans.dct.dct_calc(&s->trans.dct,  coeffs);
     245             :         }
     246             :         else if (CONFIG_BINKAUDIO_RDFT_DECODER)
     247          53 :             s->trans.rdft.rdft_calc(&s->trans.rdft, coeffs);
     248             :     }
     249             : 
     250         463 :     for (ch = 0; ch < s->channels; ch++) {
     251             :         int j;
     252         291 :         int count = s->overlap_len * s->channels;
     253         291 :         if (!s->first) {
     254         288 :             j = ch;
     255       28704 :             for (i = 0; i < s->overlap_len; i++, j += s->channels)
     256       85248 :                 out[ch][i] = (s->previous[ch][i] * (count - j) +
     257       56832 :                                       out[ch][i] *          j) / count;
     258             :         }
     259         291 :         memcpy(s->previous[ch], &out[ch][s->frame_len - s->overlap_len],
     260         291 :                s->overlap_len * sizeof(*s->previous[ch]));
     261             :     }
     262             : 
     263         172 :     s->first = 0;
     264             : 
     265         172 :     return 0;
     266             : }
     267             : 
     268           6 : static av_cold int decode_end(AVCodecContext *avctx)
     269             : {
     270           6 :     BinkAudioContext * s = avctx->priv_data;
     271           6 :     av_freep(&s->bands);
     272           6 :     av_freep(&s->packet_buffer);
     273           6 :     if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
     274           2 :         ff_rdft_end(&s->trans.rdft);
     275             :     else if (CONFIG_BINKAUDIO_DCT_DECODER)
     276           4 :         ff_dct_end(&s->trans.dct);
     277             : 
     278           6 :     return 0;
     279             : }
     280             : 
     281         172 : static void get_bits_align32(GetBitContext *s)
     282             : {
     283         172 :     int n = (-get_bits_count(s)) & 31;
     284         172 :     if (n) skip_bits(s, n);
     285         172 : }
     286             : 
     287         174 : static int decode_frame(AVCodecContext *avctx, void *data,
     288             :                         int *got_frame_ptr, AVPacket *avpkt)
     289             : {
     290         174 :     BinkAudioContext *s = avctx->priv_data;
     291         174 :     AVFrame *frame      = data;
     292         174 :     GetBitContext *gb = &s->gb;
     293         174 :     int ret, consumed = 0;
     294             : 
     295         174 :     if (!get_bits_left(gb)) {
     296             :         uint8_t *buf;
     297             :         /* handle end-of-stream */
     298          94 :         if (!avpkt->size) {
     299           2 :             *got_frame_ptr = 0;
     300           2 :             return 0;
     301             :         }
     302          92 :         if (avpkt->size < 4) {
     303           0 :             av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
     304           0 :             return AVERROR_INVALIDDATA;
     305             :         }
     306          92 :         buf = av_realloc(s->packet_buffer, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
     307          92 :         if (!buf)
     308           0 :             return AVERROR(ENOMEM);
     309          92 :         memset(buf + avpkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
     310          92 :         s->packet_buffer = buf;
     311          92 :         memcpy(s->packet_buffer, avpkt->data, avpkt->size);
     312          92 :         if ((ret = init_get_bits8(gb, s->packet_buffer, avpkt->size)) < 0)
     313           0 :             return ret;
     314          92 :         consumed = avpkt->size;
     315             : 
     316             :         /* skip reported size */
     317          92 :         skip_bits_long(gb, 32);
     318             :     }
     319             : 
     320             :     /* get output buffer */
     321         172 :     frame->nb_samples = s->frame_len;
     322         172 :     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
     323           0 :         return ret;
     324             : 
     325         172 :     if (decode_block(s, (float **)frame->extended_data,
     326         172 :                      avctx->codec->id == AV_CODEC_ID_BINKAUDIO_DCT)) {
     327           0 :         av_log(avctx, AV_LOG_ERROR, "Incomplete packet\n");
     328           0 :         return AVERROR_INVALIDDATA;
     329             :     }
     330         172 :     get_bits_align32(gb);
     331             : 
     332         172 :     frame->nb_samples = s->block_size / avctx->channels;
     333         172 :     *got_frame_ptr    = 1;
     334             : 
     335         172 :     return consumed;
     336             : }
     337             : 
     338             : AVCodec ff_binkaudio_rdft_decoder = {
     339             :     .name           = "binkaudio_rdft",
     340             :     .long_name      = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)"),
     341             :     .type           = AVMEDIA_TYPE_AUDIO,
     342             :     .id             = AV_CODEC_ID_BINKAUDIO_RDFT,
     343             :     .priv_data_size = sizeof(BinkAudioContext),
     344             :     .init           = decode_init,
     345             :     .close          = decode_end,
     346             :     .decode         = decode_frame,
     347             :     .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
     348             : };
     349             : 
     350             : AVCodec ff_binkaudio_dct_decoder = {
     351             :     .name           = "binkaudio_dct",
     352             :     .long_name      = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)"),
     353             :     .type           = AVMEDIA_TYPE_AUDIO,
     354             :     .id             = AV_CODEC_ID_BINKAUDIO_DCT,
     355             :     .priv_data_size = sizeof(BinkAudioContext),
     356             :     .init           = decode_init,
     357             :     .close          = decode_end,
     358             :     .decode         = decode_frame,
     359             :     .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
     360             : };

Generated by: LCOV version 1.13