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.

316 lines
9.2KB

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