From c17aa92bee945ee327575a8a1b141b7dff74bbde Mon Sep 17 00:00:00 2001 From: Dj <43033058+DjDeveloperr@users.noreply.github.com> Date: Thu, 26 Nov 2026 01:27:49 -0500 Subject: [PATCH 1/2] media: apple: avd: validate H.264 slice start against the picture size 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 <43033058+DjDeveloperr@users.noreply.github.com> --- .../platform/apple/avd/avd-h264-params.h | 50 +++++++++++ .../media/platform/apple/avd/avd-h264-test.c | 89 +++++++++++++++++++ drivers/media/platform/apple/avd/avd-h264.c | 16 ++++ .../platform/apple/avd/tests/program-test.py | 18 +++- 4 files changed, 172 insertions(+), 1 deletion(-) diff --git a/drivers/media/platform/apple/avd/avd-h264-params.h b/drivers/media/platform/apple/avd/avd-h264-params.h index eaf6a60b71ff3..d0cbded1f2bf0 100644 --- a/drivers/media/platform/apple/avd/avd-h264-params.h +++ b/drivers/media/platform/apple/avd/avd-h264-params.h @@ -33,6 +33,56 @@ avd_h264_slice_type_supported(const struct v4l2_ctrl_h264_slice_params *sl) sl->slice_type == V4L2_H264_SLICE_TYPE_B; } +/* + * The picture a slice belongs to, in macroblocks (H.264 7.4.2.1.1 and the + * field_pic_flag semantics in 7.4.3): PicWidthInMbs, and PicSizeInMbs = + * PicWidthInMbs * FrameHeightInMbs / (1 + field_pic_flag), with + * FrameHeightInMbs = (2 - frame_mbs_only_flag) * PicHeightInMapUnits. + */ +struct avd_h264_pic_size { + u32 width_mbs; + u64 mbs; +}; + +static inline struct avd_h264_pic_size +avd_h264_pic_size(const struct v4l2_ctrl_h264_sps *sps, + const struct v4l2_ctrl_h264_decode_params *decode) +{ + bool field = decode->flags & V4L2_H264_DECODE_PARAM_FLAG_FIELD_PIC; + u32 frame_height = (sps->flags & V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY ? 1 : 2) * + ((u32)sps->pic_height_in_map_units_minus1 + 1); + struct avd_h264_pic_size pic = { + .width_mbs = (u32)sps->pic_width_in_mbs_minus1 + 1, + }; + + pic.mbs = (u64)pic.width_mbs * (frame_height / (1 + field)); + return pic; +} + +static inline bool avd_h264_same_pic_size(const struct avd_h264_pic_size *a, + const struct avd_h264_pic_size *b) +{ + return a->width_mbs == b->width_mbs && a->mbs == b->mbs; +} + +/* + * H.264 7.4.3: first_mb_in_slice * (1 + MbaffFrameFlag) is less than + * PicSizeInMbs. The slice start is programmed as a macroblock column and + * row of @pic, so a slice outside it would send the decoder outside the + * picture's buffers. + */ +static inline bool +avd_h264_slice_in_picture(const struct avd_h264_pic_size *pic, + const struct v4l2_ctrl_h264_sps *sps, + const struct v4l2_ctrl_h264_decode_params *decode, + u32 first_mb) +{ + bool mbaff = (sps->flags & V4L2_H264_SPS_FLAG_MB_ADAPTIVE_FRAME_FIELD) && + !(decode->flags & V4L2_H264_DECODE_PARAM_FLAG_FIELD_PIC); + + return ((u64)first_mb << mbaff) < pic->mbs; +} + /* Pictures are held until their final slice; ASO is not implemented. */ static inline bool avd_h264_slice_order_supported(unsigned int held, unsigned int last_mb, diff --git a/drivers/media/platform/apple/avd/avd-h264-test.c b/drivers/media/platform/apple/avd/avd-h264-test.c index 0ffc5ac66d15b..e08b2103fdde0 100644 --- a/drivers/media/platform/apple/avd/avd-h264-test.c +++ b/drivers/media/platform/apple/avd/avd-h264-test.c @@ -8,6 +8,7 @@ #include #include "avd.h" +#include "avd-h264-params.h" struct avd_slice_data_case { const char *desc; @@ -270,6 +271,92 @@ static void avd_scaling_flat_test(struct kunit *test) KUNIT_EXPECT_FALSE(test, avd_h264_scaling_is_flat(&pps, m)); } +struct avd_slice_start_case { + const char *desc; + u32 width_mbs; + u32 height_map_units; + u32 sps_flags; + u32 decode_flags; + u32 first_mb; + bool in_picture; +}; + +#define AVD_TEST_FRAME V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY +#define AVD_TEST_MBAFF V4L2_H264_SPS_FLAG_MB_ADAPTIVE_FRAME_FIELD +#define AVD_TEST_FIELD V4L2_H264_DECODE_PARAM_FLAG_FIELD_PIC + +static const struct avd_slice_start_case avd_slice_start_cases[] = { + { "64x64, first macroblock", 4, 4, AVD_TEST_FRAME, 0, 0, true }, + { "64x64, last macroblock", 4, 4, AVD_TEST_FRAME, 0, 15, true }, + { "64x64, PicSizeInMbs", 4, 4, AVD_TEST_FRAME, 0, 16 }, + { "64x64, macroblock 16383", 4, 4, AVD_TEST_FRAME, 0, 16383 }, + { "64x64, U32_MAX", 4, 4, AVD_TEST_FRAME, 0, U32_MAX }, + { "1920x1088, last macroblock", 120, 68, AVD_TEST_FRAME, 0, 8159, true }, + { "1920x1088, PicSizeInMbs", 120, 68, AVD_TEST_FRAME, 0, 8160 }, + { "4096x4096, last macroblock", 256, 256, AVD_TEST_FRAME, 0, 65535, true }, + { "4096x4096, PicSizeInMbs", 256, 256, AVD_TEST_FRAME, 0, 65536 }, + /* PicSizeInMbs is 2^32 here: no 32-bit wrap to zero */ + { "largest SPS size, U32_MAX", U16_MAX + 1, U16_MAX + 1, AVD_TEST_FRAME, + 0, U32_MAX, true }, + { "interlaced frame, last macroblock", 4, 4, 0, 0, 31, true }, + { "interlaced frame, PicSizeInMbs", 4, 4, 0, 0, 32 }, + { "field, last macroblock", 4, 4, 0, AVD_TEST_FIELD, 15, true }, + { "field, PicSizeInMbs", 4, 4, 0, AVD_TEST_FIELD, 16 }, + { "MBAFF frame, last pair", 4, 4, AVD_TEST_MBAFF, 0, 15, true }, + { "MBAFF frame, half PicSizeInMbs", 4, 4, AVD_TEST_MBAFF, 0, 16 }, + { "MBAFF SPS, field", 4, 4, AVD_TEST_MBAFF, AVD_TEST_FIELD, 15, true }, +}; + +KUNIT_ARRAY_PARAM_DESC(avd_slice_start, avd_slice_start_cases, desc); + +/* H.264 7.4.3: a slice starts inside its picture. */ +static void avd_slice_start_test(struct kunit *test) +{ + const struct avd_slice_start_case *c = test->param_value; + struct v4l2_ctrl_h264_sps sps = { + .pic_width_in_mbs_minus1 = c->width_mbs - 1, + .pic_height_in_map_units_minus1 = c->height_map_units - 1, + .flags = c->sps_flags, + }; + struct v4l2_ctrl_h264_decode_params decode = { .flags = c->decode_flags }; + struct avd_h264_pic_size pic = avd_h264_pic_size(&sps, &decode); + + KUNIT_EXPECT_EQ(test, avd_h264_slice_in_picture(&pic, &sps, &decode, + c->first_mb), + c->in_picture); +} + +/* A held picture's later slices keep the size its first slice set. */ +static void avd_slice_pic_size_test(struct kunit *test) +{ + struct v4l2_ctrl_h264_sps sps = { + .pic_width_in_mbs_minus1 = 3, + .pic_height_in_map_units_minus1 = 3, + .flags = V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY, + }; + struct v4l2_ctrl_h264_decode_params decode = {}; + struct avd_h264_pic_size first = avd_h264_pic_size(&sps, &decode); + struct avd_h264_pic_size pic; + + KUNIT_EXPECT_EQ(test, first.width_mbs, 4U); + KUNIT_EXPECT_EQ(test, first.mbs, 16ULL); + pic = avd_h264_pic_size(&sps, &decode); + KUNIT_EXPECT_TRUE(test, avd_h264_same_pic_size(&first, &pic)); + + /* As many macroblocks in another shape */ + sps.pic_width_in_mbs_minus1 = 1; + sps.pic_height_in_map_units_minus1 = 7; + pic = avd_h264_pic_size(&sps, &decode); + KUNIT_EXPECT_EQ(test, pic.mbs, 16ULL); + KUNIT_EXPECT_FALSE(test, avd_h264_same_pic_size(&first, &pic)); + + /* A taller picture of the same width */ + sps.pic_width_in_mbs_minus1 = 3; + sps.pic_height_in_map_units_minus1 = 255; + pic = avd_h264_pic_size(&sps, &decode); + KUNIT_EXPECT_FALSE(test, avd_h264_same_pic_size(&first, &pic)); +} + static struct kunit_case avd_h264_test_cases[] = { KUNIT_CASE(avd_scaling_flat_test), KUNIT_CASE_PARAM(avd_slice_data_test, avd_slice_data_gen_params), @@ -277,6 +364,8 @@ static struct kunit_case avd_h264_test_cases[] = { KUNIT_CASE_PARAM(avd_sps_size_test, avd_sps_size_gen_params), KUNIT_CASE(avd_sps_fmt_init_test), KUNIT_CASE(avd_start_slices_test), + KUNIT_CASE_PARAM(avd_slice_start_test, avd_slice_start_gen_params), + KUNIT_CASE(avd_slice_pic_size_test), {} }; diff --git a/drivers/media/platform/apple/avd/avd-h264.c b/drivers/media/platform/apple/avd/avd-h264.c index 89c12e5fb8f52..e27add29b621d 100644 --- a/drivers/media/platform/apple/avd/avd-h264.c +++ b/drivers/media/platform/apple/avd/avd-h264.c @@ -77,6 +77,8 @@ struct avd_bank { /* allocated size of the held slices */ size_t slice_bytes; u32 last_mb; + /* the size of the held picture, set by its first slice */ + struct avd_h264_pic_size pic; struct avd_h264_bufs { /* VP to PP instruction FIFO for pictures of this bank */ @@ -1109,6 +1111,7 @@ static int avd_h264_run(struct avd_ctx *ctx) struct avd_h264_ctx *h264_ctx = ctx->priv; struct v4l2_h264_reflist_builder reflist_builder; struct avd_h264_run run; + struct avd_h264_pic_size pic; size_t held = h264_ctx->b->busy && !h264_ctx->b->submitted ? h264_ctx->b->slice_num : 0; int ret; @@ -1138,10 +1141,23 @@ static int avd_h264_run(struct avd_ctx *ctx) goto err_free_jobs; } + /* The slice starts in its picture; later slices keep the held picture's size. */ + pic = avd_h264_pic_size(run.sps, run.decode_params); + if (!avd_h264_slice_in_picture(&pic, run.sps, run.decode_params, + run.slice_params->first_mb_in_slice) || + (held && !avd_h264_same_pic_size(&pic, &h264_ctx->b->pic))) { + dev_err_ratelimited(ctx->dev->dev, + "H.264 slice at macroblock %u is outside its %llu macroblock picture\n", + run.slice_params->first_mb_in_slice, pic.mbs); + ret = -EINVAL; + goto err_free_jobs; + } + if (is_new_frame(run.slice_params)) { ret = avd_h264_pick_bank(ctx, run.base.bufs.dst); if (ret) goto err_free_jobs; + h264_ctx->b->pic = pic; } if (!h264_ctx->b->busy || h264_ctx->b->submitted || diff --git a/drivers/media/platform/apple/avd/tests/program-test.py b/drivers/media/platform/apple/avd/tests/program-test.py index 77db961856499..bc9c4d3e4d438 100755 --- a/drivers/media/platform/apple/avd/tests/program-test.py +++ b/drivers/media/platform/apple/avd/tests/program-test.py @@ -507,6 +507,22 @@ def h264_prelude(): assert(hc.bank[0].busy&&hc.bank[0].submitted&&dst[0].bank==1); pps.num_slice_groups_minus1=0;sps.bit_depth_chroma_minus8=0;valid_data=true;sl.slice_type=V4L2_H264_SLICE_TYPE_I; } + /* A slice past PicSizeInMbs (8160) or of another picture size aborts only the builder. */ + for(unsigned int failure=0;failure<4;failure++) { + sl.first_mb_in_slice=0;dma_count=0;assert(!avd_h264_run(&ctx)); + sl.first_mb_in_slice=failure==0?8160:failure==1?16383:failure==2?UINT32_MAX:33; + if(failure==3)sps.pic_width_in_mbs_minus1=59; + assert(avd_h264_run(&ctx)==-EINVAL); + assert(!hc.bank[1].busy&&!hc.bank[1].slice_num&&!ctx.job.segments&&!dst[1].bank); + assert(hc.bank[0].busy&&hc.bank[0].submitted&&dst[0].bank==1); + sps.pic_width_in_mbs_minus1=119; + } + /* The last macroblock still starts a slice; one past it drops the held picture. */ + sl.first_mb_in_slice=0;dma_count=0;assert(!avd_h264_run(&ctx)); + sl.first_mb_in_slice=8159;dma_count=0;assert(!avd_h264_run(&ctx)); + assert(hc.bank[1].busy&&!hc.bank[1].submitted&&hc.bank[1].slice_num==2); + sl.first_mb_in_slice=8160;assert(avd_h264_run(&ctx)==-EINVAL); + assert(!hc.bank[1].busy&&!hc.bank[1].slice_num&&!ctx.job.segments&&!dst[1].bank); /* Final slice submits once; retirement selects CAPTURE's bank, not hc.b. */ sl.first_mb_in_slice=0;dma_count=0;assert(!avd_h264_run(&ctx)); sl.first_mb_in_slice=33;src.flags=0;dma_count=0;assert(!avd_h264_run(&ctx)); @@ -515,7 +531,7 @@ def h264_prelude(): avd_h264_done(&ctx,&src,&dst[0].base.vb,VB2_BUF_STATE_DONE); assert(!hc.bank[0].busy&&hc.bank[1].busy&&hc.b==&hc.bank[1]); avd_h264_done(&ctx,&src,&dst[1].base.vb,VB2_BUF_STATE_DONE);assert(!hc.bank[1].busy); - assert(postambles==27&&held_finishes==16&&submissions==2); + assert(postambles==38&&held_finishes==22&&submissions==2); /* The legacy transport collects the same held slices in its single bank. */ dev.queued=false;hc.nbanks=1;dev.building=&frames[0];fixture_run.base.bufs.dst=&dst[0].base.vb; src.flags=V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF;sl.first_mb_in_slice=0;dma_count=0; From b779e80bab825da0cb9b9dc2756012641c40ff1e Mon Sep 17 00:00:00 2001 From: Dj <43033058+DjDeveloperr@users.noreply.github.com> Date: Thu, 26 Nov 2026 01:28:14 -0500 Subject: [PATCH 2/2] media: apple: avd: validate the AV1 tile layout against the frame 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 <43033058+DjDeveloperr@users.noreply.github.com> --- .../media/platform/apple/avd/avd-av1-test.c | 94 +++++++++++++++++++ drivers/media/platform/apple/avd/avd-av1.c | 66 +++++++++++++ drivers/media/platform/apple/avd/avd.h | 3 + tools/asahi/avd-av1-tail-tests.py | 30 +++++- tools/asahi/run-host-tests.py | 3 +- 5 files changed, 191 insertions(+), 5 deletions(-) diff --git a/drivers/media/platform/apple/avd/avd-av1-test.c b/drivers/media/platform/apple/avd/avd-av1-test.c index c88815e38db47..78b22af446097 100644 --- a/drivers/media/platform/apple/avd/avd-av1-test.c +++ b/drivers/media/platform/apple/avd/avd-av1-test.c @@ -274,6 +274,99 @@ static void avd_av1_try_frame_size_test(struct kunit *test) v4l2_ctrl_handler_free(&ctx->ctrl_hdl); } +struct avd_av1_tile_case { + const char *desc; + u16 width, height; + bool sb128; + u8 cols, rows; + /* mi_*_starts and *_in_sbs_minus_1 of the first tiles */ + u32 col_start[3], col_sbs[3]; + u32 row_start[3], row_sbs[3]; + u8 context_tile; + int ret; +}; + +/* + * 1920x1080 is 480x270 MI units: 30x17 superblocks of 64, 15x9 of 128. + * Two uniform tiles per dimension are 15+15 columns and 9+8 rows. + */ +static const struct avd_av1_tile_case avd_av1_tile_cases[] = { + { "64x64, one tile", 64, 64, false, 1, 1, {0}, {0}, {0}, {0}, 0, 0 }, + { "64x64, 4096x4096 superblock tile", 64, 64, false, 1, 1, + {0}, {4095}, {0}, {4095}, 0, -EINVAL }, + { "64x64, tile one superblock too wide", 64, 64, false, 1, 1, + {0}, {1}, {0}, {0}, 0, -EINVAL }, + { "64x64, tile one superblock too high", 64, 64, false, 1, 1, + {0}, {0}, {0}, {1}, 0, -EINVAL }, + { "66x66, two partial superblocks", 66, 66, false, 2, 2, + {0, 16}, {0, 0}, {0, 16}, {0, 0}, 0, 0 }, + { "1080p, one tile", 1920, 1080, false, 1, 1, {0}, {29}, {0}, {16}, 0, 0 }, + { "1080p, 2x2 tiles", 1920, 1080, false, 2, 2, + {0, 240}, {14, 14}, {0, 144}, {8, 7}, 0, 0 }, + { "1080p, 128x128 superblocks", 1920, 1080, true, 1, 1, + {0}, {14}, {0}, {8}, 0, 0 }, + { "1080p, 64x64 layout with 128x128 superblocks", 1920, 1080, true, 1, 1, + {0}, {29}, {0}, {16}, 0, -EINVAL }, + { "1080p, second column one MI early", 1920, 1080, false, 2, 1, + {0, 239}, {14, 14}, {0}, {16}, 0, -EINVAL }, + { "1080p, second row one superblock late", 1920, 1080, false, 1, 2, + {0}, {29}, {0, 160}, {8, 7}, 0, -EINVAL }, + { "1080p, first column not at 0", 1920, 1080, false, 1, 1, + {16}, {29}, {0}, {16}, 0, -EINVAL }, + { "1080p, columns short of the frame", 1920, 1080, false, 2, 1, + {0, 240}, {14, 13}, {0}, {16}, 0, -EINVAL }, + { "1080p, columns past the frame", 1920, 1080, false, 2, 1, + {0, 240}, {14, 15}, {0}, {16}, 0, -EINVAL }, + { "1080p, third column past the frame", 1920, 1080, false, 3, 1, + {0, 240, 480}, {14, 14, 0}, {0}, {16}, 0, -EINVAL }, + { "1080p, largest width", 1920, 1080, false, 1, 1, + {0}, {U32_MAX}, {0}, {16}, 0, -EINVAL }, + { "1080p, width wrapping to the next column", 1920, 1080, false, 2, 1, + {0, 0}, {U32_MAX, 29}, {0}, {16}, 0, -EINVAL }, + { "1080p, largest height", 1920, 1080, false, 1, 1, + {0}, {29}, {0}, {U32_MAX}, 0, -EINVAL }, + { "1080p, no tiles", 1920, 1080, false, 0, 0, {}, {}, {}, {}, 0, -EINVAL }, + { "1080p, no columns", 1920, 1080, false, 0, 1, {}, {}, {0}, {16}, 0, -EINVAL }, + { "1080p, more columns than tile_info has", 1920, 1080, false, + V4L2_AV1_MAX_TILE_COLS + 1, 1, {0}, {0}, {0}, {16}, 0, -EINVAL }, + { "1080p, CDFs from the last tile", 1920, 1080, false, 2, 2, + {0, 240}, {14, 14}, {0, 144}, {8, 7}, 3, 0 }, + { "1080p, CDFs from past the tiles", 1920, 1080, false, 2, 2, + {0, 240}, {14, 14}, {0, 144}, {8, 7}, 4, -EINVAL }, +}; + +KUNIT_ARRAY_PARAM_DESC(avd_av1_tile, avd_av1_tile_cases, desc); + +/* The tiles cover the frame's superblock grid, no more and no less. */ +static void avd_av1_tile_info_test(struct kunit *test) +{ + const struct avd_av1_tile_case *c = test->param_value; + struct v4l2_ctrl_av1_sequence seq = { + .flags = c->sb128 ? V4L2_AV1_SEQUENCE_FLAG_USE_128X128_SUPERBLOCK : 0, + }; + struct v4l2_ctrl_av1_frame *frame; + struct v4l2_av1_tile_info *t; + unsigned int i; + + frame = kunit_kzalloc(test, sizeof(*frame), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, frame); + frame->frame_width_minus_1 = c->width - 1; + frame->frame_height_minus_1 = c->height - 1; + frame->upscaled_width = c->width; + t = &frame->tile_info; + t->tile_cols = c->cols; + t->tile_rows = c->rows; + t->context_update_tile_id = c->context_tile; + for (i = 0; i < ARRAY_SIZE(c->col_start); i++) { + t->mi_col_starts[i] = c->col_start[i]; + t->width_in_sbs_minus_1[i] = c->col_sbs[i]; + t->mi_row_starts[i] = c->row_start[i]; + t->height_in_sbs_minus_1[i] = c->row_sbs[i]; + } + + KUNIT_EXPECT_EQ(test, avd_av1_check_tile_info(NULL, &seq, frame), c->ret); +} + static struct kunit_case avd_av1_test_cases[] = { KUNIT_CASE(avd_av1_tiles_valid_test), KUNIT_CASE(avd_av1_tiles_count_test), @@ -282,6 +375,7 @@ static struct kunit_case avd_av1_test_cases[] = { KUNIT_CASE(avd_av1_copy_tile_group_test), KUNIT_CASE_PARAM(avd_av1_frame_size_test, avd_av1_size_gen_params), KUNIT_CASE(avd_av1_try_frame_size_test), + KUNIT_CASE_PARAM(avd_av1_tile_info_test, avd_av1_tile_gen_params), {} }; diff --git a/drivers/media/platform/apple/avd/avd-av1.c b/drivers/media/platform/apple/avd/avd-av1.c index 04d55dd7b0aad..de8ce1b2b13db 100644 --- a/drivers/media/platform/apple/avd/avd-av1.c +++ b/drivers/media/platform/apple/avd/avd-av1.c @@ -1452,6 +1452,68 @@ avd_av1_check_tiles(struct device *dev, unsigned int tile_rows, } EXPORT_SYMBOL_IF_KUNIT(avd_av1_check_tiles); +/* + * The tiles of one dimension must cover the frame's superblocks the way + * tile_info() lays them out (AV1 5.9.15): at least one tile, tile i starting + * at the superblocks of the tiles before it, << sbShift, in MI units, and + * the tile sizes adding up to the frame's superblocks. set_tiles() programs + * each tile's start and end from these, and the work buffers are sized from + * the tile sizes. + */ +static int avd_av1_check_tile_dim(const u32 *mi_starts, const u32 *sbs_minus_1, + unsigned int tiles, unsigned int max_tiles, + u32 mi_size, unsigned int sb_shift) +{ + u32 sbs = (mi_size + (1 << sb_shift) - 1) >> sb_shift; + u32 start = 0; + unsigned int i; + + if (!tiles || tiles > max_tiles) + return -EINVAL; + for (i = 0; i < tiles; i++) { + if (start >= sbs || mi_starts[i] != start << sb_shift || + sbs_minus_1[i] >= sbs - start) + return -EINVAL; + start += sbs_minus_1[i] + 1; + } + return start == sbs ? 0 : -EINVAL; +} + +/* + * The frame is MiCols x MiRows 4x4 blocks (AV1 5.9.9, compute_image_size()), + * in superblocks of 16 or 32 of them (sbShift 4 or 5, AV1 5.9.15). The tile + * whose CDFs are kept is one of the frame's tiles (AV1 6.8.14). + */ +VISIBLE_IF_KUNIT int +avd_av1_check_tile_info(struct device *dev, + const struct v4l2_ctrl_av1_sequence *seq, + const struct v4l2_ctrl_av1_frame *frame) +{ + const struct v4l2_av1_tile_info *t = &frame->tile_info; + unsigned int sb_shift = + seq->flags & V4L2_AV1_SEQUENCE_FLAG_USE_128X128_SUPERBLOCK ? 5 : 4; + u32 mi_cols = 2 * (((u32)frame->frame_width_minus_1 + 1 + 7) >> 3); + u32 mi_rows = 2 * (((u32)frame->frame_height_minus_1 + 1 + 7) >> 3); + + if (avd_av1_check_tile_dim(t->mi_col_starts, t->width_in_sbs_minus_1, + t->tile_cols, V4L2_AV1_MAX_TILE_COLS, + mi_cols, sb_shift) || + avd_av1_check_tile_dim(t->mi_row_starts, t->height_in_sbs_minus_1, + t->tile_rows, V4L2_AV1_MAX_TILE_ROWS, + mi_rows, sb_shift)) { + dev_err_ratelimited(dev, "AV1: %ux%u tiles do not cover the %ux%u frame\n", + t->tile_cols, t->tile_rows, + frame->frame_width_minus_1 + 1, + frame->frame_height_minus_1 + 1); + return -EINVAL; + } + if (t->context_update_tile_id >= t->tile_cols * t->tile_rows) + return -EINVAL; + + return 0; +} +EXPORT_SYMBOL_IF_KUNIT(avd_av1_check_tile_info); + /* * Setting a larger tile-group array reallocates the control's storage and * frees the old one under the control lock, which the run does not otherwise @@ -1599,6 +1661,10 @@ static int avd_av1_run_preamble(struct avd_ctx *ctx, struct avd_av1_run *run) return -EINVAL; } + ret = avd_av1_check_tile_info(ctx->dev->dev, run->seq, run->frame); + if (ret) + return ret; + ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, V4L2_CID_STATELESS_AV1_TILE_GROUP_ENTRY); if (WARN_ON(!ctrl)) diff --git a/drivers/media/platform/apple/avd/avd.h b/drivers/media/platform/apple/avd/avd.h index 6bd4b9e6dbc1b..2e16c91670859 100644 --- a/drivers/media/platform/apple/avd/avd.h +++ b/drivers/media/platform/apple/avd/avd.h @@ -421,6 +421,9 @@ struct v4l2_ctrl_av1_tile_group_entry * avd_av1_copy_tile_group(struct v4l2_ctrl *ctrl, unsigned int *entries); int avd_av1_check_frame_size(const struct v4l2_ctrl_av1_frame *frame, u32 width, u32 height); +int avd_av1_check_tile_info(struct device *dev, + const struct v4l2_ctrl_av1_sequence *seq, + const struct v4l2_ctrl_av1_frame *frame); int avd_h264_validate_sps_fmt(struct avd_ctx *ctx, const struct v4l2_ctrl_h264_sps *sps); int avd_h264_validate_sps(struct avd_ctx *ctx, diff --git a/tools/asahi/avd-av1-tail-tests.py b/tools/asahi/avd-av1-tail-tests.py index df866290b8422..b472090821cd8 100644 --- a/tools/asahi/avd-av1-tail-tests.py +++ b/tools/asahi/avd-av1-tail-tests.py @@ -122,6 +122,12 @@ def structure(source, name): static struct avd_dev fixture_dev; static struct avd_av1_stream fixture_stream; static void set_frame_ctx(struct avd_ctx *ctx){ctx->dev=&fixture_dev;((struct avd_av1_ctx *)ctx->priv)->stream=&fixture_stream;ctx->decoded_fmt.fmt.pix_mp.width=1920;ctx->decoded_fmt.fmt.pix_mp.height=1080;controls[0].p_cur.p=&sequence;controls[1].p_cur.p=&frame;controls[2].p_cur.p=&tile;controls[3].p_cur.p=&grain;} +/* One tile of width_sbs x height_sbs 64x64 superblocks, and its tile-group entry. */ +static void one_tile(unsigned int width_sbs,unsigned int height_sbs){ + frame.tile_info.tile_cols=frame.tile_info.tile_rows=1; + frame.tile_info.width_in_sbs_minus_1[0]=width_sbs-1;frame.tile_info.height_in_sbs_minus_1[0]=height_sbs-1; + controls[2].elems=1;controls[2].elem_size=sizeof(tile); +} static void read_exact(const char *path,void *data,size_t n){FILE *f=fopen(path,"rb");assert(f);assert(fread(data,1,n,f)==n);assert(fgetc(f)==EOF);assert(fclose(f)==0);} static void inheritance(unsigned int residue,unsigned int mode,const char *neighbors,bool check_addresses){ const size_t ref_len=6964992+residue,dst_len=7000064+(residue?((residue+79)%256):0); @@ -135,7 +141,7 @@ def structure(source, name): avd_av1_default_coeff_probs(47,&expected);avd_av1_set_default_cdfs(&expected); if(neighbors){unsigned char observation[9728];read_exact(neighbors,observation,sizeof(observation));assert(residue==64);memcpy(src.base.vb.vb2_buf.memory+6817344,observation,sizeof(observation));memcpy(&expected,observation+192,sizeof(expected));} else {expected.intra_inter_cdf[0]^=37;expected.coeff_base_cdf[0][0][0][0]^=59;memcpy(src.base.vb.vb2_buf.memory+ref_offset,&expected,sizeof(expected));} - memset(&frame,0,sizeof(frame));frame.frame_width_minus_1=1919;frame.upscaled_width=1920;frame.frame_height_minus_1=1079;frame.quantization.base_q_idx=47; + memset(&frame,0,sizeof(frame));frame.frame_width_minus_1=1919;frame.upscaled_width=1920;frame.frame_height_minus_1=1079;frame.quantization.base_q_idx=47;one_tile(30,17); frame.frame_type=V4L2_AV1_INTER_FRAME;frame.primary_ref_frame=0;frame.ref_frame_idx[0]=0;frame.reference_frame_ts[0]=1000; if(mode){frame.frame_type=mode==1?V4L2_AV1_KEY_FRAME:mode==2?V4L2_AV1_INTRA_ONLY_FRAME:V4L2_AV1_INTER_FRAME;if(mode==3)frame.flags=V4L2_AV1_FRAME_FLAG_ERROR_RESILIENT_MODE;if(mode==4)frame.primary_ref_frame=7;memset(&expected,0,sizeof(expected));avd_av1_default_coeff_probs(47,&expected);avd_av1_set_default_cdfs(&expected);} controls[0].p_cur.p=&sequence;controls[1].p_cur.p=&frame;controls[2].p_cur.p=&tile;controls[3].p_cur.p=&grain; @@ -165,6 +171,15 @@ def structure(source, name): struct avd_decoded_buffer dst={0};struct avd_av1_ctx av1={0};struct avd_ctx ctx={.priv=&av1};struct avd_av1_run run={0};set_frame_ctx(&ctx); dst.base.vb.vb2_buf.planes[0].length=7000064;memset(&frame,0,sizeof(frame)); frame.frame_width_minus_1=w-1;frame.upscaled_width=w;frame.frame_height_minus_1=h-1;frame.primary_ref_frame=primary;if(primary<7)frame.ref_frame_idx[primary]=idx; + one_tile(w==1280?20:30,h==720?12:17); + run.base.bufs.dst=&dst.base.vb;run.base.y_out=0x120000000UL;return avd_av1_run_preamble(&ctx,&run); +} +/* 1080p is 30x17 superblocks of 64: one tile must cover exactly that, and be the CDF tile. */ +static int tiles(unsigned int width_sbs,unsigned int height_sbs,unsigned char context){ + struct avd_decoded_buffer dst={0};struct avd_av1_ctx av1={0};struct avd_ctx ctx={.priv=&av1};struct avd_av1_run run={0};set_frame_ctx(&ctx); + dst.base.vb.vb2_buf.planes[0].length=7000064;memset(&frame,0,sizeof(frame)); + frame.frame_width_minus_1=1919;frame.upscaled_width=1920;frame.frame_height_minus_1=1079;frame.primary_ref_frame=7; + one_tile(width_sbs,height_sbs);frame.tile_info.context_update_tile_id=context; run.base.bufs.dst=&dst.base.vb;run.base.y_out=0x120000000UL;return avd_av1_run_preamble(&ctx,&run); } static void guards(void){ @@ -176,6 +191,11 @@ def structure(source, name): sequence.seq_profile=1;assert(preamble(1920,1080,0,0)==-EINVAL);sequence.seq_profile=0; sequence.flags&=~V4L2_AV1_SEQUENCE_FLAG_SUBSAMPLING_Y;assert(preamble(1920,1080,0,0)==-EINVAL);sequence.flags|=V4L2_AV1_SEQUENCE_FLAG_SUBSAMPLING_Y; assert(preamble(1920,1080,0,0)==0); + assert(tiles(30,17,0)==0); + assert(tiles(4096,4096,0)==-EINVAL);assert(tiles(31,17,0)==-EINVAL);assert(tiles(30,18,0)==-EINVAL); + assert(tiles(29,17,0)==-EINVAL);assert(tiles(30,16,0)==-EINVAL);assert(tiles(0,17,0)==-EINVAL); + assert(tiles(30,17,1)==-EINVAL); + frame.tile_info.tile_cols=0;assert(avd_av1_check_tile_info(NULL,&sequence,&frame)==-EINVAL); } /* Segmentation map kept with each frame: expect 0 zeroes it, 1 copies the reference's, 2 leaves the AVD's map. */ static void segmap(unsigned char flags,unsigned char primary,unsigned int ref_w,unsigned int ref_h,bool missing,int expect){ @@ -188,7 +208,7 @@ def structure(source, name): memset(dst.base.vb.vb2_buf.memory,0xa5,len);memset(src.base.vb.vb2_buf.memory,0x3c,ref_len); for(size_t i=0;i= V4L2_AV1_TOTAL_REFS_PER_FRAME', 'idx < 0'), # The upscaled width bound also covers the frame width, so drop the frame size check as a whole. 'frame-width-unchecked': ('avd_av1_check_frame_size', 'frame->frame_width_minus_1 >= width ||\n\t frame->frame_height_minus_1 >= height', '0'), + 'tile-info-unchecked': ('avd_av1_run_preamble', 'ret = avd_av1_check_tile_info(ctx->dev->dev, run->seq, run->frame);', 'ret = 0;'), + 'tile-end-unchecked': ('avd_av1_check_tile_dim', 'return start == sbs ? 0 : -EINVAL;', 'return 0;'), } if args.mutation: target, old, new = mutations[args.mutation] diff --git a/tools/asahi/run-host-tests.py b/tools/asahi/run-host-tests.py index a3c33382e0fb1..daf7891ac4416 100755 --- a/tools/asahi/run-host-tests.py +++ b/tools/asahi/run-host-tests.py @@ -160,7 +160,8 @@ def mutations(flag, names, base, expect=KILL): 'avd-av1-tail-tests.py': [(out(), PASS)] + [(out() + ['--case', c], PASS) for c in ('copy', 'seed', 'aligned')] + mutations('--mutation', ('round-up', 'cpu-unaligned', 'seg-cpu-unaligned', - 'seg-any-size', 'ref-index-upper', 'frame-width-unchecked'), out()), + 'seg-any-size', 'ref-index-upper', 'frame-width-unchecked', + 'tile-info-unchecked', 'tile-end-unchecked'), out()), 'avd-codec-tail-tests.py': [(out('--output'), PASS), (out('--output') + ['--mutations'], PASS)], 'bcm4377-completion-tests.py': [([], PASS)], # The old revision is the one before the J493 hardware-table ID match.