ffmpeg source code analysis - transcoding 6

This article has participated in the "Newcomer Creation Ceremony" activity, and together we will start the road to gold nugget creation.

This series is based on ffmpeg4.2 source code, download address: link: Baidu network disk  extraction code: g3k8

The ffmpeg source code analysis series starts with a simple command, ffmpeg -i a.mp4 b.flvto analyze its internal logic.

a.mp4 download link: Baidu Netdisk , extraction code: nl0s.

This article mainly analyzes reap_filter()the internal logic, the flow chart is as follows

Paste the code diagram of reap_filter() here. Red pens are important, green pens will not be implemented or are not important.

It can be seen that reap_filters() the logic of is relatively simple. Initialize the output stream first  init_output_stream() , then loop continuously, and call to   read the frame converted by the filter av_buffersink_get_frame_flags()from  there, and jump out of the while when it knows that it cannot be read. buffersinkThen  follow do_video_out() the call  do_audio_out() .

do_video_out() The logic is actually more complicated, and it seems to implement a frame rate conversion algorithm or something like that. However, as shown in the figure, the duration in the green box should be calculated according to different scenarios, so you don’t need to pay special attention to it. Also, the assignment and calculation of these variables will make you dizzy delta0. Not conducive to beginners to understand. So here I directly name these variables. You don’t need to pay special attention to these variables. Just read them once, and it’s okay if you don’t understand them. These algorithms should be combined with a specific frame rate conversion command to better explain them. You only need to pay attention   , because it is used in the for loop later, and  it is often 1 under this command.deltanb0_framesnb_framesost->last_nb0_framesnb_framesnb_frames


do_video_out()There is also a mandatory I-frame algorithm in it, and you don't need to pay special attention to it. This command is not used. Because the amount of code is too large, only very critical paragraphs are screenshotted here.

The key points have been drawn with flow charts and brushes. You can see that when do_video_out() sends a frame to the encoder, it will read the packet continuously until no packet can be read. In other words, if  buffersink no frame comes out, the encoder will not have any more packets, and there will be no packets in the cache, because all pakcets have been read in a loop last time, which is more important. So no need to handle  avcodec_receive_packet() EOF.

Another one output_packet()was not analyzed. This command does not use bitstream_filters, so output_packet()it actually calls write_packet() directly.

The process of write_packet() is relatively simple. The main point is the write cache  muxing_queue. Here is an explanation of what will write the cache.

All streams of the output file, including audio and video streams, will be called after all initialization is completed  avformat_write_header(), and then set  of->header_written to 1.

of->header_written equal to 0 means that the output stream is not initialized and init_output_stream()the function is not called. Under what circumstances will it not be called?

The video has not decoded any frame yet, resulting in  ifilter_send_frame() no execution. I said before that  ifilter_send_frame() it will be called  configure_filtergraph(), and configure_filtergraph() if it is not called, it will  ost->filter->graph->graph not be initialized. Then, about 1437 lines inside the reap_filter() function, it was  ost->filter->graph->graph executed when  the judgment was made continue , resulting in no execution  init_output_stream , no initialization of the video stream, so no call

avformat_write_header() Write header.

do_audio_out() is relatively simple and the principle is similar, so it will not be analyzed here.

 

Original  ffmpeg source code analysis - transcoding 6 - Nuggets

 

★The business card at the end of the article can receive free audio and video development learning materials, including (FFmpeg, webRTC, rtmp, hls, rtsp, ffplay, srs) and audio and video learning roadmaps, etc.

see below!

Guess you like

Origin blog.csdn.net/yinshipin007/article/details/131744306