Libav
wma.c
Go to the documentation of this file.
1 /*
2  * WMA compatible codec
3  * Copyright (c) 2002-2007 The Libav Project
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 "internal.h"
26 #include "sinewin.h"
27 #include "wma.h"
28 #include "wma_common.h"
29 #include "wma_freqs.h"
30 #include "wmadata.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 int 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  if (!run_table || !level_table || !flevel_table || !int_table) {
53  av_freep(&run_table);
54  av_freep(&level_table);
55  av_freep(&flevel_table);
56  av_freep(&int_table);
57  return AVERROR(ENOMEM);
58  }
59  i = 2;
60  level = 1;
61  k = 0;
62  while (i < n) {
63  int_table[k] = i;
64  l = levels_table[k++];
65  for (j = 0; j < l; j++) {
66  run_table[i] = j;
67  level_table[i] = level;
68  flevel_table[i] = level;
69  i++;
70  }
71  level++;
72  }
73  *prun_table = run_table;
74  *plevel_table = flevel_table;
75  *pint_table = int_table;
76  av_free(level_table);
77 
78  return 0;
79 }
80 
81 av_cold int ff_wma_init(AVCodecContext *avctx, int flags2)
82 {
83  WMACodecContext *s = avctx->priv_data;
84  int i, ret;
85  float bps1, high_freq;
86  volatile float bps;
87  int sample_rate1;
88  int coef_vlc_table;
89 
90  if (avctx->sample_rate <= 0 || avctx->sample_rate > 50000 ||
91  avctx->channels <= 0 || avctx->channels > 2 ||
92  avctx->bit_rate <= 0)
93  return -1;
94 
96 
97  if (avctx->codec->id == AV_CODEC_ID_WMAV1)
98  s->version = 1;
99  else
100  s->version = 2;
101 
102  /* compute MDCT block size */
104  s->version, 0);
108 
109  s->frame_len = 1 << s->frame_len_bits;
110  if (s->use_variable_block_len) {
111  int nb_max, nb;
112  nb = ((flags2 >> 3) & 3) + 1;
113  if ((avctx->bit_rate / avctx->channels) >= 32000)
114  nb += 2;
115  nb_max = s->frame_len_bits - BLOCK_MIN_BITS;
116  if (nb > nb_max)
117  nb = nb_max;
118  s->nb_block_sizes = nb + 1;
119  } else
120  s->nb_block_sizes = 1;
121 
122  /* init rate dependent parameters */
123  s->use_noise_coding = 1;
124  high_freq = avctx->sample_rate * 0.5;
125 
126  /* if version 2, then the rates are normalized */
127  sample_rate1 = avctx->sample_rate;
128  if (s->version == 2) {
129  if (sample_rate1 >= 44100)
130  sample_rate1 = 44100;
131  else if (sample_rate1 >= 22050)
132  sample_rate1 = 22050;
133  else if (sample_rate1 >= 16000)
134  sample_rate1 = 16000;
135  else if (sample_rate1 >= 11025)
136  sample_rate1 = 11025;
137  else if (sample_rate1 >= 8000)
138  sample_rate1 = 8000;
139  }
140 
141  bps = (float) avctx->bit_rate /
142  (float) (avctx->channels * avctx->sample_rate);
143  s->byte_offset_bits = av_log2((int) (bps * s->frame_len / 8.0 + 0.5)) + 2;
144 
145  /* compute high frequency value and choose if noise coding should
146  * be activated */
147  bps1 = bps;
148  if (avctx->channels == 2)
149  bps1 = bps * 1.6;
150  if (sample_rate1 == 44100) {
151  if (bps1 >= 0.61)
152  s->use_noise_coding = 0;
153  else
154  high_freq = high_freq * 0.4;
155  } else if (sample_rate1 == 22050) {
156  if (bps1 >= 1.16)
157  s->use_noise_coding = 0;
158  else if (bps1 >= 0.72)
159  high_freq = high_freq * 0.7;
160  else
161  high_freq = high_freq * 0.6;
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  } else if (sample_rate1 == 11025)
168  high_freq = high_freq * 0.7;
169  else if (sample_rate1 == 8000) {
170  if (bps <= 0.625)
171  high_freq = high_freq * 0.5;
172  else if (bps > 0.75)
173  s->use_noise_coding = 0;
174  else
175  high_freq = high_freq * 0.65;
176  } else {
177  if (bps >= 0.8)
178  high_freq = high_freq * 0.75;
179  else if (bps >= 0.6)
180  high_freq = high_freq * 0.6;
181  else
182  high_freq = high_freq * 0.5;
183  }
184  ff_dlog(s->avctx, "flags2=0x%x\n", flags2);
185  ff_dlog(s->avctx, "version=%d channels=%d sample_rate=%d bitrate=%d block_align=%d\n",
186  s->version, avctx->channels, avctx->sample_rate, avctx->bit_rate,
187  avctx->block_align);
188  ff_dlog(s->avctx, "bps=%f bps1=%f high_freq=%f bitoffset=%d\n",
189  bps, bps1, high_freq, s->byte_offset_bits);
190  ff_dlog(s->avctx, "use_noise_coding=%d use_exp_vlc=%d nb_block_sizes=%d\n",
192 
193  /* compute the scale factor band sizes for each MDCT block size */
194  {
195  int a, b, pos, lpos, k, block_len, i, j, n;
196  const uint8_t *table;
197 
198  if (s->version == 1)
199  s->coefs_start = 3;
200  else
201  s->coefs_start = 0;
202  for (k = 0; k < s->nb_block_sizes; k++) {
203  block_len = s->frame_len >> k;
204 
205  if (s->version == 1) {
206  lpos = 0;
207  for (i = 0; i < 25; i++) {
208  a = ff_wma_critical_freqs[i];
209  b = avctx->sample_rate;
210  pos = ((block_len * 2 * a) + (b >> 1)) / b;
211  if (pos > block_len)
212  pos = block_len;
213  s->exponent_bands[0][i] = pos - lpos;
214  if (pos >= block_len) {
215  i++;
216  break;
217  }
218  lpos = pos;
219  }
220  s->exponent_sizes[0] = i;
221  } else {
222  /* hardcoded tables */
223  table = NULL;
224  a = s->frame_len_bits - BLOCK_MIN_BITS - k;
225  if (a < 3) {
226  if (avctx->sample_rate >= 44100)
227  table = exponent_band_44100[a];
228  else if (avctx->sample_rate >= 32000)
229  table = exponent_band_32000[a];
230  else if (avctx->sample_rate >= 22050)
231  table = exponent_band_22050[a];
232  }
233  if (table) {
234  n = *table++;
235  for (i = 0; i < n; i++)
236  s->exponent_bands[k][i] = table[i];
237  s->exponent_sizes[k] = n;
238  } else {
239  j = 0;
240  lpos = 0;
241  for (i = 0; i < 25; i++) {
242  a = ff_wma_critical_freqs[i];
243  b = avctx->sample_rate;
244  pos = ((block_len * 2 * a) + (b << 1)) / (4 * b);
245  pos <<= 2;
246  if (pos > block_len)
247  pos = block_len;
248  if (pos > lpos)
249  s->exponent_bands[k][j++] = pos - lpos;
250  if (pos >= block_len)
251  break;
252  lpos = pos;
253  }
254  s->exponent_sizes[k] = j;
255  }
256  }
257 
258  /* max number of coefs */
259  s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k;
260  /* high freq computation */
261  s->high_band_start[k] = (int) ((block_len * 2 * high_freq) /
262  avctx->sample_rate + 0.5);
263  n = s->exponent_sizes[k];
264  j = 0;
265  pos = 0;
266  for (i = 0; i < n; i++) {
267  int start, end;
268  start = pos;
269  pos += s->exponent_bands[k][i];
270  end = pos;
271  if (start < s->high_band_start[k])
272  start = s->high_band_start[k];
273  if (end > s->coefs_end[k])
274  end = s->coefs_end[k];
275  if (end > start)
276  s->exponent_high_bands[k][j++] = end - start;
277  }
278  s->exponent_high_sizes[k] = j;
279  }
280  }
281 
282 #ifdef TRACE
283  {
284  int i, j;
285  for (i = 0; i < s->nb_block_sizes; i++) {
286  ff_tlog(s->avctx, "%5d: n=%2d:",
287  s->frame_len >> i,
288  s->exponent_sizes[i]);
289  for (j = 0; j < s->exponent_sizes[i]; j++)
290  ff_tlog(s->avctx, " %d", s->exponent_bands[i][j]);
291  ff_tlog(s->avctx, "\n");
292  }
293  }
294 #endif /* TRACE */
295 
296  /* init MDCT windows : simple sine window */
297  for (i = 0; i < s->nb_block_sizes; i++) {
299  s->windows[i] = ff_sine_windows[s->frame_len_bits - i];
300  }
301 
302  s->reset_block_lengths = 1;
303 
304  if (s->use_noise_coding) {
305  /* init the noise generator */
306  if (s->use_exp_vlc)
307  s->noise_mult = 0.02;
308  else
309  s->noise_mult = 0.04;
310 
311 #ifdef TRACE
312  for (i = 0; i < NOISE_TAB_SIZE; i++)
313  s->noise_table[i] = 1.0 * s->noise_mult;
314 #else
315  {
316  unsigned int seed;
317  float norm;
318  seed = 1;
319  norm = (1.0 / (float) (1LL << 31)) * sqrt(3) * s->noise_mult;
320  for (i = 0; i < NOISE_TAB_SIZE; i++) {
321  seed = seed * 314159 + 1;
322  s->noise_table[i] = (float) ((int) seed) * norm;
323  }
324  }
325 #endif /* TRACE */
326  }
327 
328  /* choose the VLC tables for the coefficients */
329  coef_vlc_table = 2;
330  if (avctx->sample_rate >= 32000) {
331  if (bps1 < 0.72)
332  coef_vlc_table = 0;
333  else if (bps1 < 1.16)
334  coef_vlc_table = 1;
335  }
336  s->coef_vlcs[0] = &coef_vlcs[coef_vlc_table * 2];
337  s->coef_vlcs[1] = &coef_vlcs[coef_vlc_table * 2 + 1];
338  ret = init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0],
339  &s->int_table[0], s->coef_vlcs[0]);
340  if (ret < 0)
341  return ret;
342 
343  return init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1],
344  &s->int_table[1], s->coef_vlcs[1]);
345 }
346 
347 int ff_wma_total_gain_to_bits(int total_gain)
348 {
349  if (total_gain < 15)
350  return 13;
351  else if (total_gain < 32)
352  return 12;
353  else if (total_gain < 40)
354  return 11;
355  else if (total_gain < 45)
356  return 10;
357  else
358  return 9;
359 }
360 
362 {
363  WMACodecContext *s = avctx->priv_data;
364  int i;
365 
366  for (i = 0; i < s->nb_block_sizes; i++)
367  ff_mdct_end(&s->mdct_ctx[i]);
368 
369  if (s->use_exp_vlc)
370  ff_free_vlc(&s->exp_vlc);
371  if (s->use_noise_coding)
372  ff_free_vlc(&s->hgain_vlc);
373  for (i = 0; i < 2; i++) {
374  ff_free_vlc(&s->coef_vlc[i]);
375  av_free(s->run_table[i]);
376  av_free(s->level_table[i]);
377  av_free(s->int_table[i]);
378  }
379 
380  return 0;
381 }
382 
389 {
391  int n_bits = 8;
393  if (get_bits1(gb)) {
394  n_bits += 8;
395  if (get_bits1(gb)) {
396  n_bits += 8;
397  if (get_bits1(gb))
398  n_bits += 7;
399  }
400  }
401  return get_bits_long(gb, n_bits);
402 }
403 
421  VLC *vlc, const float *level_table,
422  const uint16_t *run_table, int version,
423  WMACoef *ptr, int offset, int num_coefs,
424  int block_len, int frame_len_bits,
425  int coef_nb_bits)
426 {
427  int code, level, sign;
428  const uint32_t *ilvl = (const uint32_t *) level_table;
429  uint32_t *iptr = (uint32_t *) ptr;
430  const unsigned int coef_mask = block_len - 1;
431  for (; offset < num_coefs; offset++) {
432  code = get_vlc2(gb, vlc->table, VLCBITS, VLCMAX);
433  if (code > 1) {
435  offset += run_table[code];
436  sign = get_bits1(gb) - 1;
437  iptr[offset & coef_mask] = ilvl[code] ^ sign << 31;
438  } else if (code == 1) {
440  break;
441  } else {
443  if (!version) {
444  level = get_bits(gb, coef_nb_bits);
447  offset += get_bits(gb, frame_len_bits);
448  } else {
449  level = ff_wma_get_large_val(gb);
451  if (get_bits1(gb)) {
452  if (get_bits1(gb)) {
453  if (get_bits1(gb)) {
454  av_log(avctx, AV_LOG_ERROR,
455  "broken escape sequence\n");
456  return -1;
457  } else
458  offset += get_bits(gb, frame_len_bits) + 4;
459  } else
460  offset += get_bits(gb, 2) + 1;
461  }
462  }
463  sign = get_bits1(gb) - 1;
464  ptr[offset & coef_mask] = (level ^ sign) - sign;
465  }
466  }
468  if (offset > num_coefs) {
469  av_log(avctx, AV_LOG_ERROR, "overflow in spectral RLE, ignoring\n");
470  return -1;
471  }
472 
473  return 0;
474 }
#define ff_tlog(ctx,...)
Definition: internal.h:66
const struct AVCodec * codec
Definition: avcodec.h:1418
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:228
int next_block_len_bits
log2 of next block length
Definition: wma.h:105
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
int ff_wma_run_level_decode(AVCodecContext *avctx, GetBitContext *gb, VLC *vlc, const float *level_table, const uint16_t *run_table, int version, WMACoef *ptr, int offset, int num_coefs, int block_len, int frame_len_bits, int coef_nb_bits)
Decode run level compressed coefficients.
Definition: wma.c:420
#define BLOCK_MIN_BITS
Definition: wma.h:33
const uint16_t ff_wma_critical_freqs[25]
Definition: wma_freqs.c:23
const uint8_t * huffbits
VLC bit size.
Definition: wma.h:63
#define init_vlc(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, codes, codes_wrap, codes_size, flags)
Definition: vlc.h:38
int n
total number of codes
Definition: wma.h:60
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:2189
#define NOISE_TAB_SIZE
Definition: wma.h:49
Macro definitions for various function/variable attributes.
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
int high_band_start[BLOCK_NB_SIZES]
index of first coef in high band
Definition: wma.h:80
int exponent_sizes[BLOCK_NB_SIZES]
Definition: wma.h:78
uint8_t
#define av_cold
Definition: attributes.h:66
float WMACoef
type for decoded coefficients, int16_t would be enough for wma 1/2
Definition: wma.h:57
#define b
Definition: input.c:52
Various WMA tables.
const uint32_t * huffcodes
VLC bit values.
Definition: wma.h:62
int reset_block_lengths
Definition: wma.h:103
int nb_block_sizes
number of block sizes
Definition: wma.h:101
int ff_wma_total_gain_to_bits(int total_gain)
Definition: wma.c:347
uint16_t * int_table[2]
Definition: wma.h:96
enum AVCodecID id
Definition: avcodec.h:3134
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:190
#define AVERROR(e)
Definition: error.h:43
uint16_t exponent_bands[BLOCK_NB_SIZES][25]
Definition: wma.h:79
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1503
AVFloatDSPContext fdsp
Definition: wma.h:134
#define VLCBITS
Definition: wma.h:54
Definition: vlc.h:26
int exponent_high_bands[BLOCK_NB_SIZES][HIGH_BAND_MAX_SIZE]
Definition: wma.h:84
int ff_wma_end(AVCodecContext *avctx)
Definition: wma.c:361
int bit_rate
the average bitrate
Definition: avcodec.h:1473
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:788
float * windows[BLOCK_NB_SIZES]
Definition: wma.h:119
static const uint8_t exponent_band_44100[3][25]
Definition: wmadata.h:48
av_cold int ff_wma_init(AVCodecContext *avctx, int flags2)
Definition: wma.c:81
uint16_t * run_table[2]
Definition: wma.h:94
int version
1 = 0x160 (WMAV1), 2 = 0x161 (WMAV2)
Definition: wma.h:71
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:493
int frame_len
frame length in samples
Definition: wma.h:99
NULL
Definition: eval.c:55
int frame_len_bits
frame_len = 1 << frame_len_bits
Definition: wma.h:100
Libavcodec external API header.
#define ff_dlog(ctx,...)
Definition: internal.h:60
version
Definition: ffv1enc.c:1091
int sample_rate
samples per second
Definition: avcodec.h:2152
int use_exp_vlc
exponent coding: 0 = lsp, 1 = vlc + delta
Definition: wma.h:74
VLC coef_vlc[2]
Definition: wma.h:93
main external API structure.
Definition: avcodec.h:1409
#define VLCMAX
Definition: wma.h:55
static unsigned int seed
Definition: videogen.c:78
AVCodecContext * avctx
Definition: wma.h:68
int exponent_high_sizes[BLOCK_NB_SIZES]
Definition: wma.h:83
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:267
int use_noise_coding
true if perceptual noise is added
Definition: wma.h:75
SINETABLE_CONST float *const ff_sine_windows[14]
float * level_table[2]
Definition: wma.h:95
int use_variable_block_len
Definition: wma.h:73
VLC exp_vlc
Definition: wma.h:77
FFTContext mdct_ctx[BLOCK_NB_SIZES]
Definition: wma.h:118
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:300
av_cold int ff_wma_get_frame_len_bits(int sample_rate, int version, unsigned int decode_flags)
Get the samples per frame for this stream.
Definition: wma_common.c:32
av_cold void avpriv_float_dsp_init(AVFloatDSPContext *fdsp, int bit_exact)
Initialize a float DSP context.
Definition: float_dsp.c:115
uint8_t level
Definition: svq3.c:204
int prev_block_len_bits
log2 of prev block length
Definition: wma.h:106
int coefs_end[BLOCK_NB_SIZES]
max number of coded coefficients
Definition: wma.h:82
common internal api header.
#define ff_mdct_end
Definition: fft.h:152
unsigned bps
Definition: movenc.c:855
void * priv_data
Definition: avcodec.h:1451
int channels
number of audio channels
Definition: avcodec.h:2153
#define av_log2
Definition: intmath.h:85
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
static av_cold int init_coef_vlc(VLC *vlc, uint16_t **prun_table, float **plevel_table, uint16_t **pint_table, const CoefVLCTable *vlc_table)
Definition: wma.c:34
unsigned int ff_wma_get_large_val(GetBitContext *gb)
Decode an uncompressed coefficient.
Definition: wma.c:388
static const CoefVLCTable coef_vlcs[6]
Definition: wmadata.h:1375
VLC hgain_vlc
Definition: wma.h:85
int coefs_start
first coded coef
Definition: wma.h:81
int block_len_bits
log2 of current block length
Definition: wma.h:104
int byte_offset_bits
Definition: wma.h:76
static const uint8_t exponent_band_22050[3][25]
Definition: wmadata.h:35
static const uint8_t exponent_band_32000[3][25]
Definition: wmadata.h:42
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:334
void ff_init_ff_sine_windows(int index)
initialize the specified entry of ff_sine_windows
float noise_table[NOISE_TAB_SIZE]
Definition: wma.h:126
const uint16_t * levels
table to build run/level tables
Definition: wma.h:64
float noise_mult
Definition: wma.h:128