]> git.sesse.net Git - x264/blobdiff - common/macroblock.c
slightly faster loopfilter
[x264] / common / macroblock.c
index 450c1f7b6c27ec1e552f5d5c434ff525ec10383c..1507cf57b88b428e8bad8c510dc789fbabc943ff 100644 (file)
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  *****************************************************************************/
 
-#include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 
 #include "common.h"
-#include "macroblock.h"
-
-static const uint8_t block_idx_x[16] =
-{
-    0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
-};
-static const uint8_t block_idx_y[16] =
-{
-    0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
-};
-static const uint8_t block_idx_xy[4][4] =
-{
-    { 0, 2, 8,  10},
-    { 1, 3, 9,  11},
-    { 4, 6, 12, 14},
-    { 5, 7, 13, 15}
-};
 
 static const int dequant_mf[6][4][4] =
 {
@@ -54,23 +36,12 @@ static const int dequant_mf[6][4][4] =
     { {18, 23, 18, 23}, {23, 29, 23, 29}, {18, 23, 18, 23}, {23, 29, 23, 29} }
 };
 
-#if 0
-static const int i_chroma_qp_table[52] =
-{
-     0,  1,  2,  3,  4,  5,  6,  7,  8,  9,
-    10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
-    20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
-    29, 30, 31, 32, 32, 33, 34, 34, 35, 35,
-    36, 36, 37, 37, 37, 38, 38, 38, 39, 39,
-    39, 39
-};
-#endif
-
 int x264_mb_predict_intra4x4_mode( x264_t *h, int idx )
 {
     const int ma = h->mb.cache.intra4x4_pred_mode[x264_scan8[idx] - 1];
     const int mb = h->mb.cache.intra4x4_pred_mode[x264_scan8[idx] - 8];
-    const int m  = X264_MIN( ma, mb );
+    const int m  = X264_MIN( x264_mb_pred_mode4x4_fix(ma),
+                             x264_mb_pred_mode4x4_fix(mb) );
 
     if( m < 0 )
         return I_PRED_4x4_DC;
@@ -92,94 +63,24 @@ int x264_mb_predict_non_zero_code( x264_t *h, int idx )
     return i_ret & 0x7f;
 }
 
-/****************************************************************************
- * Scan and Quant functions
- ****************************************************************************/
-void x264_mb_dequant_2x2_dc( int16_t dct[2][2], int i_qscale )
+int x264_mb_transform_8x8_allowed( x264_t *h )
 {
-    const int i_qbits = i_qscale/6 - 1;
-
-    if( i_qbits >= 0 )
-    {
-        const int i_dmf = dequant_mf[i_qscale%6][0][0] << i_qbits;
-
-        dct[0][0] = dct[0][0] * i_dmf;
-        dct[0][1] = dct[0][1] * i_dmf;
-        dct[1][0] = dct[1][0] * i_dmf;
-        dct[1][1] = dct[1][1] * i_dmf;
-    }
-    else
-    {
-        const int i_dmf = dequant_mf[i_qscale%6][0][0];
-
-        dct[0][0] = ( dct[0][0] * i_dmf ) >> 1;
-        dct[0][1] = ( dct[0][1] * i_dmf ) >> 1;
-        dct[1][0] = ( dct[1][0] * i_dmf ) >> 1;
-        dct[1][1] = ( dct[1][1] * i_dmf ) >> 1;
-    }
-}
-
-void x264_mb_dequant_4x4_dc( int16_t dct[4][4], int i_qscale )
-{
-    const int i_qbits = i_qscale/6 - 2;
-    int x,y;
-
-    if( i_qbits >= 0 )
-    {
-        const int i_dmf = dequant_mf[i_qscale%6][0][0] << i_qbits;
-
-        for( y = 0; y < 4; y++ )
-        {
-            for( x = 0; x < 4; x++ )
-            {
-                dct[y][x] = dct[y][x] * i_dmf;
-            }
-        }
-    }
-    else
+    if( IS_SKIP( h->mb.i_type ) )
+        return 0;
+    if( h->mb.i_type == P_8x8 || h->mb.i_type == B_8x8 )
     {
-        const int i_dmf = dequant_mf[i_qscale%6][0][0];
-        const int f = 1 << ( 1 + i_qbits );
-
-        for( y = 0; y < 4; y++ )
-        {
-            for( x = 0; x < 4; x++ )
+        int i;
+        for( i = 0; i < 4; i++ )
+            if( !IS_SUB8x8(h->mb.i_sub_partition[i])
+                || ( h->mb.i_sub_partition[i] == D_DIRECT_8x8 && !h->sps->b_direct8x8_inference ) )
             {
-                dct[y][x] = ( dct[y][x] * i_dmf + f ) >> (-i_qbits);
+                return 0;
             }
-        }
-    }
-}
-
-void x264_mb_dequant_4x4( int16_t dct[4][4], int i_qscale )
-{
-    const int i_mf = i_qscale%6;
-    const int i_qbits = i_qscale/6;
-    int y;
-
-    for( y = 0; y < 4; y++ )
-    {
-        dct[y][0] = ( dct[y][0] * dequant_mf[i_mf][y][0] ) << i_qbits;
-        dct[y][1] = ( dct[y][1] * dequant_mf[i_mf][y][1] ) << i_qbits;
-        dct[y][2] = ( dct[y][2] * dequant_mf[i_mf][y][2] ) << i_qbits;
-        dct[y][3] = ( dct[y][3] * dequant_mf[i_mf][y][3] ) << i_qbits;
     }
-}
-
-static inline int x264_median( int a, int b, int c )
-{
-    int min = a, max =a;
-    if( b < min )
-        min = b;
-    else
-        max = b;    /* no need to do 'b > max' (more consuming than always doing affectation) */
-
-    if( c < min )
-        min = c;
-    else if( c > max )
-        max = c;
+    if( h->mb.i_type == B_DIRECT && !h->sps->b_direct8x8_inference )
+        return 0;
 
-    return a + b + c - min - max;
+    return 1;
 }
 
 void x264_mb_predict_mv( x264_t *h, int i_list, int idx, int i_width, int mvp[2] )
@@ -353,52 +254,67 @@ 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 i;
+    int i8, i4;
+    int b8x8;
+    const int type_col = h->fref1[0]->mb_type[ h->mb.i_mb_xy ];
     
     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, 0 );
     
-    /* FIXME: optimize per block size */
-    for( i = 0; i < 4; i++ )
+    if( IS_INTRA( type_col ) )
     {
-        const int x8 = 2*(i%2);
-        const int y8 = 2*(i/2);
-        /* TODO: MapColToList0 */
-        const int i_ref = h->mb.list1ref0.ref[ i_mb_8x8 + x8/2 + y8 * h->mb.i_mb_stride ];
+        x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
+        x264_macroblock_cache_mv(  h, 0, 0, 4, 4, 0, 0, 0 );
+        x264_macroblock_cache_mv(  h, 0, 0, 4, 4, 1, 0, 0 );
+        return 1;
+    }
+    b8x8 = h->sps->b_direct8x8_inference ||
+           (type_col != P_8x8 && type_col != B_SKIP && type_col != B_DIRECT && type_col != B_8x8);
 
