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.

312 lines
9.1KB

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