Files
AnyPS5/docs/dev/debt/libSceAjm.native.md
T
Adrià 142f7555aa Merge pull request #6051 from Ondra97/codex/fix-ajm-live-instance-handles
fix(ajm): preserve live decoder handles across ID wrap
2026-10-17 06:10:21 +02:00

11 KiB

libSceAjm.native technical debt

Unknown function info

  • sceAjmInstanceCreate (libSceAjm.native) - a full 14-bit instance slot namespace returns 0x80930007 (OUT_OF_RESOURCES), as shadPS4's InstanceCreate does for its own instance pool (PS4). The 16384 slots per codec prefix are this runtime's existing ID layout, not a measured console capacity; the PS5 capacity and exhaustion result are unverified.
  • sceAjmDecWVorbisCreateHeaderPacket, sceAjmDecWVorbisCreateSetupPacket (libSceAjmWVorbis, implemented in libSceAjm.native) - unknown signatures
  • sceAjmBatchJobGetInfo (libSceAjm.native) - writes the result and format sideband of a run job without buffers with the SIDEBAND_FORMAT flag (0x400000000000), as KytyPS5 does, not confirmed on a PS5 title; the format reports bitrate 0 for ATRAC9 as in KytyPS5, while shadPS4 derives it from the superframe size; before the instance is initialized it returns the not-initialized result (1) and no format, like the other run jobs here, while KytyPS5 returns result 0 with the format of the uninitialized decoder
  • sceAjmDecMp3ParseFrame (libSceAjm.native) - signature, result layout and rate tables taken from the shadPS4 reimplementation of the PS4 library, not confirmed on a PS5 title: the layer field is not checked and MPEG-2.5 stops at 64 kbps; the original file length (parse_ofl) layout (Xing/Info + LAME tag, VBRI, Fraunhofer 0xB4 block with its CRC, encoder delay = samples per frame + LAME delay + 529) also comes from shadPS4 only, and the Fraunhofer block has no other public description
  • sceAjmBatchJobGetCodecInfo, sceAjmBatchJobGetGaplessDecode (libSceAjm.native) - the RUN_GET_CODEC_INFO flag (0x800), the ATRAC9 codec info layout and its next frame size are taken from the shadPS4 and RPCSX reimplementations, not confirmed on a PS5 title; a run job without buffers returns result 0, as in shadPS4. The gapless sideband reports the total and skip counts the title set and does not count them down: RPCSX does the same, shadPS4 counts them down and BryKytyPS5 returns its current counts
  • AJM Opus decoder (libSceAjm.native, codec 24) - the initialize parameter layout and little-endian u16 packet byte count come from The Smurfs Dreams (2, 48000, 0) and Lost Soul Aside (4, 48000, 1). The third word is interpreted as the mapping family, as in KytyPS5's initialize structure. Family 1 uses the conventional stream counts, coupled counts and channel map from libopus 1.5.2's surround layout, passed to FFmpeg through an RFC 7845 section 5.1 header with zero pre-skip and gain; gapless skipping remains controlled by AJM sideband parameters. FFmpeg returns PCM in its native speaker order. The implicit stream layout, PCM channel order and invalid family-0 channel count result are not verified on a console. NHL 27 (PPSA34063) passes 1, 48000, 255. Family 255 with one channel is decoded as one mono stream, the same as family 0; this is assumed, since the stream count, coupled stream count and channel mapping that a family-255 Opus header carries (RFC 7845 section 5.1.1) are not passed. Only 48000 Hz, mapping families 0 and 1, and family 255 with one channel are implemented; other rates and families, and family 255 with more channels, throw
  • AJM decoders other than MP3, ATRAC9, M4AAC and Opus (libSceAjm.native) - decoding throws for every other codec id. Broken Sword - Shadow of the Templars: Reforged (PPSA21480) stops at codec 23 (AJM: decoding codec 23 is not implemented). The ids known here are MP3 0, ATRAC9 1, M4AAC 2 and Opus 24; id 23 is not among them and which codec it is, is unknown.
  • sceAjmBatchJobSetResampleParametersEx (libSceAjm.native) - the (info, instance, float ratio_start, float ratio_change_per_sample, flags, result) signature is inferred from the name and from sceAjmBatchJobSetResampleParameters; with a zero change per sample it sets the ratio as sceAjmBatchJobSetResampleParameters does (Watch Dogs: Legion, PPSA01500, calls it so). How a non-zero change ramps the ratio is not confirmed on a console (no source: KytyPS5 ignores it, prosper stubs it, the PS5 firmware does not export the batch builders to a payload): it is assumed to be added to the ratio after each emitted output frame, clamped at zero, and kept across batches. RIDE 4 (PPSA01599) passes one call with a change of 4.6e-7, so the exact rule matters little there.
  • sceAjmBatchJobControl (libSceAjm.native) - RESET (bit 13), INITIALIZE (bit 14) and the sideband input order (gapless decode, then initialize parameters) come from shadPS4 (PS4); other flags throw
  • sceAjmBatchJobControl (libSceAjm.native) - RESET is assumed to clear the context as sceAjmBatchJobClearContext does
  • sceAjmBatchJobControl (libSceAjm.native) - initialize parameters are 8 bytes for ATRAC9 and M4AAC, 12 for Opus and none for MP3, as one title passes them; other codecs and sizes, and an output other than the 8-byte result, throw
  • sceAjmBatchJobControl (libSceAjm.native) - SIDEBAND_GAPLESS_DECODE with RESET overwrites both counts as in RPCSX; shadPS4 ignores a zero count when the other is nonzero. Without RESET they differ, so it throws
  • sceAjmBatchJobSetResampleParameters, sceAjmBatchJobGetResampleInfo (libSceAjm) - the ratio is taken as decoded samples per output sample and num_samples as the decoded samples the resampler holds, both inferred from FMOD (Hades II); the console's interpolation is unknown, a 4-point Catmull-Rom is used. The flags argument is ignored, as in shadPS4 and Kyty

