/***************************************************************************** * x264: h264 encoder/decoder testing program. ***************************************************************************** * Copyright (C) 2003 Laurent Aimar * $Id: x264.c,v 1.1 2004/06/03 19:24:12 fenrir Exp $ * * Authors: Laurent Aimar * * 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 * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * 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. *****************************************************************************/ #define _LARGEFILE_SOURCE #define _FILE_OFFSET_BITS 64 #include #include #include #include #include #define _GNU_SOURCE #include #ifdef _MSC_VER #include /* _setmode() */ #include /* _O_BINARY */ #endif #ifndef _MSC_VER #include "config.h" #endif #ifdef AVIS_INPUT #include #include #endif #ifdef MP4_OUTPUT #include #endif #include "common/common.h" #include "x264.h" #include "matroska.h" #define DATA_MAX 3000000 uint8_t data[DATA_MAX]; typedef void *hnd_t; /* Ctrl-C handler */ static int b_ctrl_c = 0; static int b_exit_on_ctrl_c = 0; static void SigIntHandler( int a ) { if( b_exit_on_ctrl_c ) exit(0); b_ctrl_c = 1; } typedef struct { int b_decompress; int b_progress; int i_seek; hnd_t hin; hnd_t hout; } cli_opt_t; /* input file operation function pointers */ static int (*p_open_infile)( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param ); static int (*p_get_frame_total)( hnd_t handle, int i_width, int i_height ); static int (*p_read_frame)( x264_picture_t *p_pic, hnd_t handle, int i_frame, int i_width, int i_height ); static int (*p_close_infile)( hnd_t handle ); static int open_file_yuv( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param ); static int get_frame_total_yuv( hnd_t handle, int i_width, int i_height ); static int read_frame_yuv( x264_picture_t *p_pic, hnd_t handle, int i_frame, int i_width, int i_height ); static int close_file_yuv( hnd_t handle ); #ifdef AVIS_INPUT static int open_file_avis( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param ); static int get_frame_total_avis( hnd_t handle, int i_width, int i_height ); static int read_frame_avis( x264_picture_t *p_pic, hnd_t handle, int i_frame, int i_width, int i_height ); static int close_file_avis( hnd_t handle ); #endif /* 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 ); static int open_file_bsf( char *psz_filename, hnd_t *p_handle ); static int set_param_bsf( hnd_t handle, x264_param_t *p_param ); static int write_nalu_bsf( hnd_t handle, uint8_t *p_nal, int i_size ); static int set_eop_bsf( hnd_t handle, x264_picture_t *p_picture ); static int close_file_bsf( hnd_t handle ); #ifdef MP4_OUTPUT static int open_file_mp4( char *psz_filename, hnd_t *p_handle ); static int set_param_mp4( hnd_t handle, x264_param_t *p_param ); static int write_nalu_mp4( hnd_t handle, uint8_t *p_nal, int i_size ); static int set_eop_mp4( hnd_t handle, x264_picture_t *p_picture ); static int close_file_mp4( hnd_t handle ); #endif static int open_file_mkv( char *psz_filename, hnd_t *p_handle ); static int set_param_mkv( hnd_t handle, x264_param_t *p_param ); static int write_nalu_mkv( hnd_t handle, uint8_t *p_nal, int i_size ); static int set_eop_mkv( hnd_t handle, x264_picture_t *p_picture ); static int close_file_mkv( hnd_t handle ); static void Help( x264_param_t *defaults ); 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: ****************************************************************************/ int main( int argc, char **argv ) { x264_param_t param; cli_opt_t opt; #ifdef _MSC_VER _setmode(_fileno(stdin), _O_BINARY); /* thanks to Marcos Morais */ _setmode(_fileno(stdout), _O_BINARY); #endif x264_param_default( ¶m ); /* Parse command line */ if( Parse( argc, argv, ¶m, &opt ) < 0 ) return -1; /* Control-C handler */ signal( SIGINT, SigIntHandler ); return Encode( ¶m, &opt ); } static const char * const overscan_str[] = { "undef", "show", "crop", NULL }; static const char * const vidformat_str[] = { "component", "pal", "ntsc", "secam", "mac", "undef", NULL }; static const char * const fullrange_str[] = { "off", "on", NULL }; static const char * const colorprim_str[] = { "", "bt709", "undef", "", "bt470m", "bt470bg", "smpte170m", "smpte240m", "film", NULL }; static const char * const transfer_str[] = { "", "bt709", "undef", "", "bt470m", "bt470bg", "smpte170m", "smpte240m", "linear", "log100", "log316", NULL }; static const char * const colmatrix_str[] = { "GBR", "bt709", "undef", "", "fcc", "bt470bg", "smpte170m", "smpte240m", "YCgCo", NULL }; static char const *strtable_lookup( const char * const table[], int index ) { int i = 0; while( table[i] ) i++; return ( ( index >= 0 && index < i ) ? table[ index ] : "???" ); } /***************************************************************************** * Help: *****************************************************************************/ static void Help( x264_param_t *defaults ) { fprintf( stderr, "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 AVI or Avisynth if compiled with AVIS support (%s).\n" "Outfile type is selected by filename:\n" " .264 -> Raw bytestream\n" " .mkv -> Matroska\n" " .mp4 -> MP4 if compiled with GPAC support (%s)\n" "\n" "Options:\n" "\n" " -h, --help Print this help\n" "\n" "Frame-type options:\n" "\n" " -I, --keyint Maximum GOP size [%d]\n" " -i, --min-keyint Minimum GOP size [%d]\n" " --scenecut How aggressively to insert extra I-frames [%d]\n" " -b, --bframes Number of B-frames between I and P [%d]\n" " --no-b-adapt Disable adaptive B-frame decision\n" " --b-bias Influences how often B-frames are used [%d]\n" " --b-pyramid Keep some B-frames as references\n" "\n" " --no-cabac Disable CABAC\n" " -r, --ref Number of reference frames [%d]\n" " --nf Disable loop filter\n" " -f, --filter Loop filter AlphaC0 and Beta parameters [%d:%d]\n" "\n" "Ratecontrol:\n" "\n" " -q, --qp Set QP (0=lossless) [%d]\n" " -B, --bitrate Set bitrate\n" " --crf Quality-based VBR (nominal QP)\n" " --qpmin Set min QP [%d]\n" " --qpmax Set max QP [%d]\n" " --qpstep Set max QP step [%d]\n" " --ratetol Allowed variance of average bitrate [%.1f]\n" " --vbv-maxrate Max local bitrate [%d]\n" " --vbv-bufsize Size of VBV buffer [%d]\n" " --vbv-init Initial VBV buffer occupancy [%.1f]\n" "\n" " --ipratio QP factor between I and P [%.2f]\n" " --pbratio QP factor between P and B [%.2f]\n" " --chroma-qp-offset QP difference between chroma and luma [%d]\n" "\n" " -p, --pass <1|2|3> 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" " --stats Filename for 2 pass stats [\"%s\"]\n" " --rceq Ratecontrol equation [\"%s\"]\n" " --qcomp QP curve compression: 0.0 => CBR, 1.0 => CQP [%.2f]\n" " --cplxblur Reduce fluctuations in QP (before curve compression) [%.1f]\n" " --qblur Reduce fluctuations in QP (after curve compression) [%.1f]\n" "\n" " --zones //...\n" " Tweak the bitrate of some regions of the video\n" " Each zone is of the form\n" " ,,