static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){

    SnowContext *s = avctx->priv_data;

    CABACContext * const c= &s->c;

    AVFrame *pict = data;

    const int width= s->avctx->width;

    const int height= s->avctx->height;

    int used_count= 0;

    int log2_threshold, level, orientation, plane_index, i;



    ff_init_cabac_encoder(c, buf, buf_size);

    ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);

    

    s->input_picture = *pict;



    memset(s->header_state, 0, sizeof(s->header_state));



    s->keyframe=avctx->gop_size==0 || avctx->frame_number % avctx->gop_size == 0;

    pict->pict_type= s->keyframe ? FF_I_TYPE : FF_P_TYPE;

    

    if(pict->quality){

        s->qlog= rint(QROOT*log(pict->quality / (float)FF_QP2LAMBDA)/log(2));

        //<64 >60

        s->qlog += 61;

    }else{

        s->qlog= LOSSLESS_QLOG;

    }



    for(i=0; i<s->mb_band.stride * s->mb_band.height; i++){

        s->mb_band.buf[i]= s->keyframe;

    }

    

    frame_start(s);



    if(pict->pict_type == P_TYPE){

        int block_width = (width +15)>>4;

        int block_height= (height+15)>>4;

        int stride= s->current_picture.linesize[0];

        uint8_t *src_plane= s->input_picture.data[0];

        int src_stride= s->input_picture.linesize[0];

        int x,y;

        

        assert(s->current_picture.data[0]);

        assert(s->last_picture.data[0]);

     

        s->m.avctx= s->avctx;

        s->m.current_picture.data[0]= s->current_picture.data[0];

        s->m.   last_picture.data[0]= s->   last_picture.data[0];

        s->m.    new_picture.data[0]= s->  input_picture.data[0];

        s->m.current_picture_ptr= &s->m.current_picture;

        s->m.   last_picture_ptr= &s->m.   last_picture;

        s->m.linesize=

        s->m.   last_picture.linesize[0]=

        s->m.    new_picture.linesize[0]=

        s->m.current_picture.linesize[0]= stride;

        s->m.width = width;

        s->m.height= height;

        s->m.mb_width = block_width;

        s->m.mb_height= block_height;

        s->m.mb_stride=   s->m.mb_width+1;

        s->m.b8_stride= 2*s->m.mb_width+1;

        s->m.f_code=1;

        s->m.pict_type= pict->pict_type;

        s->m.me_method= s->avctx->me_method;

        s->m.me.scene_change_score=0;

        s->m.flags= s->avctx->flags;

        s->m.quarter_sample= (s->avctx->flags & CODEC_FLAG_QPEL)!=0;

        s->m.out_format= FMT_H263;

        s->m.unrestricted_mv= 1;



        s->m.lambda= pict->quality * 3/2; //FIXME bug somewhere else

        s->m.qscale= (s->m.lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);

        s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;



        if(!s->motion_val8){

            s->motion_val8 = av_mallocz(s->m.b8_stride*block_height*2*2*sizeof(int16_t));

            s->motion_val16= av_mallocz(s->m.mb_stride*block_height*2*sizeof(int16_t));

        }

        

        s->m.mb_type= s->mb_type;

        

        //dummies, to avoid segfaults

        s->m.current_picture.mb_mean  = s->mb_mean;

        s->m.current_picture.mb_var   = (int16_t*)s->dummy;

        s->m.current_picture.mc_mb_var= (int16_t*)s->dummy;

        s->m.current_picture.mb_type  = s->dummy;

        

        s->m.current_picture.motion_val[0]= s->motion_val8;

        s->m.p_mv_table= s->motion_val16;

        s->m.dsp= s->dsp; //move

        ff_init_me(&s->m);

    

        

        s->m.me.pre_pass=1;

        s->m.me.dia_size= s->avctx->pre_dia_size;

        s->m.first_slice_line=1;

        for(y= block_height-1; y >= 0; y--) {

            uint8_t src[stride*16];



            s->m.new_picture.data[0]= src - y*16*stride; //ugly

            s->m.mb_y= y;

            for(i=0; i<16 && i + 16*y<height; i++){

                memcpy(&src[i*stride], &src_plane[(i+16*y)*src_stride], width);

                for(x=width; x<16*block_width; x++)

                    src[i*stride+x]= src[i*stride+x-1];

            }

            for(; i<16 && i + 16*y<16*block_height; i++)

                memcpy(&src[i*stride], &src[(i-1)*stride], 16*block_width);



            for(x=block_width-1; x >=0 ;x--) {

                s->m.mb_x= x;

                ff_init_block_index(&s->m);

                ff_update_block_index(&s->m);

                ff_pre_estimate_p_frame_motion(&s->m, x, y);

            }

            s->m.first_slice_line=0;

        }        

        s->m.me.pre_pass=0;

        

        

        s->m.me.dia_size= s->avctx->dia_size;

        s->m.first_slice_line=1;

        for (y = 0; y < block_height; y++) {

            uint8_t src[stride*16];



            s->m.new_picture.data[0]= src - y*16*stride; //ugly

            s->m.mb_y= y;

            

            assert(width <= stride);

            assert(width <= 16*block_width);

    

            for(i=0; i<16 && i + 16*y<height; i++){

                memcpy(&src[i*stride], &src_plane[(i+16*y)*src_stride], width);

                for(x=width; x<16*block_width; x++)

                    src[i*stride+x]= src[i*stride+x-1];

            }

            for(; i<16 && i + 16*y<16*block_height; i++)

                memcpy(&src[i*stride], &src[(i-1)*stride], 16*block_width);

    

            for (x = 0; x < block_width; x++) {

                int mb_xy= x + y*(s->mb_band.stride);

                s->m.mb_x= x;

                ff_init_block_index(&s->m);

                ff_update_block_index(&s->m);

                

                ff_estimate_p_frame_motion(&s->m, x, y);

                

                s->mb_band   .buf[mb_xy]= (s->m.mb_type[x + y*s->m.mb_stride]&CANDIDATE_MB_TYPE_INTER)

                 ? 0 : 2;

                s->mv_band[0].buf[mb_xy]= s->motion_val16[x + y*s->m.mb_stride][0];

                s->mv_band[1].buf[mb_xy]= s->motion_val16[x + y*s->m.mb_stride][1];

                

                if(s->mb_band   .buf[x + y*(s->mb_band.stride)]==2 && 0){

                    int dc0=128, dc1=128, dc, dc2, dir;

                    int offset= (s->avctx->flags & CODEC_FLAG_QPEL) ? 64 : 32;

                    

                    dc       =s->mb_mean[x +  y   *s->m.mb_stride    ];

                    if(x) dc0=s->mb_mean[x +  y   *s->m.mb_stride - 1];

                    if(y) dc1=s->mb_mean[x + (y-1)*s->m.mb_stride    ];

                    dc2= (dc0+dc1)>>1;

#if 0

                    if     (ABS(dc0 - dc) < ABS(dc1 - dc) && ABS(dc0 - dc) < ABS(dc2 - dc))

                        dir= 1;

                    else if(ABS(dc0 - dc) >=ABS(dc1 - dc) && ABS(dc1 - dc) < ABS(dc2 - dc))

                        dir=-1;

                    else

                        dir=0;

#endif                    

                    if(ABS(dc0 - dc) < ABS(dc1 - dc) && x){

                        s->mv_band[0].buf[mb_xy]= s->mv_band[0].buf[x + y*(s->mb_band.stride)-1] - offset;

                        s->mv_band[1].buf[mb_xy]= s->mv_band[1].buf[x + y*(s->mb_band.stride)-1];

                        s->mb_mean[x +  y   *s->m.mb_stride    ]= dc0;

                    }else if(y){

                        s->mv_band[0].buf[mb_xy]= s->mv_band[0].buf[x + (y-1)*(s->mb_band.stride)];

                        s->mv_band[1].buf[mb_xy]= s->mv_band[1].buf[x + (y-1)*(s->mb_band.stride)] - offset;

                        s->mb_mean[x +  y   *s->m.mb_stride    ]= dc1;

                    }

                }

//                s->mb_band   .buf[x + y*(s->mb_band.stride)]=1; //FIXME intra only test

            }

            s->m.first_slice_line=0;

        }

        assert(s->m.pict_type == P_TYPE);

        if(s->m.me.scene_change_score > s->avctx->scenechange_threshold){

            s->m.pict_type= 

            pict->pict_type =I_TYPE;

            for(i=0; i<s->mb_band.stride * s->mb_band.height; i++){

                s->mb_band.buf[i]= 1;

                s->mv_band[0].buf[i]=

                s->mv_band[1].buf[i]= 0;

            }

    //printf("Scene change detected, encoding as I Frame %d %d\n", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);

        }        

    }

        

    s->m.first_slice_line=1;

    

    s->qbias= pict->pict_type == P_TYPE ? 2 : 0;



    encode_header(s);

    

    decorrelate(s, &s->mb_band   , s->mb_band   .buf, s->mb_band   .stride, 0, 1);

    decorrelate(s, &s->mv_band[0], s->mv_band[0].buf, s->mv_band[0].stride, 0, 1);

    decorrelate(s, &s->mv_band[1], s->mv_band[1].buf, s->mv_band[1].stride, 0 ,1);

    encode_subband(s, &s->mb_band   , s->mb_band   .buf, NULL, s->mb_band   .stride, 0);

    encode_subband(s, &s->mv_band[0], s->mv_band[0].buf, NULL, s->mv_band[0].stride, 0);

    encode_subband(s, &s->mv_band[1], s->mv_band[1].buf, NULL, s->mv_band[1].stride, 0);

    

