static int MPA_encode_init(AVCodecContext *avctx)

{

    MpegAudioContext *s = avctx->priv_data;

    int freq = avctx->sample_rate;

    int bitrate = avctx->bit_rate;

    int channels = avctx->channels;

    int i, v, table;

    float a;



    if (channels > 2)

        return -1;

    bitrate = bitrate / 1000;

    s->nb_channels = channels;

    s->freq = freq;

    s->bit_rate = bitrate * 1000;

    avctx->frame_size = MPA_FRAME_SIZE;



    /* encoding freq */

    s->lsf = 0;

    for(i=0;i<3;i++) {

        if (mpa_freq_tab[i] == freq)

            break;

        if ((mpa_freq_tab[i] / 2) == freq) {

            s->lsf = 1;

            break;

        }

    }

    if (i == 3){

        av_log(avctx, AV_LOG_ERROR, "Sampling rate %d is not allowed in mp2\n", freq);

        return -1;

    }

    s->freq_index = i;



    /* encoding bitrate & frequency */

    for(i=0;i<15;i++) {

        if (mpa_bitrate_tab[s->lsf][1][i] == bitrate)

            break;

    }

    if (i == 15){

        av_log(avctx, AV_LOG_ERROR, "bitrate %d is not allowed in mp2\n", bitrate);

        return -1;

    }

    s->bitrate_index = i;



    /* compute total header size & pad bit */



    a = (float)(bitrate * 1000 * MPA_FRAME_SIZE) / (freq * 8.0);

    s->frame_size = ((int)a) * 8;



    /* frame fractional size to compute padding */

    s->frame_frac = 0;

    s->frame_frac_incr = (int)((a - floor(a)) * 65536.0);



    /* select the right allocation table */

    table = l2_select_table(bitrate, s->nb_channels, freq, s->lsf);



    /* number of used subbands */

    s->sblimit = sblimit_table[table];

    s->alloc_table = alloc_tables[table];



#ifdef DEBUG

    av_log(avctx, AV_LOG_DEBUG, "%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n",

           bitrate, freq, s->frame_size, table, s->frame_frac_incr);

#endif



    for(i=0;i<s->nb_channels;i++)

        s->samples_offset[i] = 0;



    for(i=0;i<257;i++) {

        int v;

        v = mpa_enwindow[i];

#if WFRAC_BITS != 16

        v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);

#endif

        filter_bank[i] = v;

        if ((i & 63) != 0)

            v = -v;

        if (i != 0)

            filter_bank[512 - i] = v;

    }



    for(i=0;i<64;i++) {

        v = (int)(pow(2.0, (3 - i) / 3.0) * (1 << 20));

        if (v <= 0)

            v = 1;

        scale_factor_table[i] = v;

#ifdef USE_FLOATS

        scale_factor_inv_table[i] = pow(2.0, -(3 - i) / 3.0) / (float)(1 << 20);

#else

#define P 15

        scale_factor_shift[i] = 21 - P - (i / 3);

        scale_factor_mult[i] = (1 << P) * pow(2.0, (i % 3) / 3.0);

#endif

    }

    for(i=0;i<128;i++) {

        v = i - 64;

        if (v <= -3)

            v = 0;

        else if (v < 0)

            v = 1;

        else if (v == 0)

            v = 2;

        else if (v < 3)

            v = 3;

        else

            v = 4;

        scale_diff_table[i] = v;

    }



    for(i=0;i<17;i++) {

        v = quant_bits[i];

        if (v < 0)

            v = -v;

        else

            v = v * 3;

        total_quant_bits[i] = 12 * v;

    }



    avctx->coded_frame= avcodec_alloc_frame();

    avctx->coded_frame->key_frame= 1;



    return 0;

}

static int MPA_encode_init(AVCodecContext *avctx)

