changeset 2014:15c885db82a8 libavcodec

reduce dependancy between motion estimation and MpegEncContext this is practically just a s/s->me./c->/
author michael
date Tue, 11 May 2004 00:58:08 +0000
parents 85e547a18d87
children 3ab8f3e2ae6a
files motion_est.c motion_est_template.c
diffstat 2 files changed, 225 insertions(+), 209 deletions(-) [+]
line wrap: on
line diff
--- a/motion_est.c	Mon May 10 23:38:53 2004 +0000
+++ b/motion_est.c	Tue May 11 00:58:08 2004 +0000
@@ -49,14 +49,14 @@
                                   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 @@
 #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),
@@ -247,35 +246,35 @@
     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);
     
-    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(s, 0, s->avctx->me_cmp    &FF_CMP_CHROMA);
+    c->sub_flags= get_flags(s, 0, s->avctx->me_sub_cmp&FF_CMP_CHROMA);
+    c->mb_flags = get_flags(s, 0, s->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;
+            c->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
+            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;
     }
 
     c->temp= c->scratchpad;
@@ -548,16 +547,17 @@
                                   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;
@@ -565,11 +565,11 @@
     }
 //    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;
 
@@ -679,20 +679,21 @@
  */
 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(s->avctx->me_range) c->range= s->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 {
-        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;
     }
 }
 
@@ -719,7 +720,7 @@
     int same=1;
     const int stride= s->linesize;
     const int uvstride= s->uvlinesize;
-    uint8_t *mv_penalty= s->me.current_mv_penalty;
+    uint8_t *mv_penalty= c->current_mv_penalty;
 
     init_mv4_ref(s);
     
@@ -734,39 +735,39 @@
         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);
@@ -784,7 +785,7 @@
                 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;
 
@@ -806,7 +807,7 @@
         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){
@@ -821,19 +822,19 @@
         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){
     /*case FF_CMP_SSE:
@@ -841,7 +842,7 @@
     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;
     }
 }
 
@@ -898,8 +899,8 @@
             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];
@@ -937,7 +938,7 @@
                     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
                 
@@ -1117,19 +1118,19 @@
     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_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, s->avctx->me_cmp);
+    c->sub_penalty_factor= get_penalty_factor(s, s->avctx->me_sub_cmp);
+    c->mb_penalty_factor = get_penalty_factor(s, s->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];
@@ -1191,16 +1192,16 @@
             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]);
@@ -1243,7 +1244,7 @@
             s->scene_change_score+= s->qscale;
 
         if(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);
             set_p_mv_tables(s, mx, my, 1);
         }else{
             mx <<=shift;
@@ -1267,7 +1268,7 @@
             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
@@ -1276,7 +1277,7 @@
             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;
 
@@ -1284,7 +1285,7 @@
         }else
             set_p_mv_tables(s, mx, my, 1);
         if((s->flags&CODEC_FLAG_INTERLACED_ME)
-           && !s->me.skip){ //FIXME varc/d checks
+           && !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;
         }
@@ -1292,12 +1293,12 @@
         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= c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
+        if(s->avctx->me_sub_cmp != s->avctx->mb_cmp && !c->skip)
             dmin= get_mb_score(s, mx, my, 0, 0);
 
         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;
@@ -1305,7 +1306,7 @@
             }
         }
         if((s->flags&CODEC_FLAG_INTERLACED_ME)
-           && !s->me.skip){ //FIXME varc/d checks
+           && !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;
@@ -1324,13 +1325,13 @@
             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 */
@@ -1348,15 +1349,15 @@
                 
                 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;
@@ -1382,20 +1383,20 @@
     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_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, s->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) {
@@ -1408,9 +1409,9 @@
         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]);
@@ -1430,18 +1431,19 @@
 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, s->avctx->me_cmp);
+    c->sub_penalty_factor= get_penalty_factor(s, s->avctx->me_sub_cmp);
+    c->mb_penalty_factor = get_penalty_factor(s, s->avctx->mb_cmp);
+    c->current_mv_penalty= mv_penalty;
 
     get_limits(s, 16*mb_x, 16*mb_y);
 
@@ -1476,7 +1478,7 @@
             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) {
@@ -1484,15 +1486,15 @@
                 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){
@@ -1506,9 +1508,9 @@
         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)
+    if(s->avctx->me_sub_cmp != s->avctx->mb_cmp && !c->skip)
         dmin= get_mb_score(s, mx, my, 0, ref_index);
 
 //printf("%d %d %d %d//", s->mb_x, s->mb_y, mx, my);
