本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/10410320.html

本文基于FFmpeg 4.1版本。

1. 数据结构定义

struct AVPacket定义于<libavcodec/avcodec.h>

struct AVPacket packet;

AVPacket中存储的是经过编码的压缩数据。在解码中,AVPacket由解复用器输出到解码器;在编码中,AVPacket由编码器输出到复用器。下图中,解复用器(demuxer)的输出和复用器(muxer)的输入“encoded data packets”的数据类型就是AVPacket:

 _______              ______________
| | | |
| input | demuxer | encoded data | decoder
| file | ---------> | packets | -----+
|_______| |______________| |
v
_________
| |
| decoded |
| frames |
|_________|
________ ______________ |
| | | | |
| output | <-------- | encoded data | <----+
| file | muxer | packets | encoder
|________| |______________|

对于视频而言,一个AVPacket通常只包含一个压缩视频帧。而对于音频而言,一个AVPacket可能包含多个完整的音频压缩帧。AVPacket也可以不包含压缩编码数据,而只包含side data,这种包可以称为空packet。例如,编码结束后只需要更新一些参数时就可以发空packet。

AVPacket对象可以在栈上分配,注意此处指的是AVPacket对象本身。而AVPacket中包含的数据缓冲区是通过av_malloc()在堆上分配的。

TODO: 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).
*
* 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 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().
*
* @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 #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;

音视频数据缓冲区

  • uint8_t *data:
  • int size:

    数据缓冲区地址与大小。音视频编码压缩数据存储于此片内存区域。此内存区域由AVBufferRef *buf管理。
  • AVBufferRef *buf:

    数据缓冲区引用,也可叫引用计数缓冲区。对上一字段uint8_t *data指向的内存区域提供引用计数等管理机制。

    AVBufferRef对数据缓冲区提供了管理机制,用户不应直接访问数据缓冲区。参考“FFmpeg数据结构AVBuffer

    如果buf值为NULL,则data指向的数据缓冲区不使用引用计数机制。av_packet_ref(dst, src)将执行数据缓冲区的拷贝,而非仅仅增加缓冲区引用计数。

    如果buf值非NULL,则data指向的数据缓冲区使用引用计数机制。av_packet_ref(dst, src)将不拷贝缓冲区,而仅增加缓冲区引用计数。av_packet_unref()将数据缓冲区引用计数减1,当缓冲区引用计数为0时,缓冲区内存被FFmpeg回收。

    对于struct AVPacket pkt对象,如果pkt.buf值非NULL,则有pkt.data == pkt.buf->data == pkt.buf->buffer.data

额外类型数据

  • AVPacketSideData *side_data
  • int side_data_elems

    由容器(container)提供的额外包数据。TODO: 待研究

packet属性

  • int64_t pts:

    显示时间戳。单位time_base,帧率的倒数。
  • int64_t dts:

    解码时间戳。单位time_base,帧率的倒数。
  • int stream_index:

    当前包(packet)所有流(stream)的索引(index)。
  • int flags:

    packet标志位。比如是否关键帧等。
  • int64_t duration:

    当前包解码后的帧播放持续的时长。单位timebase。值等于下一帧pts减当前帧pts。
  • int64_t pos:

    当前包在流中的位置,单位字节。

2. 关键函数实现

这里列出的几个关键函数,主要是为了帮助理解struct AVPacket数据结构

2.1 av_packet_ref()

int av_packet_ref(AVPacket *dst, const AVPacket *src)
{
int ret; ret = av_packet_copy_props(dst, src);
if (ret < 0)
return ret; if (!src->buf) {
ret = packet_alloc(&dst->buf, src->size);
if (ret < 0)
goto fail;
if (src->size)
memcpy(dst->buf->data, src->data, src->size); dst->data = dst->buf->data;
} else {
dst->buf = av_buffer_ref(src->buf);
if (!dst->buf) {
ret = AVERROR(ENOMEM);
goto fail;
}
dst->data = src->data;
} dst->size = src->size; return 0;
fail:
av_packet_free_side_data(dst);
return ret;
}

av_packet_ref()作了处理如下:

a) 如果src->buf为NULL,则将src缓冲区拷贝到新创建的dst缓冲区,注意src缓冲区不支持引用计数,但新建的dst缓冲区是支持引用计数的,因为dst->buf不为NULL。

b) 如果src->buf不为空,则dst与src共用缓冲区,调用av_buffer_ref()增加缓冲区引用计数即可。av_buffer_ref()分析参考“FFmpeg数据结构AVBuffer

2.2 av_packet_unref()

void av_packet_unref(AVPacket *pkt)
{
av_packet_free_side_data(pkt);
av_buffer_unref(&pkt->buf);
av_init_packet(pkt);
pkt->data = NULL;
pkt->size = 0;
}

av_packet_unref()注销AVPacket *pkt对象,并调用av_buffer_unref(&pkt->buf);将缓冲区引用计数减1。

av_buffer_unref()中将缓冲区引用计数减1后,若缓冲区引用计数变成0,则回收缓冲区内存。av_buffer_unref()分析参考“FFmpeg数据结构AVBuffer

