ffmpeg 的内存分配架构

------------------------------------------------------------
 author: hjjdebug
 date:   2024年 08月 01日 星期四 18:00:47 CST
 descripton:  ffmpeg 的内存分配架构1
------------------------------------------------------------
ffmpeg 的内配分配搞的人晕菜,来,我们分析一下:
看看下面的调用图,就知道其中关系了!

 0 in av_malloc of libavutil/mem.c:86                //跟malloc 差不多! 一个参数(大小),只是64字节对齐
 1 in av_mallocz of libavutil/mem.c:239             //带z就是内存清0
 2 in av_buffer_create of libavutil/buffer.c:49        //引入AVBuffer的概念
 3 in av_buffer_alloc of libavutil/buffer.c:76        //跟malloc 差不多! 一个参数(大小),返回是AVBufferRef *
 4 in av_buffer_allocz of libavutil/buffer.c:85        //带z就是内存清0
 5 in pool_alloc_buffer of libavutil/buffer.c:352  //无空闲项,分配一个pool_entry,包含一个AVBufferRef* ,数据大小由pool->size决定
 6 in av_buffer_pool_get of libavutil/buffer.c:388 //从内存池(AVBufferPool)中申请内存,返回AVBufferRef *
 7 in video_get_buffer of libavcodec/decode.c:1663 // 需要拿到YUV3个平面的内存地址,需要3个AVBufferPool
 8 in avcodec_default_get_buffer2 of libavcodec/decode.c:1702 //初始化好FramePool内存池(3个AVBufferPool池),然后从video或者从audio拿内存
 9 in ff_get_buffer of libavcodec/decode.c:1937  //为frame分配内存地址及copy一些属性
10 in thread_get_buffer_internal of libavcodec/pthread_frame.c:1006 //只是个简单包装
11 in ff_thread_get_buffer of libavcodec/pthread_frame.c:1098  //只是个简单包装

0,1,2,3,4, 这4层是最基础的内存分配架构
5,6是内存池
7,8是video,audio内存分配

9. codec解码frame时前期基本属性copy及内存分配.

10,11: 只是简单封装.
以上即是codec 解码数据包时的内存分配工作. 由此来了解它的底层工作流程
----------------------------------------
第0层: av_malloc
----------------------------------------
void *av_malloc(size_t size)
{
    void *ptr=NULL;
    posix_memalign(&ptr, 64, size)
    return ptr;
}
问题: av_malloc 与 malloc 有什么区别?
答:  av_malloc 其实等价于malloc, 
     但它实际调用的c 库函数是posix_memalign,为的是实现64字节对齐

----------------------------------------
第1层: av_mallocz
----------------------------------------
void *av_mallocz(size_t size)
{
    void *ptr = av_malloc(size);
    if (ptr) memset(ptr, 0, size);
    return ptr;
}
av_mallocz 对返回的内存,用0进行了初始化.

----------------------------------------
第2层: av_buffer_create
----------------------------------------
用途: 为一个已有的内存data,size 分配一个AVBuffer 结构,以后你用AVBufferRef* 结构去访问它

AVBuffer 结构,40个字节, 是一个带有refcount 和 free函数的管理结构,管理传来的buffer(即data,size)
AVBufferRef结构,24个字节
下面看代码:
调用参数比较多,先要搞清楚意思.
第1,2参数: data,size: 被保护的内存
第3 参数: free: 释放该内存的函数
第4 参数: opaque: 内存释放函数的参数
第5 参数: flags: 标志位
返回值: AVBufferRef指针. 返回的指针结构(24byte)虽小,却能访问到所有的东西.
AVBufferRef *av_buffer_create(uint8_t *data, buffer_size_t size,
                              void (*free)(void *opaque, uint8_t *data),
                              void *opaque, int flags)
{
    AVBuffer *buf = av_mallocz(sizeof(*buf));
    buf->data     = data;
    buf->size     = size;
    buf->free     = free ? free : av_buffer_default_free;//有free给free,否则给个默认的.
    buf->opaque   = opaque;
    atomic_init(&buf->refcount, 1);
    buf->flags = flags;

    AVBufferRef *ref = av_mallocz(sizeof(*ref));
    ref->buffer = buf;
    ref->data   = data;
    ref->size   = size;
    return ref;
}
问1: 为什么要包装这么一层?
答2: 这样是为了当数据克隆时,我们只需要clone一下AVBufferRef 结构,才24个字节, 
    把AVBuffer的 refcount加1, 而其所指向的数据可能很大就不用clone了. 岂不妙哉!

