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
104 changes: 94 additions & 10 deletions drivers/media/platform/apple/prores/apple-prores-desc.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,9 @@ EXPORT_SYMBOL_IF_KUNIT(apr_dc_scale_reg);
#define D_FH_ALPHA 0x08c
#define D_FH_FLAGS 0x08f
#define D_FH_BYTES 0x94
/* Coded window and rate control */
#define D_MB_W 0x110
#define D_MB_H 0x112
/* Rate control: coefficient budgets in bytes per macroblock, Q3 */
#define D_RC_TARGET 0x110
#define D_RC_MAX 0x112
#define D_RC_THRESHOLDS 0x114 /* six 7-bit macroblock class thresholds */
#define D_RC_ALPHA 0x11c
#define D_RC_ALPHA0 0x11d
Expand All @@ -218,11 +218,29 @@ EXPORT_SYMBOL_IF_KUNIT(apr_dc_scale_reg);

/* Slices of eight macroblocks: log2 width 3 in the high nibble. */
#define APR_SLICE_LOG2 3
#define APR_QUANT_INDEX 2

/*
* Rate-control settings the engine is known to work with (m1n1). Their
* per-profile tuning is not known yet.
* Per-profile policy, as macOS uses it: the size multiplier relative to
* 422 HQ (a fraction), and the starting quantiser. The engine refines the
* slice quantisers around the starting one to meet the byte budget.
*/
static const struct {
u8 num, den;
u8 quant_index;
} apr_profile_policy[APR_PROFILES] = {
[APR_PROFILE_PROXY] = { 13, 63, 8 },
[APR_PROFILE_LT] = { 13, 28, 3 },
[APR_PROFILE_STANDARD] = { 2, 3, 2 },
[APR_PROFILE_HQ] = { 1, 1, 1 },
[APR_PROFILE_4444] = { 3, 2, 1 },
[APR_PROFILE_4444_XQ] = { 9, 4, 1 },
};

/*
* Fixed rate-control settings: macroblock class thresholds (with quantiser
* refinement off in bit 63), the alpha, alpha0 and gamma0 weights, four
* betas and sixteen correlation/quantiser thresholds. They do not depend on
* profile, size or input format.
*/
#define APR_RC_THRESHOLDS 0x8000402015100c0cULL
#define APR_RC_ALPHA 0x80
Expand All @@ -231,6 +249,70 @@ EXPORT_SYMBOL_IF_KUNIT(apr_dc_scale_reg);
static const u32 apr_rc_beta[4] = { 0x880080, 0x4e00c5, 0x9000d0, 0x200122 };
#define APR_RC_QUANT 0x400200

/*
* 422 HQ frame size for a picture of @pixels luma samples: fixed steps up
* to 2.36 Mpixel, then half a byte per pixel. Other profiles scale it.
*/
static u64 apr_hq_budget(u64 pixels)
{
static const struct { u32 pixels, bytes; } steps[] = {
{ 349920, 294912 }, { 414720, 344064 }, { 691200, 442368 },
{ 921600, 516096 }, { 1555200, 884736 }, { 2073600, 1032192 },
{ 2359297, 1179648 },
};

for (unsigned int i = 0; i < ARRAY_SIZE(steps); i++)
if (pixels <= steps[i].pixels)
return steps[i].bytes;
return pixels / 2;
}

/* Round n / 8 to a single-precision float, then down to an integer. */
static u64 apr_eighths_f32_floor(u64 n)
{
int shift;
u64 unit, q, r;

if (!n)
return 0;
/* 24 significant bits: the float's unit, in eighths, is 2^(fls - 24). */
shift = fls64(n) - 24;
if (shift <= 0)
return n / 8;
unit = 1ULL << shift;
q = n >> shift;
r = n & (unit - 1);
if (r > unit / 2 || (r == unit / 2 && (q & 1)))
q++;
return (q << shift) / 8;
}

/*
* Target and maximum frame sizes in bytes for a profile: the HQ budget
* scaled by the profile, in whole 4 KiB units, eight ninths of that as the
* target and nine eighths of the target as the maximum.
*/
void apr_frame_budget(enum apr_profile profile, u32 width, u32 height,
u32 *target, u32 *max)
{
u64 m = apr_hq_budget((u64)width * height) * apr_profile_policy[profile].num /
apr_profile_policy[profile].den;

m = round_down(max_t(u64, m, SZ_4K), SZ_4K);
*target = 8 * m / 9;
*max = apr_eighths_f32_floor(9ULL * *target);
}
EXPORT_SYMBOL_IF_KUNIT(apr_frame_budget);

