FFmpeg之AVHWAccel

这也是ffmpeg解码器中比较重要的一个模块,很多人认识它应该是通过一条命令

ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i input.mp4 -c:v h264_nvenc -b:v 5M output.mp4

命令地址:英伟达ffmpeg

大家可能觉得这就是nvcodec了,后来发现专门还有一个cuvid呢?瞬间不开心了,不知道这两个倒地时什么关系?如果你时看这个问题的,恭喜你来对了。
下面我们就说道说道。
ffmpeg是通过解码起家,所以它内部有很多自己写的软解码器,在这些软解码器的解码过程当中,比如说对于码流中的某些反量化,反变换等操作,把这些操作挪到一块硬件上,这块硬件就是加速设备。
我们可以看看ffmpeg的h264解码器,红框内部的都是h264解码器的加速插件,NVDEC只是其中之一。
在这里插入图片描述
再来看看代码


static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size)
{AVCodecContext *const avctx = h->avctx;...switch (nal->type) {case H264_NAL_IDR_SLICE:...if (h->avctx->hwaccel &&//这里就进入了加速解码的分支(ret = h->avctx->hwaccel->start_frame(h->avctx, buf, buf_size)) < 0)goto end;}...max_slice_ctx = avctx->hwaccel ? 1 : h->nb_slice_ctx;if (h->nb_slice_ctx_queued == max_slice_ctx) {//这里进入加速解码的分支中if (h->avctx->hwaccel) {ret = avctx->hwaccel->decode_slice(avctx, nal->raw_data, nal->raw_size);h->nb_slice_ctx_queued = 0;} elseret = ff_h264_execute_decode_slices(h);if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))goto end;}break;...

这就是hwaccel加速解码的原理,它是在ffmpeg软解码的基础上将一些特定算法加载到硬件上去做。而cuvid是通过ffmpeg第三方插件库的方式去实现的。

可以看看AVHWAccel结构体定义,发现有几个关键的函数指针,这些指针就是你需要去实现的。
可能你又要问了,为什么英伟达解码器要实现两套?
这个问题留给你吧。或者在我博客里面找,一定有的。


/*** @defgroup lavc_hwaccel AVHWAccel** @note  Nothing in this structure should be accessed by the user.  At some*        point in future it will not be externally visible at all.** @{*/
typedef struct AVHWAccel {/*** Name of the hardware accelerated codec.* The name is globally unique among encoders and among decoders (but an* encoder and a decoder can share the same name).*/const char *name;/*** Type of codec implemented by the hardware accelerator.** See AVMEDIA_TYPE_xxx*/enum AVMediaType type;/*** Codec implemented by the hardware accelerator.** See AV_CODEC_ID_xxx*/enum AVCodecID id;/*** Supported pixel format.** Only hardware accelerated formats are supported here.*/enum AVPixelFormat pix_fmt;/*** Hardware accelerated codec capabilities.* see AV_HWACCEL_CODEC_CAP_**/int capabilities;/****************************************************************** No fields below this line are part of the public API. They* may not be used outside of libavcodec and can be changed and* removed at will.* New public fields should be added right above.******************************************************************//*** Allocate a custom buffer*/int (*alloc_frame)(AVCodecContext *avctx, AVFrame *frame);/*** Called at the beginning of each frame or field picture.** Meaningful frame information (codec specific) is guaranteed to* be parsed at this point. This function is mandatory.** Note that buf can be NULL along with buf_size set to 0.* Otherwise, this means the whole frame is available at this point.** @param avctx the codec context* @param buf the frame data buffer base* @param buf_size the size of the frame in bytes* @return zero if successful, a negative value otherwise*/int (*start_frame)(AVCodecContext *avctx, const uint8_t *buf, uint32_t buf_size);/*** Callback for parameter data (SPS/PPS/VPS etc).** Useful for hardware decoders which keep persistent state about the* video parameters, and need to receive any changes to update that state.** @param avctx the codec context* @param type the nal unit type* @param buf the nal unit data buffer* @param buf_size the size of the nal unit in bytes* @return zero if successful, a negative value otherwise*/int (*decode_params)(AVCodecContext *avctx, int type, const uint8_t *buf, uint32_t buf_size);/*** Callback for each slice.** Meaningful slice information (codec specific) is guaranteed to* be parsed at this point. This function is mandatory.* The only exception is XvMC, that works on MB level.** @param avctx the codec context* @param buf the slice data buffer base* @param buf_size the size of the slice in bytes* @return zero if successful, a negative value otherwise*/int (*decode_slice)(AVCodecContext *avctx, const uint8_t *buf, uint32_t buf_size);/*** Called at the end of each frame or field picture.** The whole picture is parsed at this point and can now be sent* to the hardware accelerator. This function is mandatory.** @param avctx the codec context* @return zero if successful, a negative value otherwise*/int (*end_frame)(AVCodecContext *avctx);/*** Size of per-frame hardware accelerator private data.** Private data is allocated with av_mallocz() before* AVCodecContext.get_buffer() and deallocated after* AVCodecContext.release_buffer().*/int frame_priv_data_size;/*** Called for every Macroblock in a slice.** XvMC uses it to replace the ff_mpv_reconstruct_mb().* Instead of decoding to raw picture, MB parameters are* stored in an array provided by the video driver.** @param s the mpeg context*/void (*decode_mb)(struct MpegEncContext *s);/*** Initialize the hwaccel private data.** This will be called from ff_get_format(), after hwaccel and* hwaccel_context are set and the hwaccel private data in AVCodecInternal* is allocated.*/int (*init)(AVCodecContext *avctx);/*** Uninitialize the hwaccel private data.** This will be called from get_format() or avcodec_close(), after hwaccel* and hwaccel_context are already uninitialized.*/int (*uninit)(AVCodecContext *avctx);/*** Size of the private data to allocate in* AVCodecInternal.hwaccel_priv_data.*/int priv_data_size;/*** Internal hwaccel capabilities.*/int caps_internal;/*** Fill the given hw_frames context with current codec parameters. Called* from get_format. Refer to avcodec_get_hw_frames_parameters() for* details.** This CAN be called before AVHWAccel.init is called, and you must assume* that avctx->hwaccel_priv_data is invalid.*/int (*frame_params)(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx);
} AVHWAccel;

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/216019.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

PS不按比例裁剪图片

点击左侧的裁剪工具后&#xff0c;然后点击底部工具栏中的清除选项。

关于论坛中 Edge 问题解决教程汇总

常见问题 使用 Microsoft Edge 浏览器时&#xff0c;可能会遇到一些具体的问题&#xff0c;以下是一些常见问题及可能的解决方法&#xff1a; 1. 页面加载缓慢&#xff1a; 解决方法&#xff1a; 检查网络连接&#xff1a;确保网络连接稳定。清除浏览器缓存&#xff1a;打开…

【docker】容器使用(Nginx 示例)

查看 Docker 客户端命令选项 docker上面这三张图都是 常用命令&#xff1a; run 从映像创建并运行新容器exec 在运行的容器中执行命令ps 列出容器build 从Dockerfile构建映像pull 从注册表下载图像push 将图像上载到注册表…

【51单片机】俄罗斯方块游戏-LED点阵

文章目录 一、功能简介二、软件设计三、实验现象联系作者 一、功能简介 本项目使用51单片机控制器&#xff0c;使88LED点阵&#xff0c;按键等。 主要功能&#xff1a; 系统运行后&#xff0c;88LED点阵显示游戏界面&#xff0c;K1和K2键控制左右移动&#xff0c;K3和K4键控制…

【产品经理】产品的实现,需要做好战略规划

产品的实现需要做好产品规划&#xff0c;而产品的规划决定了产品的方向。本文从战略规划的重要性、产品定位、设计产品架构图三个方向&#xff0c;详细地为大家梳理了产品实现的前期准备。 我们知晓了如何去发掘问题&#xff0c;并找到解决方案。 可对于问题的处理&#xff0c…

Qt Desktop Widgets 控件绘图原理逐步分析拆解

Qt 是目前C语言首选的框架库。之所以称为框架库而不单单是GUI库&#xff0c;是因为Qt提供了远远超过GUI的功能封装&#xff0c;即使不使用GUI的后台服务&#xff0c;也可以用Qt大大提高跨平台的能力。 仅就界面来说&#xff0c;Qt 保持各个平台绘图等效果的统一&#xff0c;并…

diffusers pipeline拆解:理解pipelines、models和schedulers

diffusers pipeline拆解&#xff1a;理解pipelines、models和schedulers 翻译自&#xff1a;https://huggingface.co/docs/diffusers/using-diffusers/write_own_pipeline v0.24.0 diffusers 设计初衷就是作为一个简单且易用的工具包&#xff0c;来帮助你在自己的使用场景中构建…

将List<Map<String,Object>>转为List<Object>

经常在开发中会需要将List<Map<String&#xff0c;Object>>转为List&#xff0c;Object也就是你自己对应的目标对象&#xff0c;因为经常要用&#xff0c;干脆就自己封装了一个&#xff0c;代码示例如下&#xff1a; 假设有一个类&#xff1a;Animal.java public …

并发包原子类详解

原子类型是一种无锁的、线程安全的、使用基本数据类型和引用数据类型的线程安全解决方案。 CAS算法&#xff1a;CAS包含3个操作数&#xff0c;分别是内存值V、预期值A、要修改的新值B。当且仅当预期值A与内存值V相等时&#xff0c;将内存值V修改为B&#xff0c;否则什么都不需要…

ftp传海量文件会卡?跨境数据传输推荐使用FTP吗?

企业在传输大量文件时&#xff0c;经常会遇到FTP卡顿的问题&#xff0c;尽管采取多种方式仍无法完美解决&#xff0c;尤其是在跨境数据传输方面。对于紧急项目而言&#xff0c;文件数据无法及时同步可能导致任务无法按时完成。在传输速度方面&#xff0c;甚至可能出现每秒几KB的…

免费好用的API精选推荐

快递物流订阅与推送&#xff08;含物流轨迹&#xff09;&#xff1a;【物流订阅与推送、H5物流轨迹、单号识别】支持单号的订阅与推送&#xff0c;订阅国内物流信息&#xff0c;当信息有变化时&#xff0c;推送到您的回调地址。地图轨迹支持在地图中展示包裹运输轨迹。包括顺丰…

Jmeter入门

一、下载jmeter 官网下载 下载之后解压&#xff0c;在目录/bin下面找到jmeter.bat双击之后即可启动Jmeter。 二、使用 如下左图&#xff0c;选择语言为中文&#xff0c;可以修改测试计划的名称。如下右图&#xff0c;添加线程组 添加线程组 添加http请求 路径传参方式 …

Morphisec革命:利用移动目标防御增强Windows安全性

来源&#xff1a;艾特保IT 虹科分享 | Morphisec革命&#xff1a;利用移动目标防御增强Windows安全性 原文链接&#xff1a;虹科分享 | Morphisec革命&#xff1a;利用移动目标防御增强Windows安全性 欢迎关注虹科&#xff0c;为您提供最新资讯&#xff01; Windows 10安全工…

esp32cam的与安卓的udp服务视频传输

esp32cam /* 下载程序 按住接口板上的IO0 在程序上传的时候 按一下 开发板上的rst按钮 待程序开始上传 在松开 IO0 brownout detector was triggered报错 触发了断电探测器&#xff0c;估计是供电环境本来就不稳定 屏蔽 #include "soc/soc.h" #include "so…

js中分号产生的问题详解,第一次出现分号导致的问题的记录

图示: 现在 这段代码本来是两行,但是格式化后注意下面一行缩进了,代表按一行解析了, 结果: 加上分号后再格式化就自动对齐了,代表按两行解析. 要是按照没有分号进行解析是怎样的? GPT回答: 这段代码是一行 JavaScript 代码&#xff0c;涉及到了 JSON 对象、条件语句和跳转页面…

python python输入位置的坐标(即经纬度),计算两点的距离结果保留两位

以下是Python代码实现&#xff1a; from math import radians, sin, cos, sqrtdef distance(lat1, lon1, lat2, lon2):R 6371 # 地球平均半径&#xff0c;单位为公里d_lat radians(lat2 - lat1)d_lon radians(lon2 - lon1)lat1 radians(lat1)lat2 radians(lat2)a sin(d…

非法窃取、下载、打印公司商业秘密但未利用,构成犯罪吗?

公司的电子邮箱内往往存储了大量商业秘密&#xff0c;而商业秘密又是公司生存的根本。未经许可登录他人电子邮箱&#xff0c;并窃取、下载、兜售或以其他目的&#xff0c;泄露商业秘密属违法行为&#xff0c;但没有利用这些数据也构成犯罪吗&#xff1f; 案件 武汉一家技术公…

业务中台解释

业务中台是一个组织管理名词&#xff0c;也被称为有形的中台&#xff0c;因为是有实体部门存在的。业务中台多半是传统的成本中心&#xff0c;把后台的资源整合成前台打仗需要的“中间件”&#xff0c;方便被随需调用。典型的业务中台如字节跳动的直播中台、腾讯的技术中台等。…

图形化编程学习攻略:从新手到专家的指南

在编程世界中&#xff0c;图形化编程已经成为越来越多初学者的首选。它通过直观的图形化界面&#xff0c;让编程变得更加简单和有趣。6547网将为你提供一份全面的图形化编程攻略&#xff0c;帮助你从新手成为专家。 一、选择合适的图形化编程工具 Scratch&#xff1a;适合儿童…

深入理解RBAC权限系统

最近&#xff0c;一位朋友在面试中被问及如何设计一个权限系统。我们注意到目前许多后台管理系统&#xff08;包括一些热门的如若依快速开发平台&#xff09;都采用了RBAC访问控制策略。该策略通过将权限授予角色&#xff0c;然后将角色分配给用户&#xff0c;从而实现对系统资…