{

    MpegAudioContext *s = avctx->priv_data;

    int freq = avctx->sample_rate;

    int bitrate = avctx->bit_rate;

    int channels = avctx->channels;

    int i, v, table;

    float a;



    if (channels > 2)

        return -1;

    bitrate = bitrate / 1000;

    s->nb_channels = channels;

    s->freq = freq;

    s->bit_rate = bitrate * 1000;

    avctx->frame_size = MPA_FRAME_SIZE;



    

    s->lsf = 0;

    for(i=0;i<3;i++) {

        if (mpa_freq_tab[i] == freq)

            break;

        if ((mpa_freq_tab[i] / 2) == freq) {

            s->lsf = 1;

            break;

        }

    }

    if (i == 3){

        av_log(avctx, AV_LOG_ERROR, "Sampling rate %d is not allowed in mp2\n", freq);

        return -1;

    }

    s->freq_index = i;



    

    for(i=0;i<15;i++) {

        if (mpa_bitrate_tab[s->lsf][1][i] == bitrate)

            break;

    }

    if (i == 15){

        av_log(avctx, AV_LOG_ERROR, "bitrate %d is not allowed in mp2\n", bitrate);

        return -1;

    }

    s->bitrate_index = i;



    



    a = (float)(bitrate * 1000 * MPA_FRAME_SIZE) / (freq * 8.0);

    s->frame_size = ((int)a) * 8;



    

    s->frame_frac = 0;

    s->frame_frac_incr = (int)((a - floor(a)) * 65536.0);



    

    table = l2_select_table(bitrate, s->nb_channels, freq, s->lsf);



    

    s->sblimit = sblimit_table[table];

    s->alloc_table = alloc_tables[table];



#ifdef DEBUG

    av_log(avctx, AV_LOG_DEBUG, "%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n",

           bitrate, freq, s->frame_size, table, s->frame_frac_incr);

#endif



    for(i=0;i<s->nb_channels;i++)

        s->samples_offset[i] = 0;



    for(i=0;i<257;i++) {

        int v;

        v = mpa_enwindow[i];

#if WFRAC_BITS != 16

        v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);

#endif

        filter_bank[i] = v;

        if ((i & 63) != 0)

            v = -v;

        if (i != 0)

            filter_bank[512 - i] = v;

    }



    for(i=0;i<64;i++) {

        v = (int)(pow(2.0, (3 - i) / 3.0) * (1 << 20));

        if (v <= 0)

            v = 1;

        scale_factor_table[i] = v;

#ifdef USE_FLOATS

        scale_factor_inv_table[i] = pow(2.0, -(3 - i) / 3.0) / (float)(1 << 20);

#else

#define P 15

        scale_factor_shift[i] = 21 - P - (i / 3);

        scale_factor_mult[i] = (1 << P) * pow(2.0, (i % 3) / 3.0);

#endif

    }

    for(i=0;i<128;i++) {

        v = i - 64;

        if (v <= -3)

            v = 0;

        else if (v < 0)

            v = 1;

        else if (v == 0)

            v = 2;

        else if (v < 3)

            v = 3;

        else

            v = 4;

        scale_diff_table[i] = v;

    }



    for(i=0;i<17;i++) {

        v = quant_bits[i];

        if (v < 0)

            v = -v;

        else

            v = v * 3;

        total_quant_bits[i] = 12 * v;

    }



    avctx->coded_frame= avcodec_alloc_frame();

    avctx->coded_frame->key_frame= 1;



    return 0;

}

static int FUNC_0(AVCodecContext *VAR_0)

