]> git.sesse.net Git - x264/blobdiff - encoder/analyse.c
Change some macros to be more sensitive to memory alignment, thus avoiding
[x264] / encoder / analyse.c
index 2a294ff2d0d5ccee87a1613011897489c3827240..8ae426f1969219bd0a38213f08a52cae6d17ae0c 100644 (file)
@@ -76,7 +76,7 @@ typedef struct
     int i_lambda2;
     int i_qp;
     int16_t *p_cost_mv;
-    int b_mbrd;
+    int i_mbrd;
 
 
     /* I: Intra part */
@@ -203,19 +203,20 @@ static void x264_mb_analyse_load_costs( x264_t *h, x264_mb_analysis_t *a )
 
 static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp )
 {
+    int i = h->param.analyse.i_subpel_refine - (h->sh.i_type == SLICE_TYPE_B);
+    /* mbrd == 1 -> RD mode decision */
+    /* mbrd == 2 -> RD refinement */
+    a->i_mbrd = (i>=6) + (i>=8);
     /* conduct the analysis using this lamda and QP */
     a->i_qp = h->mb.i_qp = i_qp;
     h->mb.i_chroma_qp = h->chroma_qp_table[i_qp];
     a->i_lambda = x264_lambda_tab[i_qp];
     a->i_lambda2 = x264_lambda2_tab[i_qp];
-    a->b_mbrd = h->param.analyse.i_subpel_refine >= 6 &&
-                ( h->sh.i_type != SLICE_TYPE_B || h->param.analyse.b_bframe_rdo );
-
     h->mb.i_me_method = h->param.analyse.i_me_method;
     h->mb.i_subpel_refine = h->param.analyse.i_subpel_refine;
     h->mb.b_chroma_me = h->param.analyse.b_chroma_me && h->sh.i_type == SLICE_TYPE_P
                         && h->mb.i_subpel_refine >= 5;
-    h->mb.b_trellis = h->param.analyse.i_trellis > 1 && a->b_mbrd;
+    h->mb.b_trellis = h->param.analyse.i_trellis > 1 && a->i_mbrd;
     h->mb.b_transform_8x8 = 0;
     h->mb.b_noise_reduction = 0;
 
@@ -225,13 +226,13 @@ static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp )
     a->i_satd_i4x4   =
     a->i_satd_i8x8chroma = COST_MAX;
 
-    /* non-RD PCM decision is inaccurate, so don't do it */
-    a->i_satd_pcm = a->b_mbrd ? ((uint64_t)X264_PCM_COST*a->i_lambda2 + 128) >> 8 : COST_MAX;
+    /* non-RD PCM decision is inaccurate (as is psy-rd), so don't do it */
+    a->i_satd_pcm = !h->mb.i_psy_rd && a->i_mbrd ? ((uint64_t)X264_PCM_COST*a->i_lambda2 + 128) >> 8 : COST_MAX;
 
     a->b_fast_intra = 0;
     h->mb.i_skip_intra =
         h->mb.b_lossless ? 0 :
-        a->b_mbrd ? 2 :
+        a->i_mbrd ? 2 :
         !h->param.analyse.i_trellis && !h->param.analyse.i_noise_reduction;
 
     /* II: Inter part P/B frame */
@@ -569,8 +570,13 @@ static void x264_mb_analyse_intra_chroma( x264_t *h, x264_mb_analysis_t *a )
             int i_mode = predict_mode[i];
 
             /* we do the prediction */
-            h->predict_8x8c[i_mode]( p_dstc[0] );
-            h->predict_8x8c[i_mode]( p_dstc[1] );
+            if( h->mb.b_lossless )
+                x264_predict_lossless_8x8_chroma( h, i_mode );
+            else
+            {
+                h->predict_8x8c[i_mode]( p_dstc[0] );
+                h->predict_8x8c[i_mode]( p_dstc[1] );
+            }
 
             /* we calculate the cost */
             i_satd = h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], FDEC_STRIDE,
@@ -596,7 +602,7 @@ static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *a, int i_satd_
     int i, idx;
     int i_max;
     int predict_mode[9];
-    int b_merged_satd = !!h->pixf.intra_mbcmp_x3_16x16;
+    int b_merged_satd = !!h->pixf.intra_mbcmp_x3_16x16 && !h->mb.b_lossless;
 
     /*---------------- Try all mode and calculate their score ---------------*/
 
@@ -621,7 +627,11 @@ static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *a, int i_satd_
         {
             int i_satd;
             int i_mode = predict_mode[i];
-            h->predict_16x16[i_mode]( p_dst );
+
+            if( h->mb.b_lossless )
+                x264_predict_lossless_16x16( h, i_mode );
+            else
+                h->predict_16x16[i_mode]( p_dst );
 
             i_satd = h->pixf.mbcmp[PIXEL_16x16]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE ) +
                     a->i_lambda * bs_size_ue( x264_mb_pred_mode16x16_fix[i_mode] );
