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.

366 lines
11KB

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