static int ratecontrol_1pass(SnowContext *s, AVFrame *pict)

{

    /* estimate the frame's complexity as a sum of weighted dwt coefs.

     * FIXME we know exact mv bits at this point,

     * but ratecontrol isn't set up to include them. */

    uint32_t coef_sum= 0;

    int level, orientation, delta_qlog;



    for(level=0; level<s->spatial_decomposition_count; level++){

        for(orientation=level ? 1 : 0; orientation<4; orientation++){

            SubBand *b= &s->plane[0].band[level][orientation];

            DWTELEM *buf= b->buf;

            const int w= b->width;

            const int h= b->height;

            const int stride= b->stride;

            const int qlog= clip(2*QROOT + b->qlog, 0, QROOT*16);

            const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);

            const int qdiv= (1<<16)/qmul;

            int x, y;

            if(orientation==0)

                decorrelate(s, b, buf, stride, 1, 0);

            for(y=0; y<h; y++)

                for(x=0; x<w; x++)

                    coef_sum+= abs(buf[x+y*stride]) * qdiv >> 16;

            if(orientation==0)

                correlate(s, b, buf, stride, 1, 0);

        }

    }



    /* ugly, ratecontrol just takes a sqrt again */

    coef_sum = (uint64_t)coef_sum * coef_sum >> 16;

    assert(coef_sum < INT_MAX);



    if(pict->pict_type == I_TYPE){

        s->m.current_picture.mb_var_sum= coef_sum;

        s->m.current_picture.mc_mb_var_sum= 0;

    }else{

        s->m.current_picture.mc_mb_var_sum= coef_sum;

        s->m.current_picture.mb_var_sum= 0;

    }



    pict->quality= ff_rate_estimate_qscale(&s->m, 1);

    if (pict->quality < 0)

        return -1;

    s->lambda= pict->quality * 3/2;

    delta_qlog= qscale2qlog(pict->quality) - s->qlog;

    s->qlog+= delta_qlog;

    return delta_qlog;

}

static int ratecontrol_1pass(SnowContext *s, AVFrame *pict)

{

    





    uint32_t coef_sum= 0;

    int level, orientation, delta_qlog;



    for(level=0; level<s->spatial_decomposition_count; level++){

        for(orientation=level ? 1 : 0; orientation<4; orientation++){

            SubBand *b= &s->plane[0].band[level][orientation];

            DWTELEM *buf= b->buf;

            const int w= b->width;

            const int h= b->height;

            const int stride= b->stride;

            const int qlog= clip(2*QROOT + b->qlog, 0, QROOT*16);

            const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);

            const int qdiv= (1<<16)/qmul;

            int x, y;

            if(orientation==0)

                decorrelate(s, b, buf, stride, 1, 0);

            for(y=0; y<h; y++)

                for(x=0; x<w; x++)

                    coef_sum+= abs(buf[x+y*stride]) * qdiv >> 16;

            if(orientation==0)

                correlate(s, b, buf, stride, 1, 0);

        }

    }



    

    coef_sum = (uint64_t)coef_sum * coef_sum >> 16;

    assert(coef_sum < INT_MAX);



    if(pict->pict_type == I_TYPE){

        s->m.current_picture.mb_var_sum= coef_sum;

        s->m.current_picture.mc_mb_var_sum= 0;

    }else{

        s->m.current_picture.mc_mb_var_sum= coef_sum;

        s->m.current_picture.mb_var_sum= 0;

    }



    pict->quality= ff_rate_estimate_qscale(&s->m, 1);

    if (pict->quality < 0)

        return -1;

    s->lambda= pict->quality * 3/2;

    delta_qlog= qscale2qlog(pict->quality) - s->qlog;

    s->qlog+= delta_qlog;

    return delta_qlog;

}

static int FUNC_0(SnowContext *VAR_0, AVFrame *VAR_1)

{

    





    uint32_t coef_sum= 0;

    int VAR_2, VAR_3, VAR_4;



    for(VAR_2=0; VAR_2<VAR_0->spatial_decomposition_count; VAR_2++){

        for(VAR_3=VAR_2 ? 1 : 0; VAR_3<4; VAR_3++){

            SubBand *b= &VAR_0->plane[0].band[VAR_2][VAR_3];

            DWTELEM *buf= b->buf;

            const int w= b->width;

            const int h= b->height;

            const int stride= b->stride;

            const int qlog= clip(2*QROOT + b->qlog, 0, QROOT*16);

            const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);

            const int qdiv= (1<<16)/qmul;

            int x, y;

            if(VAR_3==0)

                decorrelate(VAR_0, b, buf, stride, 1, 0);

            for(y=0; y<h; y++)

                for(x=0; x<w; x++)

                    coef_sum+= abs(buf[x+y*stride]) * qdiv >> 16;

            if(VAR_3==0)

                correlate(VAR_0, b, buf, stride, 1, 0);

        }

    }



    

    coef_sum = (uint64_t)coef_sum * coef_sum >> 16;

    assert(coef_sum < INT_MAX);



    if(VAR_1->pict_type == I_TYPE){

        VAR_0->m.current_picture.mb_var_sum= coef_sum;

        VAR_0->m.current_picture.mc_mb_var_sum= 0;

    }else{

        VAR_0->m.current_picture.mc_mb_var_sum= coef_sum;

        VAR_0->m.current_picture.mb_var_sum= 0;

    }



    VAR_1->quality= ff_rate_estimate_qscale(&VAR_0->m, 1);

    if (VAR_1->quality < 0)

        return -1;

    VAR_0->lambda= VAR_1->quality * 3/2;

    VAR_4= qscale2qlog(VAR_1->quality) - VAR_0->qlog;

    VAR_0->qlog+= VAR_4;

    return VAR_4;

}

static int FUNC_0(SnowContext *VAR_0, AVFrame *VAR_1)
{ uint32_t coef_sum= 0; int VAR_2, VAR_3, VAR_4; for(VAR_2=0; VAR_2<VAR_0->spatial_decomposition_count; VAR_2++){ for(VAR_3=VAR_2 ? 1 : 0; VAR_3<4; VAR_3++){ SubBand *b= &VAR_0->plane[0].band[VAR_2][VAR_3]; DWTELEM *buf= b->buf; const int w= b->width; const int h= b->height; const int stride= b->stride; const int qlog= clip(2*QROOT + b->qlog, 0, QROOT*16); const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT); const int qdiv= (1<<16)/qmul; int x, y; if(VAR_3==0)
decorrelate(VAR_0, b, buf, stride, 1, 0); for(y=0; y<h; y++) for(x=0; x<w; x++) coef_sum+= abs(buf[x+y*stride]) * qdiv >> 16; if(VAR_3==0)
correlate(VAR_0, b, buf, stride, 1, 0); } } coef_sum = (uint64_t)coef_sum * coef_sum >> 16; assert(coef_sum < INT_MAX); if(VAR_1->pict_type == I_TYPE){ VAR_0->m.current_picture.mb_var_sum= coef_sum; VAR_0->m.current_picture.mc_mb_var_sum= 0; }else{ VAR_0->m.current_picture.mc_mb_var_sum= coef_sum; VAR_0->m.current_picture.mb_var_sum= 0; } VAR_1->quality= ff_rate_estimate_qscale(&VAR_0->m, 1); if (VAR_1->quality < 0)
return -1; VAR_0->lambda= VAR_1->quality * 3/2; VAR_4= qscale2qlog(VAR_1->quality) - VAR_0->qlog; VAR_0->qlog+= VAR_4; return VAR_4; }