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);
90 
91  if (avctx->codec->id == AV_CODEC_ID_WMAV1)
92  s->version = 1;
93  else
94  s->version = 2;
95 
96  /* compute MDCT block size */
98  s->version, 0);
102 
103  s->frame_len = 1 << s->frame_len_bits;
104  if (s->use_variable_block_len) {
105  int nb_max, nb;
106  nb = ((flags2 >> 3) & 3) + 1;
107  if ((avctx->bit_rate / avctx->channels) >= 32000)
108  nb += 2;
109  nb_max = s->frame_len_bits - BLOCK_MIN_BITS;
110  if (nb > nb_max)
111  nb = nb_max;
112  s->nb_block_sizes = nb + 1;
113  } else
114  s->nb_block_sizes = 1;
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  bps = (float) avctx->bit_rate /
136  (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  } else if (sample_rate1 == 22050) {
154  if (bps1 >= 1.16)
155  s->use_noise_coding = 0;
156  else if (bps1 >= 0.72)
157  high_freq = high_freq * 0.7;
158  else
159  high_freq = high_freq * 0.6;
160  } else if (sample_rate1 == 16000) {
161  if (bps > 0.5)
162  high_freq = high_freq * 0.5;
163  else
164  high_freq = high_freq * 0.3;
165  } else if (sample_rate1 == 11025)
166  high_freq = high_freq * 0.7;
167  else if (sample_rate1 == 8000) {
168  if (bps <= 0.625)
169  high_freq = high_freq * 0.5;
170  else if (bps > 0.75)
171  s->use_noise_coding = 0;
172  else
173  high_freq = high_freq * 0.65;
174  } else {
175  if (bps >= 0.8)
176  high_freq = high_freq * 0.75;
177  else if (bps >= 0.6)
178  high_freq = high_freq * 0.6;
179  else
180  high_freq = high_freq * 0.5;
181  }
182  av_dlog(s->avctx, "flags2=0x%x\n", flags2);
183  av_dlog(s->avctx, "version=%d channels=%d sample_rate=%d bitrate=%d block_align=%d\n",
184  s->version, avctx->channels, avctx->sample_rate, avctx->bit_rate,
185  avctx->block_align);
186  av_dlog(s->avctx, "bps=%f bps1=%f high_freq=%f bitoffset=%d\n",
187  bps, bps1, high_freq, s->byte_offset_bits);
188  av_dlog(s->avctx, "use_noise_coding=%d use_exp_vlc=%d nb_block_sizes=%d\n",
190 
191  /* compute the scale factor band sizes for each MDCT block size */
192  {
193  int a, b, pos, lpos, k, block_len, i, j, n;
194  const uint8_t *table;
195 
196  if (s->version == 1)
197  s->coefs_start = 3;
198  else
199  s->coefs_start = 0;
200  for (k = 0; k < s->nb_block_sizes; k++) {
201  block_len = s->frame_len >> k;
202 
203  if (s->version == 1) {
204  lpos = 0;
205  for (i = 0; i < 25; i++) {
206  a = ff_wma_critical_freqs[i];
207  b = avctx->sample_rate;
208  pos = ((block_len * 2 * a) + (b >> 1)) / b;
209  if (pos > block_len)
210  pos = block_len;
211  s->exponent_bands[0][i] = pos - lpos;
212  if (pos >= block_len) {
213  i++;
214  break;
215  }
216  lpos = pos;
217  }
218  s->exponent_sizes[0] = i;
219  } else {
220  /* hardcoded tables */
221  table = NULL;
222  a = s->frame_len_bits - BLOCK_MIN_BITS - k;
223  if (a < 3) {
224  if (avctx->sample_rate >= 44100)
225  table = exponent_band_44100[a];
226  else if (avctx->sample_rate >= 32000)
227  table = exponent_band_32000[a];
228  else if (avctx->sample_rate >= 22050)
229  table = exponent_band_22050[a];
230  }
231  if (table) {
232  n = *table++;
233  for (i = 0; i < n; i++)
234  s->exponent_bands[k][i] = table[i];
235  s->exponent_sizes[k] = n;
236  } else {
237  j = 0;
238  lpos = 0;
239  for (i = 0; i < 25; i++) {
240  a = ff_wma_critical_freqs[i];
241  b = avctx->sample_rate;
242  pos = ((block_len * 2 * a) + (b << 1)) / (4 * b);
243  pos <<= 2;
244  if (pos > block_len)
245  pos = block_len;
246  if (pos > lpos)
247  s->exponent_bands[k][j++] = pos - lpos;
248  if (pos >= block_len)
249  break;
250  lpos = pos;
251  }
252  s->exponent_sizes[k] = j;
253  }
254  }
255 
256  /* max number of coefs */
257  s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k;
258  /* high freq computation */
259  s->high_band_start[k] = (int) ((block_len * 2 * high_freq) /
260  avctx->sample_rate + 0.5);
261  n = s->exponent_sizes[k];
262  j = 0;
263  pos = 0;
264  for (i = 0; i < n; i++) {
265  int start, end;
266  start = pos;
267  pos += s->exponent_bands[k][i];
268  end = pos;
269  if (start < s->high_band_start[k])
270  start = s->high_band_start[k];
271  if (end > s->coefs_end[k])
272  end = s->coefs_end[k];
273  if (end > start)
274  s->exponent_high_bands[k][j++] = end - start;
275  }
276  s->exponent_high_sizes[k] = j;
277 #if 0
278  tprintf(s->avctx, "%5d: coefs_end=%d high_band_start=%d nb_high_bands=%d: ",
279  s->frame_len >> k,
280  s->coefs_end[k],
281  s->high_band_start[k],
282  s->exponent_high_sizes[k]);
283  for (j = 0; j < s->exponent_high_sizes[k]; j++)
284  tprintf(s->avctx, " %d", s->exponent_high_bands[k][j]);
285  tprintf(s->avctx, "\n");
286 #endif /* 0 */
287  }
288  }
289 
290 #ifdef TRACE
291  {
292  int i, j;
293  for (i = 0; i < s->nb_block_sizes; i++) {
294  tprintf(s->avctx, "%5d: n=%2d:",
295  s->frame_len >> i,
296  s->exponent_sizes[i]);
297  for (j = 0; j < s->exponent_sizes[i]; j++)
298  tprintf(s->avctx, " %d", s->exponent_bands[i][j]);
299  tprintf(s->avctx, "\n");
300  }
301  }
302 #endif /* TRACE */
303 
304  /* init MDCT windows : simple sine window */
305  for (i = 0; i < s->nb_block_sizes; i++) {
307  s->windows[i] = ff_sine_windows[s->frame_len_bits - i];
308  }
309 
310  s->reset_block_lengths = 1;
311 
312  if (s->use_noise_coding) {
313  /* init the noise generator */
314  if (s->use_exp_vlc)
315  s->noise_mult = 0.02;
316  else
317  s->noise_mult = 0.04;
318 
319 #ifdef TRACE
320  for (i = 0; i < NOISE_TAB_SIZE; i++)
321  s->noise_table[i] = 1.0 * s->noise_mult;
322 #else
323  {
324  unsigned int seed;
325  float norm;
326  seed = 1;
327  norm = (1.0 / (float) (1LL << 31)) * sqrt(3) * s->noise_mult;
328  for (i = 0; i < NOISE_TAB_SIZE; i++) {
329  seed = seed * 314159 + 1;
330  s->noise_table[i] = (float) ((int) seed) * norm;
331  }
332  }
333 #endif /* TRACE */
334  }
335 
336  /* choose the VLC tables for the coefficients */
337  coef_vlc_table = 2;
338  if (avctx->sample_rate >= 32000) {
339  if (bps1 < 0.72)
340  coef_vlc_table = 0;
341  else if (bps1 < 1.16)
342  coef_vlc_table = 1;
343  }
344  s->coef_vlcs[0] = &coef_vlcs[coef_vlc_table * 2];
345  s->coef_vlcs[1] = &coef_vlcs[coef_vlc_table * 2 + 1];
346  init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0],
347  &s->int_table[0], s->coef_vlcs[0]);
348  init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1],
349  &s->int_table[1], s->coef_vlcs[1]);
350 
351  return 0;
352 }
353 
354 int ff_wma_total_gain_to_bits(int total_gain)
355 {
356  if (total_gain < 15)
357  return 13;
358  else if (total_gain < 32)
359  return 12;
360  else if (total_gain < 40)
361  return 11;
362  else if (total_gain < 45)
363  return 10;
364  else
365  return 9;
366 }
367 
369 {
370  WMACodecContext *s = avctx->priv_data;
371  int i;
372 
373  for (i = 0; i < s->nb_block_sizes; i++)
374  ff_mdct_end(&s->mdct_ctx[i]);
375 
376  if (s->use_exp_vlc)
377  ff_free_vlc(&s->exp_vlc);
378  if (s->use_noise_coding)
379  ff_free_vlc(&s->hgain_vlc);
380  for (i = 0; i < 2; i++) {
381  ff_free_vlc(&s->coef_vlc[i]);
382  av_freep(&s->run_table[i]);
383  av_freep(&s->level_table[i]);
384  av_freep(&s->int_table[i]);
385  }
386 
387  return 0;
388 }
389 
390 /**
391  * Decode an uncompressed coefficient.
392  * @param gb GetBitContext
393  * @return the decoded coefficient
394  */
396 {
397  /** consumes up to 34 bits */
398  int n_bits = 8;
399  /** decode length */
400  if (get_bits1(gb)) {
401  n_bits += 8;
402  if (get_bits1(gb)) {
403  n_bits += 8;
404  if (get_bits1(gb))
405  n_bits += 7;
406  }
407  }
408  return get_bits_long(gb, n_bits);
409 }
410 
411 /**
412  * Decode run level compressed coefficients.
413  * @param avctx codec context
414  * @param gb bitstream reader context
415  * @param vlc vlc table for get_vlc2
416  * @param level_table level codes
417  * @param run_table run codes
418  * @param version 0 for wma1,2 1 for wmapro
419  * @param ptr output buffer
420  * @param offset offset in the output buffer
421  * @param num_coefs number of input coefficents
422  * @param block_len input buffer length (2^n)
423  * @param frame_len_bits number of bits for escaped run codes
424  * @param coef_nb_bits number of bits for escaped level codes
425  * @return 0 on success, -1 otherwise
426  */
428  VLC *vlc, const float *level_table,
429  const uint16_t *run_table, int version,
430  WMACoef *ptr, int offset, int num_coefs,
431  int block_len, int frame_len_bits,
432  int coef_nb_bits)
433 {
434  int code, level, sign;
435  const uint32_t *ilvl = (const uint32_t *) level_table;
436  uint32_t *iptr = (uint32_t *) ptr;
437  const unsigned int coef_mask = block_len - 1;
438  for (; offset < num_coefs; offset++) {
439  code = get_vlc2(gb, vlc->table, VLCBITS, VLCMAX);
440  if (code > 1) {
441  /** normal code */
442  offset += run_table[code];
443  sign = get_bits1(gb) - 1;
444  iptr[offset & coef_mask] = ilvl[code] ^ sign << 31;
445  } else if (code == 1) {
446  /** EOB */
447  break;
448  } else {
449  /** escape */
450  if (!version) {
451  level = get_bits(gb, coef_nb_bits);
452  /** NOTE: this is rather suboptimal. reading
453  * block_len_bits would be better */
454  offset += get_bits(gb, frame_len_bits);
455  } else {
456  level = ff_wma_get_large_val(gb);
457  /** escape decode */
458  if (get_bits1(gb)) {
459  if (get_bits1(gb)) {
460  if (get_bits1(gb)) {
461  av_log(avctx, AV_LOG_ERROR,
462  "broken escape sequence\n");
463  return -1;
464  } else
465  offset += get_bits(gb, frame_len_bits) + 4;
466  } else
467  offset += get_bits(gb, 2) + 1;
468  }
469  }
470  sign = get_bits1(gb) - 1;
471  ptr[offset & coef_mask] = (level ^ sign) - sign;
472  }
473  }
474  /** NOTE: EOB can be omitted */
475  if (offset > num_coefs) {
476  av_log(avctx, AV_LOG_ERROR, "overflow in spectral RLE, ignoring\n");
477  return -1;
478  }
479 
480  return 0;
481 }