Files
BozoEngine/tests/libs/mpeg_callbacks_tests.cpp
Thomas MartinandClaude Opus 5.5 fd67f9e899 libmpeg: callbacks on the caller's thread (MpegOptions::callbacksOnCaller)
Sony's libmpeg calls the stream callbacks inside sceMpegDemuxPss / sceMpegDemuxPssRing and Nodata inside
sceMpegGetPicture, on the caller's thread. The native library queued them for whichever thread ran next, which
deadlocks Steambot Chronicles' player (Sony's sample): its video callback waits on the IPU input ring's lock held by
the thread it was stacked on. With the option on (off by default), each callback is a handler bound to the calling
thread and the call is made again after it: the demux going on from where it was, GetPicture calling Nodata again
until a picture is there.

- Kernel::queue(handler, thread): a handler that runs only on that thread; handlerQueuedFor(thread) for the
  dispatcher's checks; the bindings saved in an optional section (older states load with none).
- Libraries::queueHandlerOn and restartCall: the free host stops the thread at the call's own address.
- MpegState::demuxResuming / demuxBytes, saved as an optional tail of the mpeg section.
- Tests: thread-bound handlers (handler_stacks), the demux and Nodata loop (mpeg_callbacks).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-29 13:39:05 +01:00

124 lines
5.9 KiB
C++

// SPDX-License-Identifier: MIT
// libmpeg's callbacks on the caller's thread (MpegOptions::callbacksOnCaller): sceMpegDemuxPssRing queues a stream
// packet's callback for its own thread and is made again after it, going on from where it was; sceMpegGetPicture
// queues Nodata for its own thread and is made again (the SDK's loop) until a picture is there.
#include "bozo/libs/libraries.h"
#include "bozo/libs/mpeg.h"
#include "bozo/ee/dispatcher.h"
#include <cstdio>
#include <cstring>
#include <stdexcept>
#include <vector>
using namespace bozo;
namespace
{
void check(bool v, const char *what)
{
if (!v)
throw std::runtime_error(std::string("mpeg callbacks: ") + what);
}
struct TestHost : ee::Host
{
libs::Libraries l;
void libraryCall(ee::Dispatcher &, R5900Context *, uint32_t) override { throw std::runtime_error("library"); }
void vu0Microprogram(ee::Dispatcher &, R5900Context *, uint32_t) override { throw std::runtime_error("VU"); }
void ioRead(ee::Dispatcher &, R5900Context *, uint32_t, uint32_t, uint64_t[2]) override { throw std::runtime_error("IO"); }
void ioWrite(ee::Dispatcher &, R5900Context *, uint32_t, uint32_t, const uint64_t[2]) override { throw std::runtime_error("IO"); }
void fault(ee::Dispatcher &, R5900Context *, const std::string &s) override { throw std::runtime_error(s); }
};
} // namespace
int main()
{
try
{
if (!libs::haveVideoDecoder())
{
std::puts("mpeg callbacks: no video decoder in this build, skipped");
return 0;
}
TestHost host;
ee::Dispatcher d(host);
host.l.attach(d);
host.l.mpegOptions = {.referenceLayout = false, .paced = false, .sdkNodata = true, .picturesAhead = 32,
.sdkDemux = true, .callbacksOnCaller = true};
d.boot(0x100000, 0x1fff000);
ee::Kernel &k = d.kernel();
k.setRunning(1);
uint8_t *ram = d.runtime().memory.ram();
R5900Context c;
c.r[28].uw[0] = 0x123456;
auto call = [&](const char *name, std::initializer_list<uint32_t> args) {
unsigned i = 4;
for (uint32_t a : args)
c.r[i++].uw[0] = a;
libs::Guest g(d.runtime(), &c);
const libs::Function *f = host.l.find(name);
check(f != nullptr, name);
f->handler(host.l, g);
return c.r[2].sw[0];
};
// A handler queued for thread 1 only, ended as the game's callback returning `result`.
auto endHandler = [&](int32_t result) {
check(k.handlerQueuedFor(1) && !k.handlerQueuedFor(2) && !k.handlerQueuedFor(0), "the callback is the caller's");
ee::Frame &f = k.stackHandler(*k.thread(1));
f.ctx.r[2].sd[0] = result;
auto run = host.l.takeHandlerEnd(f.sequence);
check(static_cast<bool>(run), "the callback has its end");
libs::Guest g(d.runtime(), &f.ctx);
run(host.l, g);
k.endHandler(*k.thread(1));
};
const uint32_t mp = 0x100000, work = 0x110000, data = 0x200000;
call("sceMpegInit", {});
call("sceMpegCreate", {mp, work, 0x10000});
call("sceMpegAddCallback", {mp, 1, 0x300000, 0x300100}); // Nodata
call("sceMpegAddStrCallback", {mp, 0, 0, 0x300200, 0x300300}); // the video stream
// Nodata: no picture yet, so GetPicture calls Nodata on its own thread and is made again.
call("sceMpegGetPicture", {mp, 0x400000, 1200});
check(host.l.takeRestartCall(), "GetPicture is made again after Nodata");
endHandler(1);
check(!host.l.mpeg.inputEnded, "Nodata's 1: the input goes on");
// One sector: a video packet, then padding.
const uint8_t payload[] = {0x00, 0x00, 0x01, 0xB3, 0x28, 0x01, 0xE0, 0x13};
std::vector<uint8_t> sector(0x800, 0);
const uint8_t video[] = {0x00, 0x00, 0x01, 0xE0, 0x00, 0x0B, 0x81, 0x00, 0x00};
std::memcpy(&sector[0], video, sizeof video);
std::memcpy(&sector[sizeof video], payload, sizeof payload);
const size_t padAt = sizeof video + sizeof payload, padLength = 0x800 - padAt - 6;
const uint8_t padding[] = {0x00, 0x00, 0x01, 0xBE, static_cast<uint8_t>(padLength >> 8), static_cast<uint8_t>(padLength)};
std::memcpy(&sector[padAt], padding, sizeof padding);
std::memcpy(ram + data, sector.data(), sector.size());
// The video packet's callback stops the demux: queued for this thread, the call made again.
call("sceMpegDemuxPssRing", {mp, data, 0x800, 0, 0});
check(host.l.takeRestartCall(), "the demux is made again after its callback");
check(host.l.mpeg.demuxResuming && host.l.mpeg.es.empty(), "the packet is held for its callback");
check(k.running() == 1, "the thread does not wait");
endHandler(1);
check(host.l.mpeg.es.size() == sizeof payload, "the packet the callback took goes to the decoder");
// Made again: it goes on from where it was, to the sector's end.
check(call("sceMpegDemuxPssRing", {mp, data, 0x800, 0, 0}) == 0x800, "the call ends with the whole sector");
check(!host.l.takeRestartCall() && !host.l.mpeg.demuxResuming, "nothing more to call back");
check(std::memcmp(host.l.mpeg.es.data(), payload, sizeof payload) == 0, "the video's bytes");
// Nodata's 0 (the SDK's callback): the input has ended.
call("sceMpegGetPicture", {mp, 0x400000, 1200});
if (host.l.takeRestartCall())
{
endHandler(0);
check(host.l.mpeg.inputEnded, "Nodata's 0 ends the input");
}
std::puts("mpeg callbacks on the caller's thread: demux made again around its callback, Nodata's loop, passed");
return 0;
}
catch (const std::exception &e)
{
std::fprintf(stderr, "%s\n", e.what());
return 1;
}
}