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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
31 #include "avcodec.h"
32 #include "internal.h"
33 #define BITSTREAM_READER_LE
34 #include "get_bits.h"
35 #include "dsputil.h"
36 #include "dct.h"
37 #include "rdft.h"
38 #include "fmtconvert.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 {
53  int version_b;
54  int first;
55  int channels;
56  int frame_len;
59  int num_bands;
60  unsigned int *bands;
61  float root;
63  DECLARE_ALIGNED(16, int16_t, previous)[BINK_BLOCK_MAX_SIZE / 16];
64  DECLARE_ALIGNED(16, int16_t, current)[BINK_BLOCK_MAX_SIZE / 16];
65  float *coeffs_ptr[MAX_CHANNELS];
66  float *prev_ptr[MAX_CHANNELS];
67  uint8_t *packet_buffer;
68  union {
71  } trans;
73 
74 
76 {
77  BinkAudioContext *s = avctx->priv_data;
78  int sample_rate = avctx->sample_rate;
79  int sample_rate_half;
80  int i;
81  int frame_len_bits;
82 
83  dsputil_init(&s->dsp, avctx);
84  ff_fmt_convert_init(&s->fmt_conv, avctx);
85 
86  /* determine frame length */
87  if (avctx->sample_rate < 22050) {
88  frame_len_bits = 9;
89  } else if (avctx->sample_rate < 44100) {
90  frame_len_bits = 10;
91  } else {
92  frame_len_bits = 11;
93  }
94 
95  if (avctx->channels > MAX_CHANNELS) {
96  av_log(avctx, AV_LOG_ERROR, "too many channels: %d\n", avctx->channels);
97  return -1;
98  }
99 
100  s->version_b = avctx->extradata && avctx->extradata[3] == 'b';
101 
102  if (avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT) {
103  // audio is already interleaved for the RDFT format variant
104  sample_rate *= avctx->channels;
105  s->channels = 1;
106  if (!s->version_b)
107  frame_len_bits += av_log2(avctx->channels);
108  } else {
109  s->channels = avctx->channels;
110  }
111 
112  s->frame_len = 1 << frame_len_bits;
113  s->overlap_len = s->frame_len / 16;
114  s->block_size = (s->frame_len - s->overlap_len) * s->channels;
115  sample_rate_half = (sample_rate + 1) / 2;
116  s->root = 2.0 / sqrt(s->frame_len);
117  for (i = 0; i < 96; i++) {
118  /* constant is result of 0.066399999/log10(M_E) */
119  quant_table[i] = expf(i * 0.15289164787221953823f) * s->root;
120  }
121 
122  /* calculate number of bands */
123  for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
124  if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1])
125  break;
126 
127  s->bands = av_malloc((s->num_bands + 1) * sizeof(*s->bands));
128  if (!s->bands)
129  return AVERROR(ENOMEM);
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  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
139 
140  for (i = 0; i < s->channels; i++) {
141  s->coeffs_ptr[i] = s->coeffs + i * s->frame_len;
142  s->prev_ptr[i] = s->coeffs_ptr[i] + s->frame_len - s->overlap_len;
143  }
144 
146  ff_rdft_init(&s->trans.rdft, frame_len_bits, DFT_C2R);
148  ff_dct_init(&s->trans.dct, frame_len_bits, DCT_III);
149  else
150  return -1;
151 
153  avctx->coded_frame = &s->frame;
154 
155  return 0;
156 }
157 
158 static float get_float(GetBitContext *gb)
159 {
160  int power = get_bits(gb, 5);
161  float f = ldexpf(get_bits_long(gb, 23), power - 23);
162  if (get_bits1(gb))
163  f = -f;
164  return f;
165 }
166 
167 static 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 #define GET_BITS_SAFE(out, nbits) do { \
172  if (get_bits_left(gb) < nbits) \
173  return AVERROR_INVALIDDATA; \
174  out = get_bits(gb, nbits); \
175 } while (0)
176 
182 static int decode_block(BinkAudioContext *s, int16_t *out, int use_dct)
183 {
184  int ch, i, j, k;
185  float q, quant[25];
186  int width, coeff;
187  GetBitContext *gb = &s->gb;
188 
189  if (use_dct)
190  skip_bits(gb, 2);
191 
192  for (ch = 0; ch < s->channels; ch++) {
193  FFTSample *coeffs = s->coeffs_ptr[ch];
194  if (s->version_b) {
195  if (get_bits_left(gb) < 64)
196  return AVERROR_INVALIDDATA;
197  coeffs[0] = av_int2float(get_bits_long(gb, 32)) * s->root;
198  coeffs[1] = av_int2float(get_bits_long(gb, 32)) * s->root;
199  } else {
200  if (get_bits_left(gb) < 58)
201  return AVERROR_INVALIDDATA;
202  coeffs[0] = get_float(gb) * s->root;
203  coeffs[1] = get_float(gb) * s->root;
204  }
205 
206  if (get_bits_left(gb) < s->num_bands * 8)
207  return AVERROR_INVALIDDATA;
208  for (i = 0; i < s->num_bands; i++) {
209  int value = get_bits(gb, 8);
210  quant[i] = quant_table[FFMIN(value, 95)];
211  }
212 
213  k = 0;
214  q = quant[0];
215 
216  // parse coefficients
217  i = 2;
218  while (i < s->frame_len) {
219  if (s->version_b) {
220  j = i + 16;
221  } else {
222  int v;
223  GET_BITS_SAFE(v, 1);
224  if (v) {
225  GET_BITS_SAFE(v, 4);
226  j = i + rle_length_tab[v] * 8;
227  } else {
228  j = i + 8;
229  }
230  }
231 
232  j = FFMIN(j, s->frame_len);
233 
234  GET_BITS_SAFE(width, 4);
235  if (width == 0) {
236  memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
237  i = j;
238  while (s->bands[k] < i)
239  q = quant[k++];
240  } else {
241  while (i < j) {
242  if (s->bands[k] == i)
243  q = quant[k++];
244  GET_BITS_SAFE(coeff, width);
245  if (coeff) {
246  int v;
247  GET_BITS_SAFE(v, 1);
248  if (v)
249  coeffs[i] = -q * coeff;
250  else
251  coeffs[i] = q * coeff;
252  } else {
253  coeffs[i] = 0.0f;
254  }
255  i++;
256  }
257  }
258  }
259 
260  if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
261  coeffs[0] /= 0.5;
262  s->trans.dct.dct_calc(&s->trans.dct, coeffs);
263  s->dsp.vector_fmul_scalar(coeffs, coeffs, s->frame_len / 2, s->frame_len);
264  }
266  s->trans.rdft.rdft_calc(&s->trans.rdft, coeffs);
267  }
268 
270  (const float **)s->prev_ptr,
271  s->overlap_len, s->channels);
272  s->fmt_conv.float_to_int16_interleave(out, (const float **)s->coeffs_ptr,
273  s->frame_len - s->overlap_len,
274  s->channels);
275 
276  if (!s->first) {
277  int count = s->overlap_len * s->channels;
278  int shift = av_log2(count);
279  for (i = 0; i < count; i++) {
280  out[i] = (s->previous[i] * (count - i) + out[i] * i) >> shift;
281  }
282  }
283 
284  memcpy(s->previous, s->current,
285  s->overlap_len * s->channels * sizeof(*s->previous));
286 
287  s->first = 0;
288 
289  return 0;
290 }
291 
293 {
294  BinkAudioContext * s = avctx->priv_data;
295  av_freep(&s->bands);
296  av_freep(&s->packet_buffer);
298  ff_rdft_end(&s->trans.rdft);
300  ff_dct_end(&s->trans.dct);
301 
302  return 0;
303 }
304 
306 {
307  int n = (-get_bits_count(s)) & 31;
308  if (n) skip_bits(s, n);
309 }
310 
311 static int decode_frame(AVCodecContext *avctx, void *data,
312  int *got_frame_ptr, AVPacket *avpkt)
313 {
314  BinkAudioContext *s = avctx->priv_data;
315  int16_t *samples;
316  GetBitContext *gb = &s->gb;
317  int ret, consumed = 0;
318 
319  if (!get_bits_left(gb)) {
320  uint8_t *buf;
321  /* handle end-of-stream */
322  if (!avpkt->size) {
323  *got_frame_ptr = 0;
324  return 0;
325  }
326  if (avpkt->size < 4) {
327  av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
328  return AVERROR_INVALIDDATA;
329  }
331  if (!buf)
332  return AVERROR(ENOMEM);
333  s->packet_buffer = buf;
334  memcpy(s->packet_buffer, avpkt->data, avpkt->size);
335  init_get_bits(gb, s->packet_buffer, avpkt->size * 8);
336  consumed = avpkt->size;
337 
338  /* skip reported size */
339  skip_bits_long(gb, 32);
340  }
341 
342  /* get output buffer */
343  s->frame.nb_samples = s->block_size / avctx->channels;
344  if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
345  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
346  return ret;
347  }
348  samples = (int16_t *)s->frame.data[0];
349 
350  if (decode_block(s, samples, avctx->codec->id == CODEC_ID_BINKAUDIO_DCT)) {
351  av_log(avctx, AV_LOG_ERROR, "Incomplete packet\n");
352  return AVERROR_INVALIDDATA;
353  }
354  get_bits_align32(gb);
355 
356  *got_frame_ptr = 1;
357  *(AVFrame *)data = s->frame;
358 
359  return consumed;
360 }
361 
363  .name = "binkaudio_rdft",
364  .type = AVMEDIA_TYPE_AUDIO,
366  .priv_data_size = sizeof(BinkAudioContext),
367  .init = decode_init,
368  .close = decode_end,
369  .decode = decode_frame,
370  .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
371  .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)")
372 };
373 
375  .name = "binkaudio_dct",
376  .type = AVMEDIA_TYPE_AUDIO,
378  .priv_data_size = sizeof(BinkAudioContext),
379  .init = decode_init,
380  .close = decode_end,
381  .decode = decode_frame,
382  .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
383  .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)")
384 };
av_cold void ff_rdft_end(RDFTContext *s)
Definition: rdft.c:130
static const int16_t coeffs[28]
static float get_float(GetBitContext *gb)
Definition: binkaudio.c:158
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
#define MAX_CHANNELS
Definition: binkaudio.c:45
Audio Video Frame.
Definition: avcodec.h:985
void(* dct_calc)(struct DCTContext *s, FFTSample *data)
Definition: dct.h:35
static short * samples
Definition: ffmpeg.c:233
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:237
static av_cold int decode_end(AVCodecContext *avctx)
Definition: binkaudio.c:292
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:197
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2000
static av_always_inline float av_int2float(uint32_t i)
Reinterpret a 32-bit integer as a float.
Definition: intfloat.h:40
FmtConvertContext fmt_conv
Definition: binkaudio.c:52
static const uint8_t rle_length_tab[16]
Definition: binkaudio.c:167
int size
Definition: avcodec.h:909
int width
Definition: rotozoom.c:164
#define DECLARE_ALIGNED(n, t, v)
Definition: mem.h:55
uint8_t * packet_buffer
Definition: binkaudio.c:67
enum CodecID id
Definition: avcodec.h:3198
int16_t previous[BINK_BLOCK_MAX_SIZE/16]
coeffs from previous audio block
Definition: binkaudio.c:63
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:117
struct AVCodec * codec
Definition: avcodec.h:1529
signed 16 bits
Definition: samplefmt.h:30
int16_t current[BINK_BLOCK_MAX_SIZE/16]
Definition: binkaudio.c:64
AVCodec.
Definition: avcodec.h:3189
#define v(n)
Definition: regs.h:34
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:147
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
void(* float_to_int16_interleave)(int16_t *dst, const float **src, long len, int channels)
Convert multiple arrays of float to an interleaved array of int16_t.
Definition: fmtconvert.h:69
union BinkAudioContext::@11 trans
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1464
#define av_cold
Definition: attributes.h:71
#define CONFIG_BINKAUDIO_DCT_DECODER
Definition: config.h:439
Definition: avfft.h:82
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1387
unsigned int * bands
Definition: binkaudio.c:60
const char data[16]
Definition: mxf.c:60
uint8_t * data
Definition: avcodec.h:908
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:192
bitstream reader API header.
av_cold void dsputil_init(DSPContext *c, AVCodecContext *avctx)
Definition: dsputil.c:2789
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:336
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:719
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:513
#define BINK_BLOCK_MAX_SIZE
Definition: binkaudio.c:46
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
static void get_bits_align32(GetBitContext *s)
Definition: binkaudio.c:305
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
const char * name
Name of the codec implementation.
Definition: avcodec.h:3196
float * prev_ptr[MAX_CHANNELS]
pointers to the overlap points in the coeffs array
Definition: binkaudio.c:66
void(* rdft_calc)(struct RDFTContext *s, FFTSample *z)
Definition: rdft.h:60
AVFrame frame
Definition: binkaudio.c:49
void(* vector_fmul_scalar)(float *dst, const float *src, float mul, int len)
Multiply a vector of floats by a scalar float.
Definition: dsputil.h:428
GetBitContext gb
Definition: binkaudio.c:50
#define FFMIN(a, b)
Definition: common.h:55
#define f(n)
Definition: regs.h:33
static float quant_table[96]
Definition: binkaudio.c:43
DCTContext dct
Definition: binkaudio.c:70
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Definition: utils.c:1867
Definition: dct.h:29
static av_cold int decode_init(AVCodecContext *avctx)
Definition: binkaudio.c:75
AVCodec ff_binkaudio_rdft_decoder
Definition: binkaudio.c:362
Definition: avfft.h:62
int overlap_len
overlap size (samples)
Definition: binkaudio.c:57
#define GET_BITS_SAFE(out, nbits)
Definition: binkaudio.c:171
external API header
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: binkaudio.c:311
int sample_rate
samples per second
Definition: avcodec.h:1456
AVCodec ff_binkaudio_dct_decoder
Definition: binkaudio.c:374
main external API structure.
Definition: avcodec.h:1329
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:327
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:268
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:64
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:260
av_cold int ff_dct_init(DCTContext *s, int nbits, enum DCTTransformType inverse)
Set up DCT.
Definition: dct.c:176
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:354
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:497
float * coeffs_ptr[MAX_CHANNELS]
pointers to the coeffs arrays for float_to_int16_interleave
Definition: binkaudio.c:65
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:301
static int decode_block(BinkAudioContext *s, int16_t *out, int use_dct)
Decode Bink Audio block.
Definition: binkaudio.c:182
const uint8_t * quant
DSPContext dsp
Definition: binkaudio.c:51
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
int frame_len
transform size (samples)
Definition: binkaudio.c:56
int version_b
Bink version 'b'.
Definition: binkaudio.c:53
const uint16_t ff_wma_critical_freqs[25]
Definition: wmadata.h:33
common internal api header.
#define CONFIG_BINKAUDIO_RDFT_DECODER
Definition: config.h:440
RDFTContext rdft
Definition: binkaudio.c:69
FFTSample coeffs[BINK_BLOCK_MAX_SIZE]
Definition: binkaudio.c:62
DSP utils.
void * priv_data
Definition: avcodec.h:1531
av_cold void ff_fmt_convert_init(FmtConvertContext *c, AVCodecContext *avctx)
Definition: fmtconvert.c:78
int channels
number of audio channels
Definition: avcodec.h:1457
av_cold void ff_dct_end(DCTContext *s)
Definition: dct.c:217
void avcodec_get_frame_defaults(AVFrame *pic)
Set the fields of the given AVFrame to default values.
Definition: utils.c:609
#define CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: avcodec.h:750
av_cold int ff_rdft_init(RDFTContext *s, int nbits, enum RDFTransformType trans)
Set up a real FFT.
Definition: rdft.c:99
int nb_samples
number of audio samples (per channel) described by this frame
Definition: avcodec.h:1265
DSPContext.
Definition: dsputil.h:226
float FFTSample
Definition: avfft.h:22