wmadec.c 28 KB

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