static int sonic_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,

                              const AVFrame *frame, int *got_packet_ptr)

{

    SonicContext *s = avctx->priv_data;

    RangeCoder c;

    int i, j, ch, quant = 0, x = 0;

    int ret;

    const short *samples = (const int16_t*)frame->data[0];

    uint8_t state[32];



    if ((ret = ff_alloc_packet2(avctx, avpkt, s->frame_size * 5 + 1000)) < 0)

        return ret;



    ff_init_range_encoder(&c, avpkt->data, avpkt->size);

    ff_build_rac_states(&c, 0.05*(1LL<<32), 256-8);

    memset(state, 128, sizeof(state));



    // short -> internal

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

        s->int_samples[i] = samples[i];



    if (!s->lossless)

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

            s->int_samples[i] = s->int_samples[i] << SAMPLE_SHIFT;



    switch(s->decorrelation)

    {

        case MID_SIDE:

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

            {

                s->int_samples[i] += s->int_samples[i+1];

                s->int_samples[i+1] -= shift(s->int_samples[i], 1);

            }

            break;

        case LEFT_SIDE:

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

                s->int_samples[i+1] -= s->int_samples[i];

            break;

        case RIGHT_SIDE:

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

                s->int_samples[i] -= s->int_samples[i+1];

            break;

    }



    memset(s->window, 0, 4* s->window_size);



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

        s->window[x++] = s->tail[i];



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

        s->window[x++] = s->int_samples[i];



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

        s->window[x++] = 0;



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

        s->tail[i] = s->int_samples[s->frame_size - s->tail_size + i];



    // generate taps

    modified_levinson_durbin(s->window, s->window_size,

                s->predictor_k, s->num_taps, s->channels, s->tap_quant);

    if ((ret = intlist_write(&c, state, s->predictor_k, s->num_taps, 0)) < 0)

        return ret;



    for (ch = 0; ch < s->channels; ch++)

    {

        x = s->tail_size+ch;

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

        {

            int sum = 0;

            for (j = 0; j < s->downsampling; j++, x += s->channels)

                sum += s->window[x];

            s->coded_samples[ch][i] = sum;

        }

    }



    // simple rate control code

    if (!s->lossless)

    {

        double energy1 = 0.0, energy2 = 0.0;

        for (ch = 0; ch < s->channels; ch++)

        {

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

            {

                double sample = s->coded_samples[ch][i];

                energy2 += sample*sample;

                energy1 += fabs(sample);

            }

        }



        energy2 = sqrt(energy2/(s->channels*s->block_align));

        energy1 = M_SQRT2*energy1/(s->channels*s->block_align);



        // increase bitrate when samples are like a gaussian distribution

        // reduce bitrate when samples are like a two-tailed exponential distribution



        if (energy2 > energy1)

            energy2 += (energy2-energy1)*RATE_VARIATION;



        quant = (int)(BASE_QUANT*s->quantization*energy2/SAMPLE_FACTOR);

//        av_log(avctx, AV_LOG_DEBUG, "quant: %d energy: %f / %f\n", quant, energy1, energy2);



        quant = av_clip(quant, 1, 65534);



        put_symbol(&c, state, quant, 0, NULL, NULL);



        quant *= SAMPLE_FACTOR;

    }



    // write out coded samples

    for (ch = 0; ch < s->channels; ch++)

    {

        if (!s->lossless)

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

                s->coded_samples[ch][i] = ROUNDED_DIV(s->coded_samples[ch][i], quant);



        if ((ret = intlist_write(&c, state, s->coded_samples[ch], s->block_align, 1)) < 0)

            return ret;

    }



//    av_log(avctx, AV_LOG_DEBUG, "used bytes: %d\n", (put_bits_count(&pb)+7)/8);



    avpkt->size = ff_rac_terminate(&c);

    *got_packet_ptr = 1;

    return 0;



}

static int sonic_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,

                              const AVFrame *frame, int *got_packet_ptr)

