参考链接
- FFMPEG结构体分析:AVPacket_雷霄骅的博客-CSDN博客_avpacket
AVPacket
- AVPacket是存储压缩编码数据相关信息的结构体
- 结构体的定义位于packet.h
- 重要参数介绍
- uint8_t *data:压缩编码的数据。
- 例如对于H.264来说。1个AVPacket的data通常对应一个NAL。
- 注意:在这里只是对应,而不是一模一样。他们之间有微小的差别:使用FFMPEG类库分离出多媒体文件中的H.264码流
- 因此在使用FFMPEG进行视音频处理的时候,常常可以将得到的AVPacket的data数据直接写成文件,从而得到视音频的码流文件。
- int size:data的大小
- int64_t pts:显示时间戳
- int64_t dts:解码时间戳
- int stream_index:标识该AVPacket所属的视频/音频流。
代码
/*** 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).* 对于视频,它通常应包含一个压缩帧。对于音频,它可能包含几个压缩帧。* 编码器被允许输出空包,没有压缩数据,只包含边数据(例如,在编码结束时更新一些流参数)** 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 to av_packet_unref() reduces the* reference count to 0.* 数据所有权的语义取决于 buf 字段。* 如果已设置,则数据包数据是动态分配的,并且无限期有效,直到调用 av_packet_unref() 将引用计数减少到 0** If the buf field is not set av_packet_ref() would make a copy instead* of increasing the reference count.* 如果未设置 buf 字段,av_packet_ref() 将进行复制而不是增加引用计数** The side data is always allocated with av_malloc(), copied by* av_packet_ref() and freed by av_packet_unref().* 边数据总是用 av_malloc() 分配的,由av_packet_ref() 并由 av_packet_unref() 释放* * sizeof(AVPacket) being a part of the public ABI is deprecated. once* av_init_packet() is removed, new packets will only be able to be allocated* with av_packet_alloc(), and new fields may be added to the end of the struct* with a minor bump.* sizeof(AVPacket) 作为公共 ABI 的一部分已被弃用。一次av_init_packet() 被删除,* 新的数据包只能用 av_packet_alloc() 分配,新的字段可能会被添加到结构的末尾,但会有轻微的凸起。** @see av_packet_alloc* @see av_packet_ref* @see av_packet_unref*/
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.* 可能为 NULL,则数据包数据不被引用计数*/AVBufferRef *buf;/*** Presentation timestamp in AVStream->time_base units; the time at which* the decompressed packet will be presented to the user.* 以 AVStream->time_base 单位表示的时间戳; 解压后的数据包将呈现给用户的时间* Can be AV_NOPTS_VALUE if it is not stored in the file.* 如果未存储在文件中,则可以是 AV_NOPTS_VALUE* 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.* pts 必须大于或等于 dts,因为在解压缩之前无法进行演示,除非想要查看十六进制转储* 某些格式滥用术语 dts 和 pts/cts 来表示不同的含义。 这样的时间戳必须转换为真正的 pts/dts 才能存储在 AVPacket 中*/int64_t pts;/*** Decompression timestamp in AVStream->time_base units; the time at which* the packet is decompressed. //AVStream->time_base 单位的解压时间戳; 数据包解压缩的时间* Can be AV_NOPTS_VALUE if it is not stored in the file. //如果没有存储在文件中,可以是 AV_NOPTS_VALUE*/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. //此数据包的持续时间以 AVStream->time_base 为单位,如果未知则为 0* Equals next_pts - this_pts in presentation order. //等于 next_pts - this_pts 按呈现顺序*/int64_t duration;int64_t pos; ///< byte position in stream, -1 if unknown/*** for some private data of the user //对于用户的一些私人数据*/void *opaque;/*** AVBufferRef for free use by the API user. FFmpeg will never check the* contents of the buffer ref. FFmpeg calls av_buffer_unref() on it when* the packet is unreferenced. av_packet_copy_props() calls create a new* reference with av_buffer_ref() for the target packet's opaque_ref field.* AVBufferRef 供 API 用户免费使用。 FFmpeg 永远不会检查缓冲区引用的内容。* 当数据包未被引用时,FFmpeg 在其上调用 av_buffer_unref()* av_packet_copy_props() 调用使用 av_buffer_ref() 为目标数据包的 opaque_ref 字段创建一个新引用* This is unrelated to the opaque field, although it serves a similar purpose.* 这与不透明字段无关,尽管它具有类似的目的* */AVBufferRef *opaque_ref;
。/*** Time base of the packet's timestamps. //数据包时间戳的时基* In the future, this field may be set on packets output by encoders or* demuxers, but its value will be by default ignored on input to decoders* or muxers.* 将来,此字段可能会在编码器或解复用器输出的数据包上设置,但默认情况下,* 在解码器或复用器的输入上将忽略其值*/AVRational time_base;
} AVPacket;