在前文《视频编解码硬件方案漫谈》中,我们探讨了硬件视频编解码的一般方案。本文将进一步探讨如何在ffmpeg中利用显卡硬件加速音视频编解码。

一、基本概况
ffmpeg通过对显卡厂商的SDK进行封装和集成,实现了部分硬件编解码功能。
| NVIDIA | AMD | INTEL | |
|---|---|---|---|
| 编码器 | xxx_nvenc | xxx_amf | xxxx_qsv |
| 解码器 | xxx_cuvid | 暂未实现 | xxxx_qsv |
其中,xxx代表编码类型,如h264、h265、mpeg2、vp8、vp9等。此外,ffmpeg中的软件编解码器也可以实现相关硬件加速。例如,在h264解码器中可以使用cuda加速、qsv加速、dxva2加速、d3d11va加速和opencl加速等。
| cuda | qsv | dxva2/d3d11va | opencl | |
|---|---|---|---|---|
| 应用场景 | 适用于NVIDIA显卡平台,但跨操作系统 | 适用于Intel显卡平台,但跨操作系统 | 适用于Windows操作系统,但跨硬件平台 | 仅支持opencl的硬件平台 |
二、命令行的使用
在ffmpeg中,如果使用
-vcodec xxx指定硬件编解码器,否则将使用软件编解码。例如:
ffplay -x 800 -y 600 -vcodec h264_qsv h264.mp4 ffplay -x 800 -y 600 -vcodec hevc_qsv 4k_hevc.mp4 ffmpeg.exe -i test.ts -vcodec hevc_amf -s 1280x720 output.ts
三、代码中使用
1)使用特定的编解码器
编解码器包由AVCodec描述,其中ID代表一类编码器或解码器。例如,
AV_CODEC_ID_H264代表h264编解码器,而name代表特定编码器或解码器。通常我们使用
avcodec_find_decoder(ID)和
avcodec_find_encoder(ID)来查找解码器和编码器,默认使用软件编解码。如果需要使用硬件编解码,则使用
avcodec_find_encoder_by_name(name)和
avcodec_find_decoder_by_name(name)来指定编码器。其余代码流程与软件编解码一致。
LANUX V1.0 蓝脑商务网站系统 适用于网店、公司宣传自己的品牌和产品。 系统在代码、页面方面设计简约,浏览和后台管理操作效率高。 此版本带可见即可得的html编辑器, 方便直观添加和编辑要发布的内容。 安装: 1.解压后,更换logo、分类名称、幻灯片的图片及名称和链接、联系我们等等页面。 2.将dbconfig.php里面的数据库配置更改为你的mysql数据库配置 3.将整个文件夹上传至
例如:
//codec = avcodec_find_decoder(AV_CODEC_ID_H264);
codec = avcodec_find_decoder_by_name("h264_cuvid");
if (!codec) {
fprintf(stderr, "Codec not found\n");
exit(1);
}2)使用硬件加速
使用特定编解码器的好处是跨操作系统,无论是Windows还是Linux都使用同一套代码,但缺点是不跨硬件,不同显卡厂商使用不同的编解码器。基于软件编码器的硬件加速则跨硬件显卡,如Windows上的d3d11va硬件加速,无论底层是AMD、Intel还是NVIDIA显卡都适用,相当于Windows系统屏蔽了硬件细节,我们只需调用Windows的API即可。以下是一个基于硬件加速的示例代码:
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; }
//获取GPU硬件解码帧的格式 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; }
//解码后数据格式转换,GPU到CPU拷贝,YUV数据dump到文件 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 sending a packet for decoding\n"); return ret; } while (ret >= 0) { 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 during decoding\n"); goto fail; } if (frame->format == hw_pix_fmt) { /* 将解码后的数据从GPU内存格式转为CPU内存格式,并完成GPU到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((AVPixelFormat)tmp_frame->format, tmp_frame->width, tmp_frame->height, 1); //分配内存 buffer = (uint8_t *)av_malloc(size); if (!buffer) { fprintf(stderr, "Can not alloc buffer\n"); ret = AVERROR(ENOMEM); goto fail; } //将图片数据拷贝到buffer中(按行拷贝) ret = av_image_copy_to_buffer(buffer, size, (const uint8_t* const*)tmp_frame->data, (const int*)tmp_frame->linesize, (AVPixelFormat)tmp_frame->format, tmp_frame->width, tmp_frame->height, 1); if (ret < 0) { fprintf(stderr, "Can not copy image to buffer\n"); goto fail; } fwrite(buffer, 1, size, output_file); av_frame_free(&frame); av_frame_free(&sw_frame); av_free(buffer); }fail: av_frame_free(&frame); av_frame_free(&sw_frame); av_free(buffer); return ret; }
int main(int argc, char argv[]) { AVFormatContext input_ctx = NULL; AVCodecContext decoder_ctx = NULL; AVCodec decoder = NULL; AVPacket packet; enum AVHWDeviceType type; int video_stream = -1; AVCodecParameters video = NULL; AVHWDeviceType type = AV_HWDEVICE_TYPE_NONE; const AVCodecHWConfig config = NULL;
if (argc < 4) { fprintf(stderr, "Usage: %send: fclose(output_file); avcodec_free_context(&decoder_ctx); avformat_close_input(&input_ctx); av_buffer_unref(&hw_device_ctx);
return ret;}
编译后生成
hw_decoder.exe,解码生成YUV文件如下:hw_decoder.exe dxva2 D:\videos\hevcdemo.ts test.yuv
由此可见,GPU解码器的利用率高,CPU占用率极低,硬件加速成功。










