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.

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