wma: K&R formatting cosmetics

Signed-off-by: Diego Biurrun <diego@biurrun.de>
This commit is contained in:
Gabriel Dume 2014-09-02 15:28:50 -04:00 committed by Diego Biurrun
parent c487972ed0
commit d2a4e4b9cc
6 changed files with 1745 additions and 1756 deletions

View File

@ -20,6 +20,7 @@
*/
#include "libavutil/attributes.h"
#include "avcodec.h"
#include "sinewin.h"
#include "wma.h"
@ -30,12 +31,12 @@
#include <assert.h>
/* XXX: use same run/length optimization as mpeg decoders */
//FIXME maybe split decode / encode or pass flag
// FIXME maybe split decode / encode or pass flag
static av_cold void init_coef_vlc(VLC *vlc, uint16_t **prun_table,
float **plevel_table, uint16_t **pint_table,
const CoefVLCTable *vlc_table)
{
int n = vlc_table->n;
int n = vlc_table->n;
const uint8_t *table_bits = vlc_table->huffbits;
const uint32_t *table_codes = vlc_table->huffcodes;
const uint16_t *levels_table = vlc_table->levels;
@ -45,20 +46,20 @@ static av_cold void init_coef_vlc(VLC *vlc, uint16_t **prun_table,
init_vlc(vlc, VLCBITS, n, table_bits, 1, 1, table_codes, 4, 4, 0);
run_table = av_malloc(n * sizeof(uint16_t));
level_table = av_malloc(n * sizeof(uint16_t));
flevel_table= av_malloc(n * sizeof(*flevel_table));
int_table = av_malloc(n * sizeof(uint16_t));
i = 2;
level = 1;
k = 0;
run_table = av_malloc(n * sizeof(uint16_t));
level_table = av_malloc(n * sizeof(uint16_t));
flevel_table = av_malloc(n * sizeof(*flevel_table));
int_table = av_malloc(n * sizeof(uint16_t));
i = 2;
level = 1;
k = 0;
while (i < n) {
int_table[k] = i;
l = levels_table[k++];
l = levels_table[k++];
for (j = 0; j < l; j++) {
run_table[i] = j;
level_table[i] = level;
flevel_table[i]= level;
run_table[i] = j;
level_table[i] = level;
flevel_table[i] = level;
i++;
}
level++;
@ -78,19 +79,18 @@ av_cold int ff_wma_init(AVCodecContext *avctx, int flags2)
int sample_rate1;
int coef_vlc_table;
if ( avctx->sample_rate <= 0 || avctx->sample_rate > 50000
|| avctx->channels <= 0 || avctx->channels > 2
|| avctx->bit_rate <= 0)
if (avctx->sample_rate <= 0 || avctx->sample_rate > 50000 ||
avctx->channels <= 0 || avctx->channels > 2 ||
avctx->bit_rate <= 0)
return -1;
ff_fmt_convert_init(&s->fmt_conv, avctx);
avpriv_float_dsp_init(&s->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
if (avctx->codec->id == AV_CODEC_ID_WMAV1) {
if (avctx->codec->id == AV_CODEC_ID_WMAV1)
s->version = 1;
} else {
else
s->version = 2;
}
/* compute MDCT block size */
s->frame_len_bits = ff_wma_get_frame_len_bits(avctx->sample_rate,
@ -109,76 +109,70 @@ av_cold int ff_wma_init(AVCodecContext *avctx, int flags2)
if (nb > nb_max)
nb = nb_max;
s->nb_block_sizes = nb + 1;
} else {
} else
s->nb_block_sizes = 1;
}
/* init rate dependent parameters */
s->use_noise_coding = 1;
high_freq = avctx->sample_rate * 0.5;
high_freq = avctx->sample_rate * 0.5;
/* if version 2, then the rates are normalized */
sample_rate1 = avctx->sample_rate;
if (s->version == 2) {
if (sample_rate1 >= 44100) {
if (sample_rate1 >= 44100)
sample_rate1 = 44100;
} else if (sample_rate1 >= 22050) {
else if (sample_rate1 >= 22050)
sample_rate1 = 22050;
} else if (sample_rate1 >= 16000) {
else if (sample_rate1 >= 16000)
sample_rate1 = 16000;
} else if (sample_rate1 >= 11025) {
else if (sample_rate1 >= 11025)
sample_rate1 = 11025;
} else if (sample_rate1 >= 8000) {
else if (sample_rate1 >= 8000)
sample_rate1 = 8000;
}
}
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;
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;
/* compute high frequency value and choose if noise coding should
be activated */
* be activated */
bps1 = bps;
if (avctx->channels == 2)
bps1 = bps * 1.6;
if (sample_rate1 == 44100) {
if (bps1 >= 0.61) {
if (bps1 >= 0.61)
s->use_noise_coding = 0;
} else {
else
high_freq = high_freq * 0.4;
}
} else if (sample_rate1 == 22050) {
if (bps1 >= 1.16) {
if (bps1 >= 1.16)
s->use_noise_coding = 0;
} else if (bps1 >= 0.72) {
else if (bps1 >= 0.72)
high_freq = high_freq * 0.7;
} else {
else
high_freq = high_freq * 0.6;
}
} else if (sample_rate1 == 16000) {
if (bps > 0.5) {
if (bps > 0.5)
high_freq = high_freq * 0.5;
} else {
else
high_freq = high_freq * 0.3;
}
} else if (sample_rate1 == 11025) {
} else if (sample_rate1 == 11025)
high_freq = high_freq * 0.7;
} else if (sample_rate1 == 8000) {
if (bps <= 0.625) {
else if (sample_rate1 == 8000) {
if (bps <= 0.625)
high_freq = high_freq * 0.5;
} else if (bps > 0.75) {
else if (bps > 0.75)
s->use_noise_coding = 0;
} else {
else
high_freq = high_freq * 0.65;
}
} else {
if (bps >= 0.8) {
if (bps >= 0.8)
high_freq = high_freq * 0.75;
} else if (bps >= 0.6) {
else if (bps >= 0.6)
high_freq = high_freq * 0.6;
} else {
else
high_freq = high_freq * 0.5;
}
}
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",
@ -194,19 +188,18 @@ av_cold int ff_wma_init(AVCodecContext *avctx, int flags2)
int a, b, pos, lpos, k, block_len, i, j, n;
const uint8_t *table;
if (s->version == 1) {
if (s->version == 1)
s->coefs_start = 3;
} else {
else
s->coefs_start = 0;
}
for (k = 0; k < s->nb_block_sizes; k++) {
block_len = s->frame_len >> k;
if (s->version == 1) {
lpos = 0;
for (i = 0; i < 25; i++) {
a = ff_wma_critical_freqs[i];
b = avctx->sample_rate;
a = ff_wma_critical_freqs[i];
b = avctx->sample_rate;
pos = ((block_len * 2 * a) + (b >> 1)) / b;
if (pos > block_len)
pos = block_len;
@ -221,15 +214,14 @@ av_cold int ff_wma_init(AVCodecContext *avctx, int flags2)
} else {
/* hardcoded tables */
table = NULL;
a = s->frame_len_bits - BLOCK_MIN_BITS - k;
a = s->frame_len_bits - BLOCK_MIN_BITS - k;
if (a < 3) {
if (avctx->sample_rate >= 44100) {
if (avctx->sample_rate >= 44100)
table = exponent_band_44100[a];
} else if (avctx->sample_rate >= 32000) {
else if (avctx->sample_rate >= 32000)
table = exponent_band_32000[a];
} else if (avctx->sample_rate >= 22050) {
else if (avctx->sample_rate >= 22050)
table = exponent_band_22050[a];
}
}
if (table) {
n = *table++;
@ -237,12 +229,12 @@ av_cold int ff_wma_init(AVCodecContext *avctx, int flags2)
s->exponent_bands[k][i] = table[i];
s->exponent_sizes[k] = n;
} else {
j = 0;
j = 0;
lpos = 0;
for (i = 0; i < 25; i++) {
a = ff_wma_critical_freqs[i];
b = avctx->sample_rate;
pos = ((block_len * 2 * a) + (b << 1)) / (4 * b);
a = ff_wma_critical_freqs[i];
b = avctx->sample_rate;
pos = ((block_len * 2 * a) + (b << 1)) / (4 * b);
pos <<= 2;
if (pos > block_len)
pos = block_len;
@ -259,16 +251,16 @@ av_cold int ff_wma_init(AVCodecContext *avctx, int flags2)
/* 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) /
avctx->sample_rate + 0.5);
n = s->exponent_sizes[k];
j = 0;
s->high_band_start[k] = (int) ((block_len * 2 * high_freq) /
avctx->sample_rate + 0.5);
n = s->exponent_sizes[k];
j = 0;
pos = 0;
for (i = 0; i < n; i++) {
int start, end;
start = pos;
pos += s->exponent_bands[k][i];
end = 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])
@ -286,7 +278,7 @@ av_cold int ff_wma_init(AVCodecContext *avctx, int flags2)
for (j = 0; j < s->exponent_high_sizes[k]; j++)
tprintf(s->avctx, " %d", s->exponent_high_bands[k][j]);
tprintf(s->avctx, "\n");
#endif
#endif /* 0 */
}
}
@ -302,7 +294,7 @@ av_cold int ff_wma_init(AVCodecContext *avctx, int flags2)
tprintf(s->avctx, "\n");
}
}
#endif
#endif /* TRACE */
/* init MDCT windows : simple sine window */
for (i = 0; i < s->nb_block_sizes; i++) {
@ -313,13 +305,11 @@ av_cold int ff_wma_init(AVCodecContext *avctx, int flags2)
s->reset_block_lengths = 1;
if (s->use_noise_coding) {
/* init the noise generator */
if (s->use_exp_vlc) {
if (s->use_exp_vlc)
s->noise_mult = 0.02;
} else {
else
s->noise_mult = 0.04;
}
#ifdef TRACE
for (i = 0; i < NOISE_TAB_SIZE; i++)
@ -329,41 +319,45 @@ av_cold int ff_wma_init(AVCodecContext *avctx, int flags2)
unsigned int seed;
float norm;
seed = 1;
norm = (1.0 / (float)(1LL << 31)) * sqrt(3) * s->noise_mult;
norm = (1.0 / (float) (1LL << 31)) * sqrt(3) * s->noise_mult;
for (i = 0; i < NOISE_TAB_SIZE; i++) {
seed = seed * 314159 + 1;
s->noise_table[i] = (float)((int)seed) * norm;
seed = seed * 314159 + 1;
s->noise_table[i] = (float) ((int) seed) * norm;
}
}
#endif
#endif /* TRACE */
}
/* choose the VLC tables for the coefficients */
coef_vlc_table = 2;
if (avctx->sample_rate >= 32000) {
if (bps1 < 0.72) {
if (bps1 < 0.72)
coef_vlc_table = 0;
} else if (bps1 < 1.16) {
else if (bps1 < 1.16)
coef_vlc_table = 1;
}
}
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]);
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;
}
int ff_wma_total_gain_to_bits(int total_gain)
{
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;
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)
@ -374,12 +368,10 @@ int ff_wma_end(AVCodecContext *avctx)
for (i = 0; i < s->nb_block_sizes; i++)
ff_mdct_end(&s->mdct_ctx[i]);
if (s->use_exp_vlc) {
if (s->use_exp_vlc)
ff_free_vlc(&s->exp_vlc);
}
if (s->use_noise_coding) {
if (s->use_noise_coding)
ff_free_vlc(&s->hgain_vlc);
}
for (i = 0; i < 2; i++) {
ff_free_vlc(&s->coef_vlc[i]);
av_free(s->run_table[i]);
@ -395,7 +387,7 @@ int ff_wma_end(AVCodecContext *avctx)
* @param gb GetBitContext
* @return the decoded coefficient
*/
unsigned int ff_wma_get_large_val(GetBitContext* gb)
unsigned int ff_wma_get_large_val(GetBitContext *gb)
{
/** consumes up to 34 bits */
int n_bits = 8;
@ -404,9 +396,8 @@ unsigned int ff_wma_get_large_val(GetBitContext* gb)
n_bits += 8;
if (get_bits1(gb)) {
n_bits += 8;
if (get_bits1(gb)) {
if (get_bits1(gb))
n_bits += 7;
}
}
}
return get_bits_long(gb, n_bits);
@ -428,24 +419,24 @@ unsigned int ff_wma_get_large_val(GetBitContext* gb)
* @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,
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 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)
{
int code, level, sign;
const uint32_t *ilvl = (const uint32_t*)level_table;
uint32_t *iptr = (uint32_t*)ptr;
const uint32_t *ilvl = (const uint32_t *) level_table;
uint32_t *iptr = (uint32_t *) ptr;
const unsigned int coef_mask = block_len - 1;
for (; offset < num_coefs; offset++) {
code = get_vlc2(gb, vlc->table, VLCBITS, VLCMAX);
if (code > 1) {
/** normal code */
offset += run_table[code];
sign = get_bits1(gb) - 1;
iptr[offset & coef_mask] = ilvl[code] ^ sign<<31;
offset += run_table[code];
sign = get_bits1(gb) - 1;
iptr[offset & coef_mask] = ilvl[code] ^ sign << 31;
} else if (code == 1) {
/** EOB */
break;
@ -454,7 +445,7 @@ int ff_wma_run_level_decode(AVCodecContext* avctx, GetBitContext* gb,
if (!version) {
level = get_bits(gb, coef_nb_bits);
/** NOTE: this is rather suboptimal. reading
block_len_bits would be better */
* block_len_bits would be better */
offset += get_bits(gb, frame_len_bits);
} else {
level = ff_wma_get_large_val(gb);
@ -462,8 +453,8 @@ int ff_wma_run_level_decode(AVCodecContext* avctx, GetBitContext* gb,
if (get_bits1(gb)) {
if (get_bits1(gb)) {
if (get_bits1(gb)) {
av_log(avctx,AV_LOG_ERROR,
"broken escape sequence\n");
av_log(avctx, AV_LOG_ERROR,
"broken escape sequence\n");
return -1;
} else
offset += get_bits(gb, frame_len_bits) + 4;
@ -471,8 +462,8 @@ int ff_wma_run_level_decode(AVCodecContext* avctx, GetBitContext* gb,
offset += get_bits(gb, 2) + 1;
}
}
sign = get_bits1(gb) - 1;
ptr[offset & coef_mask] = (level^sign) - sign;
sign = get_bits1(gb) - 1;
ptr[offset & coef_mask] = (level ^ sign) - sign;
}
}
/** NOTE: EOB can be omitted */

View File

@ -23,10 +23,11 @@
#define AVCODEC_WMA_H
#include "libavutil/float_dsp.h"
#include "get_bits.h"
#include "put_bits.h"
#include "fft.h"
#include "fmtconvert.h"
#include "get_bits.h"
#include "put_bits.h"
/* size of blocks */
#define BLOCK_MIN_BITS 7
@ -49,9 +50,9 @@
#define LSP_POW_BITS 7
//FIXME should be in wmadec
// FIXME should be in wmadec
#define VLCBITS 9
#define VLCMAX ((22+VLCBITS-1)/VLCBITS)
#define VLCMAX ((22 + VLCBITS - 1) / VLCBITS)
typedef float WMACoef; ///< type for decoded coefficients, int16_t would be enough for wma 1/2
@ -64,7 +65,7 @@ typedef struct CoefVLCTable {
} CoefVLCTable;
typedef struct WMACodecContext {
AVCodecContext* avctx;
AVCodecContext *avctx;
GetBitContext gb;
PutBitContext pb;
int version; ///< 1 = 0x160 (WMAV1), 2 = 0x161 (WMAV2)
@ -88,7 +89,7 @@ typedef struct WMACodecContext {
int high_band_values[MAX_CHANNELS][HIGH_BAND_MAX_SIZE];
/* there are two possible tables for spectral coefficients */
//FIXME the following 3 tables should be shared between decoders
// FIXME the following 3 tables should be shared between decoders
VLC coef_vlc[2];
uint16_t *run_table[2];
float *level_table[2];
@ -135,7 +136,7 @@ typedef struct WMACodecContext {
#ifdef TRACE
int frame_count;
#endif
#endif /* TRACE */
} WMACodecContext;
extern const uint16_t ff_wma_critical_freqs[25];
@ -145,15 +146,15 @@ extern const float ff_wma_lsp_codebook[NB_LSP_COEFS][16];
extern const uint32_t ff_aac_scalefactor_code[121];
extern const uint8_t ff_aac_scalefactor_bits[121];
int ff_wma_init(AVCodecContext * avctx, int flags2);
int ff_wma_init(AVCodecContext *avctx, int flags2);
int ff_wma_total_gain_to_bits(int total_gain);
int ff_wma_end(AVCodecContext *avctx);
unsigned int ff_wma_get_large_val(GetBitContext* gb);
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,
unsigned int ff_wma_get_large_val(GetBitContext *gb);
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);
#endif /* AVCODEC_WMA_H */

View File

@ -19,6 +19,7 @@
*/
#include "libavutil/attributes.h"
#include "wma_common.h"
/**
@ -31,31 +32,27 @@
av_cold int ff_wma_get_frame_len_bits(int sample_rate, int version,
unsigned int decode_flags)
{
int frame_len_bits;
if (sample_rate <= 16000) {
if (sample_rate <= 16000)
frame_len_bits = 9;
} else if (sample_rate <= 22050 ||
(sample_rate <= 32000 && version == 1)) {
else if (sample_rate <= 22050 || (sample_rate <= 32000 && version == 1))
frame_len_bits = 10;
} else if (sample_rate <= 48000 || version < 3) {
else if (sample_rate <= 48000 || version < 3)
frame_len_bits = 11;
} else if (sample_rate <= 96000) {
else if (sample_rate <= 96000)
frame_len_bits = 12;
} else {
else
frame_len_bits = 13;
}
if (version == 3) {
int tmp = decode_flags & 0x6;
if (tmp == 0x2) {
if (tmp == 0x2)
++frame_len_bits;
} else if (tmp == 0x4) {
else if (tmp == 0x4)
--frame_len_bits;
} else if (tmp == 0x6) {
else if (tmp == 0x6)
frame_len_bits -= 2;
}
}
return frame_len_bits;

File diff suppressed because it is too large Load Diff

View File

@ -34,6 +34,7 @@
*/
#include "libavutil/attributes.h"
#include "avcodec.h"
#include "internal.h"
#include "wma.h"
@ -42,20 +43,21 @@
#include <assert.h>
#define EXPVLCBITS 8
#define EXPMAX ((19+EXPVLCBITS-1)/EXPVLCBITS)
#define EXPMAX ((19 + EXPVLCBITS - 1) / EXPVLCBITS)
#define HGAINVLCBITS 9
#define HGAINMAX ((13+HGAINVLCBITS-1)/HGAINVLCBITS)
#define HGAINMAX ((13 + HGAINVLCBITS - 1) / HGAINVLCBITS)
static void wma_lsp_to_curve_init(WMACodecContext *s, int frame_len);
#ifdef TRACE
static void dump_floats(WMACodecContext *s, const char *name, int prec, const float *tab, int n)
static void dump_floats(WMACodecContext *s, const char *name,
int prec, const float *tab, int n)
{
int i;
tprintf(s->avctx, "%s[%d]:\n", name, n);
for(i=0;i<n;i++) {
for (i = 0; i < n; i++) {
if ((i & 7) == 0)
tprintf(s->avctx, "%4d: ", i);
tprintf(s->avctx, " %8.*f", prec, tab[i]);
@ -65,9 +67,9 @@ static void dump_floats(WMACodecContext *s, const char *name, int prec, const fl
if ((i & 7) != 0)
tprintf(s->avctx, "\n");
}
#endif
#endif /* TRACE */
static av_cold int wma_decode_init(AVCodecContext * avctx)
static av_cold int wma_decode_init(AVCodecContext *avctx)
{
WMACodecContext *s = avctx->priv_data;
int i, flags2;
@ -81,23 +83,22 @@ static av_cold int wma_decode_init(AVCodecContext * avctx)
s->avctx = avctx;
/* extract flag infos */
flags2 = 0;
flags2 = 0;
extradata = avctx->extradata;
if (avctx->codec->id == AV_CODEC_ID_WMAV1 && avctx->extradata_size >= 4) {
flags2 = AV_RL16(extradata+2);
} else if (avctx->codec->id == AV_CODEC_ID_WMAV2 && avctx->extradata_size >= 6) {
flags2 = AV_RL16(extradata+4);
}
if (avctx->codec->id == AV_CODEC_ID_WMAV1 && avctx->extradata_size >= 4)
flags2 = AV_RL16(extradata + 2);
else if (avctx->codec->id == AV_CODEC_ID_WMAV2 && avctx->extradata_size >= 6)
flags2 = AV_RL16(extradata + 4);
s->use_exp_vlc = flags2 & 0x0001;
s->use_bit_reservoir = flags2 & 0x0002;
s->use_exp_vlc = flags2 & 0x0001;
s->use_bit_reservoir = flags2 & 0x0002;
s->use_variable_block_len = flags2 & 0x0004;
if(ff_wma_init(avctx, flags2)<0)
if (ff_wma_init(avctx, flags2) < 0)
return -1;
/* init MDCT */
for(i = 0; i < s->nb_block_sizes; i++)
for (i = 0; i < s->nb_block_sizes; i++)
ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 1, 1.0 / 32768.0);
if (s->use_noise_coding) {
@ -106,13 +107,12 @@ static av_cold int wma_decode_init(AVCodecContext * avctx)
ff_wma_hgain_huffcodes, 2, 2, 0);
}
if (s->use_exp_vlc) {
init_vlc(&s->exp_vlc, EXPVLCBITS, sizeof(ff_aac_scalefactor_bits), //FIXME move out of context
if (s->use_exp_vlc)
init_vlc(&s->exp_vlc, EXPVLCBITS, sizeof(ff_aac_scalefactor_bits), // FIXME move out of context
ff_aac_scalefactor_bits, 1, 1,
ff_aac_scalefactor_code, 4, 4, 0);
} else {
else
wma_lsp_to_curve_init(s, s->frame_len);
}
avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
@ -135,12 +135,12 @@ static inline float pow_m1_4(WMACodecContext *s, float x)
float a, b;
u.f = x;
e = u.v >> 23;
m = (u.v >> (23 - LSP_POW_BITS)) & ((1 << LSP_POW_BITS) - 1);
e = u.v >> 23;
m = (u.v >> (23 - LSP_POW_BITS)) & ((1 << LSP_POW_BITS) - 1);
/* build interpolation scale: 1 <= t < 2. */
t.v = ((u.v << LSP_POW_BITS) & ((1 << 23) - 1)) | (127 << 23);
a = s->lsp_pow_m_table1[m];
b = s->lsp_pow_m_table2[m];
a = s->lsp_pow_m_table1[m];
b = s->lsp_pow_m_table2[m];
return s->lsp_pow_e_table[e] * (a + b * t.f);
}
@ -150,25 +150,25 @@ static av_cold void wma_lsp_to_curve_init(WMACodecContext *s, int frame_len)
int i, e, m;
wdel = M_PI / frame_len;
for(i=0;i<frame_len;i++)
for (i = 0; i < frame_len; i++)
s->lsp_cos_table[i] = 2.0f * cos(wdel * i);
/* tables for x^-0.25 computation */
for(i=0;i<256;i++) {
e = i - 126;
for (i = 0; i < 256; i++) {
e = i - 126;
s->lsp_pow_e_table[i] = pow(2.0, e * -0.25);
}
/* NOTE: these two tables are needed to avoid two operations in
pow_m1_4 */
* pow_m1_4 */
b = 1.0;
for(i=(1 << LSP_POW_BITS) - 1;i>=0;i--) {
m = (1 << LSP_POW_BITS) + i;
a = (float)m * (0.5 / (1 << LSP_POW_BITS));
a = pow(a, -0.25);
for (i = (1 << LSP_POW_BITS) - 1; i >= 0; i--) {
m = (1 << LSP_POW_BITS) + i;
a = (float) m * (0.5 / (1 << LSP_POW_BITS));
a = pow(a, -0.25);
s->lsp_pow_m_table1[i] = 2 * a - b;
s->lsp_pow_m_table2[i] = b - a;
b = a;
b = a;
}
}
@ -176,26 +176,25 @@ static av_cold void wma_lsp_to_curve_init(WMACodecContext *s, int frame_len)
* NOTE: We use the same code as Vorbis here
* @todo optimize it further with SSE/3Dnow
*/
static void wma_lsp_to_curve(WMACodecContext *s,
float *out, float *val_max_ptr,
static void wma_lsp_to_curve(WMACodecContext *s, float *out, float *val_max_ptr,
int n, float *lsp)
{
int i, j;
float p, q, w, v, val_max;
val_max = 0;
for(i=0;i<n;i++) {
for (i = 0; i < n; i++) {
p = 0.5f;
q = 0.5f;
w = s->lsp_cos_table[i];
for(j=1;j<NB_LSP_COEFS;j+=2){
for (j = 1; j < NB_LSP_COEFS; j += 2) {
q *= w - lsp[j - 1];
p *= w - lsp[j];
}
p *= p * (2.0f - w);
q *= q * (2.0f + w);
v = p + q;
v = pow_m1_4(s, v);
v = p + q;
v = pow_m1_4(s, v);
if (v > val_max)
val_max = v;
out[i] = v;
@ -211,7 +210,7 @@ static void decode_exp_lsp(WMACodecContext *s, int ch)
float lsp_coefs[NB_LSP_COEFS];
int val, i;
for(i = 0; i < NB_LSP_COEFS; i++) {
for (i = 0; i < NB_LSP_COEFS; i++) {
if (i == 0 || i >= 8)
val = get_bits(&s->gb, 3);
else
@ -315,41 +314,41 @@ static int decode_exp_vlc(WMACodecContext *s, int ch)
float v, max_scale;
uint32_t *q, *q_end, iv;
const float *ptab = pow_tab + 60;
const uint32_t *iptab = (const uint32_t*)ptab;
const uint32_t *iptab = (const uint32_t *) ptab;
ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
q = (uint32_t *)s->exponents[ch];
q_end = q + s->block_len;
ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
q = (uint32_t *) s->exponents[ch];
q_end = q + s->block_len;
max_scale = 0;
if (s->version == 1) {
last_exp = get_bits(&s->gb, 5) + 10;
v = ptab[last_exp];
iv = iptab[last_exp];
last_exp = get_bits(&s->gb, 5) + 10;
v = ptab[last_exp];
iv = iptab[last_exp];
max_scale = v;
n = *ptr++;
n = *ptr++;
switch (n & 3) do {
case 0: *q++ = iv;
case 3: *q++ = iv;
case 2: *q++ = iv;
case 1: *q++ = iv;
} while ((n -= 4) > 0);
}else
} else
last_exp = 36;
while (q < q_end) {
code = get_vlc2(&s->gb, s->exp_vlc.table, EXPVLCBITS, EXPMAX);
if (code < 0){
if (code < 0) {
av_log(s->avctx, AV_LOG_ERROR, "Exponent vlc invalid\n");
return -1;
}
/* NOTE: this offset is the same as MPEG4 AAC ! */
last_exp += code - 60;
if ((unsigned)last_exp + 60 >= FF_ARRAY_ELEMS(pow_tab)) {
if ((unsigned) last_exp + 60 >= FF_ARRAY_ELEMS(pow_tab)) {
av_log(s->avctx, AV_LOG_ERROR, "Exponent out of range: %d\n",
last_exp);
return -1;
}
v = ptab[last_exp];
v = ptab[last_exp];
iv = iptab[last_exp];
if (v > max_scale)
max_scale = v;
@ -365,7 +364,6 @@ static int decode_exp_vlc(WMACodecContext *s, int ch)
return 0;
}
/**
* Apply MDCT window and add into output.
*
@ -380,46 +378,44 @@ static void wma_window(WMACodecContext *s, float *out)
/* left part */
if (s->block_len_bits <= s->prev_block_len_bits) {
block_len = s->block_len;
bsize = s->frame_len_bits - s->block_len_bits;
bsize = s->frame_len_bits - s->block_len_bits;
s->fdsp.vector_fmul_add(out, in, s->windows[bsize],
out, block_len);
} else {
block_len = 1 << s->prev_block_len_bits;
n = (s->block_len - block_len) / 2;
bsize = s->frame_len_bits - s->prev_block_len_bits;
n = (s->block_len - block_len) / 2;
bsize = s->frame_len_bits - s->prev_block_len_bits;
s->fdsp.vector_fmul_add(out+n, in+n, s->windows[bsize],
out+n, block_len);
s->fdsp.vector_fmul_add(out + n, in + n, s->windows[bsize],
out + n, block_len);
memcpy(out+n+block_len, in+n+block_len, n*sizeof(float));
memcpy(out + n + block_len, in + n + block_len, n * sizeof(float));
}
out += s->block_len;
in += s->block_len;
in += s->block_len;
/* right part */
if (s->block_len_bits <= s->next_block_len_bits) {
block_len = s->block_len;
bsize = s->frame_len_bits - s->block_len_bits;
bsize = s->frame_len_bits - s->block_len_bits;
s->fdsp.vector_fmul_reverse(out, in, s->windows[bsize], block_len);
} else {
block_len = 1 << s->next_block_len_bits;
n = (s->block_len - block_len) / 2;
bsize = s->frame_len_bits - s->next_block_len_bits;
n = (s->block_len - block_len) / 2;
bsize = s->frame_len_bits - s->next_block_len_bits;
memcpy(out, in, n*sizeof(float));
memcpy(out, in, n * sizeof(float));
s->fdsp.vector_fmul_reverse(out+n, in+n, s->windows[bsize], block_len);
s->fdsp.vector_fmul_reverse(out + n, in + n, s->windows[bsize],
block_len);
memset(out+n+block_len, 0, n*sizeof(float));
memset(out + n + block_len, 0, n * sizeof(float));
}
}
/**
* @return 0 if OK. 1 if last block of frame. return -1 if
* unrecorrable error.
@ -433,8 +429,9 @@ static int wma_decode_block(WMACodecContext *s)
FFTContext *mdct;
#ifdef TRACE
tprintf(s->avctx, "***decode_block: %d:%d\n", s->frame_count - 1, s->block_num);
#endif
tprintf(s->avctx, "***decode_block: %d:%d\n",
s->frame_count - 1, s->block_num);
#endif /* TRACE */
/* compute current block length */
if (s->use_variable_block_len) {
@ -442,26 +439,32 @@ static int wma_decode_block(WMACodecContext *s)
if (s->reset_block_lengths) {
s->reset_block_lengths = 0;
v = get_bits(&s->gb, n);
if (v >= s->nb_block_sizes){
av_log(s->avctx, AV_LOG_ERROR, "prev_block_len_bits %d out of range\n", s->frame_len_bits - v);
v = get_bits(&s->gb, n);
if (v >= s->nb_block_sizes) {
av_log(s->avctx, AV_LOG_ERROR,
"prev_block_len_bits %d out of range\n",
s->frame_len_bits - v);
return -1;
}
s->prev_block_len_bits = s->frame_len_bits - v;
v = get_bits(&s->gb, n);
if (v >= s->nb_block_sizes){
av_log(s->avctx, AV_LOG_ERROR, "block_len_bits %d out of range\n", s->frame_len_bits - v);
v = get_bits(&s->gb, n);
if (v >= s->nb_block_sizes) {
av_log(s->avctx, AV_LOG_ERROR,
"block_len_bits %d out of range\n",
s->frame_len_bits - v);
return -1;
}
s->block_len_bits = s->frame_len_bits - v;
} else {
/* update block lengths */
s->prev_block_len_bits = s->block_len_bits;
s->block_len_bits = s->next_block_len_bits;
s->block_len_bits = s->next_block_len_bits;
}
v = get_bits(&s->gb, n);
if (v >= s->nb_block_sizes){
av_log(s->avctx, AV_LOG_ERROR, "next_block_len_bits %d out of range\n", s->frame_len_bits - v);
if (v >= s->nb_block_sizes) {
av_log(s->avctx, AV_LOG_ERROR,
"next_block_len_bits %d out of range\n",
s->frame_len_bits - v);
return -1;
}
s->next_block_len_bits = s->frame_len_bits - v;
@ -469,24 +472,23 @@ static int wma_decode_block(WMACodecContext *s)
/* fixed block len */
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;
s->block_len_bits = s->frame_len_bits;
}
/* now check if the block length is coherent with the frame length */
s->block_len = 1 << s->block_len_bits;
if ((s->block_pos + s->block_len) > s->frame_len){
if ((s->block_pos + s->block_len) > s->frame_len) {
av_log(s->avctx, AV_LOG_ERROR, "frame_len overflow\n");
return -1;
}
if (s->avctx->channels == 2) {
if (s->avctx->channels == 2)
s->ms_stereo = get_bits1(&s->gb);
}
v = 0;
for(ch = 0; ch < s->avctx->channels; ch++) {
a = get_bits1(&s->gb);
for (ch = 0; ch < s->avctx->channels; ch++) {
a = get_bits1(&s->gb);
s->channel_coded[ch] = a;
v |= a;
v |= a;
}
bsize = s->frame_len_bits - s->block_len_bits;
@ -497,31 +499,30 @@ static int wma_decode_block(WMACodecContext *s)
goto next;
/* read total gain and extract corresponding number of bits for
coef escape coding */
* coef escape coding */
total_gain = 1;
for(;;) {
a = get_bits(&s->gb, 7);
for (;;) {
a = get_bits(&s->gb, 7);
total_gain += a;
if (a != 127)
break;
}
coef_nb_bits= ff_wma_total_gain_to_bits(total_gain);
coef_nb_bits = ff_wma_total_gain_to_bits(total_gain);
/* compute number of coefficients */
n = s->coefs_end[bsize] - s->coefs_start;
for(ch = 0; ch < s->avctx->channels; ch++)
for (ch = 0; ch < s->avctx->channels; ch++)
nb_coefs[ch] = n;
/* complex coding */
if (s->use_noise_coding) {
for(ch = 0; ch < s->avctx->channels; ch++) {
for (ch = 0; ch < s->avctx->channels; ch++) {
if (s->channel_coded[ch]) {
int i, n, a;
n = s->exponent_high_sizes[bsize];
for(i=0;i<n;i++) {
a = get_bits1(&s->gb);
for (i = 0; i < n; i++) {
a = get_bits1(&s->gb);
s->high_band_coded[ch][i] = a;
/* if noise coding, the coefficients are not transmitted */
if (a)
@ -529,20 +530,22 @@ static int wma_decode_block(WMACodecContext *s)
}
}
}
for(ch = 0; ch < s->avctx->channels; ch++) {
for (ch = 0; ch < s->avctx->channels; ch++) {
if (s->channel_coded[ch]) {
int i, n, val, code;
n = s->exponent_high_sizes[bsize];
val = (int)0x80000000;
for(i=0;i<n;i++) {
n = s->exponent_high_sizes[bsize];
val = (int) 0x80000000;
for (i = 0; i < n; i++) {
if (s->high_band_coded[ch][i]) {
if (val == (int)0x80000000) {
if (val == (int) 0x80000000) {
val = get_bits(&s->gb, 7) - 19;
} else {
code = get_vlc2(&s->gb, s->hgain_vlc.table, HGAINVLCBITS, HGAINMAX);
if (code < 0){
av_log(s->avctx, AV_LOG_ERROR, "hgain vlc invalid\n");
code = get_vlc2(&s->gb, s->hgain_vlc.table,
HGAINVLCBITS, HGAINMAX);
if (code < 0) {
av_log(s->avctx, AV_LOG_ERROR,
"hgain vlc invalid\n");
return -1;
}
val += code - 18;
@ -555,9 +558,8 @@ static int wma_decode_block(WMACodecContext *s)
}
/* exponents can be reused in short blocks. */
if ((s->block_len_bits == s->frame_len_bits) ||
get_bits1(&s->gb)) {
for(ch = 0; ch < s->avctx->channels; ch++) {
if ((s->block_len_bits == s->frame_len_bits) || get_bits1(&s->gb)) {
for (ch = 0; ch < s->avctx->channels; ch++) {
if (s->channel_coded[ch]) {
if (s->use_exp_vlc) {
if (decode_exp_vlc(s, ch) < 0)
@ -574,29 +576,27 @@ static int wma_decode_block(WMACodecContext *s)
for (ch = 0; ch < s->avctx->channels; ch++) {
if (s->channel_coded[ch]) {
int tindex;
WMACoef* ptr = &s->coefs1[ch][0];
WMACoef *ptr = &s->coefs1[ch][0];
/* special VLC tables are used for ms stereo because
there is potentially less energy there */
* there is potentially less energy there */
tindex = (ch == 1 && s->ms_stereo);
memset(ptr, 0, s->block_len * sizeof(WMACoef));
ff_wma_run_level_decode(s->avctx, &s->gb, &s->coef_vlc[tindex],
s->level_table[tindex], s->run_table[tindex],
0, ptr, 0, nb_coefs[ch],
s->block_len, s->frame_len_bits, coef_nb_bits);
s->level_table[tindex], s->run_table[tindex],
0, ptr, 0, nb_coefs[ch],
s->block_len, s->frame_len_bits, coef_nb_bits);
}
if (s->version == 1 && s->avctx->channels >= 2) {
if (s->version == 1 && s->avctx->channels >= 2)
align_get_bits(&s->gb);
}
}
/* normalize */
{
int n4 = s->block_len / 2;
mdct_norm = 1.0 / (float)n4;
if (s->version == 1) {
mdct_norm = 1.0 / (float) n4;
if (s->version == 1)
mdct_norm *= sqrt(n4);
}
}
/* finally compute the MDCT coefficients */
@ -607,97 +607,94 @@ static int wma_decode_block(WMACodecContext *s)
int i, j, n, n1, last_high_band, esize;
float exp_power[HIGH_BAND_MAX_SIZE];
coefs1 = s->coefs1[ch];
coefs1 = s->coefs1[ch];
exponents = s->exponents[ch];
esize = s->exponents_bsize[ch];
mult = pow(10, total_gain * 0.05) / s->max_exponent[ch];
mult *= mdct_norm;
coefs = s->coefs[ch];
esize = s->exponents_bsize[ch];
mult = pow(10, total_gain * 0.05) / s->max_exponent[ch];
mult *= mdct_norm;
coefs = s->coefs[ch];
if (s->use_noise_coding) {
mult1 = mult;
/* very low freqs : noise */
for(i = 0;i < s->coefs_start; i++) {
for (i = 0; i < s->coefs_start; i++) {
*coefs++ = s->noise_table[s->noise_index] *
exponents[i<<bsize>>esize] * mult1;
s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
exponents[i << bsize >> esize] * mult1;
s->noise_index = (s->noise_index + 1) &
(NOISE_TAB_SIZE - 1);
}
n1 = s->exponent_high_sizes[bsize];
/* compute power of high bands */
exponents = s->exponents[ch] +
(s->high_band_start[bsize]<<bsize>>esize);
(s->high_band_start[bsize] << bsize >> esize);
last_high_band = 0; /* avoid warning */
for(j=0;j<n1;j++) {
for (j = 0; j < n1; j++) {
n = s->exponent_high_bands[s->frame_len_bits -
s->block_len_bits][j];
s->block_len_bits][j];
if (s->high_band_coded[ch][j]) {
float e2, v;
e2 = 0;
for(i = 0;i < n; i++) {
v = exponents[i<<bsize>>esize];
for (i = 0; i < n; i++) {
v = exponents[i << bsize >> esize];
e2 += v * v;
}
exp_power[j] = e2 / n;
exp_power[j] = e2 / n;
last_high_band = j;
tprintf(s->avctx, "%d: power=%f (%d)\n", j, exp_power[j], n);
}
exponents += n<<bsize>>esize;
exponents += n << bsize >> esize;
}
/* main freqs and high freqs */
exponents = s->exponents[ch] + (s->coefs_start<<bsize>>esize);
for(j=-1;j<n1;j++) {
if (j < 0) {
n = s->high_band_start[bsize] -
s->coefs_start;
} else {
exponents = s->exponents[ch] + (s->coefs_start << bsize >> esize);
for (j = -1; j < n1; j++) {
if (j < 0)
n = s->high_band_start[bsize] - s->coefs_start;
else
n = s->exponent_high_bands[s->frame_len_bits -
s->block_len_bits][j];
}
s->block_len_bits][j];
if (j >= 0 && s->high_band_coded[ch][j]) {
/* use noise with specified power */
mult1 = sqrt(exp_power[j] / exp_power[last_high_band]);
/* XXX: use a table */
mult1 = mult1 * pow(10, s->high_band_values[ch][j] * 0.05);
mult1 = mult1 / (s->max_exponent[ch] * s->noise_mult);
mult1 = mult1 * pow(10, s->high_band_values[ch][j] * 0.05);
mult1 = mult1 / (s->max_exponent[ch] * s->noise_mult);
mult1 *= mdct_norm;
for(i = 0;i < n; i++) {
noise = s->noise_table[s->noise_index];
for (i = 0; i < n; i++) {
noise = s->noise_table[s->noise_index];
s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
*coefs++ = noise *
exponents[i<<bsize>>esize] * mult1;
*coefs++ = noise * exponents[i << bsize >> esize] * mult1;
}
exponents += n<<bsize>>esize;
exponents += n << bsize >> esize;
} else {
/* coded values + small noise */
for(i = 0;i < n; i++) {
noise = s->noise_table[s->noise_index];
for (i = 0; i < n; i++) {
noise = s->noise_table[s->noise_index];
s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
*coefs++ = ((*coefs1++) + noise) *
exponents[i<<bsize>>esize] * mult;
*coefs++ = ((*coefs1++) + noise) *
exponents[i << bsize >> esize] * mult;
}
exponents += n<<bsize>>esize;
exponents += n << bsize >> esize;
}
}
/* very high freqs : noise */
n = s->block_len - s->coefs_end[bsize];
mult1 = mult * exponents[((-1<<bsize))>>esize];
for(i = 0; i < n; i++) {
*coefs++ = s->noise_table[s->noise_index] * mult1;
n = s->block_len - s->coefs_end[bsize];
mult1 = mult * exponents[((-1 << bsize)) >> esize];
for (i = 0; i < n; i++) {
*coefs++ = s->noise_table[s->noise_index] * mult1;
s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
}
} else {
/* XXX: optimize more */
for(i = 0;i < s->coefs_start; i++)
for (i = 0; i < s->coefs_start; i++)
*coefs++ = 0.0;
n = nb_coefs[ch];
for(i = 0;i < n; i++) {
*coefs++ = coefs1[i] * exponents[i<<bsize>>esize] * mult;
}
for (i = 0; i < n; i++)
*coefs++ = coefs1[i] * exponents[i << bsize >> esize] * mult;
n = s->block_len - s->coefs_end[bsize];
for(i = 0;i < n; i++)
for (i = 0; i < n; i++)
*coefs++ = 0.0;
}
}
@ -710,12 +707,12 @@ static int wma_decode_block(WMACodecContext *s)
dump_floats(s, "coefs", 1, s->coefs[ch], s->block_len);
}
}
#endif
#endif /* TRACE */
if (s->ms_stereo && s->channel_coded[1]) {
/* nominal case for ms stereo: we do it before mdct */
/* no need to optimize this case because it should almost
never happen */
* never happen */
if (!s->channel_coded[0]) {
tprintf(s->avctx, "rare ms-stereo case happened\n");
memset(s->coefs[0], 0, sizeof(float) * s->block_len);
@ -732,9 +729,9 @@ next:
int n4, index;
n4 = s->block_len / 2;
if(s->channel_coded[ch]){
if (s->channel_coded[ch])
mdct->imdct_calc(mdct, s->output, s->coefs[ch]);
}else if(!(s->ms_stereo && ch==1))
else if (!(s->ms_stereo && ch == 1))
memset(s->output, 0, sizeof(s->output));
/* multiply by the window and add in the frame */
@ -758,13 +755,14 @@ static int wma_decode_frame(WMACodecContext *s, float **samples,
int ret, ch;
#ifdef TRACE
tprintf(s->avctx, "***decode_frame: %d size=%d\n", s->frame_count++, s->frame_len);
#endif
tprintf(s->avctx, "***decode_frame: %d size=%d\n",
s->frame_count++, s->frame_len);
#endif /* TRACE */
/* read each block */
s->block_num = 0;
s->block_pos = 0;
for(;;) {
for (;;) {
ret = wma_decode_block(s);
if (ret < 0)
return -1;
@ -781,8 +779,9 @@ static int wma_decode_frame(WMACodecContext *s, float **samples,
s->frame_len * sizeof(*s->frame_out[ch]));
#ifdef TRACE
dump_floats(s, "samples", 6, samples[ch] + samples_offset, s->frame_len);
#endif
dump_floats(s, "samples", 6, samples[ch] + samples_offset,
s->frame_len);
#endif /* TRACE */
}
return 0;
@ -791,9 +790,9 @@ static int wma_decode_frame(WMACodecContext *s, float **samples,
static int wma_decode_superframe(AVCodecContext *avctx, void *data,
int *got_frame_ptr, AVPacket *avpkt)
{
AVFrame *frame = data;
AVFrame *frame = data;
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size;
int buf_size = avpkt->size;
WMACodecContext *s = avctx->priv_data;
int nb_frames, bit_offset, i, pos, len, ret;
uint8_t *q;
@ -802,7 +801,7 @@ static int wma_decode_superframe(AVCodecContext *avctx, void *data,
tprintf(avctx, "***decode_superframe:\n");
if(buf_size==0){
if (buf_size == 0) {
s->last_superframe_len = 0;
return 0;
}
@ -814,15 +813,14 @@ static int wma_decode_superframe(AVCodecContext *avctx, void *data,
}
buf_size = avctx->block_align;
init_get_bits(&s->gb, buf, buf_size*8);
init_get_bits(&s->gb, buf, buf_size * 8);
if (s->use_bit_reservoir) {
/* read super frame header */
skip_bits(&s->gb, 4); /* super frame index */
nb_frames = get_bits(&s->gb, 4) - (s->last_superframe_len <= 0);
} else {
} else
nb_frames = 1;
}
/* get output buffer */
frame->nb_samples = nb_frames * s->frame_len;
@ -830,7 +828,7 @@ static int wma_decode_superframe(AVCodecContext *avctx, void *data,
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
return ret;
}
samples = (float **)frame->extended_data;
samples = (float **) frame->extended_data;
samples_offset = 0;
if (s->use_bit_reservoir) {
@ -847,24 +845,24 @@ static int wma_decode_superframe(AVCodecContext *avctx, void *data,
if ((s->last_superframe_len + ((bit_offset + 7) >> 3)) >
MAX_CODED_SUPERFRAME_SIZE)
goto fail;
q = s->last_superframe + s->last_superframe_len;
q = s->last_superframe + s->last_superframe_len;
len = bit_offset;
while (len > 7) {
*q++ = (get_bits)(&s->gb, 8);
*q++ = (get_bits) (&s->gb, 8);
len -= 8;
}
if (len > 0) {
*q++ = (get_bits)(&s->gb, len) << (8 - len);
}
if (len > 0)
*q++ = (get_bits) (&s->gb, len) << (8 - len);
memset(q, 0, FF_INPUT_BUFFER_PADDING_SIZE);
/* XXX: bit_offset bits into last frame */
init_get_bits(&s->gb, s->last_superframe, s->last_superframe_len * 8 + bit_offset);
init_get_bits(&s->gb, s->last_superframe,
s->last_superframe_len * 8 + bit_offset);
/* skip unused bits */
if (s->last_bitoffset > 0)
skip_bits(&s->gb, s->last_bitoffset);
/* this frame is stored in the last superframe and in the
current one */
* current one */
if (wma_decode_frame(s, samples, samples_offset) < 0)
goto fail;
samples_offset += s->frame_len;
@ -875,23 +873,24 @@ static int wma_decode_superframe(AVCodecContext *avctx, void *data,
pos = bit_offset + 4 + 4 + s->byte_offset_bits + 3;
if (pos >= MAX_CODED_SUPERFRAME_SIZE * 8 || pos > buf_size * 8)
return AVERROR_INVALIDDATA;
init_get_bits(&s->gb, buf + (pos >> 3), (buf_size - (pos >> 3))*8);
init_get_bits(&s->gb, buf + (pos >> 3), (buf_size - (pos >> 3)) * 8);
len = pos & 7;
if (len > 0)
skip_bits(&s->gb, len);
s->reset_block_lengths = 1;
for(i=0;i<nb_frames;i++) {
for (i = 0; i < nb_frames; i++) {
if (wma_decode_frame(s, samples, samples_offset) < 0)
goto fail;
samples_offset += s->frame_len;
}
/* we copy the end of the frame in the last frame buffer */
pos = get_bits_count(&s->gb) + ((bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7);
pos = get_bits_count(&s->gb) +
((bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7);
s->last_bitoffset = pos & 7;
pos >>= 3;
len = buf_size - pos;
pos >>= 3;
len = buf_size - pos;
if (len > MAX_CODED_SUPERFRAME_SIZE || len < 0) {
av_log(s->avctx, AV_LOG_ERROR, "len %d invalid\n", len);
goto fail;
@ -907,12 +906,13 @@ static int wma_decode_superframe(AVCodecContext *avctx, void *data,
av_dlog(s->avctx, "%d %d %d %d outbytes:%td eaten:%d\n",
s->frame_len_bits, s->block_len_bits, s->frame_len, s->block_len,
(int8_t *)samples - (int8_t *)data, avctx->block_align);
(int8_t *) samples - (int8_t *) data, avctx->block_align);
*got_frame_ptr = 1;
return avctx->block_align;
fail:
fail:
/* when error, we reset the bit reservoir */
s->last_superframe_len = 0;
return -1;
@ -922,8 +922,8 @@ static av_cold void flush(AVCodecContext *avctx)
{
WMACodecContext *s = avctx->priv_data;
s->last_bitoffset=
s->last_superframe_len= 0;
s->last_bitoffset =
s->last_superframe_len = 0;
}
AVCodec ff_wmav1_decoder = {

View File

@ -20,6 +20,7 @@
*/
#include "libavutil/attributes.h"
#include "avcodec.h"
#include "internal.h"
#include "wma.h"
@ -36,8 +37,9 @@ static av_cold int encode_init(AVCodecContext *avctx)
s->avctx = avctx;
if(avctx->channels > MAX_CHANNELS) {
av_log(avctx, AV_LOG_ERROR, "too many channels: got %i, need %i or fewer",
if (avctx->channels > MAX_CHANNELS) {
av_log(avctx, AV_LOG_ERROR,
"too many channels: got %i, need %i or fewer",
avctx->channels, MAX_CHANNELS);
return AVERROR(EINVAL);
}
@ -48,8 +50,9 @@ static av_cold int encode_init(AVCodecContext *avctx)
return AVERROR(EINVAL);
}
if(avctx->bit_rate < 24*1000) {
av_log(avctx, AV_LOG_ERROR, "bitrate too low: got %i, need 24000 or higher\n",
if (avctx->bit_rate < 24 * 1000) {
av_log(avctx, AV_LOG_ERROR,
"bitrate too low: got %i, need 24000 or higher\n",
avctx->bit_rate);
return AVERROR(EINVAL);
}
@ -58,20 +61,21 @@ static av_cold int encode_init(AVCodecContext *avctx)
flags1 = 0;
flags2 = 1;
if (avctx->codec->id == AV_CODEC_ID_WMAV1) {
extradata= av_malloc(4);
avctx->extradata_size= 4;
extradata = av_malloc(4);
avctx->extradata_size = 4;
AV_WL16(extradata, flags1);
AV_WL16(extradata+2, flags2);
AV_WL16(extradata + 2, flags2);
} else if (avctx->codec->id == AV_CODEC_ID_WMAV2) {
extradata= av_mallocz(10);
avctx->extradata_size= 10;
extradata = av_mallocz(10);
avctx->extradata_size = 10;
AV_WL32(extradata, flags1);
AV_WL16(extradata+4, flags2);
}else
AV_WL16(extradata + 4, flags2);
} else {
assert(0);
avctx->extradata= extradata;
s->use_exp_vlc = flags2 & 0x0001;
s->use_bit_reservoir = flags2 & 0x0002;
}
avctx->extradata = extradata;
s->use_exp_vlc = flags2 & 0x0001;
s->use_bit_reservoir = flags2 & 0x0002;
s->use_variable_block_len = flags2 & 0x0004;
if (avctx->channels == 2)
s->ms_stereo = 1;
@ -79,57 +83,59 @@ static av_cold int encode_init(AVCodecContext *avctx)
ff_wma_init(avctx, flags2);
/* init MDCT */
for(i = 0; i < s->nb_block_sizes; i++)
for (i = 0; i < s->nb_block_sizes; i++)
ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 0, 1.0);
block_align = avctx->bit_rate * (int64_t)s->frame_len /
block_align = avctx->bit_rate * (int64_t) s->frame_len /
(avctx->sample_rate * 8);
block_align = FFMIN(block_align, MAX_CODED_SUPERFRAME_SIZE);
avctx->block_align = block_align;
avctx->bit_rate = avctx->block_align * 8LL * avctx->sample_rate /
s->frame_len;
avctx->frame_size = avctx->delay = s->frame_len;
avctx->frame_size =
avctx->delay = s->frame_len;
return 0;
}
static void apply_window_and_mdct(AVCodecContext * avctx, const AVFrame *frame)
static void apply_window_and_mdct(AVCodecContext *avctx, const AVFrame *frame)
{
WMACodecContext *s = avctx->priv_data;
float **audio = (float **)frame->extended_data;
float **audio = (float **) frame->extended_data;
int len = frame->nb_samples;
int window_index= s->frame_len_bits - s->block_len_bits;
FFTContext *mdct = &s->mdct_ctx[window_index];
int window_index = s->frame_len_bits - s->block_len_bits;
FFTContext *mdct = &s->mdct_ctx[window_index];
int ch;
const float * win = s->windows[window_index];
int window_len = 1 << s->block_len_bits;
float n = 2.0 * 32768.0 / window_len;
const float *win = s->windows[window_index];
int window_len = 1 << s->block_len_bits;
float n = 2.0 * 32768.0 / window_len;
for (ch = 0; ch < avctx->channels; ch++) {
memcpy(s->output, s->frame_out[ch], window_len * sizeof(*s->output));
s->fdsp.vector_fmul_scalar(s->frame_out[ch], audio[ch], n, len);
s->fdsp.vector_fmul_reverse(&s->output[window_len], s->frame_out[ch], win, len);
s->fdsp.vector_fmul_reverse(&s->output[window_len], s->frame_out[ch],
win, len);
s->fdsp.vector_fmul(s->frame_out[ch], s->frame_out[ch], win, len);
mdct->mdct_calc(mdct, s->coefs[ch], s->output);
}
}
//FIXME use for decoding too
static void init_exp(WMACodecContext *s, int ch, const int *exp_param){
// FIXME use for decoding too
static void init_exp(WMACodecContext *s, int ch, const int *exp_param)
{
int n;
const uint16_t *ptr;
float v, *q, max_scale, *q_end;
ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
q = s->exponents[ch];
q_end = q + s->block_len;
ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
q = s->exponents[ch];
q_end = q + s->block_len;
max_scale = 0;
while (q < q_end) {
/* XXX: use a table */
v = pow(10, *exp_param++ * (1.0 / 16.0));
max_scale= FFMAX(max_scale, v);
n = *ptr++;
v = pow(10, *exp_param++ *(1.0 / 16.0));
max_scale = FFMAX(max_scale, v);
n = *ptr++;
do {
*q++ = v;
} while (--n);
@ -137,73 +143,81 @@ static void init_exp(WMACodecContext *s, int ch, const int *exp_param){
s->max_exponent[ch] = max_scale;
}
static void encode_exp_vlc(WMACodecContext *s, int ch, const int *exp_param){
static void encode_exp_vlc(WMACodecContext *s, int ch, const int *exp_param)
{
int last_exp;
const uint16_t *ptr;
float *q, *q_end;
ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
q = s->exponents[ch];
ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
q = s->exponents[ch];
q_end = q + s->block_len;
if (s->version == 1) {
last_exp= *exp_param++;
assert(last_exp-10 >= 0 && last_exp-10 < 32);
last_exp = *exp_param++;
assert(last_exp - 10 >= 0 && last_exp - 10 < 32);
put_bits(&s->pb, 5, last_exp - 10);
q+= *ptr++;
}else
q += *ptr++;
} else
last_exp = 36;
while (q < q_end) {
int exp = *exp_param++;
int exp = *exp_param++;
int code = exp - last_exp + 60;
assert(code >= 0 && code < 120);
put_bits(&s->pb, ff_aac_scalefactor_bits[code], ff_aac_scalefactor_code[code]);
put_bits(&s->pb, ff_aac_scalefactor_bits[code],
ff_aac_scalefactor_code[code]);
/* XXX: use a table */
q+= *ptr++;
last_exp= exp;
q += *ptr++;
last_exp = exp;
}
}
static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE], int total_gain){
static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
int total_gain)
{
int v, bsize, ch, coef_nb_bits, parse_exponents;
float mdct_norm;
int nb_coefs[MAX_CHANNELS];
static const int fixed_exp[25]={20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20};
static const int fixed_exp[25] = {
20, 20, 20, 20, 20,
20, 20, 20, 20, 20,
20, 20, 20, 20, 20,
20, 20, 20, 20, 20,
20, 20, 20, 20, 20
};
//FIXME remove duplication relative to decoder
// FIXME remove duplication relative to decoder
if (s->use_variable_block_len) {
assert(0); //FIXME not implemented
}else{
assert(0); // FIXME not implemented
} else {
/* fixed block len */
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;
s->block_len_bits = s->frame_len_bits;
}
s->block_len = 1 << s->block_len_bits;
// assert((s->block_pos + s->block_len) <= s->frame_len);
bsize = s->frame_len_bits - s->block_len_bits;
//FIXME factor
// FIXME factor
v = s->coefs_end[bsize] - s->coefs_start;
for (ch = 0; ch < s->avctx->channels; ch++)
nb_coefs[ch] = v;
{
int n4 = s->block_len / 2;
mdct_norm = 1.0 / (float)n4;
if (s->version == 1) {
mdct_norm = 1.0 / (float) n4;
if (s->version == 1)
mdct_norm *= sqrt(n4);
}
}
if (s->avctx->channels == 2) {
if (s->avctx->channels == 2)
put_bits(&s->pb, 1, !!s->ms_stereo);
}
for (ch = 0; ch < s->avctx->channels; ch++) {
s->channel_coded[ch] = 1; //FIXME only set channel_coded when needed, instead of always
if (s->channel_coded[ch]) {
// FIXME only set channel_coded when needed, instead of always
s->channel_coded[ch] = 1;
if (s->channel_coded[ch])
init_exp(s, ch, fixed_exp);
}
}
for (ch = 0; ch < s->avctx->channels; ch++) {
@ -212,19 +226,19 @@ static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
float *coefs, *exponents, mult;
int i, n;
coefs1 = s->coefs1[ch];
coefs1 = s->coefs1[ch];
exponents = s->exponents[ch];
mult = pow(10, total_gain * 0.05) / s->max_exponent[ch];
mult *= mdct_norm;
coefs = src_coefs[ch];
mult = pow(10, total_gain * 0.05) / s->max_exponent[ch];
mult *= mdct_norm;
coefs = src_coefs[ch];
if (s->use_noise_coding && 0) {
assert(0); //FIXME not implemented
assert(0); // FIXME not implemented
} else {
coefs += s->coefs_start;
n = nb_coefs[ch];
for(i = 0;i < n; i++){
double t= *coefs++ / (exponents[i] * mult);
if(t<-32768 || t>32767)
n = nb_coefs[ch];
for (i = 0; i < n; i++) {
double t = *coefs++ / (exponents[i] * mult);
if (t < -32768 || t > 32767)
return -1;
coefs1[i] = lrint(t);
@ -243,19 +257,19 @@ static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
if (!v)
return 1;
for(v= total_gain-1; v>=127; v-= 127)
for (v = total_gain - 1; v >= 127; v -= 127)
put_bits(&s->pb, 7, 127);
put_bits(&s->pb, 7, v);
coef_nb_bits= ff_wma_total_gain_to_bits(total_gain);
coef_nb_bits = ff_wma_total_gain_to_bits(total_gain);
if (s->use_noise_coding) {
for (ch = 0; ch < s->avctx->channels; ch++) {
if (s->channel_coded[ch]) {
int i, n;
n = s->exponent_high_sizes[bsize];
for(i=0;i<n;i++) {
put_bits(&s->pb, 1, s->high_band_coded[ch][i]= 0);
for (i = 0; i < n; i++) {
put_bits(&s->pb, 1, s->high_band_coded[ch][i] = 0);
if (0)
nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
}
@ -264,9 +278,8 @@ static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
}
parse_exponents = 1;
if (s->block_len_bits != s->frame_len_bits) {
if (s->block_len_bits != s->frame_len_bits)
put_bits(&s->pb, 1, parse_exponents);
}
if (parse_exponents) {
for (ch = 0; ch < s->avctx->channels; ch++) {
@ -274,69 +287,68 @@ static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
if (s->use_exp_vlc) {
encode_exp_vlc(s, ch, fixed_exp);
} else {
assert(0); //FIXME not implemented
assert(0); // FIXME not implemented
// encode_exp_lsp(s, ch);
}
}
}
} else {
assert(0); //FIXME not implemented
}
} else
assert(0); // FIXME not implemented
for (ch = 0; ch < s->avctx->channels; ch++) {
if (s->channel_coded[ch]) {
int run, tindex;
WMACoef *ptr, *eptr;
tindex = (ch == 1 && s->ms_stereo);
ptr = &s->coefs1[ch][0];
eptr = ptr + nb_coefs[ch];
ptr = &s->coefs1[ch][0];
eptr = ptr + nb_coefs[ch];
run=0;
for(;ptr < eptr; ptr++){
if(*ptr){
int level= *ptr;
int abs_level= FFABS(level);
int code= 0;
if(abs_level <= s->coef_vlcs[tindex]->max_level){
if(run < s->coef_vlcs[tindex]->levels[abs_level-1])
code= run + s->int_table[tindex][abs_level-1];
}
run = 0;
for (; ptr < eptr; ptr++) {
if (*ptr) {
int level = *ptr;
int abs_level = FFABS(level);
int code = 0;
if (abs_level <= s->coef_vlcs[tindex]->max_level)
if (run < s->coef_vlcs[tindex]->levels[abs_level - 1])
code = run + s->int_table[tindex][abs_level - 1];
assert(code < s->coef_vlcs[tindex]->n);
put_bits(&s->pb, s->coef_vlcs[tindex]->huffbits[code], s->coef_vlcs[tindex]->huffcodes[code]);
put_bits(&s->pb, s->coef_vlcs[tindex]->huffbits[code],
s->coef_vlcs[tindex]->huffcodes[code]);
if(code == 0){
if(1<<coef_nb_bits <= abs_level)
if (code == 0) {
if (1 << coef_nb_bits <= abs_level)
return -1;
put_bits(&s->pb, coef_nb_bits, abs_level);
put_bits(&s->pb, s->frame_len_bits, run);
}
put_bits(&s->pb, 1, level < 0); //FIXME the sign is fliped somewhere
run=0;
}else{
// FIXME the sign is flipped somewhere
put_bits(&s->pb, 1, level < 0);
run = 0;
} else
run++;
}
}
if(run)
put_bits(&s->pb, s->coef_vlcs[tindex]->huffbits[1], s->coef_vlcs[tindex]->huffcodes[1]);
if (run)
put_bits(&s->pb, s->coef_vlcs[tindex]->huffbits[1],
s->coef_vlcs[tindex]->huffcodes[1]);
}
if (s->version == 1 && s->avctx->channels >= 2) {
if (s->version == 1 && s->avctx->channels >= 2)
avpriv_align_put_bits(&s->pb);
}
}
return 0;
}
static int encode_frame(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE], uint8_t *buf, int buf_size, int total_gain){
static int encode_frame(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
uint8_t *buf, int buf_size, int total_gain)
{
init_put_bits(&s->pb, buf, buf_size);
if (s->use_bit_reservoir) {
assert(0);//FIXME not implemented
}else{
if(encode_block(s, src_coefs, total_gain) < 0)
return INT_MAX;
}
if (s->use_bit_reservoir)
assert(0); // FIXME not implemented
else if (encode_block(s, src_coefs, total_gain) < 0)
return INT_MAX;
avpriv_align_put_bits(&s->pb);
@ -349,8 +361,8 @@ static int encode_superframe(AVCodecContext *avctx, AVPacket *avpkt,
WMACodecContext *s = avctx->priv_data;
int i, total_gain, ret;
s->block_len_bits= s->frame_len_bits; //required by non variable block len
s->block_len = 1 << s->block_len_bits;
s->block_len_bits = s->frame_len_bits; // required by non variable block len
s->block_len = 1 << s->block_len_bits;
apply_window_and_mdct(avctx, frame);
@ -358,9 +370,9 @@ static int encode_superframe(AVCodecContext *avctx, AVPacket *avpkt,
float a, b;
int i;
for(i = 0; i < s->block_len; i++) {
a = s->coefs[0][i]*0.5;
b = s->coefs[1][i]*0.5;
for (i = 0; i < s->block_len; i++) {
a = s->coefs[0][i] * 0.5;
b = s->coefs[1][i] * 0.5;
s->coefs[0][i] = a + b;
s->coefs[1][i] = a - b;
}
@ -372,33 +384,33 @@ static int encode_superframe(AVCodecContext *avctx, AVPacket *avpkt,
}
#if 1
total_gain= 128;
for(i=64; i; i>>=1){
total_gain = 128;
for (i = 64; i; i >>= 1) {
int error = encode_frame(s, s->coefs, avpkt->data, avpkt->size,
total_gain - i);
if(error<0)
total_gain-= i;
if (error < 0)
total_gain -= i;
}
#else
total_gain= 90;
total_gain = 90;
best = encode_frame(s, s->coefs, avpkt->data, avpkt->size, total_gain);
for(i=32; i; i>>=1){
for (i = 32; i; i >>= 1) {
int scoreL = encode_frame(s, s->coefs, avpkt->data, avpkt->size, total_gain - i);
int scoreR = encode_frame(s, s->coefs, avpkt->data, avpkt->size, total_gain + i);
av_log(NULL, AV_LOG_ERROR, "%d %d %d (%d)\n", scoreL, best, scoreR, total_gain);
if(scoreL < FFMIN(best, scoreR)){
best = scoreL;
if (scoreL < FFMIN(best, scoreR)) {
best = scoreL;
total_gain -= i;
}else if(scoreR < best){
best = scoreR;
} else if (scoreR < best) {
best = scoreR;
total_gain += i;
}
}
#endif
#endif /* 1 */
if ((i = encode_frame(s, s->coefs, avpkt->data, avpkt->size, total_gain)) >= 0) {
av_log(avctx, AV_LOG_ERROR, "required frame size too large. please "
"use a higher bit rate.\n");
"use a higher bit rate.\n");
return AVERROR(EINVAL);
}
assert((put_bits_count(&s->pb) & 7) == 0);
@ -410,7 +422,7 @@ static int encode_superframe(AVCodecContext *avctx, AVPacket *avpkt,
if (frame->pts != AV_NOPTS_VALUE)
avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->delay);
avpkt->size = avctx->block_align;
avpkt->size = avctx->block_align;
*got_packet_ptr = 1;
return 0;
}
@ -424,8 +436,8 @@ AVCodec ff_wmav1_encoder = {
.init = encode_init,
.encode2 = encode_superframe,
.close = ff_wma_end,
.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP,
AV_SAMPLE_FMT_NONE },
.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
AV_SAMPLE_FMT_NONE },
};
AVCodec ff_wmav2_encoder = {
@ -437,6 +449,6 @@ AVCodec ff_wmav2_encoder = {
.init = encode_init,
.encode2 = encode_superframe,
.close = ff_wma_end,
.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP,
AV_SAMPLE_FMT_NONE },
.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
AV_SAMPLE_FMT_NONE },
};