@@ -641,7 +651,7 @@ static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *a, int i_satd_
     {
         DECLARE_ALIGNED_16( uint8_t edge[33] );
         x264_pixel_cmp_t sa8d = (h->pixf.mbcmp[0] == h->pixf.satd[0]) ? h->pixf.sa8d[PIXEL_8x8] : h->pixf.mbcmp[PIXEL_8x8];
-        int i_satd_thresh = a->b_mbrd ? COST_MAX : X264_MIN( i_satd_inter, a->i_satd_i16x16 );
+        int i_satd_thresh = a->i_mbrd ? COST_MAX : X264_MIN( i_satd_inter, a->i_satd_i16x16 );
         int i_cost = 0;
         b_merged_satd = h->pixf.intra_sa8d_x3_8x8 && h->pixf.mbcmp[0] == h->pixf.satd[0];
 
@@ -681,7 +691,10 @@ static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *a, int i_satd_
                 int i_satd;
                 int i_mode = predict_mode[i];
 
-                h->predict_8x8[i_mode]( p_dst_by, edge );
+                if( h->mb.b_lossless )
+                    x264_predict_lossless_8x8( h, p_dst_by, idx, i_mode, edge );
+                else
+                    h->predict_8x8[i_mode]( p_dst_by, edge );
 
                 i_satd = sa8d( p_dst_by, FDEC_STRIDE, p_src_by, FENC_STRIDE )
                        + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
@@ -716,7 +729,7 @@ static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *a, int i_satd_
             a->i_satd_i8x8 = COST_MAX;
             i_cost = i_cost * 4/(idx+1);
         }
-        if( X264_MIN(i_cost, a->i_satd_i16x16) > i_satd_inter*(5+a->b_mbrd)/4 )
+        if( X264_MIN(i_cost, a->i_satd_i16x16) > i_satd_inter*(5+!!a->i_mbrd)/4 )
             return;
     }
 
@@ -726,7 +739,7 @@ static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *a, int i_satd_
         int i_cost;
         int i_satd_thresh = X264_MIN3( i_satd_inter, a->i_satd_i16x16, a->i_satd_i8x8 );
         b_merged_satd = h->pixf.intra_satd_x3_4x4 && h->pixf.mbcmp[0] == h->pixf.satd[0];
-        if( a->b_mbrd )
+        if( a->i_mbrd )
             i_satd_thresh = i_satd_thresh * (10-a->b_fast_intra)/8;
 
         i_cost = a->i_lambda * 24;    /* from JVT (SATD0) */
@@ -763,8 +776,10 @@ static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *a, int i_satd_
             {
                 int i_satd;
                 int i_mode = predict_mode[i];
-
-                h->predict_4x4[i_mode]( p_dst_by );
+                if( h->mb.b_lossless )
+                    x264_predict_lossless_4x4( h, p_dst_by, idx, i_mode );
+                else
+                    h->predict_4x4[i_mode]( p_dst_by );
 
                 i_satd = h->pixf.mbcmp[PIXEL_4x4]( p_dst_by, FDEC_STRIDE,
                                                    p_src_by, FENC_STRIDE )
@@ -876,7 +891,10 @@ static void x264_intra_rd_refine( x264_t *h, x264_mb_analysis_t *a )
             for( i = 0; i < i_max; i++ )
             {
                 i_mode = predict_mode[i];
-                h->predict_4x4[i_mode]( p_dst_by );
+                if( h->mb.b_lossless )
+                    x264_predict_lossless_4x4( h, p_dst_by, idx, i_mode );
+                else
+                    h->predict_4x4[i_mode]( p_dst_by );
                 i_satd = x264_rd_cost_i4x4( h, a->i_lambda2, idx, i_mode );
 
                 if( i_best > i_satd )
@@ -928,7 +946,10 @@ static void x264_intra_rd_refine( x264_t *h, x264_mb_analysis_t *a )
                 i_mode = predict_mode[i];
                 if( a->i_satd_i8x8_dir[i_mode][idx] > i_thresh )
                     continue;
-                h->predict_8x8[i_mode]( p_dst_by, edge );
+                if( h->mb.b_lossless )
+                    x264_predict_lossless_8x8( h, p_dst_by, idx, i_mode, edge );
+                else
+                    h->predict_8x8[i_mode]( p_dst_by, edge );
                 i_satd = x264_rd_cost_i8x8( h, a->i_lambda2, idx, i_mode );
 
                 if( i_best > i_satd )
@@ -980,8 +1001,13 @@ static void x264_intra_rd_refine( x264_t *h, x264_mb_analysis_t *a )
             for( i = 0; i < i_max; i++ )
             {
                 i_mode = predict_mode[i];
-                h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[1] );
-                h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[2] );
+                if( h->mb.b_lossless )
+                    x264_predict_lossless_8x8_chroma( h, i_mode );
+                else
+                {
+                    h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[1] );
+                    h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[2] );
+                }
                 /* if we've already found a mode that needs no residual, then
                  * probably any mode with a residual will be worse.
                  * so avoid dct on the remaining modes to improve speed. */
