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.

983 lines
33KB

  1. /*
  2. * WMA compatible decoder
  3. * Copyright (c) 2002 The FFmpeg Project
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * WMA compatible decoder.
  24. * This decoder handles Microsoft Windows Media Audio data, versions 1 & 2.
  25. * WMA v1 is identified by audio format 0x160 in Microsoft media files
  26. * (ASF/AVI/WAV). WMA v2 is identified by audio format 0x161.
  27. *
  28. * To use this decoder, a calling application must supply the extra data
  29. * bytes provided with the WMA data. These are the extra, codec-specific
  30. * bytes at the end of a WAVEFORMATEX data structure. Transmit these bytes
  31. * to the decoder using the extradata[_size] fields in AVCodecContext. There
  32. * should be 4 extra bytes for v1 data and 6 extra bytes for v2 data.
  33. */
  34. #include "libavutil/attributes.h"
  35. #include "avcodec.h"
  36. #include "internal.h"
  37. #include "wma.h"
  38. #undef NDEBUG
  39. #include <assert.h>
  40. #define EXPVLCBITS 8
  41. #define EXPMAX ((19+EXPVLCBITS-1)/EXPVLCBITS)
  42. #define HGAINVLCBITS 9
  43. #define HGAINMAX ((13+HGAINVLCBITS-1)/HGAINVLCBITS)
  44. static void wma_lsp_to_curve_init(WMACodecContext *s, int frame_len);
  45. #ifdef TRACE
  46. static void dump_floats(WMACodecContext *s, const char *name, int prec, const float *tab, int n)
  47. {
  48. int i;
  49. tprintf(s->avctx, "%s[%d]:\n", name, n);
  50. for(i=0;i<n;i++) {
  51. if ((i & 7) == 0)
  52. tprintf(s->avctx, "%4d: ", i);
  53. tprintf(s->avctx, " %8.*f", prec, tab[i]);
  54. if ((i & 7) == 7)
  55. tprintf(s->avctx, "\n");
  56. }
  57. if ((i & 7) != 0)
  58. tprintf(s->avctx, "\n");
  59. }
  60. #endif
  61. static av_cold int wma_decode_init(AVCodecContext * avctx)
  62. {
  63. WMACodecContext *s = avctx->priv_data;
  64. int i, flags2;
  65. uint8_t *extradata;
  66. if (!avctx->block_align) {
  67. av_log(avctx, AV_LOG_ERROR, "block_align is not set\n");
  68. return AVERROR(EINVAL);
  69. }
  70. s->avctx = avctx;
  71. /* extract flag infos */
  72. flags2 = 0;
  73. extradata = avctx->extradata;
  74. if (avctx->codec->id == AV_CODEC_ID_WMAV1 && avctx->extradata_size >= 4) {
  75. flags2 = AV_RL16(extradata+2);
  76. } else if (avctx->codec->id == AV_CODEC_ID_WMAV2 && avctx->extradata_size >= 6) {
  77. flags2 = AV_RL16(extradata+4);
  78. }
  79. s->use_exp_vlc = flags2 & 0x0001;
  80. s->use_bit_reservoir = flags2 & 0x0002;
  81. s->use_variable_block_len = flags2 & 0x0004;
  82. if(avctx->codec->id == AV_CODEC_ID_WMAV2 && avctx->extradata_size >= 8){
  83. if(AV_RL16(extradata+4)==0xd && s->use_variable_block_len){
  84. av_log(avctx, AV_LOG_WARNING, "Disabling use_variable_block_len, if this fails contact the ffmpeg developers and send us the file\n");
  85. s->use_variable_block_len= 0; // this fixes issue1503
  86. }
  87. }
  88. for (i=0; i<MAX_CHANNELS; i++)
  89. s->max_exponent[i] = 1.0;
  90. if(ff_wma_init(avctx, flags2)<0)
  91. return -1;
  92. /* init MDCT */
  93. for(i = 0; i < s->nb_block_sizes; i++)
  94. ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 1, 1.0 / 32768.0);
  95. if (s->use_noise_coding) {
  96. init_vlc(&s->hgain_vlc, HGAINVLCBITS, sizeof(ff_wma_hgain_huffbits),
  97. ff_wma_hgain_huffbits, 1, 1,
  98. ff_wma_hgain_huffcodes, 2, 2, 0);
  99. }
  100. if (s->use_exp_vlc) {
  101. init_vlc(&s->exp_vlc, EXPVLCBITS, sizeof(ff_aac_scalefactor_bits), //FIXME move out of context
  102. ff_aac_scalefactor_bits, 1, 1,
  103. ff_aac_scalefactor_code, 4, 4, 0);
  104. } else {
  105. wma_lsp_to_curve_init(s, s->frame_len);
  106. }
  107. avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
  108. return 0;
  109. }
  110. /**
  111. * compute x^-0.25 with an exponent and mantissa table. We use linear
  112. * interpolation to reduce the mantissa table size at a small speed
  113. * expense (linear interpolation approximately doubles the number of
  114. * bits of precision).
  115. */
  116. static inline float pow_m1_4(WMACodecContext *s, float x)
  117. {
  118. union {
  119. float f;
  120. unsigned int v;
  121. } u, t;
  122. unsigned int e, m;
  123. float a, b;
  124. u.f = x;
  125. e = u.v >> 23;
  126. m = (u.v >> (23 - LSP_POW_BITS)) & ((1 << LSP_POW_BITS) - 1);
  127. /* build interpolation scale: 1 <= t < 2. */
  128. t.v = ((u.v << LSP_POW_BITS) & ((1 << 23) - 1)) | (127 << 23);
  129. a = s->lsp_pow_m_table1[m];
  130. b = s->lsp_pow_m_table2[m];
  131. return s->lsp_pow_e_table[e] * (a + b * t.f);
  132. }
  133. static av_cold void wma_lsp_to_curve_init(WMACodecContext *s, int frame_len)
  134. {
  135. float wdel, a, b;
  136. int i, e, m;
  137. wdel = M_PI / frame_len;
  138. for(i=0;i<frame_len;i++)
  139. s->lsp_cos_table[i] = 2.0f * cos(wdel * i);
  140. /* tables for x^-0.25 computation */
  141. for(i=0;i<256;i++) {
  142. e = i - 126;
  143. s->lsp_pow_e_table[i] = pow(2.0, e * -0.25);
  144. }
  145. /* NOTE: these two tables are needed to avoid two operations in
  146. pow_m1_4 */
  147. b = 1.0;
  148. for(i=(1 << LSP_POW_BITS) - 1;i>=0;i--) {
  149. m = (1 << LSP_POW_BITS) + i;
  150. a = (float)m * (0.5 / (1 << LSP_POW_BITS));
  151. a = pow(a, -0.25);
  152. s->lsp_pow_m_table1[i] = 2 * a - b;
  153. s->lsp_pow_m_table2[i] = b - a;
  154. b = a;
  155. }
  156. }
  157. /**
  158. * NOTE: We use the same code as Vorbis here
  159. * @todo optimize it further with SSE/3Dnow
  160. */
  161. static void wma_lsp_to_curve(WMACodecContext *s,
  162. float *out, float *val_max_ptr,
  163. int n, float *lsp)
  164. {
  165. int i, j;
  166. float p, q, w, v, val_max;
  167. val_max = 0;
  168. for(i=0;i<n;i++) {
  169. p = 0.5f;
  170. q = 0.5f;
  171. w = s->lsp_cos_table[i];
  172. for(j=1;j<NB_LSP_COEFS;j+=2){
  173. q *= w - lsp[j - 1];
  174. p *= w - lsp[j];
  175. }
  176. p *= p * (2.0f - w);
  177. q *= q * (2.0f + w);
  178. v = p + q;
  179. v = pow_m1_4(s, v);
  180. if (v > val_max)
  181. val_max = v;
  182. out[i] = v;
  183. }
  184. *val_max_ptr = val_max;
  185. }
  186. /**
  187. * decode exponents coded with LSP coefficients (same idea as Vorbis)
  188. */
  189. static void decode_exp_lsp(WMACodecContext *s, int ch)
  190. {
  191. float lsp_coefs[NB_LSP_COEFS];
  192. int val, i;
  193. for(i = 0; i < NB_LSP_COEFS; i++) {
  194. if (i == 0 || i >= 8)
  195. val = get_bits(&s->gb, 3);
  196. else
  197. val = get_bits(&s->gb, 4);
  198. lsp_coefs[i] = ff_wma_lsp_codebook[i][val];
  199. }
  200. wma_lsp_to_curve(s, s->exponents[ch], &s->max_exponent[ch],
  201. s->block_len, lsp_coefs);
  202. }
  203. /** pow(10, i / 16.0) for i in -60..95 */
  204. static const float pow_tab[] = {
  205. 1.7782794100389e-04, 2.0535250264571e-04,
  206. 2.3713737056617e-04, 2.7384196342644e-04,
  207. 3.1622776601684e-04, 3.6517412725484e-04,
  208. 4.2169650342858e-04, 4.8696752516586e-04,
  209. 5.6234132519035e-04, 6.4938163157621e-04,
  210. 7.4989420933246e-04, 8.6596432336006e-04,
  211. 1.0000000000000e-03, 1.1547819846895e-03,
  212. 1.3335214321633e-03, 1.5399265260595e-03,
  213. 1.7782794100389e-03, 2.0535250264571e-03,
  214. 2.3713737056617e-03, 2.7384196342644e-03,
  215. 3.1622776601684e-03, 3.6517412725484e-03,
  216. 4.2169650342858e-03, 4.8696752516586e-03,
  217. 5.6234132519035e-03, 6.4938163157621e-03,
  218. 7.4989420933246e-03, 8.6596432336006e-03,
  219. 1.0000000000000e-02, 1.1547819846895e-02,
  220. 1.3335214321633e-02, 1.5399265260595e-02,
  221. 1.7782794100389e-02, 2.0535250264571e-02,
  222. 2.3713737056617e-02, 2.7384196342644e-02,
  223. 3.1622776601684e-02, 3.6517412725484e-02,
  224. 4.2169650342858e-02, 4.8696752516586e-02,
  225. 5.6234132519035e-02, 6.4938163157621e-02,
  226. 7.4989420933246e-02, 8.6596432336007e-02,
  227. 1.0000000000000e-01, 1.1547819846895e-01,
  228. 1.3335214321633e-01, 1.5399265260595e-01,
  229. 1.7782794100389e-01, 2.0535250264571e-01,
  230. 2.3713737056617e-01, 2.7384196342644e-01,
  231. 3.1622776601684e-01, 3.6517412725484e-01,
  232. 4.2169650342858e-01, 4.8696752516586e-01,
  233. 5.6234132519035e-01, 6.4938163157621e-01,
  234. 7.4989420933246e-01, 8.6596432336007e-01,
  235. 1.0000000000000e+00, 1.1547819846895e+00,
  236. 1.3335214321633e+00, 1.5399265260595e+00,
  237. 1.7782794100389e+00, 2.0535250264571e+00,
  238. 2.3713737056617e+00, 2.7384196342644e+00,
  239. 3.1622776601684e+00, 3.6517412725484e+00,
  240. 4.2169650342858e+00, 4.8696752516586e+00,
  241. 5.6234132519035e+00, 6.4938163157621e+00,
  242. 7.4989420933246e+00, 8.6596432336007e+00,
  243. 1.0000000000000e+01, 1.1547819846895e+01,
  244. 1.3335214321633e+01, 1.5399265260595e+01,
  245. 1.7782794100389e+01, 2.0535250264571e+01,
  246. 2.3713737056617e+01, 2.7384196342644e+01,
  247. 3.1622776601684e+01, 3.6517412725484e+01,
  248. 4.2169650342858e+01, 4.8696752516586e+01,
  249. 5.6234132519035e+01, 6.4938163157621e+01,
  250. 7.4989420933246e+01, 8.6596432336007e+01,
  251. 1.0000000000000e+02, 1.1547819846895e+02,
  252. 1.3335214321633e+02, 1.5399265260595e+02,
  253. 1.7782794100389e+02, 2.0535250264571e+02,
  254. 2.3713737056617e+02, 2.7384196342644e+02,
  255. 3.1622776601684e+02, 3.6517412725484e+02,
  256. 4.2169650342858e+02, 4.8696752516586e+02,
  257. 5.6234132519035e+02, 6.4938163157621e+02,
  258. 7.4989420933246e+02, 8.6596432336007e+02,
  259. 1.0000000000000e+03, 1.1547819846895e+03,
  260. 1.3335214321633e+03, 1.5399265260595e+03,
  261. 1.7782794100389e+03, 2.0535250264571e+03,
  262. 2.3713737056617e+03, 2.7384196342644e+03,
  263. 3.1622776601684e+03, 3.6517412725484e+03,
  264. 4.2169650342858e+03, 4.8696752516586e+03,
  265. 5.6234132519035e+03, 6.4938163157621e+03,
  266. 7.4989420933246e+03, 8.6596432336007e+03,
  267. 1.0000000000000e+04, 1.1547819846895e+04,
  268. 1.3335214321633e+04, 1.5399265260595e+04,
  269. 1.7782794100389e+04, 2.0535250264571e+04,
  270. 2.3713737056617e+04, 2.7384196342644e+04,
  271. 3.1622776601684e+04, 3.6517412725484e+04,
  272. 4.2169650342858e+04, 4.8696752516586e+04,
  273. 5.6234132519035e+04, 6.4938163157621e+04,
  274. 7.4989420933246e+04, 8.6596432336007e+04,
  275. 1.0000000000000e+05, 1.1547819846895e+05,
  276. 1.3335214321633e+05, 1.5399265260595e+05,
  277. 1.7782794100389e+05, 2.0535250264571e+05,
  278. 2.3713737056617e+05, 2.7384196342644e+05,
  279. 3.1622776601684e+05, 3.6517412725484e+05,
  280. 4.2169650342858e+05, 4.8696752516586e+05,
  281. 5.6234132519035e+05, 6.4938163157621e+05,
  282. 7.4989420933246e+05, 8.6596432336007e+05,
  283. };
  284. /**
  285. * decode exponents coded with VLC codes
  286. */
  287. static int decode_exp_vlc(WMACodecContext *s, int ch)
  288. {
  289. int last_exp, n, code;
  290. const uint16_t *ptr;
  291. float v, max_scale;
  292. uint32_t *q, *q_end, iv;
  293. const float *ptab = pow_tab + 60;
  294. const uint32_t *iptab = (const uint32_t*)ptab;
  295. ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
  296. q = (uint32_t *)s->exponents[ch];
  297. q_end = q + s->block_len;
  298. max_scale = 0;
  299. if (s->version == 1) {
  300. last_exp = get_bits(&s->gb, 5) + 10;
  301. v = ptab[last_exp];
  302. iv = iptab[last_exp];
  303. max_scale = v;
  304. n = *ptr++;
  305. switch (n & 3) do {
  306. case 0: *q++ = iv;
  307. case 3: *q++ = iv;
  308. case 2: *q++ = iv;
  309. case 1: *q++ = iv;
  310. } while ((n -= 4) > 0);
  311. }else
  312. last_exp = 36;
  313. while (q < q_end) {
  314. code = get_vlc2(&s->gb, s->exp_vlc.table, EXPVLCBITS, EXPMAX);
  315. if (code < 0){
  316. av_log(s->avctx, AV_LOG_ERROR, "Exponent vlc invalid\n");
  317. return -1;
  318. }
  319. /* NOTE: this offset is the same as MPEG4 AAC ! */
  320. last_exp += code - 60;
  321. if ((unsigned)last_exp + 60 >= FF_ARRAY_ELEMS(pow_tab)) {
  322. av_log(s->avctx, AV_LOG_ERROR, "Exponent out of range: %d\n",
  323. last_exp);
  324. return -1;
  325. }
  326. v = ptab[last_exp];
  327. iv = iptab[last_exp];
  328. if (v > max_scale)
  329. max_scale = v;
  330. n = *ptr++;
  331. switch (n & 3) do {
  332. case 0: *q++ = iv;
  333. case 3: *q++ = iv;
  334. case 2: *q++ = iv;
  335. case 1: *q++ = iv;
  336. } while ((n -= 4) > 0);
  337. }
  338. s->max_exponent[ch] = max_scale;
  339. return 0;
  340. }
  341. /**
  342. * Apply MDCT window and add into output.
  343. *
  344. * We ensure that when the windows overlap their squared sum
  345. * is always 1 (MDCT reconstruction rule).
  346. */
  347. static void wma_window(WMACodecContext *s, float *out)
  348. {
  349. float *in = s->output;
  350. int block_len, bsize, n;
  351. /* left part */
  352. if (s->block_len_bits <= s->prev_block_len_bits) {
  353. block_len = s->block_len;
  354. bsize = s->frame_len_bits - s->block_len_bits;
  355. s->fdsp.vector_fmul_add(out, in, s->windows[bsize],
  356. out, block_len);
  357. } else {
  358. block_len = 1 << s->prev_block_len_bits;
  359. n = (s->block_len - block_len) / 2;
  360. bsize = s->frame_len_bits - s->prev_block_len_bits;
  361. s->fdsp.vector_fmul_add(out+n, in+n, s->windows[bsize],
  362. out+n, block_len);
  363. memcpy(out+n+block_len, in+n+block_len, n*sizeof(float));
  364. }
  365. out += s->block_len;
  366. in += s->block_len;
  367. /* right part */
  368. if (s->block_len_bits <= s->next_block_len_bits) {
  369. block_len = s->block_len;
  370. bsize = s->frame_len_bits - s->block_len_bits;
  371. s->fdsp.vector_fmul_reverse(out, in, s->windows[bsize], block_len);
  372. } else {
  373. block_len = 1 << s->next_block_len_bits;
  374. n = (s->block_len - block_len) / 2;
  375. bsize = s->frame_len_bits - s->next_block_len_bits;
  376. memcpy(out, in, n*sizeof(float));
  377. s->fdsp.vector_fmul_reverse(out+n, in+n, s->windows[bsize], block_len);
  378. memset(out+n+block_len, 0, n*sizeof(float));
  379. }
  380. }
  381. /**
  382. * @return 0 if OK. 1 if last block of frame. return -1 if
  383. * unrecorrable error.
  384. */
  385. static int wma_decode_block(WMACodecContext *s)
  386. {
  387. int n, v, a, ch, bsize;
  388. int coef_nb_bits, total_gain;
  389. int nb_coefs[MAX_CHANNELS];
  390. float mdct_norm;
  391. FFTContext *mdct;
  392. #ifdef TRACE
  393. tprintf(s->avctx, "***decode_block: %d:%d\n", s->frame_count - 1, s->block_num);
  394. #endif
  395. /* compute current block length */
  396. if (s->use_variable_block_len) {
  397. n = av_log2(s->nb_block_sizes - 1) + 1;
  398. if (s->reset_block_lengths) {
  399. s->reset_block_lengths = 0;
  400. v = get_bits(&s->gb, n);
  401. if (v >= s->nb_block_sizes){
  402. av_log(s->avctx, AV_LOG_ERROR, "prev_block_len_bits %d out of range\n", s->frame_len_bits - v);
  403. return -1;
  404. }
  405. s->prev_block_len_bits = s->frame_len_bits - v;
  406. v = get_bits(&s->gb, n);
  407. if (v >= s->nb_block_sizes){
  408. av_log(s->avctx, AV_LOG_ERROR, "block_len_bits %d out of range\n", s->frame_len_bits - v);
  409. return -1;
  410. }
  411. s->block_len_bits = s->frame_len_bits - v;
  412. } else {
  413. /* update block lengths */
  414. s->prev_block_len_bits = s->block_len_bits;
  415. s->block_len_bits = s->next_block_len_bits;
  416. }
  417. v = get_bits(&s->gb, n);
  418. if (v >= s->nb_block_sizes){
  419. av_log(s->avctx, AV_LOG_ERROR, "next_block_len_bits %d out of range\n", s->frame_len_bits - v);
  420. return -1;
  421. }
  422. s->next_block_len_bits = s->frame_len_bits - v;
  423. } else {
  424. /* fixed block len */
  425. s->next_block_len_bits = s->frame_len_bits;
  426. s->prev_block_len_bits = s->frame_len_bits;
  427. s->block_len_bits = s->frame_len_bits;
  428. }
  429. if (s->frame_len_bits - s->block_len_bits >= s->nb_block_sizes){
  430. av_log(s->avctx, AV_LOG_ERROR, "block_len_bits not initialized to a valid value\n");
  431. return -1;
  432. }
  433. /* now check if the block length is coherent with the frame length */
  434. s->block_len = 1 << s->block_len_bits;
  435. if ((s->block_pos + s->block_len) > s->frame_len){
  436. av_log(s->avctx, AV_LOG_ERROR, "frame_len overflow\n");
  437. return -1;
  438. }
  439. if (s->avctx->channels == 2) {
  440. s->ms_stereo = get_bits1(&s->gb);
  441. }
  442. v = 0;
  443. for(ch = 0; ch < s->avctx->channels; ch++) {
  444. a = get_bits1(&s->gb);
  445. s->channel_coded[ch] = a;
  446. v |= a;
  447. }
  448. bsize = s->frame_len_bits - s->block_len_bits;
  449. /* if no channel coded, no need to go further */
  450. /* XXX: fix potential framing problems */
  451. if (!v)
  452. goto next;
  453. /* read total gain and extract corresponding number of bits for
  454. coef escape coding */
  455. total_gain = 1;
  456. for(;;) {
  457. if (get_bits_left(&s->gb) < 7) {
  458. av_log(s->avctx, AV_LOG_ERROR, "total_gain overread\n");
  459. return AVERROR_INVALIDDATA;
  460. }
  461. a = get_bits(&s->gb, 7);
  462. total_gain += a;
  463. if (a != 127)
  464. break;
  465. }
  466. coef_nb_bits= ff_wma_total_gain_to_bits(total_gain);
  467. /* compute number of coefficients */
  468. n = s->coefs_end[bsize] - s->coefs_start;
  469. for(ch = 0; ch < s->avctx->channels; ch++)
  470. nb_coefs[ch] = n;
  471. /* complex coding */
  472. if (s->use_noise_coding) {
  473. for(ch = 0; ch < s->avctx->channels; ch++) {
  474. if (s->channel_coded[ch]) {
  475. int i, n, a;
  476. n = s->exponent_high_sizes[bsize];
  477. for(i=0;i<n;i++) {
  478. a = get_bits1(&s->gb);
  479. s->high_band_coded[ch][i] = a;
  480. /* if noise coding, the coefficients are not transmitted */
  481. if (a)
  482. nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
  483. }
  484. }
  485. }
  486. for(ch = 0; ch < s->avctx->channels; ch++) {
  487. if (s->channel_coded[ch]) {
  488. int i, n, val, code;
  489. n = s->exponent_high_sizes[bsize];
  490. val = (int)0x80000000;
  491. for(i=0;i<n;i++) {
  492. if (s->high_band_coded[ch][i]) {
  493. if (val == (int)0x80000000) {
  494. val = get_bits(&s->gb, 7) - 19;
  495. } else {
  496. code = get_vlc2(&s->gb, s->hgain_vlc.table, HGAINVLCBITS, HGAINMAX);
  497. if (code < 0){
  498. av_log(s->avctx, AV_LOG_ERROR, "hgain vlc invalid\n");
  499. return -1;
  500. }
  501. val += code - 18;
  502. }
  503. s->high_band_values[ch][i] = val;
  504. }
  505. }
  506. }
  507. }
  508. }
  509. /* exponents can be reused in short blocks. */
  510. if ((s->block_len_bits == s->frame_len_bits) ||
  511. get_bits1(&s->gb)) {
  512. for(ch = 0; ch < s->avctx->channels; ch++) {
  513. if (s->channel_coded[ch]) {
  514. if (s->use_exp_vlc) {
  515. if (decode_exp_vlc(s, ch) < 0)
  516. return -1;
  517. } else {
  518. decode_exp_lsp(s, ch);
  519. }
  520. s->exponents_bsize[ch] = bsize;
  521. }
  522. }
  523. }
  524. /* parse spectral coefficients : just RLE encoding */
  525. for (ch = 0; ch < s->avctx->channels; ch++) {
  526. if (s->channel_coded[ch]) {
  527. int tindex;
  528. WMACoef* ptr = &s->coefs1[ch][0];
  529. /* special VLC tables are used for ms stereo because
  530. there is potentially less energy there */
  531. tindex = (ch == 1 && s->ms_stereo);
  532. memset(ptr, 0, s->block_len * sizeof(WMACoef));
  533. ff_wma_run_level_decode(s->avctx, &s->gb, &s->coef_vlc[tindex],
  534. s->level_table[tindex], s->run_table[tindex],
  535. 0, ptr, 0, nb_coefs[ch],
  536. s->block_len, s->frame_len_bits, coef_nb_bits);
  537. }
  538. if (s->version == 1 && s->avctx->channels >= 2) {
  539. align_get_bits(&s->gb);
  540. }
  541. }
  542. /* normalize */
  543. {
  544. int n4 = s->block_len / 2;
  545. mdct_norm = 1.0 / (float)n4;
  546. if (s->version == 1) {
  547. mdct_norm *= sqrt(n4);
  548. }
  549. }
  550. /* finally compute the MDCT coefficients */
  551. for (ch = 0; ch < s->avctx->channels; ch++) {
  552. if (s->channel_coded[ch]) {
  553. WMACoef *coefs1;
  554. float *coefs, *exponents, mult, mult1, noise;
  555. int i, j, n, n1, last_high_band, esize;
  556. float exp_power[HIGH_BAND_MAX_SIZE];
  557. coefs1 = s->coefs1[ch];
  558. exponents = s->exponents[ch];
  559. esize = s->exponents_bsize[ch];
  560. mult = pow(10, total_gain * 0.05) / s->max_exponent[ch];
  561. mult *= mdct_norm;
  562. coefs = s->coefs[ch];
  563. if (s->use_noise_coding) {
  564. mult1 = mult;
  565. /* very low freqs : noise */
  566. for(i = 0;i < s->coefs_start; i++) {
  567. *coefs++ = s->noise_table[s->noise_index] *
  568. exponents[i<<bsize>>esize] * mult1;
  569. s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
  570. }
  571. n1 = s->exponent_high_sizes[bsize];
  572. /* compute power of high bands */
  573. exponents = s->exponents[ch] +
  574. (s->high_band_start[bsize]<<bsize>>esize);
  575. last_high_band = 0; /* avoid warning */
  576. for(j=0;j<n1;j++) {
  577. n = s->exponent_high_bands[s->frame_len_bits -
  578. s->block_len_bits][j];
  579. if (s->high_band_coded[ch][j]) {
  580. float e2, v;
  581. e2 = 0;
  582. for(i = 0;i < n; i++) {
  583. v = exponents[i<<bsize>>esize];
  584. e2 += v * v;
  585. }
  586. exp_power[j] = e2 / n;
  587. last_high_band = j;
  588. tprintf(s->avctx, "%d: power=%f (%d)\n", j, exp_power[j], n);
  589. }
  590. exponents += n<<bsize>>esize;
  591. }
  592. /* main freqs and high freqs */
  593. exponents = s->exponents[ch] + (s->coefs_start<<bsize>>esize);
  594. for(j=-1;j<n1;j++) {
  595. if (j < 0) {
  596. n = s->high_band_start[bsize] -
  597. s->coefs_start;
  598. } else {
  599. n = s->exponent_high_bands[s->frame_len_bits -
  600. s->block_len_bits][j];
  601. }
  602. if (j >= 0 && s->high_band_coded[ch][j]) {
  603. /* use noise with specified power */
  604. mult1 = sqrt(exp_power[j] / exp_power[last_high_band]);
  605. /* XXX: use a table */
  606. mult1 = mult1 * pow(10, s->high_band_values[ch][j] * 0.05);
  607. mult1 = mult1 / (s->max_exponent[ch] * s->noise_mult);
  608. mult1 *= mdct_norm;
  609. for(i = 0;i < n; i++) {
  610. noise = s->noise_table[s->noise_index];
  611. s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
  612. *coefs++ = noise *
  613. exponents[i<<bsize>>esize] * mult1;
  614. }
  615. exponents += n<<bsize>>esize;
  616. } else {
  617. /* coded values + small noise */
  618. for(i = 0;i < n; i++) {
  619. noise = s->noise_table[s->noise_index];
  620. s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
  621. *coefs++ = ((*coefs1++) + noise) *
  622. exponents[i<<bsize>>esize] * mult;
  623. }
  624. exponents += n<<bsize>>esize;
  625. }
  626. }
  627. /* very high freqs : noise */
  628. n = s->block_len - s->coefs_end[bsize];
  629. mult1 = mult * exponents[((-1<<bsize))>>esize];
  630. for(i = 0; i < n; i++) {
  631. *coefs++ = s->noise_table[s->noise_index] * mult1;
  632. s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
  633. }
  634. } else {
  635. /* XXX: optimize more */
  636. for(i = 0;i < s->coefs_start; i++)
  637. *coefs++ = 0.0;
  638. n = nb_coefs[ch];
  639. for(i = 0;i < n; i++) {
  640. *coefs++ = coefs1[i] * exponents[i<<bsize>>esize] * mult;
  641. }
  642. n = s->block_len - s->coefs_end[bsize];
  643. for(i = 0;i < n; i++)
  644. *coefs++ = 0.0;
  645. }
  646. }
  647. }
  648. #ifdef TRACE
  649. for (ch = 0; ch < s->avctx->channels; ch++) {
  650. if (s->channel_coded[ch]) {
  651. dump_floats(s, "exponents", 3, s->exponents[ch], s->block_len);
  652. dump_floats(s, "coefs", 1, s->coefs[ch], s->block_len);
  653. }
  654. }
  655. #endif
  656. if (s->ms_stereo && s->channel_coded[1]) {
  657. /* nominal case for ms stereo: we do it before mdct */
  658. /* no need to optimize this case because it should almost
  659. never happen */
  660. if (!s->channel_coded[0]) {
  661. tprintf(s->avctx, "rare ms-stereo case happened\n");
  662. memset(s->coefs[0], 0, sizeof(float) * s->block_len);
  663. s->channel_coded[0] = 1;
  664. }
  665. s->fdsp.butterflies_float(s->coefs[0], s->coefs[1], s->block_len);
  666. }
  667. next:
  668. mdct = &s->mdct_ctx[bsize];
  669. for (ch = 0; ch < s->avctx->channels; ch++) {
  670. int n4, index;
  671. n4 = s->block_len / 2;
  672. if(s->channel_coded[ch]){
  673. mdct->imdct_calc(mdct, s->output, s->coefs[ch]);
  674. }else if(!(s->ms_stereo && ch==1))
  675. memset(s->output, 0, sizeof(s->output));
  676. /* multiply by the window and add in the frame */
  677. index = (s->frame_len / 2) + s->block_pos - n4;
  678. wma_window(s, &s->frame_out[ch][index]);
  679. }
  680. /* update block number */
  681. s->block_num++;
  682. s->block_pos += s->block_len;
  683. if (s->block_pos >= s->frame_len)
  684. return 1;
  685. else
  686. return 0;
  687. }
  688. /* decode a frame of frame_len samples */
  689. static int wma_decode_frame(WMACodecContext *s, float **samples,
  690. int samples_offset)
  691. {
  692. int ret, ch;
  693. #ifdef TRACE
  694. tprintf(s->avctx, "***decode_frame: %d size=%d\n", s->frame_count++, s->frame_len);
  695. #endif
  696. /* read each block */
  697. s->block_num = 0;
  698. s->block_pos = 0;
  699. for(;;) {
  700. ret = wma_decode_block(s);
  701. if (ret < 0)
  702. return -1;
  703. if (ret)
  704. break;
  705. }
  706. for (ch = 0; ch < s->avctx->channels; ch++) {
  707. /* copy current block to output */
  708. memcpy(samples[ch] + samples_offset, s->frame_out[ch],
  709. s->frame_len * sizeof(*s->frame_out[ch]));
  710. /* prepare for next block */
  711. memmove(&s->frame_out[ch][0], &s->frame_out[ch][s->frame_len],
  712. s->frame_len * sizeof(*s->frame_out[ch]));
  713. #ifdef TRACE
  714. dump_floats(s, "samples", 6, samples[ch] + samples_offset, s->frame_len);
  715. #endif
  716. }
  717. return 0;
  718. }
  719. static int wma_decode_superframe(AVCodecContext *avctx, void *data,
  720. int *got_frame_ptr, AVPacket *avpkt)
  721. {
  722. AVFrame *frame = data;
  723. const uint8_t *buf = avpkt->data;
  724. int buf_size = avpkt->size;
  725. WMACodecContext *s = avctx->priv_data;
  726. int nb_frames, bit_offset, i, pos, len, ret;
  727. uint8_t *q;
  728. float **samples;
  729. int samples_offset;
  730. tprintf(avctx, "***decode_superframe:\n");
  731. if(buf_size==0){
  732. s->last_superframe_len = 0;
  733. return 0;
  734. }
  735. if (buf_size < avctx->block_align) {
  736. av_log(avctx, AV_LOG_ERROR,
  737. "Input packet size too small (%d < %d)\n",
  738. buf_size, avctx->block_align);
  739. return AVERROR_INVALIDDATA;
  740. }
  741. if(avctx->block_align)
  742. buf_size = avctx->block_align;
  743. init_get_bits(&s->gb, buf, buf_size*8);
  744. if (s->use_bit_reservoir) {
  745. /* read super frame header */
  746. skip_bits(&s->gb, 4); /* super frame index */
  747. nb_frames = get_bits(&s->gb, 4) - (s->last_superframe_len <= 0);
  748. if (nb_frames <= 0) {
  749. av_log(avctx, AV_LOG_ERROR, "nb_frames is %d\n", nb_frames);
  750. return AVERROR_INVALIDDATA;
  751. }
  752. } else {
  753. nb_frames = 1;
  754. }
  755. /* get output buffer */
  756. frame->nb_samples = nb_frames * s->frame_len;
  757. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  758. return ret;
  759. samples = (float **)frame->extended_data;
  760. samples_offset = 0;
  761. if (s->use_bit_reservoir) {
  762. bit_offset = get_bits(&s->gb, s->byte_offset_bits + 3);
  763. if (bit_offset > get_bits_left(&s->gb)) {
  764. av_log(avctx, AV_LOG_ERROR,
  765. "Invalid last frame bit offset %d > buf size %d (%d)\n",
  766. bit_offset, get_bits_left(&s->gb), buf_size);
  767. goto fail;
  768. }
  769. if (s->last_superframe_len > 0) {
  770. /* add bit_offset bits to last frame */
  771. if ((s->last_superframe_len + ((bit_offset + 7) >> 3)) >
  772. MAX_CODED_SUPERFRAME_SIZE)
  773. goto fail;
  774. q = s->last_superframe + s->last_superframe_len;
  775. len = bit_offset;
  776. while (len > 7) {
  777. *q++ = (get_bits)(&s->gb, 8);
  778. len -= 8;
  779. }
  780. if (len > 0) {
  781. *q++ = (get_bits)(&s->gb, len) << (8 - len);
  782. }
  783. memset(q, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  784. /* XXX: bit_offset bits into last frame */
  785. init_get_bits(&s->gb, s->last_superframe, s->last_superframe_len * 8 + bit_offset);
  786. /* skip unused bits */
  787. if (s->last_bitoffset > 0)
  788. skip_bits(&s->gb, s->last_bitoffset);
  789. /* this frame is stored in the last superframe and in the
  790. current one */
  791. if (wma_decode_frame(s, samples, samples_offset) < 0)
  792. goto fail;
  793. samples_offset += s->frame_len;
  794. nb_frames--;
  795. }
  796. /* read each frame starting from bit_offset */
  797. pos = bit_offset + 4 + 4 + s->byte_offset_bits + 3;
  798. if (pos >= MAX_CODED_SUPERFRAME_SIZE * 8 || pos > buf_size * 8)
  799. return AVERROR_INVALIDDATA;
  800. init_get_bits(&s->gb, buf + (pos >> 3), (buf_size - (pos >> 3))*8);
  801. len = pos & 7;
  802. if (len > 0)
  803. skip_bits(&s->gb, len);
  804. s->reset_block_lengths = 1;
  805. for(i=0;i<nb_frames;i++) {
  806. if (wma_decode_frame(s, samples, samples_offset) < 0)
  807. goto fail;
  808. samples_offset += s->frame_len;
  809. }
  810. /* we copy the end of the frame in the last frame buffer */
  811. pos = get_bits_count(&s->gb) + ((bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7);
  812. s->last_bitoffset = pos & 7;
  813. pos >>= 3;
  814. len = buf_size - pos;
  815. if (len > MAX_CODED_SUPERFRAME_SIZE || len < 0) {
  816. av_log(s->avctx, AV_LOG_ERROR, "len %d invalid\n", len);
  817. goto fail;
  818. }
  819. s->last_superframe_len = len;
  820. memcpy(s->last_superframe, buf + pos, len);
  821. } else {
  822. /* single frame decode */
  823. if (wma_decode_frame(s, samples, samples_offset) < 0)
  824. goto fail;
  825. samples_offset += s->frame_len;
  826. }
  827. av_dlog(s->avctx, "%d %d %d %d outbytes:%"PTRDIFF_SPECIFIER" eaten:%d\n",
  828. s->frame_len_bits, s->block_len_bits, s->frame_len, s->block_len,
  829. (int8_t *)samples - (int8_t *)data, avctx->block_align);
  830. *got_frame_ptr = 1;
  831. return buf_size;
  832. fail:
  833. /* when error, we reset the bit reservoir */
  834. s->last_superframe_len = 0;
  835. return -1;
  836. }
  837. static av_cold void flush(AVCodecContext *avctx)
  838. {
  839. WMACodecContext *s = avctx->priv_data;
  840. s->last_bitoffset=
  841. s->last_superframe_len= 0;
  842. }
  843. #if CONFIG_WMAV1_DECODER
  844. AVCodec ff_wmav1_decoder = {
  845. .name = "wmav1",
  846. .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 1"),
  847. .type = AVMEDIA_TYPE_AUDIO,
  848. .id = AV_CODEC_ID_WMAV1,
  849. .priv_data_size = sizeof(WMACodecContext),
  850. .init = wma_decode_init,
  851. .close = ff_wma_end,
  852. .decode = wma_decode_superframe,
  853. .flush = flush,
  854. .capabilities = CODEC_CAP_DR1,
  855. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
  856. AV_SAMPLE_FMT_NONE },
  857. };
  858. #endif
  859. #if CONFIG_WMAV2_DECODER
  860. AVCodec ff_wmav2_decoder = {
  861. .name = "wmav2",
  862. .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 2"),
  863. .type = AVMEDIA_TYPE_AUDIO,
  864. .id = AV_CODEC_ID_WMAV2,
  865. .priv_data_size = sizeof(WMACodecContext),
  866. .init = wma_decode_init,
  867. .close = ff_wma_end,
  868. .decode = wma_decode_superframe,
  869. .flush = flush,
  870. .capabilities = CODEC_CAP_DR1,
  871. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
  872. AV_SAMPLE_FMT_NONE },
  873. };
  874. #endif