binkaudio.c 10.8 KB
Newer Older
Peter Ross's avatar
Peter Ross committed
1 2
/*
 * Bink Audio decoder
3
 * Copyright (c) 2007-2011 Peter Ross (pross@xvid.org)
Peter Ross's avatar
Peter Ross committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * Copyright (c) 2009 Daniel Verkamp (daniel@drv.nu)
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
24
 * @file
Peter Ross's avatar
Peter Ross committed
25 26 27 28 29 30
 * Bink Audio decoder
 *
 * Technical details here:
 *  http://wiki.multimedia.cx/index.php?title=Bink_Audio
 */

31
#include "libavutil/channel_layout.h"
32 33
#include "libavutil/intfloat.h"

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

Peter Ross's avatar
Peter Ross committed
45 46 47
#define MAX_CHANNELS 2
#define BINK_BLOCK_MAX_SIZE (MAX_CHANNELS << 11)

48
typedef struct BinkAudioContext {
Peter Ross's avatar
Peter Ross committed
49
    GetBitContext gb;
50
    int version_b;          ///< Bink version 'b'
Peter Ross's avatar
Peter Ross committed
51 52 53 54 55 56 57 58
    int first;
    int channels;
    int frame_len;          ///< transform size (samples)
    int overlap_len;        ///< overlap size (samples)
    int block_size;
    int num_bands;
    unsigned int *bands;
    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;
Peter Ross's avatar
Peter Ross committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    union {
        RDFTContext rdft;
        DCTContext dct;
    } trans;
} BinkAudioContext;


static av_cold int decode_init(AVCodecContext *avctx)
{
    BinkAudioContext *s = avctx->priv_data;
    int sample_rate = avctx->sample_rate;
    int sample_rate_half;
    int i;
    int frame_len_bits;

    /* determine frame length */
    if (avctx->sample_rate < 22050) {
        frame_len_bits = 9;
    } else if (avctx->sample_rate < 44100) {
        frame_len_bits = 10;
    } else {
        frame_len_bits = 11;
    }

86 87 88
    if (avctx->channels < 1 || avctx->channels > MAX_CHANNELS) {
        av_log(avctx, AV_LOG_ERROR, "invalid number of channels: %d\n", avctx->channels);
        return AVERROR_INVALIDDATA;
Peter Ross's avatar
Peter Ross committed
89
    }
90 91
    avctx->channel_layout = avctx->channels == 1 ? AV_CH_LAYOUT_MONO :
                                                   AV_CH_LAYOUT_STEREO;
Peter Ross's avatar
Peter Ross committed
92

93
    s->version_b = avctx->extradata_size >= 4 && avctx->extradata[3] == 'b';
94

95
    if (avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT) {
Peter Ross's avatar
Peter Ross committed
96
        // audio is already interleaved for the RDFT format variant
97
        avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
Peter Ross's avatar
Peter Ross committed
98 99
        sample_rate  *= avctx->channels;
        s->channels = 1;
100 101
        if (!s->version_b)
            frame_len_bits += av_log2(avctx->channels);
Peter Ross's avatar
Peter Ross committed
102 103
    } else {
        s->channels = avctx->channels;
104
        avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
Peter Ross's avatar
Peter Ross committed
105 106
    }

107
    s->frame_len     = 1 << frame_len_bits;
Peter Ross's avatar
Peter Ross committed
108 109 110
    s->overlap_len   = s->frame_len / 16;
    s->block_size    = (s->frame_len - s->overlap_len) * s->channels;
    sample_rate_half = (sample_rate + 1) / 2;
111
    if (avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
112
        s->root = 2.0 / (sqrt(s->frame_len) * 32768.0);
113
    else
114
        s->root = s->frame_len / (sqrt(s->frame_len) * 32768.0);
115
    for (i = 0; i < 96; i++) {
116 117 118
        /* constant is result of 0.066399999/log10(M_E) */
        quant_table[i] = expf(i * 0.15289164787221953823f) * s->root;
    }
Peter Ross's avatar
Peter Ross committed
119 120 121 122 123 124 125 126 127 128 129

    /* calculate number of bands */
    for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
        if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1])
            break;

    s->bands = av_malloc((s->num_bands + 1) * sizeof(*s->bands));
    if (!s->bands)
        return AVERROR(ENOMEM);

    /* populate bands data */
130
    s->bands[0] = 2;
Peter Ross's avatar
Peter Ross committed
131
    for (i = 1; i < s->num_bands; i++)
