]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/motion_est.c
Ministry of English Composition, reporting for duty (and the word is "skipped", not...
[ffmpeg] / libavcodec / motion_est.c
index 65b21b0248eab65e0fcc83eab60faa1fa2c74067..8fbbdf5d22d063b2e0f0ceaae6bb538472f96c1c 100644 (file)
@@ -49,14 +49,14 @@ static inline int sad_hpel_motion_search(MpegEncContext * s,
                                   int src_index, int ref_index,
                                   int size, int h);
 
-static inline int update_map_generation(MpegEncContext * s)
+static inline int update_map_generation(MotionEstContext *c)
 {
-    s->me.map_generation+= 1<<(ME_MAP_MV_BITS*2);
-    if(s->me.map_generation==0){
-        s->me.map_generation= 1<<(ME_MAP_MV_BITS*2);
-        memset(s->me.map, 0, sizeof(uint32_t)*ME_MAP_SIZE);
+    c->map_generation+= 1<<(ME_MAP_MV_BITS*2);
+    if(c->map_generation==0){
+        c->map_generation= 1<<(ME_MAP_MV_BITS*2);
+        memset(c->map, 0, sizeof(uint32_t)*ME_MAP_SIZE);
     }
-    return s->me.map_generation;
+    return c->map_generation;
 }
 
 /* shape adaptive search stuff */
@@ -77,8 +77,7 @@ static int minima_cmp(const void *a, const void *b){
 #define FLAG_CHROMA 2
 #define FLAG_DIRECT 4
 
-static inline void init_ref(MpegEncContext *s, uint8_t *src[3], uint8_t *ref[3], uint8_t *ref2[3], int x, int y, int ref_index){
-    MotionEstContext * const c= &s->me;
+static inline void init_ref(MotionEstContext *c, uint8_t *src[3], uint8_t *ref[3], uint8_t *ref2[3], int x, int y, int ref_index){
     const int offset[3]= {
           y*c->  stride + x,
         ((y*c->uvstride + x)>>1),
@@ -96,33 +95,12 @@ static inline void init_ref(MpegEncContext *s, uint8_t *src[3], uint8_t *ref[3],
     }
 }
 
-static int get_flags(MpegEncContext *s, int direct, int chroma){
-    return   ((s->flags&CODEC_FLAG_QPEL) ? FLAG_QPEL : 0)
+static int get_flags(MotionEstContext *c, int direct, int chroma){
+    return   ((c->avctx->flags&CODEC_FLAG_QPEL) ? FLAG_QPEL : 0)
            + (direct ? FLAG_DIRECT : 0) 
            + (chroma ? FLAG_CHROMA : 0);
 }
 
-static inline void init_mc(MpegEncContext *s, int size, int flags){
-    MotionEstContext * const c= &s->me;
-
-/*FIXME s->no_rounding b_type*/
-    if(flags & FLAG_CHROMA){
-        if(s->no_rounding) c->chroma_hpel_put= &s->dsp.put_no_rnd_pixels_tab[size+1];
-        else               c->chroma_hpel_put= &s->dsp.put_pixels_tab[size+1];
-    }
-
-    if(flags & FLAG_QPEL){
-        c->qpel_avg= &s->dsp.avg_qpel_pixels_tab[size];
-        if(s->no_rounding) c->qpel_put= &s->dsp.put_no_rnd_qpel_pixels_tab[size];
-        else               c->qpel_put= &s->dsp.put_qpel_pixels_tab[size];
-    }else{
-        c->hpel_avg= &s->dsp.avg_pixels_tab[size];
-        if(s->no_rounding) c->hpel_put= &s->dsp.put_no_rnd_pixels_tab[size];
-        else               c->hpel_put= &s->dsp.put_pixels_tab[size];
-    }
-    c->temp= c->scratchpad;
-}
-
 static always_inline int cmp(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
                       const int size, const int h, int ref_index, int src_index,
                       me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
@@ -241,54 +219,71 @@ static always_inline int cmp(MpegEncContext *s, const int x, const int y, const
 
 #include "motion_est_template.c"
 
-static inline int get_penalty_factor(MpegEncContext *s, int type){
-    switch(type&0xFF){
-    default:
-    case FF_CMP_SAD:
-        return s->qscale*2;
-    case FF_CMP_DCT:
-        return s->qscale*3;
-    case FF_CMP_SATD:
-        return s->qscale*6;
-    case FF_CMP_SSE:
-        return s->qscale*s->qscale*2;
-    case FF_CMP_BIT:
-        return 1;
-    case FF_CMP_RD:
-    case FF_CMP_PSNR:
-        return (s->qscale*s->qscale*185 + 64)>>7;
-    }
+static int zero_cmp(void *s, uint8_t *a, uint8_t *b, int stride, int h){
+    return 0;
+}
+
+static void zero_hpel(uint8_t *a, const uint8_t *b, int stride, int h){
 }
 
 void ff_init_me(MpegEncContext *s){
-    ff_set_cmp(&s->dsp, s->dsp.me_pre_cmp, s->avctx->me_pre_cmp);
-    ff_set_cmp(&s->dsp, s->dsp.me_cmp, s->avctx->me_cmp);
-    ff_set_cmp(&s->dsp, s->dsp.me_sub_cmp, s->avctx->me_sub_cmp);
-    ff_set_cmp(&s->dsp, s->dsp.mb_cmp, s->avctx->mb_cmp);
+    MotionEstContext * const c= &s->me;
+    c->avctx= s->avctx;
+
+    ff_set_cmp(&s->dsp, s->dsp.me_pre_cmp, c->avctx->me_pre_cmp);
+    ff_set_cmp(&s->dsp, s->dsp.me_cmp, c->avctx->me_cmp);
+    ff_set_cmp(&s->dsp, s->dsp.me_sub_cmp, c->avctx->me_sub_cmp);
+    ff_set_cmp(&s->dsp, s->dsp.mb_cmp, c->avctx->mb_cmp);
     
-    s->me.flags    = get_flags(s, 0, s->avctx->me_cmp    &FF_CMP_CHROMA);
-    s->me.sub_flags= get_flags(s, 0, s->avctx->me_sub_cmp&FF_CMP_CHROMA);
-    s->me.mb_flags = get_flags(s, 0, s->avctx->mb_cmp    &FF_CMP_CHROMA);
+    c->flags    = get_flags(c, 0, c->avctx->me_cmp    &FF_CMP_CHROMA);
+    c->sub_flags= get_flags(c, 0, c->avctx->me_sub_cmp&FF_CMP_CHROMA);
+    c->mb_flags = get_flags(c, 0, c->avctx->mb_cmp    &FF_CMP_CHROMA);
 
+/*FIXME s->no_rounding b_type*/
     if(s->flags&CODEC_FLAG_QPEL){
-        s->me.sub_motion_search= qpel_motion_search;
+        c->sub_motion_search= qpel_motion_search;
+        c->qpel_avg= s->dsp.avg_qpel_pixels_tab;
+        if(s->no_rounding) c->qpel_put= s->dsp.put_no_rnd_qpel_pixels_tab;
+        else               c->qpel_put= s->dsp.put_qpel_pixels_tab;
     }else{
-        if(s->avctx->me_sub_cmp&FF_CMP_CHROMA)
-            s->me.sub_motion_search= hpel_motion_search;
-        else if(   s->avctx->me_sub_cmp == FF_CMP_SAD 
-                && s->avctx->    me_cmp == FF_CMP_SAD 
-                && s->avctx->    mb_cmp == FF_CMP_SAD)
-            s->me.sub_motion_search= sad_hpel_motion_search; // 2050 vs. 2450 cycles
+        if(c->avctx->me_sub_cmp&FF_CMP_CHROMA)
+            c->sub_motion_search= hpel_motion_search;
+        else if(   c->avctx->me_sub_cmp == FF_CMP_SAD 
+                && c->avctx->    me_cmp == FF_CMP_SAD 
+                && c->avctx->    mb_cmp == FF_CMP_SAD)
+            c->sub_motion_search= sad_hpel_motion_search; // 2050 vs. 2450 cycles
         else
-            s->me.sub_motion_search= hpel_motion_search;
+            c->sub_motion_search= hpel_motion_search;
     }
+    c->hpel_avg= s->dsp.avg_pixels_tab;
+    if(s->no_rounding) c->hpel_put= s->dsp.put_no_rnd_pixels_tab;
+    else               c->hpel_put= s->dsp.put_pixels_tab;
+
     if(s->linesize){
-        s->me.stride  = s->linesize; 
-        s->me.uvstride= s->uvlinesize;
+        c->stride  = s->linesize; 
+        c->uvstride= s->uvlinesize;
     }else{
-        s->me.stride  = 16*s->mb_width + 32;
-        s->me.uvstride=  8*s->mb_width + 16;
+        c->stride  = 16*s->mb_width + 32;
+        c->uvstride=  8*s->mb_width + 16;
+    }
+
+    // 8x8 fullpel search would need a 4x4 chroma compare, which we dont have yet, and even if we had the motion estimation code doesnt expect it
+    if(s->codec_id != CODEC_ID_SNOW){
+        if((c->avctx->me_cmp&FF_CMP_CHROMA) && !s->dsp.me_cmp[2]){
+            s->dsp.me_cmp[2]= zero_cmp;
+        }
+        if((c->avctx->me_sub_cmp&FF_CMP_CHROMA) && !s->dsp.me_sub_cmp[2]){
+            s->dsp.me_sub_cmp[2]= zero_cmp;
+        }
+        c->hpel_put[2][0]= c->hpel_put[2][1]=
+        c->hpel_put[2][2]= c->hpel_put[2][3]= zero_hpel;
     }
+
+    if(s->codec_id == CODEC_ID_H261){
+        c->sub_motion_search= no_sub_motion_search;
+    }
+
+    c->temp= c->scratchpad;
 }
       
 #if 0
@@ -322,6 +317,7 @@ static inline void no_motion_search(MpegEncContext * s,
     *my_ptr = 16 * s->mb_y;
 }
 
+#if 0  /* the use of these functions is inside #if 0 */
 static int full_motion_search(MpegEncContext * s,
                               int *mx_ptr, int *my_ptr, int range,
                               int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
@@ -542,7 +538,7 @@ static int phods_motion_search(MpegEncContext * s,
     *my_ptr = my;
     return dminy;
 }
-
+#endif /* 0 */
 
 #define Z_THRESHOLD 256
 
@@ -558,16 +554,17 @@ static inline int sad_hpel_motion_search(MpegEncContext * s,
                                   int src_index, int ref_index,
                                   int size, int h)
 {
-    const int penalty_factor= s->me.sub_penalty_factor;
+    MotionEstContext * const c= &s->me;
+    const int penalty_factor= c->sub_penalty_factor;
     int mx, my, dminh;
     uint8_t *pix, *ptr;
-    int stride= s->me.stride;
-    const int flags= s->me.sub_flags;
+    int stride= c->stride;
+    const int flags= c->sub_flags;
     LOAD_COMMON
     
     assert(flags == 0);
 
-    if(s->me.skip){
+    if(c->skip){
 //    printf("S");
         *mx_ptr = 0;
         *my_ptr = 0;
@@ -575,11 +572,11 @@ static inline int sad_hpel_motion_search(MpegEncContext * s,
     }
 //    printf("N");
         
-    pix = s->me.src[src_index][0];
+    pix = c->src[src_index][0];
 
     mx = *mx_ptr;
     my = *my_ptr;
-    ptr = s->me.ref[ref_index][0] + (my * stride) + mx;
+    ptr = c->ref[ref_index][0] + (my * stride) + mx;
     
     dminh = dmin;
 
@@ -689,23 +686,41 @@ static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my, int mv4)
  */
 static inline void get_limits(MpegEncContext *s, int x, int y)
 {
+    MotionEstContext * const c= &s->me;
 /*
-    if(s->avctx->me_range) s->me.range= s->avctx->me_range >> 1;
-    else                   s->me.range= 16;
+    if(c->avctx->me_range) c->range= c->avctx->me_range >> 1;
+    else                   c->range= 16;
 */
     if (s->unrestricted_mv) {
-        s->me.xmin = - x - 16;
-        s->me.ymin = - y - 16;
-        s->me.xmax = - x + s->mb_width *16;
-        s->me.ymax = - y + s->mb_height*16;
+        c->xmin = - x - 16;
+        c->ymin = - y - 16;
+        c->xmax = - x + s->mb_width *16;
+        c->ymax = - y + s->mb_height*16;
+    } else if (s->out_format == FMT_H261){
+        // Search range of H261 is different from other codec standards
+        c->xmin = (x > 15) ? - 15 : 0;
+        c->ymin = (y > 15) ? - 15 : 0;
+        c->xmax = (x < s->mb_width * 16 - 16) ? 15 : 0;              
+        c->ymax = (y < s->mb_height * 16 - 16) ? 15 : 0;
     } else {
-        s->me.xmin = - x;
-        s->me.ymin = - y;
-        s->me.xmax = - x + s->mb_width *16 - 16;
-        s->me.ymax = - y + s->mb_height*16 - 16;
+        c->xmin = - x;
+        c->ymin = - y;
+        c->xmax = - x + s->mb_width *16 - 16;
+        c->ymax = - y + s->mb_height*16 - 16;
     }
 }
 
+static inline void init_mv4_ref(MotionEstContext *c){
+    const int stride= c->stride;
+
+    c->ref[1][0] = c->ref[0][0] + 8;
+    c->ref[2][0] = c->ref[0][0] + 8*stride;
+    c->ref[3][0] = c->ref[2][0] + 8;
+    c->src[1][0] = c->src[0][0] + 8;
+    c->src[2][0] = c->src[0][0] + 8*stride;
+    c->src[3][0] = c->src[2][0] + 8;
+}
+
 static inline int h263_mv4_search(MpegEncContext *s, int mx, int my, int shift)
 {
     MotionEstContext * const c= &s->me;
@@ -715,17 +730,10 @@ static inline int h263_mv4_search(MpegEncContext *s, int mx, int my, int shift)
     int P[10][2];
     int dmin_sum=0, mx4_sum=0, my4_sum=0;
     int same=1;
-    const int stride= s->linesize;
-    const int uvstride= s->uvlinesize;
-    uint8_t *mv_penalty= s->me.current_mv_penalty;
+    const int stride= c->stride;
+    uint8_t *mv_penalty= c->current_mv_penalty;
 
-    c->ref[1][0] = c->ref[0][0] + 8;
-    c->ref[2][0] = c->ref[0][0] + 8*stride;
-    c->ref[3][0] = c->ref[2][0] + 8;
-    c->src[1][0] = c->src[0][0] + 8;
-    c->src[2][0] = c->src[0][0] + 8*stride;
-    c->src[3][0] = c->src[2][0] + 8;
-    init_mc(s, size, c->flags);
+    init_mv4_ref(c);
     
     for(block=0; block<4; block++){
         int mx4, my4;
@@ -738,39 +746,39 @@ static inline int h263_mv4_search(MpegEncContext *s, int mx, int my, int shift)
         P_LEFT[0] = s->current_picture.motion_val[0][mot_xy - 1][0];
         P_LEFT[1] = s->current_picture.motion_val[0][mot_xy - 1][1];
 
-        if(P_LEFT[0]       > (s->me.xmax<<shift)) P_LEFT[0]       = (s->me.xmax<<shift);
+        if(P_LEFT[0]       > (c->xmax<<shift)) P_LEFT[0]       = (c->xmax<<shift);
 
         /* special case for first line */
         if (s->first_slice_line && block<2) {
-            s->me.pred_x= pred_x4= P_LEFT[0];
-            s->me.pred_y= pred_y4= P_LEFT[1];
+            c->pred_x= pred_x4= P_LEFT[0];
+            c->pred_y= pred_y4= P_LEFT[1];
         } else {
             P_TOP[0]      = s->current_picture.motion_val[0][mot_xy - mot_stride             ][0];
             P_TOP[1]      = s->current_picture.motion_val[0][mot_xy - mot_stride             ][1];
             P_TOPRIGHT[0] = s->current_picture.motion_val[0][mot_xy - mot_stride + off[block]][0];
             P_TOPRIGHT[1] = s->current_picture.motion_val[0][mot_xy - mot_stride + off[block]][1];
-            if(P_TOP[1]      > (s->me.ymax<<shift)) P_TOP[1]     = (s->me.ymax<<shift);
-            if(P_TOPRIGHT[0] < (s->me.xmin<<shift)) P_TOPRIGHT[0]= (s->me.xmin<<shift);
-            if(P_TOPRIGHT[0] > (s->me.xmax<<shift)) P_TOPRIGHT[0]= (s->me.xmax<<shift);
-            if(P_TOPRIGHT[1] > (s->me.ymax<<shift)) P_TOPRIGHT[1]= (s->me.ymax<<shift);
+            if(P_TOP[1]      > (c->ymax<<shift)) P_TOP[1]     = (c->ymax<<shift);
+            if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
+            if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift);
+            if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
     
             P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
             P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
 
-            s->me.pred_x= pred_x4 = P_MEDIAN[0];
-            s->me.pred_y= pred_y4 = P_MEDIAN[1];
+            c->pred_x= pred_x4 = P_MEDIAN[0];
+            c->pred_y= pred_y4 = P_MEDIAN[1];
         }
         P_MV1[0]= mx;
         P_MV1[1]= my;
 
         dmin4 = epzs_motion_search4(s, &mx4, &my4, P, block, block, s->p_mv_table, (1<<16)>>shift);
 
-        dmin4= s->me.sub_motion_search(s, &mx4, &my4, dmin4, block, block, size, h);
+        dmin4= c->sub_motion_search(s, &mx4, &my4, dmin4, block, block, size, h);
         
         if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
             int dxy;
             const int offset= ((block&1) + (block>>1)*stride)*8;
-            uint8_t *dest_y = s->me.scratchpad + offset;
+            uint8_t *dest_y = c->scratchpad + offset;
             if(s->quarter_sample){
                 uint8_t *ref= c->ref[block][0] + (mx4>>2) + (my4>>2)*stride;
                 dxy = ((my4 & 3) << 2) | (mx4 & 3);
@@ -788,7 +796,7 @@ static inline int h263_mv4_search(MpegEncContext *s, int mx, int my, int shift)
                 else
                     s->dsp.put_pixels_tab       [1][dxy](dest_y    , ref    , stride, h);
             }
-            dmin_sum+= (mv_penalty[mx4-pred_x4] + mv_penalty[my4-pred_y4])*s->me.mb_penalty_factor;
+            dmin_sum+= (mv_penalty[mx4-pred_x4] + mv_penalty[my4-pred_y4])*c->mb_penalty_factor;
         }else
             dmin_sum+= dmin4;
 
@@ -810,10 +818,10 @@ static inline int h263_mv4_search(MpegEncContext *s, int mx, int my, int shift)
         return INT_MAX;
     
     if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
-        dmin_sum += s->dsp.mb_cmp[0](s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*16*stride, s->me.scratchpad, stride, 16);
+        dmin_sum += s->dsp.mb_cmp[0](s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*16*stride, c->scratchpad, stride, 16);
     }
     
-    if(s->avctx->mb_cmp&FF_CMP_CHROMA){
+    if(c->avctx->mb_cmp&FF_CMP_CHROMA){
         int dxy;
         int mx, my;
         int offset;
@@ -825,32 +833,45 @@ static inline int h263_mv4_search(MpegEncContext *s, int mx, int my, int shift)
         offset= (s->mb_x*8 + (mx>>1)) + (s->mb_y*8 + (my>>1))*s->uvlinesize;
        
         if(s->no_rounding){
-            s->dsp.put_no_rnd_pixels_tab[1][dxy](s->me.scratchpad    , s->last_picture.data[1] + offset, s->uvlinesize, 8);
-            s->dsp.put_no_rnd_pixels_tab[1][dxy](s->me.scratchpad+8  , s->last_picture.data[2] + offset, s->uvlinesize, 8);
+            s->dsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad    , s->last_picture.data[1] + offset, s->uvlinesize, 8);
+            s->dsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad+8  , s->last_picture.data[2] + offset, s->uvlinesize, 8);
         }else{
-            s->dsp.put_pixels_tab       [1][dxy](s->me.scratchpad    , s->last_picture.data[1] + offset, s->uvlinesize, 8);
-            s->dsp.put_pixels_tab       [1][dxy](s->me.scratchpad+8  , s->last_picture.data[2] + offset, s->uvlinesize, 8);
+            s->dsp.put_pixels_tab       [1][dxy](c->scratchpad    , s->last_picture.data[1] + offset, s->uvlinesize, 8);
+            s->dsp.put_pixels_tab       [1][dxy](c->scratchpad+8  , s->last_picture.data[2] + offset, s->uvlinesize, 8);
         }
 
-        dmin_sum += s->dsp.mb_cmp[1](s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*8*s->uvlinesize, s->me.scratchpad  , s->uvlinesize, 8);
-        dmin_sum += s->dsp.mb_cmp[1](s, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*8*s->uvlinesize, s->me.scratchpad+8, s->uvlinesize, 8);
+        dmin_sum += s->dsp.mb_cmp[1](s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*8*s->uvlinesize, c->scratchpad  , s->uvlinesize, 8);
+        dmin_sum += s->dsp.mb_cmp[1](s, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*8*s->uvlinesize, c->scratchpad+8, s->uvlinesize, 8);
     }
     
-    s->me.pred_x= mx;
-    s->me.pred_y= my;
+    c->pred_x= mx;
+    c->pred_y= my;
 
-    switch(s->avctx->mb_cmp&0xFF){
+    switch(c->avctx->mb_cmp&0xFF){
     /*case FF_CMP_SSE:
         return dmin_sum+ 32*s->qscale*s->qscale;*/
     case FF_CMP_RD:
         return dmin_sum;
     default:
-        return dmin_sum+ 11*s->me.mb_penalty_factor;
+        return dmin_sum+ 11*c->mb_penalty_factor;
+    }
+}
+
+static inline void init_interlaced_ref(MpegEncContext *s, int ref_index){
+    MotionEstContext * const c= &s->me;
+
+    c->ref[1+ref_index][0] = c->ref[0+ref_index][0] + s->linesize;
+    c->src[1][0] = c->src[0][0] + s->linesize;
+    if(c->flags & FLAG_CHROMA){
+        c->ref[1+ref_index][1] = c->ref[0+ref_index][1] + s->uvlinesize;
+        c->ref[1+ref_index][2] = c->ref[0+ref_index][2] + s->uvlinesize;
+        c->src[1][1] = c->src[0][1] + s->uvlinesize;
+        c->src[1][2] = c->src[0][2] + s->uvlinesize;
     }
 }
 
 static int interlaced_search(MpegEncContext *s, int ref_index, 
-                             int16_t (*mv_tables[2][2])[2], uint8_t *field_select_tables[2], int mx, int my)
+                             int16_t (*mv_tables[2][2])[2], uint8_t *field_select_tables[2], int mx, int my, int user_field_select)
 {
     MotionEstContext * const c= &s->me;
     const int size=0;
@@ -860,7 +881,6 @@ static int interlaced_search(MpegEncContext *s, int ref_index,
     uint8_t * const mv_penalty= c->current_mv_penalty;
     int same=1;
     const int stride= 2*s->linesize;
-    const int uvstride= 2*s->uvlinesize;
     int dmin_sum= 0;
     const int mot_stride= s->mb_stride;
     const int xy= s->mb_x + s->mb_y*mot_stride;
@@ -869,15 +889,7 @@ static int interlaced_search(MpegEncContext *s, int ref_index,
     c->ymax>>=1;
     c->stride<<=1;
     c->uvstride<<=1;
-    c->ref[1+ref_index][0] = c->ref[0+ref_index][0] + s->linesize;
-    c->src[1][0] = c->src[0][0] + s->linesize;
-    if(c->flags & FLAG_CHROMA){
-        c->ref[1+ref_index][1] = c->ref[0+ref_index][1] + s->uvlinesize;
-        c->ref[1+ref_index][2] = c->ref[0+ref_index][2] + s->uvlinesize;
-        c->src[1][1] = c->src[0][1] + s->uvlinesize;
-        c->src[1][2] = c->src[0][2] + s->uvlinesize;
-    }
-    init_mc(s, size, c->flags);
+    init_interlaced_ref(s, ref_index);
     
     for(block=0; block<2; block++){
         int field_select;
@@ -888,12 +900,17 @@ static int interlaced_search(MpegEncContext *s, int ref_index,
             int dmin, mx_i, my_i;
             int16_t (*mv_table)[2]= mv_tables[block][field_select];
             
+            if(user_field_select){
+                if(field_select_tables[block][xy] != field_select)
+                    continue;
+            }
+            
             P_LEFT[0] = mv_table[xy - 1][0];
             P_LEFT[1] = mv_table[xy - 1][1];
             if(P_LEFT[0]       > (c->xmax<<1)) P_LEFT[0]       = (c->xmax<<1);
             
-            s->me.pred_x= P_LEFT[0];
-            s->me.pred_y= P_LEFT[1];
+            c->pred_x= P_LEFT[0];
+            c->pred_y= P_LEFT[1];
             
             if(!s->first_slice_line){
                 P_TOP[0]      = mv_table[xy - mot_stride][0];
@@ -931,7 +948,7 @@ static int interlaced_search(MpegEncContext *s, int ref_index,
                     s->dsp.put_pixels_tab       [size][dxy](c->scratchpad, ref    , stride, h);
                 }
                 dmin= s->dsp.mb_cmp[size](s, c->src[block][0], c->scratchpad, stride, h);
-                dmin+= (mv_penalty[mx_i-s->me.pred_x] + mv_penalty[my_i-s->me.pred_y] + 1)*c->mb_penalty_factor;
+                dmin+= (mv_penalty[mx_i-c->pred_x] + mv_penalty[my_i-c->pred_y] + 1)*c->mb_penalty_factor;
             }else
                 dmin+= c->mb_penalty_factor; //field_select bits
                 
@@ -963,7 +980,7 @@ static int interlaced_search(MpegEncContext *s, int ref_index,
     if(same)
         return INT_MAX;
     
-    switch(s->avctx->mb_cmp&0xFF){
+    switch(c->avctx->mb_cmp&0xFF){
     /*case FF_CMP_SSE:
         return dmin_sum+ 32*s->qscale*s->qscale;*/
     case FF_CMP_RD:
@@ -973,6 +990,16 @@ static int interlaced_search(MpegEncContext *s, int ref_index,
     }
 }
 
+static void clip_input_mv(MpegEncContext * s, int16_t *mv, int interlaced){
+    int ymax= s->me.ymax>>interlaced;
+    int ymin= s->me.ymin>>interlaced;
+    
+    if(mv[0] < s->me.xmin) mv[0] = s->me.xmin;
+    if(mv[0] > s->me.xmax) mv[0] = s->me.xmax;
+    if(mv[1] <       ymin) mv[1] =       ymin;
+    if(mv[1] >       ymax) mv[1] =       ymax;
+}
+
 static inline int check_input_motion(MpegEncContext * s, int mb_x, int mb_y, int p_type){
     MotionEstContext * const c= &s->me;
     Picture *p= s->current_picture_ptr;
@@ -982,35 +1009,41 @@ static inline int check_input_motion(MpegEncContext * s, int mb_x, int mb_y, int
     int flags= c->flags;
     int shift= (flags&FLAG_QPEL) + 1;
     int mask= (1<<shift)-1;
-    int x, y;
+    int x, y, i;
     int d=0;
     me_cmp_func cmpf= s->dsp.sse[0];
     me_cmp_func chroma_cmpf= s->dsp.sse[1];
     
-    assert(p_type==0 || !USES_LIST(mb_type, 1));
+    if(p_type && USES_LIST(mb_type, 1)){
+        av_log(c->avctx, AV_LOG_ERROR, "backward motion vector in P frame\n");
+        return INT_MAX/2;
+    }
     assert(IS_INTRA(mb_type) || USES_LIST(mb_type,0) || USES_LIST(mb_type,1));
     
+    for(i=0; i<4; i++){
+        int xy= s->block_index[i];
+        clip_input_mv(s, p->motion_val[0][xy], !!IS_INTERLACED(mb_type));
+        clip_input_mv(s, p->motion_val[1][xy], !!IS_INTERLACED(mb_type));
+    }
+
     if(IS_INTERLACED(mb_type)){
         int xy2= xy  + s->b8_stride;
         s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTRA;
         c->stride<<=1;
         c->uvstride<<=1;
-        c->ref[1][0] = c->ref[0][0] + s->linesize;
-        c->ref[3][0] = c->ref[2][0] + s->linesize;
-        c->src[1][0] = c->src[0][0] + s->linesize;
-        if(c->flags & FLAG_CHROMA){
-            c->ref[1][1] = c->ref[0][1] + s->uvlinesize;
-            c->ref[1][2] = c->ref[0][2] + s->uvlinesize;
-            c->ref[3][1] = c->ref[2][1] + s->uvlinesize;
-            c->ref[3][2] = c->ref[2][2] + s->uvlinesize;
-            c->src[1][1] = c->src[0][1] + s->uvlinesize;
-            c->src[1][2] = c->src[0][2] + s->uvlinesize;
+        
+        if(!(s->flags & CODEC_FLAG_INTERLACED_ME)){
+            av_log(c->avctx, AV_LOG_ERROR, "Interlaced macroblock selected but interlaced motion estimation disabled\n");
+            return INT_MAX/2;
         }
+
         if(USES_LIST(mb_type, 0)){
             int field_select0= p->ref_index[0][xy ];
             int field_select1= p->ref_index[0][xy2];
             assert(field_select0==0 ||field_select0==1);
             assert(field_select1==0 ||field_select1==1);
+            init_interlaced_ref(s, 0);
+
             if(p_type){
                 s->p_field_select_table[0][mb_xy]= field_select0;
                 s->p_field_select_table[1][mb_xy]= field_select1;
@@ -1037,6 +1070,8 @@ static inline int check_input_motion(MpegEncContext * s, int mb_x, int mb_y, int
             int field_select1= p->ref_index[1][xy2];
             assert(field_select0==0 ||field_select0==1);
             assert(field_select1==0 ||field_select1==1);
+            init_interlaced_ref(s, 2);
+
             s->b_field_select_table[1][0][mb_xy]= field_select0;
             s->b_field_select_table[1][1][mb_xy]= field_select1;
             *(uint32_t*)s->b_field_mv_table[1][0][field_select0][mb_xy]= *(uint32_t*)p->motion_val[1][xy ];
@@ -1057,6 +1092,21 @@ static inline int check_input_motion(MpegEncContext * s, int mb_x, int mb_y, int
         }
         c->stride>>=1;
         c->uvstride>>=1;
+    }else if(IS_8X8(mb_type)){
+        if(!(s->flags & CODEC_FLAG_4MV)){
+            av_log(c->avctx, AV_LOG_ERROR, "4MV macroblock selected but 4MV encoding disabled\n");
+            return INT_MAX/2;
+        }
+        cmpf= s->dsp.sse[1];
+        chroma_cmpf= s->dsp.sse[1];
+        init_mv4_ref(c);
+        for(i=0; i<4; i++){
+            xy= s->block_index[i];
+            x= p->motion_val[0][xy][0]; 
+            y= p->motion_val[0][xy][1];
+            d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 1, 8, i, i, cmpf, chroma_cmpf, flags);
+        }
+        s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER4V;
     }else{
         if(USES_LIST(mb_type, 0)){
             if(p_type){
@@ -1091,47 +1141,50 @@ void ff_estimate_p_frame_motion(MpegEncContext * s,
 {
     MotionEstContext * const c= &s->me;
     uint8_t *pix, *ppix;
-    int sum, varc, vard, mx, my, dmin, xx, yy;
+    int sum, varc, vard, mx, my, dmin;
     int P[10][2];
     const int shift= 1+s->quarter_sample;
     int mb_type=0;
     Picture * const pic= &s->current_picture;
     
-    init_ref(s, s->new_picture.data, s->last_picture.data, NULL, 16*mb_x, 16*mb_y, 0);
-    init_mc(s, 0, c->flags);
+    init_ref(c, s->new_picture.data, s->last_picture.data, NULL, 16*mb_x, 16*mb_y, 0);
 
     assert(s->quarter_sample==0 || s->quarter_sample==1);
-    assert(s->linesize == s->me.stride);
-    assert(s->uvlinesize == s->me.uvstride);
+    assert(s->linesize == c->stride);
+    assert(s->uvlinesize == c->uvstride);
 
-    s->me.penalty_factor    = get_penalty_factor(s, s->avctx->me_cmp);
-    s->me.sub_penalty_factor= get_penalty_factor(s, s->avctx->me_sub_cmp);
-    s->me.mb_penalty_factor = get_penalty_factor(s, s->avctx->mb_cmp);
-    s->me.current_mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV;
+    c->penalty_factor    = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
+    c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
+    c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
+    c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
 
     get_limits(s, 16*mb_x, 16*mb_y);
-    s->me.skip=0;
+    c->skip=0;
+
+    /* intra / predictive decision */
+    pix = c->src[0][0];
+    sum = s->dsp.pix_sum(pix, s->linesize);
+    varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
 
-    if(s->avctx->me_threshold){
+    pic->mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
+    pic->mb_var [s->mb_stride * mb_y + mb_x] = varc;
+    c->mb_var_sum_temp += varc;
+
+    if(c->avctx->me_threshold){
         vard= (check_input_motion(s, mb_x, mb_y, 1)+128)>>8;
         
-        if(vard<s->avctx->me_threshold){
-            pix = c->src[0][0];
-            sum = s->dsp.pix_sum(pix, s->linesize);
-            varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
-        
-            pic->mb_var   [s->mb_stride * mb_y + mb_x] = varc;
+        if(vard<c->avctx->me_threshold){
             pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = vard;
-            pic->mb_mean  [s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
-            s->mb_var_sum_temp    += varc;
-            s->mc_mb_var_sum_temp += vard;
+            c->mc_mb_var_sum_temp += vard;
             if (vard <= 64 || vard < varc) { //FIXME
-                s->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
+                c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
             }else{
-                s->scene_change_score+= s->qscale;
+                c->scene_change_score+= s->qscale;
             }
             return;
         }
+        if(vard<c->avctx->mb_threshold)
+            mb_type= s->mb_type[mb_x + mb_y*s->mb_stride];
     }
 
     switch(s->me_method) {
@@ -1168,16 +1221,16 @@ void ff_estimate_p_frame_motion(MpegEncContext * s,
             P_LEFT[0]       = s->current_picture.motion_val[0][mot_xy - 1][0];
             P_LEFT[1]       = s->current_picture.motion_val[0][mot_xy - 1][1];
 
-            if(P_LEFT[0]       > (s->me.xmax<<shift)) P_LEFT[0]       = (s->me.xmax<<shift);
+            if(P_LEFT[0]       > (c->xmax<<shift)) P_LEFT[0]       = (c->xmax<<shift);
 
             if(!s->first_slice_line) {
                 P_TOP[0]      = s->current_picture.motion_val[0][mot_xy - mot_stride    ][0];
                 P_TOP[1]      = s->current_picture.motion_val[0][mot_xy - mot_stride    ][1];
                 P_TOPRIGHT[0] = s->current_picture.motion_val[0][mot_xy - mot_stride + 2][0];
                 P_TOPRIGHT[1] = s->current_picture.motion_val[0][mot_xy - mot_stride + 2][1];
-                if(P_TOP[1]      > (s->me.ymax<<shift)) P_TOP[1]     = (s->me.ymax<<shift);
-                if(P_TOPRIGHT[0] < (s->me.xmin<<shift)) P_TOPRIGHT[0]= (s->me.xmin<<shift);
-                if(P_TOPRIGHT[1] > (s->me.ymax<<shift)) P_TOPRIGHT[1]= (s->me.ymax<<shift);
+                if(P_TOP[1]      > (c->ymax<<shift)) P_TOP[1]     = (c->ymax<<shift);
+                if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
+                if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
         
                 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
                 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
@@ -1195,57 +1248,65 @@ void ff_estimate_p_frame_motion(MpegEncContext * s,
             }
 
         }
-        dmin = epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift);       
+        dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);       
 
         break;
     }
 
-    /* intra / predictive decision */
-    xx = mb_x * 16;
-    yy = mb_y * 16;
-
-    pix = c->src[0][0];
     /* At this point (mx,my) are full-pell and the relative displacement */
     ppix = c->ref[0][0] + (my * s->linesize) + mx;
-    
-    sum = s->dsp.pix_sum(pix, s->linesize);
-    
-    varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
+        
     vard = (s->dsp.sse[0](NULL, pix, ppix, s->linesize, 16)+128)>>8;
 
-//printf("%d %d %d %X %X %X\n", s->mb_width, mb_x, mb_y,(int)s, (int)s->mb_var, (int)s->mc_mb_var); fflush(stdout);
-    pic->mb_var   [s->mb_stride * mb_y + mb_x] = varc;
     pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = vard;
-    pic->mb_mean  [s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
 //    pic->mb_cmp_score[s->mb_stride * mb_y + mb_x] = dmin; 
-    s->mb_var_sum_temp    += varc;
-    s->mc_mb_var_sum_temp += vard;
-//printf("E%d %d %d %X %X %X\n", s->mb_width, mb_x, mb_y,(int)s, (int)s->mb_var, (int)s->mc_mb_var); fflush(stdout);
+    c->mc_mb_var_sum_temp += vard;
     
 #if 0
     printf("varc=%4d avg_var=%4d (sum=%4d) vard=%4d mx=%2d my=%2d\n",
           varc, s->avg_mb_var, sum, vard, mx - xx, my - yy);
 #endif
-    if(s->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
+    if(mb_type){
         if (vard <= 64 || vard < varc)
-            s->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
+            c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
         else
-            s->scene_change_score+= s->qscale;
+            c->scene_change_score+= s->qscale;
+
+        if(mb_type == CANDIDATE_MB_TYPE_INTER){
+            c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
+            set_p_mv_tables(s, mx, my, 1);
+        }else{
+            mx <<=shift;
+            my <<=shift;
+        }
+        if(mb_type == CANDIDATE_MB_TYPE_INTER4V){
+            h263_mv4_search(s, mx, my, shift);
+
+            set_p_mv_tables(s, mx, my, 0);
+        }
+        if(mb_type == CANDIDATE_MB_TYPE_INTER_I){
+            interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 1);
+        }
+    }else if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
+        if (vard <= 64 || vard < varc)
+            c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
+        else
+            c->scene_change_score+= s->qscale;
 
         if (vard*2 + 200 > varc)
             mb_type|= CANDIDATE_MB_TYPE_INTRA;
         if (varc*2 + 200 > vard){
             mb_type|= CANDIDATE_MB_TYPE_INTER;
-            s->me.sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
+            c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
             if(s->flags&CODEC_FLAG_MV0)
                 if(mx || my)
-                    mb_type |= CANDIDATE_MB_TYPE_SKIPED; //FIXME check difference
+                    mb_type |= CANDIDATE_MB_TYPE_SKIPPED; //FIXME check difference
         }else{
             mx <<=shift;
             my <<=shift;
         }
         if((s->flags&CODEC_FLAG_4MV)
-           && !s->me.skip && varc>50 && vard>10){
+           && !c->skip && varc>50 && vard>10){
             if(h263_mv4_search(s, mx, my, shift) < INT_MAX)
                 mb_type|=CANDIDATE_MB_TYPE_INTER4V;
 
@@ -1253,20 +1314,20 @@ void ff_estimate_p_frame_motion(MpegEncContext * s,
         }else
             set_p_mv_tables(s, mx, my, 1);
         if((s->flags&CODEC_FLAG_INTERLACED_ME)
-           && !s->me.skip){ //FIXME varc/d checks
-            if(interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my) < INT_MAX)
+           && !c->skip){ //FIXME varc/d checks
+            if(interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0) < INT_MAX)
                 mb_type |= CANDIDATE_MB_TYPE_INTER_I;
         }
     }else{
         int intra_score, i;
         mb_type= CANDIDATE_MB_TYPE_INTER;
 
-        dmin= s->me.sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
-        if(s->avctx->me_sub_cmp != s->avctx->mb_cmp && !s->me.skip)
-            dmin= get_mb_score(s, mx, my, 0, 0);
+        dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
+        if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
+            dmin= ff_get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
 
         if((s->flags&CODEC_FLAG_4MV)
-           && !s->me.skip && varc>50 && vard>10){
+           && !c->skip && varc>50 && vard>10){
             int dmin4= h263_mv4_search(s, mx, my, shift);
             if(dmin4 < dmin){
                 mb_type= CANDIDATE_MB_TYPE_INTER4V;
@@ -1274,8 +1335,8 @@ void ff_estimate_p_frame_motion(MpegEncContext * s,
             }
         }
         if((s->flags&CODEC_FLAG_INTERLACED_ME)
-           && !s->me.skip){ //FIXME varc/d checks
-            int dmin_i= interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my);
+           && !c->skip){ //FIXME varc/d checks
+            int dmin_i= interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0);
             if(dmin_i < dmin){
                 mb_type = CANDIDATE_MB_TYPE_INTER_I;
                 dmin= dmin_i;
@@ -1286,24 +1347,24 @@ void ff_estimate_p_frame_motion(MpegEncContext * s,
         set_p_mv_tables(s, mx, my, mb_type!=CANDIDATE_MB_TYPE_INTER4V);
 
         /* get intra luma score */
-        if((s->avctx->mb_cmp&0xFF)==FF_CMP_SSE){
+        if((c->avctx->mb_cmp&0xFF)==FF_CMP_SSE){
             intra_score= (varc<<8) - 500; //FIXME dont scale it down so we dont have to fix it
         }else{
             int mean= (sum+128)>>8;
             mean*= 0x01010101;
             
             for(i=0; i<16; i++){
-                *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 0]) = mean;
-                *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 4]) = mean;
-                *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 8]) = mean;
-                *(uint32_t*)(&s->me.scratchpad[i*s->linesize+12]) = mean;
+                *(uint32_t*)(&c->scratchpad[i*s->linesize+ 0]) = mean;
+                *(uint32_t*)(&c->scratchpad[i*s->linesize+ 4]) = mean;
+                *(uint32_t*)(&c->scratchpad[i*s->linesize+ 8]) = mean;
+                *(uint32_t*)(&c->scratchpad[i*s->linesize+12]) = mean;
             }
 
-            intra_score= s->dsp.mb_cmp[0](s, s->me.scratchpad, pix, s->linesize, 16);
+            intra_score= s->dsp.mb_cmp[0](s, c->scratchpad, pix, s->linesize, 16);
         }
 #if 0 //FIXME
         /* get chroma score */
-        if(s->avctx->mb_cmp&FF_CMP_CHROMA){
+        if(c->avctx->mb_cmp&FF_CMP_CHROMA){
             for(i=1; i<3; i++){
                 uint8_t *dest_c;
                 int mean;
@@ -1317,15 +1378,15 @@ void ff_estimate_p_frame_motion(MpegEncContext * s,
                 
                 mean*= 0x01010101;
                 for(i=0; i<8; i++){
-                    *(uint32_t*)(&s->me.scratchpad[i*s->uvlinesize+ 0]) = mean;
-                    *(uint32_t*)(&s->me.scratchpad[i*s->uvlinesize+ 4]) = mean;
+                    *(uint32_t*)(&c->scratchpad[i*s->uvlinesize+ 0]) = mean;
+                    *(uint32_t*)(&c->scratchpad[i*s->uvlinesize+ 4]) = mean;
                 }
                 
-                intra_score+= s->dsp.mb_cmp[1](s, s->me.scratchpad, dest_c, s->uvlinesize);
+                intra_score+= s->dsp.mb_cmp[1](s, c->scratchpad, dest_c, s->uvlinesize);
             }                
         }
 #endif
-        intra_score += s->me.mb_penalty_factor*16;
+        intra_score += c->mb_penalty_factor*16;
         
         if(intra_score < dmin){
             mb_type= CANDIDATE_MB_TYPE_INTRA;
@@ -1334,9 +1395,9 @@ void ff_estimate_p_frame_motion(MpegEncContext * s,
             s->current_picture.mb_type[mb_y*s->mb_stride + mb_x]= 0;
         
         if (vard <= 64 || vard < varc) { //FIXME
-            s->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
+            c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
         }else{
-            s->scene_change_score+= s->qscale;
+            c->scene_change_score+= s->qscale;
         }
     }
 
@@ -1351,21 +1412,20 @@ int ff_pre_estimate_p_frame_motion(MpegEncContext * s,
     int P[10][2];
     const int shift= 1+s->quarter_sample;
     const int xy= mb_x + mb_y*s->mb_stride;
-    init_ref(s, s->new_picture.data, s->last_picture.data, NULL, 16*mb_x, 16*mb_y, 0);
-    init_mc(s, 0, c->flags);
+    init_ref(c, s->new_picture.data, s->last_picture.data, NULL, 16*mb_x, 16*mb_y, 0);
     
     assert(s->quarter_sample==0 || s->quarter_sample==1);
 
-    s->me.pre_penalty_factor    = get_penalty_factor(s, s->avctx->me_pre_cmp);
-    s->me.current_mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV;
+    c->pre_penalty_factor    = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_pre_cmp);
+    c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
 
     get_limits(s, 16*mb_x, 16*mb_y);
-    s->me.skip=0;
+    c->skip=0;
 
     P_LEFT[0]       = s->p_mv_table[xy + 1][0];
     P_LEFT[1]       = s->p_mv_table[xy + 1][1];
 
-    if(P_LEFT[0]       < (s->me.xmin<<shift)) P_LEFT[0]       = (s->me.xmin<<shift);
+    if(P_LEFT[0]       < (c->xmin<<shift)) P_LEFT[0]       = (c->xmin<<shift);
 
     /* special case for first line */
     if (s->first_slice_line) {
@@ -1378,9 +1438,9 @@ int ff_pre_estimate_p_frame_motion(MpegEncContext * s,
         P_TOP[1]      = s->p_mv_table[xy + s->mb_stride    ][1];
         P_TOPRIGHT[0] = s->p_mv_table[xy + s->mb_stride - 1][0];
         P_TOPRIGHT[1] = s->p_mv_table[xy + s->mb_stride - 1][1];
-        if(P_TOP[1]      < (s->me.ymin<<shift)) P_TOP[1]     = (s->me.ymin<<shift);
-        if(P_TOPRIGHT[0] > (s->me.xmax<<shift)) P_TOPRIGHT[0]= (s->me.xmax<<shift);
-        if(P_TOPRIGHT[1] < (s->me.ymin<<shift)) P_TOPRIGHT[1]= (s->me.ymin<<shift);
+        if(P_TOP[1]      < (c->ymin<<shift)) P_TOP[1]     = (c->ymin<<shift);
+        if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift);
+        if(P_TOPRIGHT[1] < (c->ymin<<shift)) P_TOPRIGHT[1]= (c->ymin<<shift);
     
         P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
         P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
@@ -1389,7 +1449,7 @@ int ff_pre_estimate_p_frame_motion(MpegEncContext * s,
         c->pred_y = P_MEDIAN[1];
     }
 
-    dmin = epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift);       
+    dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);       
 
     s->p_mv_table[xy][0] = mx<<shift;
     s->p_mv_table[xy][1] = my<<shift;
@@ -1400,18 +1460,19 @@ int ff_pre_estimate_p_frame_motion(MpegEncContext * s,
 static int ff_estimate_motion_b(MpegEncContext * s,
                        int mb_x, int mb_y, int16_t (*mv_table)[2], int ref_index, int f_code)
 {
+    MotionEstContext * const c= &s->me;
     int mx, my, dmin;
     int P[10][2];
     const int shift= 1+s->quarter_sample;
     const int mot_stride = s->mb_stride;
     const int mot_xy = mb_y*mot_stride + mb_x;
-    uint8_t * const mv_penalty= s->me.mv_penalty[f_code] + MAX_MV;
+    uint8_t * const mv_penalty= c->mv_penalty[f_code] + MAX_MV;
     int mv_scale;
         
-    s->me.penalty_factor    = get_penalty_factor(s, s->avctx->me_cmp);
-    s->me.sub_penalty_factor= get_penalty_factor(s, s->avctx->me_sub_cmp);
-    s->me.mb_penalty_factor = get_penalty_factor(s, s->avctx->mb_cmp);
-    s->me.current_mv_penalty= mv_penalty;
+    c->penalty_factor    = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
+    c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
+    c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
+    c->current_mv_penalty= mv_penalty;
 
     get_limits(s, 16*mb_x, 16*mb_y);
 
@@ -1446,7 +1507,7 @@ static int ff_estimate_motion_b(MpegEncContext * s,
             P_LEFT[0]        = mv_table[mot_xy - 1][0];
             P_LEFT[1]        = mv_table[mot_xy - 1][1];
 
-            if(P_LEFT[0]       > (s->me.xmax<<shift)) P_LEFT[0]       = (s->me.xmax<<shift);
+            if(P_LEFT[0]       > (c->xmax<<shift)) P_LEFT[0]       = (c->xmax<<shift);
 
             /* special case for first line */
             if (!s->first_slice_line) {
@@ -1454,15 +1515,15 @@ static int ff_estimate_motion_b(MpegEncContext * s,
                 P_TOP[1] = mv_table[mot_xy - mot_stride             ][1];
                 P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1         ][0];
                 P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1         ][1];
-                if(P_TOP[1] > (s->me.ymax<<shift)) P_TOP[1]= (s->me.ymax<<shift);
-                if(P_TOPRIGHT[0] < (s->me.xmin<<shift)) P_TOPRIGHT[0]= (s->me.xmin<<shift);
-                if(P_TOPRIGHT[1] > (s->me.ymax<<shift)) P_TOPRIGHT[1]= (s->me.ymax<<shift);
+                if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1]= (c->ymax<<shift);
+                if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
+                if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
         
                 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
                 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
             }
-            s->me.pred_x= P_LEFT[0];
-            s->me.pred_y= P_LEFT[1];
+            c->pred_x= P_LEFT[0];
+            c->pred_y= P_LEFT[1];
         }
         
         if(mv_table == s->b_forw_mv_table){
@@ -1471,15 +1532,15 @@ static int ff_estimate_motion_b(MpegEncContext * s,
             mv_scale= ((s->pb_time - s->pp_time)<<16) / (s->pp_time<<shift);
         }
         
-        dmin = epzs_motion_search(s, &mx, &my, P, 0, ref_index, s->p_mv_table, mv_scale);
+        dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, ref_index, s->p_mv_table, mv_scale, 0, 16);
  
         break;
     }
     
-    dmin= s->me.sub_motion_search(s, &mx, &my, dmin, 0, ref_index, 0, 16);
+    dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, ref_index, 0, 16);
                                    
-    if(s->avctx->me_sub_cmp != s->avctx->mb_cmp && !s->me.skip)
-        dmin= get_mb_score(s, mx, my, 0, ref_index);
+    if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
+        dmin= ff_get_mb_score(s, mx, my, 0, ref_index, 0, 16, 1);
 
 //printf("%d %d %d %d//", s->mb_x, s->mb_y, mx, my);
 //    s->mb_type[mb_y*s->mb_width + mb_x]= mb_type;
@@ -1500,10 +1561,10 @@ static inline int check_bidir_mv(MpegEncContext * s,
     //FIXME better f_code prediction (max mv & distance)
     //FIXME pointers
     MotionEstContext * const c= &s->me;
-    uint8_t * const mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
-    int stride= s->me.stride;
-    int uvstride= s->me.uvstride;
-    uint8_t *dest_y = s->me.scratchpad;
+    uint8_t * const mv_penalty= c->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
+    int stride= c->stride;
+    int uvstride= c->uvstride;
+    uint8_t *dest_y = c->scratchpad;
     uint8_t *ptr;
     int dxy;
     int src_x, src_y;
@@ -1542,11 +1603,11 @@ static inline int check_bidir_mv(MpegEncContext * s,
         s->dsp.avg_pixels_tab[size][dxy](dest_y    , ptr    , stride, h);
     }
 
-    fbmin = (mv_penalty[motion_fx-pred_fx] + mv_penalty[motion_fy-pred_fy])*s->me.mb_penalty_factor
-           +(mv_penalty[motion_bx-pred_bx] + mv_penalty[motion_by-pred_by])*s->me.mb_penalty_factor
+    fbmin = (mv_penalty[motion_fx-pred_fx] + mv_penalty[motion_fy-pred_fy])*c->mb_penalty_factor
+           +(mv_penalty[motion_bx-pred_bx] + mv_penalty[motion_by-pred_by])*c->mb_penalty_factor
            + s->dsp.mb_cmp[size](s, src_data[0], dest_y, stride, h); //FIXME new_pic
            
-    if(s->avctx->mb_cmp&FF_CMP_CHROMA){
+    if(c->avctx->mb_cmp&FF_CMP_CHROMA){
     }
     //FIXME CHROMA !!!
            
@@ -1581,6 +1642,7 @@ static inline int bidir_refine(MpegEncContext * s, int mb_x, int mb_y)
 
 static inline int direct_search(MpegEncContext * s, int mb_x, int mb_y)
 {
+    MotionEstContext * const c= &s->me;
     int P[10][2];
     const int mot_stride = s->mb_stride;
     const int mot_xy = mb_y*mot_stride + mb_x;
@@ -1591,7 +1653,7 @@ static inline int direct_search(MpegEncContext * s, int mb_x, int mb_y)
     int mx, my, xmin, xmax, ymin, ymax;
     int16_t (*mv_table)[2]= s->b_direct_mv_table;
     
-    s->me.current_mv_penalty= s->me.mv_penalty[1] + MAX_MV;
+    c->current_mv_penalty= c->mv_penalty[1] + MAX_MV;
     ymin= xmin=(-32)>>shift;
     ymax= xmax=   31>>shift;
 
@@ -1605,22 +1667,22 @@ static inline int direct_search(MpegEncContext * s, int mb_x, int mb_y)
         int index= s->block_index[i];
         int min, max;
     
-        s->me.co_located_mv[i][0]= s->next_picture.motion_val[0][index][0];
-        s->me.co_located_mv[i][1]= s->next_picture.motion_val[0][index][1];
-        s->me.direct_basis_mv[i][0]= s->me.co_located_mv[i][0]*time_pb/time_pp + ((i& 1)<<(shift+3));
-        s->me.direct_basis_mv[i][1]= s->me.co_located_mv[i][1]*time_pb/time_pp + ((i>>1)<<(shift+3));
-//        s->me.direct_basis_mv[1][i][0]= s->me.co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(shift+3);
-//        s->me.direct_basis_mv[1][i][1]= s->me.co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(shift+3);
-
-        max= FFMAX(s->me.direct_basis_mv[i][0], s->me.direct_basis_mv[i][0] - s->me.co_located_mv[i][0])>>shift;
-        min= FFMIN(s->me.direct_basis_mv[i][0], s->me.direct_basis_mv[i][0] - s->me.co_located_mv[i][0])>>shift;
+        c->co_located_mv[i][0]= s->next_picture.motion_val[0][index][0];
+        c->co_located_mv[i][1]= s->next_picture.motion_val[0][index][1];
+        c->direct_basis_mv[i][0]= c->co_located_mv[i][0]*time_pb/time_pp + ((i& 1)<<(shift+3));
+        c->direct_basis_mv[i][1]= c->co_located_mv[i][1]*time_pb/time_pp + ((i>>1)<<(shift+3));
+//        c->direct_basis_mv[1][i][0]= c->co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(shift+3);
+//        c->direct_basis_mv[1][i][1]= c->co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(shift+3);
+
+        max= FFMAX(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;
+        min= FFMIN(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;
         max+= 16*mb_x + 1; // +-1 is for the simpler rounding
         min+= 16*mb_x - 1;
         xmax= FFMIN(xmax, s->width - max);
         xmin= FFMAX(xmin, - 16     - min);
 
-        max= FFMAX(s->me.direct_basis_mv[i][1], s->me.direct_basis_mv[i][1] - s->me.co_located_mv[i][1])>>shift;
-        min= FFMIN(s->me.direct_basis_mv[i][1], s->me.direct_basis_mv[i][1] - s->me.co_located_mv[i][1])>>shift;
+        max= FFMAX(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;
+        min= FFMIN(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;
         max+= 16*mb_y + 1; // +-1 is for the simpler rounding
         min+= 16*mb_y - 1;
         ymax= FFMIN(ymax, s->height - max);
@@ -1638,14 +1700,14 @@ static inline int direct_search(MpegEncContext * s, int mb_x, int mb_y)
         return 256*256*256*64;
     }
     
-    s->me.xmin= xmin;
-    s->me.ymin= ymin;
-    s->me.xmax= xmax;
-    s->me.ymax= ymax;
-    s->me.flags     |= FLAG_DIRECT;
-    s->me.sub_flags |= FLAG_DIRECT;
-    s->me.pred_x=0;
-    s->me.pred_y=0;
+    c->xmin= xmin;
+    c->ymin= ymin;
+    c->xmax= xmax;
+    c->ymax= ymax;
+    c->flags     |= FLAG_DIRECT;
+    c->sub_flags |= FLAG_DIRECT;
+    c->pred_x=0;
+    c->pred_y=0;
 
     P_LEFT[0]        = clip(mv_table[mot_xy - 1][0], xmin<<shift, xmax<<shift);
     P_LEFT[1]        = clip(mv_table[mot_xy - 1][1], ymin<<shift, ymax<<shift);
@@ -1661,21 +1723,21 @@ static inline int direct_search(MpegEncContext * s, int mb_x, int mb_y)
         P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
     }
  
-    dmin = epzs_motion_search(s, &mx, &my, P, 0, 0, mv_table, 1<<(16-shift));
-    if(s->me.sub_flags&FLAG_QPEL) 
+    dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, mv_table, 1<<(16-shift), 0, 16);
+    if(c->sub_flags&FLAG_QPEL) 
         dmin = qpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
     else
         dmin = hpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
     
-    if(s->avctx->me_sub_cmp != s->avctx->mb_cmp && !s->me.skip)
-        dmin= get_mb_score(s, mx, my, 0, 0);
+    if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
+        dmin= ff_get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
     
-    get_limits(s, 16*mb_x, 16*mb_y); //restore s->me.?min/max, maybe not needed
+    get_limits(s, 16*mb_x, 16*mb_y); //restore c->?min/max, maybe not needed
 
     s->b_direct_mv_table[mot_xy][0]= mx;
     s->b_direct_mv_table[mot_xy][1]= my;
-    s->me.flags     &= ~FLAG_DIRECT;
-    s->me.sub_flags &= ~FLAG_DIRECT;
+    c->flags     &= ~FLAG_DIRECT;
+    c->sub_flags &= ~FLAG_DIRECT;
 
     return dmin;
 }
@@ -1683,17 +1745,20 @@ static inline int direct_search(MpegEncContext * s, int mb_x, int mb_y)
 void ff_estimate_b_frame_motion(MpegEncContext * s,
                              int mb_x, int mb_y)
 {
-    const int penalty_factor= s->me.mb_penalty_factor;
+    MotionEstContext * const c= &s->me;
+    const int penalty_factor= c->mb_penalty_factor;
     int fmin, bmin, dmin, fbmin, bimin, fimin;
     int type=0;
-    init_ref(s, s->new_picture.data, s->last_picture.data, s->next_picture.data, 16*mb_x, 16*mb_y, 2);
-    init_mc(s, 0, s->me.flags);
+    const int xy = mb_y*s->mb_stride + mb_x;
+    init_ref(c, s->new_picture.data, s->last_picture.data, s->next_picture.data, 16*mb_x, 16*mb_y, 2);
+
+    get_limits(s, 16*mb_x, 16*mb_y);
     
-    s->me.skip=0;
-    if(s->avctx->me_threshold){
+    c->skip=0;
+    if(c->avctx->me_threshold){
         int vard= (check_input_motion(s, mb_x, mb_y, 0)+128)>>8;
         
-        if(vard<s->avctx->me_threshold){
+        if(vard<c->avctx->me_threshold){
 //            pix = c->src[0][0];
 //            sum = s->dsp.pix_sum(pix, s->linesize);
 //            varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
@@ -1701,15 +1766,44 @@ void ff_estimate_b_frame_motion(MpegEncContext * s,
 //            pic->mb_var   [s->mb_stride * mb_y + mb_x] = varc;
              s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] = vard;
 /*            pic->mb_mean  [s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
-            s->mb_var_sum_temp    += varc;*/
-            s->mc_mb_var_sum_temp += vard;
+            c->mb_var_sum_temp    += varc;*/
+            c->mc_mb_var_sum_temp += vard;
 /*            if (vard <= 64 || vard < varc) {
-                s->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
+                c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
             }else{
-                s->scene_change_score+= s->qscale;
+                c->scene_change_score+= s->qscale;
             }*/
             return;
         }
+        if(vard<c->avctx->mb_threshold){
+            type= s->mb_type[mb_y*s->mb_stride + mb_x];
+            if(type == CANDIDATE_MB_TYPE_DIRECT){
+                direct_search(s, mb_x, mb_y);
+            }
+            if(type == CANDIDATE_MB_TYPE_FORWARD || type == CANDIDATE_MB_TYPE_BIDIR){
+                c->skip=0;
+                ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code);
+            }
+            if(type == CANDIDATE_MB_TYPE_BACKWARD || type == CANDIDATE_MB_TYPE_BIDIR){
+                c->skip=0;
+                ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code);
+            }
+            if(type == CANDIDATE_MB_TYPE_FORWARD_I || type == CANDIDATE_MB_TYPE_BIDIR_I){
+                c->skip=0;
+                c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
+                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], 1);
+            }
+            if(type == CANDIDATE_MB_TYPE_BACKWARD_I || type == CANDIDATE_MB_TYPE_BIDIR_I){
+                c->skip=0;
+                c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV;
+                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], 1);
+            }
+            return;
+        }
     }
 
     if (s->codec_id == CODEC_ID_MPEG4)
@@ -1717,30 +1811,28 @@ void ff_estimate_b_frame_motion(MpegEncContext * s,
     else
         dmin= INT_MAX;
 //FIXME penalty stuff for non mpeg4
-    s->me.skip=0;
+    c->skip=0;
     fmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code) + 3*penalty_factor;
     
-    s->me.skip=0;
+    c->skip=0;
     bmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code) + 2*penalty_factor;
 //printf(" %d %d ", s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]);
 
-    s->me.skip=0;
+    c->skip=0;
     fbmin= bidir_refine(s, mb_x, mb_y) + penalty_factor;
 //printf("%d %d %d %d\n", dmin, fmin, bmin, fbmin);
     
     if(s->flags & CODEC_FLAG_INTERLACED_ME){
-        const int xy = mb_y*s->mb_stride + mb_x;
-
 //FIXME mb type penalty
-        s->me.skip=0;
-        s->me.current_mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV;
+        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]);
-        s->me.current_mv_penalty= s->me.mv_penalty[s->b_code] + MAX_MV;
+                                 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]);
+                                 s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 0);
     }else
         fimin= bimin= INT_MAX;
 
@@ -1770,11 +1862,11 @@ void ff_estimate_b_frame_motion(MpegEncContext * s,
         }
         
         score= ((unsigned)(score*score + 128*256))>>16;
-        s->mc_mb_var_sum_temp += score;
+        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(s->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
+    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;
@@ -1799,7 +1891,7 @@ int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type)
 {
     if(s->me_method>=ME_EPZS){
         int score[8];
-        int i, y;
+        int i, y, range= s->avctx->me_range;
         uint8_t * fcode_tab= s->fcode_tab;
         int best_fcode=-1;
         int best_score=-10000000;
@@ -1811,10 +1903,18 @@ int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type)
             int xy= y*s->mb_stride;
             for(x=0; x<s->mb_width; x++){
                 if(s->mb_type[xy] & type){
-                    int fcode= FFMAX(fcode_tab[mv_table[xy][0] + MAX_MV],
-                                     fcode_tab[mv_table[xy][1] + MAX_MV]);
+                    int mx= mv_table[xy][0];
+                    int my= mv_table[xy][1];
+                    int fcode= FFMAX(fcode_tab[mx + MAX_MV],
+                                     fcode_tab[my + MAX_MV]);
                     int j;
                     
+                    if(range){
+                        if(mx >= range || mx < -range || 
+                           my >= range || my < -range)
+                            continue;
+                    }
+                    
                     for(j=0; j<fcode && j<8; j++){
                         if(s->pict_type==B_TYPE || s->current_picture.mc_mb_var[xy] < s->current_picture.mb_var[xy])
                             score[j]-= 170;
@@ -1845,6 +1945,7 @@ int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type)
 
 void ff_fix_long_p_mvs(MpegEncContext * s)
 {
+    MotionEstContext * const c= &s->me;
     const int f_code= s->f_code;
     int y, range;
     assert(s->pict_type==P_TYPE);
@@ -1853,7 +1954,7 @@ void ff_fix_long_p_mvs(MpegEncContext * s)
     
     if(s->msmpeg4_version) range= 16;
     
-    if(s->avctx->me_range && range > s->avctx->me_range) range= s->avctx->me_range;
+    if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
     
 //printf("%d no:%d %d//\n", clip, noclip, f_code);
     if(s->flags&CODEC_FLAG_4MV){
@@ -1895,13 +1996,14 @@ void ff_fix_long_p_mvs(MpegEncContext * s)
 void ff_fix_long_mvs(MpegEncContext * s, uint8_t *field_select_table, int field_select, 
                      int16_t (*mv_table)[2], int f_code, int type, int truncate)
 {
+    MotionEstContext * const c= &s->me;
     int y, h_range, v_range;
 
     // RAL: 8 in MPEG-1, 16 in MPEG-4
     int range = (((s->out_format == FMT_MPEG1) ? 8 : 16) << f_code);
 
     if(s->msmpeg4_version) range= 16;
-    if(s->avctx->me_range && range > s->avctx->me_range) range= s->avctx->me_range;
+    if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
 
     h_range= range;
     v_range= field_select_table ? range>>1 : range;