FFmpeg coverage


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