Sliced-threads: do hpel and deblock after returning
[x262.git] / common / macroblock.c
index 0d4f155..11c3e75 100644 (file)
@@ -1,11 +1,12 @@
 /*****************************************************************************
- * macroblock.c: h264 encoder library
+ * macroblock.c: macroblock common functions
  *****************************************************************************
- * Copyright (C) 2003-2008 x264 project
+ * Copyright (C) 2003-2012 x264 project
  *
- * Authors: Laurent Aimar <fenrir@via.ecp.fr>
+ * Authors: Jason Garrett-Glaser <darkshikari@gmail.com>
+ *          Laurent Aimar <fenrir@via.ecp.fr>
  *          Loren Merritt <lorenm@u.washington.edu>
- *          Jason Garrett-Glaser <darkshikari@gmail.com>
+ *          Henrik Gramner <hengar-6@student.ltu.se>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
+ *
+ * This program is also available under a commercial proprietary license.
+ * For more information, contact us at licensing@x264.com.
  *****************************************************************************/
 
 #include "common.h"
 #include "encoder/me.h"
 
-void x264_mb_predict_mv( x264_t *h, int i_list, int idx, int i_width, int16_t mvp[2] )
-{
-    const int i8 = x264_scan8[idx];
-    const int i_ref= h->mb.cache.ref[i_list][i8];
-    int     i_refa = h->mb.cache.ref[i_list][i8 - 1];
-    int16_t *mv_a  = h->mb.cache.mv[i_list][i8 - 1];
-    int     i_refb = h->mb.cache.ref[i_list][i8 - 8];
-    int16_t *mv_b  = h->mb.cache.mv[i_list][i8 - 8];
-    int     i_refc = h->mb.cache.ref[i_list][i8 - 8 + i_width];
-    int16_t *mv_c  = h->mb.cache.mv[i_list][i8 - 8 + i_width];
-
-    if( (idx&3) >= 2 + (i_width&1) || i_refc == -2 )
-    {
-        i_refc = h->mb.cache.ref[i_list][i8 - 8 - 1];
-        mv_c   = h->mb.cache.mv[i_list][i8 - 8 - 1];
-    }
-
-    if( h->mb.i_partition == D_16x8 )
-    {
-        if( idx == 0 )
-        {
-            if( i_refb == i_ref )
-            {
-                CP32( mvp, mv_b );
-                return;
-            }
-        }
-        else
-        {
-            if( i_refa == i_ref )
-            {
-                CP32( mvp, mv_a );
-                return;
-            }
-        }
-    }
-    else if( h->mb.i_partition == D_8x16 )
-    {
-        if( idx == 0 )
-        {
-            if( i_refa == i_ref )
-            {
-                CP32( mvp, mv_a );
-                return;
-            }
-        }
-        else
-        {
-            if( i_refc == i_ref )
-            {
-                CP32( mvp, mv_c );
-                return;
-            }
-        }
-    }
-
-    int i_count = (i_refa == i_ref) + (i_refb == i_ref) + (i_refc == i_ref);
-
-    if( i_count > 1 )
-    {
-median:
-        x264_median_mv( mvp, mv_a, mv_b, mv_c );
-    }
-    else if( i_count == 1 )
-    {
-        if( i_refa == i_ref )
-            CP32( mvp, mv_a );
-        else if( i_refb == i_ref )
-            CP32( mvp, mv_b );
-        else
-            CP32( mvp, mv_c );
-    }
-    else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
-        CP32( mvp, mv_a );
-    else
-        goto median;
-}
+#define MC_LUMA(list,p) \
+    h->mc.mc_luma( &h->mb.pic.p_fdec[p][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE, \
+                   &h->mb.pic.p_fref[list][i_ref][p*4], h->mb.pic.i_stride[p], \
+                   mvx, mvy, 4*width, 4*height, \
+                   list ? x264_weight_none : &h->sh.weight[i_ref][p] );
 
-void x264_mb_predict_mv_16x16( x264_t *h, int i_list, int i_ref, int16_t mvp[2] )
+static NOINLINE void x264_mb_mc_0xywh( x264_t *h, int x, int y, int width, int height )
 {
-    int     i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
-    int16_t *mv_a  = h->mb.cache.mv[i_list][X264_SCAN8_0 - 1];
-    int     i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
-    int16_t *mv_b  = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8];
-    int     i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
-    int16_t *mv_c  = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 + 4];
-    if( i_refc == -2 )
-    {
-        i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
-        mv_c   = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 - 1];
-    }
-
-    int i_count = (i_refa == i_ref) + (i_refb == i_ref) + (i_refc == i_ref);
-
-    if( i_count > 1 )
-    {
-median:
-        x264_median_mv( mvp, mv_a, mv_b, mv_c );
-    }
-    else if( i_count == 1 )
-    {
-        if( i_refa == i_ref )
-            CP32( mvp, mv_a );
-        else if( i_refb == i_ref )
-            CP32( mvp, mv_b );
-        else
-            CP32( mvp, mv_c );
-    }
-    else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
-        CP32( mvp, mv_a );
-    else
-        goto median;
-}
+    int i8    = x264_scan8[0]+x+8*y;
+    int i_ref = h->mb.cache.ref[0][i8];
+    int mvx   = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
+    int mvy   = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
 
+    MC_LUMA( 0, 0 );
 
-void x264_mb_predict_mv_pskip( x264_t *h, int16_t mv[2] )
-{
-    int     i_refa = h->mb.cache.ref[0][X264_SCAN8_0 - 1];
-    int     i_refb = h->mb.cache.ref[0][X264_SCAN8_0 - 8];
-    int16_t *mv_a  = h->mb.cache.mv[0][X264_SCAN8_0 - 1];
-    int16_t *mv_b  = h->mb.cache.mv[0][X264_SCAN8_0 - 8];
-
-    if( i_refa == -2 || i_refb == -2 ||
-        !( i_refa | M32( mv_a ) ) ||
-        !( i_refb | M32( mv_b ) ) )
+    if( CHROMA444 )
     {
-        M32( mv ) = 0;
+        MC_LUMA( 0, 1 );
+        MC_LUMA( 0, 2 );
     }
     else
     {
-        x264_mb_predict_mv_16x16( h, 0, 0, mv );
-    }
-}
-
-static int x264_mb_predict_mv_direct16x16_temporal( x264_t *h )
-{
-    int i_mb_4x4 = 16 * h->mb.i_mb_stride * h->mb.i_mb_y + 4 * h->mb.i_mb_x;
-    int i_mb_8x8 =  4 * h->mb.i_mb_stride * h->mb.i_mb_y + 2 * h->mb.i_mb_x;
-    int i8;
-    const int type_col = h->fref1[0]->mb_type[h->mb.i_mb_xy];
-    const int partition_col = h->fref1[0]->mb_partition[h->mb.i_mb_xy];
-
-    x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, 0 );
-
-    h->mb.i_partition = partition_col;
-
-    if( IS_INTRA( type_col ) )
-    {
-        x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
-        x264_macroblock_cache_mv(  h, 0, 0, 4, 4, 0, 0 );
-        x264_macroblock_cache_mv(  h, 0, 0, 4, 4, 1, 0 );
-        return 1;
-    }
-
-    /* Don't do any checks other than the ones we have to, based
-     * on the size of the colocated partitions.
-     * Depends on the enum order: D_8x8, D_16x8, D_8x16, D_16x16 */
-    int max_i8 = (D_16x16 - partition_col) + 1;
-    int step = (partition_col == D_16x8) + 1;
-    int width = 4 >> ((D_16x16 - partition_col)&1);
-    int height = 4 >> ((D_16x16 - partition_col)>>1);
-
-    for( i8 = 0; i8 < max_i8; i8 += step )
-    {
-        const int x8 = i8%2;
-        const int y8 = i8/2;
-        const int i_part_8x8 = i_mb_8x8 + x8 + y8 * h->mb.i_b8_stride;
-        const int i_ref1_ref = h->fref1[0]->ref[0][i_part_8x8];
-        const int i_ref = (map_col_to_list0(i_ref1_ref>>h->sh.b_mbaff) << h->sh.b_mbaff) + (i_ref1_ref&h->sh.b_mbaff);
-
-        if( i_ref >= 0 )
-        {
-            const int dist_scale_factor = h->mb.dist_scale_factor[i_ref][0];
-            const int16_t *mv_col = h->fref1[0]->mv[0][i_mb_4x4 + 3*x8 + 3*y8 * h->mb.i_b4_stride];
-            const int l0x = ( dist_scale_factor * mv_col[0] + 128 ) >> 8;
-            const int l0y = ( dist_scale_factor * mv_col[1] + 128 ) >> 8;
-            if( h->param.i_threads > 1 && (l0y > h->mb.mv_max_spel[1] || l0y-mv_col[1] > h->mb.mv_max_spel[1]) )
-                return 0;
-            x264_macroblock_cache_ref( h, 2*x8, 2*y8, width, height, 0, i_ref );
-            x264_macroblock_cache_mv( h, 2*x8, 2*y8, width, height, 0, pack16to32_mask(l0x, l0y) );
-            x264_macroblock_cache_mv( h, 2*x8, 2*y8, width, height, 1, pack16to32_mask(l0x-mv_col[0], l0y-mv_col[1]) );
-        }
-        else
-        {
-            /* 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
-             *   l1mv1 like in spatial mode */
-            return 0;
-        }
+        int v_shift = CHROMA_V_SHIFT;
+        // Chroma in 4:2:0 is offset if MCing from a field of opposite parity
+        if( v_shift & MB_INTERLACED & i_ref )
+            mvy += (h->mb.i_mb_y & 1)*4 - 2;
+
+        int offset = (4*FDEC_STRIDE>>v_shift)*y + 2*x;
+        height = 4*height >> v_shift;
+
+        h->mc.mc_chroma( &h->mb.pic.p_fdec[1][offset],
+                         &h->mb.pic.p_fdec[2][offset], FDEC_STRIDE,
+                         h->mb.pic.p_fref[0][i_ref][4], h->mb.pic.i_stride[1],
+                         mvx, 2*mvy>>v_shift, 2*width, height );
+
+        if( h->sh.weight[i_ref][1].weightfn )
+            h->sh.weight[i_ref][1].weightfn[width>>1]( &h->mb.pic.p_fdec[1][offset], FDEC_STRIDE,
+                                                       &h->mb.pic.p_fdec[1][offset], FDEC_STRIDE,
+                                                       &h->sh.weight[i_ref][1], height );
+        if( h->sh.weight[i_ref][2].weightfn )
+            h->sh.weight[i_ref][2].weightfn[width>>1]( &h->mb.pic.p_fdec[2][offset], FDEC_STRIDE,
+                                                       &h->mb.pic.p_fdec[2][offset], FDEC_STRIDE,
+                                                       &h->sh.weight[i_ref][2], height );
     }
-
-    return 1;
 }
-
-static int x264_mb_predict_mv_direct16x16_spatial( x264_t *h )
+static NOINLINE void x264_mb_mc_1xywh( x264_t *h, int x, int y, int width, int height )
 {
-    int8_t ref[2];
-    ALIGNED_ARRAY_8( int16_t, mv,[2],[2] );
-    int i_list, i8, i_ref;
-    const int8_t *l1ref0 = &h->fref1[0]->ref[0][h->mb.i_b8_xy];
-    const int8_t *l1ref1 = &h->fref1[0]->ref[1][h->mb.i_b8_xy];
-    const int16_t (*l1mv[2])[2] = { (const int16_t (*)[2]) &h->fref1[0]->mv[0][h->mb.i_b4_xy],
-                                    (const int16_t (*)[2]) &h->fref1[0]->mv[1][h->mb.i_b4_xy] };
-    const int type_col = h->fref1[0]->mb_type[h->mb.i_mb_xy];
-    const int partition_col = h->fref1[0]->mb_partition[h->mb.i_mb_xy];
-
-    h->mb.i_partition = partition_col;
-
-    for( i_list = 0; i_list < 2; i_list++ )
-    {
-        int     i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
-        int16_t *mv_a  = h->mb.cache.mv[i_list][X264_SCAN8_0 - 1];
-        int     i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
-        int16_t *mv_b  = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8];
-        int     i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
-        int16_t *mv_c  = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 + 4];
-        if( i_refc == -2 )
-        {
-            i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
-            mv_c   = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 - 1];
-        }
-
-        i_ref = X264_MIN3( (unsigned)i_refa, (unsigned)i_refb, (unsigned)i_refc );
-        if( i_ref < 0 )
-        {
-            i_ref = -1;
-            M32( mv[i_list] ) = 0;
-        }
-        else
-        {
-            /* Same as x264_mb_predict_mv_16x16, but simplified to eliminate cases
-             * not relevant to spatial direct. */
-            int i_count = (i_refa == i_ref) + (i_refb == i_ref) + (i_refc == i_ref);
-
-            if( i_count > 1 )
-                x264_median_mv( mv[i_list], mv_a, mv_b, mv_c );
-            else
-            {
-                if( i_refa == i_ref )
-                    CP32( mv[i_list], mv_a );
-                else if( i_refb == i_ref )
-                    CP32( mv[i_list], mv_b );
-                else
-                    CP32( mv[i_list], mv_c );
-            }
-        }
-
-        x264_macroblock_cache_ref( h, 0, 0, 4, 4, i_list, i_ref );
-        x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, i_list, mv[i_list] );
-        ref[i_list] = i_ref;
-    }
-
-    if( (M16( ref ) & 0x8080) == 0x8080 ) /* if( ref[0] < 0 && ref[1] < 0 ) */
-    {
-        x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
-        x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, 0 );
-        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;
-    }
-
-    if( !M64( mv ) || IS_INTRA( type_col ) || (ref[0]&&ref[1]) )
-        return 1;
+    int i8    = x264_scan8[0]+x+8*y;
+    int i_ref = h->mb.cache.ref[1][i8];
+    int mvx   = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
+    int mvy   = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
 
-    /* Don't do any checks other than the ones we have to, based
-     * on the size of the colocated partitions.
-     * Depends on the enum order: D_8x8, D_16x8, D_8x16, D_16x16 */
-    int max_i8 = (D_16x16 - partition_col) + 1;
-    int step = (partition_col == D_16x8) + 1;
-    int width = 4 >> ((D_16x16 - partition_col)&1);
-    int height = 4 >> ((D_16x16 - partition_col)>>1);
+    MC_LUMA( 1, 0 );
 
-    /* col_zero_flag */
-    for( i8 = 0; i8 < max_i8; i8 += step )
+    if( CHROMA444 )
     {
-        const int x8 = i8&1;
-        const int y8 = i8>>1;
-        const int o8 = x8 + y8 * h->mb.i_b8_stride;
-        const int o4 = 3*(x8 + y8 * h->mb.i_b4_stride);
-        int idx;
-        if( l1ref0[o8] == 0 )
-            idx = 0;
-        else if( l1ref0[o8] < 0 && l1ref1[o8] == 0 )
-            idx = 1;
-        else
-            continue;
-
-        if( abs( l1mv[idx][o4][0] ) <= 1 && abs( l1mv[idx][o4][1] ) <= 1 )
-        {
-            if( ref[0] == 0 ) x264_macroblock_cache_mv( h, 2*x8, 2*y8, width, height, 0, 0 );
-            if( ref[1] == 0 ) x264_macroblock_cache_mv( h, 2*x8, 2*y8, width, height, 1, 0 );
-        }
+        MC_LUMA( 1, 1 );
+        MC_LUMA( 1, 2 );
     }
-
-    return 1;
-}
-
-int x264_mb_predict_mv_direct16x16( x264_t *h, int *b_changed )
-{
-    int b_available;
-    if( h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_NONE )
-        return 0;
-    else if( h->sh.b_direct_spatial_mv_pred )
-        b_available = x264_mb_predict_mv_direct16x16_spatial( h );
     else
-        b_available = x264_mb_predict_mv_direct16x16_temporal( h );
-
-    if( b_changed != NULL && b_available )
-    {
-        int changed;
-
-        changed  = M32( h->mb.cache.direct_mv[0][0] ) ^ M32( h->mb.cache.mv[0][x264_scan8[0]] );
-        changed |= M32( h->mb.cache.direct_mv[1][0] ) ^ M32( h->mb.cache.mv[1][x264_scan8[0]] );
-        changed |= h->mb.cache.direct_ref[0][0] ^ h->mb.cache.ref[0][x264_scan8[0]];
-        changed |= h->mb.cache.direct_ref[1][0] ^ h->mb.cache.ref[1][x264_scan8[0]];
-        if( !changed && h->mb.i_partition != D_16x16 )
-        {
-            changed |= M32( h->mb.cache.direct_mv[0][3] ) ^ M32( h->mb.cache.mv[0][x264_scan8[12]] );
-            changed |= M32( h->mb.cache.direct_mv[1][3] ) ^ M32( h->mb.cache.mv[1][x264_scan8[12]] );
-            changed |= h->mb.cache.direct_ref[0][3] ^ h->mb.cache.ref[0][x264_scan8[12]];
-            changed |= h->mb.cache.direct_ref[1][3] ^ h->mb.cache.ref[1][x264_scan8[12]];
-        }
-        if( !changed && h->mb.i_partition == D_8x8 )
-        {
-            changed |= M32( h->mb.cache.direct_mv[0][1] ) ^ M32( h->mb.cache.mv[0][x264_scan8[4]] );
-            changed |= M32( h->mb.cache.direct_mv[1][1] ) ^ M32( h->mb.cache.mv[1][x264_scan8[4]] );
-            changed |= M32( h->mb.cache.direct_mv[0][2] ) ^ M32( h->mb.cache.mv[0][x264_scan8[8]] );
-            changed |= M32( h->mb.cache.direct_mv[1][2] ) ^ M32( h->mb.cache.mv[1][x264_scan8[8]] );
-            changed |= h->mb.cache.direct_ref[0][1] ^ h->mb.cache.ref[0][x264_scan8[4]];
-            changed |= h->mb.cache.direct_ref[1][1] ^ h->mb.cache.ref[1][x264_scan8[4]];
-            changed |= h->mb.cache.direct_ref[0][2] ^ h->mb.cache.ref[0][x264_scan8[8]];
-            changed |= h->mb.cache.direct_ref[1][2] ^ h->mb.cache.ref[1][x264_scan8[8]];
-        }
-        *b_changed = changed;
-        if( !changed )
-            return b_available;
-    }
-
-    /* cache ref & mv */
-    if( b_available )
     {
-        int l;
-        for( l = 0; l < 2; l++ )
-        {
-            CP32( h->mb.cache.direct_mv[l][0], h->mb.cache.mv[l][x264_scan8[ 0]] );
-            CP32( h->mb.cache.direct_mv[l][1], h->mb.cache.mv[l][x264_scan8[ 4]] );
-            CP32( h->mb.cache.direct_mv[l][2], h->mb.cache.mv[l][x264_scan8[ 8]] );
-            CP32( h->mb.cache.direct_mv[l][3], h->mb.cache.mv[l][x264_scan8[12]] );
-            h->mb.cache.direct_ref[l][0] = h->mb.cache.ref[l][x264_scan8[ 0]];
-            h->mb.cache.direct_ref[l][1] = h->mb.cache.ref[l][x264_scan8[ 4]];
-            h->mb.cache.direct_ref[l][2] = h->mb.cache.ref[l][x264_scan8[ 8]];
-            h->mb.cache.direct_ref[l][3] = h->mb.cache.ref[l][x264_scan8[12]];
-            h->mb.cache.direct_partition = h->mb.i_partition;
-        }
+        int v_shift = CHROMA_V_SHIFT;
+        if( v_shift & MB_INTERLACED & i_ref )
+            mvy += (h->mb.i_mb_y & 1)*4 - 2;
+
+        int offset = (4*FDEC_STRIDE>>v_shift)*y + 2*x;
+        h->mc.mc_chroma( &h->mb.pic.p_fdec[1][offset],
+                         &h->mb.pic.p_fdec[2][offset], FDEC_STRIDE,
+                         h->mb.pic.p_fref[1][i_ref][4], h->mb.pic.i_stride[1],
+                         mvx, 2*mvy>>v_shift, 2*width, 4*height>>v_shift );
     }
-
-    return b_available;
 }
 
