openh264 icon indicating copy to clipboard operation
openh264 copied to clipboard

Decoded B-frame buffer can be overwritten by next decoding frame

Open xiaotiansf opened this issue 7 years ago • 15 comments

In ffmpeg, it calls DecodeFrame2 once for each frame. However the decoded picture may not be ready immediately after the call (pDstInfo->iBufferStatus = 0). This is not a problem when B-frame decoding is not supported.

The problem comes when B-frame decoding is supported: The last decoded non-reference B-frame (pDstInfo->iBufferStatus = 0 after the call DecodeFrame2 ) (so it is disposable) buffer could be overwritten by next decoding frame because decoder thinks no-body refers to the B-frame.

So the correct way to do in ffmpeg is to call DecodeFrameNoDelay which contains two calls of DecodeFrame2. The second call allows to release the ready non-reference B-frame to be released immediately to prevent its buffer to be overwritten by next decoding frame.

Could someone provide help regarding this issue? Maybe there is a better solution?

@mstorsjo I am wondering that the issue in your test asset https://download.blender.org/peach/bigbuckbunny_movies/big_buck_bunny_480p_h264.mov. has something to do with this.

Thanks.

xiaotiansf avatar Jan 03 '19 20:01 xiaotiansf

If this is the case, the API of OpenH264 is wrong and you have the function names swapped.

If I want reordered frames, I should not call a function named DecodeFrameNoDelay. A function named DecodeFrameNoDelay should return the latest decoded frame directly without reordering, while the normal function that a caller is supposed to use, DecodeFrame2, should do reordering and generally do the right thing for users.

This would make things work for all existing users of OpenH264 even when encountering B-frames, without requiring them to change anything. If you require callers to suddenly start calling a different API, especially only if the bitstream uses a specific feature, the API is wrong/broken/unnecessarily complex.

mstorsjo avatar Jan 03 '19 20:01 mstorsjo

I can confirm that things do seem to work if using DecodeFrameNoDelay instead of DecodeFrame2, but that is not a change that you should force on all users of OpenH264. This is a detail that a decoder library should handle internally, and it is a failure of the library if it requires the callers to use a different API. And the DecodeFrameNoDelay function is completely and utterly misnamed right now.

mstorsjo avatar Jan 03 '19 20:01 mstorsjo

@mstorsjo Make sense and I'll look for an internal solution within the library then.

xiaotiansf avatar Jan 03 '19 21:01 xiaotiansf

This buffer overwritten issue has been resolved by my code update in #PR3080

xiaotianshicomcast avatar Jan 06 '19 04:01 xiaotianshicomcast

The changes in PR #3080 has not fixed the issue when using DecodeFrame2 at all. Since commit 5eba56f693e5ab52a0122bd992abeced524ebf36 decoding with DecodeFrame2 returns lots of errors - you can e.g. try with https://download.blender.org/peach/bigbuckbunny_movies/big_buck_bunny_480p_h264.mov.

And were there any change so that DecodeFrame2 actually returns the frames in the correct, reordered order which should be the default for any existing caller?

mstorsjo avatar Jan 23 '19 21:01 mstorsjo

@mstorsjo I'll take a look.

xiaotiansf avatar Jan 23 '19 22:01 xiaotiansf

@mstorsjo Do you have short movie which can also reproduce the issue? "big_buck_bunny_480p_h264.mov" is too long for me to debug because YUV is too big. Or if you can point me rough video segment locations in time in the movie so that I trim it. Thanks.

xiaotiansf avatar Jan 24 '19 00:01 xiaotiansf

I cut out a short sequence from it, available at https://martin.st/temp/big_buck_bunny_480p_h264_cut30.mov - it happens with any part of it.

Sorry, I referenced the wrong commit - before be962f9a0bc1140b925b10d5c41b22e5ed3fe63f , DecodeFrame2 returned 29 frames, returned no errors, but some of the returned frames were duplicated. After that DecodeFrame2 returns only 10 frames.

Things still do seem to work fine with DecodeFrameNoDelay though.

But the issue is very very simple. Whatever reordering and other logic that any normal caller will want applied, should be done in the normal, old DecodeFrame2 function which every user of openh264 already is using. If you want to provide a separate function which returns the latest decoded frame without any reordering, you can do that with a separate function (and DecodeFrameNoDelay sounds to me like a good name for it, but having that function do reordering is completely backwards to me).

So if you just want to swap the name of DecodeFrame2 and DecodeFrameNoDelay, that would be fine - then the old DecodeFrame2 works as it should for users.

mstorsjo avatar Jan 24 '19 07:01 mstorsjo

@mstorsjo Thanks a lot and I'll try to fix the issue without using DecodeFrameNoDelay.

xiaotiansf avatar Jan 24 '19 17:01 xiaotiansf

@mstorsjo I have tested with both DecodeFrameNoDelay and DecodeFrame2 (only) on latest openh264/master branch and I got identical yuv binary output files using your short asset https://martin.st/temp/big_buck_bunny_480p_h264_cut30.mov.

xiaotiansf avatar Jan 25 '19 02:01 xiaotiansf

@mstorsjo Maybe you are doing testing in differently?

xiaotiansf avatar Jan 25 '19 02:01 xiaotiansf

Yes, I'm testing with ffmpeg/ffplay/avconv/avplay, using libopenh264 via libavcodec.

It all boils down to whether DecodeFrameNoDelay is used, instead of DecodeFrame2, and if DecodeFrame2 is used like it is in h264dec with the -legacy option (ignoring the return value, calling DecodeFrame2 twice), and also whether NAL units are fed one at a time or a full frame at once.

With DecodeFrameNoDelay, it works as intended. I was previously firmly against using this function as I had the impression that it was a new addition for B-frame handling, but apparently it is available since openh264 1.4 and the recommended entry point for decoding. So with that in mind, I guess libavcodec, gstreamer and other users who are using DecodeFrame2 simply will need to change.

So in that case, I don't have any issue here anymore - sorry for not realizing before that DecodeFrameNoDelay was an old, existing entry point!

mstorsjo avatar Jan 25 '19 08:01 mstorsjo

@mstorsjo Thanks a lot for your input.

xiaotiansf avatar Jan 25 '19 23:01 xiaotiansf

I can confirm that using DecodeFrame2 (with git master commit 425ecbbbc4688ee1af9da957d187d380e9192e70) produces fewer frames with that sample for me - in fact only 3 when testing with GStreamer. I think that's a problem in the sense that existing code expects DecodeFrame2 to produce all frames, but possibly with a 1 frame delay between frame input and output.

It shouldn't lose frames and fail to decode now that B frames are introduced.

thaytan avatar Jan 29 '19 14:01 thaytan

@thaytan - FWIW, I changed the libavcodec wrapper to use DecodeFrameNoDelay here: https://git.libav.org/?p=libav.git;a=commitdiff;h=eec93e57096aa4804862d62760442380c70d489b - I guess GStreamer and others who still are using DecodeFrame2 need to change as well.

But plain use of DecodeFrame2 without the explicit flush is indeed rather broken on files with B-frames now.

mstorsjo avatar Jan 29 '19 14:01 mstorsjo