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.

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