首页 > 其他分享 >FFmpeg存放压缩后的音视频数据的结构体:AVPacket简介

FFmpeg存放压缩后的音视频数据的结构体:AVPacket简介

时间:2024-08-08 18:52:25浏览次数:8  
标签:AVPacket FFmpeg data av packet 音视频 FLAG AV 数据包

FFmpeg源码中通过AVPacket存储压缩后的音视频数据。它通常由解复用器(demuxers)输出,然后作为输入传递给解码器,或者从编码器作为输出接收,然后传递给多路复用器(muxers)。对于视频,它通常包含一个压缩帧;对于音频,它可能包含几个压缩帧。编码器允许输出不包含压缩音视频数据、只包含side data(边数据:例如,在编码结束时更新一些流参数)的空的数据包( empty packets)。

AVPacket结构体声明在FFmpeg源码(本文演示用的FFmpeg源码版本为7.0.1)的头文件libavcodec/packet.h中:

/**
 * @}
 */

/**
 * @defgroup lavc_packet AVPacket
 *
 * Types and functions for working with 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.
 *
 * 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().
 *
 * 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.
 *
 * @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.
     */
    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

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

成员变量buf:AVBufferRef类型指针,指向“存储packet(一个数据包的压缩后的音视频)数据的引用计数缓冲区”。如果为NULL,表示该packet数据未被引用计数。关于AVBufferRef类型可以参考:《FFmpeg引用计数数据缓冲区相关的结构体:AVBuffer、AVBufferRef简介》。

成员变量pts:显示时间戳(presentation time stamp)。即帧显示的时间刻度,用来告诉播放器在哪个时间点显示此帧(可以简单理解为这帧视频或音频数据的播放时间)。其单位不是秒,而是以AVStream->time_base为单位。pts必须大于或等于dts,因为一帧视频或音频数据必须在解码后才能播放,所以一帧视频或音频数据的显示/播放时间必须大于或等于解码时间。

成员变量dts:解码时间戳(Decompression timestamp)。即帧解码的时间刻度,用来告诉播放器在哪个时间点解码此帧。其单位不是秒,而是以AVStream->time_base为单位。

成员变量data:指针,指向“存放压缩后的一帧(对于视频通常包含一个压缩帧,对于音频可能包含几个压缩帧)音视频数据的缓冲区”。

成员变量size:成员变量data指向的缓冲区的大小,单位为字节。

成员变量stream_index:索引,用来标识该AVPacket所属的视频/音频流的序号,表示这是第几路流。注意:它是从0而不是从1开始的。

成员变量flags:AV_PKT_FLAG的组合。值有如下选择:

#define AV_PKT_FLAG_KEY     0x0001 ///< The packet contains a keyframe
#define AV_PKT_FLAG_CORRUPT 0x0002 ///< The packet content is corrupted
/**
 * Flag is used to discard packets which are required to maintain valid
 * decoder state but are not required for output and should be dropped
 * after decoding.
 **/
#define AV_PKT_FLAG_DISCARD   0x0004
/**
 * The packet comes from a trusted source.
 *
 * Otherwise-unsafe constructs such as arbitrary pointers to data
 * outside the packet may be followed.
 */
#define AV_PKT_FLAG_TRUSTED   0x0008
/**
 * Flag is used to indicate packets that contain frames that can
 * be discarded by the decoder.  I.e. Non-reference frames.
 */
#define AV_PKT_FLAG_DISPOSABLE 0x0010

AV_PKT_FLAG_KEY:数据包包含一个关键帧,即当前AVPacket是关键帧。

AV_PKT_FLAG_CORRUPT:数据包内容已损坏。

AV_PKT_FLAG_DISCARD:用于丢弃需要保持有效解码器状态但不需要输出的数据包,并且在解码后应该丢弃。带有该标志的AVPacket所携带的数据为解码器相关的信息,不会被解码出一幅图像。

AV_PKT_FLAG_TRUSTED:该数据包来自一个可信的源。

AV_PKT_FLAG_DISPOSABLE:用于指示包含可以被解码器丢弃的帧的数据包。也就是非参考帧。

成员变量side_data:存放“容器可以提供的额外数据包数据”的数组的首地址。数据包可以包含多种类型的边信息,比如,在编码结束的时候更新一些流的参数。指针side_data指向的空间存储了用于解码、展现或处理编码流的辅助信息,它通常由解复用器和编码器导出,可以以分组为单位提供给解码器和复用器,也可以作为全局侧数据(应用于整个编码流)。

成员变量side_data_elems: side_data的数量。

成员变量duration:该数据包的持续时间,以AVStream->time_base为单位。值为下一个数据包的pts减去当前数据包的pts。如果值为0表示未知。

成员变量pos:该数据包在流中的位置,单位为字节。值为-1表示未知。

成员变量opaque:指针,指向“存放用户使用的私有数据的缓冲区”。

成员变量time_base:AVRational类型,数据包时间戳的时间基准,即pts和dts的时间基。关于AVRational类型可以参考:《FFmpeg有理数相关的源码:AVRational结构体和其相关的函数分析》。

标签:AVPacket,FFmpeg,data,av,packet,音视频,FLAG,AV,数据包
From: https://blog.csdn.net/u014552102/article/details/140916507

相关文章

  • HarmonyOS 音视频之音频采集实战
    HarmonyOS音视频之音频采集实战背景应用开发过程中很多场景都有音频采集需求,比如聊天功能的发送语音功能,实时语音转文本功能,实时语音通话,实时视频通话等。在Android和iOS端,系统提供了两种形式:实时音频流采集音频文件录制系统还提供了不同形式的API,比如Android:AudioRec......
  • python合并音视频-通过moviepy模块合并音视频
    ......
  • ffmpeg和ffplay常用指令
    FFmpeg常见用法1.基本命令结构ffmpeg[global_options]-iinput_file[input_options]output_file[output_options]2.将其它格式图片转换为YUV420pffmpeg-iinput.jpg-pix_fmtyuv420poutput.yuv-iinput.jpg:指定输入文件input.jpg。-pix_fmtyuv420p:指定......
  • FFmpeg开发笔记(四十四)毕业设计可做的几个拉满颜值的音视频APP
    ​一年一度的毕业季就要到了,毕业设计算是大学生毕业前的最后一个大作业,尤其是计算机相关专业的毕业设计,通常要通过编程开发一个软件,比如开发一个图书馆管理系统,开发一个电商APP等等。一个好的毕业设计可以给作者加分,可以评优,还能获得编程开发的实战经验,所以很有必要认真去做毕业......
  • FFmpeg在游戏视频录制中的应用:画质与文件大小的综合比较
    我们游戏内的视频录制目前只支持avi固定码率,在玩家见面会上有玩家反馈希望改善录制画质,我最近在研究了有关视频画质的一些内容并做了一些统计。录制视频大小对比首先在游戏引擎中增加了对录制mp4格式的支持,并且使用h246编码可以直接在网页上播放无法再做转码测试场景:视频尺寸固......
  • FFmpeg开发笔记(四十三)使用SRS开启SRT协议的视频直播服务
    ​《FFmpeg开发实战:从零基础到短视频上线》一书在第10章介绍了轻量级流媒体服务器MediaMTX,通过该工具可以测试RTSP/RTMP等流媒体协议的推拉流。不过MediaMTX的功能实在是太简单了,无法应用于真实直播的生产环境,真正能用于生产环境的流媒体服务器还要看SRS或者ZLMediaKit。SRS是一......
  • FFmpeg是什么,主要功能介绍
     安装:sudoyuminstallffmpeg-yFFmpeg是一套开源的计算机程序,它主要用于记录、转换数字音频、视频,并能将其转化为流。FFmpeg提供了录制、转换以及流化音视频的完整解决方案,并包含了非常先进的音频/视频编解码库libavcodec等1。FFmpeg的主要功能和特点包括:视频采......
  • ffmpeg python 导致死锁
    我在使用ffmpegpython处理相机帧时遇到问题。我使用process.communicate()的第一种方法效果很好,但存在延迟问题。process=(ffmpeg.input('pipe:',format='rawvideo',pix_fmt='rgb24',s='{}x{}'.format(width,height))......
  • 在 Windows 上使用 FFmpeg
    在Windows上使用FFmpeg进行各种多媒体操作相对直接,但需要先完成安装。下面是详细的步骤,包括如何安装和使用FFmpeg:1.安装FFmpeg下载FFmpeg:访问FFmpeg官方网站。在Windows部分,点击WindowsbuildsbyBtbN或WindowsbuildsbyZeranoe(如果有可用)。解压FFmpe......
  • 【QT】QT 系统相关(事件、文件、多线程、网络、音视频)
    一、Qt事件1、事件介绍事件是应用程序内部或者外部产生的事情或者动作的统称。在Qt中使用一个对象来表示一个事件。所有的Qt事件均继承于抽象类QEvent。事件是由系统或者Qt平台本身在不同的时刻发出的。当用户按下鼠标、敲下键盘,或者是窗口需要重新绘制的时候,都会发......