C/C++音視頻庫ffmpeg的數據包AVPacket分析

C/C++音視頻庫ffmpeg的數據包AVPacket分析

ffmpeg下載地址 http://www.ffmpeg.club/
AVPacket是ffmpeg用來存放編碼後的視頻幀數據,我們來分析一下這個結構體,先貼出ffmpeg3.2中AVPacket聲明的源代碼:
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;
我們依次進行分析
AVBufferRef *buf;
用來存放引用計數的數據,如果沒有使用引用計數,值就是NULL,當你多個packet對象引用同一幀數據的時候用到。
int64_t pts;
本幀數據顯示的時間,比較關鍵的數據,在做seek和播放進度的時候都要用到它,pts只是一個數量,對應於AVStream->time_base,要根據time_base才能轉換爲具體的時間,音頻和視頻一般有不同的time_base,所以在做音視頻同步一定要做轉換,不能直接拿pts做。
轉換方式,比如轉爲毫秒
AVFormatContext *ic = NULL;
static double r2d(AVRational r)
{
return r.num == 0 || r.den == 0 ? 0. : (double)r.num / (double)r.den;
}
//。。。
int pts = (pkt->pts *r2d(ic->streams[pkt->stream_index]->time_base)) * 1000;
int64_t dts;
基本屬性等同於pts,區別就是dts對應的是解碼時間不是顯示時間,解碼後會放入緩衝,比如h264,如果有b幀,則要先解碼後面的b幀,再解碼之前的幀。
uint8_t *data; int size;
幀的數據和數據大小
int stream_index;
幀數據所屬流的索引,用來區分音頻,視頻,和字幕數據。
int flags;
標誌,其中爲1表示該數據是一個關鍵幀
AV_PKT_FLAG_KEY 0x0001 關鍵幀
AVPacketSideData *side_data;
int side_data_elems;
容器提供的一些附加數據
int64_t duration;
下一幀pts - 當前幀pts ,也就表示兩幀時間間隔。
int64_t pos;
當前幀數據在文件中的位置(字節爲單位),如果做文件移位用到,如果rtsp就沒有此數據。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章