有些功能复杂c程序需要从命令行中接收很多参数,比如x264、x265,很多编码参数都通过命令行传递给exe。下面是一个x265.exe的命令行的示意图。
x265.exe --input F:\Tennis_1920x1080_24.yuv --fps 24 --input-res 1920x1080 --frames 100 --psnr --frame-threads 1 --no-wpp --preset medium --ctu 64 --min-cu-size 8 --max-tu-size 32 --tu-intra-depth 1 --tu-inter-depth 1 --keyint 1 --bframes 0 --no-deblock --[no-]sao --qp 35 --ipratio 1 --psy-rd 0 --no-signhide --log-level full --output Tennis_1920x1080_24.265 --recon rec_Tennis_1920x1080_24.yuv
可以看到,命令行参数形式比较多样。一般都是 --option [value] 的形式, [value]是可选项,比如 --frames 100,表示编码100帧; --psnr,则表示计算编码的psnr值。在x264和x265中都是调用getopt模块来处理这些命令行参数的,这个模块可以用于所有需要处理复杂命令行的程序,下面就分析一下getopt模块是如何使用的。
使用getopt关键在于定义好命令行的option选项,包括short option和long option。short option的形式为"-o val", "-p val", long option的形式为"--input val", "--preset val"等。long option中val为可选项,有些选项可以不用指定值,直接根据option的名字就可以知道具体含义。x264在x264.c中定义了short options和long options。
struct option
{
# if (defined __STDC__ && __STDC__) || defined __cplusplus
const char *name;
# else
char *name;
# endif
/* has_arg can't be an enum because some compilers complain about
type mismatches in all the code that assumes it is an int. */
int has_arg;
int *flag;
int val;
};
static char short_options[] = "8A:B:b:f:hI:i:m:o:p:q:r:t:Vvw";
static struct option long_options[] =
{
{ "help", no_argument, NULL, 'h' },
{ "longhelp", no_argument, NULL, OPT_LONGHELP },
{ "fullhelp", no_argument, NULL, OPT_FULLHELP },
{ "version", no_argument, NULL, 'V' },
{ "profile", required_argument, NULL, OPT_PROFILE },
{ "preset", required_argument, NULL, OPT_PRESET },
{ "tune", required_argument, NULL, OPT_TUNE },
{ "slow-firstpass", no_argument, NULL, OPT_SLOWFIRSTPASS },
{ "bitrate", required_argument, NULL, 'B' },
{ "bframes", required_argument, NULL, 'b' },
{ "b-adapt", required_argument, NULL, 0 },
{ "no-b-adapt", no_argument, NULL, 0 },
{ "b-bias", required_argument, NULL, 0 },
{ "b-pyramid", required_argument, NULL, 0 },
{ "open-gop", no_argument, NULL, 0 },
{ "bluray-compat", no_argument, NULL, 0 },
{ "avcintra-class", required_argument, NULL, 0 },
{ "min-keyint", required_argument, NULL, 'i' },
{ "keyint", required_argument, NULL, 'I' },
{ "intra-refresh", no_argument, NULL, 0 },
{ "scenecut", required_argument, NULL, 0 },
{ "no-scenecut", no_argument, NULL, 0 },
{ "nf", no_argument, NULL, 0 },
{ "no-deblock", no_argument, NULL, 0 },
{ "filter", required_argument, NULL, 0 },
{ "deblock", required_argument, NULL, 'f' },
{ "interlaced", no_argument, NULL, OPT_INTERLACED },
{ "tff", no_argument, NULL, OPT_INTERLACED },
{ "bff", no_argument, NULL, OPT_INTERLACED },
{ "no-interlaced", no_argument, NULL, OPT_INTERLACED },
{ "constrained-intra", no_argument, NULL, 0 },
{ "cabac", no_argument, NULL, 0 },
{ "no-cabac", no_argument, NULL, 0 },
{ "qp", required_argument, NULL, 'q' },
{ "qpmin", required_argument, NULL, 0 },
{ "qpmax", required_argument, NULL, 0 },
{ "qpstep", required_argument, NULL, 0 },
{ "crf", required_argument, NULL, 0 },
{ "rc-lookahead",required_argument, NULL, 0 },
{ "ref", required_argument, NULL, 'r' },
{ "asm", required_argument, NULL, 0 },
{ "no-asm", no_argument, NULL, 0 },
{ "opencl", no_argument, NULL, 1 },
{ "opencl-clbin",required_argument, NULL, 0 },
{ "opencl-device",required_argument, NULL, 0 },
{ "sar", required_argument, NULL, 0 },
{ "fps", required_argument, NULL, OPT_FPS },
{ "frames", required_argument, NULL, OPT_FRAMES },
{ "seek", required_argument, NULL, OPT_SEEK },
{ "output", required_argument, NULL, 'o' },
{ "muxer", required_argument, NULL, OPT_MUXER },
{ "demuxer", required_argument, NULL, OPT_DEMUXER },
{ "stdout", required_argument, NULL, OPT_MUXER },
{ "stdin", required_argument, NULL, OPT_DEMUXER },
{ "index", required_argument, NULL, OPT_INDEX },
{ "analyse", required_argument, NULL, 0 },
{ "partitions", required_argument, NULL, 'A' },
{ "direct", required_argument, NULL, 0 },
{ "weightb", no_argument, NULL, 'w' },
{ "no-weightb", no_argument, NULL, 0 },
{ "weightp", required_argument, NULL, 0 },
{ "me", required_argument, NULL, 0 },
{ "merange", required_argument, NULL, 0 },
{ "mvrange", required_argument, NULL, 0 },
{ "mvrange-thread", required_argument, NULL, 0 },
{ "subme", required_argument, NULL, 'm' },
{ "psy-rd", required_argument, NULL, 0 },
{ "no-psy", no_argument, NULL, 0 },
{ "psy", no_argument, NULL, 0 },
{ "mixed-refs", no_argument, NULL, 0 },
{ "no-mixed-refs", no_argument, NULL, 0 },
{ "no-chroma-me", no_argument, NULL, 0 },
{ "8x8dct", no_argument, NULL, '8' },
{ "no-8x8dct", no_argument, NULL, 0 },
{ "trellis", required_argument, NULL, 't' },
{ "fast-pskip", no_argument, NULL, 0 },
{ "no-fast-pskip", no_argument, NULL, 0 },
{ "no-dct-decimate", no_argument, NULL, 0 },
{ "aq-strength", required_argument, NULL, 0 },
{ "aq-mode", required_argument, NULL, 0 },
{ "deadzone-inter", required_argument, NULL, 0 },
{ "deadzone-intra", required_argument, NULL, 0 },
{ "level", required_argument, NULL, 0 },
{ "ratetol", required_argument, NULL, 0 },
{ "vbv-maxrate", required_argument, NULL, 0 },
{ "vbv-bufsize", required_argument, NULL, 0 },
{ "vbv-init", required_argument, NULL, 0 },
{ "crf-max", required_argument, NULL, 0 },
{ "ipratio", required_argument, NULL, 0 },
{ "pbratio", required_argument, NULL, 0 },
{ "chroma-qp-offset", required_argument, NULL, 0 },
{ "pass", required_argument, NULL, 'p' },
{ "stats", required_argument, NULL, 0 },
{ "qcomp", required_argument, NULL, 0 },
{ "mbtree", no_argument, NULL, 0 },
{ "no-mbtree", no_argument, NULL, 0 },
{ "qblur", required_argument, NULL, 0 },
{ "cplxblur", required_argument, NULL, 0 },
{ "zones", required_argument, NULL, 0 },
{ "qpfile", required_argument, NULL, OPT_QPFILE },
{ "threads", required_argument, NULL, 0 },
{ "lookahead-threads", required_argument, NULL, 0 },
{ "sliced-threads", no_argument, NULL, 0 },
{ "no-sliced-threads", no_argument, NULL, 0 },
{ "slice-max-size", required_argument, NULL, 0 },
{ "slice-max-mbs", required_argument, NULL, 0 },
{ "slice-min-mbs", required_argument, NULL, 0 },
{ "slices", required_argument, NULL, 0 },
{ "slices-max", required_argument, NULL, 0 },
{ "thread-input", no_argument, NULL, OPT_THREAD_INPUT },
{ "sync-lookahead", required_argument, NULL, 0 },
{ "non-deterministic", no_argument, NULL, 0 },
{ "cpu-independent", no_argument, NULL, 0 },
{ "psnr", no_argument, NULL, 0 },
{ "ssim", no_argument, NULL, 0 },
{ "quiet", no_argument, NULL, OPT_QUIET },
{ "verbose", no_argument, NULL, 'v' },
{ "log-level", required_argument, NULL, OPT_LOG_LEVEL },
{ "no-progress", no_argument, NULL, OPT_NOPROGRESS },
{ "dump-yuv", required_argument, NULL, 0 },
{ "sps-id", required_argument, NULL, 0 },
{ "aud", no_argument, NULL, 0 },
{ "nr", required_argument, NULL, 0 },
{ "cqm", required_argument, NULL, 0 },
{ "cqmfile", required_argument, NULL, 0 },
{ "cqm4", required_argument, NULL, 0 },
{ "cqm4i", required_argument, NULL, 0 },
{ "cqm4iy", required_argument, NULL, 0 },
{ "cqm4ic", required_argument, NULL, 0 },
{ "cqm4p", required_argument, NULL, 0 },
{ "cqm4py", required_argument, NULL, 0 },
{ "cqm4pc", required_argument, NULL, 0 },
{ "cqm8", required_argument, NULL, 0 },
{ "cqm8i", required_argument, NULL, 0 },
{ "cqm8p", required_argument, NULL, 0 },
{ "overscan", required_argument, NULL, 0 },
{ "videoformat", required_argument, NULL, 0 },
{ "range", required_argument, NULL, OPT_RANGE },
{ "colorprim", required_argument, NULL, 0 },
{ "transfer", required_argument, NULL, 0 },
{ "colormatrix", required_argument, NULL, 0 },
{ "chromaloc", required_argument, NULL, 0 },
{ "force-cfr", no_argument, NULL, 0 },
{ "tcfile-in", required_argument, NULL, OPT_TCFILE_IN },
{ "tcfile-out", required_argument, NULL, OPT_TCFILE_OUT },
{ "timebase", required_argument, NULL, OPT_TIMEBASE },
{ "pic-struct", no_argument, NULL, 0 },
{ "crop-rect", required_argument, NULL, 0 },
{ "nal-hrd", required_argument, NULL, 0 },
{ "pulldown", required_argument, NULL, OPT_PULLDOWN },
{ "fake-interlaced", no_argument, NULL, 0 },
{ "frame-packing", required_argument, NULL, 0 },
{ "vf", required_argument, NULL, OPT_VIDEO_FILTER },
{ "video-filter", required_argument, NULL, OPT_VIDEO_FILTER },
{ "input-fmt", required_argument, NULL, OPT_INPUT_FMT },
{ "input-res", required_argument, NULL, OPT_INPUT_RES },
{ "input-csp", required_argument, NULL, OPT_INPUT_CSP },
{ "input-depth", required_argument, NULL, OPT_INPUT_DEPTH },
{ "dts-compress", no_argument, NULL, OPT_DTS_COMPRESSION },
{ "output-csp", required_argument, NULL, OPT_OUTPUT_CSP },
{ "input-range", required_argument, NULL, OPT_INPUT_RANGE },
{ "stitchable", no_argument, NULL, 0 },
{ "filler", no_argument, NULL, 0 },
{0, 0, 0, 0}
};
直接调用getopt_long函数,就可以对argv进行分析,每调用一次getopt_long函数,都可以分析一个参数,同时记录下一个参数的起始位置,供下次调用使用。
for( optind = 0;; )
{
int b_error = 0;
int long_options_index = -1;
int c = getopt_long( argc, argv, short_options, long_options, &long_options_index );
if( c == -1 )
{
break;
}
switch( c )
{
case 'h':
help( &defaults, 0 );
exit(0);
case OPT_LONGHELP:
help( &defaults, 1 );
exit(0);
case OPT_FULLHELP:
help( &defaults, 2 );
exit(0);
case 'V':
print_version_info();
exit(0);
case OPT_FRAMES:
param->i_frame_total = X264_MAX( atoi( optarg ), 0 );
break;
case OPT_SEEK:
opt->i_seek = X264_MAX( atoi( optarg ), 0 );
break;
......