forked from AlexVestin/x264-simd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
x264example.c
190 lines (159 loc) · 5.94 KB
/
x264example.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*****************************************************************************
* example.c: libx264 API usage example
*****************************************************************************
* Copyright (C) 2014-2020 x264 project
*
* Authors: Anton Mitrofanov <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
*
* This program is also available under a commercial proprietary license.
* For more information, contact us at [email protected].
*****************************************************************************/
#ifdef _WIN32
#include <io.h> /* _setmode() */
#include <fcntl.h> /* _O_BINARY */
#endif
#include <stdint.h>
#include <stdio.h>
#include <x264.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
int main( int argc, char **argv )
{
int width = 1920, height = 1080;
x264_param_t param;
x264_picture_t pic;
x264_picture_t pic_out;
x264_t *h;
int i_frame = 0;
int i_frame_size;
x264_nal_t *nal;
int i_nal;
// output file
FILE* f = fopen("output.h264", "wb");
/* Get default params for preset/tuning */
if( x264_param_default_preset( ¶m, "medium", NULL ) < 0 )
goto fail;
/* Configure non-default params */
param.i_bitdepth = 8;
// yuv420p
param.i_csp = X264_CSP_I420;
param.i_width = width;
param.i_height = height;
param.b_vfr_input = 0;
param.b_repeat_headers = 1;
param.b_annexb = 1;
/* Apply profile restrictions. */
if( x264_param_apply_profile( ¶m, "high" ) < 0 )
goto fail;
if( x264_picture_alloc( &pic, param.i_csp, param.i_width, param.i_height ) < 0 )
goto fail;
#undef fail
#define fail fail2
h = x264_encoder_open( ¶m );
if( !h )
goto fail;
#undef fail
#define fail fail3
int luma_size = width * height;
int chroma_size = luma_size / 4;
/* Encode frames */
int cidx;
int lidx;
size_t image_size = width * height;
const int nr_frames = 400;
printf("Encoding %d frames\n", nr_frames);
struct timespec stime, etime, encstime, encetime;
clock_gettime(CLOCK_REALTIME, &stime);
double total_time = 0;
for( i_frame = 0; i_frame < nr_frames; i_frame++ ) {
if(i_frame > 0 && !(i_frame % 50)) {
clock_gettime(CLOCK_REALTIME, &etime);
printf("%d frames encoded, time elapsed: %g \n", i_frame, (etime.tv_sec - stime.tv_sec) + 1e-9*(etime.tv_nsec - stime.tv_nsec));
}
/* Read input frame */
size_t upos = image_size;
size_t vpos = upos + upos / 4;
size_t i = 0;
uint8_t r, g, b;
cidx = 0;
lidx = 0;
for( size_t line = 0; line < height; ++line ) {
if( !(line % 2) ) {
for( size_t x = 0; x < width; x += 2 ) {
r = x % 128;
g = i_frame % 256;
b = sinf((float)x / 30 + i_frame / 100);
// y
pic.img.plane[0][cidx++] = ((66*r + 129*g + 25*b) >> 8) + 16;
// u
pic.img.plane[1][lidx] = ((-38*r + -74*g + 112*b) >> 8) + 128;
// v
pic.img.plane[2][lidx++] = ((112*r + -94*g + -18*b) >> 8) + 128;
// y again
pic.img.plane[0][cidx++] = ((66*r + 129*g + 25*b) >> 8) + 16;
}
} else {
for( size_t x = 0; x < width; x += 1 ) {
r = i_frame % 128;
g = x % 256;
b = sinf((float)x / 30 + i_frame / 100);
// y
pic.img.plane[0][cidx++] = ((66*r + 129*g + 25*b) >> 8) + 16;
}
}
}
pic.i_pts = i_frame;
clock_gettime(CLOCK_REALTIME, &encstime);
i_frame_size = x264_encoder_encode( h, &nal, &i_nal, &pic, &pic_out );
clock_gettime(CLOCK_REALTIME, &encetime);
double diff = (encetime.tv_sec - encstime.tv_sec) + 1e-9*(encetime.tv_nsec - encstime.tv_nsec);
total_time += diff;
if( i_frame_size < 0 ) {
goto fail;
} else if( i_frame_size ) {
if( !fwrite( nal->p_payload, i_frame_size, 1, f ) )
goto fail;
}
}
/* Flush delayed frames */
while( x264_encoder_delayed_frames( h ) )
{
i_frame_size = x264_encoder_encode( h, &nal, &i_nal, NULL, &pic_out );
if( i_frame_size < 0 )
goto fail;
else if( i_frame_size )
{
if( !fwrite( nal->p_payload, i_frame_size, 1, f ) )
goto fail;
}
}
x264_encoder_close( h );
x264_picture_clean( &pic );
clock_gettime(CLOCK_REALTIME, &etime);
printf("\n Time: %g\n", (etime.tv_sec - stime.tv_sec) + 1e-9*(etime.tv_nsec - stime.tv_nsec));
printf("Encode time: %f\n", total_time);
printf("Exited cleanly\n");
return 0;
#undef fail
fail3:
x264_encoder_close( h );
fail2:
x264_picture_clean( &pic );
fail:
return -1;
}