【音视频】Ubuntu安装开源H.264标准编解码库x264

1 x264

   x264是基于H.264/AVC标准的一款免费、开源的视频编解码器(库),x264是目前使用最广的、最优秀的一款H.264编解码器。x264编解码器支持的功能众多,包括:

  • 8x8与4x4自适应空间变换

  • 自适应B帧选择

  • B帧作为参考帧/任意帧的顺序

  • CAVLC / CABAC熵编码

  • 自定义量化矩阵

    I帧:所有宏块类型(16x16, 8x8, 4x4, PCM和所有预测PCM)

    P帧:所有分区(从16x16到4x4)

    B帧:从16x16到8x8的分区( 包括skip/direct )

  • 隔行扫描(MBAFF)

  • 多参考帧

  • 码率控制:恒定量化器,恒定质量, 单次或者多次编码的平均码率 ,可选VBV参数

  • 场景变化检测

  • B帧时间域、空间域direct模式自适应选择

  • 支持多个cpu上并行编码

  • 预测性无损编码

  • 用于细节保留的Psy优化(自适应量化、psyrd、psygrid)

  • 用于任意调整比特率分布的区域


2 安装

  • 汇编工具安装
  • x264库安装

2.1 安装汇编工具

  x264库依赖于汇编工具(asm),首先需安装汇编工具,否则编译x264失败。目前x264库使用的是nasm,旧版本x264库依赖于yasm;这里需安装nasm。

nasm下载地址:

  最新版本:https://www.nasm.us/pub/nasm/releasebuilds/2.14.02/

  本人使用的版本:https://www.nasm.us/pub/nasm/releasebuilds/2.13.03/

  各种压缩文件格式都有,选择一个自己系统已经安装了相应格式的解压工具,省去再安装一个额外解压工具的过程。这里选择.bz2压缩版本。

在这里插入图片描述

# 解压
tar -xvjf nasm-2.13.03.tar.bz2
# 配置
cd nasm-2.13.03
./configure
# 编译
make
# 安装到系统
sudo make install

2.2 安装x264库

  x264库下载地址:http://www.videolan.org/developers/x264.html

  x264代码仓库:https://code.videolan.org/videolan/x264.git

# 解压
tar -xvjf last_x264.tar.bz2 
# 配置
cd x264-snapshot-20190708-2245
./configure --enable-shared
# 编译
make
# 安装到系统
sudo make install

  编译完成成功后,在当前目录生成静态库libx264.a和动态库libx264.so.157

在这里插入图片描述

  安装则是将动态库libx264.so.157安装(拷贝)到/usr/local/lib目录下,并创建一个软链接文件libx264.so,链接到libx264.so.157动态库。因为,依赖于x264库的应用程序执行时,索引链接的库名称是libx264.so。使用软链接的好处就是,不因为x264库的更新导致应用程序索引不到动态库。x264的库是持续更新迭代的,编译后的动态库名称一般会带版本后缀,如libx264.so.157中的“157”后缀;如果以具体版本的动态库为索引名称,更新动态库时将需要修改脚本或者程序。因此,最理想的方式是建立一个软链接。

在这里插入图片描述


3 测试

  源码中提供了一个测试例子x264.c,该例子是一个基本的视频编码过程,可以将YUV视频流转换成264、flv、mkv等格式的视频。通过这个例子初步直观体会x264的强大功能。编译x264库的时候,已经将测试例子源码编译,编译后的执行文件为x264,可以执行./x264 -help查看使用方法。

acuity@ubuntu:/mnt/hgfs/LSW/X264/x264 tools/x264-snapshot-20190708-2245$ ./x264 -h
x264 core:157
Syntax: x264 [options] -o outfile infile

Infile can be raw (in which case resolution is required),
  or YUV4MPEG (*.y4m),
  or Avisynth if compiled with support (yes).
  or libav* formats if compiled with lavf support (no) or ffms support (no).
Outfile type is selected by filename:
 .264 -> Raw bytestream
 .mkv -> Matroska
 .flv -> Flash Video
 .mp4 -> MP4 if compiled with GPAC or L-SMASH support (no)

  从网络上下载一个YUV序列文件测试效果,编码后的文件可以通VLC播放器播放。

