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