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.

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