From: Loren Merritt Date: Wed, 26 Oct 2005 07:04:59 +0000 (+0000) Subject: If frame count is known at init time (cli & vfw), then abort if the 2nd pass X-Git-Url: http://www.zmx.ovh/git.videolan.org/?a=commitdiff_plain;h=166601503800e00a33d88eb488da744a486ecb77;p=x264.git If frame count is known at init time (cli & vfw), then abort if the 2nd pass exceeds the length of the 1st pass. If it's not known (mencoder), then report a non-fatal error when we run off the end of the 1st pass stats, and switch to constant QP. git-svn-id: svn://svn.videolan.org/x264/trunk@340 df754926-b1dd-0310-bc7b-ec298dee348c --- diff --git a/encoder/encoder.c b/encoder/encoder.c index 0618761c..43964e27 100644 --- a/encoder/encoder.c +++ b/encoder/encoder.c @@ -382,6 +382,7 @@ static int x264_validate_parameters( x264_t *h ) h->param.i_bframe = x264_clip3( h->param.i_bframe, 0, X264_BFRAME_MAX ); h->param.i_bframe_bias = x264_clip3( h->param.i_bframe_bias, -90, 100 ); h->param.b_bframe_pyramid = h->param.b_bframe_pyramid && h->param.i_bframe > 1; + h->param.b_bframe_adaptive = h->param.b_bframe_adaptive && h->param.i_bframe > 0; h->param.i_deblocking_filter_alphac0 = x264_clip3( h->param.i_deblocking_filter_alphac0, -6, 6 ); h->param.i_deblocking_filter_beta = x264_clip3( h->param.i_deblocking_filter_beta, -6, 6 ); diff --git a/encoder/ratecontrol.c b/encoder/ratecontrol.c index 8650a103..a349217d 100644 --- a/encoder/ratecontrol.c +++ b/encoder/ratecontrol.c @@ -277,11 +277,25 @@ int x264_ratecontrol_new( x264_t *h ) x264_log(h, X264_LOG_ERROR, "empty stats file\n"); return -1; } - i += h->param.i_bframe; - rc->entry = (ratecontrol_entry_t*) x264_malloc(i*sizeof(ratecontrol_entry_t)); - memset(rc->entry, 0, i*sizeof(ratecontrol_entry_t)); - /* FIXME: num_entries is sometimes treated as number of frames in the video */ - rc->num_entries= i; + rc->num_entries = i; + + if( h->param.i_frame_total < rc->num_entries ) + { + 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 + h->param.i_bframe ) + { + 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; + } + + /* FIXME: ugly padding because VfW drops delayed B-frames */ + rc->num_entries += h->param.i_bframe; + + rc->entry = (ratecontrol_entry_t*) x264_malloc(rc->num_entries * sizeof(ratecontrol_entry_t)); + memset(rc->entry, 0, rc->num_entries * sizeof(ratecontrol_entry_t)); /* init all to skipped p frames */ for(i=0; inum_entries; i++){ @@ -495,20 +509,42 @@ int x264_ratecontrol_qp( x264_t *h ) /* In 2pass, force the same frame types as in the 1st pass */ 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 >= h->rc->num_entries ) + if( frame_num >= rc->num_entries ) { - x264_log(h, X264_LOG_ERROR, "More input frames than in the 1st pass\n"); + /* We could try to initialize everything required for ABR and + * adaptive B-frames, but that would be complicated. + * So just calculate the average QP used so far. */ + + h->param.rc.i_qp_constant = (h->stat.i_slice_count[SLICE_TYPE_P] == 0) ? 24 + : 1 + h->stat.i_slice_qp[SLICE_TYPE_P] / h->stat.i_slice_count[SLICE_TYPE_P]; + rc->qp_constant[SLICE_TYPE_P] = x264_clip3( h->param.rc.i_qp_constant, 0, 51 ); + rc->qp_constant[SLICE_TYPE_I] = x264_clip3( (int)( qscale2qp( qp2qscale( h->param.rc.i_qp_constant ) / fabs( h->param.rc.f_ip_factor )) + 0.5 ), 0, 51 ); + rc->qp_constant[SLICE_TYPE_B] = x264_clip3( (int)( qscale2qp( qp2qscale( h->param.rc.i_qp_constant ) * fabs( h->param.rc.f_pb_factor )) + 0.5 ), 0, 51 ); + + x264_log(h, X264_LOG_ERROR, "2nd pass has more frames than 1st pass (%d)\n", rc->num_entries); + x264_log(h, X264_LOG_ERROR, "continuing anyway, at constant QP=%d\n", h->param.rc.i_qp_constant); + if( h->param.b_bframe_adaptive ) + x264_log(h, X264_LOG_ERROR, "disabling adaptive B-frames\n"); + + rc->b_abr = 0; + rc->b_2pass = 0; + h->param.rc.b_cbr = 0; + h->param.rc.b_stat_read = 0; + h->param.b_bframe_adaptive = 0; + if( h->param.i_bframe > 1 ) + h->param.i_bframe = 1; return X264_TYPE_P; } - switch( h->rc->entry[frame_num].pict_type ) + switch( rc->entry[frame_num].pict_type ) { case SLICE_TYPE_I: - return h->rc->entry[frame_num].kept_as_ref ? X264_TYPE_IDR : X264_TYPE_I; + return rc->entry[frame_num].kept_as_ref ? X264_TYPE_IDR : X264_TYPE_I; case SLICE_TYPE_B: - return h->rc->entry[frame_num].kept_as_ref ? X264_TYPE_BREF : X264_TYPE_B; + return rc->entry[frame_num].kept_as_ref ? X264_TYPE_BREF : X264_TYPE_B; case SLICE_TYPE_P: default: diff --git a/vfw/codec.c b/vfw/codec.c index 9ad897e8..27b85822 100644 --- a/vfw/codec.c +++ b/vfw/codec.c @@ -130,6 +130,7 @@ LRESULT compress_frames_info(CODEC * codec, ICCOMPRESSFRAMES * icf ) { codec->fincr = icf->dwScale; codec->fbase = icf->dwRate; + codec->config.i_frame_total = icf->lFrameCount; return ICERR_OK; } @@ -213,6 +214,7 @@ LRESULT compress_begin(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiO param.i_fps_num = codec->fbase; param.i_fps_den = codec->fincr; + param.i_frame_total = config->i_frame_total; param.i_frame_reference = config->i_refmax; param.i_keyint_min = config->i_keyint_min; diff --git a/vfw/x264vfw.h b/vfw/x264vfw.h index c853cc78..6ce83e54 100644 --- a/vfw/x264vfw.h +++ b/vfw/x264vfw.h @@ -46,6 +46,8 @@ typedef struct int b_fast1pass; /* turns off some flags during 1st pass */ int b_updatestats; /* updates the statsfile during 2nd pass */ + int i_frame_total; + /* Our config */ int i_refmax; int i_keyint_max; diff --git a/x264.c b/x264.c index 1c31ed26..e5aacd8f 100644 --- a/x264.c +++ b/x264.c @@ -73,7 +73,6 @@ static void SigIntHandler( int a ) typedef struct { int b_decompress; int b_progress; - int i_maxframes; int i_seek; hnd_t hin; hnd_t hout; @@ -637,7 +636,7 @@ static int Parse( int argc, char **argv, param->cpu = 0; break; case OPT_FRAMES: - opt->i_maxframes = atoi( optarg ); + param->i_frame_total = atoi( optarg ); break; case OPT_SEEK: opt->i_seek = atoi( optarg ); @@ -1041,6 +1040,11 @@ static int Encode( x264_param_t *param, cli_opt_t *opt ) int i_progress; i_frame_total = p_get_frame_total( opt->hin, param->i_width, param->i_height ); + i_frame_total -= opt->i_seek; + if( ( i_frame_total == 0 || param->i_frame_total < i_frame_total ) + && param->i_frame_total > 0 ) + i_frame_total = param->i_frame_total; + param->i_frame_total = i_frame_total; if( ( h = x264_encoder_open( param ) ) == NULL ) { @@ -1062,10 +1066,8 @@ static int Encode( x264_param_t *param, cli_opt_t *opt ) x264_picture_alloc( &pic, X264_CSP_I420, param->i_width, param->i_height ); i_start = x264_mdate(); + /* Encode frames */ - i_frame_total -= opt->i_seek; - if( opt->i_maxframes > 0 && opt->i_maxframes < i_frame_total ) - i_frame_total = opt->i_maxframes; for( i_frame = 0, i_file = 0, i_progress = 0; b_ctrl_c == 0 && (i_frame < i_frame_total || i_frame_total == 0); ) { diff --git a/x264.h b/x264.h index 13039883..ebbb4d2b 100644 --- a/x264.h +++ b/x264.h @@ -35,7 +35,7 @@ #include -#define X264_BUILD 37 +#define X264_BUILD 38 /* x264_t: * opaque handler for decoder and encoder */ @@ -127,6 +127,7 @@ typedef struct int i_height; int i_csp; /* CSP of encoded bitstream, only i420 supported */ int i_level_idc; + int i_frame_total; /* number of frames to encode if known, else 0 */ struct {