{

    MpegAudioContext *s = VAR_0->priv_data;

    int VAR_1 = VAR_0->sample_rate;

    int VAR_2 = VAR_0->bit_rate;

    int VAR_3 = VAR_0->VAR_3;

    int VAR_4, VAR_8, VAR_6;

    float VAR_7;



    if (VAR_3 > 2)

        return -1;

    VAR_2 = VAR_2 / 1000;

    s->nb_channels = VAR_3;

    s->VAR_1 = VAR_1;

    s->bit_rate = VAR_2 * 1000;

    VAR_0->frame_size = MPA_FRAME_SIZE;



    

    s->lsf = 0;

    for(VAR_4=0;VAR_4<3;VAR_4++) {

        if (mpa_freq_tab[VAR_4] == VAR_1)

            break;

        if ((mpa_freq_tab[VAR_4] / 2) == VAR_1) {

            s->lsf = 1;

            break;

        }

    }

    if (VAR_4 == 3){

        av_log(VAR_0, AV_LOG_ERROR, "Sampling rate %d is not allowed in mp2\n", VAR_1);

        return -1;

    }

    s->freq_index = VAR_4;



    

    for(VAR_4=0;VAR_4<15;VAR_4++) {

        if (mpa_bitrate_tab[s->lsf][1][VAR_4] == VAR_2)

            break;

    }

    if (VAR_4 == 15){

        av_log(VAR_0, AV_LOG_ERROR, "VAR_2 %d is not allowed in mp2\n", VAR_2);

        return -1;

    }

    s->bitrate_index = VAR_4;



    



    VAR_7 = (float)(VAR_2 * 1000 * MPA_FRAME_SIZE) / (VAR_1 * 8.0);

    s->frame_size = ((int)VAR_7) * 8;



    

    s->frame_frac = 0;

    s->frame_frac_incr = (int)((VAR_7 - floor(VAR_7)) * 65536.0);



    

    VAR_6 = l2_select_table(VAR_2, s->nb_channels, VAR_1, s->lsf);



    

    s->sblimit = sblimit_table[VAR_6];

    s->alloc_table = alloc_tables[VAR_6];



#ifdef DEBUG

    av_log(VAR_0, AV_LOG_DEBUG, "%d kb/s, %d Hz, frame_size=%d bits, VAR_6=%d, padincr=%x\n",

           VAR_2, VAR_1, s->frame_size, VAR_6, s->frame_frac_incr);

#endif



    for(VAR_4=0;VAR_4<s->nb_channels;VAR_4++)

        s->samples_offset[VAR_4] = 0;



    for(VAR_4=0;VAR_4<257;VAR_4++) {

        int VAR_8;

        VAR_8 = mpa_enwindow[VAR_4];

#if WFRAC_BITS != 16

        VAR_8 = (VAR_8 + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);

#endif

        filter_bank[VAR_4] = VAR_8;

        if ((VAR_4 & 63) != 0)

            VAR_8 = -VAR_8;

        if (VAR_4 != 0)

            filter_bank[512 - VAR_4] = VAR_8;

    }



    for(VAR_4=0;VAR_4<64;VAR_4++) {

        VAR_8 = (int)(pow(2.0, (3 - VAR_4) / 3.0) * (1 << 20));

        if (VAR_8 <= 0)

            VAR_8 = 1;

        scale_factor_table[VAR_4] = VAR_8;

#ifdef USE_FLOATS

        scale_factor_inv_table[VAR_4] = pow(2.0, -(3 - VAR_4) / 3.0) / (float)(1 << 20);

#else

#define P 15

        scale_factor_shift[VAR_4] = 21 - P - (VAR_4 / 3);

        scale_factor_mult[VAR_4] = (1 << P) * pow(2.0, (VAR_4 % 3) / 3.0);

#endif

    }

    for(VAR_4=0;VAR_4<128;VAR_4++) {

        VAR_8 = VAR_4 - 64;

        if (VAR_8 <= -3)

            VAR_8 = 0;

        else if (VAR_8 < 0)

            VAR_8 = 1;

        else if (VAR_8 == 0)

            VAR_8 = 2;

        else if (VAR_8 < 3)

            VAR_8 = 3;

        else

            VAR_8 = 4;

        scale_diff_table[VAR_4] = VAR_8;

    }



    for(VAR_4=0;VAR_4<17;VAR_4++) {

        VAR_8 = quant_bits[VAR_4];

        if (VAR_8 < 0)

            VAR_8 = -VAR_8;

        else

            VAR_8 = VAR_8 * 3;

        total_quant_bits[VAR_4] = 12 * VAR_8;

    }



    VAR_0->coded_frame= avcodec_alloc_frame();

    VAR_0->coded_frame->key_frame= 1;



    return 0;

}

