ffmpeg库视频硬编码使用流程

news/2025/3/22 20:46:00/

一、硬件编码核心流程

  1. 硬件设备初始化

    // 创建CUDA硬件设备上下文‌
    AVBufferRef *hw_device_ctx = NULL;
    av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_CUDA, NULL, NULL, 0);// 绑定硬件设备到编码器上下文‌
    codec_ctx->hw_device_ctx = av_buffer_ref(hw_device_ctx);
  2. 编码器选择与参数配置

    // 查找NVIDIA H.264硬件编码器‌
    const AVCodec *encoder = avcodec_find_encoder_by_name("h264_nvenc");// 设置编码参数(分辨率、码率、GOP等)‌
    codec_ctx->width = 1920;
    codec_ctx->height = 1080;
    codec_ctx->bit_rate = 5000000;
    codec_ctx->time_base = (AVRational){1, 30};
    codec_ctx->pix_fmt = AV_PIX_FMT_CUDA;  // 指定硬件像素格式‌// 设置编码预设参数(NVIDIA专用)‌
    av_opt_set(codec_ctx->priv_data, "preset", "llhp", 0);  // 低延迟高性能
  3. 硬件帧内存分配

    // 创建硬件帧并绑定GPU内存‌
    AVFrame *hw_frame = av_frame_alloc();
    hw_frame->format = codec_ctx->pix_fmt;
    hw_frame->width = codec_ctx->width;
    hw_frame->height = codec_ctx->height;
    av_hwframe_get_buffer(codec_ctx->hw_frames_ctx, hw_frame, 0);
  4. 编码数据流处理

    // 送入硬件编码器‌
    avcodec_send_frame(codec_ctx, hw_frame);// 接收编码后的数据包
    AVPacket *pkt = av_packet_alloc();
    while (avcodec_receive_packet(codec_ctx, pkt) >= 0) {av_interleaved_write_frame(output_ctx, pkt);  // 写入输出文件‌av_packet_unref(pkt);
    }

二、跨平台适配示例

硬件平台编码器名称像素格式初始化函数
NVIDIA GPUh264_nvencAV_PIX_FMT_CUDAav_hwdevice_ctx_create(..., AV_HWDEVICE_TYPE_CUDA)
Intel QSVh264_qsvAV_PIX_FMT_QSVav_hwdevice_ctx_create(..., AV_HWDEVICE_TYPE_QSV)
AMD AMFh264_amfAV_PIX_FMT_D3D11av_hwdevice_ctx_create(..., AV_HWDEVICE_TYPE_D3D11VA)

三、关键问题解决

  1. 编码器初始化失败

    • 检查FFmpeg编译时是否启用对应硬件加速选项(如--enable-nvenc--enable-libmfx)‌
    • 确认硬件驱动版本与FFmpeg兼容性‌
  2. CPU-GPU内存拷贝开销优化

    // 使用hwupload滤镜直接上传数据至GPU‌
    AVFilterContext *upload_filter;
    const AVFilter *hwupload = avfilter_get_by_name("hwupload");
    avfilter_graph_create_filter(&upload_filter, hwupload, "upload", NULL, NULL, filter_graph);

 ‌四、资源释放

av_buffer_unref(&hw_device_ctx);  // 释放硬件设备上下文‌
avcodec_free_context(&codec_ctx);  // 释放编码器上下文
av_frame_free(&hw_frame);          // 释放硬件帧
av_packet_free(&pkt);              // 释放数据包

五、编译依赖

  • NVIDIA平台‌:需安装CUDA Toolkit,编译时添加--enable-cuda --enable-nvenc
  • Intel平台‌:需安装Intel Media SDK,编译时添加--enable-libmfx

六、硬件编码示例代码

支持从本地YUV文件读取数据、GPU加速编码并输出H.264视频流到MP4文件。

