ffmpeg structure - AVStream and its related functions


AVStream is a structure that stores the information of each video / audio stream.

1. Structure definition

/**
 * 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*.
 */
typedef struct AVStream {
    int index;    /**< stream index in AVFormatContext */
    /**
     * Format-specific stream ID.
     * decoding: set by libavformat
     * encoding: set by the user, replaced by libavformat if left unset
     */
    int id;
#if FF_API_LAVF_AVCTX
    /**
     * @deprecated use the codecpar struct instead
     */
    attribute_deprecated
    AVCodecContext *codec;
#endif
    void *priv_data;

    /**
     * This is the fundamental unit of time (in seconds) in terms
     * of which frame timestamps are represented.
     *
     * decoding: set by libavformat
     * encoding: May be set by the caller before avformat_write_header() to
     *           provide a hint to the muxer about the desired timebase. In
     *           avformat_write_header(), the muxer will overwrite this field
     *           with the timebase that will actually be used for the timestamps
     *           written into the file (which may or may not be related to the
     *           user-provided one, depending on the format).
     */
    AVRational time_base;

    /**
     * Decoding: pts of the first frame of the stream in presentation order, in stream time base.
     * Only set this if you are absolutely 100% sure that the value you set
     * it to really is the pts of the first frame.
     * This may be undefined (AV_NOPTS_VALUE).
     * @note The ASF header does NOT contain a correct start_time the ASF
     * demuxer must NOT set this.
     */
    int64_t start_time;

    /**
     * Decoding: duration of the stream, in stream time base.
     * If a source file does not specify a duration, but does specify
     * a bitrate, this value will be estimated from bitrate and file size.
     *
     * Encoding: May be set by the caller before avformat_write_header() to
     * provide a hint to the muxer about the estimated duration.
     */
    int64_t duration;

    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.

    /**
     * sample aspect ratio (0 if unknown)
     * - encoding: Set by user.
     * - decoding: Set by libavformat.
     */
    AVRational sample_aspect_ratio;

    AVDictionary *metadata;

    /**
     * Average framerate
     *
     * - demuxing: May be set by libavformat when creating the stream or in
     *             avformat_find_stream_info().
     * - muxing: May be set by the caller before avformat_write_header().
     */
    AVRational avg_frame_rate;

    /**
     * For streams with AV_DISPOSITION_ATTACHED_PIC disposition, this packet
     * will contain the attached picture.
     *
     * decoding: set by libavformat, must not be modified by the caller.
     * encoding: unused
     */
    AVPacket attached_pic;

    /**
     * An array of side data that applies to the whole stream (i.e. the
     * container does not allow it to change between packets).
     *
     * There may be no overlap between the side data in this array and side data
     * in the packets. I.e. a given side data is either exported by the muxer
     * (demuxing) / set by the caller (muxing) in this array, then it never
     * appears in the packets, or the side data is exported / sent through
     * the packets (always in the first packet where the value becomes known or
     * changes), then it does not appear in this array.
     *
     * - demuxing: Set by libavformat when the stream is created.
     * - muxing: May be set by the caller before avformat_write_header().
     *
     * Freed by libavformat in avformat_free_context().
     *
     * @see av_format_inject_global_side_data()
     */
    AVPacketSideData *side_data;
    /**
     * The number of elements in the AVStream.side_data array.
     */
    int            nb_side_data;

    /**
     * Flags indicating events happening on the stream, a combination of
     * AVSTREAM_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().
     */
    int event_flags;
/**
 * - demuxing: the demuxer read new metadata from the file and updated
 *     AVStream.metadata accordingly
 * - muxing: the user updated AVStream.metadata and wishes the muxer to write
 *     it into the file
 */
#define AVSTREAM_EVENT_FLAG_METADATA_UPDATED 0x0001
/**
 * - demuxing: new packets for this stream were read from the file. This
 *   event is informational only and does not guarantee that new packets
 *   for this stream will necessarily be returned from av_read_frame().
 */
#define AVSTREAM_EVENT_FLAG_NEW_PACKETS (1 << 1)

    /**
     * Real base framerate of the stream.
     * This is the lowest framerate with which all timestamps can be
     * represented accurately (it is the least common multiple of all
     * framerates in the stream). Note, this value is just a guess!
     * For example, if the time base is 1/90000 and all frames have either
     * approximately 3600 or 1800 timer ticks, then r_frame_rate will be 50/1.
     */
    AVRational r_frame_rate;

#if FF_API_LAVF_FFSERVER
    /**
     * String containing pairs of key and values describing recommended encoder configuration.
     * Pairs are separated by ','.
     * Keys are separated from values by '='.
     *
     * @deprecated unused
     */
    attribute_deprecated
    char *recommended_encoder_configuration;
#endif

    /**
     * Codec parameters associated with this stream. Allocated and freed by
     * libavformat in avformat_new_stream() and avformat_free_context()
     * respectively.
     *
     * - demuxing: filled by libavformat on stream creation or in
     *             avformat_find_stream_info()
     * - muxing: filled by the caller before avformat_write_header()
     */
    AVCodecParameters *codecpar;

    /*****************************************************************
     * 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.
     * Internal note: be aware that physically removing these fields
     * will break ABI. Replace removed fields with dummy fields, and
     * add new fields to AVStreamInternal.
     *****************************************************************
     */

#if LIBAVFORMAT_VERSION_MAJOR < 59
    // kept for ABI compatibility only, do not access in any way
    void *unused;
#endif

    int pts_wrap_bits; /**< number of bits in pts (used for wrapping control) */

    // Timestamp generation support:
    /**
     * Timestamp corresponding to the last dts sync point.
     *
     * Initialized when AVCodecParserContext.dts_sync_point >= 0 and
     * a DTS is received from the underlying container. Otherwise set to
     * AV_NOPTS_VALUE by default.
     */
    int64_t first_dts;
    int64_t cur_dts;
    int64_t last_IP_pts;
    int last_IP_duration;

    /**
     * Number of packets to buffer for codec probing
     */
    int probe_packets;

    /**
     * Number of frames that have been demuxed during avformat_find_stream_info()
     */
    int codec_info_nb_frames;

    /* av_read_frame() support */
    enum AVStreamParseType need_parsing;
    struct AVCodecParserContext *parser;

#if LIBAVFORMAT_VERSION_MAJOR < 59
    // kept for ABI compatibility only, do not access in any way
    void        *unused7;
    AVProbeData  unused6;
    int64_t      unused5[16+1];
    void         *unused2;
    int          unused3;
    unsigned int unused4;
#endif

    /**
     * Stream Identifier
     * This is the MPEG-TS stream identifier +1
     * 0 means unknown
     */
    int stream_identifier;

#if LIBAVFORMAT_VERSION_MAJOR < 59
    // kept for ABI compatibility only, do not access in any way
    int unused8;
    int unused9;
    int unused10;
#endif

    /**
     * An opaque field for libavformat internal usage.
     * Must not be accessed in any way by callers.
     */
    AVStreamInternal *internal;
} AVStream;

