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"
@ -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,9 +109,8 @@ 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;
@ -120,66 +119,61 @@ av_cold int ff_wma_init(AVCodecContext *avctx, int flags2)
/* 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);
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",
s->version, avctx->channels, avctx->sample_rate, avctx->bit_rate,
@ -194,11 +188,10 @@ 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;
@ -223,14 +216,13 @@ av_cold int ff_wma_init(AVCodecContext *avctx, int flags2)
table = NULL;
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++;
for (i = 0; i < n; i++)
@ -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++)
@ -335,35 +325,39 @@ av_cold int ff_wma_init(AVCodecContext *avctx, int flags2)
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]);
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]);
@ -404,11 +396,10 @@ 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);
}
@ -429,10 +420,10 @@ unsigned int ff_wma_get_large_val(GetBitContext* gb)
* @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,
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;
@ -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);

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
@ -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];
@ -150,10 +151,10 @@ 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,
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,32 +32,28 @@
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;
}

View File

@ -28,6 +28,7 @@
#define AVCODEC_WMADATA_H
#include <stdint.h>
#include "wma.h"
const uint16_t ff_wma_critical_freqs[25] = {
@ -41,7 +42,8 @@ const uint16_t ff_wma_critical_freqs[25] = {
static const uint8_t exponent_band_22050[3][25] = {
{ 10, 4, 8, 4, 8, 8, 12, 20, 24, 24, 16, },
{ 14, 4, 8, 8, 4, 12, 12, 16, 24, 16, 20, 24, 32, 40, 36, },
{ 23, 4, 4, 4, 8, 4, 4, 8, 8, 8, 8, 8, 12, 12, 16, 16, 24, 24, 32, 44, 48, 60, 84, 72, },
{ 23, 4, 4, 4, 8, 4, 4, 8, 8, 8, 8, 8, 12, 12, 16, 16,
24, 24, 32, 44, 48, 60, 84, 72, },
};
static const uint8_t exponent_band_32000[3][25] = {
@ -65,10 +67,8 @@ const uint16_t ff_wma_hgain_huffcodes[37] = {
};
const uint8_t ff_wma_hgain_huffbits[37] = {
10, 12, 10, 13, 9, 13, 9, 8,
7, 5, 5, 4, 4, 3, 3, 3,
4, 3, 4, 4, 5, 5, 6, 8,
7, 10, 8, 10, 9, 8, 9, 9,
10, 12, 10, 13, 9, 13, 9, 8, 7, 5, 5, 4, 4, 3, 3, 3,
4, 3, 4, 4, 5, 5, 6, 8, 7, 10, 8, 10, 9, 8, 9, 9,
13, 10, 13, 13, 13,
};
@ -1380,24 +1380,12 @@ static const uint16_t levels5[40] = {
};
static const CoefVLCTable coef_vlcs[6] = {
{
sizeof(coef0_huffbits), sizeof(levels0)/2, coef0_huffcodes, coef0_huffbits, levels0,
},
{
sizeof(coef1_huffbits), sizeof(levels1)/2, coef1_huffcodes, coef1_huffbits, levels1,
},
{
sizeof(coef2_huffbits), sizeof(levels2)/2, coef2_huffcodes, coef2_huffbits, levels2,
},
{
sizeof(coef3_huffbits), sizeof(levels3)/2, coef3_huffcodes, coef3_huffbits, levels3,
},
{
sizeof(coef4_huffbits), sizeof(levels4)/2, coef4_huffcodes, coef4_huffbits, levels4,
},
{
sizeof(coef5_huffbits), sizeof(levels5)/2, coef5_huffcodes, coef5_huffbits, levels5,
},
{ sizeof(coef0_huffbits), sizeof(levels0) / 2, coef0_huffcodes, coef0_huffbits, levels0, },
{ sizeof(coef1_huffbits), sizeof(levels1) / 2, coef1_huffcodes, coef1_huffbits, levels1, },
{ sizeof(coef2_huffbits), sizeof(levels2) / 2, coef2_huffcodes, coef2_huffbits, levels2, },
{ sizeof(coef3_huffbits), sizeof(levels3) / 2, coef3_huffcodes, coef3_huffbits, levels3, },
{ sizeof(coef4_huffbits), sizeof(levels4) / 2, coef4_huffcodes, coef4_huffbits, levels4, },
{ sizeof(coef5_huffbits), sizeof(levels5) / 2, coef5_huffcodes, coef5_huffbits, levels5, },
};
#endif /* AVCODEC_WMADATA_H */

