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 };