Detailed explanation of AVFrame structure of ffmpeg

The AVFrame structure is a structure used in FFmpeg to describe media data frames (video, audio, etc.). It contains various parameters and data information of the media data frame, and is a necessary element for encoding, decoding and processing media data. The following is a detailed introduction to the AVFrame structure, and a simple code example is also given.

AVFrame structure definition

The definition of the AVFrame structure can be found in the frame.h header file of FFmpeg. It is defined as follows:

typedef struct AVFrame {
    
    
    ...
} AVFrame;

AVFrame structure member variables

The AVFrame structure contains a wealth of parameters and data information, some of the important member variables are as follows:

data[AV_NUM_DATA_POINTERS]: An array of pointers, used to store the data pointers of each channel of the media data frame. For example, in a video frame, this array typically contains one to three pointers to data for the red, green, and blue channels, respectively. In an audio frame, the array usually contains only one pointer, corresponding to the data of the audio channel.
linesize[AV_NUM_DATA_POINTERS]: An array of integers representing the number of bytes per row of data for each channel.
extended_data: A pointer to the first element of the data array, used to support some non-standard media data formats.
width、height、format: The width, height, and pixel format of the media data frame, used to indicate the size and color characteristics of the frame.
pts: The timestamp of the frame (Presentation Timestamp), which is used to indicate the presentation time of the frame data in the entire media stream.
pkt_pts、pkt_dts: The timestamp in the AVPacket structure is used for comparison, calculation and correction with the timestamp in the decoded AVFrame structure.
nb_samples: The number of samples in an audio frame.
sample_rate、channels、channel_layout: The sampling rate, number of channels and channel layout of the audio frame.
key_frame: Whether the video frame is a key frame.
repeat_pict: Whether the video frame needs to be played repeatedly, which is used to realize functions such as frame rate adjustment.
crop_top、crop_left、crop_bottom、crop_right: The cropped area of ​​the current frame.

In addition, the AVFrame structure also contains some additional parameters and flags, such as metadata, pktpos, pktsize, etc. These parameters can also be set and adjusted according to specific media data types and application requirements.
Example of using the AVFrame structure
Here is a simple code example to decode a video frame and save it as a BMP image file:

// 打开视频文件,获取 AVFormatContext 结构体
...

// 找到视频流的解码器并打开
...

// 分配 AVPacket 和 AVFrame 结构体
AVPacket *pkt = av_packet_alloc();
AVFrame *frame = av_frame_alloc();

while (av_read_frame(fmt_ctx, pkt) >= 0) {
    
    
    if (pkt->stream_index == video_stream_index) {
    
    
        // 解码视频帧
        int ret = avcodec_send_packet(codec_ctx, pkt);
        if (ret < 0) {
    
    
            // 发送数据包失败
            av_packet_unref(pkt);
            break;
        }

        while (ret >= 0) {
    
    
            ret = avcodec_receive_frame(codec_ctx, frame);
            if (ret < 0) {
    
    
                break;
            }

            // 写入 BMP 文件
            char filename[64];
            snprintf(filename, sizeof(filename), "frame-%d.bmp", codec_ctx->frame_number);
            write_frame_to_bmp(frame, filename);

            // 释放帧数据内存
            av_frame_unref(frame);
        }
    }

    av_packet_unref(pkt);
}

// 释放资源
...

In the above code, the AVPacket and AVFrame structures are used to store undecoded data packets and decoded video frame data read from the video stream, respectively. By calling the avcodecsendpacket() and avcodecreceiveframe() functions, the data packet can be sent to the decoder for decoding and the decoded video frame data can be obtained. The decoded video frame data can be saved as a BMP file through the custom writeframeto_bmp() function.
It should be noted that when using the AVFrame structure, parameters need to be adjusted and processed according to the specific media data type and application requirements, so as to realize the correct processing and transmission of media data. In addition, corresponding memory management and resource release operations are required to prevent memory leaks and resource waste.

Guess you like

Origin blog.csdn.net/qq_51282224/article/details/131006393