FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "wma.h"
40 #include "libavutil/intfloat.h"
41 
42 static float quant_table[96];
43 
44 #define MAX_CHANNELS 2
45 #define BINK_BLOCK_MAX_SIZE (MAX_CHANNELS << 11)
46 
47 typedef struct {
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  unsigned int *bands;
57  float root;
59  float previous[MAX_CHANNELS][BINK_BLOCK_MAX_SIZE / 16]; ///< coeffs from previous audio block
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  sample_rate *= avctx->channels;
98  s->channels = 1;
99  if (!s->version_b)
100  frame_len_bits += av_log2(avctx->channels);
101  } else {
102  s->channels = avctx->channels;
104  }
105 
106  s->frame_len = 1 << frame_len_bits;
107  s->overlap_len = s->frame_len / 16;
108  s->block_size = (s->frame_len - s->overlap_len) * s->channels;
109  sample_rate_half = (sample_rate + 1) / 2;
110  if (avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
111  s->root = 2.0 / (sqrt(s->frame_len) * 32768.0);
112  else
113  s->root = s->frame_len / (sqrt(s->frame_len) * 32768.0);
114  for (i = 0; i < 96; i++) {
115  /* constant is result of 0.066399999/log10(M_E) */
116  quant_table[i] = expf(i * 0.15289164787221953823f) * s->root;
117  }
118 
119  /* calculate number of bands */
120  for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
121  if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1])
122  break;
123 
124  s->bands = av_malloc((s->num_bands + 1) * sizeof(*s->bands));
125  if (!s->bands)
126  return AVERROR(ENOMEM);
127 
128  /* populate bands data */
129  s->bands[0] = 2;
130  for (i = 1; i < s->num_bands; i++)
131  s->bands[i] = (ff_wma_critical_freqs[i - 1] * s->frame_len / sample_rate_half) & ~1;
132  s->bands[s->num_bands] = s->frame_len;
133 
134  s->first = 1;
135 
136  if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
137  ff_rdft_init(&s->trans.rdft, frame_len_bits, DFT_C2R);
138  else if (CONFIG_BINKAUDIO_DCT_DECODER)
139  ff_dct_init(&s->trans.dct, frame_len_bits, DCT_III);
140  else
141  return -1;
142 
143  return 0;
144 }
145 
146 static float get_float(GetBitContext *gb)
147 {
148  int power = get_bits(gb, 5);
149  float f = ldexpf(get_bits_long(gb, 23), power - 23);
150  if (get_bits1(gb))
151  f = -f;
152  return f;
153 }
154 
155 static const uint8_t rle_length_tab[16] = {
156  2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
157 };
158 
159 /**
160  * Decode Bink Audio block
161  * @param[out] out Output buffer (must contain s->block_size elements)
162  * @return 0 on success, negative error code on failure
163  */
164 static int decode_block(BinkAudioContext *s, float **out, int use_dct)
165 {
166  int ch, i, j, k;
167  float q, quant[25];
168  int width, coeff;
169  GetBitContext *gb = &s->gb;
170 
171  if (use_dct)
172  skip_bits(gb, 2);
173 
174  for (ch = 0; ch < s->channels; ch++) {
175  FFTSample *coeffs = out[ch];
176 
177  if (s->version_b) {
178  if (get_bits_left(gb) < 64)
179  return AVERROR_INVALIDDATA;
180  coeffs[0] = av_int2float(get_bits_long(gb, 32)) * s->root;
181  coeffs[1] = av_int2float(get_bits_long(gb, 32)) * s->root;
182  } else {
183  if (get_bits_left(gb) < 58)
184  return AVERROR_INVALIDDATA;
185  coeffs[0] = get_float(gb) * s->root;
186  coeffs[1] = get_float(gb) * s->root;
187  }
188 
189  if (get_bits_left(gb) < s->num_bands * 8)
190  return AVERROR_INVALIDDATA;
191  for (i = 0; i < s->num_bands; i++) {
192  int value = get_bits(gb, 8);
193  quant[i] = quant_table[FFMIN(value, 95)];
194  }
195 
196  k = 0;
197  q = quant[0];
198 
199  // parse coefficients
200  i = 2;
201  while (i < s->frame_len) {
202  if (s->version_b) {
203  j = i + 16;
204  } else {
205  int v = get_bits1(gb);
206  if (v) {
207  v = get_bits(gb, 4);
208  j = i + rle_length_tab[v] * 8;
209  } else {
210  j = i + 8;
211  }
212  }
213 
214  j = FFMIN(j, s->frame_len);
215 
216  width = get_bits(gb, 4);
217  if (width == 0) {
218  memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
219  i = j;
220  while (s->bands[k] < i)
221  q = quant[k++];
222  } else {
223  while (i < j) {
224  if (s->bands[k] == i)
225  q = quant[k++];
226  coeff = get_bits(gb, width);
227  if (coeff) {
228  int v;
229  v = get_bits1(gb);
230  if (v)
231  coeffs[i] = -q * coeff;
232  else
233  coeffs[i] = q * coeff;
234  } else {
235  coeffs[i] = 0.0f;
236  }
237  i++;
238  }
239  }
240  }
241 
242  if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
243  coeffs[0] /= 0.5;
244  s->trans.dct.dct_calc(&s->trans.dct, coeffs);
245  }
246  else if (CONFIG_BINKAUDIO_RDFT_DECODER)
247  s->trans.rdft.rdft_calc(&s->trans.rdft, coeffs);
248  }
249 
250  for (ch = 0; ch < s->channels; ch++) {
251  int j;
252  int count = s->overlap_len * s->channels;
253  if (!s->first) {
254  j = ch;
255  for (i = 0; i < s->overlap_len; i++, j += s->channels)
256  out[ch][i] = (s->previous[ch][i] * (count - j) +
257  out[ch][i] * j) / count;
258  }
259  memcpy(s->previous[ch], &out[ch][s->frame_len - s->overlap_len],
260  s->overlap_len * sizeof(*s->previous[ch]));
261  }
262 
263  s->first = 0;
264 
265  return 0;
266 }
267 
269 {
270  BinkAudioContext * s = avctx->priv_data;
271  av_freep(&s->bands);
272  av_freep(&s->packet_buffer);
273  if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
274  ff_rdft_end(&s->trans.rdft);
275  else if (CONFIG_BINKAUDIO_DCT_DECODER)
276  ff_dct_end(&s->trans.dct);
277 
278  return 0;
279 }
280 
282 {
283  int n = (-get_bits_count(s)) & 31;
284  if (n) skip_bits(s, n);
285 }
286 
287 static int decode_frame(AVCodecContext *avctx, void *data,
288  int *got_frame_ptr, AVPacket *avpkt)
289 {
290  BinkAudioContext *s = avctx->priv_data;
291  AVFrame *frame = data;
292  GetBitContext *gb = &s->gb;
293  int ret, consumed = 0;
294 
295  if (!get_bits_left(gb)) {
296  uint8_t *buf;
297  /* handle end-of-stream */
298  if (!avpkt->size) {
299  *got_frame_ptr = 0;
300  return 0;
301  }
302  if (avpkt->size < 4) {
303  av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
304  return AVERROR_INVALIDDATA;
305  }
307  if (!buf)
308  return AVERROR(ENOMEM);
309  memset(buf + avpkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
310  s->packet_buffer = buf;
311  memcpy(s->packet_buffer, avpkt->data, avpkt->size);
312  if ((ret = init_get_bits8(gb, s->packet_buffer, avpkt->size)) < 0)
313  return ret;
314  consumed = avpkt->size;
315 
316  /* skip reported size */
317  skip_bits_long(gb, 32);
318  }
319 
320  /* get output buffer */
321  frame->nb_samples = s->frame_len;
322  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
323  return ret;
324 
325  if (decode_block(s, (float **)frame->extended_data,
326  avctx->codec->id == AV_CODEC_ID_BINKAUDIO_DCT)) {
327  av_log(avctx, AV_LOG_ERROR, "Incomplete packet\n");
328  return AVERROR_INVALIDDATA;
329  }
330  get_bits_align32(gb);
331 
332  frame->nb_samples = s->block_size / avctx->channels;
333  *got_frame_ptr = 1;
334 
335  return consumed;
336 }
337 
339  .name = "binkaudio_rdft",
340  .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)"),
341  .type = AVMEDIA_TYPE_AUDIO,
343  .priv_data_size = sizeof(BinkAudioContext),
344  .init = decode_init,
345  .close = decode_end,
346  .decode = decode_frame,
347  .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
348 };
349 
351  .name = "binkaudio_dct",
352  .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)"),
353  .type = AVMEDIA_TYPE_AUDIO,
355  .priv_data_size = sizeof(BinkAudioContext),
356  .init = decode_init,
357  .close = decode_end,
358  .decode = decode_frame,
359  .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
360 };