#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/imgutils.h>
#include <libavutil/hwcontext.h>
#include <libswscale/swscale.h>#define INPUT_FILE "input.yuv"
#define OUTPUT_FILE "output.mp4"
#define WIDTH      1280
#define HEIGHT     720
#define FRAME_RATE 30int main() {AVFormatContext *fmt_ctx = NULL;AVCodecContext *enc_ctx = NULL;AVBufferRef *hw_device_ctx = NULL;SwsContext *sws_ctx = NULL;int ret = 0;// 1. 初始化硬件设备上下文ret = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_CUDA, NULL, NULL, 0);  // ‌:ml-citation{ref="1,3" data="citationList"}if (ret < 0) {fprintf(stderr, "Failed to create CUDA device\n");goto cleanup;}// 2. 打开输出文件并配置封装格式avformat_alloc_output_context2(&fmt_ctx, NULL, NULL, OUTPUT_FILE);  // ‌:ml-citation{ref="8" data="citationList"}if (!fmt_ctx) {fprintf(stderr, "Failed to create output context\n");ret = -1;goto cleanup;}// 3. 查找并配置硬件编码器const AVCodec *encoder = avcodec_find_encoder_by_name("h264_nvenc");  // ‌:ml-citation{ref="1,4" data="citationList"}if (!encoder) {fprintf(stderr, "NVENC encoder not found\n");ret = -1;goto cleanup;}enc_ctx = avcodec_alloc_context3(encoder);enc_ctx->width = WIDTH;enc_ctx->height = HEIGHT;enc_ctx->time_base = (AVRational){1, FRAME_RATE};enc_ctx->pix_fmt = AV_PIX_FMT_CUDA;       // 硬件像素格式 ‌:ml-citation{ref="3,5" data="citationList"}enc_ctx->bit_rate = 4000000;              // 4Mbps码率 ‌:ml-citation{ref="5,8" data="citationList"}enc_ctx->hw_device_ctx = av_buffer_ref(hw_device_ctx);  // 绑定设备 ‌:ml-citation{ref="3,6" data="citationList"}// 4. 打开编码器并添加视频流if ((ret = avcodec_open2(enc_ctx, encoder, NULL)) < 0) {  // ‌:ml-citation{ref="4" data="citationList"}fprintf(stderr, "Failed to open encoder\n");goto cleanup;}AVStream *stream = avformat_new_stream(fmt_ctx, NULL);avcodec_parameters_from_context(stream->codecpar, enc_ctx);// 5. 打开输出文件并写入头信息if (!(fmt_ctx->oformat->flags & AVFMT_NOFILE)) {ret = avio_open(&fmt_ctx->pb, OUTPUT_FILE, AVIO_FLAG_WRITE);  // ‌:ml-citation{ref="8" data="citationList"}if (ret < 0) {fprintf(stderr, "Failed to open output file\n");goto cleanup;}}avformat_write_header(fmt_ctx, NULL);  // ‌:ml-citation{ref="8" data="citationList"}// 6. 初始化YUV到CUDA格式转换器sws_ctx = sws_getContext(WIDTH, HEIGHT, AV_PIX_FMT_YUV420P,WIDTH, HEIGHT, AV_PIX_FMT_CUDA,SWS_BILINEAR, NULL, NULL, NULL);  // ‌:ml-citation{ref="4,7" data="citationList"}// 7. 准备输入帧和硬件帧AVFrame *yuv_frame = av_frame_alloc();yuv_frame->width = WIDTH;yuv_frame->height = HEIGHT;yuv_frame->format = AV_PIX_FMT_YUV420P;av_frame_get_buffer(yuv_frame, 0);AVFrame *hw_frame = av_frame_alloc();hw_frame->format = enc_ctx->pix_fmt;hw_frame->width = WIDTH;hw_frame->height = HEIGHT;av_hwframe_get_buffer(enc_ctx->hw_frames_ctx, hw_frame, 0);  // ‌:ml-citation{ref="3,6" data="citationList"}// 8. 处理每一帧数据FILE *yuv_file = fopen(INPUT_FILE, "rb");AVPacket *pkt = av_packet_alloc();for (int i = 0; i < 100; i++) {  // 编码100帧测试// 从YUV文件读取数据fread(yuv_frame->data, 1, WIDTH*HEIGHT, yuv_file);          // Y分量fread(yuv_frame->data‌:ml-citation{ref="1" data="citationList"}, 1, (WIDTH/2)*(HEIGHT/2), yuv_file); // U分量fread(yuv_frame->data‌:ml-citation{ref="2" data="citationList"}, 1, (WIDTH/2)*(HEIGHT/2), yuv_file); // V分量// 转换到硬件帧sws_scale(sws_ctx, (const uint8_t**)yuv_frame->data, yuv_frame->linesize,0, HEIGHT, hw_frame->data, hw_frame->linesize);  // ‌:ml-citation{ref="4,7" data="citationList"}// 编码并写入文件avcodec_send_frame(enc_ctx, hw_frame);while (avcodec_receive_packet(enc_ctx, pkt) >= 0) {av_packet_rescale_ts(pkt, enc_ctx->time_base, stream->time_base);av_interleaved_write_frame(fmt_ctx, pkt);  // ‌:ml-citation{ref="8" data="citationList"}av_packet_unref(pkt);}}cleanup:// 9. 释放所有资源if (yuv_file) fclose(yuv_file);if (sws_ctx) sws_freeContext(sws_ctx);av_packet_free(&pkt);av_frame_free(&yuv_frame);av_frame_free(&hw_frame);if (fmt_ctx) avformat_free_context(fmt_ctx);avcodec_free_context(&enc_ctx);av_buffer_unref(&hw_device_ctx);return ret;
}

