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;
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]中
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);
原文:https://www.cnblogs.com/lidabo/p/15038535.html