From bd00852715011d1552a918921960263650a34040 Mon Sep 17 00:00:00 2001 From: Anton Mitrofanov Date: Wed, 20 Apr 2011 02:54:49 +0400 Subject: [PATCH] Validate input parameters more carefully Get rid of redundant warnings upon encoder_reconfig calls. Also avoid encoder_reconfig turning off psy_rd/trellis. --- encoder/encoder.c | 107 +++++++++++++++++++++++++++------------------- encoder/set.c | 12 +++--- 2 files changed, 69 insertions(+), 50 deletions(-) diff --git a/encoder/encoder.c b/encoder/encoder.c index cc83b36e..85bd34f6 100644 --- a/encoder/encoder.c +++ b/encoder/encoder.c @@ -381,15 +381,15 @@ static void x264_encoder_thread_init( x264_t *h ) * ****************************************************************************/ -static int x264_validate_parameters( x264_t *h ) +static int x264_validate_parameters( x264_t *h, int b_open ) { #if HAVE_MMX #ifdef __SSE__ - if( !(x264_cpu_detect() & X264_CPU_SSE) ) + if( b_open && !(x264_cpu_detect() & X264_CPU_SSE) ) { x264_log( h, X264_LOG_ERROR, "your cpu does not support SSE1, but x264 was compiled with asm support\n"); #else - if( !(x264_cpu_detect() & X264_CPU_MMXEXT) ) + if( b_open && !(x264_cpu_detect() & X264_CPU_MMXEXT) ) { x264_log( h, X264_LOG_ERROR, "your cpu does not support MMXEXT, but x264 was compiled with asm support\n"); #endif @@ -455,23 +455,10 @@ static int x264_validate_parameters( x264_t *h ) h->param.analyse.i_weighted_pred = 0; } - if( h->param.b_interlaced ) - { - if( h->param.analyse.i_me_method >= X264_ME_ESA ) - { - x264_log( h, X264_LOG_WARNING, "interlace + me=esa is not implemented\n" ); - h->param.analyse.i_me_method = X264_ME_UMH; - } - if( h->param.analyse.i_weighted_pred > 0 ) - { - x264_log( h, X264_LOG_WARNING, "interlace + weightp is not implemented\n" ); - h->param.analyse.i_weighted_pred = X264_WEIGHTP_NONE; - } - } - h->param.i_frame_packing = x264_clip3( h->param.i_frame_packing, -1, 5 ); /* Detect default ffmpeg settings and terminate with an error. */ + if( b_open ) { int score = 0; score += h->param.analyse.i_me_range == 0; @@ -500,7 +487,11 @@ static int x264_validate_parameters( x264_t *h ) return -1; } h->param.rc.f_rf_constant = x264_clip3f( h->param.rc.f_rf_constant, -QP_BD_OFFSET, 51 ); + h->param.rc.f_rf_constant_max = x264_clip3f( h->param.rc.f_rf_constant_max, -QP_BD_OFFSET, 51 ); h->param.rc.i_qp_constant = x264_clip3( h->param.rc.i_qp_constant, 0, QP_MAX ); + h->param.analyse.i_subpel_refine = x264_clip3( h->param.analyse.i_subpel_refine, 0, 10 ); + h->param.rc.f_ip_factor = X264_MAX( h->param.rc.f_ip_factor, 0.01f ); + h->param.rc.f_pb_factor = X264_MAX( h->param.rc.f_pb_factor, 0.01f ); if( h->param.rc.i_rc_method == X264_RC_CRF ) { h->param.rc.i_qp_constant = h->param.rc.f_rf_constant + QP_BD_OFFSET; @@ -536,9 +527,15 @@ static int x264_validate_parameters( x264_t *h ) h->param.rc.i_qp_max = x264_clip3( (int)(X264_MAX3( qp_p, qp_i, qp_b ) + .999), 0, QP_MAX ); h->param.rc.i_aq_mode = 0; h->param.rc.b_mb_tree = 0; + h->param.rc.i_bitrate = 0; } h->param.rc.i_qp_max = x264_clip3( h->param.rc.i_qp_max, 0, QP_MAX ); h->param.rc.i_qp_min = x264_clip3( h->param.rc.i_qp_min, 0, h->param.rc.i_qp_max ); + h->param.rc.i_qp_step = x264_clip3( h->param.rc.i_qp_step, 0, QP_MAX ); + h->param.rc.i_bitrate = X264_MAX( h->param.rc.i_bitrate, 0 ); + h->param.rc.i_vbv_buffer_size = X264_MAX( h->param.rc.i_vbv_buffer_size, 0 ); + h->param.rc.i_vbv_max_bitrate = X264_MAX( h->param.rc.i_vbv_max_bitrate, 0 ); + h->param.rc.f_vbv_buffer_init = X264_MAX( h->param.rc.f_vbv_buffer_init, 0 ); if( h->param.rc.i_vbv_buffer_size ) { if( h->param.rc.i_rc_method == X264_RC_CQP ) @@ -573,19 +570,14 @@ static int x264_validate_parameters( x264_t *h ) h->param.rc.i_vbv_max_bitrate = 0; } - if( h->param.b_interlaced && h->param.i_slice_max_size ) + h->param.b_interlaced = !!h->param.b_interlaced; + int max_slices = (h->param.i_height+((16<param.b_interlaced)-1))/(16<param.b_interlaced); + if( h->param.b_sliced_threads ) { - x264_log( h, X264_LOG_WARNING, "interlaced + slice-max-size is not implemented\n" ); + h->param.i_slice_count = x264_clip3( h->param.i_threads, 0, max_slices ); h->param.i_slice_max_size = 0; - } - if( h->param.b_interlaced && h->param.i_slice_max_mbs ) - { - x264_log( h, X264_LOG_WARNING, "interlaced + slice-max-mbs is not implemented\n" ); h->param.i_slice_max_mbs = 0; } - int max_slices = (h->param.i_height+((16<param.b_interlaced)-1))/(16<param.b_interlaced); - if( h->param.b_sliced_threads ) - h->param.i_slice_count = x264_clip3( h->param.i_threads, 0, max_slices ); else { h->param.i_slice_count = x264_clip3( h->param.i_slice_count, 0, max_slices ); @@ -594,6 +586,16 @@ static int x264_validate_parameters( x264_t *h ) if( h->param.i_slice_max_mbs || h->param.i_slice_max_size ) h->param.i_slice_count = 0; } + if( h->param.b_interlaced && h->param.i_slice_max_size ) + { + x264_log( h, X264_LOG_WARNING, "interlaced + slice-max-size is not implemented\n" ); + h->param.i_slice_max_size = 0; + } + if( h->param.b_interlaced && h->param.i_slice_max_mbs ) + { + x264_log( h, X264_LOG_WARNING, "interlaced + slice-max-mbs is not implemented\n" ); + h->param.i_slice_max_mbs = 0; + } if( h->param.b_bluray_compat ) { @@ -616,6 +618,7 @@ static int x264_validate_parameters( x264_t *h ) h->param.i_dpb_size = x264_clip3( h->param.i_dpb_size, 1, X264_REF_MAX ); if( h->param.i_scenecut_threshold < 0 ) h->param.i_scenecut_threshold = 0; + h->param.analyse.i_direct_mv_pred = x264_clip3( h->param.analyse.i_direct_mv_pred, X264_DIRECT_PRED_NONE, X264_DIRECT_PRED_AUTO ); if( !h->param.analyse.i_subpel_refine && h->param.analyse.i_direct_mv_pred > X264_DIRECT_PRED_SPATIAL ) { x264_log( h, X264_LOG_WARNING, "subme=0 + direct=temporal is not supported\n" ); @@ -626,6 +629,7 @@ static int x264_validate_parameters( x264_t *h ) if( h->param.i_bframe <= 1 ) h->param.i_bframe_pyramid = X264_B_PYRAMID_NONE; h->param.i_bframe_pyramid = x264_clip3( h->param.i_bframe_pyramid, X264_B_PYRAMID_NONE, X264_B_PYRAMID_NORMAL ); + h->param.i_bframe_adaptive = x264_clip3( h->param.i_bframe_adaptive, X264_B_ADAPT_NONE, X264_B_ADAPT_TRELLIS ); if( !h->param.i_bframe ) { h->param.i_bframe_adaptive = X264_B_ADAPT_NONE; @@ -707,7 +711,6 @@ static int x264_validate_parameters( x264_t *h ) if( h->param.analyse.i_me_method == X264_ME_TESA && (h->mb.b_lossless || h->param.analyse.i_subpel_refine <= 1) ) h->param.analyse.i_me_method = X264_ME_ESA; - h->param.analyse.i_subpel_refine = x264_clip3( h->param.analyse.i_subpel_refine, 0, 10 ); h->param.analyse.b_mixed_references = h->param.analyse.b_mixed_references && h->param.i_frame_reference > 1; h->param.analyse.inter &= X264_ANALYSE_PSUB16x16|X264_ANALYSE_PSUB8x8|X264_ANALYSE_BSUB16x16| X264_ANALYSE_I4x4|X264_ANALYSE_I8x8; @@ -721,6 +724,10 @@ static int x264_validate_parameters( x264_t *h ) } h->param.analyse.i_chroma_qp_offset = x264_clip3(h->param.analyse.i_chroma_qp_offset, -12, 12); h->param.analyse.i_trellis = x264_clip3( h->param.analyse.i_trellis, 0, 2 ); + h->param.rc.i_aq_mode = x264_clip3( h->param.rc.i_aq_mode, 0, 2 ); + h->param.rc.f_aq_strength = x264_clip3f( h->param.rc.f_aq_strength, 0, 3 ); + if( h->param.rc.f_aq_strength == 0 ) + h->param.rc.i_aq_mode = 0; if( h->param.i_log_level < X264_LOG_INFO ) { @@ -728,7 +735,7 @@ static int x264_validate_parameters( x264_t *h ) h->param.analyse.b_ssim = 0; } /* Warn users trying to measure PSNR/SSIM with psy opts on. */ - if( h->param.analyse.b_psnr || h->param.analyse.b_ssim ) + if( b_open && (h->param.analyse.b_psnr || h->param.analyse.b_ssim) ) { char *s = NULL; @@ -756,28 +763,18 @@ static int x264_validate_parameters( x264_t *h ) h->param.analyse.f_psy_rd = 0; h->param.analyse.f_psy_trellis = 0; } - if( !h->param.analyse.i_trellis ) - h->param.analyse.f_psy_trellis = 0; h->param.analyse.f_psy_rd = x264_clip3f( h->param.analyse.f_psy_rd, 0, 10 ); h->param.analyse.f_psy_trellis = x264_clip3f( h->param.analyse.f_psy_trellis, 0, 10 ); - if( h->param.analyse.i_subpel_refine < 6 ) - h->param.analyse.f_psy_rd = 0; - h->mb.i_psy_rd = FIX8( h->param.analyse.f_psy_rd ); + h->mb.i_psy_rd = h->param.analyse.i_subpel_refine >= 6 ? FIX8( h->param.analyse.f_psy_rd ) : 0; + h->mb.i_psy_trellis = h->param.analyse.i_trellis ? FIX8( h->param.analyse.f_psy_trellis / 4 ) : 0; /* Psy RDO increases overall quantizers to improve the quality of luma--this indirectly hurts chroma quality */ /* so we lower the chroma QP offset to compensate */ - /* This can be triggered repeatedly on multiple calls to parameter_validate, but since encoding - * uses the pps chroma qp offset not the param chroma qp offset, this is not a problem. */ - if( h->mb.i_psy_rd ) + if( b_open && h->mb.i_psy_rd ) h->param.analyse.i_chroma_qp_offset -= h->param.analyse.f_psy_rd < 0.25 ? 1 : 2; - h->mb.i_psy_trellis = FIX8( h->param.analyse.f_psy_trellis / 4 ); /* Psy trellis has a similar effect. */ - if( h->mb.i_psy_trellis ) + if( b_open && h->mb.i_psy_trellis ) h->param.analyse.i_chroma_qp_offset -= h->param.analyse.f_psy_trellis < 0.25 ? 1 : 2; h->param.analyse.i_chroma_qp_offset = x264_clip3(h->param.analyse.i_chroma_qp_offset, -12, 12); - h->param.rc.i_aq_mode = x264_clip3( h->param.rc.i_aq_mode, 0, 2 ); - h->param.rc.f_aq_strength = x264_clip3f( h->param.rc.f_aq_strength, 0, 3 ); - if( h->param.rc.f_aq_strength == 0 ) - h->param.rc.i_aq_mode = 0; /* MB-tree requires AQ to be on, even if the strength is zero. */ if( !h->param.rc.i_aq_mode && h->param.rc.b_mb_tree ) { @@ -818,6 +815,21 @@ static int x264_validate_parameters( x264_t *h ) } h->param.analyse.i_weighted_pred = x264_clip3( h->param.analyse.i_weighted_pred, X264_WEIGHTP_NONE, X264_WEIGHTP_SMART ); + + if( h->param.b_interlaced ) + { + if( h->param.analyse.i_me_method >= X264_ME_ESA ) + { + x264_log( h, X264_LOG_WARNING, "interlace + me=esa is not implemented\n" ); + h->param.analyse.i_me_method = X264_ME_UMH; + } + if( h->param.analyse.i_weighted_pred > 0 ) + { + x264_log( h, X264_LOG_WARNING, "interlace + weightp is not implemented\n" ); + h->param.analyse.i_weighted_pred = X264_WEIGHTP_NONE; + } + } + if( !h->param.analyse.i_weighted_pred && h->param.rc.b_mb_tree && h->param.analyse.b_psy ) h->param.analyse.i_weighted_pred = X264_WEIGHTP_FAKE; @@ -844,6 +856,8 @@ static int x264_validate_parameters( x264_t *h ) h->param.analyse.i_mv_range_thread = r2; } + if( h->param.rc.f_rate_tolerance < 0 ) + h->param.rc.f_rate_tolerance = 0; if( h->param.rc.f_qblur < 0 ) h->param.rc.f_qblur = 0; if( h->param.rc.f_complexity_blur < 0 ) @@ -854,6 +868,8 @@ static int x264_validate_parameters( x264_t *h ) if( h->param.b_interlaced ) h->param.b_pic_struct = 1; + h->param.i_nal_hrd = x264_clip3( h->param.i_nal_hrd, X264_NAL_HRD_NONE, X264_NAL_HRD_CBR ); + if( h->param.i_nal_hrd && !h->param.rc.i_vbv_buffer_size ) { x264_log( h, X264_LOG_WARNING, "NAL HRD parameters require VBV parameters\n" ); @@ -881,9 +897,12 @@ static int x264_validate_parameters( x264_t *h ) BOOLIFY( b_repeat_headers ); BOOLIFY( b_annexb ); BOOLIFY( b_vfr_input ); + BOOLIFY( b_pulldown ); + BOOLIFY( b_tff ); BOOLIFY( b_pic_struct ); BOOLIFY( b_fake_interlaced ); BOOLIFY( b_open_gop ); + BOOLIFY( b_bluray_compat ); BOOLIFY( analyse.b_transform_8x8 ); BOOLIFY( analyse.b_weighted_bipred ); BOOLIFY( analyse.b_chroma_me ); @@ -976,7 +995,7 @@ x264_t *x264_encoder_open( x264_param_t *param ) goto fail; } - if( x264_validate_parameters( h ) < 0 ) + if( x264_validate_parameters( h, 1 ) < 0 ) goto fail; if( h->param.psz_cqm_file ) @@ -1307,7 +1326,7 @@ int x264_encoder_reconfig( x264_t *h, x264_param_t *param ) mbcmp_init( h ); - int ret = x264_validate_parameters( h ); + int ret = x264_validate_parameters( h, 0 ); /* Supported reconfiguration options (1-pass only): * vbv-maxrate diff --git a/encoder/set.c b/encoder/set.c index e7cdf1c7..362cc5fe 100644 --- a/encoder/set.c +++ b/encoder/set.c @@ -205,18 +205,18 @@ void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param ) sps->vui.i_sar_height= param->vui.i_sar_height; } - sps->vui.b_overscan_info_present = ( param->vui.i_overscan ? 1 : 0 ); + sps->vui.b_overscan_info_present = param->vui.i_overscan > 0 && param->vui.i_overscan <= 2; if( sps->vui.b_overscan_info_present ) sps->vui.b_overscan_info = ( param->vui.i_overscan == 2 ? 1 : 0 ); sps->vui.b_signal_type_present = 0; - sps->vui.i_vidformat = ( param->vui.i_vidformat <= 5 ? param->vui.i_vidformat : 5 ); + sps->vui.i_vidformat = ( param->vui.i_vidformat >= 0 && param->vui.i_vidformat <= 5 ? param->vui.i_vidformat : 5 ); sps->vui.b_fullrange = ( param->vui.b_fullrange ? 1 : 0 ); sps->vui.b_color_description_present = 0; - sps->vui.i_colorprim = ( param->vui.i_colorprim <= 9 ? param->vui.i_colorprim : 2 ); - sps->vui.i_transfer = ( param->vui.i_transfer <= 11 ? param->vui.i_transfer : 2 ); - sps->vui.i_colmatrix = ( param->vui.i_colmatrix <= 9 ? param->vui.i_colmatrix : 2 ); + sps->vui.i_colorprim = ( param->vui.i_colorprim >= 0 && param->vui.i_colorprim <= 8 ? param->vui.i_colorprim : 2 ); + sps->vui.i_transfer = ( param->vui.i_transfer >= 0 && param->vui.i_transfer <= 10 ? param->vui.i_transfer : 2 ); + sps->vui.i_colmatrix = ( param->vui.i_colmatrix >= 0 && param->vui.i_colmatrix <= 8 ? param->vui.i_colmatrix : 2 ); if( sps->vui.i_colorprim != 2 || sps->vui.i_transfer != 2 || sps->vui.i_colmatrix != 2 ) @@ -232,7 +232,7 @@ void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param ) } /* FIXME: not sufficient for interlaced video */ - sps->vui.b_chroma_loc_info_present = ( param->vui.i_chroma_loc ? 1 : 0 ); + sps->vui.b_chroma_loc_info_present = param->vui.i_chroma_loc > 0 && param->vui.i_chroma_loc <= 5; if( sps->vui.b_chroma_loc_info_present ) { sps->vui.i_chroma_loc_top = param->vui.i_chroma_loc; -- 2.47.3