3. 参考资料

[1] FFmpeg数据结构:AVPacket解析, https://www.cnblogs.com/wangguchangqing/p/5790705.html

4. 修改记录

2018-12-14 V1.0 初稿

FFmpeg数据结构AVPacket的更多相关文章

  1. FFmpeg数据结构AVFrame

    本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/10404502.html 本文基于FFmpeg 4.1版本. 1. 数据结构定义 stru ...

  2. FFmpeg数据结构:AVPacket解析

    本文主要从以下几个方面对AVPacket做解析: AVPacket在FFmpeg中的作用 字段说明 AVPacket中的内存管理 AVPacket相关函数的说明 结合AVPacket队列说明下AVPa ...

  3. FFmpeg 中AVPacket的使用

    AVPacket保存的是解码前的数据,也就是压缩后的数据.该结构本身不直接包含数据,其有一个指向数据域的指针,FFmpeg中很多的数据结构都使用这种方法来管理数据. AVPacket的使用通常离不开下 ...

  4. FFmpeg数据结构AVBuffer

    本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/10399048.html AVBuffer是FFmpeg中很常用的一种缓冲区,缓冲区使用引 ...

  5. FFmpeg之AVPacket

    花满楼原创 AVPacket,是压缩数据的结构体(解码前或编码后的结构体). 本文介绍FFmepg中常见结构AVPacekt,尽量用具体值来理解. 整个用于调试的代码可以这样写: #include & ...

  6. ffmpeg常用数据结构

    from :http://my.oschina.net/u/555701/blog/56748 AVCodecContext 这是一个描述编解码器上下文的数据结构,包含了众多编解码器需要的参数信息,如 ...

  7. C/C++音视频库ffmpeg的数据包AVPacket分析

    ffmpeg下载地址 http://www.ffmpeg.club/ AVPacket是ffmpeg用来存放编码后的视频帧数据,我们来分析一下这个结构体,先贴出ffmpeg3.2中AVPacket声明 ...

  8. FFmpeg详解

    认识FFMPEG FFMPEG堪称自由软件中最完备的一套多媒体支持库,它几乎实现了所有当下常见的数据封装格式.多媒体传输协议以及音视频编解码器.因此,对于从事多媒体技术开发的工程师来说,深入研究FFM ...

  9. 最新FFMPEG解码流程

    FFMPEG解码流程: 1. 注册所有容器格式和CODEC:  av_register_all() 2. 打开文件:                    av_open_input_file() 3 ...

随机推荐

  1. Python3基础知识之数据结构List和Tuple

    问题:今天学习python数据结构中的List和Tuple. 目标:了解二者的区别,学会一般的应用 相关知识:列表(List) : 类似于 .NET ArrayList / List.元组(Tuple ...

  2. 《C#从现象到本质》读书笔记(四)第4章C#和面向对象

    <C#从现象到本质>读书笔记第4章C#和面向对象 面向对象程序设计OOP 面向对象的三大特性是: 1)封装:类可以将它的成员私有化,只暴露它认为应当暴露给外界的成员.通过私有化成员,外界不 ...

  3. PHP-自定义数组-预定义数组-自定义函数-预定义函数

    (1)自定义数组 —— 项目中的重点 (2)PHP预定义数组 —— 重点&难点 (3)自定义函数 —— 了解 (4)PHP预定义函数 —— 项目中的重点 1.自定义数组 数组:array,一个 ...

  4. cms后台管理

    {项目名称:cms}-后台管理系统 项目阶段性总结报告 1 项目信息 开发工具:eclinpse,mysql,foxmail 使用到的技术:springMVC,springJDBC,maven,fre ...

  5. 1-学习tecplot360

    FETRIANGLE(3节点三角形), FEQUADRILATERAL(4节点四边形),FETETRAHEDRON(4节点四面体), FEBRICK(8节点六面体) 参考:https://wenku. ...

  6. ASP.NET对大文件上传的解决方案

    在ASP.NET 开发的过程中,最大的问题就在于上传大文件时让开发者尤为的头疼,而且,上传时无法方便的做到多线程的操控和上传进度的显示.笔者在此给大家推荐一款简单易用的上传组件,从而快速便捷得解决了 ...

  7. 调用redis的时候二维码不断刷新的排查

    一.背景和现象. 项目是PHP开发的,点击登录的时候就根据随机数生成了二维码,缓存在了redis.用户用微信扫描了二维码分析出需要请求的链接,然后微信浏览器就请求了服务器,服务器通过了随机数认证.正当 ...

  8. leetcode144-先序遍历非递归实现

    二叉树的先序/中序/后序遍历递归/非递归实现,讲的很清楚,其中后序遍历和先序中序的处理有些不一样: https://blog.yangx.site/2016/07/22/Python-binary-t ...

  9. Spark架构

    Spark架构    为了更好地理解调度,我们先来鸟瞰一下集群模式下的Spark程序运行架构图. 1. Driver Program        用户编写的Spark程序称为Driver Progr ...

  10. Base 64 加密、解密

    1.写一个公共类 package com.boyutec.oss.sys.utils; import java.io.UnsupportedEncodingException; import java ...