问2:那个free 函数有哪些?
答2:再说.

----------------------------------------
第3层 av_buffer_alloc
----------------------------------------
代码:
AVBufferRef *av_buffer_alloc(buffer_size_t size)
{
    uint8_t *data = av_malloc(size);
    AVBufferRef *ret = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);
    return ret;
}
av_buffer_alloc 与 malloc 有什么区别?
前者返回的是AVBufferRef 指针,数据被保护了,而后者返回的直接是数据指针.
原来我们能直接找到总统,现在总统带保镖了.你只能找到保镖,不能直接找到总统了.

----------------------------------------
第4层: av_buffer_allocz
----------------------------------------
AVBufferRef *av_buffer_allocz(buffer_size_t size)
{
    AVBufferRef *ret = av_buffer_alloc(size);
    memset(ret->data, 0, size);
    return ret;
}
带z的函数多了一个数据清0的过程.

----------------------------------------
第5层: pool_alloc_buffer
----------------------------------------
该函数是有内存池创建一个内存池项,
同时创建一个AVBufferRef 项,ref所指内存数据的大小是由pool->size决定的

内存池项p_entry 是隐含的,透明的,但它与ref指针密切相关.
它返回的是AVBufferRef 指针
代码如下:
static AVBufferRef *pool_alloc_buffer(AVBufferPool *pool)
{
    AVBufferRef     *b_ref = pool->alloc2 ? pool->alloc2(pool->opaque, pool->size) :
                         pool->alloc(pool->size); //调用保存的内存池分配函数,默认是av_buffer_alloc

    BufferPoolEntry *p_entry = av_mallocz(sizeof(*p_entry));
    p_entry->data   = b_ref->buffer->data;  // entry中的数据元素来源于ref
    p_entry->opaque = b_ref->buffer->opaque;
    p_entry->free   = b_ref->buffer->free;
    p_entry->pool   = pool;

    b_ref->buffer->opaque = p_entry;  //ref 中的参数是p_entry地址
    b_ref->buffer->free   = pool_release_buffer; //修改buffer中的free函数地址
    return b_ref;
}
内存池和内存池项的结构我就不copy了.
内存池就是内存池项的链表管理. 它能增加或减少内存池项.有指针指向空闲链表头
内存池项是内存的又一个保镖,它有指针指向数据,同时还有内存释放函数.
关于内存池项对用户是透明的,看不见的. 当我们也应该清楚其内部操作.

用内存池分配的内存,它把buffer->free 改成了pool_release_buffer.
这样当释放内存的时候不是通过 free 返回系统, 而是通过pool_release_buffer 返回给pool
pool 只是把它作为空闲项加入链表,没有归还系统,留给以后分配使用.

----------------------------------------
第6层: av_buffer_pool_get
----------------------------------------
AVBufferRef *av_buffer_pool_get(AVBufferPool *pool)
{
    AVBufferRef *b_ref;
    ff_mutex_lock(&pool->mutex);
    BufferPoolEntry *p_entry = pool->pool;  //从pool 的空闲项链表取得一项
    if (p_entry) { // 如果拿到了, 则创建一个AVBufferRef 来返回,只需分配40+24bytes
        b_ref = av_buffer_create(p_entry->data, pool->size, pool_release_buffer, p_entry, 0);
        if (b_ref) { //修改pool 的空闲项指针
            pool->pool = p_entry->next;
            p_entry->next = NULL;
        }
    } else {
        b_ref = pool_alloc_buffer(pool); //pool 没有空闲置项,则分配一项,就是上面的分析过程.
    }
    ff_mutex_unlock(&pool->mutex);

// pool 的引用计数加1,
    if (b_ref) 
        atomic_fetch_add_explicit(&pool->refcount, 1, memory_order_relaxed);

    return b_ref;
}

调用相关函数,使pool引用计数减少,当pool引用计数减到0时,整个pool 会释放掉,所有分配的内存返回系统.

