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