Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions dm/host/hle_movie.c
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,19 @@ HLE_FORCE("_ZN5movie7Decoder24SendInputBufferForDecodeEiiilj", "movie decoder: s
mdec_t *d = md_of(self); if (!d) return 0x1000;
if (i < 0 || i >= MD_NIN || off < 0 || size < 0 || (uint32_t)off + (uint32_t)size > MD_INCAP) return 0x100b;
dm_host_mutex_lock(&d->mu);
for (int k = 0; k < d->npend; k++) if (d->pend[k].in == i) { pthread_mutex_unlock(&d->mu); if (!d->red_dec++) LOG("[RED] movie: input index %d sent twice, the second packet is dropped (first case)", i); return 0x100b; }
// ACodec::BaseState::onInputBufferFilled takes ownership of a client buffer but keeps it once mPortEOS[input] is set; MediaCodec's queueInputBuffer still succeeds.
// Inputs already acquired before EOS can arrive afterwards, with or without the flag. Tied Together's Unity player submits all four held indices as EOS: decoding
// each one produced four completion callbacks, and the first freed the event list still being traversed. Return only a client's buffer, once, without another decode or signal.
if (d->in_eos) {
if (!d->inown[i] && !d->instale[i]) { pthread_mutex_unlock(&d->mu); return 0x100b; }
if (d->inown[i]) d->infree[d->ninfree++] = i; // a stale buffer was already returned by Flush / Stop
d->inown[i] = d->instale[i] = 0;
pthread_mutex_unlock(&d->mu);
MD_TRACE(d, 7, "SendInputBufferForDecode index %d after end of stream: returned without decoding", i);
return 0;
}
if (d->npend == MD_NIN) { pthread_mutex_unlock(&d->mu); if (!d->red_dec++) LOG("[RED] movie: more packets pending than input buffers, one dropped (first case)"); return 0x100b; }
// An index the client acquired before a Flush and sends after it (Big Brain Academy: Brain vs. Brain loops its activity previews this way): the flush put it back in the free list,
// so it is claimed from there now. Before, it could be handed out a second time, two packets shared one buffer (VideoToolbox -12909 on the overwritten one) and the fifth pending
// packet wrote past pend[] into npend and dmu (the process died destroying dmu, 2 runs in 5)
Expand All @@ -747,14 +760,12 @@ HLE_FORCE("_ZN5movie7Decoder24SendInputBufferForDecodeEiiilj", "movie decoder: s
break;
}
d->inown[i] = d->instale[i] = 0;
if (flags & 4) d->in_eos = 1; // the last input until Flush / Stop (h_movie_AcqIn)
for (int k = 0; k < d->npend; k++) if (d->pend[k].in == i) { pthread_mutex_unlock(&d->mu); if (!d->red_dec++) LOG("[RED] movie: input index %d sent twice, the second packet is dropped (first case)", i); return 0x100b; }
if (d->npend == MD_NIN) { pthread_mutex_unlock(&d->mu); if (!d->red_dec++) LOG("[RED] movie: more packets pending than input buffers, one dropped (first case)"); return 0x100b; }
int fmt_now = 0;
if (d->type == MD_TYPE_VP9 && !d->w && size > 0 && vp9_size(d->in[i] + off, size, &d->w, &d->h)) LOG("[movie] VP9 key frame: %d×%d", d->w, d->h);
if (d->type == MD_TYPE_VP8 && !d->w && size > 0 && vp8_size(d->in[i] + off, size, &d->w, &d->h)) LOG("[movie] VP8 key frame: %d×%d", d->w, d->h);
if (!d->fmt_sent) { d->fmt_sent = 1; fmt_now = 1; if (!d->w && !MD_AUDIO(d)) { d->w = 1920; d->h = 1080; } } // the size is fixed from here on (slots are allocated by it)
d->pend[d->npend].in = i; d->pend[d->npend].off = off; d->pend[d->npend].size = size; d->pend[d->npend].pts = pts; d->pend[d->npend].flags = flags; d->npend++;
if (flags & 4) d->in_eos = 1; // commit only an accepted EOS: no more input until Flush / Stop
int queued = d->npend;
pthread_mutex_unlock(&d->mu);
MD_TRACE(d, 7, "SendInputBufferForDecode index %d offset %d length %d time %lld µs flags %#x (%d packets queued)", i, off, size, (long long)pts, flags, queued);
Expand Down
63 changes: 60 additions & 3 deletions dm/host/tests/t_movie_avc.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,26 +162,81 @@ static void sequence_boundaries(void) {
// End of stream with three pictures held for reordering (POC type 0): an end-of-stream packet without data, nout output slots of which the client
// returns each one it takes. Every picture comes out without the end-of-stream flag, then one output without a picture carries it with the packet's time
static void drain(int nout) {
static mdec_t d; d = (mdec_t){ .type = MD_TYPE_AVC, .fmt = MD_FMT_NV12, .nout = nout, .started = 1, .w = 16, .h = 16 };
static mdec_t d; d = (mdec_t){ .type = MD_TYPE_AVC, .fmt = MD_FMT_NV12, .nout = nout, .started = 1, .fmt_sent = 1, .w = 16, .h = 16 };
pthread_mutex_init(&d.mu, NULL); pthread_mutex_init(&d.dmu, NULL); pthread_mutex_init(&d.wmu, NULL); pthread_cond_init(&d.wcv, NULL);
assert(!pthread_create(&d.wk, NULL, md_worker, &d));
md_avc_open(&d); writer w; size_t n = sps(&w, 100, 0, 1, 0); keep_ps(d.avc, w.p, n);
for (int i = 0; i < 3; i++) queue_picture(d.avc);
d.pend[0].in = 0; d.pend[0].pts = 777; d.pend[0].flags = 4; d.npend = 1; d.ninfree = MD_NIN - 1;
void *self[2] = { NULL, &d }; int got = 0, idx, left, inputs[MD_NIN]; int64_t pts; uint32_t flags;
d.ninfree = MD_NIN; for (int i = 0; i < MD_NIN; i++) d.infree[i] = i;
for (int i = 0; i < MD_NIN; i++) assert(!h_movie_AcqIn(self, &inputs[i], &left));
// Hold every output so the actual Send entry point queues the first EOS without decoding it. The other three inputs were already held by the client.
for (int i = 0; i < nout; i++) d.out[i].used = 1;
for (int i = 0; i < MD_NIN; i++) assert(!h_movie_SendIn(self, inputs[i], 0, 0, 777 + i, 4));
assert(d.in_eos && d.npend == 1 && d.pend[0].pts == 777 && d.ninfree == MD_NIN - 1 && !g_ndfq);
assert(h_movie_AcqIn(self, &idx, &left) == MD_NO_INPUT);
assert(h_movie_SendIn(self, inputs[0], 0, 0, 888, 4) == 0x100b); // the first EOS is still queued, not a client-owned buffer
assert(h_movie_SendIn(self, inputs[1], 0, 0, 888, 4) == 0x100b); // an already returned late input is not returned twice
assert(d.npend == 1 && d.ninfree == MD_NIN - 1);
for (int i = 0; i < nout; i++) d.out[i].used = 0;
md_pump(&d);
// a video decoder's results are queued for the deferred thread, due at once, not signalled on the decoding thread (md_pump_ev: it is not a guest thread)
int queued_out = 0; for (int i = 0; i < g_ndfq; i++) if (g_dfq[i].d == &d && g_dfq[i].ev == 1) { assert(g_dfq[i].due == now_ns()); queued_out = 1; }
assert(queued_out); md_defer_drop(&d); assert(!g_ndfq);
void *self[2] = { NULL, &d }; int got = 0, idx, left; int64_t pts; uint32_t flags;
for (;;) { // the client returns each output it takes, so the next one is always ready
assert(!h_movie_AcqOut(self, &idx, &pts, &flags, &left));
if (++got < 4) assert(d.out[idx].pic && pts == 0 && !(flags & 4));
else { assert(!d.out[idx].pic && pts == 777 && flags == 4 && !d.avc_drain && !mavc_queued(d.avc) && !left); break; }
assert(md_rel(&d, idx));
}
assert(md_rel(&d, idx));
assert(h_movie_AcqOut(self, &idx, &pts, &flags, &left) == MD_NO_OUTPUT && d.ninfree == MD_NIN);
assert(h_movie_SendIn(self, inputs[0], 0, 0, 999, 4) == 0x100b && d.ninfree == MD_NIN); // the consumed first EOS is no longer client-owned
assert(!h_movie_Flush(self) && !d.in_eos);
assert(!h_movie_AcqIn(self, &idx, &left));
assert(!h_movie_SendIn(self, idx, 0, 0, 999, 4));
assert(!h_movie_AcqOut(self, &idx, &pts, &flags, &left) && pts == 999 && flags == 4 && !left);
assert(md_rel(&d, idx));
assert(h_movie_AcqOut(self, &idx, &pts, &flags, &left) == MD_NO_OUTPUT);
md_defer_drop(&d);
md_job(&d, MDJ_QUIT); pthread_join(d.wk, NULL);
for (int i = 0; i < MD_NOUT; i++) free(d.out[i].frm);
}
// No output slots: submissions remain queued, so input ownership and the EOS commit can be checked independently of decode scheduling.
static void terminal_inputs(void) {
mdec_t d = { .type = MD_TYPE_AVC, .fmt_sent = 1, .started = 1, .ninfree = MD_NIN };
pthread_mutex_init(&d.mu, NULL); pthread_mutex_init(&d.dmu, NULL);
void *self[2] = { NULL, &d }; int inputs[MD_NIN], idx, left;
for (int i = 0; i < MD_NIN; i++) d.infree[i] = i;
for (int i = 0; i < MD_NIN; i++) assert(!h_movie_AcqIn(self, &inputs[i], &left));
assert(h_movie_SendIn(self, -1, 0, 0, 0, 4) == 0x100b);
assert(h_movie_SendIn(self, inputs[0], -1, 0, 0, 4) == 0x100b);
assert(h_movie_SendIn(self, inputs[0], 0, MD_INCAP + 1, 0, 4) == 0x100b);
assert(!d.in_eos && !d.npend && d.inown[inputs[0]]);
assert(!h_movie_SendIn(self, inputs[0], 0, 1, 111, 0));
assert(h_movie_SendIn(self, inputs[0], 0, 0, 222, 4) == 0x100b);
assert(!d.in_eos && d.npend == 1 && d.pend[0].size == 1 && d.pend[0].pts == 111);
assert(!h_movie_SendIn(self, inputs[1], 0, 1, 222, 4)); // the accepted EOS can carry payload
assert(!h_movie_SendIn(self, inputs[2], 0, 1, 333, 0)); // ordinary data after EOS is returned too
assert(!h_movie_SendIn(self, inputs[3], 0, 0, 444, 4));
assert(d.in_eos && d.npend == 2 && d.pend[0].pts == 111 && d.pend[1].pts == 222 && d.pend[1].size == 1 && d.ninfree == 2);
// The pump removes a packet before decoding it: it is neither pending nor free nor client-owned. A repeat must not return its index while the worker still uses it.
d.npend = 0;
assert(h_movie_SendIn(self, inputs[1], 0, 0, 555, 4) == 0x100b && d.ninfree == 2);
assert(!h_movie_Flush(self) && !d.in_eos && d.ninfree == MD_NIN);
for (int i = 0; i < MD_NIN; i++) assert(!h_movie_AcqIn(self, &inputs[i], &left));
for (int i = 0; i < MD_NIN; i++) assert(!h_movie_SendIn(self, inputs[i], 0, 1, i, 0));
assert(d.npend == MD_NIN && !d.in_eos);
assert(h_movie_SendIn(self, inputs[3], 0, 0, 0, 4) == 0x100b && !d.in_eos && d.npend == MD_NIN);
// Flush returns still-held inputs before their late sends. They already occur in the free list and must not be added a second time after the next EOS.
assert(!h_movie_Flush(self));
for (int i = 0; i < MD_NIN; i++) assert(!h_movie_AcqIn(self, &inputs[i], &left));
assert(!h_movie_Flush(self));
assert(!h_movie_SendIn(self, inputs[0], 0, 0, 666, 4));
for (int i = 1; i < MD_NIN; i++) assert(!h_movie_SendIn(self, inputs[i], 0, 0, 666, 4));
assert(d.npend == 1 && d.ninfree == MD_NIN - 1);
pthread_mutex_destroy(&d.mu); pthread_mutex_destroy(&d.dmu);
}
// Native-texture slots (md_rendered): one written into the client's texture stays taken, so no packet is decoded into it before the client's own release can come (GitHub
// issue #700). A client that releases what it rendered keeps every slot until its release; one that has not yet gets the earlier slot back at its next GetOutputBuffer
static void take(mdec_t *d, int i) { d->out[i].used = d->out[i].cli = 1; }
Expand Down Expand Up @@ -235,4 +290,6 @@ int main(void) {
puts("AVC: POC type 2 returns the final picture without EOS; other POC modes retain reordering and EOS drain");
drain(MD_NOUT_VIDEO); drain(2);
puts("AVC end of stream: every queued picture leaves without the flag, then an output without a picture carries it, also when the client holds the slots");
terminal_inputs();
puts("movie inputs: already-held inputs after EOS are returned once without another output; rejected sends keep the stream open, Flush accepts a new stream");
}
1 change: 1 addition & 0 deletions docs/agents/symptoms.md
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ One line per symptom in the [subsystem references](../reference/): what you see
- a crash reading address 0 at `main+0xab4e18` right after `[movie] Stop` and `decoder … destroyed`, at the first skip of the intro movie or the first menu change (Mario Strikers: Battle League; `MoviePlayerWorker` in the thread dump). → [Video decoded off the caller's thread](../reference/movies.md#video-decoded-off-the-callers-thread)
- the process dies when an activity starts, 2 runs in 5, with no `CRASH` line: the run ends with exit 137 (SIGKILL), and `~/Library/Logs/DiagnosticReports/dmhost-*.ips` has `EXC_BREAKPOINT` in `_os_unfair_lock_corruption_abort` ← `pthread_mutex_destroy` ← `md_free` (the movie decoder's destructor). → [Input indices returned by a flush while a client holds them](../reference/movies.md#input-indices-returned-by-a-flush-while-a-client-holds-them)
- when the prologue movie was skipped, 2.25 GB were mapped within seconds, past the application pool, and the game crashed in its allocator (DRAGON QUEST XI S). → [End of stream is the last input until `Flush` or `Stop`](../reference/movies.md#end-of-stream-is-the-last-input-until-flush-or-stop)
- the intro finishes, then the game crashes while calling its movie-completion handlers. → [Input buffers already held when end of stream arrives](../reference/movies.md#input-buffers-already-held-when-end-of-stream-arrives)
- decoding the end-of-stream packet gave VideoToolbox −12909 and AudioToolbox −50. → [End-of-stream packets with no data, and deleted decoders](../reference/movies.md#end-of-stream-packets-with-no-data-and-deleted-decoders)
- the game never leaves a movie that has played to its end; the last picture stays on screen. → [H.264 drain at the end of a stream goes on with released slots](../reference/movies.md#h264-drain-at-the-end-of-a-stream-goes-on-with-released-slots)
- a looping tutorial loses B-frame presentation order after the first loop, without a `Flush` or end-of-stream packet. → [H.264 loops retain presentation order when timestamps restart](../reference/movies.md#h264-loops-retain-presentation-order-when-timestamps-restart)
Expand Down
8 changes: 8 additions & 0 deletions docs/reference/movies.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ The SDK's movie library (`movie::Decoder`, `movie::Extractor`), which the host t
- **Fix:** also as with MediaCodec, an input marked end-of-stream is the last until `Flush` / `Stop`: no input index is handed out or announced after it (`in_eos` in `hle_movie.c`).
- **Found on:** [Metroid Dread](../../dm/games/010093801237C000/README.md) (movie sub-list); [DRAGON QUEST XI S](../../dm/games/01006C300E9F0000/README.md).

### Input buffers already held when end of stream arrives

- **Symptom:** the intro finishes, then the game crashes while calling its movie-completion handlers.
- **Cause:** stopping new input acquisitions at end of stream does not cover buffers the client already holds. Tied Together's Unity player acquires all four inputs and sends each with the end-of-stream flag. The host queued all four, producing four completion notifications; the first handler freed the event list that the later notifications still used.
- **Fix:** `h_movie_SendIn` accepts the first end-of-stream packet and returns subsequent client-held inputs once, without decoding or signalling them, whether or not they carry the flag. A queued or decoding input cannot be returned this way. An input returned by `Flush` is already on the free list and is not added again. Duplicate and full-queue checks precede ownership changes, and only an accepted packet commits `in_eos`. `Flush` / `Stop` still allow a new stream.
- **Found on:** Tied Together (`0100B6D00C2DE000`) 1.0.2, with a provisional profile on the Mac. The intro reaches the title menu after the fix.
- **Check:** `t_movie_avc` feeds four held inputs through `SendInputBufferForDecode`, drains the reordered pictures and checks that only one output carries end of stream. It also covers late ordinary data, duplicate and invalid submissions, an input being decoded, and held inputs sent after `Flush`. The four-input check fails on the previous host.

### End-of-stream packets with no data, and deleted decoders

- **Symptom:** decoding the end-of-stream packet gave VideoToolbox −12909 and AudioToolbox −50.
Expand Down
Loading