diff --git a/drivers/media/platform/apple/avd/Makefile b/drivers/media/platform/apple/avd/Makefile index 66b8e3c61356a..f4e0864a52b0e 100644 --- a/drivers/media/platform/apple/avd/Makefile +++ b/drivers/media/platform/apple/avd/Makefile @@ -3,6 +3,7 @@ apple-avd-y := avd-drv.o avd-v4l2.o avd-hw.o avd-pm.o avd-h264.o avd-vp9.o \ avd-hevc.o avd-av1.o avd-av1-entropymode.o avd-queue.o avd-backend-open.o \ avd-backend-apple.o avd-backend-queue.o avd-backend-kopsia.o -apple-avd-$(CONFIG_VIDEO_APPLE_AVD_KUNIT_TEST) += avd-h264-test.o avd-queue-test.o \ - avd-pm-test.o +apple-avd-$(CONFIG_VIDEO_APPLE_AVD_KUNIT_TEST) += avd-h264-test.o \ + avd-queue-test.o avd-pm-test.o avd-hevc-test.o avd-job-test.o \ + avd-fmt-test.o avd-av1-test.o avd-v4l2-test.o avd-vp9-test.o obj-$(CONFIG_VIDEO_APPLE_AVD) += apple-avd.o diff --git a/drivers/media/platform/apple/avd/avd-av1-test.c b/drivers/media/platform/apple/avd/avd-av1-test.c new file mode 100644 index 0000000000000..c88815e38db47 --- /dev/null +++ b/drivers/media/platform/apple/avd/avd-av1-test.c @@ -0,0 +1,293 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit tests for the AV1 tile and frame size checks of the Apple video + * decoder. + */ + +#include +#include +#include + +#include "avd.h" + +#define AVD_TEST_PAYLOAD 4096 + +/* A rows x cols layout, entries in raster order, each tile 100 bytes. */ +static void avd_av1_test_layout(struct v4l2_ctrl_av1_tile_group_entry *e, + unsigned int rows, unsigned int cols) +{ + unsigned int r, c; + + for (r = 0; r < rows; r++) + for (c = 0; c < cols; c++, e++) { + e->tile_offset = (r * cols + c) * 100; + e->tile_size = 100; + e->tile_row = r; + e->tile_col = c; + } +} + +static void avd_av1_tiles_valid_test(struct kunit *test) +{ + struct v4l2_ctrl_av1_tile_group_entry e[6] = {}; + + avd_av1_test_layout(e, 2, 3); + KUNIT_EXPECT_EQ(test, avd_av1_check_tiles(NULL, 2, 3, e, 6, + AVD_TEST_PAYLOAD), 0); + /* Entries past the frame's tiles are not looked at. */ + e[5].tile_row = U32_MAX; + KUNIT_EXPECT_EQ(test, avd_av1_check_tiles(NULL, 1, 3, e, 6, + AVD_TEST_PAYLOAD), 0); + /* No tiles, no entries. */ + KUNIT_EXPECT_EQ(test, avd_av1_check_tiles(NULL, 0, 0, e, 0, + AVD_TEST_PAYLOAD), 0); +} + +static void avd_av1_tiles_count_test(struct kunit *test) +{ + struct v4l2_ctrl_av1_tile_group_entry e[6] = {}; + + avd_av1_test_layout(e, 2, 3); + KUNIT_EXPECT_EQ(test, avd_av1_check_tiles(NULL, 2, 3, e, 5, + AVD_TEST_PAYLOAD), -EINVAL); + KUNIT_EXPECT_EQ(test, avd_av1_check_tiles(NULL, V4L2_AV1_MAX_TILE_ROWS + 1, + 1, e, 6, AVD_TEST_PAYLOAD), + -EINVAL); + KUNIT_EXPECT_EQ(test, avd_av1_check_tiles(NULL, 1, + V4L2_AV1_MAX_TILE_COLS + 1, + e, 6, AVD_TEST_PAYLOAD), + -EINVAL); +} + +static void avd_av1_tiles_extent_test(struct kunit *test) +{ + struct v4l2_ctrl_av1_tile_group_entry e[6] = {}; + + avd_av1_test_layout(e, 2, 3); + /* The last tile ends exactly at the payload's end. */ + KUNIT_EXPECT_EQ(test, avd_av1_check_tiles(NULL, 2, 3, e, 6, 600), 0); + KUNIT_EXPECT_EQ(test, avd_av1_check_tiles(NULL, 2, 3, e, 6, 599), + -EINVAL); + e[2].tile_offset = U32_MAX; + KUNIT_EXPECT_EQ(test, avd_av1_check_tiles(NULL, 2, 3, e, 6, + AVD_TEST_PAYLOAD), -EINVAL); + e[2].tile_offset = 200; + e[2].tile_size = U32_MAX; + KUNIT_EXPECT_EQ(test, avd_av1_check_tiles(NULL, 2, 3, e, 6, + AVD_TEST_PAYLOAD), -EINVAL); +} + +struct avd_av1_rowcol_case { + const char *desc; + unsigned int idx; + u32 tile_row; + u32 tile_col; + int ret; +}; + +/* Entry idx of a 2x3 layout gets tile_row/tile_col; every other entry is valid. */ +static const struct avd_av1_rowcol_case avd_av1_rowcol_cases[] = { + { "last row and column", 5, 1, 2, 0 }, + { "row equals tile_rows", 5, 2, 2, -EINVAL }, + { "column equals tile_cols", 5, 1, 3, -EINVAL }, + { "first entry, row equals tile_rows", 0, 2, 0, -EINVAL }, + { "row past the tile_info arrays", 3, V4L2_AV1_MAX_TILE_ROWS + 1, 0, -EINVAL }, + { "column past the tile_info arrays", 3, 0, V4L2_AV1_MAX_TILE_COLS + 1, -EINVAL }, + { "huge row", 1, 0x3fffffff, 0, -EINVAL }, + { "huge column", 1, 0, 0x3fffffff, -EINVAL }, + { "row U32_MAX", 4, U32_MAX, 1, -EINVAL }, + { "column U32_MAX", 4, 1, U32_MAX, -EINVAL }, +}; + +KUNIT_ARRAY_PARAM_DESC(avd_av1_rowcol, avd_av1_rowcol_cases, desc); + +static void avd_av1_tiles_rowcol_test(struct kunit *test) +{ + const struct avd_av1_rowcol_case *c = test->param_value; + struct v4l2_ctrl_av1_tile_group_entry e[6] = {}; + + avd_av1_test_layout(e, 2, 3); + e[c->idx].tile_row = c->tile_row; + e[c->idx].tile_col = c->tile_col; + KUNIT_EXPECT_EQ(test, avd_av1_check_tiles(NULL, 2, 3, e, 6, + AVD_TEST_PAYLOAD), c->ret); +} + +/* + * The run takes its own copy of the tile-group entries: replacing and freeing + * the control's storage afterwards, as setting a larger array does, leaves + * the copy and its count intact. + */ +static void avd_av1_copy_tile_group_test(struct kunit *test) +{ + struct v4l2_ctrl_av1_tile_group_entry expect[6] = {}; + struct v4l2_ctrl_av1_tile_group_entry *src, *bigger, *copy; + struct v4l2_ctrl_handler *hdl; + struct v4l2_ctrl *ctrl; + unsigned int entries = 0; + + hdl = kunit_kzalloc(test, sizeof(*hdl), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, hdl); + ctrl = kunit_kzalloc(test, sizeof(*ctrl), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, ctrl); + KUNIT_ASSERT_EQ(test, v4l2_ctrl_handler_init(hdl, 0), 0); + + src = kcalloc(ARRAY_SIZE(expect), sizeof(*src), GFP_KERNEL); + bigger = kcalloc(V4L2_AV1_MAX_TILE_COUNT, sizeof(*bigger), GFP_KERNEL); + if (!src || !bigger) { + kfree(src); + kfree(bigger); + v4l2_ctrl_handler_free(hdl); + KUNIT_FAIL(test, "out of memory"); + return; + } + avd_av1_test_layout(expect, 2, 3); + memcpy(src, expect, sizeof(expect)); + ctrl->handler = hdl; + ctrl->elems = ARRAY_SIZE(expect); + ctrl->elem_size = sizeof(*src); + ctrl->p_cur.p = src; + + copy = avd_av1_copy_tile_group(ctrl, &entries); + KUNIT_EXPECT_EQ(test, entries, ARRAY_SIZE(expect)); + + /* A larger array replaces and frees the old storage. */ + memset(bigger, 0xff, V4L2_AV1_MAX_TILE_COUNT * sizeof(*bigger)); + ctrl->p_cur.p = bigger; + ctrl->elems = V4L2_AV1_MAX_TILE_COUNT; + memset(src, 0xff, sizeof(expect)); + kfree(src); + + if (copy) { + KUNIT_EXPECT_MEMEQ(test, copy, expect, sizeof(expect)); + KUNIT_EXPECT_EQ(test, avd_av1_check_tiles(NULL, 2, 3, copy, + entries, + AVD_TEST_PAYLOAD), 0); + } else { + KUNIT_FAIL(test, "no copy"); + } + KUNIT_EXPECT_EQ(test, entries, ARRAY_SIZE(expect)); + + kfree(copy); + kfree(bigger); + v4l2_ctrl_handler_free(hdl); +} + +struct avd_av1_size_case { + const char *desc; + u32 width; + u32 height; + u32 upscaled_width; + bool superres; + int ret; + u8 denom; +}; + +/* Frames for 1920x1088 CAPTURE buffers. */ +static const struct avd_av1_size_case avd_av1_size_cases[] = { + { "frame of the buffer size", 1920, 1088, 1920, false, 0 }, + { "smaller frame", 1280, 720, 1280, false, 0 }, + { "one sample", 1, 1, 1, false, 0 }, + { "superres to the buffer width", 960, 1080, 1920, true, 0, 16 }, + { "superres to a smaller width", 1000, 1080, 1280, true, 0, 16 }, + { "superres with equal widths", 1920, 1080, 1920, true, 0, 16 }, + { "frame wider than the buffer", 1921, 1080, 1921, false, -EINVAL }, + { "frame higher than the buffer", 1920, 1089, 1920, false, -EINVAL }, + { "frame width wraps", 0, 1080, 1920, false, -EINVAL }, + { "frame height wraps", 1920, 0, 1920, false, -EINVAL }, + { "no upscaled width", 1920, 1080, 0, false, -EINVAL }, + { "no upscaled width, superres", 1920, 1080, 0, true, -EINVAL, 16 }, + { "upscaled width below the frame's", 1920, 1080, 1919, true, -EINVAL, 16 }, + { "upscaled width past the buffer", 960, 1080, 1921, true, -EINVAL, 16 }, + { "huge upscaled width", 960, 1080, U32_MAX, true, -EINVAL, 16 }, + { "upscaled width without superres", 960, 1080, 1920, false, -EINVAL }, + { "superres, smallest denominator", 1072, 1080, 1920, true, 0, 9 }, + { "superres, no denominator", 960, 1080, 1920, true, -EINVAL, 0 }, + { "superres, denominator below 9", 960, 1080, 1920, true, -EINVAL, 8 }, +}; + +KUNIT_ARRAY_PARAM_DESC(avd_av1_size, avd_av1_size_cases, desc); + +static void avd_av1_frame_size_test(struct kunit *test) +{ + const struct avd_av1_size_case *c = test->param_value; + struct v4l2_ctrl_av1_frame *frame; + + 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->upscaled_width; + frame->superres_denom = c->denom; + if (c->superres) + frame->flags |= V4L2_AV1_FRAME_FLAG_USE_SUPERRES; + + KUNIT_EXPECT_EQ(test, avd_av1_check_frame_size(frame, 1920, 1088), + c->ret); +} + +/* + * A frame that does not fit the CAPTURE format is refused when the control + * is set, once CAPTURE buffers are allocated and the format can no longer + * follow it. + */ +static void avd_av1_try_frame_size_test(struct kunit *test) +{ + static const struct v4l2_ctrl_config cfg = { + .id = V4L2_CID_STATELESS_AV1_FRAME, + }; + struct v4l2_m2m_ctx *m2m_ctx; + struct v4l2_ctrl_av1_frame *frame; + struct v4l2_ctrl *ctrl; + struct avd_ctx *ctx; + + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, ctx); + m2m_ctx = kunit_kzalloc(test, sizeof(*m2m_ctx), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, m2m_ctx); + ctx->fh.m2m_ctx = m2m_ctx; + ctx->decoded_fmt.fmt.pix_mp.width = 1920; + ctx->decoded_fmt.fmt.pix_mp.height = 1088; + + v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 1); + ctrl = v4l2_ctrl_new_custom(&ctx->ctrl_hdl, &cfg, NULL); + KUNIT_ASSERT_EQ(test, ctx->ctrl_hdl.error, 0); + KUNIT_ASSERT_NOT_NULL(test, ctrl); + frame = ctrl->p_new.p_av1_frame; + + /* No buffers: the format may still be set to the stream's size. */ + frame->frame_width_minus_1 = 3839; + frame->frame_height_minus_1 = 2159; + frame->upscaled_width = 3840; + KUNIT_EXPECT_EQ(test, avd_av1_fmt_ops.try_ctrl(ctx, ctrl), 0); + + m2m_ctx->cap_q_ctx.q.is_busy = 1; + KUNIT_EXPECT_EQ(test, avd_av1_fmt_ops.try_ctrl(ctx, ctrl), -EINVAL); + frame->frame_width_minus_1 = 1919; + frame->frame_height_minus_1 = 1079; + KUNIT_EXPECT_EQ(test, avd_av1_fmt_ops.try_ctrl(ctx, ctrl), -EINVAL); + frame->upscaled_width = 1920; + KUNIT_EXPECT_EQ(test, avd_av1_fmt_ops.try_ctrl(ctx, ctrl), 0); + frame->upscaled_width = 0; + KUNIT_EXPECT_EQ(test, avd_av1_fmt_ops.try_ctrl(ctx, ctrl), -EINVAL); + + v4l2_ctrl_handler_free(&ctx->ctrl_hdl); +} + +static struct kunit_case avd_av1_test_cases[] = { + KUNIT_CASE(avd_av1_tiles_valid_test), + KUNIT_CASE(avd_av1_tiles_count_test), + KUNIT_CASE(avd_av1_tiles_extent_test), + KUNIT_CASE_PARAM(avd_av1_tiles_rowcol_test, avd_av1_rowcol_gen_params), + 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), + {} +}; + +static struct kunit_suite avd_av1_test_suite = { + .name = "apple-avd-av1", + .test_cases = avd_av1_test_cases, +}; + +kunit_test_suite(avd_av1_test_suite); diff --git a/drivers/media/platform/apple/avd/avd-av1.c b/drivers/media/platform/apple/avd/avd-av1.c index 8b3c2f34c542c..04d55dd7b0aad 100644 --- a/drivers/media/platform/apple/avd/avd-av1.c +++ b/drivers/media/platform/apple/avd/avd-av1.c @@ -16,6 +16,7 @@ * Tomasz Figa */ +#include #include #include "avd.h" @@ -220,7 +221,12 @@ struct avd_av1_run { const struct v4l2_ctrl_av1_sequence *seq; const struct v4l2_ctrl_av1_frame *frame; - const struct v4l2_ctrl_av1_tile_group_entry *tile_group; + /* Private copy of the tile-group control, freed by avd_av1_run(). */ + struct v4l2_ctrl_av1_tile_group_entry *tile_group; + unsigned int tile_group_entries; + /* The tile grid tile_group was checked against */ + unsigned int tile_rows; + unsigned int tile_cols; const struct v4l2_ctrl_av1_film_grain *grain; }; @@ -267,6 +273,13 @@ struct avd_av1_stream { unsigned int nbanks; struct avd_av1_bank bank[AVD_AV1_BANKS]; struct avd_av1_bank *b; + + /* What the run being built took from the controls, see avd_ctrl_copy(). */ + struct { + struct v4l2_ctrl_av1_sequence seq; + struct v4l2_ctrl_av1_frame frame; + struct v4l2_ctrl_av1_film_grain grain; + } params; }; /* @@ -1138,12 +1151,12 @@ static void set_tiles(struct avd_ctx *ctx, struct avd_av1_run *run) seq->flags & V4L2_AV1_SEQUENCE_FLAG_USE_128X128_SUPERBLOCK ? 5 : 4; - av1_ctx->submit_num = tile_info->tile_cols * tile_info->tile_rows; + av1_ctx->submit_num = run->tile_cols * run->tile_rows; - for (row = 0; row < tile_info->tile_rows; row++) { - for (col = 0; col < tile_info->tile_cols; col++) { + for (row = 0; row < run->tile_rows; row++) { + for (col = 0; col < run->tile_cols; col++) { ctx->job.num++; - tile_id = row * tile_info->tile_cols + col; + tile_id = row * run->tile_cols + col; tile_group = &run->tile_group[tile_id]; push(AVD_OP_CODED_DATA | @@ -1396,34 +1409,67 @@ static void update_dec_buf_info(struct avd_decoded_buffer *buf, buf->av1.ref_frame_idx[i] = frame->ref_frame_idx[i]; } -/* The engine fetches each tile from these: bad values make it read wild addresses. */ -static int avd_av1_check_tiles(struct avd_ctx *ctx, struct avd_av1_run *run, - unsigned int entries) +/* + * The engine fetches each tile from these: bad values make it read wild + * addresses. set_tiles() also looks each entry's tile_row and tile_col up in + * the frame's tile_info arrays; in a conformant stream they are below + * TileRows and TileCols (AV1 5.11.1, tile_group_obu()), and TileRows and + * TileCols are at most MAX_TILE_ROWS and MAX_TILE_COLS (AV1 section 3). + */ +VISIBLE_IF_KUNIT int +avd_av1_check_tiles(struct device *dev, unsigned int tile_rows, + unsigned int tile_cols, + const struct v4l2_ctrl_av1_tile_group_entry *tile_group, + unsigned int entries, size_t payload) { - unsigned int tiles = run->frame->tile_info.tile_cols * - run->frame->tile_info.tile_rows; + unsigned int tiles = tile_cols * tile_rows; unsigned int t; - size_t payload; + if (tile_rows > V4L2_AV1_MAX_TILE_ROWS || + tile_cols > V4L2_AV1_MAX_TILE_COLS) + return -EINVAL; if (entries < tiles) { - dev_err_ratelimited(ctx->dev->dev, "AV1: %u tiles but %u tile entries\n", + dev_err_ratelimited(dev, "AV1: %u tiles but %u tile entries\n", tiles, entries); return -EINVAL; } - if (!tiles) - return 0; - payload = vb2_get_plane_payload(&run->base.bufs.src->vb2_buf, 0); for (t = 0; t < tiles; t++) { - const struct v4l2_ctrl_av1_tile_group_entry *e = &run->tile_group[t]; + const struct v4l2_ctrl_av1_tile_group_entry *e = &tile_group[t]; if ((u64)e->tile_offset + e->tile_size > payload) { - dev_err_ratelimited(ctx->dev->dev, "AV1: tile %u at %u+%u past %zu bytes\n", + dev_err_ratelimited(dev, "AV1: tile %u at %u+%u past %zu bytes\n", t, e->tile_offset, e->tile_size, payload); return -EINVAL; } + if (e->tile_row >= tile_rows || e->tile_col >= tile_cols) { + dev_err_ratelimited(dev, "AV1: tile %u at row %u col %u of %ux%u\n", + t, e->tile_row, e->tile_col, + tile_rows, tile_cols); + return -EINVAL; + } } return 0; } +EXPORT_SYMBOL_IF_KUNIT(avd_av1_check_tiles); + +/* + * 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 + * hold. Copy the entries under that lock; the run checks and uses the copy. + */ +VISIBLE_IF_KUNIT struct v4l2_ctrl_av1_tile_group_entry * +avd_av1_copy_tile_group(struct v4l2_ctrl *ctrl, unsigned int *entries) +{ + struct v4l2_ctrl_av1_tile_group_entry *copy; + + v4l2_ctrl_lock(ctrl); + *entries = ctrl->elems; + copy = kmemdup_array(ctrl->p_cur.p, ctrl->elems, ctrl->elem_size, + GFP_KERNEL); + v4l2_ctrl_unlock(ctrl); + return copy; +} +EXPORT_SYMBOL_IF_KUNIT(avd_av1_copy_tile_group); /* * Only the AV1 Main profile at 4:2:0 (8 and 10 bits) decodes. A High @@ -1433,6 +1479,14 @@ static int avd_av1_check_tiles(struct avd_ctx *ctx, struct avd_av1_run *run, * when the sequence is set and again when a frame runs, so it never * reaches the decoder. */ +static enum avd_image_fmt +avd_av1_seq_image_fmt(const struct v4l2_ctrl_av1_sequence *seq) +{ + /* avd_av1_validate_sequence() admits only Main profile 4:2:0. */ + return seq->bit_depth == 8 ? AVD_IMG_FMT_420_8BIT : + AVD_IMG_FMT_420_10BIT; +} + static int avd_av1_validate_sequence(struct avd_ctx *ctx, const struct v4l2_ctrl_av1_sequence *seq) { @@ -1451,8 +1505,46 @@ static int avd_av1_validate_sequence(struct avd_ctx *ctx, return 0; } +/* + * The map, CDFs and color data sit at the end of the CAPTURE buffer, sized by + * the frame, so the frame must fit the CAPTURE format. So must the width it + * is upscaled to, which goes into the header as it is and divides the scale + * factors: superres only widens the frame, from frame_width to upscaled_width + * (AV1 7.16), and without it the two are equal (AV1 5.9.8, superres_params()). + */ +VISIBLE_IF_KUNIT int +avd_av1_check_frame_size(const struct v4l2_ctrl_av1_frame *frame, u32 width, + u32 height) +{ + u32 frame_width; + + if (frame->frame_width_minus_1 >= width || + frame->frame_height_minus_1 >= height) + return -EINVAL; + + frame_width = frame->frame_width_minus_1 + 1; + if (frame->upscaled_width < frame_width || + frame->upscaled_width > width) + return -EINVAL; + if (!(frame->flags & V4L2_AV1_FRAME_FLAG_USE_SUPERRES) && + frame->upscaled_width != frame_width) + return -EINVAL; + /* + * With superres the denominator goes into the header less one; the + * stream codes it as 9 to 16 (AV1 5.9.8), the control core only + * bounds it from above. + */ + if ((frame->flags & V4L2_AV1_FRAME_FLAG_USE_SUPERRES) && + frame->superres_denom < 9) + return -EINVAL; + + return 0; +} +EXPORT_SYMBOL_IF_KUNIT(avd_av1_check_frame_size); + static int avd_av1_run_preamble(struct avd_ctx *ctx, struct avd_av1_run *run) { + struct avd_av1_stream *av1_ctx = ((struct avd_av1_ctx *)ctx->priv)->stream; struct v4l2_ctrl *ctrl; int dst_len, ret, tlb_len; @@ -1460,25 +1552,39 @@ static int avd_av1_run_preamble(struct avd_ctx *ctx, struct avd_av1_run *run) if (run->base.setup_err) return run->base.setup_err; - ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, V4L2_CID_STATELESS_AV1_SEQUENCE); - if (WARN_ON(!ctrl)) - return -EINVAL; - run->seq = ctrl->p_cur.p; + /* + * The checks below and the builders must see the same parameters, and + * the controls can be set again at any time: use copies. + */ + ret = avd_ctrl_copy(ctx, V4L2_CID_STATELESS_AV1_SEQUENCE, + &av1_ctx->params.seq, sizeof(av1_ctx->params.seq)); + if (!ret) + ret = avd_ctrl_copy(ctx, V4L2_CID_STATELESS_AV1_FRAME, + &av1_ctx->params.frame, + sizeof(av1_ctx->params.frame)); + if (!ret) + ret = avd_ctrl_copy(ctx, V4L2_CID_STATELESS_AV1_FILM_GRAIN, + &av1_ctx->params.grain, + sizeof(av1_ctx->params.grain)); + if (WARN_ON(ret)) + return ret; + run->seq = &av1_ctx->params.seq; + run->frame = &av1_ctx->params.frame; + run->grain = &av1_ctx->params.grain; /* The sequence may have been set, unchecked, while another codec was selected. */ ret = avd_av1_validate_sequence(ctx, run->seq); + if (!ret) + ret = avd_check_run_image_fmt(ctx, + avd_av1_seq_image_fmt(run->seq)); if (ret) return ret; - ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, V4L2_CID_STATELESS_AV1_FRAME); - if (WARN_ON(!ctrl)) - return -EINVAL; - run->frame = ctrl->p_cur.p; - - /* The map, CDFs and color data sit at the buffer's end, sized by the frame. */ - if (run->frame->frame_width_minus_1 + 1 > ctx->decoded_fmt.fmt.pix_mp.width || - run->frame->frame_height_minus_1 + 1 > ctx->decoded_fmt.fmt.pix_mp.height) - return -EINVAL; + ret = avd_av1_check_frame_size(run->frame, + ctx->decoded_fmt.fmt.pix_mp.width, + ctx->decoded_fmt.fmt.pix_mp.height); + if (ret) + return ret; /* * The CDF and segmentation map loads index the reference arrays with @@ -1497,17 +1603,17 @@ static int avd_av1_run_preamble(struct avd_ctx *ctx, struct avd_av1_run *run) V4L2_CID_STATELESS_AV1_TILE_GROUP_ENTRY); if (WARN_ON(!ctrl)) return -EINVAL; - run->tile_group = ctrl->p_cur.p; - ret = avd_av1_check_tiles(ctx, run, ctrl->elems); + run->tile_group = avd_av1_copy_tile_group(ctrl, &run->tile_group_entries); + if (!run->tile_group) + return -ENOMEM; + run->tile_rows = run->frame->tile_info.tile_rows; + run->tile_cols = run->frame->tile_info.tile_cols; + ret = avd_av1_check_tiles(ctx->dev->dev, run->tile_rows, run->tile_cols, + run->tile_group, run->tile_group_entries, + vb2_get_plane_payload(&run->base.bufs.src->vb2_buf, 0)); if (ret) return ret; - ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, - V4L2_CID_STATELESS_AV1_FILM_GRAIN); - if (WARN_ON(!ctrl)) - return -EINVAL; - run->grain = ctrl->p_cur.p; - dst_len = run->base.bufs.dst->vb2_buf.planes[0].length; tlb_len = avd_color_size(run->frame->frame_width_minus_1 + 1, run->frame->frame_height_minus_1 + 1); @@ -1665,7 +1771,7 @@ static int avd_av1_alloc_work_bufs(struct avd_ctx *ctx, struct avd_av1_run *run) static int avd_av1_run(struct avd_ctx *ctx) { - struct avd_av1_run run; + struct avd_av1_run run = {}; struct avd_av1_stream *av1_ctx = ((struct avd_av1_ctx *)ctx->priv)->stream; struct avd_decoded_buffer *dst, *ref; int ret; @@ -1673,7 +1779,7 @@ static int avd_av1_run(struct avd_ctx *ctx) ret = avd_av1_run_preamble(ctx, &run); if (ret) { avd_run_postamble(ctx, &run.base); - return ret; + goto out; } /* The CDF and segmentation map copies need the CPU mappings. */ @@ -1687,7 +1793,8 @@ static int avd_av1_run(struct avd_ctx *ctx) if (!vb2_plane_vaddr(&dst->base.vb.vb2_buf, 0) || !vb2_plane_vaddr(&ref->base.vb.vb2_buf, 0)) { avd_run_postamble(ctx, &run.base); - return -ENOMEM; + ret = -ENOMEM; + goto out; } if (av1_ctx->nbanks > 1) { struct avd_av1_bank *bank = NULL; @@ -1699,7 +1806,8 @@ static int avd_av1_run(struct avd_ctx *ctx) } if (!bank) { avd_run_postamble(ctx, &run.base); - return -EBUSY; + ret = -EBUSY; + goto out; } av1_ctx->b = bank; smp_store_release(&ctx->priv, &bank->codec); @@ -1714,18 +1822,16 @@ static int avd_av1_run(struct avd_ctx *ctx) } ret = avd_init_job(ctx, AVD_CODEC_AV1, - run.frame->tile_info.tile_cols * - run.frame->tile_info.tile_rows + - 1); + run.tile_cols * run.tile_rows + 1); if (ret) { avd_run_postamble(ctx, &run.base); - return ret; + goto out; } ret = avd_av1_alloc_work_bufs(ctx, &run); if (ret) { avd_run_postamble(ctx, &run.base); - return ret; + goto out; } update_dec_buf_info(dst, run.seq, run.frame); @@ -1742,7 +1848,10 @@ static int avd_av1_run(struct avd_ctx *ctx) avd_run_postamble(ctx, &run.base); - return avd_submit_job(ctx); + ret = avd_submit_job(ctx); +out: + kfree(run.tile_group); + return ret; } @@ -1881,18 +1990,10 @@ static int avd_av1_start(struct avd_ctx *ctx) static enum avd_image_fmt avd_av1_get_image_fmt(struct avd_ctx *ctx, struct v4l2_ctrl *ctrl) { -#define BIT_DEPTH(chroma) \ - (seq->bit_depth == 8 ? AVD_IMG_FMT_##chroma##_8BIT : \ - AVD_IMG_FMT_##chroma##_10BIT) - - const struct v4l2_ctrl_av1_sequence *seq = ctrl->p_new.p_av1_sequence; - if (ctrl->id != V4L2_CID_STATELESS_AV1_SEQUENCE) return AVD_IMG_FMT_ANY; - /* avd_av1_validate_sequence() admits only Main profile 4:2:0. */ - return BIT_DEPTH(420); -#undef BIT_DEPTH + return avd_av1_seq_image_fmt(ctrl->p_new.p_av1_sequence); } static unsigned int avd_av1_pp_kicks(struct avd_ctx *ctx) @@ -1935,10 +2036,25 @@ static void avd_av1_adjust_decoded_fmt(struct avd_ctx *ctx, static int avd_av1_try_ctrl(struct avd_ctx *ctx, struct v4l2_ctrl *ctrl) { + struct vb2_queue *vq; + if (ctrl->id == V4L2_CID_STATELESS_AV1_SEQUENCE) return avd_av1_validate_sequence(ctx, ctrl->p_new.p_av1_sequence); /* width should also be > 8 */ + if (ctrl->id != V4L2_CID_STATELESS_AV1_FRAME) + return 0; + + /* + * The CAPTURE format is fixed while its buffers are allocated: a frame + * that does not fit them cannot be decoded, as the run finds too. + * Before that the format may still be set to the stream's. + */ + vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE); + if (vb2_is_busy(vq)) + return avd_av1_check_frame_size(ctrl->p_new.p_av1_frame, + ctx->decoded_fmt.fmt.pix_mp.width, + ctx->decoded_fmt.fmt.pix_mp.height); return 0; } diff --git a/drivers/media/platform/apple/avd/avd-backend-open.c b/drivers/media/platform/apple/avd/avd-backend-open.c index 13508cc5d5272..6f6028ad5895e 100644 --- a/drivers/media/platform/apple/avd/avd-backend-open.c +++ b/drivers/media/platform/apple/avd/avd-backend-open.c @@ -29,6 +29,9 @@ int avd_open_build_program(struct avd_ctx *ctx) if (!f || !job->segments) return -EINVAL; + /* The builder ran past the table or a segment; nothing fits. */ + if (job->overflow || job->num >= job->max) + return -EOVERFLOW; for (i = 0; i <= job->num; i++) { if (job->segments[i].num > AVD_MAX_INST || check_add_overflow(count, job->segments[i].num + !!i, &count)) @@ -91,6 +94,9 @@ int avd_open_submit(struct avd_dev *avd, struct avd_frame *f) if (codec > AVD_CODEC_AV1 || !words || !f->prog_words || f->prog_words > f->prog->size / sizeof(u32)) return -EINVAL; + /* The context FIFO only exists while the OUTPUT queue streams. */ + if (!ctx->inst.cpu) + return -EINVAL; if (!(avd->capabilities & BIT(codec))) return -EOPNOTSUPP; for (i = 0; i < codec; i++) diff --git a/drivers/media/platform/apple/avd/avd-drv.c b/drivers/media/platform/apple/avd/avd-drv.c index 3de3f241430f5..c96a9e3b1c571 100644 --- a/drivers/media/platform/apple/avd/avd-drv.c +++ b/drivers/media/platform/apple/avd/avd-drv.c @@ -100,9 +100,11 @@ int avd_init_job(struct avd_ctx *ctx, enum avd_codec codec, size_t segments) job->codec = codec; job->num = 0; + job->overflow = false; /* A table that was built but never submitted still belongs to us. */ kvfree(job->segments); job->segments = kvcalloc(segments, sizeof(*job->segments), GFP_KERNEL); + job->max = job->segments ? segments : 0; if (!job->segments) ret = -ENOMEM; return ret; @@ -188,10 +190,6 @@ static int avd_open(struct file *filp) ctx->dev = avd; - ret = avd_buf_alloc(avd, &ctx->inst, fifo_size()); - if (ret) - goto err_free_ctx; - INIT_LIST_HEAD(&ctx->queue_node); ctx->next_dep = AVD_DEP_PREV_DONE; @@ -219,7 +217,6 @@ static int avd_open(struct file *filp) v4l2_m2m_ctx_release(ctx->fh.m2m_ctx); err_free_ctx: - avd_buf_free(avd, &ctx->inst); kfree(ctx); return ret; } diff --git a/drivers/media/platform/apple/avd/avd-fmt-test.c b/drivers/media/platform/apple/avd/avd-fmt-test.c new file mode 100644 index 0000000000000..63a935a2ab047 --- /dev/null +++ b/drivers/media/platform/apple/avd/avd-fmt-test.c @@ -0,0 +1,150 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit tests for the run-time picture format check of the Apple video + * decoder. + */ + +#include +#include +#include + +#include "avd.h" + +static const struct avd_decoded_fmt_desc avd_fmt_test_decoded[] = { + { .fourcc = V4L2_PIX_FMT_NV12, .image_fmt = AVD_IMG_FMT_420_8BIT }, + { .fourcc = V4L2_PIX_FMT_P010, .image_fmt = AVD_IMG_FMT_420_10BIT }, + { .fourcc = V4L2_PIX_FMT_NV16, .image_fmt = AVD_IMG_FMT_422_8BIT }, +}; + +static const struct avd_coded_fmt_desc avd_fmt_test_desc = { + .num_decoded_fmts = ARRAY_SIZE(avd_fmt_test_decoded), + .decoded_fmts = avd_fmt_test_decoded, +}; + +/* A context whose CAPTURE format was negotiated for @sized as @fourcc. */ +static struct avd_ctx *avd_fmt_test_ctx(struct kunit *test, + enum avd_image_fmt sized, u32 fourcc) +{ + struct avd_ctx *ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + struct avd_dev *avd = kunit_kzalloc(test, sizeof(*avd), GFP_KERNEL); + struct v4l2_pix_format_mplane *pix; + + KUNIT_ASSERT_NOT_NULL(test, ctx); + KUNIT_ASSERT_NOT_NULL(test, avd); + avd->dev = kunit_device_register(test, "avd-fmt-test"); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, avd->dev); + ctx->dev = avd; + ctx->coded_fmt_desc = &avd_fmt_test_desc; + ctx->image_fmt = sized; + pix = &ctx->decoded_fmt.fmt.pix_mp; + pix->pixelformat = fourcc; + pix->width = 64; + pix->height = 64; + fill_comp(&ctx->comp, sized, pix->width, pix->height); + return ctx; +} + +static void avd_fmt_any_test(struct kunit *test) +{ + struct avd_ctx *ctx = avd_fmt_test_ctx(test, AVD_IMG_FMT_ANY, + V4L2_PIX_FMT_NV12); + + /* Buffers sized for "any" are 8-bit 4:2:0. */ + KUNIT_EXPECT_EQ(test, avd_check_run_image_fmt(ctx, AVD_IMG_FMT_ANY), 0); + KUNIT_EXPECT_EQ(test, + avd_check_run_image_fmt(ctx, AVD_IMG_FMT_420_8BIT), 0); + KUNIT_EXPECT_EQ(test, + avd_check_run_image_fmt(ctx, AVD_IMG_FMT_420_10BIT), + -EINVAL); + KUNIT_EXPECT_EQ(test, + avd_check_run_image_fmt(ctx, AVD_IMG_FMT_422_8BIT), + -EINVAL); +} + +/* "Any" with a 10-bit CAPTURE fourcc still has 8-bit sized storage. */ +static void avd_fmt_any_p010_test(struct kunit *test) +{ + struct avd_ctx *ctx = avd_fmt_test_ctx(test, AVD_IMG_FMT_ANY, + V4L2_PIX_FMT_P010); + + KUNIT_EXPECT_EQ(test, + avd_check_run_image_fmt(ctx, AVD_IMG_FMT_420_10BIT), + -EINVAL); +} + +static void avd_fmt_10bit_test(struct kunit *test) +{ + struct avd_ctx *ctx = avd_fmt_test_ctx(test, AVD_IMG_FMT_420_10BIT, + V4L2_PIX_FMT_P010); + + KUNIT_EXPECT_EQ(test, + avd_check_run_image_fmt(ctx, AVD_IMG_FMT_420_10BIT), 0); + KUNIT_EXPECT_EQ(test, + avd_check_run_image_fmt(ctx, AVD_IMG_FMT_420_8BIT), + -EINVAL); +} + +static void avd_fmt_422_test(struct kunit *test) +{ + struct avd_ctx *ctx = avd_fmt_test_ctx(test, AVD_IMG_FMT_422_8BIT, + V4L2_PIX_FMT_NV16); + + KUNIT_EXPECT_EQ(test, + avd_check_run_image_fmt(ctx, AVD_IMG_FMT_422_8BIT), 0); + KUNIT_EXPECT_EQ(test, + avd_check_run_image_fmt(ctx, AVD_IMG_FMT_420_8BIT), + -EINVAL); + KUNIT_EXPECT_EQ(test, + avd_check_run_image_fmt(ctx, AVD_IMG_FMT_422_10BIT), + -EINVAL); +} + +/* Compressed storage laid out for another size does not fit either. */ +static void avd_fmt_comp_mismatch_test(struct kunit *test) +{ + struct avd_ctx *ctx = avd_fmt_test_ctx(test, AVD_IMG_FMT_420_8BIT, + V4L2_PIX_FMT_NV12); + + fill_comp(&ctx->comp, AVD_IMG_FMT_420_8BIT, 32, 32); + KUNIT_EXPECT_EQ(test, + avd_check_run_image_fmt(ctx, AVD_IMG_FMT_420_8BIT), + -EINVAL); +} + +/* Scratch sized at STREAMON for 8 bits does not take a 10-bit picture. */ +static void avd_fmt_scratch_depth_test(struct kunit *test) +{ + struct avd_ctx *ctx = avd_fmt_test_ctx(test, AVD_IMG_FMT_420_10BIT, + V4L2_PIX_FMT_P010); + + ctx->scratch_sized = true; + ctx->scratch_fmt = AVD_IMG_FMT_ANY; + KUNIT_EXPECT_EQ(test, + avd_check_run_image_fmt(ctx, AVD_IMG_FMT_420_10BIT), + -EINVAL); + ctx->scratch_fmt = AVD_IMG_FMT_420_10BIT; + KUNIT_EXPECT_EQ(test, + avd_check_run_image_fmt(ctx, AVD_IMG_FMT_420_10BIT), 0); + /* Codecs that size no scratch from the format are not limited. */ + ctx->scratch_sized = false; + ctx->scratch_fmt = AVD_IMG_FMT_ANY; + KUNIT_EXPECT_EQ(test, + avd_check_run_image_fmt(ctx, AVD_IMG_FMT_420_10BIT), 0); +} + +static struct kunit_case avd_fmt_test_cases[] = { + KUNIT_CASE(avd_fmt_any_test), + KUNIT_CASE(avd_fmt_any_p010_test), + KUNIT_CASE(avd_fmt_10bit_test), + KUNIT_CASE(avd_fmt_422_test), + KUNIT_CASE(avd_fmt_comp_mismatch_test), + KUNIT_CASE(avd_fmt_scratch_depth_test), + {} +}; + +static struct kunit_suite avd_fmt_test_suite = { + .name = "apple-avd-fmt", + .test_cases = avd_fmt_test_cases, +}; + +kunit_test_suite(avd_fmt_test_suite); diff --git a/drivers/media/platform/apple/avd/avd-h264-test.c b/drivers/media/platform/apple/avd/avd-h264-test.c index 54d2114f45e98..3b7fec1d7b51b 100644 --- a/drivers/media/platform/apple/avd/avd-h264-test.c +++ b/drivers/media/platform/apple/avd/avd-h264-test.c @@ -5,6 +5,7 @@ #include #include +#include #include "avd.h" @@ -107,6 +108,79 @@ static void avd_slice_fits_test(struct kunit *test) c->fits); } +/* The H.264 OUTPUT frame size limits in avd-v4l2.c */ +static const struct v4l2_frmsize_stepwise avd_test_h264_frmsize = { + .min_width = 64, .max_width = 4096, .step_width = 64, + .min_height = 64, .max_height = 4096, .step_height = 16, +}; + +struct avd_sps_size_case { + const char *desc; + /* OUTPUT format as the client asks for it */ + u32 width; + u32 height; + u16 width_mbs; + u16 height_map_units; + int ret; +}; + +static const struct avd_sps_size_case avd_sps_size_cases[] = { + { "1080p, OUTPUT 1920x1080", 1920, 1080, 120, 68, 0 }, + { "1080p, OUTPUT 1920x1088", 1920, 1088, 120, 68, 0 }, + { "720p", 1280, 720, 80, 45, 0 }, + { "720p, one row over", 1280, 720, 80, 46, -EINVAL }, + { "720p, one column over", 1280, 720, 81, 45, -EINVAL }, + { "picture smaller than the format", 1920, 1088, 40, 30, 0 }, + { "64x64, matching picture", 64, 64, 4, 4, 0 }, + { "64x64, 4096x4096 picture", 64, 64, 256, 256, -EINVAL }, + { "64x64, largest SPS size", 64, 64, U16_MAX, U16_MAX, -EINVAL }, +}; + +KUNIT_ARRAY_PARAM_DESC(avd_sps_size, avd_sps_size_cases, desc); + +static void avd_sps_size_test(struct kunit *test) +{ + const struct avd_sps_size_case *c = test->param_value; + struct v4l2_ctrl_h264_sps sps = { + .chroma_format_idc = 1, + .pic_width_in_mbs_minus1 = c->width_mbs - 1, + .pic_height_in_map_units_minus1 = c->height_map_units - 1, + .flags = V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY, + }; + struct avd_ctx *ctx; + u32 w = c->width, h = c->height; + + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, ctx); + /* As S_FMT(OUTPUT) does */ + v4l2_apply_frmsize_constraints(&w, &h, &avd_test_h264_frmsize); + ctx->coded_fmt.fmt.pix_mp.width = w; + ctx->coded_fmt.fmt.pix_mp.height = h; + + KUNIT_EXPECT_EQ(test, avd_h264_validate_sps(ctx, &sps), c->ret); +} + +static void avd_start_slices_test(struct kunit *test) +{ + struct v4l2_ctrl_h264_sps sps = { + .flags = V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY, + }; + + /* One slice per row */ + sps.pic_width_in_mbs_minus1 = 119; + sps.pic_height_in_map_units_minus1 = 67; + KUNIT_EXPECT_EQ(test, avd_h264_start_slices(&sps), 68); + + /* No more rows than macroblocks, and never more than the table */ + sps.pic_width_in_mbs_minus1 = 0; + sps.pic_height_in_map_units_minus1 = U16_MAX; + KUNIT_EXPECT_LE(test, avd_h264_start_slices(&sps), AVD_TEST_MAX_SLICES); + sps.pic_width_in_mbs_minus1 = U16_MAX; + KUNIT_EXPECT_LE(test, avd_h264_start_slices(&sps), AVD_TEST_MAX_SLICES); + sps.flags = 0; + KUNIT_EXPECT_LE(test, avd_h264_start_slices(&sps), AVD_TEST_MAX_SLICES); +} + static void avd_scaling_flat_test(struct kunit *test) { struct v4l2_ctrl_h264_pps pps = {}; @@ -134,6 +208,8 @@ 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), KUNIT_CASE_PARAM(avd_slice_fits_test, avd_slice_fits_gen_params), + KUNIT_CASE_PARAM(avd_sps_size_test, avd_sps_size_gen_params), + KUNIT_CASE(avd_start_slices_test), {} }; diff --git a/drivers/media/platform/apple/avd/avd-h264.c b/drivers/media/platform/apple/avd/avd-h264.c index db4830e7aca22..73d86270ca80c 100644 --- a/drivers/media/platform/apple/avd/avd-h264.c +++ b/drivers/media/platform/apple/avd/avd-h264.c @@ -102,6 +102,16 @@ struct avd_h264_ctx { struct avd_bank bank[AVD_H264_BANKS]; /* bank of the picture being built */ struct avd_bank *b; + + /* What the run being built took from the controls, see avd_ctrl_copy(). */ + struct { + struct v4l2_ctrl_h264_decode_params decode_params; + struct v4l2_ctrl_h264_sps sps; + struct v4l2_ctrl_h264_pps pps; + struct v4l2_ctrl_h264_slice_params slice_params; + struct v4l2_ctrl_h264_scaling_matrix scaling_matrix; + struct v4l2_ctrl_h264_pred_weights pred_weights; + } params; }; /* ffmpeg submits wrong timestamp, use first_mb_in_slice as a workaround */ @@ -672,9 +682,41 @@ static int avd_h264_validate_slice(struct avd_ctx *ctx, return 0; } -static int avd_h264_validate_sps(struct avd_ctx *ctx, - const struct v4l2_ctrl_h264_sps *sps) +static enum avd_image_fmt +avd_h264_sps_image_fmt(const struct v4l2_ctrl_h264_sps *sps) +{ + if (sps->bit_depth_luma_minus8 == 0) { + if (sps->chroma_format_idc == 2) + return AVD_IMG_FMT_422_8BIT; + else + return AVD_IMG_FMT_420_8BIT; + } else if (sps->bit_depth_luma_minus8 == 2) { + if (sps->chroma_format_idc == 2) + return AVD_IMG_FMT_422_10BIT; + else + return AVD_IMG_FMT_420_10BIT; + } + + return AVD_IMG_FMT_ANY; +} + +/* + * H.264 7.4.2.1.1: the picture is PicWidthInMbs macroblocks wide and + * FrameHeightInMbs = (2 - frame_mbs_only_flag) * PicHeightInMapUnits high. + * The decoded buffers and the per-row scratch are sized from the OUTPUT + * format, which S_FMT rounds up to whole macroblocks, so a picture must fit + * in it. + */ +VISIBLE_IF_KUNIT int avd_h264_validate_sps(struct avd_ctx *ctx, + const struct v4l2_ctrl_h264_sps *sps) { + u32 width = (sps->pic_width_in_mbs_minus1 + 1) * 16; + u32 height = (sps->pic_height_in_map_units_minus1 + 1) * 16 * + (sps->flags & V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY ? 1 : 2); + + if (width > ctx->coded_fmt.fmt.pix_mp.width || + height > ctx->coded_fmt.fmt.pix_mp.height) + return -EINVAL; if (sps->chroma_format_idc > 2) /* Only 4:0:0, 4:2:0 and 4:2:2 are supported */ return -EINVAL; @@ -697,6 +739,7 @@ static int avd_h264_validate_sps(struct avd_ctx *ctx, return 0; } +EXPORT_SYMBOL_IF_KUNIT(avd_h264_validate_sps); /* Free the slices held for one bank's picture. */ static void avd_h264_drop_bank_slices(struct avd_ctx *ctx, struct avd_bank *bank) @@ -715,6 +758,7 @@ static void avd_h264_stop(struct avd_ctx *ctx) unsigned int i; ctx->max_frames = 1; + ctx->scratch_sized = false; if (!h264_ctx) return; @@ -736,6 +780,9 @@ static void avd_h264_stop(struct avd_ctx *ctx) ctx->priv = NULL; } +VISIBLE_IF_KUNIT size_t +avd_h264_start_slices(const struct v4l2_ctrl_h264_sps *sps); + static int avd_h264_start(struct avd_ctx *ctx) { struct avd_h264_ctx *h264_ctx; @@ -746,11 +793,11 @@ static int avd_h264_start(struct avd_ctx *ctx) if (!ctrl) return -EINVAL; - ret = avd_h264_validate_sps(ctx, ctrl->p_new.p_h264_sps); + ret = avd_h264_validate_sps(ctx, ctrl->p_cur.p_h264_sps); if (ret) return ret; - h264_ctx = kzalloc(sizeof(*h264_ctx), GFP_KERNEL); + h264_ctx = kzalloc(sizeof(*h264_ctx), GFP_KERNEL_ACCOUNT); if (!h264_ctx) return -ENOMEM; @@ -762,11 +809,10 @@ static int avd_h264_start(struct avd_ctx *ctx) for (unsigned int i = 0; i < h264_ctx->nbanks; i++) { struct avd_bank *bank = &h264_ctx->bank[i]; - /* assume one slice per row */ bank->alloc_slice_num = - ctrl->p_new.p_h264_sps->pic_height_in_map_units_minus1 + 1; + avd_h264_start_slices(ctrl->p_cur.p_h264_sps); bank->slices = kcalloc(bank->alloc_slice_num, - sizeof(*bank->slices), GFP_KERNEL); + sizeof(*bank->slices), GFP_KERNEL_ACCOUNT); if (!bank->slices) { ret = -ENOMEM; goto err_free_ctx; @@ -777,6 +823,9 @@ static int avd_h264_start(struct avd_ctx *ctx) goto err_free_ctx; } ctx->max_frames = h264_ctx->nbanks; + /* avd_h264_alloc_bufs() sized the scratch rows for this depth */ + ctx->scratch_fmt = ctx->image_fmt; + ctx->scratch_sized = true; return 0; @@ -785,28 +834,11 @@ static int avd_h264_start(struct avd_ctx *ctx) return ret; } -static void avd_h264_run_preamble(struct avd_ctx *ctx, struct avd_h264_run *run) +static int avd_h264_run_preamble(struct avd_ctx *ctx, struct avd_h264_run *run) { - struct v4l2_ctrl *ctrl; + struct avd_h264_ctx *h264_ctx = ctx->priv; u32 dst_len, mv_color_len; - ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, - V4L2_CID_STATELESS_H264_DECODE_PARAMS); - run->decode_params = ctrl ? ctrl->p_cur.p : NULL; - ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, V4L2_CID_STATELESS_H264_SPS); - run->sps = ctrl ? ctrl->p_cur.p : NULL; - ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, V4L2_CID_STATELESS_H264_PPS); - run->pps = ctrl ? ctrl->p_cur.p : NULL; - ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, - V4L2_CID_STATELESS_H264_SLICE_PARAMS); - run->slice_params = ctrl ? ctrl->p_cur.p : NULL; - ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, - V4L2_CID_STATELESS_H264_SCALING_MATRIX); - run->scaling_matrix = ctrl ? ctrl->p_cur.p : NULL; - ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, - V4L2_CID_STATELESS_H264_PRED_WEIGHTS); - run->pred_weights = ctrl ? ctrl->p_cur.p : NULL; - avd_run_preamble(ctx, &run->base); dst_len = run->base.bufs.dst->vb2_buf.planes[0].length; @@ -814,6 +846,43 @@ static void avd_h264_run_preamble(struct avd_ctx *ctx, struct avd_h264_run *run) mv_color_len = mv_color_size(ctx, fmt_width(ctx), fmt_height(ctx)); run->addresses.mv_color = run->base.y_out + (dst_len - mv_color_len); + + if (run->base.setup_err) + return run->base.setup_err; + + /* + * The checks in avd_h264_run() and the builders must see the same + * parameters, and the controls can be set again at any time: use + * copies. + */ + if (avd_ctrl_copy(ctx, V4L2_CID_STATELESS_H264_DECODE_PARAMS, + &h264_ctx->params.decode_params, + sizeof(h264_ctx->params.decode_params)) || + avd_ctrl_copy(ctx, V4L2_CID_STATELESS_H264_SPS, + &h264_ctx->params.sps, + sizeof(h264_ctx->params.sps)) || + avd_ctrl_copy(ctx, V4L2_CID_STATELESS_H264_PPS, + &h264_ctx->params.pps, + sizeof(h264_ctx->params.pps)) || + avd_ctrl_copy(ctx, V4L2_CID_STATELESS_H264_SLICE_PARAMS, + &h264_ctx->params.slice_params, + sizeof(h264_ctx->params.slice_params)) || + avd_ctrl_copy(ctx, V4L2_CID_STATELESS_H264_SCALING_MATRIX, + &h264_ctx->params.scaling_matrix, + sizeof(h264_ctx->params.scaling_matrix)) || + avd_ctrl_copy(ctx, V4L2_CID_STATELESS_H264_PRED_WEIGHTS, + &h264_ctx->params.pred_weights, + sizeof(h264_ctx->params.pred_weights))) + return -EINVAL; + + run->decode_params = &h264_ctx->params.decode_params; + run->sps = &h264_ctx->params.sps; + run->pps = &h264_ctx->params.pps; + run->slice_params = &h264_ctx->params.slice_params; + run->scaling_matrix = &h264_ctx->params.scaling_matrix; + run->pred_weights = &h264_ctx->params.pred_weights; + + return 0; } static int avd_h264_realloc_slices(struct avd_ctx *ctx, size_t max_slices) @@ -854,6 +923,7 @@ static int avd_h264_realloc_slices(struct avd_ctx *ctx, size_t max_slices) memcpy(ctx->job.segments, tmp, sizeof(*ctx->job.segments) * (ctx->job.num + 1)); + ctx->job.max = alloc_slice_num + 1; kvfree(tmp); return 0; @@ -922,6 +992,15 @@ static size_t avd_h264_max_slices(const struct v4l2_ctrl_h264_sps *sps) return min_t(size_t, mbs, MAX_SLICES); } +/* Slice table entries made at STREAMON: one slice per row to begin with. */ +VISIBLE_IF_KUNIT size_t +avd_h264_start_slices(const struct v4l2_ctrl_h264_sps *sps) +{ + return min_t(size_t, sps->pic_height_in_map_units_minus1 + 1, + avd_h264_max_slices(sps)); +} +EXPORT_SYMBOL_IF_KUNIT(avd_h264_start_slices); + /* * Coded data held for one picture. A picture of PCM macroblocks is about the * size of the decoded one and emulation prevention adds at most one byte for @@ -995,19 +1074,15 @@ static int avd_h264_run(struct avd_ctx *ctx) h264_ctx->b->slice_num : 0; int ret; - avd_h264_run_preamble(ctx, &run); - - if (run.base.setup_err) { - ret = run.base.setup_err; - goto err_free_jobs; - } - if (!run.sps || !run.pps || !run.slice_params || !run.decode_params) { - ret = -EINVAL; + ret = avd_h264_run_preamble(ctx, &run); + if (ret) goto err_free_jobs; - } /* Recheck controls applied from this request before building instructions. */ ret = avd_h264_validate_sps(ctx, run.sps); + if (!ret) + ret = avd_check_run_image_fmt(ctx, + avd_h264_sps_image_fmt(run.sps)); if (!ret) ret = avd_h264_validate_pps(ctx, run.pps); if (!ret) @@ -1166,24 +1241,10 @@ static void avd_h264_done(struct avd_ctx *ctx, struct vb2_v4l2_buffer *src_buf, static enum avd_image_fmt avd_h264_get_image_fmt(struct avd_ctx *ctx, struct v4l2_ctrl *ctrl) { - const struct v4l2_ctrl_h264_sps *sps = ctrl->p_new.p_h264_sps; - if (ctrl->id != V4L2_CID_STATELESS_H264_SPS) return AVD_IMG_FMT_ANY; - if (sps->bit_depth_luma_minus8 == 0) { - if (sps->chroma_format_idc == 2) - return AVD_IMG_FMT_422_8BIT; - else - return AVD_IMG_FMT_420_8BIT; - } else if (sps->bit_depth_luma_minus8 == 2) { - if (sps->chroma_format_idc == 2) - return AVD_IMG_FMT_422_10BIT; - else - return AVD_IMG_FMT_420_10BIT; - } - - return AVD_IMG_FMT_ANY; + return avd_h264_sps_image_fmt(ctrl->p_new.p_h264_sps); } static void avd_h264_adjust_decoded_fmt(struct avd_ctx *ctx, diff --git a/drivers/media/platform/apple/avd/avd-hevc-test.c b/drivers/media/platform/apple/avd/avd-hevc-test.c new file mode 100644 index 0000000000000..907c0c8093708 --- /dev/null +++ b/drivers/media/platform/apple/avd/avd-hevc-test.c @@ -0,0 +1,384 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit tests for the HEVC picture, tile and slice checks of the Apple video + * decoder. + */ + +#include + +#include "avd.h" + +/* 64x64 luma samples in 16x16 CTBs: 4x4 CTBs. */ +static void avd_hevc_test_sps(struct v4l2_ctrl_hevc_sps *sps) +{ + *sps = (struct v4l2_ctrl_hevc_sps) { + .pic_width_in_luma_samples = 64, + .pic_height_in_luma_samples = 64, + .log2_min_luma_coding_block_size_minus3 = 0, + .log2_diff_max_min_luma_coding_block_size = 1, + }; +} + +struct avd_hevc_geometry_case { + const char *desc; + u16 width; + u16 height; + u8 min_cb_minus3; + u8 diff; + int ret; +}; + +static const struct avd_hevc_geometry_case avd_hevc_geometry_cases[] = { + { "16x16 CTBs", 64, 64, 0, 1, 0 }, + { "32x32 CTBs", 1920, 1080, 0, 2, 0 }, + { "64x64 CTBs", 4096, 2160, 0, 3, 0 }, + { "64x64 CTBs from 16x16 coding blocks", 4096, 2160, 1, 2, 0 }, + { "8x8 CTBs", 64, 64, 0, 0, -EINVAL }, + { "128x128 CTBs", 64, 64, 0, 4, -EINVAL }, + { "coding block size beyond any CTB", 64, 64, 255, 255, -EINVAL }, + { "zero width", 0, 64, 0, 1, -EINVAL }, + { "zero height", 64, 0, 0, 1, -EINVAL }, +}; + +KUNIT_ARRAY_PARAM_DESC(avd_hevc_geometry, avd_hevc_geometry_cases, desc); + +static void avd_hevc_geometry_test(struct kunit *test) +{ + const struct avd_hevc_geometry_case *c = test->param_value; + struct v4l2_ctrl_hevc_sps sps = { + .pic_width_in_luma_samples = c->width, + .pic_height_in_luma_samples = c->height, + .log2_min_luma_coding_block_size_minus3 = c->min_cb_minus3, + .log2_diff_max_min_luma_coding_block_size = c->diff, + }; + + KUNIT_EXPECT_EQ(test, avd_hevc_check_sps_geometry(&sps), c->ret); +} + +struct avd_hevc_tiles_case { + const char *desc; + bool uniform; + u8 cols_minus1; + u8 rows_minus1; + u8 col_width_minus1[2]; + u8 row_height_minus1[2]; + int ret; + /* tile id of every CTB in tile scan order, if accepted */ + u32 ids[16]; +}; + +static const struct avd_hevc_tiles_case avd_hevc_tiles_cases[] = { + { "explicit 2x2 tiles", false, 1, 1, { 0 }, { 1 }, 0, + { 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 3, 3 } }, + { "explicit 3x1 tiles", false, 2, 0, { 1, 0 }, { 0 }, 0, + { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2 } }, + { "uniform 3x1 tiles", true, 2, 0, { 0 }, { 0 }, 0, + { 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2 } }, + { "uniform 4x4 tiles", true, 3, 3, { 0 }, { 0 }, 0, + { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } }, + { "explicit columns leave the last one empty", false, 1, 0, { 3 }, + { 0 }, -EINVAL }, + { "explicit columns wider than the picture", false, 1, 0, { 4 }, + { 0 }, -EINVAL }, + { "explicit columns far wider than the picture", false, 2, 0, + { 200, 200 }, { 0 }, -EINVAL }, + { "explicit rows taller than the picture", false, 0, 1, { 0 }, + { 4 }, -EINVAL }, + { "explicit rows leave the last one empty", false, 0, 2, { 0 }, + { 1, 1 }, -EINVAL }, + { "uniform, more columns than CTBs", true, 4, 0, { 0 }, { 0 }, + -EINVAL }, + { "uniform, more rows than CTBs", true, 0, 4, { 0 }, { 0 }, + -EINVAL }, +}; + +KUNIT_ARRAY_PARAM_DESC(avd_hevc_tiles, avd_hevc_tiles_cases, desc); + +static void avd_hevc_tiles_test(struct kunit *test) +{ + const struct avd_hevc_tiles_case *c = test->param_value; + struct v4l2_ctrl_hevc_sps sps; + struct v4l2_ctrl_hevc_pps pps = { + .flags = V4L2_HEVC_PPS_FLAG_TILES_ENABLED | + (c->uniform ? V4L2_HEVC_PPS_FLAG_UNIFORM_SPACING : 0), + .num_tile_columns_minus1 = c->cols_minus1, + .num_tile_rows_minus1 = c->rows_minus1, + }; + u32 ids[16]; + + avd_hevc_test_sps(&sps); + memcpy(pps.column_width_minus1, c->col_width_minus1, + sizeof(c->col_width_minus1)); + memcpy(pps.row_height_minus1, c->row_height_minus1, + sizeof(c->row_height_minus1)); + + KUNIT_EXPECT_EQ(test, avd_hevc_check_tiles(&sps, &pps), c->ret); + + /* The table builder must refuse the same layouts, before any write. */ + memset(ids, 0xff, sizeof(ids)); + KUNIT_ASSERT_EQ(test, + avd_hevc_kunit_compute_tiles(&sps, &pps, ids, + ARRAY_SIZE(ids)), + c->ret); + if (!c->ret) + KUNIT_EXPECT_MEMEQ(test, ids, c->ids, sizeof(ids)); +} + +/* + * More tile columns or rows than the PPS control has size entries for are + * refused by the check itself, even for a picture wide or tall enough. + */ +static void avd_hevc_tiles_array_test(struct kunit *test) +{ + struct v4l2_ctrl_hevc_sps sps; + struct v4l2_ctrl_hevc_pps pps = { + .flags = V4L2_HEVC_PPS_FLAG_TILES_ENABLED | + V4L2_HEVC_PPS_FLAG_UNIFORM_SPACING, + }; + + avd_hevc_test_sps(&sps); + sps.pic_width_in_luma_samples = 8192; + sps.pic_height_in_luma_samples = 4320; + + pps.num_tile_columns_minus1 = ARRAY_SIZE(pps.column_width_minus1) - 1; + pps.num_tile_rows_minus1 = ARRAY_SIZE(pps.row_height_minus1) - 1; + KUNIT_EXPECT_EQ(test, avd_hevc_check_tiles(&sps, &pps), 0); + + pps.num_tile_columns_minus1 = ARRAY_SIZE(pps.column_width_minus1); + KUNIT_EXPECT_EQ(test, avd_hevc_check_tiles(&sps, &pps), -EINVAL); + + pps.num_tile_columns_minus1 = ARRAY_SIZE(pps.column_width_minus1) - 1; + pps.num_tile_rows_minus1 = ARRAY_SIZE(pps.row_height_minus1); + KUNIT_EXPECT_EQ(test, avd_hevc_check_tiles(&sps, &pps), -EINVAL); +} + +static void avd_hevc_no_tiles_test(struct kunit *test) +{ + struct v4l2_ctrl_hevc_sps sps; + struct v4l2_ctrl_hevc_pps pps = {}; + u32 ids[16]; + + avd_hevc_test_sps(&sps); + KUNIT_EXPECT_EQ(test, avd_hevc_check_tiles(&sps, &pps), 0); + KUNIT_EXPECT_EQ(test, + avd_hevc_kunit_compute_tiles(&sps, &pps, ids, + ARRAY_SIZE(ids)), + 0); +} + +struct avd_hevc_slices_case { + const char *desc; + bool tiles; + u8 type; + u32 flags; + u32 addr; + u8 col_idx; + u8 l0_last; + u8 l0_ref; + u32 bytes; + u32 data_offset; + u32 num_entry_points; + u32 entry_point; + u32 available_entry_points; + u32 payload; + int ret; +}; + +#define AVD_TMVP V4L2_HEVC_SLICE_PARAMS_FLAG_SLICE_TEMPORAL_MVP_ENABLED + +static const struct avd_hevc_slices_case avd_hevc_slices_cases[] = { + { "I slice at the first CTB", .type = V4L2_HEVC_SLICE_TYPE_I, + .bytes = 100, .data_offset = 10, .payload = 100 }, + { "I slice at the last CTB", .type = V4L2_HEVC_SLICE_TYPE_I, + .addr = 15, .bytes = 100, .data_offset = 10, .payload = 100 }, + { "slice address past the picture", .type = V4L2_HEVC_SLICE_TYPE_I, + .addr = 16, .bytes = 100, .payload = 100, .ret = -EINVAL }, + { "slice address far past the picture", .type = V4L2_HEVC_SLICE_TYPE_I, + .addr = U32_MAX, .bytes = 100, .payload = 100, .ret = -EINVAL }, + { "slice data ends one byte after the payload", + .type = V4L2_HEVC_SLICE_TYPE_I, .bytes = 100, .data_offset = 10, + .payload = 99, .ret = -EINVAL }, + { "slice header longer than the slice", .type = V4L2_HEVC_SLICE_TYPE_I, + .bytes = 10, .data_offset = 11, .payload = 100, .ret = -EINVAL }, + { "payload beyond the 31-bit range", .type = V4L2_HEVC_SLICE_TYPE_I, + .bytes = 100, .payload = U32_MAX, .ret = -EINVAL }, + { "P slice, collocated picture in the list", + .type = V4L2_HEVC_SLICE_TYPE_P, .flags = AVD_TMVP, .col_idx = 1, + .l0_last = 1, .l0_ref = 15, .bytes = 100, .payload = 100 }, + { "P slice, collocated index past the active list", + .type = V4L2_HEVC_SLICE_TYPE_P, .flags = AVD_TMVP, .col_idx = 2, + .l0_last = 1, .bytes = 100, .payload = 100, .ret = -EINVAL }, + { "P slice without TMVP, unused collocated index", + .type = V4L2_HEVC_SLICE_TYPE_P, .col_idx = 2, .l0_last = 1, + .bytes = 100, .payload = 100 }, + { "P slice, collocated index past the list array", + .type = V4L2_HEVC_SLICE_TYPE_P, .col_idx = 16, .l0_last = 15, + .bytes = 100, .payload = 100, .ret = -EINVAL }, + { "P slice, missing reference marked past the DPB", + .type = V4L2_HEVC_SLICE_TYPE_P, .l0_ref = 255, .bytes = 100, + .payload = 100 }, + { "B slice, missing L1 reference marked past the DPB", + .type = V4L2_HEVC_SLICE_TYPE_B, .l0_ref = 255, .bytes = 100, + .payload = 100 }, + { "two tiles, entry point inside the slice", .tiles = true, + .type = V4L2_HEVC_SLICE_TYPE_I, .bytes = 100, .data_offset = 10, + .num_entry_points = 1, .entry_point = 40, + .available_entry_points = 1, .payload = 100 }, + { "two tiles, entry point past the payload", .tiles = true, + .type = V4L2_HEVC_SLICE_TYPE_I, .bytes = 100, .data_offset = 10, + .num_entry_points = 1, .entry_point = 91, + .available_entry_points = 1, .payload = 100, .ret = -EINVAL }, + { "two tiles, entry point far past the payload", .tiles = true, + .type = V4L2_HEVC_SLICE_TYPE_I, .bytes = 100, .data_offset = 10, + .num_entry_points = 1, .entry_point = U32_MAX, + .available_entry_points = 1, .payload = 100, .ret = -EINVAL }, + { "two tiles, more entry points than given", .tiles = true, + .type = V4L2_HEVC_SLICE_TYPE_I, .bytes = 100, .data_offset = 10, + .num_entry_points = 2, .entry_point = 40, + .available_entry_points = 1, .payload = 100, .ret = -EINVAL }, +}; + +KUNIT_ARRAY_PARAM_DESC(avd_hevc_slices, avd_hevc_slices_cases, desc); + +static void avd_hevc_slices_test(struct kunit *test) +{ + const struct avd_hevc_slices_case *c = test->param_value; + struct v4l2_ctrl_hevc_decode_params *decode; + struct v4l2_ctrl_hevc_slice_params *sl; + struct v4l2_ctrl_hevc_sps sps; + struct v4l2_ctrl_hevc_pps pps = {}; + u32 ep[2] = { c->entry_point, c->entry_point }; + + decode = kunit_kzalloc(test, sizeof(*decode), GFP_KERNEL); + sl = kunit_kzalloc(test, sizeof(*sl), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, decode); + KUNIT_ASSERT_NOT_NULL(test, sl); + + avd_hevc_test_sps(&sps); + if (c->tiles) { + pps.flags = V4L2_HEVC_PPS_FLAG_TILES_ENABLED | + V4L2_HEVC_PPS_FLAG_UNIFORM_SPACING; + pps.num_tile_columns_minus1 = 1; + } + decode->num_active_dpb_entries = 2; + sl->slice_type = c->type; + sl->flags = c->flags; + sl->slice_segment_addr = c->addr; + sl->collocated_ref_idx = c->col_idx; + sl->num_ref_idx_l0_active_minus1 = c->l0_last; + sl->num_ref_idx_l1_active_minus1 = c->l0_last; + memset(sl->ref_idx_l0, c->l0_ref, sizeof(sl->ref_idx_l0)); + memset(sl->ref_idx_l1, c->l0_ref, sizeof(sl->ref_idx_l1)); + sl->bit_size = c->bytes * 8; + sl->data_byte_offset = c->data_offset; + sl->num_entry_point_offsets = c->num_entry_points; + + KUNIT_EXPECT_EQ(test, + avd_hevc_check_slices(&sps, &pps, decode, sl, 1, ep, + c->available_entry_points, + c->payload), + c->ret); +} + +/* A slice only fits after the ones before it. */ +static void avd_hevc_two_slices_test(struct kunit *test) +{ + struct v4l2_ctrl_hevc_decode_params *decode; + struct v4l2_ctrl_hevc_slice_params *sl; + struct v4l2_ctrl_hevc_sps sps; + struct v4l2_ctrl_hevc_pps pps = {}; + u32 ep = 0; + + decode = kunit_kzalloc(test, sizeof(*decode), GFP_KERNEL); + sl = kunit_kcalloc(test, 2, sizeof(*sl), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, decode); + KUNIT_ASSERT_NOT_NULL(test, sl); + + avd_hevc_test_sps(&sps); + sl[0].slice_type = V4L2_HEVC_SLICE_TYPE_I; + sl[0].bit_size = 60 * 8; + sl[0].data_byte_offset = 5; + sl[1] = sl[0]; + sl[1].slice_segment_addr = 8; + + KUNIT_EXPECT_EQ(test, avd_hevc_check_slices(&sps, &pps, decode, sl, 2, + &ep, 1, 120), 0); + KUNIT_EXPECT_EQ(test, avd_hevc_check_slices(&sps, &pps, decode, sl, 2, + &ep, 1, 119), -EINVAL); +} + +/* + * The entry point offsets split a slice's data into its tiles; the last tile + * takes what the others leave. Offsets that add up to more than the slice + * holds leave it less than nothing, even when each one fits the payload. + */ +static void avd_hevc_entry_points_sum_test(struct kunit *test) +{ + struct v4l2_ctrl_hevc_decode_params *decode; + struct v4l2_ctrl_hevc_slice_params *sl; + struct v4l2_ctrl_hevc_sps sps; + struct v4l2_ctrl_hevc_pps pps = { + .flags = V4L2_HEVC_PPS_FLAG_TILES_ENABLED | + V4L2_HEVC_PPS_FLAG_UNIFORM_SPACING, + .num_tile_columns_minus1 = 3, + }; + u32 ep[6] = { 20, 20, 20, 20, 20, 20 }; + + decode = kunit_kzalloc(test, sizeof(*decode), GFP_KERNEL); + sl = kunit_kcalloc(test, 2, sizeof(*sl), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, decode); + KUNIT_ASSERT_NOT_NULL(test, sl); + + avd_hevc_test_sps(&sps); + sl[0].slice_type = V4L2_HEVC_SLICE_TYPE_I; + sl[0].bit_size = 100 * 8; + sl[0].data_byte_offset = 10; + sl[0].num_entry_point_offsets = 3; + sl[1] = sl[0]; + sl[1].slice_segment_addr = 8; + + /* 10 header bytes, three tiles of 20 bytes and a last one of 30. */ + KUNIT_EXPECT_EQ(test, avd_hevc_check_slices(&sps, &pps, decode, sl, 1, + ep, 3, 100), 0); + KUNIT_EXPECT_EQ(test, avd_hevc_check_slices(&sps, &pps, decode, sl, 2, + ep, 6, 200), 0); + + /* One byte more than the 90 data bytes of the slice. */ + ep[0] = 30; + ep[1] = 30; + ep[2] = 31; + KUNIT_EXPECT_EQ(test, avd_hevc_check_slices(&sps, &pps, decode, sl, 1, + ep, 3, 100), -EINVAL); + /* + * The same in the first of two slices: every offset and their sum + * stay inside the payload, but not inside the slice. + */ + KUNIT_EXPECT_EQ(test, avd_hevc_check_slices(&sps, &pps, decode, sl, 2, + ep, 6, 200), -EINVAL); + + /* And in the second slice. */ + ep[0] = 20; + ep[1] = 20; + ep[2] = 20; + ep[5] = 51; + KUNIT_EXPECT_EQ(test, avd_hevc_check_slices(&sps, &pps, decode, sl, 2, + ep, 6, 200), -EINVAL); +} + +static struct kunit_case avd_hevc_test_cases[] = { + KUNIT_CASE_PARAM(avd_hevc_geometry_test, avd_hevc_geometry_gen_params), + KUNIT_CASE_PARAM(avd_hevc_tiles_test, avd_hevc_tiles_gen_params), + KUNIT_CASE(avd_hevc_tiles_array_test), + KUNIT_CASE(avd_hevc_no_tiles_test), + KUNIT_CASE_PARAM(avd_hevc_slices_test, avd_hevc_slices_gen_params), + KUNIT_CASE(avd_hevc_two_slices_test), + KUNIT_CASE(avd_hevc_entry_points_sum_test), + {} +}; + +static struct kunit_suite avd_hevc_test_suite = { + .name = "apple-avd-hevc", + .test_cases = avd_hevc_test_cases, +}; + +kunit_test_suite(avd_hevc_test_suite); diff --git a/drivers/media/platform/apple/avd/avd-hevc.c b/drivers/media/platform/apple/avd/avd-hevc.c index 04d43b759f0d7..f6fe7f7b3d3a3 100644 --- a/drivers/media/platform/apple/avd/avd-hevc.c +++ b/drivers/media/platform/apple/avd/avd-hevc.c @@ -13,6 +13,7 @@ * Tomasz Figa */ +#include #include #include "avd.h" @@ -598,8 +599,9 @@ static void stream_slice_mv(struct avd_ctx *ctx, struct avd_hevc_run *run, { const struct v4l2_ctrl_hevc_decode_params *decode = run->decode; struct avd_decoded_buffer *dst, *ref; - bool ref_valid; + bool ref_valid, col_in_dpb; const u8 *ref_list; + u8 col; if (sl->slice_type == V4L2_HEVC_SLICE_TYPE_I) { push(AVD_OP_SL_REF | @@ -616,11 +618,17 @@ static void stream_slice_mv(struct avd_ctx *ctx, struct avd_hevc_run *run, sl->ref_idx_l1; dst = vb2_to_avd_decoded_buf(&run->base.bufs.dst->vb2_buf); - ref = avd_get_ref_buf( - ctx, &dst->base.vb, - decode->dpb[ref_list[sl->collocated_ref_idx]].timestamp); + /* + * Clients mark a reference they do not have with an index past the + * DPB (0xff); there is no collocated picture to take MVs from then. + */ + col = ref_list[sl->collocated_ref_idx]; + col_in_dpb = col < ARRAY_SIZE(decode->dpb); + ref = col_in_dpb ? + avd_get_ref_buf(ctx, &dst->base.vb, decode->dpb[col].timestamp) : + dst; - ref_valid = !(sl->flags & + ref_valid = col_in_dpb && !(sl->flags & V4L2_HEVC_SLICE_PARAMS_FLAG_DEPENDENT_SLICE_SEGMENT) && (sl->flags & V4L2_HEVC_SLICE_PARAMS_FLAG_SLICE_TEMPORAL_MVP_ENABLED) && @@ -661,8 +669,7 @@ static void stream_slice_mv(struct avd_ctx *ctx, struct avd_hevc_run *run, mv_color_size(fmt_width(ctx), fmt_height(ctx)), AVD_ALIGN); pusha(mv_color_addr, "slc_bd4_sps_tile_addr2_lsb8", - decode->dpb[ref_list[sl->collocated_ref_idx]] - .pic_order_cnt_val); + decode->dpb[col].pic_order_cnt_val); } } @@ -832,20 +839,25 @@ static void compute_rs_to_ts(struct avd_hevc_run *run, u32 pic_in_ctbs_size, } } -static void compute_tile_ids(struct avd_hevc_run *run, u32 pic_in_ctbs_width, - u32 *col_bd, u32 *row_bd, u32 *ctb_addr_rs_to_ts, - u32 *tile_ids) +static void compute_tile_ids(struct avd_hevc_run *run, u32 pic_in_ctbs_size, + u32 pic_in_ctbs_width, u32 *col_bd, u32 *row_bd, + u32 *ctb_addr_rs_to_ts, u32 *tile_ids) { const struct v4l2_ctrl_hevc_pps *pps = run->pps; int j, i, y, x; - u32 tile_idx; + u32 tile_idx, rs, ts; + for (j = 0, tile_idx = 0; j <= pps->num_tile_rows_minus1; j++) for (i = 0; i <= pps->num_tile_columns_minus1; i++, tile_idx++) for (y = row_bd[j]; y < row_bd[j + 1]; y++) - for (x = col_bd[i]; x < col_bd[i + 1]; x++) - tile_ids[ctb_addr_rs_to_ts - [y * pic_in_ctbs_width + - x]] = tile_idx; + for (x = col_bd[i]; x < col_bd[i + 1]; x++) { + rs = y * pic_in_ctbs_width + x; + if (rs >= pic_in_ctbs_size) + continue; + ts = ctb_addr_rs_to_ts[rs]; + if (ts < pic_in_ctbs_size) + tile_ids[ts] = tile_idx; + } } struct sl_ctx { @@ -952,6 +964,10 @@ static int stream_slices(struct avd_ctx *ctx, struct avd_hevc_run *run) row = pos / num_cols; col = pos % num_cols; + /* More tiles than the boundary tables describe */ + if (tiles_enabled && (slice_flag & NEW_TILE_ID) && + row + 1 >= ARRAY_SIZE(tile_info->row_bd)) + return -EINVAL; /* * not a fan of this, in JCT-VC-HEVC_V1 only TILES_B_Cisco_1 @@ -1022,14 +1038,9 @@ static void avd_hevc_adjust_decoded_fmt(struct avd_ctx *ctx, mv_color_size(pix_mp->width, pix_mp->height); } -static enum avd_image_fmt avd_hevc_get_image_fmt(struct avd_ctx *ctx, - struct v4l2_ctrl *ctrl) +static enum avd_image_fmt +avd_hevc_sps_image_fmt(const struct v4l2_ctrl_hevc_sps *sps) { - const struct v4l2_ctrl_hevc_sps *sps = ctrl->p_new.p_hevc_sps; - - if (ctrl->id != V4L2_CID_STATELESS_HEVC_SPS) - return AVD_IMG_FMT_ANY; - /* avd_hevc_validate_sps() refuses 4:4:4 and depths other than 8/10. */ if (sps->bit_depth_luma_minus8 == 0) { if (sps->chroma_format_idc == 2) @@ -1046,9 +1057,165 @@ static enum avd_image_fmt avd_hevc_get_image_fmt(struct avd_ctx *ctx, return AVD_IMG_FMT_ANY; } +static enum avd_image_fmt avd_hevc_get_image_fmt(struct avd_ctx *ctx, + struct v4l2_ctrl *ctrl) +{ + if (ctrl->id != V4L2_CID_STATELESS_HEVC_SPS) + return AVD_IMG_FMT_ANY; + + return avd_hevc_sps_image_fmt(ctrl->p_new.p_hevc_sps); +} + +/* + * The picture must have at least one sample in each direction and the CTB + * size must be one of those H.265 allows (CtbLog2SizeY 4 to 6, 7.4.3.2.1): + * the picture size in CTBs sizes the tile tables and divides slice addresses. + */ +VISIBLE_IF_KUNIT int +avd_hevc_check_sps_geometry(const struct v4l2_ctrl_hevc_sps *sps) +{ + unsigned int ctb_log2 = sps->log2_min_luma_coding_block_size_minus3 + 3 + + sps->log2_diff_max_min_luma_coding_block_size; + + if (!sps->pic_width_in_luma_samples || !sps->pic_height_in_luma_samples) + return -EINVAL; + if (ctb_log2 < 4 || ctb_log2 > 6) + return -EINVAL; + return 0; +} +EXPORT_SYMBOL_IF_KUNIT(avd_hevc_check_sps_geometry); + +static void avd_hevc_pic_in_ctbs(const struct v4l2_ctrl_hevc_sps *sps, + u32 *width, u32 *height) +{ + u32 ctb = 1 << (sps->log2_min_luma_coding_block_size_minus3 + 3 + + sps->log2_diff_max_min_luma_coding_block_size); + + *width = DIV_ROUND_UP(sps->pic_width_in_luma_samples, ctb); + *height = DIV_ROUND_UP(sps->pic_height_in_luma_samples, ctb); +} + +/* + * The tile columns and rows must cover the picture, each at least one CTB + * wide or high (H.265 7.4.3.3.1). Explicitly sized tiles give all but the + * last width and height; the last one takes the rest and must not be empty. + * avd_hevc_check_sps_geometry() must have accepted the SPS. + */ +VISIBLE_IF_KUNIT int avd_hevc_check_tiles(const struct v4l2_ctrl_hevc_sps *sps, + const struct v4l2_ctrl_hevc_pps *pps) +{ + u32 width, height, sum; + int i; + + if (!(pps->flags & V4L2_HEVC_PPS_FLAG_TILES_ENABLED)) + return 0; + + if (pps->num_tile_columns_minus1 >= ARRAY_SIZE(pps->column_width_minus1) || + pps->num_tile_rows_minus1 >= ARRAY_SIZE(pps->row_height_minus1)) + return -EINVAL; + + avd_hevc_pic_in_ctbs(sps, &width, &height); + if (pps->num_tile_columns_minus1 >= width || + pps->num_tile_rows_minus1 >= height) + return -EINVAL; + if (pps->flags & V4L2_HEVC_PPS_FLAG_UNIFORM_SPACING) + return 0; + + for (i = 0, sum = 0; i < pps->num_tile_columns_minus1; i++) + sum += pps->column_width_minus1[i] + 1; + if (sum >= width) + return -EINVAL; + + for (i = 0, sum = 0; i < pps->num_tile_rows_minus1; i++) + sum += pps->row_height_minus1[i] + 1; + if (sum >= height) + return -EINVAL; + + return 0; +} +EXPORT_SYMBOL_IF_KUNIT(avd_hevc_check_tiles); + +/* + * Check what the slice builder takes from the slice parameters before it + * uses them: the slice address must be a CTB of the picture, the collocated + * reference must be an entry of its reference list (H.265 7.4.7.1), and + * every slice segment's data must lie in the payload. The + * segment layout is the one stream_slices() programs. + * avd_hevc_check_sps_geometry() must have accepted the SPS. + */ +VISIBLE_IF_KUNIT int +avd_hevc_check_slices(const struct v4l2_ctrl_hevc_sps *sps, + const struct v4l2_ctrl_hevc_pps *pps, + const struct v4l2_ctrl_hevc_decode_params *decode, + const struct v4l2_ctrl_hevc_slice_params *slices, + u32 num_slices, const u32 *entry_points, + u32 num_entry_points, u32 payload) +{ + bool tiles = pps->flags & V4L2_HEVC_PPS_FLAG_TILES_ENABLED; + u32 width, height, ep = 0, s, i, to; + s64 offset = 0, seg_offset, size, next; + + if (payload > INT_MAX) + return -EINVAL; + avd_hevc_pic_in_ctbs(sps, &width, &height); + + for (s = 0; s < num_slices; s++) { + const struct v4l2_ctrl_hevc_slice_params *sl = &slices[s]; + s64 bytes = sl->bit_size / 8; + + if (sl->slice_segment_addr >= width * height) + return -EINVAL; + + if (sl->slice_type != V4L2_HEVC_SLICE_TYPE_I) { + bool l0 = sl->slice_type == V4L2_HEVC_SLICE_TYPE_P || + sl->flags & + V4L2_HEVC_SLICE_PARAMS_FLAG_COLLOCATED_FROM_L0; + u8 last = l0 ? sl->num_ref_idx_l0_active_minus1 : + sl->num_ref_idx_l1_active_minus1; + + if (sl->collocated_ref_idx >= ARRAY_SIZE(sl->ref_idx_l0)) + return -EINVAL; + if ((sl->flags & + V4L2_HEVC_SLICE_PARAMS_FLAG_SLICE_TEMPORAL_MVP_ENABLED) && + sl->collocated_ref_idx > last) + return -EINVAL; + } + + to = tiles ? sl->num_entry_point_offsets + 1 : 1; + if (tiles && sl->num_entry_point_offsets > + num_entry_points - ep) + return -EINVAL; + + seg_offset = 0; + for (i = 0; i < to; i++) { + if (to > 1 && i < sl->num_entry_point_offsets) { + size = entry_points[ep++]; + next = size; + } else if (to > 1) { + size = bytes - sl->data_byte_offset - seg_offset; + next = size + sl->data_byte_offset; + } else { + size = bytes - sl->data_byte_offset; + next = size + sl->data_byte_offset; + } + if (size < 0 || + offset + seg_offset + sl->data_byte_offset + size > + payload) + return -EINVAL; + seg_offset += next; + } + offset += bytes; + } + + return 0; +} +EXPORT_SYMBOL_IF_KUNIT(avd_hevc_check_slices); + static int avd_hevc_validate_sps(struct avd_ctx *ctx, const struct v4l2_ctrl_hevc_sps *sps) { + if (avd_hevc_check_sps_geometry(sps)) + return -EINVAL; if (sps->pic_width_in_luma_samples > ctx->coded_fmt.fmt.pix_mp.width || sps->pic_height_in_luma_samples > ctx->coded_fmt.fmt.pix_mp.height) return -EINVAL; @@ -1292,6 +1459,9 @@ static int avd_hevc_compute_tiles(struct avd_ctx *ctx, struct avd_hevc_run *run) tiles_enabled = !!(pps->flags & V4L2_HEVC_PPS_FLAG_TILES_ENABLED); + if (avd_hevc_check_tiles(sps, pps)) + return -EINVAL; + tile_info->ctb_addr_rs_to_ts = kzalloc( sizeof(*tile_info->ctb_addr_rs_to_ts) * pic_in_ctbs_size, GFP_KERNEL); @@ -1332,7 +1502,8 @@ static int avd_hevc_compute_tiles(struct avd_ctx *ctx, struct avd_hevc_run *run) tile_info->col_bd, tile_info->row_bd, tile_info->col_width, tile_info->row_height, tile_info->ctb_addr_rs_to_ts); - compute_tile_ids(run, pic_in_ctbs_width, tile_info->col_bd, + compute_tile_ids(run, pic_in_ctbs_size, pic_in_ctbs_width, + tile_info->col_bd, tile_info->row_bd, tile_info->ctb_addr_rs_to_ts, tile_info->tile_ids); @@ -1346,6 +1517,36 @@ static int avd_hevc_compute_tiles(struct avd_ctx *ctx, struct avd_hevc_run *run) return 0; } +#if IS_ENABLED(CONFIG_KUNIT) +/* Build the tile tables of a picture and copy out tile_ids in tile scan order. */ +int avd_hevc_kunit_compute_tiles(const struct v4l2_ctrl_hevc_sps *sps, + const struct v4l2_ctrl_hevc_pps *pps, + u32 *tile_ids, size_t n) +{ + struct avd_hevc_run *run; + u32 width, height; + int ret; + + run = kzalloc(sizeof(*run), GFP_KERNEL); + if (!run) + return -ENOMEM; + run->sps = sps; + run->pps = pps; + ret = avd_hevc_compute_tiles(NULL, run); + if (!ret) { + avd_hevc_pic_in_ctbs(sps, &width, &height); + memcpy(tile_ids, run->tile_info.tile_ids, + min_t(size_t, n, (size_t)width * height) * + sizeof(*tile_ids)); + } + kfree(run->tile_info.ctb_addr_rs_to_ts); + kfree(run->tile_info.tile_ids); + kfree(run); + return ret; +} +EXPORT_SYMBOL_IF_KUNIT(avd_hevc_kunit_compute_tiles); +#endif + /* * The slice builder below expects the GStreamer layout: the slices tile the * OUTPUT buffer as whole NALs, bit_size covers the whole NAL and @@ -1391,9 +1592,34 @@ static void avd_hevc_normalize_slices(struct avd_hevc_run *run, u32 payload) } } +/* + * A copy of a control's current value. The caller holds the control lock, + * so the control must have been looked up before (v4l2_ctrl_find() takes + * that lock too). + */ +static void *avd_hevc_ctrl_dup(struct v4l2_ctrl *ctrl) +{ + if (!ctrl) + return NULL; + return kmemdup(ctrl->p_cur.p, ctrl->elems * ctrl->elem_size, + GFP_KERNEL); +} + +static void avd_hevc_free_run(struct avd_hevc_run *run) +{ + kfree(run->tile_info.ctb_addr_rs_to_ts); + kfree(run->tile_info.tile_ids); + kfree(run->sl); + kfree(run->entry_point_offsets); + kfree(run->decode); + kfree(run->sps); + kfree(run->pps); + kfree(run->scaling_matrix); +} + static int avd_hevc_run_preamble(struct avd_ctx *ctx, struct avd_hevc_run *run) { - struct v4l2_ctrl *ctrl, *sl, *ep; + struct v4l2_ctrl *sl, *ep, *decode, *sps, *pps, *scaling; u32 dst_len, mv_color_len; if (!v4l2_ctrl_find(&ctx->ctrl_hdl, V4L2_CID_STATELESS_HEVC_SLICE_PARAMS) || @@ -1411,20 +1637,15 @@ static int avd_hevc_run_preamble(struct avd_ctx *ctx, struct avd_hevc_run *run) if (run->base.setup_err) return run->base.setup_err; - ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, - V4L2_CID_STATELESS_HEVC_DECODE_PARAMS); - run->decode = ctrl ? ctrl->p_cur.p : NULL; - ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, V4L2_CID_STATELESS_HEVC_SPS); - run->sps = ctrl ? ctrl->p_cur.p : NULL; - ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, V4L2_CID_STATELESS_HEVC_PPS); - run->pps = ctrl ? ctrl->p_cur.p : NULL; - ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, - V4L2_CID_STATELESS_HEVC_SCALING_MATRIX); - run->scaling_matrix = ctrl ? ctrl->p_cur.p : NULL; - sl = v4l2_ctrl_find(&ctx->ctrl_hdl, V4L2_CID_STATELESS_HEVC_SLICE_PARAMS); ep = v4l2_ctrl_find(&ctx->ctrl_hdl, V4L2_CID_STATELESS_HEVC_ENTRY_POINT_OFFSETS); + decode = v4l2_ctrl_find(&ctx->ctrl_hdl, + V4L2_CID_STATELESS_HEVC_DECODE_PARAMS); + sps = v4l2_ctrl_find(&ctx->ctrl_hdl, V4L2_CID_STATELESS_HEVC_SPS); + pps = v4l2_ctrl_find(&ctx->ctrl_hdl, V4L2_CID_STATELESS_HEVC_PPS); + scaling = v4l2_ctrl_find(&ctx->ctrl_hdl, + V4L2_CID_STATELESS_HEVC_SCALING_MATRIX); /* * Setting a larger dynamic array on a queued request reallocates the @@ -1432,15 +1653,24 @@ static int avd_hevc_run_preamble(struct avd_ctx *ctx, struct avd_hevc_run *run) * the run needs while holding the control lock. */ v4l2_ctrl_lock(sl); + /* + * A plain VIDIOC_S_EXT_CTRLS can also change the picture controls + * while the job is built. The checks below and the builder must see + * the same values, so take copies of those too. + */ + run->decode = avd_hevc_ctrl_dup(decode); + run->sps = avd_hevc_ctrl_dup(sps); + run->pps = avd_hevc_ctrl_dup(pps); + run->scaling_matrix = avd_hevc_ctrl_dup(scaling); run->num_slices = sl->elems; run->sl = kmemdup(sl->p_cur.p, sl->elems * sl->elem_size, GFP_KERNEL); run->num_entry_point_offsets = ep->elems; run->entry_point_offsets = kmemdup(ep->p_cur.p, ep->elems * ep->elem_size, GFP_KERNEL); v4l2_ctrl_unlock(sl); - if (!run->sl || !run->entry_point_offsets) { - kfree(run->sl); - kfree(run->entry_point_offsets); + if (!run->sl || !run->entry_point_offsets || !run->decode || + !run->sps || !run->pps || !run->scaling_matrix) { + avd_hevc_free_run(run); avd_run_postamble(ctx, &run->base); return -ENOMEM; } @@ -1459,6 +1689,7 @@ static int avd_hevc_run(struct avd_ctx *ctx) struct avd_hevc_ctx *hevc_ctx = ctx->priv; struct avd_hevc_run run = {}; struct avd_decoded_buffer *dst; + u32 payload; int ret; ret = avd_hevc_run_preamble(ctx, &run); @@ -1467,11 +1698,19 @@ static int avd_hevc_run(struct avd_ctx *ctx) /* Also validate controls applied from a queued request before submission. */ ret = avd_hevc_validate_sps(ctx, run.sps); + if (!ret) + ret = avd_check_run_image_fmt(ctx, + avd_hevc_sps_image_fmt(run.sps)); if (ret) goto complete; - avd_hevc_normalize_slices(&run, - vb2_get_plane_payload(&run.base.bufs.src->vb2_buf, 0)); + payload = vb2_get_plane_payload(&run.base.bufs.src->vb2_buf, 0); + avd_hevc_normalize_slices(&run, payload); + ret = avd_hevc_check_slices(run.sps, run.pps, run.decode, run.sl, + run.num_slices, run.entry_point_offsets, + run.num_entry_point_offsets, payload); + if (ret) + goto complete; dst = vb2_to_avd_decoded_buf(&run.base.bufs.dst->vb2_buf); update_dec_buf_info(dst, &run.sl[0]); @@ -1505,10 +1744,7 @@ static int avd_hevc_run(struct avd_ctx *ctx) /* The request was applied; complete it on every way out. */ avd_run_postamble(ctx, &run.base); done: - kfree(run.tile_info.ctb_addr_rs_to_ts); - kfree(run.tile_info.tile_ids); - kfree(run.sl); - kfree(run.entry_point_offsets); + avd_hevc_free_run(&run); return ret; } diff --git a/drivers/media/platform/apple/avd/avd-inst.h b/drivers/media/platform/apple/avd/avd-inst.h index adf670a79645c..d35efe68ebd51 100644 --- a/drivers/media/platform/apple/avd/avd-inst.h +++ b/drivers/media/platform/apple/avd/avd-inst.h @@ -169,7 +169,17 @@ static inline bool boolify(u32 v) static inline void push(struct avd_ctx *ctx, u32 inst) { struct avd_job *job = &ctx->job; - struct avd_segment *seg = &job->segments[job->num]; + struct avd_segment *seg; + + if (job->num >= job->max) { + job->overflow = true; + return; + } + seg = &job->segments[job->num]; + if (seg->num >= AVD_MAX_INST) { + job->overflow = true; + return; + } seg->instructions[seg->num++] = inst; } diff --git a/drivers/media/platform/apple/avd/avd-job-test.c b/drivers/media/platform/apple/avd/avd-job-test.c new file mode 100644 index 0000000000000..8263cddfd88ec --- /dev/null +++ b/drivers/media/platform/apple/avd/avd-job-test.c @@ -0,0 +1,85 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit tests for the instruction job table of the Apple video decoder. + */ + +#include + +#include "avd.h" +#include "avd-inst.h" + +static void avd_job_free(void *data) +{ + struct avd_ctx *ctx = data; + + kvfree(ctx->job.segments); +} + +static struct avd_ctx *avd_job_test_ctx(struct kunit *test, size_t segments) +{ + struct avd_ctx *ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + + KUNIT_ASSERT_NOT_NULL(test, ctx); + KUNIT_ASSERT_EQ(test, avd_init_job(ctx, AVD_CODEC_H264, segments), 0); + KUNIT_ASSERT_EQ(test, kunit_add_action_or_reset(test, avd_job_free, ctx), + 0); + KUNIT_EXPECT_EQ(test, ctx->job.max, segments); + KUNIT_EXPECT_FALSE(test, ctx->job.overflow); + return ctx; +} + +/* A full segment takes no more instructions. */ +static void avd_job_segment_full_test(struct kunit *test) +{ + struct avd_ctx *ctx = avd_job_test_ctx(test, 2); + int i; + + for (i = 0; i < AVD_MAX_INST; i++) + push(i, "fill"); + KUNIT_EXPECT_EQ(test, ctx->job.segments[0].num, AVD_MAX_INST); + KUNIT_EXPECT_FALSE(test, ctx->job.overflow); + + push(0xdeadbeef, "one too many"); + KUNIT_EXPECT_EQ(test, ctx->job.segments[0].num, AVD_MAX_INST); + KUNIT_EXPECT_TRUE(test, ctx->job.overflow); + /* The next segment was not written either. */ + KUNIT_EXPECT_EQ(test, ctx->job.segments[1].num, 0); +} + +/* Segments past the allocated table are never written. */ +static void avd_job_table_full_test(struct kunit *test) +{ + struct avd_ctx *ctx = avd_job_test_ctx(test, 2); + int i; + + push(1, "header"); + ctx->job.num++; + push(2, "first segment"); + KUNIT_EXPECT_FALSE(test, ctx->job.overflow); + + for (i = 0; i < 8; i++) { + ctx->job.num++; + push(3, "past the table"); + } + KUNIT_EXPECT_TRUE(test, ctx->job.overflow); + KUNIT_EXPECT_EQ(test, ctx->job.segments[0].num, 1); + KUNIT_EXPECT_EQ(test, ctx->job.segments[1].num, 1); + + /* A new job starts clean. */ + KUNIT_ASSERT_EQ(test, avd_init_job(ctx, AVD_CODEC_H264, 1), 0); + KUNIT_EXPECT_FALSE(test, ctx->job.overflow); + KUNIT_EXPECT_EQ(test, ctx->job.max, 1); +} + +static struct kunit_case avd_job_test_cases[] = { + KUNIT_CASE(avd_job_segment_full_test), + KUNIT_CASE(avd_job_table_full_test), + {} +}; + +static struct kunit_suite avd_job_test_suite = { + .name = "apple-avd-job", + .test_cases = avd_job_test_cases, +}; + +kunit_test_suite(avd_job_test_suite); diff --git a/drivers/media/platform/apple/avd/avd-queue-test.c b/drivers/media/platform/apple/avd/avd-queue-test.c index 8d343b1b93399..7de5e49bdae26 100644 --- a/drivers/media/platform/apple/avd/avd-queue-test.c +++ b/drivers/media/platform/apple/avd/avd-queue-test.c @@ -207,6 +207,48 @@ static void avd_submit_order(struct kunit *test) KUNIT_EXPECT_FALSE(test, avd_slot_submit_first(pool, &pool[1])); } +/* + * The legacy transport programs the context FIFO, which only exists while + * the OUTPUT queue streams. With no codec capability the submission stops + * before it touches the hardware either way. + */ +static void avd_open_submit_needs_fifo(struct kunit *test) +{ + static const struct avd_coded_fmt_desc desc = {}; + struct avd_dev *avd; + struct avd_ctx *ctx; + struct avd_frame *f; + struct avd_buf *prog; + u32 *words; + + avd = kunit_kzalloc(test, sizeof(*avd), GFP_KERNEL); + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + f = kunit_kzalloc(test, sizeof(*f), GFP_KERNEL); + prog = kunit_kzalloc(test, sizeof(*prog), GFP_KERNEL); + words = kunit_kcalloc(test, 4, sizeof(*words), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, avd); + KUNIT_ASSERT_NOT_NULL(test, ctx); + KUNIT_ASSERT_NOT_NULL(test, f); + KUNIT_ASSERT_NOT_NULL(test, prog); + KUNIT_ASSERT_NOT_NULL(test, words); + + ctx->dev = avd; + ctx->coded_fmt_desc = &desc; + prog->cpu = words; + prog->size = 4 * sizeof(*words); + f->ctx = ctx; + f->prog = prog; + f->prog_words = 1; + f->cmd[0] = AVD_CODEC_H264; + + KUNIT_EXPECT_EQ(test, avd_open_backend.submit(avd, f), -EINVAL); + KUNIT_EXPECT_NULL(test, avd->compat_active); + + ctx->inst.cpu = words; + KUNIT_EXPECT_EQ(test, avd_open_backend.submit(avd, f), -EOPNOTSUPP); + KUNIT_EXPECT_NULL(test, avd->compat_active); +} + static struct kunit_case avd_queue_cases[] = { KUNIT_CASE(avd_slots_and_tags), KUNIT_CASE(avd_retirement_order), @@ -215,6 +257,7 @@ static struct kunit_case avd_queue_cases[] = { KUNIT_CASE(avd_command_builders), KUNIT_CASE(avd_dispatch_reservation), KUNIT_CASE(avd_submit_order), + KUNIT_CASE(avd_open_submit_needs_fifo), {} }; diff --git a/drivers/media/platform/apple/avd/avd-v4l2-test.c b/drivers/media/platform/apple/avd/avd-v4l2-test.c new file mode 100644 index 0000000000000..3b2e09b6f6d9c --- /dev/null +++ b/drivers/media/platform/apple/avd/avd-v4l2-test.c @@ -0,0 +1,277 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit tests for the format ioctls and the control helpers of the Apple + * video decoder. + */ + +#include +#include +#include +#include +#include + +#include "avd.h" + +static const struct avd_coded_fmt_ops avd_test_ops = {}; + +static const struct avd_decoded_fmt_desc avd_test_decoded_fmts[] = { + { .fourcc = V4L2_PIX_FMT_NV12, .image_fmt = AVD_IMG_FMT_420_8BIT }, +}; + +static const struct avd_coded_fmt_desc avd_test_coded_desc = { + .fourcc = V4L2_PIX_FMT_H264_SLICE, + .frmsize = { + .min_width = 64, .max_width = 4096, .step_width = 64, + .min_height = 64, .max_height = 4096, .step_height = 16, + }, + .ops = &avd_test_ops, + .num_decoded_fmts = ARRAY_SIZE(avd_test_decoded_fmts), + .decoded_fmts = avd_test_decoded_fmts, +}; + +struct avd_test_fmt { + struct avd_ctx *ctx; + struct file *file; + struct vb2_queue *cap_q; +}; + +/* A 64x64 context with its default CAPTURE format, as after open(). */ +static void avd_test_fmt_init(struct kunit *test, struct avd_test_fmt *t) +{ + struct v4l2_m2m_ctx *m2m_ctx; + + t->ctx = kunit_kzalloc(test, sizeof(*t->ctx), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, t->ctx); + m2m_ctx = kunit_kzalloc(test, sizeof(*m2m_ctx), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, m2m_ctx); + t->file = kunit_kzalloc(test, sizeof(*t->file), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, t->file); + + t->ctx->fh.m2m_ctx = m2m_ctx; + t->file->private_data = &t->ctx->fh; + t->cap_q = &m2m_ctx->cap_q_ctx.q; + + t->ctx->coded_fmt_desc = &avd_test_coded_desc; + t->ctx->coded_fmt.fmt.pix_mp.width = 64; + t->ctx->coded_fmt.fmt.pix_mp.height = 64; + t->ctx->image_fmt = AVD_IMG_FMT_420_8BIT; + avd_reset_decoded_fmt(t->ctx); +} + +static void avd_test_capture_fmt(struct v4l2_format *f, u32 width, u32 height) +{ + memset(f, 0, sizeof(*f)); + f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; + f->fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12; + f->fmt.pix_mp.width = width; + f->fmt.pix_mp.height = height; +} + +static void avd_test_expect_comp(struct kunit *test, + const struct avd_comp *got, + const struct avd_comp *want) +{ + KUNIT_EXPECT_EQ(test, got->size, want->size); + KUNIT_EXPECT_EQ(test, got->start_offset, want->start_offset); + KUNIT_EXPECT_MEMEQ(test, got->offsets, want->offsets, + sizeof(want->offsets)); +} + +/* TRY_FMT(CAPTURE) must leave the context's buffer layout alone. */ +static void avd_try_capture_fmt_test(struct kunit *test) +{ + struct avd_test_fmt t; + struct v4l2_format f, decoded; + struct avd_comp comp; + + avd_test_fmt_init(test, &t); + comp = t.ctx->comp; + decoded = t.ctx->decoded_fmt; + KUNIT_ASSERT_NE(test, comp.size, 0); + + /* CAPTURE buffers allocated, as when streaming. */ + t.cap_q->is_busy = 1; + avd_test_capture_fmt(&f, 4096, 4096); + KUNIT_EXPECT_EQ(test, avd_try_capture_fmt(t.file, NULL, &f), 0); + KUNIT_EXPECT_EQ(test, f.fmt.pix_mp.width, 4096); + KUNIT_EXPECT_GT(test, f.fmt.pix_mp.plane_fmt[0].sizeimage, + decoded.fmt.pix_mp.plane_fmt[0].sizeimage); + avd_test_expect_comp(test, &t.ctx->comp, &comp); + KUNIT_EXPECT_MEMEQ(test, &t.ctx->decoded_fmt, &decoded, sizeof(decoded)); + + /* S_FMT is refused on the busy queue and changes nothing either. */ + avd_test_capture_fmt(&f, 4096, 4096); + KUNIT_EXPECT_EQ(test, avd_s_capture_fmt(t.file, NULL, &f), -EBUSY); + avd_test_expect_comp(test, &t.ctx->comp, &comp); + + /* The same holds with no buffers. */ + t.cap_q->is_busy = 0; + avd_test_capture_fmt(&f, 1920, 1088); + KUNIT_EXPECT_EQ(test, avd_try_capture_fmt(t.file, NULL, &f), 0); + avd_test_expect_comp(test, &t.ctx->comp, &comp); + KUNIT_EXPECT_MEMEQ(test, &t.ctx->decoded_fmt, &decoded, sizeof(decoded)); +} + +/* S_FMT(CAPTURE) lays out the compressed area for the format it sets. */ +static void avd_s_capture_fmt_test(struct kunit *test) +{ + struct avd_test_fmt t; + struct v4l2_format f; + struct avd_comp want = {}; + + avd_test_fmt_init(test, &t); + + avd_test_capture_fmt(&f, 1920, 1088); + KUNIT_ASSERT_EQ(test, avd_s_capture_fmt(t.file, NULL, &f), 0); + + want.start_offset = 1920 * 1088 * 3 / 2; + fill_comp(&want, AVD_IMG_FMT_420_8BIT, 1920, 1088); + avd_test_expect_comp(test, &t.ctx->comp, &want); + KUNIT_EXPECT_EQ(test, t.ctx->decoded_fmt.fmt.pix_mp.width, 1920); + KUNIT_EXPECT_EQ(test, t.ctx->decoded_fmt.fmt.pix_mp.height, 1088); + KUNIT_EXPECT_EQ(test, + t.ctx->decoded_fmt.fmt.pix_mp.plane_fmt[0].sizeimage, + want.start_offset + want.size); + + /* A later TRY_FMT reports its own layout but stores nothing. */ + avd_test_capture_fmt(&f, 4096, 2176); + KUNIT_EXPECT_EQ(test, avd_try_capture_fmt(t.file, NULL, &f), 0); + avd_test_expect_comp(test, &t.ctx->comp, &want); +} + +/* A context whose control handler holds the VP9 frame control. */ +static struct v4l2_ctrl *avd_test_ctrl_init(struct kunit *test, + struct avd_ctx **ctxp) +{ + static const struct v4l2_ctrl_config cfg = { + .id = V4L2_CID_STATELESS_VP9_FRAME, + }; + struct avd_ctx *ctx; + struct v4l2_ctrl *ctrl; + + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, ctx); + v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 1); + ctrl = v4l2_ctrl_new_custom(&ctx->ctrl_hdl, &cfg, NULL); + KUNIT_ASSERT_EQ(test, ctx->ctrl_hdl.error, 0); + KUNIT_ASSERT_NOT_NULL(test, ctrl); + + *ctxp = ctx; + return ctrl; +} + +static int avd_test_set_vp9_width(struct v4l2_ctrl *ctrl, u16 width) +{ + struct v4l2_ctrl_vp9_frame frame = *ctrl->p_cur.p_vp9_frame; + + frame.frame_width_minus_1 = width - 1; + frame.frame_height_minus_1 = width - 1; + return v4l2_ctrl_s_ctrl_compound(ctrl, V4L2_CTRL_TYPE_VP9_FRAME, + &frame); +} + +/* A copy keeps the value it was taken from when the control is set again. */ +static void avd_ctrl_copy_test(struct kunit *test) +{ + struct v4l2_ctrl_vp9_frame *copy; + struct v4l2_ctrl *ctrl; + struct avd_ctx *ctx; + + ctrl = avd_test_ctrl_init(test, &ctx); + copy = kunit_kzalloc(test, sizeof(*copy) + 1, GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, copy); + + KUNIT_ASSERT_EQ(test, avd_test_set_vp9_width(ctrl, 64), 0); + KUNIT_ASSERT_EQ(test, avd_ctrl_copy(ctx, V4L2_CID_STATELESS_VP9_FRAME, + copy, sizeof(*copy)), 0); + KUNIT_EXPECT_MEMEQ(test, copy, ctrl->p_cur.p, sizeof(*copy)); + KUNIT_EXPECT_TRUE(test, avd_vp9_frame_fits(copy, 64, 64)); + + KUNIT_ASSERT_EQ(test, avd_test_set_vp9_width(ctrl, 4096), 0); + KUNIT_EXPECT_FALSE(test, avd_vp9_frame_fits(ctrl->p_cur.p_vp9_frame, + 64, 64)); + KUNIT_EXPECT_EQ(test, copy->frame_width_minus_1, 63); + KUNIT_EXPECT_EQ(test, copy->frame_height_minus_1, 63); + KUNIT_EXPECT_TRUE(test, avd_vp9_frame_fits(copy, 64, 64)); + + /* Only a destination of the control's own size is filled. */ + memset(copy, 0, sizeof(*copy)); + KUNIT_EXPECT_EQ(test, avd_ctrl_copy(ctx, V4L2_CID_STATELESS_VP9_FRAME, + copy, sizeof(*copy) - 1), -EINVAL); + KUNIT_EXPECT_EQ(test, avd_ctrl_copy(ctx, V4L2_CID_STATELESS_VP9_FRAME, + copy, sizeof(*copy) + 1), -EINVAL); + KUNIT_EXPECT_EQ(test, copy->frame_width_minus_1, 0); + /* So is a control that the handler does not have. */ + KUNIT_EXPECT_EQ(test, + avd_ctrl_copy(ctx, V4L2_CID_STATELESS_VP9_COMPRESSED_HDR, + copy, sizeof(*copy)), -EINVAL); + + v4l2_ctrl_handler_free(&ctx->ctrl_hdl); +} + +struct avd_test_copy_work { + struct work_struct work; + struct completion done; + struct v4l2_ctrl *ctrl; + struct v4l2_ctrl_vp9_frame copy; + int ret; +}; + +static void avd_test_copy_work_fn(struct work_struct *work) +{ + struct avd_test_copy_work *w = + container_of(work, struct avd_test_copy_work, work); + + w->ret = avd_ctrl_copy_value(w->ctrl, &w->copy, sizeof(w->copy)); + complete(&w->done); +} + +/* The copy waits for whoever holds the control lock and sees its whole update. */ +static void avd_ctrl_copy_locked_test(struct kunit *test) +{ + struct avd_test_copy_work *w; + struct v4l2_ctrl *ctrl; + struct avd_ctx *ctx; + + ctrl = avd_test_ctrl_init(test, &ctx); + w = kunit_kzalloc(test, sizeof(*w), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, w); + w->ctrl = ctrl; + w->ret = -EINPROGRESS; + init_completion(&w->done); + INIT_WORK(&w->work, avd_test_copy_work_fn); + + KUNIT_ASSERT_EQ(test, avd_test_set_vp9_width(ctrl, 64), 0); + + v4l2_ctrl_lock(ctrl); + ctrl->p_cur.p_vp9_frame->frame_width_minus_1 = 4095; + schedule_work(&w->work); + KUNIT_EXPECT_EQ(test, + wait_for_completion_timeout(&w->done, + msecs_to_jiffies(200)), 0); + KUNIT_EXPECT_EQ(test, w->ret, -EINPROGRESS); + ctrl->p_cur.p_vp9_frame->frame_height_minus_1 = 4095; + v4l2_ctrl_unlock(ctrl); + + flush_work(&w->work); + KUNIT_EXPECT_EQ(test, w->ret, 0); + KUNIT_EXPECT_EQ(test, w->copy.frame_width_minus_1, 4095); + KUNIT_EXPECT_EQ(test, w->copy.frame_height_minus_1, 4095); + + v4l2_ctrl_handler_free(&ctx->ctrl_hdl); +} + +static struct kunit_case avd_v4l2_test_cases[] = { + KUNIT_CASE(avd_try_capture_fmt_test), + KUNIT_CASE(avd_s_capture_fmt_test), + KUNIT_CASE(avd_ctrl_copy_test), + KUNIT_CASE(avd_ctrl_copy_locked_test), + {} +}; + +static struct kunit_suite avd_v4l2_test_suite = { + .name = "apple-avd-v4l2", + .test_cases = avd_v4l2_test_cases, +}; + +kunit_test_suite(avd_v4l2_test_suite); diff --git a/drivers/media/platform/apple/avd/avd-v4l2.c b/drivers/media/platform/apple/avd/avd-v4l2.c index 2a4903ee76aaf..5c010a7c9c66d 100644 --- a/drivers/media/platform/apple/avd/avd-v4l2.c +++ b/drivers/media/platform/apple/avd/avd-v4l2.c @@ -11,6 +11,7 @@ * Copyright (C) 2011 Samsung Electronics Co., Ltd. */ +#include #include #include @@ -18,6 +19,7 @@ #include #include "avd.h" +#include "avd-inst.h" static bool avd_image_fmt_match(enum avd_image_fmt fmt1, enum avd_image_fmt fmt2) @@ -65,6 +67,41 @@ static bool avd_image_fmt_fits_capture(struct avd_ctx *ctx, !memcmp(comp.offsets, ctx->comp.offsets, sizeof(comp.offsets)); } +/* + * Whether a job may program @image_fmt, the picture format that the current + * value of the codec's format control describes. The CAPTURE buffers and the + * per-context scratch were sized for ctx->image_fmt (ANY sizes them as 8-bit + * 4:2:0), and the control's value can differ from it: it may have been set + * while another codec was selected, or kept over an OUTPUT format change, and + * an unchanged value never reaches s_ctrl. Refuse the job unless both are the + * same and the CAPTURE buffers fit it, and unless the scratch a codec sized at + * STREAMON has the bit depth of the picture (s_ctrl only refuses a change + * while CAPTURE buffers are allocated, not while OUTPUT streams). + */ +static bool avd_image_fmt_10bit(enum avd_image_fmt fmt) +{ + return fmt == AVD_IMG_FMT_420_10BIT || fmt == AVD_IMG_FMT_422_10BIT; +} + +int avd_check_run_image_fmt(struct avd_ctx *ctx, enum avd_image_fmt image_fmt) +{ + enum avd_image_fmt sized = ctx->image_fmt; + + if (sized == AVD_IMG_FMT_ANY) + sized = AVD_IMG_FMT_420_8BIT; + if (image_fmt == AVD_IMG_FMT_ANY) + image_fmt = AVD_IMG_FMT_420_8BIT; + if (image_fmt != sized || !avd_image_fmt_fits_capture(ctx, image_fmt) || + (ctx->scratch_sized && + avd_image_fmt_10bit(ctx->scratch_fmt) != avd_image_fmt_10bit(image_fmt))) { + dev_dbg_ratelimited(ctx->dev->dev, + "picture format %d does not fit the buffers (%d)\n", + image_fmt, ctx->image_fmt); + return -EINVAL; + } + return 0; +} + static u32 avd_enum_decoded_fmt(struct avd_ctx *ctx, int index, enum avd_image_fmt image_fmt) { @@ -103,15 +140,21 @@ static bool avd_is_valid_fmt(struct avd_ctx *ctx, u32 fourcc, return false; } +/* + * Lay out a decoded buffer: the picture, then the compressed reference area + * described by @comp, then any codec specific tail. Only @pix_mp and @comp + * are written, so TRY_FMT can use it on a context that is streaming. + */ static void avd_fill_decoded_pixfmt(struct avd_ctx *ctx, - struct v4l2_pix_format_mplane *pix_mp) + struct v4l2_pix_format_mplane *pix_mp, + struct avd_comp *comp) { v4l2_fill_pixfmt_mp(pix_mp, pix_mp->pixelformat, pix_mp->width, pix_mp->height); - ctx->comp.start_offset = pix_mp->plane_fmt[0].sizeimage; - fill_comp(&ctx->comp, ctx->image_fmt, pix_mp->width, pix_mp->height); - pix_mp->plane_fmt[0].sizeimage += ctx->comp.size; + comp->start_offset = pix_mp->plane_fmt[0].sizeimage; + fill_comp(comp, ctx->image_fmt, pix_mp->width, pix_mp->height); + pix_mp->plane_fmt[0].sizeimage += comp->size; if (ctx->coded_fmt_desc->ops->adjust_decoded_fmt) ctx->coded_fmt_desc->ops->adjust_decoded_fmt(ctx, pix_mp); } @@ -138,7 +181,7 @@ void avd_reset_decoded_fmt(struct avd_ctx *ctx) f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; f->fmt.pix_mp.width = ctx->coded_fmt.fmt.pix_mp.width; f->fmt.pix_mp.height = ctx->coded_fmt.fmt.pix_mp.height; - avd_fill_decoded_pixfmt(ctx, &f->fmt.pix_mp); + avd_fill_decoded_pixfmt(ctx, &f->fmt.pix_mp, &ctx->comp); } static int avd_try_ctrl(struct v4l2_ctrl *ctrl) @@ -396,6 +439,7 @@ static const struct avd_ctrl_desc avd_av1_ctrl_descs[] = { }, { .cfg.id = V4L2_CID_STATELESS_AV1_FRAME, + .cfg.ops = &avd_ctrl_ops, }, { .cfg.id = V4L2_CID_STATELESS_AV1_FILM_GRAIN, @@ -566,11 +610,10 @@ static int avd_querycap(struct file *file, void *priv, return 0; } -static int avd_try_capture_fmt(struct file *file, void *priv, - struct v4l2_format *f) +static int avd_try_decoded_fmt(struct avd_ctx *ctx, struct v4l2_format *f, + struct avd_comp *comp) { struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp; - struct avd_ctx *ctx = file_to_ctx(file); const struct avd_coded_fmt_desc *coded_desc; /* @@ -594,12 +637,21 @@ static int avd_try_capture_fmt(struct file *file, void *priv, v4l2_apply_frmsize_constraints(&pix_mp->width, &pix_mp->height, &coded_desc->frmsize); - avd_fill_decoded_pixfmt(ctx, pix_mp); + avd_fill_decoded_pixfmt(ctx, pix_mp, comp); pix_mp->field = V4L2_FIELD_NONE; return 0; } +VISIBLE_IF_KUNIT int avd_try_capture_fmt(struct file *file, void *priv, + struct v4l2_format *f) +{ + struct avd_comp comp; + + return avd_try_decoded_fmt(file_to_ctx(file), f, &comp); +} +EXPORT_SYMBOL_IF_KUNIT(avd_try_capture_fmt); + static int avd_try_output_fmt(struct file *file, void *priv, struct v4l2_format *f) { @@ -627,10 +679,11 @@ static int avd_try_output_fmt(struct file *file, void *priv, return 0; } -static int avd_s_capture_fmt(struct file *file, void *priv, - struct v4l2_format *f) +VISIBLE_IF_KUNIT int avd_s_capture_fmt(struct file *file, void *priv, + struct v4l2_format *f) { struct avd_ctx *ctx = file_to_ctx(file); + struct avd_comp comp; struct vb2_queue *vq; int ret; @@ -640,13 +693,15 @@ static int avd_s_capture_fmt(struct file *file, void *priv, if (vb2_is_busy(vq)) return -EBUSY; - ret = avd_try_capture_fmt(file, priv, f); + ret = avd_try_decoded_fmt(ctx, f, &comp); if (ret) return ret; ctx->decoded_fmt = *f; + ctx->comp = comp; return 0; } +EXPORT_SYMBOL_IF_KUNIT(avd_s_capture_fmt); static int avd_s_output_fmt(struct file *file, void *priv, struct v4l2_format *f) @@ -928,14 +983,21 @@ static int avd_start_streaming(struct vb2_queue *q, unsigned int count) goto err_return_buffers; } + /* The context's instruction FIFO is only used while OUTPUT streams. */ + ret = avd_buf_alloc(ctx->dev, &ctx->inst, fifo_size()); + if (ret) + goto err_return_buffers; + if (desc->ops->start) { ret = desc->ops->start(ctx); if (ret) - goto err_return_buffers; + goto err_free_inst; } return 0; +err_free_inst: + avd_buf_free(ctx->dev, &ctx->inst); err_return_buffers: /* Keep requests queued for a later STREAMON retry. */ while ((vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx))) @@ -977,6 +1039,7 @@ static void avd_stop_streaming(struct vb2_queue *q) if (desc->ops->stop) desc->ops->stop(ctx); + avd_buf_free(ctx->dev, &ctx->inst); /* Drop a job table left over from a frame that never finished. */ kvfree(ctx->job.segments); @@ -1054,6 +1117,40 @@ void avd_run_postamble(struct avd_ctx *ctx, struct avd_run *run) v4l2_ctrl_request_complete(src_req, &ctx->ctrl_hdl); } +/* + * Copy the current value of a fixed-size control for a run. Requests are + * applied to the controls before a run reads them, but a control can also be + * set directly at any time, which rewrites its value under the control lock + * only. A run that checks a value and then reads it again from the control + * may see two different values. Take one copy under the lock; the run checks + * and uses that copy. @size must be the size of the control's value. + */ +VISIBLE_IF_KUNIT int avd_ctrl_copy_value(struct v4l2_ctrl *ctrl, void *dst, + size_t size) +{ + int ret = -EINVAL; + + v4l2_ctrl_lock(ctrl); + if ((size_t)ctrl->elems * ctrl->elem_size == size) { + memcpy(dst, ctrl->p_cur.p, size); + ret = 0; + } + v4l2_ctrl_unlock(ctrl); + + return ret; +} +EXPORT_SYMBOL_IF_KUNIT(avd_ctrl_copy_value); + +/* v4l2_ctrl_find() takes the control lock itself, so look up before copying. */ +int avd_ctrl_copy(struct avd_ctx *ctx, u32 id, void *dst, size_t size) +{ + struct v4l2_ctrl *ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, id); + + if (!ctrl) + return -EINVAL; + return avd_ctrl_copy_value(ctrl, dst, size); +} + static int avd_add_ctrls(struct avd_ctx *ctx, const struct avd_ctrls *ctrls) { unsigned int i; diff --git a/drivers/media/platform/apple/avd/avd-vp9-test.c b/drivers/media/platform/apple/avd/avd-vp9-test.c new file mode 100644 index 0000000000000..7bf89193a8f40 --- /dev/null +++ b/drivers/media/platform/apple/avd/avd-vp9-test.c @@ -0,0 +1,218 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit tests for the VP9 frame checks of the Apple video decoder. + */ + +#include +#include + +#include "avd.h" + +struct avd_vp9_tiling_case { + const char *desc; + u16 width; + u8 tile_rows_log2; + u8 tile_cols_log2; + int ret; +}; + +static const struct avd_vp9_tiling_case avd_vp9_tiling_cases[] = { + { "one tile", 1920, 0, 0, 0 }, + { "two tile rows", 1920, 1, 0, 0 }, + { "four tile rows", 1920, 2, 0, 0 }, + { "eight tile rows", 1920, 3, 0, -EINVAL }, + { "2^26 tile rows", 4096, 26, 6, -EINVAL }, + { "2^31 tile rows", 4096, 31, 1, -EINVAL }, + { "255 tile rows log2", 1920, 255, 0, -EINVAL }, + { "four tile columns at 1920", 1920, 0, 2, 0 }, + { "eight tile columns at 1920", 1920, 0, 3, -EINVAL }, + { "sixteen tile columns at 4096", 4096, 2, 4, 0 }, + { "32 tile columns at 4096", 4096, 0, 5, -EINVAL }, + { "64 tile columns at 4096", 4096, 0, 6, -EINVAL }, + { "two tile columns at 64", 64, 0, 1, -EINVAL }, + { "one tile column at 8192", 8192, 0, 0, -EINVAL }, + { "two tile columns at 8192", 8192, 0, 1, 0 }, +}; + +KUNIT_ARRAY_PARAM_DESC(avd_vp9_tiling, avd_vp9_tiling_cases, desc); + +static void avd_vp9_tiling_test(struct kunit *test) +{ + const struct avd_vp9_tiling_case *c = test->param_value; + struct v4l2_ctrl_vp9_frame frame = { + .frame_width_minus_1 = c->width - 1, + .frame_height_minus_1 = 1079, + .tile_rows_log2 = c->tile_rows_log2, + .tile_cols_log2 = c->tile_cols_log2, + }; + + KUNIT_EXPECT_EQ(test, avd_vp9_check_tiling(&frame), c->ret); +} + +#define AVD_TEST_MAX_TILES 2 + +struct avd_vp9_tiles_case { + const char *desc; + const u8 *data; + u32 payload; + u32 header; + unsigned int tiles; + int ret; + u32 offsets[AVD_TEST_MAX_TILES]; + u32 sizes[AVD_TEST_MAX_TILES]; +}; + +/* Two tiles after a 2-byte header: a 3-byte tile, then the rest. */ +static const u8 avd_test_two_tiles[] = { + 0xaa, 0xbb, 0x00, 0x00, 0x00, 0x03, 0x01, 0x02, 0x03, 0x04, 0x05, +}; + +/* The first tile's size reaches one byte past the payload. */ +static const u8 avd_test_long_tile[] = { + 0xaa, 0xbb, 0x00, 0x00, 0x00, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, +}; + +/* The first tile's size is the largest u32. */ +static const u8 avd_test_huge_tile[] = { + 0xaa, 0xbb, 0xff, 0xff, 0xff, 0xff, 0x01, 0x02, 0x03, 0x04, 0x05, +}; + +/* The first tile fills the payload and leaves nothing for the last one. */ +static const u8 avd_test_no_last_tile[] = { + 0xaa, 0xbb, 0x00, 0x00, 0x00, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05, +}; + +/* An empty first tile. */ +static const u8 avd_test_empty_tile[] = { + 0xaa, 0xbb, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, +}; + +static const struct avd_vp9_tiles_case avd_vp9_tiles_cases[] = { + { "one tile", avd_test_two_tiles, 11, 2, 1, 0, { 2 }, { 9 } }, + { "two tiles", avd_test_two_tiles, 11, 2, 2, 0, { 6, 9 }, { 3, 2 } }, + { "headers beyond the payload", avd_test_two_tiles, 11, 12, 1, -EINVAL }, + { "headers fill the payload", avd_test_two_tiles, 11, 11, 1, -EINVAL }, + { "headers beyond the payload, two tiles", avd_test_two_tiles, 11, + 0xffff + 0xffff, 2, -EINVAL }, + { "three bytes for a tile size", avd_test_two_tiles, 5, 2, 2, -EINVAL }, + { "no bytes for a tile size", avd_test_two_tiles, 2, 2, 2, -EINVAL }, + { "tile beyond the payload", avd_test_long_tile, 11, 2, 2, -EINVAL }, + { "tile size wraps", avd_test_huge_tile, 11, 2, 2, -EINVAL }, + { "no data for the last tile", avd_test_no_last_tile, 11, 2, 2, -EINVAL, + { 6 }, { 5 } }, + { "empty tile", avd_test_empty_tile, 11, 2, 2, -EINVAL }, + { "no mapping", NULL, 11, 2, 1, -EINVAL }, +}; + +KUNIT_ARRAY_PARAM_DESC(avd_vp9_tiles, avd_vp9_tiles_cases, desc); + +/* Walks the tiles as set_tiles() does. */ +static void avd_vp9_tiles_test(struct kunit *test) +{ + const struct avd_vp9_tiles_case *c = test->param_value; + u32 offset = c->header, size; + int ret = 0; + + for (unsigned int i = 0; i < c->tiles; i++) { + ret = avd_vp9_next_tile(c->data, c->payload, i == c->tiles - 1, + &offset, &size); + if (ret) + break; + KUNIT_EXPECT_EQ(test, offset, c->offsets[i]); + KUNIT_EXPECT_EQ(test, size, c->sizes[i]); + KUNIT_EXPECT_LE(test, (u64)offset + size, (u64)c->payload); + offset += size; + } + KUNIT_EXPECT_EQ(test, ret, c->ret); +} + +/* The frame size against a 1920x1088 coded format. */ +static void avd_vp9_frame_fits_test(struct kunit *test) +{ + struct v4l2_ctrl_vp9_frame frame = { + .frame_width_minus_1 = 1919, + .frame_height_minus_1 = 1079, + }; + + KUNIT_EXPECT_TRUE(test, avd_vp9_frame_fits(&frame, 1920, 1088)); + frame.frame_height_minus_1 = 1087; + KUNIT_EXPECT_TRUE(test, avd_vp9_frame_fits(&frame, 1920, 1088)); + frame.frame_width_minus_1 = 959; + frame.frame_height_minus_1 = 539; + KUNIT_EXPECT_TRUE(test, avd_vp9_frame_fits(&frame, 1920, 1088)); + + frame.frame_width_minus_1 = 1920; + frame.frame_height_minus_1 = 1079; + KUNIT_EXPECT_FALSE(test, avd_vp9_frame_fits(&frame, 1920, 1088)); + frame.frame_width_minus_1 = 1919; + frame.frame_height_minus_1 = 1088; + KUNIT_EXPECT_FALSE(test, avd_vp9_frame_fits(&frame, 1920, 1088)); + frame.frame_width_minus_1 = 4095; + frame.frame_height_minus_1 = 2175; + KUNIT_EXPECT_FALSE(test, avd_vp9_frame_fits(&frame, 64, 64)); + frame.frame_width_minus_1 = 0xffff; + frame.frame_height_minus_1 = 0xffff; + KUNIT_EXPECT_FALSE(test, avd_vp9_frame_fits(&frame, 4096, 4096)); +} + +/* + * A frame larger than the OUTPUT format is refused when the control is set, + * once the OUTPUT queue streams and the format can no longer follow it. + */ +static void avd_vp9_try_frame_size_test(struct kunit *test) +{ + static const struct v4l2_ctrl_config cfg = { + .id = V4L2_CID_STATELESS_VP9_FRAME, + }; + struct v4l2_m2m_ctx *m2m_ctx; + struct v4l2_ctrl_vp9_frame *frame; + struct v4l2_ctrl *ctrl; + struct avd_ctx *ctx; + + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, ctx); + m2m_ctx = kunit_kzalloc(test, sizeof(*m2m_ctx), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, m2m_ctx); + ctx->fh.m2m_ctx = m2m_ctx; + ctx->coded_fmt.fmt.pix_mp.width = 1920; + ctx->coded_fmt.fmt.pix_mp.height = 1088; + + v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 1); + ctrl = v4l2_ctrl_new_custom(&ctx->ctrl_hdl, &cfg, NULL); + KUNIT_ASSERT_EQ(test, ctx->ctrl_hdl.error, 0); + KUNIT_ASSERT_NOT_NULL(test, ctrl); + frame = ctrl->p_new.p_vp9_frame; + + /* Not streaming: the format may still be set to the stream's size. */ + frame->frame_width_minus_1 = 4095; + frame->frame_height_minus_1 = 2159; + KUNIT_EXPECT_EQ(test, avd_vp9_fmt_ops.try_ctrl(ctx, ctrl), 0); + + m2m_ctx->out_q_ctx.q.streaming = 1; + KUNIT_EXPECT_EQ(test, avd_vp9_fmt_ops.try_ctrl(ctx, ctrl), -EINVAL); + frame->frame_width_minus_1 = 1919; + frame->frame_height_minus_1 = 1088; + KUNIT_EXPECT_EQ(test, avd_vp9_fmt_ops.try_ctrl(ctx, ctrl), -EINVAL); + frame->frame_height_minus_1 = 1087; + KUNIT_EXPECT_EQ(test, avd_vp9_fmt_ops.try_ctrl(ctx, ctrl), 0); + frame->frame_width_minus_1 = 1279; + frame->frame_height_minus_1 = 719; + KUNIT_EXPECT_EQ(test, avd_vp9_fmt_ops.try_ctrl(ctx, ctrl), 0); + + v4l2_ctrl_handler_free(&ctx->ctrl_hdl); +} + +static struct kunit_case avd_vp9_test_cases[] = { + KUNIT_CASE(avd_vp9_frame_fits_test), + KUNIT_CASE(avd_vp9_try_frame_size_test), + KUNIT_CASE_PARAM(avd_vp9_tiling_test, avd_vp9_tiling_gen_params), + KUNIT_CASE_PARAM(avd_vp9_tiles_test, avd_vp9_tiles_gen_params), + {} +}; + +static struct kunit_suite avd_vp9_test_suite = { + .name = "apple-avd-vp9", + .test_cases = avd_vp9_test_cases, +}; + +kunit_test_suite(avd_vp9_test_suite); diff --git a/drivers/media/platform/apple/avd/avd-vp9.c b/drivers/media/platform/apple/avd/avd-vp9.c index a596b420ac2c7..695ba69d9b03f 100644 --- a/drivers/media/platform/apple/avd/avd-vp9.c +++ b/drivers/media/platform/apple/avd/avd-vp9.c @@ -14,6 +14,7 @@ * Alpha Lin */ +#include #include #include @@ -492,19 +493,53 @@ static void set_header(struct avd_ctx *ctx, struct avd_vp9_run *run) set_refs(ctx, run); } -static void set_tiles(struct avd_ctx *ctx, struct avd_vp9_run *run) +/* + * Find the next tile in the frame (VP9 6.4 decode_tiles()): at @offset each + * tile but the last starts with its size as a 32-bit big-endian word, the + * last one takes the rest of the payload. Returns the tile's data offset in + * @offset and its size in @size, both within @payload. A tile holds at least + * one byte (9.2.1: sz is at least 1). + */ +VISIBLE_IF_KUNIT int avd_vp9_next_tile(const u8 *data, u32 payload, bool last, + u32 *offset, u32 *size) +{ + u32 off = *offset, tile_size; + + if (!data || off > payload) + return -EINVAL; + + if (last) { + tile_size = payload - off; + } else { + if (payload - off < 4) + return -EINVAL; + tile_size = get_unaligned_be32(&data[off]); + off += 4; + if (tile_size > payload - off) + return -EINVAL; + } + if (!tile_size) + return -EINVAL; + + *offset = off; + *size = tile_size; + return 0; +} +EXPORT_SYMBOL_IF_KUNIT(avd_vp9_next_tile); + +static int set_tiles(struct avd_ctx *ctx, struct avd_vp9_run *run) { const struct v4l2_ctrl_vp9_frame *frame = run->decode_params; struct avd_vp9_ctx *vp9_ctx = ctx->priv; struct vb2_v4l2_buffer *src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); const u8 *data = vb2_plane_vaddr(&src->vb2_buf, 0); - + u32 payload = vb2_get_plane_payload(&src->vb2_buf, 0); u32 offset = frame->uncompressed_header_size + frame->compressed_header_size; - u32 size = vb2_get_plane_payload(&src->vb2_buf, 0) - offset; u32 num_tile_rows = 1 << frame->tile_rows_log2; u32 num_tile_cols = 1 << frame->tile_cols_log2; u32 tile_size; + int ret; /* 6.2.6 Compute image size syntax */ u32 sb_64_cols = (((frame->frame_width_minus_1 + 8) >> 3) + 7) >> 3; @@ -512,18 +547,15 @@ static void set_tiles(struct avd_ctx *ctx, struct avd_vp9_run *run) for (int row = 0; row < num_tile_rows; row++) for (int col = 0; col < num_tile_cols; col++) { + /* Every tile lies within the payload. */ + ret = avd_vp9_next_tile(data, payload, + row == num_tile_rows - 1 && + col == num_tile_cols - 1, + &offset, &tile_size); + if (ret) + return ret; + ctx->job.num++; - if (row == num_tile_rows - 1 && - col == num_tile_cols - 1) { - tile_size = size; - } else { - tile_size = get_unaligned_be32(&data[offset]); - /* i have crashed my computer because of this */ - if (tile_size > size - 4) - return; - offset += 4; - size -= 4; - } push(AVD_OP_CODED_DATA | AVD_OP_CODED_DATA_ADDR( run->base.coded_in >> 32), @@ -546,9 +578,10 @@ static void set_tiles(struct avd_ctx *ctx, struct avd_vp9_run *run) "til_ac0_tile_dims"); offset += tile_size; - size -= tile_size; vp9_ctx->b->submit_num++; } + + return 0; } static void update_dec_buf_info(struct avd_decoded_buffer *buf, @@ -665,6 +698,28 @@ static void init_probs(struct avd_ctx *ctx, const struct avd_vp9_run *run) sizeof(avd_probs->seg.tree_probs)); } +static enum avd_image_fmt +avd_vp9_frame_image_fmt(const struct v4l2_ctrl_vp9_frame *frame) +{ + /* avd_vp9_try_ctrl() admits only 4:2:0 (profiles 0 and 2). */ + return frame->bit_depth == 8 ? AVD_IMG_FMT_420_8BIT : + AVD_IMG_FMT_420_10BIT; +} + +/* + * The row and column scratch is sized at STREAMON from the OUTPUT (coded) + * format, which cannot change while it streams. The CAPTURE format can be + * larger and can change meanwhile, so the frame must fit the coded size. + */ +VISIBLE_IF_KUNIT bool +avd_vp9_frame_fits(const struct v4l2_ctrl_vp9_frame *frame, u32 width, + u32 height) +{ + return frame->frame_width_minus_1 + 1 <= width && + frame->frame_height_minus_1 + 1 <= height; +} +EXPORT_SYMBOL_IF_KUNIT(avd_vp9_frame_fits); + static int validate_dec_params(struct avd_ctx *ctx, const struct v4l2_ctrl_vp9_frame *dec_params) { @@ -683,6 +738,9 @@ static int validate_dec_params(struct avd_ctx *ctx, dec_params->frame_height_minus_1 + 1 < 64)) return -EINVAL; + if (!avd_vp9_frame_fits(dec_params, fmt_width(ctx), fmt_height(ctx))) + return -EINVAL; + aligned_width = max_t(unsigned int, round_up(dec_params->frame_width_minus_1 + 1, 64), 64); @@ -780,6 +838,40 @@ static int avd_vp9_alloc_bufs(struct avd_ctx *ctx, struct avd_vp9_bank *bank) return 0; } +/* + * VP9 6.2.13 tile_info(): tile_rows_log2 is 0, 1 or 2, and tile_cols_log2 + * lies between calc_min_log2_tile_cols() and calc_max_log2_tile_cols() of + * 6.2.14, which follow from the frame width. The job has one segment per + * tile and the per-column scratch is sized for the column count this allows. + */ +#define VP9_MIN_TILE_WIDTH_B64 4 +#define VP9_MAX_TILE_WIDTH_B64 64 +#define VP9_MAX_TILE_ROWS_LOG2 2 + +VISIBLE_IF_KUNIT int +avd_vp9_check_tiling(const struct v4l2_ctrl_vp9_frame *frame) +{ + /* 6.2.6 Compute image size syntax */ + u32 sb_64_cols = (((frame->frame_width_minus_1 + 8) >> 3) + 7) >> 3; + u32 min_log2 = 0, max_log2 = 1; + + if (frame->tile_rows_log2 > VP9_MAX_TILE_ROWS_LOG2) + return -EINVAL; + + while ((VP9_MAX_TILE_WIDTH_B64 << min_log2) < sb_64_cols) + min_log2++; + while ((sb_64_cols >> max_log2) >= VP9_MIN_TILE_WIDTH_B64) + max_log2++; + max_log2--; + + if (frame->tile_cols_log2 < min_log2 || + frame->tile_cols_log2 > max_log2) + return -EINVAL; + + return 0; +} +EXPORT_SYMBOL_IF_KUNIT(avd_vp9_check_tiling); + /* * Only VP9 profiles 0 and 2 (4:2:0, 8 and 10 bits) decode. A profile 1 * frame (4:2:2 or 4:4:4) is never completed: it times out and the whole @@ -796,12 +888,12 @@ static int avd_vp9_validate_frame(struct avd_ctx *ctx, if (frame->bit_depth != (frame->profile == 2 ? 10 : 8)) return -EINVAL; - return 0; + return avd_vp9_check_tiling(frame); } static int avd_vp9_run_preamble(struct avd_ctx *ctx, struct avd_vp9_run *run) { - struct v4l2_ctrl *ctrl; + struct v4l2_ctrl *ctrl, *hdr; const struct v4l2_ctrl_vp9_frame *dec_params; struct avd_vp9_ctx *vp9_ctx = ctx->priv; unsigned int fctx_idx; @@ -811,16 +903,25 @@ static int avd_vp9_run_preamble(struct avd_ctx *ctx, struct avd_vp9_run *run) if (run->base.setup_err) return run->base.setup_err; - ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, - V4L2_CID_STATELESS_VP9_COMPRESSED_HDR); - if (WARN_ON(!ctrl)) - return -EINVAL; - run->prob_updates = ctrl->p_cur.p; - + hdr = v4l2_ctrl_find(&ctx->ctrl_hdl, + V4L2_CID_STATELESS_VP9_COMPRESSED_HDR); ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, V4L2_CID_STATELESS_VP9_FRAME); - if (WARN_ON(!ctrl)) + if (WARN_ON(!hdr || !ctrl)) return -EINVAL; - dec_params = ctrl->p_cur.p; + + /* + * A plain VIDIOC_S_EXT_CTRLS can change the controls while the job is + * built; check and build from copies taken under the control lock. + */ + v4l2_ctrl_lock(ctrl); + run->prob_updates = kmemdup(hdr->p_cur.p, sizeof(*run->prob_updates), + GFP_KERNEL); + run->decode_params = kmemdup(ctrl->p_cur.p, + sizeof(*run->decode_params), GFP_KERNEL); + v4l2_ctrl_unlock(ctrl); + if (!run->prob_updates || !run->decode_params) + return -ENOMEM; + dec_params = run->decode_params; ret = validate_dec_params(ctx, dec_params); if (ret) @@ -828,11 +929,12 @@ static int avd_vp9_run_preamble(struct avd_ctx *ctx, struct avd_vp9_run *run) /* The frame may have been set, unchecked, while another codec was selected. */ ret = avd_vp9_validate_frame(ctx, dec_params); + if (!ret) + ret = avd_check_run_image_fmt(ctx, + avd_vp9_frame_image_fmt(dec_params)); if (ret) return ret; - run->decode_params = dec_params; - if (avd_backend_is_qfw(ctx->dev)) { struct avd_vp9_bank *bank = NULL; @@ -869,7 +971,7 @@ static void avd_vp9_update_probs(struct avd_ctx *ctx, static int avd_vp9_run(struct avd_ctx *ctx) { - struct avd_vp9_run run; + struct avd_vp9_run run = {}; struct avd_vp9_ctx *vp9_ctx; struct avd_decoded_buffer *dst; int ret; @@ -877,17 +979,16 @@ static int avd_vp9_run(struct avd_ctx *ctx) ret = avd_vp9_run_preamble(ctx, &run); if (ret) { avd_run_postamble(ctx, &run.base); - return ret; + goto out; } - ret = avd_init_job( - ctx, AVD_CODEC_VP9, - (1 << run.decode_params->tile_rows_log2) * - (1 << run.decode_params->tile_cols_log2) + - 1); + /* One segment per tile, bounded by avd_vp9_check_tiling(), and the header. */ + ret = avd_init_job(ctx, AVD_CODEC_VP9, + ((size_t)1 << (run.decode_params->tile_rows_log2 + + run.decode_params->tile_cols_log2)) + 1); if (ret) { avd_run_postamble(ctx, &run.base); - return ret; + goto out; } init_probs(ctx, &run); @@ -899,8 +1000,10 @@ static int avd_vp9_run(struct avd_ctx *ctx) set_header(ctx, &run); vp9_ctx->b->submit_num = 0; - set_tiles(ctx, &run); + ret = set_tiles(ctx, &run); avd_run_postamble(ctx, &run.base); + if (ret) + goto out; if (avd_backend_is_qfw(ctx->dev)) { /* Forward updates need no hardware result; publish before next prepare. */ @@ -909,7 +1012,11 @@ static int avd_vp9_run(struct avd_ctx *ctx) update_ctx_last_info(vp9_ctx); ctx->job_fifo = vp9_ctx->b->bufs.inst.addr; } - return avd_submit_job(ctx); + ret = avd_submit_job(ctx); +out: + kfree(run.decode_params); + kfree(run.prob_updates); + return ret; } #define copy_tx_and_skip(p1, p2) \ @@ -1157,6 +1264,7 @@ static void avd_vp9_stop(struct avd_ctx *ctx) /* drain() excludes readiness callbacks before private storage is freed. */ avd_vp9_free(ctx, ctx->priv); ctx->priv = NULL; + ctx->scratch_sized = false; } static int avd_vp9_start(struct avd_ctx *ctx) @@ -1177,6 +1285,9 @@ static int avd_vp9_start(struct avd_ctx *ctx) avd_init_v4l2_vp9_count_tbl(&vp9_ctx->bank[i]); } ctx->max_frames = vp9_ctx->nbanks; + /* avd_vp9_alloc_bufs() sized the scratch for this depth */ + ctx->scratch_fmt = ctx->image_fmt; + ctx->scratch_sized = true; /* job_ready can run while STREAMON is still initializing this codec. */ smp_store_release(&ctx->priv, vp9_ctx); @@ -1190,25 +1301,31 @@ static int avd_vp9_start(struct avd_ctx *ctx) static enum avd_image_fmt avd_vp9_get_image_fmt(struct avd_ctx *ctx, struct v4l2_ctrl *ctrl) { -#define BIT_DEPTH(chroma) \ - (frame->bit_depth == 8 ? AVD_IMG_FMT_##chroma##_8BIT : \ - AVD_IMG_FMT_##chroma##_10BIT) - const struct v4l2_ctrl_vp9_frame *frame = ctrl->p_new.p_vp9_frame; - if (ctrl->id != V4L2_CID_STATELESS_VP9_FRAME) return AVD_IMG_FMT_ANY; - /* avd_vp9_try_ctrl() admits only 4:2:0 (profiles 0 and 2). */ - return BIT_DEPTH(420); -#undef BIT_DEPTH + return avd_vp9_frame_image_fmt(ctrl->p_new.p_vp9_frame); } static int avd_vp9_try_ctrl(struct avd_ctx *ctx, struct v4l2_ctrl *ctrl) { - if (ctrl->id == V4L2_CID_STATELESS_VP9_FRAME) - return avd_vp9_validate_frame(ctx, ctrl->p_new.p_vp9_frame); + const struct v4l2_ctrl_vp9_frame *frame = ctrl->p_new.p_vp9_frame; + struct vb2_queue *vq; - return 0; + if (ctrl->id != V4L2_CID_STATELESS_VP9_FRAME) + return 0; + + /* + * The OUTPUT format is fixed while its queue streams and the scratch + * is sized from it: a larger frame cannot be decoded, as the run + * finds too. Before that the format may still be set to the stream's. + */ + vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE); + if (vb2_is_streaming(vq) && + !avd_vp9_frame_fits(frame, fmt_width(ctx), fmt_height(ctx))) + return -EINVAL; + + return avd_vp9_validate_frame(ctx, frame); } static unsigned int avd_vp9_pp_kicks(struct avd_ctx *ctx) diff --git a/drivers/media/platform/apple/avd/avd.h b/drivers/media/platform/apple/avd/avd.h index 7ec7d5a9fcd30..3f0e2b0eaa517 100644 --- a/drivers/media/platform/apple/avd/avd.h +++ b/drivers/media/platform/apple/avd/avd.h @@ -302,7 +302,12 @@ struct avd_segment { struct avd_job { enum avd_codec codec; + /* index of the segment being written */ size_t num; + /* segments allocated */ + size_t max; + /* an instruction did not fit; the job must not be submitted */ + bool overflow; struct avd_segment *segments; }; @@ -340,6 +345,12 @@ struct avd_ctx { void *priv; struct avd_comp comp; + /* + * Picture format the codec sized its per-stream scratch for at + * STREAMON, if it sizes any from the picture format. + */ + bool scratch_sized; + enum avd_image_fmt scratch_fmt; int fifo_idx; /* Pictures of this context the queue may hold at once (0 or 1: one). */ unsigned int max_frames; @@ -363,15 +374,48 @@ void avd_job_finish_no_pm(struct avd_ctx *ctx, enum vb2_buffer_state result); void avd_job_finish(struct avd_ctx *ctx, enum vb2_buffer_state result); void avd_run_preamble(struct avd_ctx *ctx, struct avd_run *run); +int avd_check_run_image_fmt(struct avd_ctx *ctx, enum avd_image_fmt image_fmt); void avd_run_postamble(struct avd_ctx *ctx, struct avd_run *run); +int avd_ctrl_copy(struct avd_ctx *ctx, u32 id, void *dst, size_t size); #if IS_ENABLED(CONFIG_KUNIT) +int avd_ctrl_copy_value(struct v4l2_ctrl *ctrl, void *dst, size_t size); bool avd_h264_find_slice_data(const u8 *data, u32 len, u32 header_len, u32 *off); bool avd_h264_slice_fits(const struct v4l2_ctrl_h264_sps *sps, size_t held, size_t held_bytes, u32 len, size_t decoded_size); bool avd_h264_scaling_is_flat(const struct v4l2_ctrl_h264_pps *pps, const struct v4l2_ctrl_h264_scaling_matrix *scaling); +int avd_hevc_check_sps_geometry(const struct v4l2_ctrl_hevc_sps *sps); +int avd_hevc_check_tiles(const struct v4l2_ctrl_hevc_sps *sps, + const struct v4l2_ctrl_hevc_pps *pps); +int avd_hevc_check_slices(const struct v4l2_ctrl_hevc_sps *sps, + const struct v4l2_ctrl_hevc_pps *pps, + const struct v4l2_ctrl_hevc_decode_params *decode, + const struct v4l2_ctrl_hevc_slice_params *slices, + u32 num_slices, const u32 *entry_points, + u32 num_entry_points, u32 payload); +int avd_hevc_kunit_compute_tiles(const struct v4l2_ctrl_hevc_sps *sps, + const struct v4l2_ctrl_hevc_pps *pps, + u32 *tile_ids, size_t n); +int avd_av1_check_tiles(struct device *dev, unsigned int tile_rows, + unsigned int tile_cols, + const struct v4l2_ctrl_av1_tile_group_entry *tile_group, + unsigned int entries, size_t payload); +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_h264_validate_sps(struct avd_ctx *ctx, + const struct v4l2_ctrl_h264_sps *sps); +size_t avd_h264_start_slices(const struct v4l2_ctrl_h264_sps *sps); +int avd_try_capture_fmt(struct file *file, void *priv, struct v4l2_format *f); +int avd_s_capture_fmt(struct file *file, void *priv, struct v4l2_format *f); +int avd_vp9_check_tiling(const struct v4l2_ctrl_vp9_frame *frame); +int avd_vp9_next_tile(const u8 *data, u32 payload, bool last, u32 *offset, + u32 *size); +bool avd_vp9_frame_fits(const struct v4l2_ctrl_vp9_frame *frame, u32 width, + u32 height); #endif extern const struct avd_coded_fmt_ops avd_h264_fmt_ops; diff --git a/drivers/media/platform/apple/avd/tests/av1-test.py b/drivers/media/platform/apple/avd/tests/av1-test.py index b073c8929fe69..df9a0e4ae1afe 100644 --- a/drivers/media/platform/apple/avd/tests/av1-test.py +++ b/drivers/media/platform/apple/avd/tests/av1-test.py @@ -77,7 +77,14 @@ static unsigned long vb2_get_plane_payload(struct vb2_buffer *b,unsigned int p){(void)b;(void)p;return 1UL<<20;} static struct vb2_v4l2_buffer *current_src,*current_dst; static struct vb2_v4l2_buffer *v4l2_m2m_next_src_buf(void *m){(void)m;return current_src;} -struct v4l2_ctrl {unsigned int id; unsigned int elems; union {void *p;} p_cur;}; +struct device; +struct v4l2_ctrl {unsigned int id; unsigned int elems, elem_size; union {void *p;} p_cur;}; +static void v4l2_ctrl_lock(struct v4l2_ctrl *c){(void)c;} +static void v4l2_ctrl_unlock(struct v4l2_ctrl *c){(void)c;} +static void *kmemdup_array(const void *p,size_t n,size_t size,int f){ + (void)f;void *d=calloc(n?n:1,size);if(d)memcpy(d,p,n*size);return d;} +/* The run-time format check has its own KUnit suite (avd-fmt-test.c). */ +static int avd_check_run_image_fmt(struct avd_ctx *ctx,int f){(void)ctx;(void)f;return 0;} struct v4l2_ctrl_ref {struct v4l2_ctrl_ref *next; struct v4l2_ctrl *ctrl; bool p_req_valid; union {void *p;} p_req;}; struct v4l2_ctrl_handler {struct v4l2_ctrl_ref *ctrl_refs;}; #define list_for_each_entry(ref,head,node) for((ref)=*(head);(ref);(ref)=(ref)->next) @@ -92,12 +99,14 @@ static struct v4l2_ctrl_handler *v4l2_ctrl_request_hdl_find(void *req,void *h){(void)h;assert(req==&request_hdl);request_gets++;return &request_hdl;} static void v4l2_ctrl_request_hdl_put(struct v4l2_ctrl_handler *h){assert(h==&request_hdl);request_puts++;} static struct v4l2_ctrl *v4l2_ctrl_find(void *h,u32 id){ - (void)h;controls[0].p_cur.p=&seq;controls[1].p_cur.p=¶ms;controls[2].p_cur.p=&tile;controls[2].elems=1;controls[3].p_cur.p=&grain; + (void)h;controls[0].p_cur.p=&seq;controls[1].p_cur.p=¶ms;controls[2].p_cur.p=&tile;controls[2].elems=1;controls[2].elem_size=sizeof(tile);controls[3].p_cur.p=&grain; if(id==V4L2_CID_STATELESS_AV1_SEQUENCE)return &controls[0]; if(id==V4L2_CID_STATELESS_AV1_FRAME)return &controls[1]; if(id==V4L2_CID_STATELESS_AV1_TILE_GROUP_ENTRY)return &controls[2]; return &controls[3]; } +static int avd_ctrl_copy(struct avd_ctx *ctx,u32 id,void *dst,size_t size){ + (void)ctx;memcpy(dst,v4l2_ctrl_find(NULL,id)->p_cur.p,size);return 0;} static void avd_run_preamble(struct avd_ctx *ctx,struct avd_run *run){ (void)ctx;memset(run,0,sizeof(*run));run->bufs.src=current_src;run->bufs.dst=current_dst; run->coded_in=current_src->vb2_buf.dma;run->comp_out=current_dst->vb2_buf.dma; @@ -106,7 +115,7 @@ static void avd_run_postamble(struct avd_ctx *ctx,struct avd_run *run){(void)ctx;(void)run;} static int avd_init_job(struct avd_ctx *ctx,int codec,size_t segments){ free(ctx->job.segments);ctx->job.codec=codec;ctx->job.num=0; - ctx->job.segments=calloc(segments,sizeof(*ctx->job.segments));return ctx->job.segments?0:-ENOMEM; + ctx->job.segments=calloc(segments,sizeof(*ctx->job.segments));ctx->job.overflow=false;ctx->job.max=ctx->job.segments?segments:0;return ctx->job.segments?0:-ENOMEM; } static int avd_submit_job(struct avd_ctx *ctx); ''' diff --git a/drivers/media/platform/apple/avd/tests/host_source.py b/drivers/media/platform/apple/avd/tests/host_source.py index b473b65e3c4ed..0251d1e3b833c 100644 --- a/drivers/media/platform/apple/avd/tests/host_source.py +++ b/drivers/media/platform/apple/avd/tests/host_source.py @@ -4,7 +4,8 @@ def function(source, name): - match = re.search(r"^[A-Za-z_][\w *]*\b" + re.escape(name) + + # The return type may sit on the line before the name. + match = re.search(r"^(?:[A-Za-z_][\w *]*\n)?(?:[A-Za-z_][\w *]*\b)?" + re.escape(name) + r"\([^;{}]*?\)\n\{", source, re.M) if not match: raise ValueError(name) diff --git a/drivers/media/platform/apple/avd/tests/program-test.py b/drivers/media/platform/apple/avd/tests/program-test.py index 5502470ca6aae..a0ee9b1d3b95d 100755 --- a/drivers/media/platform/apple/avd/tests/program-test.py +++ b/drivers/media/platform/apple/avd/tests/program-test.py @@ -55,8 +55,8 @@ #define AVD_MAX_INST 512 #define AVD_CODEC_AV1 3 #define AVD_MAX_FRAMES 16 -#define AVD_IMG_FMT_420_10BIT 1 -#define AVD_IMG_FMT_422_10BIT 2 +enum avd_image_fmt { AVD_IMG_FMT_ANY, AVD_IMG_FMT_420_8BIT, AVD_IMG_FMT_420_10BIT, + AVD_IMG_FMT_422_8BIT, AVD_IMG_FMT_422_10BIT }; #define AVD_FRAME_SUBMITTED 2 #define AVD_FRAME_DONE 3 #define IRQ_HANDLED 1 @@ -91,7 +91,7 @@ dma_addr_t fifo_iova; u32 cmd[24], tag; unsigned long deadline; int state,status; struct avd_ctx *ctx; }; struct avd_segment { u32 instructions[AVD_MAX_INST]; size_t num; }; -struct avd_job { struct avd_segment *segments; unsigned int num, codec; }; +struct avd_job { struct avd_segment *segments; unsigned int num, codec; size_t max; bool overflow; }; struct avd_dev; struct avd_variant { unsigned int quirks, revision, fifo_slots, vp_slots[4], vp_slot_base[4]; void (*configure_stream)(struct avd_dev *,dma_addr_t,u8,u32); }; @@ -265,7 +265,7 @@ decode.flags=idx%3?0:V4L2_H264_DECODE_PARAM_FLAG_IDR_PIC; decode.dpb[0].flags=idx%3?V4L2_H264_DPB_ENTRY_FLAG_VALID:0; sl.slice_type=idx%3==0?V4L2_H264_SLICE_TYPE_I:idx%3==1?V4L2_H264_SLICE_TYPE_P:V4L2_H264_SLICE_TYPE_B; - ctx.job.segments=calloc(3,sizeof(*ctx.job.segments)); assert(ctx.job.segments); + ctx.job.segments=calloc(3,sizeof(*ctx.job.segments)); assert(ctx.job.segments); ctx.job.max=3; stream_hdr(&ctx,&run); ctx.job.num=1;sl.first_mb_in_slice=0;slice.addr=0x200800000ULL; stream_slice(&ctx,&run); size_t continuation=1+ctx.job.segments[0].num+ctx.job.segments[1].num; @@ -418,15 +418,18 @@ def h264_prelude(): static struct vb2_v4l2_buffer *v4l2_m2m_next_src_buf(void *m) { (void)m;return fixture_run.base.bufs.src; } static size_t vb2_get_plane_payload(struct vb2_buffer *b,int p) { (void)b;(void)p;return valid_data?16:2; } static void *vb2_plane_vaddr(struct vb2_buffer *b,int p) { (void)b;(void)p;static u8 data[16];return data; } -static void avd_h264_run_preamble(struct avd_ctx *ctx,struct avd_h264_run *r) { (void)ctx;*r=fixture_run; } +static int avd_h264_run_preamble(struct avd_ctx *ctx,struct avd_h264_run *r) { (void)ctx;*r=fixture_run;return 0; } +/* The run-time format check has its own KUnit suite (avd-fmt-test.c). */ +static int avd_check_run_image_fmt(struct avd_ctx *ctx,int f) { (void)ctx;(void)f;return 0; } static void avd_run_postamble(struct avd_ctx *ctx,struct avd_run *r) { (void)ctx;(void)r;postambles++; } static bool avd_h264_slice_fits(const struct v4l2_ctrl_h264_sps *s,size_t n,size_t b,u32 l,size_t z) { (void)s;(void)b;(void)l;(void)z;return n<20; } static size_t avd_h264_max_slices(const struct v4l2_ctrl_h264_sps *s) { (void)s;return 20; } static int avd_h264_realloc_slices(struct avd_ctx *c,size_t n) { (void)c;(void)n;abort(); } static int avd_init_job(struct avd_ctx *c,unsigned int codec,size_t n) { - free(c->job.segments);c->job.codec=codec;c->job.num=0; - c->job.segments=calloc(n,sizeof(*c->job.segments));return c->job.segments?0:-ENOMEM; + free(c->job.segments);c->job.codec=codec;c->job.num=0;c->job.overflow=false; + c->job.segments=calloc(n,sizeof(*c->job.segments));c->job.max=c->job.segments?n:0; + return c->job.segments?0:-ENOMEM; } static void avd_job_finish(struct avd_ctx *c,enum vb2_buffer_state s) { (void)c;assert(s==VB2_BUF_STATE_DONE);held_finishes++; } static int avd_submit_job(struct avd_ctx *c) { submissions++;return avd_open_build_program(c); } @@ -457,6 +460,7 @@ def h264_prelude(): } /* Applied SPS controls reject unequal depths for both codecs. */ struct v4l2_ctrl_hevc_sps hevc={.chroma_format_idc=1, + .log2_min_luma_coding_block_size_minus3=1,.log2_diff_max_min_luma_coding_block_size=2, .pic_width_in_luma_samples=1920,.pic_height_in_luma_samples=1088}; ctx.coded_fmt.fmt.pix_mp.width=1920;ctx.coded_fmt.fmt.pix_mp.height=1088; for(unsigned int luma=0;luma<5;luma++) for(unsigned int chroma=0;chroma<5;chroma++) { @@ -475,7 +479,7 @@ def h264_prelude(): assert(!avd_h264_run(&ctx));assert(submissions==1&&hc.bank[0].submitted&&dst[0].bank==1); /* Compare the complete header to one built with the emitted VALID count. */ struct avd_segment expected={0};struct avd_job saved=ctx.job; - ctx.job=(struct avd_job){.segments=&expected};fixture_run.num_valid=2;fixture_run.cur_poc=40; + ctx.job=(struct avd_job){.segments=&expected,.max=1};fixture_run.num_valid=2;fixture_run.cur_poc=40; stream_hdr(&ctx,&fixture_run); assert(!memcmp((u32 *)programs[0].cpu+1,expected.instructions,expected.num*sizeof(u32))); ctx.job=saved; @@ -546,8 +550,9 @@ def prepare_regressions(out): 'avd_h264_abort_picture', 'avd_h264_refs_valid', 'avd_h264_done'): code += function(source, name) code += function((ROOT / 'avd-backend-open.c').read_text(), 'avd_open_build_program') + code += function((ROOT / 'avd-hevc.c').read_text(), 'avd_hevc_check_sps_geometry') code += function((ROOT / 'avd-hevc.c').read_text(), 'avd_hevc_validate_sps') - code += RUN_FIXTURE + function(source, 'avd_h264_run') + RUN_MAIN + code += RUN_FIXTURE + function(source, 'avd_h264_sps_image_fmt') + function(source, 'avd_h264_run') + RUN_MAIN dest = out / 'prepare.c' dest.write_text(code) exe = dest.with_suffix('') @@ -568,7 +573,7 @@ def prepare_regressions(out): struct avd_decoded_buffer destination = {0}; struct avd_segment segments[2]; struct avd_ctx ctx_storage = { .priv = &hc, .dev = &dev, - .fh.m2m_ctx = &m2m, .job.segments = segments, .decomp = true }; + .fh.m2m_ctx = &m2m, .job.segments = segments, .job.max = 2, .decomp = true }; struct avd_ctx *ctx = &ctx_storage; struct v4l2_ctrl_h264_decode_params dec = {0}; struct v4l2_ctrl_h264_sps sps = { .chroma_format_idc = 1, diff --git a/drivers/media/platform/apple/avd/tests/vp9-test.py b/drivers/media/platform/apple/avd/tests/vp9-test.py index 083c06e095a1f..c6ccf836b4636 100644 --- a/drivers/media/platform/apple/avd/tests/vp9-test.py +++ b/drivers/media/platform/apple/avd/tests/vp9-test.py @@ -84,7 +84,7 @@ static void avd_run_postamble(struct avd_ctx *ctx,struct avd_run *run) { (void)ctx;(void)run; } static int avd_init_job(struct avd_ctx *ctx,int codec,size_t segments) { free(ctx->job.segments);ctx->job.codec=codec;ctx->job.num=0; - ctx->job.segments=calloc(segments,sizeof(*ctx->job.segments));return ctx->job.segments?0:-ENOMEM; + ctx->job.segments=calloc(segments,sizeof(*ctx->job.segments));ctx->job.overflow=false;ctx->job.max=ctx->job.segments?segments:0;return ctx->job.segments?0:-ENOMEM; } static int avd_submit_job(struct avd_ctx *ctx); ''' diff --git a/drivers/media/v4l2-core/v4l2-ctrls-core.c b/drivers/media/v4l2-core/v4l2-ctrls-core.c index 648b88c868bc6..18718345a7ddd 100644 --- a/drivers/media/v4l2-core/v4l2-ctrls-core.c +++ b/drivers/media/v4l2-core/v4l2-ctrls-core.c @@ -634,6 +634,10 @@ validate_vp9_frame(struct v4l2_ctrl_vp9_frame *frame) if (frame->tile_cols_log2 > 6) return -EINVAL; + /* tile_rows_log2 is coded as up to two one-bit increments. */ + if (frame->tile_rows_log2 > 2) + return -EINVAL; + if (frame->reference_mode > V4L2_VP9_REFERENCE_MODE_SELECT) return -EINVAL; diff --git a/tools/asahi/avd-av1-tail-tests.py b/tools/asahi/avd-av1-tail-tests.py index 6f1d5a98b8f3f..df866290b8422 100644 --- a/tools/asahi/avd-av1-tail-tests.py +++ b/tools/asahi/avd-av1-tail-tests.py @@ -15,7 +15,8 @@ def function(source, name): - match = re.search(r'^(?:static )?(?:int|void|u64)\s+' + name + r'\(', source, re.M) + # The return type may sit on the line before the name. + match = re.search(r'^(?:static |VISIBLE_IF_KUNIT )?(?:int|void|u64|enum \w+|struct \w+ \*)\s+' + name + r'\(', source, re.M) if not match: raise ValueError(name) start = match.start() @@ -55,6 +56,10 @@ def structure(source, name): #define WARN_ON(x) (x) #define DIV_ROUND_UP(n,d) (((n)+(d)-1)/(d)) #define dev_err_ratelimited(...) ((void)0) +#define VISIBLE_IF_KUNIT +#define GFP_KERNEL 0 +struct device; +enum avd_image_fmt { AVD_IMG_FMT_ANY, AVD_IMG_FMT_420_8BIT, AVD_IMG_FMT_420_10BIT }; @ALIGNMENT@ @CDF_HEADER@ @CDF_DEFAULTS@ @@ -67,10 +72,22 @@ def structure(source, name): struct avd_buf {void *cpu; dma_addr_t addr;}; struct avd_run {struct {struct vb2_v4l2_buffer *src,*dst;} bufs; dma_addr_t y_out; int setup_err;}; @RUN_STRUCT@ +/* The preamble only uses the stream's copies of the controls. */ +struct avd_av1_stream { + struct {struct v4l2_ctrl_av1_sequence seq; struct v4l2_ctrl_av1_frame frame; struct v4l2_ctrl_av1_film_grain grain;} params; +}; @CTX_STRUCT@ -struct v4l2_ctrl {unsigned int elems; struct {void *p;} p_cur;}; +struct v4l2_ctrl {unsigned int elems, elem_size; struct {void *p;} p_cur;}; +static void v4l2_ctrl_lock(struct v4l2_ctrl *c){(void)c;} +static void v4l2_ctrl_unlock(struct v4l2_ctrl *c){(void)c;} +/* avd_av1_run() frees the run's copy; these tests only run the preamble. */ +static struct v4l2_ctrl_av1_tile_group_entry tile_copy[4]; +static void *kmemdup_array(const void *p,size_t n,size_t size,int f){(void)f;assert(n*size<=sizeof(tile_copy));memcpy(tile_copy,p,n*size);return tile_copy;} +/* The run-time format check has its own KUnit suite (avd-fmt-test.c). */ +static int avd_check_run_image_fmt(void *ctx,enum avd_image_fmt f){(void)ctx;(void)f;return 0;} +struct avd_dev {struct device *dev;}; struct v4l2_pix_format_mplane {unsigned int width,height; struct {unsigned int sizeimage;} plane_fmt[1];}; -struct avd_ctx {void *priv; int ctrl_hdl; struct {struct {struct {unsigned int width,height;} pix_mp;} fmt;} decoded_fmt;}; +struct avd_ctx {struct avd_dev *dev; void *priv; int ctrl_hdl; struct {struct {struct {unsigned int width,height;} pix_mp;} fmt;} decoded_fmt;}; static struct avd_decoded_buffer *reference; static struct v4l2_ctrl_av1_frame frame; static struct v4l2_ctrl_av1_sequence sequence={.bit_depth=8,.flags=V4L2_AV1_SEQUENCE_FLAG_SUBSAMPLING_X|V4L2_AV1_SEQUENCE_FLAG_SUBSAMPLING_Y}; @@ -83,7 +100,9 @@ def structure(source, name): static dma_addr_t vb2_dma_contig_plane_dma_addr(struct vb2_buffer *b,unsigned int p){assert(p==0);return b->dma;} static struct avd_decoded_buffer *vb2_to_avd_decoded_buf(struct vb2_buffer *b){return (void *)b;} static struct avd_decoded_buffer *avd_get_ref_buf(struct avd_ctx *ctx,struct vb2_v4l2_buffer *dst,uint64_t timestamp){(void)ctx;(void)dst;assert(timestamp==1000);lookup_calls++;return reference;} -static void avd_run_preamble(struct avd_ctx *ctx,struct avd_run *run){(void)ctx;(void)run;preamble_calls++;} +/* Like the real one, this hands the run its OUTPUT buffer. */ +static struct vb2_v4l2_buffer fixture_src; +static void avd_run_preamble(struct avd_ctx *ctx,struct avd_run *run){(void)ctx;if(!run->bufs.src)run->bufs.src=&fixture_src;preamble_calls++;} static struct v4l2_ctrl *v4l2_ctrl_find(int *h,unsigned int id){ (void)h; if(id==V4L2_CID_STATELESS_AV1_SEQUENCE)return &controls[0]; @@ -92,13 +111,17 @@ def structure(source, name): if(id==V4L2_CID_STATELESS_AV1_FILM_GRAIN)return &controls[3]; return NULL; } +static int avd_ctrl_copy(struct avd_ctx *ctx,unsigned int id,void *dst,size_t size){ + struct v4l2_ctrl *c=v4l2_ctrl_find(&ctx->ctrl_hdl,id);if(!c)return -EINVAL;memcpy(dst,c->p_cur.p,size);return 0;} ''' TESTS = r''' static size_t aligned_cdf(size_t length,size_t color){return (length&~255UL)-((color+255)&~255UL)-9216;} /* 3 bits per 4x4 block over 128x128 superblocks, 256-byte aligned. */ static size_t seg_bytes(size_t w,size_t h){return ALIGN(ALIGN(w,128)*ALIGN(h,128)/128*3,256);} -static void set_frame_ctx(struct avd_ctx *ctx){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;} +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;} 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); @@ -112,7 +135,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.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; 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; @@ -141,7 +164,7 @@ def structure(source, name): static int preamble(unsigned int w,unsigned int h,unsigned char primary,signed char idx){ 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.frame_height_minus_1=h-1;frame.primary_ref_frame=primary;if(primary<7)frame.ref_frame_idx[primary]=idx; + 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; run.base.bufs.dst=&dst.base.vb;run.base.y_out=0x120000000UL;return avd_av1_run_preamble(&ctx,&run); } static void guards(void){ @@ -165,7 +188,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', 'UNALIGNED_SEG_OFFSET(dst->'), 'seg-any-size': ('avd_av1_set_seg', 'DIV_ROUND_UP(ref->av1.width + 1, 8)', 'DIV_ROUND_UP(frame->frame_width_minus_1 + 1, 8)'), 'ref-index-upper': ('avd_av1_run_preamble', 'idx < 0 || idx >= V4L2_AV1_TOTAL_REFS_PER_FRAME', 'idx < 0'), - 'frame-width-unchecked': ('avd_av1_run_preamble', 'run->frame->frame_width_minus_1 + 1 > ctx->decoded_fmt.fmt.pix_mp.width ||', '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'), } if args.mutation: target, old, new = mutations[args.mutation]