关键代码说明:‌

组件功能说明依赖项
hw_device_ctx管理CUDA设备上下文,用于GPU内存分配和硬件加速操作CUDA驱动和NVENC支持 ‌
sws_ctx将CPU端的YUV420P数据转换为GPU端的CUDA格式(如NV12)libswscale库 
av_hwframe_get_buffer直接从GPU显存分配帧内存,避免CPU-GPU内存拷贝FFmpeg硬件帧支持
avcodec_send_frame将硬件帧送入编码器队列,触发异步编码操作编码器线程模型

http://www.ppmy.cn/news/1581222.html

相关文章

使用Three.js渲染器创建炫酷3D场景

引言 在当今数字化的时代&#xff0c;3D图形技术正以其独特的魅力在各个领域掀起波澜。从影视制作到游戏开发&#xff0c;从虚拟现实到网页交互&#xff0c;3D场景以其强烈的视觉冲击力和沉浸式的体验&#xff0c;成为了吸引用户、传达信息的重要手段。而Three.js&#xff0c;…

Walrus 基金会完成 1.4 亿美元融资,由 Standard Crypto 领投

Walrus 基金会 — — 致力于推动 Walrus 发展与增长的组织&#xff0c;3 月 20 日宣布完成 1.4 亿美元的私募。 本轮融资由 Standard Crypto 领投&#xff0c;a16z crypto、Electric Capital、Lvna Capital、Protagonist、Franklin Templeton Digital Assets、Karatage、RW3 …

音视频学习(二十九):YUV与RGB

YUV 和 RGB 是两种不同的颜色表示方式&#xff0c;主要用于图像和视频处理。 1. RGB 颜色空间 RGB&#xff08;Red, Green, Blue&#xff09;是基于加色原理的颜色空间&#xff0c;每种颜色由红、绿、蓝三种基本色按不同的比例混合得到&#xff0c;常用于显示设备&#xff08…

荣耀手机卸载应用商店、快应用中心等系统自带的

1.下载abd ADB Download - Get the latest version of ADB and fastboot 2.手机打开开发者选项 3.手机接电脑打开USB调试 4.下载MT管理器查看系统包名 D:\1.LFD\ADB\platform-tools-latest-windows\platform-tools>adb shell adb.exe: no devices/emulators found 这边是…

k8s中的通信与调度

一 k8s网络通信 1.1 k8s通信整体架构 k8s通过CNI接口接入其他插件来实现网络通讯。目前比较流行的插件有flannel&#xff0c;calico等 CNI插件存放位置&#xff1a;# cat /etc/cni/net.d/10-flannel.conflist 插件使用的解决方案如下 虚拟网桥&#xff0c;虚拟网卡&#xff…

Linux并发程序设计(5):线程的相关操作

目录 进程与线程的对比 1、地址 2、开销 3、并发性 一、线程创建 二、线程结束 三、线程查看tid 四、线程回收 五、线程分离 1、使用pthread_detach 2、在创建线程时指定分离属性 六、取消线程 七、清理线程 进程与线程的对比 1、地址 进程是资源分配的最小单位…

基于Matlab实现语音识别算法(源码+数据)

语音识别技术是现代信息技术中的一个重要领域&#xff0c;特别是在人机交互、智能设备、智能家居、自动驾驶等多个领域有着广泛应用。MATLAB作为一种强大的数值计算和数据可视化环境&#xff0c;因其易用性和丰富的库支持&#xff0c;常被用来实现复杂的算法&#xff0c;包括语…

JVM常用概念之压缩引用

问题 什么是压缩的 oop/引用&#xff1f;压缩引用存在什么问题? 基础知识 Java 规范并未规定数据类型的存储大小。即使对于原始数据类型&#xff0c;它也只规定了原始类型应明确支持的范围及其操作行为&#xff0c;而没有规定实际的存储大小。例如&#xff0c;在某些实现中…