]> git.sesse.net Git - x264/blobdiff - common/macroblock.c
get_ref_sse2
[x264] / common / macroblock.c
index 1eca58cc4ddaf0bb6b34b61a11da1f664d4e358b..ead6a7c5eb531e5055e9dfdc4e1115ba7c1352db 100644 (file)
@@ -21,9 +21,6 @@
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  *****************************************************************************/
 
-#include <stdio.h>
-#include <string.h>
-
 #include "common.h"
 
 int x264_mb_predict_intra4x4_mode( x264_t *h, int idx )
@@ -55,22 +52,40 @@ int x264_mb_predict_non_zero_code( x264_t *h, int idx )
 
 int x264_mb_transform_8x8_allowed( x264_t *h )
 {
-    if( IS_SKIP( h->mb.i_type ) )
+    // intra and skip are disallowed
+    // large partitions are allowed
+    // direct and 8x8 are conditional
+    static const uint8_t partition_tab[X264_MBTYPE_MAX] = {
+        0,0,0,0,1,2,0,2,1,1,1,1,1,1,1,1,1,2,0,
+    };
+    int p, i;
+
+    if( !h->pps->b_transform_8x8_mode )
         return 0;
-    if( h->mb.i_type == P_8x8 || h->mb.i_type == B_8x8 )
+    p = partition_tab[h->mb.i_type];
+    if( p < 2 )
+        return p;
+    else if( h->mb.i_type == B_DIRECT )
+        return h->sps->b_direct8x8_inference;
+    else if( h->mb.i_type == P_8x8 )
     {
-        int i;
-        for( i = 0; i < 4; i++ )
-            if( !IS_SUB8x8(h->mb.i_sub_partition[i])
-                || ( h->mb.i_sub_partition[i] == D_DIRECT_8x8 && !h->sps->b_direct8x8_inference ) )
-            {
+        if( !(h->param.analyse.inter & X264_ANALYSE_PSUB8x8) )
+            return 1;
+        for( i=0; i<4; i++ )
+            if( h->mb.i_sub_partition[i] != D_L0_8x8 )
                 return 0;
-            }
+        return 1;
+    }
+    else // B_8x8
+    {
+        // x264 currently doesn't use sub-8x8 B partitions, so don't check for them
+        if( h->sps->b_direct8x8_inference )
+            return 1;
+        for( i=0; i<4; i++ )
+            if( h->mb.i_sub_partition[i] == D_DIRECT_8x8 )
+                return 0;
+        return 1;
     }
-    if( h->mb.i_type == B_DIRECT && !h->sps->b_direct8x8_inference )
-        return 0;
-
-    return 1;
 }
 
 void x264_mb_predict_mv( x264_t *h, int i_list, int idx, int i_width, int mvp[2] )
