x264编译测试
# 编译nasm
下载地址:https://www.nasm.us/pub/nasm/releasebuilds/2.14.01/
下载nasm-2.14.01.tar.bz2
解压
tar -jxvf nasm-2.14.01.tar.bz2
1
1
./configure
make
make install
1
2
3
2
3
1
2
3
2
3
# 编译x264
下载
官网: x264, the best H.264/AVC encoder - VideoLAN (opens new window)
解压
tar -jxvf x264-master.tar.bz2
1
1
安装
./configure --prefix=/usr/local/x264 --enable-static --enable-shared
1
1
如果想开启gprof
则需对configure之后生成的config.mak中的CFLAGS和LDFLAGS增加-pg选项,另外需要去掉-fomit-frame-pointer选项
make
make install
1
2
2
1
2
2
vim /etc/ld.so.conf
1
1
安装
apt-get install libx264-dev
1
1
# 测试
素材网站:
YUV Sequences (asu.edu) (opens new window)
./x264 -o output.h264 bridge-close_352x288.yuv
1
1
如果为yuv格式,则需命名加上分辨率,如:
xxx_353x288.yuv
性能分析:
gprof ./x264 gmon.out >profile.txt
11
编译自带的example.c
gcc example.c -o example -lx264
1
1
./example
1
1
# Example.c
新建demo.c
#include <stdint.h>
#include "x264.h"
#include "x264_config.h"
#include <stdio.h>
int main()
{
int width = 480;
int height = 272;
int fps = 25;
size_t yuv_size = width * height * 3 / 2;
x264_t *encoder;
x264_picture_t pic_in, pic_out;
int inf, outf;
uint8_t *yuv_buffer;
x264_param_t m_param;
x264_param_default_preset(&m_param, "veryfast", "zerolatency");
m_param.i_threads = 1;
m_param.i_width = width;
m_param.i_height = height;
m_param.i_fps_num = fps;
m_param.i_bframe = 10;
m_param.i_fps_den = 1;
m_param.i_keyint_max = 25;
m_param.b_intra_refresh = 1;
m_param.b_annexb = 1;
x264_param_apply_profile(&m_param, "high422");
encoder = x264_encoder_open(&m_param);
x264_encoder_parameters(encoder, &m_param);
x264_picture_alloc(&pic_in, X264_CSP_I420, width, height);
yuv_buffer = malloc(yuv_size);
pic_in.img.plane[0] = yuv_buffer;
pic_in.img.plane[1] = pic_in.img.plane[0] + width * height;
pic_in.img.plane[2] = pic_in.img.plane[1] + width * height / 4;
FILE *infile = fopen("bridge_352x288.yuv", "rb");
FILE *outfile = fopen("out.h264", "ab");
if (!infile || !outfile)
{
printf("open file error\n");
return 0;
}
int64_t i_pts = 0;
x264_nal_t *nals;
int nnal;
while (fread(yuv_buffer, 1, yuv_size, infile) > 0)
{
pic_in.i_pts = i_pts++;
x264_encoder_encode(encoder, &nals, &nnal, &pic_in, &pic_out);
x264_nal_t *nal;
for (nal = nals; nal < nals + nnal; nal++)
{
fwrite(nal->p_payload, 1, nal->i_payload, outfile);
}
}
x264_encoder_close(encoder);
close(infile);
close(outfile);
free(yuv_buffer);
return 0;
}
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
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
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
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
gcc demo.c -o demo -lx264
1
1
Last updated: 2022/12/04, 13:58:10