ffmpeg4.2.3--libavutil模块内存管理

核心模块

内存管理

buffer.h / buffer_internal.h

主要是ffmpeg缓存数据的主要接口,主要包含以下几个数据结构:AVBuffer,AVBufferRef,BufferPoolEntry,AVBufferPool。

AVBuffer/AVBufferRef 结构

struct AVBuffer {
uint8_t *data; /**< data described by this buffer */
//缓冲区地址
int      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;
//提供给free回调函数的参数
/**
* A combination of BUFFER_FLAG_*
*/
int flags;
// 缓冲区标志
};

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.
*/
//缓冲区地址,实际等于buffer->data
int      size;
//缓冲区大小,实际等于buffer->size
} AVBufferRef;

AVBufferRef *av_buffer_alloc(int size)
{
AVBufferRef *ret = NULL;
uint8_t    *data = NULL;

data = av_malloc(size);
//分配缓冲区
if (!data)
return NULL;

ret = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);
if (!ret)
av_freep(&data);

return ret;
}

AVBufferRef *av_buffer_create(uint8_t *data, int size,
void (*free)(void *opaque, uint8_t *data),
void *opaque, int flags)
{
AVBufferRef *ref = NULL;
AVBuffer    *buf = NULL;

buf = av_mallocz(sizeof(*buf));
if (!buf)
return NULL;

buf->data     = data;
buf->size     = size;
buf->free     = free ? free : av_buffer_default_free;
buf->opaque   = opaque;

atomic_init(&buf->refcount, 1);

if (flags & AV_BUFFER_FLAG_READONLY)
buf->flags |= BUFFER_FLAG_READONLY;

ref = av_mallocz(sizeof(*ref));
if (!ref) {
av_freep(&buf);
return NULL;
}

ref->buffer = buf;
ref->data   = data;
ref->size   = size;

return ref;
}

void av_buffer_default_free(void *opaque, uint8_t *data)
{
av_free(data);
}

AVBufferRef *av_buffer_ref(AVBufferRef *buf)
{
AVBufferRef *ret = av_mallocz(sizeof(*ret));

if (!ret)
return NULL;

*ret = *buf;
//共用同一份缓冲区
atomic_fetch_add_explicit(&buf->buffer->refcount, 1, memory_order_relaxed);
//引用计数加1
return ret;
}

static void buffer_replace(AVBufferRef **dst, AVBufferRef **src)
{
AVBuffer *b;

b = (*dst)->buffer;

if (src) {
**dst = **src;
av_freep(src);
} else
av_freep(dst);
//引用计数减1
if (atomic_fetch_add_explicit(&b->refcount, -1, memory_order_acq_rel) == 1) {
b->free(b->opaque, b->data);
av_freep(&b);
// 缓冲区引用计数变为0,则将缓冲区也回收
}
}

void av_buffer_unref(AVBufferRef **buf)
{
if (!buf || !*buf)
return;

buffer_replace(buf, NULL);
}

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;

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;

int size;
void *opaque;
AVBufferRef* (*alloc)(int size);
AVBufferRef* (*alloc2)(void *opaque, int size);
void         (*pool_free)(void *opaque);
};

AVBufferPool *av_buffer_pool_init2(int size, void *opaque,
AVBufferRef* (*alloc)(void *opaque, int size),
void (*pool_free)(void *opaque))
{
AVBufferPool *pool = av_mallocz(sizeof(*pool));
if (!pool)
return NULL;

ff_mutex_init(&pool->mutex, NULL);

pool->size      = size;
pool->opaque    = opaque;
pool->alloc2    = alloc;
pool->pool_free = pool_free;

atomic_init(&pool->refcount, 1);

return pool;
}

void av_buffer_pool_uninit(AVBufferPool **ppool)
{
AVBufferPool *pool;

if (!ppool || !*ppool)
return;
pool   = *ppool;
*ppool = NULL;

if (atomic_fetch_add_explicit(&pool->refcount, -1, memory_order_acq_rel) == 1)
buffer_pool_free(pool);
}

