1 /*****************************************************************************
2 * example.c: libx264 API usage example
3 *****************************************************************************
4 * Copyright (C) 2014-2019 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 */
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;
79 /* Apply profile restrictions. */
80 if( x264_param_apply_profile( ¶m
, "high" ) < 0 )
83 if( x264_picture_alloc( &pic
, param
.i_csp
, param
.i_width
, param
.i_height
) < 0 )
88 h
= x264_encoder_open( ¶m
);
94 int luma_size
= width
* height
;
95 int chroma_size
= luma_size
/ 4;
99 /* Read input frame */
100 if( fread( pic
.img
.plane
[0], 1, luma_size
, stdin
) != luma_size
)
102 if( fread( pic
.img
.plane
[1], 1, chroma_size
, stdin
) != chroma_size
)
104 if( fread( pic
.img
.plane
[2], 1, chroma_size
, stdin
) != chroma_size
)
108 i_frame_size
= x264_encoder_encode( h
, &nal
, &i_nal
, &pic
, &pic_out
);
109 if( i_frame_size
< 0 )
111 else if( i_frame_size
)
113 if( !fwrite( nal
->p_payload
, i_frame_size
, 1, stdout
) )
117 /* Flush delayed frames */
118 while( x264_encoder_delayed_frames( h
) )
120 i_frame_size
= x264_encoder_encode( h
, &nal
, &i_nal
, NULL
, &pic_out
);
121 if( i_frame_size
< 0 )
123 else if( i_frame_size
)
125 if( !fwrite( nal
->p_payload
, i_frame_size
, 1, stdout
) )
130 x264_encoder_close( h
);
131 x264_picture_clean( &pic
);
136 x264_encoder_close( h
);
138 x264_picture_clean( &pic
);