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数据保存到文件并和源文件比较,看下图:

左边是packet保存的数据,右边是源文件数据,此帧是一个关键帧。
再看下图,此图是程序中对应packet的数据视图:

可以看出源文件中00dc表示视频流数据,12 30 00 00 四个字节表示长度,根据存储规则,长度为0x3012,pos为26ee,第一个四字节为00 00 01 00为关键帧标示,和源文件一一对应。

再看下图,为非关键帧和源文件对比:

左边是packet数据,右边是源文件,
再来看与之对应的packet数据视图:

可以看出packet存储的是源文件00dc和数据长度后面的数据。
从关键帧和非关键帧的对比中可以看出,在avi中data的头4字节标示了是否为关键帧。

以x264编码的MP4文件:
下图是read出的第一个视频流pack:

可以看到,很多关于x264的参数都在这里。




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dancing_night

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值