{

    SonicContext *s = avctx->priv_data;

    RangeCoder c;

    int i, j, ch, quant = 0, x = 0;

    int ret;

    const short *samples = (const int16_t*)frame->data[0];

    uint8_t state[32];



    if ((ret = ff_alloc_packet2(avctx, avpkt, s->frame_size * 5 + 1000)) < 0)

        return ret;



    ff_init_range_encoder(&c, avpkt->data, avpkt->size);

    ff_build_rac_states(&c, 0.05*(1LL<<32), 256-8);

    memset(state, 128, sizeof(state));



    

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

        s->int_samples[i] = samples[i];



    if (!s->lossless)

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

            s->int_samples[i] = s->int_samples[i] << SAMPLE_SHIFT;



    switch(s->decorrelation)

    {

        case MID_SIDE:

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

            {

                s->int_samples[i] += s->int_samples[i+1];

                s->int_samples[i+1] -= shift(s->int_samples[i], 1);

            }

            break;

        case LEFT_SIDE:

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

                s->int_samples[i+1] -= s->int_samples[i];

            break;

        case RIGHT_SIDE:

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

                s->int_samples[i] -= s->int_samples[i+1];

            break;

    }



    memset(s->window, 0, 4* s->window_size);



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

        s->window[x++] = s->tail[i];



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

        s->window[x++] = s->int_samples[i];



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

        s->window[x++] = 0;



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

        s->tail[i] = s->int_samples[s->frame_size - s->tail_size + i];



    

    modified_levinson_durbin(s->window, s->window_size,

                s->predictor_k, s->num_taps, s->channels, s->tap_quant);

    if ((ret = intlist_write(&c, state, s->predictor_k, s->num_taps, 0)) < 0)

        return ret;



    for (ch = 0; ch < s->channels; ch++)

    {

        x = s->tail_size+ch;

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

        {

            int sum = 0;

            for (j = 0; j < s->downsampling; j++, x += s->channels)

                sum += s->window[x];

            s->coded_samples[ch][i] = sum;

        }

    }



    

    if (!s->lossless)

    {

        double energy1 = 0.0, energy2 = 0.0;

        for (ch = 0; ch < s->channels; ch++)

        {

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

            {

                double sample = s->coded_samples[ch][i];

                energy2 += sample*sample;

                energy1 += fabs(sample);

            }

        }



        energy2 = sqrt(energy2/(s->channels*s->block_align));

        energy1 = M_SQRT2*energy1/(s->channels*s->block_align);



        

        



        if (energy2 > energy1)

            energy2 += (energy2-energy1)*RATE_VARIATION;



        quant = (int)(BASE_QUANT*s->quantization*energy2/SAMPLE_FACTOR);





        quant = av_clip(quant, 1, 65534);



        put_symbol(&c, state, quant, 0, NULL, NULL);



        quant *= SAMPLE_FACTOR;

    }



    

    for (ch = 0; ch < s->channels; ch++)

    {

        if (!s->lossless)

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

                s->coded_samples[ch][i] = ROUNDED_DIV(s->coded_samples[ch][i], quant);



        if ((ret = intlist_write(&c, state, s->coded_samples[ch], s->block_align, 1)) < 0)

            return ret;

    }







    avpkt->size = ff_rac_terminate(&c);

    *got_packet_ptr = 1;

    return 0;



}

static int FUNC_0(AVCodecContext *VAR_0, AVPacket *VAR_1,

                              const AVFrame *VAR_2, int *VAR_3)