Functional

  • sceAjmDecMp3ParseFrame (libSceAjm.native) throws when the original file length is requested (parse_ofl non-zero) for a layer other than III, for a CRC-protected frame, or for a VBRI header outside MPEG-1 stereo: shadPS4 looks for the tag after the CRC and after the side information, while LAME and FFmpeg place it without the CRC and VBRI always at byte 36.
  • ATRAC9 gapless segments (libSceAjm.native) - a run job stops after the gapless total of output samples counted from the start of the segment, leaving the rest of its input unconsumed, and the next run starts the next segment with the decoder as it is and without a second skip. FMOD in PPSA29141 sets a total shorter than a sound's frames and reads the frames after it on later runs; with the output held at the total its stream reader waited for them forever and the last buffer repeated. The console's handling of the frames after the total is not documented; a reset (SetGaplessDecode with the reset flag, ClearContext, initialization) starts a new segment. An ATRAC9 run job without RUN_MULTIPLE_FRAMES decodes its whole input as one with it: FMOD in PPSA29141 puts two run jobs in one batch with the second's input right after the whole of the first's. Whether the console stops after one frame there is not verified; MP3 and Opus run jobs still stop after one frame
  • AJM M4AAC decoder (libSceAjm.native, codec 2) - Ghost of Yotei (PPSA26344) initializes it through sceAjmBatchJobControl. The initialize parameters (u32 config type, 1 ADTS or 2 raw, then u32 sampling frequency index), instance flags (channels in bits 3-6, ENABLE_SBR_DECODE bit 32, ENABLE_NONDELAY_OUTPUT bit 33, surround interleave order bits 34-35), {heaac, reserved} codec info and decoder error result (CODEC_ERROR | FATAL) follow shadPS4 (PS4, fdk-aac) and are unverified on PS5; decoding uses FFmpeg's AAC decoder. Without ENABLE_NONDELAY_OUTPUT, the first two frames after initialization or reset are consumed without output. Raw input takes its channel configuration from the instance (0 means stereo) and is decoded as one access unit per run job. The format reports the last frame's bitrate. Invalid config type or raw sampling frequency index above 11 returns invalid-parameter result 4. The implementation throws for ADTS without a frame header, profiles other than AAC LC, multiple raw data blocks per ADTS frame, unsupported channel configurations, more channels than the instance supports, HE-AAC without ENABLE_SBR_DECODE, raw input with RUN_MULTIPLE_FRAMES, output room smaller than the previous frame length, and unknown codec flags.
  • AJM, APR and NGS2 state is allocated with new and never freed on purpose: title threads still call into these libraries while std::exit runs static destructors, so ordinary statics would be destroyed under them. Keep them leaked; guest_ajm_teardown covers the AJM case on Unix.
  • AJM Opus mapping family 255 with several channels (libSceAjm.native): the initialization words carry no stream count or channel mapping, so the decoder opens on the first packet. It counts the self-delimited streams of that multistream packet (RFC 6716 appendix B) and takes each stream's stereo flag from its TOC byte as coupled, then decodes with an identity channel mapping. A packet whose streams do not add up to the channel count throws. The layout is inferred, not read from the console; unverified on hardware.