@@ -1530,10 +1532,10 @@
     //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;
@@ -1572,8 +1574,8 @@
         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){
@@ -1611,6 +1613,7 @@
 
 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;
@@ -1621,7 +1624,7 @@
     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;
 
@@ -1635,22 +1638,22 @@
         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);
+        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(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;
+        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);
@@ -1668,14 +1671,14 @@
         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);
@@ -1692,20 +1695,20 @@
     }
  
     dmin = epzs_motion_search(s, &mx, &my, P, 0, 0, mv_table, 1<<(16-shift));
-    if(s->me.sub_flags&FLAG_QPEL) 
+    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)
+    if(s->avctx->me_sub_cmp != s->avctx->mb_cmp && !c->skip)
         dmin= get_mb_score(s, mx, my, 0, 0);
     
-    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;
 }
@@ -1713,14 +1716,15 @@
 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;
     const int xy = mb_y*s->mb_stride + mb_x;
-    init_ref(s, s->new_picture.data, s->last_picture.data, s->next_picture.data, 16*mb_x, 16*mb_y, 2);
+    init_ref(c, s->new_picture.data, s->last_picture.data, s->next_picture.data, 16*mb_x, 16*mb_y, 2);
 
     
-    s->me.skip=0;
+    c->skip=0;
     if(s->avctx->me_threshold){
         int vard= (check_input_motion(s, mb_x, mb_y, 0)+128)>>8;
         
@@ -1747,23 +1751,23 @@
                 direct_search(s, mb_x, mb_y);
             }
             if(type == CANDIDATE_MB_TYPE_FORWARD || type == CANDIDATE_MB_TYPE_BIDIR){
-                s->me.skip=0;
+                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){
-                s->me.skip=0;
+                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){
-                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;
                 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){
-                s->me.skip=0;
-                s->me.current_mv_penalty= s->me.mv_penalty[s->b_code] + MAX_MV;
+                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);
@@ -1777,25 +1781,25 @@
     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){
 //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], 0);
-        s->me.current_mv_penalty= s->me.mv_penalty[s->b_code] + MAX_MV;
+        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);
--- a/motion_est_template.c	Mon May 10 23:38:53 2004 +0000
+++ b/motion_est_template.c	Tue May 11 00:58:08 2004 +0000
@@ -25,14 +25,14 @@
 
 //lets hope gcc will remove the unused vars ...(gcc 3.2.2 seems to do it ...)
 #define LOAD_COMMON\
