Detect spatial direct MBs partitioned smaller than 16x16 that can be partitioned
[ffmpeg.git] / libavcodec / h264_direct.c
index 35fa1e7..a2f1e1d 100644 (file)
@@ -30,7 +30,6 @@
 #include "avcodec.h"
 #include "mpegvideo.h"
 #include "h264.h"
-#include "h264_mvpred.h"
 #include "rectangle.h"
 
 //#undef NDEBUG
@@ -221,18 +220,39 @@ single_col:
         int mv[2][2];
         int list;
 
-        /* FIXME interlacing + spatial direct uses wrong colocated block positions */
-
         /* ref = min(neighbors) */
         for(list=0; list<2; list++){
-            int refa = h->ref_cache[list][scan8[0] - 1];
-            int refb = h->ref_cache[list][scan8[0] - 8];
+            int left_ref = h->ref_cache[list][scan8[0] - 1];
+            int top_ref  = h->ref_cache[list][scan8[0] - 8];
             int refc = h->ref_cache[list][scan8[0] - 8 + 4];
-            if(refc == PART_NOT_AVAILABLE)
+            const int16_t *C= h->mv_cache[list][ scan8[0] - 8 + 4];
+            if(refc == PART_NOT_AVAILABLE){
                 refc = h->ref_cache[list][scan8[0] - 8 - 1];
-            ref[list] = FFMIN3((unsigned)refa, (unsigned)refb, (unsigned)refc);
+                C    = h-> mv_cache[list][scan8[0] - 8 - 1];
+            }
+            ref[list] = FFMIN3((unsigned)left_ref, (unsigned)top_ref, (unsigned)refc);
             if(ref[list] >= 0){
-                pred_motion(h, 0, 4, list, ref[list], &mv[list][0], &mv[list][1]);
+                //this is just pred_motion() but with the cases removed that cannot happen for direct blocks
+                const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
+                const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
+
+                int match_count= (left_ref==ref[list]) + (top_ref==ref[list]) + (refc==ref[list]);
+                if(match_count > 1){ //most common
+                    mv[list][0]= mid_pred(A[0], B[0], C[0]);
+                    mv[list][1]= mid_pred(A[1], B[1], C[1]);
+                }else {
+                    assert(match_count==1);
+                    if(left_ref==ref[list]){
+                        mv[list][0]= A[0];
+                        mv[list][1]= A[1];
+                    }else if(top_ref==ref[list]){
+                        mv[list][0]= B[0];
+                        mv[list][1]= B[1];
+                    }else{
+                        mv[list][0]= C[0];
+                        mv[list][1]= C[1];
+                    }
+                }
             }else{
                 int mask= ~(MB_TYPE_L0 << (2*list));
                 mv[list][0] = mv[list][1] = 0;
@@ -286,7 +306,7 @@ single_col:
             if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref
                && (   (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1)
                    || (l1ref0[0]  < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1
-                       && (h->x264_build>33 || !h->x264_build)))){
+                       && h->x264_build>33U))){
                 a=b=0;
                 if(ref[0] > 0)
                     a= pack16to32(mv[0][0],mv[0][1]);
@@ -299,6 +319,7 @@ single_col:
             fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
             fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
         }else{
+            int n=0;
             for(i8=0; i8<4; i8++){
                 const int x8 = i8&1;
                 const int y8 = i8>>1;
@@ -315,7 +336,7 @@ single_col:
                 /* col_zero_flag */
                 if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref && (   l1ref0[x8 + y8*b8_stride] == 0
                                               || (l1ref0[x8 + y8*b8_stride] < 0 && l1ref1[x8 + y8*b8_stride] == 0
-                                                  && (h->x264_build>33 || !h->x264_build)))){
+                                                  && h->x264_build>33U))){
                     const int16_t (*l1mv)[2]= l1ref0[x8 + y8*b8_stride] == 0 ? l1mv0 : l1mv1;
                     if(IS_SUB_8X8(sub_mb_type)){
                         const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
@@ -324,8 +345,10 @@ single_col:
                                 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
                             if(ref[1] == 0)
                                 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
+                            n+=4;
                         }
-                    }else
+                    }else{
+                        int m=0;
                     for(i4=0; i4<4; i4++){
                         const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
                         if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
@@ -333,23 +356,29 @@ single_col:
                                 *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0;
                             if(ref[1] == 0)
                                 *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0;
+                            m++;
                         }
                     }
+                    if(!(m&3))
+                        h->sub_mb_type[i8]+= MB_TYPE_16x16 - MB_TYPE_8x8;
+                    n+=m;
+                    }
                 }
             }
+            if(!is_b8x8 && !(n&15))
+                *mb_type= (*mb_type & ~(MB_TYPE_8x8|MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_P1L0|MB_TYPE_P1L1))|MB_TYPE_16x16|MB_TYPE_DIRECT2;
         }
     }else{ /* direct temporal mv pred */
         const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
         const int *dist_scale_factor = h->dist_scale_factor;
-        int ref_offset= 0;
+        int ref_offset;
 
         if(FRAME_MBAFF && IS_INTERLACED(*mb_type)){
             map_col_to_list0[0] = h->map_col_to_list0_field[s->mb_y&1][0];
             map_col_to_list0[1] = h->map_col_to_list0_field[s->mb_y&1][1];
             dist_scale_factor   =h->dist_scale_factor_field[s->mb_y&1];
         }
-        if(h->ref_list[1][0].mbaff && IS_INTERLACED(mb_type_col[0]))
-            ref_offset += 16;
+        ref_offset = (h->ref_list[1][0].mbaff<<4) & (mb_type_col[0]>>3); //if(h->ref_list[1][0].mbaff && IS_INTERLACED(mb_type_col[0])) ref_offset=16 else 0
 
         if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
             int y_shift  = 2*!IS_INTERLACED(*mb_type);