static void encode_picture(MpegEncContext *s, int picture_number)

{

    int mb_x, mb_y, last_gob, pdif = 0;

    int i;

    int bits;

    MpegEncContext best_s, backup_s;

    UINT8 bit_buf[7][3000]; //FIXME check that this is ALLWAYS large enogh for a MB



    s->picture_number = picture_number;



    s->block_wrap[0]=

    s->block_wrap[1]=

    s->block_wrap[2]=

    s->block_wrap[3]= s->mb_width*2 + 2;

    s->block_wrap[4]=

    s->block_wrap[5]= s->mb_width + 2;

    

    /* Reset the average MB variance */

    s->avg_mb_var = 0;

    s->mc_mb_var = 0;



    /* we need to initialize some time vars before we can encode b-frames */

    if (s->h263_pred && !s->h263_msmpeg4)

        ff_set_mpeg4_time(s, s->picture_number); 



    /* Estimate motion for every MB */

    if(s->pict_type != I_TYPE){

//        int16_t (*tmp)[2]= s->p_mv_table;

//        s->p_mv_table= s->last_mv_table;

//        s->last_mv_table= s->mv_table;

    

        for(mb_y=0; mb_y < s->mb_height; mb_y++) {

            s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;

            s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);

            s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;

            s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);

            for(mb_x=0; mb_x < s->mb_width; mb_x++) {

                s->mb_x = mb_x;

                s->mb_y = mb_y;

                s->block_index[0]+=2;

                s->block_index[1]+=2;

                s->block_index[2]+=2;

                s->block_index[3]+=2;



                /* compute motion vector & mb_type and store in context */

                if(s->pict_type==B_TYPE)

                    ff_estimate_b_frame_motion(s, mb_x, mb_y);

                else

                    ff_estimate_p_frame_motion(s, mb_x, mb_y);

//                s->mb_type[mb_y*s->mb_width + mb_x]=MB_TYPE_INTER;

            }

        }

        emms_c();

    }else if(s->pict_type == I_TYPE){

        /* I-Frame */

        //FIXME do we need to zero them?

        memset(s->motion_val[0], 0, sizeof(INT16)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);

        memset(s->p_mv_table   , 0, sizeof(INT16)*(s->mb_width+2)*(s->mb_height+2)*2);

        memset(s->mb_type      , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);

    }



    if(s->avg_mb_var < s->mc_mb_var && s->pict_type == P_TYPE){ //FIXME subtract MV bits

        s->pict_type= I_TYPE;

        memset(s->mb_type   , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);

        if(s->max_b_frames==0){

            s->input_pict_type= I_TYPE;

            s->input_picture_in_gop_number=0;

        }

//printf("Scene change detected, encoding as I Frame\n");

    }

    

    if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) 

        s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);

        ff_fix_long_p_mvs(s);

    if(s->pict_type==B_TYPE){

        s->f_code= ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD);

        s->b_code= ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD);



        ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD);

        ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD);

        ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR);

        ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR);

    }

    

//printf("f_code %d ///\n", s->f_code);



//    printf("%d %d\n", s->avg_mb_var, s->mc_mb_var);



    if(s->flags&CODEC_FLAG_PASS2)

        s->qscale = ff_rate_estimate_qscale_pass2(s);

    else if (!s->fixed_qscale) 

        s->qscale = ff_rate_estimate_qscale(s);





    /* precompute matrix */

    if (s->out_format == FMT_MJPEG) {

        /* for mjpeg, we do include qscale in the matrix */

        s->intra_matrix[0] = default_intra_matrix[0];

        for(i=1;i<64;i++)

            s->intra_matrix[i] = (default_intra_matrix[i] * s->qscale) >> 3;

        convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->intra_matrix, 8);

    } else {

        convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->intra_matrix, s->qscale);

        convert_matrix(s->q_non_intra_matrix, s->q_non_intra_matrix16, s->non_intra_matrix, s->qscale);

    }



    s->last_bits= get_bit_count(&s->pb);

    switch(s->out_format) {

    case FMT_MJPEG:

        mjpeg_picture_header(s);

        break;

    case FMT_H263:

        if (s->h263_msmpeg4) 

            msmpeg4_encode_picture_header(s, picture_number);

        else if (s->h263_pred)

            mpeg4_encode_picture_header(s, picture_number);

        else if (s->h263_rv10) 

            rv10_encode_picture_header(s, picture_number);

        else

            h263_encode_picture_header(s, picture_number);

        break;

    case FMT_MPEG1:

        mpeg1_encode_picture_header(s, picture_number);

        break;

    }

    bits= get_bit_count(&s->pb);

    s->header_bits= bits - s->last_bits;

    s->last_bits= bits;

    s->mv_bits=0;

    s->misc_bits=0;

    s->i_tex_bits=0;

    s->p_tex_bits=0;

    s->i_count=0;

    s->p_count=0;

    s->skip_count=0;



    /* init last dc values */

    /* note: quant matrix value (8) is implied here */

    s->last_dc[0] = 128;

    s->last_dc[1] = 128;

    s->last_dc[2] = 128;

    s->mb_incr = 1;

    s->last_mv[0][0][0] = 0;

    s->last_mv[0][0][1] = 0;



    /* Get the GOB height based on picture height */

    if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4) {

        if (s->height <= 400)

            s->gob_index = 1;

        else if (s->height <= 800)

            s->gob_index = 2;

        else

            s->gob_index = 4;

    }

        

    s->avg_mb_var = s->avg_mb_var / s->mb_num;        

    

    for(mb_y=0; mb_y < s->mb_height; mb_y++) {

        /* Put GOB header based on RTP MTU */

        /* TODO: Put all this stuff in a separate generic function */

        if (s->rtp_mode) {

            if (!mb_y) {

                s->ptr_lastgob = s->pb.buf;

                s->ptr_last_mb_line = s->pb.buf;

            } else if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4 && !(mb_y % s->gob_index)) {

                last_gob = h263_encode_gob_header(s, mb_y);

                if (last_gob) {

                    s->first_gob_line = 1;

                }

            }

        }

        

        s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;

        s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);

        s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;

        s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);

        s->block_index[4]= s->block_wrap[4]*(mb_y + 1)                    + s->block_wrap[0]*(s->mb_height*2 + 2);

        s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);

        for(mb_x=0; mb_x < s->mb_width; mb_x++) {

            const int mb_type= s->mb_type[mb_y * s->mb_width + mb_x];

            const int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;

            PutBitContext pb;

            int d;

            int dmin=10000000;

            int best=0;



            s->mb_x = mb_x;

            s->mb_y = mb_y;

            s->block_index[0]+=2;

            s->block_index[1]+=2;

            s->block_index[2]+=2;

            s->block_index[3]+=2;

            s->block_index[4]++;

            s->block_index[5]++;

            if(mb_type & (mb_type-1)){ // more than 1 MB type possible

                int next_block=0;

                pb= s->pb;



                copy_context_before_encode(&backup_s, s, -1);



                if(mb_type&MB_TYPE_INTER){

                    s->mv_dir = MV_DIR_FORWARD;

                    s->mv_type = MV_TYPE_16X16;

                    s->mb_intra= 0;

                    s->mv[0][0][0] = s->p_mv_table[xy][0];

                    s->mv[0][0][1] = s->p_mv_table[xy][1];

                    init_put_bits(&s->pb, bit_buf[1], 3000, NULL, NULL);

                    s->block= s->blocks[next_block];

                    s->last_bits= 0; //done in copy_context_before_encode but we skip that here



                    encode_mb(s, s->mv[0][0][0], s->mv[0][0][1]);

                    d= get_bit_count(&s->pb);

                    if(d<dmin){

                        flush_put_bits(&s->pb);

                        dmin=d;

                        copy_context_after_encode(&best_s, s, MB_TYPE_INTER);

                        best=1;

                        next_block^=1;

                    }

                }

                if(mb_type&MB_TYPE_INTER4V){                 

                    copy_context_before_encode(s, &backup_s, MB_TYPE_INTER4V);

                    s->mv_dir = MV_DIR_FORWARD;

                    s->mv_type = MV_TYPE_8X8;

                    s->mb_intra= 0;

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

                        s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];

                        s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];

                    }

                    init_put_bits(&s->pb, bit_buf[2], 3000, NULL, NULL);

                    s->block= s->blocks[next_block];



                    encode_mb(s, 0, 0);

                    d= get_bit_count(&s->pb);

                    if(d<dmin){

                        flush_put_bits(&s->pb);

                        dmin=d;

                        copy_context_after_encode(&best_s, s, MB_TYPE_INTER4V);

                        best=2;

                        next_block^=1;

                    }

                }

                if(mb_type&MB_TYPE_FORWARD){

                    copy_context_before_encode(s, &backup_s, MB_TYPE_FORWARD);

                    s->mv_dir = MV_DIR_FORWARD;

                    s->mv_type = MV_TYPE_16X16;

                    s->mb_intra= 0;

                    s->mv[0][0][0] = s->b_forw_mv_table[xy][0];

                    s->mv[0][0][1] = s->b_forw_mv_table[xy][1];

                    init_put_bits(&s->pb, bit_buf[3], 3000, NULL, NULL);

                    s->block= s->blocks[next_block];



                    encode_mb(s, s->mv[0][0][0], s->mv[0][0][1]);

                    d= get_bit_count(&s->pb);

                    if(d<dmin){

                        flush_put_bits(&s->pb);

                        dmin=d;

                        copy_context_after_encode(&best_s, s, MB_TYPE_FORWARD);

                        best=3;

                        next_block^=1;

                    }

                }

                if(mb_type&MB_TYPE_BACKWARD){

                    copy_context_before_encode(s, &backup_s, MB_TYPE_BACKWARD);

                    s->mv_dir = MV_DIR_BACKWARD;

                    s->mv_type = MV_TYPE_16X16;

                    s->mb_intra= 0;

                    s->mv[1][0][0] = s->b_back_mv_table[xy][0];

                    s->mv[1][0][1] = s->b_back_mv_table[xy][1];

                    init_put_bits(&s->pb, bit_buf[4], 3000, NULL, NULL);

                    s->block= s->blocks[next_block];



                    encode_mb(s, s->mv[1][0][0], s->mv[1][0][1]);

                    d= get_bit_count(&s->pb);

                    if(d<dmin){

                        flush_put_bits(&s->pb);

                        dmin=d;

                        copy_context_after_encode(&best_s, s, MB_TYPE_BACKWARD);

                        best=4;

                        next_block^=1;

                    }

                }

                if(mb_type&MB_TYPE_BIDIR){

                    copy_context_before_encode(s, &backup_s, MB_TYPE_BIDIR);

                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;

                    s->mv_type = MV_TYPE_16X16;

                    s->mb_intra= 0;

                    s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];

                    s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];

                    s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];

                    s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];

                    init_put_bits(&s->pb, bit_buf[5], 3000, NULL, NULL);

                    s->block= s->blocks[next_block];



                    encode_mb(s, 0, 0);

                    d= get_bit_count(&s->pb);

                    if(d<dmin){

                        flush_put_bits(&s->pb);

                        dmin=d;

                        copy_context_after_encode(&best_s, s, MB_TYPE_BIDIR);

                        best=5;

                        next_block^=1;

                    }

                }

                if(mb_type&MB_TYPE_DIRECT){

                    copy_context_before_encode(s, &backup_s, MB_TYPE_DIRECT);

                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;

                    s->mv_type = MV_TYPE_16X16; //FIXME

                    s->mb_intra= 0;

                    s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];

                    s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];

                    s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];

                    s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];

                    init_put_bits(&s->pb, bit_buf[6], 3000, NULL, NULL);

                    s->block= s->blocks[next_block];



                    encode_mb(s, s->b_direct_mv_table[xy][0], s->b_direct_mv_table[xy][1]);

                    d= get_bit_count(&s->pb);

                    if(d<dmin){

                        flush_put_bits(&s->pb);

                        dmin=d;

                        copy_context_after_encode(&best_s, s, MB_TYPE_DIRECT);

                        best=6;

                        next_block^=1;

                    }

                }

                if(mb_type&MB_TYPE_INTRA){

                    copy_context_before_encode(s, &backup_s, MB_TYPE_INTRA);

                    s->mv_dir = MV_DIR_FORWARD;

                    s->mv_type = MV_TYPE_16X16;

                    s->mb_intra= 1;

                    s->mv[0][0][0] = 0;

                    s->mv[0][0][1] = 0;

                    init_put_bits(&s->pb, bit_buf[0], 3000, NULL, NULL);

                    s->block= s->blocks[next_block];

                   

                    encode_mb(s, 0, 0);

                    d= get_bit_count(&s->pb);

                    if(d<dmin){

                        flush_put_bits(&s->pb);

                        dmin=d;

                        copy_context_after_encode(&best_s, s, MB_TYPE_INTRA);

                        best=0;

                        next_block^=1;

                    }

                    /* force cleaning of ac/dc pred stuff if needed ... */

                    if(s->h263_pred || s->h263_aic)

                        s->mbintra_table[mb_x + mb_y*s->mb_width]=1;

                }

                copy_context_after_encode(s, &best_s, -1);

                copy_bits(&pb, bit_buf[best], dmin);

                s->pb= pb;

                s->last_bits= get_bit_count(&s->pb);

            } else {

                int motion_x, motion_y;

                s->mv_type=MV_TYPE_16X16;

                // only one MB-Type possible

                switch(mb_type){

                case MB_TYPE_INTRA:

                    s->mv_dir = MV_DIR_FORWARD;

                    s->mb_intra= 1;

                    motion_x= s->mv[0][0][0] = 0;

                    motion_y= s->mv[0][0][1] = 0;

                    break;

                case MB_TYPE_INTER:

                    s->mv_dir = MV_DIR_FORWARD;

                    s->mb_intra= 0;

                    motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];

                    motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];

                    break;

                case MB_TYPE_DIRECT:

                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;

                    s->mb_intra= 0;

                    motion_x=s->b_direct_mv_table[xy][0];

                    motion_y=s->b_direct_mv_table[xy][1];

                    s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];

                    s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];

                    s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];

                    s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];

                    break;

                case MB_TYPE_BIDIR:

                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;

                    s->mb_intra= 0;

                    motion_x=0;

                    motion_y=0;

                    s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];

                    s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];

                    s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];

                    s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];

                    break;

                case MB_TYPE_BACKWARD:

                    s->mv_dir = MV_DIR_BACKWARD;

                    s->mb_intra= 0;

                    motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];

                    motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];

                    break;

                case MB_TYPE_FORWARD:

                    s->mv_dir = MV_DIR_FORWARD;

                    s->mb_intra= 0;

                    motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];

                    motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];

