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.

1338 lines
42KB

  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 wmadec.c
  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 "avcodec.h"
  35. #include "bitstream.h"
  36. #include "dsputil.h"
  37. /* size of blocks */
  38. #define BLOCK_MIN_BITS 7
  39. #define BLOCK_MAX_BITS 11
  40. #define BLOCK_MAX_SIZE (1 << BLOCK_MAX_BITS)
  41. #define BLOCK_NB_SIZES (BLOCK_MAX_BITS - BLOCK_MIN_BITS + 1)
  42. /* XXX: find exact max size */
  43. #define HIGH_BAND_MAX_SIZE 16
  44. #define NB_LSP_COEFS 10
  45. /* XXX: is it a suitable value ? */
  46. #define MAX_CODED_SUPERFRAME_SIZE 16384
  47. #define MAX_CHANNELS 2
  48. #define NOISE_TAB_SIZE 8192
  49. #define LSP_POW_BITS 7
  50. #define VLCBITS 9
  51. #define VLCMAX ((22+VLCBITS-1)/VLCBITS)
  52. #define EXPVLCBITS 8
  53. #define EXPMAX ((19+EXPVLCBITS-1)/EXPVLCBITS)
  54. #define HGAINVLCBITS 9
  55. #define HGAINMAX ((13+HGAINVLCBITS-1)/HGAINVLCBITS)
  56. typedef struct WMADecodeContext {
  57. GetBitContext gb;
  58. int sample_rate;
  59. int nb_channels;
  60. int bit_rate;
  61. int version; /* 1 = 0x160 (WMAV1), 2 = 0x161 (WMAV2) */
  62. int block_align;
  63. int use_bit_reservoir;
  64. int use_variable_block_len;
  65. int use_exp_vlc; /* exponent coding: 0 = lsp, 1 = vlc + delta */
  66. int use_noise_coding; /* true if perceptual noise is added */
  67. int byte_offset_bits;
  68. VLC exp_vlc;
  69. int exponent_sizes[BLOCK_NB_SIZES];
  70. uint16_t exponent_bands[BLOCK_NB_SIZES][25];
  71. int high_band_start[BLOCK_NB_SIZES]; /* index of first coef in high band */
  72. int coefs_start; /* first coded coef */
  73. int coefs_end[BLOCK_NB_SIZES]; /* max number of coded coefficients */
  74. int exponent_high_sizes[BLOCK_NB_SIZES];
  75. int exponent_high_bands[BLOCK_NB_SIZES][HIGH_BAND_MAX_SIZE];
  76. VLC hgain_vlc;
  77. /* coded values in high bands */
  78. int high_band_coded[MAX_CHANNELS][HIGH_BAND_MAX_SIZE];
  79. int high_band_values[MAX_CHANNELS][HIGH_BAND_MAX_SIZE];
  80. /* there are two possible tables for spectral coefficients */
  81. VLC coef_vlc[2];
  82. uint16_t *run_table[2];
  83. uint16_t *level_table[2];
  84. /* frame info */
  85. int frame_len; /* frame length in samples */
  86. int frame_len_bits; /* frame_len = 1 << frame_len_bits */
  87. int nb_block_sizes; /* number of block sizes */
  88. /* block info */
  89. int reset_block_lengths;
  90. int block_len_bits; /* log2 of current block length */
  91. int next_block_len_bits; /* log2 of next block length */
  92. int prev_block_len_bits; /* log2 of prev block length */
  93. int block_len; /* block length in samples */
  94. int block_num; /* block number in current frame */
  95. int block_pos; /* current position in frame */
  96. uint8_t ms_stereo; /* true if mid/side stereo mode */
  97. uint8_t channel_coded[MAX_CHANNELS]; /* true if channel is coded */
  98. DECLARE_ALIGNED_16(float, exponents[MAX_CHANNELS][BLOCK_MAX_SIZE]);
  99. float max_exponent[MAX_CHANNELS];
  100. int16_t coefs1[MAX_CHANNELS][BLOCK_MAX_SIZE];
  101. DECLARE_ALIGNED_16(float, coefs[MAX_CHANNELS][BLOCK_MAX_SIZE]);
  102. DECLARE_ALIGNED_16(FFTSample, output[BLOCK_MAX_SIZE * 2]);
  103. DECLARE_ALIGNED_16(float, window[BLOCK_MAX_SIZE * 2]);
  104. MDCTContext mdct_ctx[BLOCK_NB_SIZES];
  105. float *windows[BLOCK_NB_SIZES];
  106. DECLARE_ALIGNED_16(FFTSample, mdct_tmp[BLOCK_MAX_SIZE]); /* temporary storage for imdct */
  107. /* output buffer for one frame and the last for IMDCT windowing */
  108. DECLARE_ALIGNED_16(float, frame_out[MAX_CHANNELS][BLOCK_MAX_SIZE * 2]);
  109. /* last frame info */
  110. uint8_t last_superframe[MAX_CODED_SUPERFRAME_SIZE + 4]; /* padding added */
  111. int last_bitoffset;
  112. int last_superframe_len;
  113. float noise_table[NOISE_TAB_SIZE];
  114. int noise_index;
  115. float noise_mult; /* XXX: suppress that and integrate it in the noise array */
  116. /* lsp_to_curve tables */
  117. float lsp_cos_table[BLOCK_MAX_SIZE];
  118. float lsp_pow_e_table[256];
  119. float lsp_pow_m_table1[(1 << LSP_POW_BITS)];
  120. float lsp_pow_m_table2[(1 << LSP_POW_BITS)];
  121. DSPContext dsp;
  122. #ifdef TRACE
  123. int frame_count;
  124. #endif
  125. } WMADecodeContext;
  126. typedef struct CoefVLCTable {
  127. int n; /* total number of codes */
  128. const uint32_t *huffcodes; /* VLC bit values */
  129. const uint8_t *huffbits; /* VLC bit size */
  130. const uint16_t *levels; /* table to build run/level tables */
  131. } CoefVLCTable;
  132. static void wma_lsp_to_curve_init(WMADecodeContext *s, int frame_len);
  133. #include "wmadata.h"
  134. #ifdef TRACE
  135. static void dump_shorts(const char *name, const short *tab, int n)
  136. {
  137. int i;
  138. tprintf("%s[%d]:\n", name, n);
  139. for(i=0;i<n;i++) {
  140. if ((i & 7) == 0)
  141. tprintf("%4d: ", i);
  142. tprintf(" %5d.0", tab[i]);
  143. if ((i & 7) == 7)
  144. tprintf("\n");
  145. }
  146. }
  147. static void dump_floats(const char *name, int prec, const float *tab, int n)
  148. {
  149. int i;
  150. tprintf("%s[%d]:\n", name, n);
  151. for(i=0;i<n;i++) {
  152. if ((i & 7) == 0)
  153. tprintf("%4d: ", i);
  154. tprintf(" %8.*f", prec, tab[i]);
  155. if ((i & 7) == 7)
  156. tprintf("\n");
  157. }
  158. if ((i & 7) != 0)
  159. tprintf("\n");
  160. }
  161. #endif
  162. /* XXX: use same run/length optimization as mpeg decoders */
  163. static void init_coef_vlc(VLC *vlc,
  164. uint16_t **prun_table, uint16_t **plevel_table,
  165. const CoefVLCTable *vlc_table)
  166. {
  167. int n = vlc_table->n;
  168. const uint8_t *table_bits = vlc_table->huffbits;
  169. const uint32_t *table_codes = vlc_table->huffcodes;
  170. const uint16_t *levels_table = vlc_table->levels;
  171. uint16_t *run_table, *level_table;
  172. const uint16_t *p;
  173. int i, l, j, level;
  174. init_vlc(vlc, VLCBITS, n, table_bits, 1, 1, table_codes, 4, 4, 0);
  175. run_table = av_malloc(n * sizeof(uint16_t));
  176. level_table = av_malloc(n * sizeof(uint16_t));
  177. p = levels_table;
  178. i = 2;
  179. level = 1;
  180. while (i < n) {
  181. l = *p++;
  182. for(j=0;j<l;j++) {
  183. run_table[i] = j;
  184. level_table[i] = level;
  185. i++;
  186. }
  187. level++;
  188. }
  189. *prun_table = run_table;
  190. *plevel_table = level_table;
  191. }
  192. static int wma_decode_init(AVCodecContext * avctx)
  193. {
  194. WMADecodeContext *s = avctx->priv_data;
  195. int i, flags1, flags2;
  196. float *window;
  197. uint8_t *extradata;
  198. float bps1, high_freq;
  199. volatile float bps;
  200. int sample_rate1;
  201. int coef_vlc_table;
  202. s->sample_rate = avctx->sample_rate;
  203. s->nb_channels = avctx->channels;
  204. s->bit_rate = avctx->bit_rate;
  205. s->block_align = avctx->block_align;
  206. dsputil_init(&s->dsp, avctx);
  207. if (avctx->codec->id == CODEC_ID_WMAV1) {
  208. s->version = 1;
  209. } else {
  210. s->version = 2;
  211. }
  212. /* extract flag infos */
  213. flags1 = 0;
  214. flags2 = 0;
  215. extradata = avctx->extradata;
  216. if (s->version == 1 && avctx->extradata_size >= 4) {
  217. flags1 = extradata[0] | (extradata[1] << 8);
  218. flags2 = extradata[2] | (extradata[3] << 8);
  219. } else if (s->version == 2 && avctx->extradata_size >= 6) {
  220. flags1 = extradata[0] | (extradata[1] << 8) |
  221. (extradata[2] << 16) | (extradata[3] << 24);
  222. flags2 = extradata[4] | (extradata[5] << 8);
  223. }
  224. s->use_exp_vlc = flags2 & 0x0001;
  225. s->use_bit_reservoir = flags2 & 0x0002;
  226. s->use_variable_block_len = flags2 & 0x0004;
  227. /* compute MDCT block size */
  228. if (s->sample_rate <= 16000) {
  229. s->frame_len_bits = 9;
  230. } else if (s->sample_rate <= 22050 ||
  231. (s->sample_rate <= 32000 && s->version == 1)) {
  232. s->frame_len_bits = 10;
  233. } else {
  234. s->frame_len_bits = 11;
  235. }
  236. s->frame_len = 1 << s->frame_len_bits;
  237. if (s->use_variable_block_len) {
  238. int nb_max, nb;
  239. nb = ((flags2 >> 3) & 3) + 1;
  240. if ((s->bit_rate / s->nb_channels) >= 32000)
  241. nb += 2;
  242. nb_max = s->frame_len_bits - BLOCK_MIN_BITS;
  243. if (nb > nb_max)
  244. nb = nb_max;
  245. s->nb_block_sizes = nb + 1;
  246. } else {
  247. s->nb_block_sizes = 1;
  248. }
  249. /* init rate dependant parameters */
  250. s->use_noise_coding = 1;
  251. high_freq = s->sample_rate * 0.5;
  252. /* if version 2, then the rates are normalized */
  253. sample_rate1 = s->sample_rate;
  254. if (s->version == 2) {
  255. if (sample_rate1 >= 44100)
  256. sample_rate1 = 44100;
  257. else if (sample_rate1 >= 22050)
  258. sample_rate1 = 22050;
  259. else if (sample_rate1 >= 16000)
  260. sample_rate1 = 16000;
  261. else if (sample_rate1 >= 11025)
  262. sample_rate1 = 11025;
  263. else if (sample_rate1 >= 8000)
  264. sample_rate1 = 8000;
  265. }
  266. bps = (float)s->bit_rate / (float)(s->nb_channels * s->sample_rate);
  267. s->byte_offset_bits = av_log2((int)(bps * s->frame_len / 8.0 + 0.5)) + 2;
  268. /* compute high frequency value and choose if noise coding should
  269. be activated */
  270. bps1 = bps;
  271. if (s->nb_channels == 2)
  272. bps1 = bps * 1.6;
  273. if (sample_rate1 == 44100) {
  274. if (bps1 >= 0.61)
  275. s->use_noise_coding = 0;
  276. else
  277. high_freq = high_freq * 0.4;
  278. } else if (sample_rate1 == 22050) {
  279. if (bps1 >= 1.16)
  280. s->use_noise_coding = 0;
  281. else if (bps1 >= 0.72)
  282. high_freq = high_freq * 0.7;
  283. else
  284. high_freq = high_freq * 0.6;
  285. } else if (sample_rate1 == 16000) {
  286. if (bps > 0.5)
  287. high_freq = high_freq * 0.5;
  288. else
  289. high_freq = high_freq * 0.3;
  290. } else if (sample_rate1 == 11025) {
  291. high_freq = high_freq * 0.7;
  292. } else if (sample_rate1 == 8000) {
  293. if (bps <= 0.625) {
  294. high_freq = high_freq * 0.5;
  295. } else if (bps > 0.75) {
  296. s->use_noise_coding = 0;
  297. } else {
  298. high_freq = high_freq * 0.65;
  299. }
  300. } else {
  301. if (bps >= 0.8) {
  302. high_freq = high_freq * 0.75;
  303. } else if (bps >= 0.6) {
  304. high_freq = high_freq * 0.6;
  305. } else {
  306. high_freq = high_freq * 0.5;
  307. }
  308. }
  309. dprintf("flags1=0x%x flags2=0x%x\n", flags1, flags2);
  310. dprintf("version=%d channels=%d sample_rate=%d bitrate=%d block_align=%d\n",
  311. s->version, s->nb_channels, s->sample_rate, s->bit_rate,
  312. s->block_align);
  313. dprintf("bps=%f bps1=%f high_freq=%f bitoffset=%d\n",
  314. bps, bps1, high_freq, s->byte_offset_bits);
  315. dprintf("use_noise_coding=%d use_exp_vlc=%d nb_block_sizes=%d\n",
  316. s->use_noise_coding, s->use_exp_vlc, s->nb_block_sizes);
  317. /* compute the scale factor band sizes for each MDCT block size */
  318. {
  319. int a, b, pos, lpos, k, block_len, i, j, n;
  320. const uint8_t *table;
  321. if (s->version == 1) {
  322. s->coefs_start = 3;
  323. } else {
  324. s->coefs_start = 0;
  325. }
  326. for(k = 0; k < s->nb_block_sizes; k++) {
  327. block_len = s->frame_len >> k;
  328. if (s->version == 1) {
  329. lpos = 0;
  330. for(i=0;i<25;i++) {
  331. a = wma_critical_freqs[i];
  332. b = s->sample_rate;
  333. pos = ((block_len * 2 * a) + (b >> 1)) / b;
  334. if (pos > block_len)
  335. pos = block_len;
  336. s->exponent_bands[0][i] = pos - lpos;
  337. if (pos >= block_len) {
  338. i++;
  339. break;
  340. }
  341. lpos = pos;
  342. }
  343. s->exponent_sizes[0] = i;
  344. } else {
  345. /* hardcoded tables */
  346. table = NULL;
  347. a = s->frame_len_bits - BLOCK_MIN_BITS - k;
  348. if (a < 3) {
  349. if (s->sample_rate >= 44100)
  350. table = exponent_band_44100[a];
  351. else if (s->sample_rate >= 32000)
  352. table = exponent_band_32000[a];
  353. else if (s->sample_rate >= 22050)
  354. table = exponent_band_22050[a];
  355. }
  356. if (table) {
  357. n = *table++;
  358. for(i=0;i<n;i++)
  359. s->exponent_bands[k][i] = table[i];
  360. s->exponent_sizes[k] = n;
  361. } else {
  362. j = 0;
  363. lpos = 0;
  364. for(i=0;i<25;i++) {
  365. a = wma_critical_freqs[i];
  366. b = s->sample_rate;
  367. pos = ((block_len * 2 * a) + (b << 1)) / (4 * b);
  368. pos <<= 2;
  369. if (pos > block_len)
  370. pos = block_len;
  371. if (pos > lpos)
  372. s->exponent_bands[k][j++] = pos - lpos;
  373. if (pos >= block_len)
  374. break;
  375. lpos = pos;
  376. }
  377. s->exponent_sizes[k] = j;
  378. }
  379. }
  380. /* max number of coefs */
  381. s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k;
  382. /* high freq computation */
  383. s->high_band_start[k] = (int)((block_len * 2 * high_freq) /
  384. s->sample_rate + 0.5);
  385. n = s->exponent_sizes[k];
  386. j = 0;
  387. pos = 0;
  388. for(i=0;i<n;i++) {
  389. int start, end;
  390. start = pos;
  391. pos += s->exponent_bands[k][i];
  392. end = pos;
  393. if (start < s->high_band_start[k])
  394. start = s->high_band_start[k];
  395. if (end > s->coefs_end[k])
  396. end = s->coefs_end[k];
  397. if (end > start)
  398. s->exponent_high_bands[k][j++] = end - start;
  399. }
  400. s->exponent_high_sizes[k] = j;
  401. #if 0
  402. tprintf("%5d: coefs_end=%d high_band_start=%d nb_high_bands=%d: ",
  403. s->frame_len >> k,
  404. s->coefs_end[k],
  405. s->high_band_start[k],
  406. s->exponent_high_sizes[k]);
  407. for(j=0;j<s->exponent_high_sizes[k];j++)
  408. tprintf(" %d", s->exponent_high_bands[k][j]);
  409. tprintf("\n");
  410. #endif
  411. }
  412. }
  413. #ifdef TRACE
  414. {
  415. int i, j;
  416. for(i = 0; i < s->nb_block_sizes; i++) {
  417. tprintf("%5d: n=%2d:",
  418. s->frame_len >> i,
  419. s->exponent_sizes[i]);
  420. for(j=0;j<s->exponent_sizes[i];j++)
  421. tprintf(" %d", s->exponent_bands[i][j]);
  422. tprintf("\n");
  423. }
  424. }
  425. #endif
  426. /* init MDCT */
  427. for(i = 0; i < s->nb_block_sizes; i++)
  428. ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 1);
  429. /* init MDCT windows : simple sinus window */
  430. for(i = 0; i < s->nb_block_sizes; i++) {
  431. int n, j;
  432. float alpha;
  433. n = 1 << (s->frame_len_bits - i);
  434. window = av_malloc(sizeof(float) * n);
  435. alpha = M_PI / (2.0 * n);
  436. for(j=0;j<n;j++) {
  437. window[n - j - 1] = sin((j + 0.5) * alpha);
  438. }
  439. s->windows[i] = window;
  440. }
  441. s->reset_block_lengths = 1;
  442. if (s->use_noise_coding) {
  443. /* init the noise generator */
  444. if (s->use_exp_vlc)
  445. s->noise_mult = 0.02;
  446. else
  447. s->noise_mult = 0.04;
  448. #ifdef TRACE
  449. for(i=0;i<NOISE_TAB_SIZE;i++)
  450. s->noise_table[i] = 1.0 * s->noise_mult;
  451. #else
  452. {
  453. unsigned int seed;
  454. float norm;
  455. seed = 1;
  456. norm = (1.0 / (float)(1LL << 31)) * sqrt(3) * s->noise_mult;
  457. for(i=0;i<NOISE_TAB_SIZE;i++) {
  458. seed = seed * 314159 + 1;
  459. s->noise_table[i] = (float)((int)seed) * norm;
  460. }
  461. }
  462. #endif
  463. init_vlc(&s->hgain_vlc, HGAINVLCBITS, sizeof(hgain_huffbits),
  464. hgain_huffbits, 1, 1,
  465. hgain_huffcodes, 2, 2, 0);
  466. }
  467. if (s->use_exp_vlc) {
  468. init_vlc(&s->exp_vlc, EXPVLCBITS, sizeof(scale_huffbits),
  469. scale_huffbits, 1, 1,
  470. scale_huffcodes, 4, 4, 0);
  471. } else {
  472. wma_lsp_to_curve_init(s, s->frame_len);
  473. }
  474. /* choose the VLC tables for the coefficients */
  475. coef_vlc_table = 2;
  476. if (s->sample_rate >= 32000) {
  477. if (bps1 < 0.72)
  478. coef_vlc_table = 0;
  479. else if (bps1 < 1.16)
  480. coef_vlc_table = 1;
  481. }
  482. init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0],
  483. &coef_vlcs[coef_vlc_table * 2]);
  484. init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1],
  485. &coef_vlcs[coef_vlc_table * 2 + 1]);
  486. return 0;
  487. }
  488. /* interpolate values for a bigger or smaller block. The block must
  489. have multiple sizes */
  490. static void interpolate_array(float *scale, int old_size, int new_size)
  491. {
  492. int i, j, jincr, k;
  493. float v;
  494. if (new_size > old_size) {
  495. jincr = new_size / old_size;
  496. j = new_size;
  497. for(i = old_size - 1; i >=0; i--) {
  498. v = scale[i];
  499. k = jincr;
  500. do {
  501. scale[--j] = v;
  502. } while (--k);
  503. }
  504. } else if (new_size < old_size) {
  505. j = 0;
  506. jincr = old_size / new_size;
  507. for(i = 0; i < new_size; i++) {
  508. scale[i] = scale[j];
  509. j += jincr;
  510. }
  511. }
  512. }
  513. /* compute x^-0.25 with an exponent and mantissa table. We use linear
  514. interpolation to reduce the mantissa table size at a small speed
  515. expense (linear interpolation approximately doubles the number of
  516. bits of precision). */
  517. static inline float pow_m1_4(WMADecodeContext *s, float x)
  518. {
  519. union {
  520. float f;
  521. unsigned int v;
  522. } u, t;
  523. unsigned int e, m;
  524. float a, b;
  525. u.f = x;
  526. e = u.v >> 23;
  527. m = (u.v >> (23 - LSP_POW_BITS)) & ((1 << LSP_POW_BITS) - 1);
  528. /* build interpolation scale: 1 <= t < 2. */
  529. t.v = ((u.v << LSP_POW_BITS) & ((1 << 23) - 1)) | (127 << 23);
  530. a = s->lsp_pow_m_table1[m];
  531. b = s->lsp_pow_m_table2[m];
  532. return s->lsp_pow_e_table[e] * (a + b * t.f);
  533. }
  534. static void wma_lsp_to_curve_init(WMADecodeContext *s, int frame_len)
  535. {
  536. float wdel, a, b;
  537. int i, e, m;
  538. wdel = M_PI / frame_len;
  539. for(i=0;i<frame_len;i++)
  540. s->lsp_cos_table[i] = 2.0f * cos(wdel * i);
  541. /* tables for x^-0.25 computation */
  542. for(i=0;i<256;i++) {
  543. e = i - 126;
  544. s->lsp_pow_e_table[i] = pow(2.0, e * -0.25);
  545. }
  546. /* NOTE: these two tables are needed to avoid two operations in
  547. pow_m1_4 */
  548. b = 1.0;
  549. for(i=(1 << LSP_POW_BITS) - 1;i>=0;i--) {
  550. m = (1 << LSP_POW_BITS) + i;
  551. a = (float)m * (0.5 / (1 << LSP_POW_BITS));
  552. a = pow(a, -0.25);
  553. s->lsp_pow_m_table1[i] = 2 * a - b;
  554. s->lsp_pow_m_table2[i] = b - a;
  555. b = a;
  556. }
  557. #if 0
  558. for(i=1;i<20;i++) {
  559. float v, r1, r2;
  560. v = 5.0 / i;
  561. r1 = pow_m1_4(s, v);
  562. r2 = pow(v,-0.25);
  563. printf("%f^-0.25=%f e=%f\n", v, r1, r2 - r1);
  564. }
  565. #endif
  566. }
  567. /* NOTE: We use the same code as Vorbis here */
  568. /* XXX: optimize it further with SSE/3Dnow */
  569. static void wma_lsp_to_curve(WMADecodeContext *s,
  570. float *out, float *val_max_ptr,
  571. int n, float *lsp)
  572. {
  573. int i, j;
  574. float p, q, w, v, val_max;
  575. val_max = 0;
  576. for(i=0;i<n;i++) {
  577. p = 0.5f;
  578. q = 0.5f;
  579. w = s->lsp_cos_table[i];
  580. for(j=1;j<NB_LSP_COEFS;j+=2){
  581. q *= w - lsp[j - 1];
  582. p *= w - lsp[j];
  583. }
  584. p *= p * (2.0f - w);
  585. q *= q * (2.0f + w);
  586. v = p + q;
  587. v = pow_m1_4(s, v);
  588. if (v > val_max)
  589. val_max = v;
  590. out[i] = v;
  591. }
  592. *val_max_ptr = val_max;
  593. }
  594. /* decode exponents coded with LSP coefficients (same idea as Vorbis) */
  595. static void decode_exp_lsp(WMADecodeContext *s, int ch)
  596. {
  597. float lsp_coefs[NB_LSP_COEFS];
  598. int val, i;
  599. for(i = 0; i < NB_LSP_COEFS; i++) {
  600. if (i == 0 || i >= 8)
  601. val = get_bits(&s->gb, 3);
  602. else
  603. val = get_bits(&s->gb, 4);
  604. lsp_coefs[i] = lsp_codebook[i][val];
  605. }
  606. wma_lsp_to_curve(s, s->exponents[ch], &s->max_exponent[ch],
  607. s->block_len, lsp_coefs);
  608. }
  609. /* decode exponents coded with VLC codes */
  610. static int decode_exp_vlc(WMADecodeContext *s, int ch)
  611. {
  612. int last_exp, n, code;
  613. const uint16_t *ptr, *band_ptr;
  614. float v, *q, max_scale, *q_end;
  615. band_ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
  616. ptr = band_ptr;
  617. q = s->exponents[ch];
  618. q_end = q + s->block_len;
  619. max_scale = 0;
  620. if (s->version == 1) {
  621. last_exp = get_bits(&s->gb, 5) + 10;
  622. /* XXX: use a table */
  623. v = pow(10, last_exp * (1.0 / 16.0));
  624. max_scale = v;
  625. n = *ptr++;
  626. do {
  627. *q++ = v;
  628. } while (--n);
  629. }
  630. last_exp = 36;
  631. while (q < q_end) {
  632. code = get_vlc2(&s->gb, s->exp_vlc.table, EXPVLCBITS, EXPMAX);
  633. if (code < 0)
  634. return -1;
  635. /* NOTE: this offset is the same as MPEG4 AAC ! */
  636. last_exp += code - 60;
  637. /* XXX: use a table */
  638. v = pow(10, last_exp * (1.0 / 16.0));
  639. if (v > max_scale)
  640. max_scale = v;
  641. n = *ptr++;
  642. do {
  643. *q++ = v;
  644. } while (--n);
  645. }
  646. s->max_exponent[ch] = max_scale;
  647. return 0;
  648. }
  649. /* return 0 if OK. return 1 if last block of frame. return -1 if
  650. unrecorrable error. */
  651. static int wma_decode_block(WMADecodeContext *s)
  652. {
  653. int n, v, a, ch, code, bsize;
  654. int coef_nb_bits, total_gain, parse_exponents;
  655. int nb_coefs[MAX_CHANNELS];
  656. float mdct_norm;
  657. #ifdef TRACE
  658. tprintf("***decode_block: %d:%d\n", s->frame_count - 1, s->block_num);
  659. #endif
  660. /* compute current block length */
  661. if (s->use_variable_block_len) {
  662. n = av_log2(s->nb_block_sizes - 1) + 1;
  663. if (s->reset_block_lengths) {
  664. s->reset_block_lengths = 0;
  665. v = get_bits(&s->gb, n);
  666. if (v >= s->nb_block_sizes)
  667. return -1;
  668. s->prev_block_len_bits = s->frame_len_bits - v;
  669. v = get_bits(&s->gb, n);
  670. if (v >= s->nb_block_sizes)
  671. return -1;
  672. s->block_len_bits = s->frame_len_bits - v;
  673. } else {
  674. /* update block lengths */
  675. s->prev_block_len_bits = s->block_len_bits;
  676. s->block_len_bits = s->next_block_len_bits;
  677. }
  678. v = get_bits(&s->gb, n);
  679. if (v >= s->nb_block_sizes)
  680. return -1;
  681. s->next_block_len_bits = s->frame_len_bits - v;
  682. } else {
  683. /* fixed block len */
  684. s->next_block_len_bits = s->frame_len_bits;
  685. s->prev_block_len_bits = s->frame_len_bits;
  686. s->block_len_bits = s->frame_len_bits;
  687. }
  688. /* now check if the block length is coherent with the frame length */
  689. s->block_len = 1 << s->block_len_bits;
  690. if ((s->block_pos + s->block_len) > s->frame_len)
  691. return -1;
  692. if (s->nb_channels == 2) {
  693. s->ms_stereo = get_bits(&s->gb, 1);
  694. }
  695. v = 0;
  696. for(ch = 0; ch < s->nb_channels; ch++) {
  697. a = get_bits(&s->gb, 1);
  698. s->channel_coded[ch] = a;
  699. v |= a;
  700. }
  701. /* if no channel coded, no need to go further */
  702. /* XXX: fix potential framing problems */
  703. if (!v)
  704. goto next;
  705. bsize = s->frame_len_bits - s->block_len_bits;
  706. /* read total gain and extract corresponding number of bits for
  707. coef escape coding */
  708. total_gain = 1;
  709. for(;;) {
  710. a = get_bits(&s->gb, 7);
  711. total_gain += a;
  712. if (a != 127)
  713. break;
  714. }
  715. if (total_gain < 15)
  716. coef_nb_bits = 13;
  717. else if (total_gain < 32)
  718. coef_nb_bits = 12;
  719. else if (total_gain < 40)
  720. coef_nb_bits = 11;
  721. else if (total_gain < 45)
  722. coef_nb_bits = 10;
  723. else
  724. coef_nb_bits = 9;
  725. /* compute number of coefficients */
  726. n = s->coefs_end[bsize] - s->coefs_start;
  727. for(ch = 0; ch < s->nb_channels; ch++)
  728. nb_coefs[ch] = n;
  729. /* complex coding */
  730. if (s->use_noise_coding) {
  731. for(ch = 0; ch < s->nb_channels; ch++) {
  732. if (s->channel_coded[ch]) {
  733. int i, n, a;
  734. n = s->exponent_high_sizes[bsize];
  735. for(i=0;i<n;i++) {
  736. a = get_bits(&s->gb, 1);
  737. s->high_band_coded[ch][i] = a;
  738. /* if noise coding, the coefficients are not transmitted */
  739. if (a)
  740. nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
  741. }
  742. }
  743. }
  744. for(ch = 0; ch < s->nb_channels; ch++) {
  745. if (s->channel_coded[ch]) {
  746. int i, n, val, code;
  747. n = s->exponent_high_sizes[bsize];
  748. val = (int)0x80000000;
  749. for(i=0;i<n;i++) {
  750. if (s->high_band_coded[ch][i]) {
  751. if (val == (int)0x80000000) {
  752. val = get_bits(&s->gb, 7) - 19;
  753. } else {
  754. code = get_vlc2(&s->gb, s->hgain_vlc.table, HGAINVLCBITS, HGAINMAX);
  755. if (code < 0)
  756. return -1;
  757. val += code - 18;
  758. }
  759. s->high_band_values[ch][i] = val;
  760. }
  761. }
  762. }
  763. }
  764. }
  765. /* exposant can be interpolated in short blocks. */
  766. parse_exponents = 1;
  767. if (s->block_len_bits != s->frame_len_bits) {
  768. parse_exponents = get_bits(&s->gb, 1);
  769. }
  770. if (parse_exponents) {
  771. for(ch = 0; ch < s->nb_channels; ch++) {
  772. if (s->channel_coded[ch]) {
  773. if (s->use_exp_vlc) {
  774. if (decode_exp_vlc(s, ch) < 0)
  775. return -1;
  776. } else {
  777. decode_exp_lsp(s, ch);
  778. }
  779. }
  780. }
  781. } else {
  782. for(ch = 0; ch < s->nb_channels; ch++) {
  783. if (s->channel_coded[ch]) {
  784. interpolate_array(s->exponents[ch], 1 << s->prev_block_len_bits,
  785. s->block_len);
  786. }
  787. }
  788. }
  789. /* parse spectral coefficients : just RLE encoding */
  790. for(ch = 0; ch < s->nb_channels; ch++) {
  791. if (s->channel_coded[ch]) {
  792. VLC *coef_vlc;
  793. int level, run, sign, tindex;
  794. int16_t *ptr, *eptr;
  795. const uint16_t *level_table, *run_table;
  796. /* special VLC tables are used for ms stereo because
  797. there is potentially less energy there */
  798. tindex = (ch == 1 && s->ms_stereo);
  799. coef_vlc = &s->coef_vlc[tindex];
  800. run_table = s->run_table[tindex];
  801. level_table = s->level_table[tindex];
  802. /* XXX: optimize */
  803. ptr = &s->coefs1[ch][0];
  804. eptr = ptr + nb_coefs[ch];
  805. memset(ptr, 0, s->block_len * sizeof(int16_t));
  806. for(;;) {
  807. code = get_vlc2(&s->gb, coef_vlc->table, VLCBITS, VLCMAX);
  808. if (code < 0)
  809. return -1;
  810. if (code == 1) {
  811. /* EOB */
  812. break;
  813. } else if (code == 0) {
  814. /* escape */
  815. level = get_bits(&s->gb, coef_nb_bits);
  816. /* NOTE: this is rather suboptimal. reading
  817. block_len_bits would be better */
  818. run = get_bits(&s->gb, s->frame_len_bits);
  819. } else {
  820. /* normal code */
  821. run = run_table[code];
  822. level = level_table[code];
  823. }
  824. sign = get_bits(&s->gb, 1);
  825. if (!sign)
  826. level = -level;
  827. ptr += run;
  828. if (ptr >= eptr)
  829. {
  830. av_log(NULL, AV_LOG_ERROR, "overflow in spectral RLE, ignoring\n");
  831. break;
  832. }
  833. *ptr++ = level;
  834. /* NOTE: EOB can be omitted */
  835. if (ptr >= eptr)
  836. break;
  837. }
  838. }
  839. if (s->version == 1 && s->nb_channels >= 2) {
  840. align_get_bits(&s->gb);
  841. }
  842. }
  843. /* normalize */
  844. {
  845. int n4 = s->block_len / 2;
  846. mdct_norm = 1.0 / (float)n4;
  847. if (s->version == 1) {
  848. mdct_norm *= sqrt(n4);
  849. }
  850. }
  851. /* finally compute the MDCT coefficients */
  852. for(ch = 0; ch < s->nb_channels; ch++) {
  853. if (s->channel_coded[ch]) {
  854. int16_t *coefs1;
  855. float *coefs, *exponents, mult, mult1, noise, *exp_ptr;
  856. int i, j, n, n1, last_high_band;
  857. float exp_power[HIGH_BAND_MAX_SIZE];
  858. coefs1 = s->coefs1[ch];
  859. exponents = s->exponents[ch];
  860. mult = pow(10, total_gain * 0.05) / s->max_exponent[ch];
  861. mult *= mdct_norm;
  862. coefs = s->coefs[ch];
  863. if (s->use_noise_coding) {
  864. mult1 = mult;
  865. /* very low freqs : noise */
  866. for(i = 0;i < s->coefs_start; i++) {
  867. *coefs++ = s->noise_table[s->noise_index] * (*exponents++) * mult1;
  868. s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
  869. }
  870. n1 = s->exponent_high_sizes[bsize];
  871. /* compute power of high bands */
  872. exp_ptr = exponents +
  873. s->high_band_start[bsize] -
  874. s->coefs_start;
  875. last_high_band = 0; /* avoid warning */
  876. for(j=0;j<n1;j++) {
  877. n = s->exponent_high_bands[s->frame_len_bits -
  878. s->block_len_bits][j];
  879. if (s->high_band_coded[ch][j]) {
  880. float e2, v;
  881. e2 = 0;
  882. for(i = 0;i < n; i++) {
  883. v = exp_ptr[i];
  884. e2 += v * v;
  885. }
  886. exp_power[j] = e2 / n;
  887. last_high_band = j;
  888. tprintf("%d: power=%f (%d)\n", j, exp_power[j], n);
  889. }
  890. exp_ptr += n;
  891. }
  892. /* main freqs and high freqs */
  893. for(j=-1;j<n1;j++) {
  894. if (j < 0) {
  895. n = s->high_band_start[bsize] -
  896. s->coefs_start;
  897. } else {
  898. n = s->exponent_high_bands[s->frame_len_bits -
  899. s->block_len_bits][j];
  900. }
  901. if (j >= 0 && s->high_band_coded[ch][j]) {
  902. /* use noise with specified power */
  903. mult1 = sqrt(exp_power[j] / exp_power[last_high_band]);
  904. /* XXX: use a table */
  905. mult1 = mult1 * pow(10, s->high_band_values[ch][j] * 0.05);
  906. mult1 = mult1 / (s->max_exponent[ch] * s->noise_mult);
  907. mult1 *= mdct_norm;
  908. for(i = 0;i < n; i++) {
  909. noise = s->noise_table[s->noise_index];
  910. s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
  911. *coefs++ = (*exponents++) * noise * mult1;
  912. }
  913. } else {
  914. /* coded values + small noise */
  915. for(i = 0;i < n; i++) {
  916. noise = s->noise_table[s->noise_index];
  917. s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
  918. *coefs++ = ((*coefs1++) + noise) * (*exponents++) * mult;
  919. }
  920. }
  921. }
  922. /* very high freqs : noise */
  923. n = s->block_len - s->coefs_end[bsize];
  924. mult1 = mult * exponents[-1];
  925. for(i = 0; i < n; i++) {
  926. *coefs++ = s->noise_table[s->noise_index] * mult1;
  927. s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
  928. }
  929. } else {
  930. /* XXX: optimize more */
  931. for(i = 0;i < s->coefs_start; i++)
  932. *coefs++ = 0.0;
  933. n = nb_coefs[ch];
  934. for(i = 0;i < n; i++) {
  935. *coefs++ = coefs1[i] * exponents[i] * mult;
  936. }
  937. n = s->block_len - s->coefs_end[bsize];
  938. for(i = 0;i < n; i++)
  939. *coefs++ = 0.0;
  940. }
  941. }
  942. }
  943. #ifdef TRACE
  944. for(ch = 0; ch < s->nb_channels; ch++) {
  945. if (s->channel_coded[ch]) {
  946. dump_floats("exponents", 3, s->exponents[ch], s->block_len);
  947. dump_floats("coefs", 1, s->coefs[ch], s->block_len);
  948. }
  949. }
  950. #endif
  951. if (s->ms_stereo && s->channel_coded[1]) {
  952. float a, b;
  953. int i;
  954. /* nominal case for ms stereo: we do it before mdct */
  955. /* no need to optimize this case because it should almost
  956. never happen */
  957. if (!s->channel_coded[0]) {
  958. tprintf("rare ms-stereo case happened\n");
  959. memset(s->coefs[0], 0, sizeof(float) * s->block_len);
  960. s->channel_coded[0] = 1;
  961. }
  962. for(i = 0; i < s->block_len; i++) {
  963. a = s->coefs[0][i];
  964. b = s->coefs[1][i];
  965. s->coefs[0][i] = a + b;
  966. s->coefs[1][i] = a - b;
  967. }
  968. }
  969. /* build the window : we ensure that when the windows overlap
  970. their squared sum is always 1 (MDCT reconstruction rule) */
  971. /* XXX: merge with output */
  972. {
  973. int i, next_block_len, block_len, prev_block_len, n;
  974. float *wptr;
  975. block_len = s->block_len;
  976. prev_block_len = 1 << s->prev_block_len_bits;
  977. next_block_len = 1 << s->next_block_len_bits;
  978. /* right part */
  979. wptr = s->window + block_len;
  980. if (block_len <= next_block_len) {
  981. for(i=0;i<block_len;i++)
  982. *wptr++ = s->windows[bsize][i];
  983. } else {
  984. /* overlap */
  985. n = (block_len / 2) - (next_block_len / 2);
  986. for(i=0;i<n;i++)
  987. *wptr++ = 1.0;
  988. for(i=0;i<next_block_len;i++)
  989. *wptr++ = s->windows[s->frame_len_bits - s->next_block_len_bits][i];
  990. for(i=0;i<n;i++)
  991. *wptr++ = 0.0;
  992. }
  993. /* left part */
  994. wptr = s->window + block_len;
  995. if (block_len <= prev_block_len) {
  996. for(i=0;i<block_len;i++)
  997. *--wptr = s->windows[bsize][i];
  998. } else {
  999. /* overlap */
  1000. n = (block_len / 2) - (prev_block_len / 2);
  1001. for(i=0;i<n;i++)
  1002. *--wptr = 1.0;
  1003. for(i=0;i<prev_block_len;i++)
  1004. *--wptr = s->windows[s->frame_len_bits - s->prev_block_len_bits][i];
  1005. for(i=0;i<n;i++)
  1006. *--wptr = 0.0;
  1007. }
  1008. }
  1009. for(ch = 0; ch < s->nb_channels; ch++) {
  1010. if (s->channel_coded[ch]) {
  1011. float *ptr;
  1012. int n4, index, n;
  1013. n = s->block_len;
  1014. n4 = s->block_len / 2;
  1015. s->mdct_ctx[bsize].fft.imdct_calc(&s->mdct_ctx[bsize],
  1016. s->output, s->coefs[ch], s->mdct_tmp);
  1017. /* XXX: optimize all that by build the window and
  1018. multipying/adding at the same time */
  1019. /* multiply by the window and add in the frame */
  1020. index = (s->frame_len / 2) + s->block_pos - n4;
  1021. ptr = &s->frame_out[ch][index];
  1022. s->dsp.vector_fmul_add_add(ptr,s->window,s->output,ptr,0,2*n,1);
  1023. /* specific fast case for ms-stereo : add to second
  1024. channel if it is not coded */
  1025. if (s->ms_stereo && !s->channel_coded[1]) {
  1026. ptr = &s->frame_out[1][index];
  1027. s->dsp.vector_fmul_add_add(ptr,s->window,s->output,ptr,0,2*n,1);
  1028. }
  1029. }
  1030. }
  1031. next:
  1032. /* update block number */
  1033. s->block_num++;
  1034. s->block_pos += s->block_len;
  1035. if (s->block_pos >= s->frame_len)
  1036. return 1;
  1037. else
  1038. return 0;
  1039. }
  1040. /* decode a frame of frame_len samples */
  1041. static int wma_decode_frame(WMADecodeContext *s, int16_t *samples)
  1042. {
  1043. int ret, i, n, a, ch, incr;
  1044. int16_t *ptr;
  1045. float *iptr;
  1046. #ifdef TRACE
  1047. tprintf("***decode_frame: %d size=%d\n", s->frame_count++, s->frame_len);
  1048. #endif
  1049. /* read each block */
  1050. s->block_num = 0;
  1051. s->block_pos = 0;
  1052. for(;;) {
  1053. ret = wma_decode_block(s);
  1054. if (ret < 0)
  1055. return -1;
  1056. if (ret)
  1057. break;
  1058. }
  1059. /* convert frame to integer */
  1060. n = s->frame_len;
  1061. incr = s->nb_channels;
  1062. for(ch = 0; ch < s->nb_channels; ch++) {
  1063. ptr = samples + ch;
  1064. iptr = s->frame_out[ch];
  1065. for(i=0;i<n;i++) {
  1066. a = lrintf(*iptr++);
  1067. if (a > 32767)
  1068. a = 32767;
  1069. else if (a < -32768)
  1070. a = -32768;
  1071. *ptr = a;
  1072. ptr += incr;
  1073. }
  1074. /* prepare for next block */
  1075. memmove(&s->frame_out[ch][0], &s->frame_out[ch][s->frame_len],
  1076. s->frame_len * sizeof(float));
  1077. /* XXX: suppress this */
  1078. memset(&s->frame_out[ch][s->frame_len], 0,
  1079. s->frame_len * sizeof(float));
  1080. }
  1081. #ifdef TRACE
  1082. dump_shorts("samples", samples, n * s->nb_channels);
  1083. #endif
  1084. return 0;
  1085. }
  1086. static int wma_decode_superframe(AVCodecContext *avctx,
  1087. void *data, int *data_size,
  1088. uint8_t *buf, int buf_size)
  1089. {
  1090. WMADecodeContext *s = avctx->priv_data;
  1091. int nb_frames, bit_offset, i, pos, len;
  1092. uint8_t *q;
  1093. int16_t *samples;
  1094. tprintf("***decode_superframe:\n");
  1095. if(buf_size==0){
  1096. s->last_superframe_len = 0;
  1097. return 0;
  1098. }
  1099. samples = data;
  1100. init_get_bits(&s->gb, buf, buf_size*8);
  1101. if (s->use_bit_reservoir) {
  1102. /* read super frame header */
  1103. get_bits(&s->gb, 4); /* super frame index */
  1104. nb_frames = get_bits(&s->gb, 4) - 1;
  1105. bit_offset = get_bits(&s->gb, s->byte_offset_bits + 3);
  1106. if (s->last_superframe_len > 0) {
  1107. // printf("skip=%d\n", s->last_bitoffset);
  1108. /* add bit_offset bits to last frame */
  1109. if ((s->last_superframe_len + ((bit_offset + 7) >> 3)) >
  1110. MAX_CODED_SUPERFRAME_SIZE)
  1111. goto fail;
  1112. q = s->last_superframe + s->last_superframe_len;
  1113. len = bit_offset;
  1114. while (len > 7) {
  1115. *q++ = (get_bits)(&s->gb, 8);
  1116. len -= 8;
  1117. }
  1118. if (len > 0) {
  1119. *q++ = (get_bits)(&s->gb, len) << (8 - len);
  1120. }
  1121. /* XXX: bit_offset bits into last frame */
  1122. init_get_bits(&s->gb, s->last_superframe, MAX_CODED_SUPERFRAME_SIZE*8);
  1123. /* skip unused bits */
  1124. if (s->last_bitoffset > 0)
  1125. skip_bits(&s->gb, s->last_bitoffset);
  1126. /* this frame is stored in the last superframe and in the
  1127. current one */
  1128. if (wma_decode_frame(s, samples) < 0)
  1129. goto fail;
  1130. samples += s->nb_channels * s->frame_len;
  1131. }
  1132. /* read each frame starting from bit_offset */
  1133. pos = bit_offset + 4 + 4 + s->byte_offset_bits + 3;
  1134. init_get_bits(&s->gb, buf + (pos >> 3), (MAX_CODED_SUPERFRAME_SIZE - (pos >> 3))*8);
  1135. len = pos & 7;
  1136. if (len > 0)
  1137. skip_bits(&s->gb, len);
  1138. s->reset_block_lengths = 1;
  1139. for(i=0;i<nb_frames;i++) {
  1140. if (wma_decode_frame(s, samples) < 0)
  1141. goto fail;
  1142. samples += s->nb_channels * s->frame_len;
  1143. }
  1144. /* we copy the end of the frame in the last frame buffer */
  1145. pos = get_bits_count(&s->gb) + ((bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7);
  1146. s->last_bitoffset = pos & 7;
  1147. pos >>= 3;
  1148. len = buf_size - pos;
  1149. if (len > MAX_CODED_SUPERFRAME_SIZE || len < 0) {
  1150. goto fail;
  1151. }
  1152. s->last_superframe_len = len;
  1153. memcpy(s->last_superframe, buf + pos, len);
  1154. } else {
  1155. /* single frame decode */
  1156. if (wma_decode_frame(s, samples) < 0)
  1157. goto fail;
  1158. samples += s->nb_channels * s->frame_len;
  1159. }
  1160. *data_size = (int8_t *)samples - (int8_t *)data;
  1161. return s->block_align;
  1162. fail:
  1163. /* when error, we reset the bit reservoir */
  1164. s->last_superframe_len = 0;
  1165. return -1;
  1166. }
  1167. static int wma_decode_end(AVCodecContext *avctx)
  1168. {
  1169. WMADecodeContext *s = avctx->priv_data;
  1170. int i;
  1171. for(i = 0; i < s->nb_block_sizes; i++)
  1172. ff_mdct_end(&s->mdct_ctx[i]);
  1173. for(i = 0; i < s->nb_block_sizes; i++)
  1174. av_free(s->windows[i]);
  1175. if (s->use_exp_vlc) {
  1176. free_vlc(&s->exp_vlc);
  1177. }
  1178. if (s->use_noise_coding) {
  1179. free_vlc(&s->hgain_vlc);
  1180. }
  1181. for(i = 0;i < 2; i++) {
  1182. free_vlc(&s->coef_vlc[i]);
  1183. av_free(s->run_table[i]);
  1184. av_free(s->level_table[i]);
  1185. }
  1186. return 0;
  1187. }
  1188. AVCodec wmav1_decoder =
  1189. {
  1190. "wmav1",
  1191. CODEC_TYPE_AUDIO,
  1192. CODEC_ID_WMAV1,
  1193. sizeof(WMADecodeContext),
  1194. wma_decode_init,
  1195. NULL,
  1196. wma_decode_end,
  1197. wma_decode_superframe,
  1198. };
  1199. AVCodec wmav2_decoder =
  1200. {
  1201. "wmav2",
  1202. CODEC_TYPE_AUDIO,
  1203. CODEC_ID_WMAV2,
  1204. sizeof(WMADecodeContext),
  1205. wma_decode_init,
  1206. NULL,
  1207. wma_decode_end,
  1208. wma_decode_superframe,
  1209. };