----------------------------------------
第7层: video_get_buffer
----------------------------------------
它为AVFrame 分配了内存, 使frame->buf,frame->data 有了数值
视频包含3个平面Y,U,V  所以从3个内存池中申请内存
static int video_get_buffer(AVCodecContext *s, AVFrame *frame)
{
    int i;
    if (frame->data[0] || frame->data[1] || frame->data[2] || frame->data[3]) {
        av_log(s, AV_LOG_ERROR, "frame->data[*]!=NULL in avcodec_default_get_buffer\n");
        return -1;
    }
    //frame->data 是8个指针 unsigned char *[8], 不过一般用3个就够了.
    memset(frame->data, 0, sizeof(frame->data));
    frame->extended_data = frame->data;

    //这里有FramePool的概念,它有4个AVBufferPool 及其它一些参数
    //惊叹号!s->internal->pool是一个AVBufferRef

    FramePool *fr_pool = (FramePool*)s->internal->pool->data; 
    for (i = 0; i < 4 && fr_pool->pools[i]; i++) {
        frame->linesize[i] = fr_pool->linesize[i];
        frame->buf[i] = av_buffer_pool_get(fr_pool->pools[i]); //frame 从缓冲池中得到内存,上面的分析
        frame->data[i] = frame->buf[i]->data;
    }
    for (; i < AV_NUM_DATA_POINTERS; i++) {
        frame->data[i] = NULL;
        frame->linesize[i] = 0;
    }
    return 0;
fail:
    av_frame_unref(frame);
    return AVERROR(ENOMEM);
}
缓冲池由AVCodecContext 来保存! 生命周期是整个数据周期

----------------------------------------
第8层: avcodec_default_get_buffer2
----------------------------------------
int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
{
    int ret;
    //该函数的意义?参看补充
    if ((ret = update_frame_pool(avctx, frame)) < 0) return ret;
    switch (avctx->codec_type) {
    case AVMEDIA_TYPE_VIDEO:
        return video_get_buffer(avctx, frame); // 这就是我们上面分析的.
    case AVMEDIA_TYPE_AUDIO:
        return audio_get_buffer(avctx, frame);
    default:
        return -1;
    }
}

----------------------------------------
第9层 ff_get_buffer
----------------------------------------
为frame分配内存地址及copy一些属性,属性来源于pkt(发来的数据)及avctx
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
{
    if (frame->width <= 0 || frame->height <= 0) { //从avctx中设置 frame的width,height属性
        frame->width  = FFMAX(avctx->width,  AV_CEIL_RSHIFT(avctx->coded_width,  avctx->lowres));
        frame->height = FFMAX(avctx->height, AV_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
        override_dimensions = 0;
    }
    ret = ff_decode_frame_props(avctx, frame); // 给frame设置一些属性
    avctx->sw_pix_fmt = avctx->pix_fmt;
    ret = avctx->get_buffer2(avctx, frame, flags);  //它指向avcodec_default_get_buffer2,就是上面的分析
    validate_avframe_allocation(avctx, frame); //检查frame 内存分配是否合法, 简单!
    ret = ff_attach_decode_data(frame); //简单,创建了一个FrameDecodeData 对象,作为frame->private_ref
    return ret;
}

int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
{
   // 从未解码包中(就是发来的packet数据)copy一些属性
    AVPacket *pkt = avctx->internal->last_pkt;
    frame->pts = pkt->pts;
    frame->pkt_pts = pkt->pts;
    frame->pkt_pos      = pkt->pos;
    frame->pkt_duration = pkt->duration;
    frame->pkt_size     = pkt->size;
    //把pkt的size_data 也copy 过来 ...
    ...
    //从avctx 中copy 一些属性
    frame->reordered_opaque = avctx->reordered_opaque;
    if (frame->color_primaries == AVCOL_PRI_UNSPECIFIED)
        frame->color_primaries = avctx->color_primaries;
    if (frame->color_trc == AVCOL_TRC_UNSPECIFIED)
        frame->color_trc = avctx->color_trc;
    if (frame->colorspace == AVCOL_SPC_UNSPECIFIED)
        frame->colorspace = avctx->colorspace;
    if (frame->color_range == AVCOL_RANGE_UNSPECIFIED)
        frame->color_range = avctx->color_range;
    if (frame->chroma_location == AVCHROMA_LOC_UNSPECIFIED)
        frame->chroma_location = avctx->chroma_sample_location;

    switch (avctx->codec->type) {
    case AVMEDIA_TYPE_VIDEO:
        frame->format              = avctx->pix_fmt;
        if (!frame->sample_aspect_ratio.num)
            frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
        }
        break;
    case AVMEDIA_TYPE_AUDIO:
        if (!frame->sample_rate) frame->sample_rate    = avctx->sample_rate;
        if (frame->format < 0) frame->format         = avctx->sample_fmt;
        frame->channels = avctx->channels;
        break;
    }
    return 0;
}

