3 * Copyright (c) 2002-2007 The Libav Project
5 * This file is part of Libav.
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.
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.
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
25 #include "wma_common.h"
31 /* XXX: use same run/length optimization as mpeg decoders */
32 //FIXME maybe split decode / encode or pass flag
33 static void init_coef_vlc(VLC *vlc, uint16_t **prun_table,
34 float **plevel_table, uint16_t **pint_table,
35 const CoefVLCTable *vlc_table)
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;
43 int i, l, j, k, level;
45 init_vlc(vlc, VLCBITS, n, table_bits, 1, 1, table_codes, 4, 4, 0);
47 run_table = av_malloc(n * sizeof(uint16_t));
48 level_table = av_malloc(n * sizeof(uint16_t));
49 flevel_table= av_malloc(n * sizeof(*flevel_table));
50 int_table = av_malloc(n * sizeof(uint16_t));
56 l = levels_table[k++];
57 for (j = 0; j < l; j++) {
59 level_table[i] = level;
60 flevel_table[i]= level;
65 *prun_table = run_table;
66 *plevel_table = flevel_table;
67 *pint_table = int_table;
71 int ff_wma_init(AVCodecContext *avctx, int flags2)
73 WMACodecContext *s = avctx->priv_data;
75 float bps1, high_freq;
80 if ( avctx->sample_rate <= 0 || avctx->sample_rate > 50000
81 || avctx->channels <= 0 || avctx->channels > 8
82 || avctx->bit_rate <= 0)
85 s->sample_rate = avctx->sample_rate;
86 s->nb_channels = avctx->channels;
87 s->bit_rate = avctx->bit_rate;
88 s->block_align = avctx->block_align;
90 ff_dsputil_init(&s->dsp, avctx);
91 ff_fmt_convert_init(&s->fmt_conv, avctx);
93 if (avctx->codec->id == CODEC_ID_WMAV1) {
99 /* compute MDCT block size */
100 s->frame_len_bits = ff_wma_get_frame_len_bits(s->sample_rate, s->version, 0);
101 s->next_block_len_bits = s->frame_len_bits;
102 s->prev_block_len_bits = s->frame_len_bits;
103 s->block_len_bits = s->frame_len_bits;
105 s->frame_len = 1 << s->frame_len_bits;
106 if (s->use_variable_block_len) {
108 nb = ((flags2 >> 3) & 3) + 1;
109 if ((s->bit_rate / s->nb_channels) >= 32000)
111 nb_max = s->frame_len_bits - BLOCK_MIN_BITS;
114 s->nb_block_sizes = nb + 1;
116 s->nb_block_sizes = 1;
119 /* init rate dependent parameters */
120 s->use_noise_coding = 1;
121 high_freq = s->sample_rate * 0.5;
123 /* if version 2, then the rates are normalized */
124 sample_rate1 = s->sample_rate;
125 if (s->version == 2) {
126 if (sample_rate1 >= 44100) {
127 sample_rate1 = 44100;
128 } else if (sample_rate1 >= 22050) {
129 sample_rate1 = 22050;
130 } else if (sample_rate1 >= 16000) {
131 sample_rate1 = 16000;
132 } else if (sample_rate1 >= 11025) {
133 sample_rate1 = 11025;
134 } else if (sample_rate1 >= 8000) {
139 bps = (float)s->bit_rate / (float)(s->nb_channels * s->sample_rate);
140 s->byte_offset_bits = av_log2((int)(bps * s->frame_len / 8.0 + 0.5)) + 2;
142 /* compute high frequency value and choose if noise coding should
145 if (s->nb_channels == 2)
147 if (sample_rate1 == 44100) {
149 s->use_noise_coding = 0;
151 high_freq = high_freq * 0.4;
153 } else if (sample_rate1 == 22050) {
155 s->use_noise_coding = 0;
156 } else if (bps1 >= 0.72) {
157 high_freq = high_freq * 0.7;
159 high_freq = high_freq * 0.6;
161 } else if (sample_rate1 == 16000) {
163 high_freq = high_freq * 0.5;
165 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) {
171 high_freq = high_freq * 0.5;
172 } else if (bps > 0.75) {
173 s->use_noise_coding = 0;
175 high_freq = high_freq * 0.65;
179 high_freq = high_freq * 0.75;
180 } else if (bps >= 0.6) {
181 high_freq = high_freq * 0.6;
183 high_freq = high_freq * 0.5;
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, s->nb_channels, s->sample_rate, s->bit_rate,
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",
193 s->use_noise_coding, s->use_exp_vlc, s->nb_block_sizes);
195 /* compute the scale factor band sizes for each MDCT block size */
197 int a, b, pos, lpos, k, block_len, i, j, n;
198 const uint8_t *table;
200 if (s->version == 1) {
205 for (k = 0; k < s->nb_block_sizes; k++) {
206 block_len = s->frame_len >> k;
208 if (s->version == 1) {
210 for (i = 0; i < 25; i++) {
211 a = ff_wma_critical_freqs[i];
213 pos = ((block_len * 2 * a) + (b >> 1)) / b;
216 s->exponent_bands[0][i] = pos - lpos;
217 if (pos >= block_len) {
223 s->exponent_sizes[0] = i;
225 /* hardcoded tables */
227 a = s->frame_len_bits - BLOCK_MIN_BITS - k;
229 if (s->sample_rate >= 44100) {
230 table = exponent_band_44100[a];
231 } else if (s->sample_rate >= 32000) {
232 table = exponent_band_32000[a];
233 } else if (s->sample_rate >= 22050) {
234 table = exponent_band_22050[a];
239 for (i = 0; i < n; i++)
240 s->exponent_bands[k][i] = table[i];
241 s->exponent_sizes[k] = n;
245 for (i = 0; i < 25; i++) {
246 a = ff_wma_critical_freqs[i];
248 pos = ((block_len * 2 * a) + (b << 1)) / (4 * b);
253 s->exponent_bands[k][j++] = pos - lpos;
254 if (pos >= block_len)
258 s->exponent_sizes[k] = j;
262 /* max number of coefs */
263 s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k;
264 /* high freq computation */
265 s->high_band_start[k] = (int)((block_len * 2 * high_freq) /
266 s->sample_rate + 0.5);
267 n = s->exponent_sizes[k];
270 for (i = 0; i < n; i++) {
273 pos += s->exponent_bands[k][i];
275 if (start < s->high_band_start[k])
276 start = s->high_band_start[k];
277 if (end > s->coefs_end[k])
278 end = s->coefs_end[k];
280 s->exponent_high_bands[k][j++] = end - start;
282 s->exponent_high_sizes[k] = j;
284 tprintf(s->avctx, "%5d: coefs_end=%d high_band_start=%d nb_high_bands=%d: ",
287 s->high_band_start[k],
288 s->exponent_high_sizes[k]);
289 for (j = 0; j < s->exponent_high_sizes[k]; j++)
290 tprintf(s->avctx, " %d", s->exponent_high_bands[k][j]);
291 tprintf(s->avctx, "\n");
299 for (i = 0; i < s->nb_block_sizes; i++) {
300 tprintf(s->avctx, "%5d: n=%2d:",
302 s->exponent_sizes[i]);
303 for (j = 0; j < s->exponent_sizes[i]; j++)
304 tprintf(s->avctx, " %d", s->exponent_bands[i][j]);
305 tprintf(s->avctx, "\n");
310 /* init MDCT windows : simple sinus window */
311 for (i = 0; i < s->nb_block_sizes; i++) {
312 ff_init_ff_sine_windows(s->frame_len_bits - i);
313 s->windows[i] = ff_sine_windows[s->frame_len_bits - i];
316 s->reset_block_lengths = 1;
318 if (s->use_noise_coding) {
320 /* init the noise generator */
321 if (s->use_exp_vlc) {
322 s->noise_mult = 0.02;
324 s->noise_mult = 0.04;
328 for (i = 0; i < NOISE_TAB_SIZE; i++)
329 s->noise_table[i] = 1.0 * s->noise_mult;
335 norm = (1.0 / (float)(1LL << 31)) * sqrt(3) * s->noise_mult;
336 for (i = 0; i < NOISE_TAB_SIZE; i++) {
337 seed = seed * 314159 + 1;
338 s->noise_table[i] = (float)((int)seed) * norm;
344 /* choose the VLC tables for the coefficients */
346 if (s->sample_rate >= 32000) {
349 } else if (bps1 < 1.16) {
353 s->coef_vlcs[0]= &coef_vlcs[coef_vlc_table * 2 ];
354 s->coef_vlcs[1]= &coef_vlcs[coef_vlc_table * 2 + 1];
355 init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0], &s->int_table[0],
357 init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1], &s->int_table[1],
363 int ff_wma_total_gain_to_bits(int total_gain)
365 if (total_gain < 15) return 13;
366 else if (total_gain < 32) return 12;
367 else if (total_gain < 40) return 11;
368 else if (total_gain < 45) return 10;
372 int ff_wma_end(AVCodecContext *avctx)
374 WMACodecContext *s = avctx->priv_data;
377 for (i = 0; i < s->nb_block_sizes; i++)
378 ff_mdct_end(&s->mdct_ctx[i]);
380 if (s->use_exp_vlc) {
381 ff_free_vlc(&s->exp_vlc);
383 if (s->use_noise_coding) {
384 ff_free_vlc(&s->hgain_vlc);
386 for (i = 0; i < 2; i++) {
387 ff_free_vlc(&s->coef_vlc[i]);
388 av_free(s->run_table[i]);
389 av_free(s->level_table[i]);
390 av_free(s->int_table[i]);
397 * Decode an uncompressed coefficient.
398 * @param gb GetBitContext
399 * @return the decoded coefficient
401 unsigned int ff_wma_get_large_val(GetBitContext* gb)
403 /** consumes up to 34 bits */
415 return get_bits_long(gb, n_bits);
419 * Decode run level compressed coefficients.
420 * @param avctx codec context
421 * @param gb bitstream reader context
422 * @param vlc vlc table for get_vlc2
423 * @param level_table level codes
424 * @param run_table run codes
425 * @param version 0 for wma1,2 1 for wmapro
426 * @param ptr output buffer
427 * @param offset offset in the output buffer
428 * @param num_coefs number of input coefficents
429 * @param block_len input buffer length (2^n)
430 * @param frame_len_bits number of bits for escaped run codes
431 * @param coef_nb_bits number of bits for escaped level codes
432 * @return 0 on success, -1 otherwise
434 int ff_wma_run_level_decode(AVCodecContext* avctx, GetBitContext* gb,
436 const float *level_table, const uint16_t *run_table,
437 int version, WMACoef *ptr, int offset,
438 int num_coefs, int block_len, int frame_len_bits,
441 int code, level, sign;
442 const uint32_t *ilvl = (const uint32_t*)level_table;
443 uint32_t *iptr = (uint32_t*)ptr;
444 const unsigned int coef_mask = block_len - 1;
445 for (; offset < num_coefs; offset++) {
446 code = get_vlc2(gb, vlc->table, VLCBITS, VLCMAX);
449 offset += run_table[code];
450 sign = get_bits1(gb) - 1;
451 iptr[offset & coef_mask] = ilvl[code] ^ sign<<31;
452 } else if (code == 1) {
458 level = get_bits(gb, coef_nb_bits);
459 /** NOTE: this is rather suboptimal. reading
460 block_len_bits would be better */
461 offset += get_bits(gb, frame_len_bits);
463 level = ff_wma_get_large_val(gb);
468 av_log(avctx,AV_LOG_ERROR,
469 "broken escape sequence\n");
472 offset += get_bits(gb, frame_len_bits) + 4;
474 offset += get_bits(gb, 2) + 1;
477 sign = get_bits1(gb) - 1;
478 ptr[offset & coef_mask] = (level^sign) - sign;
481 /** NOTE: EOB can be omitted */
482 if (offset > num_coefs) {
483 av_log(avctx, AV_LOG_ERROR, "overflow in spectral RLE, ignoring\n");