-        if( i_ref == -1 )
-        {
-            x264_macroblock_cache_ref( h, x8, y8, 2, 2, 0, 0 );
-            x264_macroblock_cache_mv(  h, x8, y8, 2, 2, 0, 0, 0 );
-            x264_macroblock_cache_mv(  h, x8, y8, 2, 2, 1, 0, 0 );
-        }
-        else
+    for( i8 = 0; i8 < 4; i8++ )
+    {
+        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_ref = h->mb.map_col_to_list0[ h->fref1[0]->ref[0][ i_part_8x8 ] ];
+
+        if( i_ref >= 0 )
         {
-            int poc0 = h->fref0[i_ref]->i_poc;
-            int poc1 = h->fref1[0]->i_poc;
-            int td = x264_clip3( poc1 - poc0, -128, 127 );
-            int dist_scale_factor;
-            int x4, y4;
-            if( td == 0 /* || pic0 is a long-term ref */ )
-                dist_scale_factor = 256;
-            else
-            {
-                int tb = x264_clip3( h->fdec->i_poc - poc0, -128, 127 );
-                int tx = (16384 + (abs(td) >> 1)) / td;
-                dist_scale_factor = x264_clip3( (tb * tx + 32) >> 6, -1024, 1023 );
-            }
+            const int dist_scale_factor = h->mb.dist_scale_factor[i_ref][0];
 
-            x264_macroblock_cache_ref( h, x8, y8, 2, 2, 0, i_ref );
+            x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, i_ref );
 
-            for( y4 = y8; y4 < y8+2; y4++ )
-                for( x4 = x8; x4 < x8+2; x4++ )
+            if( b8x8 )
+            {
+                const int16_t *mv_col = h->fref1[0]->mv[0][ i_mb_4x4 + 3*x8 + 3*y8 * h->mb.i_b4_stride];
+                int mv_l0[2];
+                mv_l0[0] = ( dist_scale_factor * mv_col[0] + 128 ) >> 8;
+                mv_l0[1] = ( dist_scale_factor * mv_col[1] + 128 ) >> 8;
+                x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, mv_l0[0], mv_l0[1] );
+                x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 1, mv_l0[0] - mv_col[0], mv_l0[1] - mv_col[1] );
+            }
+            else
+            {
+                for( i4 = 0; i4 < 4; i4++ )
                 {
-                    const int16_t *mv_col = h->mb.list1ref0.mv[ i_mb_4x4 + x4 + y4 * 4 * h->mb.i_mb_stride ];
+                    const int x4 = i4%2 + 2*x8;
+                    const int y4 = i4/2 + 2*y8;
+                    const int16_t *mv_col = h->fref1[0]->mv[0][ i_mb_4x4 + x4 + y4 * h->mb.i_b4_stride ];
                     int mv_l0[2];
                     mv_l0[0] = ( dist_scale_factor * mv_col[0] + 128 ) >> 8;
                     mv_l0[1] = ( dist_scale_factor * mv_col[1] + 128 ) >> 8;
                     x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, mv_l0[0], mv_l0[1] );
                     x264_macroblock_cache_mv( h, x4, y4, 1, 1, 1, mv_l0[0] - mv_col[0], mv_l0[1] - mv_col[1] );
                 }
+            }
+        }
+        else
+        {
+            /* the colocated 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;
         }
     }
 
@@ -411,11 +327,12 @@ static int x264_mb_predict_mv_direct16x16_spatial( x264_t *h )
     int mv[2][2];
     int i_list;
     int i8, i4;
-    const int s8x8 = 2 * h->mb.i_mb_stride;
-    const int s4x4 = 4 * h->mb.i_mb_stride;
-    const int8_t *l1ref = &h->mb.list1ref0.ref[ 2*h->mb.i_mb_x + 2*s8x8*h->mb.i_mb_y ];
-    const int16_t (*l1mv)[2] = (const int16_t (*)[2])
-        &h->mb.list1ref0.mv[ 4*h->mb.i_mb_x + 4*s4x4*h->mb.i_mb_y ];
+    int b8x8;
+    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 (*l1mv0)[2] = (const int16_t (*)[2]) &h->fref1[0]->mv[0][ h->mb.i_b4_xy ];
+    const int16_t (*l1mv1)[2] = (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 ];
 
     for( i_list=0; i_list<2; i_list++ )
     {
@@ -454,31 +371,50 @@ static int x264_mb_predict_mv_direct16x16_spatial( x264_t *h )
         }
     }
 
-    /* FIXME: clip mv ? */
-    
     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, ref[0] );
     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, ref[1] );
     x264_macroblock_cache_mv(  h, 0, 0, 4, 4, 0, mv[0][0], mv[0][1] );
     x264_macroblock_cache_mv(  h, 0, 0, 4, 4, 1, mv[1][0], mv[1][1] );
 
+    if( IS_INTRA( type_col ) )
+        return 1;
+    b8x8 = h->sps->b_direct8x8_inference ||
+           (type_col != P_8x8 && type_col != B_SKIP && type_col != B_DIRECT && type_col != B_8x8);
+
     /* col_zero_flag */
     for( i8=0; i8<4; i8++ )
     {
         const int x8 = i8%2;
         const int y8 = i8/2;
-        if( l1ref[ x8 + y8*s8x8 ] == 0 )
+        const int o8 = x8 + y8 * h->mb.i_b8_stride;
+        if( l1ref0[o8] == 0 || ( l1ref0[o8] < 0 && l1ref1[o8] == 0 ) )
         {
-            for( i4=0; i4<4; i4++ )
+            const int16_t (*l1mv)[2] = (l1ref0[o8] == 0) ? l1mv0 : l1mv1;
+            if( b8x8 )
             {
-                const int x4 = i4%2 + 2*x8;
-                const int y4 = i4/2 + 2*y8;
-                const int16_t *mvcol = l1mv[x4 + y4*s4x4];
+                const int16_t *mvcol = l1mv[3*x8 + 3*y8 * h->mb.i_b4_stride];
                 if( abs( mvcol[0] ) <= 1 && abs( mvcol[1] ) <= 1 )
                 {
                     if( ref[0] == 0 )
-                        x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, 0, 0 );
+                        x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, 0, 0 );
                     if( ref[1] == 0 )
-                        x264_macroblock_cache_mv( h, x4, y4, 1, 1, 1, 0, 0 );
+                        x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 1, 0, 0 );
+                }
+            }
+            else
+            {
+                for( i4=0; i4<4; i4++ )
+                {
+                    const int x4 = i4%2 + 2*x8;
+                    const int y4 = i4/2 + 2*y8;
+                    const int16_t *mvcol = l1mv[x4 + y4 * h->mb.i_b4_stride];
+                    if( abs( mvcol[0] ) <= 1 && abs( mvcol[1] ) <= 1 )
+                    {
+                        if( ref[0] == 0 )
+                            x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, 0, 0 );
+                        if( ref[1] == 0 )
+                            x264_macroblock_cache_mv( h, x4, y4, 1, 1, 1, 0, 0 );
+                    }
                 }
             }
         }
@@ -487,7 +423,7 @@ static int x264_mb_predict_mv_direct16x16_spatial( x264_t *h )
     return 1;
 }
 
