FFmpeg源码分析: AVFrame与AVPacket

2024-06-15 10:38

本文主要是介绍FFmpeg源码分析: AVFrame与AVPacket,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

FFmpeg有两个存储帧数据的结构体,其中AVPacket是解封装后保存压缩数据包,AVFrame是解码后保存音视频帧。

AVPacket结构体以前放在avcodec.h头文件中,在FFmpeg4.4以后放在单独packet.h头文件。官方对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结构体定义如下:

typedef struct AVPacket {AVBufferRef *buf;// 显示时间戳,单位为AVStream->time_baseint64_t pts;// 解码时间戳,单位为AVStream->time_baseint64_t dts;// 音视频数据uint8_t *data;// 数据包大小int   size;// 码流索引下标int   stream_index;// 帧类型int   flags;// 额外数据AVPacketSideData *side_data;int side_data_elems;// 帧显示时长,单位为AVStream->time_baseint64_t duration;// 数据包所在码流的positionint64_t pos;} AVPacket;

AVPacket的分配与释放有对应的API,需要注意的是释放所传的参数为AVPacket指针的地址。API与示例如下:

API:
AVPacket *av_packet_alloc(void);void av_packet_unref(AVPacket *pkt);void av_packet_free(AVPacket **pkt);demo:
AVPacket *pkt = av_packet_alloc();av_packet_unref(pkt);av_packet_free(&pkt);

AVFrame结构体位于frame.h头文件,用于存储解码后的音视频帧数据,使用av_frame_alloc进行分配,使用av_frame_free进行释放,AVFrame分配一次,多次复用,使用av_frame_unref可以去引用。官方关于AVFrame的描述如下:

/*** This structure describes decoded (raw) audio or video data.** AVFrame must be allocated using av_frame_alloc(). Note that this only* allocates the AVFrame itself, the buffers for the data must be managed* through other means (see below).* AVFrame must be freed with av_frame_free().** AVFrame is typically allocated once and then reused multiple times to hold* different data (e.g. a single AVFrame to hold frames received from a* decoder). In such a case, av_frame_unref() will free any references held by* the frame and reset it to its original clean state before it* is reused again.*/

AVFrame是解码后用于存储音视频帧,包括:data数组、width、height、pts、pkt_dts、pkt_size、pkt_duration、pkt_pos等信息,我们在判断是否为关键帧时,用key_frame参数进行判断。结构体的定义如下:

typedef struct AVFrame {
#define AV_NUM_DATA_POINTERS 8// pointer to the picture/channel planes.uint8_t *data[AV_NUM_DATA_POINTERS];/*** For video, size in bytes of each picture line.* For audio, size in bytes of each plane.** For audio, only linesize[0] may be set. For planar audio, each channel* plane must be the same size.** For video the linesizes should be multiples of the CPUs alignment* preference, this is 16 or 32 for modern desktop CPUs.* Some code requires such alignment other code can be slower without* correct alignment, for yet other it makes no difference.*/int linesize[AV_NUM_DATA_POINTERS];/*** pointers to the data planes/channels.** For video, this should simply point to data[].** For planar audio, each channel has a separate data pointer, and* linesize[0] contains the size of each channel buffer.* For packed audio, there is just one data pointer, and linesize[0]* contains the total size of the buffer for all channels.*/uint8_t **extended_data;/*** @name Video dimensions*/int width, height;/*** number of audio samples (per channel) described by this frame*/int nb_samples;/*** format of the frame, -1 if unknown or unset* Values correspond to enum AVPixelFormat for video frames,* enum AVSampleFormat for audio)*/int format;/*** 1 -> keyframe, 0-> not*/int key_frame;/*** Picture type of the frame.*/enum AVPictureType pict_type;/*** Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.*/AVRational sample_aspect_ratio;/*** Presentation timestamp in time_base units.*/int64_t pts;/*** DTS copied from the AVPacket that triggered returning this frame.* This is also the Presentation time of this AVFrame calculated from* only AVPacket.dts values without pts values.*/int64_t pkt_dts;/*** picture number in bitstream order*/int coded_picture_number;/*** picture number in display order*/int display_picture_number;/*** quality (between 1 (good) and FF_LAMBDA_MAX (bad))*/int quality;/*** for some private data of the user*/void *opaque;/*** When decoding, this signals how much the picture must be delayed.* extra_delay = repeat_pict / (2*fps)*/int repeat_pict;/*** The content of the picture is interlaced.*/int interlaced_frame;/*** If the content is interlaced, is top field displayed first.*/int top_field_first;/*** Tell user application that palette has changed from previous frame.*/int palette_has_changed;/*** reordered opaque 64 bits.*/int64_t reordered_opaque;/*** Sample rate of the audio data.*/int sample_rate;/*** Channel layout of the audio data.*/uint64_t channel_layout;/*** AVBuffer references backing the data for this frame. * If all elements of this array are NULL, * then this frame is not reference counted.*/AVBufferRef *buf[AV_NUM_DATA_POINTERS];/*** For planar audio which requires more than AV_NUM_DATA_POINTERS* AVBufferRef pointers, this array will hold all the references which* cannot fit into AVFrame.buf.*/AVBufferRef **extended_buf;/*** Number of elements in extended_buf.*/int        nb_extended_buf;AVFrameSideData **side_data;int            nb_side_data;/*** The frame data may be corrupted, e.g. due to decoding errors.*/
#define AV_FRAME_FLAG_CORRUPT       (1 << 0)
/*** A flag to mark the frames which need to be decoded.*/
#define AV_FRAME_FLAG_DISCARD   (1 << 2)/*** Frame flags, a combination of @ref lavu_frame_flags*/int flags;/*** MPEG vs JPEG YUV range.* - encoding: Set by user* - decoding: Set by libavcodec*/enum AVColorRange color_range;enum AVColorPrimaries color_primaries;enum AVColorTransferCharacteristic color_trc;/*** YUV colorspace type.* - encoding: Set by user* - decoding: Set by libavcodec*/enum AVColorSpace colorspace;enum AVChromaLocation chroma_location;/*** frame timestamp estimated using various heuristics, in stream time base* - encoding: unused* - decoding: set by libavcodec, read by user.*/int64_t best_effort_timestamp;/*** reordered pos from the last AVPacket that has been input into the decoder* - encoding: unused* - decoding: Read by user.*/int64_t pkt_pos;/*** duration of the corresponding packet, expressed in* AVStream->time_base units, 0 if unknown.* - encoding: unused* - decoding: Read by user.*/int64_t pkt_duration;/*** metadata.* - encoding: Set by user.* - decoding: Set by libavcodec.*/AVDictionary *metadata;/*** decode error flags of the frame, set to a combination of* FF_DECODE_ERROR_xxx flags if the decoder produced a frame,* but there were errors during the decoding.*/int decode_error_flags;
#define FF_DECODE_ERROR_INVALID_BITSTREAM   1
#define FF_DECODE_ERROR_MISSING_REFERENCE   2
#define FF_DECODE_ERROR_CONCEALMENT_ACTIVE  4
#define FF_DECODE_ERROR_DECODE_SLICES       8// number of audio channels, only used for audio.int channels;// size of the corresponding packet containing the compressed frame.int pkt_size;/*** For hwaccel-format frames, this should be a reference to the* AVHWFramesContext describing the frame.*/AVBufferRef *hw_frames_ctx;/*** 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 frame is unreferenced. av_frame_copy_props() calls create a new* reference with av_buffer_ref() for the target frame's opaque_ref field.*/AVBufferRef *opaque_ref;/*** Video frames only. The number of pixels to discard from the the* top/bottom/left/right border of the frame to obtain the sub-rectangle of* the frame intended for presentation.*/size_t crop_top;size_t crop_bottom;size_t crop_left;size_t crop_right;/*** AVBufferRef for internal use by a single libav* library.* Must not be used to transfer data between libraries.*/AVBufferRef *private_ref;
} AVFrame;

其中,帧类型使用AVPictureType(位于libavutil/avutil.h)表示,枚举定义如下:

enum AVPictureType {AV_PICTURE_TYPE_NONE = 0, ///< UndefinedAV_PICTURE_TYPE_I,     ///< IntraAV_PICTURE_TYPE_P,     ///< PredictedAV_PICTURE_TYPE_B,     ///< Bi-dir predictedAV_PICTURE_TYPE_S,     ///< S(GMC)-VOP MPEG-4AV_PICTURE_TYPE_SI,    ///< Switching IntraAV_PICTURE_TYPE_SP,    ///< Switching PredictedAV_PICTURE_TYPE_BI,    ///< BI type
};

AVFrame的分配与释放也有相应API,具体API以及使用实例如下:

API:
AVFrame *av_frame_alloc(void);void av_frame_unref(AVFrame *frame);void av_frame_free(AVFrame **frame);demo:
AVFrame *frame = av_frame_alloc();av_frame_unref(frame);av_frame_free(&frame);

这篇关于FFmpeg源码分析: AVFrame与AVPacket的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1063222

相关文章

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.

MySQL中的表连接原理分析

《MySQL中的表连接原理分析》:本文主要介绍MySQL中的表连接原理分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、环境3、表连接原理【1】驱动表和被驱动表【2】内连接【3】外连接【4编程】嵌套循环连接【5】join buffer4、总结1、背景

python中Hash使用场景分析

《python中Hash使用场景分析》Python的hash()函数用于获取对象哈希值,常用于字典和集合,不可变类型可哈希,可变类型不可,常见算法包括除法、乘法、平方取中和随机数哈希,各有优缺点,需根... 目录python中的 Hash除法哈希算法乘法哈希算法平方取中法随机数哈希算法小结在Python中,

Java Stream的distinct去重原理分析

《JavaStream的distinct去重原理分析》Javastream中的distinct方法用于去除流中的重复元素,它返回一个包含过滤后唯一元素的新流,该方法会根据元素的hashcode和eq... 目录一、distinct 的基础用法与核心特性二、distinct 的底层实现原理1. 顺序流中的去重

Python中对FFmpeg封装开发库FFmpy详解

《Python中对FFmpeg封装开发库FFmpy详解》:本文主要介绍Python中对FFmpeg封装开发库FFmpy,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、FFmpy简介与安装1.1 FFmpy概述1.2 安装方法二、FFmpy核心类与方法2.1 FF

关于MyISAM和InnoDB对比分析

《关于MyISAM和InnoDB对比分析》:本文主要介绍关于MyISAM和InnoDB对比分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录开篇:从交通规则看存储引擎选择理解存储引擎的基本概念技术原理对比1. 事务支持:ACID的守护者2. 锁机制:并发控制的艺

基于Linux的ffmpeg python的关键帧抽取

《基于Linux的ffmpegpython的关键帧抽取》本文主要介绍了基于Linux的ffmpegpython的关键帧抽取,实现以按帧或时间间隔抽取关键帧,文中通过示例代码介绍的非常详细,对大家的学... 目录1.FFmpeg的环境配置1) 创建一个虚拟环境envjavascript2) ffmpeg-py

MyBatis Plus 中 update_time 字段自动填充失效的原因分析及解决方案(最新整理)

《MyBatisPlus中update_time字段自动填充失效的原因分析及解决方案(最新整理)》在使用MyBatisPlus时,通常我们会在数据库表中设置create_time和update... 目录前言一、问题现象二、原因分析三、总结:常见原因与解决方法对照表四、推荐写法前言在使用 MyBATis

Python主动抛出异常的各种用法和场景分析

《Python主动抛出异常的各种用法和场景分析》在Python中,我们不仅可以捕获和处理异常,还可以主动抛出异常,也就是以类的方式自定义错误的类型和提示信息,这在编程中非常有用,下面我将详细解释主动抛... 目录一、为什么要主动抛出异常?二、基本语法:raise关键字基本示例三、raise的多种用法1. 抛

github打不开的问题分析及解决

《github打不开的问题分析及解决》:本文主要介绍github打不开的问题分析及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、找到github.com域名解析的ip地址二、找到github.global.ssl.fastly.net网址解析的ip地址三