18、x264编码在zedboard上的实现(软编码)
一、x264开源包获取
x264-snapshot提供了开源x264源代码,已经在X86和ARM架构下均已实现。linux下可以使用git获得最新的代码包
git clone git://git.videolan.org/x264.git
也可以通过网页下载:
http://download.videolan.org/pub/videolan/x264/snapshots/
二、配置、安装x264-snapshot
下载到源码后,对其进行配置,以便在后续make时编译出zed上可以运行的可执行程序
1、解压源码包后,进入源码目录
./configure --host=arm-xilinx-linux-gnueabi --prefix=/opt/rcq --enable-shared --disable-asm
congfig之后,设定了host为arm-xilinx-linux-gnueabi,install路径是/opt/rcq
2、修改config.mak文件
针对zed的gcc,需要修改config.mak文件
1 SRCPATH=. 2 prefix=/opt/rcq 3 exec_prefix=${prefix} 4 bindir=${exec_prefix}/bin 5 libdir=${exec_prefix}/lib 6 includedir=${prefix}/include 7 ARCH=ARM 8 SYS=LINUX 9 CC=arm-xilinx-linux-gnueabi-gcc 10 CFLAGS=-Wshadow -O3 -fno-fast-math -Wall -I. -I$(SRCPATH) -std=gnu99 -fPIC -fomit-frame-pointer -fno-tree-vectorize 11 DEPMM=-MM -g0 12 DEPMT=-MT 13 LD=arm-xilinx-linux-gnueabi-gcc -o 14 LDFLAGS= -lm -lpthread 15 LIBX264=libx264.a 16 AR=arm-xilinx-linux-gnueabi-ar rc 17 RANLIB=arm-xilinx-linux-gnueabi-ranlib 18 STRIP=arm-xilinx-linux-gnueabi-strip 19 AS= 20 ASFLAGS= -DHAVE_ALIGNED_STACK=1 -DPIC -DHIGH_BIT_DEPTH=0 -DBIT_DEPTH=8 21 RC= 22 RCFLAGS= 23 EXE= 24 HAVE_GETOPT_LONG=1 25 DEVNULL=/dev/null 26 PROF_GEN_CC=-fprofile-generate 27 PROF_GEN_LD=-fprofile-generate 28 PROF_USE_CC=-fprofile-use 29 PROF_USE_LD=-fprofile-use 30 default: cli 31 install: install-cli 32 SOSUFFIX=so 33 SONAME=libx264.so.129 34 SOFLAGS=-shared -Wl,-soname,$(SONAME) -Wl,-Bsymbolic 35 default: lib-shared 36 install: install-lib-shared 37 LDFLAGSCLI = 38 CLI_LIBX264 = $(LIBX264)
其中9、13、16、17、18行为修改后的。
3、make
make install make
完运行库的安装和成执行文件的生成
三、在zed上测试x264
将可执行x264和测试rcq.yuv文件拷贝到zed上,使用默认运行命令
./x264 rcq.yuv --input-res 640x480 -o rcq.flv
其中--input-res 640x480设定输入yuv视频帧的大小,rcq.flv是输出文件,可以设定为.mkv/.mp4/H264等。从控制台可以看到
使用的profile是最高等级High level 3.0,但是的编码速率只有1.40fps……情何以堪……
编码结束后:
生成的rcq.flv文件大小348k,相比较原来的rcq.yuv文件大小33M,压缩率在1%。
可以使用最快编码方式,命令和参数如下
./x264 rcq.yuv --input-res 640x480 -o rcq.flv --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
编码速率能有22fps,这样还凑合。生成的rcq.flv文件大小787K,压缩率在5%。
四、x264命令和参数(fullhelp)
1 Syntax: x264 [options] -o outfile infile 2 3 Infile can be raw (in which case resolution is required), 4 or YUV4MPEG (*.y4m), 5 or Avisynth if compiled with support (no). 6 or libav* formats if compiled with lavf support (no) or ffms support (no). 7 Outfile type is selected by filename: 8 .264 -> Raw bytestream 9 .mkv -> Matroska 10 .flv -> Flash Video 11 .mp4 -> MP4 if compiled with GPAC support (no) 12 Output bit depth: 8 (configured at compile time) 13 14 Options: 15 16 -h, --help List basic options 17 --longhelp List more options 18 --fullhelp List all options 19 20 Example usage: 21 22 Constant quality mode: 23 x264 --crf 24 -o <output> <input> 24 25 Two-pass with a bitrate of 1000kbps: 26 x264 --pass 1 --bitrate 1000 -o <output> <input> 27 x264 --pass 2 --bitrate 1000 -o <output> <input> 28 29 Lossless: 30 x264 --qp 0 -o <output> <input> 31 32 Maximum PSNR at the cost of speed and visual quality: 33 x264 --preset placebo --tune psnr -o <output> <input> 34 35 Constant bitrate at 1000kbps with a 2 second-buffer: 36 x264 --vbv-bufsize 2000 --bitrate 1000 -o <output> <input> 37 38 Presets: 39 40 --profile <string> Force the limits of an H.264 profile 41 Overrides all settings. 42 - baseline: 43 --no-8x8dct --bframes 0 --no-cabac 44 --cqm flat --weightp 0 45 No interlaced. 46 No lossless. 47 - main: 48 --no-8x8dct --cqm flat 49 No lossless. 50 - high: 51 No lossless. 52 - high10: 53 No lossless. 54 Support for bit depth 8-10. 55 - high422: 56 No lossless. 57 Support for bit depth 8-10. 58 Support for 4:2:0/4:2:2 chroma subsampling. 59 - high444: 60 Support for bit depth 8-10. 61 Support for 4:2:0/4:2:2/4:4:4 chroma subsampling. 62 --preset <string> Use a preset to select encoding settings [medium] 63 Overridden by user settings. 64 - ultrafast: 65 --no-8x8dct --aq-mode 0 --b-adapt 0 66 --bframes 0 --no-cabac --no-deblock 67 --no-mbtree --me dia --no-mixed-refs 68 --partitions none --rc-lookahead 0 --ref 1 69 --scenecut 0 --subme 0 --trellis 0 70 --no-weightb --weightp 0 71 - superfast: 72 --no-mbtree --me dia --no-mixed-refs 73 --partitions i8x8,i4x4 --rc-lookahead 0 74 --ref 1 --subme 1 --trellis 0 --weightp 1 75 - veryfast: 76 --no-mixed-refs --rc-lookahead 10 77 --ref 1 --subme 2 --trellis 0 --weightp 1 78 - faster: 79 --no-mixed-refs --rc-lookahead 20 80 --ref 2 --subme 4 --weightp 1 81 - fast: 82 --rc-lookahead 30 --ref 2 --subme 6 83 --weightp 1 84 - medium: 85 Default settings apply. 86 - slow: 87 --b-adapt 2 --direct auto --me umh 88 --rc-lookahead 50 --ref 5 --subme 8 89 - slower: 90 --b-adapt 2 --direct auto --me umh 91 --partitions all --rc-lookahead 60 92 --ref 8 --subme 9 --trellis 2 93 - veryslow: 94 --b-adapt 2 --bframes 8 --direct auto 95 --me umh --merange 24 --partitions all 96 --ref 16 --subme 10 --trellis 2 97 --rc-lookahead 60 98 - placebo: 99 --bframes 16 --b-adapt 2 --direct auto 100 --slow-firstpass --no-fast-pskip 101 --me tesa --merange 24 --partitions all 102 --rc-lookahead 60 --ref 16 --subme 11 103 --trellis 2 104 --tune <string> Tune the settings for a particular type of source 105 or situation 106 Overridden by user settings. 107 Multiple tunings are separated by commas. 108 Only one psy tuning can be used at a time. 109 - film (psy tuning): 110 --deblock -1:-1 --psy-rd <unset>:0.15 111 - animation (psy tuning): 112 --bframes {+2} --deblock 1:1 113 --psy-rd 0.4:<unset> --aq-strength 0.6 114 --ref {Double if >1 else 1} 115 - grain (psy tuning): 116 --aq-strength 0.5 --no-dct-decimate 117 --deadzone-inter 6 --deadzone-intra 6 118 --deblock -2:-2 --ipratio 1.1 119 --pbratio 1.1 --psy-rd <unset>:0.25 120 --qcomp 0.8 121 - stillimage (psy tuning): 122 --aq-strength 1.2 --deblock -3:-3 123 --psy-rd 2.0:0.7 124 - psnr (psy tuning): 125 --aq-mode 0 --no-psy 126 - ssim (psy tuning): 127 --aq-mode 2 --no-psy 128 \ - fastdecode: 129 --no-cabac --no-deblock --no-weightb 130 --weightp 0 131 - zerolatency: 132 --bframes 0 --force-cfr --no-mbtree 133 --sync-lookahead 0 --sliced-threads 134 --rc-lookahead 0 135 --slow-firstpass Don't force these faster settings with --pass 1: 136 --no-8x8dct --me dia --partitions none 137 --ref 1 --subme {2 if >2 else unchanged} 138 --trellis 0 --fast-pskip 139 140 Frame-type options: 141 142 -I, --keyint <integer or "infinite"> Maximum GOP size [250] 143 -i, --min-keyint <integer> Minimum GOP size [auto] 144 --no-scenecut Disable adaptive I-frame decision 145 --scenecut <integer> How aggressively to insert extra I-frames [40] 146 --intra-refresh Use Periodic Intra Refresh instead of IDR frames 147 -b, --bframes <integer> Number of B-frames between I and P [3] 148 --b-adapt <integer> Adaptive B-frame decision method [1] 149 Higher values may lower threading efficiency. 150 - 0: Disabled 151 - 1: Fast 152 - 2: Optimal (slow with high --bframes) 153 --b-bias <integer> Influences how often B-frames are used [0] 154 --b-pyramid <string> Keep some B-frames as references [normal] 155 - none: Disabled 156 - strict: Strictly hierarchical pyramid 157 - normal: Non-strict (not Blu-ray compatible) 158 --open-gop Use recovery points to close GOPs 159 Only available with b-frames 160 --no-cabac Disable CABAC 161 -r, --ref <integer> Number of reference frames [3] 162 --no-deblock Disable loop filter 163 -f, --deblock <alpha:beta> Loop filter parameters [0:0] 164 --slices <integer> Number of slices per frame; forces rectangular 165 slices and is overridden by other slicing options 166 --slice-max-size <integer> Limit the size of each slice in bytes 167 --slice-max-mbs <integer> Limit the size of each slice in macroblocks 168 --tff Enable interlaced mode (top field first) 169 --bff Enable interlaced mode (bottom field first) 170 --constrained-intra Enable constrained intra prediction. 171 --pulldown <string> Use soft pulldown to change frame rate 172 - none, 22, 32, 64, double, triple, euro (requires cfr input) 173 --fake-interlaced Flag stream as interlaced but encode progressive. 174 Makes it possible to encode 25p and 30p Blu-Ray 175 streams. Ignored in interlaced mode. 176 --frame-packing <integer> For stereoscopic videos define frame arrangement 177 - 0: checkerboard - pixels are alternatively from L and R 178 - 1: column alternation - L and R are interlaced by column 179 - 2: row alternation - L and R are interlaced by row 180 - 3: side by side - L is on the left, R on the right 181 - 4: top bottom - L is on top, R on bottom 182 - 5: frame alternation - one view per frame 183 184 Ratecontrol: 185 186 -q, --qp <integer> Force constant QP (0-69, 0=lossless) 187 -B, --bitrate <integer> Set bitrate (kbit/s) 188 --crf <float> Quality-based VBR (0-51) [23.0] 189 --rc-lookahead <integer> Number of frames for frametype lookahead [40] 190 --vbv-maxrate <integer> Max local bitrate (kbit/s) [0] 191 --vbv-bufsize <integer> Set size of the VBV buffer (kbit) [0] 192 --vbv-init <float> Initial VBV buffer occupancy [0.9] 193 --crf-max <float> With CRF+VBV, limit RF to this value 194 May cause VBV underflows! 195 --qpmin <integer> Set min QP [0] 196 --qpmax <integer> Set max QP [69] 197 --qpstep <integer> Set max QP step [4] 198 --ratetol <float> Tolerance of ABR ratecontrol and VBV [1.0] 199 --ipratio <float> QP factor between I and P [1.40] 200 --pbratio <float> QP factor between P and B [1.30] 201 --chroma-qp-offset <integer> QP difference between chroma and luma [0] 202 --aq-mode <integer> AQ method [1] 203 - 0: Disabled 204 - 1: Variance AQ (complexity mask) 205 - 2: Auto-variance AQ (experimental) 206 --aq-strength <float> Reduces blocking and blurring in flat and 207 textured areas. [1.0] 208 209 -p, --pass <integer> Enable multipass ratecontrol 210 - 1: First pass, creates stats file 211 - 2: Last pass, does not overwrite stats file 212 - 3: Nth pass, overwrites stats file 213 --stats <string> Filename for 2 pass stats ["x264_2pass.log"] 214 --no-mbtree Disable mb-tree ratecontrol. 215 --qcomp <float> QP curve compression [0.60] 216 --cplxblur <float> Reduce fluctuations in QP (before curve compression) [20.0] 217 --qblur <float> Reduce fluctuations in QP (after curve compression) [0.5] 218 --zones <zone0>/<zone1>/... Tweak the bitrate of regions of the video 219 Each zone is of the form 220 <start frame>,<end frame>,<option> 221 where <option> is either 222 q=<integer> (force QP) 223 or b=<float> (bitrate multiplier) 224 --qpfile <string> Force frametypes and QPs for some or all frames 225 Format of each line: framenumber frametype QP 226 QP is optional (none lets x264 choose). Frametypes: I,i,K,P,B,b. 227 K=<I or i> depending on open-gop setting 228 QPs are restricted by qpmin/qpmax. 229 230 Analysis: 231 232 -A, --partitions <string> Partitions to consider ["p8x8,b8x8,i8x8,i4x4"] 233 - p8x8, p4x4, b8x8, i8x8, i4x4 234 - none, all 235 (p4x4 requires p8x8. i8x8 requires --8x8dct.) 236 --direct <string> Direct MV prediction mode ["spatial"] 237 - none, spatial, temporal, auto 238 --no-weightb Disable weighted prediction for B-frames 239 --weightp <integer> Weighted prediction for P-frames [2] 240 - 0: Disabled 241 - 1: Weighted refs 242 - 2: Weighted refs + Duplicates 243 --me <string> Integer pixel motion estimation method ["hex"] 244 - dia: diamond search, radius 1 (fast) 245 - hex: hexagonal search, radius 2 246 - umh: uneven multi-hexagon search 247 - esa: exhaustive search 248 - tesa: hadamard exhaustive search (slow) 249 --merange <integer> Maximum motion vector search range [16] 250 --mvrange <integer> Maximum motion vector length [-1 (auto)] 251 --mvrange-thread <int> Minimum buffer between threads [-1 (auto)] 252 -m, --subme <integer> Subpixel motion estimation and mode decision [7] 253 - 0: fullpel only (not recommended) 254 - 1: SAD mode decision, one qpel iteration 255 - 2: SATD mode decision 256 - 3-5: Progressively more qpel 257 - 6: RD mode decision for I/P-frames 258 - 7: RD mode decision for all frames 259 - 8: RD refinement for I/P-frames 260 - 9: RD refinement for all frames 261 - 10: QP-RD - requires trellis=2, aq-mode>0 262 - 11: Full RD: disable all early terminations 263 --psy-rd <float:float> Strength of psychovisual optimization ["1.0:0.0"] 264 #1: RD (requires subme>=6) 265 #2: Trellis (requires trellis, experimental) 266 --no-psy Disable all visual optimizations that worsen 267 both PSNR and SSIM. 268 --no-mixed-refs Don't decide references on a per partition basis 269 --no-chroma-me Ignore chroma in motion estimation 270 --no-8x8dct Disable adaptive spatial transform size 271 -t, --trellis <integer> Trellis RD quantization. [1] 272 - 0: disabled 273 - 1: enabled only on the final encode of a MB 274 - 2: enabled on all mode decisions 275 --no-fast-pskip Disables early SKIP detection on P-frames 276 --no-dct-decimate Disables coefficient thresholding on P-frames 277 --nr <integer> Noise reduction [0] 278 279 --deadzone-inter <int> Set the size of the inter luma quantization deadzone [21] 280 --deadzone-intra <int> Set the size of the intra luma quantization deadzone [11] 281 Deadzones should be in the range 0 - 32. 282 --cqm <string> Preset quant matrices ["flat"] 283 - jvt, flat 284 --cqmfile <string> Read custom quant matrices from a JM-compatible file 285 Overrides any other --cqm* options. 286 --cqm4 <list> Set all 4x4 quant matrices 287 Takes a comma-separated list of 16 integers. 288 --cqm8 <list> Set all 8x8 quant matrices 289 Takes a comma-separated list of 64 integers. 290 --cqm4i, --cqm4p, --cqm8i, --cqm8p <list> 291 Set both luma and chroma quant matrices 292 --cqm4iy, --cqm4ic, --cqm4py, --cqm4pc <list> 293 Set individual quant matrices 294 295 Video Usability Info (Annex E): 296 The VUI settings are not used by the encoder but are merely suggestions to 297 the playback equipment. See doc/vui.txt for details. Use at your own risk. 298 299 --overscan <string> Specify crop overscan setting ["undef"] 300 - undef, show, crop 301 --videoformat <string> Specify video format ["undef"] 302 - component, pal, ntsc, secam, mac, undef 303 --range <string> Specify color range ["auto"] 304 - auto, tv, pc 305 --colorprim <string> Specify color primaries ["undef"] 306 - undef, bt709, bt470m, bt470bg 307 smpte170m, smpte240m, film 308 --transfer <string> Specify transfer characteristics ["undef"] 309 - undef, bt709, bt470m, bt470bg, linear, 310 log100, log316, smpte170m, smpte240m 311 --colormatrix <string> Specify color matrix setting ["???"] 312 - undef, bt709, fcc, bt470bg 313 smpte170m, smpte240m, GBR, YCgCo 314 --chromaloc <integer> Specify chroma sample location (0 to 5) [0] 315 --nal-hrd <string> Signal HRD information (requires vbv-bufsize) 316 - none, vbr, cbr (cbr not allowed in .mp4) 317 --pic-struct Force pic_struct in Picture Timing SEI 318 --crop-rect <string> Add 'left,top,right,bottom' to the bitstream-level 319 cropping rectangle 320 321 Input/Output: 322 323 -o, --output <string> Specify output file 324 --muxer <string> Specify output container format ["auto"] 325 - auto, raw, mkv, flv 326 --demuxer <string> Specify input container format ["auto"] 327 - auto, raw, y4m 328 --input-fmt <string> Specify input file format (requires lavf support) 329 --input-csp <string> Specify input colorspace format for raw input 330 - valid csps for `raw' demuxer: 331 i420, yv12, nv12, i422, yv16, nv16, i444, yv24, bgr, bgra, rgb 332 --output-csp <string> Specify output colorspace ["i420"] 333 - i420, i422, i444, rgb 334 --input-depth <integer> Specify input bit depth for raw input 335 --input-range <string> Specify input color range ["auto"] 336 - auto, tv, pc 337 --input-res <intxint> Specify input resolution (width x height) 338 --index <string> Filename for input index file 339 --sar width:height Specify Sample Aspect Ratio 340 --fps <float|rational> Specify framerate 341 --seek <integer> First frame to encode 342 --frames <integer> Maximum number of frames to encode 343 --level <string> Specify level (as defined by Annex A) 344 --bluray-compat Enable compatibility hacks for Blu-ray support 345 346 -v, --verbose Print stats for each frame 347 --no-progress Don't show the progress indicator while encoding 348 --quiet Quiet Mode 349 --log-level <string> Specify the maximum level of logging ["info"] 350 - none, error, warning, info, debug 351 --psnr Enable PSNR computation 352 --ssim Enable SSIM computation 353 --threads <integer> Force a specific number of threads 354 --lookahead-threads <integer> Force a specific number of lookahead threads 355 --sliced-threads Low-latency but lower-efficiency threading 356 --thread-input Run Avisynth in its own thread 357 --sync-lookahead <integer> Number of buffer frames for threaded lookahead 358 --non-deterministic Slightly improve quality of SMP, at the cost of repeatability 359 --cpu-independent Ensure exact reproducibility across different cpus, 360 as opposed to letting them select different algorithms 361 --asm <integer> Override CPU detection 362 --no-asm Disable all CPU optimizations 363 --visualize Show MB types overlayed on the encoded video 364 --dump-yuv <string> Save reconstructed frames 365 --sps-id <integer> Set SPS and PPS id numbers [0] 366 --aud Use access unit delimiters 367 --force-cfr Force constant framerate timestamp generation 368 --tcfile-in <string> Force timestamp generation with timecode file 369 --tcfile-out <string> Output timecode v2 file from input timestamps 370 --timebase <int/int> Specify timebase numerator and denominator 371 <integer> Specify timebase numerator for input timecode file 372 or specify timebase denominator for other input 373 --dts-compress Eliminate initial delay with container DTS hack 374 375 Filtering: 376 377 --vf, --video-filter <filter0>/<filter1>/... Apply video filtering to the input file 378 379 Filter options may be specified in <filter>:<option>=<value> format. 380 381 Available filters: 382 crop:left,top,right,bottom 383 removes pixels from the edges of the frame 384 select_every:step,offset1[,...] 385 apply a selection pattern to input frames 386 step: the number of frames in the pattern 387 offsets: the offset into the step to select a frame 388 see: http://avisynth.org/mediawiki/Select#SelectEvery
五、关于YUV420的格式说明
YUV格式有两大类:planar和packed。
对于planar的YUV格式,先连续存储所有像素点的Y,紧接着存储所有像素点的U,随后是所有像素点的V。
对于packed的YUV格式,每个像素点的Y,U,V是连续交*存储的。
一般的录像程序是先从摄像头得到yuv420 planar的数据,然后编码成h264格式 帧,最后存储成3gp/mpeg等格式的视频文件。
以720×488大小图象YUV420 planar为例,其存储格式是:
共大小为(720×480×3>>1)字节,分为三个部分:Y,U和V
Y分量: (720×480)个字节
U(Cb)分量:(720×480>>2)个字节
V(Cr)分量:(720×480>>2)个字节
三个部分内部均是行优先存储,三个部分之间是Y,U,V 顺序存储。
即YUV数据的0--720×480字节是Y分量值,
720×480--720×480×5/4字节是U分量
720×480×5/4 --720×480×3/2字节是V分量。
一副w*h的yuv420图像数据的大小为w*h*1.5。
:2: 2 和4:2:0 转换:
最简单的方式:
YUV4:2:2 ---> YUV4:2:0 Y不变,将U和V信号值在行(垂直方向)在进行一次隔行抽样。
YUV4:2:0 ---> YUV4:2:2 Y不变,将U和V信号值的每一行分别拷贝一份形成连续两行数据。
六、测试视频
0、x264-snapshots:http://download.videolan.org/pub/videolan/x264/snapshots/
1、zed可执行文件:https://files.cnblogs.com/files/liusiluandzhangkun/x264.rar
2、测试yuv文件:https://files.cnblogs.com/files/liusiluandzhangkun/rcq.part1.rar https://files.cnblogs.com/files/liusiluandzhangkun/rcq.part2.rar
3、其他通用测试yuv序列:http://www.cipr.rpi.edu/resource/sequences/sif.html