热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

FFmpeg总结(四)AV系列结构体之AVBuffer、AVBufferRef、AVBufferPool

位置:libavutilbuffer.hAVBuffer采用引用计数的数据Buffer的API。有两个核心对象这个API——AVBuffer和AVBufferRef。

位置:libavutil/buffer.h



这里写图片描述



AVBuffer采用引用计数的数据Buffer的API。

有两个核心对象这个API——AVBuffer和AVBufferRef。

AVBuffer代表数据缓冲区本身,它是私有的,不能直接被调用者调用。我们可以通过AVBufferRef,调用者须要检查两个AVBuffer指针是否指向两个不同的引用在同一数据buffer中。AVBufferRef 代表一个单个引用指向AVBuffer,调用者可以直接调它。

有两个功能函数提供创建一个AVBuffer对象在一个单引用中,av_buffer_alloc()分配置一个新buffer空间。av_buffer_create()负责包装已存在数组的AVBuffer对象。从一个已存在引用,其他的引用将创建通过av_buffer_ref()方法。
使用av_buffer_unref(),交释放这个引用(包含数据引用及计数引用)。

在已经存在的AVBuffer引用中,FFmpeg认为AVBuffer是可写入相关数据的。(不会被标识只读),av_buffer_is_writable() 函数提供是否可write的功能,如果不能将自动创建一个新的可写的buffer。

