av_read_frame读出的视频流数据在AVPacket中的存储

1、先看看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.
     */
    int   duration;
#if FF_API_DESTRUCT_PACKET
    attribute_deprecated
    void  (*destruct)(struct AVPacket *);
    attribute_deprecated
    void  *priv;
#endif
    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;
这应该是ffmpeg中定义算很少的结构体了,大致意思注释已经说很清楚了。

2、下面说下我的理解

data存储的是从文件中读出来的流数据。

pts和dts为显示、解码时间戳,他们两个乘以AVStream中的timebase可以得到真实的时间。

flags可以通过if(pkt.flags &AV_PKT_FLAG_KEY )来判断是否为关键帧。

//////如果是h264编码,从帧数据判断
frame = |length|data|
length后面紧跟着的第一个byte后5位如果是01001则为关键帧
if((frame[4] & 0x1f) == 5)
{
keyframe
}

else

{
not keyframe
}

pos是此帧在源文件中的位置。

stream_index为流数据索引号。

size为此packet大小。

3、源文件中的数据如何在packet的data中存储的

以avi文件为例,把packet数据保存到文件并和源文件比较,看下图:
av_read_frame读出的视频流数据在AVPacket中的存储_第1张图片
左边是packet保存的数据,右边是源文件数据,此帧是一个关键帧。
再看下图,此图是程序中对应packet的数据视图:
av_read_frame读出的视频流数据在AVPacket中的存储_第2张图片
可以看出源文件中00dc表示视频流数据,12 30 00 00 四个字节表示长度,根据存储规则,长度为0x3012,pos为26ee,第一个四字节为00 00 01 00为关键帧标示,和源文件一一对应。

再看下图,为非关键帧和源文件对比:
av_read_frame读出的视频流数据在AVPacket中的存储_第3张图片
左边是packet数据,右边是源文件,
再来看与之对应的packet数据视图:
av_read_frame读出的视频流数据在AVPacket中的存储_第4张图片
可以看出packet存储的是源文件00dc和数据长度后面的数据。
从关键帧和非关键帧的对比中可以看出,在avi中data的头4字节标示了是否为关键帧。

以x264编码的MP4文件:
下图是read出的第一个视频流pack:
av_read_frame读出的视频流数据在AVPacket中的存储_第5张图片
可以看到,很多关于x264的参数都在这里。




你可能感兴趣的:(ffmpeg,ffmpeg学习笔记)