]> granicus.if.org Git - libx264/blob - example.c
mp4: Update GPAC support to v0.8.0 or later
[libx264] / example.c
1 /*****************************************************************************
2  * example.c: libx264 API usage example
3  *****************************************************************************
4  * Copyright (C) 2014-2020 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 #include <io.h>       /* _setmode() */
28 #include <fcntl.h>    /* _O_BINARY */
29 #endif
30
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <x264.h>
34
35 #define FAIL_IF_ERROR( cond, ... )\
36 do\
37 {\
38     if( cond )\
39     {\
40         fprintf( stderr, __VA_ARGS__ );\
41         goto fail;\
42     }\
43 } while( 0 )
44
45 int main( int argc, char **argv )
46 {
47     int width, height;
48     x264_param_t param;
49     x264_picture_t pic;
50     x264_picture_t pic_out;
51     x264_t *h;
52     int i_frame = 0;
53     int i_frame_size;
54     x264_nal_t *nal;
55     int i_nal;
56
57 #ifdef _WIN32
58     _setmode( _fileno( stdin ),  _O_BINARY );
59     _setmode( _fileno( stdout ), _O_BINARY );
60     _setmode( _fileno( stderr ), _O_BINARY );
61 #endif
62
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" );
65
66     /* Get default params for preset/tuning */
67     if( x264_param_default_preset( &param, "medium", NULL ) < 0 )
68         goto fail;
69
70     /* Configure non-default params */
71     param.i_bitdepth = 8;
72     param.i_csp = X264_CSP_I420;
73     param.i_width  = width;
74     param.i_height = height;
75     param.b_vfr_input = 0;
76     param.b_repeat_headers = 1;
77     param.b_annexb = 1;
78
79     /* Apply profile restrictions. */
80     if( x264_param_apply_profile( &param, "high" ) < 0 )
81         goto fail;
82
83     if( x264_picture_alloc( &pic, param.i_csp, param.i_width, param.i_height ) < 0 )
84         goto fail;
85 #undef fail
86 #define fail fail2
87
88     h = x264_encoder_open( &param );
89     if( !h )
90         goto fail;
91 #undef fail
92 #define fail fail3
93
94     int luma_size = width * height;
95     int chroma_size = luma_size / 4;
96     /* Encode frames */
97     for( ;; i_frame++ )
98     {
99         /* Read input frame */
100         if( fread( pic.img.plane[0], 1, luma_size, stdin ) != (unsigned)luma_size )
101             break;
102         if( fread( pic.img.plane[1], 1, chroma_size, stdin ) != (unsigned)chroma_size )
103             break;
104         if( fread( pic.img.plane[2], 1, chroma_size, stdin ) != (unsigned)chroma_size )
105             break;
106
107         pic.i_pts = i_frame;
108         i_frame_size = x264_encoder_encode( h, &nal, &i_nal, &pic, &pic_out );
109         if( i_frame_size < 0 )
110             goto fail;
111         else if( i_frame_size )
112         {
113             if( !fwrite( nal->p_payload, i_frame_size, 1, stdout ) )
114                 goto fail;
115         }
116     }
117     /* Flush delayed frames */
118     while( x264_encoder_delayed_frames( h ) )
119     {
120         i_frame_size = x264_encoder_encode( h, &nal, &i_nal, NULL, &pic_out );
121         if( i_frame_size < 0 )
122             goto fail;
123         else if( i_frame_size )
124         {
125             if( !fwrite( nal->p_payload, i_frame_size, 1, stdout ) )
126                 goto fail;
127         }
128     }
129
130     x264_encoder_close( h );
131     x264_picture_clean( &pic );
132     return 0;
133
134 #undef fail
135 fail3:
136     x264_encoder_close( h );
137 fail2:
138     x264_picture_clean( &pic );
139 fail:
140     return -1;
141 }