static void pool_release_buffer(void *opaque, uint8_t *data)
{
BufferPoolEntry *buf = opaque;
AVBufferPool *pool = buf->pool;

if(CONFIG_MEMORY_POISONING)
memset(buf->data, FF_MEMORY_POISON, pool->size);

ff_mutex_lock(&pool->mutex); //加锁
buf->next = pool->pool;
pool->pool = buf;
ff_mutex_unlock(&pool->mutex);

if (atomic_fetch_add_explicit(&pool->refcount, -1, memory_order_acq_rel) == 1)
buffer_pool_free(pool);
}

static AVBufferRef *pool_alloc_buffer(AVBufferPool *pool)
{
BufferPoolEntry *buf;
AVBufferRef     *ret;

ret = pool->alloc2 ? pool->alloc2(pool->opaque, pool->size) :
pool->alloc(pool->size);
if (!ret)
return NULL;

buf = av_mallocz(sizeof(*buf));
if (!buf) {
av_buffer_unref(&ret);
return NULL;
}

buf->data   = ret->buffer->data;
buf->opaque = ret->buffer->opaque;
buf->free   = ret->buffer->free;
buf->pool   = pool;

ret->buffer->opaque = buf;
ret->buffer->free   = pool_release_buffer;

return ret;
}

AVBufferRef *av_buffer_pool_get(AVBufferPool *pool)
{
AVBufferRef *ret;
BufferPoolEntry *buf;
//加锁
ff_mutex_lock(&pool->mutex);
buf = pool->pool;
if (buf) {
ret = av_buffer_create(buf->data, pool->size, pool_release_buffer,
buf, 0);
if (ret) {
pool->pool = buf->next;
buf->next = NULL;
}
} else {
ret = pool_alloc_buffer(pool);
}
ff_mutex_unlock(&pool->mutex);

if (ret)
atomic_fetch_add_explicit(&pool->refcount, 1, memory_order_relaxed);

return ret;
}

  • 内部每生成一个buffer,引用计数+1,回收一个buffer,引用计数-1。这两者也是匹配的。
  • 音视频编解码中大量使用内存池的方式
  • 因为有锁的控制,可以在多线程的环境中使用。
  • c语言中结合引用计数来管理内存基本上大多数开源库的解决方案。
  • av_buffer_pool_init的时候,引用计数为初始值1,调用av_buffer_pool_uninit标记为可销毁,引用计数减1。
  • 从池里生成的buffer,在释放的时候,是再回到池里,并且池的引用计数-1。也就是这是一个循环使用的缓冲池,使用引用计数来标记内部的缓冲区。
  • pool是一个缓冲池,管理者众多的内存缓冲区AVBuffer
  1. pool_alloc_buffer / av_buffer_pool_get

    内存池中无法获取到buffer就创建

  2. pool_release_buffer

    回收内存池的buffer

  3. av_buffer_pool_uninit

    释放内存池

  4. av_buffer_pool_init2

    创建缓冲池,得到AVBufferPool指针pool, pool的refcount为1,pool的pool变量值为NULL

    AVBufferPool主要解决用户需要使用同样长度的缓冲区的情况,比如原始音视频帧。 在开始用户可以调用av_buffer_pool_init来创建缓冲池。然后在任何时间都可以调用av_buffer_pool_get()来获得buffer,在该buffer的引用计数为0时,将会返回给缓冲池,这样就可以被循环使用了。

AVBufferPool 相关的函数
AVBufferPool 结构
  1. av_buffer_unref
  2. av_buffer_ref

av_buffer_default_free是默认的内存回收函数

两个函数的关系很清楚,av_buffer_create会给AVBuffer里面字段进行赋值

  1. av_buffer_create

非常非常标准的c语言内存申请模式,返回AVBufferRef的指针来操作。

  1. av_buffer_alloc
AVBuffer/AVBufferRef 关键函数
  1. AVBufferRef则对AVBuffer缓冲区提供了一层封装,最主要的是作引用计数处理,实现了一种安全机制
  2. 用户不应直接访问AVBuffer,应通过AVBufferRef来访问AVBuffer,以保证安全。
  3. FFmpeg中很多基础的数据结构都包含了AVBufferRef成员,来间接使用AVBuffer缓冲区。
  4. 内部,对于refcount的访问都是原子函数,比如:atomic_load(&buf->buffer->refcount)。

你可能感兴趣的:(ffmpeg,c)