当前位置: 首页 > news >正文

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 = av_buffer_alloc(pool->size);
    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;
}
 

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 模型优化学习笔记—动量梯度下降
  • 微软蓝屏事件揭示的网络安全深层问题与未来应对策略
  • 【Unity】web gl inputFied 中文输入,同时支持TextMeshInputFied,支持全屏
  • Redis过期键的删除策略
  • 【数据结构】栈和队列(c语言实现)(附源码)
  • 学python的第一天:PyCharm创建项目
  • kickstart自动安装脚本
  • 通信原理实验——PCM编译码
  • 什么是V2X?
  • Vue+live2d实现虚拟人物互动(一次体验叙述)
  • RocketMQ 的消息存储机制
  • 3.4数组和特殊矩阵
  • Java开发:文件上传和下载
  • 按摩虎口穴位的作用
  • Laravel php框架与Yii php 框架的优缺点
  •  D - 粉碎叛乱F - 其他起义
  • Django 博客开发教程 8 - 博客文章详情页
  • fetch 从初识到应用
  • HTML中设置input等文本框为不可操作
  • iOS | NSProxy
  • JS学习笔记——闭包
  • PHP变量
  • 得到一个数组中任意X个元素的所有组合 即C(n,m)
  • 互联网大裁员:Java程序员失工作,焉知不能进ali?
  • 简单实现一个textarea自适应高度
  • 前端面试之闭包
  • 驱动程序原理
  • 如何邀请好友注册您的网站(模拟百度网盘)
  • 适配mpvue平台的的微信小程序日历组件mpvue-calendar
  • 手写一个CommonJS打包工具(一)
  • 我与Jetbrains的这些年
  • 一个普通的 5 年iOS开发者的自我总结,以及5年开发经历和感想!
  • Spring第一个helloWorld
  • 关于Android全面屏虚拟导航栏的适配总结
  • ​​​​​​​开发面试“八股文”:助力还是阻力?
  • ​Java基础复习笔记 第16章:网络编程
  • ​Python 3 新特性:类型注解
  • # 透过事物看本质的能力怎么培养?
  • (2)MFC+openGL单文档框架glFrame
  • (4)Elastix图像配准:3D图像
  • (day6) 319. 灯泡开关
  • (六)Flink 窗口计算
  • (排序详解之 堆排序)
  • (三)mysql_MYSQL(三)
  • (图)IntelliTrace Tools 跟踪云端程序
  • (一)UDP基本编程步骤
  • (转)项目管理杂谈-我所期望的新人
  • (最优化理论与方法)第二章最优化所需基础知识-第三节:重要凸集举例
  • .Net 4.0并行库实用性演练
  • .NET/ASP.NETMVC 深入剖析 Model元数据、HtmlHelper、自定义模板、模板的装饰者模式(二)...
  • .NET/C# 在代码中测量代码执行耗时的建议(比较系统性能计数器和系统时间)
  • .Net接口调试与案例
  • .net生成的类,跨工程调用显示注释
  • @EnableConfigurationProperties注解使用
  • @Pointcut 使用