oneam / h264bsd

A simple h264 software decoding library
Other
196 stars 52 forks source link

How to handle h264bsdNextOutputPicture returning NULL? #27

Open alban-rochel opened 9 months ago

alban-rochel commented 9 months ago

Hello,

I am having trouble processing some H.264 streams. After using h264bsdDecode() and getting H264BSD_PIC_RDY, sometimes h264bsdNextOutputPicture() returns a NULL pointer. In the end, for a 122 images stream (122 times H264BSD_PIC_RDY, and 122 frames produced by ffmpeg when used to convert the .264 file to png), I may end up being able to decode 112 of them.

Those streams are specifically those extracted from MP4 generated by Safari on iPhones using the html MediaRecorder api (mimeType 'video/mp4;codecs="avc1.42001e" for instance). ffprobe shows that they are baseline 3.1 profiles, exactly as similar files produced on Macs (which do work). The only difference I can see is that iPhone files seem to be full color range, but patching this info does not seem to change the result.

Any lead or clue would be greatly appreciated.

Cheers,

Alban

alban-rochel commented 9 months ago

By the way, I found that if I hard-coded seqParamSet.maxDpbSize value to 2 after h264bsdDecodeSeqParamSet(&strm, &seqParamSet); in h264bsdDecode (default value is 5), I could decode more pictures (but not all of them). I don't know if my stream is ill-formed or if h264bsd misconfigures the decoder, but that might give clues regarding the nature of this bug.

alban-rochel commented 9 months ago

More testing shows that in my case (decoding streams from Safari encoded with the MediaRecorder API), forcing seqParamSet.numRefFrames = 1; seqParamSet.maxDpbSize = 1; allows decoding every frame but the last one on iOS sources, and all frames on OSX sources.

I hate doing these kinds of hacks, but at least it will do the trick for the moment.