FFmpeg-基础组件-AVBufferPool

FFmpeg实现了一个AVBufferPool ,这个pool可以用来提前做些内存分配等,在ffmpeg cuvid插件中hwcontext_cuda.c文件夹中可以看到这个Pool的用法。

下面是关键结构体的定义,可以看到几个比较重要的函数指针,比如:

void (*free)(void *opaque, uint8_t *data);

这个是用来释放AVBuffer 中的data数据的,可以由用户来指定。


/*** The buffer was av_realloc()ed, so it is reallocatable.*/
#define BUFFER_FLAG_REALLOCATABLE (1 << 0)struct AVBuffer {uint8_t *data; /**< data described by this buffer */buffer_size_t size; /**< size of data in bytes *//***  number of existing AVBufferRef instances referring to this buffer*/atomic_uint refcount;/*** a callback for freeing the data*/void (*free)(void *opaque, uint8_t *data);/*** an opaque pointer, to be used by the freeing callback*/void *opaque;/*** A combination of AV_BUFFER_FLAG_**/int flags;/*** A combination of BUFFER_FLAG_**/int flags_internal;
};

下面是Pool的元素,BufferPoolEntry,可以看到一个next指针,其实就是一个单向链表。
其中free用来释放buffer


typedef struct BufferPoolEntry {uint8_t *data;/** Backups of the original opaque/free of the AVBuffer corresponding to* data. They will be used to free the buffer when the pool is freed.*/void *opaque;void (*free)(void *opaque, uint8_t *data);AVBufferPool *pool;struct BufferPoolEntry *next;
} BufferPoolEntry;

下面是一个bufferPoll的定义,其中有一个refcount,作为ref来使用,另外有两个alloc函数和pool_free


struct AVBufferPool {AVMutex mutex;BufferPoolEntry *pool;/** This is used to track when the pool is to be freed.* The pointer to the pool itself held by the caller is considered to* be one reference. Each buffer requested by the caller increases refcount* by one, returning the buffer to the pool decreases it by one.* refcount reaches zero when the buffer has been uninited AND all the* buffers have been released, then it's safe to free the pool and all* the buffers in it.*/atomic_uint refcount;buffer_size_t size;void *opaque;AVBufferRef* (*alloc)(buffer_size_t size);AVBufferRef* (*alloc2)(void *opaque, buffer_size_t size);void         (*pool_free)(void *opaque);
};

下面的注释,其实已经说明了该怎么去使用buffer poll。

