这是sdk官网
https://developer.nvidia.com/nvidia-video-codec-sdk
https://docs.nvidia.com/video-technologies/video-codec-sdk/
下载包目录地址:
https://developer.nvidia.com/video-codec-sdk-archive
这是cudaAPI
入口
关于显卡介绍
入口
微软官方流媒体学习
入口
NVDEC_VideoDecoder_API_ProgGuide(中文版本)
入口
API文档在这里
https://docs.nvidia.com/video-technologies/video-codec-sdk/nvenc-application-note/index.html
如何编译Video Codec SDK
准备环境
首先得有张卡吧,安装驱动,安装的时候要看看这张卡匹配的驱动版本。
切记切记:
不要通过apt install nvidia-cuda-toolkit
安装cuda,因为版本比较老,有可能是9.1版本,最新的都到12以上了。
如果不小心安装了,可以通过下面命令删除
apt-get remove --autoremove nvidia-cuda-toolkit
可以通过dpkg -l |grep -i nvidia
查看安装的包有没有被删除
rr开头的表示只有配置文件
最后通过下面命令删除config
apt -y purge xxx
下载入口:CUDA Toolkit 12.0 Downloads
历史下载链接:CUDA Toolkit Archive
wget https://developer.download.nvidia.com/compute/cuda/12.0.0/local_installers/cuda_12.0.0_525.60.13_linux.run
sudo sh cuda_12.0.0_525.60.13_linux.run
安装好以后,执行命令
nvidia-smi
可以看到下面的提示
root@sse-jq-114-106:~# nvidia-smi
Fri Dec 8 14:09:39 2023
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 525.60.13 Driver Version: 525.60.13 CUDA Version: 12.0 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 NVIDIA GeForce ... Off | 00000000:2D:00.0 Off | N/A |
| 0% 36C P0 N/A / 370W | 0MiB / 12288MiB | 0% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
| No running processes found |
+-----------------------------------------------------------------------------+
到这一步,恭喜你,宿主机上的环境安装好了。
我们可以去下面文件夹下看看安装好的文件
root@sse-jq-114-106:/usr/local# ll
total 72
drwxr-xr-x 18 root root 4096 Dec 7 20:23 ./
drwxr-xr-x 12 root root 4096 Sep 25 16:44 ../
lrwxrwxrwx 1 root root 21 Dec 7 20:23 cuda -> /usr/local/cuda-12.0//
drwxr-xr-x 17 root root 4096 Dec 7 20:24 cuda-12.0/
...
进入cuda文件夹,里面就是安装好的文件
root@sse-jq-114-106:/usr/local/cuda# ll
total 144
drwxr-xr-x 17 root root 4096 Dec 7 20:24 ./
drwxr-xr-x 18 root root 4096 Dec 7 20:23 ../
drwxr-xr-x 3 root root 4096 Dec 7 20:24 bin/
drwxr-xr-x 5 root root 4096 Dec 7 20:24 compute-sanitizer/
-rw-r--r-- 1 root root 160 Dec 7 20:24 DOCS
-rw-r--r-- 1 root root 61498 Dec 7 20:24 EULA.txt
drwxr-xr-x 5 root root 4096 Dec 7 20:24 extras/
drwxr-xr-x 6 root root 4096 Dec 7 20:23 gds/
drwxr-xr-x 2 root root 4096 Dec 7 20:23 gds-12.0/
lrwxrwxrwx 1 root root 28 Dec 7 20:24 include -> targets/x86_64-linux/include/
lrwxrwxrwx 1 root root 24 Dec 7 20:24 lib64 -> targets/x86_64-linux/lib/
drwxr-xr-x 7 root root 4096 Dec 7 20:24 libnvvp/
drwxr-xr-x 7 root root 4096 Dec 7 20:24 nsight-compute-2022.4.0/
drwxr-xr-x 2 root root 4096 Dec 7 20:24 nsightee_plugins/
drwxr-xr-x 6 root root 4096 Dec 7 20:24 nsight-systems-2022.4.2/
drwxr-xr-x 3 root root 4096 Dec 7 20:23 nvml/
drwxr-xr-x 7 root root 4096 Dec 7 20:24 nvvm/
-rw-r--r-- 1 root root 524 Dec 7 20:24 README
drwxr-xr-x 3 root root 4096 Dec 7 20:24 share/
drwxr-xr-x 2 root root 4096 Dec 7 20:23 src/
drwxr-xr-x 3 root root 4096 Dec 7 20:23 targets/
drwxr-xr-x 2 root root 4096 Dec 7 20:24 tools/
-rw-r--r-- 1 root root 2932 Dec 7 20:23 version.json
接着进入bin文件夹中,可以看到nvcc等编译器,所以如果我们要使用使用nvcc,可以到这个文件夹中来找
root@sse-jq-114-106:/usr/local/cuda/bin# ll
total 134060
drwxr-xr-x 3 root root 4096 Dec 7 20:24 ./
drwxr-xr-x 17 root root 4096 Dec 7 20:24 ../
-rwxr-xr-x 1 root root 88848 Dec 7 20:24 bin2c*
lrwxrwxrwx 1 root root 4 Dec 7 20:24 computeprof -> nvvp*
-r-xr-xr-x 1 root root 115 Dec 7 20:24 compute-sanitizer*
drwxr-xr-x 2 root root 4096 Dec 7 20:24 crt/
-rwxr-xr-x 1 root root 6732760 Dec 7 20:24 cudafe++*
-rwxr-xr-x 1 root root 15658080 Dec 7 20:24 cuda-gdb*
-rwxr-xr-x 1 root root 807704 Dec 7 20:24 cuda-gdbserver*
-rwxr-xr-x 1 root root 1046898 Dec 7 20:24 cuda-uninstaller*
-rwxr-xr-x 1 root root 75928 Dec 7 20:24 cu++filt*
-rwxr-xr-x 1 root root 511392 Dec 7 20:24 cuobjdump*
-rwxr-xr-x 1 root root 281728 Dec 7 20:24 fatbinary*
-r-xr-xr-x 1 root root 3633 Dec 7 20:24 ncu*
-r-xr-xr-x 1 root root 3423 Dec 7 20:24 ncu-ui*
-rwxr-xr-x 1 root root 1580 Dec 7 20:24 nsight_ee_plugins_manage.sh*
-r-xr-xr-x 1 root root 82 Dec 7 20:24 nsight-sys*
-r-xr-xr-x 1 root root 751 Dec 7 20:24 nsys*
-r-xr-xr-x 1 root root 104 Dec 7 20:24 nsys-exporter*
-r-xr-xr-x 1 root root 847 Dec 7 20:24 nsys-ui*
-rwxr-xr-x 1 root root 15264104 Dec 7 20:24 nvcc*
-rwxr-xr-x 1 root root 659024 Dec 7 20:24 __nvcc_device_query*
-rw-r--r-- 1 root root 417 Dec 7 20:24 nvcc.profile
-rwxr-xr-x 1 root root 50625304 Dec 7 20:24 nvdisasm*
-rwxr-xr-x 1 root root 19932024 Dec 7 20:24 nvlink*
-rwxr-xr-x 1 root root 5734432 Dec 7 20:24 nvprof*
-rwxr-xr-x 1 root root 113632 Dec 7 20:24 nvprune*
-rwxr-xr-x 1 root root 285 Dec 7 20:24 nvvp*
-rwxr-xr-x 1 root root 19668112 Dec 7 20:24 ptxas*
准备docker环境
想要在docker环境识别到英伟达显卡,还是要做点准备的。
至于怎么安装下面的docker 要自己去百度一下,反正安装好就是下面这个样子
用下面脚本启动docker,注意的是docker镜像我们使用nvidia提供的
#! /bin/bash
# 可以从下面网址查看可以使用的镜像
# https://hub.docker.com/r/nvidia/cuda/tags?page=1&name=18.04
docker rm -f nvcodec
docker run -d -i \
--init --gpus all -e NVIDIA_DRIVER_CAPABILITIES=compute,utility,video \
--name nvcodec \
--volume=/home:/home \
--volume=/data:/data \
--volume=/log:/log \
--volume=/root:/root \
-h indocker \
-e TZ=Asia/Shanghai \
--volume=/lib/modules/:/lib/modules/ \
--volume=/usr/src/:/usr/src/ \
--ipc=host --ulimit core=-1 --security-opt seccomp=unconfined \
--network host \
--privileged nvidia/cuda:12.0.0-devel-ubuntu18.04
进入docker
docker exec -it nvcodec /bin/bash
一定要注意这句:
--gpus all -e NVIDIA_DRIVER_CAPABILITIES=compute,utility,video
只有添加了这句话,才能在docker容器中识别出来显卡
在容器中执行nvidia-smi如果执行成功,说明可以使用了。
划重点:
上文中使用的是nvidia/cuda:12.0.0-devel-ubuntu18.04
镜像,如果你使用自己的镜像,那么你执行
$ mount
你就会发现很多宿主机上的UMD(runtimes)全部已经被映射到docker中了。可以不用执行下面的流程。
因为nvidia提供的这个镜像里面很多编译必须的东西都没有,所以我们通过下面脚本安装,其中cmake是通过源码安装的
#! /bin/bash
apt update
apt-get -y install pthread
apt-get -y install libbz2-dev
apt-get -y install liblzma-dev
apt-get -y install zlib1g-dev
apt-get -y install pkg-config
apt-get -y install git
apt-get -y install build-essential gdb
apt-get -y install wget
apt-get -y install libssl-dev
apt-get -y install openssl
apt-get -y install pv
cmake_tar_file=cmake-3.26.3.tar.gz
cmake_dir=cmake-3.26.3
echo "------cmake tar :${cmake_tar_file}"
echo "------cmake dir :${cmake_dir}"
if [ ! -e ${cmake_tar_file} ];then
echo "------download ${cmake_tar_file}"
wget -c https://github.com/Kitware/CMake/releases/download/v3.26.3/${cmake_tar_file}
fi
if [ -d ${cmake_dir} ];then
echo "------delete old ${cmake_dir}"
rm -rf ${cmake_dir}
fi
echo "------uncompress ${cmake_tar_file}"
pv ${cmake_tar_file} | tar -zxvf - >/dev/null 2>&1
pushd ${cmake_dir}
./bootstrap
make -j12
make install
cmake --version
popd
到此为止,docker也可以使用了
编译NVIDIA Video Codec SDK
下载sample 代码:入口
链接进入后如下图所示,选择你想要的版本就可以了。
因为要依赖ffmpeg,所以我们首先要编译ffmpeg,通过下面的脚本,我们先创建一个目录3rd,和Video_COdec_SDk12.0同级。
mkdir 3rd
cd 3rd
git clone https://github.com/FFmpeg/FFmpeg.git
然后执行下面的脚本就会编译好ffmpeg,本文我们ffmpeg切换到n4.5-dev tag上(你可以根据自己的需要来切换)
#! /bin/bash
DIR=$(dirname $(readlink -f $0))
echo "current: ${DIR}"
FFMPEG_TAG=n4.5-dev
FFMPEG_BUILD_DIR=${DIR}/libffmpeg-n4.5
if [ -d ${FFMPEG_BUILD_DIR} ];then
echo "rm -rf ${FFMPEG_BUILD_DIR}"
rm -rf ${FFMPEG_BUILD_DIR}
fi
pushd FFmpeg
echo "git checkout ${FFMPEG_TAG}"
git checkout ${FFMPEG_TAG}
CFG_PARAM="--prefix=$FFMPEG_BUILD_DIR --disable-x86asm --disable-static --enable-shared"
echo "${CFG_PARAM}"
./configure ${CFG_PARAM}
if [ $? -ne 0 ]; then
echo "------------configure ffmpeg failed,exit."
exit 1
fi
echo "make -j12"
make -j12
if [ $? -ne 0 ]; then
echo "------------compile ffmpeg failed,exit."
exit 1
fi
echo "make install"
make install
echo ""make distclean
make distclean
popd
编译好之后,接着下载另外一个依赖,还是在3rd目录下
下载VULKAN
wget https://sdk.lunarg.com/sdk/download/1.2.189.0/linux/vulkansdk-linux-x86_64-1.2.189.0.tar.gz
tar xvf https://sdk.lunarg.com/sdk/download/1.2.189.0/linux/vulkansdk-linux-x86_64-1.2.189.0.tar.gz
如何让Video Codec SDK识别到刚才那两个库呢,我们在3rd目录下创建文件3rd.cmake
#用来为SDK链接ffmpeg和vulkan
# 定义要添加的路径列表
set(PKG_CONFIG_PATH_LIST
${CMAKE_CURRENT_SOURCE_DIR}/../3rd/libffmpeg-n4.5/lib/pkgconfig
${CMAKE_CURRENT_SOURCE_DIR}/../3rd/vulkansdk-1.2.189.0/x86_64/lib/pkgconfig
)
# 将每个路径添加到PKG_CONFIG_PATH中
foreach(dir ${PKG_CONFIG_PATH_LIST})
set(ENV{PKG_CONFIG_PATH} "$ENV{PKG_CONFIG_PATH}:${dir}")
endforeach()
# 打印更新后的PKG_CONFIG_PATH环境变量
message("Updated PKG_CONFIG_PATH: $ENV{PKG_CONFIG_PATH}")
set(ENV{CUDA_TOOLKIT_ROOT_DIR} /usr/local/cuda)
# 设置cuvid和nvdia-encode的路径
set (CMAKE_LIBRARY_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../Lib/linux/stubs/x86_64)
set(VULKAN_LIBS vulkan)
set(Vulkan_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../3rd/vulkansdk-1.2.189.0/x86_64/include)
然后进入/workspace/nvcodec/Video_Codec_SDK_12.0.16/Samples/CMakeLists.txt中修改:
...
# Check for WSL
if (EXISTS /usr/lib/wsl/lib)
set(WSL_FLAG TRUE)
endif()
#我们自己的添加的代码
#----------------------------3rd-------------------
message(STATUS "${CMAKE_CURRENT_SOURCE_DIR}/../3rd/3rd.cmake")
include(${CMAKE_CURRENT_SOURCE_DIR}/../3rd/3rd.cmake)
#--------------------------------------------------------
...
然后执行脚本来编译
#! /bin/bash
DIR=$(dirname $(readlink -f $0))
echo ${DIR}
FFMPEG_TAG=n4.5-dev
build_path=${DIR}/build_samples
if [ ! -d ${build_path} ];then
echo "mkdir build path:${build_path}"
mkdir ${build_path}
fi
echo "pushd:"
pushd ${build_path}
echo "rm -rf *"
rm -rf *
echo "cmake ../Samples"
cmake ../Samples
echo "make."
make
make install
popd
顺利编译好。
参考:
浅谈Cuda driver API
https://zhuanlan.zhihu.com/p/111602648