Skip to content

media: apple: avd: validate H.264 slice starts and AV1 tile layouts against the picture - #3197

Merged
RyanTheTide merged 4 commits into
mainfrom
fix/avd-slice-tile-bounds
Nov 29, 2026
Merged

RyanTheTide merged 4 commits into
mainfrom
fix/avd-slice-tile-bounds

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What: Two decoder geometry checks.

  • H.264: first_mb_in_slice became the programmed macroblock position unchecked, so a slice continuing a held picture could start far outside a small picture. It must now be below PicSizeInMbs (H.264 7.4.3), and later slices of a held picture must keep that picture's size.
  • AV1: the per-tile starts and sizes became the programmed tile start and end without being tied to the frame. The tiles must now cover the frame's superblock grid the way tile_info() lays it out (AV1 5.9.15).

Both checks refuse the request with -EINVAL before anything is programmed.

Tested: these pass: the host fixtures program-test.py (production avd_h264_run()), av1-test.py and avd-av1-tail-tests.py (both new mutations are killed), and the AVD KUnit suites on arm64 QEMU (219/219). J700 (Neo) at b779e80bab82, in the combined candidate 7.3.0-neo-combined1126, which carries it unchanged: AV1 3/3 and single-slice H.264 2/2 streams decode exactly as on base (180/180 frames each). Every malformed H.264 continuation and AV1 tile layout is refused by its own check, on a fresh stream per case (4 and 5 refusal lines). 0 DART faults, 0 decoder timeouts. J714s (M5 Pro) at b779e80bab82, in the combined candidate 7.3.0-m5-rows-sec2, which carries it unchanged: H.264 (120 frames) and AV1 (60 frames) decode exactly as on base. 0 DART faults.

Risk: two-way · all Macs with AVD H.264 or AV1 decode. A conformant stream passes both checks.

Not yet: the M5 Pro's malformed-input re-run with a fresh stream per case; its decode rows already match base.

Details

H.264 (first commit):

  • avd_h264_pic_size() gives PicWidthInMbs and PicSizeInMbs in u64, as 7.4.2.1.1 and 7.4.3 define them, including field and MBAFF pictures.
  • avd_h264_run() refuses a slice with first_mb_in_slice * (1 + MbaffFrameFlag) >= PicSizeInMbs.
  • A held picture's first slice records the picture size. A later slice whose SPS gives another size is refused, and, as with the other refused slices, the held picture is dropped.

AV1 (second commit):

  • avd_av1_check_tile_info() runs in the run preamble, after the frame size check and before the tile group is read.
  • Per dimension it requires at least one tile and no more than tile_info holds. Each tile must start at the superblocks of the tiles before it (<< sbShift, in MI units), and the sizes must add up to the frame's superblocks. Those come from MiCols/MiRows of the frame size (AV1 5.9.9) and the sequence's 64 or 128 superblock size.
  • Sizes are bounded before they are added, so a huge size cannot wrap into place.
  • context_update_tile_id must name one of the frame's tiles (AV1 6.8.14).
  • The fixture in avd-av1-tail-tests.py now gives its frames one tile covering them, and checks the preamble's refusals. Its two new mutations are registered in run-host-tests.py.
Evidence
  • python3 drivers/media/platform/apple/avd/tests/program-test.py OUT: PASS. Against the unfixed avd-h264.c, the new case fails at the continuation slice at 8160.
  • tools/asahi/run-host-tests.py --only avd: 14/14, including --mutation tile-info-unchecked and --mutation tile-end-unchecked killed.
  • python3 drivers/media/platform/apple/avd/tests/av1-test.py OUT: PASS.
  • KUnit: ./tools/testing/kunit/kunit.py run --arch=arm64 --make_options LLVM=1 --kunitconfig=drivers/media/platform/apple/avd/tests/.kunitconfig. New: avd_slice_start_test (17 cases), avd_slice_pic_size_test, avd_av1_tile_info_test (23 cases).

