From 9f5d074a0ecfbdb531bfd8e8e3b899789b832b0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Wed, 21 Oct 2026 16:24:09 +0200 Subject: [PATCH] sceVideocodec: Picture records, SEI and sequence timing, for UMD Video The VSH's UMD Video player reads what the ME writes beside each decoded picture, which games' mpeg.prx only passes through (pspautotests video/mpeg/avcdetail, recorded on a PSP): - Given a 0x540-byte buffer at ctx+0x38, as mpeg_vsh.prx provides in the modes the player uses, the ME points each picture descriptor's +0x24 and +0x28 at two 0xA4-byte records for it. The first is the picture's SEI record: cpb_removal_delay at +0x18, dpb_output_delay at +0x1C, the pic_struct at +0x20 (which the player picks a duration with), and flags of 7 for a keyframe and 2 otherwise. sceVideocodecGetSEI writes the record of the last access unit decoded to the buffer at ctx+0x50. - A descriptor's +0x20 is 1 for a keyframe. The player drops every picture after starting or seeking until it sees one. - The 0x64 bytes at ctx+0x30 carry the VUI timing (num_units_in_tick at 240000 ticks a second), which the player checks against 5005 for film. All of it comes from what ffmpeg's H.264 parser reports per access unit, which AvcDecoder now hands back with each picture. Each picture also goes into the next of the four frame stores, as the ME rotates through its own: the player converts a picture in strips spread over its next few frames, while decoding goes on. Co-Authored-By: Claude Opus 5.5 (1M context) --- Core/HLE/sceVideocodec.cpp | 92 +++++++++++++++++++++++++++++++++++++- Core/HW/AvcDecoder.cpp | 45 +++++++++++++++++++ Core/HW/AvcDecoder.h | 20 +++++++++ pspautotests | 2 +- 4 files changed, 156 insertions(+), 3 deletions(-) diff --git a/Core/HLE/sceVideocodec.cpp b/Core/HLE/sceVideocodec.cpp index 71eaf04e38..7eabd4ffd9 100644 --- a/Core/HLE/sceVideocodec.cpp +++ b/Core/HLE/sceVideocodec.cpp @@ -45,6 +45,7 @@ #include "Core/HLE/scePower.h" #include "Core/HW/AvcDecoder.h" #include "Core/MemMap.h" +#include "Core/MemMapHelpers.h" #include "Core/MIPS/MIPS.h" // The context the caller hands us is 96 bytes. The offsets below are what mpeg.prx actually @@ -60,6 +61,9 @@ enum { CTX_AU_DATA = 36, // the access unit to decode CTX_AU_SIZE = 40, CTX_YUV_STRUCT = 44, // type 0: pointer to the eight output buffers + CTX_SEQUENCE_INFO = 48, // 0x64 bytes the ME fills from the stream's parameter sets + CTX_PICTURE_INFO = 56, // optional 0x540 bytes for per-picture records (mpeg_vsh.prx's modes 3-6) + CTX_SEI = 80, // where sceVideocodecGetSEI writes CTX_EDRAM_RAW = 92, }; @@ -98,6 +102,9 @@ struct VideocodecCtx { u32 frameBuffersSize = 0; int frameBufferWidth = 0; int frameBufferHeight = 0; + // Which of the MAX_PICTURES frame stores the next picture goes into. Not saved: after a load the + // pictures already handed out are gone either way. + int nextFrameStore = 0; }; static std::map g_videocodecCtxs; @@ -316,7 +323,7 @@ static bool PublishFrameBuffers(VideocodecCtx &vctx, u32 structAddr, int width, } for (int i = 0; i < 8; i++) { - buffers[i] = vctx.frameBuffers + slot * total + offsets[i]; + buffers[i] = vctx.frameBuffers + vctx.nextFrameStore * total + offsets[i]; } structAddr += slot * PICTURE_DESCRIPTOR_SIZE; @@ -333,6 +340,62 @@ static bool PublishFrameBuffers(VideocodecCtx &vctx, u32 structAddr, int width, return true; } +// The ME's SEI record (0xA4 bytes), which sceVideocodecGetSEI hands back for the last access unit +// decoded and the picture records below carry for each picture as it comes out (pspautotests +// video/mpeg/avcdetail). Read off that test's stream: +0x18 counts up by 2 per access unit from +// each buffering period and +0x1C stays 2, which is cpb_removal_delay and dpb_output_delay; +0x00 +// is 7 for the stream's IDR picture and 2 for the rest; a buffering period sets +0x64 and puts +// 90000 (its initial_cpb_removal_delay?) at +0x08 and +0x10, which the parser doesn't give us. +// +0x20 is 0 for the test's frames, and the UMD Video player reads it as the pic_struct. +// +0x21, +0x24 and +0x28 are copied from the test. +static const u32 SEI_RECORD_SIZE = 0xA4; + +static void WriteSeiRecord(u32 addr, const AvcTiming &timing) { + Memory::Memset(addr, 0, SEI_RECORD_SIZE, "VideocodecSei"); + if (!timing.picTiming) { + return; + } + Memory::WriteUnchecked_U32(timing.keyFrame ? 7 : 2, addr + 0x00); + Memory::WriteUnchecked_U32(timing.cpbRemovalDelay, addr + 0x18); + Memory::WriteUnchecked_U32(timing.dpbOutputDelay, addr + 0x1C); + Memory::WriteUnchecked_U32(0x100 | (timing.picStruct & 0xFF), addr + 0x20); + Memory::WriteUnchecked_U32(0x00030100, addr + 0x24); + Memory::WriteUnchecked_U32(1, addr + 0x28); + Memory::WriteUnchecked_U32(timing.bufferingPeriod ? 1 : 0, addr + 0x64); +} + +// Per-picture records, which the VSH's UMD Video player reads and games' mpeg.prx doesn't. Given a +// buffer at CTX_PICTURE_INFO, the ME points a picture descriptor's +0x24 and +0x28 at two 0xA4-byte +// records for it, 0x148 bytes per picture; without one both stay 0 (pspautotests +// video/mpeg/avcdetail). The first is the picture's SEI record; the second stayed zero there. +static void PublishPictureInfo(u32 ctxAddr, u32 yuvStructAddr, int slot, const AvcPicture &pic) { + const u32 descAddr = yuvStructAddr + slot * PICTURE_DESCRIPTOR_SIZE; + const u32 records = Memory::ReadUnchecked_U32(ctxAddr + CTX_PICTURE_INFO); + if (records == 0 || !Memory::IsValidRange(records + slot * 2 * SEI_RECORD_SIZE, 2 * SEI_RECORD_SIZE) || + !Memory::IsValidRange(descAddr, PICTURE_DESCRIPTOR_SIZE)) { + return; + } + const u32 record = records + slot * 2 * SEI_RECORD_SIZE; + WriteSeiRecord(record, pic.timing); + Memory::Memset(record + SEI_RECORD_SIZE, 0, SEI_RECORD_SIZE, "VideocodecPictureInfo"); + Memory::WriteUnchecked_U32(record, descAddr + 0x24); + Memory::WriteUnchecked_U32(record + SEI_RECORD_SIZE, descAddr + 0x28); +} + +// The ME fills this from the stream's parameter sets. Only the VUI timing is filled in here: the +// PSP's streams count time at 240000 ticks a second, so num_units_in_tick is 4004 for 29.97 fps +// (pspautotests video/mpeg/avcdetail) and 5005 for 23.976, which the UMD Video player checks for. +static void PublishSequenceInfo(const VideocodecCtx &vctx, u32 ctxAddr) { + const u32 info = Memory::ReadUnchecked_U32(ctxAddr + CTX_SEQUENCE_INFO); + int num, den; + if (info == 0 || !Memory::IsValidRange(info, 0x64) || !vctx.decoder->FrameRate(&num, &den)) { + return; + } + const u32 timeScale = 240000; + Memory::WriteUnchecked_U32((u32)((u64)timeScale * den / (2 * (u64)num)), info + 0x20); + Memory::WriteUnchecked_U32(timeScale, info + 0x24); +} + void VideocodecGetCtxInfo(std::vector *infos) { infos->clear(); for (const auto &[addr, ctx] : g_videocodecCtxs) { @@ -575,11 +638,21 @@ static int PublishPictures(VideocodecCtx &vctx, u32 ctxAddr, u32 outAddr) { if (published >= MAX_PICTURES || !PublishFrameBuffers(vctx, yuvStructAddr, pic.width, pic.height, published, buffers)) { break; } + // Each picture gets the next frame store, as the ME rotates through its own. A caller can + // still be reading the last one: the VSH's UMD Video player converts a picture in strips + // spread over its next few frames. + vctx.nextFrameStore = (vctx.nextFrameStore + 1) % MAX_PICTURES; WriteTiledYCbCr(buffers, pic); + // 1 for a picture decoding can start from (the stream's IDR in pspautotests + // video/mpeg/avcdetail), which mpeg.prx passes back from sceMpegAvcDecodeDetail. The UMD + // Video player drops pictures after starting or seeking until it sees one. + Memory::WriteUnchecked_U32(pic.timing.keyFrame ? 1 : 0, yuvStructAddr + published * PICTURE_DESCRIPTOR_SIZE + 0x20); + PublishPictureInfo(ctxAddr, yuvStructAddr, published, pic); published++; } } if (published > 0) { + PublishSequenceInfo(vctx, ctxAddr); Memory::WriteUnchecked_U32(pictures[0].width, outAddr + 8); Memory::WriteUnchecked_U32(pictures[0].height, outAddr + 12); Memory::WriteUnchecked_U32(1, outAddr + 28); @@ -761,8 +834,23 @@ static int sceVideocodecGetVersion(u32 ctxAddr, int type) { return MECall(hleLogInfo(Log::ME, 0), "videocodec getversion", getVersionUs); } +// Writes the SEI record of the last access unit decoded to the buffer at CTX_SEI. mpeg_vsh.prx's +// sceMpeg_6F314410 asks for it, and the UMD Video player times pictures from its +0x1C. static int sceVideocodecGetSEI(u32 ctxAddr, int type) { - return hleLogWarning(Log::ME, 0, "UNIMPL"); + if (!Memory::IsValidRange(ctxAddr, 96)) { + return hleLogError(Log::ME, -1, "bad context pointer"); + } + const u32 sei = Memory::ReadUnchecked_U32(ctxAddr + CTX_SEI); + if (!Memory::IsValidRange(sei, SEI_RECORD_SIZE)) { + return hleLogError(Log::ME, -1, "bad SEI buffer %08x", sei); + } + auto it = g_videocodecCtxs.find(ctxAddr); + AvcTiming timing; + if (it != g_videocodecCtxs.end() && it->second.decoder) { + timing = it->second.decoder->LastTiming(); + } + WriteSeiRecord(sei, timing); + return hleLogDebug(Log::ME, 0, "cpb_removal_delay %d, dpb_output_delay %d", timing.cpbRemovalDelay, timing.dpbOutputDelay); } static int sceVideocodecScanHeader(u32 ctxAddr, int type) { diff --git a/Core/HW/AvcDecoder.cpp b/Core/HW/AvcDecoder.cpp index 9b787ca40b..2d808c100d 100644 --- a/Core/HW/AvcDecoder.cpp +++ b/Core/HW/AvcDecoder.cpp @@ -166,6 +166,8 @@ void AvcDecoder::Flush() { #endif decoded_.clear(); outputTicks_.clear(); + timings_.clear(); + lastTiming_ = AvcTiming(); bufferingTick_ = 0; firstTiming_ = true; nextIndex_ = 0; @@ -360,6 +362,7 @@ void AvcDecoder::Release() { decoded_.pop_front(); outputTicks_.erase(d.index); released_++; + auto timing = timings_.find(d.index); outFrames_.push_back(d.frame); AvcPicture pic; for (int i = 0; i < 3; i++) { @@ -368,10 +371,44 @@ void AvcDecoder::Release() { } pic.width = d.frame->width; pic.height = d.frame->height; + if (timing != timings_.end()) { + pic.timing = timing->second; + timings_.erase(timing); + } pictures_.push_back(pic); #endif } +#ifdef USE_FFMPEG +// The parser doesn't report pic_struct itself, only what it decides from it (h264_parser.c). +static int PicStructFromParser(const AVCodecParserContext *parser) { + if (parser->picture_structure == AV_PICTURE_STRUCTURE_TOP_FIELD) { + return 1; + } + if (parser->picture_structure == AV_PICTURE_STRUCTURE_BOTTOM_FIELD) { + return 2; + } + switch (parser->repeat_pict) { + case 1: return parser->field_order == AV_FIELD_TT ? 3 : (parser->field_order == AV_FIELD_BB ? 4 : 0); + case 2: return parser->field_order == AV_FIELD_BB ? 6 : 5; + case 3: return 7; + case 5: return 8; + default: return 0; + } +} +#endif + +bool AvcDecoder::FrameRate(int *num, int *den) const { +#ifdef USE_FFMPEG + if (codecCtx_ && codecCtx_->framerate.num > 0 && codecCtx_->framerate.den > 0) { + *num = codecCtx_->framerate.num; + *den = codecCtx_->framerate.den; + return true; + } +#endif + return false; +} + int AvcDecoder::DecodePictures(const u8 *data, int size) { ClearPictures(); #ifdef USE_FFMPEG @@ -384,6 +421,7 @@ int AvcDecoder::DecodePictures(const u8 *data, int size) { // cpb_removal_delay and dpb_output_delay here, and whether this unit starts a buffering period. bool timed = false; s64 removalTick = 0; + lastTiming_ = AvcTiming(); if (parser_) { u8 *out = nullptr; int outSize = 0; @@ -398,7 +436,14 @@ int AvcDecoder::DecodePictures(const u8 *data, int size) { } firstTiming_ = false; outputTicks_[index] = removalTick + parser_->pts_dts_delta; + lastTiming_.picTiming = true; + lastTiming_.bufferingPeriod = parser_->dts_sync_point > 0; + lastTiming_.cpbRemovalDelay = parser_->dts_ref_dts_delta; + lastTiming_.dpbOutputDelay = parser_->pts_dts_delta; } + lastTiming_.picStruct = PicStructFromParser(parser_); + lastTiming_.keyFrame = parser_->key_frame == 1; + timings_[index] = lastTiming_; } FeedFfmpeg(data, size, index); diff --git a/Core/HW/AvcDecoder.h b/Core/HW/AvcDecoder.h index f2affa6e9d..192ffc6aa1 100644 --- a/Core/HW/AvcDecoder.h +++ b/Core/HW/AvcDecoder.h @@ -27,6 +27,17 @@ struct AVCodecContext; struct AVCodecParserContext; struct AVFrame; +// An access unit's timing SEI, as far as ffmpeg's parser reports it. +struct AvcTiming { + bool picTiming = false; // a picture timing SEI came with it + bool bufferingPeriod = false; // and a buffering period SEI + bool keyFrame = false; // a picture decoding can start from + int cpbRemovalDelay = 0; + int dpbOutputDelay = 0; + // H.264 pic_struct (0 a frame, 1/2 a field, 3-8 a frame shown as that many fields). + int picStruct = 0; +}; + // One decoded picture: three 8-bit 4:2:0 planes (Y, Cb, Cr). Strides are in bytes and are not // necessarily the width. struct AvcPicture { @@ -34,6 +45,7 @@ struct AvcPicture { int strides[3]{}; int width = 0; int height = 0; + AvcTiming timing; }; // An H.264 decoder for sceVideocodec, which is a low level interface to the PSP Media Engine. @@ -70,6 +82,11 @@ public: // Throws away any decoder state, for a seek or a new stream. void Flush(); + // The timing of the last access unit DecodePictures was given. + const AvcTiming &LastTiming() const { return lastTiming_; } + // The frame rate from the stream's parameter sets, false until they've been seen. + bool FrameRate(int *num, int *den) const; + bool HasFrame() const { return haveFrame_; } int Width() const { return width_; } int Height() const { return height_; } @@ -117,6 +134,9 @@ private: std::deque decoded_; // When each access unit's picture is due, in clock ticks, for streams with picture timing. std::map outputTicks_; + // Each access unit's timing, while its picture hasn't come out yet. + std::map timings_; + AvcTiming lastTiming_; s64 bufferingTick_ = 0; bool firstTiming_ = true; int nextIndex_ = 0; diff --git a/pspautotests b/pspautotests index 2bed6f5c3a..db67bdd33c 160000 --- a/pspautotests +++ b/pspautotests @@ -1 +1 @@ -Subproject commit 2bed6f5c3a62e9e6c704c2697b1b2c986aafe367 +Subproject commit db67bdd33c84cb4d46b236145a03c67ee74c9602