static inline int bidir_refine(MpegEncContext * s, int mb_x, int mb_y)

{

    MotionEstContext * const c= &s->me;

    const int mot_stride = s->mb_stride;

    const int xy = mb_y *mot_stride + mb_x;

    int fbmin;

    int pred_fx= s->b_bidir_forw_mv_table[xy-1][0];

    int pred_fy= s->b_bidir_forw_mv_table[xy-1][1];

    int pred_bx= s->b_bidir_back_mv_table[xy-1][0];

    int pred_by= s->b_bidir_back_mv_table[xy-1][1];

    int motion_fx= s->b_bidir_forw_mv_table[xy][0]= s->b_forw_mv_table[xy][0];

    int motion_fy= s->b_bidir_forw_mv_table[xy][1]= s->b_forw_mv_table[xy][1];

    int motion_bx= s->b_bidir_back_mv_table[xy][0]= s->b_back_mv_table[xy][0];

    int motion_by= s->b_bidir_back_mv_table[xy][1]= s->b_back_mv_table[xy][1];

    const int flags= c->sub_flags;

    const int qpel= flags&FLAG_QPEL;

    const int shift= 1+qpel;

    const int xmin= c->xmin<<shift;

    const int ymin= c->ymin<<shift;

    const int xmax= c->xmax<<shift;

    const int ymax= c->ymax<<shift;

    uint8_t map[8][8][8][8];



    memset(map,0,sizeof(map));

#define BIDIR_MAP(fx,fy,bx,by) \

    map[(motion_fx+fx)&7][(motion_fy+fy)&7][(motion_bx+bx)&7][(motion_by+by)&7]

    BIDIR_MAP(0,0,0,0) = 1;



    fbmin= check_bidir_mv(s, motion_fx, motion_fy,

                          motion_bx, motion_by,

                          pred_fx, pred_fy,

                          pred_bx, pred_by,

                          0, 16);



    if(s->avctx->bidir_refine){

        int score, end;

#define CHECK_BIDIR(fx,fy,bx,by)\

    if( !BIDIR_MAP(fx,fy,bx,by)\

       &&(fx<=0 || motion_fx+fx<=xmax) && (fy<=0 || motion_fy+fy<=ymax) && (bx<=0 || motion_bx+bx<=xmax) && (by<=0 || motion_by+by<=ymax)\

       &&(fx>=0 || motion_fx+fx>=xmin) && (fy>=0 || motion_fy+fy>=ymin) && (bx>=0 || motion_bx+bx>=xmin) && (by>=0 || motion_by+by>=ymin)){\

        BIDIR_MAP(fx,fy,bx,by) = 1;\

        score= check_bidir_mv(s, motion_fx+fx, motion_fy+fy, motion_bx+bx, motion_by+by, pred_fx, pred_fy, pred_bx, pred_by, 0, 16);\

        if(score < fbmin){\

            fbmin= score;\

            motion_fx+=fx;\

            motion_fy+=fy;\

            motion_bx+=bx;\

            motion_by+=by;\

            end=0;\

        }\

    }

#define CHECK_BIDIR2(a,b,c,d)\

CHECK_BIDIR(a,b,c,d)\

CHECK_BIDIR(-a,-b,-c,-d)



#define CHECK_BIDIRR(a,b,c,d)\

CHECK_BIDIR2(a,b,c,d)\

CHECK_BIDIR2(b,c,d,a)\

CHECK_BIDIR2(c,d,a,b)\

CHECK_BIDIR2(d,a,b,c)



        do{

            end=1;



            CHECK_BIDIRR( 0, 0, 0, 1)

            if(s->avctx->bidir_refine > 1){

                CHECK_BIDIRR( 0, 0, 1, 1)

                CHECK_BIDIR2( 0, 1, 0, 1)

                CHECK_BIDIR2( 1, 0, 1, 0)

                CHECK_BIDIRR( 0, 0,-1, 1)

                CHECK_BIDIR2( 0,-1, 0, 1)

                CHECK_BIDIR2(-1, 0, 1, 0)

                if(s->avctx->bidir_refine > 2){

                    CHECK_BIDIRR( 0, 1, 1, 1)

                    CHECK_BIDIRR( 0,-1, 1, 1)

                    CHECK_BIDIRR( 0, 1,-1, 1)

                    CHECK_BIDIRR( 0, 1, 1,-1)

                    if(s->avctx->bidir_refine > 3){

                        CHECK_BIDIR2( 1, 1, 1, 1)

                        CHECK_BIDIRR( 1, 1, 1,-1)

                        CHECK_BIDIR2( 1, 1,-1,-1)

                        CHECK_BIDIR2( 1,-1,-1, 1)

                        CHECK_BIDIR2( 1,-1, 1,-1)

                    }

                }

            }

        }while(!end);

    }



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

    s->b_bidir_forw_mv_table[xy][1]= motion_fy;

    s->b_bidir_back_mv_table[xy][0]= motion_bx;

    s->b_bidir_back_mv_table[xy][1]= motion_by;



    return fbmin;

}

