]> git.sesse.net Git - x264/blobdiff - encoder/analyse.c
mkv: Write the x264 version into the file header
[x264] / encoder / analyse.c
index 96d95e53f3efff08f8313d2ec1f6431e9ef6ad9a..8606eb84cf0ddea927e777b26333f3642ed8b15e 100644 (file)
@@ -362,6 +362,8 @@ static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp )
 
     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->mb.b_dct_decimate = h->sh.i_type == SLICE_TYPE_B ||
@@ -2305,9 +2307,10 @@ static inline void x264_mb_analyse_qp_rd( x264_t *h, x264_mb_analysis_t *a )
     int orig_qp = h->mb.i_qp, bqp = h->mb.i_qp;
     int last_qp_tried = 0;
     origcost = bcost = x264_rd_cost_mb( h, a->i_lambda2 );
+    int origcbp = h->mb.cbp[h->mb.i_mb_xy];
 
     /* If CBP is already zero, don't raise the quantizer any higher. */
-    for( direction = h->mb.cbp[h->mb.i_mb_xy] ? 1 : -1; direction >= -1; direction-=2 )
+    for( direction = origcbp ? 1 : -1; direction >= -1; direction-=2 )
     {
         /* Without psy-RD, require monotonicity when moving quant away from previous
          * macroblock's quant; allow 1 failure when moving quant towards previous quant.
@@ -2322,14 +2325,47 @@ static inline void x264_mb_analyse_qp_rd( x264_t *h, x264_mb_analysis_t *a )
         h->mb.i_qp = orig_qp;
         failures = 0;
         prevcost = origcost;
+
+        /* If the current QP results in an empty CBP, it's highly likely that lower QPs
+         * (up to a point) will too.  So, jump down to where the threshold will kick in
+         * and check the QP there.  If the CBP is still empty, skip the main loop.
+         * If it isn't empty, we would have ended up having to check this QP anyways,
+         * so as long as we store it for later lookup, we lose nothing. */
+        int already_checked_qp = -1;
+        int already_checked_cost = COST_MAX;
+        if( direction == -1 )
+        {
+            if( !origcbp )
+            {
+                h->mb.i_qp = X264_MAX( h->mb.i_qp - threshold - 1, h->param.rc.i_qp_min );
+                h->mb.i_chroma_qp = h->chroma_qp_table[h->mb.i_qp];
+                already_checked_cost = x264_rd_cost_mb( h, a->i_lambda2 );
+                if( !h->mb.cbp[h->mb.i_mb_xy] )
+                {
+                    /* If our empty-CBP block is lower QP than the last QP,
+                     * the last QP almost surely doesn't have a CBP either. */
+                    if( h->mb.i_last_qp > h->mb.i_qp )
+                        last_qp_tried = 1;
+                    break;
+                }
+                already_checked_qp = h->mb.i_qp;
+                h->mb.i_qp = orig_qp;
+            }
+        }
+
         h->mb.i_qp += direction;
         while( h->mb.i_qp >= h->param.rc.i_qp_min && h->mb.i_qp <= h->param.rc.i_qp_max )
         {
             if( h->mb.i_last_qp == h->mb.i_qp )
                 last_qp_tried = 1;
-            h->mb.i_chroma_qp = h->chroma_qp_table[h->mb.i_qp];
-            cost = x264_rd_cost_mb( h, a->i_lambda2 );
-            COPY2_IF_LT( bcost, cost, bqp, h->mb.i_qp );
+            if( h->mb.i_qp == already_checked_qp )
+                cost = already_checked_cost;
+            else
+            {
+                h->mb.i_chroma_qp = h->chroma_qp_table[h->mb.i_qp];
+                cost = x264_rd_cost_mb( h, a->i_lambda2 );
+                COPY2_IF_LT( bcost, cost, bqp, h->mb.i_qp );
+            }
 
             /* We can't assume that the costs are monotonic over QPs.
              * Tie case-as-failure seems to give better results. */