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.

302 lines
8.7KB

  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 libavcodec/binkaudio.c
  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. extern const uint16_t ff_wma_critical_freqs[25];
  34. #define MAX_CHANNELS 2
  35. #define BINK_BLOCK_MAX_SIZE (MAX_CHANNELS << 11)
  36. typedef struct {
  37. AVCodecContext *avctx;
  38. GetBitContext gb;
  39. DSPContext dsp;
  40. int first;
  41. int channels;
  42. int frame_len; ///< transform size (samples)
  43. int overlap_len; ///< overlap size (samples)
  44. int block_size;
  45. int num_bands;
  46. unsigned int *bands;
  47. float root;
  48. DECLARE_ALIGNED_16(FFTSample, coeffs)[BINK_BLOCK_MAX_SIZE];
  49. DECLARE_ALIGNED_16(short, previous)[BINK_BLOCK_MAX_SIZE / 16]; ///< coeffs from previous audio block
  50. float *coeffs_ptr[MAX_CHANNELS]; ///< pointers to the coeffs arrays for float_to_int16_interleave
  51. union {
  52. RDFTContext rdft;
  53. DCTContext dct;
  54. } trans;
  55. } BinkAudioContext;
  56. static av_cold int decode_init(AVCodecContext *avctx)
  57. {
  58. BinkAudioContext *s = avctx->priv_data;
  59. int sample_rate = avctx->sample_rate;
  60. int sample_rate_half;
  61. int i;
  62. int frame_len_bits;
  63. s->avctx = avctx;
  64. dsputil_init(&s->dsp, avctx);
  65. /* determine frame length */
  66. if (avctx->sample_rate < 22050) {
  67. frame_len_bits = 9;
  68. } else if (avctx->sample_rate < 44100) {
  69. frame_len_bits = 10;
  70. } else {
  71. frame_len_bits = 11;
  72. }
  73. s->frame_len = 1 << frame_len_bits;
  74. if (s->channels > MAX_CHANNELS) {
  75. av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
  76. return -1;
  77. }
  78. if (avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT) {
  79. // audio is already interleaved for the RDFT format variant
  80. sample_rate *= avctx->channels;
  81. s->frame_len *= avctx->channels;
  82. s->channels = 1;
  83. if (avctx->channels == 2)
  84. frame_len_bits++;
  85. } else {
  86. s->channels = avctx->channels;
  87. }
  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] = 1;
  101. for (i = 1; i < s->num_bands; i++)
  102. s->bands[i] = ff_wma_critical_freqs[i - 1] * (s->frame_len / 2) / sample_rate_half;
  103. s->bands[s->num_bands] = s->frame_len / 2;
  104. s->first = 1;
  105. avctx->sample_fmt = 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, IRIDFT);
  110. else if (CONFIG_BINKAUDIO_DCT_DECODER)
  111. ff_dct_init(&s->trans.dct, frame_len_bits, 0);
  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. q = 0.0f;
  142. coeffs[0] = get_float(gb) * s->root;
  143. coeffs[1] = get_float(gb) * s->root;
  144. for (i = 0; i < s->num_bands; i++) {
  145. /* constant is result of 0.066399999/log10(M_E) */
  146. int value = get_bits(gb, 8);
  147. quant[i] = expf(FFMIN(value, 95) * 0.15289164787221953823f) * s->root;
  148. }
  149. // find band (k)
  150. for (k = 0; s->bands[k] < 1; k++) {
  151. q = quant[k];
  152. }
  153. // parse coefficients
  154. i = 2;
  155. while (i < s->frame_len) {
  156. if (get_bits1(gb)) {
  157. j = i + rle_length_tab[get_bits(gb, 4)] * 8;
  158. } else {
  159. j = i + 8;
  160. }
  161. j = FFMIN(j, s->frame_len);
  162. width = get_bits(gb, 4);
  163. if (width == 0) {
  164. memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
  165. i = j;
  166. while (s->bands[k] * 2 < i)
  167. q = quant[k++];
  168. } else {
  169. while (i < j) {
  170. if (s->bands[k] * 2 == i)
  171. q = quant[k++];
  172. coeff = get_bits(gb, width);
  173. if (coeff) {
  174. if (get_bits1(gb))
  175. coeffs[i] = -q * coeff;
  176. else
  177. coeffs[i] = q * coeff;
  178. } else {
  179. coeffs[i] = 0.0f;
  180. }
  181. i++;
  182. }
  183. }
  184. }
  185. if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct)
  186. ff_dct_calc (&s->trans.dct, coeffs);
  187. else if (CONFIG_BINKAUDIO_RDFT_DECODER)
  188. ff_rdft_calc(&s->trans.rdft, coeffs);
  189. }
  190. s->dsp.float_to_int16_interleave(out, (const float **)s->coeffs_ptr, s->frame_len, s->channels);
  191. if (!s->first) {
  192. int count = s->overlap_len * s->channels;
  193. int shift = av_log2(count);
  194. for (i = 0; i < count; i++) {
  195. out[i] = (s->previous[i] * (count - i) + out[i] * i) >> shift;
  196. }
  197. }
  198. memcpy(s->previous, out + s->block_size,
  199. s->overlap_len * s->channels * sizeof(*out));
  200. s->first = 0;
  201. }
  202. static av_cold int decode_end(AVCodecContext *avctx)
  203. {
  204. BinkAudioContext * s = avctx->priv_data;
  205. av_freep(&s->bands);
  206. if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
  207. ff_rdft_end(&s->trans.rdft);
  208. else if (CONFIG_BINKAUDIO_DCT_DECODER)
  209. ff_dct_end(&s->trans.dct);
  210. return 0;
  211. }
  212. static void get_bits_align32(GetBitContext *s)
  213. {
  214. int n = (-get_bits_count(s)) & 31;
  215. if (n) skip_bits(s, n);
  216. }
  217. static int decode_frame(AVCodecContext *avctx,
  218. void *data, int *data_size,
  219. AVPacket *avpkt)
  220. {
  221. BinkAudioContext *s = avctx->priv_data;
  222. const uint8_t *buf = avpkt->data;
  223. int buf_size = avpkt->size;
  224. short *samples = data;
  225. short *samples_end = (short*)((uint8_t*)data + *data_size);
  226. int reported_size;
  227. GetBitContext *gb = &s->gb;
  228. init_get_bits(gb, buf, buf_size * 8);
  229. reported_size = get_bits_long(gb, 32);
  230. while (get_bits_count(gb) / 8 < buf_size &&
  231. samples + s->block_size <= samples_end) {
  232. decode_block(s, samples, avctx->codec->id == CODEC_ID_BINKAUDIO_DCT);
  233. samples += s->block_size;
  234. get_bits_align32(gb);
  235. }
  236. *data_size = FFMIN(reported_size, (uint8_t*)samples - (uint8_t*)data);
  237. return buf_size;
  238. }
  239. AVCodec binkaudio_rdft_decoder = {
  240. "binkaudio_rdft",
  241. CODEC_TYPE_AUDIO,
  242. CODEC_ID_BINKAUDIO_RDFT,
  243. sizeof(BinkAudioContext),
  244. decode_init,
  245. NULL,
  246. decode_end,
  247. decode_frame,
  248. .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)")
  249. };
  250. AVCodec binkaudio_dct_decoder = {
  251. "binkaudio_dct",
  252. CODEC_TYPE_AUDIO,
  253. CODEC_ID_BINKAUDIO_DCT,
  254. sizeof(BinkAudioContext),
  255. decode_init,
  256. NULL,
  257. decode_end,
  258. decode_frame,
  259. .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)")
  260. };