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.

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