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