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 first;
57 int frame_len; ///< transform size (samples)
58 int overlap_len; ///< overlap size (samples)
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];
69
70
72{
74 int sample_rate = avctx->sample_rate;
75 int sample_rate_half;
76 int i, ret;
77 int frame_len_bits;
78 int max_channels = avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT ? MAX_CHANNELS : MAX_DCT_CHANNELS;
79 int channels = avctx->ch_layout.nb_channels;
80
81 /* determine frame length */
82 if (avctx->sample_rate < 22050) {
83 frame_len_bits = 9;
84 } else if (avctx->sample_rate < 44100) {
85 frame_len_bits = 10;
86 } else {
87 frame_len_bits = 11;
88 }
89
90 if (channels < 1 || channels > max_channels) {
91 av_log(avctx, AV_LOG_ERROR, "invalid number of channels: %d\n", channels);
93 }
96
97 s->version_b = avctx->extradata_size >= 4 && avctx->extradata[3] == 'b';
98
99 if (avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT) {
100 // audio is already interleaved for the RDFT format variant
102 if (sample_rate > INT_MAX / channels)
103 return AVERROR_INVALIDDATA;
104 sample_rate *= channels;
105 s->channels = 1;
106 if (!s->version_b)
107 frame_len_bits += av_log2(channels);
108 } else {
109 s->channels = channels;
111 }
112
113 s->frame_len = 1 << frame_len_bits;
114 s->overlap_len = s->frame_len / 16;
115 s->block_size = (s->frame_len - s->overlap_len) * FFMIN(MAX_CHANNELS, s->channels);
116 sample_rate_half = (sample_rate + 1LL) / 2;
117 if (avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
118 s->root = 2.0 / (sqrt(s->frame_len) * 32768.0);
119 else
120 s->root = s->frame_len / (sqrt(s->frame_len) * 32768.0);
121 for (i = 0; i < 96; i++) {
122 /* constant is result of 0.066399999/log10(M_E) */
123 s->quant_table[i] = expf(i * 0.15289164787221953823f) * s->root;
124 }
125
126 /* calculate number of bands */
127 for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
128 if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1])
129 break;
130
131 /* populate bands data */
132 s->bands[0] = 2;
133 for (i = 1; i < s->num_bands; i++)
134 s->bands[i] = (ff_wma_critical_freqs[i - 1] * s->frame_len / sample_rate_half) & ~1;
135 s->bands[s->num_bands] = s->frame_len;
136
137 s->first = 1;
138
139 if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT) {
140 float scale = 0.5;
141 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 float scale = 1.0 / (1 << frame_len_bits);
144 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 if (ret < 0)
149 return ret;
150
151 s->pkt = avctx->internal->in_pkt;
152
153 return 0;
154}
155
156static float get_float(GetBitContext *gb)
157{
158 int power = get_bits(gb, 5);
159 float f = ldexpf(get_bits(gb, 23), power - 23);
160 if (get_bits1(gb))
161 f = -f;
162 return f;
163}
164
165static 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 */
174static 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 GetBitContext *gb = &s->gb;
181 LOCAL_ALIGNED_32(float, coeffs, [4098]);
182
183 if (use_dct)
184 skip_bits(gb, 2);
185
186 for (ch = 0; ch < channels; ch++) {
187 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 if (get_bits_left(gb) < 58)
194 return AVERROR_INVALIDDATA;
195 coeffs[0] = get_float(gb) * s->root;
196 coeffs[1] = get_float(gb) * s->root;
197 }
198
199 if (get_bits_left(gb) < s->num_bands * 8)
200 return AVERROR_INVALIDDATA;
201 for (i = 0; i < s->num_bands; i++) {
202 int value = get_bits(gb, 8);
203 quant[i] = s->quant_table[FFMIN(value, 95)];
204 }
205
206 k = 0;
207 q = quant[0];
208
209 // parse coefficients
210 i = 2;
211 while (i < s->frame_len) {
212 if (s->version_b) {
213 j = i + 16;
214 } else {
215 int v = get_bits1(gb);
216 if (v) {
217 v = get_bits(gb, 4);
218 j = i + rle_length_tab[v] * 8;
219 } else {
220 j = i + 8;
221 }
222 }
223
224 j = FFMIN(j, s->frame_len);
225
226 width = get_bits(gb, 4);
227 if (width == 0) {
228 memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
229 i = j;
230 while (s->bands[k] < i)
231 q = quant[k++];
232 } else {
233 while (i < j) {
234 if (s->bands[k] == i)
235 q = quant[k++];
236 coeff = get_bits(gb, width);
237 if (coeff) {
238 int v;
239 v = get_bits1(gb);
240 if (v)
241 coeffs[i] = -q * coeff;
242 else
243 coeffs[i] = q * coeff;
244 } else {
245 coeffs[i] = 0.0f;
246 }
247 i++;
248 }
249 }
250 }
251
252 if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
253 coeffs[0] /= 0.5;
254 s->tx_fn(s->tx, out[ch + ch_offset], coeffs, sizeof(float));
255 } else if (CONFIG_BINKAUDIO_RDFT_DECODER) {
256 for (int i = 2; i < s->frame_len; i += 2)
257 coeffs[i + 1] *= -1;
258
259 coeffs[s->frame_len + 0] = coeffs[1];
260 coeffs[s->frame_len + 1] = coeffs[1] = 0;
261 s->tx_fn(s->tx, out[ch + ch_offset], coeffs, sizeof(AVComplexFloat));
262 }
263 }
264
265 for (ch = 0; ch < channels; ch++) {
266 int j;
267 int count = s->overlap_len * channels;
268 if (!s->first) {
269 j = ch;
270 for (i = 0; i < s->overlap_len; i++, j += channels)
271 out[ch + ch_offset][i] = (s->previous[ch + ch_offset][i] * (count - j) +
272 out[ch + ch_offset][i] * j) / count;
273 }
274 memcpy(s->previous[ch + ch_offset], &out[ch + ch_offset][s->frame_len - s->overlap_len],
275 s->overlap_len * sizeof(*s->previous[ch + ch_offset]));
276 }
277
278 s->first = 0;
279
280 return 0;
281}
282
284{
285 BinkAudioContext * s = avctx->priv_data;
286 av_tx_uninit(&s->tx);
287 return 0;
288}
289
291{
292 int n = (-get_bits_count(s)) & 31;
293 if (n) skip_bits(s, n);
294}
295
297{
298 BinkAudioContext *s = avctx->priv_data;
299 GetBitContext *gb = &s->gb;
300 int new_pkt, ret;
301
302again:
303 new_pkt = !s->pkt->data;
304 if (!s->pkt->data) {
305 ret = ff_decode_get_packet(avctx, s->pkt);
306 if (ret < 0) {
307 s->ch_offset = 0;
308 return ret;
309 }
310
311 if (s->pkt->size < 4) {
312 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
314 goto fail;
315 }
316
317 ret = init_get_bits8(gb, s->pkt->data, s->pkt->size);
318 if (ret < 0)
319 goto fail;
320
321 /* skip reported size */
322 skip_bits_long(gb, 32);
323 }
324
325 /* get output buffer */
326 if (s->ch_offset == 0) {
327 frame->nb_samples = s->frame_len;
328 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
329 goto fail;
330 if (!new_pkt)
331 frame->pts = AV_NOPTS_VALUE;
332 }
333
334 if (decode_block(s, (float **)frame->extended_data,
336 FFMIN(MAX_CHANNELS, s->channels - s->ch_offset), s->ch_offset)) {
337 av_log(avctx, AV_LOG_ERROR, "Incomplete packet\n");
339 goto fail;
340 }
341 s->ch_offset += MAX_CHANNELS;
343 if (!get_bits_left(gb)) {
344 memset(gb, 0, sizeof(*gb));
345 av_packet_unref(s->pkt);
346 }
347 if (s->ch_offset >= s->channels) {
348 s->ch_offset = 0;
349 } else {
350 goto again;
351 }
352
353 frame->nb_samples = s->block_size / FFMIN(avctx->ch_layout.nb_channels, MAX_CHANNELS);
354
355 return 0;
356fail:
357 s->ch_offset = 0;
358 av_packet_unref(s->pkt);
359 return ret;
360}
361
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
373 .p.name = "binkaudio_rdft",
374 CODEC_LONG_NAME("Bink Audio (RDFT)"),
375 .p.type = AVMEDIA_TYPE_AUDIO,
377 .priv_data_size = sizeof(BinkAudioContext),
378 .init = decode_init,
380 .close = decode_end,
382 .p.capabilities = AV_CODEC_CAP_DR1,
383 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
384};
385
387 .p.name = "binkaudio_dct",
388 CODEC_LONG_NAME("Bink Audio (DCT)"),
389 .p.type = AVMEDIA_TYPE_AUDIO,
391 .priv_data_size = sizeof(BinkAudioContext),
392 .init = decode_init,
394 .close = decode_end,
396 .p.capabilities = AV_CODEC_CAP_DR1,
397 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
398};
#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:372
const FFCodec ff_binkaudio_dct_decoder
Definition binkaudio.c:386
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:362
static const uint8_t rle_length_tab[16]
Definition binkaudio.c:165
#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:71
static av_cold int decode_end(AVCodecContext *avctx)
Definition binkaudio.c:283
static int binkaudio_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Definition binkaudio.c:296
static int decode_block(BinkAudioContext *s, float **out, int use_dct, int channels, int ch_offset)
Decode Bink Audio block.
Definition binkaudio.c:174
static void get_bits_align32(GetBitContext *s)
Definition binkaudio.c:290
static float get_float(GetBitContext *gb)
Definition binkaudio.c:156
#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:1777
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:501
@ AV_CODEC_ID_BINKAUDIO_RDFT
Definition codec_id.h:500
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 expf(x)
Definition libm.h:285
#define ldexpf(x, exp)
Definition libm.h:391
#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:63
av_tx_fn tx_fn
Definition binkaudio.c:67
float quant_table[96]
Definition binkaudio.c:64
AVPacket * pkt
Definition binkaudio.c:65
int overlap_len
overlap size (samples)
Definition binkaudio.c:58
int frame_len
transform size (samples)
Definition binkaudio.c:57
AVTXContext * tx
Definition binkaudio.c:66
GetBitContext gb
Definition binkaudio.c:52
int version_b
Bink version 'b'.
Definition binkaudio.c:53
unsigned int bands[26]
Definition binkaudio.c:62
#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