feat(libSceAjm): parse CRC-protected and non-stereo MP3 metadata frames #8468

Merged
Gabrieljr42 merged 3 commits from upstream-pr/8468 into main 2026-10-22 20:14:29 +00:00
Gabrieljr42 commented 2026-10-20 13:10:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gabrieljr42 on 2026-10-20T13:10:11Z.


What

Implement missing metadata paths for sceAjmDecMp3ParseFrame:

  • Read protected-frame side information after the CRC bytes and calculate metadata offsets from that position.
  • Support Xing/Info and LAME tags at the calculated offset, with the standard fixed Xing offset retained for LAME compatibility.
  • Support VBRI headers at byte 36 for mono, MPEG-2 and MPEG-2.5 frames, and preserve bounded next-frame lookahead.
  • Preserve Layer I/II header-only parsing when parseOfl == 0; reject unsupported OFL requests before modifying output.
  • Restrict FGH validation to the first marker at or after the calculated metadata offset.

Tested

Linux, GCC 13, branch updated with upstream main at 3e91c8edf:

  • Built the actual guest_ajm_tests target with bundled dependencies; ctest --test-dir build-mp3 --no-tests=error -R '^guest_ajm$' --output-on-failure passed 1/1.
  • Exact production-parser and GuestAjm MP3 test functions passed AddressSanitizer/UBSan checks, including prefix-length bounds.
  • Regression coverage includes protected nonzero side information, fixed-offset LAME compatibility, Layer I/II header-only behavior, unchanged output on rejected requests, FGH marker ordering and bounded VBRI lookahead. Key regressions fail with the previous behavior.
  • python3 tools/check_conventions.py --base upstream/main and git diff --check passed.
  • The overlap check exited successfully but could not compare shared files with conflicting PRs #3205 and #6044; its warnings remain visible.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4936230868 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8468). Originally posted by [Gabrieljr42](https://github.com/Gabrieljr42) on 2026-10-20T13:10:11Z. --- ### What Implement missing metadata paths for `sceAjmDecMp3ParseFrame`: - Read protected-frame side information after the CRC bytes and calculate metadata offsets from that position. - Support Xing/Info and LAME tags at the calculated offset, with the standard fixed Xing offset retained for LAME compatibility. - Support VBRI headers at byte 36 for mono, MPEG-2 and MPEG-2.5 frames, and preserve bounded next-frame lookahead. - Preserve Layer I/II header-only parsing when `parseOfl == 0`; reject unsupported OFL requests before modifying output. - Restrict FGH validation to the first marker at or after the calculated metadata offset. ### Tested Linux, GCC 13, branch updated with upstream `main` at `3e91c8edf`: - Built the actual `guest_ajm_tests` target with bundled dependencies; `ctest --test-dir build-mp3 --no-tests=error -R '^guest_ajm$' --output-on-failure` passed 1/1. - Exact production-parser and GuestAjm MP3 test functions passed AddressSanitizer/UBSan checks, including prefix-length bounds. - Regression coverage includes protected nonzero side information, fixed-offset LAME compatibility, Layer I/II header-only behavior, unchanged output on rejected requests, FGH marker ordering and bounded VBRI lookahead. Key regressions fail with the previous behavior. - `python3 tools/check_conventions.py --base upstream/main` and `git diff --check` passed. - The overlap check exited successfully but could not compare shared files with conflicting PRs #3205 and #6044; its warnings remain visible. ### Checklist - [x] Based on current main; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation .md files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-20 13:10:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-20T13:10:48Z.


Progress report (47fa6ee → df0a74e)

📈 System libraries: 48.68% (+0.01%, +1 implemented functions)

✅ 1 implemented
Library Name
libSceAjm.native sceAjmDecMp3ParseFrame
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6291634591 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8468#issuecomment-6291634591). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-20T13:10:48Z. --- <!-- progress-report --> ### Progress report (47fa6ee → df0a74e) 📈 **System libraries**: 48.68% (+0.01%, +1 implemented functions) <details> <summary>✅ 1 implemented</summary> | Library | Name | | - | - | | libSceAjm.native | `sceAjmDecMp3ParseFrame` | </details>
oneandonlydean commented 2026-10-21 02:53:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-21T02:53:37Z.