-int x264_mb_predict_mv_direct16x16( x264_t *h )
+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 )
@@ -497,6 +433,29 @@ int x264_mb_predict_mv_direct16x16( x264_t *h )
     else
         b_available = x264_mb_predict_mv_direct16x16_temporal( h );
 
+    if( b_changed != NULL && b_available )
+    {
+        int type_col = h->fref1[0]->mb_type[ h->mb.i_mb_xy ];
+        if( IS_INTRA(type_col) || type_col == P_SKIP )
+        {
+            *b_changed = h->mb.cache.direct_ref[0][0] != h->mb.cache.ref[0][X264_SCAN8_0]
+                      || h->mb.cache.direct_ref[1][0] != h->mb.cache.ref[1][X264_SCAN8_0]
+                      || *(uint32_t*)h->mb.cache.direct_mv[0][X264_SCAN8_0] != *(uint32_t*)h->mb.cache.mv[0][X264_SCAN8_0]
+                      || *(uint32_t*)h->mb.cache.direct_mv[1][X264_SCAN8_0] != *(uint32_t*)h->mb.cache.mv[1][X264_SCAN8_0];
+        }
+        else
+        {
+            int i, l;
+            *b_changed = 0;
+            for( l = 0; l < 2; l++ )
+                for( i = 0; i < 4; i++ )
+                    *b_changed |= h->mb.cache.direct_ref[l][i] != h->mb.cache.ref[l][x264_scan8[i*4]];
+            *b_changed = *b_changed || memcmp(h->mb.cache.direct_mv, h->mb.cache.mv, sizeof(h->mb.cache.mv));
+        }
+        if( !*b_changed )
+            return b_available;
+    }
+
     /* cache ref & mv */
     if( b_available )
     {
@@ -527,38 +486,77 @@ void x264_mb_load_mv_direct8x8( x264_t *h, int idx )
 }
 
 /* This just improves encoder performance, it's not part of the spec */
-void x264_mb_predict_mv_ref16x16( x264_t *h, int i_list, int i_ref, int mvc[4][2], int *i_mvc )
+void x264_mb_predict_mv_ref16x16( x264_t *h, int i_list, int i_ref, int mvc[8][2], int *i_mvc )
 {
     int16_t (*mvr)[2] = h->mb.mvr[i_list][i_ref];
-
     int i = 0;
-    if( h->mb.i_mb_x > 0 )
+
+#define SET_MVP(mvp) { \
+        mvc[i][0] = mvp[0]; \
+        mvc[i][1] = mvp[1]; \
+        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]] );
+    }
+
+    /* spatial predictors */
+    if( h->mb.i_neighbour & MB_LEFT )
     {
         int i_mb_l = h->mb.i_mb_xy - 1;
-        mvc[i][0] = mvr[i_mb_l][0];
-        mvc[i][1] = mvr[i_mb_l][1];
-        i++;
+        /* 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_mb_y > 0 )
+    if( h->mb.i_neighbour & MB_TOP )
     {
         int i_mb_t = h->mb.i_mb_xy - h->mb.i_mb_stride;
-        mvc[i][0] = mvr[i_mb_t][0];
-        mvc[i][1] = mvr[i_mb_t][1];
-        i++;
+        if( !IS_SKIP( h->mb.type[i_mb_t] ) )
+            SET_MVP( mvr[i_mb_t] );
 
-        if( h->mb.i_mb_x > 0 )
-        {
-            mvc[i][0] = mvr[i_mb_t - 1][0];
-            mvc[i][1] = mvr[i_mb_t - 1][1];
-            i++;
-        }
-        if( h->mb.i_mb_x < h->mb.i_mb_stride - 1 )
-        {
-            mvc[i][0] = mvr[i_mb_t + 1][0];
-            mvc[i][1] = mvr[i_mb_t + 1][1];
-            i++;
+        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
+
+    /* temporal predictors */
+    if( h->fref0[0]->i_ref[0] > 0 )
+    {
+        x264_frame_t *l0 = h->fref0[0];
+        int ref_col_cur, ref_col_prev = -1;
+        int scale = 0;
+
+#define SET_TMVP(dx, dy) { \
+            int i_b4 = h->mb.i_b4_xy + dx*4 + dy*4*h->mb.i_b4_stride; \
+            int i_b8 = h->mb.i_b8_xy + dx*2 + dy*2*h->mb.i_b8_stride; \
+            ref_col_cur = l0->ref[0][i_b8]; \
+            if( ref_col_cur >= 0 ) \
+            { \
+                /* TODO: calc once per frame and tablize? */\
+                if( ref_col_cur != ref_col_prev ) \
+                    scale = 256 * (h->fenc->i_poc - h->fref0[i_ref]->i_poc) \
+                                / (l0->i_poc - l0->ref_poc[0][ref_col_cur]); \
+                mvc[i][0] = l0->mv[0][i_b4][0] * scale / 256; \
+                mvc[i][1] = l0->mv[0][i_b4][1] * scale / 256; \
+                i++; \
+                ref_col_prev = ref_col_cur; \
+            } \
         }
+
+        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
     }
+
     *i_mvc = i;
 }
 