@@ -1016,7 +1042,7 @@ static void x264_mb_analyse_inter_p16x16( x264_t *h, x264_mb_analysis_t *a )
 {
     x264_me_t m;
     int i_ref, i_mvc;
-    DECLARE_ALIGNED_4( int16_t mvc[7][2] );
+    DECLARE_ALIGNED_4( int16_t mvc[8][2] );
     int i_halfpel_thresh = INT_MAX;
     int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
 
@@ -1069,7 +1095,7 @@ static void x264_mb_analyse_inter_p16x16( x264_t *h, x264_mb_analysis_t *a )
     assert( a->l0.me16x16.mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
 
     h->mb.i_type = P_L0;
-    if( a->b_mbrd )
+    if( a->i_mbrd )
     {
         x264_mb_cache_fenc_satd( h );
         if( a->l0.me16x16.i_ref == 0 && *(uint32_t*)a->l0.me16x16.mv == *(uint32_t*)h->mb.cache.pskip_mv )
@@ -1474,26 +1500,21 @@ static void x264_mb_analyse_inter_direct( x264_t *h, x264_mb_analysis_t *a )
     }
 }
 
-#define WEIGHTED_AVG( size, pix1, stride1, src2, stride2 ) \
-    { \
-        if( h->param.analyse.b_weighted_bipred ) \
-            h->mc.avg_weight[size]( pix1, stride1, src2, stride2, \
-                    h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] ); \
-        else \
-            h->mc.avg[size]( pix1, stride1, src2, stride2 ); \
-    }
+#define WEIGHTED_AVG( size, pix, stride, src1, stride1, src2, stride2 ) \
+{ \
+    h->mc.avg[size]( pix, stride, src1, stride1, src2, stride2, h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] ); \
+}
 
 static void x264_mb_analyse_inter_b16x16( x264_t *h, x264_mb_analysis_t *a )
 {
+    DECLARE_ALIGNED_16( uint8_t pix0[16*16] );
     DECLARE_ALIGNED_16( uint8_t pix1[16*16] );
-    DECLARE_ALIGNED_16( uint8_t pix2[16*16] );
-    uint8_t *src2;
-    int stride2 = 16;
-    int weight;
+    uint8_t *src0, *src1;
+    int stride0 = 16, stride1 = 16;
 
     x264_me_t m;
     int i_ref, i_mvc;
-    DECLARE_ALIGNED_4( int16_t mvc[8][2] );
+    DECLARE_ALIGNED_4( int16_t mvc[9][2] );
     int i_halfpel_thresh = INT_MAX;
     int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
 
@@ -1559,41 +1580,16 @@ static void x264_mb_analyse_inter_b16x16( x264_t *h, x264_mb_analysis_t *a )
     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
 
     /* get cost of BI mode */
-    weight = h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref];
-    if ( (*(uint32_t*)a->l0.me16x16.mv & 0x10001) == 0 )
-    {
-        /* l0 reference is halfpel, so get_ref on it will make it faster */
-        src2 =
-        h->mc.get_ref( pix2, &stride2,
-                       h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
-                       a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
-                       16, 16 );
-        h->mc.mc_luma( pix1, 16,
-                       h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
-                       a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
-                       16, 16 );
-        weight = 64 - weight;
-    }
-    else
-    {
-        /* if l0 was qpel, we'll use get_ref on l1 instead */
-        h->mc.mc_luma( pix1, 16,
-                       h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
-                       a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
-                       16, 16 );
-        src2 =
-        h->mc.get_ref( pix2, &stride2,
-                       h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
-                       a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
-                       16, 16 );
-    }
+    src0 = h->mc.get_ref( pix0, &stride0,
+                           h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
+                           a->l0.me16x16.mv[0], a->l0.me16x16.mv[1], 16, 16 );
+    src1 = h->mc.get_ref( pix1, &stride1,
+                           h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
+                           a->l1.me16x16.mv[0], a->l1.me16x16.mv[1], 16, 16 );
 
-    if( h->param.analyse.b_weighted_bipred )
-        h->mc.avg_weight[PIXEL_16x16]( pix1, 16, src2, stride2, weight );
-    else
-        h->mc.avg[PIXEL_16x16]( pix1, 16, src2, stride2 );
+    h->mc.avg[PIXEL_16x16]( pix0, 16, src0, stride0, src1, stride1, h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] );
 