A slice's first_mb_in_slice is programmed as the decoder's macroblock
column and row, first_mb_in_slice % PicWidthInMbs and
first_mb_in_slice / PicWidthInMbs. Neither the control core nor the
driver bounded it: a slice continuing a held picture could start far
below a small picture, so the decoder was told a position outside the
picture its output and scratch buffers were sized for.

H.264 7.4.3 requires first_mb_in_slice * (1 + MbaffFrameFlag) to be less
than PicSizeInMbs. Check that in u64 when the slice runs, against the
SPS and field flag of the request, and refuse the request with -EINVAL
otherwise. The first slice of a held picture also records the picture
size, and a later slice of that picture must have the same size, so an
SPS change in the middle of a picture cannot move its slices out of it.
As for the other refused slices, the held picture is dropped.

Add KUnit cases for slices at and past PicSizeInMbs (16 and 16383 on a
64x64 picture, U32_MAX, a picture of 2^32 macroblocks), field and MBAFF
pictures, and held pictures of another size, and cases to the production
H.264 run fixture in tests/program-test.py: continuation slices at 8160,
16383 and U32_MAX on 1920x1088 and one with another SPS width are
refused and drop only the held picture, and one at 8159 is taken.

Signed-off-by: Dj <[email protected]>
set_tiles() programs every tile's start and end superblock from the
frame's tile_info: mi_col_starts[] and mi_row_starts[] for the start and
width_in_sbs_minus_1[] and height_in_sbs_minus_1[] for the size. The work
buffers are sized from the same sizes. Only the tile counts and the
tile-group indices were checked, so a small frame could carry a tile of
4096x4096 superblocks, and the decoder was told to decode far outside
the frame its buffers were sized for.

Check the layout the way tile_info() builds it (AV1 5.9.15): per
dimension at least one tile and no more than tile_info holds, each tile
starting at the superblocks of the tiles before it, and the tile sizes
adding up to the frame's superblocks, from MiCols and MiRows of the frame
size (AV1 5.9.9) and the sequence's superblock size. The sums are bounded
before they are made, so a huge size cannot wrap into place. The CDF
update tile must be one of the frame's tiles (AV1 6.8.14). A request
that fails is refused with -EINVAL before any tile is programmed.

Add KUnit cases for one tile and 2x2 uniform tiles at 1080p, partial
superblocks at 66x66 and 128x128 superblocks, and for the 4096x4096
superblock tile on a 64x64 frame, starts and sizes one off, a size that
wraps into the next column, no tiles, more tiles than tile_info holds and
a CDF tile past the tiles. The production preamble fixture in
avd-av1-tail-tests.py now gives its frames one tile covering them, checks
that the preamble refuses bad layouts, and runs two new mutations.

Signed-off-by: Dj <[email protected]>
@DjDeveloperr
DjDeveloperr requested review from a team as code owners November 26, 2026 06:53
@github-actions github-actions Bot added area:gpu Area from CODEOWNERS (bot) area:media Area from CODEOWNERS (bot) blast:all Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) needs-hw-row No hardware row at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Nov 26, 2026
@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Merge readiness at 0e6c8b04: not ready yet

Check Detail
CI at head running running: KUnit asahi, KUnit spi-hid-apple, KUnit avd, KUnit ave, KUnit isp, KUnit jpeg
Review no needs one approving review from Core or Omarchy Mac (not the author)
Requested changes ok none
Hardware row at head no no line with the head SHA 0e6c8b04, a Mac and a result in the PR body or a comment
Conflicts ok none
PR text ok Risk: two-way · all Macs with AVD H.264 or AV1 decode. A conformant stream passes both checks.

Areas: area:gpu, area:media

Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels.

@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

Verdict at b779e80bab82: ready

Blockers: none