-    uint32_t * const score_map= s->me.score_map;\
-    const int xmin= s->me.xmin;\
-    const int ymin= s->me.ymin;\
-    const int xmax= s->me.xmax;\
-    const int ymax= s->me.ymax;\
-    uint8_t *mv_penalty= s->me.current_mv_penalty;\
-    const int pred_x= s->me.pred_x;\
-    const int pred_y= s->me.pred_y;\
+    uint32_t * const score_map= c->score_map;\
+    const int xmin= c->xmin;\
+    const int ymin= c->ymin;\
+    const int xmax= c->xmax;\
+    const int ymax= c->ymax;\
+    uint8_t *mv_penalty= c->current_mv_penalty;\
+    const int pred_x= c->pred_x;\
+    const int pred_y= c->pred_y;\
 
 #define CHECK_HALF_MV(dx, dy, x, y)\
 {\
@@ -53,7 +53,7 @@
     const int yy = 16 * s->mb_y + 8*(n>>1);
     const int mx = *mx_ptr;
     const int my = *my_ptr;
-    const int penalty_factor= s->me.sub_penalty_factor;
+    const int penalty_factor= c->sub_penalty_factor;
     
     LOAD_COMMON
     
@@ -73,7 +73,7 @@
     cmp_sub= s->dsp.me_sub_cmp[size];
     chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
 
-    if(s->me.skip){ //FIXME somehow move up (benchmark)
+    if(c->skip){ //FIXME somehow move up (benchmark)
         *mx_ptr = 0;
         *my_ptr = 0;
         return dmin;
@@ -117,21 +117,22 @@
                                   int src_index, int ref_index,
                                   int size, int h)
 {
+    MotionEstContext * const c= &s->me;
     const int mx = *mx_ptr;
     const int my = *my_ptr;   
-    const int penalty_factor= s->me.sub_penalty_factor;
+    const int penalty_factor= c->sub_penalty_factor;
     me_cmp_func cmp_sub, chroma_cmp_sub;
     int bx=2*mx, by=2*my;
 
     LOAD_COMMON
-    int flags= s->me.sub_flags;
+    int flags= c->sub_flags;
     
  //FIXME factorize
 
     cmp_sub= s->dsp.me_sub_cmp[size];
     chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
 
-    if(s->me.skip){ //FIXME move out of hpel?
+    if(c->skip){ //FIXME move out of hpel?
         *mx_ptr = 0;
         *my_ptr = 0;
         return dmin;
@@ -148,19 +149,19 @@
         int d= dmin;
         const int index= (my<<ME_MAP_SHIFT) + mx;
         const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)] 
-                     + (mv_penalty[bx   - pred_x] + mv_penalty[by-2 - pred_y])*s->me.penalty_factor;
+                     + (mv_penalty[bx   - pred_x] + mv_penalty[by-2 - pred_y])*c->penalty_factor;
         const int l= score_map[(index- 1               )&(ME_MAP_SIZE-1)]
-                     + (mv_penalty[bx-2 - pred_x] + mv_penalty[by   - pred_y])*s->me.penalty_factor;
+                     + (mv_penalty[bx-2 - pred_x] + mv_penalty[by   - pred_y])*c->penalty_factor;
         const int r= score_map[(index+ 1               )&(ME_MAP_SIZE-1)]
-                     + (mv_penalty[bx+2 - pred_x] + mv_penalty[by   - pred_y])*s->me.penalty_factor;
+                     + (mv_penalty[bx+2 - pred_x] + mv_penalty[by   - pred_y])*c->penalty_factor;
         const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)]
-                     + (mv_penalty[bx   - pred_x] + mv_penalty[by+2 - pred_y])*s->me.penalty_factor;
+                     + (mv_penalty[bx   - pred_x] + mv_penalty[by+2 - pred_y])*c->penalty_factor;
     
 #if 1
         int key;
-        int map_generation= s->me.map_generation;
+        int map_generation= c->map_generation;
 #ifndef NDEBUG
-        uint32_t *map= s->me.map;
+        uint32_t *map= c->map;
 #endif
         key= ((my-1)<<ME_MAP_MV_BITS) + (mx) + map_generation;
         assert(map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)] == key);
