C/C++音視頻庫ffmpeg的數據包AVPacket分析html
ffmpeg下載地址 http://www.ffmpeg.club/app
AVPacket是ffmpeg用來存放編碼後的視頻幀數據,咱們來分析一下這個結構體,先貼出ffmpeg3.2中AVPacket聲明的源代碼:less
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;
咱們依次進行分析ide
AVBufferRef *buf;ui
用來存放引用計數的數據,若是沒有使用引用計數,值就是NULL,當你多個packet對象引用同一幀數據的時候用到。this
int64_t pts;編碼
本幀數據顯示的時間,比較關鍵的數據,在作seek和播放進度的時候都要用到它,pts只是一個數量,對應於AVStream->time_base,要根據time_base才能轉換爲具體的時間,音頻和視頻通常有不一樣的time_base,因此在作音視頻同步必定要作轉換,不能直接拿pts作。spa
轉換方式,好比轉爲毫秒orm
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就沒有此數據。
夏老師的課堂 http://edu.51cto.com/lecturer/12016059.html
http://edu.51cto.com/course/course_id-8059.html