Checked:

  • H.264 slice start.
    • avd_h264_pic_size() computes PicWidthInMbs and PicSizeInMbs in u64 as 7.4.2.1.1 and 7.4.3 define them: frame height from frame_mbs_only_flag, divided by 1 + field_pic_flag.
    • The bound is first_mb_in_slice << MbaffFrameFlag < PicSizeInMbs, with MBAFF counted only for frame pictures. It runs before a bank is picked or anything is pushed.
    • The held picture's size is recorded only from a new frame's first slice. A continuation with another SPS size is refused through the same error path that drops the held picture. The fixture shows that bank 0's submitted picture is untouched.
  • AV1 tiles.
    • avd_av1_check_tile_dim() follows tile_info(): at least one tile and no more than the uapi arrays hold. Each mi_*_starts[i] must equal the running superblock start << sbShift, and each size must fit in the superblocks left before it is added, so the sum cannot wrap. The sizes must cover exactly the (Mi + sbMask) >> sbShift superblocks.
    • MiCols and MiRows come from frame_width_minus_1 and frame_height_minus_1 as in 5.9.9, and the superblock size from the sequence's 128x128 flag.
    • set_tiles() and the work-buffer sizing read only mi_*_starts[i] and *_in_sbs_minus_1[i] for i below the tile counts, which this check covers.
    • context_update_tile_id must be below TileCols * TileRows, as 6.8.14 requires.
  • Conformant streams: spec-derived uniform layouts (last tile sb - (n-1) * tileSb) add up to exactly the rounded-up superblock count, so they pass. The decode rows on the Neo and the M5 Pro listed under "Not yet" will confirm that on real streams.
  • Merged-code rules: no runtime switch and no SoC or board gate. The only constants are the spec's shifts and the uapi array sizes.
Evidence
  • Build at b779e80bab82 with the asahi profile (LLVM): drivers/media/platform/apple/avd/, all objects rebuilt, exit 0, no warnings.
  • run-host-tests.py --only avd: 14/14 pass. That includes avd-av1-tail-tests --mutation tile-info-unchecked and --mutation tile-end-unchecked, both killed as expected.
  • tests/program-test.py: PASS. The new continuation cases at 8160, 16383, UINT32_MAX and another SPS width are refused, and 8159 is taken. tests/av1-test.py: PASS.
  • CI KUnit avd at this head: 219/219, including avd_slice_start_test, avd_slice_pic_size_test and avd_av1_tile_info_test.
  • The hardware decode rows are still listed under "Not yet".

@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

