From 8a68d8fa2602815ea63d77768d610331f6b8826f Mon Sep 17 00:00:00 2001 From: Nick Date: Thu, 19 Nov 2026 10:50:00 +0700 Subject: [PATCH 1/3] media: apple: ave: flag B pictures on the coded buffers The firmware reorders the pictures of a B-frame session itself, so the driver does not know a coded picture's type when it hands the buffer out, and every picture that is not a key frame was flagged as a P frame. Read the slice type of the access unit's first slice instead: a few bytes of its slice header, with emulation-prevention bytes removed. HEVC needs the PPS's num_extra_slice_header_bits to reach slice_type, so the session remembers it from the last PPS it saw. Key frames are scanned too: they carry the parameter sets. Anything the reader does not expect leaves the picture flagged as a P frame, as before, and so does a buffer that comes back with an error: the flag is cleared before anything else is read. A KUnit case feeds a PPS with two extra bits, then a B slice that reads as I without them. The reader, built in user space, agrees with ffprobe's picture types on every access unit of x264 (two slices per picture) and x265 streams with 3 B-frames. On the MacBook Air 15" M5 (J815), with ffmpeg's v4l2m2m encoders and -bf 3, the buffers flagged B match the B pictures in the streams exactly: 27 and 30 of 60 (H.264), 43 and 44 of 60 (HEVC). Signed-off-by: Nick --- .../media/platform/apple/ave/ave-abi-test.c | 25 +++ drivers/media/platform/apple/ave/ave-sess.c | 151 ++++++++++++++++++ drivers/media/platform/apple/ave/ave-v4l2.c | 3 +- drivers/media/platform/apple/ave/ave.h | 3 + 4 files changed, 181 insertions(+), 1 deletion(-) diff --git a/drivers/media/platform/apple/ave/ave-abi-test.c b/drivers/media/platform/apple/ave/ave-abi-test.c index 72b7d969169c3..52ddde19da7c0 100644 --- a/drivers/media/platform/apple/ave/ave-abi-test.c +++ b/drivers/media/platform/apple/ave/ave-abi-test.c @@ -1561,6 +1561,30 @@ static void ave_t8112_26b123_build(struct kunit *test) &ave_layout_6070_11_1); } +/* + * HEVC B flag with a PPS whose num_extra_slice_header_bits is 2. The PPS + * comes with the IDR access unit; the B slice after it has two extra bits + * before slice_type, and reads as an I slice if they are not skipped. + */ +static void ave_hevc_bframe_extra_bits(struct kunit *test) +{ + static const u8 idr_au[] = { + 0, 0, 0, 1, 0x44, 0x01, 0xc5, 0x80, /* PPS: ids 0/0, 2 extra bits */ + 0, 0, 1, 0x26, 0x01, 0xa3, 0x80, /* IDR_W_RADL slice, I */ + }; + static const u8 b_au[] = { + 0, 0, 0, 1, 0x02, 0x01, 0xdc, 0x80, /* TRAIL_R slice, extra bits 01, B */ + }; + u8 bits = 0; + + KUNIT_EXPECT_FALSE(test, ave_au_bframe(idr_au, sizeof(idr_au), true, true, &bits)); + KUNIT_EXPECT_EQ(test, bits, 2); + KUNIT_EXPECT_TRUE(test, ave_au_bframe(b_au, sizeof(b_au), true, false, &bits)); + /* Without the PPS's bits the same slice reads as I. */ + bits = 0; + KUNIT_EXPECT_FALSE(test, ave_au_bframe(b_au, sizeof(b_au), true, false, &bits)); +} + static void ave_t6041_text_boot_word(struct kunit *test) { const u32 off = ave_t6041.text_boot_word, size = off + 0x100; @@ -1635,6 +1659,7 @@ static struct kunit_case ave_abi_cases[] = { KUNIT_CASE(ave_m3_probe_only), KUNIT_CASE(ave_26b123_layout), KUNIT_CASE(ave_t8112_26b123_build), + KUNIT_CASE(ave_hevc_bframe_extra_bits), {} }; diff --git a/drivers/media/platform/apple/ave/ave-sess.c b/drivers/media/platform/apple/ave/ave-sess.c index 595284b0e6c47..14242054fcffd 100644 --- a/drivers/media/platform/apple/ave/ave-sess.c +++ b/drivers/media/platform/apple/ave/ave-sess.c @@ -1105,6 +1105,155 @@ static bool ave_starts_annexb(const u8 *p, size_t len) (len >= 4 && !p[0] && !p[1] && !p[2] && p[3] == 1); } +/* + * Slice type of the first slice of an access unit, for the CAPTURE buffer's + * B/P flag: the firmware reorders B-frames itself, so the driver learns a + * picture's type from its bitstream. Only the first few bytes after the NAL + * header are read (emulation-prevention bytes removed); anything unexpected + * gives "not B". + */ +struct ave_rbsp { + u8 b[24]; + unsigned int n, pos; /* bytes in b; next bit */ +}; + +static void ave_rbsp_init(struct ave_rbsp *r, const u8 *p, size_t len) +{ + unsigned int zeros = 0; + + r->n = 0; + r->pos = 0; + for (size_t i = 0; i < len && r->n < sizeof(r->b); i++) { + if (zeros >= 2 && p[i] == 3) { + zeros = 0; + continue; + } + zeros = p[i] ? 0 : zeros + 1; + r->b[r->n++] = p[i]; + } +} + +static int ave_rbsp_bit(struct ave_rbsp *r) +{ + int bit; + + if (r->pos >= r->n * 8) + return -1; + bit = (r->b[r->pos / 8] >> (7 - r->pos % 8)) & 1; + r->pos++; + return bit; +} + +/* ue(v), or -1 past the bytes read or above 31 leading zeros. */ +static s64 ave_rbsp_ue(struct ave_rbsp *r) +{ + unsigned int zeros = 0; + u64 v = 0; + int bit; + + while ((bit = ave_rbsp_bit(r)) == 0) + if (++zeros > 31) + return -1; + if (bit < 0) + return -1; + for (unsigned int i = 0; i < zeros; i++) { + bit = ave_rbsp_bit(r); + if (bit < 0) + return -1; + v = v << 1 | bit; + } + return (s64)((1ULL << zeros) - 1 + v); +} + +/* HEVC PPS: num_extra_slice_header_bits, needed to reach slice_type. */ +static int ave_hevc_pps_extra_bits(const u8 *nal, size_t len) +{ + struct ave_rbsp r; + int v = 0; + + ave_rbsp_init(&r, nal + 2, len - 2); + if (ave_rbsp_ue(&r) < 0 || ave_rbsp_ue(&r) < 0) /* pps id, sps id */ + return -1; + if (ave_rbsp_bit(&r) < 0 || ave_rbsp_bit(&r) < 0) /* dependent slices, output flag */ + return -1; + for (unsigned int i = 0; i < 3; i++) { + int bit = ave_rbsp_bit(&r); + + if (bit < 0) + return -1; + v = v << 1 | bit; + } + return v; +} + +static bool ave_slice_is_b(const u8 *nal, size_t len, bool hevc, u8 extra_bits) +{ + u32 nal_type = (nal[0] >> 1) & 0x3f; + struct ave_rbsp r; + s64 type; + + if (!hevc) { + ave_rbsp_init(&r, nal + 1, len - 1); + if (ave_rbsp_ue(&r) < 0) /* first_mb_in_slice */ + return false; + type = ave_rbsp_ue(&r); + return type == 1 || type == 6; /* B, B (all slices) */ + } + + ave_rbsp_init(&r, nal + 2, len - 2); + if (ave_rbsp_bit(&r) != 1) /* first_slice_segment_in_pic_flag */ + return false; + if (nal_type >= 16 && nal_type <= 23 && ave_rbsp_bit(&r) < 0) + return false; + if (ave_rbsp_ue(&r) < 0) /* slice_pic_parameter_set_id */ + return false; + for (unsigned int i = 0; i < extra_bits; i++) + if (ave_rbsp_bit(&r) < 0) + return false; + type = ave_rbsp_ue(&r); + return type == 0; /* HEVC: 0 = B, 1 = P, 2 = I */ +} + +/* Whether the access unit's first slice is a B slice; learns the PPS bits. */ +static bool ave_au_is_b(const u8 *p, size_t len, bool hevc, u8 *extra_bits) +{ + for (size_t i = 0; i + 3 < len; i++) { + const u8 *nal; + size_t room; + u32 type; + + if (p[i] || p[i + 1] || p[i + 2] != 1) + continue; + nal = p + i + 3; + room = len - i - 3; + if (room < (hevc ? 3 : 2)) + break; + type = hevc ? (nal[0] >> 1) & 0x3f : nal[0] & 0x1f; + if (hevc && type == 34) { + int bits = ave_hevc_pps_extra_bits(nal, room); + + if (bits >= 0) + *extra_bits = bits; + } else if (hevc ? type <= 21 : type == 1 || type == 5) { + return ave_slice_is_b(nal, room, hevc, *extra_bits); + } + i += 2; + } + return false; +} + +/* + * Whether a coded access unit is a B picture. Key frames are scanned too: + * they carry the parameter sets, and the PPS's extra slice header bits are + * needed for every later B/P decision. + */ +bool ave_au_bframe(const u8 *p, size_t len, bool hevc, bool key, u8 *extra_bits) +{ + bool b = ave_au_is_b(p, len, hevc, extra_bits); + + return !key && b; +} + /* NAL unit types in an Annex B buffer: key frame, parameter sets, filler? */ static void ave_scan_nals(const u8 *p, size_t len, bool hevc, bool *key, bool *params, bool *filler) @@ -1312,6 +1461,7 @@ int ave_sess_output(struct ave_ctx *ctx, unsigned int slot, struct vb2_v4l2_buff int at, ret; *keyframe = false; + s->bframe = false; if (!out) return -EFAULT; if (s->status) { @@ -1378,6 +1528,7 @@ int ave_sess_output(struct ave_ctx *ctx, unsigned int slot, struct vb2_v4l2_buff if (!ave_starts_annexb(out, len)) ave_sess_dump(ctx, s, "not Annex B"); ave_scan_nals(out, len, hevc, keyframe, ¶ms, &filler); + 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); diff --git a/drivers/media/platform/apple/ave/ave-v4l2.c b/drivers/media/platform/apple/ave/ave-v4l2.c index 1f2e933ed893a..a5ac2073a2a26 100644 --- a/drivers/media/platform/apple/ave/ave-v4l2.c +++ b/drivers/media/platform/apple/ave/ave-v4l2.c @@ -1152,7 +1152,8 @@ static bool ave_deliver(struct ave_ctx *ctx) v4l2_m2m_buf_copy_metadata(s->src, dst); dst->flags &= ~(V4L2_BUF_FLAG_KEYFRAME | V4L2_BUF_FLAG_PFRAME | V4L2_BUF_FLAG_BFRAME | V4L2_BUF_FLAG_LAST); - dst->flags |= key ? V4L2_BUF_FLAG_KEYFRAME : V4L2_BUF_FLAG_PFRAME; + dst->flags |= key ? V4L2_BUF_FLAG_KEYFRAME : + s->bframe ? V4L2_BUF_FLAG_BFRAME : V4L2_BUF_FLAG_PFRAME; dst->sequence = ctx->dst_sequence++; sess->done_head = (sess->done_head + 1) % AVE_MAX_SLOTS; diff --git a/drivers/media/platform/apple/ave/ave.h b/drivers/media/platform/apple/ave/ave.h index a0bda7749166a..e58f5d44a8df8 100644 --- a/drivers/media/platform/apple/ave/ave.h +++ b/drivers/media/platform/apple/ave/ave.h @@ -441,6 +441,7 @@ struct ave_slot { u32 status; 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) */ }; struct ave_frame_result { @@ -468,6 +469,7 @@ struct ave_frame_result { struct ave_session { struct ave_session *failed_next; struct ave_params p; + u8 hevc_extra_bits; /* PPS num_extra_slice_header_bits, for the B flag */ struct ave_buf hdr; struct ave_buf client_mem; /* 6070: per-session firmware-generated VPS/SPS/PPS output. */ @@ -643,6 +645,7 @@ int ave_build_process(struct ave_ctx *ctx, u8 *cmd, size_t len, unsigned int slot, dma_addr_t input, u32 bitrate, bool force_key, bool last); int ave_init_codec_state(struct ave_session *sess); +bool ave_au_bframe(const u8 *p, size_t len, bool hevc, bool key, u8 *extra_bits); /* ave-cmd.c */ int ave_cmd_config(struct ave_dev *ave); From ff4daf11b165924c37c860e8ec5bdacadc5b85d9 Mon Sep 17 00:00:00 2001 From: Nick Date: Thu, 19 Nov 2026 10:50:00 +0700 Subject: [PATCH 2/3] media: apple: ave: 26A428: the Start's second frame rate and the HEVC low-delay word macOS 27.2.1 on the M5 (T8142) writes the integer frame rate a second time at Start +0x320c (60 at 60 fps), and for an HEVC session without B-frames 3 at Process +0xce4, the same low-delay word the 9003 layout (ave_layout_9003_78_0) has at +0xcf8. Write both on the 26A428 layout. The 26B123 layout shares 26A428's fields through one macro, which now takes the two offsets. 26B123 passes 0 for both, so it writes neither: its 60 fps and no-B sessions were run without them, and nothing has traced its commands at those settings. At 30 fps the second rate is the 0x1e the 26A428 Start ledger already holds, and the low-delay word only applies to HEVC without B-frames, which this layout does not run yet, so qualified sessions on every chip send the same commands as before. The 26B123 KUnit case still checks that the two layouts agree on every other field. Signed-off-by: Nick --- .../media/platform/apple/ave/ave-abi-test.c | 17 +++++++++++++++-- drivers/media/platform/apple/ave/ave-layout.c | 18 ++++++++++++++---- drivers/media/platform/apple/ave/ave-layout.h | 2 ++ drivers/media/platform/apple/ave/ave-writer.c | 2 ++ 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/drivers/media/platform/apple/ave/ave-abi-test.c b/drivers/media/platform/apple/ave/ave-abi-test.c index 52ddde19da7c0..bdcb4a49f6bf3 100644 --- a/drivers/media/platform/apple/ave/ave-abi-test.c +++ b/drivers/media/platform/apple/ave/ave-abi-test.c @@ -1472,6 +1472,8 @@ static void ave_26b123_layout(struct kunit *test) { const struct ave_fw_layout *a = &ave_layout_26a428; const struct ave_fw_layout *b = ave_layout_for_crc(0x99fc929a); + struct ave_start_layout *start = kunit_kzalloc(test, sizeof(a->start), GFP_KERNEL); + struct ave_proc_layout *process = kunit_kzalloc(test, sizeof(a->process), GFP_KERNEL); KUNIT_ASSERT_PTR_EQ(test, b, &ave_layout_26b123); KUNIT_EXPECT_TRUE(test, b->encode_ready); @@ -1485,8 +1487,19 @@ 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); - KUNIT_EXPECT_EQ(test, memcmp(b->start, a->start, sizeof(a->start)), 0); - KUNIT_EXPECT_EQ(test, memcmp(b->process, a->process, sizeof(a->process)), 0); + /* Apart from the two words only the M5 26A428 sessions were run with. */ + KUNIT_ASSERT_NOT_NULL(test, start); + KUNIT_ASSERT_NOT_NULL(test, process); + memcpy(start, a->start, sizeof(a->start)); + memcpy(process, a->process, sizeof(a->process)); + 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[1].low_delay, 0U); + process[1].low_delay = 0; + KUNIT_EXPECT_EQ(test, memcmp(b->start, start, sizeof(a->start)), 0); + KUNIT_EXPECT_EQ(test, memcmp(b->process, process, sizeof(a->process)), 0); KUNIT_EXPECT_EQ(test, memcmp(b->result, a->result, sizeof(a->result)), 0); KUNIT_EXPECT_EQ(test, b->hdr_bytes, a->hdr_bytes); KUNIT_EXPECT_EQ(test, b->max_slots, a->max_slots); diff --git a/drivers/media/platform/apple/ave/ave-layout.c b/drivers/media/platform/apple/ave/ave-layout.c index 377406027f9ab..bce97316b0af9 100644 --- a/drivers/media/platform/apple/ave/ave-layout.c +++ b/drivers/media/platform/apple/ave/ave-layout.c @@ -281,9 +281,11 @@ 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). + * (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. */ -#define AVE_LAYOUT_26A428_FIELDS \ +#define AVE_LAYOUT_26A428_FIELDS(rate_copy, hevc_low_delay) \ .abi = &ave_abi_9003, \ .kind = AVE_LAYOUT_26A428, \ .encode_ready = true, \ @@ -335,6 +337,7 @@ const struct ave_fw_layout ave_layout_9003_78_0 = { .const_214 = 0x35f4, \ .const_218 = 0x35f8, \ .fps = { .numerator = 0x3600 }, \ + .fps_int_copy = rate_copy, \ .rc_mode = 0x3614, \ .bitrate = 0x3618, \ .init_qp = 0x3620, \ @@ -423,6 +426,7 @@ const struct ave_fw_layout ave_layout_9003_78_0 = { .const_214 = 0x35f4, \ .const_218 = 0x35f8, \ .fps = { .numerator = 0x3600 }, \ + .fps_int_copy = rate_copy, \ .rc_mode = 0x3614, \ .bitrate = 0x3618, \ .init_qp = 0x3620, \ @@ -524,6 +528,7 @@ const struct ave_fw_layout ave_layout_9003_78_0 = { .meta_size = 0x6060, \ .extra = 0x6068, \ .crop = 0x3190, \ + .low_delay = hevc_low_delay, \ }, \ }, \ .result = { \ @@ -541,7 +546,12 @@ const struct ave_fw_layout ave_layout_9003_78_0 = { const struct ave_fw_layout ave_layout_26a428 = { .name = "26A428", .bytes = { { 0x3b40, 0x6df8 }, { 0x13ee0, 0x78b8 } }, - AVE_LAYOUT_26A428_FIELDS + /* + * macOS 27.2.1 on the M5 writes the rate a second time at Start + * +0x320c (60 at 60 fps), and 3 at Process +0xce4 for HEVC without + * B-frames. + */ + AVE_LAYOUT_26A428_FIELDS(0x320c, 0xce4) }; /* @@ -553,7 +563,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 + AVE_LAYOUT_26A428_FIELDS(0, 0) .any_gop = true, }; diff --git a/drivers/media/platform/apple/ave/ave-layout.h b/drivers/media/platform/apple/ave/ave-layout.h index e5cb20074d276..d499102fb2adb 100644 --- a/drivers/media/platform/apple/ave/ave-layout.h +++ b/drivers/media/platform/apple/ave/ave-layout.h @@ -135,6 +135,8 @@ struct ave_start_layout { u32 const_214; u32 const_218; struct ave_rational_layout fps; + /* A second copy of the integer frame rate (26A428 Start). */ + u32 fps_int_copy; u32 rc_mode; /* Optional ABI-specific wire value for the driver's fixed-QP mode. */ u32 rc_mode_fixed_qp; diff --git a/drivers/media/platform/apple/ave/ave-writer.c b/drivers/media/platform/apple/ave/ave-writer.c index 8dbc0192a778a..fe621ab833e00 100644 --- a/drivers/media/platform/apple/ave/ave-writer.c +++ b/drivers/media/platform/apple/ave/ave-writer.c @@ -783,6 +783,8 @@ static void ave_start_config(const struct ave_params *p, u8 *cmd, } else { ave_start_put32(cmd, l->fps.numerator, p->fps_int); } + /* A second copy of the integer rate: macOS 27.2.1 writes 60 at 60 fps (M5). */ + ave_start_put32(cmd, l->fps_int_copy, p->fps_int); /* Rate control */ /* From b3e6c51a4b5fc1c394b2589fc7a63cf78b61ca0e Mon Sep 17 00:00:00 2001 From: Nick Date: Sat, 21 Nov 2026 19:11:51 +0700 Subject: [PATCH 3/3] media: apple: ave: 26A428: other frame rates and sessions without B-frames The 26A428 layout runs only 30 fps sessions with B-frames (or H.264 Baseline and intra-only ones): those are the ones its command ledger was qualified with. We traced the Start and Process commands macOS 27.2.1 sends on the M5 (T8142) for 60 fps sessions and sessions without B-frames, and compared them with what the driver sends for the same settings. With the two words the previous commit adds, they match, apart from words that also differ on a qualified 30 fps session with B-frames. Set any_gop on the layout, as the 26B123 layout has it, so the refusal no longer applies to it. The flag is on the layout, so the M5 Pro and Max (T6050) firmware on the same layout gets it too. Only the M5 has encoded with it so far. Tested on the MacBook Air 15" M5 (J815), firmware 26B123 (the macOS 27.2 stub's, on the 26A428 layout), on two boots: 30 encode cases (H.264 Baseline/Main/High and HEVC Main; 30 and 60 fps; with and without B-frames; VBR and CBR; 640x360 to 2560x1440), every stream decoding to the expected frame count, PSNR 27-42 dB against the source. Signed-off-by: Nick --- drivers/media/platform/apple/ave/ave-abi-test.c | 4 ++-- drivers/media/platform/apple/ave/ave-layout.c | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/drivers/media/platform/apple/ave/ave-abi-test.c b/drivers/media/platform/apple/ave/ave-abi-test.c index bdcb4a49f6bf3..001f5f4b62384 100644 --- a/drivers/media/platform/apple/ave/ave-abi-test.c +++ b/drivers/media/platform/apple/ave/ave-abi-test.c @@ -1504,9 +1504,9 @@ static void ave_26b123_layout(struct kunit *test) KUNIT_EXPECT_EQ(test, b->hdr_bytes, a->hdr_bytes); KUNIT_EXPECT_EQ(test, b->max_slots, a->max_slots); KUNIT_EXPECT_EQ(test, b->process_subtype, a->process_subtype); - /* Both counted; only the 27.2 build runs sessions without B-frames and at any rate. */ + /* Both counted, and both run sessions without B-frames and at any rate. */ KUNIT_EXPECT_TRUE(test, a->counted && b->counted); - KUNIT_EXPECT_FALSE(test, a->any_gop); + KUNIT_EXPECT_TRUE(test, a->any_gop); KUNIT_EXPECT_TRUE(test, b->any_gop); KUNIT_EXPECT_PTR_EQ(test, ave_layout_for_crc(AVE_FW_TEXT_CRC_T8122_26B123), b); } diff --git a/drivers/media/platform/apple/ave/ave-layout.c b/drivers/media/platform/apple/ave/ave-layout.c index bce97316b0af9..b05103511532f 100644 --- a/drivers/media/platform/apple/ave/ave-layout.c +++ b/drivers/media/platform/apple/ave/ave-layout.c @@ -552,6 +552,8 @@ const struct ave_fw_layout ave_layout_26a428 = { * B-frames. */ AVE_LAYOUT_26A428_FIELDS(0x320c, 0xce4) + /* 60 fps and sessions without B-frames encode on the M5 (J815). */ + .any_gop = true, }; /*