Decrypts the inside story of FFmpeg playback Status Control and decrypts the inside story of ffmpeg playback.

Source: Internet
Author: User

Decrypts the inside story of FFmpeg playback Status Control and decrypts the inside story of ffmpeg playback.

In the previous article (http://my.oschina.net/u/2336532/blog/400790), we solved how to deal with the extended data of H264 and AAC in FFmpeg, and restored the starting code of H264 and the ADTS header of AAC according to the NALU length, in this case, playing is normal. This article describes how to implement the track mode control based on FFmpeg, that is, how to use the functions provided by FFmpeg to implement basic seek, fast forward, and fast return. Well, there is little nonsense. Next we will start our FFmpeg-based track mode tour.

FFmpeg provides a seek function. The prototype is as follows:

Int av_seek_frame (AVFormatContext * s, int stream_index, int64_t timestamp, int flags );

Parameter description:

S: Operation context;

Stream_index: Basic stream index, indicating which basic stream the current seek is for, such as video or audio.

Timestamp: the time point for seek. The unit is time_base or AV_TIME_BASE.

Flags: seek flag, which can be set to byte. When seek is used, the key frame before or after the specified point is taken, and the key frame seek is not followed. For details, see avformat of FFmpeg. h description. Almost all track modes based on FFmpeg are implemented directly or indirectly using this function.

 

  • Seek of the fileFunction implementation

To jump to the first I frame of the video in 100 seconds (100 ms) (if not, find the first one forward ):

Av_seek_frame (pFormatCtx, vid_index, 100000 * vid_time_scale/time_base, AVSEEK_FLAG_BACKWARD );

Jump to the frame (sampling) of the audio at 80 seconds (80 000 milliseconds ):

Av_seek_frame (pFormatCtx, aud_index, 80000 * aud_time_scale/time_base, AVSEEK_FLAG_BACKWARD );

Jump to the start of the file to start playing:

Av_seek_frame (pFormatCtx, vid_index, 0, AVSEEK_FLAG_BACKWARD );

The above time_scale and time_base can be obtained through the over-current information. Please refer to the previous article. Some files may not succeed in seek. You can consider changing AVSEEK_FLAG_BACKWARD to AVSEEK_FLAG_ANY to seek in case of failure. However, the video frame sent by seek may not be an I frame.

This function can be seek to any reasonable position no matter what time you are currently. For example, if you want to jump back or forward to the current base for 10 seconds, the package obtained by the av_read_frame function contains the current timestamp. You can add or remove a minimum of 10000 (converted to the unit of playback time) and then seek. Therefore, this function can be used for drag-and-drop, forward/backward, loop playback, and other functions.

For fast forward, the decoder can achieve twice or even higher playback speeds. In this case, you can simply follow the basic playback process of the previous article. However, for high-speed playback, such as 4 times, 8 times, 16 times, and 32 times, it is generally not possible to send data at a frame as traditional playback, not just the decoding capability, data Reading may also be unable to keep up with the bandwidth. We can only extract the I frame for playing and discard the B and P frames. Next we will discuss the implementation process of this process.

In fast-forward mode, obtain the current time PTS through the current data packet, convert the PTS into a time plus a short time, and find the key frame as the seek time point. In this case, flags can be set to AVSEEK_FLAG_FRAME. Then the key frame is obtained using av_read_frame. After decoding and displaying the frame, add a short time before the PTS time of the frame, and then repeat the above process. The process is as follows:

 

During fast return, the current time PTS is obtained through the current data packet. The PTS is converted to a time minus a short time and used as the seek time point to locate the key frame. In this case, flags can be set to AVSEEK_FLAG_BACKWARD. Then the key frame is obtained using av_read_frame. After decoding and displaying the frame, subtract a short period of time from the PTS time of the frame before seek. In this way, the above results are repeated. The process is as follows:

In this way, we can use the av_seek_frame function to implement various track modes of the file playback. After understanding this function, you will have many other methods to implement it, here, we only provide a simple method with a small amount of memory. In a specific case, you can go through all frames and record the required information, such as the time stamp, frame number, and position of all I frames, then, you can directly obtain information from the table and then perform seek and read these key frames for fast playback.

 

To get more original articles in the first place, please follow the personal public platform: coder_online, scan the QR code below or search for coder_online. There are a large number of Android, Chromium, linux, programming skills, and other related articles are waiting for you. We can also communicate online.

If you need to reprint this article, please indicate the source: http://my.oschina.net/u/2336532.

Thank you for your cooperation!

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.