2007-02-06 20:19:04 +00:00
|
|
|
/*
|
|
|
|
* WMA compatible codec
|
2009-01-19 15:46:40 +00:00
|
|
|
* Copyright (c) 2002-2007 The FFmpeg Project
|
2007-02-06 20:19:04 +00:00
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* 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
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2013-02-01 09:31:59 +00:00
|
|
|
#include "libavutil/attributes.h"
|
2014-09-02 19:28:50 +00:00
|
|
|
|
2007-02-06 20:19:04 +00:00
|
|
|
#include "avcodec.h"
|
2011-03-19 23:44:00 +00:00
|
|
|
#include "sinewin.h"
|
2007-02-06 20:19:04 +00:00
|
|
|
#include "wma.h"
|
2012-03-06 18:07:10 +00:00
|
|
|
#include "wma_common.h"
|
2014-09-03 10:27:27 +00:00
|
|
|
#include "wma_freqs.h"
|
2007-02-06 20:19:04 +00:00
|
|
|
#include "wmadata.h"
|
|
|
|
|
|
|
|
#undef NDEBUG
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
/* XXX: use same run/length optimization as mpeg decoders */
|
2014-09-02 19:28:50 +00:00
|
|
|
// FIXME maybe split decode / encode or pass flag
|
2013-02-01 09:31:59 +00:00
|
|
|
static av_cold void init_coef_vlc(VLC *vlc, uint16_t **prun_table,
|
|
|
|
float **plevel_table, uint16_t **pint_table,
|
|
|
|
const CoefVLCTable *vlc_table)
|
2007-02-06 20:19:04 +00:00
|
|
|
{
|
2014-09-02 19:28:50 +00:00
|
|
|
int n = vlc_table->n;
|
2009-06-12 13:12:13 +00:00
|
|
|
const uint8_t *table_bits = vlc_table->huffbits;
|
|
|
|
const uint32_t *table_codes = vlc_table->huffcodes;
|
2007-02-06 20:19:04 +00:00
|
|
|
const uint16_t *levels_table = vlc_table->levels;
|
|
|
|
uint16_t *run_table, *level_table, *int_table;
|
2009-09-29 10:38:34 +00:00
|
|
|
float *flevel_table;
|
2007-02-06 20:19:04 +00:00
|
|
|
int i, l, j, k, level;
|
|
|
|
|
|
|
|
init_vlc(vlc, VLCBITS, n, table_bits, 1, 1, table_codes, 4, 4, 0);
|
|
|
|
|
2014-09-03 20:03:21 +00:00
|
|
|
run_table = av_malloc_array(n, sizeof(uint16_t));
|
|
|
|
level_table = av_malloc_array(n, sizeof(uint16_t));
|
|
|
|
flevel_table = av_malloc_array(n, sizeof(*flevel_table));
|
|
|
|
int_table = av_malloc_array(n, sizeof(uint16_t));
|
2014-09-02 19:28:50 +00:00
|
|
|
i = 2;
|
|
|
|
level = 1;
|
|
|
|
k = 0;
|
2007-02-06 20:19:04 +00:00
|
|
|
while (i < n) {
|
2009-06-12 13:12:13 +00:00
|
|
|
int_table[k] = i;
|
2014-09-02 19:28:50 +00:00
|
|
|
l = levels_table[k++];
|
2009-06-12 13:12:13 +00:00
|
|
|
for (j = 0; j < l; j++) {
|
2014-09-02 19:28:50 +00:00
|
|
|
run_table[i] = j;
|
|
|
|
level_table[i] = level;
|
|
|
|
flevel_table[i] = level;
|
2007-02-06 20:19:04 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
level++;
|
|
|
|
}
|
2009-06-12 13:12:13 +00:00
|
|
|
*prun_table = run_table;
|
2009-09-29 10:38:34 +00:00
|
|
|
*plevel_table = flevel_table;
|
2009-06-12 13:12:13 +00:00
|
|
|
*pint_table = int_table;
|
2009-09-29 10:38:34 +00:00
|
|
|
av_free(level_table);
|
2007-02-06 20:19:04 +00:00
|
|
|
}
|
|
|
|
|
2013-02-01 09:31:59 +00:00
|
|
|
av_cold int ff_wma_init(AVCodecContext *avctx, int flags2)
|
2007-02-06 20:19:04 +00:00
|
|
|
{
|
2007-02-27 09:54:48 +00:00
|
|
|
WMACodecContext *s = avctx->priv_data;
|
2007-02-06 20:19:04 +00:00
|
|
|
int i;
|
|
|
|
float bps1, high_freq;
|
|
|
|
volatile float bps;
|
|
|
|
int sample_rate1;
|
|
|
|
int coef_vlc_table;
|
|
|
|
|
2014-09-02 19:28:50 +00:00
|
|
|
if (avctx->sample_rate <= 0 || avctx->sample_rate > 50000 ||
|
|
|
|
avctx->channels <= 0 || avctx->channels > 2 ||
|
|
|
|
avctx->bit_rate <= 0)
|
2007-05-30 23:06:37 +00:00
|
|
|
return -1;
|
|
|
|
|
2011-01-30 15:06:46 +00:00
|
|
|
ff_fmt_convert_init(&s->fmt_conv, avctx);
|
2007-02-06 20:19:04 +00:00
|
|
|
|
2014-09-02 19:28:50 +00:00
|
|
|
if (avctx->codec->id == AV_CODEC_ID_WMAV1)
|
2007-02-06 20:19:04 +00:00
|
|
|
s->version = 1;
|
2014-09-02 19:28:50 +00:00
|
|
|
else
|
2007-02-06 20:19:04 +00:00
|
|
|
s->version = 2;
|
|
|
|
|
|
|
|
/* compute MDCT block size */
|
2012-10-23 20:30:59 +00:00
|
|
|
s->frame_len_bits = ff_wma_get_frame_len_bits(avctx->sample_rate,
|
|
|
|
s->version, 0);
|
2011-11-22 18:37:52 +00: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 15:21:43 +00:00
|
|
|
|
2007-02-06 20:19:04 +00: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 20:30:59 +00:00
|
|
|
if ((avctx->bit_rate / avctx->channels) >= 32000)
|
2007-02-06 20:19:04 +00: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;
|
2014-09-02 19:28:50 +00:00
|
|
|
} else
|
2007-02-06 20:19:04 +00:00
|
|
|
s->nb_block_sizes = 1;
|
|
|
|
|
2007-02-24 09:41:43 +00:00
|
|
|
/* init rate dependent parameters */
|
2007-02-06 20:19:04 +00:00
|
|
|
s->use_noise_coding = 1;
|
2014-09-02 19:28:50 +00:00
|
|
|
high_freq = avctx->sample_rate * 0.5;
|
2007-02-06 20:19:04 +00:00
|
|
|
|
|
|
|
/* if version 2, then the rates are normalized */
|
2012-10-23 20:30:59 +00:00
|
|
|
sample_rate1 = avctx->sample_rate;
|
2007-02-06 20:19:04 +00:00
|
|
|
if (s->version == 2) {
|
2014-09-02 19:28:50 +00:00
|
|
|
if (sample_rate1 >= 44100)
|
2007-02-06 20:19:04 +00:00
|
|
|
sample_rate1 = 44100;
|
2014-09-02 19:28:50 +00:00
|
|
|
else if (sample_rate1 >= 22050)
|
2007-02-06 20:19:04 +00:00
|
|
|
sample_rate1 = 22050;
|
2014-09-02 19:28:50 +00:00
|
|
|
else if (sample_rate1 >= 16000)
|
2007-02-06 20:19:04 +00:00
|
|
|
sample_rate1 = 16000;
|
2014-09-02 19:28:50 +00:00
|
|
|
else if (sample_rate1 >= 11025)
|
2007-02-06 20:19:04 +00:00
|
|
|
sample_rate1 = 11025;
|
2014-09-02 19:28:50 +00:00
|
|
|
else if (sample_rate1 >= 8000)
|
2007-02-06 20:19:04 +00:00
|
|
|
sample_rate1 = 8000;
|
|
|
|
}
|
|
|
|
|
2014-09-02 19:28:50 +00:00
|
|
|
bps = (float) avctx->bit_rate /
|
|
|
|
(float) (avctx->channels * avctx->sample_rate);
|
|
|
|
s->byte_offset_bits = av_log2((int) (bps * s->frame_len / 8.0 + 0.5)) + 2;
|
2013-01-30 21:56:45 +00:00
|
|
|
if (s->byte_offset_bits + 3 > MIN_CACHE_BITS) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "byte_offset_bits %d is too large\n", s->byte_offset_bits);
|
|
|
|
return AVERROR_PATCHWELCOME;
|
|
|
|
}
|
2007-02-06 20:19:04 +00:00
|
|
|
|
|
|
|
/* compute high frequency value and choose if noise coding should
|
2014-09-02 19:28:50 +00:00
|
|
|
* be activated */
|
2007-02-06 20:19:04 +00:00
|
|
|
bps1 = bps;
|
2012-10-23 20:30:59 +00:00
|
|
|
if (avctx->channels == 2)
|
2007-02-06 20:19:04 +00:00
|
|
|
bps1 = bps * 1.6;
|
|
|
|
if (sample_rate1 == 44100) {
|
2014-09-02 19:28:50 +00:00
|
|
|
if (bps1 >= 0.61)
|
2007-02-06 20:19:04 +00:00
|
|
|
s->use_noise_coding = 0;
|
2014-09-02 19:28:50 +00:00
|
|
|
else
|
2007-02-06 20:19:04 +00:00
|
|
|
high_freq = high_freq * 0.4;
|
|
|
|
} else if (sample_rate1 == 22050) {
|
2014-09-02 19:28:50 +00:00
|
|
|
if (bps1 >= 1.16)
|
2007-02-06 20:19:04 +00:00
|
|
|
s->use_noise_coding = 0;
|
2014-09-02 19:28:50 +00:00
|
|
|
else if (bps1 >= 0.72)
|
2007-02-06 20:19:04 +00:00
|
|
|
high_freq = high_freq * 0.7;
|
2014-09-02 19:28:50 +00:00
|
|
|
else
|
2007-02-06 20:19:04 +00:00
|
|
|
high_freq = high_freq * 0.6;
|
|
|
|
} else if (sample_rate1 == 16000) {
|
2014-09-02 19:28:50 +00:00
|
|
|
if (bps > 0.5)
|
2007-02-06 20:19:04 +00:00
|
|
|
high_freq = high_freq * 0.5;
|
2014-09-02 19:28:50 +00:00
|
|
|
else
|
2007-02-06 20:19:04 +00:00
|
|
|
high_freq = high_freq * 0.3;
|
2014-09-02 19:28:50 +00:00
|
|
|
} else if (sample_rate1 == 11025)
|
2007-02-06 20:19:04 +00:00
|
|
|
high_freq = high_freq * 0.7;
|
2014-09-02 19:28:50 +00:00
|
|
|
else if (sample_rate1 == 8000) {
|
|
|
|
if (bps <= 0.625)
|
2007-02-06 20:19:04 +00:00
|
|
|
high_freq = high_freq * 0.5;
|
2014-09-02 19:28:50 +00:00
|
|
|
else if (bps > 0.75)
|
2007-02-06 20:19:04 +00:00
|
|
|
s->use_noise_coding = 0;
|
2014-09-02 19:28:50 +00:00
|
|
|
else
|
2007-02-06 20:19:04 +00:00
|
|
|
high_freq = high_freq * 0.65;
|
|
|
|
} else {
|
2014-09-02 19:28:50 +00:00
|
|
|
if (bps >= 0.8)
|
2007-02-06 20:19:04 +00:00
|
|
|
high_freq = high_freq * 0.75;
|
2014-09-02 19:28:50 +00:00
|
|
|
else if (bps >= 0.6)
|
2007-02-06 20:19:04 +00:00
|
|
|
high_freq = high_freq * 0.6;
|
2014-09-02 19:28:50 +00:00
|
|
|
else
|
2007-02-06 20:19:04 +00:00
|
|
|
high_freq = high_freq * 0.5;
|
|
|
|
}
|
2011-01-29 16:46:18 +00: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 20:30:59 +00:00
|
|
|
s->version, avctx->channels, avctx->sample_rate, avctx->bit_rate,
|
|
|
|
avctx->block_align);
|
2011-01-29 16:46:18 +00:00
|
|
|
av_dlog(s->avctx, "bps=%f bps1=%f high_freq=%f bitoffset=%d\n",
|
2009-06-12 13:12:13 +00:00
|
|
|
bps, bps1, high_freq, s->byte_offset_bits);
|
2011-01-29 16:46:18 +00:00
|
|
|
av_dlog(s->avctx, "use_noise_coding=%d use_exp_vlc=%d nb_block_sizes=%d\n",
|
2009-06-12 13:12:13 +00:00
|
|
|
s->use_noise_coding, s->use_exp_vlc, s->nb_block_sizes);
|
2007-02-06 20:19:04 +00: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;
|
|
|
|
|
2014-09-02 19:28:50 +00:00
|
|
|
if (s->version == 1)
|
2007-02-06 20:19:04 +00:00
|
|
|
s->coefs_start = 3;
|
2014-09-02 19:28:50 +00:00
|
|
|
else
|
2007-02-06 20:19:04 +00:00
|
|
|
s->coefs_start = 0;
|
2009-06-12 13:12:13 +00:00
|
|
|
for (k = 0; k < s->nb_block_sizes; k++) {
|
2007-02-06 20:19:04 +00:00
|
|
|
block_len = s->frame_len >> k;
|
|
|
|
|
|
|
|
if (s->version == 1) {
|
|
|
|
lpos = 0;
|
2009-06-12 13:12:13 +00:00
|
|
|
for (i = 0; i < 25; i++) {
|
2014-09-02 19:28:50 +00:00
|
|
|
a = ff_wma_critical_freqs[i];
|
|
|
|
b = avctx->sample_rate;
|
2009-06-12 13:12:13 +00:00
|
|
|
pos = ((block_len * 2 * a) + (b >> 1)) / b;
|
2007-02-06 20:19:04 +00: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;
|
2014-09-02 19:28:50 +00:00
|
|
|
a = s->frame_len_bits - BLOCK_MIN_BITS - k;
|
2007-02-06 20:19:04 +00:00
|
|
|
if (a < 3) {
|
2014-09-02 19:28:50 +00:00
|
|
|
if (avctx->sample_rate >= 44100)
|
2007-02-06 20:19:04 +00:00
|
|
|
table = exponent_band_44100[a];
|
2014-09-02 19:28:50 +00:00
|
|
|
else if (avctx->sample_rate >= 32000)
|
2007-02-06 20:19:04 +00:00
|
|
|
table = exponent_band_32000[a];
|
2014-09-02 19:28:50 +00:00
|
|
|
else if (avctx->sample_rate >= 22050)
|
2007-02-06 20:19:04 +00:00
|
|
|
table = exponent_band_22050[a];
|
|
|
|
}
|
|
|
|
if (table) {
|
|
|
|
n = *table++;
|
2009-06-12 13:12:13 +00:00
|
|
|
for (i = 0; i < n; i++)
|
2007-02-06 20:19:04 +00:00
|
|
|
s->exponent_bands[k][i] = table[i];
|
|
|
|
s->exponent_sizes[k] = n;
|
|
|
|
} else {
|
2014-09-02 19:28:50 +00:00
|
|
|
j = 0;
|
2007-02-06 20:19:04 +00:00
|
|
|
lpos = 0;
|
2009-06-12 13:12:13 +00:00
|
|
|
for (i = 0; i < 25; i++) {
|
2014-09-02 19:28:50 +00:00
|
|
|
a = ff_wma_critical_freqs[i];
|
|
|
|
b = avctx->sample_rate;
|
|
|
|
pos = ((block_len * 2 * a) + (b << 1)) / (4 * b);
|
2007-02-06 20:19:04 +00: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 */
|
2014-09-02 19:28:50 +00:00
|
|
|
s->high_band_start[k] = (int) ((block_len * 2 * high_freq) /
|
|
|
|
avctx->sample_rate + 0.5);
|
|
|
|
n = s->exponent_sizes[k];
|
|
|
|
j = 0;
|
2007-02-06 20:19:04 +00:00
|
|
|
pos = 0;
|
2009-06-12 13:12:13 +00:00
|
|
|
for (i = 0; i < n; i++) {
|
2007-02-06 20:19:04 +00:00
|
|
|
int start, end;
|
|
|
|
start = pos;
|
2014-09-02 19:28:50 +00:00
|
|
|
pos += s->exponent_bands[k][i];
|
|
|
|
end = pos;
|
2007-02-06 20:19:04 +00:00
|
|
|
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 09:39:04 +00:00
|
|
|
tprintf(s->avctx, "%5d: coefs_end=%d high_band_start=%d nb_high_bands=%d: ",
|
2009-06-12 13:12:13 +00: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 09:39:04 +00:00
|
|
|
tprintf(s->avctx, " %d", s->exponent_high_bands[k][j]);
|
|
|
|
tprintf(s->avctx, "\n");
|
2014-09-02 19:28:50 +00:00
|
|
|
#endif /* 0 */
|
2007-02-06 20:19:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TRACE
|
|
|
|
{
|
|
|
|
int i, j;
|
2009-06-12 13:12:13 +00:00
|
|
|
for (i = 0; i < s->nb_block_sizes; i++) {
|
2007-02-27 09:39:04 +00:00
|
|
|
tprintf(s->avctx, "%5d: n=%2d:",
|
2009-06-12 13:12:13 +00:00
|
|
|
s->frame_len >> i,
|
|
|
|
s->exponent_sizes[i]);
|
|
|
|
for (j = 0; j < s->exponent_sizes[i]; j++)
|
2007-02-27 09:39:04 +00:00
|
|
|
tprintf(s->avctx, " %d", s->exponent_bands[i][j]);
|
|
|
|
tprintf(s->avctx, "\n");
|
2007-02-06 20:19:04 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-02 19:28:50 +00:00
|
|
|
#endif /* TRACE */
|
2007-02-06 20:19:04 +00:00
|
|
|
|
2013-05-04 19:18:13 +00:00
|
|
|
/* init MDCT windows : simple sine window */
|
2009-06-12 13:12:13 +00:00
|
|
|
for (i = 0; i < s->nb_block_sizes; i++) {
|
2010-01-09 13:28:04 +00:00
|
|
|
ff_init_ff_sine_windows(s->frame_len_bits - i);
|
2009-09-15 16:18:16 +00:00
|
|
|
s->windows[i] = ff_sine_windows[s->frame_len_bits - i];
|
2007-02-06 20:19:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s->reset_block_lengths = 1;
|
|
|
|
|
|
|
|
if (s->use_noise_coding) {
|
|
|
|
/* init the noise generator */
|
2014-09-02 19:28:50 +00:00
|
|
|
if (s->use_exp_vlc)
|
2007-02-06 20:19:04 +00:00
|
|
|
s->noise_mult = 0.02;
|
2014-09-02 19:28:50 +00:00
|
|
|
else
|
2007-02-06 20:19:04 +00:00
|
|
|
s->noise_mult = 0.04;
|
|
|
|
|
|
|
|
#ifdef TRACE
|
2009-06-12 13:12:13 +00:00
|
|
|
for (i = 0; i < NOISE_TAB_SIZE; i++)
|
2007-02-06 20:19:04 +00:00
|
|
|
s->noise_table[i] = 1.0 * s->noise_mult;
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
unsigned int seed;
|
|
|
|
float norm;
|
|
|
|
seed = 1;
|
2014-09-02 19:28:50 +00:00
|
|
|
norm = (1.0 / (float) (1LL << 31)) * sqrt(3) * s->noise_mult;
|
2009-06-12 13:12:13 +00:00
|
|
|
for (i = 0; i < NOISE_TAB_SIZE; i++) {
|
2014-09-02 19:28:50 +00:00
|
|
|
seed = seed * 314159 + 1;
|
|
|
|
s->noise_table[i] = (float) ((int) seed) * norm;
|
2007-02-06 20:19:04 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-02 19:28:50 +00:00
|
|
|
#endif /* TRACE */
|
2007-02-06 20:19:04 +00:00
|
|
|
}
|
|
|
|
|
2014-11-30 10:19:29 +00:00
|
|
|
s->fdsp = avpriv_float_dsp_alloc(avctx->flags & CODEC_FLAG_BITEXACT);
|
|
|
|
if (!s->fdsp)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2007-02-06 20:19:04 +00:00
|
|
|
/* choose the VLC tables for the coefficients */
|
|
|
|
coef_vlc_table = 2;
|
2012-10-23 20:30:59 +00:00
|
|
|
if (avctx->sample_rate >= 32000) {
|
2014-09-02 19:28:50 +00:00
|
|
|
if (bps1 < 0.72)
|
2007-02-06 20:19:04 +00:00
|
|
|
coef_vlc_table = 0;
|
2014-09-02 19:28:50 +00:00
|
|
|
else if (bps1 < 1.16)
|
2007-02-06 20:19:04 +00:00
|
|
|
coef_vlc_table = 1;
|
|
|
|
}
|
2014-09-02 19:28:50 +00: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]);
|
2007-02-06 20:19:04 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-12 13:12:13 +00:00
|
|
|
int ff_wma_total_gain_to_bits(int total_gain)
|
|
|
|
{
|
2014-09-02 19:28:50 +00: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;
|
2007-02-06 20:19:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ff_wma_end(AVCodecContext *avctx)
|
|
|
|
{
|
2007-02-27 09:54:48 +00:00
|
|
|
WMACodecContext *s = avctx->priv_data;
|
2007-02-06 20:19:04 +00:00
|
|
|
int i;
|
|
|
|
|
2009-06-12 13:12:13 +00:00
|
|
|
for (i = 0; i < s->nb_block_sizes; i++)
|
2007-02-06 20:19:04 +00:00
|
|
|
ff_mdct_end(&s->mdct_ctx[i]);
|
|
|
|
|
2014-09-02 19:28:50 +00:00
|
|
|
if (s->use_exp_vlc)
|
2012-02-09 09:37:58 +00:00
|
|
|
ff_free_vlc(&s->exp_vlc);
|
2014-09-02 19:28:50 +00:00
|
|
|
if (s->use_noise_coding)
|
2012-02-09 09:37:58 +00:00
|
|
|
ff_free_vlc(&s->hgain_vlc);
|
2009-06-12 13:12:13 +00:00
|
|
|
for (i = 0; i < 2; i++) {
|
2012-02-09 09:37:58 +00:00
|
|
|
ff_free_vlc(&s->coef_vlc[i]);
|
2014-04-05 19:34:03 +00:00
|
|
|
av_freep(&s->run_table[i]);
|
|
|
|
av_freep(&s->level_table[i]);
|
|
|
|
av_freep(&s->int_table[i]);
|
2007-02-06 20:19:04 +00:00
|
|
|
}
|
2014-11-30 10:19:29 +00:00
|
|
|
av_freep(&s->fdsp);
|
2007-02-06 20:19:04 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2009-06-12 16:00:26 +00:00
|
|
|
|
2009-06-20 10:28:55 +00:00
|
|
|
/**
|
|
|
|
* Decode an uncompressed coefficient.
|
2010-07-02 11:55:42 +00:00
|
|
|
* @param gb GetBitContext
|
2009-06-20 10:28:55 +00:00
|
|
|
* @return the decoded coefficient
|
|
|
|
*/
|
2014-09-02 19:28:50 +00:00
|
|
|
unsigned int ff_wma_get_large_val(GetBitContext *gb)
|
2009-06-20 10:28:55 +00:00
|
|
|
{
|
|
|
|
/** 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;
|
2014-09-02 19:28:50 +00:00
|
|
|
if (get_bits1(gb))
|
2009-06-20 10:28:55 +00:00
|
|
|
n_bits += 7;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return get_bits_long(gb, n_bits);
|
|
|
|
}
|
|
|
|
|
2009-06-12 16:00:26 +00: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
|
|
|
|
*/
|
2014-09-02 19:28:50 +00:00
|
|
|
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,
|
2009-06-12 16:00:26 +00:00
|
|
|
int coef_nb_bits)
|
|
|
|
{
|
2009-06-20 11:06:48 +00:00
|
|
|
int code, level, sign;
|
2014-09-02 19:28:50 +00:00
|
|
|
const uint32_t *ilvl = (const uint32_t *) level_table;
|
|
|
|
uint32_t *iptr = (uint32_t *) ptr;
|
2009-06-20 11:06:48 +00:00
|
|
|
const unsigned int coef_mask = block_len - 1;
|
|
|
|
for (; offset < num_coefs; offset++) {
|
2009-06-12 16:00:26 +00:00
|
|
|
code = get_vlc2(gb, vlc->table, VLCBITS, VLCMAX);
|
2009-06-20 11:06:48 +00:00
|
|
|
if (code > 1) {
|
|
|
|
/** normal code */
|
2014-09-02 19:28:50 +00:00
|
|
|
offset += run_table[code];
|
|
|
|
sign = get_bits1(gb) - 1;
|
|
|
|
iptr[offset & coef_mask] = ilvl[code] ^ sign << 31;
|
2009-06-20 11:06:48 +00:00
|
|
|
} else if (code == 1) {
|
2009-06-20 11:22:25 +00:00
|
|
|
/** EOB */
|
2009-06-12 16:00:26 +00:00
|
|
|
break;
|
2009-06-20 11:06:48 +00:00
|
|
|
} else {
|
2009-06-20 11:22:25 +00:00
|
|
|
/** escape */
|
2009-06-20 10:28:55 +00:00
|
|
|
if (!version) {
|
2009-06-20 10:33:18 +00:00
|
|
|
level = get_bits(gb, coef_nb_bits);
|
2009-06-20 11:22:25 +00:00
|
|
|
/** NOTE: this is rather suboptimal. reading
|
2014-09-02 19:28:50 +00:00
|
|
|
* block_len_bits would be better */
|
2009-06-20 11:06:48 +00:00
|
|
|
offset += get_bits(gb, frame_len_bits);
|
2009-06-20 10:28:55 +00:00
|
|
|
} else {
|
|
|
|
level = ff_wma_get_large_val(gb);
|
|
|
|
/** escape decode */
|
|
|
|
if (get_bits1(gb)) {
|
|
|
|
if (get_bits1(gb)) {
|
|
|
|
if (get_bits1(gb)) {
|
2014-09-02 19:28:50 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"broken escape sequence\n");
|
2009-06-20 10:28:55 +00:00
|
|
|
return -1;
|
|
|
|
} else
|
2009-06-20 11:06:48 +00:00
|
|
|
offset += get_bits(gb, frame_len_bits) + 4;
|
2009-06-20 10:28:55 +00:00
|
|
|
} else
|
2009-06-20 11:06:48 +00:00
|
|
|
offset += get_bits(gb, 2) + 1;
|
|
|
|
}
|
2009-06-20 10:28:55 +00:00
|
|
|
}
|
2014-09-02 19:28:50 +00:00
|
|
|
sign = get_bits1(gb) - 1;
|
|
|
|
ptr[offset & coef_mask] = (level ^ sign) - sign;
|
2009-06-12 16:00:26 +00:00
|
|
|
}
|
|
|
|
}
|
2009-06-20 11:06:48 +00: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 16:00:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|