上网查了ffmpeg使用GPU加速的教程,发现有些时间老旧,或者过于复杂了,自己探索了一下,发现也不难,现将方法整理如下:
环境安装
1.下载nvidia video codec SDK
先从nvidia官网下载SDK开发包,以及相关的Guide
https://developer.nvidia.com/nvidia-video-codec-sdk/download
百度网盘地址:
链接:https://pan.baidu.com/s/18RcZilHycKq1b3Ar7xSegQ
提取码:72af
下载SDK
需要说明的是,SDK9.1 对应的NVIDIA驱动以及CUDA版本较高,而SDK8.1对应的版本驱动低一些,对比如下:
- SDK9.1
- SDK8
我这里已经下载下来了,链接如下:
安装英伟达驱动和CUDA
这个网上有很多教程,去网上搜索一下就可以
下载Guide
在下载网页下方,有相关的安装教程,下面的方法也是按照此教程进行安装。
2.编译ffmpeg
以下内容按照Guide教程中2.2.2.1Compiling for Linux部分进行编译安装。
-
Clone ffnvcodec
git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
在执行时,发现下载不下来,
解决方法:
https://github.com/FFmpeg/nv-codec-headers
找到对应的版本git clone下载下来 -
Install ffnvcodec
cd nv-codec-headers && sudo make install && cd –
-
Clone FFmpeg’s public GIT repository.
git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg/
也可在http://ffmpeg.org/download.html 中找到对应的版本下载
-
Install necessary packages
sudo apt-get install build-essential yasm cmake libtool libc6 libc6-dev unzip wget libnuma1 libnuma-dev
-
Configure
cd ffmpeg ./configure --enable-nonfree -–enable-cuda-sdk –enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64
如果想编译到指定目录,在后面加入–prefix选项
--prefix=/lib/ffmpeg_gpu
-
Compile
make -j 8
-
Install the libraries.
sudo make install
如果没有报错,就表明安装完成,如果指定目录,那么就可以在指定目录中找到
里面包含了需要用的include和lib,注意上面生成的库文件为静态库文件,我尝试生成动态库,没有成功,但是也不影响使用。
3.测试ffmpeg
在终端,或者你指定的生成路径的bin目录下,输入命令
ffmpeg -decoders | grep hevc
输出如下:
ffmpeg -encoders | grep nv
输出如下:
则表明安装成功
代码编程
1.CMakelists.txt编写
set(FFmpeg_INC /lib/ffmpeg_gpu/include)
set(FFmpeg_LIB /lib/ffmpeg_gpu/lib)
include_directories(
${FFmpeg_INC}
)
target_link_libraries(project
${FFmpeg_LIB}
${FFmpeg_LIB}/libavdevice.a
${FFmpeg_LIB}/libavfilter.a
${FFmpeg_LIB}/libavformat.a
${FFmpeg_LIB}/libavutil.a
${FFmpeg_LIB}/libswresample.a
${FFmpeg_LIB}/libswscale.a
${FFmpeg_LIB}/libavcodec.a
/lib/x86_64-linux-gnu/libdl.so.2
/usr/lib/x86_64-linux-gnu/libpthread.so
/lib/x86_64-linux-gnu/libz.so.1
/lib/x86_64-linux-gnu/liblzma.so.5
)
2.代码部分
这个主要参考官方例程
使用方法和非硬件接口使用方法类似,
更多相关的代码部分,可以利用关键函数搜索github,这里给出一些比较有参考价值的仓库链接
Stream中的src/NVStream.cpp部分
FFmpeg4.0-study中的Cff/CDecode.cpp部分
sunshine-old中的client/sunshine-client-opengl/VideoPlayer.cpp部分
shaka-player-embedded中的/shaka/src/media/frame_converter.cc部分
以下代码是就是ffmpeg中官方例程的内容
#include <stdio.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/pixdesc.h>
#include <libavutil/hwcontext.h>
#include <libavutil/opt.h>
#include <libavutil/avassert.h>
#include <libavutil/imgutils.h>
static AVBufferRef *hw_device_ctx = NULL;
static enum AVPixelFormat hw_pix_fmt;
static FILE *output_file = NULL;
static int hw_decoder_init(AVCodecContext *ctx, const enum AVHWDeviceType type)
{
int err = 0;
if ((err = av_hwdevice_ctx_create(&hw_device_ctx, type,
NULL, NULL, 0)) < 0) {
fprintf(stderr, "Failed to create specified HW device.\n");
return err;
}
ctx->hw_device_ctx = av_buffer_ref(hw_device_ctx);
return err;
}
static enum AVPixelFormat get_hw_format(AVCodecContext *ctx,
const enum AVPixelFormat *pix_fmts)
{
const enum AVPixelFormat *p;
for (p = pix_fmts; *p != -1; p++) {
if (*p == hw_pix_fmt)
return *p;
}
fprintf(stderr, "Failed to get HW surface format.\n");
return AV_PIX_FMT_NONE;
}
static int decode_write(AVCodecContext *avctx, AVPacket *packet)
{
AVFrame *frame = NULL, *sw_frame = NULL;
AVFrame *tmp_frame = NULL;
uint8_t *buffer = NULL;
int size;
int ret = 0;
ret = avcodec_send_packet(avctx, packet);
if (ret < 0) {
fprintf(stderr, "Error during decoding\n");
return ret;
}
while (1) {
if (!(frame = av_frame_alloc()) || !(sw_frame = av_frame_alloc())) {
fprintf(stderr, "Can not alloc frame\n");
ret = AVERROR(ENOMEM);
goto fail;
}
ret = avcodec_receive_frame(avctx, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
av_frame_free(&frame);
av_frame_free(&sw_frame);
return 0;
} else if (ret < 0) {
fprintf(stderr, "Error while decoding\n");
goto fail;
}
if (frame->format == hw_pix_fmt) {
/* retrieve data from GPU to CPU */
if ((ret = av_hwframe_transfer_data(sw_frame, frame, 0)) < 0) {
fprintf(stderr, "Error transferring the data to system memory\n");
goto fail;
}
tmp_frame = sw_frame;
} else
tmp_frame = frame;
size = av_image_get_buffer_size(tmp_frame->format, tmp_frame->width,
tmp_frame->height, 1);
buffer = av_malloc(size);
if (!buffer) {
fprintf(stderr, "Can not alloc buffer\n");
ret = AVERROR(ENOMEM);
goto fail;
}
ret = av_image_copy_to_buffer(buffer, size,
(const uint8_t * const *)tmp_frame->data,
(const int *)tmp_frame->linesize, tmp_frame->format,
tmp_frame->width, tmp_frame->height, 1);
if (ret < 0) {
fprintf(stderr, "Can not copy image to buffer\n");
goto fail;
}
if ((ret = fwrite(buffer, 1, size, output_file)) < 0) {
fprintf(stderr, "Failed to dump raw data.\n");
goto fail;
}
fail:
av_frame_free(&frame);
av_frame_free(&sw_frame);
av_freep(&buffer);
if (ret < 0)
return ret;
}
}
int main(int argc, char *argv[])
{
AVFormatContext *input_ctx = NULL;
int video_stream, ret;
AVStream *video = NULL;
AVCodecContext *decoder_ctx = NULL;
AVCodec *decoder = NULL;
AVPacket packet;
enum AVHWDeviceType type;
int i;
if (argc < 4) {
fprintf(stderr, "Usage: %s <device type> <input file> <output file>\n", argv[0]);
return -1;
}
type = av_hwdevice_find_type_by_name(argv[1]);
if (type == AV_HWDEVICE_TYPE_NONE) {
fprintf(stderr, "Device type %s is not supported.\n", argv[1]);
fprintf(stderr, "Available device types:");
while((type = av_hwdevice_iterate_types(type)) != AV_HWDEVICE_TYPE_NONE)
fprintf(stderr, " %s", av_hwdevice_get_type_name(type));
fprintf(stderr, "\n");
return -1;
}
/* open the input file */
if (avformat_open_input(&input_ctx, argv[2], NULL, NULL) != 0) {
fprintf(stderr, "Cannot open input file '%s'\n", argv[2]);
return -1;
}
if (avformat_find_stream_info(input_ctx, NULL) < 0) {
fprintf(stderr, "Cannot find input stream information.\n");
return -1;
}
/* find the video stream information */
ret = av_find_best_stream(input_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &decoder, 0);
if (ret < 0) {
fprintf(stderr, "Cannot find a video stream in the input file\n");
return -1;
}
video_stream = ret;
for (i = 0;; i++) {
const AVCodecHWConfig *config = avcodec_get_hw_config(decoder, i);
if (!config) {
fprintf(stderr, "Decoder %s does not support device type %s.\n",
decoder->name, av_hwdevice_get_type_name(type));
return -1;
}
if (config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &&
config->device_type == type) {
hw_pix_fmt = config->pix_fmt;
break;
}
}
if (!(decoder_ctx = avcodec_alloc_context3(decoder)))
return AVERROR(ENOMEM);
video = input_ctx->streams[video_stream];
if (avcodec_parameters_to_context(decoder_ctx, video->codecpar) < 0)
return -1;
decoder_ctx->get_format = get_hw_format;
if (hw_decoder_init(decoder_ctx, type) < 0)
return -1;
if ((ret = avcodec_open2(decoder_ctx, decoder, NULL)) < 0) {
fprintf(stderr, "Failed to open codec for stream #%u\n", video_stream);
return -1;
}
/* open the file to dump raw data */
output_file = fopen(argv[3], "w+");
/* actual decoding and dump the raw data */
while (ret >= 0) {
if ((ret = av_read_frame(input_ctx, &packet)) < 0)
break;
if (video_stream == packet.stream_index)
ret = decode_write(decoder_ctx, &packet);
av_packet_unref(&packet);
}
/* flush the decoder */
packet.data = NULL;
packet.size = 0;
ret = decode_write(decoder_ctx, &packet);
av_packet_unref(&packet);
if (output_file)
fclose(output_file);
avcodec_free_context(&decoder_ctx);
avformat_close_input(&input_ctx);
av_buffer_unref(&hw_device_ctx);
return 0;
}