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