{

    SonicContext *s = VAR_0->priv_data;

    RangeCoder c;

    int VAR_4, VAR_5, VAR_6, VAR_7 = 0, VAR_8 = 0;

    int VAR_9;

    const short *VAR_10 = (const int16_t*)VAR_2->data[0];

    uint8_t state[32];



    if ((VAR_9 = ff_alloc_packet2(VAR_0, VAR_1, s->frame_size * 5 + 1000)) < 0)

        return VAR_9;



    ff_init_range_encoder(&c, VAR_1->data, VAR_1->size);

    ff_build_rac_states(&c, 0.05*(1LL<<32), 256-8);

    memset(state, 128, sizeof(state));



    

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

        s->int_samples[VAR_4] = VAR_10[VAR_4];



    if (!s->lossless)

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

            s->int_samples[VAR_4] = s->int_samples[VAR_4] << SAMPLE_SHIFT;



    switch(s->decorrelation)

    {

        case MID_SIDE:

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

            {

                s->int_samples[VAR_4] += s->int_samples[VAR_4+1];

                s->int_samples[VAR_4+1] -= shift(s->int_samples[VAR_4], 1);

            }

            break;

        case LEFT_SIDE:

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

                s->int_samples[VAR_4+1] -= s->int_samples[VAR_4];

            break;

        case RIGHT_SIDE:

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

                s->int_samples[VAR_4] -= s->int_samples[VAR_4+1];

            break;

    }



    memset(s->window, 0, 4* s->window_size);



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

        s->window[VAR_8++] = s->tail[VAR_4];



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

        s->window[VAR_8++] = s->int_samples[VAR_4];



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

        s->window[VAR_8++] = 0;



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

        s->tail[VAR_4] = s->int_samples[s->frame_size - s->tail_size + VAR_4];



    

    modified_levinson_durbin(s->window, s->window_size,

                s->predictor_k, s->num_taps, s->channels, s->tap_quant);

    if ((VAR_9 = intlist_write(&c, state, s->predictor_k, s->num_taps, 0)) < 0)

        return VAR_9;



    for (VAR_6 = 0; VAR_6 < s->channels; VAR_6++)

    {

        VAR_8 = s->tail_size+VAR_6;

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

        {

            int sum = 0;

            for (VAR_5 = 0; VAR_5 < s->downsampling; VAR_5++, VAR_8 += s->channels)

                sum += s->window[VAR_8];

            s->coded_samples[VAR_6][VAR_4] = sum;

        }

    }



    

    if (!s->lossless)

    {

        double VAR_11 = 0.0, VAR_12 = 0.0;

        for (VAR_6 = 0; VAR_6 < s->channels; VAR_6++)

        {

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

            {

                double sample = s->coded_samples[VAR_6][VAR_4];

                VAR_12 += sample*sample;

                VAR_11 += fabs(sample);

            }

        }



        VAR_12 = sqrt(VAR_12/(s->channels*s->block_align));

        VAR_11 = M_SQRT2*VAR_11/(s->channels*s->block_align);



        

        



        if (VAR_12 > VAR_11)

            VAR_12 += (VAR_12-VAR_11)*RATE_VARIATION;



        VAR_7 = (int)(BASE_QUANT*s->quantization*VAR_12/SAMPLE_FACTOR);





        VAR_7 = av_clip(VAR_7, 1, 65534);



        put_symbol(&c, state, VAR_7, 0, NULL, NULL);



        VAR_7 *= SAMPLE_FACTOR;

    }



    

    for (VAR_6 = 0; VAR_6 < s->channels; VAR_6++)

    {

        if (!s->lossless)

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

                s->coded_samples[VAR_6][VAR_4] = ROUNDED_DIV(s->coded_samples[VAR_6][VAR_4], VAR_7);



        if ((VAR_9 = intlist_write(&c, state, s->coded_samples[VAR_6], s->block_align, 1)) < 0)

            return VAR_9;

    }







    VAR_1->size = ff_rac_terminate(&c);

    *VAR_3 = 1;

    return 0;



}