-/* 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, int16_t mvc[9][2], int *i_mvc )
-{
-    int16_t (*mvr)[2] = h->mb.mvr[i_list][i_ref];
-    int i = 0;
-
-#define SET_MVP(mvp) { \
-        CP32( mvc[i], mvp ); \
-        i++; \
-    }
-
-    /* b_direct */
-    if( h->sh.i_type == SLICE_TYPE_B
-        && h->mb.cache.ref[i_list][x264_scan8[12]] == i_ref )
-    {
-        SET_MVP( h->mb.cache.mv[i_list][x264_scan8[12]] );
-    }
-
-    if( i_ref == 0 && h->frames.b_have_lowres )
-    {
-        int16_t (*lowres_mv)[2] = i_list ? h->fenc->lowres_mvs[1][h->fref1[0]->i_frame-h->fenc->i_frame-1]
-                                         : h->fenc->lowres_mvs[0][h->fenc->i_frame-h->fref0[0]->i_frame-1];
-        if( lowres_mv[0][0] != 0x7fff )
-        {
-            M32( mvc[i] ) = (M32( lowres_mv[h->mb.i_mb_xy] )*2)&0xfffeffff;
-            i++;
-        }
-    }
-
-    /* spatial predictors */
-    if( h->mb.i_neighbour & MB_LEFT )
-    {
-        int i_mb_l = h->mb.i_mb_xy - 1;
-        /* skip MBs didn't go through the whole search process, so mvr is undefined */
-        if( !IS_SKIP( h->mb.type[i_mb_l] ) )
-            SET_MVP( mvr[i_mb_l] );
-    }
-    if( h->mb.i_neighbour & MB_TOP )
-    {
-        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] );
-
-        if( h->mb.i_neighbour & MB_TOPLEFT && !IS_SKIP( h->mb.type[i_mb_t - 1] ) )
-            SET_MVP( mvr[i_mb_t-1] );
-        if( h->mb.i_mb_x < h->mb.i_mb_stride - 1 && !IS_SKIP( h->mb.type[i_mb_t + 1] ) )
-            SET_MVP( mvr[i_mb_t+1] );
-    }
-#undef SET_MVP
+#define MC_LUMA_BI(p) \
+    src0 = h->mc.get_ref( tmp0, &i_stride0, &h->mb.pic.p_fref[0][i_ref0][p*4], h->mb.pic.i_stride[p], \
+                          mvx0, mvy0, 4*width, 4*height, x264_weight_none ); \
+    src1 = h->mc.get_ref( tmp1, &i_stride1, &h->mb.pic.p_fref[1][i_ref1][p*4], h->mb.pic.i_stride[p], \
+                          mvx1, mvy1, 4*width, 4*height, x264_weight_none ); \
+    h->mc.avg[i_mode]( &h->mb.pic.p_fdec[p][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE, \
+                       src0, i_stride0, src1, i_stride1, weight );
 
-    /* temporal predictors */
-    if( h->fref0[0]->i_ref[0] > 0 )
+static NOINLINE void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int height )
+{
+    int i8 = x264_scan8[0]+x+8*y;
+    int i_ref0 = h->mb.cache.ref[0][i8];
+    int i_ref1 = h->mb.cache.ref[1][i8];
+    int weight = h->mb.bipred_weight[i_ref0][i_ref1];
+    int mvx0   = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
+    int mvx1   = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
+    int mvy0   = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
+    int mvy1   = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
+    int i_mode = x264_size2pixel[height][width];
+    intptr_t i_stride0 = 16, i_stride1 = 16;
+    ALIGNED_ARRAY_16( pixel, tmp0,[16*16] );
+    ALIGNED_ARRAY_16( pixel, tmp1,[16*16] );
+    pixel *src0, *src1;
+
+    MC_LUMA_BI( 0 );
+
+    if( CHROMA444 )
     {
-        x264_frame_t *l0 = h->fref0[0];
-        int field = h->mb.i_mb_y&1;
-        int curpoc = h->fdec->i_poc + field*h->sh.i_delta_poc_bottom;
-        int refpoc = h->fref0[i_ref>>h->sh.b_mbaff]->i_poc;
-        if( h->sh.b_mbaff && field^(i_ref&1) )
-            refpoc += h->sh.i_delta_poc_bottom;
-
-#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; \
-            int ref_col = l0->ref[0][i_b8]; \
-            if( ref_col >= 0 ) \
-            { \
-                int scale = (curpoc - refpoc) * l0->inv_ref_poc[h->mb.b_interlaced&field][ref_col];\
-                mvc[i][0] = (l0->mv[0][i_b4][0]*scale + 128) >> 8;\
-                mvc[i][1] = (l0->mv[0][i_b4][1]*scale + 128) >> 8;\
-                i++; \
-            } \
-        }
-
-        SET_TMVP(0,0);
-        if( h->mb.i_mb_x < h->sps->i_mb_width-1 )
-            SET_TMVP(1,0);
-        if( h->mb.i_mb_y < h->sps->i_mb_height-1 )
-            SET_TMVP(0,1);
-#undef SET_TMVP
+        MC_LUMA_BI( 1 );
+        MC_LUMA_BI( 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, field;
-    for( field = 0; field <= h->sh.b_mbaff; field++ )
+    else
     {
-        int curpoc = h->fdec->i_poc + field*h->sh.i_delta_poc_bottom;
-        for( i = 0; i < (h->i_ref0<<h->sh.b_mbaff); i++ )
-        {
-            int refpoc = h->fref0[i>>h->sh.b_mbaff]->i_poc;
-            if( h->sh.b_mbaff && field^(i&1) )
-                refpoc += h->sh.i_delta_poc_bottom;
-            int delta = curpoc - refpoc;
-
-            h->fdec->inv_ref_poc[field][i] = (256 + delta/2) / delta;
-        }
+        int v_shift = CHROMA_V_SHIFT;
+        if( v_shift & MB_INTERLACED & i_ref0 )
+            mvy0 += (h->mb.i_mb_y & 1)*4 - 2;
+        if( v_shift & MB_INTERLACED & i_ref1 )
+            mvy1 += (h->mb.i_mb_y & 1)*4 - 2;
+
+        h->mc.mc_chroma( tmp0, tmp0+8, 16, h->mb.pic.p_fref[0][i_ref0][4], h->mb.pic.i_stride[1],
+                         mvx0, 2*mvy0>>v_shift, 2*width, 4*height>>v_shift );
+        h->mc.mc_chroma( tmp1, tmp1+8, 16, h->mb.pic.p_fref[1][i_ref1][4], h->mb.pic.i_stride[1],
+                         mvx1, 2*mvy1>>v_shift, 2*width, 4*height>>v_shift );
+
+        int chromapix = h->luma2chroma_pixel[i_mode];
+        int offset = (4*FDEC_STRIDE>>v_shift)*y + 2*x;
+        h->mc.avg[chromapix]( &h->mb.pic.p_fdec[1][offset], FDEC_STRIDE, tmp0,   16, tmp1,   16, weight );
+        h->mc.avg[chromapix]( &h->mb.pic.p_fdec[2][offset], FDEC_STRIDE, tmp0+8, 16, tmp1+8, 16, weight );
     }
 }
 
-static NOINLINE 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] ) + 4*4*x;
-    int       mvy   = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
-
-    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, mvy, 4*width, 4*height, &h->sh.weight[i_ref][0] );
-
-    // 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_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
-                     h->mb.pic.p_fref[0][i_ref][4], h->mb.pic.i_stride[1],
-                     mvx, mvy, 2*width, 2*height );
-
-    if( h->sh.weight[i_ref][1].weightfn )
-        h->sh.weight[i_ref][1].weightfn[width>>1]( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
-                                                   &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
-                                                   &h->sh.weight[i_ref][1], height*2 );
-
-    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], h->mb.pic.i_stride[2],
-                     mvx, mvy, 2*width, 2*height );
-
-    if( h->sh.weight[i_ref][2].weightfn )
-        h->sh.weight[i_ref][2].weightfn[width>>1]( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
-                                                   &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
-                                                   &h->sh.weight[i_ref][2],height*2 );
-
-}
-static NOINLINE 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] ) + 4*4*x;
-    int       mvy   = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
-
-    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, mvy, 4*width, 4*height, weight_none );
-
-    if( h->mb.b_interlaced & i_ref )
-        mvy += (h->mb.i_mb_y & 1)*4 - 2;
-
-    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], 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[1][i_ref][5], h->mb.pic.i_stride[2],
-                     mvx, mvy, 2*width, 2*height );
-}
-
-static NOINLINE void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int height )
-{
-    const int i8 = x264_scan8[0]+x+8*y;
-    const int i_ref0 = h->mb.cache.ref[0][i8];
-    const int i_ref1 = h->mb.cache.ref[1][i8];
-    const int weight = h->mb.bipred_weight[i_ref0][i_ref1];
-    const int mvx0   = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
-    const int mvx1   = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
-    int       mvy0   = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
-    int       mvy1   = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
-    int       i_mode = x264_size2pixel[height][width];
-    int       i_stride0 = 16, i_stride1 = 16;
-    ALIGNED_ARRAY_16( uint8_t, tmp0,[16*16] );
-    ALIGNED_ARRAY_16( uint8_t, tmp1,[16*16] );
-    uint8_t *src0, *src1;
-
-    src0 = h->mc.get_ref( tmp0, &i_stride0, h->mb.pic.p_fref[0][i_ref0], h->mb.pic.i_stride[0],
-                          mvx0, mvy0, 4*width, 4*height, weight_none );
-    src1 = h->mc.get_ref( tmp1, &i_stride1, h->mb.pic.p_fref[1][i_ref1], h->mb.pic.i_stride[0],
-                          mvx1, mvy1, 4*width, 4*height, weight_none );
-    h->mc.avg[i_mode]( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
-                       src0, i_stride0, src1, i_stride1, weight );
-
-    if( h->mb.b_interlaced & i_ref0 )
-        mvy0 += (h->mb.i_mb_y & 1)*4 - 2;
-    if( h->mb.b_interlaced & i_ref1 )
-        mvy1 += (h->mb.i_mb_y & 1)*4 - 2;
-
-    h->mc.mc_chroma( tmp0, 16, h->mb.pic.p_fref[0][i_ref0][4], h->mb.pic.i_stride[1],
-                     mvx0, mvy0, 2*width, 2*height );
-    h->mc.mc_chroma( tmp1, 16, h->mb.pic.p_fref[1][i_ref1][4], 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, tmp0, 16, tmp1, 16, weight );
-    h->mc.mc_chroma( tmp0, 16, h->mb.pic.p_fref[0][i_ref0][5], h->mb.pic.i_stride[2],
-                     mvx0, mvy0, 2*width, 2*height );
-    h->mc.mc_chroma( tmp1, 16, h->mb.pic.p_fref[1][i_ref1][5], 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, tmp0, 16, tmp1, 16, weight );
-}
+#undef MC_LUMA
+#undef MC_LUMA_BI
 
 void x264_mb_mc_8x8( x264_t *h, int i8 )
 {
-    const int x = 2*(i8&1);
-    const int y = 2*(i8>>1);
+    int x = 2*(i8&1);
+    int y = 2*(i8>>1);
 
     if( h->sh.i_type == SLICE_TYPE_P )
     {
@@ -626,10 +186,10 @@ void x264_mb_mc_8x8( x264_t *h, int i8 )
     }
     else
     {
-        const int i8 = x264_scan8[0] + x + 8*y;
+        int scan8 = x264_scan8[0] + x + 8*y;
 
-        if( h->mb.cache.ref[0][i8] >= 0 )
-            if( h->mb.cache.ref[1][i8] >= 0 )
+        if( h->mb.cache.ref[0][scan8] >= 0 )
+            if( h->mb.cache.ref[1][scan8] >= 0 )
                 x264_mb_mc_01xywh( h, x, y, 2, 2 );
             else
                 x264_mb_mc_0xywh( h, x, y, 2, 2 );
@@ -642,16 +202,15 @@ void x264_mb_mc( x264_t *h )
 {
     if( h->mb.i_partition == D_8x8 )
     {
-        int i;
-        for( i = 0; i < 4; i++ )
+        for( int i = 0; i < 4; i++ )
             x264_mb_mc_8x8( h, i );
     }
     else
     {
-        const int ref0a = h->mb.cache.ref[0][x264_scan8[ 0]];
-        const int ref0b = h->mb.cache.ref[0][x264_scan8[12]];
-        const int ref1a = h->mb.cache.ref[1][x264_scan8[ 0]];
-        const int ref1b = h->mb.cache.ref[1][x264_scan8[12]];
+        int ref0a = h->mb.cache.ref[0][x264_scan8[ 0]];
+        int ref0b = h->mb.cache.ref[0][x264_scan8[12]];
+        int ref1a = h->mb.cache.ref[1][x264_scan8[ 0]];
+        int ref1b = h->mb.cache.ref[1][x264_scan8[12]];
 
         if( h->mb.i_partition == D_16x16 )
         {
@@ -687,53 +246,55 @@ void x264_mb_mc( x264_t *h )
     }
 }
 
-int x264_macroblock_cache_init( x264_t *h )
+int x264_macroblock_cache_allocate( x264_t *h )
 {
-    int i, j;
     int i_mb_count = h->mb.i_mb_count;
 
-    h->mb.i_mb_stride = h->sps->i_mb_width;
-    h->mb.i_b8_stride = h->sps->i_mb_width * 2;
-    h->mb.i_b4_stride = h->sps->i_mb_width * 4;
+    h->mb.i_mb_stride = h->mb.i_mb_width;
+    h->mb.i_b8_stride = h->mb.i_mb_width * 2;
+    h->mb.i_b4_stride = h->mb.i_mb_width * 4;
 
-    h->mb.b_interlaced = h->param.b_interlaced;
+    h->mb.b_interlaced = PARAM_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) );
     CHECKED_MALLOC( h->mb.mb_transform_size, i_mb_count * sizeof(int8_t) );
+    CHECKED_MALLOC( h->mb.slice_table, i_mb_count * sizeof(uint16_t) );
+    memset( h->mb.slice_table, -1, i_mb_count * sizeof(uint16_t) );
 
     /* 0 -> 3 top(4), 4 -> 6 : left(3) */
     CHECKED_MALLOC( h->mb.intra4x4_pred_mode, i_mb_count * 8 * sizeof(int8_t) );
 
     /* all coeffs */
-    CHECKED_MALLOC( h->mb.non_zero_count, i_mb_count * 24 * sizeof(uint8_t) );
+    CHECKED_MALLOC( h->mb.non_zero_count, i_mb_count * 48 * sizeof(uint8_t) );
 
     if( h->param.b_cabac )
     {
+        CHECKED_MALLOC( h->mb.skipbp, i_mb_count * sizeof(int8_t) );
         CHECKED_MALLOC( h->mb.chroma_pred_mode, i_mb_count * sizeof(int8_t) );
-        CHECKED_MALLOC( h->mb.mvd[0], 2*8 * i_mb_count * sizeof(uint8_t) );
-        CHECKED_MALLOC( h->mb.mvd[1], 2*8 * i_mb_count * sizeof(uint8_t) );
+        CHECKED_MALLOC( h->mb.mvd[0], i_mb_count * sizeof( **h->mb.mvd ) );
+        if( h->param.i_bframe )
+            CHECKED_MALLOC( h->mb.mvd[1], i_mb_count * sizeof( **h->mb.mvd ) );
     }
 
-    for( i=0; i<2; i++ )
+    for( int i = 0; i < 2; i++ )
     {
-        int i_refs = X264_MIN(16, (i ? 1 + !!h->param.i_bframe_pyramid : h->param.i_frame_reference) ) << h->param.b_interlaced;
+        int i_refs = X264_MIN(X264_REF_MAX, (i ? 1 + !!h->param.i_bframe_pyramid : h->param.i_frame_reference) ) << PARAM_INTERLACED;
         if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART )
-            i_refs = X264_MIN(16, i_refs + 2); //smart weights add two duplicate frames
-        else if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_BLIND )
-            i_refs = X264_MIN(16, i_refs + 1); //blind weights add one duplicate frame
+            i_refs = X264_MIN(X264_REF_MAX, i_refs + 1 + (BIT_DEPTH == 8)); //smart weights add two duplicate frames, one in >8-bit
 
-        for( j=0; j < i_refs; j++ )
-            CHECKED_MALLOC( h->mb.mvr[i][j], 2 * i_mb_count * sizeof(int16_t) );
+        for( int j = !i; j < i_refs; j++ )
+        {
+            CHECKED_MALLOC( h->mb.mvr[i][j], 2 * (i_mb_count + 1) * sizeof(int16_t) );
+            M32( h->mb.mvr[i][j][0] ) = 0;
+            h->mb.mvr[i][j]++;
+        }
     }
 
     if( h->param.analyse.i_weighted_pred )
     {
-        int i_padv = PADV << h->param.b_interlaced;
-#define ALIGN(x,a) (((x)+((a)-1))&~((a)-1))
-        int align = h->param.cpu&X264_CPU_CACHELINE_64 ? 64 : h->param.cpu&X264_CPU_CACHELINE_32 ? 32 : 16;
-        int i_stride, luma_plane_size;
+        int i_padv = PADV << PARAM_INTERLACED;
+        int luma_plane_size = 0;
         int numweightbuf;
 
         if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_FAKE )
