/* 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;
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 )
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;
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 )
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 }
};