static int x264_validate_parameters( x264_t *h )
{
int max_slices;
#ifdef HAVE_MMX MMX是由英特尔开发的一种SIMD多媒体指令集,共有57条指令。它最早集成在英特尔奔腾(Pentium)MMX处理器上,以提高其多媒体数据的处理能力。
if( !(x264_cpu_detect() & X264_CPU_SSE) )x264_cpu_detect返回值是cpu=4730,X264_CPU_SSE返回值为0x20.
{
x264_log( h, X264_LOG_ERROR, "your cpu does not support SSE1, but x264 was compiled with asm support/n");
x264_log( h, X264_LOG_ERROR, "to run x264, recompile without asm support (configure --disable-asm)/n");
return -1;
}
#endif
if( h->param.i_width <= 0 || h->param.i_height <= 0 )
{
x264_log( h, X264_LOG_ERROR, "invalid width x height (%dx%d)/n",
h->param.i_width, h->param.i_height );
return -1;
}检测图像大小是否有效。
if( h->param.i_width % 2 || h->param.i_height % 2 )
{
x264_log( h, X264_LOG_ERROR, "width or height not divisible by 2 (%dx%d)/n",
h->param.i_width, h->param.i_height );
return -1;
}检测图像宽和高是否能被2整除?不懂
if( h->param.i_csp != X264_CSP_I420 )
{
x264_log( h, X264_LOG_ERROR, "invalid CSP (only I420 supported)/n" );
return -1;
}检测视频颜色空间是否是 X264_CSP_I420
if( h->param.i_threads == X264_THREADS_AUTO )自动选择最佳的线程数
h->param.i_threads = x264_cpu_num_processors() * 3/2;
h->param.i_threads = x264_clip3( h->param.i_threads, 1, X264_THREAD_MAX );
if( h->param.i_threads > 1 )如果线程数定义大于1,却不支持并行线程,提示错误。
{
#ifndef HAVE_PTHREAD
x264_log( h, X264_LOG_WARNING, "not compiled with pthread support!/n");
h->param.i_threads = 1;
#endif
}
if( h->param.b_interlaced )判断是否支持隔行扫描。隔行扫描不支持esa和temporal.
{
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_direct_mv_pred > X264_DIRECT_PRED_SPATIAL )
{
x264_log( h, X264_LOG_WARNING, "interlace + direct=temporal is not implemented/n" );
h->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_SPATIAL;
}
}
如果支持ffmpeg设置,则提示错误。
{
int score = 0;
score += h->param.analyse.i_me_range == 0;
score += h->param.rc.i_qp_step == 3;
score += h->param.i_keyint_max == 12;
score += h->param.rc.i_qp_min == 2;
score += h->param.rc.i_qp_max == 31;
score += h->param.rc.f_qcompress == 0.5;
score += fabs(h->param.rc.f_ip_factor - 1.25) < 0.01;
score += fabs(h->param.rc.f_pb_factor - 1.25) < 0.01;
score += h->param.analyse.inter == 0 && h->param.analyse.i_subpel_refine == 8;
if( score >= 5 )
{
x264_log( h, X264_LOG_ERROR, "broken ffmpeg default settings detected/n" );
x264_log( h, X264_LOG_ERROR, "use an encoding preset (vpre)/n" );
return -1;
}
}
if( h->param.rc.i_rc_method < 0 || h->param.rc.i_rc_method > 2 ) 针对不同的应用场合,学者们提出了多种码率控制(Rate Control)策略。其中,实时编码码率控制方法主要有两种:用先前宏块编码产生的比特数来预测当前宏块编码产生比特数,或者通过视频编码率失真函数来预测当前宏块编码产生的比特数。(判断是否有码率控制策略)
{
x264_log( h, X264_LOG_ERROR, "no ratecontrol method specified/n" );
return -1;
}
h->param.rc.f_rf_constant = x264_clip3f( h->param.rc.f_rf_constant, 0, 51 );
h->param.rc.i_qp_constant = x264_clip3( h->param.rc.i_qp_constant, 0, 51 );
if( h->param.rc.i_rc_method == X264_RC_CRF )
{
h->param.rc.i_qp_constant = h->param.rc.f_rf_constant;
h->param.rc.i_bitrate = 0;
}
if( (h->param.rc.i_rc_method == X264_RC_CQP || h->param.rc.i_rc_method == X264_RC_CRF)
&& h->param.rc.i_qp_constant == 0 ) CQP为恒定质量,CRF为恒定码率,ABR为平均码率,NULL为码率控制的四种方法。
{
h->mb.b_lossless = 1;
h->param.i_cqm_preset = X264_CQM_FLAT;
h->param.psz_cqm_file = NULL;
h->param.rc.i_rc_method = X264_RC_CQP;
h->param.rc.f_ip_factor = 1;
h->param.rc.f_pb_factor = 1;
h->param.analyse.b_psnr = 0;
h->param.analyse.b_ssim = 0;
h->param.analyse.i_chroma_qp_offset = 0;
h->param.analyse.i_trellis = 0;
h->param.analyse.b_fast_pskip = 0;
h->param.analyse.i_noise_reduction = 0;
h->param.analyse.f_psy_rd = 0;
h->param.i_bframe = 0;
/* 8x8dct is not useful at all in CAVLC lossless */
if( !h->param.b_cabac )
h->param.analyse.b_transform_8x8 = 0;
}
if( h->param.rc.i_rc_method == X264_RC_CQP )
{
float qp_p = h->param.rc.i_qp_constant;
float qp_i = qp_p - 6*log(h->param.rc.f_ip_factor)/log(2);
float qp_b = qp_p + 6*log(h->param.rc.f_pb_factor)/log(2);
h->param.rc.i_qp_min = x264_clip3( (int)(X264_MIN3( qp_p, qp_i, qp_b )), 0, 51 );
h->param.rc.i_qp_max = x264_clip3( (int)(X264_MAX3( qp_p, qp_i, qp_b ) + .999), 0, 51 );
h->param.rc.i_aq_mode = 0;
h->param.rc.b_mb_tree = 0;
}
h->param.rc.i_qp_max = x264_clip3( h->param.rc.i_qp_max, 0, 51 );
h->param.rc.i_qp_min = x264_clip3( h->param.rc.i_qp_min, 0, h->param.rc.i_qp_max );
if( ( h->param.i_width % 16 || h->param.i_height % 16 )
&& h->param.i_height != 1080 && !h->mb.b_lossless )
{
// There's nothing special about 1080 in that the warning still applies to it,
// but chances are the user can't help it if his content is already 1080p,
// so there's no point in warning in that case.
x264_log( h, X264_LOG_WARNING,
"width or height not divisible by 16 (%dx%d), compression will suffer./n",
h->param.i_width, h->param.i_height );
}
max_slices = (h->param.i_height+((16<<h->param.b_interlaced)-1))/(16<<h->param.b_interlaced);
h->param.i_slice_count = x264_clip3( h->param.i_slice_count, 0, max_slices );i_slice_count为每帧片的数量
h->param.i_slice_max_size = X264_MAX( h->param.i_slice_max_size, 0 ); i_slice_max_size为每片的最大尺寸,为字节为单位。
h->param.i_slice_max_mbs = X264_MAX( h->param.i_slice_max_mbs, 0 ); i_slice_max_mbs每个片中宏块的最大数目。
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.i_slice_max_mbs || h->param.i_slice_max_size )
h->param.i_slice_count = 0;
h->param.i_frame_reference = x264_clip3( h->param.i_frame_reference, 1, 16 ); i_frame_reference为最大数量的参考帧。
if( h->param.i_keyint_max <= 0 ) i_keyint_max为IDR关键帧之间的最大帧数。IDR片仅用于构成一个完整的IDR图像,也就是IDR图像中的所有片必须是IDR片,一个IDR片只能作为IDR图像的一部分。在解码器端,当解码完一幅IDR图像后,解码器立即将所有的参考图像标识为“未用作参考”。这样,后续图像被解码时,肢不参考该IDR图像前面的任何图像。每个视频序列的第一幅图像一定是IDR图像。
h->param.i_keyint_max = 1;
if( h->param.i_scenecut_threshold < 0 ) 插入I帧的阀值,表示多少帧插入一个I帧
h->param.i_scenecut_threshold = 0;
h->param.i_keyint_min = x264_clip3( h->param.i_keyint_min, 1, h->param.i_keyint_max/2+1 );
if( !h->param.analyse.i_subpel_refine && h->param.analyse.i_direct_mv_pred > X264_DIRECT_PRED_SPATIAL ) i_direct_mv_pred为运动向量预测(亚像素运动估计和时间预测冲突。)
{
x264_log( h, X264_LOG_WARNING, "subme=0 + direct=temporal is not supported/n" );
h->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_SPATIAL;
}
h->param.i_bframe = x264_clip3( h->param.i_bframe, 0, X264_BFRAME_MAX ); i_bframe为两个参考图像之间的B帧数。
if( h->param.i_keyint_max == 1 )
h->param.i_bframe = 0;
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;
if( !h->param.i_bframe )
{
h->param.i_bframe_adaptive = X264_B_ADAPT_NONE;
h->param.analyse.i_direct_mv_pred = 0;
h->param.analyse.b_weighted_bipred = 0;
}
h->param.rc.i_lookahead = x264_clip3( h->param.rc.i_lookahead, 0, X264_LOOKAHEAD_MAX );
{
int maxrate = X264_MAX( h->param.rc.i_vbv_max_bitrate, h->param.rc.i_bitrate );
float bufsize = maxrate ? (float)h->param.rc.i_vbv_buffer_size / maxrate : 0;
float fps = h->param.i_fps_num > 0 && h->param.i_fps_den > 0 ? (float) h->param.i_fps_num / h->param.i_fps_den : 25.0;
h->param.rc.i_lookahead = X264_MIN( h->param.rc.i_lookahead, X264_MAX( h->param.i_keyint_max, bufsize*fps ) );
}
if( h->param.rc.b_stat_read )
h->param.rc.i_lookahead = 0;
else if( !h->param.rc.i_lookahead || h->param.i_keyint_max == 1 )
h->param.rc.b_mb_tree = 0;
if( h->param.rc.f_qcompress == 1 )
h->param.rc.b_mb_tree = 0;宏块树,不懂?
#ifdef HAVE_PTHREAD
if( h->param.i_sync_lookahead )
h->param.i_sync_lookahead = x264_clip3( h->param.i_sync_lookahead, h->param.i_threads + h->param.i_bframe, X264_LOOKAHEAD_MAX );
if( h->param.rc.b_stat_read || h->param.i_threads == 1 )
h->param.i_sync_lookahead = 0;
#else
h->param.i_sync_lookahead = 0;
#endif
h->mb.b_direct_auto_write = h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_AUTO
&& h->param.i_bframe
&& ( h->param.rc.b_stat_write || !h->param.rc.b_stat_read );
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 );
h->param.analyse.i_luma_deadzone[0] = x264_clip3( h->param.analyse.i_luma_deadzone[0], 0, 32 );亮度无效区域{帧间 帧内}
h->param.analyse.i_luma_deadzone[1] = x264_clip3( h->param.analyse.i_luma_deadzone[1], 0, 32 );
h->param.i_cabac_init_idc = x264_clip3( h->param.i_cabac_init_idc, 0, 2 );
if( h->param.i_cqm_preset < X264_CQM_FLAT || h->param.i_cqm_preset > X264_CQM_CUSTOM )
h->param.i_cqm_preset = X264_CQM_FLAT; 自定义量化矩阵(CQM),初始化量化模式为flat,共有三种,分别为flat,custom和jvt。
if( h->param.analyse.i_me_method < X264_ME_DIA ||
h->param.analyse.i_me_method > X264_ME_TESA )
h->param.analyse.i_me_method = X264_ME_HEX;如果定义的运动估计方法不在预设范围内,则使用x264_me_hex.
if( h->param.analyse.i_me_range < 4 )运动估计的范围如果小于4的话则赋值为4.默认是16
h->param.analyse.i_me_range = 4;
if( h->param.analyse.i_me_range > 16 && h->param.analyse.i_me_method <= X264_ME_HEX )
h->param.analyse.i_me_range = 16;如果运动估计的范围大于16并且估计方法是DIA或HEX,则运动估计的范围赋值为16.
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;是否有参考帧,由.b_mixed_references和i_frame_reference 并得
h->param.analyse.inter &= X264_ANALYSE_PSUB16x16|X264_ANALYSE_PSUB8x8|X264_ANALYSE_BSUB16x16|
X264_ANALYSE_I4x4|X264_ANALYSE_I8x8;
h->param.analyse.intra &= X264_ANALYSE_I4x4|X264_ANALYSE_I8x8;
if( !(h->param.analyse.inter & X264_ANALYSE_PSUB16x16) )
h->param.analyse.inter &= ~X264_ANALYSE_PSUB8x8;?
if( !h->param.analyse.b_transform_8x8 )是否支持8*8分区。
{
h->param.analyse.inter &= ~X264_ANALYSE_I8x8;
h->param.analyse.intra &= ~X264_ANALYSE_I8x8;
}
h->param.analyse.i_chroma_qp_offset = x264_clip3(h->param.analyse.i_chroma_qp_offset, -12, 12);色度量化步长偏移量。默认为0.
if( !h->param.b_cabac )如果不使用自适应算术编码,那么关闭trellis量化。Trellis--trellis:Trellis量化,对每个8x8的块寻找合适的量化值,需要勾选CABAC才能使用该参数。可选项:0 - None: 关闭;1 - Final MB: 只在最后编码时使用;2 - Always:一直使用。默认值:0
h->param.analyse.i_trellis = 0;
h->param.analyse.i_trellis = x264_clip3( h->param.analyse.i_trellis, 0, 2 );
if( !h->param.analyse.b_psy )?http://cache.baidu.com/c?m=9d78d513d98100e44fede53e5b4bc0676943f0662ba7a6020fd1843b99701c011969b9fd61600705a0d8613e5ce81f0fb1e7360574587aeac6df883d8ce6cd3568952731751d8c4a15d604fc8b007e907ec74deedb19e6baf03896acd5d5c8542490155a24&p=9d33c64ad09711a05befdc36114d&user=baidu
{
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 );
/* 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 )
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 )
h->param.analyse.i_chroma_qp_offset -= h->param.analyse.f_psy_trellis < 0.25 ? 1 : 2;
else
h->mb.i_psy_trellis = 0;
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 )
{
h->param.rc.i_aq_mode = 1;
h->param.rc.f_aq_strength = 0;
}
if( h->param.rc.b_mb_tree && h->param.b_bframe_pyramid )
{
x264_log( h, X264_LOG_WARNING, "b-pyramid + mb-tree is not supported/n" );
h->param.b_bframe_pyramid = 0;
}
h->param.analyse.i_noise_reduction = x264_clip3( h->param.analyse.i_noise_reduction, 0, 1<<16 );
if( h->param.analyse.i_subpel_refine == 10 && (h->param.analyse.i_trellis != 2 || !h->param.rc.i_aq_mode) )
h->param.analyse.i_subpel_refine = 9;
{
const x264_level_t *l = x264_levels;
if( h->param.i_level_idc < 0 )
{
int maxrate_bak = h->param.rc.i_vbv_max_bitrate;
if( h->param.rc.i_rc_method == X264_RC_ABR && h->param.rc.i_vbv_buffer_size <= 0 )
h->param.rc.i_vbv_max_bitrate = h->param.rc.i_bitrate * 2;
h->sps = h->sps_array;序列参数集数组
x264_sps_init( h->sps, h->param.i_sps_id, &h->param );
do h->param.i_level_idc = l->level_idc;
while( l[1].level_idc && x264_validate_levels( h, 0 ) && l++ );
h->param.rc.i_vbv_max_bitrate = maxrate_bak;
}
else
{
while( l->level_idc && l->level_idc != h->param.i_level_idc )
l++;
if( l->level_idc == 0 )
{
x264_log( h, X264_LOG_ERROR, "invalid level_idc: %d/n", h->param.i_level_idc );
return -1;
}
}
if( h->param.analyse.i_mv_range <= 0 )
h->param.analyse.i_mv_range = l->mv_range >> h->param.b_interlaced;
else
h->param.analyse.i_mv_range = x264_clip3(h->param.analyse.i_mv_range, 32, 512 >> h->param.b_interlaced);
}
if( h->param.i_threads > 1 )
{
int r = h->param.analyse.i_mv_range_thread;
int r2;
if( r <= 0 )
{
// half of the available space is reserved and divided evenly among the threads,
// the rest is allocated to whichever thread is far enough ahead to use it.
// reserving more space increases quality for some videos, but costs more time
// in thread synchronization.
int max_range = (h->param.i_height + X264_THREAD_HEIGHT) / h->param.i_threads - X264_THREAD_HEIGHT;
r = max_range / 2;
}
r = X264_MAX( r, h->param.analyse.i_me_range );
r = X264_MIN( r, h->param.analyse.i_mv_range );
// round up to use the whole mb row
r2 = (r & ~15) + ((-X264_THREAD_HEIGHT) & 15);
if( r2 < r )
r2 += 16;
x264_log( h, X264_LOG_DEBUG, "using mv_range_thread = %d/n", r2 );
h->param.analyse.i_mv_range_thread = r2;
}
if( h->param.rc.f_qblur < 0 )时间上的模糊量化
h->param.rc.f_qblur = 0;
if( h->param.rc.f_complexity_blur < 0 ) 时间上模糊复杂性
h->param.rc.f_complexity_blur = 0;
h->param.i_sps_id &= 31;
if( h->param.i_log_level < X264_LOG_INFO )
{
h->param.analyse.b_psnr = 0; b_psnr是是否计算和打印了信噪比统计
h->param.analyse.b_ssim = 0;
}
/* ensure the booleans are 0 or 1 so they can be used in math */确定bool型是0或者1,以至于它们能用于数学计算。
#define BOOLIFY(x) h->param.x = !!h->param.x
BOOLIFY( b_cabac );
BOOLIFY( b_deblocking_filter );
BOOLIFY( b_interlaced );
BOOLIFY( analyse.b_transform_8x8 );
BOOLIFY( analyse.b_chroma_me );
BOOLIFY( analyse.b_fast_pskip );
BOOLIFY( rc.b_stat_write );
BOOLIFY( rc.b_stat_read );
BOOLIFY( rc.b_mb_tree );
#undef BOOLIFY
return 0;
}