-    a->i_cost16x16bi = h->pixf.mbcmp[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE, pix1, 16 )
+    a->i_cost16x16bi = h->pixf.mbcmp[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE, pix0, 16 )
                      + REF_COST( 0, a->l0.i_ref )
                      + REF_COST( 1, a->l1.i_ref )
                      + a->l0.me16x16.cost_mv
@@ -1709,6 +1705,8 @@ static void x264_mb_analyse_inter_b8x8( x264_t *h, x264_mb_analysis_t *a )
         const int y8 = i/2;
         int i_part_cost;
         int i_part_cost_bi = 0;
+        int stride[2] = {8,8};
+        uint8_t *src[2];
 
         for( l = 0; l < 2; l++ )
         {
@@ -1727,13 +1725,12 @@ static void x264_mb_analyse_inter_b8x8( x264_t *h, x264_mb_analysis_t *a )
             x264_macroblock_cache_mv_ptr( h, 2*x8, 2*y8, 2, 2, l, m->mv );
 
             /* BI mode */
-            h->mc.mc_luma( pix[l], 8, m->p_fref, m->i_stride[0],
-                           m->mv[0], m->mv[1], 8, 8 );
+            src[l] = h->mc.get_ref( pix[l], &stride[l], m->p_fref, m->i_stride[0],
+                                    m->mv[0], m->mv[1], 8, 8 );
             i_part_cost_bi += m->cost_mv;
             /* FIXME: ref cost */
         }
-
-        WEIGHTED_AVG( PIXEL_8x8, pix[0], 8, pix[1], 8 );
+        h->mc.avg[PIXEL_8x8]( pix[0], 8, src[0], stride[0], src[1], stride[1], h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] );
         i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x8]( a->l0.me8x8[i].p_fenc[0], FENC_STRIDE, pix[0], 8 )
                         + a->i_lambda * i_sub_mb_b_cost_table[D_BI_8x8];
         a->l0.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
@@ -1759,7 +1756,7 @@ static void x264_mb_analyse_inter_b16x8( x264_t *h, x264_mb_analysis_t *a )
     uint8_t **p_fref[2] =
         { h->mb.pic.p_fref[0][a->l0.i_ref],
           h->mb.pic.p_fref[1][a->l1.i_ref] };
-    DECLARE_ALIGNED_16( uint8_t  pix[2][16*8] );
+    DECLARE_ALIGNED_16( uint8_t pix[2][16*8] );
     DECLARE_ALIGNED_4( int16_t mvc[2][2] );
     int i, l;
 
@@ -1770,6 +1767,8 @@ static void x264_mb_analyse_inter_b16x8( x264_t *h, x264_mb_analysis_t *a )
     {
         int i_part_cost;
         int i_part_cost_bi = 0;
+        int stride[2] = {16,16};
+        uint8_t *src[2];
 
         /* TODO: check only the list(s) that were used in b8x8? */
         for( l = 0; l < 2; l++ )
@@ -1790,13 +1789,12 @@ static void x264_mb_analyse_inter_b16x8( x264_t *h, x264_mb_analysis_t *a )
             x264_me_search( h, m, mvc, 2 );
 
             /* BI mode */
-            h->mc.mc_luma( pix[l], 16, m->p_fref, m->i_stride[0],
-                           m->mv[0], m->mv[1], 16, 8 );
+            src[l] = h->mc.get_ref( pix[l], &stride[l], m->p_fref, m->i_stride[0],
+                                    m->mv[0], m->mv[1], 16, 8 );
             /* FIXME: ref cost */
             i_part_cost_bi += m->cost_mv;
         }
-
-        WEIGHTED_AVG( PIXEL_16x8, pix[0], 16, pix[1], 16 );
+        h->mc.avg[PIXEL_16x8]( pix[0], 16, src[0], stride[0], src[1], stride[1], h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] );
         i_part_cost_bi += h->pixf.mbcmp[PIXEL_16x8]( a->l0.me16x8[i].p_fenc[0], FENC_STRIDE, pix[0], 16 );
 
         i_part_cost = a->l0.me16x8[i].cost;
@@ -1839,6 +1837,8 @@ static void x264_mb_analyse_inter_b8x16( x264_t *h, x264_mb_analysis_t *a )
     {
         int i_part_cost;
         int i_part_cost_bi = 0;
+        int stride[2] = {8,8};
+        uint8_t *src[2];
 
         for( l = 0; l < 2; l++ )
         {
@@ -1858,13 +1858,13 @@ static void x264_mb_analyse_inter_b8x16( x264_t *h, x264_mb_analysis_t *a )
             x264_me_search( h, m, mvc, 2 );
 
             /* BI mode */
-            h->mc.mc_luma( pix[l], 8, m->p_fref, m->i_stride[0],
-                           m->mv[0], m->mv[1], 8, 16 );
+            src[l] = h->mc.get_ref( pix[l], &stride[l], m->p_fref,  m->i_stride[0],
+                                    m->mv[0], m->mv[1], 8, 16 );
             /* FIXME: ref cost */
             i_part_cost_bi += m->cost_mv;
         }
 
-        WEIGHTED_AVG( PIXEL_8x16, pix[0], 8, pix[1], 8 );
+        h->mc.avg[PIXEL_8x16]( pix[0], 8, src[0], stride[0], src[1], stride[1], h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] );
         i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x16]( a->l0.me8x16[i].p_fenc[0], FENC_STRIDE, pix[0], 8 );
 
         i_part_cost = a->l0.me8x16[i].cost;
