1 /*****************************************************************************
2 * example.c: libx264 API usage example
3 *****************************************************************************
4 * Copyright (C) 2014-2015 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 #include <io.h> /* _setmode() */
28 #include <fcntl.h> /* _O_BINARY */
35 #define FAIL_IF_ERROR( cond, ... )\
40 fprintf( stderr, __VA_ARGS__ );\
45 int main( int argc, char **argv )
50 x264_picture_t pic_out;
58 _setmode( _fileno( stdin ), _O_BINARY );
59 _setmode( _fileno( stdout ), _O_BINARY );
60 _setmode( _fileno( stderr ), _O_BINARY );
63 FAIL_IF_ERROR( !(argc > 1), "Example usage: example 352x288 <input.yuv >output.h264\n" );
64 FAIL_IF_ERROR( 2 != sscanf( argv[1], "%dx%d", &width, &height ), "resolution not specified or incorrect\n" );
66 /* Get default params for preset/tuning */
67 if( x264_param_default_preset( ¶m, "medium", NULL ) < 0 )
70 /* Configure non-default params */
71 param.i_csp = X264_CSP_I420;
72 param.i_width = width;
73 param.i_height = height;
74 param.b_vfr_input = 0;
75 param.b_repeat_headers = 1;
78 /* Apply profile restrictions. */
79 if( x264_param_apply_profile( ¶m, "high" ) < 0 )
82 if( x264_picture_alloc( &pic, param.i_csp, param.i_width, param.i_height ) < 0 )
87 h = x264_encoder_open( ¶m );
93 int luma_size = width * height;
94 int chroma_size = luma_size / 4;
98 /* Read input frame */
99 if( fread( pic.img.plane[0], 1, luma_size, stdin ) != luma_size )
101 if( fread( pic.img.plane[1], 1, chroma_size, stdin ) != chroma_size )
103 if( fread( pic.img.plane[2], 1, chroma_size, stdin ) != chroma_size )
107 i_frame_size = x264_encoder_encode( h, &nal, &i_nal, &pic, &pic_out );
108 if( i_frame_size < 0 )
110 else if( i_frame_size )
112 if( !fwrite( nal->p_payload, i_frame_size, 1, stdout ) )
116 /* Flush delayed frames */
117 while( x264_encoder_delayed_frames( h ) )
119 i_frame_size = x264_encoder_encode( h, &nal, &i_nal, NULL, &pic_out );
120 if( i_frame_size < 0 )
122 else if( i_frame_size )
124 if( !fwrite( nal->p_payload, i_frame_size, 1, stdout ) )
129 x264_encoder_close( h );
130 x264_picture_clean( &pic );
135 x264_encoder_close( h );
137 x264_picture_clean( &pic );