@@ -742,8 +303,7 @@ int x264_macroblock_cache_init( x264_t *h )
             if( !h->param.i_sync_lookahead || h == h->thread[h->param.i_threads] )
             {
                 // Fake analysis only works on lowres
-                i_stride = ALIGN( h->sps->i_mb_width*8 + 2*PADH, align );
-                luma_plane_size = i_stride * (h->sps->i_mb_height*8+2*i_padv);
+                luma_plane_size = h->fdec->i_stride_lowres * (h->mb.i_mb_height*8+2*i_padv);
                 // Only need 1 buffer for analysis
                 numweightbuf = 1;
             }
@@ -752,100 +312,179 @@ int x264_macroblock_cache_init( x264_t *h )
         }
         else
         {
-            i_stride = ALIGN( h->sps->i_mb_width*16 + 2*PADH, align );
-            luma_plane_size = i_stride * (h->sps->i_mb_height*16+2*i_padv);
+            /* Both ref and fenc is stored for 4:2:0 and 4:2:2 which means that 4:2:0 and 4:4:4
+             * needs the same amount of space and 4:2:2 needs twice that much */
+            luma_plane_size = h->fdec->i_stride[0] * (h->mb.i_mb_height*(16<<(CHROMA_FORMAT==CHROMA_422))+2*i_padv);
 
             if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART )
-                //SMART can weight one ref and one offset -1
-                numweightbuf = 2;
+                //smart can weight one ref and one offset -1 in 8-bit
+                numweightbuf = 1 + (BIT_DEPTH == 8);
             else
-                //blind only has one weighted copy (offset -1)
+                //simple only has one weighted ref
                 numweightbuf = 1;
         }
 
-        for( i = 0; i < numweightbuf; i++ )
-            CHECKED_MALLOC( h->mb.p_weight_buf[i], luma_plane_size );
-#undef ALIGN
+        for( int i = 0; i < numweightbuf; i++ )
+            CHECKED_MALLOC( h->mb.p_weight_buf[i], luma_plane_size * sizeof(pixel) );
     }
 
-    for( i=0; i<=h->param.b_interlaced; i++ )
-        for( j=0; j<3; j++ )
-        {
-            /* shouldn't really be initialized, just silences a valgrind false-positive in predict_8x8_filter_mmx */
-            CHECKED_MALLOCZERO( h->mb.intra_border_backup[i][j], (h->sps->i_mb_width*16+32)>>!!j );
-            h->mb.intra_border_backup[i][j] += 8;
-        }
-
     return 0;
-fail: return -1;
+fail:
+    return -1;
 }
-void x264_macroblock_cache_end( x264_t *h )
+void x264_macroblock_cache_free( 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++ )
-        for( j=0; j<32; j++ )
-            x264_free( h->mb.mvr[i][j] );
-    for( i=0; i<16; i++ )
+    for( int i = 0; i < 2; i++ )
+        for( int j = !i; j < X264_REF_MAX*2; j++ )
+            if( h->mb.mvr[i][j] )
+                x264_free( h->mb.mvr[i][j]-1 );
+    for( int i = 0; i < X264_REF_MAX; i++ )
         x264_free( h->mb.p_weight_buf[i] );
 
     if( h->param.b_cabac )
     {
+        x264_free( h->mb.skipbp );
         x264_free( h->mb.chroma_pred_mode );
         x264_free( h->mb.mvd[0] );
         x264_free( h->mb.mvd[1] );
     }
+    x264_free( h->mb.slice_table );
     x264_free( h->mb.intra4x4_pred_mode );
     x264_free( h->mb.non_zero_count );
     x264_free( h->mb.mb_transform_size );
-    x264_free( h->mb.skipbp );
     x264_free( h->mb.cbp );
     x264_free( h->mb.qp );
 }
-void x264_macroblock_slice_init( x264_t *h )
+
+int x264_macroblock_thread_allocate( x264_t *h, int b_lookahead )
+{
+    if( !b_lookahead )
+    {
+        for( int i = 0; i < (PARAM_INTERLACED ? 5 : 2); i++ )
+            for( int j = 0; j < (CHROMA444 ? 3 : 2); j++ )
+            {
+                CHECKED_MALLOC( h->intra_border_backup[i][j], (h->sps->i_mb_width*16+32) * sizeof(pixel) );
+                h->intra_border_backup[i][j] += 16;
+            }
+        for( int i = 0; i <= PARAM_INTERLACED; i++ )
+        {
+            if( h->param.b_sliced_threads )
+            {
+                /* Only allocate the first one, and allocate it for the whole frame, because we
+                 * won't be deblocking until after the frame is fully encoded. */
+                if( h == h->thread[0] && !i )
+                    CHECKED_MALLOC( h->deblock_strength[0], sizeof(**h->deblock_strength) * h->mb.i_mb_count );
+                else
+                    h->deblock_strength[i] = h->thread[0]->deblock_strength[0];
+            }
+            else
+                CHECKED_MALLOC( h->deblock_strength[i], sizeof(**h->deblock_strength) * h->mb.i_mb_width );
+            h->deblock_strength[1] = h->deblock_strength[i];
+        }
+    }
+
+    /* Allocate scratch buffer */
+    int scratch_size = 0;
+    if( !b_lookahead )
+    {
+        int buf_hpel = (h->thread[0]->fdec->i_width[0]+48) * sizeof(int16_t);
+        int buf_ssim = h->param.analyse.b_ssim * 8 * (h->param.i_width/4+3) * sizeof(int);
+        int me_range = X264_MIN(h->param.analyse.i_me_range, h->param.analyse.i_mv_range);
+        int buf_tesa = (h->param.analyse.i_me_method >= X264_ME_ESA) *
+            ((me_range*2+24) * sizeof(int16_t) + (me_range+4) * (me_range+1) * 4 * sizeof(mvsad_t));
+        scratch_size = X264_MAX3( buf_hpel, buf_ssim, buf_tesa );
+    }
+    int buf_mbtree = h->param.rc.b_mb_tree * ((h->mb.i_mb_width+7)&~7) * sizeof(int);
+    scratch_size = X264_MAX( scratch_size, buf_mbtree );
+    if( scratch_size )
+        CHECKED_MALLOC( h->scratch_buffer, scratch_size );
+    else
+        h->scratch_buffer = NULL;
+
+    return 0;
+fail:
+    return -1;
+}
+
+void x264_macroblock_thread_free( x264_t *h, int b_lookahead )
 {
-    int i, j;
+    if( !b_lookahead )
+    {
+        for( int i = 0; i <= PARAM_INTERLACED; i++ )
+            if( !h->param.b_sliced_threads || (h == h->thread[0] && !i) )
+                x264_free( h->deblock_strength[i] );
+        for( int i = 0; i < (PARAM_INTERLACED ? 5 : 2); i++ )
+            for( int j = 0; j < (CHROMA444 ? 3 : 2); j++ )
+                x264_free( h->intra_border_backup[i][j] - 16 );
+    }
+    x264_free( h->scratch_buffer );
+}
 
+void x264_macroblock_slice_init( x264_t *h )
+{
     h->mb.mv[0] = h->fdec->mv[0];
     h->mb.mv[1] = h->fdec->mv[1];
+    h->mb.mvr[0][0] = h->fdec->mv16x16;
     h->mb.ref[0] = h->fdec->ref[0];
     h->mb.ref[1] = h->fdec->ref[1];
     h->mb.type = h->fdec->mb_type;
     h->mb.partition = h->fdec->mb_partition;
+    h->mb.field = h->fdec->field;
 
-    h->fdec->i_ref[0] = h->i_ref0;
-    h->fdec->i_ref[1] = h->i_ref1;
-    for( i = 0; i < h->i_ref0; i++ )
-        h->fdec->ref_poc[0][i] = h->fref0[i]->i_poc;
+    h->fdec->i_ref[0] = h->i_ref[0];
+    h->fdec->i_ref[1] = h->i_ref[1];
+    for( int i = 0; i < h->i_ref[0]; i++ )
+        h->fdec->ref_poc[0][i] = h->fref[0][i]->i_poc;
     if( h->sh.i_type == SLICE_TYPE_B )
     {
-        for( i = 0; i < h->i_ref1; i++ )
-            h->fdec->ref_poc[1][i] = h->fref1[i]->i_poc;
+        for( int i = 0; i < h->i_ref[1]; i++ )
+            h->fdec->ref_poc[1][i] = h->fref[1][i]->i_poc;
 
         map_col_to_list0(-1) = -1;
         map_col_to_list0(-2) = -2;
-        for( i = 0; i < h->fref1[0]->i_ref[0]; i++ )
+        for( int i = 0; i < h->fref[1][0]->i_ref[0]; i++ )
         {
-            int poc = h->fref1[0]->ref_poc[0][i];
+            int poc = h->fref[1][0]->ref_poc[0][i];
             map_col_to_list0(i) = -2;
-            for( j = 0; j < h->i_ref0; j++ )
-                if( h->fref0[j]->i_poc == poc )
+            for( int j = 0; j < h->i_ref[0]; j++ )
+                if( h->fref[0][j]->i_poc == poc )
                 {
                     map_col_to_list0(i) = j;
                     break;
                 }
         }
     }
-    if( h->sh.i_type == SLICE_TYPE_P )
-        memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
+    else if( h->sh.i_type == SLICE_TYPE_P )
+    {
+        if( h->sh.i_disable_deblocking_filter_idc != 1 && h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART )
+        {
+            deblock_ref_table(-2) = -2;
+            deblock_ref_table(-1) = -1;
+            for( int i = 0; i < h->i_ref[0] << SLICE_MBAFF; i++ )
+            {
+                /* Mask off high bits to avoid frame num collisions with -1/-2.
+                 * In current x264 frame num values don't cover a range of more
+                 * than 32, so 6 bits is enough for uniqueness. */
+                if( !MB_INTERLACED )
+                    deblock_ref_table(i) = h->fref[0][i]->i_frame_num&63;
+                else
+                    deblock_ref_table(i) = ((h->fref[0][i>>1]->i_frame_num&63)<<1) + (i&1);
+            }
+        }
+    }
 
     /* 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 ) );
+    memset( h->mb.cache.ref, -2, sizeof( h->mb.cache.ref ) );
 
-    setup_inverse_delta_pocs( h );
+    if( h->i_ref[0] > 0 )
+        for( int field = 0; field <= SLICE_MBAFF; field++ )
+        {
+            int curpoc = h->fdec->i_poc + h->fdec->i_delta_poc[field];
+            int refpoc = h->fref[0][0]->i_poc + h->fref[0][0]->i_delta_poc[field];
+            int delta = curpoc - refpoc;
+
+            h->fdec->inv_ref_poc[field] = (256 + delta/2) / delta;
+        }
 
     h->mb.i_neighbour4[6] =
     h->mb.i_neighbour4[9] =
@@ -861,395 +500,851 @@ void x264_macroblock_slice_init( x264_t *h )
 
 void x264_macroblock_thread_init( x264_t *h )
 {
-    /* fdec:      fenc:
-     * yyyyyyy
-     * yYYYY      YYYY
-     * yYYYY      YYYY
-     * yYYYY      YYYY
-     * yYYYY      YYYY
-     * uuu vvv    UUVV
-     * uUU vVV    UUVV
-     * uUU vVV
+    h->mb.i_me_method = h->param.analyse.i_me_method;
+    h->mb.i_subpel_refine = h->param.analyse.i_subpel_refine;
+    if( h->sh.i_type == SLICE_TYPE_B && (h->mb.i_subpel_refine == 6 || h->mb.i_subpel_refine == 8) )
+        h->mb.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->sh.i_type == SLICE_TYPE_B && h->mb.i_subpel_refine >= 9));
+    h->mb.b_dct_decimate = h->sh.i_type == SLICE_TYPE_B ||
+                          (h->param.analyse.b_dct_decimate && h->sh.i_type != SLICE_TYPE_I);
+    h->mb.i_mb_prev_xy = -1;
+
+    /*          4:2:0                      4:2:2                      4:4:4
+     * fdec            fenc       fdec            fenc       fdec            fenc
+     * y y y y y y y   Y Y Y Y    y y y y y y y   Y Y Y Y    y y y y y y y   Y Y Y Y
+     * y Y Y Y Y       Y Y Y Y    y Y Y Y Y       Y Y Y Y    y Y Y Y Y       Y Y Y Y
+     * y Y Y Y Y       Y Y Y Y    y Y Y Y Y       Y Y Y Y    y Y Y Y Y       Y Y Y Y
+     * y Y Y Y Y       Y Y Y Y    y Y Y Y Y       Y Y Y Y    y Y Y Y Y       Y Y Y Y
+     * y Y Y Y Y       U U V V    y Y Y Y Y       U U V V    y Y Y Y Y       U U U U
+     * u u u   v v v   U U V V    u u u   v v v   U U V V    u u u u u u u   U U U U
+     * u U U   v V V              u U U   v V V   U U V V    u U U U U       U U U U
+     * u U U   v V V              u U U   v V V   U U V V    u U U U U       U U U U
+     *                            u U U   v V V              u U U U U       V V V V
+     *                            u U U   v V V              u U U U U       V V V V
+     *                                                       v v v v v v v   V V V V
+     *                                                       v V V V V       V V V V
+     *                                                       v V V V V
+     *                                                       v V V V V
+     *                                                       v V V V V
      */
     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_fenc[1] = h->mb.pic.fenc_buf + 16*FENC_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;
+    if( CHROMA444 )
+    {
+        h->mb.pic.p_fenc[2] = h->mb.pic.fenc_buf + 32*FENC_STRIDE;
+        h->mb.pic.p_fdec[2] = h->mb.pic.fdec_buf + 36*FDEC_STRIDE;
+    }
+    else
+    {
+        h->mb.pic.p_fenc[2] = h->mb.pic.fenc_buf + 16*FENC_STRIDE + 8;
+        h->mb.pic.p_fdec[2] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE + 16;
+    }
 }
 
 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);
+    int off_y  = 16 * i_mb_x + 16 * i_mb_y * stride_y;
+    int off_uv = 16 * i_mb_x + (16 * i_mb_y * stride_uv >> CHROMA_V_SHIFT);
     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 );
+                         fenc->plane[1]+off_uv, stride_uv, i_mb_x );
 }
 
-static NOINLINE void copy_column8( uint8_t *dst, uint8_t *src )
+NOINLINE void x264_copy_column8( pixel *dst, pixel *src )
 {
     // input pointers are offset by 4 rows because that's faster (smaller instruction size on x86)
-    int i;
-    for( i = -4; i < 4; i++ )
+    for( int i = -4; i < 4; i++ )
         dst[i*FDEC_STRIDE] = src[i*FDEC_STRIDE];
 }
 
-static void ALWAYS_INLINE x264_macroblock_load_pic_pointers( x264_t *h, int i_mb_x, int i_mb_y, int i)
+static void ALWAYS_INLINE x264_macroblock_load_pic_pointers( x264_t *h, int mb_x, int mb_y, int i, int b_chroma, int b_mbaff )
 {
-    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);
-    const uint8_t *plane_fdec = &h->fdec->plane[i][i_pix_offset];
-    const uint8_t *intra_fdec = h->param.b_sliced_threads ? plane_fdec-i_stride2 :
-                                &h->mb.intra_border_backup[i_mb_y & h->sh.b_mbaff][i][i_mb_x*16>>!!i];
+    int mb_interlaced = b_mbaff && MB_INTERLACED;
+    int height = b_chroma ? 16 >> CHROMA_V_SHIFT : 16;
+    int i_stride = h->fdec->i_stride[i];
+    int i_stride2 = i_stride << mb_interlaced;
+    int i_pix_offset = mb_interlaced
+                     ? 16 * mb_x + height * (mb_y&~1) * i_stride + (mb_y&1) * i_stride
+                     : 16 * mb_x + height * mb_y * i_stride;
+    pixel *plane_fdec = &h->fdec->plane[i][i_pix_offset];
+    int fdec_idx = b_mbaff ? (mb_interlaced ? (3 + (mb_y&1)) : (mb_y&1) ? 2 : 4) : !(mb_y&1);
+    pixel *intra_fdec = &h->intra_border_backup[fdec_idx][i][mb_x*16];
     int ref_pix_offset[2] = { i_pix_offset, i_pix_offset };
-    x264_frame_t **fref[2] = { h->fref0, h->fref1 };
-    int j, k;
-    if( h->mb.b_interlaced )
-        ref_pix_offset[1] += (1-2*(i_mb_y&1)) * i_stride;
+    /* ref_pix_offset[0] references the current field and [1] the opposite field. */
+    if( mb_interlaced )
+        ref_pix_offset[1] += (1-2*(mb_y&1)) * i_stride;
     h->mb.pic.i_stride[i] = i_stride2;
     h->mb.pic.p_fenc_plane[i] = &h->fenc->plane[i][i_pix_offset];
-    h->mc.copy[i?PIXEL_8x8:PIXEL_16x16]( h->mb.pic.p_fenc[i], FENC_STRIDE,
-        h->mb.pic.p_fenc_plane[i], i_stride2, w );
-    if( i_mb_y > 0 )
-        memcpy( &h->mb.pic.p_fdec[i][-1-FDEC_STRIDE], intra_fdec-1, w*3/2+1 );
+    if( b_chroma )
+    {
+        h->mc.load_deinterleave_chroma_fenc( h->mb.pic.p_fenc[1], h->mb.pic.p_fenc_plane[1], i_stride2, height );
+        memcpy( h->mb.pic.p_fdec[1]-FDEC_STRIDE, intra_fdec, 8*sizeof(pixel) );
+        memcpy( h->mb.pic.p_fdec[2]-FDEC_STRIDE, intra_fdec+8, 8*sizeof(pixel) );
+        h->mb.pic.p_fdec[1][-FDEC_STRIDE-1] = intra_fdec[-1-8];
+        h->mb.pic.p_fdec[2][-FDEC_STRIDE-1] = intra_fdec[-1];
+    }
     else