/* A frame budget as Q3 bytes per macroblock, after header and slice-table bytes. */
static u16 apr_mb_budget(u32 bytes, u32 slices, u32 mbs)
{
u32 overhead = 164 + 8 * slices;
u32 left = bytes > overhead ? bytes - overhead : 1;

return min(16383U, 8 * (left / mbs));
}

/* Slices per picture: full slices per row, then the remainder in powers of two. */
u32 apr_slice_count(u32 width, u32 height, u32 log2_slice)
{
Expand All @@ -245,7 +327,7 @@ int apr_build_desc(const struct apr_frame *f, u8 *desc)
bool is444 = apr_input_is_444(f->input);
bool deep = f->input == APR_INPUT_422_16 || f->input == APR_INPUT_444_16;
enum apr_qtable table = apr_profile_qtable(f->profile);
u32 min_luma, min_chroma, cmd, slices;
u32 min_luma, min_chroma, cmd, slices, mbs, target, max;
unsigned int i;

if (f->profile >= APR_PROFILES || is444 != apr_profile_is_444(f->profile))
Expand All @@ -264,6 +346,8 @@ int apr_build_desc(const struct apr_frame *f, u8 *desc)
slices = apr_slice_count(f->width, f->height, APR_SLICE_LOG2);
if (slices > U16_MAX)
return -EINVAL;
mbs = DIV_ROUND_UP(f->width, 16) * DIV_ROUND_UP(f->height, 16);
apr_frame_budget(f->profile, f->width, f->height, &target, &max);

memset(desc, 0, APR_DESC_BYTES);

Expand All @@ -288,7 +372,7 @@ int apr_build_desc(const struct apr_frame *f, u8 *desc)

put_unaligned_be16(slices, desc + D_SLICES);
desc[D_SLICE_LOG2] = APR_SLICE_LOG2 << 4;
desc[D_QUANT_INDEX] = APR_QUANT_INDEX;
desc[D_QUANT_INDEX] = apr_profile_policy[f->profile].quant_index;

put_unaligned_be16(D_FH_BYTES, desc + D_FH_SIZE);
desc[D_FH_VERSION] = is444 ? 1 : 0;
Expand All @@ -304,8 +388,8 @@ int apr_build_desc(const struct apr_frame *f, u8 *desc)
/* Both quantisation matrices are carried in the frame header. */
desc[D_FH_FLAGS] = 0x03;

put_unaligned_le16(DIV_ROUND_UP(f->width, 16), desc + D_MB_W);
put_unaligned_le16(DIV_ROUND_UP(f->height, 16), desc + D_MB_H);
put_unaligned_le16(apr_mb_budget(target, slices, mbs), desc + D_RC_TARGET);
put_unaligned_le16(apr_mb_budget(max, slices, mbs), desc + D_RC_MAX);
put_unaligned_le64(APR_RC_THRESHOLDS, desc + D_RC_THRESHOLDS);
desc[D_RC_ALPHA] = APR_RC_ALPHA;
desc[D_RC_ALPHA0] = APR_RC_ALPHA0;
Expand Down
54 changes: 52 additions & 2 deletions drivers/media/platform/apple/prores/apple-prores-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,10 @@ static void apr_test_desc_422(struct kunit *test)
/* 20 macroblocks per row: two 8-wide slices and one 4-wide. */
KUNIT_EXPECT_EQ(test, get_unaligned_be16(d + 0x78), 45);
KUNIT_EXPECT_EQ(test, d[0x7a], 0x30);
KUNIT_EXPECT_EQ(test, get_unaligned_le16(d + 0x110), 20);
KUNIT_EXPECT_EQ(test, get_unaligned_le16(d + 0x112), 15);
KUNIT_EXPECT_EQ(test, d[0x7b], 2); /* 422 starting quantiser */
/* 320x240 422: 174762 and 196607 bytes over 300 macroblocks. */
KUNIT_EXPECT_EQ(test, get_unaligned_le16(d + 0x110), 4640);
KUNIT_EXPECT_EQ(test, get_unaligned_le16(d + 0x112), 5224);
KUNIT_EXPECT_EQ(test, d[0x90], 0); /* matrices live in the engine */
KUNIT_EXPECT_EQ(test, get_unaligned_le32(d + 0x170), 0x23);
KUNIT_EXPECT_EQ(test, get_unaligned_le32(d + 0x174), 0);
Expand Down Expand Up @@ -168,6 +170,53 @@ static void apr_test_desc_rejects(struct kunit *test)
KUNIT_EXPECT_EQ(test, apr_build_desc(&f, d), -EINVAL);
}