132 133
        s->bands[i] = (ff_wma_critical_freqs[i - 1] * s->frame_len / sample_rate_half) & ~1;
    s->bands[s->num_bands] = s->frame_len;
Peter Ross's avatar
Peter Ross committed
134 135 136

    s->first = 1;

137
    if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
138
        ff_rdft_init(&s->trans.rdft, frame_len_bits, DFT_C2R);
139
    else if (CONFIG_BINKAUDIO_DCT_DECODER)
140
        ff_dct_init(&s->trans.dct, frame_len_bits, DCT_III);
141
    else
142
        av_assert0(0);
Peter Ross's avatar
Peter Ross committed
143

144 145 146 147
    s->pkt = av_packet_alloc();
    if (!s->pkt)
        return AVERROR(ENOMEM);

Peter Ross's avatar
Peter Ross committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    return 0;
}

static float get_float(GetBitContext *gb)
{
    int power = get_bits(gb, 5);
    float f = ldexpf(get_bits_long(gb, 23), power - 23);
    if (get_bits1(gb))
        f = -f;
    return f;
}

static const uint8_t rle_length_tab[16] = {
    2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
};

/**
 * Decode Bink Audio block
 * @param[out] out Output buffer (must contain s->block_size elements)
167
 * @return 0 on success, negative error code on failure
Peter Ross's avatar
Peter Ross committed
168
 */
169
static int decode_block(BinkAudioContext *s, float **out, int use_dct)
Peter Ross's avatar
Peter Ross committed
170 171 172 173 174 175 176 177 178 179
{
    int ch, i, j, k;
    float q, quant[25];
    int width, coeff;
    GetBitContext *gb = &s->gb;

    if (use_dct)
        skip_bits(gb, 2);

    for (ch = 0; ch < s->channels; ch++) {
180 181
        FFTSample *coeffs = out[ch];

182
        if (s->version_b) {
183 184
            if (get_bits_left(gb) < 64)
                return AVERROR_INVALIDDATA;
185 186
            coeffs[0] = av_int2float(get_bits_long(gb, 32)) * s->root;
            coeffs[1] = av_int2float(get_bits_long(gb, 32)) * s->root;
187
        } else {
188 189
            if (get_bits_left(gb) < 58)
                return AVERROR_INVALIDDATA;
190 191 192
            coeffs[0] = get_float(gb) * s->root;
            coeffs[1] = get_float(gb) * s->root;
        }
Peter Ross's avatar
Peter Ross committed
193

194 195
        if (get_bits_left(gb) < s->num_bands * 8)
            return AVERROR_INVALIDDATA;
Peter Ross's avatar
Peter Ross committed
196 197
        for (i = 0; i < s->num_bands; i++) {
            int value = get_bits(gb, 8);
198
            quant[i]  = quant_table[FFMIN(value, 95)];
Peter Ross's avatar
Peter Ross committed
199 200
        }

201 202
        k = 0;
        q = quant[0];
Peter Ross's avatar
Peter Ross committed
203 204 205 206

        // parse coefficients
        i = 2;
        while (i < s->frame_len) {
207 208
            if (s->version_b) {
                j = i + 16;
Peter Ross's avatar
Peter Ross committed
209
            } else {
210
                int v = get_bits1(gb);
211
                if (v) {
212
                    v = get_bits(gb, 4);
213 214 215 216
                    j = i + rle_length_tab[v] * 8;
                } else {
                    j = i + 8;
                }
Peter Ross's avatar
Peter Ross committed
217 218 219 220
            }

            j = FFMIN(j, s->frame_len);

221
            width = get_bits(gb, 4);
Peter Ross's avatar
Peter Ross committed
222 223 224
            if (width == 0) {
                memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
                i = j;
225
                while (s->bands[k] < i)
Peter Ross's avatar
Peter Ross committed
226 227 228
                    q = quant[k++];
            } else {
                while (i < j) {
229
                    if (s->bands[k] == i)
Peter Ross's avatar
Peter Ross committed
230
                        q = quant[k++];
231
                    coeff = get_bits(gb, width);
Peter Ross's avatar
Peter Ross committed
232
                    if (coeff) {
233
                        int v;
234
                        v = get_bits1(gb);
235
                        if (v)
Peter Ross's avatar
Peter Ross committed
236 237 238 239 240 241 242 243 244 245 246
                            coeffs[i] = -q * coeff;
                        else
                            coeffs[i] =  q * coeff;
                    } else {
                        coeffs[i] = 0.0f;
                    }
                    i++;
                }
            }
        }

247 248
        if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
            coeffs[0] /= 0.5;
249
            s->trans.dct.dct_calc(&s->trans.dct,  coeffs);
250
        }
251
        else if (CONFIG_BINKAUDIO_RDFT_DECODER)
252
            s->trans.rdft.rdft_calc(&s->trans.rdft, coeffs);
Peter Ross's avatar
Peter Ross committed
253 254
    }