@Gabrieljr42 thanks for this. Two changes, and one question:

  1. Protected frames: please fix the side-info offset instead of widening the searches. ParseMp3Ofl reads side info from byte 4 (stream[4 + bit / 8] in readBits). In a CRC-protected frame (protection bit 0), bytes 4-5 are the CRC and the side info starts at byte 6, so mainDataBits and at come from the wrong bytes. The extra candidates (at + 2, the fixed 38/23/15 Xing offsets, VBRI at at + 2/38, and the FGH scan from byte 4) compensate for that. Starting readBits at 4 + (protected ? 2 : 0) makes at correct, and the tag lookups can stay at at (plus the standard fixed offsets, if you want them for encoders that don't zero the side info).

  2. Layer I/II now return SCE_AJM_ERROR_INVALID_PARAMETER even with parseOfl == 0. On main those headers parse successfully when no OFL lookup is asked for. If the firmware rejects them, please cite where (the decompiled sceAjmDecMp3ParseFrame check or a console result). Otherwise, please keep main's parseOfl == 0 behaviour and only stop the OFL lookup outside Layer III.

  3. Question: the FGH loop now verifies the CRC-8 at every 0xB4 from byte 4 to the end of the frame, where main checked only the first one at or after at. Was scanning the side info and audio data intended? Each extra 0xB4 is another chance of a false FGH match.

The bounded VBRI lookahead and the untouched-output check for invalid layers look good.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6306847993 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8468#issuecomment-6306847993). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-21T02:53:37Z. --- @Gabrieljr42 thanks for this. Two changes, and one question: 1. **Protected frames: please fix the side-info offset instead of widening the searches.** `ParseMp3Ofl` reads side info from byte 4 (`stream[4 + bit / 8]` in `readBits`). In a CRC-protected frame (protection bit 0), bytes 4-5 are the CRC and the side info starts at byte 6, so `mainDataBits` and `at` come from the wrong bytes. The extra candidates (`at + 2`, the fixed 38/23/15 Xing offsets, VBRI at `at + 2`/38, and the FGH scan from byte 4) compensate for that. Starting `readBits` at `4 + (protected ? 2 : 0)` makes `at` correct, and the tag lookups can stay at `at` (plus the standard fixed offsets, if you want them for encoders that don't zero the side info). 2. **Layer I/II now return `SCE_AJM_ERROR_INVALID_PARAMETER` even with `parseOfl == 0`.** On main those headers parse successfully when no OFL lookup is asked for. If the firmware rejects them, please cite where (the decompiled `sceAjmDecMp3ParseFrame` check or a console result). Otherwise, please keep main's `parseOfl == 0` behaviour and only stop the OFL lookup outside Layer III. 3. Question: the FGH loop now verifies the CRC-8 at every `0xB4` from byte 4 to the end of the frame, where main checked only the first one at or after `at`. Was scanning the side info and audio data intended? Each extra `0xB4` is another chance of a false FGH match. The bounded VBRI lookahead and the untouched-output check for invalid layers look good.
xXPBSXx commented 2026-10-21 02:57:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T02:57:31Z.


Windows data point for 78bf409ec merged onto main 6696def6e (clean, git merge-tree rc 0): Windows 10.0.19045, MinGW-w64 GCC 15.2.0, Release. ctest -N -R '^guest_ajm$' lists exactly that one test; ctest --no-tests=error -R '^guest_ajm$' was run three times per tree.

  • With the PR: passes 3 of 3 (0.3-0.6 s).
  • Same tree with core/libs/prx/libSceAjm.native/src/Ajm.cpp restored to main (the new test cases kept): fails 3 of 3, exit code 0xc0000409 (the test process aborts).
    Not tested: other platforms, a title run.
    AI-assisted (Claude Code).
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6306899128 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8468#issuecomment-6306899128). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T02:57:31Z. --- Windows data point for `78bf409ec` merged onto `main` `6696def6e` (clean, `git merge-tree` rc 0): Windows 10.0.19045, MinGW-w64 GCC 15.2.0, Release. `ctest -N -R '^guest_ajm$'` lists exactly that one test; `ctest --no-tests=error -R '^guest_ajm$'` was run three times per tree. - With the PR: passes 3 of 3 (0.3-0.6 s). - Same tree with `core/libs/prx/libSceAjm.native/src/Ajm.cpp` restored to `main` (the new test cases kept): fails 3 of 3, exit code `0xc0000409` (the test process aborts). Not tested: other platforms, a title run. AI-assisted (Claude Code).
adriadam10 commented 2026-10-21 11:28:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-21T11:28:34Z.

Review: DISMISSED


Checked the diff against main and @oneandonlydean's review above; his three points are still open on this head (78bf409), so this is changes on the same items:

  • core/libs/prx/libSceAjm.native/src/Ajm.cpp:874-950: in a CRC-protected frame the side info starts at byte 6, not 4. Read it from 4 + (protected ? 2 : 0) so at is correct, then drop the compensating candidates (at + 2, the VBRI at + 2/38 probes, the FGH scan starting at byte 4). The fixed Xing offsets can stay only if you want them for encoders that zero the side info.
  • Ajm.cpp:972: layer == 1u in valid makes Layer I/II fail with SCE_AJM_ERROR_INVALID_PARAMETER also when parseOfl == 0, which main parses fine. Either cite where the firmware rejects them (decompiled check or a console result), or keep main's parseOfl == 0 behaviour and only reject the OFL lookup outside Layer III. The test at GuestAjm.cpp:394-402 and the debt line change with it.
  • Ajm.cpp:939-955: the FGH loop now checks the CRC-8 at every 0xB4 to the end of the frame, where main checked only the first at or after at. Every extra 0xB4 in side info or audio data is another chance of a false FGH match; please restrict it or say why it is intended.

The bounded VBRI lookahead, the prefix-length test and the untouched-output check for invalid layers look good. @xXPBSXx's Windows run confirms guest_ajm fails without the Ajm.cpp change.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5598960602 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8468#pullrequestreview-5598960602). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-21T11:28:34Z. **Review:** DISMISSED --- Checked the diff against main and @oneandonlydean's review above; his three points are still open on this head (`78bf409`), so this is `changes` on the same items: - `core/libs/prx/libSceAjm.native/src/Ajm.cpp:874-950`: in a CRC-protected frame the side info starts at byte 6, not 4. Read it from `4 + (protected ? 2 : 0)` so `at` is correct, then drop the compensating candidates (`at + 2`, the VBRI `at + 2`/38 probes, the FGH scan starting at byte 4). The fixed Xing offsets can stay only if you want them for encoders that zero the side info. - `Ajm.cpp:972`: `layer == 1u` in `valid` makes Layer I/II fail with `SCE_AJM_ERROR_INVALID_PARAMETER` also when `parseOfl == 0`, which main parses fine. Either cite where the firmware rejects them (decompiled check or a console result), or keep main's `parseOfl == 0` behaviour and only reject the OFL lookup outside Layer III. The test at `GuestAjm.cpp:394-402` and the debt line change with it. - `Ajm.cpp:939-955`: the FGH loop now checks the CRC-8 at every `0xB4` to the end of the frame, where main checked only the first at or after `at`. Every extra `0xB4` in side info or audio data is another chance of a false FGH match; please restrict it or say why it is intended. The bounded VBRI lookahead, the prefix-length test and the untouched-output check for invalid layers look good. @xXPBSXx's Windows run confirms `guest_ajm` fails without the `Ajm.cpp` change.
Gabrieljr42 commented 2026-10-21 11:49:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gabrieljr42 on 2026-10-21T11:49:19Z.


Thanks @adriadam10 and @oneandonlydean. Fixed in df0a74e7c, after updating the branch with main at 3e91c8edf.

  • Side-information reads, bounds and the calculated metadata offset now account for CRC bytes. The compensating at + 2 and VBRI byte-38 probes are removed.
  • I retained one standard fixed Xing/Info offset for LAME compatibility: LAME keeps its tag at that offset even when CRC is enabled. The calculated offset is checked first.
  • Layer I/II header-only parsing is accepted with parseOfl == 0. Unsupported OFL requests are rejected before modifying output; reserved layer values remain invalid. The tests and debt entry now reflect this.
  • On the FGH question: the broad scan was an overly permissive fallback. I don't have firmware evidence to justify scanning side information or retrying later markers after a bad CRC, so I removed it. Lookup now starts at the corrected at and checks only the first marker's CRC. A regression with a bad first CRC followed by a valid marker detects scanning past that first marker. The bounded VBRI lookahead stays in place.

Validated on Linux with GCC 13:

  • Built the actual guest_ajm_tests target from bundled dependencies; ctest --test-dir build-mp3 --no-tests=error -R '^guest_ajm$' --output-on-failure passed 1/1.
  • The exact MP3 parser and TestMp3ParseFrame, TestMp3ParseOfl, and TestMp3MetadataBounds passed AddressSanitizer/UBSan checks.
  • The protected-frame regression with nonzero side information fails with the previous parser, and restoring only the old FGH scan makes the first-marker regression fail.
  • Conventions and git diff --check passed. The overlap tool still reports the existing shared-file warnings for #3205 and #6044.

AI-assisted with Codex.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6315173767 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8468#issuecomment-6315173767). Originally posted by [Gabrieljr42](https://github.com/Gabrieljr42) on 2026-10-21T11:49:19Z. --- Thanks @adriadam10 and @oneandonlydean. Fixed in df0a74e7c, after updating the branch with `main` at `3e91c8edf`. - Side-information reads, bounds and the calculated metadata offset now account for CRC bytes. The compensating `at + 2` and VBRI byte-38 probes are removed. - I retained one standard fixed Xing/Info offset for LAME compatibility: [LAME keeps its tag at that offset even when CRC is enabled](https://github.com/lameproject/lame/blob/master/libmp3lame/VbrTag.c#L854-L862). The calculated offset is checked first. - Layer I/II header-only parsing is accepted with `parseOfl == 0`. Unsupported OFL requests are rejected before modifying output; reserved layer values remain invalid. The tests and debt entry now reflect this. - On the FGH question: the broad scan was an overly permissive fallback. I don't have firmware evidence to justify scanning side information or retrying later markers after a bad CRC, so I removed it. Lookup now starts at the corrected `at` and checks only the first marker's CRC. A regression with a bad first CRC followed by a valid marker detects scanning past that first marker. The bounded VBRI lookahead stays in place. Validated on Linux with GCC 13: - Built the actual `guest_ajm_tests` target from bundled dependencies; `ctest --test-dir build-mp3 --no-tests=error -R '^guest_ajm$' --output-on-failure` passed 1/1. - The exact MP3 parser and `TestMp3ParseFrame`, `TestMp3ParseOfl`, and `TestMp3MetadataBounds` passed AddressSanitizer/UBSan checks. - The protected-frame regression with nonzero side information fails with the previous parser, and restoring only the old FGH scan makes the first-marker regression fail. - Conventions and `git diff --check` passed. The overlap tool still reports the existing shared-file warnings for #3205 and #6044. AI-assisted with Codex.
ntamas94 commented 2026-10-22 13:35:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ntamas94 on 2026-10-22T13:35:14Z.


#9637 (PS4 batch buffers through libSceAjm) adds entries to docs/dev/debt/libSceAjm.native.md next to the sceAjmDecMp3ParseFrame lines this PR rewrites, so the file will conflict on one hunk. This one is older, so I propose it lands first; I rebase #9637 the same day and list it under Depends on there. The code does not overlap.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6342094076 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8468#issuecomment-6342094076). Originally posted by [ntamas94](https://github.com/ntamas94) on 2026-10-22T13:35:14Z. --- #9637 (PS4 batch buffers through libSceAjm) adds entries to `docs/dev/debt/libSceAjm.native.md` next to the `sceAjmDecMp3ParseFrame` lines this PR rewrites, so the file will conflict on one hunk. This one is older, so I propose it lands first; I rebase #9637 the same day and list it under Depends on there. The code does not overlap. AI-assisted: yes (Claude Code)
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!8468
No description provided.