-        memset( &h->mb.pic.p_fdec[i][-1-FDEC_STRIDE], 0, w*3/2+1 );
-    if( h->mb.b_interlaced || h->mb.b_reencode_mb )
-        for( j = 0; j < w; j++ )
-            h->mb.pic.p_fdec[i][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride2];
-    for( j = 0; j < h->mb.pic.i_fref[0]; j++ )
     {
-        h->mb.pic.p_fref[0][j][i==0 ? 0:i+3] = &fref[0][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[0][j][k] = &fref[0][j >> h->mb.b_interlaced]->filtered[k][ref_pix_offset[j&1]];
-            if( h->sh.weight[j][0].weightfn )
-                h->mb.pic.p_fref_w[j] = &h->fenc->weighted[j >> h->mb.b_interlaced][ref_pix_offset[j&1]];
+        h->mc.copy[PIXEL_16x16]( h->mb.pic.p_fenc[i], FENC_STRIDE, h->mb.pic.p_fenc_plane[i], i_stride2, 16 );
+        memcpy( h->mb.pic.p_fdec[i]-FDEC_STRIDE, intra_fdec, 24*sizeof(pixel) );
+        h->mb.pic.p_fdec[i][-FDEC_STRIDE-1] = intra_fdec[-1];
+    }
+    if( b_mbaff || h->mb.b_reencode_mb )
+    {
+        for( int j = 0; j < height; j++ )
+            if( b_chroma )
+            {
+                h->mb.pic.p_fdec[1][-1+j*FDEC_STRIDE] = plane_fdec[-2+j*i_stride2];
+                h->mb.pic.p_fdec[2][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride2];
+            }
             else
-                h->mb.pic.p_fref_w[j] = h->mb.pic.p_fref[0][j][0];
+                h->mb.pic.p_fdec[i][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride2];
+    }
+    pixel *plane_src, **filtered_src;
+    for( int j = 0; j < h->mb.pic.i_fref[0]; j++ )
+    {
+        // Interpolate between pixels in same field.
+        if( mb_interlaced )
+        {
+            plane_src = h->fref[0][j>>1]->plane_fld[i];
+            filtered_src = h->fref[0][j>>1]->filtered_fld[i];
+        }
+        else
+        {
+            plane_src = h->fref[0][j]->plane[i];
+            filtered_src = h->fref[0][j]->filtered[i];
+        }
+        h->mb.pic.p_fref[0][j][i*4] = plane_src + ref_pix_offset[j&1];
+
+        if( !b_chroma )
+        {
+            for( int k = 1; k < 4; k++ )
+                h->mb.pic.p_fref[0][j][i*4+k] = filtered_src[k] + ref_pix_offset[j&1];
+            if( !i )
+            {
+                if( h->sh.weight[j][0].weightfn )
+                    h->mb.pic.p_fref_w[j] = &h->fenc->weighted[j >> mb_interlaced][ref_pix_offset[j&1]];
+                else
+                    h->mb.pic.p_fref_w[j] = h->mb.pic.p_fref[0][j][0];
+            }
         }
     }
     if( h->sh.i_type == SLICE_TYPE_B )
-        for( j = 0; j < h->mb.pic.i_fref[1]; j++ )
+        for( int j = 0; j < h->mb.pic.i_fref[1]; j++ )
         {
-            h->mb.pic.p_fref[1][j][i==0 ? 0:i+3] = &fref[1][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[1][j][k] = &fref[1][j >> h->mb.b_interlaced]->filtered[k][ref_pix_offset[j&1]];
+            if( mb_interlaced )
+            {
+                plane_src = h->fref[1][j>>1]->plane_fld[i];
+                filtered_src = h->fref[1][j>>1]->filtered_fld[i];
+            }
+            else
+            {
+                plane_src = h->fref[1][j]->plane[i];
+                filtered_src = h->fref[1][j]->filtered[i];
+            }
+            h->mb.pic.p_fref[1][j][i*4] = plane_src + ref_pix_offset[j&1];
+
+            if( !b_chroma )
+                for( int k = 1; k < 4; k++ )
+                    h->mb.pic.p_fref[1][j][i*4+k] = filtered_src[k] + ref_pix_offset[j&1];
         }
 }
 
-void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
+static const x264_left_table_t left_indices[4] =
 {
-    int i_mb_xy = i_mb_y * h->mb.i_mb_stride + i_mb_x;
-    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;
-
-    /* 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;
+    /* Current is progressive */
+    {{ 4, 4, 5, 5}, { 3,  3,  7,  7}, {16+1, 16+1, 32+1, 32+1}, {0, 0, 1, 1}, {0, 0, 0, 0}},
+    {{ 6, 6, 3, 3}, {11, 11, 15, 15}, {16+5, 16+5, 32+5, 32+5}, {2, 2, 3, 3}, {1, 1, 1, 1}},
+    /* Current is interlaced */
+    {{ 4, 6, 4, 6}, { 3, 11,  3, 11}, {16+1, 16+1, 32+1, 32+1}, {0, 2, 0, 2}, {0, 1, 0, 1}},
+    /* Both same */
+    {{ 4, 5, 6, 3}, { 3,  7, 11, 15}, {16+1, 16+5, 32+1, 32+5}, {0, 1, 2, 3}, {0, 0, 1, 1}}
+};
+
+static void ALWAYS_INLINE x264_macroblock_cache_load_neighbours( x264_t *h, int mb_x, int mb_y, int b_interlaced )
+{
+    const int mb_interlaced = b_interlaced && MB_INTERLACED;
+    int top_y = mb_y - (1 << mb_interlaced);
+    int top = top_y * h->mb.i_mb_stride + mb_x;
+
+    h->mb.i_mb_x = mb_x;
+    h->mb.i_mb_y = mb_y;
+    h->mb.i_mb_xy = mb_y * h->mb.i_mb_stride + mb_x;
+    h->mb.i_b8_xy = 2*(mb_y * h->mb.i_b8_stride + mb_x);
+    h->mb.i_b4_xy = 4*(mb_y * h->mb.i_b4_stride + mb_x);
+    h->mb.left_b8[0] =
+    h->mb.left_b8[1] = -1;
+    h->mb.left_b4[0] =
+    h->mb.left_b4[1] = -1;
     h->mb.i_neighbour = 0;
     h->mb.i_neighbour_intra = 0;
-
-    /* load cache */
-    if( i_top_xy >= h->sh.i_first_mb )
+    h->mb.i_neighbour_frame = 0;
+    h->mb.i_mb_top_xy = -1;
+    h->mb.i_mb_top_y = -1;
+    h->mb.i_mb_left_xy[0] = h->mb.i_mb_left_xy[1] = -1;
+    h->mb.i_mb_topleft_xy = -1;
+    h->mb.i_mb_topright_xy = -1;
+    h->mb.i_mb_type_top = -1;
+    h->mb.i_mb_type_left[0] = h->mb.i_mb_type_left[1] = -1;
+    h->mb.i_mb_type_topleft = -1;
+    h->mb.i_mb_type_topright = -1;
+    h->mb.left_index_table = &left_indices[3];
+    h->mb.topleft_partition = 0;
+
+    int topleft_y = top_y;
+    int topright_y = top_y;
+    int left[2];
+
+    left[0] = left[1] = h->mb.i_mb_xy - 1;
+    h->mb.left_b8[0] = h->mb.left_b8[1] = h->mb.i_b8_xy - 2;
+    h->mb.left_b4[0] = h->mb.left_b4[1] = h->mb.i_b4_xy - 4;
+
+    if( b_interlaced )
     {
-        h->mb.i_mb_type_top =
-        i_top_type = h->mb.type[i_top_xy];
-        h->mb.cache.i_cbp_top = h->mb.cbp[i_top_xy];
-
-        h->mb.i_neighbour |= MB_TOP;
+        h->mb.i_mb_top_mbpair_xy = h->mb.i_mb_xy - 2*h->mb.i_mb_stride;
+        h->mb.i_mb_topleft_y = -1;
+        h->mb.i_mb_topright_y = -1;
 
-        if( !h->param.b_constrained_intra || IS_INTRA( i_top_type ) )
-            h->mb.i_neighbour_intra |= MB_TOP;
-
-        /* load intra4x4 */
-        CP32( &h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8], &h->mb.intra4x4_pred_mode[i_top_xy][0] );
+        if( mb_y&1 )
+        {
+            if( mb_x && mb_interlaced != h->mb.field[h->mb.i_mb_xy-1] )
+            {
+                left[0] = left[1] = h->mb.i_mb_xy - 1 - h->mb.i_mb_stride;
+                h->mb.left_b8[0] = h->mb.left_b8[1] = h->mb.i_b8_xy - 2 - 2*h->mb.i_b8_stride;
+                h->mb.left_b4[0] = h->mb.left_b4[1] = h->mb.i_b4_xy - 4 - 4*h->mb.i_b4_stride;
 
-        /* load non_zero_count */
-        CP32( &h->mb.cache.non_zero_count[x264_scan8[0] - 8], &h->mb.non_zero_count[i_top_xy][12] );
-        /* shift because x264_scan8[16] is misaligned */
-        M32( &h->mb.cache.non_zero_count[x264_scan8[16+0] - 9] ) = M16( &h->mb.non_zero_count[i_top_xy][18] ) << 8;
-        M32( &h->mb.cache.non_zero_count[x264_scan8[16+4] - 9] ) = M16( &h->mb.non_zero_count[i_top_xy][22] ) << 8;
-    }
-    else
-    {
-        h->mb.i_mb_type_top = -1;
-        h->mb.cache.i_cbp_top = -1;
+                if( mb_interlaced )
+                {
+                    h->mb.left_index_table = &left_indices[2];
+                    left[1] += h->mb.i_mb_stride;
+                    h->mb.left_b8[1] += 2*h->mb.i_b8_stride;
+                    h->mb.left_b4[1] += 4*h->mb.i_b4_stride;
+                }
+                else
+                {
+                    h->mb.left_index_table = &left_indices[1];
+                    topleft_y++;
+                    h->mb.topleft_partition = 1;
+                }
+            }
+            if( !mb_interlaced )
+                topright_y = -1;
+        }
+        else
+        {
+            if( mb_interlaced && top >= 0 )
+            {
+                if( !h->mb.field[top] )
+                {
+                    top += h->mb.i_mb_stride;
+                    top_y++;
+                }
+                if( mb_x )
+                    topleft_y += !h->mb.field[h->mb.i_mb_stride*topleft_y + mb_x - 1];
+                if( mb_x < h->mb.i_mb_width-1 )
+                    topright_y += !h->mb.field[h->mb.i_mb_stride*topright_y + mb_x + 1];
+            }
+            if( mb_x && mb_interlaced != h->mb.field[h->mb.i_mb_xy-1] )
+            {
+                if( mb_interlaced )
+                {
+                    h->mb.left_index_table = &left_indices[2];
+                    left[1] += h->mb.i_mb_stride;
+                    h->mb.left_b8[1] += 2*h->mb.i_b8_stride;
+                    h->mb.left_b4[1] += 4*h->mb.i_b4_stride;
+                }
+                else
+                    h->mb.left_index_table = &left_indices[0];
+            }
+        }
+    }
 
-        /* load intra4x4 */
-        M32( &h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] ) = 0xFFFFFFFFU;
+    if( mb_x > 0 )
+    {
+        h->mb.i_neighbour_frame |= MB_LEFT;
+        h->mb.i_mb_left_xy[0] = left[0];
+        h->mb.i_mb_left_xy[1] = left[1];
+        h->mb.i_mb_type_left[0] = h->mb.type[h->mb.i_mb_left_xy[0]];
+        h->mb.i_mb_type_left[1] = h->mb.type[h->mb.i_mb_left_xy[1]];
+        if( h->mb.slice_table[left[0]] == h->sh.i_first_mb )
+        {
+            h->mb.i_neighbour |= MB_LEFT;
 
-        /* load non_zero_count */
-        M32( &h->mb.cache.non_zero_count[x264_scan8[   0] - 8] ) = 0x80808080U;
-        M32( &h->mb.cache.non_zero_count[x264_scan8[16+0] - 9] ) = 0x80808080U;
-        M32( &h->mb.cache.non_zero_count[x264_scan8[16+4] - 9] ) = 0x80808080U;
+            // FIXME: We don't currently support constrained intra + mbaff.
+            if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_left[0] ) )
+                h->mb.i_neighbour_intra |= MB_LEFT;
+        }
     }
 
-    if( i_mb_x > 0 && i_mb_xy > h->sh.i_first_mb )
+    /* We can't predict from the previous threadslice since it hasn't been encoded yet. */
+    if( (h->i_threadslice_start >> mb_interlaced) != (mb_y >> mb_interlaced) )
     {
-        i_left_xy = i_mb_xy - 1;
-        h->mb.i_mb_type_left =
-        i_left_type = h->mb.type[i_left_xy];
-        h->mb.cache.i_cbp_left = h->mb.cbp[h->mb.i_mb_xy - 1];
+        if( top >= 0 )
+        {
+            h->mb.i_neighbour_frame |= MB_TOP;
+            h->mb.i_mb_top_xy = top;
+            h->mb.i_mb_top_y = top_y;
+            h->mb.i_mb_type_top = h->mb.type[h->mb.i_mb_top_xy];
+            if( h->mb.slice_table[top] == h->sh.i_first_mb )
+            {
+                h->mb.i_neighbour |= MB_TOP;
+
+                if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_top ) )
+                    h->mb.i_neighbour_intra |= MB_TOP;
+
+                /* We only need to prefetch the top blocks because the left was just written
+                 * to as part of the previous cache_save.  Since most target CPUs use write-allocate
+                 * caches, left blocks are near-guaranteed to be in L1 cache.  Top--not so much. */
+                x264_prefetch( &h->mb.cbp[top] );
+                x264_prefetch( h->mb.intra4x4_pred_mode[top] );
+                x264_prefetch( &h->mb.non_zero_count[top][12] );
+                /* These aren't always allocated, but prefetching an invalid address can't hurt. */
+                x264_prefetch( &h->mb.mb_transform_size[top] );
+                x264_prefetch( &h->mb.skipbp[top] );
+            }
+        }
+
+        if( mb_x > 0 && topleft_y >= 0  )
+        {
+            h->mb.i_neighbour_frame |= MB_TOPLEFT;
+            h->mb.i_mb_topleft_xy = h->mb.i_mb_stride*topleft_y + mb_x - 1;
+            h->mb.i_mb_topleft_y = topleft_y;
+            h->mb.i_mb_type_topleft = h->mb.type[h->mb.i_mb_topleft_xy];
+            if( h->mb.slice_table[h->mb.i_mb_topleft_xy] == h->sh.i_first_mb )
+            {
+                h->mb.i_neighbour |= MB_TOPLEFT;
+
+                if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_topleft ) )
+                    h->mb.i_neighbour_intra |= MB_TOPLEFT;
+            }
+        }
+
+        if( mb_x < h->mb.i_mb_width - 1 && topright_y >= 0 )
+        {
+            h->mb.i_neighbour_frame |= MB_TOPRIGHT;
+            h->mb.i_mb_topright_xy = h->mb.i_mb_stride*topright_y + mb_x + 1;
+            h->mb.i_mb_topright_y = topright_y;
+            h->mb.i_mb_type_topright = h->mb.type[h->mb.i_mb_topright_xy];
+            if( h->mb.slice_table[h->mb.i_mb_topright_xy] == h->sh.i_first_mb )
+            {
+                h->mb.i_neighbour |= MB_TOPRIGHT;
 
-        h->mb.i_neighbour |= MB_LEFT;
+                if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_topright ) )
+                    h->mb.i_neighbour_intra |= MB_TOPRIGHT;
+            }
+        }
+    }
+}
+
+#define LTOP 0
+#if HAVE_INTERLACED
+#   define LBOT 1
+#else
+#   define LBOT 0
+#endif
+
+static void ALWAYS_INLINE x264_macroblock_cache_load( x264_t *h, int mb_x, int mb_y, int b_mbaff )
+{
+    x264_macroblock_cache_load_neighbours( h, mb_x, mb_y, b_mbaff );
+
+    int *left = h->mb.i_mb_left_xy;
+    int top  = h->mb.i_mb_top_xy;
+    int top_y = h->mb.i_mb_top_y;
+    int s8x8 = h->mb.i_b8_stride;
+    int s4x4 = h->mb.i_b4_stride;
+    int top_8x8 = (2*top_y+1) * s8x8 + 2*mb_x;
+    int top_4x4 = (4*top_y+3) * s4x4 + 4*mb_x;
+    int lists = (1 << h->sh.i_type) & 3;
+
+    /* GCC pessimizes direct loads from heap-allocated arrays due to aliasing. */
+    /* By only dereferencing them once, we avoid this issue. */
+    int8_t (*i4x4)[8] = h->mb.intra4x4_pred_mode;
+    uint8_t (*nnz)[48] = h->mb.non_zero_count;
+    int16_t *cbp = h->mb.cbp;
+
+    const x264_left_table_t *left_index_table = h->mb.left_index_table;
 
-        if( !h->param.b_constrained_intra || IS_INTRA( i_left_type ) )
-            h->mb.i_neighbour_intra |= MB_LEFT;
+    h->mb.cache.deblock_strength = h->deblock_strength[mb_y&1][h->param.b_sliced_threads?h->mb.i_mb_xy:mb_x];
 
+    /* load cache */
+    if( h->mb.i_neighbour & MB_TOP )
+    {
+        h->mb.cache.i_cbp_top = cbp[top];
         /* load intra4x4 */
-        h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][4];
-        h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][5];
-        h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][6];
-        h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][3];
+        CP32( &h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8], &i4x4[top][0] );
 
         /* load non_zero_count */
-        h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = h->mb.non_zero_count[i_left_xy][3];
-        h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = h->mb.non_zero_count[i_left_xy][7];
-        h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = h->mb.non_zero_count[i_left_xy][11];
-        h->mb.cache.non_zero_count[x264_scan8[10] - 1] = h->mb.non_zero_count[i_left_xy][15];
+        CP32( &h->mb.cache.non_zero_count[x264_scan8[ 0] - 8], &nnz[top][12] );
+        CP32( &h->mb.cache.non_zero_count[x264_scan8[16] - 8], &nnz[top][16-4 + (16>>CHROMA_V_SHIFT)] );
+        CP32( &h->mb.cache.non_zero_count[x264_scan8[32] - 8], &nnz[top][32-4 + (16>>CHROMA_V_SHIFT)] );
 
-        h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] = h->mb.non_zero_count[i_left_xy][16+1];
-        h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] = h->mb.non_zero_count[i_left_xy][16+3];
-
-        h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] = h->mb.non_zero_count[i_left_xy][16+4+1];
-        h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = h->mb.non_zero_count[i_left_xy][16+4+3];
+        /* Finish the prefetching */
+        for( int l = 0; l < lists; l++ )
+        {
+            x264_prefetch( &h->mb.mv[l][top_4x4-1] );
+            /* Top right being not in the same cacheline as top left will happen
+             * once every 4 MBs, so one extra prefetch is worthwhile */
+            x264_prefetch( &h->mb.mv[l][top_4x4+4] );
+            x264_prefetch( &h->mb.ref[l][top_8x8-1] );
+            x264_prefetch( &h->mb.mvd[l][top] );
+        }
     }
     else
     {
-        h->mb.i_mb_type_left = -1;
-        h->mb.cache.i_cbp_left = -1;
+        h->mb.cache.i_cbp_top = -1;
 
-        h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] =
-        h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] =
-        h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] =
-        h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = -1;
+        /* load intra4x4 */
+        M32( &h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] ) = 0xFFFFFFFFU;
 
         /* load non_zero_count */
