ffmpeg常用数据结构4

AVPacket

AVPacket定义在avcodec.h中,如下:

typedefstructAVPacket {

    /**

     * 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_tpts;

    /**

     * 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_tdts;

    uint8_t *data;

int   size;

intstream_index;

int   flags;

    /**

     * Duration of this packet in AVStream->time_base units, 0 if unknown.

     * Equals next_pts - this_pts in presentation order.

     */

int   duration;

void  (*destruct)(structAVPacket *);

void  *priv;

int64_tpos;                            ///< 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.

     *

     * 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使用AVPacket来暂存解复用之后、解码之前的媒体数据(一个音/视频帧、一个字幕包等)及附加信息(解码时间戳、显示时间戳、时长等)。其中:

·         dts表示解码时间戳decoding time stamp ,pts表示显示时间戳presentation time stamp,它们的单位是所属媒体流的时间基准

·         stream_index给出所属媒体流的索引;

·         data为数据缓冲区指针,size为长度;

·         duration为数据的时长,也是以所属媒体流的时间基准为单位;

·         pos表示该数据在媒体流中字节偏移量

·         destruct为用于释放数据缓冲区的函数指针;

·         flags为标志域,其中,最低为置1表示该数据是一个关键帧

AVPacket结构本身只是个容器,它使用data成员引用实际的数据缓冲区。这个缓冲区通常是由av_new_packet创建的,但也可能由 FFMPEG的API创建(如av_read_frame)。当某个AVPacket结构的数据缓冲区不再被使用时,要需要通过调用 av_free_packet释放。av_free_packet调用的是结构体本身的destruct函数,它的值有两种情 况:1)av_destruct_packet_nofree或0;2)av_destruct_packet,其中,情况1)仅仅是将data和 size的值清0而已,情况2)才会真正地释放缓冲区。

 

FFMPEG内部使用AVPacket结构建立缓冲区装载数据,同时提供destruct函数,如果FFMPEG打算自己维护缓冲区,则将 destruct设为av_destruct_packet_nofree,用户调用av_free_packet清理缓冲区时并不能够将其释放;如果 FFMPEG打算将该缓冲区彻底交给调用者,则将destruct设为av_destruct_packet,表示它能够被释放。安全起见,如果用户希望 自由地使用一个FFMPEG内部创建的AVPacket结构,最好调用av_dup_packet进行缓冲区的克隆,将其转化为缓冲区能够被释放的 AVPacket,以免对缓冲区的不当占用造成异常错误。av_dup_packet会为destruct指针为 av_destruct_packet_nofree的AVPacket新建一个缓冲区,然后将原缓冲区的数据拷贝至新缓冲区,置data的值为新缓冲区 的地址,同时设destruct指针为av_destruct_packet。
原文地址:https://www.cnblogs.com/elesos/p/2990589.html