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