wmadec.c
Go to the documentation of this file.
1 /*
2  * WMA compatible decoder
3  * Copyright (c) 2002 The Libav Project
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
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  if (!avctx->block_align) {
76  av_log(avctx, AV_LOG_ERROR, "block_align is not set\n");
77  return AVERROR(EINVAL);
78  }
79 
80  s->avctx = avctx;
81 
82  /* extract flag infos */
83  flags2 = 0;
84  extradata = avctx->extradata;
85  if (avctx->codec->id == AV_CODEC_ID_WMAV1 && avctx->extradata_size >= 4) {
86  flags2 = AV_RL16(extradata+2);
87  } else if (avctx->codec->id == AV_CODEC_ID_WMAV2 && avctx->extradata_size >= 6) {
88  flags2 = AV_RL16(extradata+4);
89  }
90 
91  s->use_exp_vlc = flags2 & 0x0001;
92  s->use_bit_reservoir = flags2 & 0x0002;
93  s->use_variable_block_len = flags2 & 0x0004;
94 
95  if(ff_wma_init(avctx, flags2)<0)
96  return -1;
97 
98  /* init MDCT */
99  for(i = 0; i < s->nb_block_sizes; i++)
100  ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 1, 1.0 / 32768.0);
101 
102  if (s->use_noise_coding) {
104  ff_wma_hgain_huffbits, 1, 1,
105  ff_wma_hgain_huffcodes, 2, 2, 0);
106  }
107 
108  if (s->use_exp_vlc) {
109  init_vlc(&s->exp_vlc, EXPVLCBITS, sizeof(ff_aac_scalefactor_bits), //FIXME move out of context
111  ff_aac_scalefactor_code, 4, 4, 0);
112  } else {
114  }
115 
117 
119  avctx->coded_frame = &s->frame;
120 
121  return 0;
122 }
123 
130 static inline float pow_m1_4(WMACodecContext *s, float x)
131 {
132  union {
133  float f;
134  unsigned int v;
135  } u, t;
136  unsigned int e, m;
137  float a, b;
138 
139  u.f = x;
140  e = u.v >> 23;
141  m = (u.v >> (23 - LSP_POW_BITS)) & ((1 << LSP_POW_BITS) - 1);
142  /* build interpolation scale: 1 <= t < 2. */
143  t.v = ((u.v << LSP_POW_BITS) & ((1 << 23) - 1)) | (127 << 23);
144  a = s->lsp_pow_m_table1[m];
145  b = s->lsp_pow_m_table2[m];
146  return s->lsp_pow_e_table[e] * (a + b * t.f);
147 }
148 
149 static void wma_lsp_to_curve_init(WMACodecContext *s, int frame_len)
150 {
151  float wdel, a, b;
152  int i, e, m;
153 
154  wdel = M_PI / frame_len;
155  for(i=0;i<frame_len;i++)
156  s->lsp_cos_table[i] = 2.0f * cos(wdel * i);
157 
158  /* tables for x^-0.25 computation */
159  for(i=0;i<256;i++) {
160  e = i - 126;
161  s->lsp_pow_e_table[i] = pow(2.0, e * -0.25);
162  }
163 
164  /* NOTE: these two tables are needed to avoid two operations in
165  pow_m1_4 */
166  b = 1.0;
167  for(i=(1 << LSP_POW_BITS) - 1;i>=0;i--) {
168  m = (1 << LSP_POW_BITS) + i;
169  a = (float)m * (0.5 / (1 << LSP_POW_BITS));
170  a = pow(a, -0.25);
171  s->lsp_pow_m_table1[i] = 2 * a - b;
172  s->lsp_pow_m_table2[i] = b - a;
173  b = a;
174  }
175 }
176 
182  float *out, float *val_max_ptr,
183  int n, float *lsp)
184 {
185  int i, j;
186  float p, q, w, v, val_max;
187 
188  val_max = 0;
189  for(i=0;i<n;i++) {
190  p = 0.5f;
191  q = 0.5f;
192  w = s->lsp_cos_table[i];
193  for(j=1;j<NB_LSP_COEFS;j+=2){
194  q *= w - lsp[j - 1];
195  p *= w - lsp[j];
196  }
197  p *= p * (2.0f - w);
198  q *= q * (2.0f + w);
199  v = p + q;
200  v = pow_m1_4(s, v);
201  if (v > val_max)
202  val_max = v;
203  out[i] = v;
204  }
205  *val_max_ptr = val_max;
206 }
207 
211 static void decode_exp_lsp(WMACodecContext *s, int ch)
212 {
213  float lsp_coefs[NB_LSP_COEFS];
214  int val, i;
215 
216  for(i = 0; i < NB_LSP_COEFS; i++) {
217  if (i == 0 || i >= 8)
218  val = get_bits(&s->gb, 3);
219  else
220  val = get_bits(&s->gb, 4);
221  lsp_coefs[i] = ff_wma_lsp_codebook[i][val];
222  }
223 
224  wma_lsp_to_curve(s, s->exponents[ch], &s->max_exponent[ch],
225  s->block_len, lsp_coefs);
226 }
227 
229 static const float pow_tab[] = {
230  1.7782794100389e-04, 2.0535250264571e-04,
231  2.3713737056617e-04, 2.7384196342644e-04,
232  3.1622776601684e-04, 3.6517412725484e-04,
233  4.2169650342858e-04, 4.8696752516586e-04,
234  5.6234132519035e-04, 6.4938163157621e-04,
235  7.4989420933246e-04, 8.6596432336006e-04,
236  1.0000000000000e-03, 1.1547819846895e-03,
237  1.3335214321633e-03, 1.5399265260595e-03,
238  1.7782794100389e-03, 2.0535250264571e-03,
239  2.3713737056617e-03, 2.7384196342644e-03,
240  3.1622776601684e-03, 3.6517412725484e-03,
241  4.2169650342858e-03, 4.8696752516586e-03,
242  5.6234132519035e-03, 6.4938163157621e-03,
243  7.4989420933246e-03, 8.6596432336006e-03,
244  1.0000000000000e-02, 1.1547819846895e-02,
245  1.3335214321633e-02, 1.5399265260595e-02,
246  1.7782794100389e-02, 2.0535250264571e-02,
247  2.3713737056617e-02, 2.7384196342644e-02,
248  3.1622776601684e-02, 3.6517412725484e-02,
249  4.2169650342858e-02, 4.8696752516586e-02,
250  5.6234132519035e-02, 6.4938163157621e-02,
251  7.4989420933246e-02, 8.6596432336007e-02,
252  1.0000000000000e-01, 1.1547819846895e-01,
253  1.3335214321633e-01, 1.5399265260595e-01,
254  1.7782794100389e-01, 2.0535250264571e-01,
255  2.3713737056617e-01, 2.7384196342644e-01,
256  3.1622776601684e-01, 3.6517412725484e-01,
257  4.2169650342858e-01, 4.8696752516586e-01,
258  5.6234132519035e-01, 6.4938163157621e-01,
259  7.4989420933246e-01, 8.6596432336007e-01,
260  1.0000000000000e+00, 1.1547819846895e+00,
261  1.3335214321633e+00, 1.5399265260595e+00,
262  1.7782794100389e+00, 2.0535250264571e+00,
263  2.3713737056617e+00, 2.7384196342644e+00,
264  3.1622776601684e+00, 3.6517412725484e+00,
265  4.2169650342858e+00, 4.8696752516586e+00,
266  5.6234132519035e+00, 6.4938163157621e+00,
267  7.4989420933246e+00, 8.6596432336007e+00,
268  1.0000000000000e+01, 1.1547819846895e+01,
269  1.3335214321633e+01, 1.5399265260595e+01,
270  1.7782794100389e+01, 2.0535250264571e+01,
271  2.3713737056617e+01, 2.7384196342644e+01,
272  3.1622776601684e+01, 3.6517412725484e+01,
273  4.2169650342858e+01, 4.8696752516586e+01,
274  5.6234132519035e+01, 6.4938163157621e+01,
275  7.4989420933246e+01, 8.6596432336007e+01,
276  1.0000000000000e+02, 1.1547819846895e+02,
277  1.3335214321633e+02, 1.5399265260595e+02,
278  1.7782794100389e+02, 2.0535250264571e+02,
279  2.3713737056617e+02, 2.7384196342644e+02,
280  3.1622776601684e+02, 3.6517412725484e+02,
281  4.2169650342858e+02, 4.8696752516586e+02,
282  5.6234132519035e+02, 6.4938163157621e+02,
283  7.4989420933246e+02, 8.6596432336007e+02,
284  1.0000000000000e+03, 1.1547819846895e+03,
285  1.3335214321633e+03, 1.5399265260595e+03,
286  1.7782794100389e+03, 2.0535250264571e+03,
287  2.3713737056617e+03, 2.7384196342644e+03,
288  3.1622776601684e+03, 3.6517412725484e+03,
289  4.2169650342858e+03, 4.8696752516586e+03,
290  5.6234132519035e+03, 6.4938163157621e+03,
291  7.4989420933246e+03, 8.6596432336007e+03,
292  1.0000000000000e+04, 1.1547819846895e+04,
293  1.3335214321633e+04, 1.5399265260595e+04,
294  1.7782794100389e+04, 2.0535250264571e+04,
295  2.3713737056617e+04, 2.7384196342644e+04,
296  3.1622776601684e+04, 3.6517412725484e+04,
297  4.2169650342858e+04, 4.8696752516586e+04,
298  5.6234132519035e+04, 6.4938163157621e+04,
299  7.4989420933246e+04, 8.6596432336007e+04,
300  1.0000000000000e+05, 1.1547819846895e+05,
301  1.3335214321633e+05, 1.5399265260595e+05,
302  1.7782794100389e+05, 2.0535250264571e+05,
303  2.3713737056617e+05, 2.7384196342644e+05,
304  3.1622776601684e+05, 3.6517412725484e+05,
305  4.2169650342858e+05, 4.8696752516586e+05,
306  5.6234132519035e+05, 6.4938163157621e+05,
307  7.4989420933246e+05, 8.6596432336007e+05,
308 };
309 
313 static int decode_exp_vlc(WMACodecContext *s, int ch)
314 {
315  int last_exp, n, code;
316  const uint16_t *ptr;
317  float v, max_scale;
318  uint32_t *q, *q_end, iv;
319  const float *ptab = pow_tab + 60;
320  const uint32_t *iptab = (const uint32_t*)ptab;
321 
322  ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
323  q = (uint32_t *)s->exponents[ch];
324  q_end = q + s->block_len;
325  max_scale = 0;
326  if (s->version == 1) {
327  last_exp = get_bits(&s->gb, 5) + 10;
328  v = ptab[last_exp];
329  iv = iptab[last_exp];
330  max_scale = v;
331  n = *ptr++;
332  switch (n & 3) do {
333  case 0: *q++ = iv;
334  case 3: *q++ = iv;
335  case 2: *q++ = iv;
336  case 1: *q++ = iv;
337  } while ((n -= 4) > 0);
338  }else
339  last_exp = 36;
340 
341  while (q < q_end) {
342  code = get_vlc2(&s->gb, s->exp_vlc.table, EXPVLCBITS, EXPMAX);
343  if (code < 0){
344  av_log(s->avctx, AV_LOG_ERROR, "Exponent vlc invalid\n");
345  return -1;
346  }
347  /* NOTE: this offset is the same as MPEG4 AAC ! */
348  last_exp += code - 60;
349  if ((unsigned)last_exp + 60 >= FF_ARRAY_ELEMS(pow_tab)) {
350  av_log(s->avctx, AV_LOG_ERROR, "Exponent out of range: %d\n",
351  last_exp);
352  return -1;
353  }
354  v = ptab[last_exp];
355  iv = iptab[last_exp];
356  if (v > max_scale)
357  max_scale = v;
358  n = *ptr++;
359  switch (n & 3) do {
360  case 0: *q++ = iv;
361  case 3: *q++ = iv;
362  case 2: *q++ = iv;
363  case 1: *q++ = iv;
364  } while ((n -= 4) > 0);
365  }
366  s->max_exponent[ch] = max_scale;
367  return 0;
368 }
369 
370 
377 static void wma_window(WMACodecContext *s, float *out)
378 {
379  float *in = s->output;
380  int block_len, bsize, n;
381 
382  /* left part */
383  if (s->block_len_bits <= s->prev_block_len_bits) {
384  block_len = s->block_len;
385  bsize = s->frame_len_bits - s->block_len_bits;
386 
387  s->dsp.vector_fmul_add(out, in, s->windows[bsize],
388  out, block_len);
389 
390  } else {
391  block_len = 1 << s->prev_block_len_bits;
392  n = (s->block_len - block_len) / 2;
393  bsize = s->frame_len_bits - s->prev_block_len_bits;
394 
395  s->dsp.vector_fmul_add(out+n, in+n, s->windows[bsize],
396  out+n, block_len);
397 
398  memcpy(out+n+block_len, in+n+block_len, n*sizeof(float));
399  }
400 
401  out += s->block_len;
402  in += s->block_len;
403 
404  /* right part */
405  if (s->block_len_bits <= s->next_block_len_bits) {
406  block_len = s->block_len;
407  bsize = s->frame_len_bits - s->block_len_bits;
408 
409  s->dsp.vector_fmul_reverse(out, in, s->windows[bsize], block_len);
410 
411  } else {
412  block_len = 1 << s->next_block_len_bits;
413  n = (s->block_len - block_len) / 2;
414  bsize = s->frame_len_bits - s->next_block_len_bits;
415 
416  memcpy(out, in, n*sizeof(float));
417 
418  s->dsp.vector_fmul_reverse(out+n, in+n, s->windows[bsize], block_len);
419 
420  memset(out+n+block_len, 0, n*sizeof(float));
421  }
422 }
423 
424 
430 {
431  int n, v, a, ch, bsize;
432  int coef_nb_bits, total_gain;
433  int nb_coefs[MAX_CHANNELS];
434  float mdct_norm;
435  FFTContext *mdct;
436 
437 #ifdef TRACE
438  tprintf(s->avctx, "***decode_block: %d:%d\n", s->frame_count - 1, s->block_num);
439 #endif
440 
441  /* compute current block length */
442  if (s->use_variable_block_len) {
443  n = av_log2(s->nb_block_sizes - 1) + 1;
444 
445  if (s->reset_block_lengths) {
446  s->reset_block_lengths = 0;
447  v = get_bits(&s->gb, n);
448  if (v >= s->nb_block_sizes){
449  av_log(s->avctx, AV_LOG_ERROR, "prev_block_len_bits %d out of range\n", s->frame_len_bits - v);
450  return -1;
451  }
453  v = get_bits(&s->gb, n);
454  if (v >= s->nb_block_sizes){
455  av_log(s->avctx, AV_LOG_ERROR, "block_len_bits %d out of range\n", s->frame_len_bits - v);
456  return -1;
457  }
458  s->block_len_bits = s->frame_len_bits - v;
459  } else {
460  /* update block lengths */
463  }
464  v = get_bits(&s->gb, n);
465  if (v >= s->nb_block_sizes){
466  av_log(s->avctx, AV_LOG_ERROR, "next_block_len_bits %d out of range\n", s->frame_len_bits - v);
467  return -1;
468  }
470  } else {
471  /* fixed block len */
475  }
476 
477  /* now check if the block length is coherent with the frame length */
478  s->block_len = 1 << s->block_len_bits;
479  if ((s->block_pos + s->block_len) > s->frame_len){
480  av_log(s->avctx, AV_LOG_ERROR, "frame_len overflow\n");
481  return -1;
482  }
483 
484  if (s->avctx->channels == 2) {
485  s->ms_stereo = get_bits1(&s->gb);
486  }
487  v = 0;
488  for(ch = 0; ch < s->avctx->channels; ch++) {
489  a = get_bits1(&s->gb);
490  s->channel_coded[ch] = a;
491  v |= a;
492  }
493 
494  bsize = s->frame_len_bits - s->block_len_bits;
495 
496  /* if no channel coded, no need to go further */
497  /* XXX: fix potential framing problems */
498  if (!v)
499  goto next;
500 
501  /* read total gain and extract corresponding number of bits for
502  coef escape coding */
503  total_gain = 1;
504  for(;;) {
505  a = get_bits(&s->gb, 7);
506  total_gain += a;
507  if (a != 127)
508  break;
509  }
510 
511  coef_nb_bits= ff_wma_total_gain_to_bits(total_gain);
512 
513  /* compute number of coefficients */
514  n = s->coefs_end[bsize] - s->coefs_start;
515  for(ch = 0; ch < s->avctx->channels; ch++)
516  nb_coefs[ch] = n;
517 
518  /* complex coding */
519  if (s->use_noise_coding) {
520 
521  for(ch = 0; ch < s->avctx->channels; ch++) {
522  if (s->channel_coded[ch]) {
523  int i, n, a;
524  n = s->exponent_high_sizes[bsize];
525  for(i=0;i<n;i++) {
526  a = get_bits1(&s->gb);
527  s->high_band_coded[ch][i] = a;
528  /* if noise coding, the coefficients are not transmitted */
529  if (a)
530  nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
531  }
532  }
533  }
534  for(ch = 0; ch < s->avctx->channels; ch++) {
535  if (s->channel_coded[ch]) {
536  int i, n, val, code;
537 
538  n = s->exponent_high_sizes[bsize];
539  val = (int)0x80000000;
540  for(i=0;i<n;i++) {
541  if (s->high_band_coded[ch][i]) {
542  if (val == (int)0x80000000) {
543  val = get_bits(&s->gb, 7) - 19;
544  } else {
545  code = get_vlc2(&s->gb, s->hgain_vlc.table, HGAINVLCBITS, HGAINMAX);
546  if (code < 0){
547  av_log(s->avctx, AV_LOG_ERROR, "hgain vlc invalid\n");
548  return -1;
549  }
550  val += code - 18;
551  }
552  s->high_band_values[ch][i] = val;
553  }
554  }
555  }
556  }
557  }
558 
559  /* exponents can be reused in short blocks. */
560  if ((s->block_len_bits == s->frame_len_bits) ||
561  get_bits1(&s->gb)) {
562  for(ch = 0; ch < s->avctx->channels; ch++) {
563  if (s->channel_coded[ch]) {
564  if (s->use_exp_vlc) {
565  if (decode_exp_vlc(s, ch) < 0)
566  return -1;
567  } else {
568  decode_exp_lsp(s, ch);
569  }
570  s->exponents_bsize[ch] = bsize;
571  }
572  }
573  }
574 
575  /* parse spectral coefficients : just RLE encoding */
576  for (ch = 0; ch < s->avctx->channels; ch++) {
577  if (s->channel_coded[ch]) {
578  int tindex;
579  WMACoef* ptr = &s->coefs1[ch][0];
580 
581  /* special VLC tables are used for ms stereo because
582  there is potentially less energy there */
583  tindex = (ch == 1 && s->ms_stereo);
584  memset(ptr, 0, s->block_len * sizeof(WMACoef));
585  ff_wma_run_level_decode(s->avctx, &s->gb, &s->coef_vlc[tindex],
586  s->level_table[tindex], s->run_table[tindex],
587  0, ptr, 0, nb_coefs[ch],
588  s->block_len, s->frame_len_bits, coef_nb_bits);
589  }
590  if (s->version == 1 && s->avctx->channels >= 2) {
591  align_get_bits(&s->gb);
592  }
593  }
594 
595  /* normalize */
596  {
597  int n4 = s->block_len / 2;
598  mdct_norm = 1.0 / (float)n4;
599  if (s->version == 1) {
600  mdct_norm *= sqrt(n4);
601  }
602  }
603 
604  /* finally compute the MDCT coefficients */
605  for (ch = 0; ch < s->avctx->channels; ch++) {
606  if (s->channel_coded[ch]) {
607  WMACoef *coefs1;
608  float *coefs, *exponents, mult, mult1, noise;
609  int i, j, n, n1, last_high_band, esize;
610  float exp_power[HIGH_BAND_MAX_SIZE];
611 
612  coefs1 = s->coefs1[ch];
613  exponents = s->exponents[ch];
614  esize = s->exponents_bsize[ch];
615  mult = pow(10, total_gain * 0.05) / s->max_exponent[ch];
616  mult *= mdct_norm;
617  coefs = s->coefs[ch];
618  if (s->use_noise_coding) {
619  mult1 = mult;
620  /* very low freqs : noise */
621  for(i = 0;i < s->coefs_start; i++) {
622  *coefs++ = s->noise_table[s->noise_index] *
623  exponents[i<<bsize>>esize] * mult1;
624  s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
625  }
626 
627  n1 = s->exponent_high_sizes[bsize];
628 
629  /* compute power of high bands */
630  exponents = s->exponents[ch] +
631  (s->high_band_start[bsize]<<bsize>>esize);
632  last_high_band = 0; /* avoid warning */
633  for(j=0;j<n1;j++) {
635  s->block_len_bits][j];
636  if (s->high_band_coded[ch][j]) {
637  float e2, v;
638  e2 = 0;
639  for(i = 0;i < n; i++) {
640  v = exponents[i<<bsize>>esize];
641  e2 += v * v;
642  }
643  exp_power[j] = e2 / n;
644  last_high_band = j;
645  tprintf(s->avctx, "%d: power=%f (%d)\n", j, exp_power[j], n);
646  }
647  exponents += n<<bsize>>esize;
648  }
649 
650  /* main freqs and high freqs */
651  exponents = s->exponents[ch] + (s->coefs_start<<bsize>>esize);
652  for(j=-1;j<n1;j++) {
653  if (j < 0) {
654  n = s->high_band_start[bsize] -
655  s->coefs_start;
656  } else {
658  s->block_len_bits][j];
659  }
660  if (j >= 0 && s->high_band_coded[ch][j]) {
661  /* use noise with specified power */
662  mult1 = sqrt(exp_power[j] / exp_power[last_high_band]);
663  /* XXX: use a table */
664  mult1 = mult1 * pow(10, s->high_band_values[ch][j] * 0.05);
665  mult1 = mult1 / (s->max_exponent[ch] * s->noise_mult);
666  mult1 *= mdct_norm;
667  for(i = 0;i < n; i++) {
668  noise = s->noise_table[s->noise_index];
669  s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
670  *coefs++ = noise *
671  exponents[i<<bsize>>esize] * mult1;
672  }
673  exponents += n<<bsize>>esize;
674  } else {
675  /* coded values + small noise */
676  for(i = 0;i < n; i++) {
677  noise = s->noise_table[s->noise_index];
678  s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
679  *coefs++ = ((*coefs1++) + noise) *
680  exponents[i<<bsize>>esize] * mult;
681  }
682  exponents += n<<bsize>>esize;
683  }
684  }
685 
686  /* very high freqs : noise */
687  n = s->block_len - s->coefs_end[bsize];
688  mult1 = mult * exponents[((-1<<bsize))>>esize];
689  for(i = 0; i < n; i++) {
690  *coefs++ = s->noise_table[s->noise_index] * mult1;
691  s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
692  }
693  } else {
694  /* XXX: optimize more */
695  for(i = 0;i < s->coefs_start; i++)
696  *coefs++ = 0.0;
697  n = nb_coefs[ch];
698  for(i = 0;i < n; i++) {
699  *coefs++ = coefs1[i] * exponents[i<<bsize>>esize] * mult;
700  }
701  n = s->block_len - s->coefs_end[bsize];
702  for(i = 0;i < n; i++)
703  *coefs++ = 0.0;
704  }
705  }
706  }
707 
708 #ifdef TRACE
709  for (ch = 0; ch < s->avctx->channels; ch++) {
710  if (s->channel_coded[ch]) {
711  dump_floats(s, "exponents", 3, s->exponents[ch], s->block_len);
712  dump_floats(s, "coefs", 1, s->coefs[ch], s->block_len);
713  }
714  }
715 #endif
716 
717  if (s->ms_stereo && s->channel_coded[1]) {
718  /* nominal case for ms stereo: we do it before mdct */
719  /* no need to optimize this case because it should almost
720  never happen */
721  if (!s->channel_coded[0]) {
722  tprintf(s->avctx, "rare ms-stereo case happened\n");
723  memset(s->coefs[0], 0, sizeof(float) * s->block_len);
724  s->channel_coded[0] = 1;
725  }
726 
727  s->dsp.butterflies_float(s->coefs[0], s->coefs[1], s->block_len);
728  }
729 
730 next:
731  mdct = &s->mdct_ctx[bsize];
732 
733  for (ch = 0; ch < s->avctx->channels; ch++) {
734  int n4, index;
735 
736  n4 = s->block_len / 2;
737  if(s->channel_coded[ch]){
738  mdct->imdct_calc(mdct, s->output, s->coefs[ch]);
739  }else if(!(s->ms_stereo && ch==1))
740  memset(s->output, 0, sizeof(s->output));
741 
742  /* multiply by the window and add in the frame */
743  index = (s->frame_len / 2) + s->block_pos - n4;
744  wma_window(s, &s->frame_out[ch][index]);
745  }
746 
747  /* update block number */
748  s->block_num++;
749  s->block_pos += s->block_len;
750  if (s->block_pos >= s->frame_len)
751  return 1;
752  else
753  return 0;
754 }
755 
756 /* decode a frame of frame_len samples */
757 static int wma_decode_frame(WMACodecContext *s, float **samples,
758  int samples_offset)
759 {
760  int ret, ch;
761 
762 #ifdef TRACE
763  tprintf(s->avctx, "***decode_frame: %d size=%d\n", s->frame_count++, s->frame_len);
764 #endif
765 
766  /* read each block */
767  s->block_num = 0;
768  s->block_pos = 0;
769  for(;;) {
770  ret = wma_decode_block(s);
771  if (ret < 0)
772  return -1;
773  if (ret)
774  break;
775  }
776 
777  for (ch = 0; ch < s->avctx->channels; ch++) {
778  /* copy current block to output */
779  memcpy(samples[ch] + samples_offset, s->frame_out[ch],
780  s->frame_len * sizeof(*s->frame_out[ch]));
781  /* prepare for next block */
782  memmove(&s->frame_out[ch][0], &s->frame_out[ch][s->frame_len],
783  s->frame_len * sizeof(*s->frame_out[ch]));
784 
785 #ifdef TRACE
786  dump_floats(s, "samples", 6, samples[ch] + samples_offset, s->frame_len);
787 #endif
788  }
789 
790  return 0;
791 }
792 
793 static int wma_decode_superframe(AVCodecContext *avctx, void *data,
794  int *got_frame_ptr, AVPacket *avpkt)
795 {
796  const uint8_t *buf = avpkt->data;
797  int buf_size = avpkt->size;
798  WMACodecContext *s = avctx->priv_data;
799  int nb_frames, bit_offset, i, pos, len, ret;
800  uint8_t *q;
801  float **samples;
802  int samples_offset;
803 
804  tprintf(avctx, "***decode_superframe:\n");
805 
806  if(buf_size==0){
807  s->last_superframe_len = 0;
808  return 0;
809  }
810  if (buf_size < avctx->block_align) {
811  av_log(avctx, AV_LOG_ERROR,
812  "Input packet size too small (%d < %d)\n",
813  buf_size, avctx->block_align);
814  return AVERROR_INVALIDDATA;
815  }
816  buf_size = avctx->block_align;
817 
818  init_get_bits(&s->gb, buf, buf_size*8);
819 
820  if (s->use_bit_reservoir) {
821  /* read super frame header */
822  skip_bits(&s->gb, 4); /* super frame index */
823  nb_frames = get_bits(&s->gb, 4) - (s->last_superframe_len <= 0);
824  } else {
825  nb_frames = 1;
826  }
827 
828  /* get output buffer */
829  s->frame.nb_samples = nb_frames * s->frame_len;
830  if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
831  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
832  return ret;
833  }
834  samples = (float **)s->frame.extended_data;
835  samples_offset = 0;
836 
837  if (s->use_bit_reservoir) {
838  bit_offset = get_bits(&s->gb, s->byte_offset_bits + 3);
839  if (bit_offset > get_bits_left(&s->gb)) {
840  av_log(avctx, AV_LOG_ERROR,
841  "Invalid last frame bit offset %d > buf size %d (%d)\n",
842  bit_offset, get_bits_left(&s->gb), buf_size);
843  goto fail;
844  }
845 
846  if (s->last_superframe_len > 0) {
847  /* add bit_offset bits to last frame */
848  if ((s->last_superframe_len + ((bit_offset + 7) >> 3)) >
850  goto fail;
852  len = bit_offset;
853  while (len > 7) {
854  *q++ = (get_bits)(&s->gb, 8);
855  len -= 8;
856  }
857  if (len > 0) {
858  *q++ = (get_bits)(&s->gb, len) << (8 - len);
859  }
860  memset(q, 0, FF_INPUT_BUFFER_PADDING_SIZE);
861 
862  /* XXX: bit_offset bits into last frame */
863  init_get_bits(&s->gb, s->last_superframe, s->last_superframe_len * 8 + bit_offset);
864  /* skip unused bits */
865  if (s->last_bitoffset > 0)
866  skip_bits(&s->gb, s->last_bitoffset);
867  /* this frame is stored in the last superframe and in the
868  current one */
869  if (wma_decode_frame(s, samples, samples_offset) < 0)
870  goto fail;
871  samples_offset += s->frame_len;
872  nb_frames--;
873  }
874 
875  /* read each frame starting from bit_offset */
876  pos = bit_offset + 4 + 4 + s->byte_offset_bits + 3;
877  if (pos >= MAX_CODED_SUPERFRAME_SIZE * 8 || pos > buf_size * 8)
878  return AVERROR_INVALIDDATA;
879  init_get_bits(&s->gb, buf + (pos >> 3), (buf_size - (pos >> 3))*8);
880  len = pos & 7;
881  if (len > 0)
882  skip_bits(&s->gb, len);
883 
884  s->reset_block_lengths = 1;
885  for(i=0;i<nb_frames;i++) {
886  if (wma_decode_frame(s, samples, samples_offset) < 0)
887  goto fail;
888  samples_offset += s->frame_len;
889  }
890 
891  /* we copy the end of the frame in the last frame buffer */
892  pos = get_bits_count(&s->gb) + ((bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7);
893  s->last_bitoffset = pos & 7;
894  pos >>= 3;
895  len = buf_size - pos;
896  if (len > MAX_CODED_SUPERFRAME_SIZE || len < 0) {
897  av_log(s->avctx, AV_LOG_ERROR, "len %d invalid\n", len);
898  goto fail;
899  }
901  memcpy(s->last_superframe, buf + pos, len);
902  } else {
903  /* single frame decode */
904  if (wma_decode_frame(s, samples, samples_offset) < 0)
905  goto fail;
906  samples_offset += s->frame_len;
907  }
908 
909  av_dlog(s->avctx, "%d %d %d %d outbytes:%td eaten:%d\n",
911  (int8_t *)samples - (int8_t *)data, avctx->block_align);
912 
913  *got_frame_ptr = 1;
914  *(AVFrame *)data = s->frame;
915 
916  return avctx->block_align;
917  fail:
918  /* when error, we reset the bit reservoir */
919  s->last_superframe_len = 0;
920  return -1;
921 }
922 
923 static av_cold void flush(AVCodecContext *avctx)
924 {
925  WMACodecContext *s = avctx->priv_data;
926 
927  s->last_bitoffset=
928  s->last_superframe_len= 0;
929 }
930 
932  .name = "wmav1",
933  .type = AVMEDIA_TYPE_AUDIO,
934  .id = AV_CODEC_ID_WMAV1,
935  .priv_data_size = sizeof(WMACodecContext),
937  .close = ff_wma_end,
939  .flush = flush,
940  .capabilities = CODEC_CAP_DR1,
941  .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 1"),
942  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
944 };
945 
947  .name = "wmav2",
948  .type = AVMEDIA_TYPE_AUDIO,
949  .id = AV_CODEC_ID_WMAV2,
950  .priv_data_size = sizeof(WMACodecContext),
952  .close = ff_wma_end,
954  .flush = flush,
955  .capabilities = CODEC_CAP_DR1,
956  .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 2"),
957  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
959 };
const struct AVCodec * codec
Definition: avcodec.h:1348
static void wma_lsp_to_curve(WMACodecContext *s, float *out, float *val_max_ptr, int n, float *lsp)
NOTE: We use the same code as Vorbis here.
Definition: wmadec.c:181
static int16_t * samples
uint8_t last_superframe[MAX_CODED_SUPERFRAME_SIZE+FF_INPUT_BUFFER_PADDING_SIZE]
Definition: wma.h:124
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
AVFrame frame
Definition: wma.h:69
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:237
int next_block_len_bits
log2 of next block length
Definition: wma.h:106
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2725
static void align_get_bits(GetBitContext *s)
Definition: get_bits.h:412
static const float pow_tab[]
pow(10, i / 16.0) for i in -60..95
Definition: wmadec.c:229
int size
Definition: avcodec.h:916
GetBitContext gb
Definition: wma.h:70
int ff_wma_run_level_decode(AVCodecContext *avctx, GetBitContext *gb, VLC *vlc, const float *level_table, const uint16_t *run_table, int version, WMACoef *ptr, int offset, int num_coefs, int block_len, int frame_len_bits, int coef_nb_bits)
Decode run level compressed coefficients.
Definition: wma.c:431
int block_len
block length in samples
Definition: wma.h:108
#define AV_RL16
Definition: intreadwrite.h:42
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
AVCodec ff_wmav1_decoder
Definition: wmadec.c:931
float exponents[MAX_CHANNELS][BLOCK_MAX_SIZE]
Definition: wma.h:114
AVCodec.
Definition: avcodec.h:2960
static void wma_window(WMACodecContext *s, float *out)
Apply MDCT window and add into output.
Definition: wmadec.c:377
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:2141
#define NOISE_TAB_SIZE
Definition: wma.h:49
float lsp_pow_m_table2[(1<< LSP_POW_BITS)]
Definition: wma.h:134
static int wma_decode_block(WMACodecContext *s)
Definition: wmadec.c:429
float lsp_cos_table[BLOCK_MAX_SIZE]
Definition: wma.h:131
int high_band_start[BLOCK_NB_SIZES]
index of first coef in high band
Definition: wma.h:81
#define HGAINVLCBITS
Definition: wmadec.c:46
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2112
uint8_t
float WMACoef
type for decoded coefficients, int16_t would be enough for wma 1/2
Definition: wma.h:57
#define b
Definition: input.c:52
const uint8_t ff_aac_scalefactor_bits[121]
Definition: aactab.c:70
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1454
const char * name
int block_pos
current position in frame
Definition: wma.h:110
static int decode_exp_vlc(WMACodecContext *s, int ch)
decode exponents coded with VLC codes
Definition: wmadec.c:313
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:192
float lsp_pow_m_table1[(1<< LSP_POW_BITS)]
Definition: wma.h:133
#define EXPMAX
Definition: wmadec.c:44
static float t
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
float, planar
Definition: samplefmt.h:60
void(* vector_fmul_add)(float *dst, const float *src0, const float *src1, const float *src2, int len)
Definition: dsputil.h:356
int reset_block_lengths
Definition: wma.h:104
int nb_block_sizes
number of block sizes
Definition: wma.h:102
int ff_wma_total_gain_to_bits(int total_gain)
Definition: wma.c:360
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:547
enum AVCodecID id
Definition: avcodec.h:2974
static float pow_m1_4(WMACodecContext *s, float x)
compute x^-0.25 with an exponent and mantissa table.
Definition: wmadec.c:130
uint16_t exponent_bands[BLOCK_NB_SIZES][25]
Definition: wma.h:80
uint8_t channel_coded[MAX_CHANNELS]
true if channel is coded
Definition: wma.h:112
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
static void wma_lsp_to_curve_init(WMACodecContext *s, int frame_len)
Definition: wmadec.c:149
int last_superframe_len
Definition: wma.h:126
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
static int wma_decode_superframe(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: wmadec.c:793
FFTSample output[BLOCK_MAX_SIZE *2]
Definition: wma.h:118
#define ff_mdct_init
Definition: fft.h:146
const uint8_t ff_wma_hgain_huffbits[37]
Definition: wmadata.h:67
static int wma_decode_init(AVCodecContext *avctx)
Definition: wmadec.c:69
int noise_index
Definition: wma.h:128
void(* imdct_calc)(struct FFTContext *s, FFTSample *output, const FFTSample *input)
Definition: fft.h:81
int exponent_high_bands[BLOCK_NB_SIZES][HIGH_BAND_MAX_SIZE]
Definition: wma.h:85
int ff_wma_end(AVCodecContext *avctx)
Definition: wma.c:369
int high_band_values[MAX_CHANNELS][HIGH_BAND_MAX_SIZE]
Definition: wma.h:90
Definition: fft.h:62
int use_bit_reservoir
Definition: wma.h:73
float * windows[BLOCK_NB_SIZES]
Definition: wma.h:120
#define MAX_CODED_SUPERFRAME_SIZE
Definition: wma.h:45
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Get a buffer for a frame.
Definition: utils.c:464
uint16_t * run_table[2]
Definition: wma.h:95
const uint16_t ff_wma_hgain_huffcodes[37]
Definition: wmadata.h:59
int version
1 = 0x160 (WMAV1), 2 = 0x161 (WMAV2)
Definition: wma.h:72
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:515
int frame_len
frame length in samples
Definition: wma.h:100
int last_bitoffset
Definition: wma.h:125
static int noise(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args, uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size, int keyframe)
Definition: noise_bsf.c:28
static av_cold void flush(AVCodecContext *avctx)
Definition: wmadec.c:923
int frame_len_bits
frame_len = 1 << frame_len_bits
Definition: wma.h:101
external API header
static int wma_decode_frame(WMACodecContext *s, float **samples, int samples_offset)
Definition: wmadec.c:757
AV_SAMPLE_FMT_NONE
Definition: avconv_filter.c:63
#define HIGH_BAND_MAX_SIZE
Definition: wma.h:40
int use_exp_vlc
exponent coding: 0 = lsp, 1 = vlc + delta
Definition: wma.h:75
VLC coef_vlc[2]
Definition: wma.h:94
#define NB_LSP_COEFS
Definition: wma.h:42
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
void(* butterflies_float)(float *restrict v1, float *restrict v2, int len)
Calculate the sum and difference of two vectors of floats.
Definition: dsputil.h:374
#define init_vlc(vlc, nb_bits, nb_codes,bits, bits_wrap, bits_size,codes, codes_wrap, codes_size,flags)
Definition: get_bits.h:418
AVCodecContext * avctx
Definition: wma.h:68
float frame_out[MAX_CHANNELS][BLOCK_MAX_SIZE *2]
Definition: wma.h:122
static int16_t mult(Float11 *f1, Float11 *f2)
Definition: g726.c:56
int extradata_size
Definition: avcodec.h:1455
int exponent_high_sizes[BLOCK_NB_SIZES]
Definition: wma.h:84
static void decode_exp_lsp(WMACodecContext *s, int ch)
decode exponents coded with LSP coefficients (same idea as Vorbis)
Definition: wmadec.c:211
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:268
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
Definition: utils.c:602
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:260
int index
Definition: gxfenc.c:72
int block_num
block number in current frame
Definition: wma.h:109
int use_noise_coding
true if perceptual noise is added
Definition: wma.h:76
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:372
float * level_table[2]
Definition: wma.h:96
#define LSP_POW_BITS
Definition: wma.h:51
#define MAX_CHANNELS
Definition: aac.h:43
int use_variable_block_len
Definition: wma.h:74
uint8_t ms_stereo
true if mid/side stereo mode
Definition: wma.h:111
VLC exp_vlc
Definition: wma.h:78
FFTContext mdct_ctx[BLOCK_NB_SIZES]
Definition: wma.h:119
const uint32_t ff_aac_scalefactor_code[121]
Definition: aactab.c:51
int exponents_bsize[MAX_CHANNELS]
log2 ratio frame/exp. length
Definition: wma.h:113
void(* vector_fmul_reverse)(float *dst, const float *src0, const float *src1, int len)
Definition: dsputil.h:354
float coefs[MAX_CHANNELS][BLOCK_MAX_SIZE]
Definition: wma.h:117
int prev_block_len_bits
log2 of prev block length
Definition: wma.h:107
int coefs_end[BLOCK_NB_SIZES]
max number of coded coefficients
Definition: wma.h:83
struct WMACodecContext WMACodecContext
float lsp_pow_e_table[256]
Definition: wma.h:132
#define tprintf(p,...)
Definition: get_bits.h:613
const float ff_wma_lsp_codebook[NB_LSP_COEFS][16]
Definition: wmadata.h:75
common internal api header.
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
void * priv_data
Definition: avcodec.h:1382
DSPContext dsp
Definition: wma.h:135
int len
int channels
number of audio channels
Definition: avcodec.h:2105
#define av_log2
Definition: intmath.h:85
VLC_TYPE(* table)[2]
code, bits
Definition: get_bits.h:65
#define EXPVLCBITS
Definition: wmadec.c:43
WMACoef coefs1[MAX_CHANNELS][BLOCK_MAX_SIZE]
Definition: wma.h:116
AVCodec ff_wmav2_decoder
Definition: wmadec.c:946
#define HGAINMAX
Definition: wmadec.c:47
static const struct twinvq_data tab
float max_exponent[MAX_CHANNELS]
Definition: wma.h:115
VLC hgain_vlc
Definition: wma.h:86
int coefs_start
first coded coef
Definition: wma.h:82
int block_len_bits
log2 of current block length
Definition: wma.h:105
int byte_offset_bits
Definition: wma.h:77
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: avcodec.h:1028
This structure stores compressed data.
Definition: avcodec.h:898
int nb_samples
number of audio samples (per channel) described by this frame
Definition: avcodec.h:1042
int high_band_coded[MAX_CHANNELS][HIGH_BAND_MAX_SIZE]
Definition: wma.h:89
int ff_wma_init(AVCodecContext *avctx, int flags2)
Definition: wma.c:71
float noise_table[NOISE_TAB_SIZE]
Definition: wma.h:127
float noise_mult
Definition: wma.h:129
if(!(ptr_align%ac->ptr_align)&&samples_align >=aligned_len)