-        h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] =
-        h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] =
-        h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] =
-        h->mb.cache.non_zero_count[x264_scan8[10] - 1] =
-        h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] =
-        h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] =
-        h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] =
-        h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = 0x80;
+        M32( &h->mb.cache.non_zero_count[x264_scan8[ 0] - 8] ) = 0x80808080U;
+        M32( &h->mb.cache.non_zero_count[x264_scan8[16] - 8] ) = 0x80808080U;
+        M32( &h->mb.cache.non_zero_count[x264_scan8[32] - 8] ) = 0x80808080U;
     }
 
-    if( i_mb_x < h->sps->i_mb_width - 1 && i_top_xy + 1 >= h->sh.i_first_mb )
+    if( h->mb.i_neighbour & MB_LEFT )
     {
-        h->mb.i_neighbour |= MB_TOPRIGHT;
-        h->mb.i_mb_type_topright = h->mb.type[ i_top_xy + 1 ];
-        if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_topright ) )
-            h->mb.i_neighbour_intra |= MB_TOPRIGHT;
+        int ltop = left[LTOP];
+        int lbot = b_mbaff ? left[LBOT] : ltop;
+        if( b_mbaff )
+        {
+            const int16_t top_luma = (cbp[ltop] >> (left_index_table->mv[0]&(~1))) & 2;
+            const int16_t bot_luma = (cbp[lbot] >> (left_index_table->mv[2]&(~1))) & 2;
+            h->mb.cache.i_cbp_left = (cbp[ltop] & 0xfff0) | (bot_luma<<2) | top_luma;
+        }
+        else
+            h->mb.cache.i_cbp_left = cbp[ltop];
+
+        /* load intra4x4 */
+        h->mb.cache.intra4x4_pred_mode[x264_scan8[ 0] - 1] = i4x4[ltop][left_index_table->intra[0]];
+        h->mb.cache.intra4x4_pred_mode[x264_scan8[ 2] - 1] = i4x4[ltop][left_index_table->intra[1]];
+        h->mb.cache.intra4x4_pred_mode[x264_scan8[ 8] - 1] = i4x4[lbot][left_index_table->intra[2]];
+        h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = i4x4[lbot][left_index_table->intra[3]];
+
+        /* load non_zero_count */
+        h->mb.cache.non_zero_count[x264_scan8[ 0] - 1] = nnz[ltop][left_index_table->nnz[0]];
+        h->mb.cache.non_zero_count[x264_scan8[ 2] - 1] = nnz[ltop][left_index_table->nnz[1]];
+        h->mb.cache.non_zero_count[x264_scan8[ 8] - 1] = nnz[lbot][left_index_table->nnz[2]];
+        h->mb.cache.non_zero_count[x264_scan8[10] - 1] = nnz[lbot][left_index_table->nnz[3]];
+
+        if( CHROMA_FORMAT >= CHROMA_422 )
+        {
+            int offset = (4>>CHROMA_H_SHIFT) - 4;
+            h->mb.cache.non_zero_count[x264_scan8[16+ 0] - 1] = nnz[ltop][left_index_table->nnz[0]+16+offset];
+            h->mb.cache.non_zero_count[x264_scan8[16+ 2] - 1] = nnz[ltop][left_index_table->nnz[1]+16+offset];
+            h->mb.cache.non_zero_count[x264_scan8[16+ 8] - 1] = nnz[lbot][left_index_table->nnz[2]+16+offset];
+            h->mb.cache.non_zero_count[x264_scan8[16+10] - 1] = nnz[lbot][left_index_table->nnz[3]+16+offset];
+            h->mb.cache.non_zero_count[x264_scan8[32+ 0] - 1] = nnz[ltop][left_index_table->nnz[0]+32+offset];
+            h->mb.cache.non_zero_count[x264_scan8[32+ 2] - 1] = nnz[ltop][left_index_table->nnz[1]+32+offset];
+            h->mb.cache.non_zero_count[x264_scan8[32+ 8] - 1] = nnz[lbot][left_index_table->nnz[2]+32+offset];
+            h->mb.cache.non_zero_count[x264_scan8[32+10] - 1] = nnz[lbot][left_index_table->nnz[3]+32+offset];
+        }
+        else
+        {
+            h->mb.cache.non_zero_count[x264_scan8[16+ 0] - 1] = nnz[ltop][left_index_table->nnz_chroma[0]];
+            h->mb.cache.non_zero_count[x264_scan8[16+ 2] - 1] = nnz[lbot][left_index_table->nnz_chroma[1]];
+            h->mb.cache.non_zero_count[x264_scan8[32+ 0] - 1] = nnz[ltop][left_index_table->nnz_chroma[2]];
+            h->mb.cache.non_zero_count[x264_scan8[32+ 2] - 1] = nnz[lbot][left_index_table->nnz_chroma[3]];
+        }
     }
     else
-        h->mb.i_mb_type_topright = -1;
-    if( i_mb_x > 0 && i_top_xy - 1 >= h->sh.i_first_mb )
     {
-        h->mb.i_neighbour |= MB_TOPLEFT;
-        h->mb.i_mb_type_topleft = h->mb.type[ i_top_xy - 1 ];
-        if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_topleft ) )
-            h->mb.i_neighbour_intra |= MB_TOPLEFT;
+        h->mb.cache.i_cbp_left = -1;
+
+        h->mb.cache.intra4x4_pred_mode[x264_scan8[ 0] - 1] =
+        h->mb.cache.intra4x4_pred_mode[x264_scan8[ 2] - 1] =
+        h->mb.cache.intra4x4_pred_mode[x264_scan8[ 8] - 1] =
+        h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = -1;
+
+        /* load non_zero_count */
+        h->mb.cache.non_zero_count[x264_scan8[ 0] - 1] =
+        h->mb.cache.non_zero_count[x264_scan8[ 2] - 1] =
+        h->mb.cache.non_zero_count[x264_scan8[ 8] - 1] =
+        h->mb.cache.non_zero_count[x264_scan8[10] - 1] =
+        h->mb.cache.non_zero_count[x264_scan8[16+ 0] - 1] =
+        h->mb.cache.non_zero_count[x264_scan8[16+ 2] - 1] =
+        h->mb.cache.non_zero_count[x264_scan8[32+ 0] - 1] =
+        h->mb.cache.non_zero_count[x264_scan8[32+ 2] - 1] = 0x80;
+        if( CHROMA_FORMAT >= CHROMA_422 )
+        {
+            h->mb.cache.non_zero_count[x264_scan8[16+ 8] - 1] =
+            h->mb.cache.non_zero_count[x264_scan8[16+10] - 1] =
+            h->mb.cache.non_zero_count[x264_scan8[32+ 8] - 1] =
+            h->mb.cache.non_zero_count[x264_scan8[32+10] - 1] = 0x80;
+        }
     }
-    else
-        h->mb.i_mb_type_topleft = -1;
 
     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]  );
+            ( (h->mb.i_neighbour & MB_LEFT) && h->mb.mb_transform_size[left[0]] )
+          + ( (h->mb.i_neighbour & MB_TOP) && h->mb.mb_transform_size[top]  );
     }
 
-    if( h->sh.b_mbaff )
+    if( 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);
+        h->mb.pic.i_fref[0] = h->i_ref[0] << MB_INTERLACED;
+        h->mb.pic.i_fref[1] = h->i_ref[1] << MB_INTERLACED;
     }
 
-    if( !h->mb.b_interlaced && !h->mb.b_reencode_mb )
+    if( !b_mbaff )
+    {
+        x264_copy_column8( h->mb.pic.p_fdec[0]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+ 4*FDEC_STRIDE );
+        x264_copy_column8( h->mb.pic.p_fdec[0]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+12*FDEC_STRIDE );
+        x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 0, 0, 0 );
+        if( CHROMA444 )
+        {
+            x264_copy_column8( h->mb.pic.p_fdec[1]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[1]+15+ 4*FDEC_STRIDE );
+            x264_copy_column8( h->mb.pic.p_fdec[1]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[1]+15+12*FDEC_STRIDE );
+            x264_copy_column8( h->mb.pic.p_fdec[2]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[2]+15+ 4*FDEC_STRIDE );
+            x264_copy_column8( h->mb.pic.p_fdec[2]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[2]+15+12*FDEC_STRIDE );
+            x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 1, 0, 0 );
+            x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 2, 0, 0 );
+        }
+        else
+        {
+            x264_copy_column8( h->mb.pic.p_fdec[1]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[1]+ 7+ 4*FDEC_STRIDE );
+            x264_copy_column8( h->mb.pic.p_fdec[2]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[2]+ 7+ 4*FDEC_STRIDE );
+            if( CHROMA_FORMAT == CHROMA_422 )
+            {
+                x264_copy_column8( h->mb.pic.p_fdec[1]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[1]+ 7+12*FDEC_STRIDE );
+                x264_copy_column8( h->mb.pic.p_fdec[2]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[2]+ 7+12*FDEC_STRIDE );
+            }
+            x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 1, 1, 0 );
+        }
+    }
+    else
     {
-        copy_column8( h->mb.pic.p_fdec[0]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+ 4*FDEC_STRIDE );
-        copy_column8( h->mb.pic.p_fdec[0]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+12*FDEC_STRIDE );
-        copy_column8( h->mb.pic.p_fdec[1]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[1]+ 7+ 4*FDEC_STRIDE );
-        copy_column8( h->mb.pic.p_fdec[2]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[2]+ 7+ 4*FDEC_STRIDE );
+        x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 0, 0, 1 );
+        if( CHROMA444 )
+        {
+            x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 1, 0, 1 );
+            x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 2, 0, 1 );
+        }
+        else
+            x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 1, 1, 1 );
     }
 
-    /* load picture pointers */
-    x264_macroblock_load_pic_pointers( h, i_mb_x, i_mb_y, 0 );
-    x264_macroblock_load_pic_pointers( h, i_mb_x, i_mb_y, 1 );
-    x264_macroblock_load_pic_pointers( h, i_mb_x, i_mb_y, 2 );
-
     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] )];
+        int offset = 16 * (mb_x + mb_y * h->fdec->i_stride[0]);
+        for( int list = 0; list < 2; list++ )
+            for( int i = 0; i < h->mb.pic.i_fref[list]; i++ )
+                h->mb.pic.p_integral[list][i] = &h->fref[list][i]->integral[offset];
     }
 
-    x264_prefetch_fenc( h, h->fenc, i_mb_x, i_mb_y );
+    x264_prefetch_fenc( h, h->fenc, mb_x, mb_y );
 
     /* load ref/mv/mvd */
-    if( h->sh.i_type != SLICE_TYPE_I )
+    for( int l = 0; l < lists; l++ )
     {
-        const int s8x8 = h->mb.i_b8_stride;
-        const int s4x4 = h->mb.i_b4_stride;
+        int16_t (*mv)[2] = h->mb.mv[l];
+        int8_t *ref = h->mb.ref[l];
 
-        int i_list;
+        int i8 = x264_scan8[0] - 1 - 1*8;
+        if( h->mb.i_neighbour & MB_TOPLEFT )
+        {
+            int ir = b_mbaff ? 2*(s8x8*h->mb.i_mb_topleft_y + mb_x-1)+1+s8x8 : top_8x8 - 1;
+            int iv = b_mbaff ? 4*(s4x4*h->mb.i_mb_topleft_y + mb_x-1)+3+3*s4x4 : top_4x4 - 1;
+            if( b_mbaff && h->mb.topleft_partition )
+            {
+                /* Take motion vector from the middle of macroblock instead of
+                 * the bottom right as usual. */
+                iv -= 2*s4x4;
+                ir -= s8x8;
+            }
+            h->mb.cache.ref[l][i8] = ref[ir];
+            CP32( h->mb.cache.mv[l][i8], mv[iv] );
+        }
+        else
+        {
+            h->mb.cache.ref[l][i8] = -2;
+            M32( h->mb.cache.mv[l][i8] ) = 0;
+        }
+
+        i8 = x264_scan8[0] - 8;
+        if( h->mb.i_neighbour & MB_TOP )
+        {
+            h->mb.cache.ref[l][i8+0] =
+            h->mb.cache.ref[l][i8+1] = ref[top_8x8 + 0];
+            h->mb.cache.ref[l][i8+2] =
+            h->mb.cache.ref[l][i8+3] = ref[top_8x8 + 1];
+            CP128( h->mb.cache.mv[l][i8], mv[top_4x4] );
+        }
+        else
+        {
+            M128( h->mb.cache.mv[l][i8] ) = M128_ZERO;
+            M32( &h->mb.cache.ref[l][i8] ) = (uint8_t)(-2) * 0x01010101U;
+        }
 
-        for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2  : 1 ); i_list++ )
+        i8 = x264_scan8[0] + 4 - 1*8;
+        if( h->mb.i_neighbour & MB_TOPRIGHT )
         {
-            /*
-            h->mb.cache.ref[i_list][x264_scan8[5 ]+1] =
-            h->mb.cache.ref[i_list][x264_scan8[7 ]+1] =
-            h->mb.cache.ref[i_list][x264_scan8[13]+1] = -2;
-            */
+            int ir = b_mbaff ? 2*(s8x8*h->mb.i_mb_topright_y + (mb_x+1))+s8x8 : top_8x8 + 2;
+            int iv = b_mbaff ? 4*(s4x4*h->mb.i_mb_topright_y + (mb_x+1))+3*s4x4 : top_4x4 + 4;
+            h->mb.cache.ref[l][i8] = ref[ir];
+            CP32( h->mb.cache.mv[l][i8], mv[iv] );
+        }
+        else
+             h->mb.cache.ref[l][i8] = -2;
 
-            if( h->mb.i_neighbour & MB_TOPLEFT )
+        i8 = x264_scan8[0] - 1;
+        if( h->mb.i_neighbour & MB_LEFT )
+        {
+            if( b_mbaff )
             {
-                const int i8 = x264_scan8[0] - 1 - 1*8;
-                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];
-                CP32( h->mb.cache.mv[i_list][i8], h->mb.mv[i_list][iv] );
+                h->mb.cache.ref[l][i8+0*8] = ref[h->mb.left_b8[LTOP] + 1 + s8x8*left_index_table->ref[0]];
+                h->mb.cache.ref[l][i8+1*8] = ref[h->mb.left_b8[LTOP] + 1 + s8x8*left_index_table->ref[1]];
+                h->mb.cache.ref[l][i8+2*8] = ref[h->mb.left_b8[LBOT] + 1 + s8x8*left_index_table->ref[2]];
+                h->mb.cache.ref[l][i8+3*8] = ref[h->mb.left_b8[LBOT] + 1 + s8x8*left_index_table->ref[3]];
+
+                CP32( h->mb.cache.mv[l][i8+0*8], mv[h->mb.left_b4[LTOP] + 3 + s4x4*left_index_table->mv[0]] );
+                CP32( h->mb.cache.mv[l][i8+1*8], mv[h->mb.left_b4[LTOP] + 3 + s4x4*left_index_table->mv[1]] );
+                CP32( h->mb.cache.mv[l][i8+2*8], mv[h->mb.left_b4[LBOT] + 3 + s4x4*left_index_table->mv[2]] );
+                CP32( h->mb.cache.mv[l][i8+3*8], mv[h->mb.left_b4[LBOT] + 3 + s4x4*left_index_table->mv[3]] );
             }
             else
             {
-                const int i8 = x264_scan8[0] - 1 - 1*8;
-                h->mb.cache.ref[i_list][i8] = -2;
-                M32( h->mb.cache.mv[i_list][i8] ) = 0;
+                const int ir = h->mb.i_b8_xy - 1;
+                const int iv = h->mb.i_b4_xy - 1;
+                h->mb.cache.ref[l][i8+0*8] =
+                h->mb.cache.ref[l][i8+1*8] = ref[ir + 0*s8x8];
+                h->mb.cache.ref[l][i8+2*8] =
+                h->mb.cache.ref[l][i8+3*8] = ref[ir + 1*s8x8];
+
+                CP32( h->mb.cache.mv[l][i8+0*8], mv[iv + 0*s4x4] );
+                CP32( h->mb.cache.mv[l][i8+1*8], mv[iv + 1*s4x4] );
+                CP32( h->mb.cache.mv[l][i8+2*8], mv[iv + 2*s4x4] );
+                CP32( h->mb.cache.mv[l][i8+3*8], mv[iv + 3*s4x4] );
             }
+        }
+        else
+        {
+            for( int i = 0; i < 4; i++ )
+            {
+                h->mb.cache.ref[l][i8+i*8] = -2;
+                M32( h->mb.cache.mv[l][i8+i*8] ) = 0;
+            }
+        }
+
+        /* Extra logic for top right mv in mbaff.
+         * . . . d  . . a .
+         * . . . e  . . . .
+         * . . . f  b . c .
+         * . . . .  . . . .
+         *
+         * If the top right of the 4x4 partitions labeled a, b and c in the
+         * above diagram do not exist, but the entries d, e and f exist (in
+         * the macroblock to the left) then use those instead.
+         */
+        if( b_mbaff && (h->mb.i_neighbour & MB_LEFT) )
+        {
+            if( MB_INTERLACED && !h->mb.field[h->mb.i_mb_xy-1] )
+            {
+                h->mb.cache.topright_ref[l][0] = ref[h->mb.left_b8[0] + 1 + s8x8*0];
+                h->mb.cache.topright_ref[l][1] = ref[h->mb.left_b8[0] + 1 + s8x8*1];
+                h->mb.cache.topright_ref[l][2] = ref[h->mb.left_b8[1] + 1 + s8x8*0];
+                CP32( h->mb.cache.topright_mv[l][0], mv[h->mb.left_b4[0] + 3 + s4x4*(left_index_table->mv[0]+1)] );
+                CP32( h->mb.cache.topright_mv[l][1], mv[h->mb.left_b4[0] + 3 + s4x4*(left_index_table->mv[1]+1)] );
+                CP32( h->mb.cache.topright_mv[l][2], mv[h->mb.left_b4[1] + 3 + s4x4*(left_index_table->mv[2]+1)] );
+            }
+            else if( !MB_INTERLACED && h->mb.field[h->mb.i_mb_xy-1] )
+            {
+                // Looking at the bottom field so always take the bottom macroblock of the pair.
+                h->mb.cache.topright_ref[l][0] = ref[h->mb.left_b8[0] + 1 + s8x8*2 + s8x8*left_index_table->ref[0]];
+                h->mb.cache.topright_ref[l][1] = ref[h->mb.left_b8[0] + 1 + s8x8*2 + s8x8*left_index_table->ref[0]];
+                h->mb.cache.topright_ref[l][2] = ref[h->mb.left_b8[0] + 1 + s8x8*2 + s8x8*left_index_table->ref[2]];
+                CP32( h->mb.cache.topright_mv[l][0], mv[h->mb.left_b4[0] + 3 + s4x4*4 + s4x4*left_index_table->mv[0]] );
+                CP32( h->mb.cache.topright_mv[l][1], mv[h->mb.left_b4[0] + 3 + s4x4*4 + s4x4*left_index_table->mv[1]] );
+                CP32( h->mb.cache.topright_mv[l][2], mv[h->mb.left_b4[0] + 3 + s4x4*4 + s4x4*left_index_table->mv[2]] );
+            }
+        }
 
