himix200 opencv ffmpeg


原文链接: himix200 opencv ffmpeg

红超的吾记之谈: 移植 opencv + ffmpeg + x264 到 Hi3519AV100/Hi3516DV300

toolchains/himix200.toolchain.cmake

# set cross-compiled system type, it's better not use the type which cmake cannot recognized.
SET ( CMAKE_SYSTEM_NAME Linux )
SET ( CMAKE_SYSTEM_PROCESSOR arm )
# when hislicon SDK was installed, toolchain was installed in the path as below: 
SET ( CMAKE_C_COMPILER /opt/hisi-linux/x86-arm/arm-himix200-linux/bin/arm-himix200-linux-gcc )
SET ( CMAKE_CXX_COMPILER /opt/hisi-linux/x86-arm/arm-himix200-linux/bin/arm-himix200-linux-g++ )

# set searching rules for cross-compiler
SET ( CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER )
SET ( CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY )
SET ( CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY )

# set ${CMAKE_C_FLAGS} and ${CMAKE_CXX_FLAGS}flag for cross-compiled process
SET ( CROSS_COMPILATION_ARM himix200 )
SET ( CROSS_COMPILATION_ARCHITECTURE armv7-a )

# set g++ param
SET ( CMAKE_CXX_FLAGS "-std=c++11 -march=armv7-a -mfloat-abi=softfp -mfpu=neon-vfpv4 -fopenmp ${CMAKE_CXX_FLAGS}" )

add_definitions(-D__ARM_NEON)
add_definitions(-D__ANDROID__)

移植 opencv + ffmpeg + x264 到 Hi3519AV100/Hi3516DV300

1.源码下载:
下载x264
git clone git://git.videolan.org/x264.git
下载ffmpeg
git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg
安装 cmake :

sudo apt-get install cmake
下载 OpenCV 源码:
https://github.com/opencv/opencv/archive/4.1.0.zip
2.编译安装:

x264编译
创建静态库
CC=arm-himix200-linux-gcc \
./configure \
--prefix=/home/nsc/workspace/himix200_x264_install \
--host=arm-himix200-linux \
--cross-prefix=arm-himix200-linux- \
--enable-static \
--disable-opencl
创建动态库
CC=arm-himix200-linux-gcc \
./configure \
--prefix=/home/nsc/workspace/himix200_x264_install \
--host=arm-himix200-linux \
--cross-prefix=arm-himix200-linux- \
--enable-shared

make
make install

ffmpeg 编译安装
export PKG_CONFIG_PATH=/home/nsc/workspace/himix200_x264_install/lib/pkgconfig:$PKG_CONFIG_PATH
创建静态库
CC=arm-himix200-linux-gcc \
./configure \
--prefix=/home/nsc/workspace/himix200_ffmpeg_install \
--cross-prefix=arm-himix200-linux- \
--arch=arm \
--target-os=linux \
--host-os=linux \
--enable-static \
--enable-filters \
--enable-asm \
--enable-ffmpeg \
--enable-ffplay \
--enable-ffprobe \
--enable-doc \
--enable-cross-compile \
--enable-gpl \
--enable-version3 \
--enable-small \
--enable-encoder=rawvideo \
--enable-encoder=mpeg2video \
--enable-encoder=mp2 \
--enable-encoder=libx264 \
--enable-libx264 \
--enable-muxer=adts \
--enable-muxer=mpegts \
--enable-muxer=mpeg2video \
--enable-muxer=rtsp \
--enable-muxer=hls \
--enable-muxer=h264 \
--enable-muxer=rtp \
--enable-muxer=avi \
--enable-muxer=flv \
--enable-muxer=mp4 \
--enable-demuxer=mpegts \
--enable-demuxer=rtsp \
--enable-demuxer=hls \
--enable-demuxer=h264 \
--enable-demuxer=rtp \
--enable-demuxer=flv \
--enable-muxer=mp4 \
--enable-demuxer=mpegts \
--enable-demuxer=rtsp \
--enable-demuxer=hls \
--enable-demuxer=h264 \
--enable-demuxer=rtp \
--enable-demuxer=flv \
--enable-demuxer=avi \
--enable-demuxer=mpegps \
--enable-demuxer=mpegtsraw \
--enable-demuxer=mpegvideo \
--enable-parser=aac \
--enable-parser=h264 \
--enable-parser=mpeg4video \
--enable-parser=mpegvideo \
--enable-parser=mpegaudio \
--enable-bsf=h264_mp4toannexb \
--enable-protocol=http \
--enable-protocol=rtp \
--enable-protocol=rtmpts \
--enable-protocol=https \
--enable-protocol=hls \
--enable-protocol=file \
--enable-protocol=data \
--enable-gray \
--enable-safe-bitstream-reader \
--extra-cflags=-I/home/nsc/workspace/himix200_x264_install/include \
--extra-ldflags=-L/home/nsc/workspace/himix200_x264_install/lib

