build ffmpeg with CUDA

CUDA (Compute Unified Device Architecture)

CUDA is a parallel programming framework by Nvidia

CUDA® is a parallel computing platform and programming model developed by NVIDIA for general computing on graphical processing units (GPUs).
With CUDA, developers are able to dramatically speed up computing applications by harnessing the power of GPUs.

In GPU-accelerated applications, the sequential part of the workload runs on the CPU – which is optimized for single-threaded performance – while the compute intensive portion of the application runs on thousands of GPU cores in parallel.
When using CUDA, developers program in popular languages such as C, C++, Fortran, Python and MATLAB and express parallelism through extensions in the form of a few basic keywords.

NVENC & NVDEC

FFmpeg可通过Nvidia的GPU进行加速,其中高层接口是通过Video Codec SDK来实现GPU资源的调用。
Video Codec SDK包含完整的的高性能工具、源码及文档,支持,可以运行在Windows和Linux系统之上。
从软件上来说,SDK包含两类硬件加速接口:

  • 用于编码加速的NVENCODE API
  • 用于解码加速的NVDECODE API(之前被称为NVCUVID API)
build ffmpeg with CUDA_第1张图片

从硬件上来说,Nvidia GPU有一到多个编解码器(解码器又称硬件加速引擎),它们独立于CUDA核
...
Video Codec SDK已经被集成在ffmpeg工程中,但是ffmpeg对编解码器配置参数较少,如果需要充分的发挥编解码器特性,还需要直接使用SDK进行编程。

build ffmpeg with CUDA

Getting Started with FFmpeg/libav using NVIDIA GPUs

Using NVIDIA hardware acceleration in FFmpeg/libav requires the following steps

  • Download the latest FFmpeg or libav source code, by cloning the corresponding GIT repositories
  • FFmpeg: https://git.ffmpeg.org/ffmpeg.git
  • Libav: https://github.com/libav/libav
  • Download and install the compatible driver from NVIDIA web site
  • Downoad and install the CUDA Toolkit CUDA toolkit
  • Use the following configure command (Use correct CUDA library path in config command below)
    ./configure --enable-cuda --enable-cuvid --enable-nvenc --enable-nonfree --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64
  • Use following command for build: make -j 10
  • Use FFmpeg/libav binary as required. To start with FFmpeg, try the below sample command line for 1:2 transcoding
    ffmpeg -y -hwaccel cuvid -c:v h264_cuvid -vsync 0 -i -vf scale_npp=1920:1072 -vcodec h264_nvenc -vf scale_npp=1280:720 -vcodec h264_nvenc

build成功后,可以查看目前ffmpeg所支持的nvidia硬件编解码器。
我的ffmpeg版本是: 3.2.2

  • 查看nvidia硬件解码器

CUVID, which is also called NVDEC by NVIDIA now, can be used for decoding on Windows and Linux. In combination with NVENC, it offers full hardware transcoding.

# ffmpeg -codecs | grep cuvid
 ...
 DEV.L. h263                 H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2 (decoders: h263 h263_cuvid )
 DEV.LS h264                 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (decoders: h264 h264_cuvid ) (encoders: h264_nvenc nvenc nvenc_h264 )
 DEV.L. hevc                 H.265 / HEVC (High Efficiency Video Coding) (decoders: hevc hevc_cuvid ) (encoders: nvenc_hevc hevc_nvenc )
 DEVIL. mjpeg                Motion JPEG (decoders: mjpeg mjpeg_cuvid )
 DEV.L. mpeg1video           MPEG-1 video (decoders: mpeg1video mpeg1_cuvid )
 DEV.L. mpeg2video           MPEG-2 video (decoders: mpeg2video mpegvideo mpeg2_cuvid )
 DEV.L. mpeg4                MPEG-4 part 2 (decoders: mpeg4 mpeg4_cuvid )
 D.V.L. vc1                  SMPTE VC-1 (decoders: vc1 vc1_cuvid )
 D.V.L. vp8                  On2 VP8 (decoders: vp8 vp8_cuvid )
 D.V.L. vp9                  Google VP9 (decoders: vp9 vp9_cuvid )
  • 查看nvidia硬件编码器
#ffmpeg -codecs | grep nvenc
...
DEV.LS h264                 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (decoders: h264 h264_cuvid ) (encoders: h264_nvenc nvenc nvenc_h264 )
DEV.L. hevc                 H.265 / HEVC (High Efficiency Video Coding) (decoders: hevc hevc_cuvid ) (encoders: nvenc_hevc hevc_nvenc )

其中前缀含义如下:
前缀含义
D….. = Decoding supported
.E…. = Encoding supported
..V… = Video codec
..A… = Audio codec
..S… = Subtitle codec
…I.. = Intra frame-only codec
….L. = Lossy compression
…..S = Lossless compression

References:

https://developer.nvidia.com/ffmpeg
https://developer.nvidia.com/nvidia-video-codec-sdk
https://blog.csdn.net/weixin_35804181/article/details/54377365
https://blog.csdn.net/LeoChen1983/article/details/72730802
https://developer.nvidia.com/cuda-zone
http://trac.ffmpeg.org/wiki/HWAccelIntro

你可能感兴趣的:(build ffmpeg with CUDA)