void ff_estimate_b_frame_motion(MpegEncContext * s,

                             int mb_x, int mb_y)

{

    MotionEstContext * const c= &s->me;

    const int penalty_factor= c->mb_penalty_factor;

    int fmin, bmin, dmin, fbmin, bimin, fimin;

    int type=0;

    const int xy = mb_y*s->mb_stride + mb_x;

    init_ref(c, s->new_picture.f.data, s->last_picture.f.data,

             s->next_picture.f.data, 16 * mb_x, 16 * mb_y, 2);



    get_limits(s, 16*mb_x, 16*mb_y);



    c->skip=0;



    if (s->codec_id == AV_CODEC_ID_MPEG4 && s->next_picture.mbskip_table[xy]) {

        int score= direct_search(s, mb_x, mb_y); //FIXME just check 0,0



        score= ((unsigned)(score*score + 128*256))>>16;

        c->mc_mb_var_sum_temp += score;

        s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE

        s->mb_type[mb_y*s->mb_stride + mb_x]= CANDIDATE_MB_TYPE_DIRECT0;



        return;

    }



    if (s->codec_id == AV_CODEC_ID_MPEG4)

        dmin= direct_search(s, mb_x, mb_y);

    else

        dmin= INT_MAX;

//FIXME penalty stuff for non mpeg4

    c->skip=0;

    fmin = estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code) +

           3 * penalty_factor;



    c->skip=0;

    bmin = estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code) +

           2 * penalty_factor;

    av_dlog(s, " %d %d ", s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]);



    c->skip=0;

    fbmin= bidir_refine(s, mb_x, mb_y) + penalty_factor;

    av_dlog(s, "%d %d %d %d\n", dmin, fmin, bmin, fbmin);



    if(s->flags & CODEC_FLAG_INTERLACED_ME){

//FIXME mb type penalty

        c->skip=0;

        c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;

        fimin= interlaced_search(s, 0,

                                 s->b_field_mv_table[0], s->b_field_select_table[0],

                                 s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 0);

        c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV;

        bimin= interlaced_search(s, 2,

                                 s->b_field_mv_table[1], s->b_field_select_table[1],

                                 s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 0);

    }else

        fimin= bimin= INT_MAX;



    {

        int score= fmin;

        type = CANDIDATE_MB_TYPE_FORWARD;



        if (dmin <= score){

            score = dmin;

            type = CANDIDATE_MB_TYPE_DIRECT;

        }

        if(bmin<score){

            score=bmin;

            type= CANDIDATE_MB_TYPE_BACKWARD;

        }

        if(fbmin<score){

            score=fbmin;

            type= CANDIDATE_MB_TYPE_BIDIR;

        }

        if(fimin<score){

            score=fimin;

            type= CANDIDATE_MB_TYPE_FORWARD_I;

        }

        if(bimin<score){

            score=bimin;

            type= CANDIDATE_MB_TYPE_BACKWARD_I;

        }



        score= ((unsigned)(score*score + 128*256))>>16;

        c->mc_mb_var_sum_temp += score;

        s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE

    }



    if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){

        type= CANDIDATE_MB_TYPE_FORWARD | CANDIDATE_MB_TYPE_BACKWARD | CANDIDATE_MB_TYPE_BIDIR | CANDIDATE_MB_TYPE_DIRECT;

        if(fimin < INT_MAX)

            type |= CANDIDATE_MB_TYPE_FORWARD_I;

        if(bimin < INT_MAX)

            type |= CANDIDATE_MB_TYPE_BACKWARD_I;

        if(fimin < INT_MAX && bimin < INT_MAX){

            type |= CANDIDATE_MB_TYPE_BIDIR_I;

        }

         //FIXME something smarter

        if(dmin>256*256*16) type&= ~CANDIDATE_MB_TYPE_DIRECT; //do not try direct mode if it is invalid for this MB

        if(s->codec_id == AV_CODEC_ID_MPEG4 && type&CANDIDATE_MB_TYPE_DIRECT && s->flags&CODEC_FLAG_MV0 && *(uint32_t*)s->b_direct_mv_table[xy])

            type |= CANDIDATE_MB_TYPE_DIRECT0;

    }



    s->mb_type[mb_y*s->mb_stride + mb_x]= type;

}

