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.

318 lines
9.3KB

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