Analysis of Application and release of FFmpeg AVFormatContext variable
Currently using version 3.4 of FFmpeg
Av_register_all
Avformat_open_input
Avformat_find_stream_info
Avcodec_find_decoder
Avcodec_open2
Av_read_frame
Avcodec_send_packet
Avcodec_receive_frame
Application process
1) call avformat_alloc_context to create an instance of AVFormatContext variable pAVFormatContext
AVFormatContext* pAVFormatContext = avformat_alloc_context ()
2) call avio_alloc_context to create an instance of AVIOContext variable pAVIOContext
AVIOContext* pAVIOContext = avio_alloc_context (mallocBuffer, mallocBufferSize, 0, this, ReadStreamData, NULL, NULL)
In this function, ReadStreamData is used to read the video or audio stream from the read network or file. MallocBuffer is used to save the read data for analysis. MallocBufferSize is the allocated cache length. Once the cache length requested by mallocBufferSize is less than the length of the data returned for reading, the data copied into the cache will be out of bounds and the program will crash.
3) if you already know that the format of the data is h364, call av_find_input_format to create an instance of AVInputFormat variable pAVInputFormat
AVInputFormat* pAVInputFormat = av_find_input_format ("h364")
PAVFormatContext- > iformat = pAVInputFormat
If (avformat_open_input (& pAVFormatContext, ", pAVInputFormat, NULL)
< 0) 4)开始探测码流格式 avformat_find_stream_info(pAVFormatContext, NULL); 释放过程 avformat_close_input(pAVFormatContext); 分析该函数分为三部分 第一部分 关闭输入: if (s->Iformat)
If (s-> iformat- > read_close)
S-> iformat- > read_close (s)
For playing rtsp://admin:admin888@192.168.28.130:554/h364/ch2/main/av_stream, it is mainly to send TearDown instructions to the camera.
Part II
Avio_close (pb)
Part III
Avformat_free_context (s)
The core of this function is to release the video and audio streams created by the application.
For (I = s-> nb_streams-1; I > = 0; iMury -)
Ff_free_stream (s, s-> streams [I])