static inline int bidir_refine(MpegEncContext * s, int mb_x, int mb_y)

{

    MotionEstContext * const c= &s->me;

    const int mot_stride = s->mb_stride;

    const int xy = mb_y *mot_stride + mb_x;

    int fbmin;

    int pred_fx= s->b_bidir_forw_mv_table[xy-1][0];

    int pred_fy= s->b_bidir_forw_mv_table[xy-1][1];

    int pred_bx= s->b_bidir_back_mv_table[xy-1][0];

    int pred_by= s->b_bidir_back_mv_table[xy-1][1];

    int motion_fx= s->b_bidir_forw_mv_table[xy][0]= s->b_forw_mv_table[xy][0];

    int motion_fy= s->b_bidir_forw_mv_table[xy][1]= s->b_forw_mv_table[xy][1];

    int motion_bx= s->b_bidir_back_mv_table[xy][0]= s->b_back_mv_table[xy][0];

    int motion_by= s->b_bidir_back_mv_table[xy][1]= s->b_back_mv_table[xy][1];

    const int flags= c->sub_flags;

    const int qpel= flags&FLAG_QPEL;

    const int shift= 1+qpel;

    const int xmin= c->xmin<<shift;

    const int ymin= c->ymin<<shift;

    const int xmax= c->xmax<<shift;

    const int ymax= c->ymax<<shift;

    uint8_t map[8][8][8][8];



    memset(map,0,sizeof(map));

#define BIDIR_MAP(fx,fy,bx,by) \

    map[(motion_fx+fx)&7][(motion_fy+fy)&7][(motion_bx+bx)&7][(motion_by+by)&7]

    BIDIR_MAP(0,0,0,0) = 1;



    fbmin= check_bidir_mv(s, motion_fx, motion_fy,

                          motion_bx, motion_by,

                          pred_fx, pred_fy,

                          pred_bx, pred_by,

                          0, 16);



    if(s->avctx->bidir_refine){

        int score, end;

#define CHECK_BIDIR(fx,fy,bx,by)\

    if( !BIDIR_MAP(fx,fy,bx,by)\

       &&(fx<=0 || motion_fx+fx<=xmax) && (fy<=0 || motion_fy+fy<=ymax) && (bx<=0 || motion_bx+bx<=xmax) && (by<=0 || motion_by+by<=ymax)\

       &&(fx>=0 || motion_fx+fx>=xmin) && (fy>=0 || motion_fy+fy>=ymin) && (bx>=0 || motion_bx+bx>=xmin) && (by>=0 || motion_by+by>=ymin)){\

        BIDIR_MAP(fx,fy,bx,by) = 1;\

        score= check_bidir_mv(s, motion_fx+fx, motion_fy+fy, motion_bx+bx, motion_by+by, pred_fx, pred_fy, pred_bx, pred_by, 0, 16);\

        if(score < fbmin){\

            fbmin= score;\

            motion_fx+=fx;\

            motion_fy+=fy;\

            motion_bx+=bx;\

            motion_by+=by;\

            end=0;\

        }\

    }

#define CHECK_BIDIR2(a,b,c,d)\

CHECK_BIDIR(a,b,c,d)\

CHECK_BIDIR(-a,-b,-c,-d)



#define CHECK_BIDIRR(a,b,c,d)\

CHECK_BIDIR2(a,b,c,d)\

CHECK_BIDIR2(b,c,d,a)\

CHECK_BIDIR2(c,d,a,b)\

CHECK_BIDIR2(d,a,b,c)



        do{

            end=1;



            CHECK_BIDIRR( 0, 0, 0, 1)

            if(s->avctx->bidir_refine > 1){

                CHECK_BIDIRR( 0, 0, 1, 1)

                CHECK_BIDIR2( 0, 1, 0, 1)

                CHECK_BIDIR2( 1, 0, 1, 0)

                CHECK_BIDIRR( 0, 0,-1, 1)

                CHECK_BIDIR2( 0,-1, 0, 1)

                CHECK_BIDIR2(-1, 0, 1, 0)

                if(s->avctx->bidir_refine > 2){

                    CHECK_BIDIRR( 0, 1, 1, 1)

                    CHECK_BIDIRR( 0,-1, 1, 1)

                    CHECK_BIDIRR( 0, 1,-1, 1)

                    CHECK_BIDIRR( 0, 1, 1,-1)

                    if(s->avctx->bidir_refine > 3){

                        CHECK_BIDIR2( 1, 1, 1, 1)

                        CHECK_BIDIRR( 1, 1, 1,-1)

                        CHECK_BIDIR2( 1, 1,-1,-1)

                        CHECK_BIDIR2( 1,-1,-1, 1)

                        CHECK_BIDIR2( 1,-1, 1,-1)

                    }

                }

            }

        }while(!end);

    }



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

    s->b_bidir_forw_mv_table[xy][1]= motion_fy;

    s->b_bidir_back_mv_table[xy][0]= motion_bx;

    s->b_bidir_back_mv_table[xy][1]= motion_by;



    return fbmin;

}

