1 /*****************************************************************************
2 * example.c: libx264 API usage example
3 *****************************************************************************
4 * Copyright (C) 2014 x264 project
6 * Authors: Anton Mitrofanov <BugMaster@narod.ru>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
22 * This program is also available under a commercial proprietary license.
23 * For more information, contact us at licensing@x264.com.
24 *****************************************************************************/
27 /* The following two defines must be located before the inclusion of any system header files. */
29 #define _WIN32_WINNT 0x0500
31 #include <io.h> /* _setmode() */
32 #include <fcntl.h> /* _O_BINARY */
41 static volatile int b_ctrl_c = 0;
42 static void sigint_handler( int a )
47 #define FAIL_IF_ERROR( cond, ... )\
52 fprintf( stderr, __VA_ARGS__ );\
57 int main( int argc, char **argv )
62 x264_picture_t pic_out;
70 _setmode( _fileno( stdin ), _O_BINARY );
71 _setmode( _fileno( stdout ), _O_BINARY );
72 _setmode( _fileno( stderr ), _O_BINARY );
75 /* Control-C handler */
76 signal( SIGINT, sigint_handler );
78 FAIL_IF_ERROR( !(argc > 1), "Example usage: example 352x288 <input.yuv >output.h264\n" );
79 FAIL_IF_ERROR( 2 != sscanf( argv[1], "%dx%d", &width, &height ), "resolution not specified or incorrect\n" );
81 /* Get default params for preset/tuning */
82 if( x264_param_default_preset( ¶m, "medium", NULL ) < 0 )
85 /* Configure non-default params */
86 param.i_csp = X264_CSP_I420;
87 param.i_width = width;
88 param.i_height = height;
89 param.b_vfr_input = 0;
90 param.b_repeat_headers = 1;
93 /* Apply profile restrictions. */
94 if( x264_param_apply_profile( ¶m, "high" ) < 0 )
97 if( x264_picture_alloc( &pic, param.i_csp, param.i_width, param.i_height ) < 0 )
102 h = x264_encoder_open( ¶m );
109 for( ; !b_ctrl_c; i_frame++ )
111 /* Read input frame */
112 int plane_size = width * height;
113 if( fread( pic.img.plane[0], 1, plane_size, stdin ) != plane_size )
115 plane_size = ((width + 1) >> 1) * ((height + 1) >> 1);
116 if( fread( pic.img.plane[1], 1, plane_size, stdin ) != plane_size )
118 if( fread( pic.img.plane[2], 1, plane_size, stdin ) != plane_size )
122 i_frame_size = x264_encoder_encode( h, &nal, &i_nal, &pic, &pic_out );
123 if( i_frame_size < 0 )
125 else if( i_frame_size )
127 if( !fwrite( nal->p_payload, i_frame_size, 1, stdout ) )
131 /* Flush delayed frames */
132 while( !b_ctrl_c && x264_encoder_delayed_frames( h ) )
134 i_frame_size = x264_encoder_encode( h, &nal, &i_nal, NULL, &pic_out );
135 if( i_frame_size < 0 )
137 else if( i_frame_size )
139 if( !fwrite( nal->p_payload, i_frame_size, 1, stdout ) )
144 x264_encoder_close( h );
145 x264_picture_clean( &pic );
150 x264_encoder_close( h );
152 x264_picture_clean( &pic );