//FIXME avoid this

    correlate(s, &s->mb_band   , s->mb_band   .buf, s->mb_band   .stride, 1, 1);

    correlate(s, &s->mv_band[0], s->mv_band[0].buf, s->mv_band[0].stride, 1, 1);

    correlate(s, &s->mv_band[1], s->mv_band[1].buf, s->mv_band[1].stride, 1, 1);

    

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

        Plane *p= &s->plane[plane_index];

        int w= p->width;

        int h= p->height;

        int x, y;

        int bits= put_bits_count(&s->c.pb);



        //FIXME optimize

     if(pict->data[plane_index]) //FIXME gray hack

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

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

                s->spatial_dwt_buffer[y*w + x]= pict->data[plane_index][y*pict->linesize[plane_index] + x]<<8;

            }

        }

        predict_plane(s, s->spatial_dwt_buffer, plane_index, 0);

        if(s->qlog == LOSSLESS_QLOG){

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

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

                    s->spatial_dwt_buffer[y*w + x]= (s->spatial_dwt_buffer[y*w + x] + 127)>>8;

                }

            }

        }

 

        ff_spatial_dwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);



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

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

                SubBand *b= &p->band[level][orientation];

                

                quantize(s, b, b->buf, b->stride, s->qbias);

                if(orientation==0)

                    decorrelate(s, b, b->buf, b->stride, pict->pict_type == P_TYPE, 0);

                encode_subband(s, b, b->buf, b->parent ? b->parent->buf : NULL, b->stride, orientation);

                assert(b->parent==NULL || b->parent->stride == b->stride*2);

                if(orientation==0)

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

            }

        }

//        av_log(NULL, AV_LOG_DEBUG, "plane:%d bits:%d\n", plane_index, put_bits_count(&s->c.pb) - bits);



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

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

                SubBand *b= &p->band[level][orientation];



                dequantize(s, b, b->buf, b->stride);

            }

        }



        ff_spatial_idwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);

        if(s->qlog == LOSSLESS_QLOG){

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

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

                    s->spatial_dwt_buffer[y*w + x]<<=8;

                }

            }

        }

        predict_plane(s, s->spatial_dwt_buffer, plane_index, 1);

        //FIXME optimize

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

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

                int v= (s->spatial_dwt_buffer[y*w + x]+128)>>8;

                if(v&(~255)) v= ~(v>>31);

                s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x]= v;

            }

        }

        if(s->avctx->flags&CODEC_FLAG_PSNR){

            int64_t error= 0;

            

    if(pict->data[plane_index]) //FIXME gray hack

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

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

                    int d= s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x] - pict->data[plane_index][y*pict->linesize[plane_index] + x];

                    error += d*d;

                }

            }

            s->avctx->error[plane_index] += error;

            s->avctx->error[3] += error;

        }

    }



    if(s->last_picture.data[0])

        avctx->release_buffer(avctx, &s->last_picture);



    emms_c();

    

    return put_cabac_terminate(c, 1);

}

