MPEG decoding in reverse using FFmpeg

I have a so-called "block" that stores some of the MPEG4 frames (I, p, p, p, p ...). For each "block", the frame begins with frame "I" and ends before the next "I" frame. (VOL - "visual_object_sequence_start_code" is always on before frame "I")

I need to be able to play these “blocking” frames in backward mode. The thickness is as follows:

  • It is impossible to simply take the last frame in my block and perform decoding, because it is a “P” frame, and it needs to decode “inter-frame (I)” correctly.

  • I can’t just take my first “I” frame and then pass it to the ffmpeg function “avcodec_decode_video” and only then pass my last “P” frame to ffmpeg, because this last “P” frame depends on the “P” frame before it , right? (well ... as far as I tested this method, my last decoded P-frame had artifacts)

Now the way that I perform reverse playback is to first decode all my “block” frames in RGB and store them in memory. (in most cases it will be ~ 25 frames per max block.) But this method really requires a lot of memory ... (especially if the frame resolutions are high) And I have the feeling that this is the wrong way to do it ...

So, I would like to ask if anyone has any suggestions on how this “reverse” decoding / playback of frames can be done using FFmpeg?

thanks

+4
source share
2 answers

What are you actually looking for research? To get a glimpse of the general approach, take a look at the following article:

Essentially, there is still an extended encoding based on key frames, however you can cancel the motion compensation process to achieve reverse flow. This is done by converting P-frames in the fly to I-frames. This requires special attention, but does not require much more memory. Perhaps you can save this as a new file and then apply it to a standard decoder with reverse playback requirements.

However, it is very difficult, and I have seen rare programs that do this in practice.

+3
source

I don’t think there is a way to start with an I-frame and decode all the P-frames due to the P-frame depending on the previous frame. To process decoded frames, they can be saved to a file or, with limited memory and additional processor power, older P-frames can be dropped and recounted later.

At the command level, you can convert the input video into a series of images:

ffmpeg -i input_video output%4d.jpg 

then somehow reverse the order and convert back to video:

 ffmpeg -r FRAME_RATE -i reverse_output%4d.jpg output_video 

You may consider preprocessing if this is an option.

+1
source

Source: https://habr.com/ru/post/1397757/


All Articles