void ff_estimate_b_frame_motion(MpegEncContext * s,

                             int mb_x, int mb_y)

{

    MotionEstContext * const c= &s->me;

    const int penalty_factor= c->mb_penalty_factor;

    int fmin, bmin, dmin, fbmin, bimin, fimin;

    int type=0;

    const int xy = mb_y*s->mb_stride + mb_x;

    init_ref(c, s->new_picture.f.data, s->last_picture.f.data,

             s->next_picture.f.data, 16 * mb_x, 16 * mb_y, 2);



    get_limits(s, 16*mb_x, 16*mb_y);



    c->skip=0;



    if (s->codec_id == AV_CODEC_ID_MPEG4 && s->next_picture.mbskip_table[xy]) {

        int score= direct_search(s, mb_x, mb_y); 



        score= ((unsigned)(score*score + 128*256))>>16;

        c->mc_mb_var_sum_temp += score;

        s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score; 

        s->mb_type[mb_y*s->mb_stride + mb_x]= CANDIDATE_MB_TYPE_DIRECT0;



        return;

    }



    if (s->codec_id == AV_CODEC_ID_MPEG4)

        dmin= direct_search(s, mb_x, mb_y);

    else

        dmin= INT_MAX;



    c->skip=0;

    fmin = estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code) +

           3 * penalty_factor;



    c->skip=0;

    bmin = estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code) +

           2 * penalty_factor;

    av_dlog(s, " %d %d ", s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]);



    c->skip=0;

    fbmin= bidir_refine(s, mb_x, mb_y) + penalty_factor;

    av_dlog(s, "%d %d %d %d\n", dmin, fmin, bmin, fbmin);



    if(s->flags & CODEC_FLAG_INTERLACED_ME){



        c->skip=0;

        c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;

        fimin= interlaced_search(s, 0,

                                 s->b_field_mv_table[0], s->b_field_select_table[0],

                                 s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 0);

        c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV;

        bimin= interlaced_search(s, 2,

                                 s->b_field_mv_table[1], s->b_field_select_table[1],

                                 s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 0);

    }else

        fimin= bimin= INT_MAX;



    {

        int score= fmin;

        type = CANDIDATE_MB_TYPE_FORWARD;



        if (dmin <= score){

            score = dmin;

            type = CANDIDATE_MB_TYPE_DIRECT;

        }

        if(bmin<score){

            score=bmin;

            type= CANDIDATE_MB_TYPE_BACKWARD;

        }

        if(fbmin<score){

            score=fbmin;

            type= CANDIDATE_MB_TYPE_BIDIR;

        }

        if(fimin<score){

            score=fimin;

            type= CANDIDATE_MB_TYPE_FORWARD_I;

        }

        if(bimin<score){

            score=bimin;

            type= CANDIDATE_MB_TYPE_BACKWARD_I;

        }



        score= ((unsigned)(score*score + 128*256))>>16;

        c->mc_mb_var_sum_temp += score;

        s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score; 

    }



    if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){

        type= CANDIDATE_MB_TYPE_FORWARD | CANDIDATE_MB_TYPE_BACKWARD | CANDIDATE_MB_TYPE_BIDIR | CANDIDATE_MB_TYPE_DIRECT;

        if(fimin < INT_MAX)

            type |= CANDIDATE_MB_TYPE_FORWARD_I;

        if(bimin < INT_MAX)

            type |= CANDIDATE_MB_TYPE_BACKWARD_I;

        if(fimin < INT_MAX && bimin < INT_MAX){

            type |= CANDIDATE_MB_TYPE_BIDIR_I;

        }

         

        if(dmin>256*256*16) type&= ~CANDIDATE_MB_TYPE_DIRECT; 

        if(s->codec_id == AV_CODEC_ID_MPEG4 && type&CANDIDATE_MB_TYPE_DIRECT && s->flags&CODEC_FLAG_MV0 && *(uint32_t*)s->b_direct_mv_table[xy])

            type |= CANDIDATE_MB_TYPE_DIRECT0;

    }



    s->mb_type[mb_y*s->mb_stride + mb_x]= type;

}

    init_ref(c, s->new_picture.f.data, s->last_picture.f.data,              s->next_picture.f.data, 16 * mb_x, 16 * mb_y, 2);
