FFmpeg源码分析: AVFrame与AVPacket

FFmpeg有两个存储帧数据的结构体,其中AVPacket是解封装后保存压缩数据包,AVFrame是解码后保存音视频帧。

AVPacket结构体以前放在avcodec.h头文件中,在FFmpeg4.4以后放在单独packet.h头文件。官方对AVPacket的说明如下:

/**
 * This structure stores compressed data. It is typically exported by demuxers
 * and then passed as input to decoders, or received as output from encoders and
 * then passed to muxers.
 *
 * For video, it should typically contain one compressed frame. For audio it may
 * contain several compressed frames. Encoders are allowed to output empty
 * packets, with no compressed data, containing only side data
 * (e.g. to update some stream parameters at the end of encoding).
 *
 */

AVPacket结构体定义如下:

typedef struct AVPacket {
 
    AVBufferRef *buf;
    // 显示时间戳,单位为AVStream->time_base
    int64_t pts;
    // 解码时间戳,单位为AVStream->time_base
    int64_t dts;
    // 音视频数据
    uint8_t *data;
    // 数据包大小
    int   size;
    // 码流索引下标
    int   stream_index;
    // 帧类型
    int   flags;
    // 额外数据
    AVPacketSideData *side_data;
    int side_data_elems;
    // 帧显示时长,单位为AVStream->time_base
    int64_t duration;
    // 数据包所在码流的position
    int64_t pos;
 
} AVPacket;

AVPacket的分配与释放有对应的API,需要注意的是释放所传的参数为AVPacket指针的地址。API与示例如下:

API:
AVPacket *av_packet_alloc(void);
 
void av_packet_unref(AVPacket *pkt);
 
void av_packet_free(AVPacket **pkt);
 
demo:
AVPacket *pkt = av_packet_alloc();
 
av_packet_unref(pkt);
 
av_packet_free(&pkt);

AVFrame结构体位于frame.h头文件,用于存储解码后的音视频帧数据,使用av_frame_alloc进行分配,使用av_frame_free进行释放,AVFrame分配一次,多次复用,使用av_frame_unref可以去引用。官方关于AVFrame的描述如下:

/**
 * This structure describes decoded (raw) audio or video data.
 *
 * AVFrame must be allocated using av_frame_alloc(). Note that this only
 * allocates the AVFrame itself, the buffers for the data must be managed
 * through other means (see below).
 * AVFrame must be freed with av_frame_free().
 *
 * AVFrame is typically allocated once and then reused multiple times to hold
 * different data (e.g. a single AVFrame to hold frames received from a
 * decoder). In such a case, av_frame_unref() will free any references held by
 * the frame and reset it to its original clean state before it
 * is reused again.
 */

AVFrame是解码后用于存储音视频帧,包括:data数组、width、height、pts、pkt_dts、pkt_size、pkt_duration、pkt_pos等信息,我们在判断是否为关键帧时,用key_frame参数进行判断。结构体的定义如下:

