Skip to content

media: apple: isp: apply per-module lens shading and DSC to setfile - #3902

Closed
RyanTheTide wants to merge 3 commits into
camera-all/per-module-lsc-nvmfrom
camera-all/per-module-lsc-apply
Closed

RyanTheTide wants to merge 3 commits into
camera-all/per-module-lsc-nvmfrom
camera-all/per-module-lsc-apply

Conversation

@RyanTheTide

@RyanTheTide RyanTheTide commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Replaced by #3930 (same work, cleaned up: Signed-off-by on every commit, host-test fix, values limited to what our own measurements support). Closing this one.

Resample the cached per-module lens shading grid from sensor NVM onto
setfile section 0xc003 using integer fixed-point math (non-uniform
Bessel-Hermite bicubic interpolation in the interior, bilinear on border
cells, against an ideal cos^4 illumination curve) before computing the
SNF radial gains:

- Average Gr (ch 0) and Gb (ch 3) shading tables and update their channel
  header maxima on legacy ISP generations.
- Mark section 0x0a2a when present so firmware knows per-module shading
  was applied.
- Multiply section 0xc003 by section 0xc021 (Q12 differential shading
  correction) when present after replacing 0xc003 with per-module gains.

Add KUnit tests covering IEEE-754 coordinate conversion, 8-bit legacy and
10-bit H17 resampling, green averaging, DSC multiplication, and invalid
grid rejection.
@RyanTheTide
RyanTheTide requested review from a team as code owners December 2, 2026 08:44
@github-actions github-actions Bot added area:media Area from CODEOWNERS (bot) ci-failing A check failed at the PR head (bot) door:two-way Revertible or not, from Merge risk (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Dec 2, 2026
@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Merge readiness at 6c28a3d7: not ready yet

Check Detail
CI at head ok 3 checks passed
Review ok approved by satchlj
Requested changes ok none
Hardware row at head ok in the PR body at 6c28a3d7
Conflicts ok none
PR text ok Risk: two-way · Macs with Apple ISP front cameras (J413, J615, J713, J616s, J700, K114s)
Hold no labelled hold by a maintainer

Areas: area:media

Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels.

@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Additional live identical-scene A/B on J413 M2 Air (T8112, Legacy ABI, 7.3.0-aurora-fleet94, IMX558, platform 0x0e):

  • 0x0507 + 0x0511 -> ch 0: per-module LSC 15x15 (8-bit), 0x0a4e accepted (ideal percentage 84), setfile: applied per-module LSC (15x15, ideal 84%, flip 1), 40/40 1920x1080 NV12 frames, 0 new errors, stock module restored.
  • Identical-scene A/B (stock 1921_01XX -> 1922_02XX + per-module LSC): neutral_dev 3.69 -> 1.55 (-58.0%), mean_dCb 1.76 -> 0.69 (-60.8%), mean_dCr 2.60 -> 1.12 (-56.9%), corner luma asymmetry 0.3040 -> 0.1875 (-38.3%).

@RyanTheTide

RyanTheTide commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor Author

6th and final lab Mac verified — live identical-scene A/B on MacBook Neo (J700) (T8140, H17 ABI, 7.3.0-aurora-fleet94, IMX558, platform 0x70, apple/isp_1925_03XX.dat + per-module LSC + 0xc021 DSC multiplier):

  • 0x0507 + 0x0511 -> ch 0: per-module LSC 15x15 (8-bit), 0x0a4e accepted (ideal percentage 84), setfile: applied per-module LSC (15x15, ideal 84%, flip 0), loaded apple/isp_1925_03XX.dat (0xcbc0 bytes), 40/40 1920x1080 NV12 frames, 0 firmware errors, stock module restored, Enforcing, 0 failed units.
  • Identical-scene A/B (same 1925_03XX setfile, before vs after per-module LSC + 0xc021 DSC): corner B/G chroma shading mean_dBG 0.0985 -> 0.0334 (-66.1%), max_dBG 0.1604 -> 0.0822 (-48.8%), max_dRG 0.1493 -> 0.0962 (-35.6%), mean_dCb 2.21 -> 1.29 (-41.6%), neutral_dev 2.54 -> 2.14 (-15.7%).

Address review findings on malformed setfile or NVM inputs:
- Require ideal_pct >= 10 so the cos^4 denominator stays well below 2^32.
- Validate channel grid strides (1..1024) and pixel extent (<= 16384)
  before resampling so radius and bilinear arithmetic cannot overflow.
- Require minimum knot spacing (LSC_ONE >> 10), mask 9..16-bit NVM
  samples to bit_depth bits, and clamp vertical cubic intermediates so
  non-uniform cubic interpolation cannot overflow s64.
- Populate the 10-bit KUnit fixture with cpu_to_le16() for endianness
  safety.
@github-actions github-actions Bot removed the ci-failing A check failed at the PR head (bot) label Dec 2, 2026

@satchlj satchlj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: MERGE at 6c28a3d7b279 (after #3901 merges and this PR retargets main)

Checked: Audited isp-cam.c and isp-cam-test.c for fixed-point overflow, setfile bounds validation, and interpolation math. isp_f32_to_q24() rejects negative/NaN/Inf/> 1.0f values and rounds without u32 overflow. isp_lsc_cubic() stays well within s64 bounds (|c3 * s| <= 2^61) given the LSC_ONE >> 10 minimum knot spacing and [0, 2 * LSC_ONE] vertical pass clamp; interior bicubic and border bilinear index ranges are strictly in bounds with x1 > x0 / y1 > y0 guards. isp_setfile_apply_lsc() validates all four channel records (sx,sy in 1..1024, gw,gh >= 2, table size within lsc_size, pixel extent <= 16384) before mutating the setfile, keeps r2 * k_q30 and denom * denom within u64 for ideal_pct in 10..100, averages Gr/Gb and updates channel maxima on legacy ABIs, and sets section 0x0a2a when flag_size >= 5. isp_setfile_apply_dsc() checks 0xc003/0xc021 count and sizes, avoids u32 overflow in DIV_ROUND_CLOSEST(a * b, SNF_ONE) even at (U16_MAX, U16_MAX), and recomputes all four channel maxima. pr-readiness CI is green on the stacked base, with exact-head J713 M4 Air verification at 6c28a3d7b279, 6-Mac identical-scene A/B (J413, J615, J713, J616s, J700, K114s), and ARM64 KUnit 55/55 PASS.

Evidence
  • Checked fixed-point bounds: worst-case ideal_pct = 10 gives k_q30 < 2^32, r2 <= 2^31 -> r2 * k_q30 < 2^63; denom < 2^32 -> denom * denom < 2^64.
  • Checked isp_setfile_apply_dsc(): 65535 * 65535 + 2048 = 0xfffe0801 <= 0xffffffff (no u32 wrap).
  • Stacked on #3901 (camera-all/per-module-lsc-nvm): merge #3901 first, then retarget main so full apple-build/host-tests/KUnit CI runs on the final merge head.

@github-actions github-actions Bot added ready-to-merge Meets the merge policy: merge it (bot) and removed needs-review No approving review from Core or Omarchy Mac yet (bot) labels Dec 2, 2026
@DjDeveloperr

Copy link
Copy Markdown
Contributor

Local CI run (hosted CI doesn't run for this base, and is queued on #3901): host-tests fail at 6c28a3d7b279, from the #3901 commits this stack carries. The base branch isn't main, so I merged the head into current main 105e3fbc6adc (merge tree 18ded1cda90e). I also tested the whole camera set, main + #3900 + this PR: merge tree 95452875b533, with the #3900 conflict resolved by keeping both sides.

What fails: the same 5 host-test jobs as on #3901: tools/asahi/isp-tests.py --suite session and its send, overwrite, stop and reset controls. The test still extracts only isp_cmd_send_chan and isp_cmd_send, but #3901 moved that body, ipc_error guard included, into isp_cmd_send_buf():

FAIL isp-tests --suite session (0.1s): exit 1
FAIL isp-tests --suite session --control overwrite (0.1s): exit 1 without a control failure
out/session.c:453:9: error: call to undeclared function 'isp_cmd_send_buf'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
RuntimeError: overwrite negative control no longer matches isp_cmd_send_chan

The fix belongs in #3901 (or here, if the two land together). Nothing else failed.

Check Command main + this PR main + #3900 + this PR
apple-build (Apple media dir) arm64 defconfig + merge_config.sh -m asahi.config + olddefconfig, LLVM=1, Rust; make W=1 modules_prepare drivers/media/platform/apple/ pass, no new warnings (the one W=1 warning in isp/ is also on main: isp-drv.c:1343 unused apple_isp_hw_t6040) pass, same
KUnit plan scripts/ci/apple-kunit.py: isp/ maps to the isp suite isp isp
KUnit isp, arm64 build kunit.py build --arch=arm64 --make_options LLVM=1 --kunitconfig=drivers/media/platform/apple/isp builds, 0 warnings builds, 0 warnings
KUnit isp, run the same suite on UML, because our build hosts don't use QEMU (kunit.py run --make_options LLVM=1 --kconfig_add CONFIG_WERROR=n) 59/61, permodule_lsc_math and permodule_lsc_wire pass 60/62, setfile_platform_select, permodule_lsc_math and permodule_lsc_wire pass
host-tests run-host-tests.py --jobs 8 in a container with the workflow's pins (Ubuntu 24.04, clang 18, gcc 13, rust 1.96.0, Python 3.14), --network=none --only isp: 26/31, the 5 above full suite: 947/952, only the 5 above
checkpatch scripts/checkpatch.pl --strict on 57c9b23d88d1 and 6c28a3d7b279 (the merge commit skipped) 0 warnings, 0 checks. 1 error on each commit: Missing Signed-off-by: line(s)

The 2 KUnit failures on UML in every tree are isp_test_dapf_program and isp_test_dapf_program_t6050. They fail the same way on main under UML (57/59), because UML drops MMIO writes to the test's kmalloc'd register bank, and none of these PRs touch them.

On the #3900 conflict, the combined-tree resolution keeps #3900's enum isp_sensor_id id = fmt->id; / &isp_setfiles[id] and adds this stack's lsc_params line. The tests keep both new functions and all three KUNIT_CASEs. That combined tree builds and passes everything above except the 5 jobs.

@DjDeveloperr DjDeveloperr added the hold Held on purpose; skipped by the PR digest label Dec 2, 2026
@github-actions github-actions Bot removed the ready-to-merge Meets the merge policy: merge it (bot) label Dec 2, 2026
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Replaced by #3930 (same work, cleaned up: Signed-off-by on every commit, host-test fix, values limited to what our own measurements support). Closing this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:media Area from CODEOWNERS (bot) door:two-way Revertible or not, from Merge risk (bot) hold Held on purpose; skipped by the PR digest

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants