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