static int FUNC_0(AVCodecContext *VAR_0, AVPacket *VAR_1,
const AVFrame *VAR_2, int *VAR_3)
{ SonicContext *s = VAR_0->priv_data; RangeCoder c; int VAR_4, VAR_5, VAR_6, VAR_7 = 0, VAR_8 = 0; int VAR_9; const short *VAR_10 = (const int16_t*)VAR_2->data[0]; uint8_t state[32]; if ((VAR_9 = ff_alloc_packet2(VAR_0, VAR_1, s->frame_size * 5 + 1000)) < 0)
return VAR_9; ff_init_range_encoder(&c, VAR_1->data, VAR_1->size); ff_build_rac_states(&c, 0.05*(1LL<<32), 256-8); memset(state, 128, sizeof(state)); for (VAR_4 = 0; VAR_4 < s->frame_size; VAR_4++) s->int_samples[VAR_4] = VAR_10[VAR_4]; if (!s->lossless)
for (VAR_4 = 0; VAR_4 < s->frame_size; VAR_4++) s->int_samples[VAR_4] = s->int_samples[VAR_4] << SAMPLE_SHIFT; switch(s->decorrelation)
{ case MID_SIDE:
for (VAR_4 = 0; VAR_4 < s->frame_size; VAR_4 += s->channels) { s->int_samples[VAR_4] += s->int_samples[VAR_4+1]; s->int_samples[VAR_4+1] -= shift(s->int_samples[VAR_4], 1); } break; case LEFT_SIDE:
for (VAR_4 = 0; VAR_4 < s->frame_size; VAR_4 += s->channels) s->int_samples[VAR_4+1] -= s->int_samples[VAR_4]; break; case RIGHT_SIDE:
for (VAR_4 = 0; VAR_4 < s->frame_size; VAR_4 += s->channels) s->int_samples[VAR_4] -= s->int_samples[VAR_4+1]; break; } memset(s->window, 0, 4* s->window_size); for (VAR_4 = 0; VAR_4 < s->tail_size; VAR_4++) s->window[VAR_8++] = s->tail[VAR_4]; for (VAR_4 = 0; VAR_4 < s->frame_size; VAR_4++) s->window[VAR_8++] = s->int_samples[VAR_4]; for (VAR_4 = 0; VAR_4 < s->tail_size; VAR_4++) s->window[VAR_8++] = 0; for (VAR_4 = 0; VAR_4 < s->tail_size; VAR_4++) s->tail[VAR_4] = s->int_samples[s->frame_size - s->tail_size + VAR_4]; modified_levinson_durbin(s->window, s->window_size,
s->predictor_k, s->num_taps, s->channels, s->tap_quant); if ((VAR_9 = intlist_write(&c, state, s->predictor_k, s->num_taps, 0)) < 0)
return VAR_9; for (VAR_6 = 0; VAR_6 < s->channels; VAR_6++) { VAR_8 = s->tail_size+VAR_6; for (VAR_4 = 0; VAR_4 < s->block_align; VAR_4++) { int sum = 0; for (VAR_5 = 0; VAR_5 < s->downsampling; VAR_5++, VAR_8 += s->channels) sum += s->window[VAR_8]; s->coded_samples[VAR_6][VAR_4] = sum; } } if (!s->lossless)
{ double VAR_11 = 0.0, VAR_12 = 0.0; for (VAR_6 = 0; VAR_6 < s->channels; VAR_6++) { for (VAR_4 = 0; VAR_4 < s->block_align; VAR_4++) { double sample = s->coded_samples[VAR_6][VAR_4]; VAR_12 += sample*sample; VAR_11 += fabs(sample); } } VAR_12 = sqrt(VAR_12/(s->channels*s->block_align)); VAR_11 = M_SQRT2*VAR_11/(s->channels*s->block_align); if (VAR_12 > VAR_11)
VAR_12 += (VAR_12-VAR_11)*RATE_VARIATION; VAR_7 = (int)(BASE_QUANT*s->quantization*VAR_12/SAMPLE_FACTOR); VAR_7 = av_clip(VAR_7, 1, 65534); put_symbol(&c, state, VAR_7, 0, NULL, NULL); VAR_7 *= SAMPLE_FACTOR; } for (VAR_6 = 0; VAR_6 < s->channels; VAR_6++) { if (!s->lossless)
for (VAR_4 = 0; VAR_4 < s->block_align; VAR_4++) s->coded_samples[VAR_6][VAR_4] = ROUNDED_DIV(s->coded_samples[VAR_6][VAR_4], VAR_7); if ((VAR_9 = intlist_write(&c, state, s->coded_samples[VAR_6], s->block_align, 1)) < 0)
return VAR_9; } VAR_1->size = ff_rac_terminate(&c); *VAR_3 = 1; return 0; }