//                    printf(" %d %d ", motion_x, motion_y);

                    break;

                default:

                    motion_x=motion_y=0; //gcc warning fix

                    printf("illegal MB type\n");

                }

                encode_mb(s, motion_x, motion_y);

            }

            /* clean the MV table in IPS frames for direct mode in B frames */

            if(s->mb_intra /* && I,P,S_TYPE */){

                s->p_mv_table[xy][0]=0;

                s->p_mv_table[xy][1]=0;

            }



            MPV_decode_mb(s, s->block);

        }





        /* Obtain average GOB size for RTP */

        if (s->rtp_mode) {

            if (!mb_y)

                s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line;

            else if (!(mb_y % s->gob_index)) {    

                s->mb_line_avgsize = (s->mb_line_avgsize + pbBufPtr(&s->pb) - s->ptr_last_mb_line) >> 1;

                s->ptr_last_mb_line = pbBufPtr(&s->pb);

            }

            //fprintf(stderr, "\nMB line: %d\tSize: %u\tAvg. Size: %u", s->mb_y, 

            //                    (s->pb.buf_ptr - s->ptr_last_mb_line), s->mb_line_avgsize);

            s->first_gob_line = 0;

        }

    }

    emms_c();



    if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type == I_TYPE)

        msmpeg4_encode_ext_header(s);



    //if (s->gob_number)

    //    fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);

    

    /* Send the last GOB if RTP */    

    if (s->rtp_mode) {

        flush_put_bits(&s->pb);

        pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;

        /* Call the RTP callback to send the last GOB */

        if (s->rtp_callback)

            s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);

        s->ptr_lastgob = pbBufPtr(&s->pb);

        //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);

    }

}

static void encode_picture(MpegEncContext *s, int picture_number)

