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.

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