(Conversion) FFMPEG-Data Structure explanation (avcodeccontext, avstream, avformatcontext)

Source: Internet
Author: User
Avcodeccontext

This is a data structure that describes the decoder context and contains the parameter information required by many codecs.

If libavcodec is used, the caller needs to initialize the information. If the entire FFMPEG library is used, when av_open_input_file and av_find_stream_info are called, Initialization is completed based on the file header information and the header information in the media stream. The interpretation of several major domains is as follows:

  1. Extradata/extradata_size: This buffer stores additional information that may be used by the decoder and fills in av_read_frame. In general, demuxer in a specific format fills in extradata when reading the format header information. Secondly, if demuxer does not do this, for example, if there may be no relevant codec information at the root of the header, the corresponding parser will continue to search for the media streams that have been deduplicated. This buffer pointer is null if no additional information is found.
  2. Time_base:
  3. Width/height: Specifies the width and height of the video.
  4. Sample_rate/channels: the audio sampling rate and number of channels.
  5. Sample_fmt: original audio sampling format.
  6. Codec_name/codec_type/codec_id/codec_tag: decoder information.

Avstream

This struct describes a media stream.

The main fields are interpreted as follows. The values of most fields can be determined by av_open_input_file Based on the file header information. The missing information needs to be obtained by calling av_find_stream_info to read frames and soft decoding:

  1. Index/ID: Index of the expected stream. This number is automatically generated. The index can be used to index the stream from the avformatcontext: streams table. The ID is the ID of the stream, depends on the specific container format. For example, for the mpeg ts format, ID is PID.
  2. Time_base: the time reference of a stream. It is a real number. The PTS and DTS of the media data in the stream are based on this time reference. Generally, av_rescale/av_rescale_q can be used to achieve conversion of different time benchmarks.
  3. Start_time: the start time of the stream. The unit is based on the stream time. It is usually the PTS of the first frame in the stream.
  4. Duration: the total time of the stream, in the unit of time reference of the stream.
  5. Need_parsing: the control domain of the stream parsing process.
  6. Nb_frames: Number of frames in the stream.
  7. R_frame_rate/framerate/avg_frame_rate: frame rate.
  8. Codec: Specifies the avcodeccontext structure corresponding to the stream. It is generated when av_open_input_file is called.
  9. Parser: point to the avcodecparsercontext structure corresponding to the stream, which is generated when av_find_stream_info is called.

Avformatcontext

This struct describes the composition and basic information of a media file or stream.

This is the most basic structure in FFMPEG, the root of all other structures, and the fundamental abstraction of a multimedia file or stream. The avstream structure pointer arrays represented by nb_streams and streams contain descriptions of all embedded media streams; iformat and oformat point to the corresponding demuxer and muxer pointers; PB points to a byteiocontext structure that controls underlying data read/write.

  • Start_time and duration are the start time and length of the multimedia file that is pushed out from each avstream in the streams array. The unit is subtle.

Generally, this structure is created internally by av_open_input_file and some members are initialized by default. However, if the caller wants to create the structure by himself, it needs to explicitly set the default value for some members of the structure-if there is no default value, it will cause exceptions in subsequent actions. The following members should be noted:

  • Probesize
  • Mux_rate
  • Packet_size
  • Flags
  • Max_analyze_duration
  • Key
  • Max_index_size
  • Max_picture_buffer
  • Max_delay

 

Avpacket

Avpacket is defined in avcodec. h.

FFmpeg uses avpacket for temporary storage of media data after decoding and multiplexing (one audio/video frame, one subtitle package, and so on) and additional information (Decoding timestamp, display timestamp, duration, etc). Where:

  • DTS indicates the decoding timestamp, PTS indicates the display timestamp, and their unit is the time benchmark of the media stream.
  • Stream_index indicates the index of the media stream to which it belongs;
  • Data is the data buffer pointer and size is the length;
  • Duration is the data duration, which is also based on the time of the media stream;
  • Pos indicates the byte offset of the data in the media stream;
  • Destruct is the function pointer used to release the data buffer;
  • Flags indicates the flag field. If the value is set to 1 at the lowest, the data is a key frame.

The avpacket structure is only a container. It uses the data member to point to the actual data buffer. This buffer can be created through av_new_packet and can be copied through av_dup_packet, it can also be generated by FFMPEG APIs (such as av_read_frame). After use, you need to call av_free_packet to release the packet.

Av_free_packet calls the destruct function of the struct, which has two values: (1) av_destruct_packet_nofree or 0; (2) av_destruct_packet, where, the former only clears the values of data and size by 0, and the latter truly releases the buffer zone. FFmpeg uses the avpacket structure to create a buffer to load data, and provides the destruct function. If FFMPEG wants to maintain its own buffer, set destruct to av_destruct_packet_nofree, the user cannot release the buffer when calling av_free_packet. If FFMPEG no longer uses the buffer, set destruct to av_destruct_packet, indicating that it can be released. For avpackt that the buffer cannot be released, it is recommended that you call av_dup_packet to clone the buffer and convert it to the avpacket that can be released by the buffer before use, to avoid exceptions and errors caused by improper use of the buffer zone. Av_dup_packet creates a buffer for avpacket with the destruct pointer av_destruct_packet_nofree, copies the data in the original buffer to the new buffer, sets the data value to the address of the new buffer, and sets the destruct pointer to av_destruct_packet.

Time Information

The time information is used for multimedia synchronization.

The purpose of synchronization is to maintain the inherent temporal relationship between media objects when displaying multimedia information. There are two types of synchronization: Intra-stream synchronization. The main task of intra-stream synchronization is to ensure the time relationship within a single media stream to meet the perception requirements, such as playing a video at a specified frame rate; another type is stream synchronization. The main task is to ensure the time relationship between different media streams, such as the relationship between audio and video (lipsync ).

For fixed-rate media, such as video with Fixed Frame Rate or audio with fixed bit rate, you can place the time information (frame rate or bit rate) in the file header ), for example, Avi's hdrl list and MP4 moov box also have a relatively complicated solution: embedding time information into the media stream, such as mpeg ts and real video, this solution can handle media with a variable speed and effectively avoid time drift during synchronization.

FFmpeg tags each data packet to more effectively support the synchronization mechanism of upper-layer applications. There are two types of time tags: DTS and PTS. For the sound, the two time tags are the same, but for some video encoding formats, the two-way prediction technology will cause inconsistency between DTs and PTS.

Obtain time information:

By calling av_find_stream_info, multimedia applications can obtain the time information of media files from the avformatcontext object, including the total time length and start time. In addition, the bit rate and file size related to the time information are also available. The unit of time information is av_time_base: microseconds.

From: http://blog.csdn.net/yuan892173701/article/details/8702333

 

(Conversion) FFMPEG-Data Structure explanation (avcodeccontext, avstream, avformatcontext)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.