void FUNC_0(MpegEncContext * VAR_0,

                             int VAR_1, int VAR_2)

{

    MotionEstContext * const c= &VAR_0->me;

    const int VAR_3= c->mb_penalty_factor;

    int VAR_4, VAR_5, VAR_6, VAR_7, VAR_8, VAR_9;

    int VAR_10=0;

    const int VAR_11 = VAR_2*VAR_0->mb_stride + VAR_1;

    init_ref(c, VAR_0->new_picture.f.data, VAR_0->last_picture.f.data,

             VAR_0->next_picture.f.data, 16 * VAR_1, 16 * VAR_2, 2);



    get_limits(VAR_0, 16*VAR_1, 16*VAR_2);



    c->skip=0;



    if (VAR_0->codec_id == AV_CODEC_ID_MPEG4 && VAR_0->next_picture.mbskip_table[VAR_11]) {

        int VAR_13= direct_search(VAR_0, VAR_1, VAR_2); 



        VAR_13= ((unsigned)(VAR_13*VAR_13 + 128*256))>>16;

        c->mc_mb_var_sum_temp += VAR_13;

        VAR_0->current_picture.mc_mb_var[VAR_2*VAR_0->mb_stride + VAR_1] = VAR_13; 

        VAR_0->mb_type[VAR_2*VAR_0->mb_stride + VAR_1]= CANDIDATE_MB_TYPE_DIRECT0;



        return;

    }



    if (VAR_0->codec_id == AV_CODEC_ID_MPEG4)

        VAR_6= direct_search(VAR_0, VAR_1, VAR_2);

    else

        VAR_6= INT_MAX;



    c->skip=0;

    VAR_4 = estimate_motion_b(VAR_0, VAR_1, VAR_2, VAR_0->b_forw_mv_table, 0, VAR_0->f_code) +

           3 * VAR_3;



    c->skip=0;

    VAR_5 = estimate_motion_b(VAR_0, VAR_1, VAR_2, VAR_0->b_back_mv_table, 2, VAR_0->b_code) +

           2 * VAR_3;

    av_dlog(VAR_0, " %d %d ", VAR_0->b_forw_mv_table[VAR_11][0], VAR_0->b_forw_mv_table[VAR_11][1]);



    c->skip=0;

    VAR_7= bidir_refine(VAR_0, VAR_1, VAR_2) + VAR_3;

    av_dlog(VAR_0, "%d %d %d %d\n", VAR_6, VAR_4, VAR_5, VAR_7);



    if(VAR_0->flags & CODEC_FLAG_INTERLACED_ME){



        c->skip=0;

        c->current_mv_penalty= c->mv_penalty[VAR_0->f_code] + MAX_MV;

        VAR_9= interlaced_search(VAR_0, 0,

                                 VAR_0->b_field_mv_table[0], VAR_0->b_field_select_table[0],

                                 VAR_0->b_forw_mv_table[VAR_11][0], VAR_0->b_forw_mv_table[VAR_11][1], 0);

        c->current_mv_penalty= c->mv_penalty[VAR_0->b_code] + MAX_MV;

        VAR_8= interlaced_search(VAR_0, 2,

                                 VAR_0->b_field_mv_table[1], VAR_0->b_field_select_table[1],

                                 VAR_0->b_back_mv_table[VAR_11][0], VAR_0->b_back_mv_table[VAR_11][1], 0);

    }else

        VAR_9= VAR_8= INT_MAX;



    {

        int VAR_13= VAR_4;

        VAR_10 = CANDIDATE_MB_TYPE_FORWARD;



        if (VAR_6 <= VAR_13){

            VAR_13 = VAR_6;

            VAR_10 = CANDIDATE_MB_TYPE_DIRECT;

        }

        if(VAR_5<VAR_13){

            VAR_13=VAR_5;

            VAR_10= CANDIDATE_MB_TYPE_BACKWARD;

        }

        if(VAR_7<VAR_13){

            VAR_13=VAR_7;

            VAR_10= CANDIDATE_MB_TYPE_BIDIR;

        }

        if(VAR_9<VAR_13){

            VAR_13=VAR_9;

            VAR_10= CANDIDATE_MB_TYPE_FORWARD_I;

        }

        if(VAR_8<VAR_13){

            VAR_13=VAR_8;

            VAR_10= CANDIDATE_MB_TYPE_BACKWARD_I;

        }



        VAR_13= ((unsigned)(VAR_13*VAR_13 + 128*256))>>16;

        c->mc_mb_var_sum_temp += VAR_13;

        VAR_0->current_picture.mc_mb_var[VAR_2*VAR_0->mb_stride + VAR_1] = VAR_13; 

    }



    if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){

        VAR_10= CANDIDATE_MB_TYPE_FORWARD | CANDIDATE_MB_TYPE_BACKWARD | CANDIDATE_MB_TYPE_BIDIR | CANDIDATE_MB_TYPE_DIRECT;

        if(VAR_9 < INT_MAX)

            VAR_10 |= CANDIDATE_MB_TYPE_FORWARD_I;

        if(VAR_8 < INT_MAX)

            VAR_10 |= CANDIDATE_MB_TYPE_BACKWARD_I;

        if(VAR_9 < INT_MAX && VAR_8 < INT_MAX){

            VAR_10 |= CANDIDATE_MB_TYPE_BIDIR_I;

        }

         

        if(VAR_6>256*256*16) VAR_10&= ~CANDIDATE_MB_TYPE_DIRECT; 

        if(VAR_0->codec_id == AV_CODEC_ID_MPEG4 && VAR_10&CANDIDATE_MB_TYPE_DIRECT && VAR_0->flags&CODEC_FLAG_MV0 && *(uint32_t*)VAR_0->b_direct_mv_table[VAR_11])

            VAR_10 |= CANDIDATE_MB_TYPE_DIRECT0;

    }



    VAR_0->mb_type[VAR_2*VAR_0->mb_stride + VAR_1]= VAR_10;

}