{

    int mb_x, mb_y, last_gob, pdif = 0;

    int i;

    int bits;

    MpegEncContext best_s, backup_s;

    UINT8 bit_buf[7][3000]; 



    s->picture_number = picture_number;



    s->block_wrap[0]=

    s->block_wrap[1]=

    s->block_wrap[2]=

    s->block_wrap[3]= s->mb_width*2 + 2;

    s->block_wrap[4]=

    s->block_wrap[5]= s->mb_width + 2;

    

    

    s->avg_mb_var = 0;

    s->mc_mb_var = 0;



    

    if (s->h263_pred && !s->h263_msmpeg4)

        ff_set_mpeg4_time(s, s->picture_number); 



    

    if(s->pict_type != I_TYPE){







    

        for(mb_y=0; mb_y < s->mb_height; mb_y++) {

            s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;

            s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);

            s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;

            s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);

            for(mb_x=0; mb_x < s->mb_width; mb_x++) {

                s->mb_x = mb_x;

                s->mb_y = mb_y;

                s->block_index[0]+=2;

                s->block_index[1]+=2;

                s->block_index[2]+=2;

                s->block_index[3]+=2;



                

                if(s->pict_type==B_TYPE)

                    ff_estimate_b_frame_motion(s, mb_x, mb_y);

                else

                    ff_estimate_p_frame_motion(s, mb_x, mb_y);



            }

        }

        emms_c();

    }else if(s->pict_type == I_TYPE){

        

        

        memset(s->motion_val[0], 0, sizeof(INT16)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);

        memset(s->p_mv_table   , 0, sizeof(INT16)*(s->mb_width+2)*(s->mb_height+2)*2);

        memset(s->mb_type      , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);

    }



    if(s->avg_mb_var < s->mc_mb_var && s->pict_type == P_TYPE){ 

        s->pict_type= I_TYPE;

        memset(s->mb_type   , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);

        if(s->max_b_frames==0){

            s->input_pict_type= I_TYPE;

            s->input_picture_in_gop_number=0;

        }



    }

    

    if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) 

        s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);

        ff_fix_long_p_mvs(s);

    if(s->pict_type==B_TYPE){

        s->f_code= ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD);

        s->b_code= ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD);



        ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD);

        ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD);

        ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR);

        ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR);

    }

    









    if(s->flags&CODEC_FLAG_PASS2)

        s->qscale = ff_rate_estimate_qscale_pass2(s);

    else if (!s->fixed_qscale) 

        s->qscale = ff_rate_estimate_qscale(s);





    

    if (s->out_format == FMT_MJPEG) {

        

        s->intra_matrix[0] = default_intra_matrix[0];

        for(i=1;i<64;i++)

            s->intra_matrix[i] = (default_intra_matrix[i] * s->qscale) >> 3;

        convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->intra_matrix, 8);

    } else {

        convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->intra_matrix, s->qscale);

        convert_matrix(s->q_non_intra_matrix, s->q_non_intra_matrix16, s->non_intra_matrix, s->qscale);

    }



    s->last_bits= get_bit_count(&s->pb);

    switch(s->out_format) {

    case FMT_MJPEG:

        mjpeg_picture_header(s);

        break;

    case FMT_H263:

        if (s->h263_msmpeg4) 

            msmpeg4_encode_picture_header(s, picture_number);

        else if (s->h263_pred)

            mpeg4_encode_picture_header(s, picture_number);

        else if (s->h263_rv10) 

            rv10_encode_picture_header(s, picture_number);

        else

            h263_encode_picture_header(s, picture_number);

        break;

    case FMT_MPEG1:

        mpeg1_encode_picture_header(s, picture_number);

        break;

    }

    bits= get_bit_count(&s->pb);

    s->header_bits= bits - s->last_bits;

    s->last_bits= bits;

    s->mv_bits=0;

    s->misc_bits=0;

    s->i_tex_bits=0;

    s->p_tex_bits=0;

    s->i_count=0;

    s->p_count=0;

    s->skip_count=0;



    

    

    s->last_dc[0] = 128;

    s->last_dc[1] = 128;

    s->last_dc[2] = 128;

    s->mb_incr = 1;

    s->last_mv[0][0][0] = 0;

    s->last_mv[0][0][1] = 0;



    

    if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4) {

        if (s->height <= 400)

            s->gob_index = 1;

        else if (s->height <= 800)

            s->gob_index = 2;

        else

            s->gob_index = 4;

    }

        

    s->avg_mb_var = s->avg_mb_var / s->mb_num;        

    

    for(mb_y=0; mb_y < s->mb_height; mb_y++) {

        

        

        if (s->rtp_mode) {

            if (!mb_y) {

                s->ptr_lastgob = s->pb.buf;

                s->ptr_last_mb_line = s->pb.buf;

            } else if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4 && !(mb_y % s->gob_index)) {

                last_gob = h263_encode_gob_header(s, mb_y);

                if (last_gob) {

                    s->first_gob_line = 1;

                }

            }

        }

        

        s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;

        s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);

        s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;

        s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);

        s->block_index[4]= s->block_wrap[4]*(mb_y + 1)                    + s->block_wrap[0]*(s->mb_height*2 + 2);

        s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);

        for(mb_x=0; mb_x < s->mb_width; mb_x++) {

            const int mb_type= s->mb_type[mb_y * s->mb_width + mb_x];

            const int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;

            PutBitContext pb;

            int d;

            int dmin=10000000;

            int best=0;



            s->mb_x = mb_x;

            s->mb_y = mb_y;

            s->block_index[0]+=2;

            s->block_index[1]+=2;

            s->block_index[2]+=2;

            s->block_index[3]+=2;

            s->block_index[4]++;

            s->block_index[5]++;

            if(mb_type & (mb_type-1)){ 

                int next_block=0;

                pb= s->pb;



                copy_context_before_encode(&backup_s, s, -1);



                if(mb_type&MB_TYPE_INTER){

                    s->mv_dir = MV_DIR_FORWARD;

                    s->mv_type = MV_TYPE_16X16;

                    s->mb_intra= 0;

                    s->mv[0][0][0] = s->p_mv_table[xy][0];

                    s->mv[0][0][1] = s->p_mv_table[xy][1];

                    init_put_bits(&s->pb, bit_buf[1], 3000, NULL, NULL);

                    s->block= s->blocks[next_block];

                    s->last_bits= 0; 



                    encode_mb(s, s->mv[0][0][0], s->mv[0][0][1]);

                    d= get_bit_count(&s->pb);

                    if(d<dmin){

                        flush_put_bits(&s->pb);

                        dmin=d;

                        copy_context_after_encode(&best_s, s, MB_TYPE_INTER);

                        best=1;

                        next_block^=1;

                    }

                }

                if(mb_type&MB_TYPE_INTER4V){                 

                    copy_context_before_encode(s, &backup_s, MB_TYPE_INTER4V);

                    s->mv_dir = MV_DIR_FORWARD;

                    s->mv_type = MV_TYPE_8X8;

                    s->mb_intra= 0;

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

                        s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];

                        s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];

                    }

                    init_put_bits(&s->pb, bit_buf[2], 3000, NULL, NULL);

                    s->block= s->blocks[next_block];



                    encode_mb(s, 0, 0);

                    d= get_bit_count(&s->pb);

                    if(d<dmin){

                        flush_put_bits(&s->pb);

                        dmin=d;

                        copy_context_after_encode(&best_s, s, MB_TYPE_INTER4V);

                        best=2;

                        next_block^=1;

                    }

                }

                if(mb_type&MB_TYPE_FORWARD){

                    copy_context_before_encode(s, &backup_s, MB_TYPE_FORWARD);

                    s->mv_dir = MV_DIR_FORWARD;

                    s->mv_type = MV_TYPE_16X16;

                    s->mb_intra= 0;

                    s->mv[0][0][0] = s->b_forw_mv_table[xy][0];

                    s->mv[0][0][1] = s->b_forw_mv_table[xy][1];

                    init_put_bits(&s->pb, bit_buf[3], 3000, NULL, NULL);

                    s->block= s->blocks[next_block];



                    encode_mb(s, s->mv[0][0][0], s->mv[0][0][1]);

                    d= get_bit_count(&s->pb);

                    if(d<dmin){

                        flush_put_bits(&s->pb);

                        dmin=d;

                        copy_context_after_encode(&best_s, s, MB_TYPE_FORWARD);

                        best=3;

                        next_block^=1;

                    }

                }

                if(mb_type&MB_TYPE_BACKWARD){

                    copy_context_before_encode(s, &backup_s, MB_TYPE_BACKWARD);

                    s->mv_dir = MV_DIR_BACKWARD;

                    s->mv_type = MV_TYPE_16X16;

                    s->mb_intra= 0;

                    s->mv[1][0][0] = s->b_back_mv_table[xy][0];

                    s->mv[1][0][1] = s->b_back_mv_table[xy][1];

                    init_put_bits(&s->pb, bit_buf[4], 3000, NULL, NULL);

                    s->block= s->blocks[next_block];



                    encode_mb(s, s->mv[1][0][0], s->mv[1][0][1]);

                    d= get_bit_count(&s->pb);

                    if(d<dmin){

                        flush_put_bits(&s->pb);

                        dmin=d;

                        copy_context_after_encode(&best_s, s, MB_TYPE_BACKWARD);

                        best=4;

                        next_block^=1;

                    }

                }

                if(mb_type&MB_TYPE_BIDIR){

                    copy_context_before_encode(s, &backup_s, MB_TYPE_BIDIR);

                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;

                    s->mv_type = MV_TYPE_16X16;

                    s->mb_intra= 0;

                    s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];

                    s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];

                    s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];

                    s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];

                    init_put_bits(&s->pb, bit_buf[5], 3000, NULL, NULL);

                    s->block= s->blocks[next_block];



                    encode_mb(s, 0, 0);

                    d= get_bit_count(&s->pb);

                    if(d<dmin){

                        flush_put_bits(&s->pb);

                        dmin=d;

                        copy_context_after_encode(&best_s, s, MB_TYPE_BIDIR);

                        best=5;

                        next_block^=1;

                    }

                }

                if(mb_type&MB_TYPE_DIRECT){

                    copy_context_before_encode(s, &backup_s, MB_TYPE_DIRECT);

                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;

                    s->mv_type = MV_TYPE_16X16; 

                    s->mb_intra= 0;

                    s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];

                    s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];

                    s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];

                    s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];

                    init_put_bits(&s->pb, bit_buf[6], 3000, NULL, NULL);

                    s->block= s->blocks[next_block];



                    encode_mb(s, s->b_direct_mv_table[xy][0], s->b_direct_mv_table[xy][1]);

                    d= get_bit_count(&s->pb);

                    if(d<dmin){

                        flush_put_bits(&s->pb);

                        dmin=d;

                        copy_context_after_encode(&best_s, s, MB_TYPE_DIRECT);

                        best=6;

                        next_block^=1;

                    }

                }

                if(mb_type&MB_TYPE_INTRA){

                    copy_context_before_encode(s, &backup_s, MB_TYPE_INTRA);

                    s->mv_dir = MV_DIR_FORWARD;

                    s->mv_type = MV_TYPE_16X16;

                    s->mb_intra= 1;

                    s->mv[0][0][0] = 0;

                    s->mv[0][0][1] = 0;

                    init_put_bits(&s->pb, bit_buf[0], 3000, NULL, NULL);

                    s->block= s->blocks[next_block];

                   

                    encode_mb(s, 0, 0);

                    d= get_bit_count(&s->pb);

                    if(d<dmin){

                        flush_put_bits(&s->pb);

                        dmin=d;

                        copy_context_after_encode(&best_s, s, MB_TYPE_INTRA);

                        best=0;

                        next_block^=1;

                    }

                    

                    if(s->h263_pred || s->h263_aic)

                        s->mbintra_table[mb_x + mb_y*s->mb_width]=1;

                }

                copy_context_after_encode(s, &best_s, -1);

                copy_bits(&pb, bit_buf[best], dmin);

                s->pb= pb;

                s->last_bits= get_bit_count(&s->pb);

            } else {

                int motion_x, motion_y;

                s->mv_type=MV_TYPE_16X16;

                

                switch(mb_type){

                case MB_TYPE_INTRA:

                    s->mv_dir = MV_DIR_FORWARD;

                    s->mb_intra= 1;

                    motion_x= s->mv[0][0][0] = 0;

                    motion_y= s->mv[0][0][1] = 0;

                    break;

                case MB_TYPE_INTER:

                    s->mv_dir = MV_DIR_FORWARD;

                    s->mb_intra= 0;

                    motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];

                    motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];

                    break;

                case MB_TYPE_DIRECT:

                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;

                    s->mb_intra= 0;

                    motion_x=s->b_direct_mv_table[xy][0];

                    motion_y=s->b_direct_mv_table[xy][1];

                    s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];

                    s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];

                    s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];

                    s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];

                    break;

                case MB_TYPE_BIDIR:

                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;

                    s->mb_intra= 0;

                    motion_x=0;

                    motion_y=0;

                    s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];

                    s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];

                    s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];

                    s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];

                    break;

                case MB_TYPE_BACKWARD:

                    s->mv_dir = MV_DIR_BACKWARD;

                    s->mb_intra= 0;

                    motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];

                    motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];

                    break;

                case MB_TYPE_FORWARD:

                    s->mv_dir = MV_DIR_FORWARD;

                    s->mb_intra= 0;

                    motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];

                    motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];



                    break;

                default:

                    motion_x=motion_y=0; 

                    printf("illegal MB type\n");

                }

                encode_mb(s, motion_x, motion_y);

            }

            

            if(s->mb_intra ){

                s->p_mv_table[xy][0]=0;

                s->p_mv_table[xy][1]=0;

            }



            MPV_decode_mb(s, s->block);

        }





        

        if (s->rtp_mode) {

            if (!mb_y)

                s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line;

            else if (!(mb_y % s->gob_index)) {    

                s->mb_line_avgsize = (s->mb_line_avgsize + pbBufPtr(&s->pb) - s->ptr_last_mb_line) >> 1;

                s->ptr_last_mb_line = pbBufPtr(&s->pb);

            }

            

            

            s->first_gob_line = 0;

        }

    }

    emms_c();



    if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type == I_TYPE)

        msmpeg4_encode_ext_header(s);



    

    

    

        

    if (s->rtp_mode) {

        flush_put_bits(&s->pb);

        pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;

        

        if (s->rtp_callback)

            s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);

        s->ptr_lastgob = pbBufPtr(&s->pb);

        

    }

}

    int i;     } else {         convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->intra_matrix, 8);     } else {         convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->intra_matrix, s->qscale);         convert_matrix(s->q_non_intra_matrix, s->q_non_intra_matrix16, s->non_intra_matrix, s->qscale);             } else {
static void FUNC_0(MpegEncContext *VAR_0, int VAR_1)

{

    int VAR_2, VAR_3, VAR_4, VAR_5 = 0;

    int VAR_6;

    int VAR_7;

    MpegEncContext best_s, backup_s;

    UINT8 bit_buf[7][3000]; 



    VAR_0->VAR_1 = VAR_1;



    VAR_0->block_wrap[0]=

    VAR_0->block_wrap[1]=

    VAR_0->block_wrap[2]=

    VAR_0->block_wrap[3]= VAR_0->mb_width*2 + 2;

    VAR_0->block_wrap[4]=

    VAR_0->block_wrap[5]= VAR_0->mb_width + 2;

    

    

    VAR_0->avg_mb_var = 0;

    VAR_0->mc_mb_var = 0;



    

    if (VAR_0->h263_pred && !VAR_0->h263_msmpeg4)

        ff_set_mpeg4_time(VAR_0, VAR_0->VAR_1); 



    

    if(VAR_0->pict_type != I_TYPE){







    

        for(VAR_3=0; VAR_3 < VAR_0->mb_height; VAR_3++) {

            VAR_0->block_index[0]= VAR_0->block_wrap[0]*(VAR_3*2 + 1) - 1;

            VAR_0->block_index[1]= VAR_0->block_wrap[0]*(VAR_3*2 + 1);

            VAR_0->block_index[2]= VAR_0->block_wrap[0]*(VAR_3*2 + 2) - 1;

            VAR_0->block_index[3]= VAR_0->block_wrap[0]*(VAR_3*2 + 2);

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

                VAR_0->VAR_2 = VAR_2;

                VAR_0->VAR_3 = VAR_3;

                VAR_0->block_index[0]+=2;

                VAR_0->block_index[1]+=2;

                VAR_0->block_index[2]+=2;

                VAR_0->block_index[3]+=2;



                

                if(VAR_0->pict_type==B_TYPE)

                    ff_estimate_b_frame_motion(VAR_0, VAR_2, VAR_3);

                else

                    ff_estimate_p_frame_motion(VAR_0, VAR_2, VAR_3);



            }

        }

        emms_c();

    }else if(VAR_0->pict_type == I_TYPE){

        

        

        memset(VAR_0->motion_val[0], 0, sizeof(INT16)*(VAR_0->mb_width*2 + 2)*(VAR_0->mb_height*2 + 2)*2);

        memset(VAR_0->p_mv_table   , 0, sizeof(INT16)*(VAR_0->mb_width+2)*(VAR_0->mb_height+2)*2);

        memset(VAR_0->mb_type      , MB_TYPE_INTRA, sizeof(UINT8)*VAR_0->mb_width*VAR_0->mb_height);

    }



    if(VAR_0->avg_mb_var < VAR_0->mc_mb_var && VAR_0->pict_type == P_TYPE){ 

        VAR_0->pict_type= I_TYPE;

        memset(VAR_0->mb_type   , MB_TYPE_INTRA, sizeof(UINT8)*VAR_0->mb_width*VAR_0->mb_height);

        if(VAR_0->max_b_frames==0){

            VAR_0->input_pict_type= I_TYPE;

            VAR_0->input_picture_in_gop_number=0;

        }



    }

    

    if(VAR_0->pict_type==P_TYPE || VAR_0->pict_type==S_TYPE) 

        VAR_0->f_code= ff_get_best_fcode(VAR_0, VAR_0->p_mv_table, MB_TYPE_INTER);

        ff_fix_long_p_mvs(VAR_0);

    if(VAR_0->pict_type==B_TYPE){

        VAR_0->f_code= ff_get_best_fcode(VAR_0, VAR_0->b_forw_mv_table, MB_TYPE_FORWARD);

        VAR_0->b_code= ff_get_best_fcode(VAR_0, VAR_0->b_back_mv_table, MB_TYPE_BACKWARD);



        ff_fix_long_b_mvs(VAR_0, VAR_0->b_forw_mv_table, VAR_0->f_code, MB_TYPE_FORWARD);

        ff_fix_long_b_mvs(VAR_0, VAR_0->b_back_mv_table, VAR_0->b_code, MB_TYPE_BACKWARD);

        ff_fix_long_b_mvs(VAR_0, VAR_0->b_bidir_forw_mv_table, VAR_0->f_code, MB_TYPE_BIDIR);

        ff_fix_long_b_mvs(VAR_0, VAR_0->b_bidir_back_mv_table, VAR_0->b_code, MB_TYPE_BIDIR);

    }

    









    if(VAR_0->flags&CODEC_FLAG_PASS2)

        VAR_0->qscale = ff_rate_estimate_qscale_pass2(VAR_0);

    else if (!VAR_0->fixed_qscale) 

        VAR_0->qscale = ff_rate_estimate_qscale(VAR_0);





    

    if (VAR_0->out_format == FMT_MJPEG) {

        

        VAR_0->intra_matrix[0] = default_intra_matrix[0];

        for(VAR_6=1;VAR_6<64;VAR_6++)

            VAR_0->intra_matrix[VAR_6] = (default_intra_matrix[VAR_6] * VAR_0->qscale) >> 3;

        convert_matrix(VAR_0->q_intra_matrix, VAR_0->q_intra_matrix16, VAR_0->intra_matrix, 8);

    } else {

        convert_matrix(VAR_0->q_intra_matrix, VAR_0->q_intra_matrix16, VAR_0->intra_matrix, VAR_0->qscale);

        convert_matrix(VAR_0->q_non_intra_matrix, VAR_0->q_non_intra_matrix16, VAR_0->non_intra_matrix, VAR_0->qscale);

    }



    VAR_0->last_bits= get_bit_count(&VAR_0->pb);

    switch(VAR_0->out_format) {

    case FMT_MJPEG:

        mjpeg_picture_header(VAR_0);

        break;

    case FMT_H263:

        if (VAR_0->h263_msmpeg4) 

            msmpeg4_encode_picture_header(VAR_0, VAR_1);

        else if (VAR_0->h263_pred)

            mpeg4_encode_picture_header(VAR_0, VAR_1);

        else if (VAR_0->h263_rv10) 

            rv10_encode_picture_header(VAR_0, VAR_1);

        else

            h263_encode_picture_header(VAR_0, VAR_1);

        break;

    case FMT_MPEG1:

        mpeg1_encode_picture_header(VAR_0, VAR_1);

        break;

    }

    VAR_7= get_bit_count(&VAR_0->pb);

    VAR_0->header_bits= VAR_7 - VAR_0->last_bits;

    VAR_0->last_bits= VAR_7;

    VAR_0->mv_bits=0;

    VAR_0->misc_bits=0;

    VAR_0->i_tex_bits=0;

    VAR_0->p_tex_bits=0;

    VAR_0->i_count=0;

    VAR_0->p_count=0;

    VAR_0->skip_count=0;



    

    

    VAR_0->last_dc[0] = 128;

    VAR_0->last_dc[1] = 128;

    VAR_0->last_dc[2] = 128;

    VAR_0->mb_incr = 1;

    VAR_0->last_mv[0][0][0] = 0;

    VAR_0->last_mv[0][0][1] = 0;



    

    if (VAR_0->out_format == FMT_H263 && !VAR_0->h263_pred && !VAR_0->h263_msmpeg4) {

        if (VAR_0->height <= 400)

            VAR_0->gob_index = 1;

        else if (VAR_0->height <= 800)

            VAR_0->gob_index = 2;

        else

            VAR_0->gob_index = 4;

    }

        

    VAR_0->avg_mb_var = VAR_0->avg_mb_var / VAR_0->mb_num;        

    

    for(VAR_3=0; VAR_3 < VAR_0->mb_height; VAR_3++) {

        

        

        if (VAR_0->rtp_mode) {

            if (!VAR_3) {

                VAR_0->ptr_lastgob = VAR_0->pb.buf;

                VAR_0->ptr_last_mb_line = VAR_0->pb.buf;

            } else if (VAR_0->out_format == FMT_H263 && !VAR_0->h263_pred && !VAR_0->h263_msmpeg4 && !(VAR_3 % VAR_0->gob_index)) {

                VAR_4 = h263_encode_gob_header(VAR_0, VAR_3);

                if (VAR_4) {

                    VAR_0->first_gob_line = 1;

                }

            }

        }

        

        VAR_0->block_index[0]= VAR_0->block_wrap[0]*(VAR_3*2 + 1) - 1;

        VAR_0->block_index[1]= VAR_0->block_wrap[0]*(VAR_3*2 + 1);

        VAR_0->block_index[2]= VAR_0->block_wrap[0]*(VAR_3*2 + 2) - 1;

        VAR_0->block_index[3]= VAR_0->block_wrap[0]*(VAR_3*2 + 2);

        VAR_0->block_index[4]= VAR_0->block_wrap[4]*(VAR_3 + 1)                    + VAR_0->block_wrap[0]*(VAR_0->mb_height*2 + 2);

        VAR_0->block_index[5]= VAR_0->block_wrap[4]*(VAR_3 + 1 + VAR_0->mb_height + 2) + VAR_0->block_wrap[0]*(VAR_0->mb_height*2 + 2);

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

            const int mb_type= VAR_0->mb_type[VAR_3 * VAR_0->mb_width + VAR_2];

            const int xy= (VAR_3+1) * (VAR_0->mb_width+2) + VAR_2 + 1;

            PutBitContext pb;

            int d;

            int dmin=10000000;

            int best=0;



            VAR_0->VAR_2 = VAR_2;

            VAR_0->VAR_3 = VAR_3;

            VAR_0->block_index[0]+=2;

            VAR_0->block_index[1]+=2;

            VAR_0->block_index[2]+=2;

            VAR_0->block_index[3]+=2;

            VAR_0->block_index[4]++;

            VAR_0->block_index[5]++;

            if(mb_type & (mb_type-1)){ 

                int next_block=0;

                pb= VAR_0->pb;



                copy_context_before_encode(&backup_s, VAR_0, -1);



                if(mb_type&MB_TYPE_INTER){

                    VAR_0->mv_dir = MV_DIR_FORWARD;

                    VAR_0->mv_type = MV_TYPE_16X16;

                    VAR_0->mb_intra= 0;

                    VAR_0->mv[0][0][0] = VAR_0->p_mv_table[xy][0];

                    VAR_0->mv[0][0][1] = VAR_0->p_mv_table[xy][1];

                    init_put_bits(&VAR_0->pb, bit_buf[1], 3000, NULL, NULL);

                    VAR_0->block= VAR_0->blocks[next_block];

                    VAR_0->last_bits= 0; 



                    encode_mb(VAR_0, VAR_0->mv[0][0][0], VAR_0->mv[0][0][1]);

                    d= get_bit_count(&VAR_0->pb);

                    if(d<dmin){

                        flush_put_bits(&VAR_0->pb);

                        dmin=d;

                        copy_context_after_encode(&best_s, VAR_0, MB_TYPE_INTER);

                        best=1;

                        next_block^=1;

                    }

                }

                if(mb_type&MB_TYPE_INTER4V){                 

                    copy_context_before_encode(VAR_0, &backup_s, MB_TYPE_INTER4V);

                    VAR_0->mv_dir = MV_DIR_FORWARD;

                    VAR_0->mv_type = MV_TYPE_8X8;

                    VAR_0->mb_intra= 0;

                    for(VAR_6=0; VAR_6<4; VAR_6++){

                        VAR_0->mv[0][VAR_6][0] = VAR_0->motion_val[VAR_0->block_index[VAR_6]][0];

                        VAR_0->mv[0][VAR_6][1] = VAR_0->motion_val[VAR_0->block_index[VAR_6]][1];

                    }

                    init_put_bits(&VAR_0->pb, bit_buf[2], 3000, NULL, NULL);

                    VAR_0->block= VAR_0->blocks[next_block];



                    encode_mb(VAR_0, 0, 0);

                    d= get_bit_count(&VAR_0->pb);

                    if(d<dmin){

                        flush_put_bits(&VAR_0->pb);

                        dmin=d;

                        copy_context_after_encode(&best_s, VAR_0, MB_TYPE_INTER4V);

                        best=2;

                        next_block^=1;

                    }

                }

                if(mb_type&MB_TYPE_FORWARD){

                    copy_context_before_encode(VAR_0, &backup_s, MB_TYPE_FORWARD);

                    VAR_0->mv_dir = MV_DIR_FORWARD;

                    VAR_0->mv_type = MV_TYPE_16X16;

                    VAR_0->mb_intra= 0;

                    VAR_0->mv[0][0][0] = VAR_0->b_forw_mv_table[xy][0];

                    VAR_0->mv[0][0][1] = VAR_0->b_forw_mv_table[xy][1];

                    init_put_bits(&VAR_0->pb, bit_buf[3], 3000, NULL, NULL);

                    VAR_0->block= VAR_0->blocks[next_block];



                    encode_mb(VAR_0, VAR_0->mv[0][0][0], VAR_0->mv[0][0][1]);

                    d= get_bit_count(&VAR_0->pb);

                    if(d<dmin){

                        flush_put_bits(&VAR_0->pb);

                        dmin=d;

                        copy_context_after_encode(&best_s, VAR_0, MB_TYPE_FORWARD);

                        best=3;

                        next_block^=1;

                    }

                }

                if(mb_type&MB_TYPE_BACKWARD){

                    copy_context_before_encode(VAR_0, &backup_s, MB_TYPE_BACKWARD);

                    VAR_0->mv_dir = MV_DIR_BACKWARD;

                    VAR_0->mv_type = MV_TYPE_16X16;

                    VAR_0->mb_intra= 0;

                    VAR_0->mv[1][0][0] = VAR_0->b_back_mv_table[xy][0];

                    VAR_0->mv[1][0][1] = VAR_0->b_back_mv_table[xy][1];

                    init_put_bits(&VAR_0->pb, bit_buf[4], 3000, NULL, NULL);

                    VAR_0->block= VAR_0->blocks[next_block];



                    encode_mb(VAR_0, VAR_0->mv[1][0][0], VAR_0->mv[1][0][1]);

                    d= get_bit_count(&VAR_0->pb);

                    if(d<dmin){

                        flush_put_bits(&VAR_0->pb);

                        dmin=d;

                        copy_context_after_encode(&best_s, VAR_0, MB_TYPE_BACKWARD);

                        best=4;

                        next_block^=1;

                    }

                }

                if(mb_type&MB_TYPE_BIDIR){

                    copy_context_before_encode(VAR_0, &backup_s, MB_TYPE_BIDIR);

                    VAR_0->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;

                    VAR_0->mv_type = MV_TYPE_16X16;

                    VAR_0->mb_intra= 0;

                    VAR_0->mv[0][0][0] = VAR_0->b_bidir_forw_mv_table[xy][0];

                    VAR_0->mv[0][0][1] = VAR_0->b_bidir_forw_mv_table[xy][1];

                    VAR_0->mv[1][0][0] = VAR_0->b_bidir_back_mv_table[xy][0];

                    VAR_0->mv[1][0][1] = VAR_0->b_bidir_back_mv_table[xy][1];

                    init_put_bits(&VAR_0->pb, bit_buf[5], 3000, NULL, NULL);

                    VAR_0->block= VAR_0->blocks[next_block];



                    encode_mb(VAR_0, 0, 0);

                    d= get_bit_count(&VAR_0->pb);

                    if(d<dmin){

                        flush_put_bits(&VAR_0->pb);

                        dmin=d;

                        copy_context_after_encode(&best_s, VAR_0, MB_TYPE_BIDIR);

                        best=5;

                        next_block^=1;

                    }

                }

                if(mb_type&MB_TYPE_DIRECT){

                    copy_context_before_encode(VAR_0, &backup_s, MB_TYPE_DIRECT);

                    VAR_0->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;

                    VAR_0->mv_type = MV_TYPE_16X16; 

                    VAR_0->mb_intra= 0;

                    VAR_0->mv[0][0][0] = VAR_0->b_direct_forw_mv_table[xy][0];

                    VAR_0->mv[0][0][1] = VAR_0->b_direct_forw_mv_table[xy][1];

                    VAR_0->mv[1][0][0] = VAR_0->b_direct_back_mv_table[xy][0];

                    VAR_0->mv[1][0][1] = VAR_0->b_direct_back_mv_table[xy][1];

                    init_put_bits(&VAR_0->pb, bit_buf[6], 3000, NULL, NULL);

                    VAR_0->block= VAR_0->blocks[next_block];



                    encode_mb(VAR_0, VAR_0->b_direct_mv_table[xy][0], VAR_0->b_direct_mv_table[xy][1]);

                    d= get_bit_count(&VAR_0->pb);

                    if(d<dmin){

                        flush_put_bits(&VAR_0->pb);

                        dmin=d;

                        copy_context_after_encode(&best_s, VAR_0, MB_TYPE_DIRECT);

                        best=6;

                        next_block^=1;

                    }

                }

                if(mb_type&MB_TYPE_INTRA){

                    copy_context_before_encode(VAR_0, &backup_s, MB_TYPE_INTRA);

                    VAR_0->mv_dir = MV_DIR_FORWARD;

                    VAR_0->mv_type = MV_TYPE_16X16;

                    VAR_0->mb_intra= 1;

                    VAR_0->mv[0][0][0] = 0;

                    VAR_0->mv[0][0][1] = 0;

                    init_put_bits(&VAR_0->pb, bit_buf[0], 3000, NULL, NULL);

                    VAR_0->block= VAR_0->blocks[next_block];

                   

                    encode_mb(VAR_0, 0, 0);

                    d= get_bit_count(&VAR_0->pb);

                    if(d<dmin){

                        flush_put_bits(&VAR_0->pb);

                        dmin=d;

                        copy_context_after_encode(&best_s, VAR_0, MB_TYPE_INTRA);

                        best=0;

                        next_block^=1;

                    }

                    

                    if(VAR_0->h263_pred || VAR_0->h263_aic)

                        VAR_0->mbintra_table[VAR_2 + VAR_3*VAR_0->mb_width]=1;

                }

                copy_context_after_encode(VAR_0, &best_s, -1);

                copy_bits(&pb, bit_buf[best], dmin);

                VAR_0->pb= pb;

                VAR_0->last_bits= get_bit_count(&VAR_0->pb);

            } else {

                int motion_x, motion_y;

                VAR_0->mv_type=MV_TYPE_16X16;

                

                switch(mb_type){

                case MB_TYPE_INTRA:

                    VAR_0->mv_dir = MV_DIR_FORWARD;

                    VAR_0->mb_intra= 1;

                    motion_x= VAR_0->mv[0][0][0] = 0;

                    motion_y= VAR_0->mv[0][0][1] = 0;

                    break;

                case MB_TYPE_INTER:

                    VAR_0->mv_dir = MV_DIR_FORWARD;

                    VAR_0->mb_intra= 0;

                    motion_x= VAR_0->mv[0][0][0] = VAR_0->p_mv_table[xy][0];

                    motion_y= VAR_0->mv[0][0][1] = VAR_0->p_mv_table[xy][1];

                    break;

                case MB_TYPE_DIRECT:

                    VAR_0->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;

                    VAR_0->mb_intra= 0;

                    motion_x=VAR_0->b_direct_mv_table[xy][0];

                    motion_y=VAR_0->b_direct_mv_table[xy][1];

                    VAR_0->mv[0][0][0] = VAR_0->b_direct_forw_mv_table[xy][0];

                    VAR_0->mv[0][0][1] = VAR_0->b_direct_forw_mv_table[xy][1];

                    VAR_0->mv[1][0][0] = VAR_0->b_direct_back_mv_table[xy][0];

                    VAR_0->mv[1][0][1] = VAR_0->b_direct_back_mv_table[xy][1];

                    break;

                case MB_TYPE_BIDIR:

                    VAR_0->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;

                    VAR_0->mb_intra= 0;

                    motion_x=0;

                    motion_y=0;

                    VAR_0->mv[0][0][0] = VAR_0->b_bidir_forw_mv_table[xy][0];

                    VAR_0->mv[0][0][1] = VAR_0->b_bidir_forw_mv_table[xy][1];

                    VAR_0->mv[1][0][0] = VAR_0->b_bidir_back_mv_table[xy][0];

                    VAR_0->mv[1][0][1] = VAR_0->b_bidir_back_mv_table[xy][1];

                    break;

                case MB_TYPE_BACKWARD:

                    VAR_0->mv_dir = MV_DIR_BACKWARD;

                    VAR_0->mb_intra= 0;

                    motion_x= VAR_0->mv[1][0][0] = VAR_0->b_back_mv_table[xy][0];

                    motion_y= VAR_0->mv[1][0][1] = VAR_0->b_back_mv_table[xy][1];

                    break;

                case MB_TYPE_FORWARD:

                    VAR_0->mv_dir = MV_DIR_FORWARD;

                    VAR_0->mb_intra= 0;

                    motion_x= VAR_0->mv[0][0][0] = VAR_0->b_forw_mv_table[xy][0];

                    motion_y= VAR_0->mv[0][0][1] = VAR_0->b_forw_mv_table[xy][1];



                    break;

                default:

                    motion_x=motion_y=0; 

                    printf("illegal MB type\n");

                }

                encode_mb(VAR_0, motion_x, motion_y);

            }

            

            if(VAR_0->mb_intra ){

                VAR_0->p_mv_table[xy][0]=0;

                VAR_0->p_mv_table[xy][1]=0;

            }



            MPV_decode_mb(VAR_0, VAR_0->block);

        }





        

        if (VAR_0->rtp_mode) {

            if (!VAR_3)

                VAR_0->mb_line_avgsize = pbBufPtr(&VAR_0->pb) - VAR_0->ptr_last_mb_line;

            else if (!(VAR_3 % VAR_0->gob_index)) {    

                VAR_0->mb_line_avgsize = (VAR_0->mb_line_avgsize + pbBufPtr(&VAR_0->pb) - VAR_0->ptr_last_mb_line) >> 1;

                VAR_0->ptr_last_mb_line = pbBufPtr(&VAR_0->pb);

            }

            

            

            VAR_0->first_gob_line = 0;

        }

    }

    emms_c();



    if (VAR_0->h263_msmpeg4 && VAR_0->msmpeg4_version<4 && VAR_0->pict_type == I_TYPE)

        msmpeg4_encode_ext_header(VAR_0);



    

    

    

        

    if (VAR_0->rtp_mode) {

        flush_put_bits(&VAR_0->pb);

        VAR_5 = pbBufPtr(&VAR_0->pb) - VAR_0->ptr_lastgob;

        

        if (VAR_0->rtp_callback)

            VAR_0->rtp_callback(VAR_0->ptr_lastgob, VAR_5, VAR_0->gob_number);

        VAR_0->ptr_lastgob = pbBufPtr(&VAR_0->pb);

        

    }

}

