diff --git a/drivers/media/platform/apple/avd/Makefile b/drivers/media/platform/apple/avd/Makefile index 66b8e3c61356a..be42b7f645da8 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-vp9-test.o obj-$(CONFIG_VIDEO_APPLE_AVD) += apple-avd.o diff --git a/drivers/media/platform/apple/avd/avd-backend-open.c b/drivers/media/platform/apple/avd/avd-backend-open.c index 13508cc5d5272..2681eeb63b777 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)) diff --git a/drivers/media/platform/apple/avd/avd-drv.c b/drivers/media/platform/apple/avd/avd-drv.c index 3de3f241430f5..c79379d2a50f2 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; diff --git a/drivers/media/platform/apple/avd/avd-h264.c b/drivers/media/platform/apple/avd/avd-h264.c index db4830e7aca22..6d2b4ff0643c3 100644 --- a/drivers/media/platform/apple/avd/avd-h264.c +++ b/drivers/media/platform/apple/avd/avd-h264.c @@ -854,6 +854,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; 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..31aee3e1d542b --- /dev/null +++ b/drivers/media/platform/apple/avd/avd-hevc-test.c @@ -0,0 +1,182 @@ +// 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); +} + +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), + {} +}; + +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..d655ba8c648bf 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" @@ -832,20 +833,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 { @@ -1046,9 +1052,80 @@ static enum avd_image_fmt avd_hevc_get_image_fmt(struct avd_ctx *ctx, return AVD_IMG_FMT_ANY; } +/* + * 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); + 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 +1369,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 +1412,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 +1427,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 +1502,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 +1547,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 +1563,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; } @@ -1505,10 +1645,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-vp9-test.c b/drivers/media/platform/apple/avd/avd-vp9-test.c new file mode 100644 index 0000000000000..469de40988618 --- /dev/null +++ b/drivers/media/platform/apple/avd/avd-vp9-test.c @@ -0,0 +1,61 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit tests for the VP9 frame checks of the Apple video decoder. + */ + +#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); +} + +static struct kunit_case avd_vp9_test_cases[] = { + KUNIT_CASE_PARAM(avd_vp9_tiling_test, avd_vp9_tiling_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..71b80796fd090 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 @@ -780,6 +781,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 +831,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 +846,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) @@ -831,8 +875,6 @@ static int avd_vp9_run_preamble(struct avd_ctx *ctx, struct avd_vp9_run *run) if (ret) return ret; - run->decode_params = dec_params; - if (avd_backend_is_qfw(ctx->dev)) { struct avd_vp9_bank *bank = NULL; @@ -869,7 +911,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 +919,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); @@ -909,7 +950,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) \ diff --git a/drivers/media/platform/apple/avd/avd.h b/drivers/media/platform/apple/avd/avd.h index 7ec7d5a9fcd30..6fee27521eed2 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; }; @@ -372,6 +377,13 @@ 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_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_vp9_check_tiling(const struct v4l2_ctrl_vp9_frame *frame); #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..b133465c5a1b4 100644 --- a/drivers/media/platform/apple/avd/tests/av1-test.py +++ b/drivers/media/platform/apple/avd/tests/av1-test.py @@ -106,7 +106,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..8d681bda35886 100755 --- a/drivers/media/platform/apple/avd/tests/program-test.py +++ b/drivers/media/platform/apple/avd/tests/program-test.py @@ -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; @@ -425,8 +425,9 @@ def h264_prelude(): 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 +458,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 +477,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,6 +548,7 @@ 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 dest = out / 'prepare.c' @@ -568,7 +571,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;