]> git.sesse.net Git - x264/blobdiff - encoder/slicetype.c
Fix glitches with slow-firstpass + weightb + multiref + 2pass
[x264] / encoder / slicetype.c
index 439bf03a439c28c37b238db9de4c474c05b03e57..e9c70089230169e0dc813f854ded29892c15c1ba 100644 (file)
@@ -349,7 +349,7 @@ static int x264_slicetype_frame_cost( x264_t *h, x264_mb_analysis_t *a,
         }
 
         if( b != p1 )
-            i_score = i_score * 100 / (120 + h->param.i_bframe_bias);
+            i_score = (uint64_t)i_score * 100 / (120 + h->param.i_bframe_bias);
         else
             frames[b]->b_intra_calculated = 1;
 
@@ -621,23 +621,21 @@ static void x264_slicetype_path( x264_t *h, x264_mb_analysis_t *a, x264_frame_t
 {
     char paths[X264_BFRAME_MAX+2][X264_LOOKAHEAD_MAX] = {{0}};
     int num_paths = X264_MIN(max_bframes+1, length);
-    int suffix_size, loc, path;
+    int path;
     int best_cost = COST_MAX;
     int best_path_index = 0;
-    length = X264_MIN(length,X264_LOOKAHEAD_MAX);
+    length = X264_MIN( length, X264_LOOKAHEAD_MAX );
 
-    /* Iterate over all currently possible paths and add suffixes to each one */
-    for( suffix_size = 0; suffix_size < num_paths; suffix_size++ )
-    {
-        memcpy( paths[suffix_size], best_paths[length - (suffix_size + 1)], length - (suffix_size + 1) );
-        for( loc = 0; loc < suffix_size; loc++ )
-            strcat( paths[suffix_size], "B" );
-        strcat( paths[suffix_size], "P" );
-    }
-
-    /* Calculate the actual cost of each of the current paths */
+    /* Iterate over all currently possible paths */
     for( path = 0; path < num_paths; path++ )
     {
+        /* Add suffixes to the current path */
+        int len = length - (path + 1);
+        memcpy( paths[path], best_paths[len], len );
+        memset( paths[path]+len, 'B', path );
+        strcat( paths[path], "P" );
+
+        /* Calculate the actual cost of the current path */
         int cost = x264_slicetype_path_cost( h, a, frames, paths[path], best_cost );
         if( cost < best_cost )
         {
@@ -697,7 +695,7 @@ static int scenecut( x264_t *h, x264_mb_analysis_t *a, x264_frame_t **frames, in
     int curp0, curp1, i, maxp1 = p0 + 1;
 
     /* Only do analysis during a normal scenecut check. */
-    if( real_scenecut )
+    if( real_scenecut && h->param.i_bframe )
     {
         /* Look ahead to avoid coding short flashes as scenecuts. */
         if( h->param.i_bframe_adaptive == X264_B_ADAPT_TRELLIS )
@@ -997,9 +995,19 @@ void x264_slicetype_decide( x264_t *h )
 
         x264_slicetype_frame_cost( h, &a, frames, p0, p1, b, 0 );
 
-        /* We need the intra costs for row SATDs. */
         if( b && h->param.rc.i_vbv_buffer_size )
+        {
+            /* We need the intra costs for row SATDs. */
             x264_slicetype_frame_cost( h, &a, frames, b, b, b, 0 );
+
+            /* We need B-frame costs for row SATDs. */
+            for( i = 0; i < bframes; i++ )
+            {
+                b = bframes - i;
+                frames[b] = h->lookahead->next.list[i];
+                x264_slicetype_frame_cost( h, &a, frames, p0, p1, b, 0 );
+            }
+        }
     }
 }
 
@@ -1011,8 +1019,14 @@ int x264_rc_analyse_slice( x264_t *h )
 
     if( IS_X264_TYPE_I(h->fenc->i_type) )
         p1 = b = 0;
-    else // P
+    else if( h->fenc->i_type == X264_TYPE_P )
         p1 = b = h->fenc->i_bframes + 1;
+    else //B
+    {
+        p1 = (h->fref1[0]->i_poc - h->fref0[0]->i_poc)/2;
+        b  = (h->fref1[0]->i_poc - h->fenc->i_poc)/2;
+        frames[p1] = h->fref1[0];
+    }
     frames[p0] = h->fref0[0];
     frames[b] = h->fenc;