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.

1335 lines
42KB

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