void FUNC_0(MpegEncContext * VAR_0,
int VAR_1, int VAR_2)
{ MotionEstContext * const c= &VAR_0->me; const int VAR_3= c->mb_penalty_factor; int VAR_4, VAR_5, VAR_6, VAR_7, VAR_8, VAR_9; int VAR_10=0; const int VAR_11 = VAR_2*VAR_0->mb_stride + VAR_1; init_ref(c, VAR_0->new_picture.f.data, VAR_0->last_picture.f.data,
VAR_0->next_picture.f.data, 16 * VAR_1, 16 * VAR_2, 2); get_limits(VAR_0, 16*VAR_1, 16*VAR_2); c->skip=0; if (VAR_0->codec_id == AV_CODEC_ID_MPEG4 && VAR_0->next_picture.mbskip_table[VAR_11]) { int VAR_13= direct_search(VAR_0, VAR_1, VAR_2); VAR_13= ((unsigned)(VAR_13*VAR_13 + 128*256))>>16; c->mc_mb_var_sum_temp += VAR_13; VAR_0->current_picture.mc_mb_var[VAR_2*VAR_0->mb_stride + VAR_1] = VAR_13; VAR_0->mb_type[VAR_2*VAR_0->mb_stride + VAR_1]= CANDIDATE_MB_TYPE_DIRECT0; return; } if (VAR_0->codec_id == AV_CODEC_ID_MPEG4)
VAR_6= direct_search(VAR_0, VAR_1, VAR_2); else
VAR_6= INT_MAX; c->skip=0; VAR_4 = estimate_motion_b(VAR_0, VAR_1, VAR_2, VAR_0->b_forw_mv_table, 0, VAR_0->f_code) +
3 * VAR_3; c->skip=0; VAR_5 = estimate_motion_b(VAR_0, VAR_1, VAR_2, VAR_0->b_back_mv_table, 2, VAR_0->b_code) +
2 * VAR_3; av_dlog(VAR_0, " %d %d ", VAR_0->b_forw_mv_table[VAR_11][0], VAR_0->b_forw_mv_table[VAR_11][1]); c->skip=0; VAR_7= bidir_refine(VAR_0, VAR_1, VAR_2) + VAR_3; av_dlog(VAR_0, "%d %d %d %d\n", VAR_6, VAR_4, VAR_5, VAR_7); if(VAR_0->flags & CODEC_FLAG_INTERLACED_ME){ c->skip=0; c->current_mv_penalty= c->mv_penalty[VAR_0->f_code] + MAX_MV; VAR_9= interlaced_search(VAR_0, 0,
VAR_0->b_field_mv_table[0], VAR_0->b_field_select_table[0],
VAR_0->b_forw_mv_table[VAR_11][0], VAR_0->b_forw_mv_table[VAR_11][1], 0); c->current_mv_penalty= c->mv_penalty[VAR_0->b_code] + MAX_MV; VAR_8= interlaced_search(VAR_0, 2,
VAR_0->b_field_mv_table[1], VAR_0->b_field_select_table[1],
VAR_0->b_back_mv_table[VAR_11][0], VAR_0->b_back_mv_table[VAR_11][1], 0); }else VAR_9= VAR_8= INT_MAX; { int VAR_13= VAR_4; VAR_10 = CANDIDATE_MB_TYPE_FORWARD; if (VAR_6 <= VAR_13){ VAR_13 = VAR_6; VAR_10 = CANDIDATE_MB_TYPE_DIRECT; } if(VAR_5<VAR_13){ VAR_13=VAR_5; VAR_10= CANDIDATE_MB_TYPE_BACKWARD; } if(VAR_7<VAR_13){ VAR_13=VAR_7; VAR_10= CANDIDATE_MB_TYPE_BIDIR; } if(VAR_9<VAR_13){ VAR_13=VAR_9; VAR_10= CANDIDATE_MB_TYPE_FORWARD_I; } if(VAR_8<VAR_13){ VAR_13=VAR_8; VAR_10= CANDIDATE_MB_TYPE_BACKWARD_I; } VAR_13= ((unsigned)(VAR_13*VAR_13 + 128*256))>>16; c->mc_mb_var_sum_temp += VAR_13; VAR_0->current_picture.mc_mb_var[VAR_2*VAR_0->mb_stride + VAR_1] = VAR_13; } if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){ VAR_10= CANDIDATE_MB_TYPE_FORWARD | CANDIDATE_MB_TYPE_BACKWARD | CANDIDATE_MB_TYPE_BIDIR | CANDIDATE_MB_TYPE_DIRECT; if(VAR_9 < INT_MAX)
VAR_10 |= CANDIDATE_MB_TYPE_FORWARD_I; if(VAR_8 < INT_MAX)
VAR_10 |= CANDIDATE_MB_TYPE_BACKWARD_I; if(VAR_9 < INT_MAX && VAR_8 < INT_MAX){ VAR_10 |= CANDIDATE_MB_TYPE_BIDIR_I; } if(VAR_6>256*256*16) VAR_10&= ~CANDIDATE_MB_TYPE_DIRECT; if(VAR_0->codec_id == AV_CODEC_ID_MPEG4 && VAR_10&CANDIDATE_MB_TYPE_DIRECT && VAR_0->flags&CODEC_FLAG_MV0 && *(uint32_t*)VAR_0->b_direct_mv_table[VAR_11])
VAR_10 |= CANDIDATE_MB_TYPE_DIRECT0; } VAR_0->mb_type[VAR_2*VAR_0->mb_stride + VAR_1]= VAR_10; }