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 #include "libavutil/mem_internal.h"
34 
35 #define BITSTREAM_READER_LE
36 #include "avcodec.h"
37 #include "dct.h"
38 #include "decode.h"
39 #include "get_bits.h"
40 #include "internal.h"
41 #include "rdft.h"
42 #include "wma_freqs.h"
43 
44 #define MAX_CHANNELS 2
45 #define BINK_BLOCK_MAX_SIZE (MAX_CHANNELS << 11)
46 
47 typedef struct BinkAudioContext {
49  int version_b; ///< Bink version 'b'
50  int first;
51  int channels;
52  int frame_len; ///< transform size (samples)
53  int overlap_len; ///< overlap size (samples)
55  int num_bands;
56  float root;
57  unsigned int bands[26];
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, ret;
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  /* populate bands data */
127  s->bands[0] = 2;
128  for (i = 1; i < s->num_bands; i++)
129  s->bands[i] = (ff_wma_critical_freqs[i - 1] * s->frame_len / sample_rate_half) & ~1;
130  s->bands[s->num_bands] = s->frame_len;
131 
132  s->first = 1;
133 
134  if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
135  ret = ff_rdft_init(&s->trans.rdft, frame_len_bits, DFT_C2R);
136  else if (CONFIG_BINKAUDIO_DCT_DECODER)
137  ret = ff_dct_init(&s->trans.dct, frame_len_bits, DCT_III);
138  else
139  av_assert0(0);
140  if (ret < 0)
141  return ret;
142 
143  s->pkt = av_packet_alloc();
144  if (!s->pkt)
145  return AVERROR(ENOMEM);
146 
147  return 0;
148 }
149 
150 static float get_float(GetBitContext *gb)
151 {
152  int power = get_bits(gb, 5);
153  float f = ldexpf(get_bits(gb, 23), power - 23);
154  if (get_bits1(gb))
155  f = -f;
156  return f;
157 }
158 
159 static const uint8_t rle_length_tab[16] = {
160  2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
161 };
162 
163 /**
164  * Decode Bink Audio block
165  * @param[out] out Output buffer (must contain s->block_size elements)
166  * @return 0 on success, negative error code on failure
167  */
168 static int decode_block(BinkAudioContext *s, float **out, int use_dct)
169 {
170  int ch, i, j, k;
171  float q, quant[25];
172  int width, coeff;
173  GetBitContext *gb = &s->gb;
174 
175  if (use_dct)
176  skip_bits(gb, 2);
177 
178  for (ch = 0; ch < s->channels; ch++) {
179  FFTSample *coeffs = out[ch];
180 
181  if (s->version_b) {
182  if (get_bits_left(gb) < 64)
183  return AVERROR_INVALIDDATA;
184  coeffs[0] = av_int2float(get_bits_long(gb, 32)) * s->root;
185  coeffs[1] = av_int2float(get_bits_long(gb, 32)) * s->root;
186  } else {
187  if (get_bits_left(gb) < 58)
188  return AVERROR_INVALIDDATA;
189  coeffs[0] = get_float(gb) * s->root;
190  coeffs[1] = get_float(gb) * s->root;
191  }
192 
193  if (get_bits_left(gb) < s->num_bands * 8)
194  return AVERROR_INVALIDDATA;
195  for (i = 0; i < s->num_bands; i++) {
196  int value = get_bits(gb, 8);
197  quant[i] = s->quant_table[FFMIN(value, 95)];
198  }
199 
200  k = 0;
201  q = quant[0];
202 
203  // parse coefficients
204  i = 2;
205  while (i < s->frame_len) {
206  if (s->version_b) {
207  j = i + 16;
208  } else {
209  int v = get_bits1(gb);
210  if (v) {
211  v = get_bits(gb, 4);
212  j = i + rle_length_tab[v] * 8;
213  } else {
214  j = i + 8;
215  }
216  }
217 
218  j = FFMIN(j, s->frame_len);
219 
220  width = get_bits(gb, 4);
221  if (width == 0) {
222  memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
223  i = j;
224  while (s->bands[k] < i)
225  q = quant[k++];
226  } else {
227  while (i < j) {
228  if (s->bands[k] == i)
229  q = quant[k++];
230  coeff = get_bits(gb, width);
231  if (coeff) {
232  int v;
233  v = get_bits1(gb);
234  if (v)
235  coeffs[i] = -q * coeff;
236  else
237  coeffs[i] = q * coeff;
238  } else {
239  coeffs[i] = 0.0f;
240  }
241  i++;
242  }
243  }
244  }
245 
246  if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
247  coeffs[0] /= 0.5;
248  s->trans.dct.dct_calc(&s->trans.dct, coeffs);
249  }
250  else if (CONFIG_BINKAUDIO_RDFT_DECODER)
251  s->trans.rdft.rdft_calc(&s->trans.rdft, coeffs);
252  }
253 
254  for (ch = 0; ch < s->channels; ch++) {
255  int j;
256  int count = s->overlap_len * s->channels;
257  if (!s->first) {
258  j = ch;
259  for (i = 0; i < s->overlap_len; i++, j += s->channels)
260  out[ch][i] = (s->previous[ch][i] * (count - j) +
261  out[ch][i] * j) / count;
262  }
263  memcpy(s->previous[ch], &out[ch][s->frame_len - s->overlap_len],
264  s->overlap_len * sizeof(*s->previous[ch]));
265  }
266 
267  s->first = 0;
268 
269  return 0;
270 }
271 
273 {
274  BinkAudioContext * s = avctx->priv_data;
275  if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
276  ff_rdft_end(&s->trans.rdft);
277  else if (CONFIG_BINKAUDIO_DCT_DECODER)
278  ff_dct_end(&s->trans.dct);
279 
280  av_packet_free(&s->pkt);
281 
282  return 0;
283 }
284 
286 {
287  int n = (-get_bits_count(s)) & 31;
288  if (n) skip_bits(s, n);
289 }
290 
292 {
293  BinkAudioContext *s = avctx->priv_data;
294  GetBitContext *gb = &s->gb;
295  int ret;
296 
297  if (!s->pkt->data) {
298  ret = ff_decode_get_packet(avctx, s->pkt);
299  if (ret < 0)
300  return ret;
301 
302  if (s->pkt->size < 4) {
303  av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
304  ret = AVERROR_INVALIDDATA;
305  goto fail;
306  }
307 
308  ret = init_get_bits8(gb, s->pkt->data, s->pkt->size);
309  if (ret < 0)
310  goto fail;
311 
312  /* skip reported size */
313  skip_bits_long(gb, 32);
314  }
315 
316  /* get output buffer */
317  frame->nb_samples = s->frame_len;
318  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
319  return ret;
320 
321  if (decode_block(s, (float **)frame->extended_data,
322  avctx->codec->id == AV_CODEC_ID_BINKAUDIO_DCT)) {
323  av_log(avctx, AV_LOG_ERROR, "Incomplete packet\n");
324  return AVERROR_INVALIDDATA;
325  }
326  get_bits_align32(gb);
327  if (!get_bits_left(gb)) {
328  memset(gb, 0, sizeof(*gb));
329  av_packet_unref(s->pkt);
330  }
331 
332  frame->nb_samples = s->block_size / avctx->channels;
333 
334  return 0;
335 fail:
336  av_packet_unref(s->pkt);
337  return ret;
338 }
339 
341  .name = "binkaudio_rdft",
342  .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)"),
343  .type = AVMEDIA_TYPE_AUDIO,
345  .priv_data_size = sizeof(BinkAudioContext),
346  .init = decode_init,
347  .close = decode_end,
349  .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
350  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
351 };
352 
354  .name = "binkaudio_dct",
355  .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)"),
356  .type = AVMEDIA_TYPE_AUDIO,
358  .priv_data_size = sizeof(BinkAudioContext),
359  .init = decode_init,
360  .close = decode_end,
362  .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
363  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
364 };
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:49
av_cold void ff_rdft_end(RDFTContext *s)
Definition: rdft.c:114
float, planar
Definition: samplefmt.h:69
const struct AVCodec * codec
Definition: avcodec.h:545
static float get_float(GetBitContext *gb)
Definition: binkaudio.c:150
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define MAX_CHANNELS
Definition: binkaudio.c:44
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
Definition: avfft.h:75
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
static av_cold int decode_end(AVCodecContext *avctx)
Definition: binkaudio.c:272
Definition: avfft.h:95
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:291
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
static av_always_inline float av_int2float(uint32_t i)
Reinterpret a 32-bit integer as a float.
Definition: intfloat.h:40
static const uint8_t rle_length_tab[16]
Definition: binkaudio.c:159
int size
Definition: packet.h:370
int av_log2(unsigned v)
Definition: intmath.c:26
const uint16_t ff_wma_critical_freqs[25]
Definition: wma_freqs.c:23
static CopyRet receive_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame)
Definition: crystalhd.c:560
#define AV_CH_LAYOUT_STEREO
AVCodec.
Definition: codec.h:197
#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:77
unsigned int bands[26]
Definition: binkaudio.c:57
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:75
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1204
uint8_t
#define av_cold
Definition: attributes.h:88
#define f(width, name)
Definition: cbs_vp9.c:255
int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
Called by decoders to get the next packet for decoding.
Definition: decode.c:222
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:108
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:637
uint8_t * data
Definition: packet.h:369
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
static int binkaudio_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Definition: binkaudio.c:291
bitstream reader API header.
float previous[MAX_CHANNELS][BINK_BLOCK_MAX_SIZE/16]
coeffs from previous audio block
Definition: binkaudio.c:58
#define av_log(a,...)
#define expf(x)
Definition: libm.h:283
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
enum AVCodecID id
Definition: codec.h:211
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
#define BINK_BLOCK_MAX_SIZE
Definition: binkaudio.c:45
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:117
static void get_bits_align32(GetBitContext *s)
Definition: binkaudio.c:285
static int decode_block(BinkAudioContext *s, float **out, int use_dct)
Decode Bink Audio block.
Definition: binkaudio.c:168
const char * name
Name of the codec implementation.
Definition: codec.h:204
GLsizei count
Definition: opengl_enc.c:108
float FFTSample
Definition: avfft.h:35
#define fail()
Definition: checkasm.h:133
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1247
GetBitContext gb
Definition: binkaudio.c:48
audio channel layout utility functions
#define FFMIN(a, b)
Definition: common.h:105
#define width
void(* dct_calc)(struct DCTContext *s, FFTSample *data)
Definition: dct.h:38
DCTContext dct
Definition: binkaudio.c:63
Definition: dct.h:32
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
#define s(width, name)
Definition: cbs_vp9.c:257
static av_cold int decode_init(AVCodecContext *avctx)
Definition: binkaudio.c:68
void(* rdft_calc)(struct RDFTContext *s, FFTSample *z)
Definition: rdft.h:38
AVCodec ff_binkaudio_rdft_decoder
Definition: binkaudio.c:340
float quant_table[96]
Definition: binkaudio.c:59
int overlap_len
overlap size (samples)
Definition: binkaudio.c:53
sample_rate
Libavcodec external API header.
int sample_rate
samples per second
Definition: avcodec.h:1196
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
AVCodec ff_binkaudio_dct_decoder
Definition: binkaudio.c:353
main external API structure.
Definition: avcodec.h:536
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:634
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1893
#define ldexpf(x, exp)
Definition: libm.h:389
int extradata_size
Definition: avcodec.h:638
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
AVPacket * pkt
Definition: binkaudio.c:60
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:467
av_cold int ff_dct_init(DCTContext *s, int nbits, enum DCTTransformType inverse)
Set up DCT.
Definition: dct.c:177
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:546
const uint8_t * quant
int frame_len
transform size (samples)
Definition: binkaudio.c:52
int version_b
Bink version &#39;b&#39;.
Definition: binkaudio.c:49
common internal api header.
RDFTContext rdft
Definition: binkaudio.c:62
void * priv_data
Definition: avcodec.h:563
int channels
number of audio channels
Definition: avcodec.h:1197
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:64
static const double coeff[2][5]
Definition: vf_owdenoise.c:73
av_cold void ff_dct_end(DCTContext *s)
Definition: dct.c:221
FILE * out
Definition: movenc.c:54
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
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:365
#define AV_CH_LAYOUT_MONO
av_cold int ff_rdft_init(RDFTContext *s, int nbits, enum RDFTransformType trans)
Set up a real FFT.
Definition: rdft.c:88
This structure stores compressed data.
Definition: packet.h:346
union BinkAudioContext::@23 trans
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:384
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators...
Definition: codec.h:52
int i
Definition: input.c:407