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