FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
wma.c
Go to the documentation of this file.
1 /*
2  * WMA compatible codec
3  * Copyright (c) 2002-2007 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 #include "libavutil/attributes.h"
23 
24 #include "avcodec.h"
25 #include "sinewin.h"
26 #include "wma.h"
27 #include "wma_common.h"
28 #include "wma_freqs.h"
29 #include "wmadata.h"
30 
31 #undef NDEBUG
32 #include <assert.h>
33 
34 /* XXX: use same run/length optimization as mpeg decoders */
35 // FIXME maybe split decode / encode or pass flag
36 static av_cold int init_coef_vlc(VLC *vlc, uint16_t **prun_table,
37  float **plevel_table, uint16_t **pint_table,
38  const CoefVLCTable *vlc_table)
39 {
40  int n = vlc_table->n;
41  const uint8_t *table_bits = vlc_table->huffbits;
42  const uint32_t *table_codes = vlc_table->huffcodes;
43  const uint16_t *levels_table = vlc_table->levels;
44  uint16_t *run_table, *level_table, *int_table;
45  float *flevel_table;
46  int i, l, j, k, level;
47 
48  init_vlc(vlc, VLCBITS, n, table_bits, 1, 1, table_codes, 4, 4, 0);
49 
50  run_table = av_malloc_array(n, sizeof(uint16_t));
51  level_table = av_malloc_array(n, sizeof(uint16_t));
52  flevel_table = av_malloc_array(n, sizeof(*flevel_table));
53  int_table = av_malloc_array(n, sizeof(uint16_t));
54  if (!run_table || !level_table || !flevel_table || !int_table) {
55  av_freep(&run_table);
56  av_freep(&level_table);
57  av_freep(&flevel_table);
58  av_freep(&int_table);
59  return AVERROR(ENOMEM);
60  }
61  i = 2;
62  level = 1;
63  k = 0;
64  while (i < n) {
65  int_table[k] = i;
66  l = levels_table[k++];
67  for (j = 0; j < l; j++) {
68  run_table[i] = j;
69  level_table[i] = level;
70  flevel_table[i] = level;
71  i++;
72  }
73  level++;
74  }
75  *prun_table = run_table;
76  *plevel_table = flevel_table;
77  *pint_table = int_table;
78  av_free(level_table);
79 
80  return 0;
81 }
82 
83 av_cold int ff_wma_init(AVCodecContext *avctx, int flags2)
84 {
85  WMACodecContext *s = avctx->priv_data;
86  int i, ret;
87  float bps1, high_freq;
88  volatile float bps;
89  int sample_rate1;
90  int coef_vlc_table;
91 
92  if (avctx->sample_rate <= 0 || avctx->sample_rate > 50000 ||
93  avctx->channels <= 0 || avctx->channels > 2 ||
94  avctx->bit_rate <= 0)
95  return -1;
96 
97  ff_fmt_convert_init(&s->fmt_conv, avctx);
98 
99  if (avctx->codec->id == AV_CODEC_ID_WMAV1)
100  s->version = 1;
101  else
102  s->version = 2;
103 
104  /* compute MDCT block size */
106  s->version, 0);
110 
111  s->frame_len = 1 << s->frame_len_bits;
112  if (s->use_variable_block_len) {
113  int nb_max, nb;
114  nb = ((flags2 >> 3) & 3) + 1;
115  if ((avctx->bit_rate / avctx->channels) >= 32000)
116  nb += 2;
117  nb_max = s->frame_len_bits - BLOCK_MIN_BITS;
118  if (nb > nb_max)
119  nb = nb_max;
120  s->nb_block_sizes = nb + 1;
121  } else
122  s->nb_block_sizes = 1;
123 
124  /* init rate dependent parameters */
125  s->use_noise_coding = 1;
126  high_freq = avctx->sample_rate * 0.5;
127 
128  /* if version 2, then the rates are normalized */
129  sample_rate1 = avctx->sample_rate;
130  if (s->version == 2) {
131  if (sample_rate1 >= 44100)
132  sample_rate1 = 44100;
133  else if (sample_rate1 >= 22050)
134  sample_rate1 = 22050;
135  else if (sample_rate1 >= 16000)
136  sample_rate1 = 16000;
137  else if (sample_rate1 >= 11025)
138  sample_rate1 = 11025;
139  else if (sample_rate1 >= 8000)
140  sample_rate1 = 8000;
141  }
142 
143  bps = (float) avctx->bit_rate /
144  (float) (avctx->channels * avctx->sample_rate);
145  s->byte_offset_bits = av_log2((int) (bps * s->frame_len / 8.0 + 0.5)) + 2;
146  if (s->byte_offset_bits + 3 > MIN_CACHE_BITS) {
147  av_log(avctx, AV_LOG_ERROR, "byte_offset_bits %d is too large\n", s->byte_offset_bits);
148  return AVERROR_PATCHWELCOME;
149  }
150 
151  /* compute high frequency value and choose if noise coding should
152  * be activated */
153  bps1 = bps;
154  if (avctx->channels == 2)
155  bps1 = bps * 1.6;
156  if (sample_rate1 == 44100) {
157  if (bps1 >= 0.61)
158  s->use_noise_coding = 0;
159  else
160  high_freq = high_freq * 0.4;
161  } else if (sample_rate1 == 22050) {
162  if (bps1 >= 1.16)
163  s->use_noise_coding = 0;
164  else if (bps1 >= 0.72)
165  high_freq = high_freq * 0.7;
166  else
167  high_freq = high_freq * 0.6;
168  } else if (sample_rate1 == 16000) {
169  if (bps > 0.5)
170  high_freq = high_freq * 0.5;
171  else
172  high_freq = high_freq * 0.3;
173  } else if (sample_rate1 == 11025)
174  high_freq = high_freq * 0.7;
175  else if (sample_rate1 == 8000) {
176  if (bps <= 0.625)
177  high_freq = high_freq * 0.5;
178  else if (bps > 0.75)
179  s->use_noise_coding = 0;
180  else
181  high_freq = high_freq * 0.65;
182  } else {
183  if (bps >= 0.8)
184  high_freq = high_freq * 0.75;
185  else if (bps >= 0.6)
186  high_freq = high_freq * 0.6;
187  else
188  high_freq = high_freq * 0.5;
189  }
190  av_dlog(s->avctx, "flags2=0x%x\n", flags2);
191  av_dlog(s->avctx, "version=%d channels=%d sample_rate=%d bitrate=%d block_align=%d\n",
192  s->version, avctx->channels, avctx->sample_rate, avctx->bit_rate,
193  avctx->block_align);
194  av_dlog(s->avctx, "bps=%f bps1=%f high_freq=%f bitoffset=%d\n",
195  bps, bps1, high_freq, s->byte_offset_bits);
196  av_dlog(s->avctx, "use_noise_coding=%d use_exp_vlc=%d nb_block_sizes=%d\n",
198 
199  /* compute the scale factor band sizes for each MDCT block size */
200  {
201  int a, b, pos, lpos, k, block_len, i, j, n;
202  const uint8_t *table;
203 
204  if (s->version == 1)
205  s->coefs_start = 3;
206  else
207  s->coefs_start = 0;
208  for (k = 0; k < s->nb_block_sizes; k++) {
209  block_len = s->frame_len >> k;
210 
211  if (s->version == 1) {
212  lpos = 0;
213  for (i = 0; i < 25; i++) {
214  a = ff_wma_critical_freqs[i];
215  b = avctx->sample_rate;
216  pos = ((block_len * 2 * a) + (b >> 1)) / b;
217  if (pos > block_len)
218  pos = block_len;
219  s->exponent_bands[0][i] = pos - lpos;
220  if (pos >= block_len) {
221  i++;
222  break;
223  }
224  lpos = pos;
225  }
226  s->exponent_sizes[0] = i;
227  } else {
228  /* hardcoded tables */
229  table = NULL;
230  a = s->frame_len_bits - BLOCK_MIN_BITS - k;
231  if (a < 3) {
232  if (avctx->sample_rate >= 44100)
233  table = exponent_band_44100[a];
234  else if (avctx->sample_rate >= 32000)
235  table = exponent_band_32000[a];
236  else if (avctx->sample_rate >= 22050)
237  table = exponent_band_22050[a];
238  }
239  if (table) {
240  n = *table++;
241  for (i = 0; i < n; i++)
242  s->exponent_bands[k][i] = table[i];
243  s->exponent_sizes[k] = n;
244  } else {
245  j = 0;
246  lpos = 0;
247  for (i = 0; i < 25; i++) {
248  a = ff_wma_critical_freqs[i];
249  b = avctx->sample_rate;
250  pos = ((block_len * 2 * a) + (b << 1)) / (4 * b);
251  pos <<= 2;
252  if (pos > block_len)
253  pos = block_len;
254  if (pos > lpos)
255  s->exponent_bands[k][j++] = pos - lpos;
256  if (pos >= block_len)
257  break;
258  lpos = pos;
259  }
260  s->exponent_sizes[k] = j;
261  }
262  }
263 
264  /* max number of coefs */
265  s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k;
266  /* high freq computation */
267  s->high_band_start[k] = (int) ((block_len * 2 * high_freq) /
268  avctx->sample_rate + 0.5);
269  n = s->exponent_sizes[k];
270  j = 0;
271  pos = 0;
272  for (i = 0; i < n; i++) {
273  int start, end;
274  start = pos;
275  pos += s->exponent_bands[k][i];
276  end = pos;
277  if (start < s->high_band_start[k])
278  start = s->high_band_start[k];
279  if (end > s->coefs_end[k])
280  end = s->coefs_end[k];
281  if (end > start)
282  s->exponent_high_bands[k][j++] = end - start;
283  }
284  s->exponent_high_sizes[k] = j;
285 #if 0
286  tprintf(s->avctx, "%5d: coefs_end=%d high_band_start=%d nb_high_bands=%d: ",
287  s->frame_len >> k,
288  s->coefs_end[k],
289  s->high_band_start[k],
290  s->exponent_high_sizes[k]);
291  for (j = 0; j < s->exponent_high_sizes[k]; j++)
292  tprintf(s->avctx, " %d", s->exponent_high_bands[k][j]);
293  tprintf(s->avctx, "\n");
294 #endif /* 0 */
295  }
296  }
297 
298 #ifdef TRACE
299  {
300  int i, j;
301  for (i = 0; i < s->nb_block_sizes; i++) {
302  tprintf(s->avctx, "%5d: n=%2d:",
303  s->frame_len >> i,
304  s->exponent_sizes[i]);
305  for (j = 0; j < s->exponent_sizes[i]; j++)
306  tprintf(s->avctx, " %d", s->exponent_bands[i][j]);
307  tprintf(s->avctx, "\n");
308  }
309  }
310 #endif /* TRACE */
311 
312  /* init MDCT windows : simple sine window */
313  for (i = 0; i < s->nb_block_sizes; i++) {
315  s->windows[i] = ff_sine_windows[s->frame_len_bits - i];
316  }
317 
318  s->reset_block_lengths = 1;
319 
320  if (s->use_noise_coding) {
321  /* init the noise generator */
322  if (s->use_exp_vlc)
323  s->noise_mult = 0.02;
324  else
325  s->noise_mult = 0.04;
326 
327 #ifdef TRACE
328  for (i = 0; i < NOISE_TAB_SIZE; i++)
329  s->noise_table[i] = 1.0 * s->noise_mult;
330 #else
331  {
332  unsigned int seed;
333  float norm;
334  seed = 1;
335  norm = (1.0 / (float) (1LL << 31)) * sqrt(3) * s->noise_mult;
336  for (i = 0; i < NOISE_TAB_SIZE; i++) {
337  seed = seed * 314159 + 1;
338  s->noise_table[i] = (float) ((int) seed) * norm;
339  }
340  }
341 #endif /* TRACE */
342  }
343 
344  s->fdsp = avpriv_float_dsp_alloc(avctx->flags & CODEC_FLAG_BITEXACT);
345  if (!s->fdsp)
346  return AVERROR(ENOMEM);
347 
348  /* choose the VLC tables for the coefficients */
349  coef_vlc_table = 2;
350  if (avctx->sample_rate >= 32000) {
351  if (bps1 < 0.72)
352  coef_vlc_table = 0;
353  else if (bps1 < 1.16)
354  coef_vlc_table = 1;
355  }
356  s->coef_vlcs[0] = &coef_vlcs[coef_vlc_table * 2];
357  s->coef_vlcs[1] = &coef_vlcs[coef_vlc_table * 2 + 1];
358  ret = init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0],
359  &s->int_table[0], s->coef_vlcs[0]);
360  if (ret < 0)
361  return ret;
362 
363  return init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1],
364  &s->int_table[1], s->coef_vlcs[1]);
365 }
366 
367 int ff_wma_total_gain_to_bits(int total_gain)
368 {
369  if (total_gain < 15)
370  return 13;
371  else if (total_gain < 32)
372  return 12;
373  else if (total_gain < 40)
374  return 11;
375  else if (total_gain < 45)
376  return 10;
377  else
378  return 9;
379 }
380 
382 {
383  WMACodecContext *s = avctx->priv_data;
384  int i;
385 
386  for (i = 0; i < s->nb_block_sizes; i++)
387  ff_mdct_end(&s->mdct_ctx[i]);
388 
389  if (s->use_exp_vlc)
390  ff_free_vlc(&s->exp_vlc);
391  if (s->use_noise_coding)
392  ff_free_vlc(&s->hgain_vlc);
393  for (i = 0; i < 2; i++) {
394  ff_free_vlc(&s->coef_vlc[i]);
395  av_freep(&s->run_table[i]);
396  av_freep(&s->level_table[i]);
397  av_freep(&s->int_table[i]);
398  }
399  av_freep(&s->fdsp);
400 
401  return 0;
402 }
403 
404 /**
405  * Decode an uncompressed coefficient.
406  * @param gb GetBitContext
407  * @return the decoded coefficient
408  */
410 {
411  /** consumes up to 34 bits */
412  int n_bits = 8;
413  /** decode length */
414  if (get_bits1(gb)) {
415  n_bits += 8;
416  if (get_bits1(gb)) {
417  n_bits += 8;
418  if (get_bits1(gb))
419  n_bits += 7;
420  }
421  }
422  return get_bits_long(gb, n_bits);
423 }
424 
425 /**
426  * Decode run level compressed coefficients.
427  * @param avctx codec context
428  * @param gb bitstream reader context
429  * @param vlc vlc table for get_vlc2
430  * @param level_table level codes
431  * @param run_table run codes
432  * @param version 0 for wma1,2 1 for wmapro
433  * @param ptr output buffer
434  * @param offset offset in the output buffer
435  * @param num_coefs number of input coefficents
436  * @param block_len input buffer length (2^n)
437  * @param frame_len_bits number of bits for escaped run codes
438  * @param coef_nb_bits number of bits for escaped level codes
439  * @return 0 on success, -1 otherwise
440  */
442  VLC *vlc, const float *level_table,
443  const uint16_t *run_table, int version,
444  WMACoef *ptr, int offset, int num_coefs,
445  int block_len, int frame_len_bits,
446  int coef_nb_bits)
447 {
448  int code, level, sign;
449  const uint32_t *ilvl = (const uint32_t *) level_table;
450  uint32_t *iptr = (uint32_t *) ptr;
451  const unsigned int coef_mask = block_len - 1;
452  for (; offset < num_coefs; offset++) {
453  code = get_vlc2(gb, vlc->table, VLCBITS, VLCMAX);
454  if (code > 1) {
455  /** normal code */
456  offset += run_table[code];
457  sign = get_bits1(gb) - 1;
458  iptr[offset & coef_mask] = ilvl[code] ^ sign << 31;
459  } else if (code == 1) {
460  /** EOB */
461  break;
462  } else {
463  /** escape */
464  if (!version) {
465  level = get_bits(gb, coef_nb_bits);
466  /** NOTE: this is rather suboptimal. reading
467  * block_len_bits would be better */
468  offset += get_bits(gb, frame_len_bits);
469  } else {
470  level = ff_wma_get_large_val(gb);
471  /** escape decode */
472  if (get_bits1(gb)) {
473  if (get_bits1(gb)) {
474  if (get_bits1(gb)) {
475  av_log(avctx, AV_LOG_ERROR,
476  "broken escape sequence\n");
477  return -1;
478  } else
479  offset += get_bits(gb, frame_len_bits) + 4;
480  } else
481  offset += get_bits(gb, 2) + 1;
482  }
483  }
484  sign = get_bits1(gb) - 1;
485  ptr[offset & coef_mask] = (level ^ sign) - sign;
486  }
487  }
488  /** NOTE: EOB can be omitted */
489  if (offset > num_coefs) {
490  av_log(avctx, AV_LOG_ERROR, "overflow in spectral RLE, ignoring\n");
491  return -1;
492  }
493 
494  return 0;
495 }