typedef struct AVFrame {
#define AV_NUM_DATA_POINTERS 8
 
    // pointer to the picture/channel planes.
    uint8_t *data[AV_NUM_DATA_POINTERS];
 
    /**
     * For video, size in bytes of each picture line.
     * For audio, size in bytes of each plane.
     *
     * For audio, only linesize[0] may be set. For planar audio, each channel
     * plane must be the same size.
     *
     * For video the linesizes should be multiples of the CPUs alignment
     * preference, this is 16 or 32 for modern desktop CPUs.
     * Some code requires such alignment other code can be slower without
     * correct alignment, for yet other it makes no difference.
     */
    int linesize[AV_NUM_DATA_POINTERS];
 
    /**
     * pointers to the data planes/channels.
     *
     * For video, this should simply point to data[].
     *
     * For planar audio, each channel has a separate data pointer, and
     * linesize[0] contains the size of each channel buffer.
     * For packed audio, there is just one data pointer, and linesize[0]
     * contains the total size of the buffer for all channels.
     */
    uint8_t **extended_data;
 
    /**
     * @name Video dimensions
     */
    int width, height;
 
    /**
     * number of audio samples (per channel) described by this frame
     */
    int nb_samples;
 
    /**
     * format of the frame, -1 if unknown or unset
     * Values correspond to enum AVPixelFormat for video frames,
     * enum AVSampleFormat for audio)
     */
    int format;
 
    /**
     * 1 -> keyframe, 0-> not
     */
    int key_frame;
 
    /**
     * Picture type of the frame.
     */
    enum AVPictureType pict_type;
 
    /**
     * Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
     */
    AVRational sample_aspect_ratio;
 
    /**
     * Presentation timestamp in time_base units.
     */
    int64_t pts;
 
    /**
     * DTS copied from the AVPacket that triggered returning this frame.
     * This is also the Presentation time of this AVFrame calculated from
     * only AVPacket.dts values without pts values.
     */
    int64_t pkt_dts;
 
    /**
     * picture number in bitstream order
     */
    int coded_picture_number;
    /**
     * picture number in display order
     */
    int display_picture_number;
 
    /**
     * quality (between 1 (good) and FF_LAMBDA_MAX (bad))
     */
    int quality;
 
    /**
     * for some private data of the user
     */
    void *opaque;
 
    /**
     * When decoding, this signals how much the picture must be delayed.
     * extra_delay = repeat_pict / (2*fps)
     */
    int repeat_pict;
 
    /**
     * The content of the picture is interlaced.
     */
    int interlaced_frame;
 
    /**
     * If the content is interlaced, is top field displayed first.
     */
    int top_field_first;
 
    /**
     * Tell user application that palette has changed from previous frame.
     */
    int palette_has_changed;
 
    /**
     * reordered opaque 64 bits.
     */
    int64_t reordered_opaque;
 
    /**
     * Sample rate of the audio data.
     */
    int sample_rate;
 
    /**
     * Channel layout of the audio data.
     */
    uint64_t channel_layout;
 
    /**
     * AVBuffer references backing the data for this frame. 
	 * If all elements of this array are NULL, 
     * then this frame is not reference counted.
     */
    AVBufferRef *buf[AV_NUM_DATA_POINTERS];
 
    /**
     * For planar audio which requires more than AV_NUM_DATA_POINTERS
     * AVBufferRef pointers, this array will hold all the references which
     * cannot fit into AVFrame.buf.
     */
    AVBufferRef **extended_buf;
    /**
     * Number of elements in extended_buf.
     */
    int        nb_extended_buf;
 
    AVFrameSideData **side_data;
    int            nb_side_data;
 
/**
 * The frame data may be corrupted, e.g. due to decoding errors.
 */
#define AV_FRAME_FLAG_CORRUPT       (1 << 0)
/**
 * A flag to mark the frames which need to be decoded.
 */
#define AV_FRAME_FLAG_DISCARD   (1 << 2)
 
    /**
     * Frame flags, a combination of @ref lavu_frame_flags
     */
    int flags;
 
    /**
     * MPEG vs JPEG YUV range.
     * - encoding: Set by user
     * - decoding: Set by libavcodec
     */
    enum AVColorRange color_range;
 
    enum AVColorPrimaries color_primaries;
 
    enum AVColorTransferCharacteristic color_trc;
 
    /**
     * YUV colorspace type.
     * - encoding: Set by user
     * - decoding: Set by libavcodec
     */
    enum AVColorSpace colorspace;
 
    enum AVChromaLocation chroma_location;
 
    /**
     * frame timestamp estimated using various heuristics, in stream time base
     * - encoding: unused
     * - decoding: set by libavcodec, read by user.
     */
    int64_t best_effort_timestamp;
 
    /**
     * reordered pos from the last AVPacket that has been input into the decoder
     * - encoding: unused
     * - decoding: Read by user.
     */
    int64_t pkt_pos;
 
    /**
     * duration of the corresponding packet, expressed in
     * AVStream->time_base units, 0 if unknown.
     * - encoding: unused
     * - decoding: Read by user.
     */
    int64_t pkt_duration;
 
    /**
     * metadata.
     * - encoding: Set by user.
     * - decoding: Set by libavcodec.
     */
    AVDictionary *metadata;
 
    /**
     * decode error flags of the frame, set to a combination of
     * FF_DECODE_ERROR_xxx flags if the decoder produced a frame,
     * but there were errors during the decoding.
     */
    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
 
    // number of audio channels, only used for audio.
    int channels;
 
    // size of the corresponding packet containing the compressed frame.
    int pkt_size;
 
    /**
     * For hwaccel-format frames, this should be a reference to the
     * AVHWFramesContext describing the frame.
     */
    AVBufferRef *hw_frames_ctx;
 
    /**
     * AVBufferRef for free use by the API user. FFmpeg will never check the
     * contents of the buffer ref. FFmpeg calls av_buffer_unref() on it when
     * the frame is unreferenced. av_frame_copy_props() calls create a new
     * reference with av_buffer_ref() for the target frame's opaque_ref field.
     */
    AVBufferRef *opaque_ref;
 
    /**
     * Video frames only. The number of pixels to discard from the the
     * top/bottom/left/right border of the frame to obtain the sub-rectangle of
     * the frame intended for presentation.
     */
    size_t crop_top;
    size_t crop_bottom;
    size_t crop_left;
    size_t crop_right;
 
    /**
     * AVBufferRef for internal use by a single libav* library.
     * Must not be used to transfer data between libraries.
     */
    AVBufferRef *private_ref;
} AVFrame;

其中,帧类型使用AVPictureType(位于libavutil/avutil.h)表示,枚举定义如下:

enum AVPictureType {
    AV_PICTURE_TYPE_NONE = 0, ///< Undefined
    AV_PICTURE_TYPE_I,     ///< Intra
    AV_PICTURE_TYPE_P,     ///< Predicted
    AV_PICTURE_TYPE_B,     ///< Bi-dir predicted
    AV_PICTURE_TYPE_S,     ///< S(GMC)-VOP MPEG-4
    AV_PICTURE_TYPE_SI,    ///< Switching Intra
    AV_PICTURE_TYPE_SP,    ///< Switching Predicted
    AV_PICTURE_TYPE_BI,    ///< BI type
};

AVFrame的分配与释放也有相应API,具体API以及使用实例如下:

API:
AVFrame *av_frame_alloc(void);
 
void av_frame_unref(AVFrame *frame);
 
void av_frame_free(AVFrame **frame);
 
demo:
AVFrame *frame = av_frame_alloc();
 
av_frame_unref(frame);
 
av_frame_free(&frame);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值