FFmpeg coverage


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