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.

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