binkaudio.c 9.3 KB

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