Ffmpeg结构体——AVFormatContext

AVFormatContext是FFmpeg中一个重要的结构体,它在解复用(解封装)过程中起到关键作用,存储了媒体文件或媒体流的基本信息,如输入/输出格式、流信息、文件名、时长、比特率等。该结构体还包含I/O上下文、流信息数组以及各种标志,用于控制解码和复用过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

该结构体是贯穿始终的一个结构体!是ffmpeg解复用(解封装)用的结构体!(在这里仅是解码情况)
此结构描述了一个媒体文件或媒体流的构成的基本信息。
typedef struct AVFormatContext {
    /**
     * A class for logging and @ref avoptions. Set by avformat_alloc_conttypedef 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;

    /**
     * The input container format.
     *
     * Demuxing only, set by avformat_open_input().
     */
    struct AVInputFormat *iformat;//输入数据的封装格式,解复用的时候用

    /**
     * The output container format.
     *
     * Muxing only, must be set by the caller before avformat_write_header().
     */
    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()
     */
    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.
     */
    AVIOContext *pb;//输入数据的缓存,指向一个控制底层数据读写的ByteIOContext

    /* 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;//音视频流的个数
    /**
     * 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;//该结构指针数组和nb_streams包含了所有内嵌媒体流的描述

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

    /**
     * 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;//视频时长(单位:ms)

    /**
     * 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;//比特率(单位bps, 转换为kbps需要除以1000)

    unsigned int packet_size;
    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.
#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;

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

    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;

    /**
     * Forced audio codec_id.
     * Demuxing: Set by user.
     */
    enum AVCodecID audio_codec_id;

    /**
     * Forced subtitle codec_id.
     * Demuxing: Set by user.
     */
    enum AVCodecID subtitle_codec_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 me
### FFmpegAVFormatContext 的定义与使用 #### AVFormatContext 结构体概述 `AVFormatContext` 是 FFMPEG 库中至关重要的数据结构之一,在整个多媒体处理流程中扮演着核心角色。该结构不仅用于解封装操作,还涵盖了多种媒体文件格式的支持[^1]。 此结构体包含了关于媒体文件或流的各种元信息,如编码器配置、时间戳基数、比特率等,并且提供了访问底层 I/O 和协议层的功能接口[^4]。 #### 主要成员变量解释 以下是 `AVFormatContext` 中一些重要字段及其用途: - **iformat / oformat**: 输入/输出格式指针,指向当前打开文件所对应的解析器实例; - **pb (AVIOContext*)**: 文件读写缓冲区对象,负责实际的数据传输工作; - **nb_streams**: 流数量统计; - **streams[] (AVStream)**: 存储各个独立音视频轨道的信息列表; - **duration**: 媒体总长度(单位为微秒),可用于进度条显示等功能实现; - **metadata**: 用户自定义标签集合,比如艺术家名称、专辑封面图片链接等等; 这些属性共同构成了完整的媒体资源描述框架,使得开发者能够方便地获取并操控所需素材特性。 #### 实际应用场景举例 当涉及到具体编程实践时,通常会按照如下方式初始化和释放这个结构体: ```c // 初始化部分 AVFormatContext *fmt_ctx = NULL; if ((ret = avformat_open_input(&fmt_ctx, filename, NULL, NULL)) < 0) { fprintf(stderr, "无法打开输入文件\n"); exit(1); } if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < 0) { fprintf(stderr, "未能检索到流信息\n"); exit(1); } // 处理逻辑... // 清理收尾阶段 avformat_close_input(&fmt_ctx); // 关闭并清理关联资源 ``` 上述代码片段展示了如何通过调用库函数来创建一个新的 `AVFormatContext` 对象,并对其进行必要的设置以便后续操作。最后记得适时关闭连接以防止内存泄漏等问题发生。 #### 协议支持机制简介 除了基本的文件I/O外,FFMPEG 还允许用户指定不同的网络传输方案来进行远程资源加载。这依赖于内部维护的一系列 `URLProtocol` 表项,它们各自实现了特定类型的通信握手过程和服务端交互细节[^3]。 例如 HTTP(S), RTMP 等流行的标准都被集成到了官方版本之中,默认情况下即可无缝衔接各种在线直播源或者云盘分享链接等内容形式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值