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 "avcodec.h"
33 #define BITSTREAM_READER_LE
34 #include "get_bits.h"
35 #include "dct.h"
36 #include "rdft.h"
37 #include "fmtconvert.h"
38 #include "internal.h"
39 #include "libavutil/intfloat.h"
40 
41 extern const uint16_t ff_wma_critical_freqs[25];
42 
43 static float quant_table[96];
44 
45 #define MAX_CHANNELS 2
46 #define BINK_BLOCK_MAX_SIZE (MAX_CHANNELS << 11)
47 
48 typedef struct {
50  int version_b; ///< Bink version 'b'
51  int first;
52  int channels;
53  int frame_len; ///< transform size (samples)
54  int overlap_len; ///< overlap size (samples)
56  int num_bands;
57  unsigned int *bands;
58  float root;
60  float previous[MAX_CHANNELS][BINK_BLOCK_MAX_SIZE / 16]; ///< coeffs from previous audio block
61  uint8_t *packet_buffer;
62  union {
63  RDFTContext rdft;
64  DCTContext dct;
65  } trans;
67 
68 
70 {
71  BinkAudioContext *s = avctx->priv_data;
72  int sample_rate = avctx->sample_rate;
73  int sample_rate_half;
74  int i;
75  int frame_len_bits;
76 
77  /* determine frame length */
78  if (avctx->sample_rate < 22050) {
79  frame_len_bits = 9;
80  } else if (avctx->sample_rate < 44100) {
81  frame_len_bits = 10;
82  } else {
83  frame_len_bits = 11;
84  }
85 
86  if (avctx->channels < 1 || avctx->channels > MAX_CHANNELS) {
87  av_log(avctx, AV_LOG_ERROR, "invalid number of channels: %d\n", avctx->channels);
88  return AVERROR_INVALIDDATA;
89  }
90  avctx->channel_layout = avctx->channels == 1 ? AV_CH_LAYOUT_MONO :
92 
93  s->version_b = avctx->extradata_size >= 4 && avctx->extradata[3] == 'b';
94 
95  if (avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT) {
96  // audio is already interleaved for the RDFT format variant
98  sample_rate *= avctx->channels;
99  s->channels = 1;
100  if (!s->version_b)
101  frame_len_bits += av_log2(avctx->channels);
102  } else {
103  s->channels = avctx->channels;
105  }
106 
107  s->frame_len = 1 << frame_len_bits;
108  s->overlap_len = s->frame_len / 16;
109  s->block_size = (s->frame_len - s->overlap_len) * s->channels;
110  sample_rate_half = (sample_rate + 1) / 2;
111  if (avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
112  s->root = 2.0 / (sqrt(s->frame_len) * 32768.0);
113  else
114  s->root = s->frame_len / (sqrt(s->frame_len) * 32768.0);
115  for (i = 0; i < 96; i++) {
116  /* constant is result of 0.066399999/log10(M_E) */
117  quant_table[i] = expf(i * 0.15289164787221953823f) * s->root;
118  }
119 
120  /* calculate number of bands */
121  for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
122  if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1])
123  break;
124 
125  s->bands = av_malloc((s->num_bands + 1) * sizeof(*s->bands));
126  if (!s->bands)
127  return AVERROR(ENOMEM);
128 
129  /* populate bands data */
130  s->bands[0] = 2;
131  for (i = 1; i < s->num_bands; i++)
132  s->bands[i] = (ff_wma_critical_freqs[i - 1] * s->frame_len / sample_rate_half) & ~1;
133  s->bands[s->num_bands] = s->frame_len;
134 
135  s->first = 1;
136 
138  ff_rdft_init(&s->trans.rdft, frame_len_bits, DFT_C2R);
140  ff_dct_init(&s->trans.dct, frame_len_bits, DCT_III);
141  else
142  return -1;
143 
144  return 0;
145 }
146 
147 static float get_float(GetBitContext *gb)
148 {
149  int power = get_bits(gb, 5);
150  float f = ldexpf(get_bits_long(gb, 23), power - 23);
151  if (get_bits1(gb))
152  f = -f;
153  return f;
154 }
155 
156 static const uint8_t rle_length_tab[16] = {
157  2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
158 };
159 
160 /**
161  * Decode Bink Audio block
162  * @param[out] out Output buffer (must contain s->block_size elements)
163  * @return 0 on success, negative error code on failure
164  */
165 static int decode_block(BinkAudioContext *s, float **out, int use_dct)
166 {
167  int ch, i, j, k;
168  float q, quant[25];
169  int width, coeff;
170  GetBitContext *gb = &s->gb;
171 
172  if (use_dct)
173  skip_bits(gb, 2);
174 
175  for (ch = 0; ch < s->channels; ch++) {
176  FFTSample *coeffs = out[ch];
177 
178  if (s->version_b) {
179  if (get_bits_left(gb) < 64)
180  return AVERROR_INVALIDDATA;
181  coeffs[0] = av_int2float(get_bits_long(gb, 32)) * s->root;
182  coeffs[1] = av_int2float(get_bits_long(gb, 32)) * s->root;
183  } else {
184  if (get_bits_left(gb) < 58)
185  return AVERROR_INVALIDDATA;
186  coeffs[0] = get_float(gb) * s->root;
187  coeffs[1] = get_float(gb) * s->root;
188  }
189 
190  if (get_bits_left(gb) < s->num_bands * 8)
191  return AVERROR_INVALIDDATA;
192  for (i = 0; i < s->num_bands; i++) {
193  int value = get_bits(gb, 8);
194  quant[i] = quant_table[FFMIN(value, 95)];
195  }
196 
197  k = 0;
198  q = quant[0];
199 
200  // parse coefficients
201  i = 2;
202  while (i < s->frame_len) {
203  if (s->version_b) {
204  j = i + 16;
205  } else {
206  int v = get_bits1(gb);
207  if (v) {
208  v = get_bits(gb, 4);
209  j = i + rle_length_tab[v] * 8;
210  } else {
211  j = i + 8;
212  }
213  }
214 
215  j = FFMIN(j, s->frame_len);
216 
217  width = get_bits(gb, 4);
218  if (width == 0) {
219  memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
220  i = j;
221  while (s->bands[k] < i)
222  q = quant[k++];
223  } else {
224  while (i < j) {
225  if (s->bands[k] == i)
226  q = quant[k++];
227  coeff = get_bits(gb, width);
228  if (coeff) {
229  int v;
230  v = get_bits1(gb);
231  if (v)
232  coeffs[i] = -q * coeff;
233  else
234  coeffs[i] = q * coeff;
235  } else {
236  coeffs[i] = 0.0f;
237  }
238  i++;
239  }
240  }
241  }
242 
243  if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
244  coeffs[0] /= 0.5;
245  s->trans.dct.dct_calc(&s->trans.dct, coeffs);
246  }
248  s->trans.rdft.rdft_calc(&s->trans.rdft, coeffs);
249  }
250 
251  for (ch = 0; ch < s->channels; ch++) {
252  int j;
253  int count = s->overlap_len * s->channels;
254  if (!s->first) {
255  j = ch;
256  for (i = 0; i < s->overlap_len; i++, j += s->channels)
257  out[ch][i] = (s->previous[ch][i] * (count - j) +
258  out[ch][i] * j) / count;
259  }
260  memcpy(s->previous[ch], &out[ch][s->frame_len - s->overlap_len],
261  s->overlap_len * sizeof(*s->previous[ch]));
262  }
263 
264  s->first = 0;
265 
266  return 0;
267 }
268 
270 {
271  BinkAudioContext * s = avctx->priv_data;
272  av_freep(&s->bands);
273  av_freep(&s->packet_buffer);
275  ff_rdft_end(&s->trans.rdft);
277  ff_dct_end(&s->trans.dct);
278 
279  return 0;
280 }
281 
283 {
284  int n = (-get_bits_count(s)) & 31;
285  if (n) skip_bits(s, n);
286 }
287 
288 static int decode_frame(AVCodecContext *avctx, void *data,
289  int *got_frame_ptr, AVPacket *avpkt)
290 {
291  BinkAudioContext *s = avctx->priv_data;
292  AVFrame *frame = data;
293  GetBitContext *gb = &s->gb;
294  int ret, consumed = 0;
295 
296  if (!get_bits_left(gb)) {
297  uint8_t *buf;
298  /* handle end-of-stream */
299  if (!avpkt->size) {
300  *got_frame_ptr = 0;
301  return 0;
302  }
303  if (avpkt->size < 4) {
304  av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
305  return AVERROR_INVALIDDATA;
306  }
307  buf = av_realloc(s->packet_buffer, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
308  if (!buf)
309  return AVERROR(ENOMEM);
310  s->packet_buffer = buf;
311  memcpy(s->packet_buffer, avpkt->data, avpkt->size);
312  init_get_bits(gb, s->packet_buffer, avpkt->size * 8);
313  consumed = avpkt->size;
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 
331  frame->nb_samples = s->block_size / avctx->channels;
332  *got_frame_ptr = 1;
333 
334  return consumed;
335 }
336 
338  .name = "binkaudio_rdft",
339  .type = AVMEDIA_TYPE_AUDIO,
341  .priv_data_size = sizeof(BinkAudioContext),
342  .init = decode_init,
343  .close = decode_end,
344  .decode = decode_frame,
345  .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
346  .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)")
347 };
348 
350  .name = "binkaudio_dct",
351  .type = AVMEDIA_TYPE_AUDIO,
353  .priv_data_size = sizeof(BinkAudioContext),
354  .init = decode_init,
355  .close = decode_end,
356  .decode = decode_frame,
357  .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
358  .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)")
359 };
av_cold void ff_rdft_end(RDFTContext *s)
Definition: rdft.c:130
const struct AVCodec * codec
float v
static float get_float(GetBitContext *gb)
Definition: binkaudio.c:147
const char * s
Definition: avisynth_c.h:668
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define MAX_CHANNELS
Definition: binkaudio.c:45
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
Definition: avfft.h:75
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
static av_cold int decode_end(AVCodecContext *avctx)
Definition: binkaudio.c:269
Definition: avfft.h:95
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:198
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
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:156
Sinusoidal phase f
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:141
#define AV_CH_LAYOUT_STEREO
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
enum AVSampleFormat sample_fmt
audio sample format
uint8_t
#define av_cold
Definition: attributes.h:78
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
unsigned int * bands
Definition: binkaudio.c:57
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
uint8_t * data
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:193
bitstream reader API header.
integer sqrt
Definition: avutil.txt:2
float, planar
Definition: samplefmt.h:60
frame
Definition: stft.m:14
#define expf(x)
Definition: libm.h:72
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:557
enum AVCodecID id
#define BINK_BLOCK_MAX_SIZE
Definition: binkaudio.c:46
#define CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Spectrum Plot time data
static void get_bits_align32(GetBitContext *s)
Definition: binkaudio.c:282
static int decode_block(BinkAudioContext *s, float **out, int use_dct)
Decode Bink Audio block.
Definition: binkaudio.c:165
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
const char * name
Name of the codec implementation.
DECLARE_ALIGNED(32, FFTSample, coeffs)
Definition: binkaudio.c:59
float FFTSample
Definition: avfft.h:35
external API header
uint64_t channel_layout
Audio channel layout.
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
GetBitContext gb
Definition: binkaudio.c:49
audio channel layout utility functions
#define FFMIN(a, b)
Definition: common.h:58
ret
Definition: avfilter.c:821
static float quant_table[96]
Definition: binkaudio.c:43
Definition: dct.h:31
static av_cold int decode_init(AVCodecContext *avctx)
Definition: binkaudio.c:69
AVCodec ff_binkaudio_rdft_decoder
Definition: binkaudio.c:337
int overlap_len
overlap size (samples)
Definition: binkaudio.c:54
for k
static int width
Definition: tests/utils.c:158
sample_rate
#define CONFIG_BINKAUDIO_DCT_DECODER
Definition: config.h:654
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: binkaudio.c:288
int sample_rate
samples per second
AVCodec ff_binkaudio_dct_decoder
Definition: binkaudio.c:349
main external API structure.
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:375
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
#define ldexpf(x, exp)
Definition: libm.h:107
void * buf
Definition: avisynth_c.h:594
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:273
#define CONFIG_BINKAUDIO_RDFT_DECODER
Definition: config.h:655
double value
Definition: eval.c:82
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:265
av_cold int ff_dct_init(DCTContext *s, int nbits, enum DCTTransformType inverse)
Set up DCT.
Definition: dct.c:177
synthesis window for stochastic i
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:379
static const double coeff[2][5]
Definition: vf_ow.c:64
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:306
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
const uint8_t * quant
int frame_len
transform size (samples)
Definition: binkaudio.c:53
int version_b
Bink version &#39;b&#39;.
Definition: binkaudio.c:50
const uint16_t ff_wma_critical_freqs[25]
Definition: wmadata.h:33
common internal api header.
int channels
number of audio channels
#define av_log2
Definition: intmath.h:89
av_cold void ff_dct_end(DCTContext *s)
Definition: dct.c:218
void INT64 INT64 count
Definition: avisynth_c.h:594
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31))))#define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac){}void ff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map){AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);return NULL;}return ac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;}int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){int use_generic=1;int len=in->nb_samples;int p;if(ac->dc){av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> out
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: crystalhd.c:868
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:117
#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:99
This structure stores compressed data.
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:127