@@ -299,7 +314,7 @@ static int x264_mb_predict_mv_direct16x16_temporal( x264_t *h )
         }
         else
         {
-            /* the colocated ref isn't in the current list0 */
+            /* the collocated ref isn't in the current list0 */
             /* FIXME: we might still be able to use direct_8x8 on some partitions */
             /* FIXME: with B-pyramid + extensive ref list reordering
              *   (not currently used), we would also have to check
@@ -308,6 +323,27 @@ static int x264_mb_predict_mv_direct16x16_temporal( x264_t *h )
         }
     }
 
+    if( h->param.i_threads > 1 )
+    {
+        int di = b8x8 ? 4 : 1;
+        for( i4=0; i4<16; i4+=di )
+        {
+            if( h->mb.cache.mv[0][x264_scan8[i4]][1] > h->mb.mv_max_spel[1]
+             || h->mb.cache.mv[1][x264_scan8[i4]][1] > h->mb.mv_max_spel[1] )
+            {
+#if 0
+                fprintf(stderr, "direct_temporal: (%d,%d) (%d,%d) > %d \n",
+                        h->mb.cache.mv[0][x264_scan8[i4]][0],
+                        h->mb.cache.mv[0][x264_scan8[i4]][1],
+                        h->mb.cache.mv[1][x264_scan8[i4]][0],
+                        h->mb.cache.mv[1][x264_scan8[i4]][1],
+                        h->mb.mv_max_spel[1]);
+#endif
+                return 0;
+            }
+        }
+    }
+
     return 1;
 }
 
@@ -368,6 +404,19 @@ static int x264_mb_predict_mv_direct16x16_spatial( x264_t *h )
 
     if( IS_INTRA( type_col ) )
         return 1;
+
+    if( h->param.i_threads > 1
+        && ( mv[0][1] > h->mb.mv_max_spel[1]
+          || mv[1][1] > h->mb.mv_max_spel[1] ) )
+    {
+#if 0
+        fprintf(stderr, "direct_spatial: (%d,%d) (%d,%d) > %d \n",
+                mv[0][0], mv[0][1], mv[1][0], mv[1][1],
+                h->mb.mv_max_spel[1]);
+#endif
+        return 0;
+    }
+
     b8x8 = h->sps->b_direct8x8_inference ||
            (type_col != P_8x8 && type_col != B_SKIP && type_col != B_DIRECT && type_col != B_8x8);
 
@@ -453,7 +502,7 @@ int x264_mb_predict_mv_direct16x16( x264_t *h, int *b_changed )
         for( l = 0; l < 2; l++ )
             for( i = 0; i < 4; i++ )
                 h->mb.cache.direct_ref[l][i] = h->mb.cache.ref[l][x264_scan8[i*4]];
-        memcpy(h->mb.cache.direct_mv, h->mb.cache.mv, sizeof(h->mb.cache.mv));
+        h->mc.memcpy_aligned(h->mb.cache.direct_mv, h->mb.cache.mv, sizeof(h->mb.cache.mv));
     }
 
     return b_available;
@@ -475,6 +524,8 @@ void x264_mb_load_mv_direct8x8( x264_t *h, int idx )
     }
 }
 
+#define FIXED_SCALE 256
+
 /* This just improves encoder performance, it's not part of the spec */
 void x264_mb_predict_mv_ref16x16( x264_t *h, int i_list, int i_ref, int mvc[8][2], int *i_mvc )
 {
@@ -504,7 +555,7 @@ void x264_mb_predict_mv_ref16x16( x264_t *h, int i_list, int i_ref, int mvc[8][2
     }
     if( h->mb.i_neighbour & MB_TOP )
     {
-        int i_mb_t = h->mb.i_mb_xy - h->mb.i_mb_stride;
+        int i_mb_t = h->mb.i_mb_top_xy;
         if( !IS_SKIP( h->mb.type[i_mb_t] ) )
             SET_MVP( mvr[i_mb_t] );
 
@@ -516,26 +567,21 @@ void x264_mb_predict_mv_ref16x16( x264_t *h, int i_list, int i_ref, int mvc[8][2
 #undef SET_MVP
 
     /* temporal predictors */
-    if( h->fref0[0]->i_ref[0] > 0 )
+    /* FIXME temporal scaling w/ interlace */
+    if( h->fref0[0]->i_ref[0] > 0 && !h->sh.b_mbaff )
     {
         x264_frame_t *l0 = h->fref0[0];
-        int ref_col_cur, ref_col_prev = -1;
-        int scale = 0;
 
 #define SET_TMVP(dx, dy) { \
             int i_b4 = h->mb.i_b4_xy + dx*4 + dy*4*h->mb.i_b4_stride; \
             int i_b8 = h->mb.i_b8_xy + dx*2 + dy*2*h->mb.i_b8_stride; \
-            ref_col_cur = l0->ref[0][i_b8]; \
-            if( ref_col_cur >= 0 ) \
+            int ref_col = l0->ref[0][i_b8]; \
+            if( ref_col >= 0 ) \
             { \
-                /* TODO: calc once per frame and tablize? */\
-                if( ref_col_cur != ref_col_prev ) \
-                    scale = 256 * (h->fenc->i_poc - h->fref0[i_ref]->i_poc) \
-                                / (l0->i_poc - l0->ref_poc[0][ref_col_cur]); \
-                mvc[i][0] = l0->mv[0][i_b4][0] * scale / 256; \
-                mvc[i][1] = l0->mv[0][i_b4][1] * scale / 256; \
+                int scale = (h->fdec->i_poc - h->fdec->ref_poc[0][i_ref]) * l0->inv_ref_poc[ref_col];\
+                mvc[i][0] = l0->mv[0][i_b4][0] * scale / FIXED_SCALE; \
+                mvc[i][1] = l0->mv[0][i_b4][1] * scale / FIXED_SCALE; \
                 i++; \
-                ref_col_prev = ref_col_cur; \
             } \
         }
 
@@ -550,43 +596,61 @@ void x264_mb_predict_mv_ref16x16( x264_t *h, int i_list, int i_ref, int mvc[8][2
     *i_mvc = i;
 }
 
+/* Set up a lookup table for delta pocs to reduce an IDIV to an IMUL */
+static void setup_inverse_delta_pocs( x264_t *h )
+{
+    int i;
+    for( i = 0; i < h->i_ref0; i++ )
+    {
+        int delta = h->fdec->i_poc - h->fref0[i]->i_poc;
+        h->fdec->inv_ref_poc[i] = (FIXED_SCALE + delta/2) / delta;
+    }
+}
+
 static inline void x264_mb_mc_0xywh( x264_t *h, int x, int y, int width, int height )
 {
     const int i8 = x264_scan8[0]+x+8*y;
     const int i_ref = h->mb.cache.ref[0][i8];
     const int mvx   = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
-    const int mvy   = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
+    int       mvy   = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
 
-    h->mc.mc_luma( h->mb.pic.p_fref[0][i_ref], h->mb.pic.i_stride[0],
-                    &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
-                    mvx + 4*4*x, mvy + 4*4*y, 4*width, 4*height );
+    h->mc.mc_luma( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
+                   h->mb.pic.p_fref[0][i_ref], h->mb.pic.i_stride[0],
+                   mvx + 4*4*x, mvy + 4*4*y, 4*width, 4*height );
 
-    h->mc.mc_chroma( &h->mb.pic.p_fref[0][i_ref][4][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
-                      &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
-                      mvx, mvy, 2*width, 2*height );
+    // chroma is offset if MCing from a field of opposite parity
+    if( h->mb.b_interlaced & i_ref )
+        mvy += (h->mb.i_mb_y & 1)*4 - 2;
 
-    h->mc.mc_chroma( &h->mb.pic.p_fref[0][i_ref][5][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
-                      &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
-                      mvx, mvy, 2*width, 2*height );
+    h->mc.mc_chroma( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
+                     &h->mb.pic.p_fref[0][i_ref][4][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
+                     mvx, mvy, 2*width, 2*height );
+
+    h->mc.mc_chroma( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
+                     &h->mb.pic.p_fref[0][i_ref][5][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
+                     mvx, mvy, 2*width, 2*height );
 }
 static inline void x264_mb_mc_1xywh( x264_t *h, int x, int y, int width, int height )
 {
     const int i8 = x264_scan8[0]+x+8*y;
     const int i_ref = h->mb.cache.ref[1][i8];
     const int mvx   = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
-    const int mvy   = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
+    int       mvy   = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
+
+    h->mc.mc_luma( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
+                   h->mb.pic.p_fref[1][i_ref], h->mb.pic.i_stride[0],
+                   mvx + 4*4*x, mvy + 4*4*y, 4*width, 4*height );
 
-    h->mc.mc_luma( h->mb.pic.p_fref[1][i_ref], h->mb.pic.i_stride[0],
-                    &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
-                    mvx + 4*4*x, mvy + 4*4*y, 4*width, 4*height );
+    if( h->mb.b_interlaced & i_ref )
+        mvy += (h->mb.i_mb_y & 1)*4 - 2;
 
-    h->mc.mc_chroma( &h->mb.pic.p_fref[1][i_ref][4][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
-                      &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
-                      mvx, mvy, 2*width, 2*height );
+    h->mc.mc_chroma( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
+                     &h->mb.pic.p_fref[1][i_ref][4][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
+                     mvx, mvy, 2*width, 2*height );
 
-    h->mc.mc_chroma( &h->mb.pic.p_fref[1][i_ref][5][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
-                      &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
-                      mvx, mvy, 2*width, 2*height );
+    h->mc.mc_chroma( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
+                     &h->mb.pic.p_fref[1][i_ref][5][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
+                     mvx, mvy, 2*width, 2*height );
 }
 
 static inline void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int height )
@@ -595,14 +659,17 @@ static inline void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int he
 
     const int i_ref1 = h->mb.cache.ref[1][i8];
     const int mvx1   = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
-    const int mvy1   = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
+    int       mvy1   = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
     DECLARE_ALIGNED( uint8_t, tmp[16*16], 16 );
     int i_mode = x264_size2pixel[height][width];
 
     x264_mb_mc_0xywh( h, x, y, width, height );
 
-    h->mc.mc_luma( h->mb.pic.p_fref[1][i_ref1], h->mb.pic.i_stride[0],
-                    tmp, 16, mvx1 + 4*4*x, mvy1 + 4*4*y, 4*width, 4*height );
+    h->mc.mc_luma( tmp, 16, h->mb.pic.p_fref[1][i_ref1], h->mb.pic.i_stride[0],
+                   mvx1 + 4*4*x, mvy1 + 4*4*y, 4*width, 4*height );
+
+    if( h->mb.b_interlaced & i_ref1 )
+        mvy1 += (h->mb.i_mb_y & 1)*4 - 2;
 
     if( h->param.analyse.b_weighted_bipred )
     {
@@ -611,24 +678,24 @@ static inline void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int he
 
         h->mc.avg_weight[i_mode]( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE, tmp, 16, weight );
 
-        h->mc.mc_chroma( &h->mb.pic.p_fref[1][i_ref1][4][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
-                          tmp, 16, mvx1, mvy1, 2*width, 2*height );
+        h->mc.mc_chroma( tmp, 16, &h->mb.pic.p_fref[1][i_ref1][4][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
+                         mvx1, mvy1, 2*width, 2*height );
         h->mc.avg_weight[i_mode+3]( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16, weight );
 
-        h->mc.mc_chroma( &h->mb.pic.p_fref[1][i_ref1][5][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
-                          tmp, 16, mvx1, mvy1, 2*width, 2*height );
+        h->mc.mc_chroma( tmp, 16, &h->mb.pic.p_fref[1][i_ref1][5][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
+                         mvx1, mvy1, 2*width, 2*height );
         h->mc.avg_weight[i_mode+3]( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16, weight );
     }
     else
     {
         h->mc.avg[i_mode]( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE, tmp, 16 );
 
-        h->mc.mc_chroma( &h->mb.pic.p_fref[1][i_ref1][4][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
-                          tmp, 16, mvx1, mvy1, 2*width, 2*height );
+        h->mc.mc_chroma( tmp, 16, &h->mb.pic.p_fref[1][i_ref1][4][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
+                         mvx1, mvy1, 2*width, 2*height );
         h->mc.avg[i_mode+3]( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16 );
 
-        h->mc.mc_chroma( &h->mb.pic.p_fref[1][i_ref1][5][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
-                          tmp, 16, mvx1, mvy1, 2*width, 2*height );
+        h->mc.mc_chroma( tmp, 16, &h->mb.pic.p_fref[1][i_ref1][5][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
+                         mvx1, mvy1, 2*width, 2*height );
         h->mc.avg[i_mode+3]( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16 );
     }
 }
@@ -823,6 +890,8 @@ int x264_macroblock_cache_init( x264_t *h )
     h->mb.i_b8_stride = h->sps->i_mb_width * 2;
     h->mb.i_b4_stride = h->sps->i_mb_width * 4;
 
+    h->mb.b_interlaced = h->param.b_interlaced;
+
     CHECKED_MALLOC( h->mb.qp, i_mb_count * sizeof(int8_t) );
     CHECKED_MALLOC( h->mb.cbp, i_mb_count * sizeof(int16_t) );
     CHECKED_MALLOC( h->mb.skipbp, i_mb_count * sizeof(int8_t) );
@@ -833,6 +902,7 @@ int x264_macroblock_cache_init( x264_t *h )
 
     /* all coeffs */
     CHECKED_MALLOC( h->mb.non_zero_count, i_mb_count * 24 * sizeof(uint8_t) );
+    CHECKED_MALLOC( h->mb.nnz_backup, h->sps->i_mb_width * 4 * 16 * sizeof(uint8_t) );
 
     if( h->param.b_cabac )
     {
@@ -843,12 +913,19 @@ int x264_macroblock_cache_init( x264_t *h )
 
     for( i=0; i<2; i++ )
     {
-        int i_refs = (i ? 1 : h->param.i_frame_reference) + h->param.b_bframe_pyramid;
-        for( j=0; j < i_refs && j < 16; j++ )
+        int i_refs = X264_MIN(16, (i ? 1 : h->param.i_frame_reference) + h->param.b_bframe_pyramid) << h->param.b_interlaced;
+        for( j=0; j < i_refs; j++ )
             CHECKED_MALLOC( h->mb.mvr[i][j], 2 * i_mb_count * sizeof(int16_t) );
     }
 
-    /* init with not avaiable (for top right idx=7,15) */
+    for( i=0; i<=h->param.b_interlaced; i++ )
+        for( j=0; j<3; j++ )
+        {
+            CHECKED_MALLOC( h->mb.intra_border_backup[i][j], h->fdec->i_stride[j] );
+            h->mb.intra_border_backup[i][j] += 8;
+        }
+
+    /* init with not available (for top right idx=7,15) */
     memset( h->mb.cache.ref[0], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
     memset( h->mb.cache.ref[1], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
 
@@ -858,12 +935,12 @@ fail: return -1;
 void x264_macroblock_cache_end( x264_t *h )
 {
     int i, j;
+    for( i=0; i<=h->param.b_interlaced; i++ )
+        for( j=0; j<3; j++ )
+            x264_free( h->mb.intra_border_backup[i][j] - 8 );
     for( i=0; i<2; i++ )
-    {
-        int i_refs = i ? 1 + h->param.b_bframe_pyramid : h->param.i_frame_reference;
-        for( j=0; j < i_refs; j++ )
+        for( j=0; j<32; j++ )
             x264_free( h->mb.mvr[i][j] );
-    }
     if( h->param.b_cabac )
     {
         x264_free( h->mb.chroma_pred_mode );
@@ -872,6 +949,7 @@ void x264_macroblock_cache_end( x264_t *h )
     }
     x264_free( h->mb.intra4x4_pred_mode );
     x264_free( h->mb.non_zero_count );
+    x264_free( h->mb.nnz_backup );
     x264_free( h->mb.mb_transform_size );
     x264_free( h->mb.skipbp );
     x264_free( h->mb.cbp );
@@ -912,85 +990,49 @@ void x264_macroblock_slice_init( x264_t *h )
     }
     if( h->sh.i_type == SLICE_TYPE_P )
         memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
+
+    setup_inverse_delta_pocs( h );
 }
 
+void x264_prefetch_fenc( x264_t *h, x264_frame_t *fenc, int i_mb_x, int i_mb_y )
+{
+    int stride_y  = fenc->i_stride[0];
+    int stride_uv = fenc->i_stride[1];
+    int off_y = 16 * (i_mb_x + i_mb_y * stride_y);
+    int off_uv = 8 * (i_mb_x + i_mb_y * stride_uv);
+    h->mc.prefetch_fenc( fenc->plane[0]+off_y, stride_y,
+                         fenc->plane[1+(i_mb_x&1)]+off_uv, stride_uv, i_mb_x );
+}
 
 void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
 {
-    const int i_mb_4x4 = 4*(i_mb_y * h->mb.i_b4_stride + i_mb_x);
-    const int i_mb_8x8 = 2*(i_mb_y * h->mb.i_b8_stride + i_mb_x);
-
     int i_mb_xy = i_mb_y * h->mb.i_mb_stride + i_mb_x;
-    int i_top_xy = i_mb_xy - h->mb.i_mb_stride;
+    int i_mb_4x4 = 4*(i_mb_y * h->mb.i_b4_stride + i_mb_x);
+    int i_mb_8x8 = 2*(i_mb_y * h->mb.i_b8_stride + i_mb_x);
+    int i_top_y = i_mb_y - (1 << h->mb.b_interlaced);
+    int i_top_xy = i_top_y * h->mb.i_mb_stride + i_mb_x;
+    int i_top_4x4 = (4*i_top_y+3) * h->mb.i_b4_stride + 4*i_mb_x;
+    int i_top_8x8 = (2*i_top_y+1) * h->mb.i_b8_stride + 2*i_mb_x;
     int i_left_xy = -1;
     int i_top_type = -1;    /* gcc warn */
     int i_left_type= -1;
 
     int i;
 
+    assert( h->mb.i_b8_stride == 2*h->mb.i_mb_stride );
+    assert( h->mb.i_b4_stride == 4*h->mb.i_mb_stride );
+
     /* init index */
     h->mb.i_mb_x = i_mb_x;
     h->mb.i_mb_y = i_mb_y;
     h->mb.i_mb_xy = i_mb_xy;
     h->mb.i_b8_xy = i_mb_8x8;
     h->mb.i_b4_xy = i_mb_4x4;
+    h->mb.i_mb_top_xy = i_top_xy;
     h->mb.i_neighbour = 0;
 
-    /* fdec:      fenc:
-     * yyyyyyy
-     * yYYYY      YYYY
-     * yYYYY      YYYY
-     * yYYYY      YYYY
-     * yYYYY      YYYY
-     * uuu vvv    UUVV
-     * uUU vVV    UUVV
-     * uUU vVV
-     */
-    h->mb.pic.p_fenc[0] = h->mb.pic.fenc_buf;
-    h->mb.pic.p_fenc[1] = h->mb.pic.fenc_buf + 16*FENC_STRIDE;
-    h->mb.pic.p_fenc[2] = h->mb.pic.fenc_buf + 16*FENC_STRIDE + 8;
-    h->mb.pic.p_fdec[0] = h->mb.pic.fdec_buf + 2*FDEC_STRIDE;
-    h->mb.pic.p_fdec[1] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE;
-    h->mb.pic.p_fdec[2] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE + 16;
-
-    /* load picture pointers */
-    for( i = 0; i < 3; i++ )
-    {
-        const int w = (i == 0 ? 16 : 8);
-        const int i_stride = h->fdec->i_stride[i];
-        const uint8_t *plane_fdec = &h->fdec->plane[i][ w * (i_mb_x + i_mb_y * i_stride) ];
-        int   j;
-
-        h->mb.pic.i_stride[i] = i_stride;
-
-        h->mc.copy[i?PIXEL_8x8:PIXEL_16x16]( h->mb.pic.p_fenc[i], FENC_STRIDE,
-            &h->fenc->plane[i][ w * (i_mb_x + i_mb_y * i_stride) ], i_stride, w );
-        memcpy( &h->mb.pic.p_fdec[i][-1-FDEC_STRIDE], &plane_fdec[-1-i_stride], w*3/2+1 );
-        for( j = 0; j < w; j++ )
-            h->mb.pic.p_fdec[i][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride];
-
-        for( j = 0; j < h->i_ref0; j++ )
-        {
-            h->mb.pic.p_fref[0][j][i==0 ? 0:i+3] = &h->fref0[j]->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
-            h->mb.pic.p_fref[0][j][i+1] = &h->fref0[j]->filtered[i+1][ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
-        }
-        for( j = 0; j < h->i_ref1; j++ )
-        {
-            h->mb.pic.p_fref[1][j][i==0 ? 0:i+3] = &h->fref1[j]->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
-            h->mb.pic.p_fref[1][j][i+1] = &h->fref1[j]->filtered[i+1][ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
-        }
-    }
-
-    if( h->fdec->integral )
-    {
-        for( i = 0; i < h->i_ref0; i++ )
-            h->mb.pic.p_integral[0][i] = &h->fref0[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
-        for( i = 0; i < h->i_ref1; i++ )
-            h->mb.pic.p_integral[1][i] = &h->fref1[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
-    }
-
     /* load cache */
-    if( i_mb_xy >= h->sh.i_first_mb + h->mb.i_mb_stride )
+    if( i_top_xy >= h->sh.i_first_mb )
     {
         h->mb.i_mb_type_top =
         i_top_type= h->mb.type[i_top_xy];
@@ -1098,13 +1140,88 @@ void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
     else
         h->mb.i_mb_type_topleft = -1;
 
-    if( h->param.analyse.b_transform_8x8 )
+    if( h->pps->b_transform_8x8_mode )
     {
         h->mb.cache.i_neighbour_transform_size =
             ( i_left_type >= 0 && h->mb.mb_transform_size[i_left_xy] )
           + ( i_top_type  >= 0 && h->mb.mb_transform_size[i_top_xy]  );
     }
 
+    if( h->sh.b_mbaff )
+    {
+        h->mb.pic.i_fref[0] = h->i_ref0 << h->mb.b_interlaced;
+        h->mb.pic.i_fref[1] = h->i_ref1 << h->mb.b_interlaced;
+        h->mb.cache.i_neighbour_interlaced =
+            !!(h->mb.i_neighbour & MB_LEFT)
+          + !!(h->mb.i_neighbour & MB_TOP);
+    }
+
+    /* fdec:      fenc:
+     * yyyyyyy
+     * yYYYY      YYYY
+     * yYYYY      YYYY
+     * yYYYY      YYYY
+     * yYYYY      YYYY
+     * uuu vvv    UUVV
+     * uUU vVV    UUVV
+     * uUU vVV
+     */
+    h->mb.pic.p_fenc[0] = h->mb.pic.fenc_buf;
+    h->mb.pic.p_fenc[1] = h->mb.pic.fenc_buf + 16*FENC_STRIDE;
+    h->mb.pic.p_fenc[2] = h->mb.pic.fenc_buf + 16*FENC_STRIDE + 8;
+    h->mb.pic.p_fdec[0] = h->mb.pic.fdec_buf + 2*FDEC_STRIDE;
+    h->mb.pic.p_fdec[1] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE;
+    h->mb.pic.p_fdec[2] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE + 16;
+
+    /* load picture pointers */
+    for( i = 0; i < 3; i++ )
+    {
+        const int w = (i == 0 ? 16 : 8);
+        const int i_stride = h->fdec->i_stride[i];
+        const int i_stride2 = i_stride << h->mb.b_interlaced;
+        const int i_pix_offset = h->mb.b_interlaced
+                               ? w * (i_mb_x + (i_mb_y&~1) * i_stride) + (i_mb_y&1) * i_stride
+                               : w * (i_mb_x + i_mb_y * i_stride);
+        int ref_pix_offset[2] = { i_pix_offset, i_pix_offset };
+        const uint8_t *plane_fdec = &h->fdec->plane[i][i_pix_offset];
+        const uint8_t *intra_fdec = &h->mb.intra_border_backup[i_mb_y & h->sh.b_mbaff][i][i_mb_x*16>>!!i];
+        x264_frame_t **fref[2] = { h->fref0, h->fref1 };
+        int j, k, l;
+
+        if( h->mb.b_interlaced )
+            ref_pix_offset[1] += (1-2*(i_mb_y&1)) * i_stride;
+
+        h->mb.pic.i_stride[i] = i_stride2;
+
+        h->mc.copy[i?PIXEL_8x8:PIXEL_16x16]( h->mb.pic.p_fenc[i], FENC_STRIDE,
+            &h->fenc->plane[i][i_pix_offset], i_stride2, w );
+        memcpy( &h->mb.pic.p_fdec[i][-1-FDEC_STRIDE], intra_fdec-1, w*3/2+1 );
+        for( j = 0; j < w; j++ )
+            h->mb.pic.p_fdec[i][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride2];
+
+        for( l=0; l<2; l++ )
+        {
+            for( j=0; j<h->mb.pic.i_fref[l]; j++ )
+            {
+                h->mb.pic.p_fref[l][j][i==0 ? 0:i+3] = &fref[l][j >> h->mb.b_interlaced]->plane[i][ref_pix_offset[j&1]];
+                if( i == 0 )
+                    for( k = 1; k < 4; k++ )
+                        h->mb.pic.p_fref[l][j][k] = &fref[l][j >> h->mb.b_interlaced]->filtered[k][ref_pix_offset[j&1]];
+            }
+        }
+    }
+
+    if( h->fdec->integral )
+    {
+        assert( !h->mb.b_interlaced );
+        for( i = 0; i < h->mb.pic.i_fref[0]; i++ )
+            h->mb.pic.p_integral[0][i] = &h->fref0[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
+        for( i = 0; i < h->mb.pic.i_fref[1]; i++ )
+            h->mb.pic.p_integral[1][i] = &h->fref1[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
+    }
+
+    x264_prefetch_fenc( h, h->fenc, i_mb_x, i_mb_y );
+
     /* load ref/mv/mvd */
     if( h->sh.i_type != SLICE_TYPE_I )
     {
@@ -1124,8 +1241,8 @@ void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
             if( h->mb.i_neighbour & MB_TOPLEFT )
             {
                 const int i8 = x264_scan8[0] - 1 - 1*8;
-                const int ir = i_mb_8x8 - s8x8 - 1;
-                const int iv = i_mb_4x4 - s4x4 - 1;
+                const int ir = i_top_8x8 - 1;
+                const int iv = i_top_4x4 - 1;
                 h->mb.cache.ref[i_list][i8]  = h->mb.ref[i_list][ir];
                 h->mb.cache.mv[i_list][i8][0] = h->mb.mv[i_list][iv][0];
                 h->mb.cache.mv[i_list][i8][1] = h->mb.mv[i_list][iv][1];
@@ -1141,9 +1258,8 @@ void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
             if( h->mb.i_neighbour & MB_TOP )
             {
                 const int i8 = x264_scan8[0] - 8;
-                const int ir = i_mb_8x8 - s8x8;
-                const int iv = i_mb_4x4 - s4x4;
-
+                const int ir = i_top_8x8;
+                const int iv = i_top_4x4;
                 h->mb.cache.ref[i_list][i8+0] =
                 h->mb.cache.ref[i_list][i8+1] = h->mb.ref[i_list][ir + 0];
                 h->mb.cache.ref[i_list][i8+2] =
@@ -1169,9 +1285,8 @@ void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
             if( h->mb.i_neighbour & MB_TOPRIGHT )
             {
                 const int i8 = x264_scan8[0] + 4 - 1*8;
-                const int ir = i_mb_8x8 - s8x8 + 2;
-                const int iv = i_mb_4x4 - s4x4 + 4;
-
+                const int ir = i_top_8x8 + 2;
+                const int iv = i_top_4x4 + 4;
                 h->mb.cache.ref[i_list][i8]  = h->mb.ref[i_list][ir];
                 h->mb.cache.mv[i_list][i8][0] = h->mb.mv[i_list][iv][0];
                 h->mb.cache.mv[i_list][i8][1] = h->mb.mv[i_list][iv][1];
@@ -1189,7 +1304,6 @@ void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
                 const int i8 = x264_scan8[0] - 1;
                 const int ir = i_mb_8x8 - 1;
                 const int iv = i_mb_4x4 - 1;
-
                 h->mb.cache.ref[i_list][i8+0*8] =
                 h->mb.cache.ref[i_list][i8+1*8] = h->mb.ref[i_list][ir + 0*s8x8];
                 h->mb.cache.ref[i_list][i8+2*8] =
@@ -1217,7 +1331,7 @@ void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
                 if( i_top_type >= 0 )
                 {
                     const int i8 = x264_scan8[0] - 8;
-                    const int iv = i_mb_4x4 - s4x4;
+                    const int iv = i_top_4x4;
                     for( i = 0; i < 4; i++ )
                     {
                         h->mb.cache.mvd[i_list][i8+i][0] = h->mb.mvd[i_list][iv + i][0];
@@ -1271,6 +1385,9 @@ void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
                 h->mb.cache.skip[x264_scan8[4] - 8] = h->mb.skipbp[i_top_xy] & 0x8;
             }
         }
+
+        if( h->sh.i_type == SLICE_TYPE_P )
+            x264_mb_predict_mv_pskip( h, h->mb.cache.pskip_mv );
     }
 
     h->mb.i_neighbour4[0] =
@@ -1311,12 +1428,18 @@ void x264_macroblock_cache_save( x264_t *h )
     for( i = 0; i < 3; i++ )
     {
         int w = i ? 8 : 16;
+        int i_stride = h->fdec->i_stride[i];
+        int i_stride2 = i_stride << h->mb.b_interlaced;
+        int i_pix_offset = h->mb.b_interlaced
+                         ? w * (h->mb.i_mb_x + (h->mb.i_mb_y&~1) * i_stride) + (h->mb.i_mb_y&1) * i_stride
+                         : w * (h->mb.i_mb_x + h->mb.i_mb_y * i_stride);
         h->mc.copy[i?PIXEL_8x8:PIXEL_16x16](
-            &h->fdec->plane[i][ w * (h->mb.i_mb_x + h->mb.i_mb_y * h->fdec->i_stride[i]) ],
-            h->fdec->i_stride[i],
+            &h->fdec->plane[i][i_pix_offset], i_stride2,
             h->mb.pic.p_fdec[i], FDEC_STRIDE, w );
     }
 
+    x264_prefetch_fenc( h, h->fdec, h->mb.i_mb_x, h->mb.i_mb_y );
+
     h->mb.type[i_mb_xy] = i_mb_type;
 
     if( h->mb.i_type != I_16x16 && h->mb.i_cbp_luma == 0 && h->mb.i_cbp_chroma == 0 )
@@ -1325,6 +1448,7 @@ void x264_macroblock_cache_save( x264_t *h )
 
     h->mb.i_last_dqp = h->mb.i_qp - h->mb.i_last_qp;
     h->mb.i_last_qp = h->mb.i_qp;
+    h->mb.i_mb_prev_xy = h->mb.i_mb_xy;
 
     /* save intra4x4 */
     if( i_mb_type == I_4x4 )
@@ -1502,4 +1626,10 @@ void x264_macroblock_bipred_init( x264_t *h )
                 h->mb.bipred_weight[i_ref0][i_ref1] = 32;
         }
     }
+    if( h->sh.b_mbaff )
+    {
+        for( i_ref0 = 2*h->i_ref0-1; i_ref0 >= 0; i_ref0-- )
+            for( i_ref1 = 2*h->i_ref1-1; i_ref1 >= 0; i_ref1-- )
+                h->mb.bipred_weight[i_ref0][i_ref1] = h->mb.bipred_weight[i_ref0>>1][i_ref1>>1];
+    }
 }