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