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.

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