1.描述
AVPacket用于存儲壓縮數據,位于avcodec.h文件中。
通常由demuxer導出,然后作為輸入傳遞給解碼器;或者作為編碼器的輸出,然后傳遞給muxer。
對于視頻,它通常應包含一個壓縮幀。 對于音頻,它可能包含幾個壓縮幀。 允許編碼器輸出空分組,沒有壓縮數據,僅包含輔助數據(例如,在編碼結束時更新一些參數)。
2.結構體定義
typedef struct AVPacket {
/**
* A reference to the reference-counted buffer where the packet data is
* stored.
* May be NULL, then the packet data is not reference-counted.
*/
AVBufferRef *buf;
/**
* 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.
*/
AVPacketSideData *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.
*/
int64_t duration;
int64_t pos; ///< byte position in stream, -1 if unknown
#if FF_API_CONVERGENCE_DURATION
/**
* @deprecated Same as the duration field, but as int64_t. This was required
* for Matroska subtitles, whose duration values could overflow when the
* duration field was still an int.
*/
attribute_deprecated
int64_t convergence_duration;
#endif
} AVPacket;
3.常見變量及其作用
AVPacket這個結構體變量分析:
AVBufferRef *buf; //緩沖包數據存儲位置。
int64_t pts; //顯示時間戳,以AVStream->time_base為單位。
int64_t dts; //解碼時間戳,以AVStream->time_base為單位。
uint8_t *data; //壓縮編碼的數據。
int size;//data的大小。
int stream_index;//給出所屬媒體流的索引。
int flags;//標識域,其中,最低位置1表示該數據是一個關鍵幀。
AVPacketSideData *side_data;//容器提供的額外數據包。
int64_t duration;//Packet持續的時間,以AVStream->time_base為單位。
int64_t pos;//表示該數據在媒體流中的字節偏移量。
AVPacket結構本身只是一個容器,它使用data成員引用實際的數據緩沖區。這個緩沖區通常是由av_new_packet創建的,但也可能由FFmpeg的API創建(如av_read_frame)。當某個AVPacket結構的數據緩沖區不再被使用時,要需要通過調用av_free_packet釋放。
這個結構體在FFmpeg中非常常見,需要重點掌握。