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.

360 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. float root;
  51. unsigned int bands[26];
  52. float previous[MAX_CHANNELS][BINK_BLOCK_MAX_SIZE / 16]; ///< coeffs from previous audio block
  53. float quant_table[96];
  54. AVPacket *pkt;
  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 < 1 || avctx->channels > MAX_CHANNELS) {
  76. av_log(avctx, AV_LOG_ERROR, "invalid number of channels: %d\n", avctx->channels);
  77. return AVERROR_INVALIDDATA;
  78. }
  79. avctx->channel_layout = avctx->channels == 1 ? AV_CH_LAYOUT_MONO :
  80. AV_CH_LAYOUT_STEREO;
  81. s->version_b = avctx->extradata_size >= 4 && 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. if (sample_rate > INT_MAX / avctx->channels)
  86. return AVERROR_INVALIDDATA;
  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 + 1LL) / 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. s->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. /* populate bands data */
  112. s->bands[0] = 2;
  113. for (i = 1; i < s->num_bands; i++)
  114. s->bands[i] = (ff_wma_critical_freqs[i - 1] * s->frame_len / sample_rate_half) & ~1;
  115. s->bands[s->num_bands] = s->frame_len;
  116. s->first = 1;
  117. if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
  118. ff_rdft_init(&s->trans.rdft, frame_len_bits, DFT_C2R);
  119. else if (CONFIG_BINKAUDIO_DCT_DECODER)
  120. ff_dct_init(&s->trans.dct, frame_len_bits, DCT_III);
  121. else
  122. av_assert0(0);
  123. s->pkt = av_packet_alloc();
  124. if (!s->pkt)
  125. return AVERROR(ENOMEM);
  126. return 0;
  127. }
  128. static float get_float(GetBitContext *gb)
  129. {
  130. int power = get_bits(gb, 5);
  131. float f = ldexpf(get_bits(gb, 23), power - 23);
  132. if (get_bits1(gb))
  133. f = -f;
  134. return f;
  135. }
  136. static const uint8_t rle_length_tab[16] = {
  137. 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
  138. };
  139. /**
  140. * Decode Bink Audio block
  141. * @param[out] out Output buffer (must contain s->block_size elements)
  142. * @return 0 on success, negative error code on failure
  143. */
  144. static int decode_block(BinkAudioContext *s, float **out, int use_dct)
  145. {
  146. int ch, i, j, k;
  147. float q, quant[25];
  148. int width, coeff;
  149. GetBitContext *gb = &s->gb;
  150. if (use_dct)
  151. skip_bits(gb, 2);
  152. for (ch = 0; ch < s->channels; ch++) {
  153. FFTSample *coeffs = out[ch];
  154. if (s->version_b) {
  155. if (get_bits_left(gb) < 64)
  156. return AVERROR_INVALIDDATA;
  157. coeffs[0] = av_int2float(get_bits_long(gb, 32)) * s->root;
  158. coeffs[1] = av_int2float(get_bits_long(gb, 32)) * s->root;
  159. } else {
  160. if (get_bits_left(gb) < 58)
  161. return AVERROR_INVALIDDATA;
  162. coeffs[0] = get_float(gb) * s->root;
  163. coeffs[1] = get_float(gb) * s->root;
  164. }
  165. if (get_bits_left(gb) < s->num_bands * 8)
  166. return AVERROR_INVALIDDATA;
  167. for (i = 0; i < s->num_bands; i++) {
  168. int value = get_bits(gb, 8);
  169. quant[i] = s->quant_table[FFMIN(value, 95)];
  170. }
  171. k = 0;
  172. q = quant[0];
  173. // parse coefficients
  174. i = 2;
  175. while (i < s->frame_len) {
  176. if (s->version_b) {
  177. j = i + 16;
  178. } else {
  179. int v = get_bits1(gb);
  180. if (v) {
  181. v = get_bits(gb, 4);
  182. j = i + rle_length_tab[v] * 8;
  183. } else {
  184. j = i + 8;
  185. }
  186. }
  187. j = FFMIN(j, s->frame_len);
  188. width = get_bits(gb, 4);
  189. if (width == 0) {
  190. memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
  191. i = j;
  192. while (s->bands[k] < i)
  193. q = quant[k++];
  194. } else {
  195. while (i < j) {
  196. if (s->bands[k] == i)
  197. q = quant[k++];
  198. coeff = get_bits(gb, width);
  199. if (coeff) {
  200. int v;
  201. v = get_bits1(gb);
  202. if (v)
  203. coeffs[i] = -q * coeff;
  204. else
  205. coeffs[i] = q * coeff;
  206. } else {
  207. coeffs[i] = 0.0f;
  208. }
  209. i++;
  210. }
  211. }
  212. }
  213. if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
  214. coeffs[0] /= 0.5;
  215. s->trans.dct.dct_calc(&s->trans.dct, coeffs);
  216. }
  217. else if (CONFIG_BINKAUDIO_RDFT_DECODER)
  218. s->trans.rdft.rdft_calc(&s->trans.rdft, coeffs);
  219. }
  220. for (ch = 0; ch < s->channels; ch++) {
  221. int j;
  222. int count = s->overlap_len * s->channels;
  223. if (!s->first) {
  224. j = ch;
  225. for (i = 0; i < s->overlap_len; i++, j += s->channels)
  226. out[ch][i] = (s->previous[ch][i] * (count - j) +
  227. out[ch][i] * j) / count;
  228. }
  229. memcpy(s->previous[ch], &out[ch][s->frame_len - s->overlap_len],
  230. s->overlap_len * sizeof(*s->previous[ch]));
  231. }
  232. s->first = 0;
  233. return 0;
  234. }
  235. static av_cold int decode_end(AVCodecContext *avctx)
  236. {
  237. BinkAudioContext * s = avctx->priv_data;
  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. av_packet_free(&s->pkt);
  243. return 0;
  244. }
  245. static void get_bits_align32(GetBitContext *s)
  246. {
  247. int n = (-get_bits_count(s)) & 31;
  248. if (n) skip_bits(s, n);
  249. }
  250. static int binkaudio_receive_frame(AVCodecContext *avctx, AVFrame *frame)
  251. {
  252. BinkAudioContext *s = avctx->priv_data;
  253. GetBitContext *gb = &s->gb;
  254. int ret;
  255. if (!s->pkt->data) {
  256. ret = ff_decode_get_packet(avctx, s->pkt);
  257. if (ret < 0)
  258. return ret;
  259. if (s->pkt->size < 4) {
  260. av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
  261. ret = AVERROR_INVALIDDATA;
  262. goto fail;
  263. }
  264. ret = init_get_bits8(gb, s->pkt->data, s->pkt->size);
  265. if (ret < 0)
  266. goto fail;
  267. /* skip reported size */
  268. skip_bits_long(gb, 32);
  269. }
  270. /* get output buffer */
  271. frame->nb_samples = s->frame_len;
  272. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  273. return ret;
  274. if (decode_block(s, (float **)frame->extended_data,
  275. avctx->codec->id == AV_CODEC_ID_BINKAUDIO_DCT)) {
  276. av_log(avctx, AV_LOG_ERROR, "Incomplete packet\n");
  277. return AVERROR_INVALIDDATA;
  278. }
  279. get_bits_align32(gb);
  280. if (!get_bits_left(gb)) {
  281. memset(gb, 0, sizeof(*gb));
  282. av_packet_unref(s->pkt);
  283. }
  284. frame->nb_samples = s->block_size / avctx->channels;
  285. return 0;
  286. fail:
  287. av_packet_unref(s->pkt);
  288. return ret;
  289. }
  290. AVCodec ff_binkaudio_rdft_decoder = {
  291. .name = "binkaudio_rdft",
  292. .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)"),
  293. .type = AVMEDIA_TYPE_AUDIO,
  294. .id = AV_CODEC_ID_BINKAUDIO_RDFT,
  295. .priv_data_size = sizeof(BinkAudioContext),
  296. .init = decode_init,
  297. .close = decode_end,
  298. .receive_frame = binkaudio_receive_frame,
  299. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
  300. };
  301. AVCodec ff_binkaudio_dct_decoder = {
  302. .name = "binkaudio_dct",
  303. .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)"),
  304. .type = AVMEDIA_TYPE_AUDIO,
  305. .id = AV_CODEC_ID_BINKAUDIO_DCT,
  306. .priv_data_size = sizeof(BinkAudioContext),
  307. .init = decode_init,
  308. .close = decode_end,
  309. .receive_frame = binkaudio_receive_frame,
  310. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
  311. };