从一篇博客看的ffmpeg各种数据结构的关系 。博客的地址为:http://blog.csdn.net/leixiaohua1020/article/details/11693997
FFMPEG中结构体很多。最关键的结构体可以分成以下几类:
a) 解协议(http,rtsp,rtmp,mms)
AVIOContext,URLProtocol,URLContext主要存储视音频使用的协议的类型以及状态。URLProtocol存储输入视音频使用的封装格式。每种协议都对应一个URLProtocol结构。(注意:FFMPEG中文件也被当做一种协议“file”)
b) 解封装(flv,avi,rmvb,mp4)
AVFormatContext主要存储视音频封装格式中包含的信息;AVInputFormat存储输入视音频使用的封装格式。每种视音频封装格式都对应一个AVInputFormat 结构。
c) 解码(h264,mpeg2,aac,mp3)
每个AVStream存储一个视频/音频流的相关数据;每个AVStream对应一个AVCodecContext,存储该视频/音频流使用解码方式的相关数据;每个AVCodecContext中对应一个AVCodec,包含该视频/音频对应的解码器。每种解码器都对应一个AVCodec结构。
d) 存数据
视频的话,每个结构一般是存一帧;音频可能有好几帧
解码前数据:AVPacket
解码后数据:AVFrame
他们之间的对应关系如下所示:
下面就各种数据结构进行源码的阅读
一 .AVFormatContext
首先看看这个数据结构的注释和位置,它在avformat.h文件中,是对源的数据结构的抽象,源包括输入源和输出源,对于该数据结构,官网有着详细的说明
官网对该结构体的说明:http://ffmpeg.org/doxygen/1.0/structAVFormatContext.html
#include <avformat.h>
Data Fields
const AVClass * av_class A class for logging and AVOptions. struct AVInputFormat * iformat Can only be iformat or oformat, not both at the same time. struct AVOutputFormat * oformat void * priv_data Format private data. AVIOContext * pb I/O context. int ctx_flags Format-specific flags, see AVFMTCTX_xx. unsigned int nb_streams A list of all streams in the file. AVStream ** streams char filename [1024] input or output filename int64_t start_time Decoding: position of the first frame of the component, in AV_TIME_BASE fractional seconds. int64_t duration Decoding: duration of the stream, in AV_TIME_BASE fractional seconds. int bit_rate Decoding: total stream bitrate in bit/s, 0 if not available. unsigned int packet_size int max_delay int flags unsigned int probesize decoding: size of data to probe; encoding: unused. int max_analyze_duration decoding: maximum time (in AV_TIME_BASE units) during which the input should be analyzed in avformat_find_stream_info(). const uint8_t * key int keylen unsigned int nb_programs AVProgram ** programs enum AVCodecID video_codec_id Forced video codec_id. //枚举类型 视频的ID enum AVCodecID audio_codec_id Forced audio codec_id. //音频的ID enum AVCodecID subtitle_codec_id //字幕的ID 不知道水印的也是不是这个ID Forced subtitle codec_id. unsigned int max_index_size Maximum amount of memory in bytes to use for the index of each stream. unsigned int max_picture_buffer Maximum amount of memory in bytes to use for buffering frames obtained from realtime capture devices. unsigned int nb_chapters AVChapter ** chapters AVDictionary * metadata int64_t start_time_realtime Start time of the stream in real world time, in microseconds since the unix epoch (00:00 1st January 1970). int fps_probe_size decoding: number of frames used to probe fps int error_recognition Error recognition; higher values will detect more errors but may misdetect some more or less valid parts as errors. AVIOInterruptCB interrupt_callback Custom interrupt callbacks for the I/O layer. int debug Flags to enable debugging. int ts_id Transport stream id. int audio_preload Audio preload in microseconds. int max_chunk_duration Max chunk time in microseconds. int max_chunk_size Max chunk size in bytes Note, not all formats support this and unpredictable things may happen if it is used when not supported. int use_wallclock_as_timestamps forces the use of wallclock timestamps as pts/dts of packets This has undefined results in the presence of B frames. int avoid_negative_ts Avoids negative timestamps during muxing 0 -> allow negative timestamps 1 -> avoid negative timestamps -1 -> choose automatically (default) Note, this is only works when interleave_packet_per_dts is in use
- encoding: Set by user via AVOptions (NO direct access)
- decoding: unused.
int avio_flags avio flags, used to force AVIO_FLAG_DIRECT. enum AVDurationEstimationMethod duration_estimation_method The duration field can be estimated through various ways, and this field can be used to know how the duration was estimated. struct AVPacketList * packet_buffer This buffer is only needed when packets were already buffered but not decoded, for example to get the codec parameters in MPEG streams. struct AVPacketList * packet_buffer_end int64_t data_offset offset of the first packet struct AVPacketList * raw_packet_buffer Raw packets from the demuxer, prior to parsing and decoding. struct AVPacketList * raw_packet_buffer_end struct AVPacketList * parse_queue Packets split by the parser get queued here. struct AVPacketList * parse_queue_end int raw_packet_buffer_remaining_size
Detailed Description
Format I/O context.New fields can be added to the end with minor version bumps. Removal, reordering and changes to existing fields require a major version bump. sizeof(AVFormatContext) must not be used outside libav*, useavformat_alloc_context() to create an AVFormatContext.
Definition at line 894 of file avformat.h.
const AVClass *av_class:日志和AVOptions的类
struct AVInputFormat* iformat:复用器muxer,注意,muxer和demuxer同时只能存在一个
struct AVOutputFormat* oformat:解复用器demuxer
void* priv_data:容器的私有数据
AVIOContext* pb:I/O上下文信息
int ctx_flags:容器特定的标志,即AVFMTCTX_*
unsigned int nb_streams:文件中所有数据流的列表
AVStream** streams:数据流的抽象,二级指针,用以存放多个数据流,其中**streams指向每一个数据流具体内容,*streams指向每一个数据流的首地址,streams指向数据流串的首地址
char* filename[1024]:输入或输出的文件名
int64_t start_time:解码时用:数据流中第一帧在时间轴上的位置,以AV_TIME_BASE为单位,微秒级
int64_t duration:解码时用:数据流的持续时间,以AV_TIME_BASE为单位,微秒级
int bit_rate:解码时用:整个数据流的比特率,以bit/s为单位,若不可用,则为0
unsigned int packet_size:数据包的尺寸
int max_delay:最大的停顿,是指在最后期限之前已经完成编解码,这段空余的时间
int flags:一些标志信息,包括:
00972 #define AVFMT_FLAG_GENPTS 0x0001 产生pts
00973 #define AVFMT_FLAG_IGNIDX 0x0002 忽略index
00974 #define AVFMT_FLAG_NONBLOCK 0x0004 不分块
00975 #define AVFMT_FLAG_IGNDTS 0x0008 忽略dts
00976 #define AVFMT_FLAG_NOFILLIN 0x0010 不填充,不填充什么呢
00977 #define AVFMT_FLAG_NOPARSE 0x0020 不解析,不解析什么呢
00978 #define AVFMT_FLAG_NOBUFFER 0x0040 没有缓冲
00979 #define AVFMT_FLAG_CUSTOM_IO 0x0080 自定义IO
00980 #define AVFMT_FLAG_DISCARD_CORRUPT 0x0100 丢弃破坏了的数据
00981 #define AVFMT_FLAG_MP4A_LATM 0x8000 MP4相关的,不懂
00982 #define AVFMT_FLAG_SORT_DTS 0x10000 选择的dts?
00983 #define AVFMT_FLAG_PRIV_OPT 0x20000 私有的参数选项
00984 #define AVFMT_FLAG_KEEP_SIDE_DATA 0x40000 保持side_data的标志
unsigned int probesize:解码时用:将要探测的数据的尺寸
int max_analyse_duration:解码时用:输入数据流在avformat_find_stream_info()函数里分析时所用的最大时间,以AV_TIME_BASE为单位
enum AVCodecID video_codec_id:强制性的视频编解码器的id
int max_chunk_size:数据块最大的生存时间,以微秒为单位
struct AVPacketList* packet_buffer:当数据包已经缓冲完毕但是还没有解码时,使用这个缓冲区域;
struct AVPacketList * packet_buffer_end:什么意思?
/* av_seek_frame() support */
int64_t data_offset:第一个数据包的偏移,为啥是64位整型呢
struct AVPakcetList* raw_packet_buffer:来自解复用器的原始数据包,在解析和解码之前存在
struct AVPacketList * raw_packet_buffer_end:什么意思?
struct AVPacketList* parse_queue:数据包经解析器划分后,在这个数据结构中成为队列
struct AVPacketList *parse_queue_end:什么意思?
对出现的数据结构说明:
1.
二 .AVClass
#include <log.h>
Data Fields
const char * class_name The name of the class; usually it is the same name as the context structure type to which the AVClass is associated. const char *(* item_name )(void *ctx) A pointer to a function which returns the name of a context instance ctx associated with the class. struct AVOption * option a pointer to the first option specified in the class if any or NULL //标明这个类 int version LIBAVUTIL_VERSION with which this structure was created. int log_level_offset_offset Offset in the structure where log_level_offset is stored. int parent_log_context_offset Offset in the structure where a pointer to the parent context for loging is stored. void *(* child_next )(void *obj, void *prev) Return next AVOptions-enabled child or NULL. struct AVClass *(* child_class_next )(const struct AVClass *prev) Return an AVClass corresponding to next potential AVOptions-enabled child. AVClassCategory category Category used for visualization (like color) This is only set if the category is equal for all objects using this class. AVClassCategory(* get_category )(void *ctx) Callback to return the category.
Detailed Description
Describe the class of an AVClass context structure.That is an arbitrary(任意) struct of which the first field is a pointer to an AVClass struct (e.g. AVCodecContext, AVFormatContext etc.).//任意的数据结构的第一个字段都是该数据类型
Definition at line 48 of file log.h.
对于出现的数据结构,下面给出一个说明
1.AVOption类
这个类是干什么的呢? 对所有数据结构(不仅仅只是抽象的数据结构,而是所有的)进行赋值的抽象数据结构
#include <opt.h>
Data Fields
const char * name const char * help short English help text int offset The offset relative to the context structure where the option value is stored. enum AVOptionType type union { int64_t i64 double dbl const char * str AVRational q } default_val the default value for scalar options double min minimum valid value for the option double max maximum valid value for the option int flags const char * unit The logical unit to which the option belongs.
Detailed Description
AVOption 枚举类型
a .AVRational 说明:
#include <rational.h>
Data Fields
int num numerator int den denominator
Detailed Description
rational number numerator/denominator(有理数的分子分母)
Definition at line 43 of file rational.h.
2.AVClassCategory是一个枚举类型
Defines
#define AV_LOG_QUIET -8 #define AV_LOG_PANIC 0 Something went really wrong and we will crash now. #define AV_LOG_FATAL 8 Something went wrong and recovery is not possible. #define AV_LOG_ERROR 16 Something went wrong and cannot losslessly be recovered. #define AV_LOG_WARNING 24 Something somehow does not look correct. #define AV_LOG_INFO 32 #define AV_LOG_VERBOSE 40 #define AV_LOG_DEBUG 48 Stuff which is only useful for libav* developers. #define AV_LOG_MAX_OFFSET (AV_LOG_DEBUG - AV_LOG_QUIET) #define av_dlog(pctx,...) do { if (0) av_log(pctx, AV_LOG_DEBUG, __VA_ARGS__); } while (0) av_dlog macros Useful to print debug messages that shouldn't get compiled in normally. #define AV_LOG_SKIP_REPEATED 1 Skip repeated messages, this requires the user app to use av_log() instead of (f)printf as the 2 would otherwise interfere and lead to "Last message repeated x times" messages below (f)printf messages with some bad luck.
Enumerations
enum AVClassCategory {
AV_CLASS_CATEGORY_NA = 0, AV_CLASS_CATEGORY_INPUT, AV_CLASS_CATEGORY_OUTPUT, AV_CLASS_CATEGORY_MUXER,
AV_CLASS_CATEGORY_DEMUXER, AV_CLASS_CATEGORY_ENCODER, AV_CLASS_CATEGORY_DECODER, AV_CLASS_CATEGORY_FILTER,
AV_CLASS_CATEGORY_BITSTREAM_FILTER, AV_CLASS_CATEGORY_SWSCALER, AV_CLASS_CATEGORY_SWRESAMPLER, AV_CLASS_CATEGORY_NB
}
三.AVStream
AVStream结构保存与数据流相关的编解码器、数据段等信息。比较重要的有如下两
个成员:AVCodecContext *codec; void *priv_data; 其中codec指针保存的就是
encoder或者decoder结构。priv_data指针保存的是和具体编解码流相关的数据。
如ASF解码过程中,priv_data保存的就是ASFStream结构体的数据。
#include <avformat.h>
Data Fields
int index stream index in AVFormatContext int id Format-specific stream ID. AVCodecContext * codec Codec context associated with this stream. AVRational r_frame_rate Real base framerate of the stream. void * priv_data struct AVFrac pts encoding: pts generation when outputting stream AVRational time_base This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented. int64_t start_time Decoding: pts of the first frame of the stream in presentation order, in stream time base. int64_t duration Decoding: duration of the stream, in stream time base. int64_t nb_frames number of frames in this stream if known or 0 int disposition AV_DISPOSITION_* bit field. enum AVDiscard discard Selects which packets can be discarded at will and do not need to be demuxed. AVRational sample_aspect_ratio sample aspect ratio (0 if unknown)
- encoding: Set by user.
AVDictionary * metadata AVRational avg_frame_rate Average framerate. AVPacket attached_pic For streams with AV_DISPOSITION_ATTACHED_PIC disposition, this packet will contain the attached picture. struct { int64_t last_dts int64_t duration_gcd int duration_count double duration_error [2][2][MAX_STD_TIMEBASES] int64_t codec_info_duration int found_decoder int64_t fps_first_dts Those are used for average framerate estimation. int fps_first_dts_idx int64_t fps_last_dts int fps_last_dts_idx } info int pts_wrap_bits number of bits in pts (used for wrapping control) int64_t reference_dts Timestamp corresponding to the last dts sync point. int64_t first_dts int64_t cur_dts int64_t last_IP_pts int last_IP_duration int probe_packets int codec_info_nb_frames Number of frames that have been demuxed during av_find_stream_info(). int stream_identifier Stream Identifier This is the MPEG-TS stream identifier +1 0 means unknown. int64_t interleaver_chunk_size int64_t interleaver_chunk_duration enum AVStreamParseType need_parsing struct AVCodecParserContext * parser struct AVPacketList * last_in_packet_buffer last packet in packet_buffer for this stream when muxing. AVProbeData probe_data int64_t pts_buffer [MAX_REORDER_DELAY+1] AVIndexEntry * index_entries Only used if the format does not support seeking natively. int nb_index_entries unsigned int index_entries_allocated_size int request_probe stream probing state -1 -> probing finished 0 -> no probing requested rest -> perform probing with request_probe being the minimum score to accept. int skip_to_keyframe Indicates that everything up to the next keyframe should be discarded. int skip_samples Number of samples to skip at the start of the frame decoded from the next packet. int nb_decoded_frames Number of internally decoded frames, used internally in libavformat, do not access its lifetime differs from info which is why its not in that structure. int64_t mux_ts_offset Timestamp offset added to timestamps before muxing NOT PART OF PUBLIC API.
Detailed Description
Stream structure.New fields can be added to the end with minor version bumps. Removal, reordering and changes to existing fields require a major version bump. sizeof(AVStream) must not be used outside libav*.
Definition at line 632 of file avformat.h.
需要了解的数据结构:
1. AVDictionary
Data Fields
int count AVDictionaryEntry * elems
Detailed Description
四.AVOutputFormat详细解析
Data Fields
const char * name const char * long_name Descriptive name for the format, meant to be more human-readable than name. const char * mime_type const char * extensions comma-separated filename extensions enum AVCodecID audio_codec default audio codec enum AVCodecID video_codec default video codec enum AVCodecID subtitle_codec default subtitle codec int flags can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_RAWPICTURE, AVFMT_GLOBALHEADER, AVFMT_NOTIMESTAMPS, AVFMT_VARIABLE_FPS, AVFMT_NODIMENSIONS, AVFMT_NOSTREAMS, AVFMT_ALLOW_FLUSH, AVFMT_TS_NONSTRICT struct AVCodecTag *const * codec_tag List of supported codec_id-codec_tag pairs, ordered by "better choice first". const AVClass * priv_class AVClass for the private context. struct AVOutputFormat * next int priv_data_size size of private data so that it can be allocated in the wrapper int(* write_header )(struct AVFormatContext *) int(* write_packet )(struct AVFormatContext *, AVPacket *pkt) Write a packet. int(* write_trailer )(struct AVFormatContext *) int(* interleave_packet )(struct AVFormatContext *, AVPacket *out, AVPacket *in, int flush) Currently only used to set pixel format if not YUV420P. int(* query_codec )(enum AVCodecID id, int std_compliance) Test if the given codec can be stored in this container. void(* get_output_timestamp )(struct AVFormatContext *s, int stream, int64_t *dts, int64_t *wall)
Detailed Description
Definition at line 373 of file avformat.h.
五.AVInputForma
先看看官网的说明: http://ffmpeg.org/doxygen/1.0/structAVInputFormat.html
Data Fields
const char * name A comma separated list of short names for the format. const char * long_name Descriptive name for the format, meant to be more human-readable than name. int flags Can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_SHOW_IDS, AVFMT_GENERIC_INDEX, AVFMT_TS_DISCONT, AVFMT_NOBINSEARCH, AVFMT_NOGENSEARCH, AVFMT_NO_BYTE_SEEK, AVFMT_SEEK_TO_PTS. const char * extensions If extensions are defined, then no probe is done. struct AVCodecTag *const * codec_tag const AVClass * priv_class AVClass for the private context. struct AVInputFormat * next int raw_codec_id Raw demuxers store their codec ID here. int priv_data_size Size of private data so that it can be allocated in the wrapper. int(* read_probe )(AVProbeData *) Tell if a given file has a chance of being parsed as this format. int(* read_header )(struct AVFormatContext *) Read the format header and initialize the AVFormatContext structure. int(* read_packet )(struct AVFormatContext *, AVPacket *pkt) Read one packet and put it in 'pkt'. int(* read_close )(struct AVFormatContext *) Close the stream. int(* read_seek )(struct AVFormatContext *, int stream_index, int64_t timestamp, int flags) Seek to a given timestamp relative to the frames in stream component stream_index. int64_t(* read_timestamp )(struct AVFormatContext *s, int stream_index, int64_t *pos, int64_t pos_limit) Get the next timestamp in stream[stream_index].time_base units. int(* read_play )(struct AVFormatContext *) Start/resume playing - only meaningful if using a network-based format (RTSP). int(* read_pause )(struct AVFormatContext *) Pause playing - only meaningful if using a network-based format (RTSP). int(* read_seek2 )(struct AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags) Seek to timestamp ts.
关键数据结构说明
Int file_index:多媒体文件的索引号
AVStream *st:输入文件中数据流的抽象
int discard:如果数据流数据将要丢弃,则此标志位为真
int decoding_needed:如果数据包必须以”raw_fifo”模式解码,则此标志位为真
AVCodec *dec:解码器
AVFrame *decoded_frame:解码帧
int64_t start:读数据开始的时间
int64_t next_dts:下一帧的dts,如果一个数据包中包含多个数据帧,则用来指当前数据包中当前帧的下一帧
int64_t dts:当前数据帧的dts
int64_t next_pts:对比上边的dts
int64_t pts:当前数据帧的pts
FrameBuffer *buffer_pool:解码数据的缓冲池
出现的数据结构
1.AVCodecTag
Data Fields
enum AVCodecID id unsigned int tag
六.AVCodec
该数据结构就是表示编码解码信息的结构体,包括解码编码的函数指针等信息。例如下面是png编码信息
该结构体的官方说明:
- AVCodec ff_png_encoder = {
- .name = "png", //名字
- .type = AVMEDIA_TYPE_VIDEO, //编码器的数据类型
- .id = CODEC_ID_PNG, //编码器的ID号
- .priv_data_size = sizeof(PNGEncContext), //数据的大小
- .init = png_enc_init, //编码器的初始化 一个函数指针
- .encode = encode_frame, //这个看看一看,也没注释,要深入进去看了给出答案
- .pix_fmts= (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB32, PIX_FMT_PAL8, PIX_FMT_GRAY8, PIX_FMT_MONOBLACK, PIX_FMT_NONE},//<span style="color: rgb(43, 43, 43); font-family: arial, sans-serif; font-size: 18px; line-height: 27px; background-color: rgb(250, 250, 250);"><strong>数组支持的像素格式,或NULL如果未知,数组终止1</strong></span>
- .long_name= NULL_IF_CONFIG_SMALL("PNG image"), // 这个也需要看看
- };
Data Fields
const char * name Name of the codec implementation. const char * long_name Descriptive name for the codec, meant to be more human readable than name. enum AVMediaType type enum AVCodecID id //id号唯一 int capabilities Codec capabilities. const AVRational * supported_framerates array of supported framerates, or NULL if any, array is terminated by {0,0} enum PixelFormat *pix_fmts
const int
*supported_samplerates enum
AVSampleFormat *sample_fmts
const uint64_t
*channel_layouts uint8_t
max_lowres const AVClass
*priv_class const AVProfile
*profiles int priv_data_size
struct AVCodec *next array of supported pixel formats, or NULL if unknown, array is terminated by -1 const AVCodecDefault * defaults Private codec-specific defaults. void(* init_static_data )(struct AVCodec *codec) Initialize codec static data, called from avcodec_register(). int(* init )(AVCodecContext *) int(* encode_sub )(AVCodecContext *, uint8_t *buf, int buf_size, const struct AVSubtitle *sub) int(* encode2 )(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr) Encode data to an AVPacket. int(* decode )(AVCodecContext *, void *outdata, int *outdata_size, AVPacket *avpkt) int(* close )(AVCodecContext *) void(* flush )(AVCodecContext *) Flush buffers. Frame-level threading support functionsint(* init_thread_copy )(AVCodecContext *) If defined, called on thread contexts when they are created. int(* update_thread_context )(AVCodecContext *dst, const AVCodecContext *src) Copy necessary context variables from a previous thread context to the current one.
Detailed Description