2. Common variables and their functions

Identify the video / audio stream and the index in AVFormatContext. This number is automatically generated and can be used to index the stream from AVFormatContext::streams table.

int index;

The identification of the flow depends on the specific container format. Decoding: set by libavformat. Code: set by the user. If it is not set, it will be replaced by libavformat.

int id;

Point to the AVCodecContext structure corresponding to the stream and call avformat_ open_ Generated on input.

AVCodecContext *codec;

This is the basic time unit (in seconds) representing the frame timestamp. The pts and dts of the media data in the stream will take this time benchmark as the granularity.

AVRational time_base;

The starting time of the stream, in the unit of the time base of the stream. To set, 100% make sure that the value you set is really the pts of the first frame.

int64_t start_time;

Decoding: the duration of the stream. If the source file does not specify a duration, but specifies a bit rate, the value is estimated based on the bit rate and file size.

int64_t duration;

The number of frames in this stream, if known, or 0.

int64_t nb_frames; 

Choose which packets can be discarded at will without going to demux.

enum AVDiscard discard;

Sample aspect ratio (0 if unknown).

AVRational sample_aspect_ratio;

Metadata information.

AVDictionary *metadata;

Average frame rate. Unpacking: it can be set to libavformat or avformat when creating a stream_ find_ stream_ Set in info(). Encapsulation: can be performed by the caller in avformat_ write_ Set before header().

AVRational avg_frame_rate;

Attached pictures. For example, some MP3 and AAC audio files come with album covers.

AVPacket attached_pic;

The number of packets used by codec for probe.

int probe_packets;

In AV_ find_ stream_ The number of frames that have been unpacked during info().

int codec_info_nb_frames;

Flow detection status: 1 indicates that the detection is completed, 0 indicates that there is no detection request, and rest executes the detection.

int request_probe;

Indicates that all content up to the next keyframe should be discarded.

int skip_to_keyframe;

The number of samples to skip at the beginning of the frame decoded from the next packet.

int skip_samples;

If not 0, the number of samples that should be skipped from the beginning of the stream.

int64_t start_skip_samples;

If it is not 0, the first audio sample should be discarded from the stream.

int64_t first_discard_sample;

Internal data, generate dts from pts.

uint8_t pts_reorder_error_count[MAX_REORDER_DELAY+1];

Internal data for analyzing dts and detecting fault mpeg streams.

uint8_t dts_misordered;

Displays the aspect ratio.

AVRational display_aspect_ratio;

Keywords: ffmpeg

Added by lemmin on Sun, 20 Feb 2022 04:21:50 +0200