FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/binkaudio.c
Date: 2025-10-10 03:51:19
Exec Total Coverage
Lines: 152 183 83.1%
Functions: 6 7 85.7%
Branches: 79 100 79.0%

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