[x264-devel] commit: Abide by the MinCR level limit (Jason Garrett-Glaser )

git version control git at videolan.org
Tue Feb 23 11:05:36 CET 2010


x264 | branch: master | Jason Garrett-Glaser <darkshikari at gmail.com> | Sun Feb 21 13:20:19 2010 -0800| [a6c91c9f2af09d666755eef0ac2cc18228ce7a7c] | committer: Jason Garrett-Glaser 

Abide by the MinCR level limit
Some Blu-ray analyzers were complaining about this.

> http://git.videolan.org/gitweb.cgi/x264.git/?a=commit;h=a6c91c9f2af09d666755eef0ac2cc18228ce7a7c
---

 encoder/ratecontrol.c |   29 +++++++++++++++++++++++++++--
 encoder/set.c         |   32 ++++++++++++++++----------------
 x264.h                |    3 ++-
 3 files changed, 45 insertions(+), 19 deletions(-)

diff --git a/encoder/ratecontrol.c b/encoder/ratecontrol.c
index 3d86aaa..d0fdb50 100644
--- a/encoder/ratecontrol.c
+++ b/encoder/ratecontrol.c
@@ -136,6 +136,7 @@ struct x264_ratecontrol_t
     /* MBRC stuff */
     float frame_size_estimated; /* Access to this variable must be atomic: double is
                                  * not atomic on all arches we care about */
+    double frame_size_maximum;  /* Maximum frame size due to MinCR */
     double frame_size_planned;
     double slice_size_planned;
     double max_frame_error;
@@ -1039,6 +1040,24 @@ void x264_ratecontrol_start( x264_t *h, int i_force_qp, int overhead )
         memset( h->fdec->i_row_bits, 0, h->sps->i_mb_height * sizeof(int) );
         rc->row_pred = &rc->row_preds[h->sh.i_type];
         update_vbv_plan( h, overhead );
+
+        const x264_level_t *l = x264_levels;
+        while( l->level_idc != 0 && l->level_idc != h->param.i_level_idc )
+            l++;
+
+        /* The spec has a bizarre special case for the first frame. */
+        if( h->i_frame == 0 )
+        {
+            //384 * ( Max( PicSizeInMbs, fR * MaxMBPS ) + MaxMBPS * ( tr( 0 ) - tr,n( 0 ) ) ) / MinCR
+            double fr = 1. / 172;
+            int pic_size_in_mbs = h->sps->i_mb_width * h->sps->i_mb_height;
+            rc->frame_size_maximum = 384 * 8 * X264_MAX( pic_size_in_mbs, fr*l->mbps ) / l->mincr;
+        }
+        else
+        {
+            //384 * MaxMBPS * ( tr( n ) - tr( n - 1 ) ) / MinCR
+            rc->frame_size_maximum = 384 * 8 * (1 / rc->fps) * l->mbps / l->mincr;
+        }
     }
 
     if( h->sh.i_type != SLICE_TYPE_B )
@@ -1220,9 +1239,10 @@ void x264_ratecontrol_mb( x264_t *h, int bits )
             b1 = predict_row_size_sum( h, y, rc->qpm ) + size_of_other_slices;
         }
 
-        /* avoid VBV underflow */
+        /* avoid VBV underflow or MinCR violation */
         while( (rc->qpm < h->param.rc.i_qp_max)
-               && (rc->buffer_fill - b1 < rc->buffer_rate * rc->max_frame_error) )
+               && ((rc->buffer_fill - b1 < rc->buffer_rate * rc->max_frame_error) ||
+                   (rc->frame_size_maximum - b1 < rc->frame_size_maximum * rc->max_frame_error)))
         {
             rc->qpm ++;
             b1 = predict_row_size_sum( h, y, rc->qpm ) + size_of_other_slices;
@@ -1677,6 +1697,11 @@ static double clip_qscale( x264_t *h, int pict_type, double q )
             q = X264_MAX( q0, q );
         }
 
+        /* Apply MinCR restrictions */
+        double bits = predict_size( &rcc->pred[h->sh.i_type], q, rcc->last_satd );
+        if( bits > rcc->frame_size_maximum )
+            q *= bits / rcc->frame_size_maximum;
+
         /* Check B-frame complexity, and use up any bits that would
          * overflow before the next P-frame. */
         if( h->sh.i_type == SLICE_TYPE_P && !rcc->single_frame_vbv )
