ffmpeg423libavutil模块内存管理

7次阅读

共计 5960 个字符,预计需要花费 15 分钟才能阅读完成。

核心模块

内存管理

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)。
正文完
 0