/**
* 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*, use
* avformat_alloc_context() to create an AVFormatContext.
*/
typedef struct AVFormatContext {
/**
* A class for logging and @ref avoptions. Set by avformat_alloc_context().
* Exports (de)muxer private options if they exist.
*/
const AVClass *av_class; //由avformat_malloc_context()函数来设置,我们知道AVFormatContext就是由avformat_malloc_context()申请的,申请的时候就设置了AVClass,主要是导出muxer或者demuxer的私有选项
/**
* The input container format.
*
* Demuxing only, set by avformat_open_input().
*/
struct AVInputFormat *iformat; //表示输入的封装格式,在avformat_open_input()中设置,解封装中设置
/**
* 输出容器格式The output container format.
*
* Muxing only, must be set by the caller before avformat_write_header().
*/
struct AVOutputFormat *oformat; //表示输出的封装格式,必须在avformat_write_header()之前被调用者设置,封装中设置
/**
* Format private data. This is an AVOptions-enabled struct
* if and only if iformat/oformat.priv_class is not NULL.
*
* - muxing: set by avformat_write_header() * - demuxing: set by avformat_open_input()
*/
void *priv_data; //格式私有的数据,解封装的过程中,由avformat_open_input()函数设置,
记住avformat_open_input()打开一个输入流并且读取头部,这时候编解码器还没有打开 ,这个打开的流必须用avformat_close_input()函数来关闭
/**
* I/O context.
*
* - demuxing: either set by the user before avformat_open_input() (then
* the user must close it manually) or set by avformat_open_input().
* - muxing: set by the user before avformat_write_header(). The caller must
* take care of closing / freeing the IO context.
*
* Do NOT set this field if AVFMT_NOFILE flag is set in
* iformat/oformat.flags. In such a case, the (de)muxer will handle
* I/O in some other way and this field will be NULL.
*/
AVIOContext *pb; //输入输出上下文,输入数据的缓存
解封装中:要么由用户在avformat_open_input()之前设置(用户手动释放),要么就由avformat_open_input()来设置
封装中:由用户在avformat_write_header()之前设置,调用者必须当心关闭和释放IO上下文
/* stream info */
int ctx_flags; /**< Format-specific flags, see AVFMTCTX_xx */
/**
* Number of elements in AVFormatContext.streams.
*
* Set by avformat_new_stream(), must not be modified by any other code.
*/
unsigned int nb_streams; //AVFormatContext.streams数组中元素的个数,由avformat_new_stream()设置,必须不能被其他代码修改
/**
* A list of all streams in the file. New streams are created with
* avformat_new_stream().
*
* - demuxing: streams are created by libavformat in avformat_open_input().
* If AVFMTCTX_NOHEADER is set in ctx_flags, then new streams may also
* appear in av_read_frame().
* - muxing: streams are created by the user before avformat_write_header().
*
* Freed by libavformat in avformat_free_context().
*/
AVStream **streams; //文件中所有流的列表,新流是用avformat_new_stream()函数创建的,由avformat_free_context()函数来释放
/**
* input or output filename
*
* - demuxing: set by avformat_open_input()
* - muxing: may be set by the caller before avformat_write_header()
*/
char filename[1024]; //流总要打开文件吧,这个数组就是文件名
/**
* Position of the first frame of the component, in
* AV_TIME_BASE fractional seconds. NEVER set this value directly:
* It is deduced from the AVStream values.
*
* Demuxing only, set by libavformat.
*/
int64_t start_time; //第一帧的位置,单位为 AV_TIME_BASE fractional seconds,它是由AVStream的值中推导出来的,不要直接设置
/**
* Duration of the stream, in AV_TIME_BASE fractional
* seconds. Only set this value if you know none of the individual stream
* durations and also do not set any of them. This is deduced from the
* AVStream values if not set.
*
* Demuxing only, set by libavformat.
*/
int64_t duration; //以AV_TIME_BASE小数秒为单位的流的持续时间,如果完全不知道任何一个流的持续时间的时候才设置,不要设置他们中的任何一个。这种情况下,这个是从AVStream的值中推导出来的
/**
* Total stream bitrate in bit/s, 0 if not
* available. Never set it directly if the file_size and the
* duration are known as FFmpeg can compute it automatically.
*/
int bit_rate; //以b/s为单位的平均码率,不可获取时为0.如果文件的大小和持续时间可以被FFmpeg自动计算出来则绝不要设置
unsigned int packet_size; //pakcet的大小
int max_delay; //最大的时延
int flags;
#define AVFMT_FLAG_GENPTS 0x0001 ///< Generate missing pts even if it requires parsing future frames.
#define AVFMT_FLAG_IGNIDX 0x0002 ///< Ignore index.
#define AVFMT_FLAG_NONBLOCK 0x0004 ///< Do not block when reading packets from input.
#define AVFMT_FLAG_IGNDTS 0x0008 ///< Ignore DTS on frames that contain both DTS & PTS
#define AVFMT_FLAG_NOFILLIN 0x0010 ///< Do not infer any values from other values, just return what is stored in the container
#define AVFMT_FLAG_NOPARSE 0x0020 ///< Do not use AVParsers, you also must set AVFMT_FLAG_NOFILLIN as the fillin code works on frames and no parsing -> no frames. Also seeking to frames can not work if parsing to find frame boundaries has been disabled
#define AVFMT_FLAG_NOBUFFER 0x0040 ///< Do not buffer frames when possible
#define AVFMT_FLAG_CUSTOM_IO 0x0080 ///< The caller has supplied a custom AVIOContext, don't avio_close() it.
#define AVFMT_FLAG_DISCARD_CORRUPT 0x0100 ///< Discard frames marked corrupted
#define AVFMT_FLAG_FLUSH_PACKETS 0x0200 ///< Flush the AVIOContext every packet. //这就是值为512的对应的flag,每一个包都刷新AVIOContext结构体
#define AVFMT_FLAG_MP4A_LATM 0x8000 ///< Enable RTP MP4A-LATM payload
#define AVFMT_FLAG_SORT_DTS 0x10000 ///< try to interleave outputted packets by dts (using this flag can slow demuxing down)
#define AVFMT_FLAG_PRIV_OPT 0x20000 ///< Enable use of private options by delaying codec open (this could be made default once all code is converted)
#define AVFMT_FLAG_KEEP_SIDE_DATA 0x40000 ///< Don't merge side data but keep it separate.
/**
* Maximum size of the data read from input for determining
* the input container format.
* Demuxing only, set by the caller before avformat_open_input().
*/
unsigned int probesize; //用于决定输入容器格式的,从输入读取的数据的最大大小,仅用于解封装,由调用者在avformat_open_input()之前设置
/**
* Maximum duration (in AV_TIME_BASE units) of the data read
* from input in avformat_find_stream_info().
* Demuxing only, set by the caller before avformat_find_stream_info().
*/
int max_analyze_duration; //在avformat_find_stream_info()中的input读取的数据的最大的持续时间(以AV_TIME_BASE为单位),仅用于解封装,由调用者在avformat_find_stream_info()之前设置。
const uint8_t *key;
int keylen;
unsigned int nb_programs;
AVProgram **programs;
/**
* Forced video codec_id.
* Demuxing: Set by user.
*/
enum AVCodecID video_codec_id; //强制视频编解码器的ID,在解封装的时候可由用户设定
/**
* Forced audio codec_id.
* Demuxing: Set by user.
*/
enum AVCodecID audio_codec_id; //强制音频编解码器的ID,在解封装的时候可由用户设定
/**
* Forced subtitle codec_id.
* Demuxing: Set by user.
*/
enum AVCodecID subtitle_codec_id; //强制副编解码器的ID,在解封装的时候可由用户设定
/**
* Maximum amount of memory in bytes to use for the index of each stream.
* If the index exceeds this size, entries will be discarded as
* needed to maintain a smaller size. This can lead to slower or less
* accurate seeking (depends on demuxer).
* Demuxers for which a full in-memory index is mandatory will ignore
* this.
* - muxing: unused
* - demuxing: set by user
*/
unsigned int max_index_size; //每一个流的索引使用的最大数量的内存(以字节为单位)
//如果索引超过了这个大小,条目将会被丢弃来维持一个更小的尺寸,这能够导致更慢或者定位精度降低。一个完全在内存中的索引的解封装器将强制忽略这个。封装的时候不用,解封装的时候由用户设置
/**
* Maximum amount of memory in bytes to use for buffering frames
* obtained from realtime capture devices.
*/
unsigned int max_picture_buffer; //从实时捕捉设备中获取的那些缓冲帧使用的内存的最大字节数
/**
* Number of chapters in AVChapter array.
* When muxing, chapters are normally written in the file header,
* so nb_chapters should normally be initialized before write_header
* is called. Some muxers (e.g. mov and mkv) can also write chapters
* in the trailer. To write chapters in the trailer, nb_chapters
* must be zero when write_header is called and non-zero when
* write_trailer is called.
* - muxing: set by user
* - demuxing: set by libavformat
*/
unsigned int nb_chapters; //
AVChapter **chapters; //
/**
* Metadata that applies to the whole file.
*
* - demuxing: set by libavformat in avformat_open_input()
* - muxing: may be set by the caller before avformat_write_header()
*
* Freed by libavformat in avformat_free_context().
*/
AVDictionary *metadata; //应用到整个文件的元数据,由Libavformat在avformat_free_context()函数释放
//解封装的时候:由libavformat库在函数avformat_open_input()中设置
//封装的时候:可以由用户在avformat_write_header()之前设置
/**
* Start time of the stream in real world time, in microseconds
* since the Unix epoch (00:00 1st January 1970). That is, pts=0 in the
* stream was captured at this real world time.
* - muxing: Set by the caller before avformat_write_header(). If set to
* either 0 or AV_NOPTS_VALUE, then the current wall-time will
* be used.
* - demuxing: Set by libavformat. AV_NOPTS_VALUE if unknown. Note that
* the value may become known after some number of frames
* have been received.
*/
int64_t start_time_realtime; //以从1970年开始的真实时间的us数来计算的起始时间,也就是说流中pts=0的是在这个真实时间捕获的
//封装的时候:由调用者在avformat_write_header()之前设置,设为0,则使用当前的墙钟时间
//解封装的时候:由libavformat设置
/**
* The number of frames used for determining the framerate in
* avformat_find_stream_info().
* Demuxing only, set by the caller before avformat_find_stream_info().
*/
int fps_probe_size; //在avformat_find_stream_info()函数中使用的用于计算帧率的帧数,仅用于解封装,caller在avformat_find_stream_info()函数之前设置
/**
* Error recognition; higher values will detect more errors but may
* misdetect some more or less valid parts as errors.
* Demuxing only, set by the caller before avformat_open_input().
*/
int error_recognition; //错误识别,更高的值将检测更多的错误也会把多少有用的部分当做错误,仅用于解封装,caller在avformat_find_stream_info()函数之前设置
/**
* Custom interrupt callbacks for the I/O layer.
*
* demuxing: set by the user before avformat_open_input().
* muxing: set by the user before avformat_write_header()
* (mainly useful for AVFMT_NOFILE formats). The callback
* should also be passed to avio_open2() if it's used to
* open the file.
*/
AVIOInterruptCB interrupt_callback; //定制IO层的中断回调函数
/**
* Flags to enable debugging.
*/
int debug; //使能调试的标志位
#define FF_FDEBUG_TS 0x0001
/**
* Maximum buffering duration for interleaving.
*
* To ensure all the streams are interleaved correctly,
* av_interleaved_write_frame() will wait until it has at least one packet
* for each stream before actually writing any packets to the output file.
* When some streams are "sparse" (i.e. there are large gaps between
* successive packets), this can result in excessive buffering.
*
* This field specifies the maximum difference between the timestamps of the
* first and the last packet in the muxing queue, above which libavformat
* will output a packet regardless of whether it has queued a packet for all
* the streams.
*
* Muxing only, set by the caller before avformat_write_header().
*/
int64_t max_interleave_delta;
/**
* Transport stream id.
* This will be moved into demuxer private options. Thus no API/ABI compatibility
*/
int ts_id; //转换流id,这个将改为解封装器私有选项,故没有兼容性
/**
* Audio preload in microseconds.
* Note, not all formats support this and unpredictable things may happen if it is used when not supported.
* - encoding: Set by user via AVOptions (NO direct access)
* - decoding: unused
*/
int audio_preload; //以us为单位的音频预加载
/**
* Max chunk time in microseconds.
* Note, not all formats support this and unpredictable things may happen if it is used when not supported.
* - encoding: Set by user via AVOptions (NO direct access)
* - decoding: unused
*/
int max_chunk_duration;
/**
* Max chunk size in bytes
* Note, not all formats support this and unpredictable things may happen if it is used when not supported.
* - encoding: Set by user via AVOptions (NO direct access)
* - decoding: unused
*/
int max_chunk_size; //以字节为单位的chunk的最大尺寸
/**
* forces the use of wallclock timestamps as pts/dts of packets
* This has undefined results in the presence of B frames.
* - encoding: unused
* - decoding: Set by user via AVOptions (NO direct access)
*/
int use_wallclock_as_timestamps; //强制packet的pts/dts使用墙钟时间戳
/**
* Avoid negative timestamps during muxing.
* 0 -> allow negative timestamps
* 1 -> avoid negative timestamps
* -1 -> choose automatically (default)
* Note, this only works when interleave_packet_per_dts is in use.
* - encoding: Set by user via AVOptions (NO direct access)
* - decoding: unused
*/
int avoid_negative_ts;
/**
* avio flags, used to force AVIO_FLAG_DIRECT.
* - encoding: unused
* - decoding: Set by user via AVOptions (NO direct access)
*/
int avio_flags;
/**
* The duration field can be estimated through various ways, and this field can be used
* to know how the duration was estimated.
* - encoding: unused
* - decoding: Read by user via AVOptions (NO direct access)
*/
enum AVDurationEstimationMethod duration_estimation_method;//因为持续时间可以通过很多方法估算出来,这个字段用于指导持续时间是如何计算的
/**
* Skip initial bytes when opening stream
* - encoding: unused
* - decoding: Set by user via AVOptions (NO direct access)
*/
unsigned int skip_initial_bytes;//打开流时跳过最初的字节数
/**
* Correct single timestamp overflows
* - encoding: unused
* - decoding: Set by user via AVOptions (NO direct access)
*/
unsigned int correct_ts_overflow;//改正单个时间戳溢出
/**
* Force seeking to any (also non key) frames.
* - encoding: unused
* - decoding: Set by user via AVOptions (NO direct access)
*/
int seek2any; //强制定位到任何帧(非关键帧也一样)
/**
* Flush the I/O context after each packet.
* - encoding: Set by user via AVOptions (NO direct access)
* - decoding: unused
*/
int flush_packets;//在每一个packet之后刷新IO上下文
/**
* format probing score.
* The maximal score is AVPROBE_SCORE_MAX, its set when the demuxer probes
* the format.
* - encoding: unused
* - decoding: set by avformat, read by user via av_format_get_probe_score() (NO direct access)
*/
int probe_score; //格式探索分数,当解封装器探测出格式的时候设为最大AVPROBE_SCORE_MAX,解封装的时候,由libavforamt设置,用户通过av_format_get_probe_score()读取
/*****************************************************************华丽的分割线
* All fields below this line are not part of the public API. They
* may not be used outside of libavformat and can be changed and
* removed at will.
* New public fields should be added right above.
*****************************************************************
*/
/**
* 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;
struct AVPacketList *packet_buffer_end;
/* av_seek_frame() support */
int64_t data_offset; /**< offset of the first packet */
/**
* Raw packets from the demuxer, prior to parsing and decoding.
* This buffer is used for buffering packets until the codec can
* be identified, as parsing cannot be done without knowing the
* codec.
*/
struct AVPacketList *raw_packet_buffer;
struct AVPacketList *raw_packet_buffer_end;
/**
* Packets split by the parser get queued here.
*/
struct AVPacketList *parse_queue;
struct AVPacketList *parse_queue_end;
/**
* Remaining size available for raw_packet_buffer, in bytes.
*/
#define RAW_PACKET_BUFFER_SIZE 2500000
int raw_packet_buffer_remaining_size;
/**
* Offset to remap timestamps to be non-negative.
* Expressed in timebase units.
* @see AVStream.mux_ts_offset
*/
int64_t offset;
/**
* Timebase for the timestamp offset.
*/
AVRational offset_timebase;
/**
* An opaque field for libavformat internal usage.
* Must not be accessed in any way by callers.
*/
AVFormatInternal *internal;
/**
* IO repositioned flag.
* This is set by avformat when the underlaying IO context read pointer
* is repositioned, for example when doing byte based seeking.
* Demuxers can use the flag to detect such changes.
*/
int io_repositioned;
/**
* Forced video codec.
* This allows forcing a specific decoder, even when there are multiple with
* the same codec_id.
* Demuxing: Set by user via av_format_set_video_codec (NO direct access).
*/
AVCodec *video_codec;
/**
* Forced audio codec.
* This allows forcing a specific decoder, even when there are multiple with
* the same codec_id.
* Demuxing: Set by user via av_format_set_audio_codec (NO direct access).
*/
AVCodec *audio_codec;
/**
* Forced subtitle codec.
* This allows forcing a specific decoder, even when there are multiple with
* the same codec_id.
* Demuxing: Set by user via av_format_set_subtitle_codec (NO direct access).
*/
AVCodec *subtitle_codec;
/**
* Number of bytes to be written as padding in a metadata header.
* Demuxing: Unused.
* Muxing: Set by user via av_format_set_metadata_header_padding.
*/
int metadata_header_padding;
/**
* User data.
* This is a place for some private data of the user.
* Mostly usable with control_message_cb or any future callbacks in device's context.
*/
void *opaque;
/**
* Callback used by devices to communicate with application.
*/
av_format_control_message control_message_cb;
/**
* Output timestamp offset, in microseconds.
* Muxing: set by user via AVOptions (NO direct access)
*/
int64_t output_ts_offset;
} AVFormatContext;