• ffmpeg之-AVFrame解析(五)


    前言

    AVFrame 位于libavutil/frame.h中,AVpacket一样,是FFmpeg中很重要的结构体。它用于表示未压缩的音视频数据(编码前或者解码后),使用了引用计数机制来管理内存

    源码

    首先是源码部分(基于ffmpeg版本为4.2),已去掉注释部分

    typedef struct AVFrame {
    #define AV_NUM_DATA_POINTERS 8
        uint8_t *data[AV_NUM_DATA_POINTERS];
        int linesize[AV_NUM_DATA_POINTERS];
        uint8_t **extended_data;
        int width, height;
        int nb_samples;
        int format;
        int key_frame;
        enum AVPictureType pict_type;
        int64_t pts;
        
    #if FF_API_PKT_PTS
        attribute_deprecated
        int64_t pkt_pts;
    #endif
        int64_t pkt_dts;
        int coded_picture_number;
        int display_picture_number;
        int quality;
    #if FF_API_ERROR_FRAME
        attribute_deprecated
        uint64_t error[AV_NUM_DATA_POINTERS];
    #endif
        int repeat_pict;
        int interlaced_frame;
        int top_field_first;
        int palette_has_changed;
        int64_t reordered_opaque;
        int sample_rate;
        uint64_t channel_layout;
        AVBufferRef *buf[AV_NUM_DATA_POINTERS];
        AVBufferRef **extended_buf;
        int        nb_extended_buf;
        AVFrameSideData **side_data;
        int            nb_side_data;
    #define AV_FRAME_FLAG_CORRUPT       (1 << 0)
    #define AV_FRAME_FLAG_DISCARD   (1 << 2)
        int flags;
        enum AVColorRange color_range;
        enum AVColorPrimaries color_primaries;
        enum AVColorTransferCharacteristic color_trc;
        enum AVColorSpace colorspace;
        enum AVChromaLocation chroma_location;
        int64_t best_effort_timestamp;
        int64_t pkt_pos;
        int64_t pkt_duration;
        int decode_error_flags;
    #define FF_DECODE_ERROR_INVALID_BITSTREAM   1
    #define FF_DECODE_ERROR_MISSING_REFERENCE   2
    #define FF_DECODE_ERROR_CONCEALMENT_ACTIVE  4
    #define FF_DECODE_ERROR_DECODE_SLICES       8
        
        int channels;
        int pkt_size;
        
    #if FF_API_FRAME_QP
        /**
         * QP table
         */
        attribute_deprecated
        int8_t *qscale_table;
        /**
         * QP store stride
         */
        attribute_deprecated
        int qstride;
        
        attribute_deprecated
        int qscale_type;
        
        attribute_deprecated
        AVBufferRef *qp_table_buf;
    #endif
        AVBufferRef *hw_frames_ctx;
        AVBufferRef *opaque_ref;
        AVBufferRef *private_ref;
    } AVFrame;
    
    • uint8_t *data[AV_NUM_DATA_POINTERS];
      存储原始的音视频数据。有两种存储音视频的方式,planner方式和packet方式

    planner方式:通道n的数据分别存储在data[n]中;拿YUV视频来说,就是data[0],data[1],data[2]分别存储Y,U,V的数据。拿双声道的音频来说,就是data[0],data[1]分别存储左声道,右声道数据;对于音频,声道数有可能大于AV_NUM_DATA_POINTERS,那么多出来的将存储在extended_data字段中
    packet方式:所有数据都存储在data[0]中

    • int linesize[AV_NUM_DATA_POINTERS];
      表示每一行数据的大小;对于planner格式和packet格式,这里的取值也不一样
    • uint8_t **extended_data;
      存放data存放不下的数据
    • int width, height;
      视频宽高
    • int nb_samples;
      音频采样率
    • int format;
      音视频格式;视频对应于enum AVPixelFormat,音频对应于enum AVSampleFormat
    • int key_frame;
      是否关键帧;1代表关键帧,0代表非关键帧;对于音频,都是1
      enum AVPictureType pict_type;
      视频帧的类型,比如I帧,B帧,P帧
    • sample_rate
      音频采样率
    • uint64_t channel_layout;
      音频声道类型
    • int channels;
      音频声道数
    • AVBufferRef *buf[AV_NUM_DATA_POINTERS];
      对data内存进行引用计数管理的字段。如果buf[n]对应的不为NULL,那么表示data[n]对应的使用了引用计数管理内存,否则由用户手动管理内存
    • AVBufferRef **extended_buf;
      对应于extended_data,用于引用计数管理内存

    常用相关函数

    • AVFrame *av_frame_alloc(void);
      用于在堆内存中创建一个AVFrame对象,但是uint8_t *data[AV_NUM_DATA_POINTERS];等默认分配为NULL
    • void av_frame_free(AVFrame **frame);
      释放由av_frame_alloc()分配的对象;内部会调用一次av_frame_unref()函数
    • int av_frame_ref(AVFrame *dst, const AVFrame *src);
      如果src的buf[n]不为NULL,则dst的buf会指向src的buf,相当于把src的值赋值给dst,没有数据拷贝。如果src的buf为NULL,则dst会创建一个新的buf,并将src的buf中data拷贝过去
    • void av_frame_unref(AVFrame *frame);
      将引用计数-1;如果AVFrame引用计数为0,则释放AVFrame分配的uint8_t *data[AV_NUM_DATA_POINTERS]等等内存
    • int av_frame_get_buffer(AVFrame *frame, int align);
      如果AVFrame已经分配了内存,再次调用会造成内存泄漏和不可预知错误;参数二传0即可,表示根据目前cpu类型自动选择对齐的字节数
      为AVFrame分配内存,调用此函数前必须先设置format;width/height(video);nb_samples/channel_layout(audio)
    • int av_frame_make_writable(AVFrame *frame);

    AVFrame是否可写:buf不为空,并且对应的flags非AV_BUFFER_FLAG_READONLY

    首先判断是否可写,不可写则重新创建buf,并将data指向内存拷贝到buf中,将引用计数设置为1

    tips:AVFrame对象如果由av_frame_free()等函数释放了,则不能调用此函数了,会奔溃

    使用示例

    1、创建AVFrame并分配内存的方式 一

    AVFrame     *p1Frame;
    p1Frame = av_frame_alloc();
    p1Frame->format=AV_PIX_FMT_YUV420P;
    p1Frame->width = 1280;
    p1Frame->height = 720;
    // 为AVFrame分配内存,调用此函数前必须先设置format;width/height(video);nb_samples/channel_layout(audio)
    // 如果AVFrame已经分配了内存,再次调用会造成内存泄漏和不可预知错误;参数二传0即可,表示根据目前cpu类型自动选择对齐的字节数
    av_frame_get_buffer(p1Frame, 0);
    // 让Frame可写
    av_frame_make_writable(p1Frame);
    
    。。。。。这里是用于编码的伪代码。。。。。。
    装入数据
    uint8_t *srcVideodata = NULL;
    memcpy(p1Frame->data, srcVideodata,100);
    // 前面准备好了未压缩的AVFrame数据送入编码器;
    // 因为原始数据大小一般都固定,所以可以循环使用p1Frame的data字段
    avcodec_send_frame(codecCtx,p1Frame);
    。。。。。这里是伪代码。。。。。。
    
    // 解码结束
    av_frame_free(&p1Frame);
    

    2、创建AVFrame并分配内存的方式 二

    AVFrame     *p2Frame;
    p2Frame = av_frame_alloc();
    // 先设置值
    p1Frame->format=AV_PIX_FMT_YUV420P;
    p1Frame->width = 1280;
    p1Frame->height = 720;
    // 根据给定的参数分配一块内存空间;注意此时p2Frame的用于引用计数管理的AVBufferRef *buf[AV_NUM_DATA_POINTERS];是NULL,
    // 所以必须通过
    av_image_alloc(p2Frame->data, p2Frame->linesize, p2Frame->width, p2Frame->height, AV_PIX_FMT_YUV420P, 0);
    // 这里的内存要手动释放
    av_freep(p2Frame->data);
    av_frame_free(&p2Frame);
    

    3、创建AVFrame并分配内存的方式 三

    AVFrame     *p1Frame;
    p1Frame = av_frame_alloc();
    
    。。。。。这里是解码的伪代码。。。。。
    解码器内部会自动为AVFrame分配内存,并采用引用计数方式管理此内存
    avcodec_receive_frame(codecCtx,p1Frame);
    // av_frame_unref(p1Frame);不推荐,因为AVFrame内存大小一般固定,可重复使用,这里如果释放,那么解码器又会重新分配内存
    。。。。。这里是解码的伪代码。。。。
    
    // 解码结束
    av_frame_free(&p1Frame);


    from:https://www.jianshu.com/p/0e8baa8c5422
  • 相关阅读:
    QTP 处理webtable中的数据
    Error 1406.Setup cannot write the value Microsoft
    QTP打开WinTree中的指定节点
    Excel数据操作
    VBS中实现函数多返回值
    QTP的回放模式
    VBS中运行应用程序的两种方式及WshShell对像浅析
    程序员奇葩面试的奇葩问题
    Android如何实现毛玻璃效果之Android高级模糊技术
    Android通过用代码画虚线椭圆边框背景来学习一下shape的用法
  • 原文地址:https://www.cnblogs.com/lidabo/p/15038535.html
Copyright © 2020-2023  润新知