]> git.sesse.net Git - x264/blob - example.c
libx264 API usage example
[x264] / example.c
1 /*****************************************************************************
2  * example.c: libx264 API usage example
3  *****************************************************************************
4  * Copyright (C) 2014 x264 project
5  *
6  * Authors: Anton Mitrofanov <BugMaster@narod.ru>
7  *
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.
12  *
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.
17  *
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.
21  *
22  * This program is also available under a commercial proprietary license.
23  * For more information, contact us at licensing@x264.com.
24  *****************************************************************************/
25
26 #ifdef _WIN32
27 /* The following two defines must be located before the inclusion of any system header files. */
28 #define WINVER       0x0500
29 #define _WIN32_WINNT 0x0500
30 #include <windows.h>
31 #include <io.h>       /* _setmode() */
32 #include <fcntl.h>    /* _O_BINARY */
33 #endif
34
35 #include <stdint.h>
36 #include <stdio.h>
37 #include <signal.h>
38 #include <x264.h>
39
40 /* Ctrl-C handler */
41 static volatile int b_ctrl_c = 0;
42 static void sigint_handler( int a )
43 {
44     b_ctrl_c = 1;
45 }
46
47 #define FAIL_IF_ERROR( cond, ... )\
48 do\
49 {\
50     if( cond )\
51     {\
52         fprintf( stderr, __VA_ARGS__ );\
53         goto fail;\
54     }\
55 } while( 0 )
56
57 int main( int argc, char **argv )
58 {
59     int width, height;
60     x264_param_t param;
61     x264_picture_t pic;
62     x264_picture_t pic_out;
63     x264_t *h;
64     int i_frame = 0;
65     int i_frame_size;
66     x264_nal_t *nal;
67     int i_nal;
68
69 #ifdef _WIN32
70     _setmode( _fileno( stdin ),  _O_BINARY );
71     _setmode( _fileno( stdout ), _O_BINARY );
72     _setmode( _fileno( stderr ), _O_BINARY );
73 #endif
74
75     /* Control-C handler */
76     signal( SIGINT, sigint_handler );
77
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" );
80
81     /* Get default params for preset/tuning */
82     if( x264_param_default_preset( &param, "medium", NULL ) < 0 )
83         goto fail;
84
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;
91     param.b_annexb = 1;
92
93     /* Apply profile restrictions. */
94     if( x264_param_apply_profile( &param, "high" ) < 0 )
95         goto fail;
96
97     if( x264_picture_alloc( &pic, param.i_csp, param.i_width, param.i_height ) < 0 )
98         goto fail;
99 #undef fail
100 #define fail fail2
101
102     h = x264_encoder_open( &param );
103     if( !h )
104         goto fail;
105 #undef fail
106 #define fail fail3
107
108     /* Encode frames */
109     for( ; !b_ctrl_c; i_frame++ )
110     {
111         /* Read input frame */
112         int plane_size = width * height;
113         if( fread( pic.img.plane[0], 1, plane_size, stdin ) != plane_size )
114             break;
115         plane_size = ((width + 1) >> 1) * ((height + 1) >> 1);
116         if( fread( pic.img.plane[1], 1, plane_size, stdin ) != plane_size )
117             break;
118         if( fread( pic.img.plane[2], 1, plane_size, stdin ) != plane_size )
119             break;
120
121         pic.i_pts = i_frame;
122         i_frame_size = x264_encoder_encode( h, &nal, &i_nal, &pic, &pic_out );
123         if( i_frame_size < 0 )
124             goto fail;
125         else if( i_frame_size )
126         {
127             if( !fwrite( nal->p_payload, i_frame_size, 1, stdout ) )
128                 goto fail;
129         }
130     }
131     /* Flush delayed frames */
132     while( !b_ctrl_c && x264_encoder_delayed_frames( h ) )
133     {
134         i_frame_size = x264_encoder_encode( h, &nal, &i_nal, NULL, &pic_out );
135         if( i_frame_size < 0 )
136             goto fail;
137         else if( i_frame_size )
138         {
139             if( !fwrite( nal->p_payload, i_frame_size, 1, stdout ) )
140                 goto fail;
141         }
142     }
143
144     x264_encoder_close( h );
145     x264_picture_clean( &pic );
146     return 0;
147
148 #undef fail
149 fail3:
150     x264_encoder_close( h );
151 fail2:
152     x264_picture_clean( &pic );
153 fail:
154     return -1;
155 }