GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/binkaudio.c Lines: 139 163 85.3 %
Date: 2019-11-22 03:34:36 Branches: 76 96 79.2 %

Line Branch Exec Source
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
        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
        av_log(avctx, AV_LOG_ERROR, "invalid number of channels: %d\n", avctx->channels);
88
        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
        if (sample_rate > INT_MAX / avctx->channels)
99
            return AVERROR_INVALIDDATA;
100
2
        sample_rate  *= avctx->channels;
101
2
        s->channels = 1;
102
2
        if (!s->version_b)
103
2
            frame_len_bits += av_log2(avctx->channels);
104
    } else {
105
4
        s->channels = avctx->channels;
106
4
        avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
107
    }
108
109
6
    s->frame_len     = 1 << frame_len_bits;
110
6
    s->overlap_len   = s->frame_len / 16;
111
6
    s->block_size    = (s->frame_len - s->overlap_len) * s->channels;
112
6
    sample_rate_half = (sample_rate + 1) / 2;
113
6
    if (avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
114
2
        s->root = 2.0 / (sqrt(s->frame_len) * 32768.0);
115
    else
116
4
        s->root = s->frame_len / (sqrt(s->frame_len) * 32768.0);
117
582
    for (i = 0; i < 96; i++) {
118
        /* constant is result of 0.066399999/log10(M_E) */
119
576
        quant_table[i] = expf(i * 0.15289164787221953823f) * s->root;
120
    }
121
122
    /* calculate number of bands */
123
150
    for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
124
144
        if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1])
125
            break;
126
127
6
    s->bands = av_malloc((s->num_bands + 1) * sizeof(*s->bands));
128
6
    if (!s->bands)
129
        return AVERROR(ENOMEM);
130
131
    /* populate bands data */
132
6
    s->bands[0] = 2;
133
150
    for (i = 1; i < s->num_bands; i++)
134
144
        s->bands[i] = (ff_wma_critical_freqs[i - 1] * s->frame_len / sample_rate_half) & ~1;
135
6
    s->bands[s->num_bands] = s->frame_len;
136
137
6
    s->first = 1;
138
139
6
    if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
140
2
        ff_rdft_init(&s->trans.rdft, frame_len_bits, DFT_C2R);
141
    else if (CONFIG_BINKAUDIO_DCT_DECODER)
142
4
        ff_dct_init(&s->trans.dct, frame_len_bits, DCT_III);
143
    else
144
        av_assert0(0);
145
146
6
    s->pkt = av_packet_alloc();
147
6
    if (!s->pkt)
148
        return AVERROR(ENOMEM);
149
150
6
    return 0;
151
}
152
153
582
static float get_float(GetBitContext *gb)
154
{
155
582
    int power = get_bits(gb, 5);
156
582
    float f = ldexpf(get_bits_long(gb, 23), power - 23);
157
582
    if (get_bits1(gb))
158
314
        f = -f;
159
582
    return f;
160
}
161
162
static const uint8_t rle_length_tab[16] = {
163
    2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
164
};
165
166
/**
167
 * Decode Bink Audio block
168
 * @param[out] out Output buffer (must contain s->block_size elements)
169
 * @return 0 on success, negative error code on failure
170
 */
171
172
static int decode_block(BinkAudioContext *s, float **out, int use_dct)
172
{
173
    int ch, i, j, k;
174
    float q, quant[25];
175
    int width, coeff;
176
172
    GetBitContext *gb = &s->gb;
177
178
172
    if (use_dct)
179
119
        skip_bits(gb, 2);
180
181
463
    for (ch = 0; ch < s->channels; ch++) {
182
291
        FFTSample *coeffs = out[ch];
183
184
291
        if (s->version_b) {
185
            if (get_bits_left(gb) < 64)
186
                return AVERROR_INVALIDDATA;
187
            coeffs[0] = av_int2float(get_bits_long(gb, 32)) * s->root;
188
            coeffs[1] = av_int2float(get_bits_long(gb, 32)) * s->root;
189
        } else {
190
291
            if (get_bits_left(gb) < 58)
191
                return AVERROR_INVALIDDATA;
192
291
            coeffs[0] = get_float(gb) * s->root;
193
291
            coeffs[1] = get_float(gb) * s->root;
194
        }
195
196
291
        if (get_bits_left(gb) < s->num_bands * 8)
197
            return AVERROR_INVALIDDATA;
198
7566
        for (i = 0; i < s->num_bands; i++) {
199
7275
            int value = get_bits(gb, 8);
200
7275
            quant[i]  = quant_table[FFMIN(value, 95)];
201
        }
202
203
291
        k = 0;
204
291
        q = quant[0];
205
206
        // parse coefficients
207
291
        i = 2;
208
21612
        while (i < s->frame_len) {
209
21321
            if (s->version_b) {
210
                j = i + 16;
211
            } else {
212
21321
                int v = get_bits1(gb);
213
21321
                if (v) {
214
7766
                    v = get_bits(gb, 4);
215
7766
                    j = i + rle_length_tab[v] * 8;
216
                } else {
217
13555
                    j = i + 8;
218
                }
219
            }
220
221
21321
            j = FFMIN(j, s->frame_len);
222
223
21321
            width = get_bits(gb, 4);
224
21321
            if (width == 0) {
225
827
                memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
226
827
                i = j;
227
1089
                while (s->bands[k] < i)
228
262
                    q = quant[k++];
229
            } else {
230
371504
                while (i < j) {
231
351010
                    if (s->bands[k] == i)
232
7013
                        q = quant[k++];
233
351010
                    coeff = get_bits(gb, width);
234
351010
                    if (coeff) {
235
                        int v;
236
286706
                        v = get_bits1(gb);
237
286706
                        if (v)
238
138673
                            coeffs[i] = -q * coeff;
239
                        else
240
148033
                            coeffs[i] =  q * coeff;
241
                    } else {
242
64304
                        coeffs[i] = 0.0f;
243
                    }
244
351010
                    i++;
245
                }
246
            }
247
        }
248
249
291
        if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
250
238
            coeffs[0] /= 0.5;
251
238
            s->trans.dct.dct_calc(&s->trans.dct,  coeffs);
252
        }
253
        else if (CONFIG_BINKAUDIO_RDFT_DECODER)
254
53
            s->trans.rdft.rdft_calc(&s->trans.rdft, coeffs);
255
    }
