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.6KB

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