diff --git a/encoder/ratecontrol.c b/encoder/ratecontrol.c index b082cc7..ae26d38 100644 --- a/encoder/ratecontrol.c +++ b/encoder/ratecontrol.c @@ -546,17 +546,18 @@ int x264_ratecontrol_new( x264_t *h ) x264_log( h, X264_LOG_WARNING, "2nd pass has fewer frames than 1st pass (%d vs %d)\n", h->param.i_frame_total, rc->num_entries ); } - if( h->param.i_frame_total > rc->num_entries ) + if( h->param.i_frame_total > rc->num_entries + 2*X264_LOOKAHEAD_MAX ) { x264_log( h, X264_LOG_ERROR, "2nd pass has more frames than 1st pass (%d vs %d)\n", h->param.i_frame_total, rc->num_entries ); return -1; } - CHECKED_MALLOCZERO( rc->entry, rc->num_entries * sizeof(ratecontrol_entry_t) ); + /* FIXME: ugly padding because VfW can drop delayed frames */ + CHECKED_MALLOCZERO( rc->entry, (rc->num_entries + 2*X264_LOOKAHEAD_MAX) * sizeof(ratecontrol_entry_t) ); /* init all to skipped p frames */ - for(i=0; inum_entries; i++) + for(i=0; inum_entries + 2*X264_LOOKAHEAD_MAX; i++) { ratecontrol_entry_t *rce = &rc->entry[i]; rce->pict_type = SLICE_TYPE_P; @@ -584,7 +585,7 @@ int x264_ratecontrol_new( x264_t *h ) } e = sscanf(p, " in:%d ", &frame_number); - if(frame_number < 0 || frame_number >= rc->num_entries) + if(frame_number < 0 || frame_number >= rc->num_entries + 2*X264_LOOKAHEAD_MAX) { x264_log(h, X264_LOG_ERROR, "bad frame number (%d) at stats line %d\n", frame_number, i); return -1; @@ -905,7 +906,7 @@ void x264_ratecontrol_start( x264_t *h, int i_force_qp, int overhead ) if( h->param.rc.b_stat_read ) { int frame = h->fenc->i_frame; - assert( frame >= 0 && frame < rc->num_entries ); + assert( frame >= 0 && frame < rc->num_entries + 2*X264_LOOKAHEAD_MAX ); rce = h->rc->rce = &h->rc->entry[frame]; if( h->sh.i_type == SLICE_TYPE_B @@ -1118,7 +1119,7 @@ int x264_ratecontrol_slice_type( x264_t *h, int frame_num ) x264_ratecontrol_t *rc = h->rc; if( h->param.rc.b_stat_read ) { - if( frame_num >= rc->num_entries ) + if( frame_num >= rc->num_entries + 2*X264_LOOKAHEAD_MAX ) { /* We could try to initialize everything required for ABR and * adaptive B-frames, but that would be complicated.