+        if( h->param.b_cabac )
+        {
+            uint8_t (*mvd)[8][2] = h->mb.mvd[l];
             if( h->mb.i_neighbour & MB_TOP )
+                CP64( h->mb.cache.mvd[l][x264_scan8[0] - 8], mvd[top][0] );
+            else
+                M64( h->mb.cache.mvd[l][x264_scan8[0] - 8] ) = 0;
+
+            if( h->mb.i_neighbour & MB_LEFT && (!b_mbaff || h->mb.cache.ref[l][x264_scan8[0]-1] >= 0) )
             {
-                const int i8 = x264_scan8[0] - 8;
-                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] =
-                h->mb.cache.ref[i_list][i8+3] = h->mb.ref[i_list][ir + 1];
-                CP64( h->mb.cache.mv[i_list][i8+0], h->mb.mv[i_list][iv+0] );
-                CP64( h->mb.cache.mv[i_list][i8+2], h->mb.mv[i_list][iv+2] );
+                CP16( h->mb.cache.mvd[l][x264_scan8[0 ] - 1], mvd[left[LTOP]][left_index_table->intra[0]] );
+                CP16( h->mb.cache.mvd[l][x264_scan8[2 ] - 1], mvd[left[LTOP]][left_index_table->intra[1]] );
             }
             else
             {
-                const int i8 = x264_scan8[0] - 8;
-                M64( h->mb.cache.mv[i_list][i8+0] ) = 0;
-                M64( h->mb.cache.mv[i_list][i8+2] ) = 0;
-                M32( &h->mb.cache.ref[i_list][i8] ) = (uint8_t)(-2) * 0x01010101U;
+                M16( h->mb.cache.mvd[l][x264_scan8[0]-1+0*8] ) = 0;
+                M16( h->mb.cache.mvd[l][x264_scan8[0]-1+1*8] ) = 0;
             }
-
-            if( h->mb.i_neighbour & MB_TOPRIGHT )
+            if( h->mb.i_neighbour & MB_LEFT && (!b_mbaff || h->mb.cache.ref[l][x264_scan8[0]-1+2*8] >=0) )
             {
-                const int i8 = x264_scan8[0] + 4 - 1*8;
-                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];
-                CP32( h->mb.cache.mv[i_list][i8], h->mb.mv[i_list][iv] );
+                CP16( h->mb.cache.mvd[l][x264_scan8[8 ] - 1], mvd[left[LBOT]][left_index_table->intra[2]] );
+                CP16( h->mb.cache.mvd[l][x264_scan8[10] - 1], mvd[left[LBOT]][left_index_table->intra[3]] );
             }
             else
             {
-                const int i8 = x264_scan8[0] + 4 - 1*8;
-                h->mb.cache.ref[i_list][i8] = -2;
-                M32( h->mb.cache.mv[i_list][i8] ) = 0;
+                M16( h->mb.cache.mvd[l][x264_scan8[0]-1+2*8] ) = 0;
+                M16( h->mb.cache.mvd[l][x264_scan8[0]-1+3*8] ) = 0;
             }
+        }
 
-            if( h->mb.i_neighbour & MB_LEFT )
+        /* If motion vectors are cached from frame macroblocks but this
+         * macroblock is a field macroblock then the motion vector must be
+         * halved. Similarly, motion vectors from field macroblocks are doubled. */
+        if( b_mbaff )
+        {
+#define MAP_MVS\
+                if( FIELD_DIFFERENT(h->mb.i_mb_topleft_xy) )\
+                    MAP_F2F(mv, ref, x264_scan8[0] - 1 - 1*8)\
+                if( FIELD_DIFFERENT(top) )\
+                {\
+                    MAP_F2F(mv, ref, x264_scan8[0] + 0 - 1*8)\
+                    MAP_F2F(mv, ref, x264_scan8[0] + 1 - 1*8)\
+                    MAP_F2F(mv, ref, x264_scan8[0] + 2 - 1*8)\
+                    MAP_F2F(mv, ref, x264_scan8[0] + 3 - 1*8)\
+                }\
+                if( FIELD_DIFFERENT(h->mb.i_mb_topright_xy) )\
+                    MAP_F2F(mv, ref, x264_scan8[0] + 4 - 1*8)\
+                if( FIELD_DIFFERENT(left[0]) )\
+                {\
+                    MAP_F2F(mv, ref, x264_scan8[0] - 1 + 0*8)\
+                    MAP_F2F(mv, ref, x264_scan8[0] - 1 + 1*8)\
+                    MAP_F2F(mv, ref, x264_scan8[0] - 1 + 2*8)\
+                    MAP_F2F(mv, ref, x264_scan8[0] - 1 + 3*8)\
+                    MAP_F2F(topright_mv, topright_ref, 0)\
+                    MAP_F2F(topright_mv, topright_ref, 1)\
+                    MAP_F2F(topright_mv, topright_ref, 2)\
+                }
+
+            if( MB_INTERLACED )
             {
-                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] =
-                h->mb.cache.ref[i_list][i8+3*8] = h->mb.ref[i_list][ir + 1*s8x8];
-
-                CP32( h->mb.cache.mv[i_list][i8+0*8], h->mb.mv[i_list][iv + 0*s4x4] );
-                CP32( h->mb.cache.mv[i_list][i8+1*8], h->mb.mv[i_list][iv + 1*s4x4] );
-                CP32( h->mb.cache.mv[i_list][i8+2*8], h->mb.mv[i_list][iv + 2*s4x4] );
-                CP32( h->mb.cache.mv[i_list][i8+3*8], h->mb.mv[i_list][iv + 3*s4x4] );
+#define FIELD_DIFFERENT(macroblock) (macroblock >= 0 && !h->mb.field[macroblock])
+#define MAP_F2F(varmv, varref, index)\
+                if( h->mb.cache.varref[l][index] >= 0 )\
+                {\
+                    h->mb.cache.varref[l][index] <<= 1;\
+                    h->mb.cache.varmv[l][index][1] /= 2;\
+                    h->mb.cache.mvd[l][index][1] >>= 1;\
+                }
+                MAP_MVS
+#undef MAP_F2F
+#undef FIELD_DIFFERENT
             }
             else
             {
-                const int i8 = x264_scan8[0] - 1;
-                for( i = 0; i < 4; i++ )
-                {
-                    h->mb.cache.ref[i_list][i8+i*8] = -2;
-                    M32( h->mb.cache.mv[i_list][i8+i*8] ) = 0;
+#define FIELD_DIFFERENT(macroblock) (macroblock >= 0 && h->mb.field[macroblock])
+#define MAP_F2F(varmv, varref, index)\
+                if( h->mb.cache.varref[l][index] >= 0 )\
+                {\
+                    h->mb.cache.varref[l][index] >>= 1;\
+                    h->mb.cache.varmv[l][index][1] <<= 1;\
+                    h->mb.cache.mvd[l][index][1] <<= 1;\
                 }
+                MAP_MVS
+#undef MAP_F2F
+#undef FIELD_DIFFERENT
             }
+        }
+    }
+
+    if( b_mbaff && mb_x == 0 && !(mb_y&1) && mb_y > 0 )
+        h->mb.field_decoding_flag = h->mb.field[h->mb.i_mb_xy - h->mb.i_mb_stride];
 
-            if( h->param.b_cabac )
+    /* Check whether skip here would cause decoder to predict interlace mode incorrectly.
+     * FIXME: It might be better to change the interlace type rather than forcing a skip to be non-skip. */
+    h->mb.b_allow_skip = 1;
+    if( b_mbaff )
+    {
+        if( MB_INTERLACED != h->mb.field_decoding_flag &&
+            h->mb.i_mb_prev_xy >= 0 && IS_SKIP(h->mb.type[h->mb.i_mb_prev_xy]) )
+            h->mb.b_allow_skip = 0;
+        if( (mb_y&1) && IS_SKIP(h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride]) )
+        {
+            if( h->mb.i_neighbour & MB_LEFT )
             {
-                if( i_top_type >= 0 )
-                    CP64( h->mb.cache.mvd[i_list][x264_scan8[0]-8], h->mb.mvd[i_list][i_top_xy*8] );
-                else
-                    M64( h->mb.cache.mvd[i_list][x264_scan8[0]-8] ) = 0;
+                if( h->mb.field[h->mb.i_mb_xy - 1] != MB_INTERLACED )
+                    h->mb.b_allow_skip = 0;
+            }
+            else if( h->mb.i_neighbour & MB_TOP )
+            {
+                if( h->mb.field[h->mb.i_mb_top_xy] != MB_INTERLACED )
+                    h->mb.b_allow_skip = 0;
+            }
+            else // Frame mb pair is predicted
+            {
+                if( MB_INTERLACED )
+                    h->mb.b_allow_skip = 0;
+            }
+        }
+    }
 
-                if( i_left_type >= 0 )
-                {
-                    CP16( h->mb.cache.mvd[i_list][x264_scan8[0]-1+0*8], h->mb.mvd[i_list][i_left_xy*8+4] );
-                    CP16( h->mb.cache.mvd[i_list][x264_scan8[0]-1+1*8], h->mb.mvd[i_list][i_left_xy*8+5] );
-                    CP16( h->mb.cache.mvd[i_list][x264_scan8[0]-1+2*8], h->mb.mvd[i_list][i_left_xy*8+6] );
-                    CP16( h->mb.cache.mvd[i_list][x264_scan8[0]-1+3*8], h->mb.mvd[i_list][i_left_xy*8+3] );
-                }
-                else
-                    for( i = 0; i < 4; i++ )
-                        M16( h->mb.cache.mvd[i_list][x264_scan8[0]-1+i*8] ) = 0;
+    if( h->param.b_cabac )
+    {
+        if( b_mbaff )
+        {
+            int left_xy, top_xy;
+            /* Neighbours here are calculated based on field_decoding_flag */
+            int mb_xy = mb_x + (mb_y&~1)*h->mb.i_mb_stride;
+            left_xy = mb_xy - 1;
+            if( (mb_y&1) && mb_x > 0 && h->mb.field_decoding_flag == h->mb.field[left_xy] )
+                left_xy += h->mb.i_mb_stride;
+            if( h->mb.field_decoding_flag )
+            {
+                top_xy = mb_xy - h->mb.i_mb_stride;
+                if( !(mb_y&1) && top_xy >= 0 && h->mb.slice_table[top_xy] == h->sh.i_first_mb && h->mb.field[top_xy] )
+                    top_xy -= h->mb.i_mb_stride;
             }
+            else
+                top_xy = mb_x + (mb_y-1)*h->mb.i_mb_stride;
+
+            h->mb.cache.i_neighbour_skip =   (mb_x >  0 && h->mb.slice_table[left_xy] == h->sh.i_first_mb && !IS_SKIP( h->mb.type[left_xy] ))
+                                         + (top_xy >= 0 && h->mb.slice_table[top_xy]  == h->sh.i_first_mb && !IS_SKIP( h->mb.type[top_xy] ));
+        }
+        else
+        {
+            h->mb.cache.i_neighbour_skip = ((h->mb.i_neighbour & MB_LEFT) && !IS_SKIP( h->mb.i_mb_type_left[0] ))
+                                         + ((h->mb.i_neighbour & MB_TOP)  && !IS_SKIP( h->mb.i_mb_type_top ));
         }
+    }
 
-        /* load skip */
-        if( h->sh.i_type == SLICE_TYPE_B )
+    /* load skip */
+    if( h->sh.i_type == SLICE_TYPE_B )
+    {
+        h->mb.bipred_weight = h->mb.bipred_weight_buf[MB_INTERLACED][MB_INTERLACED&(mb_y&1)];
+        h->mb.dist_scale_factor = h->mb.dist_scale_factor_buf[MB_INTERLACED][MB_INTERLACED&(mb_y&1)];
+        if( h->param.b_cabac )
         {
-            h->mb.bipred_weight = h->mb.bipred_weight_buf[h->mb.b_interlaced&(i_mb_y&1)];
-            h->mb.dist_scale_factor = h->mb.dist_scale_factor_buf[h->mb.b_interlaced&(i_mb_y&1)];
-            if( h->param.b_cabac )
+            uint8_t skipbp;
+            x264_macroblock_cache_skip( h, 0, 0, 4, 4, 0 );
+            if( b_mbaff )
             {
-                uint8_t skipbp;
-                x264_macroblock_cache_skip( h, 0, 0, 4, 4, 0 );
-                skipbp = i_left_type >= 0 ? h->mb.skipbp[i_left_xy] : 0;
+                skipbp = (h->mb.i_neighbour & MB_LEFT) ? h->mb.skipbp[left[LTOP]] : 0;
+                h->mb.cache.skip[x264_scan8[0] - 1] = (skipbp >> (1+(left_index_table->mv[0]&~1))) & 1;
+                skipbp = (h->mb.i_neighbour & MB_LEFT) ? h->mb.skipbp[left[LBOT]] : 0;
+                h->mb.cache.skip[x264_scan8[8] - 1] = (skipbp >> (1+(left_index_table->mv[2]&~1))) & 1;
+            }
+            else
+            {
+                skipbp = (h->mb.i_neighbour & MB_LEFT) ? h->mb.skipbp[left[0]] : 0;
                 h->mb.cache.skip[x264_scan8[0] - 1] = skipbp & 0x2;
                 h->mb.cache.skip[x264_scan8[8] - 1] = skipbp & 0x8;
-                skipbp = i_top_type >= 0 ? h->mb.skipbp[i_top_xy] : 0;
-                h->mb.cache.skip[x264_scan8[0] - 8] = skipbp & 0x4;
-                h->mb.cache.skip[x264_scan8[4] - 8] = skipbp & 0x8;
             }
+            skipbp = (h->mb.i_neighbour & MB_TOP) ? h->mb.skipbp[top] : 0;
+            h->mb.cache.skip[x264_scan8[0] - 8] = skipbp & 0x4;
+            h->mb.cache.skip[x264_scan8[4] - 8] = skipbp & 0x8;
         }
-
-        if( h->sh.i_type == SLICE_TYPE_P )
-            x264_mb_predict_mv_pskip( h, h->mb.cache.pskip_mv );
     }
 
+    if( h->sh.i_type == SLICE_TYPE_P )
+        x264_mb_predict_mv_pskip( h, h->mb.cache.pskip_mv );
+
     h->mb.i_neighbour4[0] =
     h->mb.i_neighbour8[0] = (h->mb.i_neighbour_intra & (MB_TOP|MB_LEFT|MB_TOPLEFT))
                             | ((h->mb.i_neighbour_intra & MB_TOP) ? MB_TOPRIGHT : 0);
@@ -1264,17 +1359,326 @@ void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
                             | ((h->mb.i_neighbour_intra & MB_TOP) ? MB_TOP|MB_TOPLEFT : 0);
 }
 