static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){

    SnowContext *s = avctx->priv_data;

    CABACContext * const c= &s->c;

    AVFrame *pict = data;

    const int width= s->avctx->width;

    const int height= s->avctx->height;

    int used_count= 0;

    int log2_threshold, level, orientation, plane_index, i;



    ff_init_cabac_encoder(c, buf, buf_size);

    ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);

    

    s->input_picture = *pict;



    memset(s->header_state, 0, sizeof(s->header_state));



    s->keyframe=avctx->gop_size==0 || avctx->frame_number % avctx->gop_size == 0;

    pict->pict_type= s->keyframe ? FF_I_TYPE : FF_P_TYPE;

    

    if(pict->quality){

        s->qlog= rint(QROOT*log(pict->quality / (float)FF_QP2LAMBDA)/log(2));

        

        s->qlog += 61;

    }else{

        s->qlog= LOSSLESS_QLOG;

    }



    for(i=0; i<s->mb_band.stride * s->mb_band.height; i++){

        s->mb_band.buf[i]= s->keyframe;

    }

    

    frame_start(s);



    if(pict->pict_type == P_TYPE){

        int block_width = (width +15)>>4;

        int block_height= (height+15)>>4;

        int stride= s->current_picture.linesize[0];

        uint8_t *src_plane= s->input_picture.data[0];

        int src_stride= s->input_picture.linesize[0];

        int x,y;

        

        assert(s->current_picture.data[0]);

        assert(s->last_picture.data[0]);

     

        s->m.avctx= s->avctx;

        s->m.current_picture.data[0]= s->current_picture.data[0];

        s->m.   last_picture.data[0]= s->   last_picture.data[0];

        s->m.    new_picture.data[0]= s->  input_picture.data[0];

        s->m.current_picture_ptr= &s->m.current_picture;

        s->m.   last_picture_ptr= &s->m.   last_picture;

        s->m.linesize=

        s->m.   last_picture.linesize[0]=

        s->m.    new_picture.linesize[0]=

        s->m.current_picture.linesize[0]= stride;

        s->m.width = width;

        s->m.height= height;

        s->m.mb_width = block_width;

        s->m.mb_height= block_height;

        s->m.mb_stride=   s->m.mb_width+1;

        s->m.b8_stride= 2*s->m.mb_width+1;

        s->m.f_code=1;

        s->m.pict_type= pict->pict_type;

        s->m.me_method= s->avctx->me_method;

        s->m.me.scene_change_score=0;

        s->m.flags= s->avctx->flags;

        s->m.quarter_sample= (s->avctx->flags & CODEC_FLAG_QPEL)!=0;

        s->m.out_format= FMT_H263;

        s->m.unrestricted_mv= 1;



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

        s->m.qscale= (s->m.lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);

        s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;



        if(!s->motion_val8){

            s->motion_val8 = av_mallocz(s->m.b8_stride*block_height*2*2*sizeof(int16_t));

            s->motion_val16= av_mallocz(s->m.mb_stride*block_height*2*sizeof(int16_t));

        }

        

        s->m.mb_type= s->mb_type;

        

        

        s->m.current_picture.mb_mean  = s->mb_mean;

        s->m.current_picture.mb_var   = (int16_t*)s->dummy;

        s->m.current_picture.mc_mb_var= (int16_t*)s->dummy;

        s->m.current_picture.mb_type  = s->dummy;

        

        s->m.current_picture.motion_val[0]= s->motion_val8;

        s->m.p_mv_table= s->motion_val16;

        s->m.dsp= s->dsp; 

        ff_init_me(&s->m);

    

        

        s->m.me.pre_pass=1;

        s->m.me.dia_size= s->avctx->pre_dia_size;

        s->m.first_slice_line=1;

        for(y= block_height-1; y >= 0; y--) {

            uint8_t src[stride*16];



            s->m.new_picture.data[0]= src - y*16*stride; 

            s->m.mb_y= y;

            for(i=0; i<16 && i + 16*y<height; i++){

                memcpy(&src[i*stride], &src_plane[(i+16*y)*src_stride], width);

                for(x=width; x<16*block_width; x++)

                    src[i*stride+x]= src[i*stride+x-1];

            }

            for(; i<16 && i + 16*y<16*block_height; i++)

                memcpy(&src[i*stride], &src[(i-1)*stride], 16*block_width);



            for(x=block_width-1; x >=0 ;x--) {

                s->m.mb_x= x;

                ff_init_block_index(&s->m);

                ff_update_block_index(&s->m);

                ff_pre_estimate_p_frame_motion(&s->m, x, y);

            }

            s->m.first_slice_line=0;

        }        

        s->m.me.pre_pass=0;

        

        

        s->m.me.dia_size= s->avctx->dia_size;

        s->m.first_slice_line=1;

        for (y = 0; y < block_height; y++) {

            uint8_t src[stride*16];



            s->m.new_picture.data[0]= src - y*16*stride; 

            s->m.mb_y= y;

            

            assert(width <= stride);

            assert(width <= 16*block_width);

    

            for(i=0; i<16 && i + 16*y<height; i++){

                memcpy(&src[i*stride], &src_plane[(i+16*y)*src_stride], width);

                for(x=width; x<16*block_width; x++)

                    src[i*stride+x]= src[i*stride+x-1];

            }

            for(; i<16 && i + 16*y<16*block_height; i++)

                memcpy(&src[i*stride], &src[(i-1)*stride], 16*block_width);

    

            for (x = 0; x < block_width; x++) {

                int mb_xy= x + y*(s->mb_band.stride);

                s->m.mb_x= x;

                ff_init_block_index(&s->m);

                ff_update_block_index(&s->m);

                

                ff_estimate_p_frame_motion(&s->m, x, y);

                

                s->mb_band   .buf[mb_xy]= (s->m.mb_type[x + y*s->m.mb_stride]&CANDIDATE_MB_TYPE_INTER)

                 ? 0 : 2;

                s->mv_band[0].buf[mb_xy]= s->motion_val16[x + y*s->m.mb_stride][0];

                s->mv_band[1].buf[mb_xy]= s->motion_val16[x + y*s->m.mb_stride][1];

                

                if(s->mb_band   .buf[x + y*(s->mb_band.stride)]==2 && 0){

                    int dc0=128, dc1=128, dc, dc2, dir;

                    int offset= (s->avctx->flags & CODEC_FLAG_QPEL) ? 64 : 32;

                    

                    dc       =s->mb_mean[x +  y   *s->m.mb_stride    ];

                    if(x) dc0=s->mb_mean[x +  y   *s->m.mb_stride - 1];

                    if(y) dc1=s->mb_mean[x + (y-1)*s->m.mb_stride    ];

                    dc2= (dc0+dc1)>>1;

#if 0

                    if     (ABS(dc0 - dc) < ABS(dc1 - dc) && ABS(dc0 - dc) < ABS(dc2 - dc))

                        dir= 1;

                    else if(ABS(dc0 - dc) >=ABS(dc1 - dc) && ABS(dc1 - dc) < ABS(dc2 - dc))

                        dir=-1;

                    else

                        dir=0;

#endif                    

                    if(ABS(dc0 - dc) < ABS(dc1 - dc) && x){

                        s->mv_band[0].buf[mb_xy]= s->mv_band[0].buf[x + y*(s->mb_band.stride)-1] - offset;

                        s->mv_band[1].buf[mb_xy]= s->mv_band[1].buf[x + y*(s->mb_band.stride)-1];

                        s->mb_mean[x +  y   *s->m.mb_stride    ]= dc0;

                    }else if(y){

                        s->mv_band[0].buf[mb_xy]= s->mv_band[0].buf[x + (y-1)*(s->mb_band.stride)];

                        s->mv_band[1].buf[mb_xy]= s->mv_band[1].buf[x + (y-1)*(s->mb_band.stride)] - offset;

                        s->mb_mean[x +  y   *s->m.mb_stride    ]= dc1;

                    }

                }



            }

            s->m.first_slice_line=0;

        }

        assert(s->m.pict_type == P_TYPE);

        if(s->m.me.scene_change_score > s->avctx->scenechange_threshold){

            s->m.pict_type= 

            pict->pict_type =I_TYPE;

            for(i=0; i<s->mb_band.stride * s->mb_band.height; i++){

                s->mb_band.buf[i]= 1;

                s->mv_band[0].buf[i]=

                s->mv_band[1].buf[i]= 0;

            }

    

        }        

    }

        

    s->m.first_slice_line=1;

    

    s->qbias= pict->pict_type == P_TYPE ? 2 : 0;



    encode_header(s);

    

    decorrelate(s, &s->mb_band   , s->mb_band   .buf, s->mb_band   .stride, 0, 1);

    decorrelate(s, &s->mv_band[0], s->mv_band[0].buf, s->mv_band[0].stride, 0, 1);

    decorrelate(s, &s->mv_band[1], s->mv_band[1].buf, s->mv_band[1].stride, 0 ,1);

    encode_subband(s, &s->mb_band   , s->mb_band   .buf, NULL, s->mb_band   .stride, 0);

    encode_subband(s, &s->mv_band[0], s->mv_band[0].buf, NULL, s->mv_band[0].stride, 0);

    encode_subband(s, &s->mv_band[1], s->mv_band[1].buf, NULL, s->mv_band[1].stride, 0);

    



    correlate(s, &s->mb_band   , s->mb_band   .buf, s->mb_band   .stride, 1, 1);

    correlate(s, &s->mv_band[0], s->mv_band[0].buf, s->mv_band[0].stride, 1, 1);

    correlate(s, &s->mv_band[1], s->mv_band[1].buf, s->mv_band[1].stride, 1, 1);

    

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

        Plane *p= &s->plane[plane_index];

        int w= p->width;

        int h= p->height;

        int x, y;

        int bits= put_bits_count(&s->c.pb);



        

     if(pict->data[plane_index]) 

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

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

                s->spatial_dwt_buffer[y*w + x]= pict->data[plane_index][y*pict->linesize[plane_index] + x]<<8;

            }

        }

        predict_plane(s, s->spatial_dwt_buffer, plane_index, 0);

        if(s->qlog == LOSSLESS_QLOG){

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

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

                    s->spatial_dwt_buffer[y*w + x]= (s->spatial_dwt_buffer[y*w + x] + 127)>>8;

                }

            }

        }

 

        ff_spatial_dwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);



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

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

                SubBand *b= &p->band[level][orientation];

                

                quantize(s, b, b->buf, b->stride, s->qbias);

                if(orientation==0)

                    decorrelate(s, b, b->buf, b->stride, pict->pict_type == P_TYPE, 0);

                encode_subband(s, b, b->buf, b->parent ? b->parent->buf : NULL, b->stride, orientation);

                assert(b->parent==NULL || b->parent->stride == b->stride*2);

                if(orientation==0)

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

            }

        }





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

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

                SubBand *b= &p->band[level][orientation];



                dequantize(s, b, b->buf, b->stride);

            }

        }



        ff_spatial_idwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);

        if(s->qlog == LOSSLESS_QLOG){

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

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

                    s->spatial_dwt_buffer[y*w + x]<<=8;

                }

            }

        }

        predict_plane(s, s->spatial_dwt_buffer, plane_index, 1);

        

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

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

                int v= (s->spatial_dwt_buffer[y*w + x]+128)>>8;

                if(v&(~255)) v= ~(v>>31);

                s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x]= v;

            }

        }

        if(s->avctx->flags&CODEC_FLAG_PSNR){

            int64_t error= 0;

            

    if(pict->data[plane_index]) 

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

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

                    int d= s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x] - pict->data[plane_index][y*pict->linesize[plane_index] + x];

                    error += d*d;

                }

            }

            s->avctx->error[plane_index] += error;

            s->avctx->error[3] += error;

        }

    }



    if(s->last_picture.data[0])

        avctx->release_buffer(avctx, &s->last_picture);



    emms_c();

    

    return put_cabac_terminate(c, 1);

}

