AVPacket

Posted wangyaoguo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AVPacket相关的知识,希望对你有一定的参考价值。

AVPacket注解

  • AVPacket 是一个结构体,存储压缩数据。可作为编码器的输出,解码器的输入。
  • 对于 Video 一般包含一个压缩帧,对于 Audio 可能包含多个压缩帧。
  • 编码器允许输出空 packets,没有包含压缩数据,仅包含附加数据,比如在编码结尾更新参数。
  • AVPackets 是 FFmpeg 中为数不多结构体,它的size的公有 ABI 的一部分,只能被 libavcodec 和 libavformat 在栈上分配。

This structure stores compressed data. It is typically exported by demuxers and then passed as input to decoders, or received as output from encoders and then passed to muxers.
For video, it should typically contain one compressed frame. For audio it may contain several compressed frames. Encoders are allowed to output empty packets, with no compressed data, containing only side data (e.g. to update some stream parameters at the end of encoding).
AVPacket is one of the few structs in FFmpeg, whose size is a part of public ABI. Thus it may be allocated on stack and no new fields can be added to it without libavcodec and libavformat major bump.
The semantics of data ownership depends on the buf field. If it is set, the packet data is dynamically allocated and is valid indefinitely until a call av_packet_unref() reduces the reference count to 0.
If the buf field is not set av_packet_ref() would make a copy instead of increasing the reference count.
The side data is always allocated with av_malloc(), copied by av_packet_ref() and freed by av_packet_unref().

AVPacket字段

  • AVBufferRef *buf 指向packet数据存储buffer的引用计数,为NULL,packet没有引用计数
  • int64_t pts 压缩数据显示时间戳,度量解码后的视频帧什么时候被显示出来
  • int64_t dts 压缩数据解码时间戳
  • 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 压缩数据时长
  • int64_t pos 压缩数据在媒体流中的偏移量

pts:压缩数据显示时间戳。比较关键的数据,在做seek和播放进度的时候都要用到它,pts只是一个数量,对应于AVStream->time_base,要根据time_base才能转换为具体的时间,音频和视频一般有不同的time_base,所以在做音视频同步一定要做转换,不能直接拿pts做

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;

以上是关于AVPacket的主要内容,如果未能解决你的问题,请参考以下文章

FFmpeg5.0源码阅读——AVPacket

FFmpeg AVPacket详解

FFmpeg: AVPacket 结构体分析

FFmpeg总结AV系列结构体之AVPacket

FFmpeg总结AV系列结构体之AVPacket

ffmpeg中AVPacket与AVFrame中数据的传递与释放