创建动态库
CC=arm-himix200-linux-gcc \
./configure \
--prefix=/home/nsc/workspace/himix200_ffmpeg_install \
--cross-prefix=arm-himix200-linux- \
--arch=arm \
--target-os=linux \
--host-os=linux \
--enable-shared \
--enable-filters \
--enable-asm \
--enable-ffmpeg \
--enable-ffplay \
--enable-ffprobe \
--enable-doc \
--enable-cross-compile \
--enable-gpl \
--enable-version3 \
--enable-small \
--enable-encoder=rawvideo \
--enable-encoder=mpeg2video \
--enable-encoder=mp2 \
--enable-encoder=libx264 \
--enable-libx264 \
--enable-muxer=adts \
--enable-muxer=mpegts \
--enable-muxer=mpeg2video \
--enable-muxer=rtsp \
--enable-muxer=hls \
--enable-muxer=h264 \
--enable-muxer=rtp \
--enable-muxer=avi \
--enable-muxer=flv \
--enable-muxer=mp4 \
--enable-demuxer=mpegts \
--enable-demuxer=rtsp \
--enable-demuxer=hls \
--enable-demuxer=h264 \
--enable-demuxer=rtp \
--enable-demuxer=flv \
--enable-muxer=mp4 \
--enable-demuxer=mpegts \
--enable-demuxer=rtsp \
--enable-demuxer=hls \
--enable-demuxer=h264 \
--enable-demuxer=rtp \
--enable-demuxer=flv \
--enable-demuxer=avi \
--enable-demuxer=mpegps \
--enable-demuxer=mpegtsraw \
--enable-demuxer=mpegvideo \
--enable-parser=aac \
--enable-parser=h264 \
--enable-parser=mpeg4video \
--enable-parser=mpegvideo \
--enable-parser=mpegaudio \
--enable-bsf=h264_mp4toannexb \
--enable-protocol=http \
--enable-protocol=rtp \
--enable-protocol=rtmpts \
--enable-protocol=https \
--enable-protocol=hls \
--enable-protocol=file \
--enable-protocol=data \
--enable-gray \
--enable-safe-bitstream-reader \
--extra-cflags=-I/home/nsc/workspace/himix200_x264_install/include \
--extra-ldflags=-L/home/nsc/workspace/himix200_x264_install/lib

make
make install

opencv 编译安装

解压缩后,
进去到 opencv-4.1.0 目录下,
创建 build 文件夹,
根据需要配置编译选项如下:

export PKG_CONFIG_PATH=/home/nsc/workspace/himix200_ffmpeg_install/lib/pkgconfig:$PKG_CONFIG_PATH