static int FUNC_0(AVCodecContext *VAR_0, unsigned char *VAR_1, int VAR_2, void *VAR_3){

    SnowContext *s = VAR_0->priv_data;

    CABACContext * const c= &s->c;

    AVFrame *pict = VAR_3;

    const int VAR_4= s->VAR_0->VAR_4;

    const int VAR_5= s->VAR_0->VAR_5;

    int VAR_6= 0;

    int VAR_7, VAR_8, VAR_9, VAR_10, VAR_11;



    ff_init_cabac_encoder(c, VAR_1, VAR_2);

    ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);

    

    s->input_picture = *pict;



    memset(s->header_state, 0, sizeof(s->header_state));



    s->keyframe=VAR_0->gop_size==0 || VAR_0->frame_number % VAR_0->gop_size == 0;

    pict->pict_type= s->keyframe ? FF_I_TYPE : FF_P_TYPE;

    

    if(pict->quality){

        s->qlog= rint(QROOT*log(pict->quality / (float)FF_QP2LAMBDA)/log(2));

        

        s->qlog += 61;

    }else{

        s->qlog= LOSSLESS_QLOG;

    }



    for(VAR_11=0; VAR_11<s->mb_band.VAR_14 * s->mb_band.VAR_5; VAR_11++){

        s->mb_band.VAR_1[VAR_11]= s->keyframe;

    }

    

    frame_start(s);



    if(pict->pict_type == P_TYPE){

        int VAR_12 = (VAR_4 +15)>>4;

        int VAR_13= (VAR_5+15)>>4;

        int VAR_14= s->current_picture.linesize[0];

        uint8_t *src_plane= s->input_picture.VAR_3[0];

        int VAR_15= s->input_picture.linesize[0];

        int VAR_27,VAR_27;

        

        assert(s->current_picture.VAR_3[0]);

        assert(s->last_picture.VAR_3[0]);

     

        s->m.VAR_0= s->VAR_0;

        s->m.current_picture.VAR_3[0]= s->current_picture.VAR_3[0];

        s->m.   last_picture.VAR_3[0]= s->   last_picture.VAR_3[0];

        s->m.    new_picture.VAR_3[0]= s->  input_picture.VAR_3[0];

        s->m.current_picture_ptr= &s->m.current_picture;

        s->m.   last_picture_ptr= &s->m.   last_picture;

        s->m.linesize=

        s->m.   last_picture.linesize[0]=

        s->m.    new_picture.linesize[0]=

        s->m.current_picture.linesize[0]= VAR_14;

        s->m.VAR_4 = VAR_4;

        s->m.VAR_5= VAR_5;

        s->m.mb_width = VAR_12;

        s->m.mb_height= VAR_13;

        s->m.mb_stride=   s->m.mb_width+1;

        s->m.b8_stride= 2*s->m.mb_width+1;

        s->m.f_code=1;

        s->m.pict_type= pict->pict_type;

        s->m.me_method= s->VAR_0->me_method;

        s->m.me.scene_change_score=0;

        s->m.flags= s->VAR_0->flags;

        s->m.quarter_sample= (s->VAR_0->flags & CODEC_FLAG_QPEL)!=0;

        s->m.out_format= FMT_H263;

        s->m.unrestricted_mv= 1;



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

        s->m.qscale= (s->m.lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);

        s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;



        if(!s->motion_val8){

            s->motion_val8 = av_mallocz(s->m.b8_stride*VAR_13*2*2*sizeof(int16_t));

            s->motion_val16= av_mallocz(s->m.mb_stride*VAR_13*2*sizeof(int16_t));

        }

        

        s->m.mb_type= s->mb_type;

        

        

        s->m.current_picture.mb_mean  = s->mb_mean;

        s->m.current_picture.mb_var   = (int16_t*)s->dummy;

        s->m.current_picture.mc_mb_var= (int16_t*)s->dummy;

        s->m.current_picture.mb_type  = s->dummy;

        

        s->m.current_picture.motion_val[0]= s->motion_val8;

        s->m.p_mv_table= s->motion_val16;

        s->m.dsp= s->dsp; 

        ff_init_me(&s->m);

    

        

        s->m.me.pre_pass=1;

        s->m.me.dia_size= s->VAR_0->pre_dia_size;

        s->m.first_slice_line=1;

        for(VAR_27= VAR_13-1; VAR_27 >= 0; VAR_27--) {

            uint8_t src[VAR_14*16];



            s->m.new_picture.VAR_3[0]= src - VAR_27*16*VAR_14; 

            s->m.mb_y= VAR_27;

            for(VAR_11=0; VAR_11<16 && VAR_11 + 16*VAR_27<VAR_5; VAR_11++){

                memcpy(&src[VAR_11*VAR_14], &src_plane[(VAR_11+16*VAR_27)*VAR_15], VAR_4);

                for(VAR_27=VAR_4; VAR_27<16*VAR_12; VAR_27++)

                    src[VAR_11*VAR_14+VAR_27]= src[VAR_11*VAR_14+VAR_27-1];

            }

            for(; VAR_11<16 && VAR_11 + 16*VAR_27<16*VAR_13; VAR_11++)

                memcpy(&src[VAR_11*VAR_14], &src[(VAR_11-1)*VAR_14], 16*VAR_12);



            for(VAR_27=VAR_12-1; VAR_27 >=0 ;VAR_27--) {

                s->m.mb_x= VAR_27;

                ff_init_block_index(&s->m);

                ff_update_block_index(&s->m);

                ff_pre_estimate_p_frame_motion(&s->m, VAR_27, VAR_27);

            }

            s->m.first_slice_line=0;

        }        

        s->m.me.pre_pass=0;

        

        

        s->m.me.dia_size= s->VAR_0->dia_size;

        s->m.first_slice_line=1;

        for (VAR_27 = 0; VAR_27 < VAR_13; VAR_27++) {

            uint8_t src[VAR_14*16];



            s->m.new_picture.VAR_3[0]= src - VAR_27*16*VAR_14; 

            s->m.mb_y= VAR_27;

            

            assert(VAR_4 <= VAR_14);

            assert(VAR_4 <= 16*VAR_12);

    

            for(VAR_11=0; VAR_11<16 && VAR_11 + 16*VAR_27<VAR_5; VAR_11++){

                memcpy(&src[VAR_11*VAR_14], &src_plane[(VAR_11+16*VAR_27)*VAR_15], VAR_4);

                for(VAR_27=VAR_4; VAR_27<16*VAR_12; VAR_27++)

                    src[VAR_11*VAR_14+VAR_27]= src[VAR_11*VAR_14+VAR_27-1];

            }

            for(; VAR_11<16 && VAR_11 + 16*VAR_27<16*VAR_13; VAR_11++)

                memcpy(&src[VAR_11*VAR_14], &src[(VAR_11-1)*VAR_14], 16*VAR_12);

    

            for (VAR_27 = 0; VAR_27 < VAR_12; VAR_27++) {

                int VAR_18= VAR_27 + VAR_27*(s->mb_band.VAR_14);

                s->m.mb_x= VAR_27;

                ff_init_block_index(&s->m);

                ff_update_block_index(&s->m);

                

                ff_estimate_p_frame_motion(&s->m, VAR_27, VAR_27);

                

                s->mb_band   .VAR_1[VAR_18]= (s->m.mb_type[VAR_27 + VAR_27*s->m.mb_stride]&CANDIDATE_MB_TYPE_INTER)

                 ? 0 : 2;

                s->mv_band[0].VAR_1[VAR_18]= s->motion_val16[VAR_27 + VAR_27*s->m.mb_stride][0];

                s->mv_band[1].VAR_1[VAR_18]= s->motion_val16[VAR_27 + VAR_27*s->m.mb_stride][1];

                

                if(s->mb_band   .VAR_1[VAR_27 + VAR_27*(s->mb_band.VAR_14)]==2 && 0){

                    int VAR_19=128, VAR_20=128, VAR_21, VAR_22, VAR_23;

                    int VAR_24= (s->VAR_0->flags & CODEC_FLAG_QPEL) ? 64 : 32;

                    

                    VAR_21       =s->mb_mean[VAR_27 +  VAR_27   *s->m.mb_stride    ];

                    if(VAR_27) VAR_19=s->mb_mean[VAR_27 +  VAR_27   *s->m.mb_stride - 1];

                    if(VAR_27) VAR_20=s->mb_mean[VAR_27 + (VAR_27-1)*s->m.mb_stride    ];

                    VAR_22= (VAR_19+VAR_20)>>1;

#if 0

                    if     (ABS(VAR_19 - VAR_21) < ABS(VAR_20 - VAR_21) && ABS(VAR_19 - VAR_21) < ABS(VAR_22 - VAR_21))

                        VAR_23= 1;

                    else if(ABS(VAR_19 - VAR_21) >=ABS(VAR_20 - VAR_21) && ABS(VAR_20 - VAR_21) < ABS(VAR_22 - VAR_21))

                        VAR_23=-1;

                    else

                        VAR_23=0;

#endif                    

                    if(ABS(VAR_19 - VAR_21) < ABS(VAR_20 - VAR_21) && VAR_27){

                        s->mv_band[0].VAR_1[VAR_18]= s->mv_band[0].VAR_1[VAR_27 + VAR_27*(s->mb_band.VAR_14)-1] - VAR_24;

                        s->mv_band[1].VAR_1[VAR_18]= s->mv_band[1].VAR_1[VAR_27 + VAR_27*(s->mb_band.VAR_14)-1];

                        s->mb_mean[VAR_27 +  VAR_27   *s->m.mb_stride    ]= VAR_19;

                    }else if(VAR_27){

                        s->mv_band[0].VAR_1[VAR_18]= s->mv_band[0].VAR_1[VAR_27 + (VAR_27-1)*(s->mb_band.VAR_14)];

                        s->mv_band[1].VAR_1[VAR_18]= s->mv_band[1].VAR_1[VAR_27 + (VAR_27-1)*(s->mb_band.VAR_14)] - VAR_24;

                        s->mb_mean[VAR_27 +  VAR_27   *s->m.mb_stride    ]= VAR_20;

                    }

                }



            }

            s->m.first_slice_line=0;

        }

        assert(s->m.pict_type == P_TYPE);

        if(s->m.me.scene_change_score > s->VAR_0->scenechange_threshold){

            s->m.pict_type= 

            pict->pict_type =I_TYPE;

            for(VAR_11=0; VAR_11<s->mb_band.VAR_14 * s->mb_band.VAR_5; VAR_11++){

                s->mb_band.VAR_1[VAR_11]= 1;

                s->mv_band[0].VAR_1[VAR_11]=

                s->mv_band[1].VAR_1[VAR_11]= 0;

            }

    

        }        

    }

        

    s->m.first_slice_line=1;

    

    s->qbias= pict->pict_type == P_TYPE ? 2 : 0;



    encode_header(s);

    

    decorrelate(s, &s->mb_band   , s->mb_band   .VAR_1, s->mb_band   .VAR_14, 0, 1);

    decorrelate(s, &s->mv_band[0], s->mv_band[0].VAR_1, s->mv_band[0].VAR_14, 0, 1);

    decorrelate(s, &s->mv_band[1], s->mv_band[1].VAR_1, s->mv_band[1].VAR_14, 0 ,1);

    encode_subband(s, &s->mb_band   , s->mb_band   .VAR_1, NULL, s->mb_band   .VAR_14, 0);

    encode_subband(s, &s->mv_band[0], s->mv_band[0].VAR_1, NULL, s->mv_band[0].VAR_14, 0);

    encode_subband(s, &s->mv_band[1], s->mv_band[1].VAR_1, NULL, s->mv_band[1].VAR_14, 0);

    



    correlate(s, &s->mb_band   , s->mb_band   .VAR_1, s->mb_band   .VAR_14, 1, 1);

    correlate(s, &s->mv_band[0], s->mv_band[0].VAR_1, s->mv_band[0].VAR_14, 1, 1);

    correlate(s, &s->mv_band[1], s->mv_band[1].VAR_1, s->mv_band[1].VAR_14, 1, 1);

    

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

        Plane *p= &s->plane[VAR_10];

        int VAR_25= p->VAR_4;

        int VAR_26= p->VAR_5;

        int VAR_27, VAR_27;

        int VAR_27= put_bits_count(&s->c.pb);



        

     if(pict->VAR_3[VAR_10]) 

        for(VAR_27=0; VAR_27<VAR_26; VAR_27++){

            for(VAR_27=0; VAR_27<VAR_25; VAR_27++){

                s->spatial_dwt_buffer[VAR_27*VAR_25 + VAR_27]= pict->VAR_3[VAR_10][VAR_27*pict->linesize[VAR_10] + VAR_27]<<8;

            }

        }

        predict_plane(s, s->spatial_dwt_buffer, VAR_10, 0);

        if(s->qlog == LOSSLESS_QLOG){

            for(VAR_27=0; VAR_27<VAR_26; VAR_27++){

                for(VAR_27=0; VAR_27<VAR_25; VAR_27++){

                    s->spatial_dwt_buffer[VAR_27*VAR_25 + VAR_27]= (s->spatial_dwt_buffer[VAR_27*VAR_25 + VAR_27] + 127)>>8;

                }

            }

        }

 

        ff_spatial_dwt(s->spatial_dwt_buffer, VAR_25, VAR_26, VAR_25, s->spatial_decomposition_type, s->spatial_decomposition_count);



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

            for(VAR_9=VAR_8 ? 1 : 0; VAR_9<4; VAR_9++){

                SubBand *b= &p->band[VAR_8][VAR_9];

                

                quantize(s, b, b->VAR_1, b->VAR_14, s->qbias);

                if(VAR_9==0)

                    decorrelate(s, b, b->VAR_1, b->VAR_14, pict->pict_type == P_TYPE, 0);

                encode_subband(s, b, b->VAR_1, b->parent ? b->parent->VAR_1 : NULL, b->VAR_14, VAR_9);

                assert(b->parent==NULL || b->parent->VAR_14 == b->VAR_14*2);

                if(VAR_9==0)

                    correlate(s, b, b->VAR_1, b->VAR_14, 1, 0);

            }

        }





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

            for(VAR_9=VAR_8 ? 1 : 0; VAR_9<4; VAR_9++){

                SubBand *b= &p->band[VAR_8][VAR_9];



                dequantize(s, b, b->VAR_1, b->VAR_14);

            }

        }



        ff_spatial_idwt(s->spatial_dwt_buffer, VAR_25, VAR_26, VAR_25, s->spatial_decomposition_type, s->spatial_decomposition_count);

        if(s->qlog == LOSSLESS_QLOG){

            for(VAR_27=0; VAR_27<VAR_26; VAR_27++){

                for(VAR_27=0; VAR_27<VAR_25; VAR_27++){

                    s->spatial_dwt_buffer[VAR_27*VAR_25 + VAR_27]<<=8;

                }

            }

        }

        predict_plane(s, s->spatial_dwt_buffer, VAR_10, 1);

        

        for(VAR_27=0; VAR_27<VAR_26; VAR_27++){

            for(VAR_27=0; VAR_27<VAR_25; VAR_27++){

                int VAR_28= (s->spatial_dwt_buffer[VAR_27*VAR_25 + VAR_27]+128)>>8;

                if(VAR_28&(~255)) VAR_28= ~(VAR_28>>31);

                s->current_picture.VAR_3[VAR_10][VAR_27*s->current_picture.linesize[VAR_10] + VAR_27]= VAR_28;

            }

        }

        if(s->VAR_0->flags&CODEC_FLAG_PSNR){

            int64_t error= 0;

            

    if(pict->VAR_3[VAR_10]) 

            for(VAR_27=0; VAR_27<VAR_26; VAR_27++){

                for(VAR_27=0; VAR_27<VAR_25; VAR_27++){

                    int VAR_29= s->current_picture.VAR_3[VAR_10][VAR_27*s->current_picture.linesize[VAR_10] + VAR_27] - pict->VAR_3[VAR_10][VAR_27*pict->linesize[VAR_10] + VAR_27];

                    error += VAR_29*VAR_29;

                }

            }

            s->VAR_0->error[VAR_10] += error;

            s->VAR_0->error[3] += error;

        }

    }



    if(s->last_picture.VAR_3[0])

        VAR_0->release_buffer(VAR_0, &s->last_picture);



    emms_c();

    

    return put_cabac_terminate(c, 1);

}

