From 714719295c57ed58a6063be7b078ce9b36b83af1 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Wed, 2 Dec 2026 22:53:33 +1100 Subject: [PATCH 1/2] media: apple: isp: correct each module's colour shading from its NVM The setfile's lens shading tables (section 0xc003) are made for a typical module. Each module's NVM carries its own measured shading of the four Bayer planes, which the previous change reads. Use it for the colour part of the correction: - Keep the stock brightness curve: the sum of the four stock gains at each table point, scaled from the point where it is lowest. - Give each plane this module's colour ratio there, (sum of the NVM planes) / (NVM plane), relative to the same point. Every plane then leaves the same shape, so a grey scene stays grey towards the corners, and the centre keeps its stock gains. - The NVM grid can be stored turned by 180 degrees against the tables. Use the orientation whose colour ratios are closer to the stock ones. - Sample the NVM grid bilinearly at its own (non-uniform) knots, in integer fixed point. Leave the stock tables untouched if the grid or the tables look wrong (zero samples, knots out of order, tables that do not share one grid). - Keep each record's leading field equal to its table's largest gain, as in every shipped setfile. The spatial noise filter gains are computed afterwards, from the new tables. Add a KUnit test: colour flatness on a synthetic module in both orientations, centre gains and record maxima, and rejection of bad grids. Signed-off-by: Ryan Murray --- .../media/platform/apple/isp/isp-cam-test.c | 155 +++++++++++ drivers/media/platform/apple/isp/isp-cam.c | 256 ++++++++++++++++++ 2 files changed, 411 insertions(+) diff --git a/drivers/media/platform/apple/isp/isp-cam-test.c b/drivers/media/platform/apple/isp/isp-cam-test.c index 846253d48a82a..eefb2fed36293 100644 --- a/drivers/media/platform/apple/isp/isp-cam-test.c +++ b/drivers/media/platform/apple/isp/isp-cam-test.c @@ -1100,6 +1100,160 @@ static void isp_test_permodule_lsc_wire(struct kunit *test) KUNIT_EXPECT_EQ(test, w.grid_calls, 2); } +enum { + ISP_TEST_US_G = 5, + ISP_TEST_US_N = ISP_TEST_US_G * ISP_TEST_US_G, + ISP_TEST_US_LSC = 0x54 + 4 * ISP_TEST_US_N * 2, +}; + +/* Module shading of plane @t at grid point (@x, @y); @tilt shades plane 1. */ +static u32 isp_test_us_nvm(int t, int x, int y, int tilt) +{ + int r2 = (x - 2) * (x - 2) + (y - 2) * (y - 2); + + return 200 - 12 * r2 - (t == 1 ? tilt * (4 - x) : 0) - + (t == 2 ? 3 * y : 0); +} + +/* + * A 1024x1024 setfile whose 5x5 tables sit on the 5x5 NVM points. The + * stock tables correct a typical module with half this module's tilt; + * @turned tables see the module turned by 180 degrees, so the NVM grid + * must be read turned to match them. + */ +static u8 *isp_test_us_setfile(struct kunit *test, size_t *len, bool turned) +{ + u8 *data, *sec; + + *len = 0x14 + 12 + ISP_TEST_US_LSC; + data = kunit_kzalloc(test, *len, GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, data); + sec = data + 0x14 + 12; + put_unaligned_le32(8 + ISP_TEST_US_LSC, sec - 12); + put_unaligned_le32(0xc003, sec - 4); + put_unaligned_le16(1024, sec + 6); + put_unaligned_le16(1024, sec + 8); + for (int t = 0; t < 4; t++) { + u8 *rec = sec + 0xa + 18 * t; + + put_unaligned_le16(128, rec + 2); + put_unaligned_le16(128, rec + 4); + put_unaligned_le16(t * ISP_TEST_US_N, rec + 6); + put_unaligned_le16(ISP_TEST_US_G, rec + 14); + put_unaligned_le16(ISP_TEST_US_G, rec + 16); + } + for (int k = 0; k < ISP_TEST_US_N; k++) { + int x = k % ISP_TEST_US_G, y = k / ISP_TEST_US_G; + int nx = turned ? 4 - x : x, ny = turned ? 4 - y : y; + int r2 = (x - 2) * (x - 2) + (y - 2) * (y - 2); + u32 m[4], sum = 0; + + for (int t = 0; t < 4; t++) { + m[t] = isp_test_us_nvm(t, nx, ny, 2); + sum += m[t]; + } + for (int t = 0; t < 4; t++) + put_unaligned_le16((4096 + 512 * r2) * sum / (4 * m[t]), + sec + 0x54 + 2 * (t * ISP_TEST_US_N + k)); + } + return data; +} + +/* Gain times module shading of plane @t at table point @k. */ +static u64 isp_test_us_out(const u8 *sec, int t, int k, bool turned) +{ + int x = k % ISP_TEST_US_G, y = k / ISP_TEST_US_G; + + if (turned) { + x = 4 - x; + y = 4 - y; + } + return (u64)get_unaligned_le16(sec + 0x54 + 2 * (t * ISP_TEST_US_N + k)) * + isp_test_us_nvm(t, x, y, 4); +} + +static void isp_test_unit_shading(struct kunit *test) +{ + /* binary32 0.0, 0.25, 0.5, 0.75, 1.0 */ + static const u32 coords[ISP_TEST_US_G] = { + 0x00000000, 0x3e800000, 0x3f000000, 0x3f400000, 0x3f800000, + }; + struct apple_isp *isp = isp_test_capture_alloc(test); + struct isp_permodule_lsc lsc = { + .valid = true, .num_x = ISP_TEST_US_G, .num_y = ISP_TEST_US_G, + .bit_depth = 10, .data_size = 4 * ISP_TEST_US_N * 2, + }; + __le16 *nvm = kunit_kzalloc(test, lsc.data_size, GFP_KERNEL); + const int ref = 2 * ISP_TEST_US_G + 2; + + KUNIT_ASSERT_NOT_NULL(test, nvm); + KUNIT_EXPECT_EQ(test, isp_lsc_coord(0x3e800000), LSC_ONE / 4); + KUNIT_EXPECT_EQ(test, isp_lsc_coord(0x3f800000), LSC_ONE); + KUNIT_EXPECT_EQ(test, isp_lsc_coord(0x00000001), 0); + KUNIT_EXPECT_LT(test, isp_lsc_coord(0xbf000000), 0); + KUNIT_EXPECT_LT(test, isp_lsc_coord(0x3f800001), 0); + KUNIT_EXPECT_LT(test, isp_lsc_coord(0x7fc00000), 0); + + /* 10-bit samples, stored upright; garbage above bit 9 is masked. */ + for (int t = 0; t < 4; t++) + for (int k = 0; k < ISP_TEST_US_N; k++) + nvm[t * ISP_TEST_US_N + k] = cpu_to_le16(0xfc00 | + 4 * isp_test_us_nvm(t, k % ISP_TEST_US_G, + k / ISP_TEST_US_G, 4)); + lsc.data = (u8 *)nvm; + memcpy(lsc.x_coords, coords, sizeof(coords)); + memcpy(lsc.y_coords, coords, sizeof(coords)); + + for (int turned = 0; turned < 2; turned++) { + size_t len; + u8 *data = isp_test_us_setfile(test, &len, turned); + u8 *sec = data + 0x14 + 12; + u16 stock_ref[4]; + + for (int t = 0; t < 4; t++) + stock_ref[t] = get_unaligned_le16(sec + 0x54 + + 2 * (t * ISP_TEST_US_N + ref)); + KUNIT_ASSERT_TRUE(test, isp_setfile_unit_shading(isp, data, len, &lsc)); + + for (int t = 0; t < 4; t++) { + u16 top = 0; + + /* the centre keeps its stock gains */ + KUNIT_EXPECT_EQ(test, get_unaligned_le16(sec + 0x54 + + 2 * (t * ISP_TEST_US_N + ref)), stock_ref[t]); + for (int k = 0; k < ISP_TEST_US_N; k++) { + u64 a = isp_test_us_out(sec, t, k, turned) * + isp_test_us_out(sec, 0, ref, turned); + u64 b = isp_test_us_out(sec, 0, k, turned) * + isp_test_us_out(sec, t, ref, turned); + + /* every plane leaves the same shape: grey stays grey */ + KUNIT_EXPECT_LE_MSG(test, abs_diff(a, b), b / 200, + "turned %d plane %d point %d", turned, t, k); + top = max(top, get_unaligned_le16(sec + 0x54 + + 2 * (t * ISP_TEST_US_N + k))); + } + KUNIT_EXPECT_EQ(test, get_unaligned_le16(sec + 0xa + 18 * t), top); + } + } + + /* Bad grids leave the stock tables alone. */ + { + size_t len; + u8 *data = isp_test_us_setfile(test, &len, false); + u8 *copy = kunit_kmalloc(test, len, GFP_KERNEL); + + KUNIT_ASSERT_NOT_NULL(test, copy); + memcpy(copy, data, len); + lsc.x_coords[2] = lsc.x_coords[1]; + KUNIT_EXPECT_FALSE(test, isp_setfile_unit_shading(isp, data, len, &lsc)); + lsc.x_coords[2] = coords[2]; + nvm[3 * ISP_TEST_US_N + 7] = cpu_to_le16(0xfc00); + KUNIT_EXPECT_FALSE(test, isp_setfile_unit_shading(isp, data, len, &lsc)); + KUNIT_EXPECT_MEMEQ(test, data, copy, len); + } +} + static void isp_test_failed_fw_stop_lights_off(struct kunit *test) { struct apple_isp *isp = isp_test_indicator_alloc(test); @@ -1141,6 +1295,7 @@ static struct kunit_case isp_cam_test_cases[] = { KUNIT_CASE(isp_test_stream_failure_lights_off), KUNIT_CASE(isp_test_failed_fw_stop_lights_off), KUNIT_CASE(isp_test_permodule_lsc_wire), + KUNIT_CASE(isp_test_unit_shading), {} }; diff --git a/drivers/media/platform/apple/isp/isp-cam.c b/drivers/media/platform/apple/isp/isp-cam.c index ba1da5687b9e1..c3a38fd470fad 100644 --- a/drivers/media/platform/apple/isp/isp-cam.c +++ b/drivers/media/platform/apple/isp/isp-cam.c @@ -844,6 +844,259 @@ static void isp_setfile_snf_radial(struct apple_isp *isp, void *data, dev_warn_once(isp->dev, "setfile: noise filter gains not computed, keeping the stock ones\n"); } +#define LSC_Q 24 +#define LSC_ONE BIT_ULL(LSC_Q) +#define LSC_RATIO_MAX BIT_ULL(24) /* 256.0 in Q16 */ + +/* A binary32 in [0, 1] as Q24, or -1 for anything else. */ +static s64 isp_lsc_coord(u32 bits) +{ + u32 exp = (bits >> 23) & 0xff; + u64 mant = BIT(23) | (bits & GENMASK(22, 0)); + + if (bits > 0x3f800000) /* negative, above 1.0, inf or NaN */ + return -1; + if (exp < 127 - 24) + return 0; + /* value = mant * 2^(exp - 150), so Q24 = mant * 2^(exp - 126) */ + return exp >= 126 ? mant << (exp - 126) : mant >> (126 - exp); +} + +/* One NVM sample; @flip turns the grid by 180 degrees. */ +static u32 isp_lsc_raw(const struct isp_permodule_lsc *lsc, int plane, int x, + int y, bool flip) +{ + int n = lsc->num_x * lsc->num_y; + int idx = y * lsc->num_x + x; + + if (flip) + idx = n - 1 - idx; + idx += plane * n; + if (lsc->bit_depth > 8) + return get_unaligned_le16(lsc->data + 2 * idx) & + GENMASK(lsc->bit_depth - 1, 0); + return lsc->data[idx]; +} + +/* Bilinear NVM sample at (@u, @v) in Q24, in raw units times 256. */ +static u64 isp_lsc_at(const struct isp_permodule_lsc *lsc, const s64 *xq, + const s64 *yq, int plane, s64 u, s64 v, bool flip) +{ + int i = 0, j = 0; + u64 fx, fy, top, bot; + + u = clamp(u, xq[0], xq[lsc->num_x - 1]); + v = clamp(v, yq[0], yq[lsc->num_y - 1]); + while (i < lsc->num_x - 2 && u >= xq[i + 1]) + i++; + while (j < lsc->num_y - 2 && v >= yq[j + 1]) + j++; + fx = div64_u64((u64)(u - xq[i]) << 8, xq[i + 1] - xq[i]); + fy = div64_u64((u64)(v - yq[j]) << 8, yq[j + 1] - yq[j]); + + top = isp_lsc_raw(lsc, plane, i, j, flip) * (256 - fx) + + isp_lsc_raw(lsc, plane, i + 1, j, flip) * fx; + bot = isp_lsc_raw(lsc, plane, i, j + 1, flip) * (256 - fx) + + isp_lsc_raw(lsc, plane, i + 1, j + 1, flip) * fx; + return (top * (256 - fy) + bot * fy) >> 8; +} + +/* + * NVM samples for the four planes at table point @k, and their sum. + * Returns false if any of them is zero. + */ +static bool isp_lsc_point(const struct isp_permodule_lsc *lsc, const s64 *xq, + const s64 *yq, u32 k, u32 gw, u32 sx, u32 sy, + u32 plane_w, u32 plane_h, bool flip, u64 m[4], + u64 *sum) +{ + s64 u = min_t(u64, div_u64((u64)(k % gw) * sx << LSC_Q, plane_w), + LSC_ONE); + s64 v = min_t(u64, div_u64((u64)(k / gw) * sy << LSC_Q, plane_h), + LSC_ONE); + + *sum = 0; + for (int t = 0; t < 4; t++) { + m[t] = isp_lsc_at(lsc, xq, yq, t, u, v, flip); + if (!m[t]) + return false; + *sum += m[t]; + } + return true; +} + +/* @sum / @m over the same at the reference point (@ref, Q16), in Q16. */ +static u64 isp_lsc_ratio(u64 m, u64 sum, u64 ref) +{ + return min_t(u64, div64_u64(div64_u64(sum << 16, m) << 16, ref), + LSC_RATIO_MAX); +} + +/* + * Correct this module's colour shading from its own NVM measurement. + * + * Section 0xc003 holds four Q12 gain tables, one per Bayer plane, made + * for a typical module: their sum sets how much the brightness rises + * towards the edges, their ratios correct the typical colour shading. + * The NVM grid is this module's measured shading of the same four planes. + * + * Keep the stock brightness curve (the sum of the four gains at each + * point) and replace the stock colour ratios with this module's: plane t + * gets + * + * g_t(k) = g_t(ref) * S(k) / S(ref) * r_t(k) / r_t(ref) + * + * where S is the sum of the stock gains, r_t = (sum of NVM planes) / + * (NVM plane t), and ref is the point where S is lowest, the optical + * centre of the stock tables. Every plane then leaves the same shape, so + * a grey scene stays grey to the corners, and the centre keeps its stock + * gains. + * + * The NVM grid may be stored turned by 180 degrees against the tables. + * Take the orientation whose colour ratios are closer to the stock ones. + */ +static bool isp_setfile_unit_shading(struct apple_isp *isp, void *data, + size_t len, + const struct isp_permodule_lsc *lsc) +{ + u32 lsc_size, w, h, plane_w, plane_h, sx, sy, gw, gh, n; + size_t sec = isp_setfile_find(data, len, 0xc003, &lsc_size); + s64 xq[ISP_LSC_MAX_GRID_X], yq[ISP_LSC_MAX_GRID_Y]; + u64 m[4], sum, ref_sum, ref_r[4], ref_s[4], err[2] = {}; + u64 ref_stock = U64_MAX; + void *tab[4]; + u32 ref = 0; + bool flip; + int t; + + if (!sec || !lsc->valid || !lsc->data || lsc_size < 0x54) + return false; + + w = get_unaligned_le16(data + sec + 6); + h = get_unaligned_le16(data + sec + 8); + plane_w = w / 2; + plane_h = h / 2; + if (!plane_w || !plane_h || w > 16384 || h > 16384) + return false; + + sx = get_unaligned_le16(data + sec + 0xa + 2); + sy = get_unaligned_le16(data + sec + 0xa + 4); + gw = get_unaligned_le16(data + sec + 0xa + 14); + gh = get_unaligned_le16(data + sec + 0xa + 16); + n = gw * gh; + if (!sx || !sy || sx > 1024 || sy > 1024 || gw < 2 || gh < 2) + return false; + /* One grid shared by the four tables, no origin offset. */ + for (t = 0; t < 4; t++) { + const void *rec = data + sec + 0xa + 18 * t; + u64 off = 0x54 + 2ULL * get_unaligned_le16(rec + 6); + + if (get_unaligned_le16(rec + 2) != sx || + get_unaligned_le16(rec + 4) != sy || + get_unaligned_le16(rec + 10) || get_unaligned_le16(rec + 12) || + get_unaligned_le16(rec + 14) != gw || + get_unaligned_le16(rec + 16) != gh || + off + 2ULL * n > lsc_size) + return false; + tab[t] = data + sec + off; + } + + for (int i = 0; i < lsc->num_x; i++) { + xq[i] = isp_lsc_coord(lsc->x_coords[i]); + if (xq[i] < 0 || (i && xq[i] <= xq[i - 1])) + return false; + } + for (int j = 0; j < lsc->num_y; j++) { + yq[j] = isp_lsc_coord(lsc->y_coords[j]); + if (yq[j] < 0 || (j && yq[j] <= yq[j - 1])) + return false; + } + + for (u32 k = 0; k < n; k++) { + u64 s = 0; + + for (t = 0; t < 4; t++) { + u32 g = get_unaligned_le16(tab[t] + 2 * k); + + if (!g) + return false; + s += g; + } + if (s < ref_stock) { + ref_stock = s; + ref = k; + } + } + /* Stock colour ratios as plane / sum, the inverse of the NVM ones. */ + for (t = 0; t < 4; t++) + ref_s[t] = div64_u64((u64)get_unaligned_le16(tab[t] + 2 * ref) << 16, + ref_stock); + + for (int f = 0; f < 2; f++) { + if (!isp_lsc_point(lsc, xq, yq, ref, gw, sx, sy, plane_w, + plane_h, f, m, &ref_sum)) + return false; + for (t = 0; t < 4; t++) + ref_r[t] = div64_u64(ref_sum << 16, m[t]); + + for (u32 k = 0; k < n; k++) { + u64 s = 0; + + if (!isp_lsc_point(lsc, xq, yq, k, gw, sx, sy, plane_w, + plane_h, f, m, &sum)) + return false; + for (t = 0; t < 4; t++) + s += get_unaligned_le16(tab[t] + 2 * k); + for (t = 0; t < 4; t++) { + u64 unit = isp_lsc_ratio(m[t], sum, ref_r[t]); + u64 stock = isp_lsc_ratio(s, get_unaligned_le16(tab[t] + 2 * k), + ref_s[t]); + + err[f] += unit > stock ? unit - stock : stock - unit; + } + } + } + flip = err[1] < err[0]; + + isp_lsc_point(lsc, xq, yq, ref, gw, sx, sy, plane_w, plane_h, flip, m, + &ref_sum); + for (t = 0; t < 4; t++) { + ref_r[t] = div64_u64(ref_sum << 16, m[t]); + ref_s[t] = get_unaligned_le16(tab[t] + 2 * ref); + } + + for (u32 k = 0; k < n; k++) { + u64 s = 0; + + isp_lsc_point(lsc, xq, yq, k, gw, sx, sy, plane_w, plane_h, + flip, m, &sum); + for (t = 0; t < 4; t++) + s += get_unaligned_le16(tab[t] + 2 * k); + for (t = 0; t < 4; t++) { + u64 base = div64_u64(ref_s[t] * s, ref_stock); + u64 g = (base * isp_lsc_ratio(m[t], sum, ref_r[t])) >> 16; + + put_unaligned_le16(min_t(u64, g, U16_MAX), + tab[t] + 2 * k); + } + } + + /* Each record starts with its table's largest gain. */ + for (t = 0; t < 4; t++) { + u16 top = 0; + + for (u32 k = 0; k < n; k++) + top = max(top, get_unaligned_le16(tab[t] + 2 * k)); + put_unaligned_le16(top, data + sec + 0xa + 18 * t); + } + + dev_dbg(isp->dev, + "setfile: module colour shading from NVM %ux%u, %s, ref %u/%u\n", + lsc->num_x, lsc->num_y, flip ? "turned" : "upright", ref % gw, + ref / gw); + return true; +} + static int isp_ch_load_setfile(struct apple_isp *isp, u32 ch) { struct isp_format *fmt = isp_get_format(isp, ch); @@ -893,6 +1146,9 @@ static int isp_ch_load_setfile(struct apple_isp *isp, u32 ch) if (isp->hw->gen == ISP_GEN_T6040 || isp->hw->setfile_stride_fixup) isp_setfile_fixup_h17(isp->data_surf->virt, len); + if (isp->lsc.valid) + isp_setfile_unit_shading(isp, isp->data_surf->virt, len, + &isp->lsc); isp_setfile_snf_radial(isp, isp->data_surf->virt, len, setfile->snf_centre_dy); From 85d23651d15eb7b21248a83facdcd1bc1191742d Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Wed, 2 Dec 2026 23:06:29 +1100 Subject: [PATCH 2/2] media: apple: isp: anchor module colour shading at the shading centre The reference point was the table point with the lowest stock gain sum. In isp_2224_01XX.dat that point sits at the bottom edge, not at the optical centre, so the per-module colour shading was normalised to an edge of the frame there. Use the table point at the shading centre instead: the frame's centre moved down by the setfile's snf_centre_dy, the same centre the spatial noise filter uses. Reject a centre outside the frame. Signed-off-by: Ryan Murray --- .../media/platform/apple/isp/isp-cam-test.c | 8 ++-- drivers/media/platform/apple/isp/isp-cam.c | 44 +++++++++---------- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/drivers/media/platform/apple/isp/isp-cam-test.c b/drivers/media/platform/apple/isp/isp-cam-test.c index eefb2fed36293..1b131efd52ff2 100644 --- a/drivers/media/platform/apple/isp/isp-cam-test.c +++ b/drivers/media/platform/apple/isp/isp-cam-test.c @@ -1213,7 +1213,7 @@ static void isp_test_unit_shading(struct kunit *test) for (int t = 0; t < 4; t++) stock_ref[t] = get_unaligned_le16(sec + 0x54 + 2 * (t * ISP_TEST_US_N + ref)); - KUNIT_ASSERT_TRUE(test, isp_setfile_unit_shading(isp, data, len, &lsc)); + KUNIT_ASSERT_TRUE(test, isp_setfile_unit_shading(isp, data, len, &lsc, 0)); for (int t = 0; t < 4; t++) { u16 top = 0; @@ -1246,10 +1246,12 @@ static void isp_test_unit_shading(struct kunit *test) KUNIT_ASSERT_NOT_NULL(test, copy); memcpy(copy, data, len); lsc.x_coords[2] = lsc.x_coords[1]; - KUNIT_EXPECT_FALSE(test, isp_setfile_unit_shading(isp, data, len, &lsc)); + KUNIT_EXPECT_FALSE(test, isp_setfile_unit_shading(isp, data, len, &lsc, 0)); lsc.x_coords[2] = coords[2]; + /* a shading centre below the frame */ + KUNIT_EXPECT_FALSE(test, isp_setfile_unit_shading(isp, data, len, &lsc, 600)); nvm[3 * ISP_TEST_US_N + 7] = cpu_to_le16(0xfc00); - KUNIT_EXPECT_FALSE(test, isp_setfile_unit_shading(isp, data, len, &lsc)); + KUNIT_EXPECT_FALSE(test, isp_setfile_unit_shading(isp, data, len, &lsc, 0)); KUNIT_EXPECT_MEMEQ(test, data, copy, len); } } diff --git a/drivers/media/platform/apple/isp/isp-cam.c b/drivers/media/platform/apple/isp/isp-cam.c index c3a38fd470fad..331bababca4b8 100644 --- a/drivers/media/platform/apple/isp/isp-cam.c +++ b/drivers/media/platform/apple/isp/isp-cam.c @@ -947,25 +947,27 @@ static u64 isp_lsc_ratio(u64 m, u64 sum, u64 ref) * g_t(k) = g_t(ref) * S(k) / S(ref) * r_t(k) / r_t(ref) * * where S is the sum of the stock gains, r_t = (sum of NVM planes) / - * (NVM plane t), and ref is the point where S is lowest, the optical - * centre of the stock tables. Every plane then leaves the same shape, so - * a grey scene stays grey to the corners, and the centre keeps its stock - * gains. + * (NVM plane t), and ref is the table point at the shading centre, + * @centre_dy shading-frame pixels below the frame's centre. Every plane + * then leaves the same shape, so a grey scene stays grey to the corners, + * and the centre keeps its stock gains. * * The NVM grid may be stored turned by 180 degrees against the tables. * Take the orientation whose colour ratios are closer to the stock ones. */ static bool isp_setfile_unit_shading(struct apple_isp *isp, void *data, size_t len, - const struct isp_permodule_lsc *lsc) + const struct isp_permodule_lsc *lsc, + int centre_dy) { u32 lsc_size, w, h, plane_w, plane_h, sx, sy, gw, gh, n; size_t sec = isp_setfile_find(data, len, 0xc003, &lsc_size); s64 xq[ISP_LSC_MAX_GRID_X], yq[ISP_LSC_MAX_GRID_Y]; u64 m[4], sum, ref_sum, ref_r[4], ref_s[4], err[2] = {}; - u64 ref_stock = U64_MAX; + u64 ref_stock = 0; + s64 cy; void *tab[4]; - u32 ref = 0; + u32 ref; bool flip; int t; @@ -1012,21 +1014,19 @@ static bool isp_setfile_unit_shading(struct apple_isp *isp, void *data, return false; } - for (u32 k = 0; k < n; k++) { - u64 s = 0; - - for (t = 0; t < 4; t++) { - u32 g = get_unaligned_le16(tab[t] + 2 * k); - - if (!g) + for (u32 k = 0; k < n; k++) + for (t = 0; t < 4; t++) + if (!get_unaligned_le16(tab[t] + 2 * k)) return false; - s += g; - } - if (s < ref_stock) { - ref_stock = s; - ref = k; - } - } + + /* The tables cover one colour plane: half the frame's pixels. */ + cy = (s64)h / 2 + centre_dy; + if (cy <= 0 || cy >= h) + return false; + ref = min_t(u32, DIV_ROUND_CLOSEST((u32)cy / 2, sy), gh - 1) * gw + + min_t(u32, DIV_ROUND_CLOSEST(w / 4, sx), gw - 1); + for (t = 0; t < 4; t++) + ref_stock += get_unaligned_le16(tab[t] + 2 * ref); /* Stock colour ratios as plane / sum, the inverse of the NVM ones. */ for (t = 0; t < 4; t++) ref_s[t] = div64_u64((u64)get_unaligned_le16(tab[t] + 2 * ref) << 16, @@ -1148,7 +1148,7 @@ static int isp_ch_load_setfile(struct apple_isp *isp, u32 ch) if (isp->lsc.valid) isp_setfile_unit_shading(isp, isp->data_surf->virt, len, - &isp->lsc); + &isp->lsc, setfile->snf_centre_dy); isp_setfile_snf_radial(isp, isp->data_surf->virt, len, setfile->snf_centre_dy);