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.

912 lines
28KB

  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 "wma.h"
  36. #undef NDEBUG
  37. #include <assert.h>
  38. #define EXPVLCBITS 8
  39. #define EXPMAX ((19+EXPVLCBITS-1)/EXPVLCBITS)
  40. #define HGAINVLCBITS 9
  41. #define HGAINMAX ((13+HGAINVLCBITS-1)/HGAINVLCBITS)
  42. static void wma_lsp_to_curve_init(WMACodecContext *s, int frame_len);
  43. #ifdef TRACE
  44. static void dump_shorts(WMADecodeContext *s, const char *name, const short *tab, int n)
  45. {
  46. int i;
  47. tprintf(s->avctx, "%s[%d]:\n", name, n);
  48. for(i=0;i<n;i++) {
  49. if ((i & 7) == 0)
  50. tprintf(s->avctx, "%4d: ", i);
  51. tprintf(s->avctx, " %5d.0", tab[i]);
  52. if ((i & 7) == 7)
  53. tprintf(s->avctx, "\n");
  54. }
  55. }
  56. static void dump_floats(WMADecodeContext *s, const char *name, int prec, const float *tab, int n)
  57. {
  58. int i;
  59. tprintf(s->avctx, "%s[%d]:\n", name, n);
  60. for(i=0;i<n;i++) {
  61. if ((i & 7) == 0)
  62. tprintf(s->avctx, "%4d: ", i);
  63. tprintf(s->avctx, " %8.*f", prec, tab[i]);
  64. if ((i & 7) == 7)
  65. tprintf(s->avctx, "\n");
  66. }
  67. if ((i & 7) != 0)
  68. tprintf(s->avctx, "\n");
  69. }
  70. #endif
  71. static int wma_decode_init(AVCodecContext * avctx)
  72. {
  73. WMACodecContext *s = avctx->priv_data;
  74. int i, flags1, flags2;
  75. uint8_t *extradata;
  76. s->avctx = avctx;
  77. /* extract flag infos */
  78. flags1 = 0;
  79. flags2 = 0;
  80. extradata = avctx->extradata;
  81. if (avctx->codec->id == CODEC_ID_WMAV1 && avctx->extradata_size >= 4) {
  82. flags1 = extradata[0] | (extradata[1] << 8);
  83. flags2 = extradata[2] | (extradata[3] << 8);
  84. } else if (avctx->codec->id == CODEC_ID_WMAV2 && avctx->extradata_size >= 6) {
  85. flags1 = extradata[0] | (extradata[1] << 8) |
  86. (extradata[2] << 16) | (extradata[3] << 24);
  87. flags2 = extradata[4] | (extradata[5] << 8);
  88. }
  89. // for(i=0; i<avctx->extradata_size; i++)
  90. // av_log(NULL, AV_LOG_ERROR, "%02X ", extradata[i]);
  91. s->use_exp_vlc = flags2 & 0x0001;
  92. s->use_bit_reservoir = flags2 & 0x0002;
  93. s->use_variable_block_len = flags2 & 0x0004;
  94. ff_wma_init(avctx, flags2);
  95. /* init MDCT */
  96. for(i = 0; i < s->nb_block_sizes; i++)
  97. ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 1);
  98. if (s->use_noise_coding) {
  99. init_vlc(&s->hgain_vlc, HGAINVLCBITS, sizeof(ff_wma_hgain_huffbits),
  100. ff_wma_hgain_huffbits, 1, 1,
  101. ff_wma_hgain_huffcodes, 2, 2, 0);
  102. }
  103. if (s->use_exp_vlc) {
  104. init_vlc(&s->exp_vlc, EXPVLCBITS, sizeof(ff_wma_scale_huffbits), //FIXME move out of context
  105. ff_wma_scale_huffbits, 1, 1,
  106. ff_wma_scale_huffcodes, 4, 4, 0);
  107. } else {
  108. wma_lsp_to_curve_init(s, s->frame_len);
  109. }
  110. return 0;
  111. }
  112. /**
  113. * interpolate values for a bigger or smaller block. The block must
  114. * have multiple sizes
  115. */
  116. static void interpolate_array(float *scale, int old_size, int new_size)
  117. {
  118. int i, j, jincr, k;
  119. float v;
  120. if (new_size > old_size) {
  121. jincr = new_size / old_size;
  122. j = new_size;
  123. for(i = old_size - 1; i >=0; i--) {
  124. v = scale[i];
  125. k = jincr;
  126. do {
  127. scale[--j] = v;
  128. } while (--k);
  129. }
  130. } else if (new_size < old_size) {
  131. j = 0;
  132. jincr = old_size / new_size;
  133. for(i = 0; i < new_size; i++) {
  134. scale[i] = scale[j];
  135. j += jincr;
  136. }
  137. }
  138. }
  139. /**
  140. * compute x^-0.25 with an exponent and mantissa table. We use linear
  141. * interpolation to reduce the mantissa table size at a small speed
  142. * expense (linear interpolation approximately doubles the number of
  143. * bits of precision).
  144. */
  145. static inline float pow_m1_4(WMACodecContext *s, float x)
  146. {
  147. union {
  148. float f;
  149. unsigned int v;
  150. } u, t;
  151. unsigned int e, m;
  152. float a, b;
  153. u.f = x;
  154. e = u.v >> 23;
  155. m = (u.v >> (23 - LSP_POW_BITS)) & ((1 << LSP_POW_BITS) - 1);
  156. /* build interpolation scale: 1 <= t < 2. */
  157. t.v = ((u.v << LSP_POW_BITS) & ((1 << 23) - 1)) | (127 << 23);
  158. a = s->lsp_pow_m_table1[m];
  159. b = s->lsp_pow_m_table2[m];
  160. return s->lsp_pow_e_table[e] * (a + b * t.f);
  161. }
  162. static void wma_lsp_to_curve_init(WMACodecContext *s, int frame_len)
  163. {
  164. float wdel, a, b;
  165. int i, e, m;
  166. wdel = M_PI / frame_len;
  167. for(i=0;i<frame_len;i++)
  168. s->lsp_cos_table[i] = 2.0f * cos(wdel * i);
  169. /* tables for x^-0.25 computation */
  170. for(i=0;i<256;i++) {
  171. e = i - 126;
  172. s->lsp_pow_e_table[i] = pow(2.0, e * -0.25);
  173. }
  174. /* NOTE: these two tables are needed to avoid two operations in
  175. pow_m1_4 */
  176. b = 1.0;
  177. for(i=(1 << LSP_POW_BITS) - 1;i>=0;i--) {
  178. m = (1 << LSP_POW_BITS) + i;
  179. a = (float)m * (0.5 / (1 << LSP_POW_BITS));
  180. a = pow(a, -0.25);
  181. s->lsp_pow_m_table1[i] = 2 * a - b;
  182. s->lsp_pow_m_table2[i] = b - a;
  183. b = a;
  184. }
  185. #if 0
  186. for(i=1;i<20;i++) {
  187. float v, r1, r2;
  188. v = 5.0 / i;
  189. r1 = pow_m1_4(s, v);
  190. r2 = pow(v,-0.25);
  191. printf("%f^-0.25=%f e=%f\n", v, r1, r2 - r1);
  192. }
  193. #endif
  194. }
  195. /**
  196. * NOTE: We use the same code as Vorbis here
  197. * @todo optimize it further with SSE/3Dnow
  198. */
  199. static void wma_lsp_to_curve(WMACodecContext *s,
  200. float *out, float *val_max_ptr,
  201. int n, float *lsp)
  202. {
  203. int i, j;
  204. float p, q, w, v, val_max;
  205. val_max = 0;
  206. for(i=0;i<n;i++) {
  207. p = 0.5f;
  208. q = 0.5f;
  209. w = s->lsp_cos_table[i];
  210. for(j=1;j<NB_LSP_COEFS;j+=2){
  211. q *= w - lsp[j - 1];
  212. p *= w - lsp[j];
  213. }
  214. p *= p * (2.0f - w);
  215. q *= q * (2.0f + w);
  216. v = p + q;
  217. v = pow_m1_4(s, v);
  218. if (v > val_max)
  219. val_max = v;
  220. out[i] = v;
  221. }
  222. *val_max_ptr = val_max;
  223. }
  224. /**
  225. * decode exponents coded with LSP coefficients (same idea as Vorbis)
  226. */
  227. static void decode_exp_lsp(WMACodecContext *s, int ch)
  228. {
  229. float lsp_coefs[NB_LSP_COEFS];
  230. int val, i;
  231. for(i = 0; i < NB_LSP_COEFS; i++) {
  232. if (i == 0 || i >= 8)
  233. val = get_bits(&s->gb, 3);
  234. else
  235. val = get_bits(&s->gb, 4);
  236. lsp_coefs[i] = ff_wma_lsp_codebook[i][val];
  237. }
  238. wma_lsp_to_curve(s, s->exponents[ch], &s->max_exponent[ch],
  239. s->block_len, lsp_coefs);
  240. }
  241. /**
  242. * decode exponents coded with VLC codes
  243. */
  244. static int decode_exp_vlc(WMACodecContext *s, int ch)
  245. {
  246. int last_exp, n, code;
  247. const uint16_t *ptr, *band_ptr;
  248. float v, *q, max_scale, *q_end;
  249. band_ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
  250. ptr = band_ptr;
  251. q = s->exponents[ch];
  252. q_end = q + s->block_len;
  253. max_scale = 0;
  254. if (s->version == 1) {
  255. last_exp = get_bits(&s->gb, 5) + 10;
  256. /* XXX: use a table */
  257. v = pow(10, last_exp * (1.0 / 16.0));
  258. max_scale = v;
  259. n = *ptr++;
  260. do {
  261. *q++ = v;
  262. } while (--n);
  263. }else
  264. last_exp = 36;
  265. while (q < q_end) {
  266. code = get_vlc2(&s->gb, s->exp_vlc.table, EXPVLCBITS, EXPMAX);
  267. if (code < 0)
  268. return -1;
  269. /* NOTE: this offset is the same as MPEG4 AAC ! */
  270. last_exp += code - 60;
  271. /* XXX: use a table */
  272. v = pow(10, last_exp * (1.0 / 16.0));
  273. if (v > max_scale)
  274. max_scale = v;
  275. n = *ptr++;
  276. do {
  277. *q++ = v;
  278. } while (--n);
  279. }
  280. s->max_exponent[ch] = max_scale;
  281. return 0;
  282. }
  283. /**
  284. * @return 0 if OK. 1 if last block of frame. return -1 if
  285. * unrecorrable error.
  286. */
  287. static int wma_decode_block(WMACodecContext *s)
  288. {
  289. int n, v, a, ch, code, bsize;
  290. int coef_nb_bits, total_gain, parse_exponents;
  291. int nb_coefs[MAX_CHANNELS];
  292. float mdct_norm;
  293. #ifdef TRACE
  294. tprintf(s->avctx, "***decode_block: %d:%d\n", s->frame_count - 1, s->block_num);
  295. #endif
  296. /* compute current block length */
  297. if (s->use_variable_block_len) {
  298. n = av_log2(s->nb_block_sizes - 1) + 1;
  299. if (s->reset_block_lengths) {
  300. s->reset_block_lengths = 0;
  301. v = get_bits(&s->gb, n);
  302. if (v >= s->nb_block_sizes)
  303. return -1;
  304. s->prev_block_len_bits = s->frame_len_bits - v;
  305. v = get_bits(&s->gb, n);
  306. if (v >= s->nb_block_sizes)
  307. return -1;
  308. s->block_len_bits = s->frame_len_bits - v;
  309. } else {
  310. /* update block lengths */
  311. s->prev_block_len_bits = s->block_len_bits;
  312. s->block_len_bits = s->next_block_len_bits;
  313. }
  314. v = get_bits(&s->gb, n);
  315. if (v >= s->nb_block_sizes)
  316. return -1;
  317. s->next_block_len_bits = s->frame_len_bits - v;
  318. } else {
  319. /* fixed block len */
  320. s->next_block_len_bits = s->frame_len_bits;
  321. s->prev_block_len_bits = s->frame_len_bits;
  322. s->block_len_bits = s->frame_len_bits;
  323. }
  324. /* now check if the block length is coherent with the frame length */
  325. s->block_len = 1 << s->block_len_bits;
  326. if ((s->block_pos + s->block_len) > s->frame_len)
  327. return -1;
  328. if (s->nb_channels == 2) {
  329. s->ms_stereo = get_bits(&s->gb, 1);
  330. }
  331. v = 0;
  332. for(ch = 0; ch < s->nb_channels; ch++) {
  333. a = get_bits(&s->gb, 1);
  334. s->channel_coded[ch] = a;
  335. v |= a;
  336. }
  337. /* if no channel coded, no need to go further */
  338. /* XXX: fix potential framing problems */
  339. if (!v)
  340. goto next;
  341. bsize = s->frame_len_bits - s->block_len_bits;
  342. /* read total gain and extract corresponding number of bits for
  343. coef escape coding */
  344. total_gain = 1;
  345. for(;;) {
  346. a = get_bits(&s->gb, 7);
  347. total_gain += a;
  348. if (a != 127)
  349. break;
  350. }
  351. coef_nb_bits= ff_wma_total_gain_to_bits(total_gain);
  352. /* compute number of coefficients */
  353. n = s->coefs_end[bsize] - s->coefs_start;
  354. for(ch = 0; ch < s->nb_channels; ch++)
  355. nb_coefs[ch] = n;
  356. /* complex coding */
  357. if (s->use_noise_coding) {
  358. for(ch = 0; ch < s->nb_channels; ch++) {
  359. if (s->channel_coded[ch]) {
  360. int i, n, a;
  361. n = s->exponent_high_sizes[bsize];
  362. for(i=0;i<n;i++) {
  363. a = get_bits(&s->gb, 1);
  364. s->high_band_coded[ch][i] = a;
  365. /* if noise coding, the coefficients are not transmitted */
  366. if (a)
  367. nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
  368. }
  369. }
  370. }
  371. for(ch = 0; ch < s->nb_channels; ch++) {
  372. if (s->channel_coded[ch]) {
  373. int i, n, val, code;
  374. n = s->exponent_high_sizes[bsize];
  375. val = (int)0x80000000;
  376. for(i=0;i<n;i++) {
  377. if (s->high_band_coded[ch][i]) {
  378. if (val == (int)0x80000000) {
  379. val = get_bits(&s->gb, 7) - 19;
  380. } else {
  381. code = get_vlc2(&s->gb, s->hgain_vlc.table, HGAINVLCBITS, HGAINMAX);
  382. if (code < 0)
  383. return -1;
  384. val += code - 18;
  385. }
  386. s->high_band_values[ch][i] = val;
  387. }
  388. }
  389. }
  390. }
  391. }
  392. /* exposant can be interpolated in short blocks. */
  393. parse_exponents = 1;
  394. if (s->block_len_bits != s->frame_len_bits) {
  395. parse_exponents = get_bits(&s->gb, 1);
  396. }
  397. if (parse_exponents) {
  398. for(ch = 0; ch < s->nb_channels; ch++) {
  399. if (s->channel_coded[ch]) {
  400. if (s->use_exp_vlc) {
  401. if (decode_exp_vlc(s, ch) < 0)
  402. return -1;
  403. } else {
  404. decode_exp_lsp(s, ch);
  405. }
  406. }
  407. }
  408. } else {
  409. for(ch = 0; ch < s->nb_channels; ch++) {
  410. if (s->channel_coded[ch]) {
  411. interpolate_array(s->exponents[ch], 1 << s->prev_block_len_bits,
  412. s->block_len);
  413. }
  414. }
  415. }
  416. /* parse spectral coefficients : just RLE encoding */
  417. for(ch = 0; ch < s->nb_channels; ch++) {
  418. if (s->channel_coded[ch]) {
  419. VLC *coef_vlc;
  420. int level, run, sign, tindex;
  421. int16_t *ptr, *eptr;
  422. const uint16_t *level_table, *run_table;
  423. /* special VLC tables are used for ms stereo because
  424. there is potentially less energy there */
  425. tindex = (ch == 1 && s->ms_stereo);
  426. coef_vlc = &s->coef_vlc[tindex];
  427. run_table = s->run_table[tindex];
  428. level_table = s->level_table[tindex];
  429. /* XXX: optimize */
  430. ptr = &s->coefs1[ch][0];
  431. eptr = ptr + nb_coefs[ch];
  432. memset(ptr, 0, s->block_len * sizeof(int16_t));
  433. for(;;) {
  434. code = get_vlc2(&s->gb, coef_vlc->table, VLCBITS, VLCMAX);
  435. if (code < 0)
  436. return -1;
  437. if (code == 1) {
  438. /* EOB */
  439. break;
  440. } else if (code == 0) {
  441. /* escape */
  442. level = get_bits(&s->gb, coef_nb_bits);
  443. /* NOTE: this is rather suboptimal. reading
  444. block_len_bits would be better */
  445. run = get_bits(&s->gb, s->frame_len_bits);
  446. } else {
  447. /* normal code */
  448. run = run_table[code];
  449. level = level_table[code];
  450. }
  451. sign = get_bits(&s->gb, 1);
  452. if (!sign)
  453. level = -level;
  454. ptr += run;
  455. if (ptr >= eptr)
  456. {
  457. av_log(NULL, AV_LOG_ERROR, "overflow in spectral RLE, ignoring\n");
  458. break;
  459. }
  460. *ptr++ = level;
  461. /* NOTE: EOB can be omitted */
  462. if (ptr >= eptr)
  463. break;
  464. }
  465. }
  466. if (s->version == 1 && s->nb_channels >= 2) {
  467. align_get_bits(&s->gb);
  468. }
  469. }
  470. /* normalize */
  471. {
  472. int n4 = s->block_len / 2;
  473. mdct_norm = 1.0 / (float)n4;
  474. if (s->version == 1) {
  475. mdct_norm *= sqrt(n4);
  476. }
  477. }
  478. /* finally compute the MDCT coefficients */
  479. for(ch = 0; ch < s->nb_channels; ch++) {
  480. if (s->channel_coded[ch]) {
  481. int16_t *coefs1;
  482. float *coefs, *exponents, mult, mult1, noise, *exp_ptr;
  483. int i, j, n, n1, last_high_band;
  484. float exp_power[HIGH_BAND_MAX_SIZE];
  485. coefs1 = s->coefs1[ch];
  486. exponents = s->exponents[ch];
  487. mult = pow(10, total_gain * 0.05) / s->max_exponent[ch];
  488. mult *= mdct_norm;
  489. coefs = s->coefs[ch];
  490. if (s->use_noise_coding) {
  491. mult1 = mult;
  492. /* very low freqs : noise */
  493. for(i = 0;i < s->coefs_start; i++) {
  494. *coefs++ = s->noise_table[s->noise_index] * (*exponents++) * mult1;
  495. s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
  496. }
  497. n1 = s->exponent_high_sizes[bsize];
  498. /* compute power of high bands */
  499. exp_ptr = exponents +
  500. s->high_band_start[bsize] -
  501. s->coefs_start;
  502. last_high_band = 0; /* avoid warning */
  503. for(j=0;j<n1;j++) {
  504. n = s->exponent_high_bands[s->frame_len_bits -
  505. s->block_len_bits][j];
  506. if (s->high_band_coded[ch][j]) {
  507. float e2, v;
  508. e2 = 0;
  509. for(i = 0;i < n; i++) {
  510. v = exp_ptr[i];
  511. e2 += v * v;
  512. }
  513. exp_power[j] = e2 / n;
  514. last_high_band = j;
  515. tprintf(s->avctx, "%d: power=%f (%d)\n", j, exp_power[j], n);
  516. }
  517. exp_ptr += n;
  518. }
  519. /* main freqs and high freqs */
  520. for(j=-1;j<n1;j++) {
  521. if (j < 0) {
  522. n = s->high_band_start[bsize] -
  523. s->coefs_start;
  524. } else {
  525. n = s->exponent_high_bands[s->frame_len_bits -
  526. s->block_len_bits][j];
  527. }
  528. if (j >= 0 && s->high_band_coded[ch][j]) {
  529. /* use noise with specified power */
  530. mult1 = sqrt(exp_power[j] / exp_power[last_high_band]);
  531. /* XXX: use a table */
  532. mult1 = mult1 * pow(10, s->high_band_values[ch][j] * 0.05);
  533. mult1 = mult1 / (s->max_exponent[ch] * s->noise_mult);
  534. mult1 *= mdct_norm;
  535. for(i = 0;i < n; i++) {
  536. noise = s->noise_table[s->noise_index];
  537. s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
  538. *coefs++ = (*exponents++) * noise * mult1;
  539. }
  540. } else {
  541. /* coded values + small noise */
  542. for(i = 0;i < n; i++) {
  543. noise = s->noise_table[s->noise_index];
  544. s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
  545. *coefs++ = ((*coefs1++) + noise) * (*exponents++) * mult;
  546. }
  547. }
  548. }
  549. /* very high freqs : noise */
  550. n = s->block_len - s->coefs_end[bsize];
  551. mult1 = mult * exponents[-1];
  552. for(i = 0; i < n; i++) {
  553. *coefs++ = s->noise_table[s->noise_index] * mult1;
  554. s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
  555. }
  556. } else {
  557. /* XXX: optimize more */
  558. for(i = 0;i < s->coefs_start; i++)
  559. *coefs++ = 0.0;
  560. n = nb_coefs[ch];
  561. for(i = 0;i < n; i++) {
  562. *coefs++ = coefs1[i] * exponents[i] * mult;
  563. }
  564. n = s->block_len - s->coefs_end[bsize];
  565. for(i = 0;i < n; i++)
  566. *coefs++ = 0.0;
  567. }
  568. }
  569. }
  570. #ifdef TRACE
  571. for(ch = 0; ch < s->nb_channels; ch++) {
  572. if (s->channel_coded[ch]) {
  573. dump_floats(s, "exponents", 3, s->exponents[ch], s->block_len);
  574. dump_floats(s, "coefs", 1, s->coefs[ch], s->block_len);
  575. }
  576. }
  577. #endif
  578. if (s->ms_stereo && s->channel_coded[1]) {
  579. float a, b;
  580. int i;
  581. /* nominal case for ms stereo: we do it before mdct */
  582. /* no need to optimize this case because it should almost
  583. never happen */
  584. if (!s->channel_coded[0]) {
  585. tprintf(s->avctx, "rare ms-stereo case happened\n");
  586. memset(s->coefs[0], 0, sizeof(float) * s->block_len);
  587. s->channel_coded[0] = 1;
  588. }
  589. for(i = 0; i < s->block_len; i++) {
  590. a = s->coefs[0][i];
  591. b = s->coefs[1][i];
  592. s->coefs[0][i] = a + b;
  593. s->coefs[1][i] = a - b;
  594. }
  595. }
  596. /* build the window : we ensure that when the windows overlap
  597. their squared sum is always 1 (MDCT reconstruction rule) */
  598. /* XXX: merge with output */
  599. {
  600. int i, next_block_len, block_len, prev_block_len, n;
  601. float *wptr;
  602. block_len = s->block_len;
  603. prev_block_len = 1 << s->prev_block_len_bits;
  604. next_block_len = 1 << s->next_block_len_bits;
  605. /* right part */
  606. wptr = s->window + block_len;
  607. if (block_len <= next_block_len) {
  608. for(i=0;i<block_len;i++)
  609. *wptr++ = s->windows[bsize][i];
  610. } else {
  611. /* overlap */
  612. n = (block_len / 2) - (next_block_len / 2);
  613. for(i=0;i<n;i++)
  614. *wptr++ = 1.0;
  615. for(i=0;i<next_block_len;i++)
  616. *wptr++ = s->windows[s->frame_len_bits - s->next_block_len_bits][i];
  617. for(i=0;i<n;i++)
  618. *wptr++ = 0.0;
  619. }
  620. /* left part */
  621. wptr = s->window + block_len;
  622. if (block_len <= prev_block_len) {
  623. for(i=0;i<block_len;i++)
  624. *--wptr = s->windows[bsize][i];
  625. } else {
  626. /* overlap */
  627. n = (block_len / 2) - (prev_block_len / 2);
  628. for(i=0;i<n;i++)
  629. *--wptr = 1.0;
  630. for(i=0;i<prev_block_len;i++)
  631. *--wptr = s->windows[s->frame_len_bits - s->prev_block_len_bits][i];
  632. for(i=0;i<n;i++)
  633. *--wptr = 0.0;
  634. }
  635. }
  636. for(ch = 0; ch < s->nb_channels; ch++) {
  637. if (s->channel_coded[ch]) {
  638. float *ptr;
  639. int n4, index, n;
  640. n = s->block_len;
  641. n4 = s->block_len / 2;
  642. s->mdct_ctx[bsize].fft.imdct_calc(&s->mdct_ctx[bsize],
  643. s->output, s->coefs[ch], s->mdct_tmp);
  644. /* XXX: optimize all that by build the window and
  645. multipying/adding at the same time */
  646. /* multiply by the window and add in the frame */
  647. index = (s->frame_len / 2) + s->block_pos - n4;
  648. ptr = &s->frame_out[ch][index];
  649. s->dsp.vector_fmul_add_add(ptr,s->window,s->output,ptr,0,2*n,1);
  650. /* specific fast case for ms-stereo : add to second
  651. channel if it is not coded */
  652. if (s->ms_stereo && !s->channel_coded[1]) {
  653. ptr = &s->frame_out[1][index];
  654. s->dsp.vector_fmul_add_add(ptr,s->window,s->output,ptr,0,2*n,1);
  655. }
  656. }
  657. }
  658. next:
  659. /* update block number */
  660. s->block_num++;
  661. s->block_pos += s->block_len;
  662. if (s->block_pos >= s->frame_len)
  663. return 1;
  664. else
  665. return 0;
  666. }
  667. /* decode a frame of frame_len samples */
  668. static int wma_decode_frame(WMACodecContext *s, int16_t *samples)
  669. {
  670. int ret, i, n, a, ch, incr;
  671. int16_t *ptr;
  672. float *iptr;
  673. #ifdef TRACE
  674. tprintf(s->avctx, "***decode_frame: %d size=%d\n", s->frame_count++, s->frame_len);
  675. #endif
  676. /* read each block */
  677. s->block_num = 0;
  678. s->block_pos = 0;
  679. for(;;) {
  680. ret = wma_decode_block(s);
  681. if (ret < 0)
  682. return -1;
  683. if (ret)
  684. break;
  685. }
  686. /* convert frame to integer */
  687. n = s->frame_len;
  688. incr = s->nb_channels;
  689. for(ch = 0; ch < s->nb_channels; ch++) {
  690. ptr = samples + ch;
  691. iptr = s->frame_out[ch];
  692. for(i=0;i<n;i++) {
  693. a = lrintf(*iptr++);
  694. if (a > 32767)
  695. a = 32767;
  696. else if (a < -32768)
  697. a = -32768;
  698. *ptr = a;
  699. ptr += incr;
  700. }
  701. /* prepare for next block */
  702. memmove(&s->frame_out[ch][0], &s->frame_out[ch][s->frame_len],
  703. s->frame_len * sizeof(float));
  704. /* XXX: suppress this */
  705. memset(&s->frame_out[ch][s->frame_len], 0,
  706. s->frame_len * sizeof(float));
  707. }
  708. #ifdef TRACE
  709. dump_shorts(s, "samples", samples, n * s->nb_channels);
  710. #endif
  711. return 0;
  712. }
  713. static int wma_decode_superframe(AVCodecContext *avctx,
  714. void *data, int *data_size,
  715. uint8_t *buf, int buf_size)
  716. {
  717. WMACodecContext *s = avctx->priv_data;
  718. int nb_frames, bit_offset, i, pos, len;
  719. uint8_t *q;
  720. int16_t *samples;
  721. tprintf(avctx, "***decode_superframe:\n");
  722. if(buf_size==0){
  723. s->last_superframe_len = 0;
  724. return 0;
  725. }
  726. samples = data;
  727. init_get_bits(&s->gb, buf, buf_size*8);
  728. if (s->use_bit_reservoir) {
  729. /* read super frame header */
  730. get_bits(&s->gb, 4); /* super frame index */
  731. nb_frames = get_bits(&s->gb, 4) - 1;
  732. bit_offset = get_bits(&s->gb, s->byte_offset_bits + 3);
  733. if (s->last_superframe_len > 0) {
  734. // printf("skip=%d\n", s->last_bitoffset);
  735. /* add bit_offset bits to last frame */
  736. if ((s->last_superframe_len + ((bit_offset + 7) >> 3)) >
  737. MAX_CODED_SUPERFRAME_SIZE)
  738. goto fail;
  739. q = s->last_superframe + s->last_superframe_len;
  740. len = bit_offset;
  741. while (len > 7) {
  742. *q++ = (get_bits)(&s->gb, 8);
  743. len -= 8;
  744. }
  745. if (len > 0) {
  746. *q++ = (get_bits)(&s->gb, len) << (8 - len);
  747. }
  748. /* XXX: bit_offset bits into last frame */
  749. init_get_bits(&s->gb, s->last_superframe, MAX_CODED_SUPERFRAME_SIZE*8);
  750. /* skip unused bits */
  751. if (s->last_bitoffset > 0)
  752. skip_bits(&s->gb, s->last_bitoffset);
  753. /* this frame is stored in the last superframe and in the
  754. current one */
  755. if (wma_decode_frame(s, samples) < 0)
  756. goto fail;
  757. samples += s->nb_channels * s->frame_len;
  758. }
  759. /* read each frame starting from bit_offset */
  760. pos = bit_offset + 4 + 4 + s->byte_offset_bits + 3;
  761. init_get_bits(&s->gb, buf + (pos >> 3), (MAX_CODED_SUPERFRAME_SIZE - (pos >> 3))*8);
  762. len = pos & 7;
  763. if (len > 0)
  764. skip_bits(&s->gb, len);
  765. s->reset_block_lengths = 1;
  766. for(i=0;i<nb_frames;i++) {
  767. if (wma_decode_frame(s, samples) < 0)
  768. goto fail;
  769. samples += s->nb_channels * s->frame_len;
  770. }
  771. /* we copy the end of the frame in the last frame buffer */
  772. pos = get_bits_count(&s->gb) + ((bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7);
  773. s->last_bitoffset = pos & 7;
  774. pos >>= 3;
  775. len = buf_size - pos;
  776. if (len > MAX_CODED_SUPERFRAME_SIZE || len < 0) {
  777. goto fail;
  778. }
  779. s->last_superframe_len = len;
  780. memcpy(s->last_superframe, buf + pos, len);
  781. } else {
  782. /* single frame decode */
  783. if (wma_decode_frame(s, samples) < 0)
  784. goto fail;
  785. samples += s->nb_channels * s->frame_len;
  786. }
  787. //av_log(NULL, AV_LOG_ERROR, "%d %d %d %d outbytes:%d eaten:%d\n", s->frame_len_bits, s->block_len_bits, s->frame_len, s->block_len, (int8_t *)samples - (int8_t *)data, s->block_align);
  788. *data_size = (int8_t *)samples - (int8_t *)data;
  789. return s->block_align;
  790. fail:
  791. /* when error, we reset the bit reservoir */
  792. s->last_superframe_len = 0;
  793. return -1;
  794. }
  795. AVCodec wmav1_decoder =
  796. {
  797. "wmav1",
  798. CODEC_TYPE_AUDIO,
  799. CODEC_ID_WMAV1,
  800. sizeof(WMACodecContext),
  801. wma_decode_init,
  802. NULL,
  803. ff_wma_end,
  804. wma_decode_superframe,
  805. };
  806. AVCodec wmav2_decoder =
  807. {
  808. "wmav2",
  809. CODEC_TYPE_AUDIO,
  810. CODEC_ID_WMAV2,
  811. sizeof(WMACodecContext),
  812. wma_decode_init,
  813. NULL,
  814. ff_wma_end,
  815. wma_decode_superframe,
  816. };