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.

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