FFmpeg
Loading...
Searching...
No Matches
binkaudio.c
Go to the documentation of this file.
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
35#include "libavutil/intfloat.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
51typedef struct BinkAudioContext {
53 int version_b; ///< Bink version 'b'
54 int version_2; ///< Bink Audio 2
55 int first;
58 int frame_len; ///< transform size (samples)
59 int overlap_len; ///< overlap size (samples)
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];
70
71
73{
75 int sample_rate = avctx->sample_rate;
76 int sample_rate_half;
77 int i, ret;
78 int frame_len_bits;
79 int max_channels = avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT ? MAX_CHANNELS : MAX_DCT_CHANNELS;
80 int channels = avctx->ch_layout.nb_channels;
81
82 /* determine frame length */
83 if (avctx->sample_rate < 22050) {
84 frame_len_bits = 9;
85 } else if (avctx->sample_rate < 44100) {
86 frame_len_bits = 10;
87 } else {
88 frame_len_bits = 11;
89 }
90
91 if (channels < 1 || channels > max_channels) {
92 av_log(avctx, AV_LOG_ERROR, "invalid number of channels: %d\n", channels);
94 }
97
98 s->version_b = avctx->extradata_size >= 4 && avctx->extradata[3] == 'b';
99 s->version_2 = avctx->extradata_size >= 1 && avctx->extradata[0] == '2';
100
101 if (avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT) {
102 // audio is already interleaved for the RDFT format variant
104 if (sample_rate > INT_MAX / channels)
105 return AVERROR_INVALIDDATA;
106 sample_rate *= channels;
107 s->channels = 1;
108 if (!s->version_b)
109 frame_len_bits += av_log2(channels);
110 } else {
111 s->channels = channels;
113 }
114
115 s->frame_len = 1 << frame_len_bits;
116 s->overlap_len = s->frame_len / 16;
117 s->block_size = (s->frame_len - s->overlap_len) * FFMIN(MAX_CHANNELS, s->channels);
118 sample_rate_half = (sample_rate + 1LL) / 2;
119 if (avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
120 s->root = 2.0 / (sqrt(s->frame_len) * 32768.0);
121 else
122 s->root = s->frame_len / (sqrt(s->frame_len) * 32768.0);
123 for (i = 0; i < 96; i++) {
124 /* constant is result of 0.066399999/log10(M_E) */
125 s->quant_table[i] = expf(i * 0.15289164787221953823f) * s->root;
126 }
127
128 /* calculate number of bands */
129 for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
130 if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1])
131 break;
132
133 /* populate bands data */
134 s->bands[0] = 2;
135 for (i = 1; i < s->num_bands; i++)
136 s->bands[i] = (ff_wma_critical_freqs[i - 1] * s->frame_len / sample_rate_half) & ~1;
137 s->bands[s->num_bands] = s->frame_len;
138
139 s->first = 1;
140
141 if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT) {
142 float scale = 0.5;
143 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 float scale = 1.0 / (1 << frame_len_bits);
146 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 if (ret < 0)
151 return ret;
152
153 s->pkt = avctx->internal->in_pkt;
154
155 return 0;
156}
157
158static float get_float(GetBitContext *gb)
159{
160 int power = get_bits(gb, 5);
161 float f = ldexpf(get_bits(gb, 23), power - 23);
162 if (get_bits1(gb))
163 f = -f;
164 return f;
165}
166
167static 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 */
176static 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 int quant_idx_size = s->version_2 ? 7 : 8;
183 GetBitContext *gb = &s->gb;
184 LOCAL_ALIGNED_32(float, coeffs, [4098]);
185
186 if (use_dct)
187 skip_bits(gb, 2);
188
189 for (ch = 0; ch < channels; ch++) {
190 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 if (get_bits_left(gb) < 58)
197 return AVERROR_INVALIDDATA;
198 coeffs[0] = get_float(gb) * s->root;
199 coeffs[1] = get_float(gb) * s->root;
200 }
201
202 if (get_bits_left(gb) < s->num_bands * quant_idx_size)
203 return AVERROR_INVALIDDATA;
204 for (i = 0; i < s->num_bands; i++) {
205 int value = get_bits(gb, quant_idx_size);
206 quant[i] = s->quant_table[FFMIN(value, 95)];
207 }
208
209 k = 0;
210 q = quant[0];
211
212 // parse coefficients
213 i = 2;
214 while (i < s->frame_len) {
215 if (s->version_b) {
216 j = i + 16;
217 } else {
218 int v = get_bits1(gb);
219 if (v) {
220 v = get_bits(gb, 4);
221 j = i + rle_length_tab[v] * 8;
222 } else {
223 j = i + 8;
224 }
225 }
226
227 j = FFMIN(j, s->frame_len);
228
229 width = get_bits(gb, 4);
230 if (width == 0) {
231 memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
232 i = j;
233 while (s->bands[k] < i)
234 q = quant[k++];
235 } else {
236 if (s->version_2) {
237 for (int m = i; m < j; m++)
238 coeffs[m] = get_bits(gb, width);
239 while (i < j) {
240 if (s->bands[k] == i)
241 q = quant[k++];
242 if (coeffs[i] > 0) {
243 if (get_bits1(gb))
244 coeffs[i] *= -q;
245 else
246 coeffs[i] *= q;
247 }
248 i++;
249 }
250 } else {
251 while (i < j) {
252 if (s->bands[k] == i)
253 q = quant[k++];
254 coeff = get_bits(gb, width);
255 if (coeff) {
256 if (get_bits1(gb))
257 coeffs[i] = -q * coeff;
258 else
259 coeffs[i] = q * coeff;
260 } else {
261 coeffs[i] = 0.0f;
262 }
263 i++;
264 }
265 }
266 }
267 }
268
269 if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
270 coeffs[0] /= 0.5;
271 s->tx_fn(s->tx, out[ch + ch_offset], coeffs, sizeof(float));
272 } else if (CONFIG_BINKAUDIO_RDFT_DECODER) {
273 for (int i = 2; i < s->frame_len; i += 2)
274 coeffs[i + 1] *= -1;
275
276 coeffs[s->frame_len + 0] = coeffs[1];
277 coeffs[s->frame_len + 1] = coeffs[1] = 0;
278 s->tx_fn(s->tx, out[ch + ch_offset], coeffs, sizeof(AVComplexFloat));
279 }
280 }
281
282 for (ch = 0; ch < channels; ch++) {
283 int j;
284 int count = s->overlap_len * channels;
285 if (!s->first) {
286 j = ch;
287 for (i = 0; i < s->overlap_len; i++, j += channels)
288 out[ch + ch_offset][i] = (s->previous[ch + ch_offset][i] * (count - j) +
289 out[ch + ch_offset][i] * j) / count;
290 }
291 memcpy(s->previous[ch + ch_offset], &out[ch + ch_offset][s->frame_len - s->overlap_len],
292 s->overlap_len * sizeof(*s->previous[ch + ch_offset]));
293 }
294
295 s->first = 0;
296
297 return 0;
298}
299
301{
302 BinkAudioContext * s = avctx->priv_data;
303 av_tx_uninit(&s->tx);
304 return 0;
305}
306
308{
309 int n = (-get_bits_count(s)) & 31;
310 if (n) skip_bits(s, n);
311}
312
314{
315 BinkAudioContext *s = avctx->priv_data;
316 GetBitContext *gb = &s->gb;
317 int new_pkt, ret;
318
319again:
320 new_pkt = !s->pkt->data;
321 if (!s->pkt->data) {
322 ret = ff_decode_get_packet(avctx, s->pkt);
323 if (ret < 0) {
324 s->ch_offset = 0;
325 return ret;
326 }
327
328 if (s->pkt->size < 4) {
329 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
331 goto fail;
332 }
333
334 ret = init_get_bits8(gb, s->pkt->data, s->pkt->size);
335 if (ret < 0)
336 goto fail;
337
338 /* skip reported size */
339 skip_bits_long(gb, 32);
340 }
341
342 /* get output buffer */
343 if (s->ch_offset == 0) {
344 frame->nb_samples = s->frame_len;
345 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
346 goto fail;
347 if (!new_pkt)
348 frame->pts = AV_NOPTS_VALUE;
349 }
350
351 if (decode_block(s, (float **)frame->extended_data,
353 FFMIN(MAX_CHANNELS, s->channels - s->ch_offset), s->ch_offset)) {
354 av_log(avctx, AV_LOG_ERROR, "Incomplete packet\n");
356 goto fail;
357 }
358 s->ch_offset += MAX_CHANNELS;
360 if (!get_bits_left(gb)) {
361 memset(gb, 0, sizeof(*gb));
362 av_packet_unref(s->pkt);
363 }
364 if (s->ch_offset >= s->channels) {
365 s->ch_offset = 0;
366 } else {
367 goto again;
368 }
369
370 frame->nb_samples = s->block_size / FFMIN(avctx->ch_layout.nb_channels, MAX_CHANNELS);
371
372 return 0;
373fail:
374 s->ch_offset = 0;
375 av_packet_unref(s->pkt);
376 return ret;
377}
378
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
390 .p.name = "binkaudio_rdft",
391 CODEC_LONG_NAME("Bink Audio (RDFT)"),
392 .p.type = AVMEDIA_TYPE_AUDIO,
394 .priv_data_size = sizeof(BinkAudioContext),
395 .init = decode_init,
397 .close = decode_end,
399 .p.capabilities = AV_CODEC_CAP_DR1,
400 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
401};
402
404 .p.name = "binkaudio_dct",
405 CODEC_LONG_NAME("Bink Audio (DCT)"),
406 .p.type = AVMEDIA_TYPE_AUDIO,
408 .priv_data_size = sizeof(BinkAudioContext),
409 .init = decode_init,
411 .close = decode_end,
413 .p.capabilities = AV_CODEC_CAP_DR1,
414 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
415};
#define MAX_CHANNELS
Definition aac.h:33
static av_cold void decode_flush(AVCodecContext *avctx)
Definition agm.c:1253
const FFCodec ff_binkaudio_rdft_decoder
Definition binkaudio.c:389
const FFCodec ff_binkaudio_dct_decoder
Definition binkaudio.c:403
static FILE * out
channels
Definition aptx.h:31
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
Libavcodec external API header.
static av_cold void decode_flush(AVCodecContext *avctx)
Definition binkaudio.c:379
static const uint8_t rle_length_tab[16]
Definition binkaudio.c:167
#define BINK_BLOCK_MAX_SIZE
Definition binkaudio.c:49
#define MAX_DCT_CHANNELS
Definition binkaudio.c:47
static av_cold int decode_init(AVCodecContext *avctx)
Definition binkaudio.c:72
static av_cold int decode_end(AVCodecContext *avctx)
Definition binkaudio.c:300
static int binkaudio_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Definition binkaudio.c:313
static int decode_block(BinkAudioContext *s, float **out, int use_dct, int channels, int ch_offset)
Decode Bink Audio block.
Definition binkaudio.c:176
static void get_bits_align32(GetBitContext *s)
Definition binkaudio.c:307
static float get_float(GetBitContext *gb)
Definition binkaudio.c:158
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define FF_CODEC_RECEIVE_FRAME_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1781
int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
Called by decoders to get the next packet for decoding.
Definition decode.c:254
static AVFrame * frame
void(* flush)(AVBSFContext *ctx)
Definition dts2pts.c:610
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
double value
Definition eval.c:102
bitstream reader API header.
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition get_bits.h:424
static int get_bits_left(GetBitContext *gb)
Definition get_bits.h:688
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition get_bits.h:280
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static void skip_bits(GetBitContext *s, int n)
Definition get_bits.h:383
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
static int get_bits_count(const GetBitContext *s)
Definition get_bits.h:254
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
#define fail
Definition test.h:479
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_BINKAUDIO_DCT
Definition codec_id.h:502
@ AV_CODEC_ID_BINKAUDIO_RDFT
Definition codec_id.h:501
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:434
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition samplefmt.h:66
@ AV_SAMPLE_FMT_FLT
float
Definition samplefmt.h:60
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
static av_always_inline float av_int2float(uint32_t i)
Reinterpret a 32-bit integer as a float.
Definition intfloat.h:40
#define av_log2
Definition intmath.h:84
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition intra.c:278
static av_cold int decode_init(AVCodecContext *avctx)
Definition 4xm.c:998
static av_cold int decode_end(AVCodecContext *avctx)
Definition 4xm.c:980
common internal api header.
Macro definitions for various function/variable attributes.
#define av_cold
Definition attributes.h:117
#define FFMIN(a, b)
Definition macros.h:49
#define LOCAL_ALIGNED_32(t, v,...)
static float power(float r, float g, float b, float max)
int nb_channels
Number of channels in this layout.
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1047
const struct AVCodec * codec
Definition avcodec.h:452
int sample_rate
samples per second
Definition avcodec.h:1040
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int extradata_size
Definition avcodec.h:527
struct AVCodecInternal * internal
Private context used for internal data.
Definition avcodec.h:478
void * priv_data
Definition avcodec.h:470
AVPacket * in_pkt
This packet is used to hold the packet given to decoders implementing the .decode API; it is unused b...
Definition internal.h:83
enum AVCodecID id
Definition codec.h:189
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
float previous[MAX_DCT_CHANNELS][BINK_BLOCK_MAX_SIZE/16]
coeffs from previous audio block
Definition binkaudio.c:64
av_tx_fn tx_fn
Definition binkaudio.c:68
float quant_table[96]
Definition binkaudio.c:65
AVPacket * pkt
Definition binkaudio.c:66
int overlap_len
overlap size (samples)
Definition binkaudio.c:59
int version_2
Bink Audio 2.
Definition binkaudio.c:54
int frame_len
transform size (samples)
Definition binkaudio.c:58
AVTXContext * tx
Definition binkaudio.c:67
GetBitContext gb
Definition binkaudio.c:52
int version_b
Bink version 'b'.
Definition binkaudio.c:53
unsigned int bands[26]
Definition binkaudio.c:63
#define av_log(a,...)
#define width
Definition dsp.h:89
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
Definition tx.c:295
av_cold int av_tx_init(AVTXContext **ctx, av_tx_fn *tx, enum AVTXType type, int inv, int len, const void *scale, uint64_t flags)
Initialize a transform context with the given configuration (i)MDCTs with an odd length are currently...
Definition tx.c:903
@ AV_TX_FLOAT_RDFT
Real to complex and complex to real DFTs.
Definition tx.h:90
@ AV_TX_FLOAT_DCT
Real to real (DCT) transforms.
Definition tx.h:104
void(* av_tx_fn)(AVTXContext *s, void *out, void *in, ptrdiff_t stride)
Function pointer to a function to perform the transform.
Definition tx.h:151
static const double coeff[2][5]
static const uint8_t quant[64]
Definition vmixdec.c:71
const uint16_t ff_wma_critical_freqs[25]
Definition wma_freqs.c:23