From 21928ed43f580d15db1189ba018eda7f2c9716cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Thu, 17 Sep 2026 15:18:06 -0600 Subject: [PATCH] sceVideocodec: let stopping and deleting the decoder take time Both are ME round-trips that take real time on hardware, and returning from them immediately matters beyond speed. Jak and Daxter deletes its video_sound_thread straight after sceVideocodecDelete without waiting for it to exit. With no time passing in the delete, the game's audio thread never gets to run once more and deliver the wake that lets that thread notice the shutdown and exit, so sceKernelDeleteThread fails with NOT_DORMANT and the thread stays alive. It is then released from the event flag the game has just deleted, resumes on a context that has already been freed - every id and pointer in it zero - and copies from a null pointer. 2ms, chosen to sit above the 1.45ms an audio mix block takes. 100us was measured to be too short, so the fix is the time passing rather than just the reschedule. Co-Authored-By: Claude Opus 5 (1M context) --- Core/HLE/sceVideocodec.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Core/HLE/sceVideocodec.cpp b/Core/HLE/sceVideocodec.cpp index 6e74aefeec..8f1c81618f 100644 --- a/Core/HLE/sceVideocodec.cpp +++ b/Core/HLE/sceVideocodec.cpp @@ -594,12 +594,21 @@ static int sceVideocodecDecode(u32 ctxAddr, int type) { type, auBytes, gotFrame ? "frame" : "no frame yet", width, height); } +// Stopping or deleting the decoder is an ME round-trip and takes real time on hardware. Returning +// immediately matters beyond speed: a game can be relying on a thread of its own getting to run +// once more before it tears things down. Jak and Daxter deletes its video_sound_thread straight +// after sceVideocodecDelete without waiting for it to exit, and with no time passing here the audio +// thread never gets to deliver the wake that would let it exit - so the delete fails with +// NOT_DORMANT and the thread lives on, reading a context the game has already freed. +// One audio mix block is 64 samples at 44100Hz, about 1.45ms, so stay above that. +static const int videocodecTeardownDelayUs = 2000; + static int sceVideocodecStop(u32 ctxAddr, int type) { auto it = g_videocodecCtxs.find(ctxAddr); if (it != g_videocodecCtxs.end() && it->second.decoder) { it->second.decoder->Flush(); } - return hleLogInfo(Log::ME, 0); + return hleDelayResult(hleLogInfo(Log::ME, 0), "videocodec stop", videocodecTeardownDelayUs); } static int sceVideocodecDelete(u32 ctxAddr, int type) { @@ -608,7 +617,7 @@ static int sceVideocodecDelete(u32 ctxAddr, int type) { FreeContext(it->second); g_videocodecCtxs.erase(it); } - return hleLogInfo(Log::ME, 0); + return hleDelayResult(hleLogInfo(Log::ME, 0), "videocodec delete", videocodecTeardownDelayUs); } static int sceVideocodecGetVersion(u32 ctxAddr, int type) {