Log in

View Full Version : FFMPEG decoding errors with mkv to hevc bitstream


-QfG-
24th January 2025, 18:56
Hello,

can anyone help me please? i often demux mkv video streams to raw files via FFMPEG. Now i see at one movie, that ffmpg produces wrong colors in the pictures. I use this command line:

ffmpeg.exe -loglevel error -stats -i "MyMovie.mkv" -c:v copy -bsf:v hevc_mp4toannexb -f hevc "MyMovie.hevc"

HERE (https://mega.nz/folder/9k8BhIyA#p82JcpYlivQpp7Xqlb1q-A) the sample file if needed for testing.

ORIGINAL PICTURES FROM MOVIE:
https://i.ibb.co/vLnytv9/SAMPLE.jpg

DEMUXED WITH FFMPEG:
https://i.ibb.co/Wy629ww/SAMPLE-FFMPEG-DEMUX-mkv.jpg

You see in the Preview that the colors not correct. I have never seen this before. Is here anyone with a solution?

Z2697
24th January 2025, 19:41
There're 2 "sets of parameter sets" in extracted bitstream and in the PPS the chroma qp offset is different.
This is not FFmpeg's fault, the source file contains these parameter sets.
Multiple "sets of parameter sets" is normal (and needed for HDR according to x265), but not like this.
There should be only one correct "set of parameter sets" (please help me figure out a better name for them) for each IDR frame (GOP).
https://files.catbox.moe/6p3l8w.png
https://files.catbox.moe/ibouef.png
https://files.catbox.moe/wqc5sa.png

Z2697
24th January 2025, 20:20
Or it's FFmpeg's fault to some degree:
There's one extra "set of parameter sets" with incorrect PPS in the beginning of source file. That's the culprit, but FFmpeg also fails to recognize the correct one when extracting.

I guess what happened here is that:
- 1. when playing back, the decoder encountered first (incorrect) set, then immediately encountered second (correct) set, it ignores the first set, the correct one is then used. Playback is normal.
- 2. when extracting, the "HEVC muxer" encountered first (incorrect) set, thinks it's the correct one.
- 2.1 the muxer encountered second (correct) set, didn't realize it's the correct one but still pass it through.
- 2.2 the muxer encountered IDR frame, puts the first (incorrect) set in front of it.
- 2.3 the muxer encountered the correct sets for following IDR frames, but keeps ignoring and passing it through, then puts the incorrect set in front of IDR frames.
- 2.4 when playing the extracted bitsteram, the correct set is encountered first, then incorrect set, then the correct one is ignored, the incorrect one is used.

Yes, it's actually a muxer in FFmpeg.
(With somewhat recent addition of parameter sets normalization, mkvtoolnix/mkvextract can also alter the extracted bitstream, but it recognizes the correct one.)

-QfG-
24th January 2025, 20:27
i think i have a solution for this:

ffmpeg.exe -loglevel error -stats -i "MyMovie.mkv" -c:v copy -bsf:v hevc_metadata -f hevc "MyMovie.hevc"

instead of:

ffmpeg.exe -loglevel error -stats -i "MyMovie.mkv" -c:v copy -bsf:v hevc_mp4toannexb -f hevc "MyMovie.hevc"

works fine, no more errors in the hevc stream pictures.

Z2697
24th January 2025, 22:30
Better investigate how the incorrect parameter sets get in there as well.