Skip to content

fix(libSceVdecsw): report idrPictureFlag only for an H.264 IDR access unit - #21194

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
roboterspinne:fix/libscevdecsw-avc-idr-picture-flag
Nov 16, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
roboterspinne:fix/libscevdecsw-avc-idr-picture-flag

Conversation

@roboterspinne

Copy link
Copy Markdown
Contributor

What

On main d4a1472 pictureInfo (core/libs/prx/libSceVdecsw/Export.cpp:122-175), behind sceVdecswGetAvcPictureInfo and sceVdecswGetPictureInfo, fills AvcPictureInfo.idrPictureFlag from header.keyFrame (:163). That field is Picture.idr (core/libs/prx/libSceVideodec2/Export.cpp:820), which EmitDecoded sets from FFmpeg's AV_FRAME_FLAG_KEY on the output frame (:470). FFmpeg sets that flag on more than IDR pictures: the pinned 3rdparty/ffmpeg-core (9ac4cfd) builds the vcpkg FFmpeg port at df8bfe51, version 7.1.1, and in FFmpeg n7.1.1 libavcodec/h264dec.c:896-897 adds AV_FRAME_FLAG_KEY to the output frame of any picture whose access unit carries a recovery point SEI with recovery_frame_cnt 0 (h264_slice.c:510 copies the count from the access unit's SEI), while h264_slice.c:1651 sets it on the picture itself only for an IDR slice. A P, B or open-GOP I picture with such an SEI is therefore reported as IDR (counter-check under Tested). The field is H.264's IdrPicFlag, which is 1 exactly when nal_unit_type is 5 (ITU-T H.264 (08/2024), 7.4.1, equation 7-1). The repository already uses that definition twice: Videodec2's own AVC picture info sets info.idrPictureFlag = first->idr (core/libs/prx/libSceVideodec2/AvcMetadata.cpp:341, with idr = type == 5 at :314, #12633), and for HEVC #7517 (3aee4b3) separated the NAL-based hevcIdr (StartsHevcIdr, Export.cpp:378-386, set at :652) from FFmpeg's key flag, which Vdecsw reports as irapPictureFlag (Vdecsw/Export.cpp:143-144). AVC was the only path left on the key flag. This is consistency with the standard and the sibling code, not a measurement of console output.

  • Rule. StartsAvcIdr, next to StartsHevcIdr and built the same way, scans the Annex B access unit for start codes; the first NAL unit of type 1 to 5 (a coded slice or slice data partition, the VCL types of Table 7-1, as CarriesAvcSlice at :368-376 counts them) decides, and the result is type == 5. sceVideodec2Decode_nid_postfix stores it as tagged.avcIdr for an AVC decoder next to tagged.hevcIdr, PictureHeader (core/libs/prx/libSceVideodec2/include/PictureMetadata.hpp:15-25) gains bool avcIdr as its last member, Videodec2PictureHeader_nid_no_patch passes it last in the positional initializer, and Vdecsw's AVC branch reports header.avcIdr. keyFrame, Export.cpp:470 and the HEVC idrPictureFlag and irapPictureFlag are unchanged, and so is Videodec2's own AVC picture info. The NAL scan is used instead of stored.avc.info.idrPictureFlag because Videodec2Avc::Parse_nid_no_patch catches slice-header errors (AvcMetadata.cpp:326-329) and can leave that info unset for a unit that has a slice; the scan is defined for every unit and mirrors the HEVC precedent.
  • Behaviour for valid streams. An IDR access unit still reports 1. A non-IDR access unit reports 0 whether or not FFmpeg flags its picture as key; with the FFmpeg lines cited above, the pictures whose result changes are the non-IDR ones with a recovery point SEI whose recovery_frame_cnt is 0. A conforming access unit cannot mix IDR and non-IDR slices (7.4.1, semantics of nal_unit_type), so the first VCL NAL unit stands for the whole picture. The other readers of PictureHeader, libSceVideodec (core/libs/prx/libSceVideodec/Export.cpp:231, :300, read by Measure at :82) and GuestVideodec2.cpp:318, value-initialize it with {} and read named fields, so the appended member changes nothing for them.
  • Debt. No fragment. docs/dev/debt/libSceVdecsw.md says only that the AVC picture info carries Videodec2's leading fields and the cropping fields, which stays true, and no entry under docs/dev/debt/ mentions idrPictureFlag or keyFrame for Vdecsw.
  • Tests. core/libs/tests/GuestVdecsw.cpp (ctest guest_vdecsw; no other test name starts with it, no CMake change). testDecode now takes the access units, and its idrPictureFlag check moved out of the display-order check into its own check, info.idrPictureFlag == (unit == 0 ? 1 : 0) with unit = DisplayOrderUnits[pictures], which for the plain fixture is the old pictures == 0; it has its own line so that a failure names idrPictureFlag instead of the display order. The fixture H264Fixture::Stream has six access units: unit 0 holds an AUD, SPS, PPS, an x264 user-data SEI and an IDR slice, and units 1 to 5 an AUD and one non-IDR slice (type 1) each; it has no recovery point SEI, so no non-IDR picture is flagged as key by FFmpeg. The new testDecodeRecoveryPoint checks that unit 1 is an AUD followed by a type 1 slice (a reference P picture, nal_ref_idc 2, displayed at index 3), inserts the recovery point SEI 00 00 00 01 06 06 01 84 80 (H.264 D.1.8: recovery_frame_cnt 0, exact_match_flag 0, broken_link_flag 0, changing_slice_group_idc 0) after its 6-byte AUD and runs testDecode on the result. Every picture must still match PictureHashes, and checkGenericPictureInfo compares the sceVdecswGetPictureInfo bytes with the sceVdecswGetAvcPictureInfo result, so both exports are checked. The unchanged HEVC check (info.idrPictureFlag and info.irapPictureFlag at picture 0 only) passes.
  • Linux. The test decodes with the same pinned FFmpeg 7.1.1 on every host, and the key flag on the recovery point picture comes from FFmpeg's H.264 decoder (h264dec.c:896-897), not from anything host-specific. guest_vdecsw is registered under if(BUILD_TESTING) with no host condition (core/libs/prx/libSceVdecsw/CMakeLists.txt:10-15). Without the change Vdecsw reports that flag, so the new case fails on Linux at display index 3 as it does here. Nothing was run on Linux here.
  • Overlap. pr_overlap.py --names for sceVdecswGetAvcPictureInfo and sceVdecswGetPictureInfo finds no open pull request that implements them, and pr_overlap.py --branch reports no shared lines or exports with any open pull request (main d4a1472). The only open pull requests that change these files, fix(libSceVideodec2): safely copy guest decode and flush buffers #20614 (PrivacyOnion) and fix(videodec2): validate ClearOutput destinations upfront before mutating output #21081 (Kikoarl), both in core/libs/prx/libSceVideodec2/Export.cpp, do not mention idrPictureFlag, keyFrame, avcIdr or PictureHeader.

Tested

Windows 11, WinLibs GCC 15.2.0, Ninja, Release, BUILD_TESTING=ON, build directory configured through its 8.3 short path, on d4a1472. Built with cmake --build <build> --parallel 4 --target guest_vdecsw_tests guest_videodec_tests guest_hevc_metadata_tests guest_videodec2_tests guest_videodec2_hevc_aliases_tests guest_videodec2_unimplemented_tests; the counter-check and the negative control were built with --target guest_vdecsw_tests only. Every build exited 0. Before every run each core/libs/libs/unpatched/*.prx that also exists in <build>/tests (libSceVdecsw.prx, libSceVideodec.prx once built, libSceVideodec2.prx, libc.prx, libkernel.prx) was copied there with cmake -E copy_if_different, and the md5sum of each copy matched its source each time.

  • Counter-check on unchanged code (the three source files of d4a1472, new test in place): ctest --test-dir <build> -R '^guest_vdecsw$' --output-on-failure fails with picture 3 IDR flag differs from its nal_unit_type. The plain fixture run before it passes, and pictures 0 to 2 of the recovery point run, hashes included, pass before the failure. This confirms at run time that FFmpeg flags the recovery point picture as key.
  • With the change: ctest --test-dir <build> -R '^guest_vdecsw$' --output-on-failure passes. ctest --test-dir <build> -R '^(guest_vdecsw|guest_videodec|guest_videodec2|guest_videodec2_hevc_aliases|guest_videodec2_unimplemented|guest_hevc_metadata)$' --output-on-failure runs the 6 tests of libSceVdecsw, libSceVideodec and libSceVideodec2 (--show-only=json-v1 lists no other): 6/6 passed.
  • Negative control at the end: with only the three source files reverted to d4a1472 (test kept) and guest_vdecsw_tests rebuilt, -R '^guest_vdecsw$' failed again with picture 3 IDR flag differs from its nal_unit_type. With the change restored (the source diff byte-identical to the saved patch) and all six targets rebuilt, the same 6-test command passed 6/6.
  • Not run here: Linux and macOS (see the Linux bullet above).
  • python tools/check_conventions.py --base upstream/main --head HEAD with this title and description passed.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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 (Claude Code)

… unit

The AVC picture info took idrPictureFlag from FFmpeg's AV_FRAME_FLAG_KEY,
which FFmpeg also sets on a non-IDR picture whose access unit carries a
recovery point SEI with recovery_frame_cnt 0. Videodec2 now records, as
it already does for HEVC, whether the first VCL NAL unit of the access
unit has nal_unit_type 5 (H.264 IdrPicFlag), and Vdecsw reports that.
The HEVC flags and keyFrame are unchanged.
@github-actions github-actions Bot added the prx label Nov 16, 2026

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 7bd610087c:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_vdecsw_tests built cleanly.
  • CTest (guest_vdecsw):
    1/1 Test #4788: guest_vdecsw .....................   Passed    0.05 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceVdecsw/Export.cpp, core/libs/prx/libSceVideodec2/Export.cpp, core/libs/prx/libSceVideodec2/include/PictureMetadata.hpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #4788: guest_vdecsw .....................***Failed    0.07 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified StartsAvcIdr in core/libs/prx/libSceVideodec2/Export.cpp:389-397 safely checks index + 3 < unit.size() before indexing unit[index .. index + 3], mirrors StartsHevcIdr, and inspects the first VCL NAL unit (nal_unit_type 1..5) for type == 5 (IDR).
    • Verified Picture.avcIdr in core/libs/prx/libSceVideodec2/Export.cpp:220,664,832 and PictureHeader.avcIdr in core/libs/prx/libSceVideodec2/include/PictureMetadata.hpp:25 are initialized and propagated through Videodec2PictureHeader_nid_no_patch to pictureInfo in core/libs/prx/libSceVdecsw/Export.cpp:163.
    • Verified testDecode and testDecodeRecoveryPoint in core/libs/tests/GuestVdecsw.cpp:228-295 insert a recovery point SEI (recovery_frame_cnt == 0) into a non-IDR access unit and verify info.idrPictureFlag matches unit == 0 ? 1 : 0 across display-reordered frames.
    • Verified LP64/LLP64 portability (std::size_t, std::uint8_t, bool), lock scopes in sceVideodec2Decode_nid_postfix and Videodec2PictureHeader_nid_no_patch, and compliance with AGENTS.md.

Looks Good To Me

@oneandonlydean
oneandonlydean merged commit 5d29af6 into boykopovar:main Nov 16, 2026
14 checks passed
@roboterspinne
roboterspinne deleted the fix/libscevdecsw-avc-idr-picture-flag branch November 16, 2026 10:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants