FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
wmadec.c
Go to the documentation of this file.
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 /**
23  * @file
24  * WMA compatible decoder.
25  * This decoder handles Microsoft Windows Media Audio data, versions 1 & 2.
26  * WMA v1 is identified by audio format 0x160 in Microsoft media files
27  * (ASF/AVI/WAV). WMA v2 is identified by audio format 0x161.
28  *
29  * To use this decoder, a calling application must supply the extra data
30  * bytes provided with the WMA data. These are the extra, codec-specific
31  * bytes at the end of a WAVEFORMATEX data structure. Transmit these bytes
32  * to the decoder using the extradata[_size] fields in AVCodecContext. There
33  * should be 4 extra bytes for v1 data and 6 extra bytes for v2 data.
34  */
35 
36 #include "avcodec.h"
37 #include "internal.h"
38 #include "wma.h"
39 
40 #undef NDEBUG
41 #include <assert.h>
42 
43 #define EXPVLCBITS 8
44 #define EXPMAX ((19+EXPVLCBITS-1)/EXPVLCBITS)
45 
46 #define HGAINVLCBITS 9
47 #define HGAINMAX ((13+HGAINVLCBITS-1)/HGAINVLCBITS)
48 
49 static void wma_lsp_to_curve_init(WMACodecContext *s, int frame_len);
50 
51 #ifdef TRACE
52 static void dump_floats(WMACodecContext *s, const char *name, int prec, const float *tab, int n)
53 {
54  int i;
55 
56  tprintf(s->avctx, "%s[%d]:\n", name, n);
57  for(i=0;i<n;i++) {
58  if ((i & 7) == 0)
59  tprintf(s->avctx, "%4d: ", i);
60  tprintf(s->avctx, " %8.*f", prec, tab[i]);
61  if ((i & 7) == 7)
62  tprintf(s->avctx, "\n");
63  }
64  if ((i & 7) != 0)
65  tprintf(s->avctx, "\n");
66 }
67 #endif
68 
69 static int wma_decode_init(AVCodecContext * avctx)
70 {
71  WMACodecContext *s = avctx->priv_data;
72  int i, flags2;
73  uint8_t *extradata;
74 
75  s->avctx = avctx;
76 
77  /* extract flag infos */
78  flags2 = 0;
79  extradata = avctx->extradata;
80  if (avctx->codec->id == AV_CODEC_ID_WMAV1 && avctx->extradata_size >= 4) {
81  flags2 = AV_RL16(extradata+2);
82  } else if (avctx->codec->id == AV_CODEC_ID_WMAV2 && avctx->extradata_size >= 6) {
83  flags2 = AV_RL16(extradata+4);
84  }
85 
86  s->use_exp_vlc = flags2 & 0x0001;
87  s->use_bit_reservoir = flags2 & 0x0002;
88  s->use_variable_block_len = flags2 & 0x0004;
89 
90  if(avctx->codec->id == AV_CODEC_ID_WMAV2 && avctx->extradata_size >= 8){
91  if(AV_RL16(extradata+4)==0xd && s->use_variable_block_len){
92  av_log(avctx, AV_LOG_WARNING, "Disabling use_variable_block_len, if this fails contact the ffmpeg developers and send us the file\n");
93  s->use_variable_block_len= 0; // this fixes issue1503
94  }
95  }
96 
97  if(ff_wma_init(avctx, flags2)<0)
98  return -1;
99 
100  /* init MDCT */
101  for(i = 0; i < s->nb_block_sizes; i++)
102  ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 1, 1.0 / 32768.0);
103 
104  if (s->use_noise_coding) {
106  ff_wma_hgain_huffbits, 1, 1,
107  ff_wma_hgain_huffcodes, 2, 2, 0);
108  }
109 
110  if (s->use_exp_vlc) {
111  init_vlc(&s->exp_vlc, EXPVLCBITS, sizeof(ff_aac_scalefactor_bits), //FIXME move out of context
113  ff_aac_scalefactor_code, 4, 4, 0);
114  } else {
116  }
117 
119 
120  return 0;
121 }
122 
123 /**
124  * compute x^-0.25 with an exponent and mantissa table. We use linear
125  * interpolation to reduce the mantissa table size at a small speed
126  * expense (linear interpolation approximately doubles the number of
127  * bits of precision).
128  */
129 static inline float pow_m1_4(WMACodecContext *s, float x)
130 {
131  union {
132  float f;
133  unsigned int v;
134  } u, t;
135  unsigned int e, m;
136  float a, b;
137 
138  u.f = x;
139  e = u.v >> 23;
140  m = (u.v >> (23 - LSP_POW_BITS)) & ((1 << LSP_POW_BITS) - 1);
141  /* build interpolation scale: 1 <= t < 2. */
142  t.v = ((u.v << LSP_POW_BITS) & ((1 << 23) - 1)) | (127 << 23);
143  a = s->lsp_pow_m_table1[m];
144  b = s->lsp_pow_m_table2[m];
145  return s->lsp_pow_e_table[e] * (a + b * t.f);
146 }
147 
148 static void wma_lsp_to_curve_init(WMACodecContext *s, int frame_len)
149 {
150  float wdel, a, b;
151  int i, e, m;
152 
153  wdel = M_PI / frame_len;
154  for(i=0;i<frame_len;i++)
155  s->lsp_cos_table[i] = 2.0f * cos(wdel * i);
156 
157  /* tables for x^-0.25 computation */
158  for(i=0;i<256;i++) {
159  e = i - 126;
160  s->lsp_pow_e_table[i] = pow(2.0, e * -0.25);
161  }
162 
163  /* NOTE: these two tables are needed to avoid two operations in
164  pow_m1_4 */
165  b = 1.0;
166  for(i=(1 << LSP_POW_BITS) - 1;i>=0;i--) {
167  m = (1 << LSP_POW_BITS) + i;
168  a = (float)m * (0.5 / (1 << LSP_POW_BITS));
169  a = pow(a, -0.25);
170  s->lsp_pow_m_table1[i] = 2 * a - b;
171  s->lsp_pow_m_table2[i] = b - a;
172  b = a;
173  }
174 }
175 
176 /**
177  * NOTE: We use the same code as Vorbis here
178  * @todo optimize it further with SSE/3Dnow
179  */
181  float *out, float *val_max_ptr,
182  int n, float *lsp)
183 {
184  int i, j;
185  float p, q, w, v, val_max;
186 
187  val_max = 0;
188  for(i=0;i<n;i++) {
189  p = 0.5f;
190  q = 0.5f;
191  w = s->lsp_cos_table[i];
192  for(j=1;j<NB_LSP_COEFS;j+=2){
193  q *= w - lsp[j - 1];
194  p *= w - lsp[j];
195  }
196  p *= p * (2.0f - w);
197  q *= q * (2.0f + w);
198  v = p + q;
199  v = pow_m1_4(s, v);
200  if (v > val_max)
201  val_max = v;
202  out[i] = v;
203  }
204  *val_max_ptr = val_max;
205 }
206 
207 /**
208  * decode exponents coded with LSP coefficients (same idea as Vorbis)
209  */
210 static void decode_exp_lsp(WMACodecContext *s, int ch)
211 {
212  float lsp_coefs[NB_LSP_COEFS];
213  int val, i;
214 
215  for(i = 0; i < NB_LSP_COEFS; i++) {
216  if (i == 0 || i >= 8)
217  val = get_bits(&s->gb, 3);
218  else
219  val = get_bits(&s->gb, 4);
220  lsp_coefs[i] = ff_wma_lsp_codebook[i][val];
221  }
222 
223  wma_lsp_to_curve(s, s->exponents[ch], &s->max_exponent[ch],
224  s->block_len, lsp_coefs);
225 }
226 
227 /** pow(10, i / 16.0) for i in -60..95 */
228 static const float pow_tab[] = {
229  1.7782794100389e-04, 2.0535250264571e-04,
230  2.3713737056617e-04, 2.7384196342644e-04,
231  3.1622776601684e-04, 3.6517412725484e-04,
232  4.2169650342858e-04, 4.8696752516586e-04,
233  5.6234132519035e-04, 6.4938163157621e-04,
234  7.4989420933246e-04, 8.6596432336006e-04,
235  1.0000000000000e-03, 1.1547819846895e-03,
236  1.3335214321633e-03, 1.5399265260595e-03,
237  1.7782794100389e-03, 2.0535250264571e-03,
238  2.3713737056617e-03, 2.7384196342644e-03,
239  3.1622776601684e-03, 3.6517412725484e-03,
240  4.2169650342858e-03, 4.8696752516586e-03,
241  5.6234132519035e-03, 6.4938163157621e-03,
242  7.4989420933246e-03, 8.6596432336006e-03,
243  1.0000000000000e-02, 1.1547819846895e-02,
244  1.3335214321633e-02, 1.5399265260595e-02,
245  1.7782794100389e-02, 2.0535250264571e-02,
246  2.3713737056617e-02, 2.7384196342644e-02,
247  3.1622776601684e-02, 3.6517412725484e-02,
248  4.2169650342858e-02, 4.8696752516586e-02,
249  5.6234132519035e-02, 6.4938163157621e-02,
250  7.4989420933246e-02, 8.6596432336007e-02,
251  1.0000000000000e-01, 1.1547819846895e-01,
252  1.3335214321633e-01, 1.5399265260595e-01,
253  1.7782794100389e-01, 2.0535250264571e-01,
254  2.3713737056617e-01, 2.7384196342644e-01,
255  3.1622776601684e-01, 3.6517412725484e-01,
256  4.2169650342858e-01, 4.8696752516586e-01,
257  5.6234132519035e-01, 6.4938163157621e-01,
258  7.4989420933246e-01, 8.6596432336007e-01,
259  1.0000000000000e+00, 1.1547819846895e+00,
260  1.3335214321633e+00, 1.5399265260595e+00,
261  1.7782794100389e+00, 2.0535250264571e+00,
262  2.3713737056617e+00, 2.7384196342644e+00,
263  3.1622776601684e+00, 3.6517412725484e+00,
264  4.2169650342858e+00, 4.8696752516586e+00,
265  5.6234132519035e+00, 6.4938163157621e+00,
266  7.4989420933246e+00, 8.6596432336007e+00,
267  1.0000000000000e+01, 1.1547819846895e+01,
268  1.3335214321633e+01, 1.5399265260595e+01,
269  1.7782794100389e+01, 2.0535250264571e+01,
270  2.3713737056617e+01, 2.7384196342644e+01,
271  3.1622776601684e+01, 3.6517412725484e+01,
272  4.2169650342858e+01, 4.8696752516586e+01,
273  5.6234132519035e+01, 6.4938163157621e+01,
274  7.4989420933246e+01, 8.6596432336007e+01,
275  1.0000000000000e+02, 1.1547819846895e+02,
276  1.3335214321633e+02, 1.5399265260595e+02,
277  1.7782794100389e+02, 2.0535250264571e+02,
278  2.3713737056617e+02, 2.7384196342644e+02,
279  3.1622776601684e+02, 3.6517412725484e+02,
280  4.2169650342858e+02, 4.8696752516586e+02,
281  5.6234132519035e+02, 6.4938163157621e+02,
282  7.4989420933246e+02, 8.6596432336007e+02,
283  1.0000000000000e+03, 1.1547819846895e+03,
284  1.3335214321633e+03, 1.5399265260595e+03,
285  1.7782794100389e+03, 2.0535250264571e+03,
286  2.3713737056617e+03, 2.7384196342644e+03,
287  3.1622776601684e+03, 3.6517412725484e+03,
288  4.2169650342858e+03, 4.8696752516586e+03,
289  5.6234132519035e+03, 6.4938163157621e+03,
290  7.4989420933246e+03, 8.6596432336007e+03,
291  1.0000000000000e+04, 1.1547819846895e+04,
292  1.3335214321633e+04, 1.5399265260595e+04,
293  1.7782794100389e+04, 2.0535250264571e+04,
294  2.3713737056617e+04, 2.7384196342644e+04,
295  3.1622776601684e+04, 3.6517412725484e+04,
296  4.2169650342858e+04, 4.8696752516586e+04,
297  5.6234132519035e+04, 6.4938163157621e+04,
298  7.4989420933246e+04, 8.6596432336007e+04,
299  1.0000000000000e+05, 1.1547819846895e+05,
300  1.3335214321633e+05, 1.5399265260595e+05,
301  1.7782794100389e+05, 2.0535250264571e+05,
302  2.3713737056617e+05, 2.7384196342644e+05,
303  3.1622776601684e+05, 3.6517412725484e+05,
304  4.2169650342858e+05, 4.8696752516586e+05,
305  5.6234132519035e+05, 6.4938163157621e+05,
306  7.4989420933246e+05, 8.6596432336007e+05,
307 };
308 
309 /**
310  * decode exponents coded with VLC codes
311  */
312 static int decode_exp_vlc(WMACodecContext *s, int ch)
313 {
314  int last_exp, n, code;
315  const uint16_t *ptr;
316  float v, max_scale;
317  uint32_t *q, *q_end, iv;
318  const float *ptab = pow_tab + 60;
319  const uint32_t *iptab = (const uint32_t*)ptab;
320 
321  ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
322  q = (uint32_t *)s->exponents[ch];
323  q_end = q + s->block_len;
324  max_scale = 0;
325  if (s->version == 1) {
326  last_exp = get_bits(&s->gb, 5) + 10;
327  v = ptab[last_exp];
328  iv = iptab[last_exp];
329  max_scale = v;
330  n = *ptr++;
331  switch (n & 3) do {
332  case 0: *q++ = iv;
333  case 3: *q++ = iv;
334  case 2: *q++ = iv;
335  case 1: *q++ = iv;
336  } while ((n -= 4) > 0);
337  }else
338  last_exp = 36;
339 
340  while (q < q_end) {
341  code = get_vlc2(&s->gb, s->exp_vlc.table, EXPVLCBITS, EXPMAX);
342  if (code < 0){
343  av_log(s->avctx, AV_LOG_ERROR, "Exponent vlc invalid\n");
344  return -1;
345  }
346  /* NOTE: this offset is the same as MPEG4 AAC ! */
347  last_exp += code - 60;
348  if ((unsigned)last_exp + 60 >= FF_ARRAY_ELEMS(pow_tab)) {
349  av_log(s->avctx, AV_LOG_ERROR, "Exponent out of range: %d\n",
350  last_exp);
351  return -1;
352  }
353  v = ptab[last_exp];
354  iv = iptab[last_exp];
355  if (v > max_scale)
356  max_scale = v;
357  n = *ptr++;
358  switch (n & 3) do {
359  case 0: *q++ = iv;
360  case 3: *q++ = iv;
361  case 2: *q++ = iv;
362  case 1: *q++ = iv;
363  } while ((n -= 4) > 0);
364  }
365  s->max_exponent[ch] = max_scale;
366  return 0;
367 }
368 
369 
370 /**
371  * Apply MDCT window and add into output.
372  *
373  * We ensure that when the windows overlap their squared sum
374  * is always 1 (MDCT reconstruction rule).
375  */
376 static void wma_window(WMACodecContext *s, float *out)
377 {
378  float *in = s->output;
379  int block_len, bsize, n;
380 
381  /* left part */
382  if (s->block_len_bits <= s->prev_block_len_bits) {
383  block_len = s->block_len;
384  bsize = s->frame_len_bits - s->block_len_bits;
385 
386  s->fdsp.vector_fmul_add(out, in, s->windows[bsize],
387  out, block_len);
388 
389  } else {
390  block_len = 1 << s->prev_block_len_bits;
391  n = (s->block_len - block_len) / 2;
392  bsize = s->frame_len_bits - s->prev_block_len_bits;
393 
394  s->fdsp.vector_fmul_add(out+n, in+n, s->windows[bsize],
395  out+n, block_len);
396 
397  memcpy(out+n+block_len, in+n+block_len, n*sizeof(float));
398  }
399 
400  out += s->block_len;
401  in += s->block_len;
402 
403  /* right part */
404  if (s->block_len_bits <= s->next_block_len_bits) {
405  block_len = s->block_len;
406  bsize = s->frame_len_bits - s->block_len_bits;
407 
408  s->fdsp.vector_fmul_reverse(out, in, s->windows[bsize], block_len);
409 
410  } else {
411  block_len = 1 << s->next_block_len_bits;
412  n = (s->block_len - block_len) / 2;
413  bsize = s->frame_len_bits - s->next_block_len_bits;
414 
415  memcpy(out, in, n*sizeof(float));
416 
417  s->fdsp.vector_fmul_reverse(out+n, in+n, s->windows[bsize], block_len);
418 
419  memset(out+n+block_len, 0, n*sizeof(float));
420  }
421 }
422 
423 
424 /**
425  * @return 0 if OK. 1 if last block of frame. return -1 if
426  * unrecorrable error.
427  */
429 {
430  int n, v, a, ch, bsize;
431  int coef_nb_bits, total_gain;
432  int nb_coefs[MAX_CHANNELS];
433  float mdct_norm;
434  FFTContext *mdct;
435 
436 #ifdef TRACE
437  tprintf(s->avctx, "***decode_block: %d:%d\n", s->frame_count - 1, s->block_num);
438 #endif
439 
440  /* compute current block length */
441  if (s->use_variable_block_len) {
442  n = av_log2(s->nb_block_sizes - 1) + 1;
443 
444  if (s->reset_block_lengths) {
445  s->reset_block_lengths = 0;
446  v = get_bits(&s->gb, n);
447  if (v >= s->nb_block_sizes){
448  av_log(s->avctx, AV_LOG_ERROR, "prev_block_len_bits %d out of range\n", s->frame_len_bits - v);
449  return -1;
450  }
452  v = get_bits(&s->gb, n);
453  if (v >= s->nb_block_sizes){
454  av_log(s->avctx, AV_LOG_ERROR, "block_len_bits %d out of range\n", s->frame_len_bits - v);
455  return -1;
456  }
457  s->block_len_bits = s->frame_len_bits - v;
458  } else {
459  /* update block lengths */
462  }
463  v = get_bits(&s->gb, n);
464  if (v >= s->nb_block_sizes){
465  av_log(s->avctx, AV_LOG_ERROR, "next_block_len_bits %d out of range\n", s->frame_len_bits - v);
466  return -1;
467  }
469  } else {
470  /* fixed block len */
474  }
475 
476  if (s->frame_len_bits - s->block_len_bits >= s->nb_block_sizes){
477  av_log(s->avctx, AV_LOG_ERROR, "block_len_bits not initialized to a valid value\n");
478  return -1;
479  }
480 
481  /* now check if the block length is coherent with the frame length */
482  s->block_len = 1 << s->block_len_bits;
483  if ((s->block_pos + s->block_len) > s->frame_len){
484  av_log(s->avctx, AV_LOG_ERROR, "frame_len overflow\n");
485  return -1;
486  }
487 
488  if (s->avctx->channels == 2) {
489  s->ms_stereo = get_bits1(&s->gb);
490  }
491  v = 0;
492  for(ch = 0; ch < s->avctx->channels; ch++) {
493  a = get_bits1(&s->gb);
494  s->channel_coded[ch] = a;
495  v |= a;
496  }
497 
498  bsize = s->frame_len_bits - s->block_len_bits;
499 
500  /* if no channel coded, no need to go further */
501  /* XXX: fix potential framing problems */
502  if (!v)
503  goto next;
504 
505  /* read total gain and extract corresponding number of bits for
506  coef escape coding */
507  total_gain = 1;
508  for(;;) {
509  a = get_bits(&s->gb, 7);
510  total_gain += a;
511  if (a != 127)
512  break;
513  }
514 
515  coef_nb_bits= ff_wma_total_gain_to_bits(total_gain);
516 
517  /* compute number of coefficients */
518  n = s->coefs_end[bsize] - s->coefs_start;
519  for(ch = 0; ch < s->avctx->channels; ch++)
520  nb_coefs[ch] = n;
521 
522  /* complex coding */
523  if (s->use_noise_coding) {
524 
525  for(ch = 0; ch < s->avctx->channels; ch++) {
526  if (s->channel_coded[ch]) {
527  int i, n, a;
528  n = s->exponent_high_sizes[bsize];
529  for(i=0;i<n;i++) {
530  a = get_bits1(&s->gb);
531  s->high_band_coded[ch][i] = a;
532  /* if noise coding, the coefficients are not transmitted */
533  if (a)
534  nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
535  }
536  }
537  }
538  for(ch = 0; ch < s->avctx->channels; ch++) {
539  if (s->channel_coded[ch]) {
540  int i, n, val, code;
541 
542  n = s->exponent_high_sizes[bsize];
543  val = (int)0x80000000;
544  for(i=0;i<n;i++) {
545  if (s->high_band_coded[ch][i]) {
546  if (val == (int)0x80000000) {
547  val = get_bits(&s->gb, 7) - 19;
548  } else {
549  code = get_vlc2(&s->gb, s->hgain_vlc.table, HGAINVLCBITS, HGAINMAX);
550  if (code < 0){
551  av_log(s->avctx, AV_LOG_ERROR, "hgain vlc invalid\n");
552  return -1;
553  }
554  val += code - 18;
555  }
556  s->high_band_values[ch][i] = val;
557  }
558  }
559  }
560  }
561  }
562 
563  /* exponents can be reused in short blocks. */
564  if ((s->block_len_bits == s->frame_len_bits) ||
565  get_bits1(&s->gb)) {
566  for(ch = 0; ch < s->avctx->channels; ch++) {
567  if (s->channel_coded[ch]) {
568  if (s->use_exp_vlc) {
569  if (decode_exp_vlc(s, ch) < 0)
570  return -1;
571  } else {
572  decode_exp_lsp(s, ch);
573  }
574  s->exponents_bsize[ch] = bsize;
575  }
576  }
577  }
578 
579  /* parse spectral coefficients : just RLE encoding */
580  for (ch = 0; ch < s->avctx->channels; ch++) {
581  if (s->channel_coded[ch]) {
582  int tindex;
583  WMACoef* ptr = &s->coefs1[ch][0];
584 
585  /* special VLC tables are used for ms stereo because
586  there is potentially less energy there */
587  tindex = (ch == 1 && s->ms_stereo);
588  memset(ptr, 0, s->block_len * sizeof(WMACoef));
589  ff_wma_run_level_decode(s->avctx, &s->gb, &s->coef_vlc[tindex],
590  s->level_table[tindex], s->run_table[tindex],
591  0, ptr, 0, nb_coefs[ch],
592  s->block_len, s->frame_len_bits, coef_nb_bits);
593  }
594  if (s->version == 1 && s->avctx->channels >= 2) {
595  align_get_bits(&s->gb);
596  }
597  }
598 
599  /* normalize */
600  {
601  int n4 = s->block_len / 2;
602  mdct_norm = 1.0 / (float)n4;
603  if (s->version == 1) {
604  mdct_norm *= sqrt(n4);
605  }
606  }
607 
608  /* finally compute the MDCT coefficients */
609  for (ch = 0; ch < s->avctx->channels; ch++) {
610  if (s->channel_coded[ch]) {
611  WMACoef *coefs1;
612  float *coefs, *exponents, mult, mult1, noise;
613  int i, j, n, n1, last_high_band, esize;
614  float exp_power[HIGH_BAND_MAX_SIZE];
615 
616  coefs1 = s->coefs1[ch];
617  exponents = s->exponents[ch];
618  esize = s->exponents_bsize[ch];
619  mult = pow(10, total_gain * 0.05) / s->max_exponent[ch];
620  mult *= mdct_norm;
621  coefs = s->coefs[ch];
622  if (s->use_noise_coding) {
623  mult1 = mult;
624  /* very low freqs : noise */
625  for(i = 0;i < s->coefs_start; i++) {
626  *coefs++ = s->noise_table[s->noise_index] *
627  exponents[i<<bsize>>esize] * mult1;
628  s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
629  }
630 
631  n1 = s->exponent_high_sizes[bsize];
632 
633  /* compute power of high bands */
634  exponents = s->exponents[ch] +
635  (s->high_band_start[bsize]<<bsize>>esize);
636  last_high_band = 0; /* avoid warning */
637  for(j=0;j<n1;j++) {
639  s->block_len_bits][j];
640  if (s->high_band_coded[ch][j]) {
641  float e2, v;
642  e2 = 0;
643  for(i = 0;i < n; i++) {
644  v = exponents[i<<bsize>>esize];
645  e2 += v * v;
646  }
647  exp_power[j] = e2 / n;
648  last_high_band = j;
649  tprintf(s->avctx, "%d: power=%f (%d)\n", j, exp_power[j], n);
650  }
651  exponents += n<<bsize>>esize;
652  }
653 
654  /* main freqs and high freqs */
655  exponents = s->exponents[ch] + (s->coefs_start<<bsize>>esize);
656  for(j=-1;j<n1;j++) {
657  if (j < 0) {
658  n = s->high_band_start[bsize] -
659  s->coefs_start;
660  } else {
662  s->block_len_bits][j];
663  }
664  if (j >= 0 && s->high_band_coded[ch][j]) {
665  /* use noise with specified power */
666  mult1 = sqrt(exp_power[j] / exp_power[last_high_band]);
667  /* XXX: use a table */
668  mult1 = mult1 * pow(10, s->high_band_values[ch][j] * 0.05);
669  mult1 = mult1 / (s->max_exponent[ch] * s->noise_mult);
670  mult1 *= mdct_norm;
671  for(i = 0;i < n; i++) {
672  noise = s->noise_table[s->noise_index];
673  s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
674  *coefs++ = noise *
675  exponents[i<<bsize>>esize] * mult1;
676  }
677  exponents += n<<bsize>>esize;
678  } else {
679  /* coded values + small noise */
680  for(i = 0;i < n; i++) {
681  noise = s->noise_table[s->noise_index];
682  s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
683  *coefs++ = ((*coefs1++) + noise) *
684  exponents[i<<bsize>>esize] * mult;
685  }
686  exponents += n<<bsize>>esize;
687  }
688  }
689 
690  /* very high freqs : noise */
691  n = s->block_len - s->coefs_end[bsize];
692  mult1 = mult * exponents[((-1<<bsize))>>esize];
693  for(i = 0; i < n; i++) {
694  *coefs++ = s->noise_table[s->noise_index] * mult1;
695  s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
696  }
697  } else {
698  /* XXX: optimize more */
699  for(i = 0;i < s->coefs_start; i++)
700  *coefs++ = 0.0;
701  n = nb_coefs[ch];
702  for(i = 0;i < n; i++) {
703  *coefs++ = coefs1[i] * exponents[i<<bsize>>esize] * mult;
704  }
705  n = s->block_len - s->coefs_end[bsize];
706  for(i = 0;i < n; i++)
707  *coefs++ = 0.0;
708  }
709  }
710  }
711 
712 #ifdef TRACE
713  for (ch = 0; ch < s->avctx->channels; ch++) {
714  if (s->channel_coded[ch]) {
715  dump_floats(s, "exponents", 3, s->exponents[ch], s->block_len);
716  dump_floats(s, "coefs", 1, s->coefs[ch], s->block_len);
717  }
718  }
719 #endif
720 
721  if (s->ms_stereo && s->channel_coded[1]) {
722  /* nominal case for ms stereo: we do it before mdct */
723  /* no need to optimize this case because it should almost
724  never happen */
725  if (!s->channel_coded[0]) {
726  tprintf(s->avctx, "rare ms-stereo case happened\n");
727  memset(s->coefs[0], 0, sizeof(float) * s->block_len);
728  s->channel_coded[0] = 1;
729  }
730 
731  s->fdsp.butterflies_float(s->coefs[0], s->coefs[1], s->block_len);
732  }
733 
734 next:
735  mdct = &s->mdct_ctx[bsize];
736 
737  for (ch = 0; ch < s->avctx->channels; ch++) {
738  int n4, index;
739 
740  n4 = s->block_len / 2;
741  if(s->channel_coded[ch]){
742  mdct->imdct_calc(mdct, s->output, s->coefs[ch]);
743  }else if(!(s->ms_stereo && ch==1))
744  memset(s->output, 0, sizeof(s->output));
745 
746  /* multiply by the window and add in the frame */
747  index = (s->frame_len / 2) + s->block_pos - n4;
748  wma_window(s, &s->frame_out[ch][index]);
749  }
750 
751  /* update block number */
752  s->block_num++;
753  s->block_pos += s->block_len;
754  if (s->block_pos >= s->frame_len)
755  return 1;
756  else
757  return 0;
758 }
759 
760 /* decode a frame of frame_len samples */
761 static int wma_decode_frame(WMACodecContext *s, float **samples,
762  int samples_offset)
763 {
764  int ret, ch;
765 
766 #ifdef TRACE
767  tprintf(s->avctx, "***decode_frame: %d size=%d\n", s->frame_count++, s->frame_len);
768 #endif
769 
770  /* read each block */
771  s->block_num = 0;
772  s->block_pos = 0;
773  for(;;) {
774  ret = wma_decode_block(s);
775  if (ret < 0)
776  return -1;
777  if (ret)
778  break;
779  }
780 
781  for (ch = 0; ch < s->avctx->channels; ch++) {
782  /* copy current block to output */
783  memcpy(samples[ch] + samples_offset, s->frame_out[ch],
784  s->frame_len * sizeof(*s->frame_out[ch]));
785  /* prepare for next block */
786  memmove(&s->frame_out[ch][0], &s->frame_out[ch][s->frame_len],
787  s->frame_len * sizeof(*s->frame_out[ch]));
788 
789 #ifdef TRACE
790  dump_floats(s, "samples", 6, samples[ch] + samples_offset, s->frame_len);
791 #endif
792  }
793 
794  return 0;
795 }
796 
797 static int wma_decode_superframe(AVCodecContext *avctx, void *data,
798  int *got_frame_ptr, AVPacket *avpkt)
799 {
800  AVFrame *frame = data;
801  const uint8_t *buf = avpkt->data;
802  int buf_size = avpkt->size;
803  WMACodecContext *s = avctx->priv_data;
804  int nb_frames, bit_offset, i, pos, len, ret;
805  uint8_t *q;
806  float **samples;
807  int samples_offset;
808 
809  tprintf(avctx, "***decode_superframe:\n");
810 
811  if(buf_size==0){
812  s->last_superframe_len = 0;
813  return 0;
814  }
815  if (buf_size < avctx->block_align) {
816  av_log(avctx, AV_LOG_ERROR,
817  "Input packet size too small (%d < %d)\n",
818  buf_size, avctx->block_align);
819  return AVERROR_INVALIDDATA;
820  }
821  if(avctx->block_align)
822  buf_size = avctx->block_align;
823 
824  init_get_bits(&s->gb, buf, buf_size*8);
825 
826  if (s->use_bit_reservoir) {
827  /* read super frame header */
828  skip_bits(&s->gb, 4); /* super frame index */
829  nb_frames = get_bits(&s->gb, 4) - (s->last_superframe_len <= 0);
830  if (nb_frames <= 0) {
831  av_log(avctx, AV_LOG_ERROR, "nb_frames is %d\n", nb_frames);
832  return AVERROR_INVALIDDATA;
833  }
834  } else {
835  nb_frames = 1;
836  }
837 
838  /* get output buffer */
839  frame->nb_samples = nb_frames * s->frame_len;
840  if ((ret = ff_get_buffer(avctx, frame)) < 0) {
841  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
842  return ret;
843  }
844  samples = (float **)frame->extended_data;
845  samples_offset = 0;
846 
847  if (s->use_bit_reservoir) {
848  bit_offset = get_bits(&s->gb, s->byte_offset_bits + 3);
849  if (bit_offset > get_bits_left(&s->gb)) {
850  av_log(avctx, AV_LOG_ERROR,
851  "Invalid last frame bit offset %d > buf size %d (%d)\n",
852  bit_offset, get_bits_left(&s->gb), buf_size);
853  goto fail;
854  }
855 
856  if (s->last_superframe_len > 0) {
857  /* add bit_offset bits to last frame */
858  if ((s->last_superframe_len + ((bit_offset + 7) >> 3)) >
860  goto fail;
862  len = bit_offset;
863  while (len > 7) {
864  *q++ = (get_bits)(&s->gb, 8);
865  len -= 8;
866  }
867  if (len > 0) {
868  *q++ = (get_bits)(&s->gb, len) << (8 - len);
869  }
870  memset(q, 0, FF_INPUT_BUFFER_PADDING_SIZE);
871 
872  /* XXX: bit_offset bits into last frame */
873  init_get_bits(&s->gb, s->last_superframe, s->last_superframe_len * 8 + bit_offset);
874  /* skip unused bits */
875  if (s->last_bitoffset > 0)
876  skip_bits(&s->gb, s->last_bitoffset);
877  /* this frame is stored in the last superframe and in the
878  current one */
879  if (wma_decode_frame(s, samples, samples_offset) < 0)
880  goto fail;
881  samples_offset += s->frame_len;
882  nb_frames--;
883  }
884 
885  /* read each frame starting from bit_offset */
886  pos = bit_offset + 4 + 4 + s->byte_offset_bits + 3;
887  if (pos >= MAX_CODED_SUPERFRAME_SIZE * 8 || pos > buf_size * 8)
888  return AVERROR_INVALIDDATA;
889  init_get_bits(&s->gb, buf + (pos >> 3), (buf_size - (pos >> 3))*8);
890  len = pos & 7;
891  if (len > 0)
892  skip_bits(&s->gb, len);
893 
894  s->reset_block_lengths = 1;
895  for(i=0;i<nb_frames;i++) {
896  if (wma_decode_frame(s, samples, samples_offset) < 0)
897  goto fail;
898  samples_offset += s->frame_len;
899  }
900 
901  /* we copy the end of the frame in the last frame buffer */
902  pos = get_bits_count(&s->gb) + ((bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7);
903  s->last_bitoffset = pos & 7;
904  pos >>= 3;
905  len = buf_size - pos;
906  if (len > MAX_CODED_SUPERFRAME_SIZE || len < 0) {
907  av_log(s->avctx, AV_LOG_ERROR, "len %d invalid\n", len);
908  goto fail;
909  }
911  memcpy(s->last_superframe, buf + pos, len);
912  } else {
913  /* single frame decode */
914  if (wma_decode_frame(s, samples, samples_offset) < 0)
915  goto fail;
916  samples_offset += s->frame_len;
917  }
918 
919  av_dlog(s->avctx, "%d %d %d %d outbytes:%td eaten:%d\n",
921  (int8_t *)samples - (int8_t *)data, avctx->block_align);
922 
923  *got_frame_ptr = 1;
924 
925  return buf_size;
926  fail:
927  /* when error, we reset the bit reservoir */
928  s->last_superframe_len = 0;
929  return -1;
930 }
931 
932 static av_cold void flush(AVCodecContext *avctx)
933 {
934  WMACodecContext *s = avctx->priv_data;
935 
936  s->last_bitoffset=
937  s->last_superframe_len= 0;
938 }
939 
940 #if CONFIG_WMAV1_DECODER
941 AVCodec ff_wmav1_decoder = {
942  .name = "wmav1",
943  .type = AVMEDIA_TYPE_AUDIO,
944  .id = AV_CODEC_ID_WMAV1,
945  .priv_data_size = sizeof(WMACodecContext),
947  .close = ff_wma_end,
949  .flush = flush,
950  .capabilities = CODEC_CAP_DR1,
951  .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 1"),
952  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
954 };
955 #endif
956 #if CONFIG_WMAV2_DECODER
957 AVCodec ff_wmav2_decoder = {
958  .name = "wmav2",
959  .type = AVMEDIA_TYPE_AUDIO,
960  .id = AV_CODEC_ID_WMAV2,
961  .priv_data_size = sizeof(WMACodecContext),
963  .close = ff_wma_end,
965  .flush = flush,
966  .capabilities = CODEC_CAP_DR1,
967  .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 2"),
968  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
970 };
971 #endif