diff --git a/drivers/media/platform/apple/ave/ave-abi-test.c b/drivers/media/platform/apple/ave/ave-abi-test.c index 89bc114ad8f01..a818eb9a2d546 100644 --- a/drivers/media/platform/apple/ave/ave-abi-test.c +++ b/drivers/media/platform/apple/ave/ave-abi-test.c @@ -1545,7 +1545,7 @@ static void ave_26b123_layout(struct kunit *test) } KUNIT_EXPECT_EQ(test, b->bytes[0][AVE_IMAGE_PROCESS], 0x6e08); KUNIT_EXPECT_EQ(test, b->bytes[1][AVE_IMAGE_PROCESS], 0x78c8); - /* Apart from the two words only the M5 26A428 sessions were run with. */ + /* Apart from the words only one of the two layouts has qualified. */ KUNIT_ASSERT_NOT_NULL(test, start); KUNIT_ASSERT_NOT_NULL(test, process); memcpy(start, a->start, sizeof(a->start)); @@ -1553,6 +1553,24 @@ static void ave_26b123_layout(struct kunit *test) for (unsigned int c = 0; c < 2; c++) { KUNIT_EXPECT_EQ(test, b->start[c].fps_int_copy, 0U); start[c].fps_int_copy = 0; + KUNIT_EXPECT_EQ(test, b->process[c].bitrate, + b->process[c].fps_int + 0x10U); + KUNIT_EXPECT_EQ(test, b->process[c].limits, + b->process[c].fps_int + 0x18U); + KUNIT_EXPECT_EQ(test, b->process[c].limit_bytes, + b->process[c].fps_int + 0x20U); + KUNIT_EXPECT_EQ(test, b->process[c].limit_secs, + b->process[c].fps_int + 0x28U); + KUNIT_EXPECT_EQ(test, b->process[c].force_key, + b->process[c].fps_int + 0x48U); + KUNIT_EXPECT_EQ(test, b->process[c].force_type, + c == 0 ? 0x29d8U : 0x3180U); + process[c].bitrate = b->process[c].bitrate; + process[c].limits = b->process[c].limits; + process[c].limit_bytes = b->process[c].limit_bytes; + process[c].limit_secs = b->process[c].limit_secs; + process[c].force_key = b->process[c].force_key; + process[c].force_type = b->process[c].force_type; } KUNIT_EXPECT_EQ(test, b->process[1].low_delay, 0U); process[1].low_delay = 0; @@ -1569,6 +1587,73 @@ static void ave_26b123_layout(struct kunit *test) KUNIT_EXPECT_PTR_EQ(test, ave_layout_for_crc(AVE_FW_TEXT_CRC_T8122_26B123), b); } +/* + * 26B123 mid-stream forced keyframes and bit-rate updates: verify both codecs + * write the per-frame rate-control fields (with VBR limits scaled to bits/s for + * counted layouts and rescaled when the target bit rate changes) and the + * forced-keyframe pair without disturbing the 0xffffffff sentinel right after + * force_type. + */ +static void ave_26b123_process_dyn_rc(struct kunit *test) +{ + const enum ave_codec codecs[] = { AVE_CODEC_H264, AVE_CODEC_HEVC }; + + for (unsigned int i = 0; i < ARRAY_SIZE(codecs); i++) { + enum ave_codec codec = codecs[i]; + const struct ave_fw_layout *fw = &ave_layout_26b123; + const struct ave_proc_layout *pl = &fw->process[codec - 1]; + 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[codec - 1][AVE_IMAGE_PROCESS]; + 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); + + ave->layout = fw; + ctx->ave = ave; + ctx->sess = sess; + ave_test_session(sess, codec, 1280, 720, false); + sess->frames = 5; + + /* Unchanged bit rate, forced keyframe. */ + KUNIT_ASSERT_EQ(test, + ave_build_process(ctx, cmd, len, 0, 0x8f0000000ULL, + sess->p.bitrate, true, false), + 0); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(cmd + pl->bitrate), 8000000U); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(cmd + pl->limits), 1U); + /* 2,000,000 bytes/s * 8 = 16,000,000 bits/s -> IEEE-754 0x416e848000000000. */ + KUNIT_EXPECT_EQ(test, get_unaligned_le64(cmd + pl->limit_bytes), + 0x416e848000000000ULL); + KUNIT_EXPECT_EQ(test, get_unaligned_le64(cmd + pl->limit_secs), + 0x3ff0000000000000ULL); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(cmd + pl->force_key), 1U); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(cmd + pl->force_type), 2U); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(cmd + pl->force_type + 4), + 0xffffffffU); + + /* Mid-stream bit-rate drop to 2 Mbit/s: VBR peak rescales to 4 Mbit/s. */ + memset(cmd, 0, len); + KUNIT_ASSERT_EQ(test, + ave_build_process(ctx, cmd, len, 0, 0x8f0000000ULL, + 2000000U, false, false), + 0); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(cmd + pl->bitrate), 2000000U); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(cmd + pl->limits), 1U); + /* 4,000,000 bits/s -> IEEE-754 0x414e848000000000. */ + KUNIT_EXPECT_EQ(test, get_unaligned_le64(cmd + pl->limit_bytes), + 0x414e848000000000ULL); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(cmd + pl->force_key), 0U); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(cmd + pl->force_type), 0U); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(cmd + pl->force_type + 4), + 0xffffffffU); + } +} + /* * The M2 with the 27.2 stub: the reserved segment sizes select the 27.2 * build's own start (device 12, handshake type 12) and the 26B123 layout; @@ -2110,6 +2195,7 @@ static struct kunit_case ave_abi_cases[] = { KUNIT_CASE(ave_6070_smalloc), KUNIT_CASE(ave_m3_probe_only), KUNIT_CASE(ave_26b123_layout), + KUNIT_CASE(ave_26b123_process_dyn_rc), KUNIT_CASE(ave_t8112_26b123_build), KUNIT_CASE(ave_t8132_25g83_build), KUNIT_CASE(ave_t6041_26b123_build), diff --git a/drivers/media/platform/apple/ave/ave-cmd.c b/drivers/media/platform/apple/ave/ave-cmd.c index d301e36063220..3e5bea516953e 100644 --- a/drivers/media/platform/apple/ave/ave-cmd.c +++ b/drivers/media/platform/apple/ave/ave-cmd.c @@ -214,6 +214,7 @@ int ave_cmd_process(struct ave_ctx *ctx, unsigned int slot, if (ret) return ret; s->src = src; + s->bitrate = bitrate; s->busy = true; s->status = 0; sess->progress = jiffies; diff --git a/drivers/media/platform/apple/ave/ave-layout.c b/drivers/media/platform/apple/ave/ave-layout.c index 289f63c4af129..4cf58df303c8c 100644 --- a/drivers/media/platform/apple/ave/ave-layout.c +++ b/drivers/media/platform/apple/ave/ave-layout.c @@ -281,11 +281,12 @@ const struct ave_fw_layout ave_layout_9003_78_0 = { /* * The macOS 27.0 (26A428) command layout. The macOS 27.2 (26B123) build * keeps every field where 26A428 has it; only the Process commands grow - * (ave_layout_26b123). rate_copy is the Start's second integer frame rate - * and hevc_low_delay the HEVC Process low-delay word, 0 where a build's - * sessions have not been run with them. + * (ave_layout_26b123). rate_copy is the Start's second integer frame rate, + * hevc_low_delay the HEVC Process low-delay word, and dyn_rc enables the + * per-frame rate-control and forced-keyframe Process offsets; each is 0 + * where a build's sessions have not been run with it. */ -#define AVE_LAYOUT_26A428_FIELDS(rate_copy, hevc_low_delay) \ +#define AVE_LAYOUT_26A428_FIELDS(rate_copy, hevc_low_delay, dyn_rc) \ .abi = &ave_abi_9003, \ .kind = AVE_LAYOUT_26A428, \ .encode_ready = true, \ @@ -491,6 +492,12 @@ const struct ave_fw_layout ave_layout_9003_78_0 = { [0] = { \ .first_frame = 0xdc0, \ .fps_int = 0x1328, \ + .bitrate = (dyn_rc) ? 0x1338 : 0, \ + .limits = (dyn_rc) ? 0x1340 : 0, \ + .limit_bytes = (dyn_rc) ? 0x1348 : 0, \ + .limit_secs = (dyn_rc) ? 0x1350 : 0, \ + .force_key = (dyn_rc) ? 0x1370 : 0, \ + .force_type = (dyn_rc) ? 0x29d8 : 0, \ .in_w = 0x5648, \ .in_h = 0x564c, \ .ordinal = 0x5650, \ @@ -512,6 +519,12 @@ const struct ave_fw_layout ave_layout_9003_78_0 = { [1] = { \ .first_frame = 0x1568, \ .fps_int = 0x1ad0, \ + .bitrate = (dyn_rc) ? 0x1ae0 : 0, \ + .limits = (dyn_rc) ? 0x1ae8 : 0, \ + .limit_bytes = (dyn_rc) ? 0x1af0 : 0, \ + .limit_secs = (dyn_rc) ? 0x1af8 : 0, \ + .force_key = (dyn_rc) ? 0x1b18 : 0, \ + .force_type = (dyn_rc) ? 0x3180 : 0, \ .in_w = 0x5df0, \ .in_h = 0x5df4, \ .ordinal = 0x5df8, \ @@ -551,7 +564,7 @@ const struct ave_fw_layout ave_layout_26a428 = { * +0x320c (60 at 60 fps), and 3 at Process +0xce4 for HEVC without * B-frames. */ - AVE_LAYOUT_26A428_FIELDS(0x320c, 0xce4) + AVE_LAYOUT_26A428_FIELDS(0x320c, 0xce4, 0) /* 60 fps and sessions without B-frames encode on the M5 (J815). */ .any_gop = true, }; @@ -565,7 +578,7 @@ const struct ave_fw_layout ave_layout_26a428 = { const struct ave_fw_layout ave_layout_26b123 = { .name = "26B123", .bytes = { { 0x3b40, 0x6e08 }, { 0x13ee0, 0x78c8 } }, - AVE_LAYOUT_26A428_FIELDS(0, 0) + AVE_LAYOUT_26A428_FIELDS(0, 0, 1) .any_gop = true, }; diff --git a/drivers/media/platform/apple/ave/ave-sess.c b/drivers/media/platform/apple/ave/ave-sess.c index 0a03fc917447b..76cd9dfdbf626 100644 --- a/drivers/media/platform/apple/ave/ave-sess.c +++ b/drivers/media/platform/apple/ave/ave-sess.c @@ -240,6 +240,7 @@ static int ave_sess_params(struct ave_ctx *ctx, struct ave_params *p) * control on hardware. A peak of 0 means that default. */ p->rc_mode = AVE_RC_VBR; + p->bitrate_peak = ctx->bitrate_peak; p->limit_bytes = (ctx->bitrate_peak ? max(ctx->bitrate_peak, ctx->bitrate) : 2ULL * ctx->bitrate) / 8; } @@ -1338,14 +1339,15 @@ static void ave_sess_dump(struct ave_ctx *ctx, struct ave_slot *s, const char *w */ #define AVE_FILLER_OVERHEAD 7 /* start code, NAL header, trailing bits */ -static size_t ave_sess_cbr_fill(struct ave_ctx *ctx, u8 *out, size_t len, size_t room, - bool fw_filler) +static size_t ave_sess_cbr_fill(struct ave_ctx *ctx, u32 bitrate, u8 *out, + size_t len, size_t room, bool fw_filler) { struct ave_session *sess = ctx->sess; const struct ave_params *p = &sess->p; bool hevc = p->codec == AVE_CODEC_HEVC; - s64 budget = div_u64((u64)p->bitrate * p->fps_den, 8ULL * p->fps_num); - s64 vbv = p->bitrate / 16; /* 0.5 s of bit rate, in bytes */ + u32 rate = bitrate ?: p->bitrate; + s64 budget = div_u64((u64)rate * p->fps_den, 8ULL * p->fps_num); + s64 vbv = rate / 16; /* 0.5 s of bit rate, in bytes */ s64 excess, pad; u8 *f; @@ -1531,7 +1533,7 @@ int ave_sess_output(struct ave_ctx *ctx, unsigned int slot, struct vb2_v4l2_buff s->bframe = ave_au_bframe(out, len, hevc, *keyframe, &sess->hevc_extra_bits); if (!ctx->dst_sequence && !params) ave_sess_dump(ctx, s, "first frame has no parameter sets"); - len = ave_sess_cbr_fill(ctx, out, len, room, filler); + len = ave_sess_cbr_fill(ctx, s->bitrate, out, len, room, filler); vb2_set_plane_payload(&dst->vb2_buf, 0, len); return 0; } diff --git a/drivers/media/platform/apple/ave/ave-writer.c b/drivers/media/platform/apple/ave/ave-writer.c index fe621ab833e00..6405d4479b95b 100644 --- a/drivers/media/platform/apple/ave/ave-writer.c +++ b/drivers/media/platform/apple/ave/ave-writer.c @@ -1250,11 +1250,22 @@ int ave_build_process(struct ave_ctx *ctx, u8 *cmd, size_t len, if (l->bitrate) ave_put32(cmd, l->bitrate, p->rc_mode == 3 && fw->kind == AVE_LAYOUT_6070 ? 0 : bitrate); - if (l->limits) - ave_put32(cmd, l->limits, p->limit_bytes ? 1 : 0); - if (l->limit_bytes && p->limit_bytes) { - ave_put64(cmd, l->limit_bytes, ave_f64(p->limit_bytes, 1)); - ave_put64(cmd, l->limit_secs, ave_f64(1, 1)); + { + u32 limit_bytes = p->limit_bytes; + + if (fw->counted && p->rc_mode == AVE_RC_VBR && limit_bytes && + bitrate != p->bitrate) + limit_bytes = (p->bitrate_peak ? + max(p->bitrate_peak, bitrate) : + 2ULL * bitrate) / 8; + if (l->limits) + ave_put32(cmd, l->limits, limit_bytes ? 1 : 0); + if (l->limit_bytes && limit_bytes) { + ave_put64(cmd, l->limit_bytes, + ave_f64((u64)limit_bytes * + (fw->counted ? 8 : 1), 1)); + ave_put64(cmd, l->limit_secs, ave_f64(1, 1)); + } } if (l->frame_type) ave_put32(cmd, l->frame_type, pic.type); diff --git a/drivers/media/platform/apple/ave/ave.h b/drivers/media/platform/apple/ave/ave.h index ceac142fe6ac0..248bab6ff11e8 100644 --- a/drivers/media/platform/apple/ave/ave.h +++ b/drivers/media/platform/apple/ave/ave.h @@ -461,6 +461,7 @@ struct ave_params { u32 gop; u32 rc_mode; /* AVE_RC_* */ u32 bitrate; + u32 bitrate_peak; /* 0: default twice the average bit rate */ u32 limit_bytes; /* 0: no data-rate limit */ s32 min_qp; u32 max_qp; @@ -486,6 +487,7 @@ struct ave_slot { u8 mctf_input; /* MCTF: the input surface this frame used */ bool mctf_direct; /* MCTF: the engine wrote the CAPTURE buffer itself */ u32 status; + u32 bitrate; /* bit rate requested for this picture */ bool busy; /* Process sent, reply pending */ bool done; /* reply in, output not delivered yet */ bool bframe; /* the delivered picture is a B picture (its slice type) */ diff --git a/drivers/media/platform/apple/ave/tests/test.c b/drivers/media/platform/apple/ave/tests/test.c index 895a38c38f3b3..feede63d97ffc 100644 --- a/drivers/media/platform/apple/ave/tests/test.c +++ b/drivers/media/platform/apple/ave/tests/test.c @@ -160,6 +160,40 @@ static void facts(void) ave_sess_free_bufs(&dev, &session); memset(&session, 0, sizeof(session)); } + dev.layout = &ave_layout_26b123; + for (unsigned int codec = 1; codec <= 2; codec++) { + u32 d = codec == 1 ? 0 : 0x7a8; + + params(&session.p, codec, 1280, 720); + session.p.bframes = false; + session.p.reorder_off = true; + session.p.refs = 2; + session.p.slots = 3; + session.p.bitrate = 8000000; + session.p.limit_bytes = 2000000; + start(&ctx); + frame(&ctx, 0, true); + assert(wire_len == (codec == 1 ? 0x6e08U : 0x78c8U)); + assert(get_unaligned_le32(wire + 0x1338 + d) == 8000000U); + assert(get_unaligned_le32(wire + 0x1340 + d) == 1U); + assert(get_unaligned_le64(wire + 0x1348 + d) == 0x416e848000000000ULL); + assert(get_unaligned_le64(wire + 0x1350 + d) == 0x3ff0000000000000ULL); + assert(get_unaligned_le32(wire + 0x1370 + d) == 1U); + assert(get_unaligned_le32(wire + 0x29d8 + d) == 2U); + assert(get_unaligned_le32(wire + 0x29dc + d) == 0xffffffffU); + assert(ave_cmd_process(&ctx, 1, NULL, 0x8f0000000ULL, + 2000000U, false, false) == 0); + assert(session.slot[1].bitrate == 2000000U); + assert(get_unaligned_le32(wire + 0x1338 + d) == 2000000U); + assert(get_unaligned_le32(wire + 0x1340 + d) == 1U); + assert(get_unaligned_le64(wire + 0x1348 + d) == 0x414e848000000000ULL); + assert(get_unaligned_le32(wire + 0x1370 + d) == 0U); + assert(get_unaligned_le32(wire + 0x29d8 + d) == 0U); + assert(get_unaligned_le32(wire + 0x29dc + d) == 0xffffffffU); + ave_sess_free_bufs(&dev, &session); + memset(&session, 0, sizeof(session)); + } + dev.layout = &ave_layout_26a428; for (unsigned int codec = 1; codec <= 2; codec++) { params(&session.p, codec, 1920, 1080); session.p.rc_mode = 3;