Audio and video project—Audio and video player analysis based on FFmpeg and SDL (7)

introduce

In this series, I plan to spend a lot of time explaining my gitee project audio and video player. In this project, you can learn audio and video decapsulation, decoding, and SDL rendering related knowledge. If you are interested in the source code, please check out the audio and video player based on FFmpeg and SDL

If you don’t understand this article, you can refer to my previous article Audio and Video Project— Audio and Video Player Analysis Based on FFmpeg and SDL (6)

parse

This article analyzes the four functions of decodethread, Init, Start, Stop, and Run. If you don’t know much about decodethread, it is recommended that you first read the previous article Analysis of Audio and Video Player Based on FFmpeg and SDL (6)

Init function
int DecodeThread::Init(AVCodecParameters* par){
    if(!par){
        return -1;
    }
    codec_ctx = avcodec_alloc_context3(nullptr);

    int ret = avcodec_parameters_to_context(codec_ctx, par);
    if(ret < 0){
        return -1;
    }

    AVCodec* codec;
    if(codec_ctx->codec_id == AV_CODEC_ID_H264){
        codec = avcodec_find_decoder_by_name("h264_qsv");
    }else {
        codec = avcodec_find_decoder(codec_ctx->codec_id);
    }
    if(!codec){
        return -1;
    }

    ret = avcodec_open2(codec_ctx, codec, nullptr);
    if(ret < 0){
        return -1;
    }
    return 0;
}

This initialization function is mainly responsible for the initialization of AVCodecContext.

First, avcodec_alloc_context3 is used to allocate memory for the AVCodecContext variable codec_ctx.

Next, we can see the avcodec_parameters_to_context function, which is used to convert an AVCodecParameters structure into an AVCodecContext structure. AVCodecParameters is a structure used in FFmpeg to represent codec parameters, including codec type, bit rate, frame rate, resolution and other information. AVCodecContext is a structure used to represent the codec context in FFmpeg, including codec status, input and output buffers and other information.

Then, we need to find the corresponding decoder and open it.

So, first make a conditional judgment. If the codec_id of the decoder context is AV_CODEC_ID_H264, which is an identifier of the H.264 codec, then find the codec through the name of "h264_qsv". Otherwise, call the avcodec_find_decoder function to find a decoder suitable for codec_ctx. Generally speaking, it is a method to find a decoder.

Then, there is the opening operation. avcodec_open2 is the operation of opening the decoder, which is relatively easy to understand.

Start, Stop function
int DecodeThread::Start(){
    mythread = new thread(&Run, this);
    if(!mythread){
        return -1;
    }
    return 0;
}

int DecodeThread::Stop(){
    Thread::Stop();
}

This code is not long and is relatively easy to understand.

The Start function is to create a new thread, and the Stop function is to call Stop of the parent class Thread. Its function is to terminate the thread and release resources.

Okay, we still have one function left, Run, which is more, we will talk about it in the next article.

If you want to know what happened next, please listen to the breakdown next time.

Guess you like

Origin blog.csdn.net/weixin_60701731/article/details/134439603