@@ -224,10 +225,11 @@
                                int ref_index)
 {
 //    const int check_luma= s->dsp.me_sub_cmp != s->dsp.mb_cmp;
+    MotionEstContext * const c= &s->me;
     const int size= 0;
     const int h= 16;
-    const int penalty_factor= s->me.mb_penalty_factor;
-    const int flags= s->me.mb_flags;
+    const int penalty_factor= c->mb_penalty_factor;
+    const int flags= c->mb_flags;
     const int qpel= flags & FLAG_QPEL;
     const int mask= 1+2*qpel;
     me_cmp_func cmp_sub, chroma_cmp_sub;
@@ -240,7 +242,7 @@
     cmp_sub= s->dsp.mb_cmp[size];
     chroma_cmp_sub= s->dsp.mb_cmp[size+1];
     
-    assert(!s->me.skip);
+    assert(!c->skip);
     assert(s->avctx->me_sub_cmp != s->avctx->mb_cmp);
 
     d= cmp(s, mx>>(qpel+1), my>>(qpel+1), mx&mask, my&mask, size, h, ref_index, src_index, cmp_sub, chroma_cmp_sub, flags);
@@ -265,17 +267,18 @@
                                   int src_index, int ref_index,                                  
                                   int size, int h)
 {
+    MotionEstContext * const c= &s->me;
     const int mx = *mx_ptr;
     const int my = *my_ptr;   
-    const int penalty_factor= s->me.sub_penalty_factor;
-    const int map_generation= s->me.map_generation;
+    const int penalty_factor= c->sub_penalty_factor;
+    const int map_generation= c->map_generation;
     const int subpel_quality= s->avctx->me_subpel_quality;
-    uint32_t *map= s->me.map;
+    uint32_t *map= c->map;
     me_cmp_func cmpf, chroma_cmpf;
     me_cmp_func cmp_sub, chroma_cmp_sub;
 
     LOAD_COMMON
-    int flags= s->me.sub_flags;
+    int flags= c->sub_flags;
     
     cmpf= s->dsp.me_cmp[size];
     chroma_cmpf= s->dsp.me_cmp[size+1]; //factorize FIXME
@@ -284,7 +287,7 @@
     cmp_sub= s->dsp.me_sub_cmp[size];
     chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
 
-    if(s->me.skip){ //FIXME somehow move up (benchmark)
+    if(c->skip){ //FIXME somehow move up (benchmark)
         *mx_ptr = 0;
         *my_ptr = 0;
         return dmin;
@@ -330,8 +333,8 @@
                     
                     score += 1024*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
                     
-//                    if(nx&1) score-=1024*s->me.penalty_factor;
-//                    if(ny&1) score-=1024*s->me.penalty_factor;
+//                    if(nx&1) score-=1024*c->penalty_factor;
+//                    if(ny&1) score-=1024*c->penalty_factor;
                     
                     for(i=0; i<8; i++){
                         if(score < best[i]){
@@ -375,8 +378,8 @@
                     if((nx&3)==0 && (ny&3)==0) continue;
                 
                     score += 32*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
-//                    if(nx&1) score-=32*s->me.penalty_factor;
-  //                  if(ny&1) score-=32*s->me.penalty_factor;
+//                    if(nx&1) score-=32*c->penalty_factor;
+  //                  if(ny&1) score-=32*c->penalty_factor;
                     
                     for(i=0; i<8; i++){
                         if(score < best[i]){
@@ -531,7 +534,7 @@
 if( (y)>(ymax<<(S)) ) printf("%d %d %d %d %d ymax" #v, ymax, (x), (y), s->mb_x, s->mb_y);\
 
 #define LOAD_COMMON2\
-    uint32_t *map= s->me.map;\
+    uint32_t *map= c->map;\
     const int qpel= flags&FLAG_QPEL;\
     const int shift= 1+qpel;\
 
@@ -539,11 +542,12 @@
                                        int src_index, int ref_index, int const penalty_factor,
                                        int size, int h, int flags)
 {
+    MotionEstContext * const c= &s->me;
     me_cmp_func cmpf, chroma_cmpf;
     int next_dir=-1;
     LOAD_COMMON
     LOAD_COMMON2
-    int map_generation= s->me.map_generation;
+    int map_generation= c->map_generation;
     
     cmpf= s->dsp.me_cmp[size];
     chroma_cmpf= s->dsp.me_cmp[size+1];
@@ -580,11 +584,12 @@
                                        int src_index, int ref_index, int const penalty_factor,
                                        int size, int h, int flags)
 {
+    MotionEstContext * const c= &s->me;
     me_cmp_func cmpf, chroma_cmpf;
     int dia_size;
     LOAD_COMMON
     LOAD_COMMON2
-    int map_generation= s->me.map_generation;
+    int map_generation= c->map_generation;
     
     cmpf= s->dsp.me_cmp[size];
     chroma_cmpf= s->dsp.me_cmp[size+1];
@@ -670,13 +675,14 @@
                                        int src_index, int ref_index, int const penalty_factor,
                                        int size, int h, int flags)
 {
+    MotionEstContext * const c= &s->me;
     me_cmp_func cmpf, chroma_cmpf;
     Minima minima[MAX_SAB_SIZE];
-    const int minima_count= ABS(s->me.dia_size);
+    const int minima_count= ABS(c->dia_size);
     int i, j;
     LOAD_COMMON
     LOAD_COMMON2
-    int map_generation= s->me.map_generation;
+    int map_generation= c->map_generation;
     
     cmpf= s->dsp.me_cmp[size];
     chroma_cmpf= s->dsp.me_cmp[size+1];
@@ -749,16 +755,17 @@
                                        int src_index, int ref_index, int const penalty_factor,
                                        int size, int h, int flags)
 {
+    MotionEstContext * const c= &s->me;
     me_cmp_func cmpf, chroma_cmpf;
     int dia_size;
     LOAD_COMMON
     LOAD_COMMON2
-    int map_generation= s->me.map_generation;
+    int map_generation= c->map_generation;
     
     cmpf= s->dsp.me_cmp[size];
     chroma_cmpf= s->dsp.me_cmp[size+1];
 
-    for(dia_size=1; dia_size<=s->me.dia_size; dia_size++){
+    for(dia_size=1; dia_size<=c->dia_size; dia_size++){
         int dir, start, end;
         const int x= best[0];
         const int y= best[1];
@@ -824,11 +831,12 @@
 static always_inline int diamond_search(MpegEncContext * s, int *best, int dmin,
                                        int src_index, int ref_index, int const penalty_factor,
                                        int size, int h, int flags){
-    if(s->me.dia_size==-1)
+    MotionEstContext * const c= &s->me;
+    if(c->dia_size==-1)
         return funny_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
-    else if(s->me.dia_size<-1)
+    else if(c->dia_size<-1)
         return   sab_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
-    else if(s->me.dia_size<2)
+    else if(c->dia_size<2)
         return small_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
     else
         return   var_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
@@ -838,10 +846,11 @@
                              int P[10][2], int src_index, int ref_index, int16_t (*last_mv)[2], 
                              int ref_mv_scale, int flags)
 {
+    MotionEstContext * const c= &s->me;
     int best[2]={0, 0};
     int d, dmin;
     int map_generation;
-    const int penalty_factor= s->me.penalty_factor;
+    const int penalty_factor= c->penalty_factor;
     const int size=0;
     const int h=16;
     const int ref_mv_stride= s->mb_stride; //pass as arg  FIXME
@@ -854,7 +863,7 @@
     cmpf= s->dsp.me_cmp[size];
     chroma_cmpf= s->dsp.me_cmp[size+1];
     
-    map_generation= update_map_generation(s);
+    map_generation= update_map_generation(c);
 
     dmin= cmp(s, 0, 0, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);
     map[0]= map_generation;
@@ -871,7 +880,7 @@
                         |P_TOPRIGHT[0]|P_TOPRIGHT[1])==0){
             *mx_ptr= 0;
             *my_ptr= 0;
-            s->me.skip=1;
+            c->skip=1;
             return dmin;
         }
         CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
@@ -884,7 +893,7 @@
         }
     }
     if(dmin>256*4){
-        if(s->me.pre_pass){
+        if(c->pre_pass){
             CHECK_CLIPED_MV((last_mv[ref_mv_xy-1][0]*ref_mv_scale + (1<<15))>>16, 
                             (last_mv[ref_mv_xy-1][1]*ref_mv_scale + (1<<15))>>16)
             if(!s->first_slice_line)
@@ -936,14 +945,15 @@
                              int P[10][2], int src_index, int ref_index, int16_t (*last_mv)[2], 
                              int ref_mv_scale)
 {
+    MotionEstContext * const c= &s->me;
 //FIXME convert other functions in the same way if faster
-    switch(s->me.flags){
+    switch(c->flags){
     case 0:
         return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, 0);
 //    case FLAG_QPEL:
 //        return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, FLAG_QPEL);
     default:
-        return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, s->me.flags);
+        return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, c->flags);
     }
 }
 
@@ -952,23 +962,24 @@
                              int src_index, int ref_index, int16_t (*last_mv)[2], 
                              int ref_mv_scale)
 {
+    MotionEstContext * const c= &s->me;
     int best[2]={0, 0};
     int d, dmin; 
     int map_generation;
-    const int penalty_factor= s->me.penalty_factor;
+    const int penalty_factor= c->penalty_factor;
     const int size=1;
     const int h=8;
     const int ref_mv_stride= s->mb_stride;
     const int ref_mv_xy= s->mb_x + s->mb_y *ref_mv_stride;
     me_cmp_func cmpf, chroma_cmpf;
     LOAD_COMMON
-    int flags= s->me.flags;
+    int flags= c->flags;
     LOAD_COMMON2
     
     cmpf= s->dsp.me_cmp[size];
     chroma_cmpf= s->dsp.me_cmp[size+1];
 
-    map_generation= update_map_generation(s);
+    map_generation= update_map_generation(c);
 
     dmin = 1000000;
 //printf("%d %d %d %d //",xmin, ymin, xmax, ymax); 
@@ -1013,23 +1024,24 @@
                              int src_index, int ref_index, int16_t (*last_mv)[2], 
                              int ref_mv_scale)
 {
+    MotionEstContext * const c= &s->me;
     int best[2]={0, 0};
     int d, dmin; 
     int map_generation;
-    const int penalty_factor= s->me.penalty_factor;
+    const int penalty_factor= c->penalty_factor;
     const int size=0; //FIXME pass as arg
     const int h=8;
     const int ref_mv_stride= s->mb_stride;
     const int ref_mv_xy= s->mb_x + s->mb_y *ref_mv_stride;
     me_cmp_func cmpf, chroma_cmpf;
     LOAD_COMMON
-    int flags= s->me.flags;
+    int flags= c->flags;
     LOAD_COMMON2
     
     cmpf= s->dsp.me_cmp[size];
     chroma_cmpf= s->dsp.me_cmp[size+1];
 
-    map_generation= update_map_generation(s);
+    map_generation= update_map_generation(c);
 
     dmin = 1000000;
 //printf("%d %d %d %d //",xmin, ymin, xmax, ymax);