// 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 #include #include #include 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 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(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 sector(0x800, 0); const uint8_t video[] = {0x00, 0x00, 0x01, 0xE0, 0x00, 0x0B, 0x81, 0x00, 0x00}; std::memcpy(§or[0], video, sizeof video); std::memcpy(§or[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(padLength >> 8), static_cast(padLength)}; std::memcpy(§or[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; } }