View File

@ -34,6 +34,7 @@
*/
#include "libavutil/attributes.h"
#include "avcodec.h"
#include "internal.h"
#include "wma.h"
@ -50,7 +51,8 @@
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;
@ -65,7 +67,7 @@ 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)
{
@ -83,11 +85,10 @@ static av_cold int wma_decode_init(AVCodecContext * avctx)
/* extract flag infos */
flags2 = 0;
extradata = avctx->extradata;
if (avctx->codec->id == AV_CODEC_ID_WMAV1 && avctx->extradata_size >= 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) {
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;
@ -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) {
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;
@ -160,7 +160,7 @@ static av_cold void wma_lsp_to_curve_init(WMACodecContext *s, int frame_len)
}
/* 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;
@ -176,8 +176,7 @@ 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;
@ -365,7 +364,6 @@ static int decode_exp_vlc(WMACodecContext *s, int ch)
return 0;
}
/**
* Apply MDCT window and add into output.
*
@ -384,7 +382,6 @@ static void wma_window(WMACodecContext *s, float *out)
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;
@ -405,7 +402,6 @@ static void wma_window(WMACodecContext *s, float *out)
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;
@ -413,13 +409,13 @@ static void wma_window(WMACodecContext *s, float *out)
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));
}
}
/**
* @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) {
@ -444,13 +441,17 @@ static int wma_decode_block(WMACodecContext *s)
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);
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);
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;
@ -461,7 +462,9 @@ static int wma_decode_block(WMACodecContext *s)
}
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);
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;
@ -479,9 +482,8 @@ static int wma_decode_block(WMACodecContext *s)
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);
@ -497,7 +499,7 @@ 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);
@ -515,7 +517,6 @@ static int wma_decode_block(WMACodecContext *s)
/* complex coding */
if (s->use_noise_coding) {
for (ch = 0; ch < s->avctx->channels; ch++) {
if (s->channel_coded[ch]) {
int i, n, a;
@ -540,9 +541,11 @@ static int wma_decode_block(WMACodecContext *s)
if (val == (int) 0x80000000) {
val = get_bits(&s->gb, 7) - 19;
} else {
code = get_vlc2(&s->gb, s->hgain_vlc.table, HGAINVLCBITS, HGAINMAX);
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");
av_log(s->avctx, AV_LOG_ERROR,
"hgain vlc invalid\n");
return -1;
}
val += code - 18;
@ -555,8 +558,7 @@ 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)) {
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) {
@ -577,7 +579,7 @@ static int wma_decode_block(WMACodecContext *s)
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],
@ -585,19 +587,17 @@ static int wma_decode_block(WMACodecContext *s)
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) {
if (s->version == 1)
mdct_norm *= sqrt(n4);
}
}
/* finally compute the MDCT coefficients */
for (ch = 0; ch < s->avctx->channels; ch++) {
@ -619,7 +619,8 @@ static int wma_decode_block(WMACodecContext *s)
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);
s->noise_index = (s->noise_index + 1) &
(NOISE_TAB_SIZE - 1);
}
n1 = s->exponent_high_sizes[bsize];
@ -648,13 +649,11 @@ static int wma_decode_block(WMACodecContext *s)
/* 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 {
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];
}
if (j >= 0 && s->high_band_coded[ch][j]) {
/* use noise with specified power */
mult1 = sqrt(exp_power[j] / exp_power[last_high_band]);
@ -665,8 +664,7 @@ static int wma_decode_block(WMACodecContext *s)
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;
} else {
@ -693,9 +691,8 @@ static int wma_decode_block(WMACodecContext *s)
for (i = 0; i < s->coefs_start; i++)
*coefs++ = 0.0;
n = nb_coefs[ch];
for(i = 0;i < n; i++) {
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++)
*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,8 +755,9 @@ 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;
@ -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;
@ -820,9 +819,8 @@ static int wma_decode_superframe(AVCodecContext *avctx, void *data,
/* 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;
@ -853,18 +851,18 @@ static int wma_decode_superframe(AVCodecContext *avctx, void *data,
*q++ = (get_bits) (&s->gb, 8);
len -= 8;
}
if (len > 0) {
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;
@ -888,7 +886,8 @@ static int wma_decode_superframe(AVCodecContext *avctx, void *data,
}
/* 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;
@ -912,6 +911,7 @@ static int wma_decode_superframe(AVCodecContext *avctx, void *data,
*got_frame_ptr = 1;
return avctx->block_align;
fail:
/* when error, we reset the bit reservoir */
s->last_superframe_len = 0;

View File

@ -20,6 +20,7 @@
*/
#include "libavutil/attributes.h"
#include "avcodec.h"
#include "internal.h"
#include "wma.h"
@ -37,7 +38,8 @@ 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",
av_log(avctx, AV_LOG_ERROR,
"too many channels: got %i, need %i or fewer",
avctx->channels, MAX_CHANNELS);
return AVERROR(EINVAL);
}
@ -49,7 +51,8 @@ static av_cold int encode_init(AVCodecContext *avctx)
}
if (avctx->bit_rate < 24 * 1000) {
av_log(avctx, AV_LOG_ERROR, "bitrate too low: got %i, need 24000 or higher\n",
av_log(avctx, AV_LOG_ERROR,
"bitrate too low: got %i, need 24000 or higher\n",
avctx->bit_rate);
return AVERROR(EINVAL);
}
@ -67,8 +70,9 @@ static av_cold int encode_init(AVCodecContext *avctx)
avctx->extradata_size = 10;
AV_WL32(extradata, flags1);
AV_WL16(extradata + 4, flags2);
}else
} else {
assert(0);
}
avctx->extradata = extradata;
s->use_exp_vlc = flags2 & 0x0001;
s->use_bit_reservoir = flags2 & 0x0002;
@ -88,12 +92,12 @@ static av_cold int encode_init(AVCodecContext *avctx)
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)
{
WMACodecContext *s = avctx->priv_data;
@ -109,14 +113,16 @@ static void apply_window_and_mdct(AVCodecContext * avctx, const AVFrame *frame)
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){
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;
@ -137,7 +143,8 @@ 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;
@ -156,18 +163,27 @@ static void encode_exp_vlc(WMACodecContext *s, int ch, const int *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;
}
}
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
if (s->use_variable_block_len) {
@ -190,21 +206,19 @@ static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
{
int n4 = s->block_len / 2;
mdct_norm = 1.0 / (float) n4;
if (s->version == 1) {
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++) {
if (s->channel_coded[ch]) {
@ -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++) {
@ -279,9 +292,8 @@ static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
}
}
}
} else {
} else
assert(0); // FIXME not implemented
}
for (ch = 0; ch < s->avctx->channels; ch++) {
if (s->channel_coded[ch]) {
@ -297,13 +309,13 @@ static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
int level = *ptr;
int abs_level = FFABS(level);
int code = 0;
if(abs_level <= s->coef_vlcs[tindex]->max_level){
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)
@ -312,31 +324,31 @@ static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
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
// FIXME the sign is flipped somewhere
put_bits(&s->pb, 1, level < 0);
run = 0;
}else{
} else
run++;
}
}
if (run)
put_bits(&s->pb, s->coef_vlcs[tindex]->huffbits[1], s->coef_vlcs[tindex]->huffcodes[1]);
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) {
if (s->use_bit_reservoir)
assert(0); // FIXME not implemented
}else{
if(encode_block(s, src_coefs, total_gain) < 0)
else if (encode_block(s, src_coefs, total_gain) < 0)
return INT_MAX;
}
avpriv_align_put_bits(&s->pb);
@ -394,7 +406,7 @@ static int encode_superframe(AVCodecContext *avctx, AVPacket *avpkt,
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 "