-static void ALWAYS_INLINE x264_macroblock_store_pic( x264_t *h, int i )
+void x264_macroblock_cache_load_progressive( x264_t *h, int mb_x, int mb_y )
+{
+    x264_macroblock_cache_load( h, mb_x, mb_y, 0 );
+}
+
+void x264_macroblock_cache_load_interlaced( x264_t *h, int mb_x, int mb_y )
+{
+    x264_macroblock_cache_load( h, mb_x, mb_y, 1 );
+}
+
+static void x264_macroblock_deblock_strength_mbaff( x264_t *h, uint8_t (*bs)[8][4] )
+{
+    if( (h->mb.i_neighbour & MB_LEFT) && h->mb.field[h->mb.i_mb_left_xy[0]] != MB_INTERLACED )
+    {
+        static const uint8_t offset[2][2][8] =
+        {   {   { 0, 0, 0, 0, 1, 1, 1, 1 },
+                { 2, 2, 2, 2, 3, 3, 3, 3 }, },
+            {   { 0, 1, 2, 3, 0, 1, 2, 3 },
+                { 0, 1, 2, 3, 0, 1, 2, 3 }, }
+        };
+        ALIGNED_ARRAY_8( uint8_t, tmpbs, [8] );
+
+        const uint8_t *off = offset[MB_INTERLACED][h->mb.i_mb_y&1];
+        uint8_t (*nnz)[48] = h->mb.non_zero_count;
+
+        for( int i = 0; i < 8; i++ )
+        {
+            int left = h->mb.i_mb_left_xy[MB_INTERLACED ? i>>2 : i&1];
+            int nnz_this = h->mb.cache.non_zero_count[x264_scan8[0]+8*(i>>1)];
+            int nnz_left = nnz[left][3 + 4*off[i]];
+            if( !h->param.b_cabac && h->pps->b_transform_8x8_mode )
+            {
+                int j = off[i]&~1;
+                if( h->mb.mb_transform_size[left] )
+                    nnz_left = !!(M16( &nnz[left][2+4*j] ) | M16( &nnz[left][2+4*(1+j)] ));
+            }
+            tmpbs[i] = (nnz_left || nnz_this) ? 2 : 1;
+        }
+
+        if( MB_INTERLACED )
+        {
+            CP32( bs[0][0], &tmpbs[0] );
+            CP32( bs[0][4], &tmpbs[4] );
+        }
+        else
+        {
+            for( int i = 0; i < 4; i++ ) bs[0][0][i] = tmpbs[2*i];
+            for( int i = 0; i < 4; i++ ) bs[0][4][i] = tmpbs[1+2*i];
+        }
+    }
+
+    if( (h->mb.i_neighbour & MB_TOP) && MB_INTERLACED != h->mb.field[h->mb.i_mb_top_xy] )
+    {
+        if( !(h->mb.i_mb_y&1) && !MB_INTERLACED )
+        {
+            /* Need to filter both fields (even for frame macroblocks).
+             * Filter top two rows using the top macroblock of the above
+             * pair and then the bottom one. */
+            int mbn_xy = h->mb.i_mb_xy - 2 * h->mb.i_mb_stride;
+            uint8_t *nnz_cur = &h->mb.cache.non_zero_count[x264_scan8[0]];
+
+            for( int j = 0; j < 2; j++, mbn_xy += h->mb.i_mb_stride )
+            {
+                uint8_t (*nnz)[48] = h->mb.non_zero_count;
+
+                ALIGNED_4( uint8_t nnz_top[4] );
+                CP32( nnz_top, &nnz[mbn_xy][3*4] );
+
+                if( !h->param.b_cabac && h->pps->b_transform_8x8_mode && h->mb.mb_transform_size[mbn_xy] )
+                {
+                    nnz_top[0] = nnz_top[1] = M16( &nnz[mbn_xy][ 8] ) || M16( &nnz[mbn_xy][12] );
+                    nnz_top[2] = nnz_top[3] = M16( &nnz[mbn_xy][10] ) || M16( &nnz[mbn_xy][14] );
+                }
+
+                for( int i = 0; i < 4; i++ )
+                    bs[1][4*j][i] = (nnz_cur[i] || nnz_top[i]) ? 2 : 1;
+            }
+        }
+        else
+            for( int i = 0; i < 4; i++ )
+                bs[1][0][i] = X264_MAX( bs[1][0][i], 1 );
+    }
+}
+
+void x264_macroblock_deblock_strength( x264_t *h )
+{
+    uint8_t (*bs)[8][4] = h->mb.cache.deblock_strength;
+    if( IS_INTRA( h->mb.i_type ) )
+    {
+        memset( bs[0][1], 3, 3*4*sizeof(uint8_t) );
+        memset( bs[1][1], 3, 3*4*sizeof(uint8_t) );
+        return;
+    }
+
+    /* Early termination: in this case, nnz guarantees all edges use strength 2.*/
+    if( h->mb.b_transform_8x8 && !CHROMA444 )
+    {
+        int cbp_mask = 0xf >> CHROMA_V_SHIFT;
+        if( (h->mb.i_cbp_luma&cbp_mask) == cbp_mask )
+        {
+            M32( bs[0][0] ) = 0x02020202;
+            M32( bs[0][2] ) = 0x02020202;
+            M32( bs[0][4] ) = 0x02020202;
+            memset( bs[1][0], 2, 5*4*sizeof(uint8_t) ); /* [1][1] and [1][3] has to be set for 4:2:2 */
+            return;
+        }
+    }
+
+    int neighbour_changed = 0;
+    if( h->sh.i_disable_deblocking_filter_idc != 2 )
+    {
+        neighbour_changed = h->mb.i_neighbour_frame&~h->mb.i_neighbour;
+        h->mb.i_neighbour = h->mb.i_neighbour_frame;
+    }
+
+    /* MBAFF deblock uses different left neighbors from encoding */
+    if( SLICE_MBAFF && (h->mb.i_neighbour & MB_LEFT) && (h->mb.field[h->mb.i_mb_xy - 1] != MB_INTERLACED) )
+    {
+        h->mb.i_mb_left_xy[1] =
+        h->mb.i_mb_left_xy[0] = h->mb.i_mb_xy - 1;
+        if( h->mb.i_mb_y&1 )
+            h->mb.i_mb_left_xy[0] -= h->mb.i_mb_stride;
+        else
+            h->mb.i_mb_left_xy[1] += h->mb.i_mb_stride;
+    }
+
+    /* If we have multiple slices and we're deblocking on slice edges, we
+     * have to reload neighbour data. */
+    if( neighbour_changed )
+    {
+        int top_y = h->mb.i_mb_top_y;
+        int top_8x8 = (2*top_y+1) * h->mb.i_b8_stride + 2*h->mb.i_mb_x;
+        int top_4x4 = (4*top_y+3) * h->mb.i_b4_stride + 4*h->mb.i_mb_x;
+        int s8x8 = h->mb.i_b8_stride;
+        int s4x4 = h->mb.i_b4_stride;
+
+        uint8_t (*nnz)[48] = h->mb.non_zero_count;
+        const x264_left_table_t *left_index_table = SLICE_MBAFF ? h->mb.left_index_table : &left_indices[3];
+
+        if( neighbour_changed & MB_TOP )
+            CP32( &h->mb.cache.non_zero_count[x264_scan8[0] - 8], &nnz[h->mb.i_mb_top_xy][12] );
+
+        if( neighbour_changed & MB_LEFT )
+        {
+            int *left = h->mb.i_mb_left_xy;
+            h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = nnz[left[0]][left_index_table->nnz[0]];
+            h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = nnz[left[0]][left_index_table->nnz[1]];
+            h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = nnz[left[1]][left_index_table->nnz[2]];
+            h->mb.cache.non_zero_count[x264_scan8[10] - 1] = nnz[left[1]][left_index_table->nnz[3]];
+        }
+
+        for( int l = 0; l <= (h->sh.i_type == SLICE_TYPE_B); l++ )
+        {
+            int16_t (*mv)[2] = h->mb.mv[l];
+            int8_t *ref = h->mb.ref[l];
+
+            int i8 = x264_scan8[0] - 8;
+            if( neighbour_changed & MB_TOP )
+            {
+                h->mb.cache.ref[l][i8+0] =
+                h->mb.cache.ref[l][i8+1] = ref[top_8x8 + 0];
+                h->mb.cache.ref[l][i8+2] =
+                h->mb.cache.ref[l][i8+3] = ref[top_8x8 + 1];
+                CP128( h->mb.cache.mv[l][i8], mv[top_4x4] );
+            }
+
+            i8 = x264_scan8[0] - 1;
+            if( neighbour_changed & MB_LEFT )
+            {
+                h->mb.cache.ref[l][i8+0*8] =
+                h->mb.cache.ref[l][i8+1*8] = ref[h->mb.left_b8[0] + 1 + s8x8*left_index_table->ref[0]];
+                h->mb.cache.ref[l][i8+2*8] =
+                h->mb.cache.ref[l][i8+3*8] = ref[h->mb.left_b8[1] + 1 + s8x8*left_index_table->ref[2]];
+
+                CP32( h->mb.cache.mv[l][i8+0*8], mv[h->mb.left_b4[0] + 3 + s4x4*left_index_table->mv[0]] );
+                CP32( h->mb.cache.mv[l][i8+1*8], mv[h->mb.left_b4[0] + 3 + s4x4*left_index_table->mv[1]] );
+                CP32( h->mb.cache.mv[l][i8+2*8], mv[h->mb.left_b4[1] + 3 + s4x4*left_index_table->mv[2]] );
+                CP32( h->mb.cache.mv[l][i8+3*8], mv[h->mb.left_b4[1] + 3 + s4x4*left_index_table->mv[3]] );
+            }
+        }
+    }
+
+    if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART && h->sh.i_type == SLICE_TYPE_P )
+    {
+        /* Handle reference frame duplicates */
+        int i8 = x264_scan8[0] - 8;
+        h->mb.cache.ref[0][i8+0] =
+        h->mb.cache.ref[0][i8+1] = deblock_ref_table(h->mb.cache.ref[0][i8+0]);
+        h->mb.cache.ref[0][i8+2] =
+        h->mb.cache.ref[0][i8+3] = deblock_ref_table(h->mb.cache.ref[0][i8+2]);
+
+        i8 = x264_scan8[0] - 1;
+        h->mb.cache.ref[0][i8+0*8] =
+        h->mb.cache.ref[0][i8+1*8] = deblock_ref_table(h->mb.cache.ref[0][i8+0*8]);
+        h->mb.cache.ref[0][i8+2*8] =
+        h->mb.cache.ref[0][i8+3*8] = deblock_ref_table(h->mb.cache.ref[0][i8+2*8]);
+
+        int ref0 = deblock_ref_table(h->mb.cache.ref[0][x264_scan8[ 0]]);
+        int ref1 = deblock_ref_table(h->mb.cache.ref[0][x264_scan8[ 4]]);
+        int ref2 = deblock_ref_table(h->mb.cache.ref[0][x264_scan8[ 8]]);
+        int ref3 = deblock_ref_table(h->mb.cache.ref[0][x264_scan8[12]]);
+        uint32_t reftop = pack16to32( (uint8_t)ref0, (uint8_t)ref1 ) * 0x0101;
+        uint32_t refbot = pack16to32( (uint8_t)ref2, (uint8_t)ref3 ) * 0x0101;
+
+        M32( &h->mb.cache.ref[0][x264_scan8[0]+8*0] ) = reftop;
+        M32( &h->mb.cache.ref[0][x264_scan8[0]+8*1] ) = reftop;
+        M32( &h->mb.cache.ref[0][x264_scan8[0]+8*2] ) = refbot;
+        M32( &h->mb.cache.ref[0][x264_scan8[0]+8*3] ) = refbot;
+    }
+
+    /* Munge NNZ for cavlc + 8x8dct */
+    if( !h->param.b_cabac && h->pps->b_transform_8x8_mode )
+    {
+        uint8_t (*nnz)[48] = h->mb.non_zero_count;
+        int top = h->mb.i_mb_top_xy;
+        int *left = h->mb.i_mb_left_xy;
+
+        if( (h->mb.i_neighbour & MB_TOP) && h->mb.mb_transform_size[top] )
+        {
+            int i8 = x264_scan8[0] - 8;
+            int nnz_top0 = M16( &nnz[top][8] ) | M16( &nnz[top][12] );
+            int nnz_top1 = M16( &nnz[top][10] ) | M16( &nnz[top][14] );
+            M16( &h->mb.cache.non_zero_count[i8+0] ) = nnz_top0 ? 0x0101 : 0;
+            M16( &h->mb.cache.non_zero_count[i8+2] ) = nnz_top1 ? 0x0101 : 0;
+        }
+
+        if( h->mb.i_neighbour & MB_LEFT )
+        {
+            int i8 = x264_scan8[0] - 1;
+            if( h->mb.mb_transform_size[left[0]] )
+            {
+                int nnz_left0 = M16( &nnz[left[0]][2] ) | M16( &nnz[left[0]][6] );
+                h->mb.cache.non_zero_count[i8+8*0] = !!nnz_left0;
+                h->mb.cache.non_zero_count[i8+8*1] = !!nnz_left0;
+            }
+            if( h->mb.mb_transform_size[left[1]] )
+            {
+                int nnz_left1 = M16( &nnz[left[1]][10] ) | M16( &nnz[left[1]][14] );
+                h->mb.cache.non_zero_count[i8+8*2] = !!nnz_left1;
+                h->mb.cache.non_zero_count[i8+8*3] = !!nnz_left1;
+            }
+        }
+
+        if( h->mb.b_transform_8x8 )
+        {
+            int nnz0 = M16( &h->mb.cache.non_zero_count[x264_scan8[ 0]] ) | M16( &h->mb.cache.non_zero_count[x264_scan8[ 2]] );
+            int nnz1 = M16( &h->mb.cache.non_zero_count[x264_scan8[ 4]] ) | M16( &h->mb.cache.non_zero_count[x264_scan8[ 6]] );
+            int nnz2 = M16( &h->mb.cache.non_zero_count[x264_scan8[ 8]] ) | M16( &h->mb.cache.non_zero_count[x264_scan8[10]] );
+            int nnz3 = M16( &h->mb.cache.non_zero_count[x264_scan8[12]] ) | M16( &h->mb.cache.non_zero_count[x264_scan8[14]] );
+            uint32_t nnztop = pack16to32( !!nnz0, !!nnz1 ) * 0x0101;
+            uint32_t nnzbot = pack16to32( !!nnz2, !!nnz3 ) * 0x0101;
+
+            M32( &h->mb.cache.non_zero_count[x264_scan8[0]+8*0] ) = nnztop;
+            M32( &h->mb.cache.non_zero_count[x264_scan8[0]+8*1] ) = nnztop;
+            M32( &h->mb.cache.non_zero_count[x264_scan8[0]+8*2] ) = nnzbot;
+            M32( &h->mb.cache.non_zero_count[x264_scan8[0]+8*3] ) = nnzbot;
+        }
+    }
+
+    h->loopf.deblock_strength( h->mb.cache.non_zero_count, h->mb.cache.ref, h->mb.cache.mv,
+                               bs, 4 >> MB_INTERLACED, h->sh.i_type == SLICE_TYPE_B );
+
+    if( SLICE_MBAFF )
+        x264_macroblock_deblock_strength_mbaff( h, bs );
+}
+
+static void ALWAYS_INLINE x264_macroblock_store_pic( x264_t *h, int mb_x, int mb_y, int i, int b_chroma, int b_mbaff )
 {
-    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][i_pix_offset], i_stride2,
-        h->mb.pic.p_fdec[i], FDEC_STRIDE, w );
+    int height = b_chroma ? 16>>CHROMA_V_SHIFT : 16;
+    int i_stride = h->fdec->i_stride[i];
+    int i_stride2 = i_stride << (b_mbaff && MB_INTERLACED);
+    int i_pix_offset = (b_mbaff && MB_INTERLACED)
+                     ? 16 * mb_x + height * (mb_y&~1) * i_stride + (mb_y&1) * i_stride
+                     : 16 * mb_x + height * mb_y * i_stride;
+    if( b_chroma )
+        h->mc.store_interleave_chroma( &h->fdec->plane[1][i_pix_offset], i_stride2, h->mb.pic.p_fdec[1], h->mb.pic.p_fdec[2], height );
+    else
+        h->mc.copy[PIXEL_16x16]( &h->fdec->plane[i][i_pix_offset], i_stride2, h->mb.pic.p_fdec[i], FDEC_STRIDE, 16 );
+}
+
+static void ALWAYS_INLINE x264_macroblock_backup_intra( x264_t *h, int mb_x, int mb_y, int b_mbaff )
+{
+    /* In MBAFF we store the last two rows in intra_border_backup[0] and [1].
+     * For progressive mbs this is the bottom two rows, and for interlaced the
+     * bottom row of each field. We also store samples needed for the next
+     * mbpair in intra_border_backup[2]. */
+    int backup_dst = !b_mbaff ? (mb_y&1) : (mb_y&1) ? 1 : MB_INTERLACED ? 0 : 2;
+    memcpy( &h->intra_border_backup[backup_dst][0][mb_x*16  ], h->mb.pic.p_fdec[0]+FDEC_STRIDE*15, 16*sizeof(pixel) );
+    if( CHROMA444 )
+    {
+        memcpy( &h->intra_border_backup[backup_dst][1][mb_x*16  ], h->mb.pic.p_fdec[1]+FDEC_STRIDE*15, 16*sizeof(pixel) );
+        memcpy( &h->intra_border_backup[backup_dst][2][mb_x*16  ], h->mb.pic.p_fdec[2]+FDEC_STRIDE*15, 16*sizeof(pixel) );
+    }
+    else
+    {
+        int backup_src = (15>>CHROMA_V_SHIFT) * FDEC_STRIDE;
+        memcpy( &h->intra_border_backup[backup_dst][1][mb_x*16  ], h->mb.pic.p_fdec[1]+backup_src, 8*sizeof(pixel) );
+        memcpy( &h->intra_border_backup[backup_dst][1][mb_x*16+8], h->mb.pic.p_fdec[2]+backup_src, 8*sizeof(pixel) );
+    }
+    if( b_mbaff )
+    {
+        if( mb_y&1 )
+        {
+            int backup_src = (MB_INTERLACED ? 7 : 14) * FDEC_STRIDE;
+            backup_dst = MB_INTERLACED ? 2 : 0;
+            memcpy( &h->intra_border_backup[backup_dst][0][mb_x*16  ], h->mb.pic.p_fdec[0]+backup_src, 16*sizeof(pixel) );
+            if( CHROMA444 )
+            {
+                memcpy( &h->intra_border_backup[backup_dst][1][mb_x*16  ], h->mb.pic.p_fdec[1]+backup_src, 16*sizeof(pixel) );
+                memcpy( &h->intra_border_backup[backup_dst][2][mb_x*16  ], h->mb.pic.p_fdec[2]+backup_src, 16*sizeof(pixel) );
+            }
+            else
+            {
+                if( CHROMA_FORMAT == CHROMA_420 )
+                    backup_src = (MB_INTERLACED ? 3 : 6) * FDEC_STRIDE;
+                memcpy( &h->intra_border_backup[backup_dst][1][mb_x*16  ], h->mb.pic.p_fdec[1]+backup_src,  8*sizeof(pixel) );
+                memcpy( &h->intra_border_backup[backup_dst][1][mb_x*16+8], h->mb.pic.p_fdec[2]+backup_src,  8*sizeof(pixel) );
+            }
+        }
+    }
 }
 
 void x264_macroblock_cache_save( x264_t *h )
@@ -1286,59 +1690,70 @@ void x264_macroblock_cache_save( x264_t *h )
     const int i_mb_4x4 = h->mb.i_b4_xy;
     const int i_mb_8x8 = h->mb.i_b8_xy;
 
-    /* GCC pessimizes direct stores to heap-allocated 8-bit arrays due to aliasing.*/
+    /* GCC pessimizes direct stores to heap-allocated arrays due to aliasing. */
     /* By only dereferencing them once, we avoid this issue. */
-    int8_t *intra4x4_pred_mode = h->mb.intra4x4_pred_mode[i_mb_xy];
-    uint8_t *non_zero_count = h->mb.non_zero_count[i_mb_xy];
-
-    int y;
+    int8_t *i4x4 = h->mb.intra4x4_pred_mode[i_mb_xy];
+    uint8_t *nnz = h->mb.non_zero_count[i_mb_xy];
 
-    x264_macroblock_store_pic( h, 0 );
-    x264_macroblock_store_pic( h, 1 );
-    x264_macroblock_store_pic( h, 2 );
+    if( SLICE_MBAFF )
+    {
+        x264_macroblock_backup_intra( h, h->mb.i_mb_x, h->mb.i_mb_y, 1 );
+        x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 0, 0, 1 );
+        if( CHROMA444 )
+        {
+            x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 1, 0, 1 );
+            x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 2, 0, 1 );
+        }
+        else
+            x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 1, 1, 1 );
+    }
+    else
+    {
+        x264_macroblock_backup_intra( h, h->mb.i_mb_x, h->mb.i_mb_y, 0 );
+        x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 0, 0, 0 );
+        if( CHROMA444 )
+        {
+            x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 1, 0, 0 );
+            x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 2, 0, 0 );
+        }
+        else
+            x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 1, 1, 0 );
+    }
 
     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;