static int FUNC_0(AVCodecContext *VAR_0)
{ MpegAudioContext *s = VAR_0->priv_data; int VAR_1 = VAR_0->sample_rate; int VAR_2 = VAR_0->bit_rate; int VAR_3 = VAR_0->VAR_3; int VAR_4, VAR_8, VAR_6; float VAR_7; if (VAR_3 > 2)
return -1; VAR_2 = VAR_2 / 1000; s->nb_channels = VAR_3; s->VAR_1 = VAR_1; s->bit_rate = VAR_2 * 1000; VAR_0->frame_size = MPA_FRAME_SIZE; s->lsf = 0; for(VAR_4=0;VAR_4<3;VAR_4++) { if (mpa_freq_tab[VAR_4] == VAR_1)
break; if ((mpa_freq_tab[VAR_4] / 2) == VAR_1) { s->lsf = 1; break; } } if (VAR_4 == 3){ av_log(VAR_0, AV_LOG_ERROR, "Sampling rate %d is not allowed in mp2\n", VAR_1); return -1; } s->freq_index = VAR_4; for(VAR_4=0;VAR_4<15;VAR_4++) { if (mpa_bitrate_tab[s->lsf][1][VAR_4] == VAR_2)
break; } if (VAR_4 == 15){ av_log(VAR_0, AV_LOG_ERROR, "VAR_2 %d is not allowed in mp2\n", VAR_2); return -1; } s->bitrate_index = VAR_4; VAR_7 = (float)(VAR_2 * 1000 * MPA_FRAME_SIZE) / (VAR_1 * 8.0); s->frame_size = ((int)VAR_7) * 8; s->frame_frac = 0; s->frame_frac_incr = (int)((VAR_7 - floor(VAR_7)) * 65536.0); VAR_6 = l2_select_table(VAR_2, s->nb_channels, VAR_1, s->lsf); s->sblimit = sblimit_table[VAR_6]; s->alloc_table = alloc_tables[VAR_6]; #ifdef DEBUG
av_log(VAR_0, AV_LOG_DEBUG, "%d kb/s, %d Hz, frame_size=%d bits, VAR_6=%d, padincr=%x\n",
VAR_2, VAR_1, s->frame_size, VAR_6, s->frame_frac_incr); #endif
for(VAR_4=0;VAR_4<s->nb_channels;VAR_4++) s->samples_offset[VAR_4] = 0; for(VAR_4=0;VAR_4<257;VAR_4++) { int VAR_8; VAR_8 = mpa_enwindow[VAR_4]; #if WFRAC_BITS != 16
VAR_8 = (VAR_8 + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS); #endif
filter_bank[VAR_4] = VAR_8; if ((VAR_4 & 63) != 0)
VAR_8 = -VAR_8; if (VAR_4 != 0)
filter_bank[512 - VAR_4] = VAR_8; } for(VAR_4=0;VAR_4<64;VAR_4++) { VAR_8 = (int)(pow(2.0, (3 - VAR_4) / 3.0) * (1 << 20)); if (VAR_8 <= 0)
VAR_8 = 1; scale_factor_table[VAR_4] = VAR_8; #ifdef USE_FLOATS
scale_factor_inv_table[VAR_4] = pow(2.0, -(3 - VAR_4) / 3.0) / (float)(1 << 20); #else
#define P 15
scale_factor_shift[VAR_4] = 21 - P - (VAR_4 / 3); scale_factor_mult[VAR_4] = (1 << P) * pow(2.0, (VAR_4 % 3) / 3.0); #endif
} for(VAR_4=0;VAR_4<128;VAR_4++) { VAR_8 = VAR_4 - 64; if (VAR_8 <= -3)
VAR_8 = 0; else if (VAR_8 < 0)
VAR_8 = 1; else if (VAR_8 == 0)
VAR_8 = 2; else if (VAR_8 < 3)
VAR_8 = 3; else
VAR_8 = 4; scale_diff_table[VAR_4] = VAR_8; } for(VAR_4=0;VAR_4<17;VAR_4++) { VAR_8 = quant_bits[VAR_4]; if (VAR_8 < 0)
VAR_8 = -VAR_8; else
VAR_8 = VAR_8 * 3; total_quant_bits[VAR_4] = 12 * VAR_8; } VAR_0->coded_frame= avcodec_alloc_frame(); VAR_0->coded_frame->key_frame= 1; return 0; }