]> git.sesse.net Git - x264/commitdiff
MBAFF: Modify ratecontrol to update every two rows
authorSimon Horlick <simonhorlick@gmail.com>
Fri, 25 Mar 2011 13:36:21 +0000 (13:36 +0000)
committerFiona Glaser <fiona@x264.com>
Thu, 12 May 2011 06:07:01 +0000 (23:07 -0700)
encoder/encoder.c
encoder/ratecontrol.c

index 599b7032a3eb7103d756fb506ac7209971b7b83a..b90df68f4ad753e1169d41f4f16323d48f58b0ff 100644 (file)
@@ -2012,6 +2012,7 @@ static int x264_slice_write( x264_t *h )
     i_mb_y = h->sh.i_first_mb / h->mb.i_mb_width;
     i_mb_x = h->sh.i_first_mb % h->mb.i_mb_width;
     i_skip = 0;
+    int mb_size[2];
 
     while( 1 )
     {
@@ -2113,7 +2114,7 @@ reencode:
         }
 
         int total_bits = bs_pos(&h->out.bs) + x264_cabac_pos(&h->cabac);
-        int mb_size = total_bits - mb_spos;
+        mb_size[i_mb_y&1] = total_bits - mb_spos;
 
         if( slice_max_size )
         {
@@ -2233,7 +2234,14 @@ reencode:
         if( b_deblock )
             x264_macroblock_deblock_strength( h );
 
-        x264_ratecontrol_mb( h, mb_size );
+        if( h->sh.b_mbaff )
+        {
+            /* update ratecontrol per-mbpair in MBAFF */
+            if( i_mb_y&1 )
+                x264_ratecontrol_mb( h, mb_size[0]+mb_size[1] );
+        }
+        else
+            x264_ratecontrol_mb( h, mb_size[i_mb_y&1] );
 
         if( mb_xy == h->sh.i_last_mb )
             break;
index 2faee25f2ab751d49067ecad15291023e6ca4ae3..e7db2a331e8f9073164c697a008f73fd18796fd2 100644 (file)
@@ -1335,7 +1335,7 @@ static double predict_row_size( x264_t *h, int y, double qp )
 static double row_bits_so_far( x264_t *h, int y )
 {
     double bits = 0;
-    for( int i = h->i_threadslice_start; i <= y; i++ )
+    for( int i = h->i_threadslice_start+h->sh.b_mbaff; i <= y; i+=(h->sh.b_mbaff+1) )
         bits += h->fdec->i_row_bits[i];
     return bits;
 }
@@ -1343,7 +1343,7 @@ static double row_bits_so_far( x264_t *h, int y )
 static double predict_row_size_sum( x264_t *h, int y, double qp )
 {
     double bits = row_bits_so_far(h, y);
-    for( int i = y+1; i < h->i_threadslice_end; i++ )
+    for( int i = y+1+h->sh.b_mbaff; i < h->i_threadslice_end; i+=(1+h->sh.b_mbaff) )
         bits += predict_row_size( h, i, qp );
     return bits;
 }
@@ -1357,8 +1357,16 @@ void x264_ratecontrol_mb( x264_t *h, int bits )
     x264_emms();
 
     h->fdec->i_row_bits[y] += bits;
-    rc->qpa_rc += rc->qpm;
-    rc->qpa_aq += h->mb.i_qp;
+    if( h->sh.b_mbaff )
+    {
+        rc->qpa_rc += rc->qpm*2.0f;
+        rc->qpa_aq += h->mb.i_qp + h->mb.i_last_qp;
+    }
+    else
+    {
+        rc->qpa_rc += rc->qpm;
+        rc->qpa_aq += h->mb.i_qp;
+    }
 
     if( h->mb.i_mb_x != h->mb.i_mb_width - 1 || !rc->b_vbv )
         return;
@@ -1383,7 +1391,7 @@ void x264_ratecontrol_mb( x264_t *h, int bits )
         /* B-frames shouldn't use lower QP than their reference frames. */
         if( h->sh.i_type == SLICE_TYPE_B )
         {
-            qp_min = X264_MAX( qp_min, X264_MAX( h->fref[0][0]->f_row_qp[y+1], h->fref[1][0]->f_row_qp[y+1] ) );
+            qp_min = X264_MAX( qp_min, X264_MAX( h->fref[0][0]->f_row_qp[y+1+h->sh.b_mbaff], h->fref[1][0]->f_row_qp[y+1+h->sh.b_mbaff] ) );
             rc->qpm = X264_MAX( rc->qpm, qp_min );
         }