acuity@ubuntu:/mnt/hgfs/LSW/X264/x264 tools/x264-snapshot-20190708-2245$ ./x264 BUS_176x144_15_orig_01.yuv -o bus.264
yuv [info]: 176x144p 0:0 @ 25/1 fps (cfr)
x264 [info]: using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3
x264 [info]: profile Progressive High, level 1.1, 4:2:0, 8-bit
x264 [info]: frame I:1     Avg QP:28.16  size:  5745                           
x264 [info]: frame P:30    Avg QP:28.28  size:  2143
x264 [info]: frame B:44    Avg QP:31.38  size:   702
x264 [info]: consecutive B-frames:  6.7% 34.7% 32.0% 26.7%
x264 [info]: mb I  I16..4:  0.0% 54.5% 45.5%
x264 [info]: mb P  I16..4:  0.1%  3.5%  4.6%  P16..4: 25.6% 34.7% 29.1%  0.0%  0.0%    skip: 2.4%
x264 [info]: mb B  I16..4:  0.0%  0.0%  0.0%  B16..8: 43.3% 25.4% 10.6%  direct: 3.9%  skip:16.7%  L0:37.5% L1:39.9% BI:22.7%
x264 [info]: 8x8 transform intra:45.8% inter:41.9%
x264 [info]: coded y,uvDC,uvAC intra: 97.3% 84.6% 53.3% inter: 38.3% 12.6% 1.6%
x264 [info]: i16 v,h,dc,p: 25% 25% 50%  0%
x264 [info]: i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 20% 19% 24%  5%  5%  3%  6%  7% 11%
x264 [info]: i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 20% 29% 19%  6%  5%  4%  5%  6%  7%
x264 [info]: i8c dc,h,v,p: 69% 20%  6%  5%
x264 [info]: Weighted P-Frames: Y:13.3% UV:3.3%
x264 [info]: ref P L0: 65.1% 22.0%  7.3%  5.1%  0.6%
x264 [info]: ref B L0: 90.7%  8.4%  0.8%
x264 [info]: ref B L1: 99.4%  0.6%
x264 [info]: kb/s:269.10

encoded 75 frames, 631.36 fps, 269.10 kb/s

  执行编码,可以看到原始YUV序列文件的信息,编码后I帧、P帧、B帧的压缩信息,编码率为631.36fps。编码后文件(bus.264)大小为98.5K,原YUV文件为2.71M,压缩率3%。


  使用快速编码方式:

acuity@ubuntu:/mnt/hgfs/LSW/X264/x264 tools/x264-snapshot-20190708-2245$ ./x264 BUS_176x144_15_orig_01.yuv --input-res 176x144 -o bus1.264 --no-8x8dct --aq-mode 0 --b-adapt 0 --bframes 0 --no-cabac --no-deblock --no-mbtree --me dia --no-mixed-refs --partitions none --rc-lookahead 0 --ref 1 --scenecut 0 --subme 0 --trellis 0
yuv [info]: 176x144p 0:0 @ 25/1 fps (cfr)
x264 [info]: using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3
x264 [info]: profile Main, level 1.1, 4:2:0, 8-bit
x264 [info]: frame I:1     Avg QP:20.00  size: 10256                           
x264 [info]: frame P:74    Avg QP:27.54  size:  3294
x264 [info]: mb I  I16..4:  2.0%  0.0% 98.0%
x264 [info]: mb P  I16..4:  3.5%  0.0%  0.0%  P16..4: 93.2%  0.0%  0.0%  0.0%  0.0%    skip: 3.3%
x264 [info]: coded y,uvDC,uvAC intra: 97.5% 72.8% 56.4% inter: 77.3% 20.5% 3.8%
x264 [info]: i16 v,h,dc,p: 22% 68%  8%  2%
x264 [info]: i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 21% 25% 13%  6%  6%  4%  8%  6% 11%
x264 [info]: i8c dc,h,v,p: 29% 49% 16%  6%
x264 [info]: Weighted P-Frames: Y:6.8% UV:0.0%
x264 [info]: kb/s:677.44

encoded 75 frames, 1441.39 fps, 677.44 kb/s

  使用快速编码,编码率提高到1441.39fps,编码后文件为248K,压缩率接近9%(压缩率越低压缩比越大)。


4 参考文章

【1】H264基础简介
【2】H264基本原理

猜你喜欢

转载自blog.csdn.net/qq_20553613/article/details/106649938