From 2dd9d8338b6d1b3f0dccc6c45aa4ffc5681e6ede Mon Sep 17 00:00:00 2001 From: Goatman13 <101417270+Goatman13@users.noreply.github.com> Date: Sun, 29 May 2022 18:48:39 +0200 Subject: [PATCH] IPU: Implement start code validation for BDEC/IDEC According to MPEG specs when 8 "0" bits are found, we should check for 000001xxh. Recommended way is to check 24 bits (000001h), and if they won't match discard 8 bits and try again until valid code is found, or stream ends. --- pcsx2/IPU/mpeg2lib/Mpeg.cpp | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/pcsx2/IPU/mpeg2lib/Mpeg.cpp b/pcsx2/IPU/mpeg2lib/Mpeg.cpp index c8b27176b2..2014b793ac 100644 --- a/pcsx2/IPU/mpeg2lib/Mpeg.cpp +++ b/pcsx2/IPU/mpeg2lib/Mpeg.cpp @@ -920,6 +920,7 @@ finish_idec: case 3: { u8 bit8; + u32 start_check; if (!getBits8((u8*)&bit8, 0)) { ipu_cmd.pos[0] = 3; @@ -929,7 +930,21 @@ finish_idec: if (bit8 == 0) { g_BP.Align(); - ipuRegs.ctrl.SCD = 1; + do + { + if (!g_BP.FillBuffer(24)) + { + ipu_cmd.pos[0] = 3; + return false; + } + start_check = UBITS(24); + if (start_check == 1) + { + ipuRegs.ctrl.SCD = 1; + break; + } + DUMPBITS(8); + } while (start_check != 1); } } [[fallthrough]]; @@ -1196,6 +1211,7 @@ __fi bool mpeg2_slice() case 4: { u8 bit8; + u32 start_check; if (!getBits8((u8*)&bit8, 0)) { ipu_cmd.pos[0] = 4; @@ -1205,7 +1221,21 @@ __fi bool mpeg2_slice() if (bit8 == 0) { g_BP.Align(); - ipuRegs.ctrl.SCD = 1; + do + { + if (!g_BP.FillBuffer(24)) + { + ipu_cmd.pos[0] = 4; + return false; + } + start_check = UBITS(24); + if (start_check == 1) + { + ipuRegs.ctrl.SCD = 1; + break; + } + DUMPBITS(8); + } while (start_check != 1); } } [[fallthrough]];