Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
52866 views
1
/*****************************************************************************
2
* example.c: libx264 API usage example
3
*****************************************************************************
4
* Copyright (C) 2014-2016 x264 project
5
*
6
* Authors: Anton Mitrofanov <[email protected]>
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 [email protected].
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_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;
76
param.b_annexb = 1;
77
78
/* Apply profile restrictions. */
79
if( x264_param_apply_profile( &param, "high" ) < 0 )
80
goto fail;
81
82
if( x264_picture_alloc( &pic, param.i_csp, param.i_width, param.i_height ) < 0 )
83
goto fail;
84
#undef fail
85
#define fail fail2
86
87
h = x264_encoder_open( &param );
88
if( !h )
89
goto fail;
90
#undef fail
91
#define fail fail3
92
93
int luma_size = width * height;
94
int chroma_size = luma_size / 4;
95
/* Encode frames */
96
for( ;; i_frame++ )
97
{
98
/* Read input frame */
99
if( fread( pic.img.plane[0], 1, luma_size, stdin ) != luma_size )
100
break;
101
if( fread( pic.img.plane[1], 1, chroma_size, stdin ) != chroma_size )
102
break;
103
if( fread( pic.img.plane[2], 1, chroma_size, stdin ) != chroma_size )
104
break;
105
106
pic.i_pts = i_frame;
107
i_frame_size = x264_encoder_encode( h, &nal, &i_nal, &pic, &pic_out );
108
if( i_frame_size < 0 )
109
goto fail;
110
else if( i_frame_size )
111
{
112
if( !fwrite( nal->p_payload, i_frame_size, 1, stdout ) )
113
goto fail;
114
}
115
}
116
/* Flush delayed frames */
117
while( x264_encoder_delayed_frames( h ) )
118
{
119
i_frame_size = x264_encoder_encode( h, &nal, &i_nal, NULL, &pic_out );
120
if( i_frame_size < 0 )
121
goto fail;
122
else if( i_frame_size )
123
{
124
if( !fwrite( nal->p_payload, i_frame_size, 1, stdout ) )
125
goto fail;
126
}
127
}
128
129
x264_encoder_close( h );
130
x264_picture_clean( &pic );
131
return 0;
132
133
#undef fail
134
fail3:
135
x264_encoder_close( h );
136
fail2:
137
x264_picture_clean( &pic );
138
fail:
139
return -1;
140
}
141
142