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.

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