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.

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