@@ -566,38 +564,38 @@ static inline void x264_mb_mc_0xywh( x264_t *h, int x, int y, int width, int hei
 {
     const int i8 = x264_scan8[0]+x+8*y;
     const int i_ref = h->mb.cache.ref[0][i8];
-    const int mvx   = h->mb.cache.mv[0][i8][0];
-    const int mvy   = h->mb.cache.mv[0][i8][1];
+    const int mvx   = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
+    const int mvy   = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
 
-    h->mc[MC_LUMA]( &h->mb.pic.p_fref[0][i_ref][0][4*y * h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0],
-                    &h->mb.pic.p_fdec[0][4*y * h->mb.pic.i_stride[0]+4*x],           h->mb.pic.i_stride[0],
-                    mvx, mvy, 4*width, 4*height );
+    h->mc.mc_luma( h->mb.pic.p_fref[0][i_ref], h->mb.pic.i_stride[0],
+                    &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
+                    mvx + 4*4*x, mvy + 4*4*y, 4*width, 4*height );
 
-    h->mc[MC_CHROMA]( &h->mb.pic.p_fref[0][i_ref][1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
-                      &h->mb.pic.p_fdec[1][2*y*h->mb.pic.i_stride[1]+2*x],           h->mb.pic.i_stride[1],
+    h->mc.mc_chroma( &h->mb.pic.p_fref[0][i_ref][4][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
+                      &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
                       mvx, mvy, 2*width, 2*height );
 
-    h->mc[MC_CHROMA]( &h->mb.pic.p_fref[0][i_ref][2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
-                      &h->mb.pic.p_fdec[2][2*y*h->mb.pic.i_stride[2]+2*x],           h->mb.pic.i_stride[2],
+    h->mc.mc_chroma( &h->mb.pic.p_fref[0][i_ref][5][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
+                      &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
                       mvx, mvy, 2*width, 2*height );
 }
 static inline void x264_mb_mc_1xywh( x264_t *h, int x, int y, int width, int height )
 {
     const int i8 = x264_scan8[0]+x+8*y;
     const int i_ref = h->mb.cache.ref[1][i8];
-    const int mvx   = h->mb.cache.mv[1][i8][0];
-    const int mvy   = h->mb.cache.mv[1][i8][1];
+    const int mvx   = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
+    const int mvy   = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
 
-    h->mc[MC_LUMA]( &h->mb.pic.p_fref[1][i_ref][0][4*y * h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0],
-                    &h->mb.pic.p_fdec[0][4*y *h->mb.pic.i_stride[0]+4*x],            h->mb.pic.i_stride[0],
-                    mvx, mvy, 4*width, 4*height );
+    h->mc.mc_luma( h->mb.pic.p_fref[1][i_ref], h->mb.pic.i_stride[0],
+                    &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
+                    mvx + 4*4*x, mvy + 4*4*y, 4*width, 4*height );
 
-    h->mc[MC_CHROMA]( &h->mb.pic.p_fref[1][i_ref][1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
-                      &h->mb.pic.p_fdec[1][2*y*h->mb.pic.i_stride[1]+2*x],           h->mb.pic.i_stride[1],
+    h->mc.mc_chroma( &h->mb.pic.p_fref[1][i_ref][4][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
+                      &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
                       mvx, mvy, 2*width, 2*height );
 
-    h->mc[MC_CHROMA]( &h->mb.pic.p_fref[1][i_ref][2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
-                      &h->mb.pic.p_fdec[2][2*y*h->mb.pic.i_stride[2]+2*x],           h->mb.pic.i_stride[2],
+    h->mc.mc_chroma( &h->mb.pic.p_fref[1][i_ref][5][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
+                      &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
                       mvx, mvy, 2*width, 2*height );
 }
 
@@ -605,46 +603,44 @@ static inline void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int he
 {
     const int i8 = x264_scan8[0]+x+8*y;
 
-    const int i_ref0 = h->mb.cache.ref[0][i8];
-    const int mvx0   = h->mb.cache.mv[0][i8][0];
-    const int mvy0   = h->mb.cache.mv[0][i8][1];
-
     const int i_ref1 = h->mb.cache.ref[1][i8];
-    const int mvx1   = h->mb.cache.mv[1][i8][0];
-    const int mvy1   = h->mb.cache.mv[1][i8][1];
+    const int mvx1   = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
+    const int mvy1   = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
     DECLARE_ALIGNED( uint8_t, tmp[16*16], 16 );
-    int     i_mode = 0;
-
-    if( width == 4 && height == 4 ) i_mode = PIXEL_16x16;
-    else if( width == 4 && height == 2 ) i_mode = PIXEL_16x8;
-    else if( width == 2 && height == 4 ) i_mode = PIXEL_8x16;
-    else if( width == 2 && height == 2 ) i_mode = PIXEL_8x8;
-    else if( width == 2 && height == 1 ) i_mode = PIXEL_8x4;
-    else if( width == 1 && height == 2 ) i_mode = PIXEL_4x8;
-    else if( width == 1 && height == 1 ) i_mode = PIXEL_4x4;
-
-    h->mc[MC_LUMA]( &h->mb.pic.p_fref[0][i_ref0][0][4*y * h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0],
-                    &h->mb.pic.p_fdec[0][4*y *h->mb.pic.i_stride[0]+4*x],             h->mb.pic.i_stride[0],
-                    mvx0, mvy0, 4*width, 4*height );
-    h->mc[MC_CHROMA]( &h->mb.pic.p_fref[0][i_ref0][1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
-                      &h->mb.pic.p_fdec[1][2*y*h->mb.pic.i_stride[1]+2*x],            h->mb.pic.i_stride[1],
-                      mvx0, mvy0, 2*width, 2*height );
-    h->mc[MC_CHROMA]( &h->mb.pic.p_fref[0][i_ref0][2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
-                      &h->mb.pic.p_fdec[2][2*y*h->mb.pic.i_stride[2]+2*x],            h->mb.pic.i_stride[2],
-                      mvx0, mvy0, 2*width, 2*height );
-
-
-    h->mc[MC_LUMA]( &h->mb.pic.p_fref[1][i_ref1][0][4*y * h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0],
-                    tmp, 16, mvx1, mvy1, 4*width, 4*height );
-    h->pixf.avg[i_mode]( &h->mb.pic.p_fdec[0][4*y *h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0], tmp, 16 );
-
-    h->mc[MC_CHROMA]( &h->mb.pic.p_fref[1][i_ref1][1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
-                      tmp, 16, mvx1, mvy1, 2*width, 2*height );
-    h->pixf.avg[i_mode]( &h->mb.pic.p_fdec[1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1], tmp, 16 );
-
-    h->mc[MC_CHROMA]( &h->mb.pic.p_fref[1][i_ref1][2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
-                      tmp, 16, mvx1, mvy1, 2*width, 2*height );
-    h->pixf.avg[i_mode]( &h->mb.pic.p_fdec[2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2], tmp, 16 );
+    int i_mode = x264_size2pixel[height][width];
+
+    x264_mb_mc_0xywh( h, x, y, width, height );
+
+    h->mc.mc_luma( h->mb.pic.p_fref[1][i_ref1], h->mb.pic.i_stride[0],
+                    tmp, 16, mvx1 + 4*4*x, mvy1 + 4*4*y, 4*width, 4*height );
+
+    if( h->param.analyse.b_weighted_bipred )
+    {
+        const int i_ref0 = h->mb.cache.ref[0][i8];
+        const int weight = h->mb.bipred_weight[i_ref0][i_ref1];
+
+        h->mc.avg_weight[i_mode]( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE, tmp, 16, weight );
+
+        h->mc.mc_chroma( &h->mb.pic.p_fref[1][i_ref1][4][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
+                          tmp, 16, mvx1, mvy1, 2*width, 2*height );
+        h->mc.avg_weight[i_mode+3]( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16, weight );
+
+        h->mc.mc_chroma( &h->mb.pic.p_fref[1][i_ref1][5][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
+                          tmp, 16, mvx1, mvy1, 2*width, 2*height );
+        h->mc.avg_weight[i_mode+3]( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16, weight );
+    }
+    else
+    {
+        h->mc.avg[i_mode]( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE, tmp, 16 );
+
+        h->mc.mc_chroma( &h->mb.pic.p_fref[1][i_ref1][4][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
+                          tmp, 16, mvx1, mvy1, 2*width, 2*height );
+        h->mc.avg[i_mode+3]( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16 );
+
+        h->mc.mc_chroma( &h->mb.pic.p_fref[1][i_ref1][5][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
+                          tmp, 16, mvx1, mvy1, 2*width, 2*height );
+        h->mc.avg[i_mode+3]( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16 );
+    }
 }
 
 static void x264_mb_mc_direct8x8( x264_t *h, int x, int y )
@@ -691,6 +687,69 @@ static void x264_mb_mc_direct8x8( x264_t *h, int x, int y )
     }
 }
 
+void x264_mb_mc_8x8( x264_t *h, int i8 )
+{
+    const int x = 2*(i8&1);
+    const int y = 2*(i8>>1);
+    switch( h->mb.i_sub_partition[i8] )
+    {
+        case D_L0_8x8:
+            x264_mb_mc_0xywh( h, x, y, 2, 2 );
+            break;
+        case D_L0_8x4:
+            x264_mb_mc_0xywh( h, x, y+0, 2, 1 );
+            x264_mb_mc_0xywh( h, x, y+1, 2, 1 );
+            break;
+        case D_L0_4x8:
+            x264_mb_mc_0xywh( h, x+0, y, 1, 2 );
+            x264_mb_mc_0xywh( h, x+1, y, 1, 2 );
+            break;
+        case D_L0_4x4:
+            x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
+            x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
+            x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
+            x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
+            break;
+        case D_L1_8x8:
+            x264_mb_mc_1xywh( h, x, y, 2, 2 );
+            break;
+        case D_L1_8x4:
+            x264_mb_mc_1xywh( h, x, y+0, 2, 1 );
+            x264_mb_mc_1xywh( h, x, y+1, 2, 1 );
+            break;
+        case D_L1_4x8:
+            x264_mb_mc_1xywh( h, x+0, y, 1, 2 );
+            x264_mb_mc_1xywh( h, x+1, y, 1, 2 );
+            break;
+        case D_L1_4x4:
+            x264_mb_mc_1xywh( h, x+0, y+0, 1, 1 );
+            x264_mb_mc_1xywh( h, x+1, y+0, 1, 1 );
+            x264_mb_mc_1xywh( h, x+0, y+1, 1, 1 );
+            x264_mb_mc_1xywh( h, x+1, y+1, 1, 1 );
+            break;
+        case D_BI_8x8:
+            x264_mb_mc_01xywh( h, x, y, 2, 2 );
+            break;
+        case D_BI_8x4:
+            x264_mb_mc_01xywh( h, x, y+0, 2, 1 );
+            x264_mb_mc_01xywh( h, x, y+1, 2, 1 );
+            break;
+        case D_BI_4x8:
+            x264_mb_mc_01xywh( h, x+0, y, 1, 2 );
+            x264_mb_mc_01xywh( h, x+1, y, 1, 2 );
+            break;
+        case D_BI_4x4:
+            x264_mb_mc_01xywh( h, x+0, y+0, 1, 1 );
+            x264_mb_mc_01xywh( h, x+1, y+0, 1, 1 );
+            x264_mb_mc_01xywh( h, x+0, y+1, 1, 1 );
+            x264_mb_mc_01xywh( h, x+1, y+1, 1, 1 );
+            break;
+        case D_DIRECT_8x8:
+            x264_mb_mc_direct8x8( h, x, y );
+            break;
+    }
+}
+
 void x264_mb_mc( x264_t *h )
 {
     if( h->mb.i_type == P_L0 )
@@ -714,77 +773,14 @@ void x264_mb_mc( x264_t *h )
     {
         int i;
         for( i = 0; i < 4; i++ )
-        {
-            const int x = 2*(i%2);
-            const int y = 2*(i/2);
-            switch( h->mb.i_sub_partition[i] )
-            {
-                case D_L0_8x8:
-                    x264_mb_mc_0xywh( h, x, y, 2, 2 );
-                    break;
-                case D_L0_8x4:
-                    x264_mb_mc_0xywh( h, x, y+0, 2, 1 );
-                    x264_mb_mc_0xywh( h, x, y+1, 2, 1 );
-                    break;
-                case D_L0_4x8:
-                    x264_mb_mc_0xywh( h, x+0, y, 1, 2 );
-                    x264_mb_mc_0xywh( h, x+1, y, 1, 2 );
-                    break;
-                case D_L0_4x4:
-                    x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
-                    x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
-                    x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
-                    x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
-                    break;
-                case D_L1_8x8:
-                    x264_mb_mc_1xywh( h, x, y, 2, 2 );
-                    break;
-                case D_L1_8x4:
-                    x264_mb_mc_1xywh( h, x, y+0, 2, 1 );
-                    x264_mb_mc_1xywh( h, x, y+1, 2, 1 );
-                    break;
-                case D_L1_4x8:
-                    x264_mb_mc_1xywh( h, x+0, y, 1, 2 );
-                    x264_mb_mc_1xywh( h, x+1, y, 1, 2 );
-                    break;
-                case D_L1_4x4:
-                    x264_mb_mc_1xywh( h, x+0, y+0, 1, 1 );
-                    x264_mb_mc_1xywh( h, x+1, y+0, 1, 1 );
-                    x264_mb_mc_1xywh( h, x+0, y+1, 1, 1 );
-                    x264_mb_mc_1xywh( h, x+1, y+1, 1, 1 );
-                    break;
-                case D_BI_8x8:
-                    x264_mb_mc_01xywh( h, x, y, 2, 2 );
-                    break;
-                case D_BI_8x4:
-                    x264_mb_mc_01xywh( h, x, y+0, 2, 1 );
-                    x264_mb_mc_01xywh( h, x, y+1, 2, 1 );
-                    break;
-                case D_BI_4x8:
-                    x264_mb_mc_01xywh( h, x+0, y, 1, 2 );
-                    x264_mb_mc_01xywh( h, x+1, y, 1, 2 );
-                    break;
-                case D_BI_4x4:
-                    x264_mb_mc_01xywh( h, x+0, y+0, 1, 1 );
-                    x264_mb_mc_01xywh( h, x+1, y+0, 1, 1 );
-                    x264_mb_mc_01xywh( h, x+0, y+1, 1, 1 );
-                    x264_mb_mc_01xywh( h, x+1, y+1, 1, 1 );
-                    break;
-                case D_DIRECT_8x8:
-                    x264_mb_mc_direct8x8( h, x, y );
-                    break;
-            }
-        }
+            x264_mb_mc_8x8( h, i );
     }
     else if( h->mb.i_type == B_SKIP || h->mb.i_type == B_DIRECT )
     {
-        int i;
-        for( i = 0; i < 4; i++ )
-        {
-            const int x = 2*(i%2);
-            const int y = 2*(i/2);
-            x264_mb_mc_direct8x8( h, x, y );
-        }
+        x264_mb_mc_direct8x8( h, 0, 0 );
+        x264_mb_mc_direct8x8( h, 2, 0 );
+        x264_mb_mc_direct8x8( h, 0, 2 );
+        x264_mb_mc_direct8x8( h, 2, 2 );
     }
     else    /* B_*x* */
     {
@@ -834,11 +830,13 @@ void x264_macroblock_cache_init( x264_t *h )
     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.type= x264_malloc( i_mb_count * sizeof( int8_t) );
-    h->mb.qp  = x264_malloc( i_mb_count * sizeof( int8_t) );
-    h->mb.cbp = x264_malloc( i_mb_count * sizeof( int16_t) );
-    h->mb.skipbp = x264_malloc( i_mb_count * sizeof( int8_t) );
+    h->mb.qp  = x264_malloc( i_mb_count * sizeof(int8_t) );
+    h->mb.cbp = x264_malloc( i_mb_count * sizeof(int16_t) );
+    h->mb.skipbp = x264_malloc( i_mb_count * sizeof(int8_t) );
+    h->mb.mb_transform_size = x264_malloc( i_mb_count * sizeof(int8_t) );
 
     /* 0 -> 3 top(4), 4 -> 6 : left(3) */
     h->mb.intra4x4_pred_mode = x264_malloc( i_mb_count * 7 * sizeof( int8_t ) );
@@ -846,11 +844,6 @@ void x264_macroblock_cache_init( x264_t *h )
     /* all coeffs */
     h->mb.non_zero_count = x264_malloc( i_mb_count * 24 * sizeof( uint8_t ) );
 
-    h->mb.mv[0]  = x264_malloc( 2*16 * i_mb_count * sizeof( int16_t ) );
-    h->mb.mv[1]  = x264_malloc( 2*16 * i_mb_count * sizeof( int16_t ) );
-    h->mb.ref[0] = x264_malloc( 4 * i_mb_count * sizeof( int16_t ) );
-    h->mb.ref[1] = x264_malloc( 4 * i_mb_count * sizeof( int16_t ) );
-
     if( h->param.b_cabac )
     {
         h->mb.chroma_pred_mode = x264_malloc( i_mb_count * sizeof( int8_t) );
@@ -859,15 +852,10 @@ void x264_macroblock_cache_init( x264_t *h )
     }
 
     for( i=0; i<2; i++ )
-        for( j=0; j<16; j++ ) /* FIXME: alloc no more than param.i_frame_reference */
-            h->mb.mvr[i][j] = x264_malloc( 2 * i_mb_count * sizeof( int16_t ) );
-
-    h->mb.list1ref0.ref = NULL;
-    h->mb.list1ref0.mv = NULL;
-    if( h->param.i_bframe )
     {
-        h->mb.list1ref0.ref = x264_malloc( 4 * i_mb_count * sizeof( int8_t ) );
-        h->mb.list1ref0.mv = x264_malloc( 2*16 * i_mb_count * sizeof( int16_t ) );
+        int i_refs = (i ? 1 : h->param.i_frame_reference) + h->param.b_bframe_pyramid;
+        for( j=0; j < i_refs && j < 16; j++ )
+            h->mb.mvr[i][j] = x264_malloc( 2 * i_mb_count * sizeof( int16_t ) );
     }
 
     /* init with not avaiable (for top right idx=7,15) */
@@ -878,38 +866,69 @@ void x264_macroblock_cache_end( x264_t *h )
 {
     int i, j;
     for( i=0; i<2; i++ )
-        for( j=0; j<16; j++ )
+    {
+        int i_refs = i ? 1 + h->param.b_bframe_pyramid : h->param.i_frame_reference;
+        for( j=0; j < i_refs; j++ )
             x264_free( h->mb.mvr[i][j] );
+    }
     if( h->param.b_cabac )
     {
         x264_free( h->mb.chroma_pred_mode );
         x264_free( h->mb.mvd[0] );
         x264_free( h->mb.mvd[1] );
     }
-    if( h->param.i_bframe )
-    {
-        x264_free( h->mb.list1ref0.ref );
-        x264_free( h->mb.list1ref0.mv );
-    }
-    x264_free( h->mb.mv[0] );
-    x264_free( h->mb.mv[1] );
-    x264_free( h->mb.ref[0] );
-    x264_free( h->mb.ref[1] );
     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 );
-    x264_free( h->mb.type );
+}
+void x264_macroblock_slice_init( x264_t *h )
+{
+    int i, j;
+
+    h->mb.mv[0] = h->fdec->mv[0];
+    h->mb.mv[1] = h->fdec->mv[1];
+    h->mb.ref[0] = h->fdec->ref[0];
+    h->mb.ref[1] = h->fdec->ref[1];
+    h->mb.type = h->fdec->mb_type;
+
+    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;
+    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;
+
+        h->mb.map_col_to_list0[-1] = -1;
+        h->mb.map_col_to_list0[-2] = -2;
+        for( i = 0; i < h->fref1[0]->i_ref[0]; i++ )
+        {
+            int poc = h->fref1[0]->ref_poc[0][i];
+            h->mb.map_col_to_list0[i] = -2;
+            for( j = 0; j < h->i_ref0; j++ )
+                if( h->fref0[j]->i_poc == poc )
+                {
+                    h->mb.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 ) );
 }
 
 
 void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
 {
-    const int i_mb_4x4 = 16 * h->mb.i_mb_stride *i_mb_y + 4 * i_mb_x;
-    const int i_mb_8x8 =  4 * h->mb.i_mb_stride *i_mb_y + 2 * i_mb_x;
+    const int i_mb_4x4 = 4*(i_mb_y * h->mb.i_b4_stride + i_mb_x);
+    const int i_mb_8x8 = 2*(i_mb_y * h->mb.i_b8_stride + i_mb_x);
 
-    int i_top_xy = -1;
+    int i_mb_xy = i_mb_y * h->mb.i_mb_stride + i_mb_x;
+    int i_top_xy = i_mb_xy - h->mb.i_mb_stride;
     int i_left_xy = -1;
     int i_top_type = -1;    /* gcc warn */
     int i_left_type= -1;
@@ -919,36 +938,68 @@ void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
     /* 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_y * h->mb.i_mb_stride + i_mb_x;
+    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_neighbour = 0;
 
+    /* fdec:      fenc:
+     * yyyyyyy
+     * yYYYY      YYYY
+     * yYYYY      YYYY
+     * yYYYY      YYYY
+     * yYYYY      YYYY
+     * uuu vvv    UUVV
+     * uUU vVV    UUVV
+     * uUU vVV
+     */
+    h->mb.pic.p_fenc[0] = h->mb.pic.fenc_buf;
+    h->mb.pic.p_fenc[1] = h->mb.pic.fenc_buf + 16*FENC_STRIDE;
+    h->mb.pic.p_fenc[2] = h->mb.pic.fenc_buf + 16*FENC_STRIDE + 8;
+    h->mb.pic.p_fdec[0] = h->mb.pic.fdec_buf + 2*FDEC_STRIDE;
+    h->mb.pic.p_fdec[1] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE;
+    h->mb.pic.p_fdec[2] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE + 16;
+
     /* load picture pointers */
     for( i = 0; i < 3; i++ )
     {
         const int w = (i == 0 ? 16 : 8);
         const int i_stride = h->fdec->i_stride[i];
+        const uint8_t *plane_fdec = &h->fdec->plane[i][ w * (i_mb_x + i_mb_y * i_stride) ];
         int   j;
 
         h->mb.pic.i_stride[i] = i_stride;
 
-        h->mb.pic.p_fenc[i] = &h->fenc->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
-
-        h->mb.pic.p_fdec[i] = &h->fdec->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
+        h->mc.copy[i?PIXEL_8x8:PIXEL_16x16]( h->mb.pic.p_fenc[i], FENC_STRIDE,
+            &h->fenc->plane[i][ w * (i_mb_x + i_mb_y * i_stride) ], i_stride, w );
+        memcpy( &h->mb.pic.p_fdec[i][-1-FDEC_STRIDE], &plane_fdec[-1-i_stride], w*3/2+1 );
+        for( j = 0; j < w; j++ )
+            h->mb.pic.p_fdec[i][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride];
 
         for( j = 0; j < h->i_ref0; j++ )
         {
-            h->mb.pic.p_fref[0][j][i] = &h->fref0[j]->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
+            h->mb.pic.p_fref[0][j][i==0 ? 0:i+3] = &h->fref0[j]->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
+            h->mb.pic.p_fref[0][j][i+1] = &h->fref0[j]->filtered[i+1][ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
         }
         for( j = 0; j < h->i_ref1; j++ )
         {
-            h->mb.pic.p_fref[1][j][i] = &h->fref1[j]->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
+            h->mb.pic.p_fref[1][j][i==0 ? 0:i+3] = &h->fref1[j]->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
+            h->mb.pic.p_fref[1][j][i+1] = &h->fref1[j]->filtered[i+1][ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
         }
     }
 
+    if( h->fdec->integral )
+    {
+        for( i = 0; i < h->i_ref0; i++ )
+            h->mb.pic.p_integral[0][i] = &h->fref0[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
+        for( i = 0; i < h->i_ref1; i++ )
+            h->mb.pic.p_integral[1][i] = &h->fref1[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
+    }
+
     /* load cache */
-    if( i_mb_y > 0 )
+    if( i_mb_xy >= h->sh.i_first_mb + h->mb.i_mb_stride )
     {
-        i_top_xy  = h->mb.i_mb_xy - h->mb.i_mb_stride;
+        h->mb.i_mb_type_top =
         i_top_type= h->mb.type[i_top_xy];
 
         h->mb.i_neighbour |= MB_TOP;
@@ -973,6 +1024,8 @@ void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
     }
     else
     {
+        h->mb.i_mb_type_top = -1;
+        
         /* load intra4x4 */
         h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] =
         h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] =
@@ -991,10 +1044,11 @@ void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
 
     }
 
-    if( i_mb_x > 0 )
+    if( i_mb_x > 0 && i_mb_xy > h->sh.i_first_mb )
     {
-        i_left_xy  = h->mb.i_mb_xy - 1;
-        i_left_type= h->mb.type[i_left_xy];
+        i_left_xy = i_mb_xy - 1;
+        h->mb.i_mb_type_left =
+        i_left_type = h->mb.type[i_left_xy];
 
         h->mb.i_neighbour |= MB_LEFT;
 
@@ -1018,6 +1072,8 @@ void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
     }
     else
     {
+        h->mb.i_mb_type_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] =
@@ -1034,32 +1090,37 @@ void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
         h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = 0x80;
     }
 
-    if( i_mb_y > 0 && i_mb_x < h->sps->i_mb_width - 1 )
+    if( i_mb_x < h->sps->i_mb_width - 1 && i_top_xy + 1 >= h->sh.i_first_mb )
     {
         h->mb.i_neighbour |= MB_TOPRIGHT;
+        h->mb.i_mb_type_topright = h->mb.type[ i_top_xy + 1 ];
+    }
+    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 ];
+    }
+    else
+        h->mb.i_mb_type_topleft = -1;
+
+    if( h->param.analyse.b_transform_8x8 )
+    {
+        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]  );
     }
 
     /* load ref/mv/mvd */
     if( h->sh.i_type != SLICE_TYPE_I )
     {
-        int s8x8 = 2 * h->mb.i_mb_stride;
-        int s4x4 = 4 * h->mb.i_mb_stride;
-
-        int i_top_left_xy   = -1;
-        int i_top_right_xy  = -1;
+        const int s8x8 = h->mb.i_b8_stride;
+        const int s4x4 = h->mb.i_b4_stride;
 
         int i_list;
 
-        if( h->mb.i_mb_y > 0 && h->mb.i_mb_x > 0 )
-        {
-            i_top_left_xy   = i_top_xy - 1;
-        }
-        if( h->mb.i_mb_y > 0 && h->mb.i_mb_x < h->sps->i_mb_width - 1 )
-        {
-            i_top_right_xy = i_top_xy + 1;
-        }
-
-        for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_P ? 1  : 2 ); i_list++ )
+        for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2  : 1 ); i_list++ )
         {
             /*
             h->mb.cache.ref[i_list][x264_scan8[5 ]+1] =
@@ -1067,7 +1128,7 @@ void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
             h->mb.cache.ref[i_list][x264_scan8[13]+1] = -2;
             */
 
-            if( i_top_left_xy >= 0 )
+            if( h->mb.i_neighbour & MB_TOPLEFT )
             {
                 const int i8 = x264_scan8[0] - 1 - 1*8;
                 const int ir = i_mb_8x8 - s8x8 - 1;
@@ -1084,7 +1145,7 @@ void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
                 h->mb.cache.mv[i_list][i8][1] = 0;
             }
 
-            if( i_top_xy >= 0 )
+            if( h->mb.i_neighbour & MB_TOP )
             {
                 const int i8 = x264_scan8[0] - 8;
                 const int ir = i_mb_8x8 - s8x8;
@@ -1112,7 +1173,7 @@ void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
                 }
             }
 
-            if( i_top_right_xy >= 0 )
+            if( h->mb.i_neighbour & MB_TOPRIGHT )
             {
                 const int i8 = x264_scan8[0] + 4 - 1*8;
                 const int ir = i_mb_8x8 - s8x8 + 2;
@@ -1130,7 +1191,7 @@ void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
                 h->mb.cache.mv[i_list][i8][1] = 0;
             }
 
-            if( i_left_xy >= 0 )
+            if( h->mb.i_neighbour & MB_LEFT )
             {
                 const int i8 = x264_scan8[0] - 1;
                 const int ir = i_mb_8x8 - 1;
@@ -1160,7 +1221,7 @@ void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
 
             if( h->param.b_cabac )
             {
-                if( i_top_xy >= 0 )
+                if( i_top_type >= 0 )
                 {
                     const int i8 = x264_scan8[0] - 8;
                     const int iv = i_mb_4x4 - s4x4;
@@ -1180,7 +1241,7 @@ void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
                     }
                 }
 
-                if( i_left_xy >= 0 )
+                if( i_left_type >= 0 )
                 {
                     const int i8 = x264_scan8[0] - 1;
                     const int iv = i_mb_4x4 - 1;
@@ -1203,44 +1264,74 @@ void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
         }
 
         /* load skip */
-        if( h->param.b_cabac )
+        if( h->sh.i_type == SLICE_TYPE_B && h->param.b_cabac )
         {
-            if( h->sh.i_type == SLICE_TYPE_B )
+            memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
+            if( i_left_type >= 0 )
             {
-                memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
-                if( i_left_xy >= 0 )
-                {
-                    h->mb.cache.skip[x264_scan8[0] - 1] = h->mb.skipbp[i_left_xy] & 0x2;
-                    h->mb.cache.skip[x264_scan8[8] - 1] = h->mb.skipbp[i_left_xy] & 0x8;
-                }
-                if( i_top_xy >= 0 )
-                {
-                    h->mb.cache.skip[x264_scan8[0] - 8] = h->mb.skipbp[i_top_xy] & 0x4;
-                    h->mb.cache.skip[x264_scan8[4] - 8] = h->mb.skipbp[i_top_xy] & 0x8;
-                }
+                h->mb.cache.skip[x264_scan8[0] - 1] = h->mb.skipbp[i_left_xy] & 0x2;
+                h->mb.cache.skip[x264_scan8[8] - 1] = h->mb.skipbp[i_left_xy] & 0x8;
             }
-            else if( h->mb.i_mb_xy == 0 && h->sh.i_type == SLICE_TYPE_P )
+            if( i_top_type >= 0 )
             {
-                memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
+                h->mb.cache.skip[x264_scan8[0] - 8] = h->mb.skipbp[i_top_xy] & 0x4;
+                h->mb.cache.skip[x264_scan8[4] - 8] = h->mb.skipbp[i_top_xy] & 0x8;
             }
         }
     }
+
+    h->mb.i_neighbour4[0] =
+    h->mb.i_neighbour8[0] = (h->mb.i_neighbour & (MB_TOP|MB_LEFT|MB_TOPLEFT))
+                            | ((h->mb.i_neighbour & MB_TOP) ? MB_TOPRIGHT : 0);
+    h->mb.i_neighbour4[4] =
+    h->mb.i_neighbour4[1] = MB_LEFT | ((h->mb.i_neighbour & MB_TOP) ? (MB_TOP|MB_TOPLEFT|MB_TOPRIGHT) : 0);
+    h->mb.i_neighbour4[2] =
+    h->mb.i_neighbour4[8] =
+    h->mb.i_neighbour4[10] =
+    h->mb.i_neighbour8[2] = MB_TOP|MB_TOPRIGHT | ((h->mb.i_neighbour & MB_LEFT) ? (MB_LEFT|MB_TOPLEFT) : 0);
+    h->mb.i_neighbour4[3] =
+    h->mb.i_neighbour4[7] =
+    h->mb.i_neighbour4[11] =
+    h->mb.i_neighbour4[13] =
+    h->mb.i_neighbour4[15] =
+    h->mb.i_neighbour8[3] = MB_LEFT|MB_TOP|MB_TOPLEFT;
+    h->mb.i_neighbour4[5] =
+    h->mb.i_neighbour8[1] = MB_LEFT | (h->mb.i_neighbour & MB_TOPRIGHT)
+                            | ((h->mb.i_neighbour & MB_TOP) ? MB_TOP|MB_TOPLEFT : 0);
+    h->mb.i_neighbour4[6] =
+    h->mb.i_neighbour4[9] =
+    h->mb.i_neighbour4[12] =
+    h->mb.i_neighbour4[14] = MB_LEFT|MB_TOP|MB_TOPLEFT|MB_TOPRIGHT;
 }
 
 void x264_macroblock_cache_save( x264_t *h )
 {
     const int i_mb_xy = h->mb.i_mb_xy;
-    const int i_mb_type = h->mb.i_type;
-    const int i_mb_4x4 = 16 * h->mb.i_mb_stride * h->mb.i_mb_y + 4 * h->mb.i_mb_x;
-    const int i_mb_8x8 =  4 * h->mb.i_mb_stride * h->mb.i_mb_y + 2 * h->mb.i_mb_x;
+    const int i_mb_type = x264_mb_type_fix[h->mb.i_type];
+    const int s8x8 = h->mb.i_b8_stride;
+    const int s4x4 = h->mb.i_b4_stride;
+    const int i_mb_4x4 = h->mb.i_b4_xy;
+    const int i_mb_8x8 = h->mb.i_b8_xy;
 
     int i;
 
-    if( IS_SKIP( h->mb.i_type ) )
-        h->mb.qp[i_mb_xy] = h->mb.i_last_qp;
+    for( i = 0; i < 3; i++ )
+    {
+        int w = i ? 8 : 16;
+        h->mc.copy[i?PIXEL_8x8:PIXEL_16x16](
+            &h->fdec->plane[i][ w * (h->mb.i_mb_x + h->mb.i_mb_y * h->fdec->i_stride[i]) ],
+            h->fdec->i_stride[i],
+            h->mb.pic.p_fdec[i], FDEC_STRIDE, w );
+    }
+
+    h->mb.type[i_mb_xy] = i_mb_type;
+
+    if( h->mb.i_type != I_16x16 && h->mb.i_cbp_luma == 0 && h->mb.i_cbp_chroma == 0 )
+        h->mb.i_qp = h->mb.i_last_qp;
+    h->mb.qp[i_mb_xy] = h->mb.i_qp;
 
-    h->mb.i_last_dqp = h->mb.qp[i_mb_xy] - h->mb.i_last_qp;
-    h->mb.i_last_qp = h->mb.qp[i_mb_xy];
+    h->mb.i_last_dqp = h->mb.i_qp - h->mb.i_last_qp;
+    h->mb.i_last_qp = h->mb.i_qp;
 
     /* save intra4x4 */
     if( i_mb_type == I_4x4 )
@@ -1281,13 +1372,15 @@ void x264_macroblock_cache_save( x264_t *h )
         }
     }
 
+    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( !IS_INTRA( i_mb_type ) )
     {
         int i_list;
-        for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_P ? 1  : 2 ); i_list++ )
+        for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2  : 1 ); i_list++ )
         {
-            const int s8x8 = 2 * h->mb.i_mb_stride;
-            const int s4x4 = 4 * h->mb.i_mb_stride;
             int y,x;
 
             h->mb.ref[i_list][i_mb_8x8+0+0*s8x8] = h->mb.cache.ref[i_list][x264_scan8[0]];
@@ -1308,10 +1401,8 @@ void x264_macroblock_cache_save( x264_t *h )
     else
     {
         int i_list;
-        for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_P ? 1  : 2 ); i_list++ )
+        for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2  : 1 ); i_list++ )
         {
-            const int s8x8 = 2 * h->mb.i_mb_stride;
-            const int s4x4 = 4 * h->mb.i_mb_stride;
             int y,x;
 
             h->mb.ref[i_list][i_mb_8x8+0+0*s8x8] =
@@ -1333,7 +1424,7 @@ void x264_macroblock_cache_save( x264_t *h )
     if( h->param.b_cabac )
     {
         if( i_mb_type == I_4x4 || i_mb_type == I_16x16 )
-            h->mb.chroma_pred_mode[i_mb_xy] = h->mb.i_chroma_pred_mode;
+            h->mb.chroma_pred_mode[i_mb_xy] = x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ];
         else
             h->mb.chroma_pred_mode[i_mb_xy] = I_PRED_CHROMA_DC;
 
@@ -1388,10 +1479,34 @@ void x264_macroblock_cache_save( x264_t *h )
     }
 }
 
-void x264_macroblock_direct_ref_save( x264_t *h )
+void x264_macroblock_bipred_init( x264_t *h )
 {
-    /* Manipulation of ref numbers is unnecessary unless we allow
-     * ref list reordering, multiple B-frame delay, or B-frames as refs. */
-    memcpy( h->mb.list1ref0.ref, h->mb.ref[0], 4 * h->mb.i_mb_count * sizeof( int8_t ) );
-    memcpy( h->mb.list1ref0.mv, h->mb.mv[0], 2*16 * h->mb.i_mb_count * sizeof( int16_t ) );
+    int i_ref0, i_ref1;
+    for( i_ref0 = 0; i_ref0 < h->i_ref0; i_ref0++ )
+    {
+        int poc0 = h->fref0[i_ref0]->i_poc;
+        for( i_ref1 = 0; i_ref1 < h->i_ref1; i_ref1++ )
+        {
+            int dist_scale_factor;
+            int poc1 = h->fref1[i_ref1]->i_poc;
+            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( h->fdec->i_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[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[i_ref0][i_ref1] = 64 - dist_scale_factor;
+            else
+                h->mb.bipred_weight[i_ref0][i_ref1] = 32;
+        }
+    }
 }