From 825e7a503f2f6e369a9828d8ea014348ca080358 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 2 Nov 2026 13:46:01 +1100 Subject: [PATCH] media: apple: ave: encode HEVC Main 10 from P010 input Offer the HEVC Main 10 profile with P010 input on the J700 (9003) firmware. The Start fields that change with bit depth are the ones the macOS capture showed: the 8-bit flag, bit depth, profile idc, the compatibility and max-8-bit constraint bytes, the luma and chroma depths and the QP floor, which moves 12 down. Reference and tile buffers are already sized for 10-bit; the coded buffer grows by a quarter, as macOS sizes it. The firmware reads 16-bit samples for Main 10, so Main 10 takes P010 and Main takes NV12; other pairings are refused at STREAMON. The counted firmware layout keeps returning EOPNOTSUPP for 10-bit. KUnit checks that a Main 10 Start differs from a Main one only in those fields. Originally-by: Acelogic Link: https://github.com/aurora-silicon/linux-aurora/commit/3e77f272d9bb8940de8e744ead8d638e65f7500f Assisted-by: LLM Signed-off-by: Ryan Murray --- .../media/platform/apple/ave/ave-abi-test.c | 69 ++++++++++++++++++- drivers/media/platform/apple/ave/ave-sess.c | 29 +++++++- drivers/media/platform/apple/ave/ave-v4l2.c | 25 +++++-- drivers/media/platform/apple/ave/ave-writer.c | 2 +- drivers/media/platform/apple/ave/ave.h | 4 +- 5 files changed, 117 insertions(+), 12 deletions(-) diff --git a/drivers/media/platform/apple/ave/ave-abi-test.c b/drivers/media/platform/apple/ave/ave-abi-test.c index ec295b432a40a..5e3ed8e212d23 100644 --- a/drivers/media/platform/apple/ave/ave-abi-test.c +++ b/drivers/media/platform/apple/ave/ave-abi-test.c @@ -159,6 +159,72 @@ static void ave_9003_byte_equality(struct kunit *test) } } +/* + * HEVC Main 10 on 9003: compared with the same Main session, Start differs + * only in the fields the 10-30 capture showed changing with bit depth. + */ +static void ave_9003_hevc_main10(struct kunit *test) +{ + static const u32 changed[] = { + 0x208, 0x298, 0x308, 0x2598, 0x2938, 0x293c, 0x2960, + 0x5904, 0x5908, 0x592c, 0x5ad8, 0x5adc, + }; + struct ave_dev *ave = kunit_kzalloc(test, sizeof(*ave), GFP_KERNEL); + struct ave_ctx *ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + struct ave_session *sess = kunit_kzalloc(test, sizeof(*sess), GFP_KERNEL); + u8 *main8 = kunit_kzalloc(test, 0x14000, GFP_KERNEL); + u8 *main10 = kunit_kzalloc(test, 0x14000, GFP_KERNEL); + size_t len; + + KUNIT_ASSERT_NOT_NULL(test, ave); + KUNIT_ASSERT_NOT_NULL(test, ctx); + KUNIT_ASSERT_NOT_NULL(test, sess); + KUNIT_ASSERT_NOT_NULL(test, main8); + KUNIT_ASSERT_NOT_NULL(test, main10); + ave->layout = &ave_layout_9003_78_0; + ctx->ave = ave; + ctx->sess = sess; + len = ave->layout->bytes[AVE_CODEC_HEVC - 1][AVE_IMAGE_START]; + + ave_test_session(sess, AVE_CODEC_HEVC, 1920, 1080, true); + sess->p.min_qp = 0; + KUNIT_ASSERT_EQ(test, ave_build_start(ctx, main8, len), 0); + ave_test_session(sess, AVE_CODEC_HEVC, 1920, 1080, true); + sess->p.min_qp = -12; + sess->p.profile = 2; + sess->p.bit_depth = 10; + KUNIT_ASSERT_EQ(test, ave_build_start(ctx, main10, len), 0); + ave_stamp_command(ave->layout->abi, main8, 1, 2); + ave_stamp_command(ave->layout->abi, main10, 1, 2); + + KUNIT_EXPECT_EQ(test, get_unaligned_le32(main8 + 0x208), 1); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(main10 + 0x208), 0); + KUNIT_EXPECT_EQ(test, (s32)get_unaligned_le32(main10 + 0x298), -12); + KUNIT_EXPECT_EQ(test, (s32)get_unaligned_le32(main10 + 0x308), -12); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(main10 + 0x2598), 10); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(main10 + 0x2938), 2); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(main10 + 0x5904), 2); + KUNIT_EXPECT_EQ(test, main10[0x293d], 0); + KUNIT_EXPECT_EQ(test, main10[0x293e], 1); + KUNIT_EXPECT_EQ(test, main10[0x5909], 0); + KUNIT_EXPECT_EQ(test, main10[0x590a], 1); + KUNIT_EXPECT_EQ(test, main10[0x2962], 0); + KUNIT_EXPECT_EQ(test, main10[0x592e], 0); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(main10 + 0x5ad8), 2); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(main10 + 0x5adc), 2); + + for (size_t off = 0; off < len; off += 4) { + bool expected = false; + + for (unsigned int i = 0; i < ARRAY_SIZE(changed); i++) + expected |= off == changed[i]; + if (!expected) + KUNIT_EXPECT_EQ_MSG(test, get_unaligned_le32(main8 + off), + get_unaligned_le32(main10 + off), + "word +%#zx", off); + } +} + static void ave_6070_images(struct kunit *test) { const struct ave_fw_abi *abi = &ave_abi_6070; @@ -395,7 +461,8 @@ static void ave_6070_variants(struct kunit *test) } static struct kunit_case ave_abi_cases[] = { - KUNIT_CASE(ave_9003_byte_equality), KUNIT_CASE(ave_6070_images), + KUNIT_CASE(ave_9003_byte_equality), KUNIT_CASE(ave_9003_hevc_main10), + KUNIT_CASE(ave_6070_images), KUNIT_CASE(ave_6070_replies), KUNIT_CASE(ave_6070_split_surfaces), KUNIT_CASE(ave_6070_gate), KUNIT_CASE(ave_6070_channels), KUNIT_CASE(ave_6070_variants), {} diff --git a/drivers/media/platform/apple/ave/ave-sess.c b/drivers/media/platform/apple/ave/ave-sess.c index f46f8128667bf..902f6faf30fd7 100644 --- a/drivers/media/platform/apple/ave/ave-sess.c +++ b/drivers/media/platform/apple/ave/ave-sess.c @@ -36,6 +36,9 @@ #define AVE_H264_MAIN 4 #define AVE_H264_HIGH 6 #define AVE_HEVC_MAIN 1 +#define AVE_HEVC_MAIN_10 2 +/* QpBdOffset: 10-bit HEVC lets the QP go 12 below the 8-bit range. */ +#define AVE_QP_BD_OFFSET_10 12 #define AVE_META_BYTES 0xc000 /* Metadata words: coded bytes (twice), HEVC slice-header bytes, parameter sets */ @@ -114,6 +117,7 @@ static int ave_sess_params(struct ave_ctx *ctx, struct ave_params *p) p->cw = ALIGN(p->width, 16); p->ch = ALIGN(p->height, 16); p->stride = ctx->src_fmt.plane_fmt[0].bytesperline; + p->sample_bytes = ctx->src_fmt.pixelformat == V4L2_PIX_FMT_P010 ? 2 : 1; p->mbs = (p->cw / 16) * (p->ch / 16); p->fps_num = ctx->timeperframe.denominator; p->fps_den = ctx->timeperframe.numerator; @@ -144,6 +148,24 @@ static int ave_sess_params(struct ave_ctx *ctx, struct ave_params *p) if (p->min_qp > p->max_qp) p->min_qp = p->max_qp; + /* + * HEVC Main 10 encodes P010 input; Main encodes NV12. The firmware + * takes 16-bit samples for Main 10 (macOS converts 8-bit input to that + * first), so other pairings are refused rather than converted. + */ + if ((p->codec == AVE_CODEC_HEVC && + ctx->hevc_profile == V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_10) != + (p->sample_bytes == 2)) + return -EINVAL; + if (p->sample_bytes == 2) { + /* Only the 9003 layout's Main 10 fields are mapped. */ + if (ctx->ave->layout->counted) + return -EOPNOTSUPP; + p->profile = AVE_HEVC_MAIN_10; + p->bit_depth = 10; + p->min_qp -= AVE_QP_BD_OFFSET_10; + } + /* * Reference structures as macOS sets them up: a B-frame pyramid, * P frames only (low delay), or (H.264) intra only. @@ -180,6 +202,9 @@ static int ave_sess_params(struct ave_ctx *ctx, struct ave_params *p) p->coded_cap = ALIGN(3 * (p->codec == AVE_CODEC_HEVC ? ALIGN(p->width, 32) : ALIGN(p->width, 16)) * ALIGN(p->height, 16) / 2, SZ_16K); + /* 10-bit sessions get a quarter more, as macOS sizes them. */ + if (p->bit_depth == 10) + p->coded_cap = ALIGN(p->coded_cap * 5 / 4, SZ_16K); p->coded_cap = max_t(u32, p->coded_cap, AVE_CODED_MIN); if (ctx->ave->layout->counted && (p->fps_num != 30ULL * p->fps_den || @@ -361,7 +386,7 @@ static void ave_note_copy(struct ave_ctx *ctx, u32 stride, const char *how) } /* - * The input picture for @slot, as a DART address. The encoder reads NV12 + * The input picture for @slot, as a DART address. The encoder reads NV12 (P010) * with the 64-byte aligned stride S_FMT reported and chroma right after * height luma lines. Some clients (ffmpeg's v4l2m2m encoder) fill the * buffer with their own, smaller line size instead, which shows in the @@ -377,7 +402,7 @@ int ave_sess_input(struct ave_ctx *ctx, unsigned int slot, struct vb2_v4l2_buffe u32 off = vb->planes[0].data_offset; u32 used = vb2_get_plane_payload(vb, 0) - min(off, vb2_get_plane_payload(vb, 0)); u32 crows = DIV_ROUND_UP(p->height, 2), rows = p->height + crows; - u32 width = ALIGN(p->width, 2), stride; + u32 width = ALIGN(p->width, 2) * p->sample_bytes, stride; struct ave_buf *b = &sess->slot[slot].bounce; const u8 *from; u8 *to; diff --git a/drivers/media/platform/apple/ave/ave-v4l2.c b/drivers/media/platform/apple/ave/ave-v4l2.c index e01710ad07fe1..70ce7391a680f 100644 --- a/drivers/media/platform/apple/ave/ave-v4l2.c +++ b/drivers/media/platform/apple/ave/ave-v4l2.c @@ -54,6 +54,7 @@ static const u32 ave_coded_fmts[] = { static const u32 ave_raw_fmts[] = { V4L2_PIX_FMT_NV12, + V4L2_PIX_FMT_P010, }; static const struct v4l2_event ave_eos_event = { @@ -87,17 +88,22 @@ static void ave_clamp_size(u32 coded, u32 *w, u32 *h) } /* - * Raw NV12, one buffer: Y plane, then interleaved CbCr at half height. - * @coded is the context's coded format, which limits the size. + * Raw NV12 or P010, one buffer: Y plane, then interleaved CbCr at half + * height. P010 holds 10-bit samples in the high bits of 16-bit words; it + * is the input for HEVC Main 10. @coded is the context's coded format, + * which limits the size. */ static void ave_fill_raw(struct v4l2_pix_format_mplane *f, u32 coded) { - u32 stride, height; + u32 stride, height, bytes; - f->pixelformat = V4L2_PIX_FMT_NV12; + if (f->pixelformat != V4L2_PIX_FMT_P010 || coded != V4L2_PIX_FMT_HEVC) + f->pixelformat = V4L2_PIX_FMT_NV12; + bytes = f->pixelformat == V4L2_PIX_FMT_P010 ? 2 : 1; ave_clamp_size(coded, &f->width, &f->height); /* Keep a client's stride, within reason, rounded to 64 bytes. */ - stride = ALIGN(clamp_t(u32, f->plane_fmt[0].bytesperline, f->width, 2 * AVE_MAX_W), + stride = ALIGN(clamp_t(u32, f->plane_fmt[0].bytesperline, f->width * bytes, + 2 * AVE_MAX_W * bytes), AVE_STRIDE_ALIGN); height = ALIGN(f->height, AVE_STEP); f->num_planes = 1; @@ -449,8 +455,10 @@ static int ave_s_ctrl(struct v4l2_ctrl *ctrl) case V4L2_CID_MPEG_VIDEO_HEVC_MAX_QP: ctx->hevc_max_qp = ctrl->val; break; - case V4L2_CID_MPEG_VIDEO_HEADER_MODE: case V4L2_CID_MPEG_VIDEO_HEVC_PROFILE: + ctx->hevc_profile = ctrl->val; + break; + case V4L2_CID_MPEG_VIDEO_HEADER_MODE: case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT: break; default: @@ -510,8 +518,11 @@ static int ave_init_ctrls(struct ave_ctx *ctx) v4l2_ctrl_new_std(hdl, ops, V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 0); v4l2_ctrl_new_std(hdl, ops, V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51); + /* Main 10 takes P010 input; the profile is checked against it at STREAMON. */ v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_MPEG_VIDEO_HEVC_PROFILE, - V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN, 0, + V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_10, + ~(BIT(V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN) | + BIT(V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_10)), V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN); v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_MPEG_VIDEO_HEVC_LEVEL, V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1, 0, diff --git a/drivers/media/platform/apple/ave/ave-writer.c b/drivers/media/platform/apple/ave/ave-writer.c index 59f7d067bfc14..1e69481057cc8 100644 --- a/drivers/media/platform/apple/ave/ave-writer.c +++ b/drivers/media/platform/apple/ave/ave-writer.c @@ -261,7 +261,7 @@ static void ave_start_config(const struct ave_params *p, u8 *cmd, fw->counted ? 0x10000 | (hevc ? (depth == 8 ? 0x41 : 0x40) : 0) | (cq ? 0x10 : 0) : - hevc ? 1 : + hevc ? depth == 8 : 0); ave_start_put32(cmd, l->geometry_mode, (hevc ? 0x40 : 0) | (cq ? 0 : 2)); diff --git a/drivers/media/platform/apple/ave/ave.h b/drivers/media/platform/apple/ave/ave.h index 9f94eb1345290..17c776df0522f 100644 --- a/drivers/media/platform/apple/ave/ave.h +++ b/drivers/media/platform/apple/ave/ave.h @@ -227,7 +227,8 @@ struct ave_params { enum ave_codec codec; u32 width, height; /* visible picture */ u32 cw, ch; /* coded size, multiples of 16 */ - u32 stride; /* NV12 line stride, both planes */ + u32 stride; /* NV12 or P010 line stride in bytes, both planes */ + u32 sample_bytes; /* 1 for NV12, 2 for P010 */ u32 mbs; /* 16x16 macroblocks */ u32 fps_num, fps_den; /* frames per second = num / den */ u32 fps_int; /* the same, rounded */ @@ -325,6 +326,7 @@ struct ave_ctx { u32 h264_level; u32 h264_min_qp; u32 h264_max_qp; + u32 hevc_profile; u32 hevc_level; u32 hevc_min_qp; u32 hevc_max_qp;