创建静态库
cmake \
-D CMAKE_BUILD_TYPE=Release \
-D CMAKE_INSTALL_PREFIX=../himix200_opencv_410 \
-D CMAKE_C_COMPILER=arm-himix200-linux-gcc \
-D CMAKE_CXX_COMPILER=arm-himix200-linux-g++ \
-D BUILD_SHARED_LIBS=OFF \
-D WITH_FFMPEG=ON \
-D WITH_1394=OFF \
-D WITH_ARAVIS=OFF \
-D WITH_ARITH_DEC=ON \
-D WITH_ARITH_ENC=ON \
-D WITH_CLP=OFF \
-D WITH_CUBLAS=OFF \
-D WITH_CUDA=OFF \
-D WITH_CUFFT=OFF \
-D WITH_GSTREAMER=ON \
-D WITH_GSTREAMER_0_10=OFF \
-D WITH_HALIDE=OFF \
-D WITH_HPX=OFF \
-D WITH_IMGCODEC_HDR=ON \
-D WITH_IMGCODEC_PXM=ON \
-D WITH_IMGCODEC_SUNRASTER=ON \
-D WITH_INF_ENGINE=OFF \
-D WITH_IPP=OFF \
-D WITH_ITT=OFF \
-D WITH_JASPER=ON \
-D WITH_JPEG=ON \
-D WITH_LAPACK=ON \
-D WITH_LIBREALSENSE=OFF \
-D WITH_NVCUVID=OFF \
-D WITH_OPENCL=OFF \
-D WITH_OPENCLAMDBLAS=OFF \
-D WITH_OPENCLAMDFFT=OFF \
-D WITH_OPENCL_SVM=OFF \
-D WITH_OPENEXR=OFF \
-D WITH_OPENGL=OFF \
-D WITH_OPENMP=OFF \
-D WITH_OPENNNI=OFF \
-D WITH_OPENNNI2=OFF \
-D WITH_OPENVX=OFF \
-D WITH_PNG=OFF \
-D WITH_PROTOBUF=OFF \
-D WITH_PTHREADS_PF=ON \
-D WITH_PVAPI=OFF \
-D WITH_QT=OFF \
-D WITH_QUIRC=OFF \
-D WITH_TBB=OFF \
-D WITH_TIFF=ON \
-D WITH_VULKAN=OFF \
-D WITH_WEBP=ON \
-D WITH_XIMEA=OFF \
..

创建动态库
cmake \
-D CMAKE_BUILD_TYPE=Release \
-D CMAKE_INSTALL_PREFIX=../himix200_opencv_410 \
-D CMAKE_C_COMPILER=arm-himix200-linux-gcc \
-D CMAKE_CXX_COMPILER=arm-himix200-linux-g++ \
-D BUILD_SHARED_LIBS=ON \
-D CMAKE_C_FLAGS=-fPIC \
-D CMAKE_CXX_FLAGS=-fPIC \
-D CMAKE_EXE_LINKER_FLAGS=-lpthread -ldl \
-D ENABLE_PIC=ON \
-D WITH_FFMPEG=ON \
-D WITH_1394=OFF \
-D WITH_ARAVIS=OFF \
-D WITH_ARITH_DEC=ON \
-D WITH_ARITH_ENC=ON \
-D WITH_CLP=OFF \
-D WITH_CUBLAS=OFF \
-D WITH_CUDA=OFF \
-D WITH_CUFFT=OFF \
-D WITH_GSTREAMER=ON \
-D WITH_GSTREAMER_0_10=OFF \
-D WITH_HALIDE=OFF \
-D WITH_HPX=OFF \
-D WITH_IMGCODEC_HDR=ON \
-D WITH_IMGCODEC_PXM=ON \
-D WITH_IMGCODEC_SUNRASTER=ON \
-D WITH_INF_ENGINE=OFF \
-D WITH_IPP=OFF \
-D WITH_ITT=OFF \
-D WITH_JASPER=ON \
-D WITH_JPEG=ON \
-D WITH_LAPACK=ON \
-D WITH_LIBREALSENSE=OFF \
-D WITH_NVCUVID=OFF \
-D WITH_OPENCL=OFF \
-D WITH_OPENCLAMDBLAS=OFF \
-D WITH_OPENCLAMDFFT=OFF \
-D WITH_OPENCL_SVM=OFF \
-D WITH_OPENEXR=OFF \
-D WITH_OPENGL=OFF \
-D WITH_OPENMP=OFF \
-D WITH_OPENNNI=OFF \
-D WITH_OPENNNI2=OFF \
-D WITH_OPENVX=OFF \
-D WITH_PNG=OFF \
-D WITH_PROTOBUF=OFF \
-D WITH_PTHREADS_PF=ON \
-D WITH_PVAPI=OFF \
-D WITH_QT=OFF \
-D WITH_QUIRC=OFF \
-D WITH_TBB=OFF \
-D WITH_TIFF=ON \
-D WITH_VULKAN=OFF \
-D WITH_WEBP=ON \
-D WITH_XIMEA=OFF \
..

注意:最后有两个点,表示执行上级目录中的 CMakeLists.txt 文件!