diff --git a/encoder/set.c b/encoder/set.c
index f79919b..03a6dee 100644
--- a/encoder/set.c
+++ b/encoder/set.c
@@ -536,22 +536,22 @@ fail:
 
 const x264_level_t x264_levels[] =
 {
-    { 10,   1485,    99,   152064,     64,    175,  64, 64,  0, 0, 0, 1 },
-//  {"1b",  1485,    99,   152064,    128,    350,  64, 64,  0, 0, 0, 1 },
-    { 11,   3000,   396,   345600,    192,    500, 128, 64,  0, 0, 0, 1 },
-    { 12,   6000,   396,   912384,    384,   1000, 128, 64,  0, 0, 0, 1 },
-    { 13,  11880,   396,   912384,    768,   2000, 128, 64,  0, 0, 0, 1 },
-    { 20,  11880,   396,   912384,   2000,   2000, 128, 64,  0, 0, 0, 1 },
-    { 21,  19800,   792,  1824768,   4000,   4000, 256, 64,  0, 0, 0, 0 },
-    { 22,  20250,  1620,  3110400,   4000,   4000, 256, 64,  0, 0, 0, 0 },
-    { 30,  40500,  1620,  3110400,  10000,  10000, 256, 32, 22, 0, 1, 0 },
-    { 31, 108000,  3600,  6912000,  14000,  14000, 512, 16, 60, 1, 1, 0 },
-    { 32, 216000,  5120,  7864320,  20000,  20000, 512, 16, 60, 1, 1, 0 },
-    { 40, 245760,  8192, 12582912,  20000,  25000, 512, 16, 60, 1, 1, 0 },
-    { 41, 245760,  8192, 12582912,  50000,  62500, 512, 16, 24, 1, 1, 0 },
-    { 42, 522240,  8704, 13369344,  50000,  62500, 512, 16, 24, 1, 1, 1 },
-    { 50, 589824, 22080, 42393600, 135000, 135000, 512, 16, 24, 1, 1, 1 },
-    { 51, 983040, 36864, 70778880, 240000, 240000, 512, 16, 24, 1, 1, 1 },
+    { 10,   1485,    99,   152064,     64,    175,  64, 64,  0, 2, 0, 0, 1 },
+//  {"1b",  1485,    99,   152064,    128,    350,  64, 64,  0, 2, 0, 0, 1 },
+    { 11,   3000,   396,   345600,    192,    500, 128, 64,  0, 2, 0, 0, 1 },
+    { 12,   6000,   396,   912384,    384,   1000, 128, 64,  0, 2, 0, 0, 1 },
+    { 13,  11880,   396,   912384,    768,   2000, 128, 64,  0, 2, 0, 0, 1 },
+    { 20,  11880,   396,   912384,   2000,   2000, 128, 64,  0, 2, 0, 0, 1 },
+    { 21,  19800,   792,  1824768,   4000,   4000, 256, 64,  0, 2, 0, 0, 0 },
+    { 22,  20250,  1620,  3110400,   4000,   4000, 256, 64,  0, 2, 0, 0, 0 },
+    { 30,  40500,  1620,  3110400,  10000,  10000, 256, 32, 22, 2, 0, 1, 0 },
+    { 31, 108000,  3600,  6912000,  14000,  14000, 512, 16, 60, 4, 1, 1, 0 },
+    { 32, 216000,  5120,  7864320,  20000,  20000, 512, 16, 60, 4, 1, 1, 0 },
+    { 40, 245760,  8192, 12582912,  20000,  25000, 512, 16, 60, 4, 1, 1, 0 },
+    { 41, 245760,  8192, 12582912,  50000,  62500, 512, 16, 24, 2, 1, 1, 0 },
+    { 42, 522240,  8704, 13369344,  50000,  62500, 512, 16, 24, 2, 1, 1, 1 },
+    { 50, 589824, 22080, 42393600, 135000, 135000, 512, 16, 24, 2, 1, 1, 1 },
+    { 51, 983040, 36864, 70778880, 240000, 240000, 512, 16, 24, 2, 1, 1, 1 },
     { 0 }
 };
 
diff --git a/x264.h b/x264.h
index dec296c..7474a50 100644
--- a/x264.h
+++ b/x264.h
@@ -35,7 +35,7 @@
 
 #include <stdarg.h>
 
-#define X264_BUILD 87
+#define X264_BUILD 88
 
 /* x264_t:
  *      opaque handler for encoder */
@@ -346,6 +346,7 @@ typedef struct {
     int mv_range;    /* max vertical mv component range (pixels) */
     int mvs_per_2mb; /* max mvs per 2 consecutive mbs. */
     int slice_rate;  /* ?? */
+    int mincr;       /* min compression ratio */
     int bipred8x8;   /* limit bipred to >=8x8 */
     int direct8x8;   /* limit b_direct to >=8x8 */
     int frame_only;  /* forbid interlacing */



More information about the x264-devel mailing list