static inline int FUNC_0(MpegEncContext * VAR_0, int VAR_1, int VAR_2)

{

    MotionEstContext * const c= &VAR_0->me;

    const int VAR_3 = VAR_0->mb_stride;

    const int VAR_4 = VAR_2 *VAR_3 + VAR_1;

    int VAR_5;

    int VAR_6= VAR_0->b_bidir_forw_mv_table[VAR_4-1][0];

    int VAR_7= VAR_0->b_bidir_forw_mv_table[VAR_4-1][1];

    int VAR_8= VAR_0->b_bidir_back_mv_table[VAR_4-1][0];

    int VAR_9= VAR_0->b_bidir_back_mv_table[VAR_4-1][1];

    int VAR_10= VAR_0->b_bidir_forw_mv_table[VAR_4][0]= VAR_0->b_forw_mv_table[VAR_4][0];

    int VAR_11= VAR_0->b_bidir_forw_mv_table[VAR_4][1]= VAR_0->b_forw_mv_table[VAR_4][1];

    int VAR_12= VAR_0->b_bidir_back_mv_table[VAR_4][0]= VAR_0->b_back_mv_table[VAR_4][0];

    int VAR_13= VAR_0->b_bidir_back_mv_table[VAR_4][1]= VAR_0->b_back_mv_table[VAR_4][1];

    const int VAR_14= c->sub_flags;

    const int VAR_15= VAR_14&FLAG_QPEL;

    const int VAR_16= 1+VAR_15;

    const int VAR_17= c->VAR_17<<VAR_16;

    const int VAR_18= c->VAR_18<<VAR_16;

    const int VAR_19= c->VAR_19<<VAR_16;

    const int VAR_20= c->VAR_20<<VAR_16;

    uint8_t map[8][8][8][8];



    memset(map,0,sizeof(map));

#define BIDIR_MAP(fx,fy,bx,by) \

    map[(VAR_10+fx)&7][(VAR_11+fy)&7][(VAR_12+bx)&7][(VAR_13+by)&7]

    BIDIR_MAP(0,0,0,0) = 1;



    VAR_5= check_bidir_mv(VAR_0, VAR_10, VAR_11,

                          VAR_12, VAR_13,

                          VAR_6, VAR_7,

                          VAR_8, VAR_9,

                          0, 16);



    if(VAR_0->avctx->FUNC_0){

        int VAR_21, VAR_22;

#define CHECK_BIDIR(fx,fy,bx,by)\

    if( !BIDIR_MAP(fx,fy,bx,by)\

       &&(fx<=0 || VAR_10+fx<=VAR_19) && (fy<=0 || VAR_11+fy<=VAR_20) && (bx<=0 || VAR_12+bx<=VAR_19) && (by<=0 || VAR_13+by<=VAR_20)\

       &&(fx>=0 || VAR_10+fx>=VAR_17) && (fy>=0 || VAR_11+fy>=VAR_18) && (bx>=0 || VAR_12+bx>=VAR_17) && (by>=0 || VAR_13+by>=VAR_18)){\

        BIDIR_MAP(fx,fy,bx,by) = 1;\

        VAR_21= check_bidir_mv(VAR_0, VAR_10+fx, VAR_11+fy, VAR_12+bx, VAR_13+by, VAR_6, VAR_7, VAR_8, VAR_9, 0, 16);\

        if(VAR_21 < VAR_5){\

            VAR_5= VAR_21;\

            VAR_10+=fx;\

            VAR_11+=fy;\

            VAR_12+=bx;\

            VAR_13+=by;\

            VAR_22=0;\

        }\

    }

#define CHECK_BIDIR2(a,b,c,d)\

CHECK_BIDIR(a,b,c,d)\

CHECK_BIDIR(-a,-b,-c,-d)



#define CHECK_BIDIRR(a,b,c,d)\

CHECK_BIDIR2(a,b,c,d)\

CHECK_BIDIR2(b,c,d,a)\

CHECK_BIDIR2(c,d,a,b)\

CHECK_BIDIR2(d,a,b,c)



        do{

            VAR_22=1;



            CHECK_BIDIRR( 0, 0, 0, 1)

            if(VAR_0->avctx->FUNC_0 > 1){

                CHECK_BIDIRR( 0, 0, 1, 1)

                CHECK_BIDIR2( 0, 1, 0, 1)

                CHECK_BIDIR2( 1, 0, 1, 0)

                CHECK_BIDIRR( 0, 0,-1, 1)

                CHECK_BIDIR2( 0,-1, 0, 1)

                CHECK_BIDIR2(-1, 0, 1, 0)

                if(VAR_0->avctx->FUNC_0 > 2){

                    CHECK_BIDIRR( 0, 1, 1, 1)

                    CHECK_BIDIRR( 0,-1, 1, 1)

                    CHECK_BIDIRR( 0, 1,-1, 1)

                    CHECK_BIDIRR( 0, 1, 1,-1)

                    if(VAR_0->avctx->FUNC_0 > 3){

                        CHECK_BIDIR2( 1, 1, 1, 1)

                        CHECK_BIDIRR( 1, 1, 1,-1)

                        CHECK_BIDIR2( 1, 1,-1,-1)

                        CHECK_BIDIR2( 1,-1,-1, 1)

                        CHECK_BIDIR2( 1,-1, 1,-1)

                    }

                }

            }

        }while(!VAR_22);

    }



    VAR_0->b_bidir_forw_mv_table[VAR_4][0]= VAR_10;

    VAR_0->b_bidir_forw_mv_table[VAR_4][1]= VAR_11;

    VAR_0->b_bidir_back_mv_table[VAR_4][0]= VAR_12;

    VAR_0->b_bidir_back_mv_table[VAR_4][1]= VAR_13;



    return VAR_5;

}

