X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=x264.c;h=c4a740094d861198a6c4ecaf2b4f329d07d27274;hb=2b61248f4bcbb5f9df32d940732bc26d8feeda8c;hp=f68755d91528deee0056da304f506e10d77cd3f1;hpb=8d09ebe2e862688ce213d3f098ce7eca719fea23;p=x264 diff --git a/x264.c b/x264.c index f68755d9..c4a74009 100644 --- a/x264.c +++ b/x264.c @@ -1,10 +1,12 @@ /***************************************************************************** * x264: h264 encoder testing program. ***************************************************************************** - * Copyright (C) 2003 Laurent Aimar - * $Id: x264.c,v 1.1 2004/06/03 19:24:12 fenrir Exp $ + * Copyright (C) 2003-2008 x264 project * - * Authors: Laurent Aimar + * Authors: Loren Merritt + * Laurent Aimar + * Steven Walters + * Kieran Kunhya * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -18,7 +20,7 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA. *****************************************************************************/ #include @@ -29,16 +31,16 @@ #include #include "common/common.h" +#include "common/cpu.h" #include "x264.h" #include "muxers.h" -#ifndef _MSC_VER -#include "config.h" +#ifdef _WIN32 +#include +#else +#define SetConsoleTitle(t) #endif -uint8_t *mux_buffer = NULL; -int mux_buffer_size = 0; - /* Ctrl-C handler */ static int b_ctrl_c = 0; static int b_exit_on_ctrl_c = 0; @@ -55,26 +57,90 @@ typedef struct { hnd_t hin; hnd_t hout; FILE *qpfile; + FILE *tcfile_out; + double timebase_convert_multiplier; + int i_pulldown; } cli_opt_t; -/* input file operation function pointers */ -int (*p_open_infile)( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param ); -int (*p_get_frame_total)( hnd_t handle ); -int (*p_read_frame)( x264_picture_t *p_pic, hnd_t handle, int i_frame ); -int (*p_close_infile)( hnd_t handle ); +/* i/o file operation function pointer structs */ +cli_input_t input; +static cli_output_t output; + +static const char * const demuxer_names[] = +{ + "auto", + "yuv", + "y4m", +#ifdef AVS_INPUT + "avs", +#endif +#ifdef LAVF_INPUT + "lavf", +#endif +#ifdef FFMS_INPUT + "ffms", +#endif + 0 +}; + +static const char * const muxer_names[] = +{ + "auto", + "raw", + "mkv", + "flv", +#ifdef MP4_OUTPUT + "mp4", +#endif + 0 +}; + +static const char * const pulldown_names[] = { "none", "22", "32", "64", "double", "triple", "euro", 0 }; -/* output file operation function pointers */ -static int (*p_open_outfile)( char *psz_filename, hnd_t *p_handle ); -static int (*p_set_outfile_param)( hnd_t handle, x264_param_t *p_param ); -static int (*p_write_nalu)( hnd_t handle, uint8_t *p_nal, int i_size ); -static int (*p_set_eop)( hnd_t handle, x264_picture_t *p_picture ); -static int (*p_close_outfile)( hnd_t handle ); +typedef struct{ + int mod; + uint8_t pattern[24]; + float fps_factor; +} cli_pulldown_t; -static void Help( x264_param_t *defaults, int b_longhelp ); +enum pulldown_type_e +{ + X264_PULLDOWN_22 = 1, + X264_PULLDOWN_32, + X264_PULLDOWN_64, + X264_PULLDOWN_DOUBLE, + X264_PULLDOWN_TRIPLE, + X264_PULLDOWN_EURO +}; + +#define TB PIC_STRUCT_TOP_BOTTOM +#define BT PIC_STRUCT_BOTTOM_TOP +#define TBT PIC_STRUCT_TOP_BOTTOM_TOP +#define BTB PIC_STRUCT_BOTTOM_TOP_BOTTOM + +static const cli_pulldown_t pulldown_values[] = +{ + [X264_PULLDOWN_22] = {1, {TB}, 1.0}, + [X264_PULLDOWN_32] = {4, {TBT, BT, BTB, TB}, 1.25}, + [X264_PULLDOWN_64] = {2, {PIC_STRUCT_DOUBLE, PIC_STRUCT_TRIPLE}, 1.0}, + [X264_PULLDOWN_DOUBLE] = {1, {PIC_STRUCT_DOUBLE}, 2.0}, + [X264_PULLDOWN_TRIPLE] = {1, {PIC_STRUCT_TRIPLE}, 3.0}, + [X264_PULLDOWN_EURO] = {24, {TBT, BT, BT, BT, BT, BT, BT, BT, BT, BT, BT, BT, + BTB, TB, TB, TB, TB, TB, TB, TB, TB, TB, TB, TB}, 25.0/24.0} +}; + +#undef TB +#undef BT +#undef TBT +#undef BTB + +// indexed by pic_struct enum +static const float pulldown_frame_duration[10] = { 0.0, 1, 0.5, 0.5, 1, 1, 1.5, 1.5, 2, 3 }; + +static void Help( x264_param_t *defaults, int longhelp ); static int Parse( int argc, char **argv, x264_param_t *param, cli_opt_t *opt ); static int Encode( x264_param_t *param, cli_opt_t *opt ); - /**************************************************************************** * main: ****************************************************************************/ @@ -89,13 +155,11 @@ int main( int argc, char **argv ) pthread_win32_thread_attach_np(); #endif -#ifdef _MSC_VER +#ifdef _WIN32 _setmode(_fileno(stdin), _O_BINARY); _setmode(_fileno(stdout), _O_BINARY); #endif - x264_param_default( ¶m ); - /* Parse command line */ if( Parse( argc, argv, ¶m, &opt ) < 0 ) return -1; @@ -119,31 +183,59 @@ static char const *strtable_lookup( const char * const table[], int index ) return ( ( index >= 0 && index < i ) ? table[ index ] : "???" ); } +static char *stringify_names( char *buf, const char * const names[] ) +{ + int i = 0; + char *p = buf; + for( p[0] = 0; names[i]; i++ ) + { + p += sprintf( p, "%s", names[i] ); + if( names[i+1] ) + p += sprintf( p, ", " ); + } + return buf; +} + /***************************************************************************** * Help: *****************************************************************************/ -static void Help( x264_param_t *defaults, int b_longhelp ) +static void Help( x264_param_t *defaults, int longhelp ) { + char buf[50]; #define H0 printf -#define H1 if(b_longhelp) printf +#define H1 if(longhelp>=1) printf +#define H2 if(longhelp==2) printf H0( "x264 core:%d%s\n" "Syntax: x264 [options] -o outfile infile [widthxheight]\n" "\n" "Infile can be raw YUV 4:2:0 (in which case resolution is required),\n" " or YUV4MPEG 4:2:0 (*.y4m),\n" - " or AVI or Avisynth if compiled with AVIS support (%s).\n" + " or Avisynth if compiled with support (%s).\n" + " or libav* formats if compiled with lavf support (%s) or ffms support (%s).\n" "Outfile type is selected by filename:\n" " .264 -> Raw bytestream\n" " .mkv -> Matroska\n" + " .flv -> Flash Video\n" " .mp4 -> MP4 if compiled with GPAC support (%s)\n" "\n" "Options:\n" "\n" - " -h, --help List the more commonly used options\n" - " --longhelp List all options\n" + " -h, --help List basic options\n" + " --longhelp List more options\n" + " --fullhelp List all options\n" "\n", X264_BUILD, X264_VERSION, -#ifdef AVIS_INPUT +#ifdef AVS_INPUT + "yes", +#else + "no", +#endif +#ifdef LAVF_INPUT + "yes", +#else + "no", +#endif +#ifdef FFMS_INPUT "yes", #else "no", @@ -154,313 +246,712 @@ static void Help( x264_param_t *defaults, int b_longhelp ) "no" #endif ); + H0( "Example usage:\n" ); + H0( "\n" ); + H0( " Constant quality mode:\n" ); + H0( " x264 --crf 24 -o \n" ); + H0( "\n" ); + H0( " Two-pass with a bitrate of 1000kbps:\n" ); + H0( " x264 --pass 1 --bitrate 1000 -o \n" ); + H0( " x264 --pass 2 --bitrate 1000 -o \n" ); + H0( "\n" ); + H0( " Lossless:\n" ); + H0( " x264 --crf 0 -o \n" ); + H0( "\n" ); + H0( " Maximum PSNR at the cost of speed and visual quality:\n" ); + H0( " x264 --preset placebo --tune psnr -o \n" ); + H0( "\n" ); + H0( " Constant bitrate at 1000kbps with a 2 second-buffer:\n"); + H0( " x264 --vbv-bufsize 2000 --bitrate 1000 -o \n" ); + H0( "\n" ); + H0( "Presets:\n" ); + H0( "\n" ); + H0( " --profile Force the limits of an H.264 profile [high]\n" + " Overrides all settings.\n" ); + H2( " - baseline:\n" + " --no-8x8dct --bframes 0 --no-cabac\n" + " --cqm flat --weightp 0\n" + " No interlaced.\n" + " No lossless.\n" + " - main:\n" + " --no-8x8dct --cqm flat\n" + " No lossless.\n" + " - high:\n" + " No lossless.\n" ); + else H0( " - baseline,main,high\n" ); + H0( " --preset Use a preset to select encoding settings [medium]\n" + " Overridden by user settings.\n" ); + H2( " - ultrafast:\n" + " --no-8x8dct --aq-mode 0 --b-adapt 0\n" + " --bframes 0 --no-cabac --no-deblock\n" + " --no-mbtree --me dia --no-mixed-refs\n" + " --partitions none --ref 1 --scenecut 0\n" + " --subme 0 --trellis 0 --no-weightb\n" + " --weightp 0\n" + " - superfast:\n" + " --no-mbtree --me dia --no-mixed-refs\n" + " --partitions i8x8,i4x4 --ref 1\n" + " --subme 1 --trellis 0 --weightp 0\n" + " - veryfast:\n" + " --no-mbtree --no-mixed-refs --ref 1\n" + " --subme 2 --trellis 0 --weightp 0\n" + " - faster:\n" + " --no-mixed-refs --rc-lookahead 20\n" + " --ref 2 --subme 4 --weightp 1\n" + " - fast:\n" + " --rc-lookahead 30 --ref 2 --subme 6\n" + " - medium:\n" + " Default settings apply.\n" + " - slow:\n" + " --b-adapt 2 --direct auto --me umh\n" + " --rc-lookahead 50 --ref 5 --subme 8\n" + " - slower:\n" + " --b-adapt 2 --direct auto --me umh\n" + " --partitions all --rc-lookahead 60\n" + " --ref 8 --subme 9 --trellis 2\n" + " - veryslow:\n" + " --b-adapt 2 --bframes 8 --direct auto\n" + " --me umh --merange 24 --partitions all\n" + " --ref 16 --subme 10 --trellis 2\n" + " --rc-lookahead 60\n" + " - placebo:\n" + " --bframes 16 --b-adapt 2 --direct auto\n" + " --slow-firstpass --no-fast-pskip\n" + " --me tesa --merange 24 --partitions all\n" + " --rc-lookahead 60 --ref 16 --subme 10\n" + " --trellis 2\n" ); + else H0( " - ultrafast,superfast,veryfast,faster,fast\n" + " - medium,slow,slower,veryslow,placebo\n" ); + H0( " --tune Tune the settings for a particular type of source\n" + " or situation\n" + " Overridden by user settings.\n" + " Multiple tunings are separated by commas.\n" + " Only one psy tuning can be used at a time.\n" ); + H2( " - film (psy tuning):\n" + " --deblock -1:-1 --psy-rd :0.15\n" + " - animation (psy tuning):\n" + " --bframes {+2} --deblock 1:1\n" + " --psy-rd 0.4: --aq-strength 0.6\n" + " --ref {Double if >1 else 1}\n" + " - grain (psy tuning):\n" + " --aq-strength 0.5 --no-dct-decimate\n" + " --deadzone-inter 6 --deadzone-intra 6\n" + " --deblock -2:-2 --ipratio 1.1 \n" + " --pbratio 1.1 --psy-rd :0.25\n" + " --qcomp 0.8\n" + " - stillimage (psy tuning):\n" + " --aq-strength 1.2 --deblock -3:-3\n" + " --psy-rd 2.0:0.7\n" + " - psnr (psy tuning):\n" + " --aq-mode 0 --no-psy\n" + " - ssim (psy tuning):\n" + " --aq-mode 2 --no-psy\n" + " - fastdecode:\n" + " --no-cabac --no-deblock --no-weightb\n" + " --weightp 0\n" + " - zerolatency:\n" + " --bframes 0 --force-cfr --rc-lookahead 0\n" + " --sync-lookahead 0 --sliced-threads\n" ); + else H0( " - psy tunings: film,animation,grain,\n" + " stillimage,psnr,ssim\n" + " - other tunings: fastdecode,zerolatency\n" ); + H2( " --slow-firstpass Don't force these faster settings with --pass 1:\n" + " --no-8x8dct --me dia --partitions none\n" + " --ref 1 --subme {2 if >2 else unchanged}\n" + " --trellis 0 --fast-pskip\n" ); + else H1( " --slow-firstpass Don't force faster settings with --pass 1\n" ); + H0( "\n" ); H0( "Frame-type options:\n" ); H0( "\n" ); H0( " -I, --keyint Maximum GOP size [%d]\n", defaults->i_keyint_max ); - H1( " -i, --min-keyint Minimum GOP size [%d]\n", defaults->i_keyint_min ); - H1( " --scenecut How aggressively to insert extra I-frames [%d]\n", defaults->i_scenecut_threshold ); - H1( " --pre-scenecut Faster, less precise scenecut detection.\n" - " Required and implied by multi-threading.\n" ); - H0( " -b, --bframes Number of B-frames between I and P [%d]\n", defaults->i_bframe ); - H1( " --no-b-adapt Disable adaptive B-frame decision\n" ); - H1( " --b-bias Influences how often B-frames are used [%d]\n", defaults->i_bframe_bias ); - H0( " --b-pyramid Keep some B-frames as references\n" ); - H0( " --no-cabac Disable CABAC\n" ); - H0( " -r, --ref Number of reference frames [%d]\n", defaults->i_frame_reference ); + H2( " -i, --min-keyint Minimum GOP size [auto]\n" ); + H2( " --no-scenecut Disable adaptive I-frame decision\n" ); + H2( " --scenecut How aggressively to insert extra I-frames [%d]\n", defaults->i_scenecut_threshold ); + H2( " --intra-refresh Use Periodic Intra Refresh instead of IDR frames\n" ); + H1( " -b, --bframes Number of B-frames between I and P [%d]\n", defaults->i_bframe ); + H1( " --b-adapt Adaptive B-frame decision method [%d]\n" + " Higher values may lower threading efficiency.\n" + " - 0: Disabled\n" + " - 1: Fast\n" + " - 2: Optimal (slow with high --bframes)\n", defaults->i_bframe_adaptive ); + H2( " --b-bias Influences how often B-frames are used [%d]\n", defaults->i_bframe_bias ); + H1( " --b-pyramid Keep some B-frames as references [%s]\n" + " - none: Disabled\n" + " - strict: Strictly hierarchical pyramid\n" + " - normal: Non-strict (not Blu-ray compatible)\n", + strtable_lookup( x264_b_pyramid_names, defaults->i_bframe_pyramid ) ); + H1( " --no-cabac Disable CABAC\n" ); + H1( " -r, --ref Number of reference frames [%d]\n", defaults->i_frame_reference ); H1( " --no-deblock Disable loop filter\n" ); - H0( " -f, --deblock Loop filter AlphaC0 and Beta parameters [%d:%d]\n", + H1( " -f, --deblock Loop filter parameters [%d:%d]\n", defaults->i_deblocking_filter_alphac0, defaults->i_deblocking_filter_beta ); - H0( " --interlaced Enable pure-interlaced mode\n" ); + H2( " --slices Number of slices per frame; forces rectangular\n" + " slices and is overridden by other slicing options\n" ); + else H1( " --slices Number of slices per frame\n" ); + H2( " --slice-max-size Limit the size of each slice in bytes\n"); + H2( " --slice-max-mbs Limit the size of each slice in macroblocks\n"); + H0( " --tff Enable interlaced mode (top field first)\n" ); + H0( " --bff Enable interlaced mode (bottom field first)\n" ); + H2( " --constrained-intra Enable constrained intra prediction.\n" ); + H0( " --pulldown Use soft pulldown to change frame rate\n" + " - none, 22, 32, 64, double, triple, euro (requires cfr input)\n" ); + H2( " --fake-interlaced Flag stream as interlaced but encode progressive.\n" + " Makes it possible to encode 25p and 30p Blu-Ray\n" + " streams. Ignored in interlaced mode.\n" ); H0( "\n" ); H0( "Ratecontrol:\n" ); H0( "\n" ); - H0( " -q, --qp Set QP (0=lossless) [%d]\n", defaults->rc.i_qp_constant ); + H1( " -q, --qp Force constant QP (0-51, 0=lossless)\n" ); H0( " -B, --bitrate Set bitrate (kbit/s)\n" ); - H0( " --crf Quality-based VBR (nominal QP)\n" ); - H1( " --vbv-maxrate Max local bitrate (kbit/s) [%d]\n", defaults->rc.i_vbv_max_bitrate ); - H0( " --vbv-bufsize Enable CBR and set size of the VBV buffer (kbit) [%d]\n", defaults->rc.i_vbv_buffer_size ); - H1( " --vbv-init Initial VBV buffer occupancy [%.1f]\n", defaults->rc.f_vbv_buffer_init ); - H1( " --qpmin Set min QP [%d]\n", defaults->rc.i_qp_min ); - H1( " --qpmax Set max QP [%d]\n", defaults->rc.i_qp_max ); - H1( " --qpstep Set max QP step [%d]\n", defaults->rc.i_qp_step ); - H0( " --ratetol Allowed variance of average bitrate [%.1f]\n", defaults->rc.f_rate_tolerance ); - H0( " --ipratio QP factor between I and P [%.2f]\n", defaults->rc.f_ip_factor ); - H0( " --pbratio QP factor between P and B [%.2f]\n", defaults->rc.f_pb_factor ); - H1( " --chroma-qp-offset QP difference between chroma and luma [%d]\n", defaults->analyse.i_chroma_qp_offset ); - H0( "\n" ); - H0( " -p, --pass <1|2|3> Enable multipass ratecontrol\n" + H0( " --crf Quality-based VBR (0-51, 0=lossless) [%.1f]\n", defaults->rc.f_rf_constant ); + H1( " --rc-lookahead Number of frames for frametype lookahead [%d]\n", defaults->rc.i_lookahead ); + H0( " --vbv-maxrate Max local bitrate (kbit/s) [%d]\n", defaults->rc.i_vbv_max_bitrate ); + H0( " --vbv-bufsize Set size of the VBV buffer (kbit) [%d]\n", defaults->rc.i_vbv_buffer_size ); + H2( " --vbv-init Initial VBV buffer occupancy [%.1f]\n", defaults->rc.f_vbv_buffer_init ); + H2( " --crf-max With CRF+VBV, limit RF to this value\n" + " May cause VBV underflows!\n" ); + H2( " --qpmin Set min QP [%d]\n", defaults->rc.i_qp_min ); + H2( " --qpmax Set max QP [%d]\n", defaults->rc.i_qp_max ); + H2( " --qpstep Set max QP step [%d]\n", defaults->rc.i_qp_step ); + H2( " --ratetol Tolerance of ABR ratecontrol and VBV [%.1f]\n", defaults->rc.f_rate_tolerance ); + H2( " --ipratio QP factor between I and P [%.2f]\n", defaults->rc.f_ip_factor ); + H2( " --pbratio QP factor between P and B [%.2f]\n", defaults->rc.f_pb_factor ); + H2( " --chroma-qp-offset QP difference between chroma and luma [%d]\n", defaults->analyse.i_chroma_qp_offset ); + H2( " --aq-mode AQ method [%d]\n" + " - 0: Disabled\n" + " - 1: Variance AQ (complexity mask)\n" + " - 2: Auto-variance AQ (experimental)\n", defaults->rc.i_aq_mode ); + H1( " --aq-strength Reduces blocking and blurring in flat and\n" + " textured areas. [%.1f]\n", defaults->rc.f_aq_strength ); + H1( "\n" ); + H0( " -p, --pass Enable multipass ratecontrol\n" " - 1: First pass, creates stats file\n" - " - 2: Last pass, does not overwrite stats file\n" - " - 3: Nth pass, overwrites stats file\n" ); - H0( " --stats Filename for 2 pass stats [\"%s\"]\n", defaults->rc.psz_stat_out ); - H1( " --rceq Ratecontrol equation [\"%s\"]\n", defaults->rc.psz_rc_eq ); - H0( " --qcomp QP curve compression: 0.0 => CBR, 1.0 => CQP [%.2f]\n", defaults->rc.f_qcompress ); - H1( " --cplxblur Reduce fluctuations in QP (before curve compression) [%.1f]\n", defaults->rc.f_complexity_blur ); - H1( " --qblur Reduce fluctuations in QP (after curve compression) [%.1f]\n", defaults->rc.f_qblur ); - H0( " --zones //... Tweak the bitrate of some regions of the video\n" ); - H1( " Each zone is of the form\n" + " - 2: Last pass, does not overwrite stats file\n" ); + H2( " - 3: Nth pass, overwrites stats file\n" ); + H1( " --stats Filename for 2 pass stats [\"%s\"]\n", defaults->rc.psz_stat_out ); + H2( " --no-mbtree Disable mb-tree ratecontrol.\n"); + H2( " --qcomp QP curve compression [%.2f]\n", defaults->rc.f_qcompress ); + H2( " --cplxblur Reduce fluctuations in QP (before curve compression) [%.1f]\n", defaults->rc.f_complexity_blur ); + H2( " --qblur Reduce fluctuations in QP (after curve compression) [%.1f]\n", defaults->rc.f_qblur ); + H2( " --zones //... Tweak the bitrate of regions of the video\n" ); + H2( " Each zone is of the form\n" " ,,