255 256
    for (ch = 0; ch < s->channels; ch++) {
        int j;
Peter Ross's avatar
Peter Ross committed
257
        int count = s->overlap_len * s->channels;
258 259 260 261 262
        if (!s->first) {
            j = ch;
            for (i = 0; i < s->overlap_len; i++, j += s->channels)
                out[ch][i] = (s->previous[ch][i] * (count - j) +
                                      out[ch][i] *          j) / count;
Peter Ross's avatar
Peter Ross committed
263
        }
264 265
        memcpy(s->previous[ch], &out[ch][s->frame_len - s->overlap_len],
               s->overlap_len * sizeof(*s->previous[ch]));
Peter Ross's avatar
Peter Ross committed
266 267 268
    }

    s->first = 0;
269 270

    return 0;
Peter Ross's avatar
Peter Ross committed
271 272 273 274 275 276
}

static av_cold int decode_end(AVCodecContext *avctx)
{
    BinkAudioContext * s = avctx->priv_data;
    av_freep(&s->bands);
277
    if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
Peter Ross's avatar
Peter Ross committed
278
        ff_rdft_end(&s->trans.rdft);
279
    else if (CONFIG_BINKAUDIO_DCT_DECODER)
Peter Ross's avatar
Peter Ross committed
280
        ff_dct_end(&s->trans.dct);
281

282 283
    av_packet_free(&s->pkt);

Peter Ross's avatar
Peter Ross committed
284 285 286 287 288 289 290 291 292
    return 0;
}

static void get_bits_align32(GetBitContext *s)
{
    int n = (-get_bits_count(s)) & 31;
    if (n) skip_bits(s, n);
}

293
static int binkaudio_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Peter Ross's avatar
Peter Ross committed
294 295 296
{
    BinkAudioContext *s = avctx->priv_data;
    GetBitContext *gb = &s->gb;
297
    int ret;
298

299 300 301 302 303 304
    if (!s->pkt->data) {
        ret = ff_decode_get_packet(avctx, s->pkt);
        if (ret < 0)
            return ret;

        if (s->pkt->size < 4) {
305
            av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
306 307
            ret = AVERROR_INVALIDDATA;
            goto fail;
308
        }
309

310
        ret = init_get_bits8(gb, s->pkt->data, s->pkt->size);
311 312
        if (ret < 0)
            goto fail;
313 314 315

        /* skip reported size */
        skip_bits_long(gb, 32);
316
    }
Peter Ross's avatar
Peter Ross committed
317

318
    /* get output buffer */
319
    frame->nb_samples = s->frame_len;
320
    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
321
        return ret;
Peter Ross's avatar
Peter Ross committed
322

323
    if (decode_block(s, (float **)frame->extended_data,
324
                     avctx->codec->id == AV_CODEC_ID_BINKAUDIO_DCT)) {
325 326
        av_log(avctx, AV_LOG_ERROR, "Incomplete packet\n");
        return AVERROR_INVALIDDATA;
Peter Ross's avatar
Peter Ross committed
327
    }
328
    get_bits_align32(gb);
329 330
    if (!get_bits_left(gb)) {
        memset(gb, 0, sizeof(*gb));
331 332
        av_packet_unref(s->pkt);
    }
Peter Ross's avatar
Peter Ross committed
333

334
    frame->nb_samples = s->block_size / avctx->channels;
335

336 337 338 339
    return 0;
fail:
    av_packet_unref(s->pkt);
    return ret;
Peter Ross's avatar
Peter Ross committed
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 348 349
    .priv_data_size = sizeof(BinkAudioContext),
    .init           = decode_init,
    .close          = decode_end,
350
    .receive_frame  = binkaudio_receive_frame,
351
    .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
Peter Ross's avatar
Peter Ross committed
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 360 361
    .priv_data_size = sizeof(BinkAudioContext),
    .init           = decode_init,
    .close          = decode_end,
362
    .receive_frame  = binkaudio_receive_frame,
363
    .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
Peter Ross's avatar
Peter Ross committed
364
};