static inline int FUNC_0(MpegEncContext * VAR_0, int VAR_1, int VAR_2)
{ MotionEstContext * const c= &VAR_0->me; const int VAR_3 = VAR_0->mb_stride; const int VAR_4 = VAR_2 *VAR_3 + VAR_1; int VAR_5; int VAR_6= VAR_0->b_bidir_forw_mv_table[VAR_4-1][0]; int VAR_7= VAR_0->b_bidir_forw_mv_table[VAR_4-1][1]; int VAR_8= VAR_0->b_bidir_back_mv_table[VAR_4-1][0]; int VAR_9= VAR_0->b_bidir_back_mv_table[VAR_4-1][1]; int VAR_10= VAR_0->b_bidir_forw_mv_table[VAR_4][0]= VAR_0->b_forw_mv_table[VAR_4][0]; int VAR_11= VAR_0->b_bidir_forw_mv_table[VAR_4][1]= VAR_0->b_forw_mv_table[VAR_4][1]; int VAR_12= VAR_0->b_bidir_back_mv_table[VAR_4][0]= VAR_0->b_back_mv_table[VAR_4][0]; int VAR_13= VAR_0->b_bidir_back_mv_table[VAR_4][1]= VAR_0->b_back_mv_table[VAR_4][1]; const int VAR_14= c->sub_flags; const int VAR_15= VAR_14&FLAG_QPEL; const int VAR_16= 1+VAR_15; const int VAR_17= c->VAR_17<<VAR_16; const int VAR_18= c->VAR_18<<VAR_16; const int VAR_19= c->VAR_19<<VAR_16; const int VAR_20= c->VAR_20<<VAR_16; uint8_t map[8][8][8][8]; memset(map,0,sizeof(map)); #define BIDIR_MAP(fx,fy,bx,by) \
map[(VAR_10+fx)&7][(VAR_11+fy)&7][(VAR_12+bx)&7][(VAR_13+by)&7]
BIDIR_MAP(0,0,0,0) = 1; VAR_5= check_bidir_mv(VAR_0, VAR_10, VAR_11,
VAR_12, VAR_13,
VAR_6, VAR_7,
VAR_8, VAR_9,
0, 16); if(VAR_0->avctx->FUNC_0){ int VAR_21, VAR_22; #define CHECK_BIDIR(fx,fy,bx,by)\
if( !BIDIR_MAP(fx,fy,bx,by)\
&&(fx<=0 || VAR_10+fx<=VAR_19) && (fy<=0 || VAR_11+fy<=VAR_20) && (bx<=0 || VAR_12+bx<=VAR_19) && (by<=0 || VAR_13+by<=VAR_20)\
&&(fx>=0 || VAR_10+fx>=VAR_17) && (fy>=0 || VAR_11+fy>=VAR_18) && (bx>=0 || VAR_12+bx>=VAR_17) && (by>=0 || VAR_13+by>=VAR_18)){\ BIDIR_MAP(fx,fy,bx,by) = 1;\ VAR_21= check_bidir_mv(VAR_0, VAR_10+fx, VAR_11+fy, VAR_12+bx, VAR_13+by, VAR_6, VAR_7, VAR_8, VAR_9, 0, 16);\ if(VAR_21 < VAR_5){\ VAR_5= VAR_21;\ VAR_10+=fx;\ VAR_11+=fy;\ VAR_12+=bx;\ VAR_13+=by;\ VAR_22=0;\ }\ } #define CHECK_BIDIR2(a,b,c,d)\
CHECK_BIDIR(a,b,c,d)\
CHECK_BIDIR(-a,-b,-c,-d)
#define CHECK_BIDIRR(a,b,c,d)\
CHECK_BIDIR2(a,b,c,d)\
CHECK_BIDIR2(b,c,d,a)\
CHECK_BIDIR2(c,d,a,b)\
CHECK_BIDIR2(d,a,b,c)
do{ VAR_22=1; CHECK_BIDIRR( 0, 0, 0, 1)
if(VAR_0->avctx->FUNC_0 > 1){ CHECK_BIDIRR( 0, 0, 1, 1)
CHECK_BIDIR2( 0, 1, 0, 1)
CHECK_BIDIR2( 1, 0, 1, 0)
CHECK_BIDIRR( 0, 0,-1, 1)
CHECK_BIDIR2( 0,-1, 0, 1)
CHECK_BIDIR2(-1, 0, 1, 0)
if(VAR_0->avctx->FUNC_0 > 2){ CHECK_BIDIRR( 0, 1, 1, 1)
CHECK_BIDIRR( 0,-1, 1, 1)
CHECK_BIDIRR( 0, 1,-1, 1)
CHECK_BIDIRR( 0, 1, 1,-1)
if(VAR_0->avctx->FUNC_0 > 3){ CHECK_BIDIR2( 1, 1, 1, 1)
CHECK_BIDIRR( 1, 1, 1,-1)
CHECK_BIDIR2( 1, 1,-1,-1)
CHECK_BIDIR2( 1,-1,-1, 1)
CHECK_BIDIR2( 1,-1, 1,-1)
} } } }while(!VAR_22); } VAR_0->b_bidir_forw_mv_table[VAR_4][0]= VAR_10; VAR_0->b_bidir_forw_mv_table[VAR_4][1]= VAR_11; VAR_0->b_bidir_back_mv_table[VAR_4][0]= VAR_12; VAR_0->b_bidir_back_mv_table[VAR_4][1]= VAR_13; return VAR_5; }