/*** @defgroup lavu_bufferpool AVBufferPool* @ingroup lavu_data** @{* AVBufferPool is an API for a lock-free thread-safe pool of AVBuffers.** Frequently allocating and freeing large buffers may be slow. AVBufferPool is* meant to solve this in cases when the caller needs a set of buffers of the* same size (the most obvious use case being buffers for raw video or audio* frames).** At the beginning, the user must call av_buffer_pool_init() to create the* buffer pool. Then whenever a buffer is needed, call av_buffer_pool_get() to* get a reference to a new buffer, similar to av_buffer_alloc(). This new* reference works in all aspects the same way as the one created by* av_buffer_alloc(). However, when the last reference to this buffer is* unreferenced, it is returned to the pool instead of being freed and will be* reused for subsequent av_buffer_pool_get() calls.** When the caller is done with the pool and no longer needs to allocate any new* buffers, av_buffer_pool_uninit() must be called to mark the pool as freeable.* Once all the buffers are released, it will automatically be freed.** Allocating and releasing buffers with this API is thread-safe as long as* either the default alloc callback is used, or the user-supplied one is* thread-safe.*/
/*** The buffer pool. This structure is opaque and not meant to be accessed* directly. It is allocated with av_buffer_pool_init() and freed with* av_buffer_pool_uninit().*/
typedef struct AVBufferPool AVBufferPool;/*** Allocate and initialize a buffer pool.** @param size size of each buffer in this pool* @param alloc a function that will be used to allocate new buffers when the* pool is empty. May be NULL, then the default allocator will be used* (av_buffer_alloc()).* @return newly created buffer pool on success, NULL on error.*/
#if FF_API_BUFFER_SIZE_T
AVBufferPool *av_buffer_pool_init(int size, AVBufferRef* (*alloc)(int size));
#else
AVBufferPool *av_buffer_pool_init(size_t size, AVBufferRef* (*alloc)(size_t size));
#endif/*** Allocate and initialize a buffer pool with a more complex allocator.** @param size size of each buffer in this pool* @param opaque arbitrary user data used by the allocator* @param alloc a function that will be used to allocate new buffers when the*              pool is empty. May be NULL, then the default allocator will be*              used (av_buffer_alloc()).* @param pool_free a function that will be called immediately before the pool*                  is freed. I.e. after av_buffer_pool_uninit() is called*                  by the caller and all the frames are returned to the pool*                  and freed. It is intended to uninitialize the user opaque*                  data. May be NULL.* @return newly created buffer pool on success, NULL on error.*/
#if FF_API_BUFFER_SIZE_T
AVBufferPool *av_buffer_pool_init2(int size, void *opaque,AVBufferRef* (*alloc)(void *opaque, int size),
#else
AVBufferPool *av_buffer_pool_init2(size_t size, void *opaque,AVBufferRef* (*alloc)(void *opaque, size_t size),
#endifvoid (*pool_free)(void *opaque));/*** Mark the pool as being available for freeing. It will actually be freed only* once all the allocated buffers associated with the pool are released. Thus it* is safe to call this function while some of the allocated buffers are still* in use.** @param pool pointer to the pool to be freed. It will be set to NULL.*/
void av_buffer_pool_uninit(AVBufferPool **pool);/*** Allocate a new AVBuffer, reusing an old buffer from the pool when available.* This function may be called simultaneously from multiple threads.** @return a reference to the new buffer on success, NULL on error.*/
AVBufferRef *av_buffer_pool_get(AVBufferPool *pool);/*** Query the original opaque parameter of an allocated buffer in the pool.** @param ref a buffer reference to a buffer returned by av_buffer_pool_get.* @return the opaque parameter set by the buffer allocator function of the*         buffer pool.** @note the opaque parameter of ref is used by the buffer pool implementation,* therefore you have to use this function to access the original opaque* parameter of an allocated buffer.*/
void *av_buffer_pool_buffer_get_opaque(AVBufferRef *ref);

使用的流程就是上面几个函数。

文章开头也说了,hwcontext_cuda.c中使用AVBufferPool,其实是AVHWFramesContext 里面有一个bufferpool.它的使用。

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

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

相关文章

数据结构:栈(Stack)的各种操作(入栈,出栈,判断栈非空,判断栈已满,附源码)

前言&#xff1a;在前面的文章中&#xff0c;我们讲解了顺序表&#xff0c;单链表&#xff0c;双向链表。而我们今天要分享的栈则是基于之前的数据结构上搭建的&#xff0c;但是相较于顺序表和链表来说&#xff0c;栈的实现就非常简单了。 目录 一.栈(Stack)的概念 二.栈的数…

算法设计复习题

一、选择 1.算法要对异常情况进行适当的处理&#xff0c;就是算法的&#xff08;&#xff09;。 A、正确性 B、可用性 C、健壮性 D、可行性 2.&#xff08; &#xff09;指的是算法中描述的操作都可以通过已经实现的基本操作运算有限次实现。 A、可靠性 B、正确性 C、有效性 …

一些好的写法

1.多状态返回 <text class"card-left-tag-text" >{{ {0: 未交班, 1: 未接班, 2: 已完成, 3: 已延期}[items.turnoverType] }} </text> 2.年月拼接&#xff0c;pre,cur,next let date new Date();let year date.getUTCFullYear()let month date.getU…

windows下docker环境安装

开启硬件虚拟化技术 win10中开启 Hyper-V Win10 下是否开启硬件虚拟化技术&#xff0c;在控制面板&#xff0c;启用 window 功能&#xff0c;找到 Hyper-V 选项&#xff0c;点勾选确认。如图&#xff1a; Windows 11 家庭中文版新增 Hyper-V选项 注意以下的解决方案来自win1…

FFmpeg之AVHWAccel

这也是ffmpeg解码器中比较重要的一个模块&#xff0c;很多人认识它应该是通过一条命令 ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i input.mp4 -c:v h264_nvenc -b:v 5M output.mp4命令地址&#xff1a;英伟达ffmpeg 大家可能觉得这就是nvcodec了&#xff0c;后来发…

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 对象、条件语句和跳转页面…