2007-02-06 21:19:04 +01:00
|
|
|
/*
|
|
|
|
* WMA compatible codec
|
2011-03-18 18:35:10 +01:00
|
|
|
* Copyright (c) 2002-2007 The Libav Project
|
2007-02-06 21:19:04 +01:00
|
|
|
*
|
2011-03-18 18:35:10 +01:00
|
|
|
* This file is part of Libav.
|
2007-02-06 21:19:04 +01:00
|
|
|
*
|
2011-03-18 18:35:10 +01:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2007-02-06 21:19:04 +01:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2011-03-18 18:35:10 +01:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2007-02-06 21:19:04 +01:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2011-03-18 18:35:10 +01:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2007-02-06 21:19:04 +01:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "avcodec.h"
|
2011-03-20 00:44:00 +01:00
|
|
|
#include "sinewin.h"
|
2007-02-06 21:19:04 +01:00
|
|
|
#include "wma.h"
|
2012-03-06 19:07:10 +01:00
|
|
|
#include "wma_common.h"
|
2007-02-06 21:19:04 +01:00
|
|
|
#include "wmadata.h"
|
|
|
|
|
|
|
|
#undef NDEBUG
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
/* XXX: use same run/length optimization as mpeg decoders */
|
|
|
|
//FIXME maybe split decode / encode or pass flag
|
2009-06-12 15:12:13 +02:00
|
|
|
static void init_coef_vlc(VLC *vlc, uint16_t **prun_table,
|
2009-09-29 12:38:34 +02:00
|
|
|
float **plevel_table, uint16_t **pint_table,
|
2007-02-06 21:19:04 +01:00
|
|
|
const CoefVLCTable *vlc_table)
|
|
|
|
{
|
|
|
|
int n = vlc_table->n;
|
2009-06-12 15:12:13 +02:00
|
|
|
const uint8_t *table_bits = vlc_table->huffbits;
|
|
|
|
const uint32_t *table_codes = vlc_table->huffcodes;
|
2007-02-06 21:19:04 +01:00
|
|
|
const uint16_t *levels_table = vlc_table->levels;
|
|
|
|
uint16_t *run_table, *level_table, *int_table;
|
2009-09-29 12:38:34 +02:00
|
|
|
float *flevel_table;
|
2007-02-06 21:19:04 +01:00
|
|
|
int i, l, j, k, level;
|
|
|
|
|
|
|
|
init_vlc(vlc, VLCBITS, n, table_bits, 1, 1, table_codes, 4, 4, 0);
|
|
|
|
|
2009-06-12 15:12:13 +02:00
|
|
|
run_table = av_malloc(n * sizeof(uint16_t));
|
2007-02-06 21:19:04 +01:00
|
|
|
level_table = av_malloc(n * sizeof(uint16_t));
|
2009-09-29 12:38:34 +02:00
|
|
|
flevel_table= av_malloc(n * sizeof(*flevel_table));
|
2009-06-12 15:12:13 +02:00
|
|
|
int_table = av_malloc(n * sizeof(uint16_t));
|
2007-02-06 21:19:04 +01:00
|
|
|
i = 2;
|
|
|
|
level = 1;
|
|
|
|
k = 0;
|
|
|
|
while (i < n) {
|
2009-06-12 15:12:13 +02:00
|
|
|
int_table[k] = i;
|
2007-02-06 21:19:04 +01:00
|
|
|
l = levels_table[k++];
|
2009-06-12 15:12:13 +02:00
|
|
|
for (j = 0; j < l; j++) {
|
|
|
|
run_table[i] = j;
|
2007-02-06 21:19:04 +01:00
|
|
|
level_table[i] = level;
|
2009-09-29 12:38:34 +02:00
|
|
|
flevel_table[i]= level;
|
2007-02-06 21:19:04 +01:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
level++;
|
|
|
|
}
|
2009-06-12 15:12:13 +02:00
|
|
|
*prun_table = run_table;
|
2009-09-29 12:38:34 +02:00
|
|
|
*plevel_table = flevel_table;
|
2009-06-12 15:12:13 +02:00
|
|
|
*pint_table = int_table;
|
2009-09-29 12:38:34 +02:00
|
|
|
av_free(level_table);
|
2007-02-06 21:19:04 +01:00
|
|
|
}
|
|
|
|
|
2009-06-12 15:12:13 +02:00
|
|
|
int ff_wma_init(AVCodecContext *avctx, int flags2)
|
2007-02-06 21:19:04 +01:00
|
|
|
{
|
2007-02-27 10:54:48 +01:00
|
|
|
WMACodecContext *s = avctx->priv_data;
|
2007-02-06 21:19:04 +01:00
|
|
|
int i;
|
|
|
|
float bps1, high_freq;
|
|
|
|
volatile float bps;
|
|
|
|
int sample_rate1;
|
|
|
|
int coef_vlc_table;
|
|
|
|
|
2009-06-12 15:12:13 +02:00
|
|
|
if ( avctx->sample_rate <= 0 || avctx->sample_rate > 50000
|
2012-06-30 20:30:28 +02:00
|
|
|
|| avctx->channels <= 0 || avctx->channels > 2
|
2009-06-12 15:12:13 +02:00
|
|
|
|| avctx->bit_rate <= 0)
|
2007-05-31 01:06:37 +02:00
|
|
|
return -1;
|
|
|
|
|
2011-01-30 16:06:46 +01:00
|
|
|
ff_fmt_convert_init(&s->fmt_conv, avctx);
|
2012-08-25 19:44:30 +02:00
|
|
|
avpriv_float_dsp_init(&s->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
|
2007-02-06 21:19:04 +01:00
|
|
|
|
2012-08-05 11:11:04 +02:00
|
|
|
if (avctx->codec->id == AV_CODEC_ID_WMAV1) {
|
2007-02-06 21:19:04 +01:00
|
|
|
s->version = 1;
|
|
|
|
} else {
|
|
|
|
s->version = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* compute MDCT block size */
|
2012-10-23 22:30:59 +02:00
|
|
|
s->frame_len_bits = ff_wma_get_frame_len_bits(avctx->sample_rate,
|
|
|
|
s->version, 0);
|
2011-11-22 19:37:52 +01:00
|
|
|
s->next_block_len_bits = s->frame_len_bits;
|
|
|
|
s->prev_block_len_bits = s->frame_len_bits;
|
|
|
|
s->block_len_bits = s->frame_len_bits;
|
2009-06-12 17:21:43 +02:00
|
|
|
|
2007-02-06 21:19:04 +01:00
|
|
|
s->frame_len = 1 << s->frame_len_bits;
|
|
|
|
if (s->use_variable_block_len) {
|
|
|
|
int nb_max, nb;
|
|
|
|
nb = ((flags2 >> 3) & 3) + 1;
|
2012-10-23 22:30:59 +02:00
|
|
|
if ((avctx->bit_rate / avctx->channels) >= 32000)
|
2007-02-06 21:19:04 +01:00
|
|
|
nb += 2;
|
|
|
|
nb_max = s->frame_len_bits - BLOCK_MIN_BITS;
|
|
|
|
if (nb > nb_max)
|
|
|
|
nb = nb_max;
|
|
|
|
s->nb_block_sizes = nb + 1;
|
|
|
|
} else {
|
|
|
|
s->nb_block_sizes = 1;
|
|
|
|
}
|
|
|
|
|
2007-02-24 10:41:43 +01:00
|
|
|
/* init rate dependent parameters */
|
2007-02-06 21:19:04 +01:00
|
|
|
s->use_noise_coding = 1;
|
2012-10-23 22:30:59 +02:00
|
|
|
high_freq = avctx->sample_rate * 0.5;
|
2007-02-06 21:19:04 +01:00
|
|
|
|
|
|
|
/* if version 2, then the rates are normalized */
|
2012-10-23 22:30:59 +02:00
|
|
|
sample_rate1 = avctx->sample_rate;
|
2007-02-06 21:19:04 +01:00
|
|
|
if (s->version == 2) {
|
2009-06-12 17:48:41 +02:00
|
|
|
if (sample_rate1 >= 44100) {
|
2007-02-06 21:19:04 +01:00
|
|
|
sample_rate1 = 44100;
|
2009-06-12 17:48:41 +02:00
|
|
|
} else if (sample_rate1 >= 22050) {
|
2007-02-06 21:19:04 +01:00
|
|
|
sample_rate1 = 22050;
|
2009-06-12 17:48:41 +02:00
|
|
|
} else if (sample_rate1 >= 16000) {
|
2007-02-06 21:19:04 +01:00
|
|
|
sample_rate1 = 16000;
|
2009-06-12 17:48:41 +02:00
|
|
|
} else if (sample_rate1 >= 11025) {
|
2007-02-06 21:19:04 +01:00
|
|
|
sample_rate1 = 11025;
|
2009-06-12 17:48:41 +02:00
|
|
|
} else if (sample_rate1 >= 8000) {
|
2007-02-06 21:19:04 +01:00
|
|
|
sample_rate1 = 8000;
|
2009-06-12 17:48:41 +02:00
|
|
|
}
|
2007-02-06 21:19:04 +01:00
|
|
|
}
|
|
|
|
|
2012-10-23 22:30:59 +02:00
|
|
|
bps = (float)avctx->bit_rate / (float)(avctx->channels * avctx->sample_rate);
|
2007-02-06 21:19:04 +01:00
|
|
|
s->byte_offset_bits = av_log2((int)(bps * s->frame_len / 8.0 + 0.5)) + 2;
|
|
|
|
|
|
|
|
/* compute high frequency value and choose if noise coding should
|
|
|
|
be activated */
|
|
|
|
bps1 = bps;
|
2012-10-23 22:30:59 +02:00
|
|
|
if (avctx->channels == 2)
|
2007-02-06 21:19:04 +01:00
|
|
|
bps1 = bps * 1.6;
|
|
|
|
if (sample_rate1 == 44100) {
|
2009-06-12 17:48:41 +02:00
|
|
|
if (bps1 >= 0.61) {
|
2007-02-06 21:19:04 +01:00
|
|
|
s->use_noise_coding = 0;
|
2009-06-12 17:48:41 +02:00
|
|
|
} else {
|
2007-02-06 21:19:04 +01:00
|
|
|
high_freq = high_freq * 0.4;
|
2009-06-12 17:48:41 +02:00
|
|
|
}
|
2007-02-06 21:19:04 +01:00
|
|
|
} else if (sample_rate1 == 22050) {
|
2009-06-12 17:48:41 +02:00
|
|
|
if (bps1 >= 1.16) {
|
2007-02-06 21:19:04 +01:00
|
|
|
s->use_noise_coding = 0;
|
2009-06-12 17:48:41 +02:00
|
|
|
} else if (bps1 >= 0.72) {
|
2007-02-06 21:19:04 +01:00
|
|
|
high_freq = high_freq * 0.7;
|
2009-06-12 17:48:41 +02:00
|
|
|
} else {
|
2007-02-06 21:19:04 +01:00
|
|
|
high_freq = high_freq * 0.6;
|
2009-06-12 17:48:41 +02:00
|
|
|
}
|
2007-02-06 21:19:04 +01:00
|
|
|
} else if (sample_rate1 == 16000) {
|
2009-06-12 17:48:41 +02:00
|
|
|
if (bps > 0.5) {
|
2007-02-06 21:19:04 +01:00
|
|
|
high_freq = high_freq * 0.5;
|
2009-06-12 17:48:41 +02:00
|
|
|
} else {
|
2007-02-06 21:19:04 +01:00
|
|
|
high_freq = high_freq * 0.3;
|
2009-06-12 17:48:41 +02:00
|
|
|
}
|
2007-02-06 21:19:04 +01:00
|
|
|
} else if (sample_rate1 == 11025) {
|
|
|
|
high_freq = high_freq * 0.7;
|
|
|
|
} else if (sample_rate1 == 8000) {
|
|
|
|
if (bps <= 0.625) {
|
|
|
|
high_freq = high_freq * 0.5;
|
|
|
|
} else if (bps > 0.75) {
|
|
|
|
s->use_noise_coding = 0;
|
|
|
|
} else {
|
|
|
|
high_freq = high_freq * 0.65;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (bps >= 0.8) {
|
|
|
|
high_freq = high_freq * 0.75;
|
|
|
|
} else if (bps >= 0.6) {
|
|
|
|
high_freq = high_freq * 0.6;
|
|
|
|
} else {
|
|
|
|
high_freq = high_freq * 0.5;
|
|
|
|
}
|
|
|
|
}
|
2011-01-29 17:46:18 +01:00
|
|
|
av_dlog(s->avctx, "flags2=0x%x\n", flags2);
|
|
|
|
av_dlog(s->avctx, "version=%d channels=%d sample_rate=%d bitrate=%d block_align=%d\n",
|
2012-10-23 22:30:59 +02:00
|
|
|
s->version, avctx->channels, avctx->sample_rate, avctx->bit_rate,
|
|
|
|
avctx->block_align);
|
2011-01-29 17:46:18 +01:00
|
|
|
av_dlog(s->avctx, "bps=%f bps1=%f high_freq=%f bitoffset=%d\n",
|
2009-06-12 15:12:13 +02:00
|
|
|
bps, bps1, high_freq, s->byte_offset_bits);
|
2011-01-29 17:46:18 +01:00
|
|
|
av_dlog(s->avctx, "use_noise_coding=%d use_exp_vlc=%d nb_block_sizes=%d\n",
|
2009-06-12 15:12:13 +02:00
|
|
|
s->use_noise_coding, s->use_exp_vlc, s->nb_block_sizes);
|
2007-02-06 21:19:04 +01:00
|
|
|
|
|
|
|
/* compute the scale factor band sizes for each MDCT block size */
|
|
|
|
{
|
|
|
|
int a, b, pos, lpos, k, block_len, i, j, n;
|
|
|
|
const uint8_t *table;
|
|
|
|
|
|
|
|
if (s->version == 1) {
|
|
|
|
s->coefs_start = 3;
|
|
|
|
} else {
|
|
|
|
s->coefs_start = 0;
|
|
|
|
}
|
2009-06-12 15:12:13 +02:00
|
|
|
for (k = 0; k < s->nb_block_sizes; k++) {
|
2007-02-06 21:19:04 +01:00
|
|
|
block_len = s->frame_len >> k;
|
|
|
|
|
|
|
|
if (s->version == 1) {
|
|
|
|
lpos = 0;
|
2009-06-12 15:12:13 +02:00
|
|
|
for (i = 0; i < 25; i++) {
|
2010-01-23 09:39:50 +01:00
|
|
|
a = ff_wma_critical_freqs[i];
|
2012-10-23 22:30:59 +02:00
|
|
|
b = avctx->sample_rate;
|
2009-06-12 15:12:13 +02:00
|
|
|
pos = ((block_len * 2 * a) + (b >> 1)) / b;
|
2007-02-06 21:19:04 +01:00
|
|
|
if (pos > block_len)
|
|
|
|
pos = block_len;
|
|
|
|
s->exponent_bands[0][i] = pos - lpos;
|
|
|
|
if (pos >= block_len) {
|
|
|
|
i++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
lpos = pos;
|
|
|
|
}
|
|
|
|
s->exponent_sizes[0] = i;
|
|
|
|
} else {
|
|
|
|
/* hardcoded tables */
|
|
|
|
table = NULL;
|
|
|
|
a = s->frame_len_bits - BLOCK_MIN_BITS - k;
|
|
|
|
if (a < 3) {
|
2012-10-23 22:30:59 +02:00
|
|
|
if (avctx->sample_rate >= 44100) {
|
2007-02-06 21:19:04 +01:00
|
|
|
table = exponent_band_44100[a];
|
2012-10-23 22:30:59 +02:00
|
|
|
} else if (avctx->sample_rate >= 32000) {
|
2007-02-06 21:19:04 +01:00
|
|
|
table = exponent_band_32000[a];
|
2012-10-23 22:30:59 +02:00
|
|
|
} else if (avctx->sample_rate >= 22050) {
|
2007-02-06 21:19:04 +01:00
|
|
|
table = exponent_band_22050[a];
|
2009-06-12 17:48:41 +02:00
|
|
|
}
|
2007-02-06 21:19:04 +01:00
|
|
|
}
|
|
|
|
if (table) {
|
|
|
|
n = *table++;
|
2009-06-12 15:12:13 +02:00
|
|
|
for (i = 0; i < n; i++)
|
2007-02-06 21:19:04 +01:00
|
|
|
s->exponent_bands[k][i] = table[i];
|
|
|
|
s->exponent_sizes[k] = n;
|
|
|
|
} else {
|
|
|
|
j = 0;
|
|
|
|
lpos = 0;
|
2009-06-12 15:12:13 +02:00
|
|
|
for (i = 0; i < 25; i++) {
|
2010-01-23 09:39:50 +01:00
|
|
|
a = ff_wma_critical_freqs[i];
|
2012-10-23 22:30:59 +02:00
|
|
|
b = avctx->sample_rate;
|
2009-06-12 15:12:13 +02:00
|
|
|
pos = ((block_len * 2 * a) + (b << 1)) / (4 * b);
|
2007-02-06 21:19:04 +01:00
|
|
|
pos <<= 2;
|
|
|
|
if (pos > block_len)
|
|
|
|
pos = block_len;
|
|
|
|
if (pos > lpos)
|
|
|
|
s->exponent_bands[k][j++] = pos - lpos;
|
|
|
|
if (pos >= block_len)
|
|
|
|
break;
|
|
|
|
lpos = pos;
|
|
|
|
}
|
|
|
|
s->exponent_sizes[k] = j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* max number of coefs */
|
|
|
|
s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k;
|
|
|
|
/* high freq computation */
|
|
|
|
s->high_band_start[k] = (int)((block_len * 2 * high_freq) /
|
2012-10-23 22:30:59 +02:00
|
|
|
avctx->sample_rate + 0.5);
|
2007-02-06 21:19:04 +01:00
|
|
|
n = s->exponent_sizes[k];
|
|
|
|
j = 0;
|
|
|
|
pos = 0;
|
2009-06-12 15:12:13 +02:00
|
|
|
for (i = 0; i < n; i++) {
|
2007-02-06 21:19:04 +01:00
|
|
|
int start, end;
|
|
|
|
start = pos;
|
|
|
|
pos += s->exponent_bands[k][i];
|
|
|
|
end = pos;
|
|
|
|
if (start < s->high_band_start[k])
|
|
|
|
start = s->high_band_start[k];
|
|
|
|
if (end > s->coefs_end[k])
|
|
|
|
end = s->coefs_end[k];
|
|
|
|
if (end > start)
|
|
|
|
s->exponent_high_bands[k][j++] = end - start;
|
|
|
|
}
|
|
|
|
s->exponent_high_sizes[k] = j;
|
|
|
|
#if 0
|
2007-02-27 10:39:04 +01:00
|
|
|
tprintf(s->avctx, "%5d: coefs_end=%d high_band_start=%d nb_high_bands=%d: ",
|
2009-06-12 15:12:13 +02:00
|
|
|
s->frame_len >> k,
|
|
|
|
s->coefs_end[k],
|
|
|
|
s->high_band_start[k],
|
|
|
|
s->exponent_high_sizes[k]);
|
|
|
|
for (j = 0; j < s->exponent_high_sizes[k]; j++)
|
2007-02-27 10:39:04 +01:00
|
|
|
tprintf(s->avctx, " %d", s->exponent_high_bands[k][j]);
|
|
|
|
tprintf(s->avctx, "\n");
|
2007-02-06 21:19:04 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TRACE
|
|
|
|
{
|
|
|
|
int i, j;
|
2009-06-12 15:12:13 +02:00
|
|
|
for (i = 0; i < s->nb_block_sizes; i++) {
|
2007-02-27 10:39:04 +01:00
|
|
|
tprintf(s->avctx, "%5d: n=%2d:",
|
2009-06-12 15:12:13 +02:00
|
|
|
s->frame_len >> i,
|
|
|
|
s->exponent_sizes[i]);
|
|
|
|
for (j = 0; j < s->exponent_sizes[i]; j++)
|
2007-02-27 10:39:04 +01:00
|
|
|
tprintf(s->avctx, " %d", s->exponent_bands[i][j]);
|
|
|
|
tprintf(s->avctx, "\n");
|
2007-02-06 21:19:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* init MDCT windows : simple sinus window */
|
2009-06-12 15:12:13 +02:00
|
|
|
for (i = 0; i < s->nb_block_sizes; i++) {
|
2010-01-09 14:28:04 +01:00
|
|
|
ff_init_ff_sine_windows(s->frame_len_bits - i);
|
2009-09-15 18:18:16 +02:00
|
|
|
s->windows[i] = ff_sine_windows[s->frame_len_bits - i];
|
2007-02-06 21:19:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
s->reset_block_lengths = 1;
|
|
|
|
|
|
|
|
if (s->use_noise_coding) {
|
|
|
|
|
|
|
|
/* init the noise generator */
|
2009-06-12 17:48:41 +02:00
|
|
|
if (s->use_exp_vlc) {
|
2007-02-06 21:19:04 +01:00
|
|
|
s->noise_mult = 0.02;
|
2009-06-12 17:48:41 +02:00
|
|
|
} else {
|
2007-02-06 21:19:04 +01:00
|
|
|
s->noise_mult = 0.04;
|
2009-06-12 17:48:41 +02:00
|
|
|
}
|
2007-02-06 21:19:04 +01:00
|
|
|
|
|
|
|
#ifdef TRACE
|
2009-06-12 15:12:13 +02:00
|
|
|
for (i = 0; i < NOISE_TAB_SIZE; i++)
|
2007-02-06 21:19:04 +01:00
|
|
|
s->noise_table[i] = 1.0 * s->noise_mult;
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
unsigned int seed;
|
|
|
|
float norm;
|
|
|
|
seed = 1;
|
|
|
|
norm = (1.0 / (float)(1LL << 31)) * sqrt(3) * s->noise_mult;
|
2009-06-12 15:12:13 +02:00
|
|
|
for (i = 0; i < NOISE_TAB_SIZE; i++) {
|
2007-02-06 21:19:04 +01:00
|
|
|
seed = seed * 314159 + 1;
|
|
|
|
s->noise_table[i] = (float)((int)seed) * norm;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* choose the VLC tables for the coefficients */
|
|
|
|
coef_vlc_table = 2;
|
2012-10-23 22:30:59 +02:00
|
|
|
if (avctx->sample_rate >= 32000) {
|
2009-06-12 17:48:41 +02:00
|
|
|
if (bps1 < 0.72) {
|
2007-02-06 21:19:04 +01:00
|
|
|
coef_vlc_table = 0;
|
2009-06-12 17:48:41 +02:00
|
|
|
} else if (bps1 < 1.16) {
|
2007-02-06 21:19:04 +01:00
|
|
|
coef_vlc_table = 1;
|
2009-06-12 17:48:41 +02:00
|
|
|
}
|
2007-02-06 21:19:04 +01:00
|
|
|
}
|
|
|
|
s->coef_vlcs[0]= &coef_vlcs[coef_vlc_table * 2 ];
|
|
|
|
s->coef_vlcs[1]= &coef_vlcs[coef_vlc_table * 2 + 1];
|
|
|
|
init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0], &s->int_table[0],
|
|
|
|
s->coef_vlcs[0]);
|
|
|
|
init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1], &s->int_table[1],
|
|
|
|
s->coef_vlcs[1]);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-12 15:12:13 +02:00
|
|
|
int ff_wma_total_gain_to_bits(int total_gain)
|
|
|
|
{
|
2007-02-06 21:19:04 +01:00
|
|
|
if (total_gain < 15) return 13;
|
|
|
|
else if (total_gain < 32) return 12;
|
|
|
|
else if (total_gain < 40) return 11;
|
|
|
|
else if (total_gain < 45) return 10;
|
|
|
|
else return 9;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ff_wma_end(AVCodecContext *avctx)
|
|
|
|
{
|
2007-02-27 10:54:48 +01:00
|
|
|
WMACodecContext *s = avctx->priv_data;
|
2007-02-06 21:19:04 +01:00
|
|
|
int i;
|
|
|
|
|
2009-06-12 15:12:13 +02:00
|
|
|
for (i = 0; i < s->nb_block_sizes; i++)
|
2007-02-06 21:19:04 +01:00
|
|
|
ff_mdct_end(&s->mdct_ctx[i]);
|
|
|
|
|
|
|
|
if (s->use_exp_vlc) {
|
2012-02-09 10:37:58 +01:00
|
|
|
ff_free_vlc(&s->exp_vlc);
|
2007-02-06 21:19:04 +01:00
|
|
|
}
|
|
|
|
if (s->use_noise_coding) {
|
2012-02-09 10:37:58 +01:00
|
|
|
ff_free_vlc(&s->hgain_vlc);
|
2007-02-06 21:19:04 +01:00
|
|
|
}
|
2009-06-12 15:12:13 +02:00
|
|
|
for (i = 0; i < 2; i++) {
|
2012-02-09 10:37:58 +01:00
|
|
|
ff_free_vlc(&s->coef_vlc[i]);
|
2007-02-06 21:19:04 +01:00
|
|
|
av_free(s->run_table[i]);
|
|
|
|
av_free(s->level_table[i]);
|
2007-07-18 17:36:45 +02:00
|
|
|
av_free(s->int_table[i]);
|
2007-02-06 21:19:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2009-06-12 18:00:26 +02:00
|
|
|
|
2009-06-20 12:28:55 +02:00
|
|
|
/**
|
|
|
|
* Decode an uncompressed coefficient.
|
2010-07-02 13:55:42 +02:00
|
|
|
* @param gb GetBitContext
|
2009-06-20 12:28:55 +02:00
|
|
|
* @return the decoded coefficient
|
|
|
|
*/
|
|
|
|
unsigned int ff_wma_get_large_val(GetBitContext* gb)
|
|
|
|
{
|
|
|
|
/** consumes up to 34 bits */
|
|
|
|
int n_bits = 8;
|
|
|
|
/** decode length */
|
|
|
|
if (get_bits1(gb)) {
|
|
|
|
n_bits += 8;
|
|
|
|
if (get_bits1(gb)) {
|
|
|
|
n_bits += 8;
|
|
|
|
if (get_bits1(gb)) {
|
|
|
|
n_bits += 7;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return get_bits_long(gb, n_bits);
|
|
|
|
}
|
|
|
|
|
2009-06-12 18:00:26 +02:00
|
|
|
/**
|
|
|
|
* Decode run level compressed coefficients.
|
|
|
|
* @param avctx codec context
|
|
|
|
* @param gb bitstream reader context
|
|
|
|
* @param vlc vlc table for get_vlc2
|
|
|
|
* @param level_table level codes
|
|
|
|
* @param run_table run codes
|
|
|
|
* @param version 0 for wma1,2 1 for wmapro
|
|
|
|
* @param ptr output buffer
|
|
|
|
* @param offset offset in the output buffer
|
|
|
|
* @param num_coefs number of input coefficents
|
|
|
|
* @param block_len input buffer length (2^n)
|
|
|
|
* @param frame_len_bits number of bits for escaped run codes
|
|
|
|
* @param coef_nb_bits number of bits for escaped level codes
|
|
|
|
* @return 0 on success, -1 otherwise
|
|
|
|
*/
|
|
|
|
int ff_wma_run_level_decode(AVCodecContext* avctx, GetBitContext* gb,
|
|
|
|
VLC *vlc,
|
2009-09-29 12:38:34 +02:00
|
|
|
const float *level_table, const uint16_t *run_table,
|
2009-06-20 11:05:28 +02:00
|
|
|
int version, WMACoef *ptr, int offset,
|
2009-06-12 18:00:26 +02:00
|
|
|
int num_coefs, int block_len, int frame_len_bits,
|
|
|
|
int coef_nb_bits)
|
|
|
|
{
|
2009-06-20 13:06:48 +02:00
|
|
|
int code, level, sign;
|
2009-09-29 12:38:34 +02:00
|
|
|
const uint32_t *ilvl = (const uint32_t*)level_table;
|
|
|
|
uint32_t *iptr = (uint32_t*)ptr;
|
2009-06-20 13:06:48 +02:00
|
|
|
const unsigned int coef_mask = block_len - 1;
|
|
|
|
for (; offset < num_coefs; offset++) {
|
2009-06-12 18:00:26 +02:00
|
|
|
code = get_vlc2(gb, vlc->table, VLCBITS, VLCMAX);
|
2009-06-20 13:06:48 +02:00
|
|
|
if (code > 1) {
|
|
|
|
/** normal code */
|
|
|
|
offset += run_table[code];
|
2009-09-29 12:38:34 +02:00
|
|
|
sign = get_bits1(gb) - 1;
|
|
|
|
iptr[offset & coef_mask] = ilvl[code] ^ sign<<31;
|
2009-06-20 13:06:48 +02:00
|
|
|
} else if (code == 1) {
|
2009-06-20 13:22:25 +02:00
|
|
|
/** EOB */
|
2009-06-12 18:00:26 +02:00
|
|
|
break;
|
2009-06-20 13:06:48 +02:00
|
|
|
} else {
|
2009-06-20 13:22:25 +02:00
|
|
|
/** escape */
|
2009-06-20 12:28:55 +02:00
|
|
|
if (!version) {
|
2009-06-20 12:33:18 +02:00
|
|
|
level = get_bits(gb, coef_nb_bits);
|
2009-06-20 13:22:25 +02:00
|
|
|
/** NOTE: this is rather suboptimal. reading
|
|
|
|
block_len_bits would be better */
|
2009-06-20 13:06:48 +02:00
|
|
|
offset += get_bits(gb, frame_len_bits);
|
2009-06-20 12:28:55 +02:00
|
|
|
} else {
|
|
|
|
level = ff_wma_get_large_val(gb);
|
|
|
|
/** escape decode */
|
|
|
|
if (get_bits1(gb)) {
|
|
|
|
if (get_bits1(gb)) {
|
|
|
|
if (get_bits1(gb)) {
|
|
|
|
av_log(avctx,AV_LOG_ERROR,
|
|
|
|
"broken escape sequence\n");
|
|
|
|
return -1;
|
|
|
|
} else
|
2009-06-20 13:06:48 +02:00
|
|
|
offset += get_bits(gb, frame_len_bits) + 4;
|
2009-06-20 12:28:55 +02:00
|
|
|
} else
|
2009-06-20 13:06:48 +02:00
|
|
|
offset += get_bits(gb, 2) + 1;
|
|
|
|
}
|
2009-06-20 12:28:55 +02:00
|
|
|
}
|
2009-09-29 12:38:34 +02:00
|
|
|
sign = get_bits1(gb) - 1;
|
|
|
|
ptr[offset & coef_mask] = (level^sign) - sign;
|
2009-06-12 18:00:26 +02:00
|
|
|
}
|
|
|
|
}
|
2009-06-20 13:06:48 +02:00
|
|
|
/** NOTE: EOB can be omitted */
|
|
|
|
if (offset > num_coefs) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "overflow in spectral RLE, ignoring\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-06-12 18:00:26 +02:00
|
|
|
return 0;
|
|
|
|
}
|