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.

384 lines
12KB

  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 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.h"
  37. extern const uint16_t ff_wma_critical_freqs[25];
  38. static float quant_table[96];
  39. #define MAX_CHANNELS 2
  40. #define BINK_BLOCK_MAX_SIZE (MAX_CHANNELS << 11)
  41. typedef struct {
  42. AVFrame frame;
  43. GetBitContext gb;
  44. DSPContext dsp;
  45. FmtConvertContext fmt_conv;
  46. int version_b; ///< Bink version 'b'
  47. int first;
  48. int channels;
  49. int frame_len; ///< transform size (samples)
  50. int overlap_len; ///< overlap size (samples)
  51. int block_size;
  52. int num_bands;
  53. unsigned int *bands;
  54. float root;
  55. DECLARE_ALIGNED(32, FFTSample, coeffs)[BINK_BLOCK_MAX_SIZE];
  56. DECLARE_ALIGNED(16, int16_t, previous)[BINK_BLOCK_MAX_SIZE / 16]; ///< coeffs from previous audio block
  57. DECLARE_ALIGNED(16, int16_t, current)[BINK_BLOCK_MAX_SIZE / 16];
  58. float *coeffs_ptr[MAX_CHANNELS]; ///< pointers to the coeffs arrays for float_to_int16_interleave
  59. float *prev_ptr[MAX_CHANNELS]; ///< pointers to the overlap points in the coeffs array
  60. uint8_t *packet_buffer;
  61. union {
  62. RDFTContext rdft;
  63. DCTContext dct;
  64. } trans;
  65. } BinkAudioContext;
  66. static av_cold int decode_init(AVCodecContext *avctx)
  67. {
  68. BinkAudioContext *s = avctx->priv_data;
  69. int sample_rate = avctx->sample_rate;
  70. int sample_rate_half;
  71. int i;
  72. int frame_len_bits;
  73. ff_dsputil_init(&s->dsp, avctx);
  74. ff_fmt_convert_init(&s->fmt_conv, avctx);
  75. /* determine frame length */
  76. if (avctx->sample_rate < 22050) {
  77. frame_len_bits = 9;
  78. } else if (avctx->sample_rate < 44100) {
  79. frame_len_bits = 10;
  80. } else {
  81. frame_len_bits = 11;
  82. }
  83. if (avctx->channels < 1 || avctx->channels > MAX_CHANNELS) {
  84. av_log(avctx, AV_LOG_ERROR, "invalid number of channels: %d\n", avctx->channels);
  85. return AVERROR_INVALIDDATA;
  86. }
  87. s->version_b = avctx->extradata_size >= 4 && avctx->extradata[3] == 'b';
  88. if (avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT) {
  89. // audio is already interleaved for the RDFT format variant
  90. sample_rate *= avctx->channels;
  91. s->channels = 1;
  92. if (!s->version_b)
  93. frame_len_bits += av_log2(avctx->channels);
  94. } else {
  95. s->channels = avctx->channels;
  96. }
  97. s->frame_len = 1 << frame_len_bits;
  98. s->overlap_len = s->frame_len / 16;
  99. s->block_size = (s->frame_len - s->overlap_len) * s->channels;
  100. sample_rate_half = (sample_rate + 1) / 2;
  101. s->root = 2.0 / sqrt(s->frame_len);
  102. for (i = 0; i < 96; i++) {
  103. /* constant is result of 0.066399999/log10(M_E) */
  104. quant_table[i] = expf(i * 0.15289164787221953823f) * s->root;
  105. }
  106. /* calculate number of bands */
  107. for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
  108. if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1])
  109. break;
  110. s->bands = av_malloc((s->num_bands + 1) * sizeof(*s->bands));
  111. if (!s->bands)
  112. return AVERROR(ENOMEM);
  113. /* populate bands data */
  114. s->bands[0] = 2;
  115. for (i = 1; i < s->num_bands; i++)
  116. s->bands[i] = (ff_wma_critical_freqs[i - 1] * s->frame_len / sample_rate_half) & ~1;
  117. s->bands[s->num_bands] = s->frame_len;
  118. s->first = 1;
  119. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  120. for (i = 0; i < s->channels; i++) {
  121. s->coeffs_ptr[i] = s->coeffs + i * s->frame_len;
  122. s->prev_ptr[i] = s->coeffs_ptr[i] + s->frame_len - s->overlap_len;
  123. }
  124. if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
  125. ff_rdft_init(&s->trans.rdft, frame_len_bits, DFT_C2R);
  126. else if (CONFIG_BINKAUDIO_DCT_DECODER)
  127. ff_dct_init(&s->trans.dct, frame_len_bits, DCT_III);
  128. else
  129. return -1;
  130. avcodec_get_frame_defaults(&s->frame);
  131. avctx->coded_frame = &s->frame;
  132. return 0;
  133. }
  134. static float get_float(GetBitContext *gb)
  135. {
  136. int power = get_bits(gb, 5);
  137. float f = ldexpf(get_bits_long(gb, 23), power - 23);
  138. if (get_bits1(gb))
  139. f = -f;
  140. return f;
  141. }
  142. static const uint8_t rle_length_tab[16] = {
  143. 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
  144. };
  145. #define GET_BITS_SAFE(out, nbits) do { \
  146. if (get_bits_left(gb) < nbits) \
  147. return AVERROR_INVALIDDATA; \
  148. out = get_bits(gb, nbits); \
  149. } while (0)
  150. /**
  151. * Decode Bink Audio block
  152. * @param[out] out Output buffer (must contain s->block_size elements)
  153. * @return 0 on success, negative error code on failure
  154. */
  155. static int decode_block(BinkAudioContext *s, int16_t *out, int use_dct)
  156. {
  157. int ch, i, j, k;
  158. float q, quant[25];
  159. int width, coeff;
  160. GetBitContext *gb = &s->gb;
  161. if (use_dct)
  162. skip_bits(gb, 2);
  163. for (ch = 0; ch < s->channels; ch++) {
  164. FFTSample *coeffs = s->coeffs_ptr[ch];
  165. if (s->version_b) {
  166. if (get_bits_left(gb) < 64)
  167. return AVERROR_INVALIDDATA;
  168. coeffs[0] = av_int2float(get_bits_long(gb, 32)) * s->root;
  169. coeffs[1] = av_int2float(get_bits_long(gb, 32)) * s->root;
  170. } else {
  171. if (get_bits_left(gb) < 58)
  172. return AVERROR_INVALIDDATA;
  173. coeffs[0] = get_float(gb) * s->root;
  174. coeffs[1] = get_float(gb) * s->root;
  175. }
  176. if (get_bits_left(gb) < s->num_bands * 8)
  177. return AVERROR_INVALIDDATA;
  178. for (i = 0; i < s->num_bands; i++) {
  179. int value = get_bits(gb, 8);
  180. quant[i] = quant_table[FFMIN(value, 95)];
  181. }
  182. k = 0;
  183. q = quant[0];
  184. // parse coefficients
  185. i = 2;
  186. while (i < s->frame_len) {
  187. if (s->version_b) {
  188. j = i + 16;
  189. } else {
  190. int v;
  191. GET_BITS_SAFE(v, 1);
  192. if (v) {
  193. GET_BITS_SAFE(v, 4);
  194. j = i + rle_length_tab[v] * 8;
  195. } else {
  196. j = i + 8;
  197. }
  198. }
  199. j = FFMIN(j, s->frame_len);
  200. GET_BITS_SAFE(width, 4);
  201. if (width == 0) {
  202. memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
  203. i = j;
  204. while (s->bands[k] < i)
  205. q = quant[k++];
  206. } else {
  207. while (i < j) {
  208. if (s->bands[k] == i)
  209. q = quant[k++];
  210. GET_BITS_SAFE(coeff, width);
  211. if (coeff) {
  212. int v;
  213. GET_BITS_SAFE(v, 1);
  214. if (v)
  215. coeffs[i] = -q * coeff;
  216. else
  217. coeffs[i] = q * coeff;
  218. } else {
  219. coeffs[i] = 0.0f;
  220. }
  221. i++;
  222. }
  223. }
  224. }
  225. if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
  226. coeffs[0] /= 0.5;
  227. s->trans.dct.dct_calc(&s->trans.dct, coeffs);
  228. s->dsp.vector_fmul_scalar(coeffs, coeffs, s->frame_len / 2, s->frame_len);
  229. }
  230. else if (CONFIG_BINKAUDIO_RDFT_DECODER)
  231. s->trans.rdft.rdft_calc(&s->trans.rdft, coeffs);
  232. }
  233. s->fmt_conv.float_to_int16_interleave(s->current,
  234. (const float **)s->prev_ptr,
  235. s->overlap_len, s->channels);
  236. s->fmt_conv.float_to_int16_interleave(out, (const float **)s->coeffs_ptr,
  237. s->frame_len - s->overlap_len,
  238. s->channels);
  239. if (!s->first) {
  240. int count = s->overlap_len * s->channels;
  241. int shift = av_log2(count);
  242. for (i = 0; i < count; i++) {
  243. out[i] = (s->previous[i] * (count - i) + out[i] * i) >> shift;
  244. }
  245. }
  246. memcpy(s->previous, s->current,
  247. s->overlap_len * s->channels * sizeof(*s->previous));
  248. s->first = 0;
  249. return 0;
  250. }
  251. static av_cold int decode_end(AVCodecContext *avctx)
  252. {
  253. BinkAudioContext * s = avctx->priv_data;
  254. av_freep(&s->bands);
  255. av_freep(&s->packet_buffer);
  256. if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
  257. ff_rdft_end(&s->trans.rdft);
  258. else if (CONFIG_BINKAUDIO_DCT_DECODER)
  259. ff_dct_end(&s->trans.dct);
  260. return 0;
  261. }
  262. static void get_bits_align32(GetBitContext *s)
  263. {
  264. int n = (-get_bits_count(s)) & 31;
  265. if (n) skip_bits(s, n);
  266. }
  267. static int decode_frame(AVCodecContext *avctx, void *data,
  268. int *got_frame_ptr, AVPacket *avpkt)
  269. {
  270. BinkAudioContext *s = avctx->priv_data;
  271. int16_t *samples;
  272. GetBitContext *gb = &s->gb;
  273. int ret, consumed = 0;
  274. if (!get_bits_left(gb)) {
  275. uint8_t *buf;
  276. /* handle end-of-stream */
  277. if (!avpkt->size) {
  278. *got_frame_ptr = 0;
  279. return 0;
  280. }
  281. if (avpkt->size < 4) {
  282. av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
  283. return AVERROR_INVALIDDATA;
  284. }
  285. buf = av_realloc(s->packet_buffer, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  286. if (!buf)
  287. return AVERROR(ENOMEM);
  288. s->packet_buffer = buf;
  289. memcpy(s->packet_buffer, avpkt->data, avpkt->size);
  290. init_get_bits(gb, s->packet_buffer, avpkt->size * 8);
  291. consumed = avpkt->size;
  292. /* skip reported size */
  293. skip_bits_long(gb, 32);
  294. }
  295. /* get output buffer */
  296. s->frame.nb_samples = s->block_size / avctx->channels;
  297. if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
  298. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  299. return ret;
  300. }
  301. samples = (int16_t *)s->frame.data[0];
  302. if (decode_block(s, samples, avctx->codec->id == AV_CODEC_ID_BINKAUDIO_DCT)) {
  303. av_log(avctx, AV_LOG_ERROR, "Incomplete packet\n");
  304. return AVERROR_INVALIDDATA;
  305. }
  306. get_bits_align32(gb);
  307. *got_frame_ptr = 1;
  308. *(AVFrame *)data = s->frame;
  309. return consumed;
  310. }
  311. AVCodec ff_binkaudio_rdft_decoder = {
  312. .name = "binkaudio_rdft",
  313. .type = AVMEDIA_TYPE_AUDIO,
  314. .id = AV_CODEC_ID_BINKAUDIO_RDFT,
  315. .priv_data_size = sizeof(BinkAudioContext),
  316. .init = decode_init,
  317. .close = decode_end,
  318. .decode = decode_frame,
  319. .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
  320. .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)")
  321. };
  322. AVCodec ff_binkaudio_dct_decoder = {
  323. .name = "binkaudio_dct",
  324. .type = AVMEDIA_TYPE_AUDIO,
  325. .id = AV_CODEC_ID_BINKAUDIO_DCT,
  326. .priv_data_size = sizeof(BinkAudioContext),
  327. .init = decode_init,
  328. .close = decode_end,
  329. .decode = decode_frame,
  330. .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
  331. .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)")
  332. };