@@ -1926,35 +1926,36 @@ static void x264_mb_analyse_p_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd )
     {
         h->mb.i_type = P_8x8;
         h->mb.i_partition = D_8x8;
-        x264_analyse_update_cache( h, a );
-        a->l0.i_cost8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
-
         if( h->param.analyse.inter & X264_ANALYSE_PSUB8x8 )
         {
-            /* FIXME: RD per subpartition */
-            int part_bak[4];
-            int i, i_cost;
-            int b_sub8x8 = 0;
-            for( i=0; i<4; i++ )
-            {
-                part_bak[i] = h->mb.i_sub_partition[i];
-                b_sub8x8 |= (part_bak[i] != D_L0_8x8);
-            }
-            if( b_sub8x8 )
+            int i;
+            x264_macroblock_cache_ref( h, 0, 0, 2, 2, 0, a->l0.me8x8[0].i_ref );
+            x264_macroblock_cache_ref( h, 2, 0, 2, 2, 0, a->l0.me8x8[1].i_ref );
+            x264_macroblock_cache_ref( h, 0, 2, 2, 2, 0, a->l0.me8x8[2].i_ref );
+            x264_macroblock_cache_ref( h, 2, 2, 2, 2, 0, a->l0.me8x8[3].i_ref );
+            for( i = 0; i < 4; i++ )
             {
-                h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
-                h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
-                x264_analyse_update_cache( h, a );
-                i_cost = x264_rd_cost_mb( h, a->i_lambda2 );
-                if( a->l0.i_cost8x8 < i_cost )
+                int costs[4] = {a->l0.i_cost4x4[i], a->l0.i_cost8x4[i], a->l0.i_cost4x8[i], a->l0.me8x8[i].cost};
+                int thresh = X264_MIN4( costs[0], costs[1], costs[2], costs[3] ) * 5 / 4;
+                int subtype, btype = D_L0_8x8;
+                uint64_t bcost = COST_MAX64;
+                for( subtype = D_L0_4x4; subtype <= D_L0_8x8; subtype++ )
                 {
-                    for( i=0; i<4; i++ )
-                        h->mb.i_sub_partition[i] = part_bak[i];
+                    uint64_t cost;
+                    if( costs[subtype] > thresh || (subtype == D_L0_8x8 && bcost == COST_MAX64) )
+                        continue;
+                    h->mb.i_sub_partition[i] = subtype;
+                    x264_mb_cache_mv_p8x8( h, a, i );
+                    cost = x264_rd_cost_part( h, a->i_lambda2, i<<2, PIXEL_8x8 );
+                    COPY2_IF_LT( bcost, cost, btype, subtype );
                 }
-                else
-                   a->l0.i_cost8x8 = i_cost;
+                h->mb.i_sub_partition[i] = btype;
+                x264_mb_cache_mv_p8x8( h, a, i );
             }
         }
+        else
+            x264_analyse_update_cache( h, a );
+        a->l0.i_cost8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
     }
     else
         a->l0.i_cost8x8 = COST_MAX;
@@ -2030,38 +2031,41 @@ static void x264_mb_analyse_b_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd_i
     }
 }
 
-static void refine_bidir( x264_t *h, x264_mb_analysis_t *a )
+static void x264_refine_bidir( x264_t *h, x264_mb_analysis_t *a )
 {
     const int i_biweight = h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref];
     int i;
 
