当前位置:网站首页>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;
}
边栏推荐
- “\“id\“ contains an invalid value“
- Visual studio configures QT and implements project packaging through NSIS
- ECDSA signature verification in crypt
- MATLAB小技巧(22)矩阵分析--逐步回归
- 【一天学awk】基础中的基础
- Charles打断点修改请求数据&响应数据
- zabbix-server启动失败处理方式
- Js根据相同值将数组转换为二维数组
- Unity脚本的基础语法(5)-向量
- Dataworks synchronizes maxcomputer to sqlserver. Chinese characters become garbled. How can I solve it
猜你喜欢

Sarsa notes

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

60 divine vs Code plug-ins!!

After the market value evaporated by 65billion yuan, the "mask king" made steady medical treatment and focused on condoms
![[yitianxue awk] regular matching](/img/a6/608ec8d0808dfae04d19dfeea66399.png)
[yitianxue awk] regular matching

市值蒸发650亿后,“口罩大王”稳健医疗,盯上了安全套

Dark horse notes - common date API
![[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

独立站即web3.0,国家“十四五“规划要求企业建数字化网站!

rpm2rpm 打包步骤
随机推荐
kubeedge的核心理念
Js根据相同值将数组转换为二维数组
Introduction to the renewal of substrate source code: the pledge amount is greatly reduced, and rocksdb can be completely disabled
QT implementation dynamic navigation bar
Apple executives openly "open the connection": Samsung copied the iPhone and only added a large screen
How to select an OLAP database engine?
Dark horse notes -- wrapper class, regular expression, arrays class
MySQL judges the calculation result and divides it by 100
排查问题的方法论(适用于任何多方合作中产生的问题排查)
SuperMap 3D SDKs_ Unity plug-in development - connect data services for SQL queries
问卷星问卷抓包分析
Qt中的事件处理
江西财经大学智慧江财登录分析
常用的ui组件
Double dqn notes
Substrate 源码追新导读: 修复BEEFY的gossip引擎内存泄漏问题, 智能合约删除队列优化
Unity的脚本的基础语法(2)-Unity中记录时间
Q-learning notes
[one day learning awk] array usage
[yitianxue awk] regular matching