FFmpeg
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 
32 #include "libavutil/intfloat.h"
33 
34 #define BITSTREAM_READER_LE
35 #include "avcodec.h"
36 #include "dct.h"
37 #include "decode.h"
38 #include "get_bits.h"
39 #include "internal.h"
40 #include "rdft.h"
41 #include "wma_freqs.h"
42 
43 #define MAX_CHANNELS 2
44 #define BINK_BLOCK_MAX_SIZE (MAX_CHANNELS << 11)
45 
46 typedef struct BinkAudioContext {
48  int version_b; ///< Bink version 'b'
49  int first;
50  int channels;
51  int frame_len; ///< transform size (samples)
52  int overlap_len; ///< overlap size (samples)
54  int num_bands;
55  unsigned int *bands;
56  float root;
58  float previous[MAX_CHANNELS][BINK_BLOCK_MAX_SIZE / 16]; ///< coeffs from previous audio block
59  float quant_table[96];
61  union {
64  } trans;
66 
67 
69 {
70  BinkAudioContext *s = avctx->priv_data;
71  int sample_rate = avctx->sample_rate;
72  int sample_rate_half;
73  int i;
74  int frame_len_bits;
75 
76  /* determine frame length */
77  if (avctx->sample_rate < 22050) {
78  frame_len_bits = 9;
79  } else if (avctx->sample_rate < 44100) {
80  frame_len_bits = 10;
81  } else {
82  frame_len_bits = 11;
83  }
84 
85  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  avctx->channel_layout = avctx->channels == 1 ? AV_CH_LAYOUT_MONO :
91 
92  s->version_b = avctx->extradata_size >= 4 && avctx->extradata[3] == 'b';
93 
94  if (avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT) {
95  // audio is already interleaved for the RDFT format variant
97  if (sample_rate > INT_MAX / avctx->channels)
98  return AVERROR_INVALIDDATA;
99  sample_rate *= avctx->channels;
100  s->channels = 1;
101  if (!s->version_b)
102  frame_len_bits += av_log2(avctx->channels);
103  } else {
104  s->channels = avctx->channels;
106  }
107 
108  s->frame_len = 1 << frame_len_bits;
109  s->overlap_len = s->frame_len / 16;
110  s->block_size = (s->frame_len - s->overlap_len) * s->channels;
111  sample_rate_half = (sample_rate + 1LL) / 2;
112  if (avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
113  s->root = 2.0 / (sqrt(s->frame_len) * 32768.0);
114  else
115  s->root = s->frame_len / (sqrt(s->frame_len) * 32768.0);
116  for (i = 0; i < 96; i++) {
117  /* constant is result of 0.066399999/log10(M_E) */
118  s->quant_table[i] = expf(i * 0.15289164787221953823f) * s->root;
119  }
120 
121  /* calculate number of bands */
122  for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
123  if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1])
124  break;
125 
126  s->bands = av_malloc((s->num_bands + 1) * sizeof(*s->bands));
127  if (!s->bands)
128  return AVERROR(ENOMEM);
129 
130  /* populate bands data */
131  s->bands[0] = 2;
132  for (i = 1; i < s->num_bands; i++)
133  s->bands[i] = (ff_wma_critical_freqs[i - 1] * s->frame_len / sample_rate_half) & ~1;
134  s->bands[s->num_bands] = s->frame_len;
135 
136  s->first = 1;
137 
138  if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
139  ff_rdft_init(&s->trans.rdft, frame_len_bits, DFT_C2R);
140  else if (CONFIG_BINKAUDIO_DCT_DECODER)
141  ff_dct_init(&s->trans.dct, frame_len_bits, DCT_III);
142  else
143  av_assert0(0);
144 
145  s->pkt = av_packet_alloc();
146  if (!s->pkt)
147  return AVERROR(ENOMEM);
148 
149  return 0;
150 }
151 
152 static float get_float(GetBitContext *gb)
153 {
154  int power = get_bits(gb, 5);
155  float f = ldexpf(get_bits(gb, 23), power - 23);
156  if (get_bits1(gb))
157  f = -f;
158  return f;
159 }
160 
161 static const uint8_t rle_length_tab[16] = {
162  2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
163 };
164 
165 /**
166  * Decode Bink Audio block
167  * @param[out] out Output buffer (must contain s->block_size elements)
168  * @return 0 on success, negative error code on failure
169  */
170 static int decode_block(BinkAudioContext *s, float **out, int use_dct)
171 {
172  int ch, i, j, k;
173  float q, quant[25];
174  int width, coeff;
175  GetBitContext *gb = &s->gb;
176 
177  if (use_dct)
178  skip_bits(gb, 2);
179 
180  for (ch = 0; ch < s->channels; ch++) {
181  FFTSample *coeffs = out[ch];
182 
183  if (s->version_b) {
184  if (get_bits_left(gb) < 64)
185  return AVERROR_INVALIDDATA;
186  coeffs[0] = av_int2float(get_bits_long(gb, 32)) * s->root;
187  coeffs[1] = av_int2float(get_bits_long(gb, 32)) * s->root;
188  } else {
189  if (get_bits_left(gb) < 58)
190  return AVERROR_INVALIDDATA;
191  coeffs[0] = get_float(gb) * s->root;
192  coeffs[1] = get_float(gb) * s->root;
193  }
194 
195  if (get_bits_left(gb) < s->num_bands * 8)
196  return AVERROR_INVALIDDATA;
197  for (i = 0; i < s->num_bands; i++) {
198  int value = get_bits(gb, 8);
199  quant[i] = s->quant_table[FFMIN(value, 95)];
200  }
201 
202  k = 0;
203  q = quant[0];
204 
205  // parse coefficients
206  i = 2;
207  while (i < s->frame_len) {
208  if (s->version_b) {
209  j = i + 16;
210  } else {
211  int v = get_bits1(gb);
212  if (v) {
213  v = get_bits(gb, 4);
214  j = i + rle_length_tab[v] * 8;
215  } else {
216  j = i + 8;
217  }
218  }
219 
220  j = FFMIN(j, s->frame_len);
221 
222  width = get_bits(gb, 4);
223  if (width == 0) {
224  memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
225  i = j;
226  while (s->bands[k] < i)
227  q = quant[k++];
228  } else {
229  while (i < j) {
230  if (s->bands[k] == i)
231  q = quant[k++];
232  coeff = get_bits(gb, width);
233  if (coeff) {
234  int v;
235  v = get_bits1(gb);
236  if (v)
237  coeffs[i] = -q * coeff;
238  else
239  coeffs[i] = q * coeff;
240  } else {
241  coeffs[i] = 0.0f;
242  }
243  i++;
244  }
245  }
246  }
247 
248  if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
249  coeffs[0] /= 0.5;
250  s->trans.dct.dct_calc(&s->trans.dct, coeffs);
251  }
252  else if (CONFIG_BINKAUDIO_RDFT_DECODER)
253  s->trans.rdft.rdft_calc(&s->trans.rdft, coeffs);
254  }
255 
256  for (ch = 0; ch < s->channels; ch++) {
257  int j;
258  int count = s->overlap_len * s->channels;
259  if (!s->first) {
260  j = ch;
261  for (i = 0; i < s->overlap_len; i++, j += s->channels)
262  out[ch][i] = (s->previous[ch][i] * (count - j) +
263  out[ch][i] * j) / count;
264  }
265  memcpy(s->previous[ch], &out[ch][s->frame_len - s->overlap_len],
266  s->overlap_len * sizeof(*s->previous[ch]));
267  }
268 
269  s->first = 0;
270 
271  return 0;
272 }
273 
275 {
276  BinkAudioContext * s = avctx->priv_data;
277  av_freep(&s->bands);
278  if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
279  ff_rdft_end(&s->trans.rdft);
280  else if (CONFIG_BINKAUDIO_DCT_DECODER)
281  ff_dct_end(&s->trans.dct);
282 
283  av_packet_free(&s->pkt);
284 
285  return 0;
286 }
287 
289 {
290  int n = (-get_bits_count(s)) & 31;
291  if (n) skip_bits(s, n);
292 }
293 
295 {
296  BinkAudioContext *s = avctx->priv_data;
297  GetBitContext *gb = &s->gb;
298  int ret;
299 
300  if (!s->pkt->data) {
301  ret = ff_decode_get_packet(avctx, s->pkt);
302  if (ret < 0)
303  return ret;
304 
305  if (s->pkt->size < 4) {
306  av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
308  goto fail;
309  }
310 
311  ret = init_get_bits8(gb, s->pkt->data, s->pkt->size);
312  if (ret < 0)
313  goto fail;
314 
315  /* skip reported size */
316  skip_bits_long(gb, 32);
317  }
318 
319  /* get output buffer */
320  frame->nb_samples = s->frame_len;
321  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
322  return ret;
323 
324  if (decode_block(s, (float **)frame->extended_data,
325  avctx->codec->id == AV_CODEC_ID_BINKAUDIO_DCT)) {
326  av_log(avctx, AV_LOG_ERROR, "Incomplete packet\n");
327  return AVERROR_INVALIDDATA;
328  }
329  get_bits_align32(gb);
330  if (!get_bits_left(gb)) {
331  memset(gb, 0, sizeof(*gb));
332  av_packet_unref(s->pkt);
333  }
334 
335  frame->nb_samples = s->block_size / avctx->channels;
336 
337  return 0;
338 fail:
339  av_packet_unref(s->pkt);
340  return ret;
341 }
342 
344  .name = "binkaudio_rdft",
345  .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)"),
346  .type = AVMEDIA_TYPE_AUDIO,
348  .priv_data_size = sizeof(BinkAudioContext),
349  .init = decode_init,
350  .close = decode_end,
352  .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
353 };
354 
356  .name = "binkaudio_dct",
357  .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)"),
358  .type = AVMEDIA_TYPE_AUDIO,
360  .priv_data_size = sizeof(BinkAudioContext),
361  .init = decode_init,
362  .close = decode_end,
364  .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
365 };
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:605
AVCodec
AVCodec.
Definition: codec.h:190
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:69
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:291
ff_decode_get_packet
int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
Called by decoders to get the next packet for decoding.
Definition: decode.c:238
BinkAudioContext::first
int first
Definition: binkaudio.c:49
BinkAudioContext::version_b
int version_b
Bink version 'b'.
Definition: binkaudio.c:48
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecContext::channel_layout
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1237
out
FILE * out
Definition: movenc.c:54
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1186
wma_freqs.h
BinkAudioContext::channels
int channels
Definition: binkaudio.c:50
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:85
rdft.h
get_bits_long
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:546
get_float
static float get_float(GetBitContext *gb)
Definition: binkaudio.c:152
BINK_BLOCK_MAX_SIZE
#define BINK_BLOCK_MAX_SIZE
Definition: binkaudio.c:44
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
ff_binkaudio_rdft_decoder
AVCodec ff_binkaudio_rdft_decoder
Definition: binkaudio.c:343
DFT_C2R
@ DFT_C2R
Definition: avfft.h:75
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:300
internal.h
BinkAudioContext::trans
union BinkAudioContext::@23 trans
expf
#define expf(x)
Definition: libm.h:283
intfloat.h
sample_rate
sample_rate
Definition: ffmpeg_filter.c:192
av_packet_free
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:64
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
BinkAudioContext::gb
GetBitContext gb
Definition: binkaudio.c:47
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:467
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
DCT_III
@ DCT_III
Definition: avfft.h:95
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:535
fail
#define fail()
Definition: checkasm.h:123
av_int2float
static av_always_inline float av_int2float(uint32_t i)
Reinterpret a 32-bit integer as a float.
Definition: intfloat.h:40
GetBitContext
Definition: get_bits.h:61
ff_rdft_end
av_cold void ff_rdft_end(RDFTContext *s)
Definition: rdft.c:114
AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_STEREO
Definition: channel_layout.h:86
BinkAudioContext
Definition: binkaudio.c:46
BinkAudioContext::quant_table
float quant_table[96]
Definition: binkaudio.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
av_cold
#define av_cold
Definition: attributes.h:90
dct.h
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:628
width
#define width
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
decode_end
static av_cold int decode_end(AVCodecContext *avctx)
Definition: binkaudio.c:274
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
binkaudio_receive_frame
static int binkaudio_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Definition: binkaudio.c:294
AV_CODEC_ID_BINKAUDIO_DCT
@ AV_CODEC_ID_BINKAUDIO_DCT
Definition: codec_id.h:458
decode.h
get_bits.h
ff_wma_critical_freqs
const uint16_t ff_wma_critical_freqs[25]
Definition: wma_freqs.c:23
f
#define f(width, name)
Definition: cbs_vp9.c:255
ldexpf
#define ldexpf(x, exp)
Definition: libm.h:389
BinkAudioContext::bands
unsigned int * bands
Definition: binkaudio.c:55
BinkAudioContext::overlap_len
int overlap_len
overlap size (samples)
Definition: binkaudio.c:52
BinkAudioContext::previous
float previous[MAX_CHANNELS][BINK_BLOCK_MAX_SIZE/16]
coeffs from previous audio block
Definition: binkaudio.c:58
receive_frame
static CopyRet receive_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame)
Definition: crystalhd.c:560
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
FFTSample
float FFTSample
Definition: avfft.h:35
BinkAudioContext::pkt
AVPacket * pkt
Definition: binkaudio.c:60
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1854
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:50
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:188
ff_binkaudio_dct_decoder
AVCodec ff_binkaudio_dct_decoder
Definition: binkaudio.c:355
BinkAudioContext::root
float root
Definition: binkaudio.c:56
BinkAudioContext::coeffs
FFTSample coeffs[BINK_BLOCK_MAX_SIZE]
Definition: binkaudio.c:57
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1194
FFMIN
#define FFMIN(a, b)
Definition: common.h:96
rle_length_tab
static const uint8_t rle_length_tab[16]
Definition: binkaudio.c:161
BinkAudioContext::frame_len
int frame_len
transform size (samples)
Definition: binkaudio.c:51
av_packet_alloc
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:53
ff_dct_end
av_cold void ff_dct_end(DCTContext *s)
Definition: dct.c:221
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:1187
decode_init
static av_cold int decode_init(AVCodecContext *avctx)
Definition: binkaudio.c:68
AVCodec::id
enum AVCodecID id
Definition: codec.h:204
ff_rdft_init
av_cold int ff_rdft_init(RDFTContext *s, int nbits, enum RDFTransformType trans)
Set up a real FFT.
Definition: rdft.c:88
DECLARE_ALIGNED
#define DECLARE_ALIGNED(n, t, v)
Definition: mem.h:112
BinkAudioContext::rdft
RDFTContext rdft
Definition: binkaudio.c:62
ff_dct_init
av_cold int ff_dct_init(DCTContext *s, int nbits, enum DCTTransformType inverse)
Set up DCT.
Definition: dct.c:177
decode_block
static int decode_block(BinkAudioContext *s, float **out, int use_dct)
Decode Bink Audio block.
Definition: binkaudio.c:170
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:627
get_bits_align32
static void get_bits_align32(GetBitContext *s)
Definition: binkaudio.c:288
RDFTContext
Definition: rdft.h:28
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
uint8_t
uint8_t
Definition: audio_convert.c:194
DCTContext
Definition: dct.h:32
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:197
avcodec.h
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
BinkAudioContext::dct
DCTContext dct
Definition: binkaudio.c:63
AVCodecContext
main external API structure.
Definition: avcodec.h:526
channel_layout.h
AV_CODEC_CAP_DELAY
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: codec.h:75
quant
const uint8_t * quant
Definition: vorbis_enc_data.h:458
BinkAudioContext::num_bands
int num_bands
Definition: binkaudio.c:54
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:553
AVPacket
This structure stores compressed data.
Definition: packet.h:332
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
BinkAudioContext::block_size
int block_size
Definition: binkaudio.c:53
coeff
static const double coeff[2][5]
Definition: vf_owdenoise.c:72
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
AV_CODEC_ID_BINKAUDIO_RDFT
@ AV_CODEC_ID_BINKAUDIO_RDFT
Definition: codec_id.h:457
MAX_CHANNELS
#define MAX_CHANNELS
Definition: binkaudio.c:43
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
AV_SAMPLE_FMT_FLT
@ AV_SAMPLE_FMT_FLT
float
Definition: samplefmt.h:63