【FFmpeg】AVFormatContext结构体

【FFmpeg】AVFormatContext结构体

  • 1.AVFormatContext结构体
    • [1.2 const struct AVInputFormat *iformat](#1.2 const struct AVInputFormat *iformat)
    • [1.3 const struct AVOutputFormat *oformat](#1.3 const struct AVOutputFormat *oformat)

参考:
FFMPEG结构体分析:AVFormatContext

示例工程:
【FFmpeg】调用ffmpeg库实现264软编
【FFmpeg】调用ffmpeg库实现264软解
【FFmpeg】调用ffmpeg库进行RTMP推流和拉流
【FFmpeg】调用ffmpeg库进行SDL2解码后渲染

流程分析:
【FFmpeg】编码链路上主要函数的简单分析
【FFmpeg】解码链路上主要函数的简单分析

结构体分析:
【FFmpeg】AVCodec结构体
【FFmpeg】AVCodecContext结构体
【FFmpeg】AVStream结构体

1.AVFormatContext结构体

AVFormatContext的定义位于libavformat\avformat.h之中,主要包括了格式的上下文信息,定义如下

c 复制代码
/**
 * 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.
 *
 * Fields can be accessed through AVOptions (av_opt*),
 * the name string used matches the associated command line parameter name and
 * can be found in libavformat/options_table.h.
 * The AVOption/command line parameter names differ in some cases from the C
 * structure field names for historic reasons or brevity.
 */
// 格式I/O的上下文结构体
// 新字段可以添加到末尾,并伴有轻微的版本变更。
// 删除、重新排序和更改现有字段需要一个主要的版本更新。
// sizeof(AVFormatContext)不能在libav*之外使用,使用avformat_alloc_context()来创建AVFormatContext
// 字段可以通过AVOptions (av_opt*)访问,使用的名称字符串匹配相关的命令行参数名称,可以在libavformat/options_table.h中找到
// 由于历史原因或简洁,AVOption/命令行参数名在某些情况下与C结构字段名不同
typedef struct AVFormatContext {
    /**
     * A class for logging and @ref avoptions. Set by avformat_alloc_context().
     * Exports (de)muxer private options if they exist.
     */
    // 一个用于日志记录和@ref avoptions的类。由avformat_alloc_context()设置
    // 导出(删除)多个私有选项(如果存在的话)
    const AVClass *av_class;

    /**
     * The input container format.
     *
     * Demuxing only, set by avformat_open_input().
     */
    // 输入容器的格式
    // 仅用于解复用,由set by avformat_open_input()设置
    const struct AVInputFormat *iformat;

    /**
     * The output container format.
     *
     * Muxing only, must be set by the caller before avformat_write_header().
     */
    // 输出容器的格式
    // 仅用于复用,必须在使用avformat_write_header()之前由调用者设置
    const struct AVOutputFormat *oformat;

    /**
     * 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()
     */
    // 格式化私有数据。这是一个启用avoptions的结构体,当且仅当format/oformat。priv_class不是NULL
    void *priv_data;

    /**
     * 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.
     */
    // 输入输出的上下文
    // 解复用:由用户在avformat_open_input()之前设置(然后用户必须手动关闭它)或由avformat_open_input()设置
    // 复用:在avformat_write_header()之前由用户设置。调用者必须负责关闭/释放IO上下文
    // 如果在iformat/oformat.flags中设置了AVFMT_NOFILE标志,则不要设置此字段。在这种情况下,(de)muxer将以其他方式处理I/O,并且该字段将为NULL
    AVIOContext *pb;

    /* stream info */
    // 下面的变量记录了流的信息
    /**
     * Flags signalling stream properties. A combination of AVFMTCTX_*.
     * Set by libavformat.
     */
    // 描述了流信息的标志,是AVFMTCTX_*的组合,由libavformat设置
    int ctx_flags;

    /**
     * Number of elements in AVFormatContext.streams.
     *
     * Set by avformat_new_stream(), must not be modified by any other code.
     */
    // AVFormatContext.streams中的元素数量
    unsigned int nb_streams;
    /**
     * 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().
     */
    // 文件中所有流的列表。使用avformat_new_stream()创建新流
    // 解复用:流是由libavformat在avformat_open_input中创建的
    // 复用:用户在avformat_write_header()之前创建流
    AVStream **streams;

    /**
     * Number of elements in AVFormatContext.stream_groups.
     *
     * Set by avformat_stream_group_create(), must not be modified by any other code.
     */
    // AVFormatContext.stream_groups中的元素数量
    // 由avformat_stream_group_create()设置,不能被任何其他代码修改
    unsigned int nb_stream_groups;
    /**
     * A list of all stream groups in the file. New groups are created with
     * avformat_stream_group_create(), and filled with avformat_stream_group_add_stream().
     *
     * - demuxing: groups may be created by libavformat in avformat_open_input().
     *             If AVFMTCTX_NOHEADER is set in ctx_flags, then new groups may also
     *             appear in av_read_frame().
     * - muxing: groups may be created by the user before avformat_write_header().
     *
     * Freed by libavformat in avformat_free_context().
     */
    // 文件中所有流组的列表。使用avformat_stream_group_create()创建新组,并使用avformat_stream_group_add_stream()填充新组
    // 解复用:组可以通过libavformat在avformat_open_input()中创建。如果AVFMTCTX_NOHEADER在ctx_flags中设置,
    //			那么av_read_frame()中也可能出现新的组。
    // 复用:用户可以在avformat_write_header()之前创建组
    // 由libavformat在avformat_free_context()之中释放
    AVStreamGroup **stream_groups;

    /**
     * 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
     */
    // AVChapter数组中的章节数
    // 当复用时,章节通常写在文件头中,所以nb_章节通常应该在调用write_header之前初始化。
    // 一些编码器(例如mov和mkv)也可以在预告片中编写章节。要在尾文件中写入章节,调用write_header时nb_章节必须为零,
    // 调用write_trailer时nb_章节必须为非零
    unsigned int nb_chapters;
    AVChapter **chapters;

    /**
     * input or output URL. Unlike the old filename field, this field has no
     * length restriction.
     *
     * - demuxing: set by avformat_open_input(), initialized to an empty
     *             string if url parameter was NULL in avformat_open_input().
     * - muxing: may be set by the caller before calling avformat_write_header()
     *           (or avformat_init_output() if that is called first) to a string
     *           which is freeable by av_free(). Set to an empty string if it
     *           was NULL in avformat_init_output().
     *
     * Freed by libavformat in avformat_free_context().
     */
    // 输入或输出URL,与原来使用的filename变量不同,这里没有长度限制,原来的定义为 char filename[1024]
    // 解复用:由avformat_open_input()设置,如果avformat_open_input()中的url参数为NULL,则初始化为空字符串
    // 复用:可以由调用者在调用avformat_write_header()(或avformat_init_output()如果先调用它)之前设置为av_free()可释放的字符串。
    //			如果avformat_init_output()中为NULL,则设置为空字符串
    char *url;

    /**
     * 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.
     */
    // 组件的第一帧的位置,以AV_TIME_BASE小数秒为单位。千万不要直接设置这个值
    // 它是从AVStream的值推导出来的
    int64_t start_time;

    /**
     * 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.
     */
    // 流的持续时间,以AV_TIME_BASE小数秒为单位。只有在您不知道任何单个流持续时间的情况下才设置此值,
    // 并且也不要设置它们中的任何一个。如果没有设置,这是从AVStream值推断出来的
    int64_t duration;

    /**
     * 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.
     */
    // 总流比特率(以bit/s为单位),如果不可用则为0。如果file_size和持续时间被称为FFmpeg可以自动计算,则不要直接设置它
    int64_t bit_rate;
	// pakcet包的大小
    unsigned int packet_size;
    // 最大延时
    int max_delay;

    /**
     * Flags modifying the (de)muxer behaviour. A combination of AVFMT_FLAG_*.
     * Set by the user before avformat_open_input() / avformat_write_header().
     */
    // 描述复用器或者解复用器的行为,是一组AVFMT_FLAG_*组成的标志符
    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.
/**
 * When muxing, try to avoid writing any random/volatile data to the output.
 * This includes any random IDs, real-time timestamps/dates, muxer version, etc.
 *
 * This flag is mainly intended for testing.
 */
#define AVFMT_FLAG_BITEXACT         0x0400
#define AVFMT_FLAG_SORT_DTS    0x10000 ///< try to interleave outputted packets by dts (using this flag can slow demuxing down)
#define AVFMT_FLAG_FAST_SEEK   0x80000 ///< Enable fast, but inaccurate seeks for some formats
#if FF_API_LAVF_SHORTEST
#define AVFMT_FLAG_SHORTEST   0x100000 ///< Stop muxing when the shortest stream stops.
#endif
#define AVFMT_FLAG_AUTO_BSF   0x200000 ///< Add bitstream filters as requested by the muxer

    /**
     * Maximum number of bytes read from input in order to determine stream
     * properties. Used when reading the global header and in
     * avformat_find_stream_info().
     *
     * Demuxing only, set by the caller before avformat_open_input().
     *
     * @note this is \e not  used for determining the \ref AVInputFormat
     *       "input format"
     * @sa format_probesize
     */
    // 为了确定流属性而从输入读取的最大字节数。在读取全局头文件和avformat_find_stream_info()中使用
    int64_t probesize;

    /**
     * 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().
     * Can be set to 0 to let avformat choose using a heuristic.
     */
    // avformat_find_stream_info()从输入中读取数据的最大持续时间(AV_TIME_BASE单位)
    // 可以设置为0,来让avformat选择使用启发式模式?
    int64_t max_analyze_duration;

    const uint8_t *key;
    int keylen;
	// 程序描述的结构体,从定义上看似乎是区分和管理不同的程序集
    unsigned int nb_programs;
    AVProgram **programs;

    /**
     * Forced video codec_id.
     * Demuxing: Set by user.
     */
    // 视频的codec ID
    enum AVCodecID video_codec_id;

    /**
     * Forced audio codec_id.
     * Demuxing: Set by user.
     */
    // 音频的codec ID
    enum AVCodecID audio_codec_id;

    /**
     * Forced subtitle codec_id.
     * Demuxing: Set by user.
     */
    // 字幕的codec ID
    // 使用何种codec来编码和解码字幕
    enum AVCodecID subtitle_codec_id;

    /**
     * Forced Data codec_id.
     * Demuxing: Set by user.
     */
    // 数据流的codec ID
    // 用于处理非音视频的数据内容,比如字幕、加密秘钥或者自定义的元数据
    enum AVCodecID data_codec_id;

    /**
     * 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;

    /**
     * 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.
     */
    // 流在现实世界时间中的开始时间,自Unix纪元(1970年1月1日00:00)以来以微秒为单位。
    // 也就是说,流中的pts=0是在这个真实世界时间被捕获的
    int64_t start_time_realtime;

    /**
     * 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().
     */
    // 在avformat_find_stream_info()中用于确定帧率的帧数
    int fps_probe_size;

    /**
     * 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;

    /**
     * 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.
     */
    // 为I/O层定制中断回调
    AVIOInterruptCB interrupt_callback;

    /**
     * Flags to enable debugging.
     */
    // 用于debug的flag
    int debug;
#define FF_FDEBUG_TS        0x0001

    /**
     * The maximum number of streams.
     * - encoding: unused
     * - decoding: set by user
     */
    // 流的最大数
    int max_streams;

    /**
     * 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
     */
    // 用于每个流索引的最大内存量(以字节为单位)
	// 如果索引超过此大小,则根据需要丢弃条目以保持较小的索引大小。这可能导致搜索速度变慢或准确性降低(取决于demuxer)
	// 强制使用完整内存索引的解压缩器将忽略这一点
    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;

    /**
     * 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().
     */
    // 用于interleaving的最大缓冲时间
    // 1. 为了确保所有流都正确交错,av_interleaved_write_frame()将等待,直到每个流至少有一个数据包,
    // 		然后才实际将任何数据包写入输出文件。当一些流是"稀疏的"(即在连续的数据包之间有很大的间隙),这可能会导致过度的缓冲
    // 
    // 2.该字段指定muxing队列中第一个和最后一个数据包的时间戳之间的最大差异,高于此值的libavformat将输出一个数据包,
    //		而不管它是否为所有流排队
    int64_t max_interleave_delta;

    /**
     * Maximum number of packets to read while waiting for the first timestamp.
     * Decoding only.
     */
    // 等待第一个时间戳时要读取的最大数据包数
    int max_ts_probe;

    /**
     * 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
     * - 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
     * - decoding: unused
     */
    // 最大块大小(以字节为单位)
    int max_chunk_size;

    /**
     * Maximum number of packets that can be probed
     * - encoding: unused
     * - decoding: set by user
     */
    // 可以探测的最大数据包数
    int max_probe_packets;

    /**
     * Allow non-standard and experimental extension
     * @see AVCodecContext.strict_std_compliance
     */
    // 允许非标准和实验性扩展
    int strict_std_compliance;

    /**
     * Flags indicating events happening on the file, a combination of
     * AVFMT_EVENT_FLAG_*.
     *
     * - demuxing: may be set by the demuxer in avformat_open_input(),
     *   avformat_find_stream_info() and av_read_frame(). Flags must be cleared
     *   by the user once the event has been handled.
     * - muxing: may be set by the user after avformat_write_header() to
     *   indicate a user-triggered event.  The muxer will clear the flags for
     *   events it has handled in av_[interleaved]_write_frame().
     */
    // 指示文件上发生的事件的标志,AVFMT_EVENT_FLAG_*的组合
    int event_flags;
/**
 * - demuxing: the demuxer read new metadata from the file and updated
 *   AVFormatContext.metadata accordingly
 * - muxing: the user updated AVFormatContext.metadata and wishes the muxer to
 *   write it into the file
 */
#define AVFMT_EVENT_FLAG_METADATA_UPDATED 0x0001


    /**
     * Avoid negative timestamps during muxing.
     * Any value of the AVFMT_AVOID_NEG_TS_* constants.
     * Note, this works better when using av_interleaved_write_frame().
     * - muxing: Set by user
     * - demuxing: unused
     */
    // 在muxing期间避免负时间戳
    // AVFMT_AVOID_NEG_TS_*常量的任何值
    int avoid_negative_ts;
#define AVFMT_AVOID_NEG_TS_AUTO             -1 ///< Enabled when required by target format
#define AVFMT_AVOID_NEG_TS_DISABLED          0 ///< Do not shift timestamps even when they are negative.
#define AVFMT_AVOID_NEG_TS_MAKE_NON_NEGATIVE 1 ///< Shift timestamps so they are non negative
#define AVFMT_AVOID_NEG_TS_MAKE_ZERO         2 ///< Shift timestamps so that they start at 0

    /**
     * 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
     * - decoding: unused
     */
    // 以微秒计的音频预加载
    int audio_preload;

    /**
     * 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
     */
    // 强制使用时钟时间戳作为包的pts/dts
    // 这在存在B帧的情况下会产生未定义的结果
    int use_wallclock_as_timestamps;

    /**
     * Skip duration calcuation in estimate_timings_from_pts.
     * - encoding: unused
     * - decoding: set by user
     */
    // 跳过estimate_timings_from_pts中的持续时间计算
    int skip_estimate_duration_from_pts;

    /**
     * avio flags, used to force AVIO_FLAG_DIRECT.
     * - encoding: unused
     * - decoding: Set by user
     */
    // avio标志,用于强制AVIO_FLAG_DIRECT
    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
     */
    // duration字段可以通过各种方式进行估计,并且可以使用该字段了解如何估计持续时间
    enum AVDurationEstimationMethod duration_estimation_method;

    /**
     * Skip initial bytes when opening stream
     * - encoding: unused
     * - decoding: Set by user
     */
    // 打开流时跳过初始字节
    int64_t skip_initial_bytes;

    /**
     * Correct single timestamp overflows
     * - encoding: unused
     * - decoding: Set by user
     */
    // 正确的单时间戳溢出
    unsigned int correct_ts_overflow;

    /**
     * Force seeking to any (also non key) frames.
     * - encoding: unused
     * - decoding: Set by user
     */
    // 强制查找任何帧(也是非关键帧)
    int seek2any;

    /**
     * Flush the I/O context after each packet.
     * - encoding: Set by user
     * - decoding: unused
     */
    // 在每个数据包之后刷新I/O上下文
    int flush_packets;

    /**
     * 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
     */
    // 格式探测分数
    // 最大分数是AVPROBE_SCORE_MAX,它是在解解器探测格式时设置的
    int probe_score;

    /**
     * Maximum number of bytes read from input in order to identify the
     * \ref AVInputFormat "input format". Only used when the format is not set
     * explicitly by the caller.
     *
     * Demuxing only, set by the caller before avformat_open_input().
     *
     * @sa probesize
     */
    // 为了识别\ref AVInputFormat"输入格式",从输入中读取的最大字节数。仅在调用方未显式设置格式时使用
    int format_probesize;

    /**
     * ',' separated list of allowed decoders.
     * If NULL then all are allowed
     * - encoding: unused
     * - decoding: set by user
     */
    // 解码器的白名单
    char *codec_whitelist;

    /**
     * ',' separated list of allowed demuxers.
     * If NULL then all are allowed
     * - encoding: unused
     * - decoding: set by user
     */
    // 解码格式的白名单
    char *format_whitelist;

    /**
     * ',' separated list of allowed protocols.
     * - encoding: unused
     * - decoding: set by user
     */
    // 解码协议的白名单
    char *protocol_whitelist;

    /**
     * ',' separated list of disallowed protocols.
     * - encoding: unused
     * - decoding: set by user
     */
    // 解码协议的黑名单
    char *protocol_blacklist;

    /**
     * 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.
     */
    // IO重定位标志
    // 当底层IO上下文读指针被重新定位时,这是由avformat设置的,例如在进行基于字节的查找时
    // 解复用器可以使用这个标志来检测这种变化
    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
     */
    // 允许强制一个特定的视频解码器,即使有多个具有相同的codec_id
    const struct 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
     */
    // 允许强制一个特定的音频解码器,即使有多个具有相同的codec_id
    const struct 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
     */
    // 允许强制一个特定的字幕解码器,即使具有多个相同的codec_id
    const struct AVCodec *subtitle_codec;

    /**
     * Forced data codec.
     * This allows forcing a specific decoder, even when there are multiple with
     * the same codec_id.
     * Demuxing: Set by user
     */
    // 允许强制一个特定的数据解码器,即使具有多个相同的codec_id
    const struct AVCodec *data_codec;

    /**
     * Number of bytes to be written as padding in a metadata header.
     * Demuxing: Unused.
     * Muxing: Set by user.
     */
    // 在元数据头中写入填充的字节数
    int metadata_header_padding;

    /**
     * User data.
     * This is a place for some private data of the user.
     */
    // 用户数据,这里能够存放一些用户的私有数据
    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
     */
    // 输出时间戳偏移量,以微秒为单位
    int64_t output_ts_offset;

    /**
     * dump format separator.
     * can be ", " or "\n      " or anything else
     * - muxing: Set by user.
     * - demuxing: Set by user.
     */
    // 输出格式分隔符
    uint8_t *dump_separator;

    /**
     * A callback for opening new IO streams.
     *
     * Whenever a muxer or a demuxer needs to open an IO stream (typically from
     * avformat_open_input() for demuxers, but for certain formats can happen at
     * other times as well), it will call this callback to obtain an IO context.
     *
     * @param s the format context
     * @param pb on success, the newly opened IO context should be returned here
     * @param url the url to open
     * @param flags a combination of AVIO_FLAG_*
     * @param options a dictionary of additional options, with the same
     *                semantics as in avio_open2()
     * @return 0 on success, a negative AVERROR code on failure
     *
     * @note Certain muxers and demuxers do nesting, i.e. they open one or more
     * additional internal format contexts. Thus the AVFormatContext pointer
     * passed to this callback may be different from the one facing the caller.
     * It will, however, have the same 'opaque' field.
     */
    // 打开新的IO流的回调函数
    // 每当muxer或demuxer需要打开IO流时(对于demuxer通常来自avformat_open_input(),
    // 但对于某些格式也可能在其他时间发生),它将调用此回调以获取IO上下文
    // @note 某些复用器和解复用器会嵌套,也就是说,它们会打开一个或多个额外的内部格式上下文。
    //		因此,传递给这个回调的AVFormatContext指针可能与面向调用者的指针不同。但是,它将具有相同的"opaque"字段
    int (*io_open)(struct AVFormatContext *s, AVIOContext **pb, const char *url,
                   int flags, AVDictionary **options);

    /**
     * A callback for closing the streams opened with AVFormatContext.io_open().
     *
     * Using this is preferred over io_close, because this can return an error.
     * Therefore this callback is used instead of io_close by the generic
     * libavformat code if io_close is NULL or the default.
     *
     * @param s the format context
     * @param pb IO context to be closed and freed
     * @return 0 on success, a negative AVERROR code on failure
     */
    // 用于关闭使用AVFormatContext.io_open()打开的流的回调
    // 使用这个函数比使用io_close更可取,因为这会返回一个错误
    // 因此,如果io_close为NULL或默认值,则通用libavformat代码将使用此回调来代替io_close
    int (*io_close2)(struct AVFormatContext *s, AVIOContext *pb);
} AVFormatContext;

从AVFormatContext的定义来看,主要的信息包括:

(1)const struct AVInputFormat *iformat:输入容器的格式

(2)const struct AVOutputFormat *oformat:输出容器的格式

(3)AVIOContext *pb:输入输出的上下文结构体

(4)AVStream **streams:文件中流的list

(5)char *url:输入或者输出的地址

(6)int64_t bit_rate:整体流的码率

(7)enum AVCodecID video_codec_id:视频codec ID

(8)enum AVCodecID audio_codec_id:音频codec ID

(9)enum AVCodecID subtitle_codec_id:字幕codec ID

(10)enum AVCodecID data_codec_id:数据流codec ID

(11)unsigned int max_picture_buffer:最大的帧buffer

(12)char *XXX_whitelist:各类信息的白名单(位于白名单当中的工具表示可用,黑名单表示不可用)

(13)const struct AVCodec *XXX_codec:所定义的编解码器(音频、视频、字幕或者数据流)

(14)int (*io_open)(struct AVFormatContext *s, AVIOContext **pb, const char *url,int flags, AVDictionary **options):打开新的IO流

(15)int (*io_close2)(struct AVFormatContext *s, AVIOContext *pb):关闭流

从AVFormatContext的定义来看,除了定义了AVInputFormat和AVOutputFormat两种format结构体之外,还定义了输入输出上下文结构体AVIOContext、文件中流的list结构体AVStream、黑白名单list和流的打开关闭函数,这里与老版本的FFmpeg不同的地方包括:(1)将以前的char filename[1024]改成了现在的char* url,不再限制长度;(2)将AVCodec存放在AVFormatContext之中而不是AVStream之中,这里有一点将AVStream和AVCodec并列的思想,因为AVStream之中只包含了AVCodecParameters这个结构体,即只能访问部分codec的变量

下面先看看AVInputFormat和AVOutputFormat的内容(这两个定义比较简单),后续再看AVIOContext

1.2 const struct AVInputFormat *iformat

该结构体描述了输入音视频的格式,定义位于libavformat\avformat.h中

c 复制代码
/**
 * @addtogroup lavf_decoding
 * @{
 */
typedef struct AVInputFormat {
    /**
     * A comma separated list of short names for the format. New names
     * may be appended with a minor bump.
     */
    // format的名称
    const char *name;

    /**
     * Descriptive name for the format, meant to be more human-readable
     * than name. You should use the NULL_IF_CONFIG_SMALL() macro
     * to define it.
     */
    // format的全名
    const char *long_name;

    /**
     * Can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_SHOW_IDS,
     * AVFMT_NOTIMESTAMPS, AVFMT_GENERIC_INDEX, AVFMT_TS_DISCONT, AVFMT_NOBINSEARCH,
     * AVFMT_NOGENSEARCH, AVFMT_NO_BYTE_SEEK, AVFMT_SEEK_TO_PTS.
     */
    // 定义了format的行为
    int flags;

    /**
     * If extensions are defined, then no probe is done. You should
     * usually not use extension format guessing because it is not
     * reliable enough
     */
    // 如果定义了扩展,则不执行探测。通常不应该使用扩展格式猜测,因为它不够可靠
    const char *extensions;
	// AVCodecTag标识了媒体文件中的编解码格式
    const struct AVCodecTag * const *codec_tag;

    const AVClass *priv_class; ///< AVClass for the private context

    /**
     * Comma-separated list of mime types.
     * It is used check for matching mime types while probing.
     * @see av_probe_input_format2
     */
    // mime(Multipurpose Internet Mail Extensions)的类型
    const char *mime_type;
} AVInputFormat;

其中AVCodecTag标识了媒体文件中的编解码格式,定义如下

c 复制代码
typedef struct AVCodecTag {
    enum AVCodecID id;
    unsigned int tag;
} AVCodecTag;

1.3 const struct AVOutputFormat *oformat

AVOutputFormat之中记录了输出格式的类型,定义位于libavformat\avformat.h中

c 复制代码
/**
 * @addtogroup lavf_encoding
 * @{
 */
typedef struct AVOutputFormat {
	// 名称
    const char *name;
    /**
     * Descriptive name for the format, meant to be more human-readable
     * than name. You should use the NULL_IF_CONFIG_SMALL() macro
     * to define it.
     */
    // 全名
    const char *long_name;
    // mime的类型
    const char *mime_type;
    const char *extensions; /**< comma-separated filename extensions */
    /* output support */
    // 输出codec的ID
    enum AVCodecID audio_codec;    /**< default audio codec */
    enum AVCodecID video_codec;    /**< default video codec */
    enum AVCodecID subtitle_codec; /**< default subtitle codec */
    /**
     * can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER,
     * AVFMT_GLOBALHEADER, AVFMT_NOTIMESTAMPS, AVFMT_VARIABLE_FPS,
     * AVFMT_NODIMENSIONS, AVFMT_NOSTREAMS,
     * AVFMT_TS_NONSTRICT, AVFMT_TS_NEGATIVE
     */
    // 输出格式的行为
    int flags;

    /**
     * List of supported codec_id-codec_tag pairs, ordered by "better
     * choice first". The arrays are all terminated by AV_CODEC_ID_NONE.
     */
    // 支持的codec_id-codec_tag对列表,按"优先选择更好"排序。数组都以AV_CODEC_ID_NONE结束
    const struct AVCodecTag * const *codec_tag;


    const AVClass *priv_class; ///< AVClass for the private context
} AVOutputFormat;

CSDN : https://blog.csdn.net/weixin_42877471
Github : https://github.com/DoFulangChen

相关推荐
励志成为嵌入式工程师2 小时前
c语言简单编程练习9
c语言·开发语言·算法·vim
捕鲸叉3 小时前
创建线程时传递参数给线程
开发语言·c++·算法
A charmer3 小时前
【C++】vector 类深度解析:探索动态数组的奥秘
开发语言·c++·算法
Peter_chq3 小时前
【操作系统】基于环形队列的生产消费模型
linux·c语言·开发语言·c++·后端
hikktn4 小时前
如何在 Rust 中实现内存安全:与 C/C++ 的对比分析
c语言·安全·rust
青花瓷4 小时前
C++__XCode工程中Debug版本库向Release版本库的切换
c++·xcode
观音山保我别报错5 小时前
C语言扫雷小游戏
c语言·开发语言·算法
幺零九零零6 小时前
【C++】socket套接字编程
linux·服务器·网络·c++
捕鲸叉6 小时前
MVC(Model-View-Controller)模式概述
开发语言·c++·设计模式
岁月小龙6 小时前
如何让ffmpeg运行时从当前目录加载库,而不是从/lib64
ffmpeg·origin·ffprobe·rpath