当前位置:网站首页>Understanding and mastery of ffmpeg avbufferpool
Understanding and mastery of ffmpeg avbufferpool
2022-06-30 12:50:00 【hjjdebug】
FFMpeg AVBufferPool Understanding and mastery of
Deep understanding of meaning requires debugging code ,
But viewing records can get twice the result with half the effort !
pool The general idea is :
If pool There is no BufferPoolEntry, The new
When memory is released , The actual data is returned to pool in , from pool take BufferPoolEntry link .
If data is retrieved again , If pool There is entry, Then use pool in entry.
call av_buffer_pool_init function
Allocate one AVBufferPool example , Return the instance pointer ,
among pool Of refcount by 1,size For the incoming value , Allocate by this size in the future ,alloc The function defaults to av_buffer_alloc Or the memory allocation function passed in
call av_buffer_pool_get function
here pool->pool It's empty , Send buf, pool->pool yes BufferPoolEntry The pointer .
!buf establish , call pool_alloc_buffer(pool)
Call... In this function av_buffer_alloc(pool->size) Allocate memory , return AVBufferRef The pointer ret. It is also the return value of this function
Then assign another BufferPoolEntry example , Of this instance data Point to the newly requested memory space ,pool Save the incoming AVBufferPool.
The instance pointer is assigned to AVBuffeRef Of ret->buffer->opaque.
ret->buffer->free = pool_release_buffer; This sentence is the key ,AVBuffer free Call the function when
And then put pool->refcount increase 1 Turned into 2.
call av_buffer_unref function
The parameter passed in this case is AVBufferRef The pointer , Its buffer->free yes pool_release_buffer
take AVBuffer And AVBufferRef The memory space corresponding to the structure is released ,
When releasing data , The actual call pool_release_buffer function . Pass in b->opaque,b->data, b yes AVBuffer The pointer
b->opaque( Opaque , Vague ) It is passed in from the front BufferPoolEntry The pointer , From this we can get pool, Will be opaque Add to pool At the top of the list , For subsequent use
pool Of refcount -1, When reduced to 0 when , Whole pool want free. buffer_pool_free(pool).
Call again av_buffer_pool_get function
here pool->pool Not empty , It's from the previous assignment BufferPoolEntry example , Pay to buf, Not null at this time .
And then call av_buffer_create(buf->data,pool->size,pool_rease_buffer,buf,0)
It creates AVBuffer example and AVBufferRef example , then ref.
buf->data Namely AVBuffer Of buffer->data, pool->size Namely buffer->size pool_rease_buffer Namely buffer->free
buf Namely buffer->opaque, 0 yes buffer->flags
In this way, the previously released buf->data.
And then put buf->next Pay to pool->pool, adjustment pool Linked list , buf->next Pay to 0
hold pool->refcount increase 1
Description of other functions : Add comments directly to the function .
void av_buffer_pool_uninit(AVBufferPool **ppool) //uninit It's called flush(), If pool Of refcount=1, Just call free
{
AVBufferPool *pool = *ppool;
buffer_pool_flush(pool);
if (atomic_fetch_sub_explicit(&pool->refcount, 1, memory_order_acq_rel) == 1)
buffer_pool_free(pool);
}
static void buffer_pool_free(AVBufferPool *pool) // First call flush, Destroy again mutex, Yes free call free, Release your structure
{
buffer_pool_flush(pool);
ff_mutex_destroy(&pool->mutex);
if (pool->pool_free)
pool->pool_free(pool->opaque);
av_freep(&pool);
}
static void buffer_pool_flush(AVBufferPool *pool) // If pool Of entry It's true , Then release the entry, View the next item at the same time entry
{
while (pool->pool) {
BufferPoolEntry *buf = pool->pool;
pool->pool = buf->next;
buf->free(buf->opaque, buf->data);
av_freep(&buf);
}
}
Knowledge point :
0. The basis of understanding is to understand AVBuffer The concept of . It's easy to do later .
1、AVBuffer By default, the releaser of is av_buffer_default_free, Use in pool when , This value is pool_release_buffer.
2、 stay AVBuffer Structure defines a opaque( Fuzzy uncertainty ) Variable , And then BufferPoolEntry The pointer is stored here opaque in ,
Calling AVBuffer Of free Function time , Put this opaque Pass as a parameter . Memory reclaim to pool
Pay test code :
#include <QCoreApplication>
#include <iostream>
#include <iomanip>
extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libswresample/swresample.h>
#include <libavutil/replaygain.h>
#include <libavutil/pixdesc.h>
}
using namespace std;
/*
1. av_buffer_pool_init
initialization Memory pool
2 av_buffer_pool_get
Get... From memory pool buffer
3.av_buffer_pool_uninit
Free the memory pool
4.pool_release_buffer
Recycling buffer
*/
const int testAVBufferPoolLoopCount = 3; // Test for memory leaks by modifying the number of cycles
void testAVBufferPool()
{
std::cout << "\n\n--------------- Test testAVBufferPool into -------------" << std::endl;
AVBufferPool *avBufferPool;
AVBufferRef *avBufferRefT;
AVBufferRef *avBufferRef0;
AVBufferRef *avBufferRef1;
AVBufferRef *avBufferRef2;
for(int i = 0; i < testAVBufferPoolLoopCount; i++)
{
avBufferPool = av_buffer_pool_init(1*1024*1024, nullptr);
avBufferRef0 = av_buffer_pool_get(avBufferPool);
std::cout <<"avBufferRef0-hex:" << hex << avBufferRef0 <<std::endl;
std::cout << "avBufferRef0 size = " <<dec << avBufferRef0->size << std::endl;
//memcpy(avBufferRefT, avBufferRef0, sizeof(*avBufferRefT));
avBufferRefT = avBufferRef0;
av_buffer_unref(&avBufferRef0); // Release avBufferRef0
std::cout << "avBufferRefT size = " <<dec << avBufferRefT->size << std::endl;
avBufferRef1 = av_buffer_pool_get(avBufferPool);
avBufferRef2 = av_buffer_pool_get(avBufferPool);
std::cout <<"AVBufferPool" << std::endl;
std::cout <<"avBufferRef0-hex:" << hex << avBufferRef0 <<std::endl;
std::cout <<"avBufferRefT-hex:" << hex << avBufferRefT <<std::endl;
std::cout <<"avBufferRef1-hex:" << hex << avBufferRef1 <<std::endl; // Here you can see the print and avBufferRef0 It is the same when not released , This indicates that there is a recycling mechanism inside the thread pool
std::cout <<"avBufferRef2-hex:" << hex << avBufferRef2 <<std::endl;
if(avBufferRef0)
std::cout << "av_buffer_get_ref_count(avBufferRef0) = " << av_buffer_get_ref_count(avBufferRef0) << std::endl;
if(avBufferRef1)
std::cout << "av_buffer_get_ref_count(avBufferRef1) = " << av_buffer_get_ref_count(avBufferRef1) << std::endl;
avBufferRef0 = av_buffer_pool_get(avBufferPool);
av_buffer_pool_uninit(&avBufferPool);
av_buffer_unref(&avBufferRef0); // Test for memory leaks by commenting on this section
av_buffer_unref(&avBufferRef1);
av_buffer_unref(&avBufferRef2);
}
std::cout << "\n--------------- Test testAVBufferPool leave -------------" << std::endl;
}
int main()
{
testAVBufferPool();
return 0;
}
边栏推荐
- Tronapi-波场接口-PHP版本--附接口文档-基于ThinkPHP5封装-源码无加密-可二开-作者详细指导-2022年6月28日11:49:56
- Today in history: Microsoft acquires PowerPoint developers; SGI and MIPS merge
- Scratch drawing square electronic society graphical programming scratch grade examination level 2 true questions and answers analysis June 2022
- MATLAB小技巧(22)矩阵分析--逐步回归
- 【300+精选大厂面试题持续分享】大数据运维尖刀面试题专栏(二)
- Discussion on JMeter operation principle
- JMeter之性能测试流程及性能测试关注点
- 问卷星问卷抓包分析
- Qt中的数据库使用
- Unity的脚本的基础语法(2)-Unity中记录时间
猜你喜欢

