超群天晴

【原创】基于ZedBoard的Webcam设计(五):x264编码在zedboard上的实现(软编码)

0
阅读(12930)

(原创)基于ZedBoard的Webcam设计(四):MJPG编码和AVI封装中,我们在zed上完成了视频的采集和存储,但是MJPG只是对一帧一帧图片进行编码,编码效率只有10%。x264-snapshot提供了开源x264源代码,已经广泛用于嵌入式解决方案中。本小节将介绍zed上x264-snapshot源码的配置,并在zed上使用High版本、main版本对一个33M的测试视频进行编码,测试编码效率。

更多更新请关注我的博客:@超群天晴http://www.cnblogs.com/surpassal/

相关阅读:

(原创)基于ZedBoard的Webcam设计(一):USB摄像头(V4L2接口)的图片采集

(原创)基于ZedBoard的Webcam设计(二):USB摄像头图片采集+QT显示

(原创)基于ZedBoard的Webcam设计(三):视频的采集和动态显示

(原创)基于ZedBoard的Webcam设计(四):MJPG编码和AVI封装

(原创)基于ZedBoard的Webcam设计(五):x264编码在zedboard上的实现(软编码)

硬件平台:Digilent ZedBoard

开发环境:Windows XP 32 bit + Wmare 8.0 + Ubuntu 10.04 +Qt+ arm-linux-xilinx-gnueabi交叉编译环境

Zedboard linux:Digilent OOB Design

一、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=.
2prefix=/opt/rcq
3exec_prefix=${prefix}
4bindir=${exec_prefix}/bin
5libdir=${exec_prefix}/lib
6includedir=${prefix}/include
7ARCH=ARM
8SYS=LINUX
9CC=arm-xilinx-linux-gnueabi-gcc
10CFLAGS=-Wshadow -O3 -fno-fast-math -Wall -I. -I$(SRCPATH) -std=gnu99 -fPIC -fomit-frame-pointer -fno-tree-vectorize
11DEPMM=-MM -g0
12DEPMT=-MT
13LD=arm-xilinx-linux-gnueabi-gcc-o
14LDFLAGS= -lm -lpthread
15LIBX264=libx264.a
16AR=arm-xilinx-linux-gnueabi-arrc
17RANLIB=arm-xilinx-linux-gnueabi-ranlib
18STRIP=arm-xilinx-linux-gnueabi-strip
19AS=
20ASFLAGS= -DHAVE_ALIGNED_STACK=1-DPIC -DHIGH_BIT_DEPTH=0-DBIT_DEPTH=8
21RC=
22RCFLAGS=
23EXE=
24HAVE_GETOPT_LONG=1
25DEVNULL=/dev/null
26PROF_GEN_CC=-fprofile-generate
27PROF_GEN_LD=-fprofile-generate
28PROF_USE_CC=-fprofile-use
29PROF_USE_LD=-fprofile-use
30default: cli
31install: install-cli
32SOSUFFIX=so
33SONAME=libx264.so.129
34SOFLAGS=-shared -Wl,-soname,$(SONAME) -Wl,-Bsymbolic
35default: lib-shared
36install: install-lib-shared
37LDFLAGSCLI =
38CLI_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-mode0--b-adapt0--bframes0--no-cabac --no-deblock --no-mbtree --me dia --no-mixed-refs --partitions none --rc-lookahead0--ref1--scenecut0--subme0--trellis0


编码速率能有22fps,这样还凑合。生成的rcq.flv文件大小787K,压缩率在5%。

四、x264命令和参数(fullhelp)


Syntax: x264 [options] - o outfile infile

Infile can be raw (
inwhich caseresolution is required),
or YUV4MPEG (
* .y4m),
or Avisynth
if compiled with support (no).
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 support (no)
Output bit depth:
8 (configured at compile time)

Options:

-h, -- help List basic options
-- longhelp List more options
-- fullhelp List all options

Example usage:

Constant quality mode:
x264
--crf 24-o

Two
- pass with a bitrate of 1000kbps:
x264
--pass 1--bitrate 1000-o
x264
--pass 2--bitrate 1000-o

Lossless:
x264
--qp 0-o

Maximum PSNR at the cost of speed and visual quality:
x264
--preset placebo --tune psnr -o