static void FUNC_0(MpegEncContext *VAR_0, int VAR_1)
{ int VAR_2, VAR_3, VAR_4, VAR_5 = 0; int VAR_6; int VAR_7; MpegEncContext best_s, backup_s; UINT8 bit_buf[7][3000]; VAR_0->VAR_1 = VAR_1; VAR_0->block_wrap[0]=
VAR_0->block_wrap[1]=
VAR_0->block_wrap[2]=
VAR_0->block_wrap[3]= VAR_0->mb_width*2 + 2; VAR_0->block_wrap[4]=
VAR_0->block_wrap[5]= VAR_0->mb_width + 2; VAR_0->avg_mb_var = 0; VAR_0->mc_mb_var = 0; if (VAR_0->h263_pred && !VAR_0->h263_msmpeg4)
ff_set_mpeg4_time(VAR_0, VAR_0->VAR_1); if(VAR_0->pict_type != I_TYPE){ for(VAR_3=0; VAR_3 < VAR_0->mb_height; VAR_3++) { VAR_0->block_index[0]= VAR_0->block_wrap[0]*(VAR_3*2 + 1) - 1; VAR_0->block_index[1]= VAR_0->block_wrap[0]*(VAR_3*2 + 1); VAR_0->block_index[2]= VAR_0->block_wrap[0]*(VAR_3*2 + 2) - 1; VAR_0->block_index[3]= VAR_0->block_wrap[0]*(VAR_3*2 + 2); for(VAR_2=0; VAR_2 < VAR_0->mb_width; VAR_2++) { VAR_0->VAR_2 = VAR_2; VAR_0->VAR_3 = VAR_3; VAR_0->block_index[0]+=2; VAR_0->block_index[1]+=2; VAR_0->block_index[2]+=2; VAR_0->block_index[3]+=2; if(VAR_0->pict_type==B_TYPE)
ff_estimate_b_frame_motion(VAR_0, VAR_2, VAR_3); else
ff_estimate_p_frame_motion(VAR_0, VAR_2, VAR_3); } } emms_c(); }else if(VAR_0->pict_type == I_TYPE){ memset(VAR_0->motion_val[0], 0, sizeof(INT16)*(VAR_0->mb_width*2 + 2)*(VAR_0->mb_height*2 + 2)*2); memset(VAR_0->p_mv_table   , 0, sizeof(INT16)*(VAR_0->mb_width+2)*(VAR_0->mb_height+2)*2); memset(VAR_0->mb_type      , MB_TYPE_INTRA, sizeof(UINT8)*VAR_0->mb_width*VAR_0->mb_height); } if(VAR_0->avg_mb_var < VAR_0->mc_mb_var && VAR_0->pict_type == P_TYPE){ VAR_0->pict_type= I_TYPE; memset(VAR_0->mb_type   , MB_TYPE_INTRA, sizeof(UINT8)*VAR_0->mb_width*VAR_0->mb_height); if(VAR_0->max_b_frames==0){ VAR_0->input_pict_type= I_TYPE; VAR_0->input_picture_in_gop_number=0; } } if(VAR_0->pict_type==P_TYPE || VAR_0->pict_type==S_TYPE)
VAR_0->f_code= ff_get_best_fcode(VAR_0, VAR_0->p_mv_table, MB_TYPE_INTER); ff_fix_long_p_mvs(VAR_0); if(VAR_0->pict_type==B_TYPE){ VAR_0->f_code= ff_get_best_fcode(VAR_0, VAR_0->b_forw_mv_table, MB_TYPE_FORWARD); VAR_0->b_code= ff_get_best_fcode(VAR_0, VAR_0->b_back_mv_table, MB_TYPE_BACKWARD); ff_fix_long_b_mvs(VAR_0, VAR_0->b_forw_mv_table, VAR_0->f_code, MB_TYPE_FORWARD); ff_fix_long_b_mvs(VAR_0, VAR_0->b_back_mv_table, VAR_0->b_code, MB_TYPE_BACKWARD); ff_fix_long_b_mvs(VAR_0, VAR_0->b_bidir_forw_mv_table, VAR_0->f_code, MB_TYPE_BIDIR); ff_fix_long_b_mvs(VAR_0, VAR_0->b_bidir_back_mv_table, VAR_0->b_code, MB_TYPE_BIDIR); } if(VAR_0->flags&CODEC_FLAG_PASS2)
VAR_0->qscale = ff_rate_estimate_qscale_pass2(VAR_0); else if (!VAR_0->fixed_qscale)
VAR_0->qscale = ff_rate_estimate_qscale(VAR_0); if (VAR_0->out_format == FMT_MJPEG) { VAR_0->intra_matrix[0] = default_intra_matrix[0]; for(VAR_6=1;VAR_6<64;VAR_6++) VAR_0->intra_matrix[VAR_6] = (default_intra_matrix[VAR_6] * VAR_0->qscale) >> 3; convert_matrix(VAR_0->q_intra_matrix, VAR_0->q_intra_matrix16, VAR_0->intra_matrix, 8); } else { convert_matrix(VAR_0->q_intra_matrix, VAR_0->q_intra_matrix16, VAR_0->intra_matrix, VAR_0->qscale); convert_matrix(VAR_0->q_non_intra_matrix, VAR_0->q_non_intra_matrix16, VAR_0->non_intra_matrix, VAR_0->qscale); } VAR_0->last_bits= get_bit_count(&VAR_0->pb); switch(VAR_0->out_format) { case FMT_MJPEG:
mjpeg_picture_header(VAR_0); break; case FMT_H263:
if (VAR_0->h263_msmpeg4)
msmpeg4_encode_picture_header(VAR_0, VAR_1); else if (VAR_0->h263_pred)
mpeg4_encode_picture_header(VAR_0, VAR_1); else if (VAR_0->h263_rv10)
rv10_encode_picture_header(VAR_0, VAR_1); else
h263_encode_picture_header(VAR_0, VAR_1); break; case FMT_MPEG1:
mpeg1_encode_picture_header(VAR_0, VAR_1); break; } VAR_7= get_bit_count(&VAR_0->pb); VAR_0->header_bits= VAR_7 - VAR_0->last_bits; VAR_0->last_bits= VAR_7; VAR_0->mv_bits=0; VAR_0->misc_bits=0; VAR_0->i_tex_bits=0; VAR_0->p_tex_bits=0; VAR_0->i_count=0; VAR_0->p_count=0; VAR_0->skip_count=0; VAR_0->last_dc[0] = 128; VAR_0->last_dc[1] = 128; VAR_0->last_dc[2] = 128; VAR_0->mb_incr = 1; VAR_0->last_mv[0][0][0] = 0; VAR_0->last_mv[0][0][1] = 0; if (VAR_0->out_format == FMT_H263 && !VAR_0->h263_pred && !VAR_0->h263_msmpeg4) { if (VAR_0->height <= 400)
VAR_0->gob_index = 1; else if (VAR_0->height <= 800)
VAR_0->gob_index = 2; else
VAR_0->gob_index = 4; } VAR_0->avg_mb_var = VAR_0->avg_mb_var / VAR_0->mb_num; for(VAR_3=0; VAR_3 < VAR_0->mb_height; VAR_3++) { if (VAR_0->rtp_mode) { if (!VAR_3) { VAR_0->ptr_lastgob = VAR_0->pb.buf; VAR_0->ptr_last_mb_line = VAR_0->pb.buf; } else if (VAR_0->out_format == FMT_H263 && !VAR_0->h263_pred && !VAR_0->h263_msmpeg4 && !(VAR_3 % VAR_0->gob_index)) { VAR_4 = h263_encode_gob_header(VAR_0, VAR_3); if (VAR_4) { VAR_0->first_gob_line = 1; } } } VAR_0->block_index[0]= VAR_0->block_wrap[0]*(VAR_3*2 + 1) - 1; VAR_0->block_index[1]= VAR_0->block_wrap[0]*(VAR_3*2 + 1); VAR_0->block_index[2]= VAR_0->block_wrap[0]*(VAR_3*2 + 2) - 1; VAR_0->block_index[3]= VAR_0->block_wrap[0]*(VAR_3*2 + 2); VAR_0->block_index[4]= VAR_0->block_wrap[4]*(VAR_3 + 1)                    + VAR_0->block_wrap[0]*(VAR_0->mb_height*2 + 2); VAR_0->block_index[5]= VAR_0->block_wrap[4]*(VAR_3 + 1 + VAR_0->mb_height + 2) + VAR_0->block_wrap[0]*(VAR_0->mb_height*2 + 2); for(VAR_2=0; VAR_2 < VAR_0->mb_width; VAR_2++) { const int mb_type= VAR_0->mb_type[VAR_3 * VAR_0->mb_width + VAR_2]; const int xy= (VAR_3+1) * (VAR_0->mb_width+2) + VAR_2 + 1; PutBitContext pb; int d; int dmin=10000000; int best=0; VAR_0->VAR_2 = VAR_2; VAR_0->VAR_3 = VAR_3; VAR_0->block_index[0]+=2; VAR_0->block_index[1]+=2; VAR_0->block_index[2]+=2; VAR_0->block_index[3]+=2; VAR_0->block_index[4]++; VAR_0->block_index[5]++; if(mb_type & (mb_type-1)){ int next_block=0; pb= VAR_0->pb; copy_context_before_encode(&backup_s, VAR_0, -1); if(mb_type&MB_TYPE_INTER){ VAR_0->mv_dir = MV_DIR_FORWARD; VAR_0->mv_type = MV_TYPE_16X16; VAR_0->mb_intra= 0; VAR_0->mv[0][0][0] = VAR_0->p_mv_table[xy][0]; VAR_0->mv[0][0][1] = VAR_0->p_mv_table[xy][1]; init_put_bits(&VAR_0->pb, bit_buf[1], 3000, NULL, NULL); VAR_0->block= VAR_0->blocks[next_block]; VAR_0->last_bits= 0; encode_mb(VAR_0, VAR_0->mv[0][0][0], VAR_0->mv[0][0][1]); d= get_bit_count(&VAR_0->pb); if(d<dmin){ flush_put_bits(&VAR_0->pb); dmin=d; copy_context_after_encode(&best_s, VAR_0, MB_TYPE_INTER); best=1; next_block^=1; } } if(mb_type&MB_TYPE_INTER4V){ copy_context_before_encode(VAR_0, &backup_s, MB_TYPE_INTER4V); VAR_0->mv_dir = MV_DIR_FORWARD; VAR_0->mv_type = MV_TYPE_8X8; VAR_0->mb_intra= 0; for(VAR_6=0; VAR_6<4; VAR_6++){ VAR_0->mv[0][VAR_6][0] = VAR_0->motion_val[VAR_0->block_index[VAR_6]][0]; VAR_0->mv[0][VAR_6][1] = VAR_0->motion_val[VAR_0->block_index[VAR_6]][1]; } init_put_bits(&VAR_0->pb, bit_buf[2], 3000, NULL, NULL); VAR_0->block= VAR_0->blocks[next_block]; encode_mb(VAR_0, 0, 0); d= get_bit_count(&VAR_0->pb); if(d<dmin){ flush_put_bits(&VAR_0->pb); dmin=d; copy_context_after_encode(&best_s, VAR_0, MB_TYPE_INTER4V); best=2; next_block^=1; } } if(mb_type&MB_TYPE_FORWARD){ copy_context_before_encode(VAR_0, &backup_s, MB_TYPE_FORWARD); VAR_0->mv_dir = MV_DIR_FORWARD; VAR_0->mv_type = MV_TYPE_16X16; VAR_0->mb_intra= 0; VAR_0->mv[0][0][0] = VAR_0->b_forw_mv_table[xy][0]; VAR_0->mv[0][0][1] = VAR_0->b_forw_mv_table[xy][1]; init_put_bits(&VAR_0->pb, bit_buf[3], 3000, NULL, NULL); VAR_0->block= VAR_0->blocks[next_block]; encode_mb(VAR_0, VAR_0->mv[0][0][0], VAR_0->mv[0][0][1]); d= get_bit_count(&VAR_0->pb); if(d<dmin){ flush_put_bits(&VAR_0->pb); dmin=d; copy_context_after_encode(&best_s, VAR_0, MB_TYPE_FORWARD); best=3; next_block^=1; } } if(mb_type&MB_TYPE_BACKWARD){ copy_context_before_encode(VAR_0, &backup_s, MB_TYPE_BACKWARD); VAR_0->mv_dir = MV_DIR_BACKWARD; VAR_0->mv_type = MV_TYPE_16X16; VAR_0->mb_intra= 0; VAR_0->mv[1][0][0] = VAR_0->b_back_mv_table[xy][0]; VAR_0->mv[1][0][1] = VAR_0->b_back_mv_table[xy][1]; init_put_bits(&VAR_0->pb, bit_buf[4], 3000, NULL, NULL); VAR_0->block= VAR_0->blocks[next_block]; encode_mb(VAR_0, VAR_0->mv[1][0][0], VAR_0->mv[1][0][1]); d= get_bit_count(&VAR_0->pb); if(d<dmin){ flush_put_bits(&VAR_0->pb); dmin=d; copy_context_after_encode(&best_s, VAR_0, MB_TYPE_BACKWARD); best=4; next_block^=1; } } if(mb_type&MB_TYPE_BIDIR){ copy_context_before_encode(VAR_0, &backup_s, MB_TYPE_BIDIR); VAR_0->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD; VAR_0->mv_type = MV_TYPE_16X16; VAR_0->mb_intra= 0; VAR_0->mv[0][0][0] = VAR_0->b_bidir_forw_mv_table[xy][0]; VAR_0->mv[0][0][1] = VAR_0->b_bidir_forw_mv_table[xy][1]; VAR_0->mv[1][0][0] = VAR_0->b_bidir_back_mv_table[xy][0]; VAR_0->mv[1][0][1] = VAR_0->b_bidir_back_mv_table[xy][1]; init_put_bits(&VAR_0->pb, bit_buf[5], 3000, NULL, NULL); VAR_0->block= VAR_0->blocks[next_block]; encode_mb(VAR_0, 0, 0); d= get_bit_count(&VAR_0->pb); if(d<dmin){ flush_put_bits(&VAR_0->pb); dmin=d; copy_context_after_encode(&best_s, VAR_0, MB_TYPE_BIDIR); best=5; next_block^=1; } } if(mb_type&MB_TYPE_DIRECT){ copy_context_before_encode(VAR_0, &backup_s, MB_TYPE_DIRECT); VAR_0->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT; VAR_0->mv_type = MV_TYPE_16X16; VAR_0->mb_intra= 0; VAR_0->mv[0][0][0] = VAR_0->b_direct_forw_mv_table[xy][0]; VAR_0->mv[0][0][1] = VAR_0->b_direct_forw_mv_table[xy][1]; VAR_0->mv[1][0][0] = VAR_0->b_direct_back_mv_table[xy][0]; VAR_0->mv[1][0][1] = VAR_0->b_direct_back_mv_table[xy][1]; init_put_bits(&VAR_0->pb, bit_buf[6], 3000, NULL, NULL); VAR_0->block= VAR_0->blocks[next_block]; encode_mb(VAR_0, VAR_0->b_direct_mv_table[xy][0], VAR_0->b_direct_mv_table[xy][1]); d= get_bit_count(&VAR_0->pb); if(d<dmin){ flush_put_bits(&VAR_0->pb); dmin=d; copy_context_after_encode(&best_s, VAR_0, MB_TYPE_DIRECT); best=6; next_block^=1; } } if(mb_type&MB_TYPE_INTRA){ copy_context_before_encode(VAR_0, &backup_s, MB_TYPE_INTRA); VAR_0->mv_dir = MV_DIR_FORWARD; VAR_0->mv_type = MV_TYPE_16X16; VAR_0->mb_intra= 1; VAR_0->mv[0][0][0] = 0; VAR_0->mv[0][0][1] = 0; init_put_bits(&VAR_0->pb, bit_buf[0], 3000, NULL, NULL); VAR_0->block= VAR_0->blocks[next_block]; encode_mb(VAR_0, 0, 0); d= get_bit_count(&VAR_0->pb); if(d<dmin){ flush_put_bits(&VAR_0->pb); dmin=d; copy_context_after_encode(&best_s, VAR_0, MB_TYPE_INTRA); best=0; next_block^=1; } if(VAR_0->h263_pred || VAR_0->h263_aic)
VAR_0->mbintra_table[VAR_2 + VAR_3*VAR_0->mb_width]=1; } copy_context_after_encode(VAR_0, &best_s, -1); copy_bits(&pb, bit_buf[best], dmin); VAR_0->pb= pb; VAR_0->last_bits= get_bit_count(&VAR_0->pb); } else { int motion_x, motion_y; VAR_0->mv_type=MV_TYPE_16X16; switch(mb_type){ case MB_TYPE_INTRA:
VAR_0->mv_dir = MV_DIR_FORWARD; VAR_0->mb_intra= 1; motion_x= VAR_0->mv[0][0][0] = 0; motion_y= VAR_0->mv[0][0][1] = 0; break; case MB_TYPE_INTER:
VAR_0->mv_dir = MV_DIR_FORWARD; VAR_0->mb_intra= 0; motion_x= VAR_0->mv[0][0][0] = VAR_0->p_mv_table[xy][0]; motion_y= VAR_0->mv[0][0][1] = VAR_0->p_mv_table[xy][1]; break; case MB_TYPE_DIRECT:
VAR_0->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT; VAR_0->mb_intra= 0; motion_x=VAR_0->b_direct_mv_table[xy][0]; motion_y=VAR_0->b_direct_mv_table[xy][1]; VAR_0->mv[0][0][0] = VAR_0->b_direct_forw_mv_table[xy][0]; VAR_0->mv[0][0][1] = VAR_0->b_direct_forw_mv_table[xy][1]; VAR_0->mv[1][0][0] = VAR_0->b_direct_back_mv_table[xy][0]; VAR_0->mv[1][0][1] = VAR_0->b_direct_back_mv_table[xy][1]; break; case MB_TYPE_BIDIR:
VAR_0->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD; VAR_0->mb_intra= 0; motion_x=0; motion_y=0; VAR_0->mv[0][0][0] = VAR_0->b_bidir_forw_mv_table[xy][0]; VAR_0->mv[0][0][1] = VAR_0->b_bidir_forw_mv_table[xy][1]; VAR_0->mv[1][0][0] = VAR_0->b_bidir_back_mv_table[xy][0]; VAR_0->mv[1][0][1] = VAR_0->b_bidir_back_mv_table[xy][1]; break; case MB_TYPE_BACKWARD:
VAR_0->mv_dir = MV_DIR_BACKWARD; VAR_0->mb_intra= 0; motion_x= VAR_0->mv[1][0][0] = VAR_0->b_back_mv_table[xy][0]; motion_y= VAR_0->mv[1][0][1] = VAR_0->b_back_mv_table[xy][1]; break; case MB_TYPE_FORWARD:
VAR_0->mv_dir = MV_DIR_FORWARD; VAR_0->mb_intra= 0; motion_x= VAR_0->mv[0][0][0] = VAR_0->b_forw_mv_table[xy][0]; motion_y= VAR_0->mv[0][0][1] = VAR_0->b_forw_mv_table[xy][1]; break; default:
motion_x=motion_y=0; printf("illegal MB type\n"); } encode_mb(VAR_0, motion_x, motion_y); } if(VAR_0->mb_intra ){ VAR_0->p_mv_table[xy][0]=0; VAR_0->p_mv_table[xy][1]=0; } MPV_decode_mb(VAR_0, VAR_0->block); } if (VAR_0->rtp_mode) { if (!VAR_3)
VAR_0->mb_line_avgsize = pbBufPtr(&VAR_0->pb) - VAR_0->ptr_last_mb_line; else if (!(VAR_3 % VAR_0->gob_index)) { VAR_0->mb_line_avgsize = (VAR_0->mb_line_avgsize + pbBufPtr(&VAR_0->pb) - VAR_0->ptr_last_mb_line) >> 1; VAR_0->ptr_last_mb_line = pbBufPtr(&VAR_0->pb); } VAR_0->first_gob_line = 0; } } emms_c(); if (VAR_0->h263_msmpeg4 && VAR_0->msmpeg4_version<4 && VAR_0->pict_type == I_TYPE)
msmpeg4_encode_ext_header(VAR_0); if (VAR_0->rtp_mode) { flush_put_bits(&VAR_0->pb); VAR_5 = pbBufPtr(&VAR_0->pb) - VAR_0->ptr_lastgob; if (VAR_0->rtp_callback)
VAR_0->rtp_callback(VAR_0->ptr_lastgob, VAR_5, VAR_0->gob_number); VAR_0->ptr_lastgob = pbBufPtr(&VAR_0->pb); } }