H.264 movie packets are read as Annex B only: a 4-byte NAL length from 256 to 511 begins 00 00 01, so the length-prefixed fallback could not be told from a start code (issue #629)

The SDK's demuxer converts the MP4's length-prefixed samples to start codes,
also when it hands over the avcc record, so hle_movie_avc.c no longer guesses
a packet's framing: each_nal reads 3- or 4-byte start codes (leading zero
bytes allowed), and a packet or parameter set without one is refused with a
[RED] line instead of being read as NAL lengths, a fallback no game was seen
to need.

Checked on Kirby Star Allies, whose ImageMovie.mp4 has 167 of 1,386 samples
whose first NAL is 256 to 511 bytes: through real VideoToolbox, main's parser
decoded 3 of its pictures from the length-prefixed samples and all of them
from Annex B, and with this change a boot from an empty save decoded the
movie with 1387 packets in and 1387 frames out, no red line.

The ambiguity was reported by thehobbit85 in PR #1761 from an MGS3 report in
issue #629; this lands a different fix (no fallback rather than a stricter
one), so none of that PR's code is kept.
This commit is contained in:
seedds committed 2026-11-09 17:31:46 +08:00
1 parent 7238bfd18c
commit 79741184af
6 files changed
+55 -25

No files matched your search

+1
View File
@@ -61,5 +61,6 @@ No code of the game is cut, and no run-time key is set. The game asks for presen
## Found on this game
- [`nv12-colorspace` picks the game's YUV matrix](../../../docs/reference/movies.md#nv12-colorspace-picks-the-games-yuv-matrix).
- [H.264 packets are Annex B, not NAL lengths](../../../docs/reference/movies.md#h264-packets-are-annex-b-not-nal-lengths): its `ImageMovie.mp4` has 167 samples whose first NAL length begins `00 00 01`, and plays in full, so its packets come as Annex B.
- [Integer outputs told from float bits](../../../docs/reference/shaders.md#integer-outputs-told-from-float-bits).
- [A pipeline archive on an external drive is read into memory](../../../docs/reference/memory.md#a-pipeline-archive-on-an-external-drive-is-read-into-memory).
+18 -16
View File
@@ -92,26 +92,27 @@ static void keep_ps(mavc *a, const uint8_t *nal, size_t n) { // a parameter se
memcpy(dst, nal, n); *dn = (int)n; a->dirty = 1;
if (t == 7) a->depth = avc_reorder_depth(nal, n);
}
// walk the NAL units of p (Annex B start codes, or else 4-byte lengths); fn gets each unit without its prefix. Zero bytes before a start code (the first byte of a 4-byte
// start code, trailing_zero_8bits) are not part of the unit
// walk the NAL units of p, an Annex B byte stream; fn gets each unit without its start code. Zero bytes before a start code (leading_zero_8bits, the first byte of a
// 4-byte start code, trailing_zero_8bits) are not part of the unit. 0 = p does not begin with a start code, and fn saw nothing.
// The SDK's demuxer converts the MP4's length-prefixed samples to start codes, also when it hands over the avcC box (hle_movie.c), so packets and csd are Annex B. Length
// prefixes are not taken as a fallback: a 4-byte length from 256 to 511 begins 00 00 01, so a packet's framing cannot be told from its bytes (issue #629)
static size_t next_sc(const uint8_t *p, size_t n, size_t i) { for (; i + 3 <= n; i++) if (!p[i] && !p[i + 1] && p[i + 2] == 1) return i; return n; }
static void each_nal(const uint8_t *p, size_t n, void (*fn)(void *, const uint8_t *, size_t), void *ctx) {
static int each_nal(const uint8_t *p, size_t n, void (*fn)(void *, const uint8_t *, size_t), void *ctx) {
size_t s = next_sc(p, n, 0);
if (s < n && s <= 1) {
while (s < n) {
size_t b = s + 3, e = next_sc(p, n, b), end = e;
while (end > b && !p[end - 1]) end--;
if (end > b) fn(ctx, p + b, end - b);
s = e;
}
} else for (size_t i = 0; i + 4 <= n;) {
size_t len = (size_t)p[i] << 24 | (size_t)p[i + 1] << 16 | (size_t)p[i + 2] << 8 | p[i + 3]; i += 4;
if (!len || len > n - i) break;
fn(ctx, p + i, len); i += len;
if (s == n) return 0;
for (size_t i = 0; i < s; i++) if (p[i]) return 0;
while (s < n) {
size_t b = s + 3, e = next_sc(p, n, b), end = e;
while (end > b && !p[end - 1]) end--;
if (end > b) fn(ctx, p + b, end - b);
s = e;
}
return 1;
}
static void ps_only(void *ctx, const uint8_t *nal, size_t n) { int t = nal[0] & 31; if (t == 7 || t == 8) keep_ps(ctx, nal, n); }
void mavc_param_sets(mavc *a, const uint8_t *p, size_t n) { each_nal(p, n, ps_only, a); }
void mavc_param_sets(mavc *a, const uint8_t *p, size_t n) {
if (!each_nal(p, n, ps_only, a) && !a->red++) LOG("[RED] movie: AVC parameter sets without an Annex B start code (%zu bytes), not taken", n);
}
// AVCDecoderConfigurationRecord: version, profile, compatibility, level, NAL length size - 1, SPS count (low 5 bits) and 16-bit-length SPS, PPS count and 16-bit-length PPS
void mavc_avcc(mavc *a, const uint8_t *p, size_t n) {
if (n < 7 || p[0] != 1) { if (!a->red++) LOG("[RED] movie: unrecognized avcC record (%zu bytes, version %d)", n, n ? p[0] : -1); return; }
@@ -171,7 +172,8 @@ static void avc_picture_sequence(mavc *a, int idr) {
a->saw_picture = 1;
}
int mavc_decode(mavc *a, const uint8_t *p, int n, int64_t pts_us) {
au_ctx c = {a, 0}; each_nal(p, (size_t)n, au_add, &c);
au_ctx c = {a, 0};
if (n > 0 && !each_nal(p, (size_t)n, au_add, &c)) { if (!a->red++) LOG("[RED] movie: AVC packet without an Annex B start code (%d bytes), not decoded", n); return -1; }
if (!c.len) return 0;
if ((a->dirty || !a->vs) && !open_session(a)) return -1;
if (c.picture) avc_picture_sequence(a, c.idr);
+2 -2
View File
@@ -6,9 +6,9 @@
typedef struct mavc mavc;
mavc *mavc_new(void);
void mavc_free(mavc *a);
void mavc_param_sets(mavc *a, const uint8_t *p, size_t n); // SPS / PPS (Configure's csd-0 / csd-1), with or without Annex B start codes
void mavc_param_sets(mavc *a, const uint8_t *p, size_t n); // SPS / PPS (Configure's csd-0 / csd-1) with Annex B start codes
void mavc_avcc(mavc *a, const uint8_t *p, size_t n); // SPS / PPS from an AVCDecoderConfigurationRecord (the MP4 avcC box)
int mavc_decode(mavc *a, const uint8_t *p, int n, int64_t pts_us); // one access unit; 0 = decoded (or nothing to decode), -1 = error
int mavc_decode(mavc *a, const uint8_t *p, int n, int64_t pts_us); // one access unit with Annex B start codes; 0 = decoded (or nothing to decode), -1 = error (no start code)
// the next picture in presentation order, as NV12 (Y stride w, then interleaved UV at w*h, stride w; a smaller picture is padded with black, a larger one cropped):
// 1 = a picture was written (its time in *pts_us), 0 = none yet; with flush every queued picture comes out (end of stream)
int mavc_take(mavc *a, int flush, uint8_t *nv12, int w, int h, int64_t *pts_us);
+26 -7
View File
@@ -56,7 +56,7 @@ static void queue_picture(mavc *a) {
static OSStatus synthetic_decode(VTDecompressionSessionRef session, CMSampleBufferRef sample, VTDecodeFrameFlags flags,
void *source, VTDecodeInfoFlags *info) {
assert(!flags && !info); // the production callback must remain synchronous
uint8_t bytes[64]; CMBlockBufferRef block = CMSampleBufferGetDataBuffer(sample);
uint8_t bytes[64]; CMBlockBufferRef block = CMSampleBufferGetDataBuffer(sample); assert(block);
size_t n = CMBlockBufferGetDataLength(block);
assert(n <= sizeof bytes && !CMBlockBufferCopyDataBytes(block, 0, n, bytes));
int marker = -1;
@@ -88,20 +88,37 @@ static mavc *synthetic_avc(void) {
return a;
}
static void synthetic_free(mavc *a) { a->vs = NULL; mavc_free(a); }
static void submit_picture(mavc *a, int annex_b, int idr, int marker, int64_t pts) {
static void submit_picture(mavc *a, int short_sc, int idr, int marker, int64_t pts) { // start codes of 3 bytes, or of 4 (zero_byte first)
uint8_t au[12]; size_t n = 0;
for (int slice = 0; slice < (idr ? 2 : 1); slice++) {
if (annex_b) { au[n++] = 0; au[n++] = 0; au[n++] = 1; }
else { au[n++] = 0; au[n++] = 0; au[n++] = 0; au[n++] = 2; }
if (!short_sc) au[n++] = 0;
au[n++] = 0; au[n++] = 0; au[n++] = 1;
au[n++] = idr ? 0x65 : 0x41; au[n++] = (uint8_t)marker;
}
assert(!mavc_decode(a, au, (int)n, pts));
}
static void loop_order(int annex_b) {
// Packets and csd are Annex B (hle_movie_avc.c each_nal). Leading zero bytes are allowed before the first start code; a packet without a start code is refused whole,
// and NAL length prefixes are not read: 00 00 01 0d is a start code and a NAL header, not a 269-byte length (issue #629)
static void annex_b_only(void) {
mavc *a = synthetic_avc(); uint8_t out[16 * 16 * 3 / 2]; int64_t pts;
const uint8_t lead[] = {0, 0, 0, 0, 0, 1, 0x65, 50};
assert(!mavc_decode(a, lead, sizeof lead, 0) && mavc_queued(a) == 1);
const uint8_t lengths[] = {0, 0, 0, 2, 0x41, 51}, late[] = {0x41, 52, 0, 0, 1, 0x41, 53}, none[] = {0, 0, 0, 0};
assert(mavc_decode(a, lengths, sizeof lengths, 1000) == -1 && mavc_decode(a, late, sizeof late, 1000) == -1);
assert(mavc_decode(a, none, sizeof none, 1000) == -1 && mavc_decode(a, none, 0, 1000) == 0 && mavc_queued(a) == 1);
assert(mavc_take(a, 1, out, 16, 16, &pts) && out[0] == 50 && !mavc_queued(a));
synthetic_free(a);
a = mavc_new(); writer w; uint8_t ps[64] = {0, 0, 0, 1}; size_t n = sps(&w, 100, 2, 1, 0); memcpy(ps + 4, w.p, n);
mavc_param_sets(a, ps, n + 4); assert(a->nsps == (int)n && a->depth == 0);
size_t m = sps(&w, 100, 0, 1, 0); uint8_t len[64] = {0, 0, 0, (uint8_t)m}; memcpy(len + 4, w.p, m); // a length-prefixed SPS
a->dirty = 0; mavc_param_sets(a, len, m + 4); assert(!a->dirty && a->nsps == (int)n && a->depth == 0);
mavc_free(a);
}
static void loop_order(int short_sc) {
static const int decode[] = {0, 3, 1, 2, 6, 4, 5, 9, 7, 8, 11, 10};
mavc *a = synthetic_avc(); uint8_t out[16 * 16 * 3 / 2]; int64_t pts; int got = 0;
for (int loop = 0; loop < 3; loop++) for (int i = 0; i < 12; i++) {
submit_picture(a, annex_b, !i, 16 + loop * 12 + decode[i], decode[i] * 1000);
submit_picture(a, short_sc, !i, 16 + loop * 12 + decode[i], decode[i] * 1000);
// md_avc takes at most one picture for each input packet.
if (mavc_take(a, 0, out, 16, 16, &pts)) {
assert(pts == (got % 12) * 1000 && out[0] == 16 + got); got++;
@@ -183,7 +200,9 @@ static void tex_slots(void) {
}
int main(void) {
loop_order(1); loop_order(0);
puts("AVC loops: all 36 pictures ordered through packet parsing and callback refcons, with both NAL prefix formats");
puts("AVC loops: all 36 pictures ordered through packet parsing and callback refcons, with 3- and 4-byte start codes");
annex_b_only();
puts("AVC framing: leading zeros before the first start code are skipped; packets and parameter sets without a start code are refused, not read as NAL lengths");
sequence_boundaries();
puts("AVC boundaries: short sequences drain, non-IDR timestamps and SEI retain lookahead, Flush discards, zero-depth output stays immediate");
tex_slots();
+1
View File
@@ -393,6 +393,7 @@ One line per symptom in the [subsystem references](../reference/): what you see
- `[RED] movie: client texture format 0x9a is not RGBA8 / RGBX8 (sRGB included), writing in RGBA byte order`; a native-texture movie plays in the wrong colours, warm lamps blue. → [A BGRA8 client texture gets its movie with red and blue swapped](../reference/movies.md#a-bgra8-client-texture-gets-its-movie-with-red-and-blue-swapped)
- the game crashed on a null right after the failed open. → [`movie::Extractor` runs natively](../reference/movies.md#movieextractor-runs-natively)
- VP8 WebM movies play black; `movie::Decoder` is created with decoder type 2. → [VP8 uses the same libvpx worker and output paths](../reference/movies.md#vp8-uses-the-same-libvpx-worker-and-output-paths)
- an H.264 movie is black or frozen and the log has `[RED] movie: AVC packet without an Annex B start code (… bytes), not decoded` (or `AVC parameter sets without an Annex B start code`). → [H.264 packets are Annex B, not NAL lengths](../reference/movies.md#h264-packets-are-annex-b-not-nal-lengths)
- a movie with a Vorbis track starts clean, then crackles and stutters. The log has no underrun and no red line, and with `DM_AUDIO_DUMP` the game's mix has exact zeros in whole 512-sample blocks (mostly one or two at a time, 11 to 21 ms). → [Vorbis output lengths at a window change](../reference/movies.md#vorbis-output-lengths-at-a-window-change)
- a movie with a Vorbis track plays its first seconds with sound; after the game flushes and restarts it (a seek, a loop, or the app losing and regaining focus) the picture goes on but the sound does not come back, for the rest of the movie. The log shows `[movie] Flush`, `Flush`, `Start`, `Start` and no more `GetOutputBuffer … → PCM` lines. With `DM_AUDIO_DUMP`, the game's mix is real audio up to the flush and exact zeros from then on, so the AudioUnit is not at fault (0 underruns). → [Vorbis sound stops for good after a Flush](../reference/movies.md#vorbis-sound-stops-for-good-after-a-flush)
- on an iPad with an M1 (the one supported chip without I8MM), the app quits at the first AV1 movie (a title or opening movie): `[dm] CRASH sig=4 addr=put_8tap_neon_i8mm+0x… lr=dav1d_recon_b_inter_8bpc+0x…`, with a `dav1d_worker_task` thread beside it. The same build plays on an M2 or newer iPad, an A17 Pro or newer iPhone, and on the Mac. → [AV1 movies stop an M1 iPad with SIGILL in dav1d](../reference/movies.md#av1-movies-stop-an-m1-ipad-with-sigill-in-dav1d)
+7
View File
@@ -257,6 +257,13 @@ A decoder hands out as many outputs as it has slots, and while every slot is tak
- H.264 (type 1) into the client's NVN texture (output 3 = RGBA, mode 2), with AAC 5.1 sound (type 4, PCM16). VideoToolbox decodes to BGRA for that mode (`mavc_set_rgba`), and AAC goes through AudioToolbox's converter with an MPEG-4 ES descriptor built from the AudioSpecificConfig in `csd-0` (as FFmpeg's AudioToolbox decoder does). Found on [Monster Hunter Rise](../../dm/games/0100B04011742000/README.md); its end-of-stream packets and decoder deletes are in [End-of-stream packets with no data, and deleted decoders](#end-of-stream-packets-with-no-data-and-deleted-decoders).
### H.264 packets are Annex B, not NAL lengths
- **Symptom:** an H.264 movie is black or frozen and the log has `[RED] movie: AVC packet without an Annex B start code (… bytes), not decoded` (or `AVC parameter sets without an Annex B start code`).
- **Cause:** the SDK's demuxer converts the MP4's length-prefixed samples to Annex B start codes before they reach the decoder, also when it hands over the `avcc` record, and its `csd-0` / `csd-1` are Annex B too. `hle_movie_avc.c` nevertheless read packets without a leading start code as 4-byte NAL lengths, a fallback no game was seen to need. The two framings cannot be told apart from the bytes: a 4-byte length from 256 to 511 begins `00 00 01`, so such a NAL was taken as Annex B with its last length byte as the NAL header. [Issue #629](https://github.com/seedds/nxnative/issues/629) reported this ambiguity from another runtime's decoder (MGS3 clips with 269- and 407-byte NALs).
- **Fix:** `each_nal` reads only Annex B (3- or 4-byte start codes, leading zero bytes allowed); a packet or parameter set that does not begin with a start code is refused with the red line above, and `mavc_decode` returns -1. A game found to send NAL lengths would need its framing known for the stream, not guessed per packet. `t_movie_avc` runs its loops with both start-code lengths and checks the refusal.
- **Found on:** [Kirby Star Allies](../../dm/games/01007E3006DDA000/README.md) as the check. Its `ImageMovie.mp4` has 167 of 1,386 samples whose first NAL is 256 to 511 bytes (the first at decode index 2). Through real VideoToolbox, main's parser decoded 3 of its pictures from the length-prefixed samples and all 1,386 from Annex B, so the game's movie playing in full on main shows the SDK hands over Annex B. With the change, a boot from an empty save decoded that movie with "1387 packets in, 1387 frames out" and no red line (2026-11-09, Mac, on the chain its libraries were built with).
### Vorbis movie sound
- Movie sound in [The Legend of Zelda: Link's Awakening](../../dm/games/01006BB00C6F0000/README.md) (SDK 8.2.0): the decoder is asked for type 5 (Vorbis) with output format 1 (PCM16); see the other entries in this file and `hle_movie.c`.