您当前的位置: 首页 > 

鱼儿-1226

暂无认证

  • 2浏览

    0关注

    1100博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

ffmpeg之-AVFrame解析(五)

鱼儿-1226 发布时间:2020-10-29 10:29:31 ,浏览量:2

前言

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 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);

 

关注
打赏
1604459285
查看更多评论
立即登录/注册

微信扫码登录

0.0391s