Constant bitrate at 1000kbps with a
2second- buffer:
x264
--vbv-bufsize 2000--bitrate 1000-o

Presets:

--profile < string> Force the limits of an H. 264 profile
Overrides all settings.
- baseline:
--no-8x8dct --bframes 0--no- cabac
--cqm flat --weightp 0
No interlaced.
No lossless.
- main:
--no-8x8dct -- cqm flat
No lossless.
- high:
No lossless.
- high10:
No lossless.
Support
forbit depth 8- 10 .
- high422:
No lossless.
Support
forbit depth 8- 10 .
Support
for 4: 2: 0/ 4: 2: 2 chroma subsampling.
- high444:
Support
forbit depth 8- 10 .
Support
for 4: 2: 0/ 4: 2: 2/ 4: 4: 4 chroma subsampling.
--preset < string> Use a preset to select encoding settings [medium]
Overridden by user settings.
- ultrafast:
--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
--no-weightb --weightp 0
- superfast:
--no-mbtree --me dia --no-mixed- refs
--partitions i8x8,i4x4 --rc-lookahead 0
-- ref 1--subme 1--trellis 0--weightp 1
- veryfast:
--no-mixed-refs --rc-lookahead 10
-- ref 1--subme 2--trellis 0--weightp 1
- faster:
--no-mixed-refs --rc-lookahead 20
-- ref 2--subme 4--weightp 1
- fast:
--rc-lookahead 30-- ref 2--subme 6
--weightp 1
- medium:
Default settings apply.
- slow:
--b-adapt 2--direct auto -- me umh
--rc-lookahead 50-- ref 5--subme 8
- slower:
--b-adapt 2--direct auto -- me umh
--partitions all --rc-lookahead 60
-- ref 8--subme 9--trellis 2
- veryslow:
--b-adapt 2--bframes 8-- direct auto
--me umh --merange 24-- partitions all
-- ref 16--subme 10--trellis 2
--rc-lookahead 60
- placebo:
--bframes 16--b-adapt 2-- direct auto
--slow-firstpass --no-fast- pskip
--me tesa --merange 24-- partitions all
--rc-lookahead 60-- ref 16--subme 11
--trellis 2
--tune < string> Tune the settings for a particular type of source
or situation
Overridden by user settings.
Multiple tunings are separated by commas.
Only one psy tuning can be used at a time.
- film (psy tuning):
--deblock - 1:- 1--psy-rd : 0.15
- animation (psy tuning):
--bframes {+ 2} --deblock 1: 1
--psy-rd 0.4: --aq-strength 0.6
-- ref{Double if> 1 else 1 }
- grain (psy tuning):
--aq-strength 0.5--no-dct- decimate
--deadzone-inter 6--deadzone-intra 6
--deblock - 2:- 2--ipratio 1.1
--pbratio 1.1--psy-rd : 0.25
--qcomp 0.8
- stillimage (psy tuning):
--aq-strength 1.2--deblock - 3:- 3
--psy-rd 2.0: 0.7
- psnr (psy tuning):
--aq-mode 0--no- psy
- ssim (psy tuning):
--aq-mode 2--no- psy
\
- fastdecode:
--no-cabac --no-deblock --no- weightb
--weightp 0
- zerolatency:
--bframes 0--force-cfr --no- mbtree
--sync-lookahead 0--sliced- threads
--rc-lookahead 0
--slow-firstpass Don ' t force these faster settings with --pass 1:
--no-8x8dct --me dia -- partitions none
-- ref 1--subme { 2 if> 2 else unchanged}
--trellis 0--fast- pskip

Frame
- type options:

-I, --keyint " infinite "> Maximum GOP size [ 250 ]
-i, --min-keyint Minimum GOP size [auto]
--no-scenecut Disable adaptive I- frame decision
--scenecut How aggressively to insert extra I-frames [ 40 ]
--intra- refresh Use Periodic Intra Refresh instead of IDR frames
-b, --bframes Number of B-frames between I and P [ 3 ]
--b-adapt Adaptive B-frame decision method [ 1 ]
Higher values may lower threading efficiency.
- 0 : Disabled
- 1 : Fast
- 2: Optimal (slow with high -- bframes)
--b-bias Influences how often B-frames are used [ 0 ]
--b-pyramid < string> Keep some B-frames as references [normal]
- none: Disabled
- strict: Strictly hierarchical pyramid
- normal: Non-strict (not Blu- ray compatible)
--open- gop Use recovery points to close GOPs
Only available with b
- frames
--no- cabac Disable CABAC
-r, -- ref Number of reference frames [ 3 ]
--no- deblock Disable loop filter
-f, --deblock Loop filter parameters [ 0: 0 ]
--slices Number of slices per frame; forces rectangular
slices and
is overridden by other slicing options
--slice-max-size Limit the size of each slice in bytes
--slice-max-mbs Limit the size of each slice in macroblocks
-- tff Enable interlaced mode (top field first)
-- bff Enable interlaced mode (bottom field first)
--constrained- intra Enable constrained intra prediction.
--pulldown < string> Use soft pulldown to change frame rate
- none, 22, 32, 64, double , triple, euro (requires cfr input)
--fake-interlaced Flag stream as interlaced but encode progressive.
Makes it possible to encode 25p and 30p Blu
- Ray
streams. Ignored
in interlaced mode.
--frame-packing For stereoscopic videos define frame arrangement
- 0: checkerboard - pixels are alternatively from L and R
- 1: column alternation - L and R are interlaced by column
- 2: row alternation - L and R are interlaced by row
- 3: side by side - L is on the left, R on the right
- 4: top bottom - L is on top, R on bottom
- 5: frame alternation - one view per frame

Ratecontrol:

-q, --qp Force constant QP ( 0- 69, 0= lossless)
-B, --bitrate Set bitrate (kbit/ s)
--crf < float> Quality-based VBR ( 0- 51) [ 23.0 ]
--rc-lookahead Number of frames forframetype lookahead [ 40 ]
--vbv-maxrate Max local bitrate (kbit/s) [ 0 ]
--vbv-bufsize Set size of the VBV buffer (kbit) [ 0 ]
--vbv-init < float> Initial VBV buffer occupancy [ 0.9 ]
--crf-max < float> With CRF+VBV, limit RF to this value
May cause VBV underflows
!
--qpmin Set min QP [ 0 ]
--qpmax Set max QP [ 69 ]
--qpstep Set max QP step [ 4 ]
--ratetol < float> Tolerance of ABR ratecontrol and VBV [ 1.0 ]
--ipratio < float> QP factor between I and P [ 1.40 ]
--pbratio < float> QP factor between P and B [ 1.30 ]
--chroma-qp-offset QP difference between chroma and luma [ 0 ]
--aq-mode AQ method [ 1 ]
- 0 : Disabled
- 1 : Variance AQ (complexity mask)
- 2: Auto- variance AQ (experimental)
--aq-strength < float> Reduces blocking and blurring in flat and
textured areas. [
1.0 ]

-p, --pass Enable multipass ratecontrol
- 1 : First pass, creates stats file
- 2 : Last pass, does not overwrite stats file
- 3 : Nth pass, overwrites stats file
--stats < string> Filename for 2pass stats [ " x264_2pass.log " ]
--no-mbtree Disable mb- tree ratecontrol.
--qcomp < float> QP curve compression [ 0.60 ]
--cplxblur < float> Reduce fluctuations inQP (before curve compression) [ 20.0 ]
--qblur < float> Reduce fluctuations inQP (after curve compression) [ 0.5 ]
--zones // ... Tweak the bitrate of regions of the video
Each zone
is of the form
,,

五、关于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.5422和4:20转换: 最简单的方式: YUV4:2:2---> YUV4:2:0Y不变,将U和V信号值在行(垂直方向)在进行一次隔行抽样。 YUV4:2:0---> YUV4:2:2Y不变,将U和V信号值的每一行分别拷贝一份形成连续两行数据。

复制代码

六、测试视频

=====================================================

0、x264-snapshots:http://download.videolan.org/pub/videolan/x264/snapshots/

1、zed可执行文件:x264.rar

2、测试yuv文件:rcq.part1.rarrcq.part2.rar

3、其他通用测试yuv序列:http://www.cipr.rpi.edu/resource/sequences/sif.html

Baidu
map