+    if( IS_INTRA(h->mb.i_type) )
+        return;
+
     switch( h->mb.i_partition )
     {
     case D_16x16:
         if( h->mb.i_type == B_BI_BI )
-            x264_me_refine_bidir( h, &a->l0.me16x16, &a->l1.me16x16, i_biweight );
+            x264_me_refine_bidir_satd( h, &a->l0.me16x16, &a->l1.me16x16, i_biweight );
         break;
     case D_16x8:
         for( i=0; i<2; i++ )
             if( a->i_mb_partition16x8[i] == D_BI_8x8 )
-                x264_me_refine_bidir( h, &a->l0.me16x8[i], &a->l1.me16x8[i], i_biweight );
+                x264_me_refine_bidir_satd( h, &a->l0.me16x8[i], &a->l1.me16x8[i], i_biweight );
         break;
     case D_8x16:
         for( i=0; i<2; i++ )
             if( a->i_mb_partition8x16[i] == D_BI_8x8 )
-                x264_me_refine_bidir( h, &a->l0.me8x16[i], &a->l1.me8x16[i], i_biweight );
+                x264_me_refine_bidir_satd( h, &a->l0.me8x16[i], &a->l1.me8x16[i], i_biweight );
         break;
     case D_8x8:
         for( i=0; i<4; i++ )
             if( h->mb.i_sub_partition[i] == D_BI_8x8 )
-                x264_me_refine_bidir( h, &a->l0.me8x8[i], &a->l1.me8x8[i], i_biweight );
+                x264_me_refine_bidir_satd( h, &a->l0.me8x8[i], &a->l1.me8x8[i], i_biweight );
         break;
     }
 }
 
 static inline void x264_mb_analyse_transform( x264_t *h )
 {
-    if( x264_mb_transform_8x8_allowed( h ) && h->param.analyse.b_transform_8x8 )
+    if( x264_mb_transform_8x8_allowed( h ) && h->param.analyse.b_transform_8x8 && !h->mb.b_lossless )
     {
         int i_cost4, i_cost8;
         /* Only luma MC is really needed, but the full MC is re-used in macroblock_encode. */
@@ -2121,10 +2125,10 @@ void x264_macroblock_analyse( x264_t *h )
     /*--------------------------- Do the analysis ---------------------------*/
     if( h->sh.i_type == SLICE_TYPE_I )
     {
-        if( analysis.b_mbrd )
+        if( analysis.i_mbrd )
             x264_mb_cache_fenc_satd( h );
         x264_mb_analyse_intra( h, &analysis, COST_MAX );
-        if( analysis.b_mbrd )
+        if( analysis.i_mbrd )
             x264_intra_rd( h, &analysis, COST_MAX );
 
         i_cost = analysis.i_satd_i16x16;
@@ -2134,7 +2138,7 @@ void x264_macroblock_analyse( x264_t *h )
         if( analysis.i_satd_pcm < i_cost )
             h->mb.i_type = I_PCM;
 
-        else if( h->mb.i_subpel_refine >= 7 )
+        else if( analysis.i_mbrd >= 2 )
             x264_intra_rd_refine( h, &analysis );
     }
     else if( h->sh.i_type == SLICE_TYPE_P )
@@ -2246,7 +2250,7 @@ void x264_macroblock_analyse( x264_t *h )
 
             /* refine qpel */
             //FIXME mb_type costs?
-            if( analysis.b_mbrd )
+            if( analysis.i_mbrd )
             {
                 /* refine later */
             }
@@ -2325,7 +2329,7 @@ void x264_macroblock_analyse( x264_t *h )
                                       analysis.i_satd_i8x8,
                                       analysis.i_satd_i4x4 );
 
-            if( analysis.b_mbrd )
+            if( analysis.i_mbrd )
             {
                 x264_mb_analyse_p_rd( h, &analysis, X264_MIN(i_satd_inter, i_satd_intra) );
                 i_type = P_L0;
@@ -2356,7 +2360,7 @@ void x264_macroblock_analyse( x264_t *h )
             h->stat.frame.i_inter_cost += i_cost;
             h->stat.frame.i_mbs_analysed++;
 
-            if( h->mb.i_subpel_refine >= 7 && h->mb.i_type != I_PCM )
+            if( analysis.i_mbrd >= 2 && h->mb.i_type != I_PCM )
             {
                 if( IS_INTRA( h->mb.i_type ) )
                 {
@@ -2365,7 +2369,7 @@ void x264_macroblock_analyse( x264_t *h )
                 else if( i_partition == D_16x16 )
                 {
                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, analysis.l0.me16x16.i_ref );
-                    x264_me_refine_qpel_rd( h, &analysis.l0.me16x16, analysis.i_lambda2, 0 );
+                    x264_me_refine_qpel_rd( h, &analysis.l0.me16x16, analysis.i_lambda2, 0, 0 );
                 }
                 else if( i_partition == D_16x8 )
                 {
@@ -2373,8 +2377,8 @@ void x264_macroblock_analyse( x264_t *h )
                     h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
                     x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, analysis.l0.me16x8[0].i_ref );
                     x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, analysis.l0.me16x8[1].i_ref );
-                    x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[0], analysis.i_lambda2, 0 );
-                    x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[1], analysis.i_lambda2, 2 );
+                    x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[0], analysis.i_lambda2, 0, 0 );
+                    x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[1], analysis.i_lambda2, 8, 0 );
                 }
                 else if( i_partition == D_8x16 )
                 {
@@ -2382,16 +2386,37 @@ void x264_macroblock_analyse( x264_t *h )
                     h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
                     x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, analysis.l0.me8x16[0].i_ref );
                     x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, analysis.l0.me8x16[1].i_ref );
