3 * Copyright (c) 2002-2007 The FFmpeg Project
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/attributes.h"
28 #include "wma_common.h"
29 #include "wma_freqs.h"
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)
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;
44 int i, l, j, k, level;
46 init_vlc(vlc, VLCBITS, n, table_bits, 1, 1, table_codes, 4, 4, 0);
48 run_table = av_malloc_array(n, sizeof(uint16_t));
49 level_table = av_malloc_array(n, sizeof(uint16_t));
50 flevel_table = av_malloc_array(n, sizeof(*flevel_table));
51 int_table = av_malloc_array(n, sizeof(uint16_t));
52 if (!run_table || !level_table || !flevel_table || !int_table) {
54 av_freep(&level_table);
55 av_freep(&flevel_table);
57 return AVERROR(ENOMEM);
64 l = levels_table[k++];
65 for (j = 0; j < l; j++) {
67 level_table[i] = level;
68 flevel_table[i] = level;
73 *prun_table = run_table;
74 *plevel_table = flevel_table;
75 *pint_table = int_table;
81 av_cold int ff_wma_init(AVCodecContext *avctx, int flags2)
83 WMACodecContext *s = avctx->priv_data;
85 float bps1, high_freq;
90 if (avctx->sample_rate <= 0 || avctx->sample_rate > 50000 ||
91 avctx->channels <= 0 || avctx->channels > 2 ||
96 if (avctx->codec->id == AV_CODEC_ID_WMAV1)
101 /* compute MDCT block size */
102 s->frame_len_bits = ff_wma_get_frame_len_bits(avctx->sample_rate,
104 s->next_block_len_bits = s->frame_len_bits;
105 s->prev_block_len_bits = s->frame_len_bits;
106 s->block_len_bits = s->frame_len_bits;
108 s->frame_len = 1 << s->frame_len_bits;
109 if (s->use_variable_block_len) {
111 nb = ((flags2 >> 3) & 3) + 1;
112 if ((avctx->bit_rate / avctx->channels) >= 32000)
114 nb_max = s->frame_len_bits - BLOCK_MIN_BITS;
117 s->nb_block_sizes = nb + 1;
119 s->nb_block_sizes = 1;
121 /* init rate dependent parameters */
122 s->use_noise_coding = 1;
123 high_freq = avctx->sample_rate * 0.5;
125 /* if version 2, then the rates are normalized */
126 sample_rate1 = avctx->sample_rate;
127 if (s->version == 2) {
128 if (sample_rate1 >= 44100)
129 sample_rate1 = 44100;
130 else if (sample_rate1 >= 22050)
131 sample_rate1 = 22050;
132 else if (sample_rate1 >= 16000)
133 sample_rate1 = 16000;
134 else if (sample_rate1 >= 11025)
135 sample_rate1 = 11025;
136 else if (sample_rate1 >= 8000)
140 bps = (float) avctx->bit_rate /
141 (float) (avctx->channels * avctx->sample_rate);
142 s->byte_offset_bits = av_log2((int) (bps * s->frame_len / 8.0 + 0.5)) + 2;
143 if (s->byte_offset_bits + 3 > MIN_CACHE_BITS) {
144 av_log(avctx, AV_LOG_ERROR, "byte_offset_bits %d is too large\n", s->byte_offset_bits);
145 return AVERROR_PATCHWELCOME;
148 /* compute high frequency value and choose if noise coding should
151 if (avctx->channels == 2)
153 if (sample_rate1 == 44100) {
155 s->use_noise_coding = 0;
157 high_freq = high_freq * 0.4;
158 } else if (sample_rate1 == 22050) {
160 s->use_noise_coding = 0;
161 else if (bps1 >= 0.72)
162 high_freq = high_freq * 0.7;
164 high_freq = high_freq * 0.6;
165 } else if (sample_rate1 == 16000) {
167 high_freq = high_freq * 0.5;
169 high_freq = high_freq * 0.3;
170 } else if (sample_rate1 == 11025)
171 high_freq = high_freq * 0.7;
172 else if (sample_rate1 == 8000) {
174 high_freq = high_freq * 0.5;
176 s->use_noise_coding = 0;
178 high_freq = high_freq * 0.65;
181 high_freq = high_freq * 0.75;
183 high_freq = high_freq * 0.6;
185 high_freq = high_freq * 0.5;
187 ff_dlog(s->avctx, "flags2=0x%x\n", flags2);
188 ff_dlog(s->avctx, "version=%d channels=%d sample_rate=%d bitrate=%"PRId64" block_align=%d\n",
189 s->version, avctx->channels, avctx->sample_rate, avctx->bit_rate,
191 ff_dlog(s->avctx, "bps=%f bps1=%f high_freq=%f bitoffset=%d\n",
192 bps, bps1, high_freq, s->byte_offset_bits);
193 ff_dlog(s->avctx, "use_noise_coding=%d use_exp_vlc=%d nb_block_sizes=%d\n",
194 s->use_noise_coding, s->use_exp_vlc, s->nb_block_sizes);
196 /* compute the scale factor band sizes for each MDCT block size */
198 int a, b, pos, lpos, k, block_len, i, j, n;
199 const uint8_t *table;
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];
212 b = avctx->sample_rate;
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 (avctx->sample_rate >= 44100)
230 table = exponent_band_44100[a];
231 else if (avctx->sample_rate >= 32000)
232 table = exponent_band_32000[a];
233 else if (avctx->sample_rate >= 22050)
234 table = exponent_band_22050[a];
238 for (i = 0; i < n; i++)
239 s->exponent_bands[k][i] = table[i];
240 s->exponent_sizes[k] = n;
244 for (i = 0; i < 25; i++) {
245 a = ff_wma_critical_freqs[i];
246 b = avctx->sample_rate;
247 pos = ((block_len * 2 * a) + (b << 1)) / (4 * b);
252 s->exponent_bands[k][j++] = pos - lpos;
253 if (pos >= block_len)
257 s->exponent_sizes[k] = j;
261 /* max number of coefs */
262 s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k;
263 /* high freq computation */
264 s->high_band_start[k] = (int) ((block_len * 2 * high_freq) /
265 avctx->sample_rate + 0.5);
266 n = s->exponent_sizes[k];
269 for (i = 0; i < n; i++) {
272 pos += s->exponent_bands[k][i];
274 if (start < s->high_band_start[k])
275 start = s->high_band_start[k];
276 if (end > s->coefs_end[k])
277 end = s->coefs_end[k];
279 s->exponent_high_bands[k][j++] = end - start;
281 s->exponent_high_sizes[k] = j;
288 for (i = 0; i < s->nb_block_sizes; i++) {
289 ff_tlog(s->avctx, "%5d: n=%2d:",
291 s->exponent_sizes[i]);
292 for (j = 0; j < s->exponent_sizes[i]; j++)
293 ff_tlog(s->avctx, " %d", s->exponent_bands[i][j]);
294 ff_tlog(s->avctx, "\n");
299 /* init MDCT windows : simple sine window */
300 for (i = 0; i < s->nb_block_sizes; i++) {
301 ff_init_ff_sine_windows(s->frame_len_bits - i);
302 s->windows[i] = ff_sine_windows[s->frame_len_bits - i];
305 s->reset_block_lengths = 1;
307 if (s->use_noise_coding) {
308 /* init the noise generator */
310 s->noise_mult = 0.02;
312 s->noise_mult = 0.04;
315 for (i = 0; i < NOISE_TAB_SIZE; i++)
316 s->noise_table[i] = 1.0 * s->noise_mult;
322 norm = (1.0 / (float) (1LL << 31)) * sqrt(3) * s->noise_mult;
323 for (i = 0; i < NOISE_TAB_SIZE; i++) {
324 seed = seed * 314159 + 1;
325 s->noise_table[i] = (float) ((int) seed) * norm;
331 s->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
333 return AVERROR(ENOMEM);
335 /* choose the VLC tables for the coefficients */
337 if (avctx->sample_rate >= 32000) {
340 else if (bps1 < 1.16)
343 s->coef_vlcs[0] = &coef_vlcs[coef_vlc_table * 2];
344 s->coef_vlcs[1] = &coef_vlcs[coef_vlc_table * 2 + 1];
345 ret = init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0],
346 &s->int_table[0], s->coef_vlcs[0]);
350 return init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1],
351 &s->int_table[1], s->coef_vlcs[1]);
354 int ff_wma_total_gain_to_bits(int total_gain)
358 else if (total_gain < 32)
360 else if (total_gain < 40)
362 else if (total_gain < 45)
368 int ff_wma_end(AVCodecContext *avctx)
370 WMACodecContext *s = avctx->priv_data;
373 for (i = 0; i < s->nb_block_sizes; i++)
374 ff_mdct_end(&s->mdct_ctx[i]);
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]);
392 * Decode an uncompressed coefficient.
393 * @param gb GetBitContext
394 * @return the decoded coefficient
396 unsigned int ff_wma_get_large_val(GetBitContext *gb)
398 /** consumes up to 34 bits */
409 return get_bits_long(gb, n_bits);
413 * Decode run level compressed coefficients.
414 * @param avctx codec context
415 * @param gb bitstream reader context
416 * @param vlc vlc table for get_vlc2
417 * @param level_table level codes
418 * @param run_table run codes
419 * @param version 0 for wma1,2 1 for wmapro
420 * @param ptr output buffer
421 * @param offset offset in the output buffer
422 * @param num_coefs number of input coefficients
423 * @param block_len input buffer length (2^n)
424 * @param frame_len_bits number of bits for escaped run codes
425 * @param coef_nb_bits number of bits for escaped level codes
426 * @return 0 on success, -1 otherwise
428 int ff_wma_run_level_decode(AVCodecContext *avctx, GetBitContext *gb,
429 VLC *vlc, const float *level_table,
430 const uint16_t *run_table, int version,
431 WMACoef *ptr, int offset, int num_coefs,
432 int block_len, int frame_len_bits,
435 int code, level, sign;
436 const uint32_t *ilvl = (const uint32_t *) level_table;
437 uint32_t *iptr = (uint32_t *) ptr;
438 const unsigned int coef_mask = block_len - 1;
439 for (; offset < num_coefs; offset++) {
440 code = get_vlc2(gb, vlc->table, VLCBITS, VLCMAX);
443 offset += run_table[code];
444 sign = get_bits1(gb) - 1;
445 iptr[offset & coef_mask] = ilvl[code] ^ (sign & 0x80000000);
446 } else if (code == 1) {
452 level = get_bits(gb, coef_nb_bits);
453 /** NOTE: this is rather suboptimal. reading
454 * block_len_bits would be better */
455 offset += get_bits(gb, frame_len_bits);
457 level = ff_wma_get_large_val(gb);
462 av_log(avctx, AV_LOG_ERROR,
463 "broken escape sequence\n");
466 offset += get_bits(gb, frame_len_bits) + 4;
468 offset += get_bits(gb, 2) + 1;
471 sign = get_bits1(gb) - 1;
472 ptr[offset & coef_mask] = (level ^ sign) - sign;
475 /** NOTE: EOB can be omitted */
476 if (offset > num_coefs) {
477 av_log(avctx, AV_LOG_ERROR,
478 "overflow (%d > %d) in spectral RLE, ignoring\n",