说明:
CMAKE_BUILD_TYPE :指定基于 make 的产生器的构建类型(Debug, Release)
CMAKE_INSTALL_PREFIX :安装路径的前缀
CMAKE_C_COMPILER :指定 C 编译器
CMAKE_CXX_COMPILER :指定 C++ 编译器
BUILD_SHARED_LIBS :ON - 创建动态库,OFF - 创建静态库
CMAKE_C_FLAGS :设置编译 C 文件时的选项,Linux下编译动态库时,必须加上 -fPIC 参数,否则在链接时会有错误提示
CMAKE_CXX_FLAGS :设置编译 C++ 文件时的选项
CMAKE_EXE_LINKER_FLAGS :设定链接选项

$ make
$ make install

3.常见问题:

问题一:
libpng/pngstruct.h:30:18: fatal error: zlib.h: No such file or directory

方法一:

$ sudo apt-get install zlib1g-dev

重新编译

方法二:(推荐)

这个版本的OpenCV已经包含了 zlib 第三方的库!

在 CMakeLists.txt 文件中查找 OPENCV_CONFIG_FILE_INCLUDE_DIR 关键字,
并在如下这句后

ocv_include_directories(${OPENCV_CONFIG_FILE_INCLUDE_DIR})

添加如下这句

ocv_include_directories(./3rdparty/zlib/)

问题二:

opencv-4.1.0/modules/core/src/parallel.cpp:267:14:
error: ‘exception_ptr’ in namespace ‘std’ does not name a type std::exception_ptr pException;

在 opencv-4.1.0/modules/core/src/parallel.cpp 中查找 CV__EXCEPTION_PTR 关键字,

添加如下这句

undef CV__EXCEPTION_PTR // Add this line, disable the EXCEPTION_PTR

在如下内容之上

ifndef CVEXCEPTION_PTR
define CV
EXCEPTION_PTR 0
elif CV__EXCEPTION_PTR
include // std::exception_ptr
endif

问题三:

WARNING: Can't build ffmpeg test code

1.自测 ffmpeg_test.cpp 是否可以编译通过
cd /home/nsc/workspace/opencv-4.1.0/cmake/checks

arm-himix200-linux-gcc ffmpeg_test.cpp -o ffmpeg_test \
-I /home/nsc/workspace/himix200_ffmpeg_install/include \
-I /home/nsc/workspace/himix200_x264_install/include \
-L /home/nsc/workspace/himix200_ffmpeg_install/lib \
-L /home/nsc/workspace/himix200_x264_install/lib \
-lavformat -lavcodec -lavdevice -lavfilter -lavutil -lpostproc -lswresample -lswscale \
-lx264 \
-lm -lpthread


  1. 方法一:编译时找到 x264 库
    将 /home/nsc/workspace/himix200_x264_install/include 目录下的
    x264.h
    x264_config.h
    拷贝到 /home/nsc/workspace/himix200_ffmpeg_install/include 目录下

将 /home/nsc/workspace/himix200_x264_install/lib 目录下的 libx264.a 拷贝到
/home/nsc/workspace/himix200_ffmpeg_install/lib 目录下

方法二:修改 detect_ffmpeg.cmake 文件,在执行 make 时便知问题在哪里了
if(HAVE_FFMPEG AND NOT HAVE_FFMPEG_WRAPPER AND NOT OPENCV_FFMPEG_SKIP_BUILD_CHECK)
try_compile(__VALID_FFMPEG

  "${OpenCV_BINARY_DIR}"  
  "${OpenCV_SOURCE_DIR}/cmake/checks/ffmpeg_test.cpp"  
  CMAKE_FLAGS "-DINCLUDE_DIRECTORIES:STRING=${FFMPEG_INCLUDE_DIRS}"  
              "-DLINK_LIBRARIES:STRING=${FFMPEG_LIBRARIES}"  
  OUTPUT_VARIABLE TRY_OUT  

)
if(NOT __VALID_FFMPEG)

 message(FATAL_ERROR "FFMPEG: test check build log:\n${TRY_OUT}")  
message(STATUS "WARNING: Can't build ffmpeg test code")  
set(HAVE_FFMPEG FALSE)  

endif()

注释掉上面的判断检测,直接返回TRUE  

set(HAVE_FFMPEG TRUE)
endif()

`