typedef struct AVBuffer AVBuffer;/*** A reference to a data buffer.** The size of this struct is not a part of the public ABI and it is not meant* to be allocated directly.*/
typedef struct AVBufferRef {AVBuffer *buffer;/*** The data buffer. It is considered writable if and only if* this is the only reference to the buffer, in which case* av_buffer_is_writable() returns 1.*/uint8_t *data;/*** Size of data in bytes.*/int size;
} AVBufferRef;/*** Allocate an AVBuffer of the given size using av_malloc().** @return an AVBufferRef of given size or NULL when out of memory*/
AVBufferRef *av_buffer_alloc(int size);/*** Same as av_buffer_alloc(), except the returned buffer will be initialized* to zero.*/
AVBufferRef *av_buffer_allocz(int size);/*** Always treat the buffer as read-only, even when it has only one* reference.*/
#define AV_BUFFER_FLAG_READONLY (1 <<0)/*** Create an AVBuffer from an existing array.** If this function is successful, data is owned by the AVBuffer. The caller may* only access data through the returned AVBufferRef and references derived from* it.* If this function fails, data is left untouched.* &#64;param data data array* &#64;param size size of data in bytes* &#64;param free a callback for freeing this buffer&#39;s data* &#64;param opaque parameter to be got for processing or passed to free* &#64;param flags a combination of AV_BUFFER_FLAG_*** &#64;return an AVBufferRef referring to data on success, NULL on failure.*/
AVBufferRef *av_buffer_create(uint8_t *data, int size,void (*free)(void *opaque, uint8_t *data),void *opaque, int flags);/*** Default free callback, which calls av_free() on the buffer data.* This function is meant to be passed to av_buffer_create(), not called* directly.*/
void av_buffer_default_free(void *opaque, uint8_t *data);/*** Create a new reference to an AVBuffer.** &#64;return a new AVBufferRef referring to the same AVBuffer as buf or NULL on* failure.*/
AVBufferRef *av_buffer_ref(AVBufferRef *buf);/*** Free a given reference and automatically free the buffer if there are no more* references to it.** &#64;param buf the reference to be freed. The pointer is set to NULL on return.*/
void av_buffer_unref(AVBufferRef **buf);/*** &#64;return 1 if the caller may write to the data referred to by buf (which is* true if and only if buf is the only reference to the underlying AVBuffer).* Return 0 otherwise.* A positive answer is valid until av_buffer_ref() is called on buf.*/
int av_buffer_is_writable(const AVBufferRef *buf);/*** &#64;return the opaque parameter set by av_buffer_create.*/
void *av_buffer_get_opaque(const AVBufferRef *buf);int av_buffer_get_ref_count(const AVBufferRef *buf);/*** Create a writable reference from a given buffer reference, avoiding data copy* if possible.** &#64;param buf buffer reference to make writable. On success, buf is either left* untouched, or it is unreferenced and a new writable AVBufferRef is* written in its place. On failure, buf is left untouched.* &#64;return 0 on success, a negative AVERROR on failure.*/
int av_buffer_make_writable(AVBufferRef **buf);/*** Reallocate a given buffer.** &#64;param buf a buffer reference to reallocate. On success, buf will be* unreferenced and a new reference with the required size will be* written in its place. On failure buf will be left untouched. *buf* may be NULL, then a new buffer is allocated.* &#64;param size required new buffer size.* &#64;return 0 on success, a negative AVERROR on failure.** &#64;note the buffer is actually reallocated with av_realloc() only if it was* initially allocated through av_buffer_realloc(NULL) and there is only one* reference to it (i.e. the one passed to this function). In all other cases* a new buffer is allocated and the data is copied.*/
int av_buffer_realloc(AVBufferRef **buf, int size);/*** &#64;}*/typedef struct AVBufferPool AVBufferPool;/*** Allocate and initialize a buffer pool.** &#64;param size size of each buffer in this pool* &#64;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()).* &#64;return newly created buffer pool on success, NULL on error.*/
AVBufferPool *av_buffer_pool_init(int size, AVBufferRef* (*alloc)(int size));/*** Allocate and initialize a buffer pool with a more complex allocator.** &#64;param size size of each buffer in this pool* &#64;param opaque arbitrary user data used by the allocator* &#64;param alloc a function that will be used to allocate new buffers when the* pool is empty.* &#64;param pool_free a function that will be called immediately before the pool* is freed. I.e. after av_buffer_pool_can_uninit() is called* by the pool and all the frames are returned to the pool and* freed. It is intended to uninitialize the user opaque data.* &#64;return newly created buffer pool on success, NULL on error.*/
AVBufferPool *av_buffer_pool_init2(int size, void *opaque,AVBufferRef* (*alloc)(void *opaque, int size),void (*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.** &#64;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.** &#64;return a reference to the new buffer on success, NULL on error.*/
AVBufferRef *av_buffer_pool_get(AVBufferPool *pool);/*** &#64;}*/#endif /* AVUTIL_BUFFER_H */

AVBufferPool 是用来管理大buffer时的API

调用者必须使用av_buffer_pool_init()创建一个buffer池&#xff0c;无论什么时候需要&#xff0c;通过av_buffer_pool_get()&#xff0c;得到新buffer的引用&#xff0c;和av_buffer_alloc()非常像&#xff0c;当不被引用时&#xff0c;会重新归还给pool&#xff0c;而不用去像AVBuffer那样释放。当调用者不再分配新的buffer时&#xff0c;av_buffer_pool_uninit() 必须调用&#xff0c;表明这个pool将自动释放了&#xff08;因为这个pool也是一个对象&#xff09;。分配和释放buffer&#xff0c;是线程安全操作&#xff0c;只要保证没有其他默认alloc的callback在使用。这个结构体是私有的&#xff0c;不能直接被外部使用&#xff0c;外部可以用av_buffer_pool_init()&#xff0c;及av_buffer_pool_uninit()函数。


推荐阅读
  • 本文介绍了使用C++Builder实现获取USB优盘序列号的方法,包括相关的代码和说明。通过该方法,可以获取指定盘符的USB优盘序列号,并将其存放在缓冲中。该方法可以在Windows系统中有效地获取USB优盘序列号,并且适用于C++Builder开发环境。 ... [详细]
  • 本文介绍了C#中生成随机数的三种方法,并分析了其中存在的问题。首先介绍了使用Random类生成随机数的默认方法,但在高并发情况下可能会出现重复的情况。接着通过循环生成了一系列随机数,进一步突显了这个问题。文章指出,随机数生成在任何编程语言中都是必备的功能,但Random类生成的随机数并不可靠。最后,提出了需要寻找其他可靠的随机数生成方法的建议。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • C++字符字符串处理及字符集编码方案
    本文介绍了C++中字符字符串处理的问题,并详细解释了字符集编码方案,包括UNICODE、Windows apps采用的UTF-16编码、ASCII、SBCS和DBCS编码方案。同时说明了ANSI C标准和Windows中的字符/字符串数据类型实现。文章还提到了在编译时需要定义UNICODE宏以支持unicode编码,否则将使用windows code page编译。最后,给出了相关的头文件和数据类型定义。 ... [详细]
  • 深入理解Kafka服务端请求队列中请求的处理
    本文深入分析了Kafka服务端请求队列中请求的处理过程,详细介绍了请求的封装和放入请求队列的过程,以及处理请求的线程池的创建和容量设置。通过场景分析、图示说明和源码分析,帮助读者更好地理解Kafka服务端的工作原理。 ... [详细]
  • 本文介绍了Swing组件的用法,重点讲解了图标接口的定义和创建方法。图标接口用来将图标与各种组件相关联,可以是简单的绘画或使用磁盘上的GIF格式图像。文章详细介绍了图标接口的属性和绘制方法,并给出了一个菱形图标的实现示例。该示例可以配置图标的尺寸、颜色和填充状态。 ... [详细]
  • 本文整理了Java面试中常见的问题及相关概念的解析,包括HashMap中为什么重写equals还要重写hashcode、map的分类和常见情况、final关键字的用法、Synchronized和lock的区别、volatile的介绍、Syncronized锁的作用、构造函数和构造函数重载的概念、方法覆盖和方法重载的区别、反射获取和设置对象私有字段的值的方法、通过反射创建对象的方式以及内部类的详解。 ... [详细]
  • 本文讨论了如何使用GStreamer来删除H264格式视频文件中的中间部分,而不需要进行重编码。作者提出了使用gst_element_seek(...)函数来实现这个目标的思路,并提到遇到了一个解决不了的BUG。文章还列举了8个解决方案,希望能够得到更好的思路。 ... [详细]
  • Question该提问来源于开源项目:react-native-device-info/react-native-device-info ... [详细]
  • 开发笔记:图像识别基于主成分分析算法实现人脸二维码识别
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了图像识别基于主成分分析算法实现人脸二维码识别相关的知识,希望对你有一定的参考价值。 ... [详细]
  • java多线程获取线程返回结果
    我们在使用java多线程编写相关业务代码时,往往有这样一种情况,某个线程依赖于其他线程执行结果。也就是说,我们需要在一个线程中获取另一个线程的信息。可以分为两种情况,一种是轮询,一 ... [详细]
  • 【图像边缘检测】基于matlab GUI Sobel+Prewitt+Robert算子图像边缘检测【含Matlab源码 203期】
    一、获取代码方式获取代码方式1:完整代码已上传我的资源:【图像边缘检测】基于matlabGUISobelPrewittRobert算子图像边缘检测【含 ... [详细]
  • 目前正在做毕业设计,一个关于校园服务的app,我会抽取已完成的相关代码写到文章里。一是为了造福这个曾经帮助过我的社区,二是写文章的同时更能巩固相关知识的记忆。一、前言在爬取教务系统 ... [详细]
  • 篇首语:本文由编程笔记#小编为大家整理,主要介绍了利用AndroidCamera2的照相机api实现实时的图像采集与预览相关的知识,希望对你有一定的参考价值。&n ... [详细]
  • matlab串口编程由于项目需要,用matlab做了一个串口通信工具,也碰到不少坑。这里总结一下。读取串口数据matlab支持串口通信,因此直接调用串口的结构体serial就可以, ... [详细]
author-avatar
ze602
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有