-                    x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[0], analysis.i_lambda2, 0 );
-                    x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[1], analysis.i_lambda2, 1 );
+                    x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[0], analysis.i_lambda2, 0, 0 );
+                    x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[1], analysis.i_lambda2, 4, 0 );
                 }
                 else if( i_partition == D_8x8 )
                 {
                     int i8x8;
                     x264_analyse_update_cache( h, &analysis );
                     for( i8x8 = 0; i8x8 < 4; i8x8++ )
-                         if( h->mb.i_sub_partition[i8x8] == D_L0_8x8 )
-                             x264_me_refine_qpel_rd( h, &analysis.l0.me8x8[i8x8], analysis.i_lambda2, i8x8 );
+                    {
+                        if( h->mb.i_sub_partition[i8x8] == D_L0_8x8 )
+                        {
+                            x264_me_refine_qpel_rd( h, &analysis.l0.me8x8[i8x8], analysis.i_lambda2, i8x8*4, 0 );
+                        }
+                        else if( h->mb.i_sub_partition[i8x8] == D_L0_8x4 )
+                        {
+                           x264_me_refine_qpel_rd( h, &analysis.l0.me8x4[i8x8][0], analysis.i_lambda2, i8x8*4+0, 0 );
+                           x264_me_refine_qpel_rd( h, &analysis.l0.me8x4[i8x8][1], analysis.i_lambda2, i8x8*4+2, 0 );
+                        }
+                        else if( h->mb.i_sub_partition[i8x8] == D_L0_4x8 )
+                        {
+                           x264_me_refine_qpel_rd( h, &analysis.l0.me4x8[i8x8][0], analysis.i_lambda2, i8x8*4+0, 0 );
+                           x264_me_refine_qpel_rd( h, &analysis.l0.me4x8[i8x8][1], analysis.i_lambda2, i8x8*4+1, 0 );
+                        }
+                        else if( h->mb.i_sub_partition[i8x8] == D_L0_4x4 )
+                        {
+                           x264_me_refine_qpel_rd( h, &analysis.l0.me4x4[i8x8][0], analysis.i_lambda2, i8x8*4+0, 0 );
+                           x264_me_refine_qpel_rd( h, &analysis.l0.me4x4[i8x8][1], analysis.i_lambda2, i8x8*4+1, 0 );
+                           x264_me_refine_qpel_rd( h, &analysis.l0.me4x4[i8x8][2], analysis.i_lambda2, i8x8*4+2, 0 );
+                           x264_me_refine_qpel_rd( h, &analysis.l0.me4x4[i8x8][3], analysis.i_lambda2, i8x8*4+3, 0 );
+                        }
+                    }
                 }
             }
         }
@@ -2401,7 +2426,7 @@ void x264_macroblock_analyse( x264_t *h )
         int i_bskip_cost = COST_MAX;
         int b_skip = 0;
 
-        if( analysis.b_mbrd )
+        if( analysis.i_mbrd )
             x264_mb_cache_fenc_satd( h );
 
         h->mb.i_type = B_SKIP;
@@ -2437,7 +2462,7 @@ void x264_macroblock_analyse( x264_t *h )
             {
                 /* chance of skip is too small to bother */
             }
-            else if( analysis.b_mbrd )
+            else if( analysis.i_mbrd )
             {
                 i_bskip_cost = ssd_mb( h );
                 /* 6 = minimum cavlc cost of a non-skipped MB */
@@ -2475,7 +2500,7 @@ void x264_macroblock_analyse( x264_t *h )
             COPY2_IF_LT( i_cost, analysis.i_cost16x16bi, i_type, B_BI_BI );
             COPY2_IF_LT( i_cost, analysis.i_cost16x16direct, i_type, B_DIRECT );
 
-            if( analysis.b_mbrd && analysis.i_cost16x16direct <= i_cost * 33/32 )
+            if( analysis.i_mbrd && analysis.i_cost16x16direct <= i_cost * 33/32 )
             {
                 x264_mb_analyse_b_rd( h, &analysis, i_cost );
                 if( i_bskip_cost < analysis.i_rd16x16direct &&
@@ -2517,7 +2542,7 @@ void x264_macroblock_analyse( x264_t *h )
                 }
             }
 
-            if( analysis.b_mbrd )
+            if( analysis.i_mbrd )
             {
                 /* refine later */
             }
@@ -2600,7 +2625,7 @@ void x264_macroblock_analyse( x264_t *h )
                 }
             }
 
