You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

309 lines
8.9KB

  1. /*
  2. * Bink Audio decoder
  3. * Copyright (c) 2007-2010 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. * @file
  24. * Bink Audio decoder
  25. *
  26. * Technical details here:
  27. * http://wiki.multimedia.cx/index.php?title=Bink_Audio
  28. */
  29. #include "avcodec.h"
  30. #define ALT_BITSTREAM_READER_LE
  31. #include "get_bits.h"
  32. #include "dsputil.h"
  33. #include "fft.h"
  34. #include "fmtconvert.h"
  35. extern const uint16_t ff_wma_critical_freqs[25];
  36. #define MAX_CHANNELS 2
  37. #define BINK_BLOCK_MAX_SIZE (MAX_CHANNELS << 11)
  38. typedef struct {
  39. GetBitContext gb;
  40. DSPContext dsp;
  41. FmtConvertContext fmt_conv;
  42. int first;
  43. int channels;
  44. int frame_len; ///< transform size (samples)
  45. int overlap_len; ///< overlap size (samples)
  46. int block_size;
  47. int num_bands;
  48. unsigned int *bands;
  49. float root;
  50. DECLARE_ALIGNED(16, FFTSample, coeffs)[BINK_BLOCK_MAX_SIZE];
  51. DECLARE_ALIGNED(16, short, previous)[BINK_BLOCK_MAX_SIZE / 16]; ///< coeffs from previous audio block
  52. float *coeffs_ptr[MAX_CHANNELS]; ///< pointers to the coeffs arrays for float_to_int16_interleave
  53. union {
  54. RDFTContext rdft;
  55. DCTContext dct;
  56. } trans;
  57. } BinkAudioContext;
  58. static av_cold int decode_init(AVCodecContext *avctx)
  59. {
  60. BinkAudioContext *s = avctx->priv_data;
  61. int sample_rate = avctx->sample_rate;
  62. int sample_rate_half;
  63. int i;
  64. int frame_len_bits;
  65. dsputil_init(&s->dsp, avctx);
  66. ff_fmt_convert_init(&s->fmt_conv, avctx);
  67. /* determine frame length */
  68. if (avctx->sample_rate < 22050) {
  69. frame_len_bits = 9;
  70. } else if (avctx->sample_rate < 44100) {
  71. frame_len_bits = 10;
  72. } else {
  73. frame_len_bits = 11;
  74. }
  75. s->frame_len = 1 << frame_len_bits;
  76. if (avctx->channels > MAX_CHANNELS) {
  77. av_log(avctx, AV_LOG_ERROR, "too many channels: %d\n", avctx->channels);
  78. return -1;
  79. }
  80. if (avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT) {
  81. // audio is already interleaved for the RDFT format variant
  82. sample_rate *= avctx->channels;
  83. s->frame_len *= avctx->channels;
  84. s->channels = 1;
  85. if (avctx->channels == 2)
  86. frame_len_bits++;
  87. } else {
  88. s->channels = avctx->channels;
  89. }
  90. s->overlap_len = s->frame_len / 16;
  91. s->block_size = (s->frame_len - s->overlap_len) * s->channels;
  92. sample_rate_half = (sample_rate + 1) / 2;
  93. s->root = 2.0 / sqrt(s->frame_len);
  94. /* calculate number of bands */
  95. for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
  96. if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1])
  97. break;
  98. s->bands = av_malloc((s->num_bands + 1) * sizeof(*s->bands));
  99. if (!s->bands)
  100. return AVERROR(ENOMEM);
  101. /* populate bands data */
  102. s->bands[0] = 1;
  103. for (i = 1; i < s->num_bands; i++)
  104. s->bands[i] = ff_wma_critical_freqs[i - 1] * (s->frame_len / 2) / sample_rate_half;
  105. s->bands[s->num_bands] = s->frame_len / 2;
  106. s->first = 1;
  107. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  108. for (i = 0; i < s->channels; i++)
  109. s->coeffs_ptr[i] = s->coeffs + i * s->frame_len;
  110. if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
  111. ff_rdft_init(&s->trans.rdft, frame_len_bits, DFT_C2R);
  112. else if (CONFIG_BINKAUDIO_DCT_DECODER)
  113. ff_dct_init(&s->trans.dct, frame_len_bits, DCT_III);
  114. else
  115. return -1;
  116. return 0;
  117. }
  118. static float get_float(GetBitContext *gb)
  119. {
  120. int power = get_bits(gb, 5);
  121. float f = ldexpf(get_bits_long(gb, 23), power - 23);
  122. if (get_bits1(gb))
  123. f = -f;
  124. return f;
  125. }
  126. static const uint8_t rle_length_tab[16] = {
  127. 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
  128. };
  129. /**
  130. * Decode Bink Audio block
  131. * @param[out] out Output buffer (must contain s->block_size elements)
  132. */
  133. static void decode_block(BinkAudioContext *s, short *out, int use_dct)
  134. {
  135. int ch, i, j, k;
  136. float q, quant[25];
  137. int width, coeff;
  138. GetBitContext *gb = &s->gb;
  139. if (use_dct)
  140. skip_bits(gb, 2);
  141. for (ch = 0; ch < s->channels; ch++) {
  142. FFTSample *coeffs = s->coeffs_ptr[ch];
  143. q = 0.0f;
  144. coeffs[0] = get_float(gb) * s->root;
  145. coeffs[1] = get_float(gb) * s->root;
  146. for (i = 0; i < s->num_bands; i++) {
  147. /* constant is result of 0.066399999/log10(M_E) */
  148. int value = get_bits(gb, 8);
  149. quant[i] = expf(FFMIN(value, 95) * 0.15289164787221953823f) * s->root;
  150. }
  151. // find band (k)
  152. for (k = 0; s->bands[k] < 1; k++) {
  153. q = quant[k];
  154. }
  155. // parse coefficients
  156. i = 2;
  157. while (i < s->frame_len) {
  158. if (get_bits1(gb)) {
  159. j = i + rle_length_tab[get_bits(gb, 4)] * 8;
  160. } else {
  161. j = i + 8;
  162. }
  163. j = FFMIN(j, s->frame_len);
  164. width = get_bits(gb, 4);
  165. if (width == 0) {
  166. memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
  167. i = j;
  168. while (s->bands[k] * 2 < i)
  169. q = quant[k++];
  170. } else {
  171. while (i < j) {
  172. if (s->bands[k] * 2 == i)
  173. q = quant[k++];
  174. coeff = get_bits(gb, width);
  175. if (coeff) {
  176. if (get_bits1(gb))
  177. coeffs[i] = -q * coeff;
  178. else
  179. coeffs[i] = q * coeff;
  180. } else {
  181. coeffs[i] = 0.0f;
  182. }
  183. i++;
  184. }
  185. }
  186. }
  187. if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
  188. coeffs[0] /= 0.5;
  189. ff_dct_calc (&s->trans.dct, coeffs);
  190. s->dsp.vector_fmul_scalar(coeffs, coeffs, s->frame_len / 2, s->frame_len);
  191. }
  192. else if (CONFIG_BINKAUDIO_RDFT_DECODER)
  193. ff_rdft_calc(&s->trans.rdft, coeffs);
  194. }
  195. s->fmt_conv.float_to_int16_interleave(out, (const float **)s->coeffs_ptr,
  196. s->frame_len, s->channels);
  197. if (!s->first) {
  198. int count = s->overlap_len * s->channels;
  199. int shift = av_log2(count);
  200. for (i = 0; i < count; i++) {
  201. out[i] = (s->previous[i] * (count - i) + out[i] * i) >> shift;
  202. }
  203. }
  204. memcpy(s->previous, out + s->block_size,
  205. s->overlap_len * s->channels * sizeof(*out));
  206. s->first = 0;
  207. }
  208. static av_cold int decode_end(AVCodecContext *avctx)
  209. {
  210. BinkAudioContext * s = avctx->priv_data;
  211. av_freep(&s->bands);
  212. if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
  213. ff_rdft_end(&s->trans.rdft);
  214. else if (CONFIG_BINKAUDIO_DCT_DECODER)
  215. ff_dct_end(&s->trans.dct);
  216. return 0;
  217. }
  218. static void get_bits_align32(GetBitContext *s)
  219. {
  220. int n = (-get_bits_count(s)) & 31;
  221. if (n) skip_bits(s, n);
  222. }
  223. static int decode_frame(AVCodecContext *avctx,
  224. void *data, int *data_size,
  225. AVPacket *avpkt)
  226. {
  227. BinkAudioContext *s = avctx->priv_data;
  228. const uint8_t *buf = avpkt->data;
  229. int buf_size = avpkt->size;
  230. short *samples = data;
  231. short *samples_end = (short*)((uint8_t*)data + *data_size);
  232. int reported_size;
  233. GetBitContext *gb = &s->gb;
  234. init_get_bits(gb, buf, buf_size * 8);
  235. reported_size = get_bits_long(gb, 32);
  236. while (get_bits_count(gb) / 8 < buf_size &&
  237. samples + s->block_size <= samples_end) {
  238. decode_block(s, samples, avctx->codec->id == CODEC_ID_BINKAUDIO_DCT);
  239. samples += s->block_size;
  240. get_bits_align32(gb);
  241. }
  242. *data_size = FFMIN(reported_size, (uint8_t*)samples - (uint8_t*)data);
  243. return buf_size;
  244. }
  245. AVCodec ff_binkaudio_rdft_decoder = {
  246. "binkaudio_rdft",
  247. AVMEDIA_TYPE_AUDIO,
  248. CODEC_ID_BINKAUDIO_RDFT,
  249. sizeof(BinkAudioContext),
  250. decode_init,
  251. NULL,
  252. decode_end,
  253. decode_frame,
  254. .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)")
  255. };
  256. AVCodec ff_binkaudio_dct_decoder = {
  257. "binkaudio_dct",
  258. AVMEDIA_TYPE_AUDIO,
  259. CODEC_ID_BINKAUDIO_DCT,
  260. sizeof(BinkAudioContext),
  261. decode_init,
  262. NULL,
  263. decode_end,
  264. decode_frame,
  265. .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)")
  266. };