FFmpeg4.2源码之AVFrame、AVPacket

FFmpeg中与音视频数据联系非常深的两个数据结构是AVFrame和AVPacket。阅读它们的源码很重要。

AVFrame位于libavutil/frame.h。

/**
 * 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.
 * 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.
 *
 * The data described by an AVFrame is usually reference counted through the
 * AVBuffer API. The underlying buffer references are stored in AVFrame.buf /
 * AVFrame.extended_buf. An AVFrame is considered to be reference counted if at
 * least one reference is set, i.e. if AVFrame.buf[0] != NULL. In such a case,
 * every single data plane must be contained in one of the buffers in
 * AVFrame.buf or AVFrame.extended_buf.
 * There may be a single buffer for all the data, or one separate buffer for
 * each plane, or anything in between.
 *
 * sizeof(AVFrame) is not a part of the public ABI, so new fields may be added
 * to the end with a minor bump.
 *
 * Fields can be accessed through AVOptions, the name string used, matches the
 * C structure field name for fields accessible through AVOptions. The AVClass
 * for AVFrame can be obtained from avcodec_get_frame_class()
 */
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. Only linesize[0] may be set.
       For planar audio, each channel plane must be the same size. */
    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;
    int width, height;
    // number of audio samples (per channel).
    int nb_samples;
    /* Values correspond to enum AVPixelFormat for video frames, 
       enum AVSampleFormat for audio. */
    int format;
    int key_frame;
    enum AVPictureType pict_type;
    int64_t pts;
    int sample_rate;
    uint64_t channel_layout;
    AVBufferRef *buf[AV_NUM_DATA_POINTERS];
    AVBufferRef **extended_buf;
    int nb_extended_buf;
    int channels;
}

AVPacket位于libavcodec/avcodec.h

/**
 * 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 is one of the few structs in FFmpeg, whose size is a part of public
 * ABI. Thus it may be allocated on stack and no new fields can be added to it
 * without libavcodec and libavformat major bump.
 *
 * The semantics of data ownership depends on the buf field.
 * If it is set, the packet data is dynamically allocated and is
 * valid indefinitely until a call to av_packet_unref() reduces the
 * reference count to 0.
 *
 * If the buf field is not set av_packet_ref() would make a copy instead
 * of increasing the reference count.
 *
 * The side data is always allocated with av_malloc(), copied by
 * av_packet_ref() and freed by av_packet_unref().
 */
typedef struct AVPacket {
    AVBufferRef *buf;
    int64_t pts;
    int64_t dts;
    uint8_t *data;
    int   size;
    int   stream_index;

 

你可能感兴趣的:(FFmpeg,多媒体/音视频,FFmpeg)