-            if( analysis.b_mbrd )
+            if( analysis.i_mbrd )
             {
                 i_satd_inter = i_cost;
                 x264_mb_analyse_b_rd( h, &analysis, i_satd_inter );
@@ -2621,7 +2646,7 @@ void x264_macroblock_analyse( x264_t *h )
 
             x264_mb_analyse_intra( h, &analysis, i_satd_inter );
 
-            if( analysis.b_mbrd )
+            if( analysis.i_mbrd )
             {
                 x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
                 x264_intra_rd( h, &analysis, i_satd_inter * 17/16 );
@@ -2635,16 +2660,70 @@ void x264_macroblock_analyse( x264_t *h )
             h->mb.i_type = i_type;
             h->mb.i_partition = i_partition;
 
-            if( analysis.b_mbrd && h->mb.i_subpel_refine >= 7 && IS_INTRA( i_type ) && i_type != I_PCM )
+            if( analysis.i_mbrd >= 2 && IS_INTRA( i_type ) && i_type != I_PCM )
                 x264_intra_rd_refine( h, &analysis );
-            else if( h->param.analyse.b_bidir_me )
-                refine_bidir( h, &analysis );
+            if( h->mb.i_subpel_refine >= 5 )
+                x264_refine_bidir( h, &analysis );
+
+            if( analysis.i_mbrd >= 2 && i_type > B_DIRECT && i_type < B_SKIP )
+            {
+                const int i_biweight = h->mb.bipred_weight[analysis.l0.i_ref][analysis.l1.i_ref];
+                x264_analyse_update_cache( h, &analysis );
+
+                if( i_partition == D_16x16 )
+                {
+                    if( i_type == B_L0_L0 )
+                        x264_me_refine_qpel_rd( h, &analysis.l0.me16x16, analysis.i_lambda2, 0, 0 );
+                    else if( i_type == B_L1_L1 )
+                        x264_me_refine_qpel_rd( h, &analysis.l1.me16x16, analysis.i_lambda2, 0, 1 );
+                    else if( i_type == B_BI_BI )
+                        x264_me_refine_bidir_rd( h, &analysis.l0.me16x16, &analysis.l1.me16x16, i_biweight, 0, analysis.i_lambda2 );
+                }
+                else if( i_partition == D_16x8 )
+                {
+                    for( i = 0; i < 2; i++ )
+                    {
+                        h->mb.i_sub_partition[i*2] = h->mb.i_sub_partition[i*2+1] = analysis.i_mb_partition16x8[i];
+                        if( analysis.i_mb_partition16x8[i] == D_L0_8x8 )
+                            x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[i], analysis.i_lambda2, i*8, 0 );
+                        else if( analysis.i_mb_partition16x8[i] == D_L1_8x8 )
+                            x264_me_refine_qpel_rd( h, &analysis.l1.me16x8[i], analysis.i_lambda2, i*8, 1 );
+                        else if( analysis.i_mb_partition16x8[i] == D_BI_8x8 )
+                            x264_me_refine_bidir_rd( h, &analysis.l0.me16x8[i], &analysis.l1.me16x8[i], i_biweight, i*2, analysis.i_lambda2 );
+                    }
+                }
+                else if( i_partition == D_8x16 )
+                {
+                    for( i = 0; i < 2; i++ )
+                    {
+                        h->mb.i_sub_partition[i] = h->mb.i_sub_partition[i+2] = analysis.i_mb_partition8x16[i];
+                        if( analysis.i_mb_partition8x16[i] == D_L0_8x8 )
+                            x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[i], analysis.i_lambda2, i*4, 0 );
+                        else if( analysis.i_mb_partition8x16[i] == D_L1_8x8 )
+                            x264_me_refine_qpel_rd( h, &analysis.l1.me8x16[i], analysis.i_lambda2, i*4, 1 );
+                        else if( analysis.i_mb_partition8x16[i] == D_BI_8x8 )
+                            x264_me_refine_bidir_rd( h, &analysis.l0.me8x16[i], &analysis.l1.me8x16[i], i_biweight, i, analysis.i_lambda2 );
+                    }
+                }
+                else if( i_partition == D_8x8 )
+                {
+                    for( i = 0; i < 4; i++ )
+                    {
+                        if( h->mb.i_sub_partition[i] == D_L0_8x8 )
+                            x264_me_refine_qpel_rd( h, &analysis.l0.me8x8[i], analysis.i_lambda2, i*4, 0 );
+                        else if( h->mb.i_sub_partition[i] == D_L1_8x8 )
+                            x264_me_refine_qpel_rd( h, &analysis.l1.me8x8[i], analysis.i_lambda2, i*4, 1 );
+                        else if( h->mb.i_sub_partition[i] == D_BI_8x8 )
+                            x264_me_refine_bidir_rd( h, &analysis.l0.me8x8[i], &analysis.l1.me8x8[i], i_biweight, i, analysis.i_lambda2 );
+                    }
+                }
+            }
         }
     }
 
     x264_analyse_update_cache( h, &analysis );
 
-    if( !analysis.b_mbrd )
+    if( !analysis.i_mbrd )
         x264_mb_analyse_transform( h );
 
     h->mb.b_trellis = h->param.analyse.i_trellis;