From bf5971b922639f26e16e537f479ca3421432f527 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Wed, 2 Dec 2026 18:13:17 +1100 Subject: [PATCH 1/2] media: apple: ave: enable HEVC Main 10 (P010) on the 6070 encoder Enable 10-bit HEVC Main 10 encoding from P010 input on the 6070 firmware layout (M1 Pro / T6000 and T8112 13.5). The 6070 Start layout already maps the profile, compatibility flags, luma/chroma bit depth, input depth, and 10-bit reconstruction surface layout, and the session allocator already scales reconstruction and entropy channel buffers for 10-bit. Lift the layout gate in ave_main10_supported() and ave_sess_params(), scale the 6070 entropy aux channels alongside the main entropy channels, adjust the 6070 CQ QP floor by -12 (QpBdOffset) and Main compatibility flag for 10-bit, and populate the 6070 Process reconstruction pointers for 10-bit frames. Add KUnit coverage (ave_6070_hevc_main10) checking the 6070 Main 10 Start and Process command fields. Signed-off-by: Ryan Murray --- .../media/platform/apple/ave/ave-abi-test.c | 63 ++++++++++++++++++- drivers/media/platform/apple/ave/ave-sess.c | 14 ++--- drivers/media/platform/apple/ave/ave-v4l2.c | 8 +-- drivers/media/platform/apple/ave/ave-writer.c | 10 +-- drivers/media/platform/apple/ave/ave.h | 3 + drivers/media/platform/apple/ave/tests/run.py | 3 +- 6 files changed, 80 insertions(+), 21 deletions(-) diff --git a/drivers/media/platform/apple/ave/ave-abi-test.c b/drivers/media/platform/apple/ave/ave-abi-test.c index a818eb9a2d546..735676a1cced8 100644 --- a/drivers/media/platform/apple/ave/ave-abi-test.c +++ b/drivers/media/platform/apple/ave/ave-abi-test.c @@ -2177,10 +2177,71 @@ static void ave_m3_probe_only(struct kunit *test) KUNIT_EXPECT_EQ(test, ave_fw_check_boot(ave), -EOPNOTSUPP); } +static void ave_6070_hevc_main10(struct kunit *test) +{ + 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); + struct ave_6070_avc_recon_layout recon; + const struct ave_start_layout *sl = &ave_layout_6070_11_1.start[1]; + const struct ave_proc_layout *pl = &ave_layout_6070_11_1.process[1]; + size_t slen = ave_layout_6070_11_1.bytes[1][AVE_IMAGE_START]; + size_t plen = ave_layout_6070_11_1.bytes[1][AVE_IMAGE_PROCESS]; + u8 *start = kunit_kzalloc(test, slen, GFP_KERNEL); + u8 *proc = kunit_kzalloc(test, plen, GFP_KERNEL); + + KUNIT_ASSERT_NOT_NULL(test, ave); + KUNIT_ASSERT_NOT_NULL(test, ctx); + KUNIT_ASSERT_NOT_NULL(test, sess); + KUNIT_ASSERT_NOT_NULL(test, start); + KUNIT_ASSERT_NOT_NULL(test, proc); + + ave_test_6070_session(test, ave, ctx, sess, AVE_CODEC_HEVC, 1920, 1080); + sess->p.profile = 2; + sess->p.bit_depth = 10; + sess->p.sample_bytes = 2; + sess->p.stride = ALIGN(1920 * 2, 64); + sess->p.min_qp = -AVE_QP_BD_OFFSET_10; + sess->p.rc_mode = 3; + sess->p.initial_qp = 26; + + KUNIT_ASSERT_EQ(test, ave_build_start(ctx, start, slen), 0); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(start + sl->hevc_profile_a), 2U); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(start + sl->hevc_profile_b), 2U); + KUNIT_EXPECT_EQ(test, start[sl->hevc_compat_a + 1], 0U); + KUNIT_EXPECT_EQ(test, start[sl->hevc_compat_a + 2], 1U); + KUNIT_EXPECT_EQ(test, start[sl->hevc_compat_b + 1], 0U); + KUNIT_EXPECT_EQ(test, start[sl->hevc_compat_b + 2], 1U); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(start + sl->hevc_depth_luma), 2U); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(start + sl->hevc_depth_chroma), 2U); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(start + sl->hevc_input_depth), 10U); + KUNIT_EXPECT_EQ(test, (s32)get_unaligned_le32(start + sl->min_qp), -12); + + recon = ave_6070_recon_layout(1920, 1080, 10); + KUNIT_EXPECT_EQ(test, get_unaligned_le64(start + sl->recon.address), + sess->ref_a[0].dma + recon.luma_data_offset); + KUNIT_EXPECT_EQ(test, get_unaligned_le64(start + sl->recon.address + 8), + sess->ref_a[0].dma + recon.luma_metadata_offset); + + KUNIT_ASSERT_EQ(test, ave_build_process(ctx, proc, plen, 0, + 0x10000000ULL, 0, false, false), 0); + KUNIT_EXPECT_EQ(test, get_unaligned_le64(proc + pl->current_recon.luma_data), + sess->ref_a[0].dma + recon.luma_data_offset); + KUNIT_EXPECT_EQ(test, get_unaligned_le64(proc + pl->current_recon.chroma_data), + sess->ref_a[0].dma + recon.chroma_data_offset); + KUNIT_EXPECT_EQ(test, get_unaligned_le64(proc + pl->current_recon.luma_metadata), + sess->ref_a[0].dma + recon.luma_metadata_offset); + KUNIT_EXPECT_EQ(test, get_unaligned_le64(proc + pl->current_recon.chroma_metadata), + sess->ref_a[0].dma + recon.chroma_metadata_offset); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(proc + pl->y + 8), sess->p.stride); + KUNIT_EXPECT_EQ(test, get_unaligned_le64(proc + pl->uv), + 0x10000000ULL + (u64)sess->p.stride * 1080); +} + static struct kunit_case ave_abi_cases[] = { KUNIT_CASE(ave_t6041_text_boot_word), KUNIT_CASE(ave_9003_byte_equality), KUNIT_CASE(ave_9003_hevc_main10), - KUNIT_CASE(ave_26a428_hevc_main10), + KUNIT_CASE(ave_26a428_hevc_main10), KUNIT_CASE(ave_6070_hevc_main10), KUNIT_CASE(ave_9003_speed), KUNIT_CASE(ave_cq_table), KUNIT_CASE(ave_6070_images), KUNIT_CASE(ave_6070_replies), KUNIT_CASE(ave_6070_frame_result), diff --git a/drivers/media/platform/apple/ave/ave-sess.c b/drivers/media/platform/apple/ave/ave-sess.c index 76cd9dfdbf626..2e34b207ad979 100644 --- a/drivers/media/platform/apple/ave/ave-sess.c +++ b/drivers/media/platform/apple/ave/ave-sess.c @@ -37,8 +37,6 @@ #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_PARAMS 0xb2a4 #define AVE_META_SCAN_FROM 0x200 @@ -183,12 +181,10 @@ static int ave_sess_params(struct ave_ctx *ctx, struct ave_params *p) p->sample_bytes == 2 ? "10-bit P010" : "8-bit NV12"); return -EINVAL; } - if (ctx->ave->layout->kind == AVE_LAYOUT_6070 && p->sample_bytes != 1) - return -EOPNOTSUPP; /* - * The 6070 path supports 8-bit H.264 and HEVC Main, including one B per - * P, with frame widths in whole macroblocks. Wider-than-coded widths are - * not yet supported. + * The 6070 path supports 8-bit H.264 and HEVC Main/Main 10, including + * one B per P, with frame widths in whole macroblocks. Wider-than-coded + * widths are not yet supported. */ if (ctx->ave->layout->kind == AVE_LAYOUT_6070 && p->width % 16) return -EOPNOTSUPP; @@ -429,9 +425,7 @@ static int ave_sess_alloc(struct ave_dev *ave, struct ave_session *sess) for (unsigned int i = 0; i < entropy_tiles; i++) { ave_req(r, &n, &sess->tile[set][i], tile, 0, nomap); ave_req(r, &n, &sess->tile_aux[set][i], - fw6070 ? ave_6070_entropy_aux_size(w, h, hevc) : - ALIGN(h, 128), 0, - nomap); + fw6070 ? tile : ALIGN(h, 128), 0, nomap); } if (hevc) for (unsigned int i = 0; i < 2; i++) diff --git a/drivers/media/platform/apple/ave/ave-v4l2.c b/drivers/media/platform/apple/ave/ave-v4l2.c index 3000807af8339..8435221ab0adf 100644 --- a/drivers/media/platform/apple/ave/ave-v4l2.c +++ b/drivers/media/platform/apple/ave/ave-v4l2.c @@ -78,14 +78,14 @@ static bool ave_fmt_listed(const u32 *list, unsigned int n, u32 fourcc) } /* - * HEVC Main 10 (P010 input): the 9003 and the counted (26A428) layouts, - * whose Starts map every field that changes with bit depth. The 6070 - * layout has none. + * HEVC Main 10 (P010 input): supported on 9003, counted (26A428/26B123), + * and 6070 layouts, whose Starts map every field that changes with bit depth. */ static bool ave_main10_supported(const struct ave_dev *ave) { return ave->layout && (ave->layout->counted || - ave->layout->kind == AVE_LAYOUT_9003); + ave->layout->kind == AVE_LAYOUT_9003 || + ave->layout->kind == AVE_LAYOUT_6070); } /* diff --git a/drivers/media/platform/apple/ave/ave-writer.c b/drivers/media/platform/apple/ave/ave-writer.c index 6405d4479b95b..3c07f34fa5e2e 100644 --- a/drivers/media/platform/apple/ave/ave-writer.c +++ b/drivers/media/platform/apple/ave/ave-writer.c @@ -811,7 +811,7 @@ static void ave_start_config(const struct ave_params *p, u8 *cmd, ave_start_put64(cmd, l->f64_2c0, AVE_F64_NEG | ave_f64(1, 1)); ave_start_put64(cmd, l->f64_2c8, AVE_F64_NEG | ave_f64(1, 1)); if (cq && fw->kind == AVE_LAYOUT_6070) { - min_qp = 0; + min_qp = depth == 10 ? -AVE_QP_BD_OFFSET_10 : 0; max_qp = 51; } ave_start_put32(cmd, l->min_qp, min_qp); @@ -973,9 +973,9 @@ static void ave_start_config(const struct ave_params *p, u8 *cmd, ave_start_put32(cmd, l->hevc_profile_a, p->profile); ave_start_put32(cmd, l->hevc_profile_b, p->profile); if (fw->kind == AVE_LAYOUT_6070) { - cmd[l->hevc_compat_a + 1] = 1; + cmd[l->hevc_compat_a + 1] = depth == 8; cmd[l->hevc_compat_a + 2] = 1; - cmd[l->hevc_compat_b + 1] = 1; + cmd[l->hevc_compat_b + 1] = depth == 8; cmd[l->hevc_compat_b + 2] = 1; cmd[l->hevc_8bit_a] = 1; cmd[l->hevc_8bit_a + 2] = 1; @@ -1180,9 +1180,9 @@ int ave_build_process(struct ave_ctx *ctx, u8 *cmd, size_t len, } } ave_put32(cmd, l->pic_size, 0xf68); - if (p->bit_depth == 8) { + if (!p->bit_depth || p->bit_depth == 8 || p->bit_depth == 10) { recon = ave_6070_recon_layout(p->width, p->height, - p->bit_depth); + p->bit_depth ?: 8); ave_write_recon_selection(cmd, &l->current_recon, &sess->ref_a[pic.current_ref], &recon); ave_put64(cmd, l->current_colocated, diff --git a/drivers/media/platform/apple/ave/ave.h b/drivers/media/platform/apple/ave/ave.h index 248bab6ff11e8..932beb8356c09 100644 --- a/drivers/media/platform/apple/ave/ave.h +++ b/drivers/media/platform/apple/ave/ave.h @@ -508,6 +508,9 @@ struct ave_frame_result { #define AVE_6070_LOWRES_RESULTS 4 #define AVE_6070_AVC_ENTROPY_TILES 2 +/* H.265 QpBdOffsetY/C for 10-bit (6 * (10 - 8)). */ +#define AVE_QP_BD_OFFSET_10 12 + /* Fixed 6070 host-interface allocations, independent of picture geometry. */ #define AVE_6070_CLIENT_MEM_BYTES SZ_1M #define AVE_6070_CODED_HEADER_BYTES 0x23000 diff --git a/drivers/media/platform/apple/ave/tests/run.py b/drivers/media/platform/apple/ave/tests/run.py index a51347bddb3a8..4c9bea7be9dd0 100644 --- a/drivers/media/platform/apple/ave/tests/run.py +++ b/drivers/media/platform/apple/ave/tests/run.py @@ -56,7 +56,8 @@ def build(src, out, baseline): code += m[0] + '\n' # Later firmware builds; an older baseline does not define them. for name in ('AVE_MCTF_BUFS', 'AVE_MCTF_IN_BUFS', - 'AVE_MCTF_OUT_BUFS', 'AVE_MCTF_POOL_LENT', 'AVE_MCTF_POOL_READY'): + 'AVE_MCTF_OUT_BUFS', 'AVE_MCTF_POOL_LENT', 'AVE_MCTF_POOL_READY', + 'AVE_QP_BD_OFFSET_10'): m = re.search(r'^#define ' + name + r'\s+[^\n]+', header, re.M) if m: code += m[0] + '\n' From 04227a3dd7e7de0490c1b63c58c71c28957b5a5a Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Wed, 2 Dec 2026 18:21:03 +1100 Subject: [PATCH 2/2] media: apple: ave: signal HEVC VUI colorimetry and HDR10 transfer in SPS Map V4L2 OUTPUT format colorimetry (colorspace, xfer_func, ycbcr_enc, quantization) to ITU-T H.273 / H.265 Annex E VUI parameters and write them into the HEVC Start command across the 9003, 26A428/26B123, and 6070 firmware layouts. When V4L2 colorimetry is set (including Rec.709 SDR, BT.2020 + SMPTE ST 2084 PQ HDR10, and full/limited range), set vui_parameters_present_flag, video_signal_type_present_flag, video_format (unspecified), video_full_range_flag, colour_description_present_flag, colour_primaries, transfer_characteristics, and matrix_coeffs in the HEVC SPS template so decoders identify BT.709/BT.2020/HDR10 and range from the bitstream. Add KUnit coverage (ave_hevc_vui_colorimetry) across all four layouts while keeping ave_9003_byte_equality unchanged. Signed-off-by: Ryan Murray --- .../media/platform/apple/ave/ave-abi-test.c | 57 +++++++++++ drivers/media/platform/apple/ave/ave-layout.c | 3 + drivers/media/platform/apple/ave/ave-layout.h | 1 + drivers/media/platform/apple/ave/ave-sess.c | 94 +++++++++++++++++++ drivers/media/platform/apple/ave/ave-writer.c | 26 +++++ drivers/media/platform/apple/ave/ave.h | 5 + 6 files changed, 186 insertions(+) diff --git a/drivers/media/platform/apple/ave/ave-abi-test.c b/drivers/media/platform/apple/ave/ave-abi-test.c index 735676a1cced8..97308093c3cf1 100644 --- a/drivers/media/platform/apple/ave/ave-abi-test.c +++ b/drivers/media/platform/apple/ave/ave-abi-test.c @@ -2238,10 +2238,67 @@ static void ave_6070_hevc_main10(struct kunit *test) 0x10000000ULL + (u64)sess->p.stride * 1080); } +static void ave_hevc_vui_colorimetry(struct kunit *test) +{ + static const struct ave_fw_layout * const layouts[] = { + &ave_layout_9003_78_0, + &ave_layout_26a428, + &ave_layout_26b123, + &ave_layout_6070_11_1, + }; + static const u32 vui_off[] = { 0x74a0, 0x80a4, 0x80a4, 0x262bc }; + + for (unsigned int i = 0; i < ARRAY_SIZE(layouts); i++) { + const struct ave_fw_layout *fw = layouts[i]; + 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); + size_t len = fw->bytes[1][AVE_IMAGE_START]; + u8 *cmd = kunit_kzalloc(test, len, GFP_KERNEL); + + KUNIT_ASSERT_NOT_NULL(test, ave); + KUNIT_ASSERT_NOT_NULL(test, ctx); + KUNIT_ASSERT_NOT_NULL(test, sess); + KUNIT_ASSERT_NOT_NULL(test, cmd); + + if (fw->kind == AVE_LAYOUT_6070) { + ave_test_6070_session(test, ave, ctx, sess, AVE_CODEC_HEVC, 1920, 1080); + } else { + ave->layout = fw; + ctx->ave = ave; + ctx->sess = sess; + ave_test_session(sess, AVE_CODEC_HEVC, 1920, 1080, true); + } + sess->p.profile = 2; + sess->p.bit_depth = 10; + sess->p.vui_color = true; + sess->p.vui_primaries = 9; + sess->p.vui_transfer = 16; + sess->p.vui_matrix = 9; + sess->p.vui_full_range = 1; + + KUNIT_ASSERT_EQ(test, ave_build_start(ctx, cmd, len), 0); + KUNIT_EXPECT_EQ_MSG(test, cmd[vui_off[i]], 1U, "layout %u", i); + KUNIT_EXPECT_EQ_MSG(test, cmd[vui_off[i] + 0x11], 1U, "layout %u", i); + KUNIT_EXPECT_EQ_MSG(test, get_unaligned_le32(cmd + vui_off[i] + 0x14), + 5U, "layout %u", i); + KUNIT_EXPECT_EQ_MSG(test, get_unaligned_le32(cmd + vui_off[i] + 0x18), + 1U, "layout %u", i); + KUNIT_EXPECT_EQ_MSG(test, cmd[vui_off[i] + 0x1c], 1U, "layout %u", i); + KUNIT_EXPECT_EQ_MSG(test, get_unaligned_le32(cmd + vui_off[i] + 0x20), + 9U, "layout %u", i); + KUNIT_EXPECT_EQ_MSG(test, get_unaligned_le32(cmd + vui_off[i] + 0x24), + 16U, "layout %u", i); + KUNIT_EXPECT_EQ_MSG(test, get_unaligned_le32(cmd + vui_off[i] + 0x28), + 9U, "layout %u", i); + } +} + static struct kunit_case ave_abi_cases[] = { KUNIT_CASE(ave_t6041_text_boot_word), KUNIT_CASE(ave_9003_byte_equality), KUNIT_CASE(ave_9003_hevc_main10), KUNIT_CASE(ave_26a428_hevc_main10), KUNIT_CASE(ave_6070_hevc_main10), + KUNIT_CASE(ave_hevc_vui_colorimetry), KUNIT_CASE(ave_9003_speed), KUNIT_CASE(ave_cq_table), KUNIT_CASE(ave_6070_images), KUNIT_CASE(ave_6070_replies), KUNIT_CASE(ave_6070_frame_result), diff --git a/drivers/media/platform/apple/ave/ave-layout.c b/drivers/media/platform/apple/ave/ave-layout.c index 4cf58df303c8c..1ebe4034a2eff 100644 --- a/drivers/media/platform/apple/ave/ave-layout.c +++ b/drivers/media/platform/apple/ave/ave-layout.c @@ -211,6 +211,7 @@ const struct ave_fw_layout ave_layout_9003_78_0 = { .hevc_dpb_minus1 = 0x5b00, .hevc_reorder = 0x5b04, .hevc_reorder_on = 0x95fc, + .hevc_vui = 0x74a0, }, }, .process = { @@ -486,6 +487,7 @@ const struct ave_fw_layout ave_layout_9003_78_0 = { .hevc_cq_copy = 0xbefc, \ .hevc_cq_codec = 0x37c8, \ .hevc_cq_picture = 0x9e38, \ + .hevc_vui = 0x80a4, \ }, \ }, \ .process = { \ @@ -821,6 +823,7 @@ const struct ave_fw_layout ave_layout_6070_11_1 = { .hevc_sps_scaling_enabled = 0x24970, .hevc_sps_sao = 0x2621d, .hevc_sps_tmvp = 0x262b8, + .hevc_vui = 0x262bc, .hevc_sps_fw_header = 0x26375, .hevc_sps_header_len = 0x26378, .hevc_pps_extra_slice_bits = 0x2848c, diff --git a/drivers/media/platform/apple/ave/ave-layout.h b/drivers/media/platform/apple/ave/ave-layout.h index d499102fb2adb..edb133b597e07 100644 --- a/drivers/media/platform/apple/ave/ave-layout.h +++ b/drivers/media/platform/apple/ave/ave-layout.h @@ -290,6 +290,7 @@ struct ave_start_layout { u32 hevc_sps_scaling_enabled; u32 hevc_sps_sao; u32 hevc_sps_tmvp; + u32 hevc_vui; u32 hevc_sps_fw_header; u32 hevc_sps_header_len; u32 hevc_pps_extra_slice_bits; diff --git a/drivers/media/platform/apple/ave/ave-sess.c b/drivers/media/platform/apple/ave/ave-sess.c index 2e34b207ad979..af11feca86d57 100644 --- a/drivers/media/platform/apple/ave/ave-sess.c +++ b/drivers/media/platform/apple/ave/ave-sess.c @@ -122,6 +122,86 @@ u32 ave_cq_initial_qp(u32 quality) return ave_cq_qp[min(quality, 100U)]; } +/* Map V4L2 colorimetry to ITU-T H.273 / H.265 Table E.3..E.5 VUI codes. */ +static u8 ave_vui_primaries(enum v4l2_colorspace colorspace) +{ + switch (colorspace) { + case V4L2_COLORSPACE_REC709: + case V4L2_COLORSPACE_SRGB: + case V4L2_COLORSPACE_JPEG: + return 1; + case V4L2_COLORSPACE_470_SYSTEM_M: + return 4; + case V4L2_COLORSPACE_470_SYSTEM_BG: + return 5; + case V4L2_COLORSPACE_SMPTE170M: + return 6; + case V4L2_COLORSPACE_SMPTE240M: + return 7; + case V4L2_COLORSPACE_BT2020: + return 9; + case V4L2_COLORSPACE_DCI_P3: + return 11; + default: + return 2; + } +} + +static u8 ave_vui_transfer(enum v4l2_colorspace colorspace, + enum v4l2_xfer_func xfer_func, u32 bit_depth) +{ + if (xfer_func == V4L2_XFER_FUNC_DEFAULT) + xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(colorspace); + switch (xfer_func) { + case V4L2_XFER_FUNC_709: + return colorspace == V4L2_COLORSPACE_BT2020 && + bit_depth == 10 ? 14 : 1; + case V4L2_XFER_FUNC_SMPTE240M: + return 7; + case V4L2_XFER_FUNC_NONE: + return 8; + case V4L2_XFER_FUNC_SRGB: + return 13; + case V4L2_XFER_FUNC_SMPTE2084: + return 16; + default: + return 2; + } +} + +static u8 ave_vui_matrix(enum v4l2_colorspace colorspace, + enum v4l2_ycbcr_encoding ycbcr_enc) +{ + if (ycbcr_enc == V4L2_YCBCR_ENC_DEFAULT) + ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(colorspace); + switch (ycbcr_enc) { + case V4L2_YCBCR_ENC_709: + case V4L2_YCBCR_ENC_XV709: + return 1; + case V4L2_YCBCR_ENC_601: + case V4L2_YCBCR_ENC_XV601: + return colorspace == V4L2_COLORSPACE_470_SYSTEM_BG ? 5 : 6; + case V4L2_YCBCR_ENC_SMPTE240M: + return 7; + case V4L2_YCBCR_ENC_BT2020: + return 9; + case V4L2_YCBCR_ENC_BT2020_CONST_LUM: + return 10; + default: + return 2; + } +} + +static u8 ave_vui_full_range(enum v4l2_colorspace colorspace, + enum v4l2_ycbcr_encoding ycbcr_enc, + enum v4l2_quantization quantization) +{ + if (quantization == V4L2_QUANTIZATION_DEFAULT) + quantization = V4L2_MAP_QUANTIZATION_DEFAULT(false, colorspace, + ycbcr_enc); + return quantization == V4L2_QUANTIZATION_FULL_RANGE ? 1 : 0; +} + static int ave_sess_params(struct ave_ctx *ctx, struct ave_params *p) { bool baseline = false; @@ -194,6 +274,20 @@ static int ave_sess_params(struct ave_ctx *ctx, struct ave_params *p) p->bit_depth = 10; p->min_qp -= AVE_QP_BD_OFFSET_10; } + if (p->codec == AVE_CODEC_HEVC && + ctx->src_fmt.colorspace != V4L2_COLORSPACE_DEFAULT) { + p->vui_primaries = ave_vui_primaries(ctx->src_fmt.colorspace); + p->vui_transfer = ave_vui_transfer(ctx->src_fmt.colorspace, + ctx->src_fmt.xfer_func, + p->bit_depth); + p->vui_matrix = ave_vui_matrix(ctx->src_fmt.colorspace, + ctx->src_fmt.ycbcr_enc); + p->vui_full_range = ave_vui_full_range(ctx->src_fmt.colorspace, + ctx->src_fmt.ycbcr_enc, + ctx->src_fmt.quantization); + p->vui_color = p->vui_primaries != 2 || p->vui_transfer != 2 || + p->vui_matrix != 2 || p->vui_full_range; + } /* * Reference structures as macOS sets them up: a B-frame pyramid, diff --git a/drivers/media/platform/apple/ave/ave-writer.c b/drivers/media/platform/apple/ave/ave-writer.c index 3c07f34fa5e2e..47217018a5591 100644 --- a/drivers/media/platform/apple/ave/ave-writer.c +++ b/drivers/media/platform/apple/ave/ave-writer.c @@ -526,6 +526,17 @@ static void ave_start_put8(u8 *cmd, u32 off, u8 val) /* IPPP selects 0..3; IbP selects 0..6 and has a possible index-7 arm. */ #define AVE_6070_HEVC_RPS_SETS_LOW_DELAY 4 #define AVE_6070_HEVC_RPS_SETS_REORDER 8 +/* HEVC SPS VUI fields relative to l->hevc_vui (ITU-T H.265 Annex E). */ +#define AVE_HEVC_VUI_PRESENT 0x00 /* u8 */ +#define AVE_HEVC_VUI_VIDEO_SIGNAL_PRESENT 0x11 /* u8 */ +#define AVE_HEVC_VUI_VIDEO_FORMAT 0x14 /* u32 */ +#define AVE_HEVC_VUI_FULL_RANGE 0x18 /* u32 */ +#define AVE_HEVC_VUI_COLOUR_DESC_PRESENT 0x1c /* u8 */ +#define AVE_HEVC_VUI_COLOUR_PRIMARIES 0x20 /* u32 */ +#define AVE_HEVC_VUI_TRANSFER_CHAR 0x24 /* u32 */ +#define AVE_HEVC_VUI_MATRIX_COEFFS 0x28 /* u32 */ +#define AVE_HEVC_VUI_VIDEO_FORMAT_UNSPECIFIED 5 + /* HEVC Start constants (H.265 A.3 and 7.4.3.2). */ #define AVE_6070_HEVC_INPUT_CHROMA_FROM_SPS 16 /* bits [4:2] = 4: use SPS format */ #define AVE_6070_HEVC_CHROMA_420 1 /* H.265 chroma_format_idc */ @@ -1061,6 +1072,21 @@ static void ave_start_config(const struct ave_params *p, u8 *cmd, ave_start_put8(cmd, l->hevc_sps_scaling_enabled, 0); ave_start_put8(cmd, l->hevc_sps_sao, 1); ave_start_put8(cmd, l->hevc_sps_tmvp, 0); + if (l->hevc_vui && p->vui_color) { + cmd[l->hevc_vui + AVE_HEVC_VUI_PRESENT] = 1; + cmd[l->hevc_vui + AVE_HEVC_VUI_VIDEO_SIGNAL_PRESENT] = 1; + ave_put32(cmd, l->hevc_vui + AVE_HEVC_VUI_VIDEO_FORMAT, + AVE_HEVC_VUI_VIDEO_FORMAT_UNSPECIFIED); + ave_put32(cmd, l->hevc_vui + AVE_HEVC_VUI_FULL_RANGE, + p->vui_full_range); + cmd[l->hevc_vui + AVE_HEVC_VUI_COLOUR_DESC_PRESENT] = 1; + ave_put32(cmd, l->hevc_vui + AVE_HEVC_VUI_COLOUR_PRIMARIES, + p->vui_primaries); + ave_put32(cmd, l->hevc_vui + AVE_HEVC_VUI_TRANSFER_CHAR, + p->vui_transfer); + ave_put32(cmd, l->hevc_vui + AVE_HEVC_VUI_MATRIX_COEFFS, + p->vui_matrix); + } ave_start_put8(cmd, l->hevc_sps_fw_header, 1); ave_start_put32(cmd, l->hevc_sps_header_len, 0); ave_start_put32(cmd, l->hevc_pps_extra_slice_bits, 0); diff --git a/drivers/media/platform/apple/ave/ave.h b/drivers/media/platform/apple/ave/ave.h index 932beb8356c09..57c4fc33f70a9 100644 --- a/drivers/media/platform/apple/ave/ave.h +++ b/drivers/media/platform/apple/ave/ave.h @@ -472,6 +472,11 @@ struct ave_params { u32 bit_depth; /* zero also means the existing 8-bit API */ u32 initial_qp; /* CQ only; bit-rate modes use 26 */ bool speed; /* prioritize encoding speed over quality */ + bool vui_color; /* emit HEVC SPS VUI signal-type and colour description */ + u8 vui_primaries; /* ITU-T H.273 colour_primaries */ + u8 vui_transfer; /* ITU-T H.273 transfer_characteristics */ + u8 vui_matrix; /* ITU-T H.273 matrix_coeffs */ + u8 vui_full_range; /* video_full_range_flag (0 limited, 1 full) */ }; struct ave_slot {