Index: me.c
===================================================================
--- me.c	(revision 551)
+++ me.c	(working copy)
@@ -32,7 +32,7 @@
  * subpel_iters[i_subpel_refine] = { refine_hpel, refine_qpel, me_hpel, me_qpel }
  * where me_* are the number of EPZS iterations run on all candidate block types,
  * and refine_* are run only on the winner. */
-static const int subpel_iterations[][4] = 
+static const int subpel_iterations[][4] =
    {{1,0,0,0},
     {1,1,0,0},
     {0,1,1,0},
@@ -155,12 +155,15 @@
     const int bh = x264_pixel_size[m->i_pixel].h;
     const int i_pixel = m->i_pixel;
     int i_me_range = h->param.analyse.i_me_range;
-    int bmx, bmy, bcost;
-    int bpred_mx = 0, bpred_my = 0, bpred_cost = COST_MAX;
-    int omx, omy, pmx, pmy;
+    /* these are in full pel resolution */
+    int bmx, bmy, omx, omy, pmx, pmy;
+    /* these are in quarter pel resolution */
+    int bpred_mx, bpred_my;
+    int bpred_cost = COST_MAX;
+    int bcost = COST_MAX;
     uint8_t *p_fref = m->p_fref[0];
     DECLARE_ALIGNED( uint8_t, pix[16*16], 16 );
-    
+
     int i, j;
     int dir;
     int costs[6];
@@ -173,52 +176,46 @@
     const int16_t *p_cost_mvx = m->p_cost_mv - m->mvp[0];
     const int16_t *p_cost_mvy = m->p_cost_mv - m->mvp[1];
 
-    if( h->mb.i_me_method == X264_ME_UMH )
-    {
-        /* clamp mvp to inside frame+padding, so that we don't have to check it each iteration */
-        p_cost_mvx = m->p_cost_mv - x264_clip3( m->mvp[0], h->mb.mv_min_spel[0], h->mb.mv_max_spel[0] );
-        p_cost_mvy = m->p_cost_mv - x264_clip3( m->mvp[1], h->mb.mv_min_spel[1], h->mb.mv_max_spel[1] );
-    }
+    pmx = x264_clip3( (m->mvp[0] + 2) >> 2, mv_x_min, mv_x_max );
+    pmy = x264_clip3( (m->mvp[1] + 2) >> 2, mv_y_min, mv_y_max );
 
-    bmx = x264_clip3( m->mvp[0], mv_x_min*4, mv_x_max*4 );
-    bmy = x264_clip3( m->mvp[1], mv_y_min*4, mv_y_max*4 );
-    pmx = ( bmx + 2 ) >> 2;
-    pmy = ( bmy + 2 ) >> 2;
-    bcost = COST_MAX;
-
     /* try extra predictors if provided */
     if( h->mb.i_subpel_refine >= 3 )
     {
-        COST_MV_PRED( bmx, bmy );
+        /* check the MVP */
+        COST_MV_PRED( x264_clip3( m->mvp[0], h->mb.mv_min_spel[0], h->mb.mv_max_spel[0] ),
+                      x264_clip3( m->mvp[1], h->mb.mv_min_spel[1], h->mb.mv_max_spel[1] ) );
         for( i = 0; i < i_mvc; i++ )
         {
-             const int mx = x264_clip3( mvc[i][0], mv_x_min*4, mv_x_max*4 );
-             const int my = x264_clip3( mvc[i][1], mv_y_min*4, mv_y_max*4 );
-             if( mx != bpred_mx || my != bpred_my )
-                 COST_MV_PRED( mx, my );
+            const int mx = x264_clip3( mvc[i][0], h->mb.mv_min_spel[0], h->mb.mv_max_spel[0] );
+            const int my = x264_clip3( mvc[i][1], h->mb.mv_min_spel[1], h->mb.mv_max_spel[1] );
+            if( mx != bpred_mx || my != bpred_my )
+                COST_MV_PRED( mx, my );
         }
         bmx = ( bpred_mx + 2 ) >> 2;
         bmy = ( bpred_my + 2 ) >> 2;
-        COST_MV( bmx, bmy );
+        if ((bpred_mx & 3) || (bpred_my & 3))
+            COST_MV( bmx, bmy );
+        else
+            bcost = bpred_cost;
     }
     else
     {
         /* check the MVP */
         COST_MV( pmx, pmy );
-        /* I don't know why this helps */
-        bcost -= BITS_MVD(bmx,bmy);
-        
         for( i = 0; i < i_mvc; i++ )
         {
-             const int mx = x264_clip3( ( mvc[i][0] + 2 ) >> 2, mv_x_min, mv_x_max );
-             const int my = x264_clip3( ( mvc[i][1] + 2 ) >> 2, mv_y_min, mv_y_max );
-             if( mx != bmx || my != bmy )
-                 COST_MV( mx, my );
+            const int mx = x264_clip3( ( mvc[i][0] + 2 ) >> 2, mv_x_min, mv_x_max );
+            const int my = x264_clip3( ( mvc[i][1] + 2 ) >> 2, mv_y_min, mv_y_max );
+            if( mx != bmx || my != bmy )
+                COST_MV( mx, my );
         }
     }
-    
+
+    /* check 0 vector */
     COST_MV( 0, 0 );
 
+    /* why is this necessary? */
     mv_x_max += 8;
     mv_y_max += 8;
     mv_x_min -= 8;
@@ -510,11 +507,9 @@
         m->cost = bcost;
     }
 
-    /* compute the real cost */
+    /* return mv cost */
     m->cost_mv = p_cost_mvx[ m->mv[0] ] + p_cost_mvy[ m->mv[1] ];
-    if( bmx == pmx && bmy == pmy && h->mb.i_subpel_refine < 3 )
-        m->cost += m->cost_mv;
-    
+
     /* subpel refine */
     if( h->mb.i_subpel_refine >= 2 )
     {
@@ -532,7 +527,7 @@
 
     if( m->i_pixel <= PIXEL_8x8 && h->sh.i_type == SLICE_TYPE_P )
         m->cost -= m->i_ref_cost;
-	
+
     refine_subpel( h, m, hpel, qpel, NULL, 1 );
 }
 
