LCOV - code coverage report
Current view: top level - src/libavcodec - binkaudio.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 138 160 86.2 %
Date: 2017-05-26 21:11:57 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 "decode.h"
      38             : #include "get_bits.h"
      39             : #include "internal.h"
      40             : #include "rdft.h"
      41             : #include "wma_freqs.h"
      42             : 
      43             : static float quant_table[96];
      44             : 
      45             : #define MAX_CHANNELS 2
      46             : #define BINK_BLOCK_MAX_SIZE (MAX_CHANNELS << 11)
      47             : 
      48             : typedef struct BinkAudioContext {
      49             :     GetBitContext gb;
      50             :     int version_b;          ///< Bink version 'b'
      51             :     int first;
      52             :     int channels;
      53             :     int frame_len;          ///< transform size (samples)
      54             :     int overlap_len;        ///< overlap size (samples)
      55             :     int block_size;
      56             :     int num_bands;
      57             :     unsigned int *bands;
      58             :     float root;
      59             :     DECLARE_ALIGNED(32, FFTSample, coeffs)[BINK_BLOCK_MAX_SIZE];
      60             :     float previous[MAX_CHANNELS][BINK_BLOCK_MAX_SIZE / 16];  ///< coeffs from previous audio block
      61             :     AVPacket *pkt;
      62             :     union {
      63             :         RDFTContext rdft;
      64             :         DCTContext dct;
      65             :     } trans;
      66             : } BinkAudioContext;
      67             : 
      68             : 
      69           6 : static av_cold int decode_init(AVCodecContext *avctx)
      70             : {
      71           6 :     BinkAudioContext *s = avctx->priv_data;
      72           6 :     int sample_rate = avctx->sample_rate;
      73             :     int sample_rate_half;
      74             :     int i;
      75             :     int frame_len_bits;
      76             : 
      77             :     /* determine frame length */
      78           6 :     if (avctx->sample_rate < 22050) {
      79           0 :         frame_len_bits = 9;
      80           6 :     } else if (avctx->sample_rate < 44100) {
      81           2 :         frame_len_bits = 10;
      82             :     } else {
      83           4 :         frame_len_bits = 11;
      84             :     }
      85             : 
      86           6 :     if (avctx->channels < 1 || avctx->channels > MAX_CHANNELS) {
      87           0 :         av_log(avctx, AV_LOG_ERROR, "invalid number of channels: %d\n", avctx->channels);
      88           0 :         return AVERROR_INVALIDDATA;
      89             :     }
      90           6 :     avctx->channel_layout = avctx->channels == 1 ? AV_CH_LAYOUT_MONO :
      91             :                                                    AV_CH_LAYOUT_STEREO;
      92             : 
      93           6 :     s->version_b = avctx->extradata_size >= 4 && avctx->extradata[3] == 'b';
      94             : 
      95           6 :     if (avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT) {
      96             :         // audio is already interleaved for the RDFT format variant
      97           2 :         avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
      98           2 :         sample_rate  *= avctx->channels;
      99           2 :         s->channels = 1;
     100           2 :         if (!s->version_b)
     101           2 :             frame_len_bits += av_log2(avctx->channels);
     102             :     } else {
     103           4 :         s->channels = avctx->channels;
     104           4 :         avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
     105             :     }
     106             : 
     107           6 :     s->frame_len     = 1 << frame_len_bits;
     108           6 :     s->overlap_len   = s->frame_len / 16;
     109           6 :     s->block_size    = (s->frame_len - s->overlap_len) * s->channels;
     110           6 :     sample_rate_half = (sample_rate + 1) / 2;
     111           6 :     if (avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
     112           2 :         s->root = 2.0 / (sqrt(s->frame_len) * 32768.0);
     113             :     else
     114           4 :         s->root = s->frame_len / (sqrt(s->frame_len) * 32768.0);
     115         582 :     for (i = 0; i < 96; i++) {
     116             :         /* constant is result of 0.066399999/log10(M_E) */
     117         576 :         quant_table[i] = expf(i * 0.15289164787221953823f) * s->root;
     118             :     }
     119             : 
     120             :     /* calculate number of bands */
     121         150 :     for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
     122         144 :         if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1])
     123           0 :             break;
     124             : 
     125           6 :     s->bands = av_malloc((s->num_bands + 1) * sizeof(*s->bands));
     126           6 :     if (!s->bands)
     127           0 :         return AVERROR(ENOMEM);
     128             : 
     129             :     /* populate bands data */
     130           6 :     s->bands[0] = 2;
     131         150 :     for (i = 1; i < s->num_bands; i++)
     132         144 :         s->bands[i] = (ff_wma_critical_freqs[i - 1] * s->frame_len / sample_rate_half) & ~1;
     133           6 :     s->bands[s->num_bands] = s->frame_len;
     134             : 
     135           6 :     s->first = 1;
     136             : 
     137           6 :     if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
     138           2 :         ff_rdft_init(&s->trans.rdft, frame_len_bits, DFT_C2R);
     139             :     else if (CONFIG_BINKAUDIO_DCT_DECODER)
     140           4 :         ff_dct_init(&s->trans.dct, frame_len_bits, DCT_III);
     141             :     else
     142             :         return -1;
     143             : 
     144           6 :     s->pkt = av_packet_alloc();
     145           6 :     if (!s->pkt)
     146           0 :         return AVERROR(ENOMEM);
     147             : 
     148           6 :     return 0;
     149             : }
     150             : 
     151         582 : static float get_float(GetBitContext *gb)
     152             : {
     153         582 :     int power = get_bits(gb, 5);
     154         582 :     float f = ldexpf(get_bits_long(gb, 23), power - 23);
     155         582 :     if (get_bits1(gb))
     156         314 :         f = -f;
     157         582 :     return f;
     158             : }
     159             : 
     160             : static const uint8_t rle_length_tab[16] = {
     161             :     2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
     162             : };
     163             : 
     164             : /**
     165             :  * Decode Bink Audio block
     166             :  * @param[out] out Output buffer (must contain s->block_size elements)
     167             :  * @return 0 on success, negative error code on failure
     168             :  */
     169         172 : static int decode_block(BinkAudioContext *s, float **out, int use_dct)
     170             : {
     171             :     int ch, i, j, k;
     172             :     float q, quant[25];
     173             :     int width, coeff;
     174         172 :     GetBitContext *gb = &s->gb;
     175             : 
     176         172 :     if (use_dct)
     177         119 :         skip_bits(gb, 2);
     178             : 
     179         463 :     for (ch = 0; ch < s->channels; ch++) {
     180         291 :         FFTSample *coeffs = out[ch];
     181             : 
     182         291 :         if (s->version_b) {
     183           0 :             if (get_bits_left(gb) < 64)
     184           0 :                 return AVERROR_INVALIDDATA;
     185           0 :             coeffs[0] = av_int2float(get_bits_long(gb, 32)) * s->root;
     186           0 :             coeffs[1] = av_int2float(get_bits_long(gb, 32)) * s->root;
     187             :         } else {
     188         291 :             if (get_bits_left(gb) < 58)
     189           0 :                 return AVERROR_INVALIDDATA;
     190         291 :             coeffs[0] = get_float(gb) * s->root;
     191         291 :             coeffs[1] = get_float(gb) * s->root;
     192             :         }
     193             : 
     194         291 :         if (get_bits_left(gb) < s->num_bands * 8)
     195           0 :             return AVERROR_INVALIDDATA;
     196        7566 :         for (i = 0; i < s->num_bands; i++) {
     197        7275 :             int value = get_bits(gb, 8);
     198        7275 :             quant[i]  = quant_table[FFMIN(value, 95)];
     199             :         }
     200             : 
     201         291 :         k = 0;
     202         291 :         q = quant[0];
     203             : 
     204             :         // parse coefficients
     205         291 :         i = 2;
     206       21903 :         while (i < s->frame_len) {
     207       21321 :             if (s->version_b) {
     208           0 :                 j = i + 16;
     209             :             } else {
     210       21321 :                 int v = get_bits1(gb);
     211       21321 :                 if (v) {
     212        7766 :                     v = get_bits(gb, 4);
     213        7766 :                     j = i + rle_length_tab[v] * 8;
     214             :                 } else {
     215       13555 :                     j = i + 8;
     216             :                 }
     217             :             }
     218             : 
     219       21321 :             j = FFMIN(j, s->frame_len);
     220             : 
     221       21321 :             width = get_bits(gb, 4);
     222       21321 :             if (width == 0) {
     223         827 :                 memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
     224         827 :                 i = j;
     225        1916 :                 while (s->bands[k] < i)
     226         262 :                     q = quant[k++];
     227             :             } else {
     228      391998 :                 while (i < j) {
     229      351010 :                     if (s->bands[k] == i)
     230        7013 :                         q = quant[k++];
     231      351010 :                     coeff = get_bits(gb, width);
     232      351010 :                     if (coeff) {
     233             :                         int v;
     234      286706 :                         v = get_bits1(gb);
     235      286706 :                         if (v)
     236      138673 :                             coeffs[i] = -q * coeff;
     237             :                         else
     238      148033 :                             coeffs[i] =  q * coeff;
     239             :                     } else {
     240       64304 :                         coeffs[i] = 0.0f;
     241             :                     }
     242      351010 :                     i++;
     243             :                 }
     244             :             }
     245             :         }
     246             : 
     247         291 :         if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
     248         238 :             coeffs[0] /= 0.5;
     249         238 :             s->trans.dct.dct_calc(&s->trans.dct,  coeffs);
     250             :         }
     251             :         else if (CONFIG_BINKAUDIO_RDFT_DECODER)
     252          53 :             s->trans.rdft.rdft_calc(&s->trans.rdft, coeffs);
     253             :     }
     254             : 
     255         463 :     for (ch = 0; ch < s->channels; ch++) {
     256             :         int j;
     257         291 :         int count = s->overlap_len * s->channels;
     258         291 :         if (!s->first) {
     259         288 :             j = ch;
     260       28704 :             for (i = 0; i < s->overlap_len; i++, j += s->channels)
     261       85248 :                 out[ch][i] = (s->previous[ch][i] * (count - j) +
     262       56832 :                                       out[ch][i] *          j) / count;
     263             :         }
     264         291 :         memcpy(s->previous[ch], &out[ch][s->frame_len - s->overlap_len],
     265         291 :                s->overlap_len * sizeof(*s->previous[ch]));
     266             :     }
     267             : 
     268         172 :     s->first = 0;
     269             : 
     270         172 :     return 0;
     271             : }
     272             : 
     273           6 : static av_cold int decode_end(AVCodecContext *avctx)
     274             : {
     275           6 :     BinkAudioContext * s = avctx->priv_data;
     276           6 :     av_freep(&s->bands);
     277           6 :     if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
     278           2 :         ff_rdft_end(&s->trans.rdft);
     279             :     else if (CONFIG_BINKAUDIO_DCT_DECODER)
     280           4 :         ff_dct_end(&s->trans.dct);
     281             : 
     282           6 :     av_packet_free(&s->pkt);
     283             : 
     284           6 :     return 0;
     285             : }
     286             : 
     287         172 : static void get_bits_align32(GetBitContext *s)
     288             : {
     289         172 :     int n = (-get_bits_count(s)) & 31;
     290         172 :     if (n) skip_bits(s, n);
     291         172 : }
     292             : 
     293         268 : static int binkaudio_receive_frame(AVCodecContext *avctx, AVFrame *frame)
     294             : {
     295         268 :     BinkAudioContext *s = avctx->priv_data;
     296         268 :     GetBitContext *gb = &s->gb;
     297             :     int ret;
     298             : 
     299         268 :     if (!s->pkt->data) {
     300         188 :         ret = ff_decode_get_packet(avctx, s->pkt);
     301         188 :         if (ret < 0)
     302          96 :             return ret;
     303             : 
     304          92 :         if (s->pkt->size < 4) {
     305           0 :             av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
     306           0 :             ret = AVERROR_INVALIDDATA;
     307           0 :             goto fail;
     308             :         }
     309             : 
     310          92 :         ret = init_get_bits8(gb, s->pkt->data, s->pkt->size);
     311          92 :         if (ret < 0)
     312           0 :             goto fail;
     313             : 
     314             :         /* skip reported size */
     315          92 :         skip_bits_long(gb, 32);
     316             :     }
     317             : 
     318             :     /* get output buffer */
     319         172 :     frame->nb_samples = s->frame_len;
     320         172 :     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
     321           0 :         return ret;
     322             : 
     323         172 :     if (decode_block(s, (float **)frame->extended_data,
     324         172 :                      avctx->codec->id == AV_CODEC_ID_BINKAUDIO_DCT)) {
     325           0 :         av_log(avctx, AV_LOG_ERROR, "Incomplete packet\n");
     326           0 :         return AVERROR_INVALIDDATA;
     327             :     }
     328         172 :     get_bits_align32(gb);
     329         172 :     if (!get_bits_left(gb)) {
     330          92 :         memset(gb, 0, sizeof(*gb));
     331          92 :         av_packet_unref(s->pkt);
     332             :     }
     333             : 
     334         172 :     frame->nb_samples = s->block_size / avctx->channels;
     335             : 
     336         172 :     return 0;
     337             : fail:
     338           0 :     av_packet_unref(s->pkt);
     339           0 :     return ret;
     340             : }
     341             : 
     342             : AVCodec ff_binkaudio_rdft_decoder = {
     343             :     .name           = "binkaudio_rdft",
     344             :     .long_name      = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)"),
     345             :     .type           = AVMEDIA_TYPE_AUDIO,
     346             :     .id             = AV_CODEC_ID_BINKAUDIO_RDFT,
     347             :     .priv_data_size = sizeof(BinkAudioContext),
     348             :     .init           = decode_init,
     349             :     .close          = decode_end,
     350             :     .receive_frame  = binkaudio_receive_frame,
     351             :     .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
     352             : };
     353             : 
     354             : AVCodec ff_binkaudio_dct_decoder = {
     355             :     .name           = "binkaudio_dct",
     356             :     .long_name      = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)"),
     357             :     .type           = AVMEDIA_TYPE_AUDIO,
     358             :     .id             = AV_CODEC_ID_BINKAUDIO_DCT,
     359             :     .priv_data_size = sizeof(BinkAudioContext),
     360             :     .init           = decode_init,
     361             :     .close          = decode_end,
     362             :     .receive_frame  = binkaudio_receive_frame,
     363             :     .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
     364             : };

Generated by: LCOV version 1.13