----------------------------------------
第10层 thread_get_buffer_internal 
----------------------------------------

static int thread_get_buffer_internal(AVCodecContext *avctx, ThreadFrame *f, int flags)

#define FF_THREAD_FRAME   1 ///< Decode more than one frame at once
#define FF_THREAD_SLICE   2 ///< Decode more than one part of a single frame at once
    if (!(avctx->active_thread_type & FF_THREAD_FRAME)) //active_thread_type 为3
        return ff_get_buffer(avctx, f->f, flags); //直接走上面的分析
}

----------------------------------------
第11层 ff_thread_get_buffer
----------------------------------------
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
{
    return thread_get_buffer_internal(avctx, f, flags);
}
----------------------------------------
补充1: update_frame_pool
----------------------------------------
分配FramePool 并依据avctx, frame初始化FramePool 的参数
根据frame的宽度,高度得到平面的大小size, 即为pool->size, 创建3个pool
static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
{
    //先查看是否分配过, 分配过就直接返回了.
    FramePool *frame_pool = avctx->internal->pool ?
                      (FramePool*)avctx->internal->pool->data : NULL;
    if (frame_pool && frame_pool->format == frame->format) {
        if (avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
            frame_pool->width == frame->width && frame_pool->height == frame->height)
            return 0;
        if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && frame_pool->planes == planes &&
            frame_pool->channels == ch && frame->nb_samples == frame_pool->samples)
            return 0;
    }
    //显然它是分配了一个FramePool, 又创建了一个AVBuffer来包围FramePool, 返回了AVBufferRef*
    AVBufferRef *buf_ref = frame_pool_alloc();
    frame_pool = (FramePool*)buf_ref->data;

    switch (avctx->codec_type) {
    case AVMEDIA_TYPE_VIDEO: {
        int linesize[4];
        int w = frame->width; //要注意frame->width的值是怎么得来的,留后续补充.
        int h = frame->height;
        int unaligned;
        ptrdiff_t linesize1[4];
        size_t size[4];

        avcodec_align_dimensions2(avctx, &w, &h, frame_pool->stride_align);
        do { //从w 得到linesize, linesize 是被调整过的width
            ret = av_image_fill_linesizes(linesize, avctx->pix_fmt, w);
            w += w & ~(w - 1);
            unaligned = 0;
            for (i = 0; i < 4; i++)
                unaligned |= linesize[i] % frame_pool->stride_align[i];
        } while (unaligned);

        for (i = 0; i < 4; i++)
            linesize1[i] = linesize[i];
        //根据linesize及高度,得到平面的大小size, YUV 是3个平面, 所以4个空间足够
        ret = av_image_fill_plane_sizes(size, avctx->pix_fmt, h, linesize1);
        for (i = 0; i < 4; i++) {
            frame_pool->linesize[i] = linesize[i]; //保留linesize
            if (size[i]) { //按size 来初始化内存池
                frame_pool->pools[i] = av_buffer_pool_init(size[i] + 16 + STRIDE_ALIGN - 1,
                                                     CONFIG_MEMORY_POISONING ?
                                                        NULL :
                                                        av_buffer_allocz);
                }
            }
        }
        frame_pool->format = frame->format; //保留其它参数
        frame_pool->width  = frame->width;
        frame_pool->height = frame->height;
        break;
        }
    case AVMEDIA_TYPE_AUDIO: {
        ret = av_samples_get_buffer_size(&frame_pool->linesize[0], ch,
                                         frame->nb_samples, frame->format, 0);
        frame_pool->pools[0] = av_buffer_pool_init(frame_pool->linesize[0], NULL);
        frame_pool->format     = frame->format;
        frame_pool->planes     = planes;
        frame_pool->channels   = ch;
        frame_pool->samples = frame->nb_samples;
        break;
        }
    default: av_assert0(0);
    }

    av_buffer_unref(&avctx->internal->pool);
    avctx->internal->pool = buf_ref;  //重新更新framepool 
    return 0;
}
 

  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值