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