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.

304 lines
8.8KB

  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. if (avctx->channels > MAX_CHANNELS) {
  76. av_log(avctx, AV_LOG_ERROR, "too many channels: %d\n", avctx->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->channels = 1;
  83. frame_len_bits += av_log2(avctx->channels);
  84. } else {
  85. s->channels = avctx->channels;
  86. }
  87. s->frame_len = 1 << frame_len_bits;
  88. s->overlap_len = s->frame_len / 16;
  89. s->block_size = (s->frame_len - s->overlap_len) * s->channels;
  90. sample_rate_half = (sample_rate + 1) / 2;
  91. s->root = 2.0 / sqrt(s->frame_len);
  92. /* calculate number of bands */
  93. for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
  94. if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1])
  95. break;
  96. s->bands = av_malloc((s->num_bands + 1) * sizeof(*s->bands));
  97. if (!s->bands)
  98. return AVERROR(ENOMEM);
  99. /* populate bands data */
  100. s->bands[0] = 2;
  101. for (i = 1; i < s->num_bands; i++)
  102. s->bands[i] = (ff_wma_critical_freqs[i - 1] * s->frame_len / sample_rate_half) & ~1;
  103. s->bands[s->num_bands] = s->frame_len;
  104. s->first = 1;
  105. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  106. for (i = 0; i < s->channels; i++)
  107. s->coeffs_ptr[i] = s->coeffs + i * s->frame_len;
  108. if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
  109. ff_rdft_init(&s->trans.rdft, frame_len_bits, DFT_C2R);
  110. else if (CONFIG_BINKAUDIO_DCT_DECODER)
  111. ff_dct_init(&s->trans.dct, frame_len_bits, DCT_III);
  112. else
  113. return -1;
  114. return 0;
  115. }
  116. static float get_float(GetBitContext *gb)
  117. {
  118. int power = get_bits(gb, 5);
  119. float f = ldexpf(get_bits_long(gb, 23), power - 23);
  120. if (get_bits1(gb))
  121. f = -f;
  122. return f;
  123. }
  124. static const uint8_t rle_length_tab[16] = {
  125. 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
  126. };
  127. /**
  128. * Decode Bink Audio block
  129. * @param[out] out Output buffer (must contain s->block_size elements)
  130. */
  131. static void decode_block(BinkAudioContext *s, short *out, int use_dct)
  132. {
  133. int ch, i, j, k;
  134. float q, quant[25];
  135. int width, coeff;
  136. GetBitContext *gb = &s->gb;
  137. if (use_dct)
  138. skip_bits(gb, 2);
  139. for (ch = 0; ch < s->channels; ch++) {
  140. FFTSample *coeffs = s->coeffs_ptr[ch];
  141. coeffs[0] = get_float(gb) * s->root;
  142. coeffs[1] = get_float(gb) * s->root;
  143. for (i = 0; i < s->num_bands; i++) {
  144. /* constant is result of 0.066399999/log10(M_E) */
  145. int value = get_bits(gb, 8);
  146. quant[i] = expf(FFMIN(value, 95) * 0.15289164787221953823f) * s->root;
  147. }
  148. k = 0;
  149. q = quant[0];
  150. // parse coefficients
  151. i = 2;
  152. while (i < s->frame_len) {
  153. if (get_bits1(gb)) {
  154. j = i + rle_length_tab[get_bits(gb, 4)] * 8;
  155. } else {
  156. j = i + 8;
  157. }
  158. j = FFMIN(j, s->frame_len);
  159. width = get_bits(gb, 4);
  160. if (width == 0) {
  161. memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
  162. i = j;
  163. while (s->bands[k] < i)
  164. q = quant[k++];
  165. } else {
  166. while (i < j) {
  167. if (s->bands[k] == i)
  168. q = quant[k++];
  169. coeff = get_bits(gb, width);
  170. if (coeff) {
  171. if (get_bits1(gb))
  172. coeffs[i] = -q * coeff;
  173. else
  174. coeffs[i] = q * coeff;
  175. } else {
  176. coeffs[i] = 0.0f;
  177. }
  178. i++;
  179. }
  180. }
  181. }
  182. if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
  183. coeffs[0] /= 0.5;
  184. ff_dct_calc (&s->trans.dct, coeffs);
  185. s->dsp.vector_fmul_scalar(coeffs, coeffs, s->frame_len / 2, s->frame_len);
  186. }
  187. else if (CONFIG_BINKAUDIO_RDFT_DECODER)
  188. ff_rdft_calc(&s->trans.rdft, coeffs);
  189. }
  190. s->fmt_conv.float_to_int16_interleave(out, (const float **)s->coeffs_ptr,
  191. s->frame_len, s->channels);
  192. if (!s->first) {
  193. int count = s->overlap_len * s->channels;
  194. int shift = av_log2(count);
  195. for (i = 0; i < count; i++) {
  196. out[i] = (s->previous[i] * (count - i) + out[i] * i) >> shift;
  197. }
  198. }
  199. memcpy(s->previous, out + s->block_size,
  200. s->overlap_len * s->channels * sizeof(*out));
  201. s->first = 0;
  202. }
  203. static av_cold int decode_end(AVCodecContext *avctx)
  204. {
  205. BinkAudioContext * s = avctx->priv_data;
  206. av_freep(&s->bands);
  207. if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
  208. ff_rdft_end(&s->trans.rdft);
  209. else if (CONFIG_BINKAUDIO_DCT_DECODER)
  210. ff_dct_end(&s->trans.dct);
  211. return 0;
  212. }
  213. static void get_bits_align32(GetBitContext *s)
  214. {
  215. int n = (-get_bits_count(s)) & 31;
  216. if (n) skip_bits(s, n);
  217. }
  218. static int decode_frame(AVCodecContext *avctx,
  219. void *data, int *data_size,
  220. AVPacket *avpkt)
  221. {
  222. BinkAudioContext *s = avctx->priv_data;
  223. const uint8_t *buf = avpkt->data;
  224. int buf_size = avpkt->size;
  225. short *samples = data;
  226. short *samples_end = (short*)((uint8_t*)data + *data_size);
  227. int reported_size;
  228. GetBitContext *gb = &s->gb;
  229. init_get_bits(gb, buf, buf_size * 8);
  230. reported_size = get_bits_long(gb, 32);
  231. while (get_bits_count(gb) / 8 < buf_size &&
  232. samples + s->block_size <= samples_end) {
  233. decode_block(s, samples, avctx->codec->id == CODEC_ID_BINKAUDIO_DCT);
  234. samples += s->block_size;
  235. get_bits_align32(gb);
  236. }
  237. *data_size = FFMIN(reported_size, (uint8_t*)samples - (uint8_t*)data);
  238. return buf_size;
  239. }
  240. AVCodec ff_binkaudio_rdft_decoder = {
  241. "binkaudio_rdft",
  242. AVMEDIA_TYPE_AUDIO,
  243. CODEC_ID_BINKAUDIO_RDFT,
  244. sizeof(BinkAudioContext),
  245. decode_init,
  246. NULL,
  247. decode_end,
  248. decode_frame,
  249. .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)")
  250. };
  251. AVCodec ff_binkaudio_dct_decoder = {
  252. "binkaudio_dct",
  253. AVMEDIA_TYPE_AUDIO,
  254. CODEC_ID_BINKAUDIO_DCT,
  255. sizeof(BinkAudioContext),
  256. decode_init,
  257. NULL,
  258. decode_end,
  259. decode_frame,
  260. .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)")
  261. };