+    h->mb.slice_table[i_mb_xy] = h->sh.i_first_mb;
     h->mb.partition[i_mb_xy] = IS_INTRA( i_mb_type ) ? D_16x16 : h->mb.i_partition;
     h->mb.i_mb_prev_xy = i_mb_xy;
 
     /* save intra4x4 */
     if( i_mb_type == I_4x4 )
     {
-        CP32( &intra4x4_pred_mode[0], &h->mb.cache.intra4x4_pred_mode[x264_scan8[10]] );
-        M32( &intra4x4_pred_mode[4] ) = pack8to32(h->mb.cache.intra4x4_pred_mode[x264_scan8[5] ],
-                                                  h->mb.cache.intra4x4_pred_mode[x264_scan8[7] ],
-                                                  h->mb.cache.intra4x4_pred_mode[x264_scan8[13] ], 0);
+        CP32( &i4x4[0], &h->mb.cache.intra4x4_pred_mode[x264_scan8[10]] );
+        M32( &i4x4[4] ) = pack8to32( h->mb.cache.intra4x4_pred_mode[x264_scan8[5] ],
+                                     h->mb.cache.intra4x4_pred_mode[x264_scan8[7] ],
+                                     h->mb.cache.intra4x4_pred_mode[x264_scan8[13] ], 0);
     }
     else if( !h->param.b_constrained_intra || IS_INTRA(i_mb_type) )
-        M64( intra4x4_pred_mode ) = I_PRED_4x4_DC * 0x0101010101010101ULL;
+        M64( i4x4 ) = I_PRED_4x4_DC * 0x0101010101010101ULL;
     else
-        M64( intra4x4_pred_mode ) = (uint8_t)(-1) * 0x0101010101010101ULL;
+        M64( i4x4 ) = (uint8_t)(-1) * 0x0101010101010101ULL;
 
 
     if( i_mb_type == I_PCM )
     {
         h->mb.qp[i_mb_xy] = 0;
         h->mb.i_last_dqp = 0;
-        h->mb.i_cbp_chroma = 2;
+        h->mb.i_cbp_chroma = CHROMA444 ? 0 : 2;
         h->mb.i_cbp_luma = 0xf;
-        h->mb.cbp[i_mb_xy] = 0x72f;   /* all set */
+        h->mb.cbp[i_mb_xy] = (h->mb.i_cbp_chroma << 4) | h->mb.i_cbp_luma | 0x700;
         h->mb.b_transform_8x8 = 0;
-        memset( non_zero_count, 16, 24 );
+        for( int i = 0; i < 48; i++ )
+            h->mb.cache.non_zero_count[x264_scan8[i]] = h->param.b_cabac ? 1 : 16;
     }
     else
     {
-        /* save non zero count */
-        CP32( &non_zero_count[0*4], &h->mb.cache.non_zero_count[x264_scan8[0]+0*8] );
-        CP32( &non_zero_count[1*4], &h->mb.cache.non_zero_count[x264_scan8[0]+1*8] );
-        CP32( &non_zero_count[2*4], &h->mb.cache.non_zero_count[x264_scan8[0]+2*8] );
-        CP32( &non_zero_count[3*4], &h->mb.cache.non_zero_count[x264_scan8[0]+3*8] );
-        M16( &non_zero_count[16+0*2] ) = M32( &h->mb.cache.non_zero_count[x264_scan8[16+0*2]-1] ) >> 8;
-        M16( &non_zero_count[16+1*2] ) = M32( &h->mb.cache.non_zero_count[x264_scan8[16+1*2]-1] ) >> 8;
-        M16( &non_zero_count[16+2*2] ) = M32( &h->mb.cache.non_zero_count[x264_scan8[16+2*2]-1] ) >> 8;
-        M16( &non_zero_count[16+3*2] ) = M32( &h->mb.cache.non_zero_count[x264_scan8[16+3*2]-1] ) >> 8;
-
         if( h->mb.i_type != I_16x16 && h->mb.i_cbp_luma == 0 && h->mb.i_cbp_chroma == 0 )
             h->mb.i_qp = h->mb.i_last_qp;
         h->mb.qp[i_mb_xy] = h->mb.i_qp;
@@ -1346,82 +1761,103 @@ void x264_macroblock_cache_save( x264_t *h )
         h->mb.i_last_qp = h->mb.i_qp;
     }
 
+    /* save non zero count */
+    CP32( &nnz[ 0+0*4], &h->mb.cache.non_zero_count[x264_scan8[ 0]] );
+    CP32( &nnz[ 0+1*4], &h->mb.cache.non_zero_count[x264_scan8[ 2]] );
+    CP32( &nnz[ 0+2*4], &h->mb.cache.non_zero_count[x264_scan8[ 8]] );
+    CP32( &nnz[ 0+3*4], &h->mb.cache.non_zero_count[x264_scan8[10]] );
+    CP32( &nnz[16+0*4], &h->mb.cache.non_zero_count[x264_scan8[16+0]] );
+    CP32( &nnz[16+1*4], &h->mb.cache.non_zero_count[x264_scan8[16+2]] );
+    CP32( &nnz[32+0*4], &h->mb.cache.non_zero_count[x264_scan8[32+0]] );
+    CP32( &nnz[32+1*4], &h->mb.cache.non_zero_count[x264_scan8[32+2]] );
+    if( CHROMA_FORMAT >= CHROMA_422 )
+    {
+        CP32( &nnz[16+2*4], &h->mb.cache.non_zero_count[x264_scan8[16+ 8]] );
+        CP32( &nnz[16+3*4], &h->mb.cache.non_zero_count[x264_scan8[16+10]] );
+        CP32( &nnz[32+2*4], &h->mb.cache.non_zero_count[x264_scan8[32+ 8]] );
+        CP32( &nnz[32+3*4], &h->mb.cache.non_zero_count[x264_scan8[32+10]] );
+    }
+
     if( h->mb.i_cbp_luma == 0 && h->mb.i_type != I_8x8 )
         h->mb.b_transform_8x8 = 0;
     h->mb.mb_transform_size[i_mb_xy] = h->mb.b_transform_8x8;
 
     if( h->sh.i_type != SLICE_TYPE_I )
     {
+        int16_t (*mv0)[2] = &h->mb.mv[0][i_mb_4x4];
+        int16_t (*mv1)[2] = &h->mb.mv[1][i_mb_4x4];
+        int8_t *ref0 = &h->mb.ref[0][i_mb_8x8];
+        int8_t *ref1 = &h->mb.ref[1][i_mb_8x8];
         if( !IS_INTRA( i_mb_type ) )
         {
-            h->mb.ref[0][i_mb_8x8+0+0*s8x8] = h->mb.cache.ref[0][x264_scan8[0]];
-            h->mb.ref[0][i_mb_8x8+1+0*s8x8] = h->mb.cache.ref[0][x264_scan8[4]];
-            h->mb.ref[0][i_mb_8x8+0+1*s8x8] = h->mb.cache.ref[0][x264_scan8[8]];
-            h->mb.ref[0][i_mb_8x8+1+1*s8x8] = h->mb.cache.ref[0][x264_scan8[12]];
-            for( y = 0; y < 4; y++ )
-            {
-                CP64( h->mb.mv[0][i_mb_4x4+y*s4x4+0], h->mb.cache.mv[0][x264_scan8[0]+8*y+0] );
-                CP64( h->mb.mv[0][i_mb_4x4+y*s4x4+2], h->mb.cache.mv[0][x264_scan8[0]+8*y+2] );
-            }
+            ref0[0+0*s8x8] = h->mb.cache.ref[0][x264_scan8[0]];
+            ref0[1+0*s8x8] = h->mb.cache.ref[0][x264_scan8[4]];
+            ref0[0+1*s8x8] = h->mb.cache.ref[0][x264_scan8[8]];
+            ref0[1+1*s8x8] = h->mb.cache.ref[0][x264_scan8[12]];
+            CP128( &mv0[0*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*0] );
+            CP128( &mv0[1*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*1] );
+            CP128( &mv0[2*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*2] );
+            CP128( &mv0[3*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*3] );
             if( h->sh.i_type == SLICE_TYPE_B )
             {
-                h->mb.ref[1][i_mb_8x8+0+0*s8x8] = h->mb.cache.ref[1][x264_scan8[0]];
-                h->mb.ref[1][i_mb_8x8+1+0*s8x8] = h->mb.cache.ref[1][x264_scan8[4]];
-                h->mb.ref[1][i_mb_8x8+0+1*s8x8] = h->mb.cache.ref[1][x264_scan8[8]];
-                h->mb.ref[1][i_mb_8x8+1+1*s8x8] = h->mb.cache.ref[1][x264_scan8[12]];
-                for( y = 0; y < 4; y++ )
-                {
-                    CP64( h->mb.mv[1][i_mb_4x4+y*s4x4+0], h->mb.cache.mv[1][x264_scan8[0]+8*y+0] );
-                    CP64( h->mb.mv[1][i_mb_4x4+y*s4x4+2], h->mb.cache.mv[1][x264_scan8[0]+8*y+2] );
-                }
+                ref1[0+0*s8x8] = h->mb.cache.ref[1][x264_scan8[0]];
+                ref1[1+0*s8x8] = h->mb.cache.ref[1][x264_scan8[4]];
+                ref1[0+1*s8x8] = h->mb.cache.ref[1][x264_scan8[8]];
+                ref1[1+1*s8x8] = h->mb.cache.ref[1][x264_scan8[12]];
+                CP128( &mv1[0*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*0] );
+                CP128( &mv1[1*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*1] );
+                CP128( &mv1[2*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*2] );
+                CP128( &mv1[3*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*3] );
             }
         }
         else
         {
-            int i_list;
-            for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2  : 1 ); i_list++ )
+            M16( &ref0[0*s8x8] ) = (uint8_t)(-1) * 0x0101;
+            M16( &ref0[1*s8x8] ) = (uint8_t)(-1) * 0x0101;
+            M128( &mv0[0*s4x4] ) = M128_ZERO;
+            M128( &mv0[1*s4x4] ) = M128_ZERO;
+            M128( &mv0[2*s4x4] ) = M128_ZERO;
+            M128( &mv0[3*s4x4] ) = M128_ZERO;
+            if( h->sh.i_type == SLICE_TYPE_B )
             {
-                M16( &h->mb.ref[i_list][i_mb_8x8+0*s8x8] ) = (uint8_t)(-1) * 0x0101;
-                M16( &h->mb.ref[i_list][i_mb_8x8+1*s8x8] ) = (uint8_t)(-1) * 0x0101;
-                for( y = 0; y < 4; y++ )
-                {
-                    M64( h->mb.mv[i_list][i_mb_4x4+y*s4x4+0] ) = 0;
-                    M64( h->mb.mv[i_list][i_mb_4x4+y*s4x4+2] ) = 0;
-                }
+                M16( &ref1[0*s8x8] ) = (uint8_t)(-1) * 0x0101;
+                M16( &ref1[1*s8x8] ) = (uint8_t)(-1) * 0x0101;
+                M128( &mv1[0*s4x4] ) = M128_ZERO;
+                M128( &mv1[1*s4x4] ) = M128_ZERO;
+                M128( &mv1[2*s4x4] ) = M128_ZERO;
+                M128( &mv1[3*s4x4] ) = M128_ZERO;
             }
         }
     }
 
     if( h->param.b_cabac )
     {
+        uint8_t (*mvd0)[2] = h->mb.mvd[0][i_mb_xy];
+        uint8_t (*mvd1)[2] = h->mb.mvd[1][i_mb_xy];
         if( IS_INTRA(i_mb_type) && i_mb_type != I_PCM )
-            h->mb.chroma_pred_mode[i_mb_xy] = x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ];
+            h->mb.chroma_pred_mode[i_mb_xy] = x264_mb_chroma_pred_mode_fix[h->mb.i_chroma_pred_mode];
         else
             h->mb.chroma_pred_mode[i_mb_xy] = I_PRED_CHROMA_DC;
 
-        if( !IS_INTRA( i_mb_type ) && !IS_SKIP( i_mb_type ) && !IS_DIRECT( i_mb_type ) )
+        if( (0x3FF30 >> i_mb_type) & 1 ) /* !INTRA && !SKIP && !DIRECT */
         {
-            CP64( h->mb.mvd[0][i_mb_xy*8+0], h->mb.cache.mvd[0][x264_scan8[0]+8*3] );
-            CP16( h->mb.mvd[0][i_mb_xy*8+4], h->mb.cache.mvd[0][x264_scan8[0]+8*0+3] );
-            CP16( h->mb.mvd[0][i_mb_xy*8+5], h->mb.cache.mvd[0][x264_scan8[0]+8*1+3] );
-            CP16( h->mb.mvd[0][i_mb_xy*8+6], h->mb.cache.mvd[0][x264_scan8[0]+8*2+3] );
+            CP64( mvd0[0], h->mb.cache.mvd[0][x264_scan8[10]] );
+            CP16( mvd0[4], h->mb.cache.mvd[0][x264_scan8[5 ]] );
+            CP16( mvd0[5], h->mb.cache.mvd[0][x264_scan8[7 ]] );
+            CP16( mvd0[6], h->mb.cache.mvd[0][x264_scan8[13]] );
             if( h->sh.i_type == SLICE_TYPE_B )
             {
-                CP64( h->mb.mvd[1][i_mb_xy*8+0], h->mb.cache.mvd[1][x264_scan8[0]+8*3] );
-                CP16( h->mb.mvd[1][i_mb_xy*8+4], h->mb.cache.mvd[1][x264_scan8[0]+8*0+3] );
-                CP16( h->mb.mvd[1][i_mb_xy*8+5], h->mb.cache.mvd[1][x264_scan8[0]+8*1+3] );
-                CP16( h->mb.mvd[1][i_mb_xy*8+6], h->mb.cache.mvd[1][x264_scan8[0]+8*2+3] );
+                CP64( mvd1[0], h->mb.cache.mvd[1][x264_scan8[10]] );
+                CP16( mvd1[4], h->mb.cache.mvd[1][x264_scan8[5 ]] );
+                CP16( mvd1[5], h->mb.cache.mvd[1][x264_scan8[7 ]] );
+                CP16( mvd1[6], h->mb.cache.mvd[1][x264_scan8[13]] );
             }
         }
         else
         {
-            M64( h->mb.mvd[0][i_mb_xy*8+0] ) = 0;
-            M64( h->mb.mvd[0][i_mb_xy*8+4] ) = 0;
+            M128( mvd0[0] ) = M128_ZERO;
             if( h->sh.i_type == SLICE_TYPE_B )
-            {
-                M64( h->mb.mvd[1][i_mb_xy*8+0] ) = 0;
-                M64( h->mb.mvd[1][i_mb_xy*8+4] ) = 0;
-            }
+                M128( mvd1[0] ) = M128_ZERO;
         }
 
         if( h->sh.i_type == SLICE_TYPE_B )
@@ -1445,45 +1881,43 @@ void x264_macroblock_cache_save( x264_t *h )
 
 void x264_macroblock_bipred_init( x264_t *h )
 {
-    int i_ref0, i_ref1, field;
-    for( field = 0; field <= h->sh.b_mbaff; field++ )
-        for( i_ref0 = 0; i_ref0 < (h->i_ref0<<h->sh.b_mbaff); i_ref0++ )
-        {
-            int poc0 = h->fref0[i_ref0>>h->sh.b_mbaff]->i_poc;
-            if( h->sh.b_mbaff && field^(i_ref0&1) )
-                poc0 += h->sh.i_delta_poc_bottom;
-            for( i_ref1 = 0; i_ref1 < (h->i_ref1<<h->sh.b_mbaff); i_ref1++ )
+    for( int mbfield = 0; mbfield <= SLICE_MBAFF; mbfield++ )
+        for( int field = 0; field <= SLICE_MBAFF; field++ )
+            for( int i_ref0 = 0; i_ref0 < (h->i_ref[0]<<mbfield); i_ref0++ )
             {
-                int dist_scale_factor;
-                int poc1 = h->fref1[i_ref1>>h->sh.b_mbaff]->i_poc;
-                if( h->sh.b_mbaff && field^(i_ref1&1) )
-                    poc1 += h->sh.i_delta_poc_bottom;
-                int cur_poc = h->fdec->i_poc + field*h->sh.i_delta_poc_bottom;
-                int td = x264_clip3( poc1 - poc0, -128, 127 );
-                if( td == 0 /* || pic0 is a long-term ref */ )
-                    dist_scale_factor = 256;
-                else
+                x264_frame_t *l0 = h->fref[0][i_ref0>>mbfield];
+                int poc0 = l0->i_poc + mbfield*l0->i_delta_poc[field^(i_ref0&1)];
+                for( int i_ref1 = 0; i_ref1 < (h->i_ref[1]<<mbfield); i_ref1++ )
                 {
-                    int tb = x264_clip3( cur_poc - poc0, -128, 127 );
-                    int tx = (16384 + (abs(td) >> 1)) / td;
-                    dist_scale_factor = x264_clip3( (tb * tx + 32) >> 6, -1024, 1023 );
+                    int dist_scale_factor;
+                    x264_frame_t *l1 = h->fref[1][i_ref1>>mbfield];
+                    int cur_poc = h->fdec->i_poc + mbfield*h->fdec->i_delta_poc[field];
+                    int poc1 = l1->i_poc + mbfield*l1->i_delta_poc[field^(i_ref1&1)];
+                    int td = x264_clip3( poc1 - poc0, -128, 127 );
+                    if( td == 0 /* || pic0 is a long-term ref */ )
+                        dist_scale_factor = 256;
+                    else
+                    {
+                        int tb = x264_clip3( cur_poc - poc0, -128, 127 );
+                        int tx = (16384 + (abs(td) >> 1)) / td;
+                        dist_scale_factor = x264_clip3( (tb * tx + 32) >> 6, -1024, 1023 );
+                    }
+
+                    h->mb.dist_scale_factor_buf[mbfield][field][i_ref0][i_ref1] = dist_scale_factor;
+
+                    dist_scale_factor >>= 2;
+                    if( h->param.analyse.b_weighted_bipred
+                          && dist_scale_factor >= -64
+                          && dist_scale_factor <= 128 )
+                    {
+                        h->mb.bipred_weight_buf[mbfield][field][i_ref0][i_ref1] = 64 - dist_scale_factor;
+                        // ssse3 implementation of biweight doesn't support the extrema.
+                        // if we ever generate them, we'll have to drop that optimization.
+                        assert( dist_scale_factor >= -63 && dist_scale_factor <= 127 );
+                    }
+                    else
+                        h->mb.bipred_weight_buf[mbfield][field][i_ref0][i_ref1] = 32;
                 }
-
-                h->mb.dist_scale_factor_buf[field][i_ref0][i_ref1] = dist_scale_factor;
-
-                dist_scale_factor >>= 2;
-                if( h->param.analyse.b_weighted_bipred
-                      && dist_scale_factor >= -64
-                      && dist_scale_factor <= 128 )
-                {
-                    h->mb.bipred_weight_buf[field][i_ref0][i_ref1] = 64 - dist_scale_factor;
-                    // ssse3 implementation of biweight doesn't support the extrema.
-                    // if we ever generate them, we'll have to drop that optimization.
-                    assert( dist_scale_factor >= -63 && dist_scale_factor <= 127 );
-                }
-                else
-                    h->mb.bipred_weight_buf[field][i_ref0][i_ref1] = 32;
             }
-        }
 }