[Audio and Video] Ubuntu installs the open source H.264 standard codec library x264

1 x264

   x264 is a free and open source video codec (library) based on the H.264/AVC standard. x264 is currently the most widely used and best H.264 codec. The x264 codec supports many functions, including:

  • 8x8 and 4x4 adaptive spatial transformation

  • Adaptive B frame selection

  • B frame as a reference frame/arbitrary frame sequence

  • CAVLC/CABAC entropy coding

  • Custom quantization matrix

    I frame: all macroblock types (16x16, 8x8, 4x4, PCM and all predicted PCM)

    P frame: all partitions (from 16x16 to 4x4)

    B frame: partition from 16x16 to 8x8 (including skip/direct)

  • Interlaced scan (MBAFF)

  • Multiple reference frame

  • Bit rate control: constant quantizer, constant quality, average bit rate of single or multiple encoding , optional VBV parameters

  • Scene change detection

  • B-frame time domain, spatial domain direct mode adaptive selection

  • Supports parallel encoding on multiple CPUs

  • Predictive lossless coding

  • Psy optimization for detail preservation (adaptive quantization, psyrd, psygrid)

  • Area used to adjust bit rate distribution arbitrarily


2 Installation

  • Assembly tool installation
  • x264 library installation

2.1 Install assembly tools

  The x264 library depends on the assembly tool (asm). The assembly tool must be installed first, otherwise the compilation of x264 will fail. The current x264 library uses nasm, and the old version of the x264 library depends on yasm; nasm needs to be installed here.

nasm download address:

  The latest version: https://www.nasm.us/pub/nasm/releasebuilds/2.14.02/

  The version I use: https://www.nasm.us/pub/nasm/releasebuilds/2.13.03/

  There are various compressed file formats. Choose a decompression tool that has the corresponding format installed on your system, eliminating the need to install an additional decompression tool. Choose the .bz2compressed version here .

Insert picture description here

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

2.2 Install x264 library

  x264 library download address: http://www.videolan.org/developers/x264.html

  x264 code repository: 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

  After the compilation is completed successfully, a static library libx264.aand a dynamic library are generated in the current directory libx264.so.157.

Insert picture description here

  Installation is to libx264.so.157install (copy) the dynamic library to the /usr/local/libdirectory, and create a soft link file libx264.soto link to the libx264.so.157dynamic library. Because, when the application program that depends on the x264 library is executed, the library name of the index link is libx264.so. The advantage of using soft links is that the application index is not indexed by the dynamic library due to the update of the x264 library. The x264 library is continuously updated and iteratively. The compiled dynamic library name usually has a version suffix, such libx264.so.157as the "157" suffix in; if you use a specific version of the dynamic library as the index name, you will need to modify the script or program when updating the dynamic library . Therefore, the most ideal way is to establish a soft link.

Insert picture description here


3 test

  A test example is provided in the source code x264.c. This example is a basic video encoding process that can convert the YUV video stream into 264, flv, mkv and other formats. Through this example, the powerful function of x264 can be realized intuitively. When compiling the x264 library, the source code of the test example has been compiled, and the compiled executable file is x264, you can execute it to ./x264 -helpview the usage method.

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)

  Download a YUV sequence file from the Internet to test the effect, and the encoded file can be played by VLC player.

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

  Perform encoding, you can see the information of the original YUV sequence file, the compressed information of the encoded I frame, P frame, and B frame, and the encoding rate is 631.36fps. The size of the encoded file (bus.264) is 98.5K, the original YUV file is 2.71M, and the compression rate is 3%.


  Use fast encoding:

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

  Using fast encoding, the encoding rate is increased to 1443.39fps, the encoded file is 248K, and the compression rate is close to 9% (the lower the compression rate, the greater the compression ratio).


4 Reference articles

[1] H264 basic introduction
[2] H264 basic principles

Guess you like

Origin blog.csdn.net/qq_20553613/article/details/106649938