256
257
463
    for (ch = 0; ch < s->channels; ch++) {
258
        int j;
259
291
        int count = s->overlap_len * s->channels;
260
291
        if (!s->first) {
261
288
            j = ch;
262
28704
            for (i = 0; i < s->overlap_len; i++, j += s->channels)
263
28416
                out[ch][i] = (s->previous[ch][i] * (count - j) +
264
28416
                                      out[ch][i] *          j) / count;
265
        }
266
291
        memcpy(s->previous[ch], &out[ch][s->frame_len - s->overlap_len],
267
291
               s->overlap_len * sizeof(*s->previous[ch]));
268
    }
269
270
172
    s->first = 0;
271
272
172
    return 0;
273
}
274
275
6
static av_cold int decode_end(AVCodecContext *avctx)
276
{
277
6
    BinkAudioContext * s = avctx->priv_data;
278
6
    av_freep(&s->bands);
279
6
    if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
280
2
        ff_rdft_end(&s->trans.rdft);
281
    else if (CONFIG_BINKAUDIO_DCT_DECODER)
282
4
        ff_dct_end(&s->trans.dct);
283
284
6
    av_packet_free(&s->pkt);
285
286
6
    return 0;
287
}
288
289
172
static void get_bits_align32(GetBitContext *s)
290
{
291
172
    int n = (-get_bits_count(s)) & 31;
292
172
    if (n) skip_bits(s, n);
293
172
}
294
295
268
static int binkaudio_receive_frame(AVCodecContext *avctx, AVFrame *frame)
296
{
297
268
    BinkAudioContext *s = avctx->priv_data;
298
268
    GetBitContext *gb = &s->gb;
299
    int ret;
300
301
268
    if (!s->pkt->data) {
302
188
        ret = ff_decode_get_packet(avctx, s->pkt);
303
188
        if (ret < 0)
304
96
            return ret;
305
306
92
        if (s->pkt->size < 4) {
307
            av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
308
            ret = AVERROR_INVALIDDATA;
309
            goto fail;
310
        }
311
312
92
        ret = init_get_bits8(gb, s->pkt->data, s->pkt->size);
313
92
        if (ret < 0)
314
            goto fail;
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
        return ret;
324
325
172
    if (decode_block(s, (float **)frame->extended_data,
326
172
                     avctx->codec->id == AV_CODEC_ID_BINKAUDIO_DCT)) {
327
        av_log(avctx, AV_LOG_ERROR, "Incomplete packet\n");
328
        return AVERROR_INVALIDDATA;
329
    }
330
172
    get_bits_align32(gb);
331
172
    if (!get_bits_left(gb)) {
332
92
        memset(gb, 0, sizeof(*gb));
333
92
        av_packet_unref(s->pkt);
334
    }
335
336
172
    frame->nb_samples = s->block_size / avctx->channels;
337
338
172
    return 0;
339
fail:
340
    av_packet_unref(s->pkt);
341
    return ret;
342
}
343
344
AVCodec ff_binkaudio_rdft_decoder = {
345
    .name           = "binkaudio_rdft",
346
    .long_name      = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)"),
347
    .type           = AVMEDIA_TYPE_AUDIO,
348
    .id             = AV_CODEC_ID_BINKAUDIO_RDFT,
349
    .priv_data_size = sizeof(BinkAudioContext),
350
    .init           = decode_init,
351
    .close          = decode_end,
352
    .receive_frame  = binkaudio_receive_frame,
353
    .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
354
};
355
356
AVCodec ff_binkaudio_dct_decoder = {
357
    .name           = "binkaudio_dct",
358
    .long_name      = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)"),
359
    .type           = AVMEDIA_TYPE_AUDIO,
360
    .id             = AV_CODEC_ID_BINKAUDIO_DCT,
361
    .priv_data_size = sizeof(BinkAudioContext),
362
    .init           = decode_init,
363
    .close          = decode_end,
364
    .receive_frame  = binkaudio_receive_frame,
365
    .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
366
};