GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/binkaudio.c Lines: 136 159 85.5 %
Date: 2020-09-21 17:35:45 Branches: 75 94 79.8 %

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
#define MAX_CHANNELS 2
44
#define BINK_BLOCK_MAX_SIZE (MAX_CHANNELS << 11)
45
46
typedef struct BinkAudioContext {
47
    GetBitContext gb;
48
    int version_b;          ///< Bink version 'b'
49
    int first;
50
    int channels;
51
    int frame_len;          ///< transform size (samples)
52
    int overlap_len;        ///< overlap size (samples)
53
    int block_size;
54
    int num_bands;
55
    float root;
56
    unsigned int bands[26];
57
    float previous[MAX_CHANNELS][BINK_BLOCK_MAX_SIZE / 16];  ///< coeffs from previous audio block
58
    float quant_table[96];
59
    AVPacket *pkt;
60
    union {
61
        RDFTContext rdft;
62
        DCTContext dct;
63
    } trans;
64
} BinkAudioContext;
65
66
67
6
static av_cold int decode_init(AVCodecContext *avctx)
68
{
69
6
    BinkAudioContext *s = avctx->priv_data;
70
6
    int sample_rate = avctx->sample_rate;
71
    int sample_rate_half;
72
    int i;
73
    int frame_len_bits;
74
75
    /* determine frame length */
76
6
    if (avctx->sample_rate < 22050) {
77
        frame_len_bits = 9;
78
6
    } else if (avctx->sample_rate < 44100) {
79
2
        frame_len_bits = 10;
80
    } else {
81
4
        frame_len_bits = 11;
82
    }
83
84

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

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