static int FUNC_0(AVCodecContext *VAR_0, unsigned char *VAR_1, int VAR_2, void *VAR_3){ SnowContext *s = VAR_0->priv_data; CABACContext * const c= &s->c; AVFrame *pict = VAR_3; const int VAR_4= s->VAR_0->VAR_4; const int VAR_5= s->VAR_0->VAR_5; int VAR_6= 0; int VAR_7, VAR_8, VAR_9, VAR_10, VAR_11; ff_init_cabac_encoder(c, VAR_1, VAR_2); ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64); s->input_picture = *pict; memset(s->header_state, 0, sizeof(s->header_state)); s->keyframe=VAR_0->gop_size==0 || VAR_0->frame_number % VAR_0->gop_size == 0; pict->pict_type= s->keyframe ? FF_I_TYPE : FF_P_TYPE; if(pict->quality){ s->qlog= rint(QROOT*log(pict->quality / (float)FF_QP2LAMBDA)/log(2)); s->qlog += 61; }else{ s->qlog= LOSSLESS_QLOG; } for(VAR_11=0; VAR_11<s->mb_band.VAR_14 * s->mb_band.VAR_5; VAR_11++){ s->mb_band.VAR_1[VAR_11]= s->keyframe; } frame_start(s); if(pict->pict_type == P_TYPE){ int VAR_12 = (VAR_4 +15)>>4; int VAR_13= (VAR_5+15)>>4; int VAR_14= s->current_picture.linesize[0]; uint8_t *src_plane= s->input_picture.VAR_3[0]; int VAR_15= s->input_picture.linesize[0]; int VAR_27,VAR_27; assert(s->current_picture.VAR_3[0]); assert(s->last_picture.VAR_3[0]); s->m.VAR_0= s->VAR_0; s->m.current_picture.VAR_3[0]= s->current_picture.VAR_3[0]; s->m.   last_picture.VAR_3[0]= s->   last_picture.VAR_3[0]; s->m.    new_picture.VAR_3[0]= s->  input_picture.VAR_3[0]; s->m.current_picture_ptr= &s->m.current_picture; s->m.   last_picture_ptr= &s->m.   last_picture; s->m.linesize=
s->m.   last_picture.linesize[0]=
s->m.    new_picture.linesize[0]=
s->m.current_picture.linesize[0]= VAR_14; s->m.VAR_4 = VAR_4; s->m.VAR_5= VAR_5; s->m.mb_width = VAR_12; s->m.mb_height= VAR_13; s->m.mb_stride=   s->m.mb_width+1; s->m.b8_stride= 2*s->m.mb_width+1; s->m.f_code=1; s->m.pict_type= pict->pict_type; s->m.me_method= s->VAR_0->me_method; s->m.me.scene_change_score=0; s->m.flags= s->VAR_0->flags; s->m.quarter_sample= (s->VAR_0->flags & CODEC_FLAG_QPEL)!=0; s->m.out_format= FMT_H263; s->m.unrestricted_mv= 1; s->m.lambda= pict->quality * 3/2; s->m.qscale= (s->m.lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7); s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT; if(!s->motion_val8){ s->motion_val8 = av_mallocz(s->m.b8_stride*VAR_13*2*2*sizeof(int16_t)); s->motion_val16= av_mallocz(s->m.mb_stride*VAR_13*2*sizeof(int16_t)); } s->m.mb_type= s->mb_type; s->m.current_picture.mb_mean  = s->mb_mean; s->m.current_picture.mb_var   = (int16_t*)s->dummy; s->m.current_picture.mc_mb_var= (int16_t*)s->dummy; s->m.current_picture.mb_type  = s->dummy; s->m.current_picture.motion_val[0]= s->motion_val8; s->m.p_mv_table= s->motion_val16; s->m.dsp= s->dsp; ff_init_me(&s->m); s->m.me.pre_pass=1; s->m.me.dia_size= s->VAR_0->pre_dia_size; s->m.first_slice_line=1; for(VAR_27= VAR_13-1; VAR_27 >= 0; VAR_27--) { uint8_t src[VAR_14*16]; s->m.new_picture.VAR_3[0]= src - VAR_27*16*VAR_14; s->m.mb_y= VAR_27; for(VAR_11=0; VAR_11<16 && VAR_11 + 16*VAR_27<VAR_5; VAR_11++){ memcpy(&src[VAR_11*VAR_14], &src_plane[(VAR_11+16*VAR_27)*VAR_15], VAR_4); for(VAR_27=VAR_4; VAR_27<16*VAR_12; VAR_27++) src[VAR_11*VAR_14+VAR_27]= src[VAR_11*VAR_14+VAR_27-1]; } for(; VAR_11<16 && VAR_11 + 16*VAR_27<16*VAR_13; VAR_11++) memcpy(&src[VAR_11*VAR_14], &src[(VAR_11-1)*VAR_14], 16*VAR_12); for(VAR_27=VAR_12-1; VAR_27 >=0 ;VAR_27--) { s->m.mb_x= VAR_27; ff_init_block_index(&s->m); ff_update_block_index(&s->m); ff_pre_estimate_p_frame_motion(&s->m, VAR_27, VAR_27); } s->m.first_slice_line=0; } s->m.me.pre_pass=0; s->m.me.dia_size= s->VAR_0->dia_size; s->m.first_slice_line=1; for (VAR_27 = 0; VAR_27 < VAR_13; VAR_27++) { uint8_t src[VAR_14*16]; s->m.new_picture.VAR_3[0]= src - VAR_27*16*VAR_14; s->m.mb_y= VAR_27; assert(VAR_4 <= VAR_14); assert(VAR_4 <= 16*VAR_12); for(VAR_11=0; VAR_11<16 && VAR_11 + 16*VAR_27<VAR_5; VAR_11++){ memcpy(&src[VAR_11*VAR_14], &src_plane[(VAR_11+16*VAR_27)*VAR_15], VAR_4); for(VAR_27=VAR_4; VAR_27<16*VAR_12; VAR_27++) src[VAR_11*VAR_14+VAR_27]= src[VAR_11*VAR_14+VAR_27-1]; } for(; VAR_11<16 && VAR_11 + 16*VAR_27<16*VAR_13; VAR_11++) memcpy(&src[VAR_11*VAR_14], &src[(VAR_11-1)*VAR_14], 16*VAR_12); for (VAR_27 = 0; VAR_27 < VAR_12; VAR_27++) { int VAR_18= VAR_27 + VAR_27*(s->mb_band.VAR_14); s->m.mb_x= VAR_27; ff_init_block_index(&s->m); ff_update_block_index(&s->m); ff_estimate_p_frame_motion(&s->m, VAR_27, VAR_27); s->mb_band   .VAR_1[VAR_18]= (s->m.mb_type[VAR_27 + VAR_27*s->m.mb_stride]&CANDIDATE_MB_TYPE_INTER)
? 0 : 2; s->mv_band[0].VAR_1[VAR_18]= s->motion_val16[VAR_27 + VAR_27*s->m.mb_stride][0]; s->mv_band[1].VAR_1[VAR_18]= s->motion_val16[VAR_27 + VAR_27*s->m.mb_stride][1]; if(s->mb_band   .VAR_1[VAR_27 + VAR_27*(s->mb_band.VAR_14)]==2 && 0){ int VAR_19=128, VAR_20=128, VAR_21, VAR_22, VAR_23; int VAR_24= (s->VAR_0->flags & CODEC_FLAG_QPEL) ? 64 : 32; VAR_21       =s->mb_mean[VAR_27 +  VAR_27   *s->m.mb_stride    ]; if(VAR_27) VAR_19=s->mb_mean[VAR_27 +  VAR_27   *s->m.mb_stride - 1]; if(VAR_27) VAR_20=s->mb_mean[VAR_27 + (VAR_27-1)*s->m.mb_stride    ]; VAR_22= (VAR_19+VAR_20)>>1; #if 0
if     (ABS(VAR_19 - VAR_21) < ABS(VAR_20 - VAR_21) && ABS(VAR_19 - VAR_21) < ABS(VAR_22 - VAR_21))
VAR_23= 1; else if(ABS(VAR_19 - VAR_21) >=ABS(VAR_20 - VAR_21) && ABS(VAR_20 - VAR_21) < ABS(VAR_22 - VAR_21))
VAR_23=-1; else
VAR_23=0; #endif
if(ABS(VAR_19 - VAR_21) < ABS(VAR_20 - VAR_21) && VAR_27){ s->mv_band[0].VAR_1[VAR_18]= s->mv_band[0].VAR_1[VAR_27 + VAR_27*(s->mb_band.VAR_14)-1] - VAR_24; s->mv_band[1].VAR_1[VAR_18]= s->mv_band[1].VAR_1[VAR_27 + VAR_27*(s->mb_band.VAR_14)-1]; s->mb_mean[VAR_27 +  VAR_27   *s->m.mb_stride    ]= VAR_19; }else if(VAR_27){ s->mv_band[0].VAR_1[VAR_18]= s->mv_band[0].VAR_1[VAR_27 + (VAR_27-1)*(s->mb_band.VAR_14)]; s->mv_band[1].VAR_1[VAR_18]= s->mv_band[1].VAR_1[VAR_27 + (VAR_27-1)*(s->mb_band.VAR_14)] - VAR_24; s->mb_mean[VAR_27 +  VAR_27   *s->m.mb_stride    ]= VAR_20; } } } s->m.first_slice_line=0; } assert(s->m.pict_type == P_TYPE); if(s->m.me.scene_change_score > s->VAR_0->scenechange_threshold){ s->m.pict_type=
pict->pict_type =I_TYPE; for(VAR_11=0; VAR_11<s->mb_band.VAR_14 * s->mb_band.VAR_5; VAR_11++){ s->mb_band.VAR_1[VAR_11]= 1; s->mv_band[0].VAR_1[VAR_11]=
s->mv_band[1].VAR_1[VAR_11]= 0; } } } s->m.first_slice_line=1; s->qbias= pict->pict_type == P_TYPE ? 2 : 0; encode_header(s); decorrelate(s, &s->mb_band   , s->mb_band   .VAR_1, s->mb_band   .VAR_14, 0, 1); decorrelate(s, &s->mv_band[0], s->mv_band[0].VAR_1, s->mv_band[0].VAR_14, 0, 1); decorrelate(s, &s->mv_band[1], s->mv_band[1].VAR_1, s->mv_band[1].VAR_14, 0 ,1); encode_subband(s, &s->mb_band   , s->mb_band   .VAR_1, NULL, s->mb_band   .VAR_14, 0); encode_subband(s, &s->mv_band[0], s->mv_band[0].VAR_1, NULL, s->mv_band[0].VAR_14, 0); encode_subband(s, &s->mv_band[1], s->mv_band[1].VAR_1, NULL, s->mv_band[1].VAR_14, 0); correlate(s, &s->mb_band   , s->mb_band   .VAR_1, s->mb_band   .VAR_14, 1, 1); correlate(s, &s->mv_band[0], s->mv_band[0].VAR_1, s->mv_band[0].VAR_14, 1, 1); correlate(s, &s->mv_band[1], s->mv_band[1].VAR_1, s->mv_band[1].VAR_14, 1, 1); for(VAR_10=0; VAR_10<3; VAR_10++){ Plane *p= &s->plane[VAR_10]; int VAR_25= p->VAR_4; int VAR_26= p->VAR_5; int VAR_27, VAR_27; int VAR_27= put_bits_count(&s->c.pb); if(pict->VAR_3[VAR_10])
for(VAR_27=0; VAR_27<VAR_26; VAR_27++){ for(VAR_27=0; VAR_27<VAR_25; VAR_27++){ s->spatial_dwt_buffer[VAR_27*VAR_25 + VAR_27]= pict->VAR_3[VAR_10][VAR_27*pict->linesize[VAR_10] + VAR_27]<<8; } } predict_plane(s, s->spatial_dwt_buffer, VAR_10, 0); if(s->qlog == LOSSLESS_QLOG){ for(VAR_27=0; VAR_27<VAR_26; VAR_27++){ for(VAR_27=0; VAR_27<VAR_25; VAR_27++){ s->spatial_dwt_buffer[VAR_27*VAR_25 + VAR_27]= (s->spatial_dwt_buffer[VAR_27*VAR_25 + VAR_27] + 127)>>8; } } } ff_spatial_dwt(s->spatial_dwt_buffer, VAR_25, VAR_26, VAR_25, s->spatial_decomposition_type, s->spatial_decomposition_count); for(VAR_8=0; VAR_8<s->spatial_decomposition_count; VAR_8++){ for(VAR_9=VAR_8 ? 1 : 0; VAR_9<4; VAR_9++){ SubBand *b= &p->band[VAR_8][VAR_9]; quantize(s, b, b->VAR_1, b->VAR_14, s->qbias); if(VAR_9==0)
decorrelate(s, b, b->VAR_1, b->VAR_14, pict->pict_type == P_TYPE, 0); encode_subband(s, b, b->VAR_1, b->parent ? b->parent->VAR_1 : NULL, b->VAR_14, VAR_9); assert(b->parent==NULL || b->parent->VAR_14 == b->VAR_14*2); if(VAR_9==0)
correlate(s, b, b->VAR_1, b->VAR_14, 1, 0); } } for(VAR_8=0; VAR_8<s->spatial_decomposition_count; VAR_8++){ for(VAR_9=VAR_8 ? 1 : 0; VAR_9<4; VAR_9++){ SubBand *b= &p->band[VAR_8][VAR_9]; dequantize(s, b, b->VAR_1, b->VAR_14); } } ff_spatial_idwt(s->spatial_dwt_buffer, VAR_25, VAR_26, VAR_25, s->spatial_decomposition_type, s->spatial_decomposition_count); if(s->qlog == LOSSLESS_QLOG){ for(VAR_27=0; VAR_27<VAR_26; VAR_27++){ for(VAR_27=0; VAR_27<VAR_25; VAR_27++){ s->spatial_dwt_buffer[VAR_27*VAR_25 + VAR_27]<<=8; } } } predict_plane(s, s->spatial_dwt_buffer, VAR_10, 1); for(VAR_27=0; VAR_27<VAR_26; VAR_27++){ for(VAR_27=0; VAR_27<VAR_25; VAR_27++){ int VAR_28= (s->spatial_dwt_buffer[VAR_27*VAR_25 + VAR_27]+128)>>8; if(VAR_28&(~255)) VAR_28= ~(VAR_28>>31); s->current_picture.VAR_3[VAR_10][VAR_27*s->current_picture.linesize[VAR_10] + VAR_27]= VAR_28; } } if(s->VAR_0->flags&CODEC_FLAG_PSNR){ int64_t error= 0; if(pict->VAR_3[VAR_10])
for(VAR_27=0; VAR_27<VAR_26; VAR_27++){ for(VAR_27=0; VAR_27<VAR_25; VAR_27++){ int VAR_29= s->current_picture.VAR_3[VAR_10][VAR_27*s->current_picture.linesize[VAR_10] + VAR_27] - pict->VAR_3[VAR_10][VAR_27*pict->linesize[VAR_10] + VAR_27]; error += VAR_29*VAR_29; } } s->VAR_0->error[VAR_10] += error; s->VAR_0->error[3] += error; } } if(s->last_picture.VAR_3[0])
VAR_0->release_buffer(VAR_0, &s->last_picture); emms_c(); return put_cabac_terminate(c, 1); }