static void apr_test_budgets(struct kunit *test)
{
/* Target, maximum and the Q3 per-macroblock words for 1080p and 2160p. */
static const struct {
enum apr_profile profile;
u32 w, h, target, max;
u16 q_target, q_max;
} cases[] = {
{ APR_PROFILE_PROXY, 1920, 1080, 189326, 212991, 176, 200 },
{ APR_PROFILE_LT, 1920, 1080, 425984, 479232, 408, 456 },
{ APR_PROFILE_STANDARD, 1920, 1080, 611669, 688127, 584, 664 },
{ APR_PROFILE_HQ, 1920, 1080, 917504, 1032192, 888, 1000 },
{ APR_PROFILE_4444, 1920, 1080, 1376256, 1548288, 1336, 1504 },
{ APR_PROFILE_4444_XQ, 1920, 1080, 2064384, 2322432, 2008, 2264 },
{ APR_PROFILE_PROXY, 3840, 2160, 757304, 851967, 176, 200 },
{ APR_PROFILE_LT, 3840, 2160, 1711217, 1925119, 408, 464 },
{ APR_PROFILE_STANDARD, 3840, 2160, 2457600, 2764800, 592, 672 },
{ APR_PROFILE_HQ, 3840, 2160, 3684579, 4145151, 896, 1008 },
{ APR_PROFILE_4444, 3840, 2160, 5526869, 6217727, 1352, 1520 },
{ APR_PROFILE_4444_XQ, 3840, 2160, 8293944, 9330687, 2032, 2288 },
};
u8 *d = kunit_kzalloc(test, APR_DESC_BYTES, GFP_KERNEL);
unsigned int i;

KUNIT_ASSERT_NOT_NULL(test, d);
for (i = 0; i < ARRAY_SIZE(cases); i++) {
struct apr_frame f = apr_test_frame();
bool is444 = apr_profile_is_444(cases[i].profile);
u32 target, max;

apr_frame_budget(cases[i].profile, cases[i].w, cases[i].h, &target, &max);
KUNIT_EXPECT_EQ_MSG(test, target, cases[i].target, "case %u", i);
KUNIT_EXPECT_EQ_MSG(test, max, cases[i].max, "case %u", i);
f.profile = cases[i].profile;
f.input = is444 ? APR_INPUT_444_8 : APR_INPUT_422_8;
f.width = cases[i].w;
f.height = cases[i].h;
f.luma_stride = cases[i].w;
f.chroma_stride = cases[i].w * (is444 ? 2 : 1);
KUNIT_ASSERT_EQ(test, apr_build_desc(&f, d), 0);
KUNIT_EXPECT_EQ_MSG(test, get_unaligned_le16(d + 0x110), cases[i].q_target,
"case %u", i);
KUNIT_EXPECT_EQ_MSG(test, get_unaligned_le16(d + 0x112), cases[i].q_max,
"case %u", i);
}
}

static void apr_test_slices(struct kunit *test)
{
KUNIT_EXPECT_EQ(test, apr_slice_count(320, 240, 3), 45);
Expand Down Expand Up @@ -202,6 +251,7 @@ static struct kunit_case apr_test_cases[] = {
KUNIT_CASE(apr_test_desc_422),
KUNIT_CASE(apr_test_desc_444),
KUNIT_CASE(apr_test_desc_rejects),
KUNIT_CASE(apr_test_budgets),
KUNIT_CASE(apr_test_slices),
KUNIT_CASE(apr_test_output),
{}
Expand Down
2 changes: 2 additions & 0 deletions drivers/media/platform/apple/prores/apple-prores.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ bool apr_input_is_444(enum apr_input input);
enum apr_qtable apr_profile_qtable(enum apr_profile profile);
u32 apr_qmat_reg(const u8 *qmat, unsigned int index);
u32 apr_dc_scale_reg(unsigned int index);
void apr_frame_budget(enum apr_profile profile, u32 width, u32 height,
u32 *target, u32 *max);
u32 apr_slice_count(u32 width, u32 height, u32 log2_slice);
int apr_build_desc(const struct apr_frame *f, u8 *desc);
int apr_check_output(const u8 *out, size_t capacity, u32 *frame_bytes);
Expand Down