Apple executives openly "open the connection": Samsung copied the iPhone and only added a large screen

Shell编程概述
![[qnx hypervisor 2.2 user manual]6.2.3 communication between guest and external](/img/ca/9065325ce8882d95fb24c82fb62abc.png)
[qnx hypervisor 2.2 user manual]6.2.3 communication between guest and external

Three ways for flinksql to customize udaf
![[learn awk in one day] operator](/img/52/fd476d95202f3a956fd59437c2d960.png)
[learn awk in one day] operator

Visual studio configures QT and implements project packaging through NSIS

你想要的异常知识点都在这里了

Qt中的数据库使用

SuperMap iclient3d for webgl loading TMS tiles

写信宝小程序开源
随机推荐
Tronapi wave field interface PHP version - interface document attached - package based on thinkphp5 - source code without encryption - can be opened in two - detailed guidance of the author - 11:49:56
[learn awk in one day] operator
[one day learning awk] Fundamentals
Wechat launched the picture big bang function; Apple's self-developed 5g chip may have failed; Microsoft solves the bug that causes edge to stop responding | geek headlines
Tronapi- wavefield interface - source code without encryption - can be opened in two places - interface document attached - encapsulation based on thinkphp5 - detailed guidance of the author - 21:59:3
论文解读(AGC)《Attributed Graph Clustering via Adaptive Graph Convolution》
JMeter性能测试工作中遇到的问题及剖析,你遇到了几个?
Sublist3r error reporting solution
黑马笔记---List系列集合与泛型
Unity脚本的基础语法(4)-访问其他游戏对象
Four ways for flinksql to customize udtf
zabbix-server启动失败处理方式
Tronapi-波场接口-PHP版本--附接口文档-基于ThinkPHP5封装-源码无加密-可二开-作者详细指导-2022年6月28日11:49:56
电机控制Clarke(α/β)等幅值变换推导
黑马笔记---集合(Collection的常用方法与遍历方式)
如何利用AI技术优化独立站客服系统?听听专家怎么说!
【惊了】迅雷下载速度竟然比不上虚拟机中的下载速度
Unity脚本的基础语法(1)-游戏对象的常用操作
Idea has a new artifact, a set of code to adapt to multiple terminals!
【精选】资源变现资讯、新闻、自媒体、博客小程序(可引流,开通流量主,带pc后台管理)