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.

311 lines
9.0KB

  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. AVCodecContext *avctx;
  40. GetBitContext gb;
  41. DSPContext dsp;
  42. FmtConvertContext fmt_conv;
  43. int first;
  44. int channels;
  45. int frame_len; ///< transform size (samples)
  46. int overlap_len; ///< overlap size (samples)
  47. int block_size;
  48. int num_bands;
  49. unsigned int *bands;
  50. float root;
  51. DECLARE_ALIGNED(16, FFTSample, coeffs)[BINK_BLOCK_MAX_SIZE];
  52. DECLARE_ALIGNED(16, short, previous)[BINK_BLOCK_MAX_SIZE / 16]; ///< coeffs from previous audio block
  53. float *coeffs_ptr[MAX_CHANNELS]; ///< pointers to the coeffs arrays for float_to_int16_interleave
  54. union {
  55. RDFTContext rdft;
  56. DCTContext dct;
  57. } trans;
  58. } BinkAudioContext;
  59. static av_cold int decode_init(AVCodecContext *avctx)
  60. {
  61. BinkAudioContext *s = avctx->priv_data;
  62. int sample_rate = avctx->sample_rate;
  63. int sample_rate_half;
  64. int i;
  65. int frame_len_bits;
  66. s->avctx = avctx;
  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. s->frame_len = 1 << frame_len_bits;
  78. if (s->channels > MAX_CHANNELS) {
  79. av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
  80. return -1;
  81. }
  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->frame_len *= avctx->channels;
  86. s->channels = 1;
  87. if (avctx->channels == 2)
  88. frame_len_bits++;
  89. } else {
  90. s->channels = avctx->channels;
  91. }
  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] = 1;
  105. for (i = 1; i < s->num_bands; i++)
  106. s->bands[i] = ff_wma_critical_freqs[i - 1] * (s->frame_len / 2) / sample_rate_half;
  107. s->bands[s->num_bands] = s->frame_len / 2;
  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. q = 0.0f;
  146. coeffs[0] = get_float(gb) * s->root;
  147. coeffs[1] = get_float(gb) * s->root;
  148. for (i = 0; i < s->num_bands; i++) {
  149. /* constant is result of 0.066399999/log10(M_E) */
  150. int value = get_bits(gb, 8);
  151. quant[i] = expf(FFMIN(value, 95) * 0.15289164787221953823f) * s->root;
  152. }
  153. // find band (k)
  154. for (k = 0; s->bands[k] < 1; k++) {
  155. q = quant[k];
  156. }
  157. // parse coefficients
  158. i = 2;
  159. while (i < s->frame_len) {
  160. if (get_bits1(gb)) {
  161. j = i + rle_length_tab[get_bits(gb, 4)] * 8;
  162. } else {
  163. j = i + 8;
  164. }
  165. j = FFMIN(j, s->frame_len);
  166. width = get_bits(gb, 4);
  167. if (width == 0) {
  168. memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
  169. i = j;
  170. while (s->bands[k] * 2 < i)
  171. q = quant[k++];
  172. } else {
  173. while (i < j) {
  174. if (s->bands[k] * 2 == i)
  175. q = quant[k++];
  176. coeff = get_bits(gb, width);
  177. if (coeff) {
  178. if (get_bits1(gb))
  179. coeffs[i] = -q * coeff;
  180. else
  181. coeffs[i] = q * coeff;
  182. } else {
  183. coeffs[i] = 0.0f;
  184. }
  185. i++;
  186. }
  187. }
  188. }
  189. if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
  190. coeffs[0] /= 0.5;
  191. ff_dct_calc (&s->trans.dct, coeffs);
  192. s->dsp.vector_fmul_scalar(coeffs, coeffs, s->frame_len / 2, s->frame_len);
  193. }
  194. else if (CONFIG_BINKAUDIO_RDFT_DECODER)
  195. ff_rdft_calc(&s->trans.rdft, coeffs);
  196. }
  197. s->fmt_conv.float_to_int16_interleave(out, (const float **)s->coeffs_ptr,
  198. s->frame_len, s->channels);
  199. if (!s->first) {
  200. int count = s->overlap_len * s->channels;
  201. int shift = av_log2(count);
  202. for (i = 0; i < count; i++) {
  203. out[i] = (s->previous[i] * (count - i) + out[i] * i) >> shift;
  204. }
  205. }
  206. memcpy(s->previous, out + s->block_size,
  207. s->overlap_len * s->channels * sizeof(*out));
  208. s->first = 0;
  209. }
  210. static av_cold int decode_end(AVCodecContext *avctx)
  211. {
  212. BinkAudioContext * s = avctx->priv_data;
  213. av_freep(&s->bands);
  214. if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
  215. ff_rdft_end(&s->trans.rdft);
  216. else if (CONFIG_BINKAUDIO_DCT_DECODER)
  217. ff_dct_end(&s->trans.dct);
  218. return 0;
  219. }
  220. static void get_bits_align32(GetBitContext *s)
  221. {
  222. int n = (-get_bits_count(s)) & 31;
  223. if (n) skip_bits(s, n);
  224. }
  225. static int decode_frame(AVCodecContext *avctx,
  226. void *data, int *data_size,
  227. AVPacket *avpkt)
  228. {
  229. BinkAudioContext *s = avctx->priv_data;
  230. const uint8_t *buf = avpkt->data;
  231. int buf_size = avpkt->size;
  232. short *samples = data;
  233. short *samples_end = (short*)((uint8_t*)data + *data_size);
  234. int reported_size;
  235. GetBitContext *gb = &s->gb;
  236. init_get_bits(gb, buf, buf_size * 8);
  237. reported_size = get_bits_long(gb, 32);
  238. while (get_bits_count(gb) / 8 < buf_size &&
  239. samples + s->block_size <= samples_end) {
  240. decode_block(s, samples, avctx->codec->id == CODEC_ID_BINKAUDIO_DCT);
  241. samples += s->block_size;
  242. get_bits_align32(gb);
  243. }
  244. *data_size = FFMIN(reported_size, (uint8_t*)samples - (uint8_t*)data);
  245. return buf_size;
  246. }
  247. AVCodec ff_binkaudio_rdft_decoder = {
  248. "binkaudio_rdft",
  249. AVMEDIA_TYPE_AUDIO,
  250. CODEC_ID_BINKAUDIO_RDFT,
  251. sizeof(BinkAudioContext),
  252. decode_init,
  253. NULL,
  254. decode_end,
  255. decode_frame,
  256. .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)")
  257. };
  258. AVCodec ff_binkaudio_dct_decoder = {
  259. "binkaudio_dct",
  260. AVMEDIA_TYPE_AUDIO,
  261. CODEC_ID_BINKAUDIO_DCT,
  262. sizeof(BinkAudioContext),
  263. decode_init,
  264. NULL,
  265. decode_end,
  266. decode_frame,
  267. .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)")
  268. };