香橙派--安装RKMPP、x264、libdrm、FFmpeg(支持rkmpp)以及opencv(支持带rkmpp的ffmpeg)(适用于RK3588平台)
1. 安装RKMPP
git clone https://github.com/rockchip-linux/mpp
cd mpp/build/linux/aarch64
./make-Makefiles.bash
make -j8
sudo make install
RKMPP:用于编解码测试,支持RK3588平台。
2. 安装x264
git clone https://code.videolan.org/videolan/x264.git
cd x264
./configure --prefix=/usr/local/x264 --disable-opencl --enable-static --enable-shared
make -j8
sudo make install
配置环境变量:
echo 'export PKG_CONFIG_PATH="/usr/local/x264/lib/pkgconfig:$PKG_CONFIG_PATH"' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH="/usr/local/x264/lib:$LD_LIBRARY_PATH"' >> ~/.bashrc
source ~/.bashrc
创建x264配置文件:
sudo nano /etc/ld.so.conf.d/x264.conf
# 添加以下内容
/usr/local/x264/lib
3. 安装libdrm
wget https://dri.freedesktop.org/libdrm/libdrm-2.4.89.tar.bz2
tar -jxvf libdrm-2.4.89.tar.bz2
cd libdrm-2.4.89
sed -i 's/drm_private uint32_t nouveau_debug;/drm_private extern uint32_t nouveau_debug;/' nouveau/private.h
./configure --prefix=/usr/local/libdrm --host=aarch64-linux-gnu
make -j8
sudo make install
4. 安装FFmpeg
git clone https://git.ffmpeg.org/ffmpeg.git
cd ffmpeg
./configure --prefix=/usr/local/ffmpeg --enable-version3 --enable-libdrm --enable-rkmpp --enable-libx264 --enable-nonfree --enable-gpl --enable-shared
make -j8
sudo make install
4.1 配置环境变量
为了让系统正确识别FFmpeg,设置路径和库路径:
echo 'export PATH="/usr/local/my/ffmpeg/bin:$PATH"' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH="/usr/local/my/ffmpeg/lib:$LD_LIBRARY_PATH"' >> ~/.bashrc
source ~/.bashrc
4.2 配置系统库
更新系统库配置,确保新安装的库文件能被识别:
sudo vim /etc/ld.so.conf
# 添加以下内容
include /etc/ld.so.conf.d/*.conf
/usr/local/lib
/usr/local/x264/lib
/usr/local/libdrm/lib
完成以上步骤后,你就能在RK3588平台上使用RKMPP加速的FFmpeg进行高效的视频编解码。
确保每个步骤都按照顺序完成,特别是环境变量和配置文件的修改,它们非常重要。
5.安装opencv
opencv安装包下载地址:
https://github.com/opencv/opencv/
扩展库下载地址:
https://github.com/opencv/opencv_contrib
一键编译脚本build-opencv.sh,将这个脚本放在下载的opencv同级目录:
#!/bin/bash
sudo apt-get update
sudo apt-get install -y \
build-essential \
pkg-config \
libgtk2.0-dev \
libavcodec-dev \
libavformat-dev \
libswscale-dev \
libv4l-dev \
libxvidcore-dev \
libx264-dev \
libjpeg-dev \
libtiff5-dev \
libvtk7-dev
# 设置工作目录
BASE_DIR=$(pwd)
OPENCV_DIR="${BASE_DIR}/opencv-4.10.0"
INSTALL_DIR="${OPENCV_DIR}/install"
BUILD_DIR="${OPENCV_DIR}/build"
CONTRIB_DIR="${BASE_DIR}/opencv_contrib-4.10.0"
# 创建安装和构建目录
mkdir -p "${INSTALL_DIR}"
mkdir -p "${BUILD_DIR}"
# 进入构建目录
cd "${BUILD_DIR}"
# 运行 CMake
cmake \
-D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX="${INSTALL_DIR}" \
-D OPENCV_EXTRA_MODULES_PATH="${CONTRIB_DIR}/modules" \
-D WITH_LIBV4L=ON \
-D WITH_EIGEN=ON \
-D WITH_TBB=ON \
-D WITH_OPENMP=ON \
-D WITH_OPENCL=ON \
-D WITH_OPENGL=ON \
-D ENABLE_CXX11=ON \
-D WITH_FFMPEG=ON \
-D FFMPEG_INCLUDE_DIR="/usr/local/ffmpeg/include" \
-D FFMPEG_LIBRARIES="/usr/local/ffmpeg/lib/libavcodec.so;/usr/local/ffmpeg/lib/libavformat.so;/usr/local/ffmpeg/lib/libavutil.so" \
-D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_GENERATE_PKGCONFIG=YES \
-D ENABLE_PRECOMPILED_HEADERS=OFF \
"${OPENCV_DIR}" # 确保CMake指向正确的源代码目录
# 编译
make -j$(nproc) # 调整为根据实际CPU核心数选择合适的线程数
make install
5.1 删除手动安装包中的opencv版本
sudo apt-get remove --purge libopencv* python3-opencv
sudo rm -rf /usr/local/lib/libopencv_*
sudo rm -rf /usr/local/lib/pkgconfig/opencv4.pc
sudo rm -rf /usr/local/include/opencv*
sudo rm -rf /usr/local/bin/opencv_*
5.2安装依赖以及编译
5.2.1 更新系统依赖库
sudo apt-get update
sudo apt-get install -y \
build-essential \
pkg-config \
libgtk2.0-dev \
libavcodec-dev \
libavformat-dev \
libswscale-dev \
libv4l-dev \
libxvidcore-dev \
libx264-dev \
libjpeg-dev \
libtiff5-dev \
libvtk7-dev
5.2.2 创建编译build目录以及运行 CMake
cd opencv-4.10.0 && mkdir build && mkdir install && cd build
cmake \
-D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX="${INSTALL_DIR}" \
-D OPENCV_EXTRA_MODULES_PATH="${CONTRIB_DIR}/modules" \
-D WITH_LIBV4L=ON \
-D WITH_EIGEN=ON \
-D WITH_TBB=ON \
-D WITH_OPENMP=ON \
-D WITH_OPENCL=ON \
-D WITH_OPENGL=ON \
-D ENABLE_CXX11=ON \
-D WITH_FFMPEG=ON \
-D FFMPEG_INCLUDE_DIR="/usr/local/ffmpeg/include" \
-D FFMPEG_LIBRARIES="/usr/local/ffmpeg/lib/libavcodec.so;/usr/local/ffmpeg/lib/libavformat.so;/usr/local/ffmpeg/lib/libavutil.so" \
-D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_GENERATE_PKGCONFIG=YES \
-D ENABLE_PRECOMPILED_HEADERS=OFF \
5.2.3 开始编译以及生成库
make -j4 && make install
5.2.4 opencv库的安装
命令执行结束,在install生成库文件,我的文件夹结构为:
/home/orangepi/
├── opencv-4.10.0
│ ├── build
│ └── install
│ ├── bin
│ ├── include
│ ├── lib
│ └── share
└── opencv_contrib-4.10.0
└── modules
下面开始将库文件拷贝到系统目录,在install目录下打开终端,分别执行以下命令:
(1)修改opencv-4.10.0/install/lib/pkgconfig/opencv4.pc
vim lib/pkgconfig/opencv4.pc
原始的:
# Package Information for pkg-config
prefix=/home/orangepi/opencv-4.10.0/install
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include/opencv4
Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.10.0
Libs: -L${exec_prefix}/lib -lopencv_gapi -lopencv_stitching -lopencv_alphamat -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_cudabgsegm -lopencv_cudafeatures2d -lopencv_cudaobjdetect -lopencv_cudastereo -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_intensity_transform -lopencv_line_descriptor -lopencv_mcc -lopencv_quality -lopencv_rapid -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_signal -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_cudacodec -lopencv_surface_matching -lopencv_tracking -lopencv_highgui -lopencv_datasets -lopencv_text -lopencv_plot -lopencv_videostab -lopencv_cudaoptflow -lopencv_optflow -lopencv_cudalegacy -lopencv_videoio -lopencv_cudawarping -lopencv_viz -lopencv_wechat_qrcode -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_dnn -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_cudaimgproc -lopencv_cudafilters -lopencv_imgproc -lopencv_cudaarithm -lopencv_core -lopencv_cudev
Libs.private: -lm -lpthread -lcudart_static -ldl -lrt -lnppc -lnppial -lnppicc -lnppidei -lnppif -lnppig -lnppim -lnppist -lnppisu -lnppitc -lnpps -lcublas -lcudnn -lcufft -L-L/usr/local/cuda-11.4 -llib64 -L-L/usr/lib -laarch64-linux-gnu
Cflags: -I${includedir}
新的:
# Package Information for pkg-config
prefix=prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include/opencv4
Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.10.0
Libs: -L${exec_prefix}/lib -lopencv_gapi -lopencv_stitching -lopencv_alphamat -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_cudabgsegm -lopencv_cudafeatures2d -lopencv_cudaobjdetect -lopencv_cudastereo -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_intensity_transform -lopencv_line_descriptor -lopencv_mcc -lopencv_quality -lopencv_rapid -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_signal -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_cudacodec -lopencv_surface_matching -lopencv_tracking -lopencv_highgui -lopencv_datasets -lopencv_text -lopencv_plot -lopencv_videostab -lopencv_cudaoptflow -lopencv_optflow -lopencv_cudalegacy -lopencv_videoio -lopencv_cudawarping -lopencv_viz -lopencv_wechat_qrcode -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_dnn -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_cudaimgproc -lopencv_cudafilters -lopencv_imgproc -lopencv_cudaarithm -lopencv_core -lopencv_cudev
Libs.private: -lm -lpthread -lcudart_static -ldl -lrt -lnppc -lnppial -lnppicc -lnppidei -lnppif -lnppig -lnppim -lnppist -lnppisu -lnppitc -lnpps -lcublas -lcudnn -lcufft -L-L/usr/local/cuda-11.4 -llib64 -L-L/usr/lib -laarch64-linux-gnu
Cflags: -I${includedir}
(2)将 bin 文件夹复制到 /usr/local
sudo cp -r bin/* /usr/local/bin/
(3)将 include 文件夹复制到 /usr/local/include
sudo cp -r include/* /usr/local/include/
(4)将 lib 文件夹复制到 /usr/local/lib
sudo mv lib/python3.8/site-packages lib/python3.8/dist-packages
sudo cp -r lib/* /usr/local/lib/
(5)将 share 文件夹复制到 /usr/local/share
sudo cp -r share/* /usr/local/share/
5.3. 将OpenCV的库添加到系统路径
5.3.1 方法一:配置ld.so.conf文件
sudo vim /etc/ld.so.conf
在文件中加上一行
include /usr/local/lib
这个路径是cmake编译时填的动态库安装路径加上/lib
配置ld.so.conf文件
5.3.2 方法二:手动生成opencv.conf文件
sudo vim /etc/ld.so.conf.d/opencv.conf
是一个新建的空文件,直接添加路径,同理这个路径是cmake编译时填的动态库安装路径加上/lib
/usr/local/lib
5.3.3 应用配置更新
以上两种方法配置好后,执行如下命令使得配置的路径生效
sudo ldconfig
配置系统bash
因为在cmake时,选择了自动生成OpenCV的pkgconfig文件,在/usr/local/lib/pkgconfig路径可以看到文件opencv4.pc
确保文件存在,执行如下命令
sudo vim /etc/bash.bashrc
在文末添加
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
如下:
bash.bashrc
保存退出,然后执行如下命令使配置生效
source /etc/bash.bashrc
echo 'export CPLUS_INCLUDE_PATH=/usr/local/include/opencv:/usr/local/include/opencv4:$CPLUS_INCLUDE_PATH' >> ~/.bashrc
echo 'export LIBRARY_PATH=/usr/local/lib:$LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrc
至此,Linux\Ubuntu18.04环境下OpenCV的安装以及配置已经全部完成,可以使用以下命令查看是否安装和配置成功
pkg-config --modversion opencv4
pkg-config --cflags opencv4
pkg-config --libs opencv4
最后添加软链接,确保C++编译没问题:
sudo mv /usr/local/include/opencv4 /usr/local/include/opencv
sudo ln -s /usr/local/include/opencv/opencv2 /usr/local/include/opencv2
sudo ln -s /usr/local/include/opencv/opencv2 /usr/local/include/opencv4