1. 程式人生 > >FFMPEG結構體分析:AVPacket

FFMPEG結構體分析:AVPacket

注:寫了一系列的結構體的分析的文章,在這裡列一個列表:


FFMPEG有幾個最重要的結構體,包含了解協議,解封裝,解碼操作,此前已經進行過分析:

在此不再詳述,其中AVPacket是儲存壓縮編碼資料相關資訊的結構體。本文將會詳細分析一下該結構體裡重要變數的含義和作用。

首先看一下結構體的定義(位於avcodec.h檔案中):

/* 雷霄驊
 * 中國傳媒大學/數字電視技術
 * [email protected]
 *
 */
typedef struct AVPacket {
    /**
     * Presentation timestamp in AVStream->time_base units; the time at which
     * the decompressed packet will be presented to the user.
     * Can be AV_NOPTS_VALUE if it is not stored in the file.
     * pts MUST be larger or equal to dts as presentation cannot happen before
     * decompression, unless one wants to view hex dumps. Some formats misuse
     * the terms dts and pts/cts to mean something different. Such timestamps
     * must be converted to true pts/dts before they are stored in AVPacket.
     */
    int64_t pts;
    /**
     * Decompression timestamp in AVStream->time_base units; the time at which
     * the packet is decompressed.
     * Can be AV_NOPTS_VALUE if it is not stored in the file.
     */
    int64_t dts;
    uint8_t *data;
    int   size;
    int   stream_index;
    /**
     * A combination of AV_PKT_FLAG values
     */
    int   flags;
    /**
     * Additional packet data that can be provided by the container.
     * Packet can contain several types of side information.
     */
    struct {
        uint8_t *data;
        int      size;
        enum AVPacketSideDataType type;
    } *side_data;
    int side_data_elems;

    /**
     * Duration of this packet in AVStream->time_base units, 0 if unknown.
     * Equals next_pts - this_pts in presentation order.
     */
    int   duration;
    void  (*destruct)(struct AVPacket *);
    void  *priv;
    int64_t pos;                            ///< byte position in stream, -1 if unknown

    /**
     * Time difference in AVStream->time_base units from the pts of this
     * packet to the point at which the output from the decoder has converged
     * independent from the availability of previous frames. That is, the
     * frames are virtually identical no matter if decoding started from
     * the very first frame or from this keyframe.
     * Is AV_NOPTS_VALUE if unknown.
     * This field is not the display duration of the current packet.
     * This field has no meaning if the packet does not have AV_PKT_FLAG_KEY
     * set.
     *
     * The purpose of this field is to allow seeking in streams that have no
     * keyframes in the conventional sense. It corresponds to the
     * recovery point SEI in H.264 and match_time_delta in NUT. It is also
     * essential for some types of subtitle streams to ensure that all
     * subtitles are correctly displayed after seeking.
     */
    int64_t convergence_duration;
} AVPacket;

在AVPacket結構體中,重要的變數有以下幾個:

uint8_t *data:壓縮編碼的資料。

例如對於H.264來說。1個AVPacket的data通常對應一個NAL。

因此在使用FFMPEG進行視音訊處理的時候,常常可以將得到的AVPacket的data資料直接寫成檔案,從而得到視音訊的碼流檔案。

int   size:data的大小

int64_t pts:顯示時間戳

int64_t dts:解碼時間戳

int   stream_index:標識該AVPacket所屬的視訊/音訊流。

這個結構體雖然比較簡單,但是非常的常用。