Skip to content

Autofocus improvements #270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

njhollinghurst
Copy link
Collaborator

The goal is to overhaul the RPI AF algorithm to:

  • Treat low-confidence PDAF values more linearly, which may improve reliability and reduce wobble a little.
  • Check for IR lighting and suppress PDAF when it's detected (only when a new flag is set in the tuning file).
  • Allow CDAF-based scan to be re-triggered multiple times in Continuous AF mode (when there's no PDAF).
  • Allow subsequent scans to start from the current lens position, rather than always from 0 (infinity).
  • Skip the "fine" search altogether (when the tuning file has "step_fine": 0).
  • Read the default lens position from the tuning file, rather than hard-coding it to 1.0 in source code as now.
  • Not send the default position to the lens driver, when it would be immediately overridden by a user-specified manual setting.

without significant regression or making the code too messy.

@pinefeat
Copy link

pinefeat commented Jun 2, 2025

It works great!
I haven’t tried the PDAF-related changes yet, but Continuous AF mode works very well even with CDAF-based scanning on the HQ camera. My setup uses an external motorised lens. If the default lens position - read from the tuning file - is close enough to the actual focus point, the scan doesn't deviate too far from it and quickly determines the sharpest focus.

This fixes two small bugs:

We previously populated LensPosition's ControlInfo with hard-coded
values, ignoring the tuning file. Now we query the AfAlgorithm to
get limits (over all AF ranges) and default (for AfRangeNormal).

We previosuly sent a default position to the lens driver, even when
a user-specified starting position would follow. Defer doing this,
to reduce unnecessary lens movement at startup (for some drivers).

Signed-off-by: Nick Hollinghurst <[email protected]>
@njhollinghurst njhollinghurst force-pushed the af_202506 branch 4 times, most recently from 9cc9f13 to 5a7fceb Compare June 4, 2025 16:10
In getPhase(), stop using different weights for sumWc and sumWcp.
This should improve linearity e.g. in earlyTerminationByPhase().

Use AWB statistics, and optionally try to detect IR lighting and
suppress PDAF in that case, as IR interferes with PDAF on IMX708.

Make earlyTerminationByPhase() harder to trigger; extrapolate less.

Improve quadratic peak fitting in findPeak(). The old approximation
was good but only really valid when points were equally spaced and
the MAX was not at one end of the series.

For PDAF-based CAF, suppress lens movement unless "phase" has had
the same sign for 4 frames in a row. This suppresses noise/wobble.

Change the logic for falling back from PDAF to CDAF, and for re-
triggering a CDAF-based scan. It is now triggered by image change
(based on both Focus and AWB statistics) followed by stability.

Modify CDAF scan pattern so it doesn't always start at the lowest
setting, but can sometimes start from the current lens position.
(Though in the worst case this requires two coarse scans, if the
first one went in the wrong direction.)

Shorten fine scan and allow it to be skipped when step_fine == 0

Signed-off-by: Nick Hollinghurst <[email protected]>
Explicitly add new parameters: "retrigger_ratio", "retrigger_delay",
"check_for_ir". Tweak other parameters to suit algorithm changes.
(Though existing tuning files should still work acceptably.)

Add AfSpeedFast parameters for the Raspberry Pi V3 standard lens.

Signed-off-by: Nick Hollinghurst <[email protected]>
@njhollinghurst njhollinghurst marked this pull request as ready for review June 4, 2025 16:15
@njhollinghurst njhollinghurst requested a review from naushir June 4, 2025 16:15
@njhollinghurst njhollinghurst changed the title Autofocus improvements (WIP) Autofocus improvements Jun 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants