ffmpeg AVPacket结构体及其相关函数
0. 简介
AVPacket结构体并不是很复杂, 但是在ffmpeg中用的非常多. 与其相关的函数也是比较多.
AVPacket保存了解复用之后, 解码之前的数据, 和这些数据相关的一些附加信息.
对于视频来说, AVPacket通常包含一个压缩的frame; 而音频可能包含多个压缩的frame. 一个packet也可能是空的, 不包含任何压缩数据data, 而只包含附加数据side data(容器提供的关于packet的一些附加信息, 例如: 在编码结束的时候更新一些流的参数)
AVPacket是FFmpeg中为数不多的结构之一, 其大小是公共abi的一部分, 因此它可以在堆栈上分配, 如果libavcodec和libavformat没有很大的改动, 就不会向其中添加任何新字段.
官方文档:
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.
1. AVPacket数据结构定义
ffmpeg 版本3.4.1, struct AVPacket定义于<libavcodec/avcodec.h>
struct 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;
- AVBufferRef *buf;
用来管理data指针引用的数据缓存.
- int64_t pts;
显示时间, 比较关键的数据, 在做seek和播放进度的时候都要用到它, pts只是一个数量, 对应于AVStream->time_base, 要根据time_base才能转换为具体的时间, 音频和视频一般有不同的time_base, 所以在做音视频同步一定要做转换, 不能直接拿pts做.
- int64_t dts;
基本属性等同于pts, 区别就是dts对应的是解码时间.
- uint8_t *data;
保存指向未解码数据的指针.
- int size;
data的大小..
- int stream_index;
帧数据所属流的索引, 用来区分音频, 视频, 字幕等数据.
- int flags;
标志, 比如包含AV_PKT_FLAG_KEY值表示是关键帧.
#define AV_PKT_FLAG_KEY 0x0001
#define AV_PKT_FLAG_CORRUPT 0x0002
#define AV_PKT_FLAG_DISCARD 0x0004
#define AV_PKT_FLAG_TRUSTED 0x0008
- AVPacketSideData *side_data;
容器提供的一些附加数据
- int side_data_elems;
附加数据元素个数.
- int64_t duration;
数据的时长, 以所属媒体流的时间基准为单位, 未知则默认值0
- int64_t pos;
数据在媒体流中的位置, 未知则默认值-1
2. AVPacket相关函数
函数主要分为:
- AVPacket初始化
- AVPacket中data数据管理
- AVPacket中side_data数据管理
void av_init_packet(AVPacket *pkt);
初始化packet的值为默认值, 该函数不会影响data应用的数据缓存空间和size.
- int av_new_packet(AVPacket *pkt, int size);
内部调用了av_init_packet(pkt); 而且会为data分配空间.
- AVPacket *av_packet_alloc(void);
创建一个AVPacket, 将其字段设置为默认值, 没有给data分配空间.
- int av_packet_ref(AVPacket *dst, const AVPacket *src);
增加src->data引用计数.
如果src已经设置了引用计数, 则直接将引用计数+1;
如果src没有设置引用计数, 则创建一个新的引用计数buf, 并复制src->data到buf->buffer中.
最后复制src其它字段到dst中.
- void av_packet_unref(AVPacket *pkt);
将缓存空间的引用计数-1;
并将packet中的其它字段设置为初始值.
如果引用计数为0, 则释放缓存空间.
- void av_packet_free(AVPacket **pkt);
释放使用av_packet_alloc 创建的AVPacket, 该函数会调用av_packet_unref.
- AVPacket *av_packet_clone(const AVPacket *src);
函数内部调用了av_packet_alloc和av_packet_ref.
- int av_copy_packet(AVPacket *dst, const AVPacket *src);
复制一个新的packet, 包括数据缓存.
int av_copy_packet_side_data(AVPacket *pkt, const AVPacket *src);
复制src的附加数据到pkt.
- int av_grow_packet(AVPacket *pkt, int grow_by);
增大pkt->data指向的数据缓存.
- void av_shrink_packet(AVPacket *pkt, int size)
减小pkt->data指向的数据缓存.
ffmpeg AVPacket结构体及其相关函数的更多相关文章
- FFmpeg: AVPacket 结构体分析
AVPacket是FFmpeg中很重要的一个数据结构,它保存了解封装之后,解码之前的数据(注意:仍然是压缩后的数据)和关于这些数据的一些附加信息,如显示时间戳(pts).解码时间戳(dts).数据时长 ...
- ffmpeg AVFrame结构体及其相关函数
0. 简介 AVFrame中存储的是原始数据(例如视频的YUV, RGB, 音频的PCM), 此外还包含了一些相关的信息, 例如: 解码的时候存储了宏块类型表, QP表, 运动矢量等数据. 编码的时候 ...
- FFmpeg 常用结构体
0.FFmpeg 中最关键的结构体之间的关系 FFmpeg 中结构体很多.最关键的结构体可以分成以下几类: 1)解协议(http, rtsp, rtmp, mms) AVIOContext,URLPr ...
- Linux中表示“时间”的结构体和相关函数
转载于:http://blog.chinaunix.net/uid-25909722-id-2827364.html Linux中表示“时间”的结构体和相关函数 2011-09-13 17: ...
- FFmpeg: AVFormatContext 结构体分析
AVFormatContext 结构体分析这个结构体描述了一个媒体文件或媒体流的构成和基本信息.这是FFMpeg中最为基本的一个结构,是其他所有结构的根,是一个多媒体文件或流的根本抽象.主要成员释义: ...
- cdev结构体及其相关函数
一.在Linux2.6内核中一个字符设备用cdev结构来描述,其定义如下: struct cdev { struct kobject kobj; struct module *owner; //所属模 ...
- FFmpeg: AVCodecParameters 结构体分析
/** * This struct describes the properties of an encoded stream. * * sizeof(AVCodecParameters) is no ...
- FFMPEG结构体分析:AVPacket
注:写了一系列的结构体的分析的文章,在这里列一个列表: FFMPEG结构体分析:AVFrame FFMPEG结构体分析:AVFormatContext FFMPEG结构体分析:AVCodecConte ...
- FFMPEG中最关键的结构体之间的关系
FFMPEG中结构体很多.最关键的结构体可以分成以下几类: a) 解协议(http,rtsp,rtmp,mms) AVIOContext,URLProtocol,URLContext主要 ...
随机推荐
- codeforces 1272F dp+记录路径
题意 给出两个括号序列 \(S\) 和 \(T\),让你构造一个最短的合法括号序列使 \(S\) 和 \(T\) 是它的子序列. 分析 设 \(dp[i][j][k]\) 为这个最短的合法括号序列的前 ...
- Python中greenlet和gevent使用示例
目录 greenlet示例 示例1,线程切换 示例2 gevent 示例1 示例2: gevent使用monkey对所有系统自带的IO操作打patch 示例3,发送请求 示例4:使用gevent的so ...
- Raspberry Pi 4B FTP服务器配置
目录 1. 安装vsftpd并启动 2. 编辑配置文件 3. 重启服务 4. 测试 5. 为Web服务器添加管理员账户,便于通过ftp网站信息 参考资料:树莓派(raspberry pi)学习之安装f ...
- 文本处理三剑客之awk
简介 awk是一种处理文本文件的语言,是一个强大的文本编辑工具.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分在进行各种分析处理. gawk 用法:gawk [optio ...
- pip 安装报错
pip3 install uwsgi 报错 Command in /tmp/pip-build-5m77h_mm/uwsgi/ yum -y install python36-devel 解决
- 【SpringBoot】整体下载大文件与分批下载大文件(利用MySql数据库的Limit实现)
在前文里谈过一次性从数据库取一个大结果集有可能导致outofMemory,当时的想法是分批去取回来,今天把它实现了,特地把代码分享出来: 工程下载:https://files.cnblogs.com/ ...
- JAVA通过FTP方式向远程服务器或者客户端上传、下载文件,以及删除FTP服务器上的文件
1.在目标服务器上搭建FTP服务器 搭建方式有多种大家可以自行选择,例如使用Serv-U或者FTPServer.exe:这里我以FTPServer.exe为例搭建:在目标服务器(这里对应的IP是10. ...
- [go]gin框架
gin参考 Gin框架返回值 // 返回json func main() { r := gin.Default() //方法一: 自己拼接json // gin.H is a shortcut for ...
- onNewIntent
当Activity不是Standard模式,并且被复用的时候,会触发onNewIntent(Intent intent) 这个方法,一般用来获取新的Intent传递的数据 我们一般会把MainAcit ...
- 用VLC读取摄像头产生RTSP流,DSS侦听并转发(二)
用VLC读取摄像头产生RTSP流,DSS侦听并转发(二) 之前介绍过<用VLC读取摄像头产生RTSP流,DSS主动取流转发(一)>本文介绍另一种方法. 摄像机地址是192.1.101.51 ...