J714s (MacBook Pro 14" M5 Pro, T6050) row at b779e80bab82: PASS. All 9 of this PR's files in the candidate are identical to the head.

@github-actions github-actions Bot added the merge-conflict Conflicts with the base branch (bot) label Nov 28, 2026

@aurora-silicon-review aurora-silicon-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verdict: FINISH, then merge at b779e80b

Blockers:

  1. Merge conflict (CONFLICTING) against main; rebase onto current main and re-run CI (3 checks were cancelled at b779e80b).

Checked:

  • Verified mergeability (CONFLICTING) and existing J714s M5 Pro PASS row at b779e80b.
  • Deferred final diff approval until the conflict against main is resolved and CI finishes green.
Evidence
  • Head b779e80b vs main: CONFLICTING; CI 20 passed, 3 cancelled.

Forward-merge main. Only the KUnit file avd-h264-test.c conflicted:
main added the Annex-B parser and slice header cases and its
avd-h264-parse.h include, this branch the slice start and picture size
cases and avd-h264-params.h. The file keeps both sets of cases and
both includes.

Signed-off-by: Ryan Murray <[email protected]>
@RyanTheTide

Copy link
Copy Markdown
Contributor

Forward-merged main into this branch (merge commit a19ae97, no force push). Only avd-h264-test.c conflicted. Main added the Annex-B parser and slice-header cases plus the avd-h264-parse.h include, and this branch added avd_slice_start_test/avd_slice_pic_size_test plus avd-h264-params.h. The file now has both sets of cases and both includes. Driver files merged cleanly.
Tests: KUnit avd on arm64 QEMU (tests/.kunitconfig) ran 229 tests, all passed, including apple-avd-h264 (18) and apple-avd-av1. I built with KCFLAGS=-Wno-uninitialized-const-pointer because the buildbox's newer clang flags avd-pm-test.c, which this PR does not touch. run-host-tests.py --only avd passed 16/16.
Still needed: CI green and App re-review at the merge head. The rows hold because only the test file conflicted. Thanks @DjDeveloperr.

@aurora-silicon-review aurora-silicon-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verdict: MERGE at a19ae973a027

Blockers: None (wait for queued CI to finish green).

Checked:

  • Forward merge a19ae973a027 of main (MERGEABLE): avd-h264-test.c keeps both main's Annex-B/slice-header tests and this branch's avd_slice_start_test/avd_slice_pic_size_test; driver files are unchanged from b779e80b.
  • Full driver audit of drivers/media/platform/apple/avd/avd-av1.c, avd-h264-params.h, avd-h264.c, and avd.h:
    • avd_av1_check_tile_dim() and avd_av1_check_tile_info(): validates 1 <= tiles <= max_tiles, start < sbs, exact mi_starts[i] == start << sb_shift, overflow-safe sbs_minus_1[i] < sbs - start, exact frame coverage start == sbs, and context_update_tile_id < tile_cols * tile_rows before programming tile registers or sizing work buffers.
    • avd_h264_pic_size() and avd_h264_slice_in_picture(): computes picture macroblock count in u64 respecting FRAME_MBS_ONLY, FIELD_PIC, and MB_ADAPTIVE_FRAME_FIELD per H.264 7.4.2.1.1/7.4.3, validates ((u64)first_mb << mbaff) < pic->mbs, and rejects mid-picture size changes across held slices.
  • Verification at a19ae973a027: KUnit avd 229/229 pass (including apple-avd-h264 and apple-avd-av1), run-host-tests.py --only avd 16/16 pass; J714s M5 Pro hardware row from b779e80b stands with unchanged driver files.
Evidence
  • Verified avd_h264_run() validates pic before avd_h264_pick_bank() and records h264_ctx->b->pic = pic on each new frame.
  • Queued CI checks at a19ae973a027 must finish green before merge.

@RyanTheTide

Copy link
Copy Markdown
Contributor

code-owner ping <@422957901716652033> / @DjDeveloperr: merge train at a19ae973. App approved (NEON-PR-REVIEW2), KUnit and host fixtures pass, J700 row. The new AV1 tile and H.264 slice bounds run on every AVD Mac, so one decode row on another generation at this head (J616s or J413: a real H.264 and an AV1 clip decode clean, no stream rejected) covers the risk of refusing valid input. Merges under the 6-point rule after that, unless you object.

@RyanTheTide

Copy link
Copy Markdown
Contributor

code-owner ping @DjDeveloperr: closeout merge at a19ae973. The App approved at this head after the closeout forward-merge/fix. Rows: J714s + J700 at the pre-merge head; the merge only adds both sets of KUnit cases. Merging under the 6-point rule 1 h from now, once hosted CI at this head and main CI are green, unless you object.

@github-actions github-actions Bot removed ci-failing A check failed at the PR head (bot) merge-conflict Conflicts with the base branch (bot) labels Nov 29, 2026
@github-actions github-actions Bot added the needs-hw-row No hardware row at the PR head (bot) label Nov 29, 2026
@RyanTheTide

Copy link
Copy Markdown
Contributor

Merging under the 6-point rule (Ryan 09:29, permanent), at 0e6c8b04. (1) The review App approved at this head. (2) Full CI is green. (3/4) Rows: J714s-+-J700-at-the-pre-merge-head;-the-merge-only-adds-both-sets-of-KUnit-cases. (5) There's no blocking review. (6) Dj was pinged here 406 min ago with no objection. Revert: git revert -m 1 <merge>.

@RyanTheTide
RyanTheTide merged commit 7a9a04e into main Nov 29, 2026
23 checks passed
@RyanTheTide
RyanTheTide deleted the fix/avd-slice-tile-bounds branch November 29, 2026 12:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:gpu Area from CODEOWNERS (bot) area:media Area from CODEOWNERS (bot) blast:all Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) needs-hw-row No hardware row at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants