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

一、基本概况
ffmpeg通过对显卡厂商的SDK进行封装和集成,实现了部分硬件编解码功能。
编码器xxx_nvencxxx_amfxxxx_qsv解码器xxx_cuvid暂未实现xxxx_qsv
其中,xxx代表编码类型,如h264、h265、mpeg2、vp8、vp9等。此外,ffmpeg中的软件编解码器也可以实现相关硬件加速。例如,在h264解码器中可以使用cuda加速、qsv加速、dxva2加速、d3d11va加速和opencl加速等。
应用场景适用于NVIDIA显卡平台,但跨操作系统适用于Intel显卡平台,但跨操作系统适用于Windows操作系统,但跨硬件平台仅支持opencl的硬件平台
二、命令行的使用
在ffmpeg中,如果使用
-vcodec xxx
指定硬件编解码器,否则将使用软件编解码。例如:
ffplay -x 800 -y 600 -vcodec h264_qsv h264.mp4ffplay -x 800 -y 600 -vcodec hevc_qsv 4k_hevc.mp4ffmpeg.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)
来指定编码器。其余代码流程与软件编解码一致。
php订单系统可以整合支付宝接口
一、系统设置:用Dreamweaver等网页设计软件在代码视图下打开【dddingdan/config.php】系统设置文件,按注释说明进行系统设置。 二、系统使用:WFPHP在线订单系统是无台后的,不用数据库,也不用安装,解压源码包后,先进行系统设置,然后把整个【dddingdan】文件夹上传到服务器。在网页中要插入订单系统的位置,插入系统调用代码: 注意:id=01就表示使用样式01,如果要使
0 查看详情
例如:
//codec = avcodec_find_decoder(AV_CODEC_ID_H264);codec = avcodec_find_decoder_by_name("h264_cuvid");if (!codec) { fprintf(stderr, "Codec not foundn"); 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)) 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) { if (!(frame = av_frame_alloc()) || !(sw_frame = av_frame_alloc())) { fprintf(stderr, "Can not alloc framen"); 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 format == hw_pix_fmt) { /* 将解码后的数据从GPU内存格式转为CPU内存格式,并完成GPU到CPU内存的拷贝*/ if ((ret = av_hwframe_transfer_data(sw_frame, frame, 0)) format, tmp_frame->width, tmp_frame->height, 1); //分配内存 buffer = (uint8_t *)av_malloc(size); if (!buffer) { fprintf(stderr, "Can not alloc buffern"); 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 buffern"); 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: %s n", argv[0]); return -1;}// 设备类型为:cuda dxva2 qsv d3d11va opencl,通常在windows使用d3d11va或者dxva2type = 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 stream informationn"); return -1;}for (int i = 0; i nb_streams; i++) { if (input_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { video_stream = i; break; }}if (video_stream == -1) { fprintf(stderr, "No video stream foundn"); return -1;}decoder = avcodec_find_decoder(input_ctx->streams[video_stream]->codecpar->codec_id);if (!decoder) { fprintf(stderr, "Failed to find %s codecn", av_get_media_type_string(AVMEDIA_TYPE_VIDEO)); return -1;}while ((config = avcodec_get_hw_config(decoder, config ? config->config_id + 1 : 0))) { 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) get_format = get_hw_format;//硬件加速初始化if (hw_decoder_init(decoder_ctx, type) < 0) return -1;if (avcodec_open2(decoder_ctx, decoder, NULL) = 0) { if (packet.stream_index == video_stream) { ret = decode_write(decoder_ctx, &packet); if (ret < 0) goto end; } av_packet_unref(&packet);}// Flush the decoderpacket.data = NULL;packet.size = 0;ret = decode_write(decoder_ctx, &packet);if (ret < 0) goto end;end: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:videoshevcdemo.ts test.yuv
由此可见,GPU解码器的利用率高,CPU占用率极低,硬件加速成功。
以上就是FFMPEG硬件编解码器使用的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/848415.html
微信扫一扫
支付宝扫一扫