Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions drivers/media/platform/apple/ave/ave-abi-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -1485,15 +1487,26 @@ 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);
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);
}
Expand Down Expand Up @@ -1561,6 +1574,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;
Expand Down Expand Up @@ -1635,6 +1672,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),
{}
};

Expand Down
20 changes: 16 additions & 4 deletions drivers/media/platform/apple/ave/ave-layout.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand Down Expand Up @@ -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, \
Expand Down Expand Up @@ -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, \
Expand Down Expand Up @@ -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 = { \
Expand All @@ -541,7 +546,14 @@ 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)
/* 60 fps and sessions without B-frames encode on the M5 (J815). */
.any_gop = true,
};

/*
Expand All @@ -553,7 +565,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,
};

Expand Down
2 changes: 2 additions & 0 deletions drivers/media/platform/apple/ave/ave-layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
151 changes: 151 additions & 0 deletions drivers/media/platform/apple/ave/ave-sess.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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, &params, &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);
Expand Down
3 changes: 2 additions & 1 deletion drivers/media/platform/apple/ave/ave-v4l2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions drivers/media/platform/apple/ave/ave-writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
/*
Expand Down
3 changes: 3 additions & 0 deletions drivers/media/platform/apple/ave/ave.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -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);
Expand Down
Loading