[libcamera-devel,v3,1/2] libcamera: controls: Add controls for AEC/AGC flicker avoidance
diff mbox series

Message ID 20230328085521.7409-2-david.plowman@raspberrypi.com
State Accepted
Headers show
Series
  • Add flicker avoidance controls
Related show

Commit Message

David Plowman March 28, 2023, 8:55 a.m. UTC
Flicker is the term used to describe brightness banding or oscillation
of images caused typically by artificial lighting driven by a 50 or
60Hz mains supply. We add three controls intended to be used by
AEC/AGC algorithms:

AeFlickerMode to enable flicker avoidance.

AeFlickerCustom to set custom flicker periods.

AeFlickerDetected to report any flicker that is currently detected.

Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
---
 src/libcamera/control_ids.yaml | 84 +++++++++++++++++++++++++++-------
 1 file changed, 67 insertions(+), 17 deletions(-)

Comments

Naushir Patuck April 3, 2023, 7:57 a.m. UTC | #1
Hi David,

Thanks for this, it looks good to me.  Just one question below:

On Tue, 28 Mar 2023 at 09:55, David Plowman via libcamera-devel
<libcamera-devel@lists.libcamera.org> wrote:
>
> Flicker is the term used to describe brightness banding or oscillation
> of images caused typically by artificial lighting driven by a 50 or
> 60Hz mains supply. We add three controls intended to be used by
> AEC/AGC algorithms:
>
> AeFlickerMode to enable flicker avoidance.
>
> AeFlickerCustom to set custom flicker periods.
>
> AeFlickerDetected to report any flicker that is currently detected.
>
> Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
> ---
>  src/libcamera/control_ids.yaml | 84 +++++++++++++++++++++++++++-------
>  1 file changed, 67 insertions(+), 17 deletions(-)
>
> diff --git a/src/libcamera/control_ids.yaml b/src/libcamera/control_ids.yaml
> index adea5f90..b472050c 100644
> --- a/src/libcamera/control_ids.yaml
> +++ b/src/libcamera/control_ids.yaml
> @@ -156,6 +156,73 @@ controls:
>          control of which features should be automatically adjusted shouldn't
>          better be handled through a separate AE mode control.
>
> +  - AeFlickerMode:
> +      type: int32_t
> +      description: |
> +        Set the flicker mode, which determines whether, and how, the AGC/AEC
> +        algorithm attempts to hide flicker effects caused by the duty cycle of
> +        artificial lighting.
> +
> +        Although implementation dependent, many algorithms for "flicker
> +        avoidance" work by restricting this exposure time to integer multiples
> +        of the cycle period, wherever possible.
> +
> +        Implementations may not support all of the flicker modes listed below.
> +
> +      enum:
> +        - name: FlickerOff
> +          value: 0
> +          description: No flicker avoidance is performed.
> +        - name: FlickerFreq50Hz
> +          value: 1
> +          description: 50Hz flicker avoidance.
> +            Suppress flicker effects caused by lighting running with a 100Hz
> +            period (such as that produced by 50Hz mains electricity).
> +        - name: FlickerFreq60Hz
> +          value: 2
> +          description: 60Hz flicker avoidance.
> +            Suppress flicker effects caused by lighting running with a 120Hz
> +            period (such as that produced by 60Hz mains electricity).
> +        - name: FlickerCustom
> +          value: 3
> +          description: Custom flicker avoidance.
> +            Suppress flicker effects caused by lighting running with a period
> +            specified by the AeFlickerCustom control.
> +            \sa AeFlickerCustom
> +        - name: FlickerAuto
> +          value: 4
> +          description: Automatic flicker period detection and avoidance.
> +            The system will automatically determine the most likely value of
> +            flicker period, and avoid flicker of this frequency.
> +
> +  - AeFlickerCustom:
> +      type: int32_t
> +      description: Custom flicker period in microseconds.
> +        This value sets the current flicker period to avoid. It is used when
> +        AeFlickerMode is set to FlickerCustom.
> +
> +        If this control is not available, then the setting of custom flicker
> +        periods is not supported.
> +
> +        \sa AeFlickerMode
> +
> +  - AeFlickerDetected:
> +      type: int32_t
> +      description: Flicker period detected in microseconds.
> +        The value reported here indicates the currently detected flicker
> +        period, or zero if no flicker at all is detected.
> +
> +        So in the case of 50Hz mains flicker, the value would be 10000
> +        (corresponding to 100Hz), or 8333 (120Hz) for 60Hz mains flicker. But
> +        note that it is not required that any form of scene flicker is detected
> +        at all, so depending on the implementation, this metadata may just
> +        report zero or be entirely absent.
> +
> +        AeFlickerDetected may also report other values for non-standard flicker
> +        periods.
> +
> +        \sa AeFlickerMode

To clarify, AeFlickerDetected ought to be returned unconditionally on every
frame, even if AeFlickerMode is set to something other than FlickerAuto, is that
correct?

Other than that:

Reviewed-by: Naushir Patuck <naush@raspberrypi.com>

> +
>    - Brightness:
>        type: float
>        description: |
> @@ -843,23 +910,6 @@ controls:
>            value: 1
>            description: The lens shading map mode is available.
>
> -  - SceneFlicker:
> -      type: int32_t
> -      draft: true
> -      description: |
> -       Control to report the detected scene light frequency. Currently
> -       identical to ANDROID_STATISTICS_SCENE_FLICKER.
> -      enum:
> -        - name: SceneFickerOff
> -          value: 0
> -          description: No flickering detected.
> -        - name: SceneFicker50Hz
> -          value: 1
> -          description: 50Hz flickering detected.
> -        - name: SceneFicker60Hz
> -          value: 2
> -          description: 60Hz flickering detected.
> -
>    - PipelineDepth:
>        type: int32_t
>        draft: true
> --
> 2.30.2
>
David Plowman April 3, 2023, 8:20 a.m. UTC | #2
Hi Naush

On Mon, 3 Apr 2023 at 08:58, Naushir Patuck <naush@raspberrypi.com> wrote:
>
> Hi David,
>
> Thanks for this, it looks good to me.  Just one question below:
>
> On Tue, 28 Mar 2023 at 09:55, David Plowman via libcamera-devel
> <libcamera-devel@lists.libcamera.org> wrote:
> >
> > Flicker is the term used to describe brightness banding or oscillation
> > of images caused typically by artificial lighting driven by a 50 or
> > 60Hz mains supply. We add three controls intended to be used by
> > AEC/AGC algorithms:
> >
> > AeFlickerMode to enable flicker avoidance.
> >
> > AeFlickerCustom to set custom flicker periods.
> >
> > AeFlickerDetected to report any flicker that is currently detected.
> >
> > Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
> > ---
> >  src/libcamera/control_ids.yaml | 84 +++++++++++++++++++++++++++-------
> >  1 file changed, 67 insertions(+), 17 deletions(-)
> >
> > diff --git a/src/libcamera/control_ids.yaml b/src/libcamera/control_ids.yaml
> > index adea5f90..b472050c 100644
> > --- a/src/libcamera/control_ids.yaml
> > +++ b/src/libcamera/control_ids.yaml
> > @@ -156,6 +156,73 @@ controls:
> >          control of which features should be automatically adjusted shouldn't
> >          better be handled through a separate AE mode control.
> >
> > +  - AeFlickerMode:
> > +      type: int32_t
> > +      description: |
> > +        Set the flicker mode, which determines whether, and how, the AGC/AEC
> > +        algorithm attempts to hide flicker effects caused by the duty cycle of
> > +        artificial lighting.
> > +
> > +        Although implementation dependent, many algorithms for "flicker
> > +        avoidance" work by restricting this exposure time to integer multiples
> > +        of the cycle period, wherever possible.
> > +
> > +        Implementations may not support all of the flicker modes listed below.
> > +
> > +      enum:
> > +        - name: FlickerOff
> > +          value: 0
> > +          description: No flicker avoidance is performed.
> > +        - name: FlickerFreq50Hz
> > +          value: 1
> > +          description: 50Hz flicker avoidance.
> > +            Suppress flicker effects caused by lighting running with a 100Hz
> > +            period (such as that produced by 50Hz mains electricity).
> > +        - name: FlickerFreq60Hz
> > +          value: 2
> > +          description: 60Hz flicker avoidance.
> > +            Suppress flicker effects caused by lighting running with a 120Hz
> > +            period (such as that produced by 60Hz mains electricity).
> > +        - name: FlickerCustom
> > +          value: 3
> > +          description: Custom flicker avoidance.
> > +            Suppress flicker effects caused by lighting running with a period
> > +            specified by the AeFlickerCustom control.
> > +            \sa AeFlickerCustom
> > +        - name: FlickerAuto
> > +          value: 4
> > +          description: Automatic flicker period detection and avoidance.
> > +            The system will automatically determine the most likely value of
> > +            flicker period, and avoid flicker of this frequency.
> > +
> > +  - AeFlickerCustom:
> > +      type: int32_t
> > +      description: Custom flicker period in microseconds.
> > +        This value sets the current flicker period to avoid. It is used when
> > +        AeFlickerMode is set to FlickerCustom.
> > +
> > +        If this control is not available, then the setting of custom flicker
> > +        periods is not supported.
> > +
> > +        \sa AeFlickerMode
> > +
> > +  - AeFlickerDetected:
> > +      type: int32_t
> > +      description: Flicker period detected in microseconds.
> > +        The value reported here indicates the currently detected flicker
> > +        period, or zero if no flicker at all is detected.
> > +
> > +        So in the case of 50Hz mains flicker, the value would be 10000
> > +        (corresponding to 100Hz), or 8333 (120Hz) for 60Hz mains flicker. But
> > +        note that it is not required that any form of scene flicker is detected
> > +        at all, so depending on the implementation, this metadata may just
> > +        report zero or be entirely absent.
> > +
> > +        AeFlickerDetected may also report other values for non-standard flicker
> > +        periods.
> > +
> > +        \sa AeFlickerMode
>
> To clarify, AeFlickerDetected ought to be returned unconditionally on every
> frame, even if AeFlickerMode is set to something other than FlickerAuto, is that
> correct?

Actually I think that's a good question. A lot of this stuff _could_
be quite implementation dependent, I think, and we can't really
mandate that implementations will do auto-detection in all
circumstances (some may well not do it al all).

For example, if we're actually doing flicker avoidance at 50Hz, you
might expect that it gets tricky to detect 50Hz flicker. So what do we
report? Maybe we should require that the metadata is absent (or invent
a "can't tell" value) in these circumstances. We should probably be
clearer on this, so maybe another version is required

David.

>
> Other than that:
>
> Reviewed-by: Naushir Patuck <naush@raspberrypi.com>
>
> > +
> >    - Brightness:
> >        type: float
> >        description: |
> > @@ -843,23 +910,6 @@ controls:
> >            value: 1
> >            description: The lens shading map mode is available.
> >
> > -  - SceneFlicker:
> > -      type: int32_t
> > -      draft: true
> > -      description: |
> > -       Control to report the detected scene light frequency. Currently
> > -       identical to ANDROID_STATISTICS_SCENE_FLICKER.
> > -      enum:
> > -        - name: SceneFickerOff
> > -          value: 0
> > -          description: No flickering detected.
> > -        - name: SceneFicker50Hz
> > -          value: 1
> > -          description: 50Hz flickering detected.
> > -        - name: SceneFicker60Hz
> > -          value: 2
> > -          description: 60Hz flickering detected.
> > -
> >    - PipelineDepth:
> >        type: int32_t
> >        draft: true
> > --
> > 2.30.2
> >
Jacopo Mondi April 3, 2023, 2:30 p.m. UTC | #3
Hi David

On Tue, Mar 28, 2023 at 09:55:20AM +0100, David Plowman via libcamera-devel wrote:
> Flicker is the term used to describe brightness banding or oscillation
> of images caused typically by artificial lighting driven by a 50 or
> 60Hz mains supply. We add three controls intended to be used by
> AEC/AGC algorithms:
>
> AeFlickerMode to enable flicker avoidance.
>
> AeFlickerCustom to set custom flicker periods.
>
> AeFlickerDetected to report any flicker that is currently detected.
>
> Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
> ---
>  src/libcamera/control_ids.yaml | 84 +++++++++++++++++++++++++++-------
>  1 file changed, 67 insertions(+), 17 deletions(-)
>
> diff --git a/src/libcamera/control_ids.yaml b/src/libcamera/control_ids.yaml
> index adea5f90..b472050c 100644
> --- a/src/libcamera/control_ids.yaml
> +++ b/src/libcamera/control_ids.yaml
> @@ -156,6 +156,73 @@ controls:
>          control of which features should be automatically adjusted shouldn't
>          better be handled through a separate AE mode control.
>
> +  - AeFlickerMode:
> +      type: int32_t
> +      description: |
> +        Set the flicker mode, which determines whether, and how, the AGC/AEC
> +        algorithm attempts to hide flicker effects caused by the duty cycle of
> +        artificial lighting.
> +
> +        Although implementation dependent, many algorithms for "flicker
> +        avoidance" work by restricting this exposure time to integer multiples
> +        of the cycle period, wherever possible.
> +
> +        Implementations may not support all of the flicker modes listed below.

           If the automatic control of exposure time is disabled, this
           control has no effect.
> +
> +      enum:
> +        - name: FlickerOff
> +          value: 0
> +          description: No flicker avoidance is performed.
> +        - name: FlickerFreq50Hz
> +          value: 1
> +          description: 50Hz flicker avoidance.
> +            Suppress flicker effects caused by lighting running with a 100Hz
> +            period (such as that produced by 50Hz mains electricity).
> +        - name: FlickerFreq60Hz
> +          value: 2
> +          description: 60Hz flicker avoidance.
> +            Suppress flicker effects caused by lighting running with a 120Hz
> +            period (such as that produced by 60Hz mains electricity).
> +        - name: FlickerCustom
> +          value: 3
> +          description: Custom flicker avoidance.
> +            Suppress flicker effects caused by lighting running with a period
> +            specified by the AeFlickerCustom control.
> +            \sa AeFlickerCustom
> +        - name: FlickerAuto
> +          value: 4
> +          description: Automatic flicker period detection and avoidance.
> +            The system will automatically determine the most likely value of
> +            flicker period, and avoid flicker of this frequency.
> +
> +  - AeFlickerCustom:
> +      type: int32_t
> +      description: Custom flicker period in microseconds.

microseconds or Hz ?

> +        This value sets the current flicker period to avoid. It is used when
> +        AeFlickerMode is set to FlickerCustom.
> +
> +        If this control is not available, then the setting of custom flicker
> +        periods is not supported.

I presume pipeline handlers/IPA should not register FlickerCustom
among the supported AeFlickerMode instead.

> +
> +        \sa AeFlickerMode
> +
> +  - AeFlickerDetected:

Is this "Ae" ?
Or should it be just (Scene)FlickerDetected ?

> +      type: int32_t
> +      description: Flicker period detected in microseconds.
> +        The value reported here indicates the currently detected flicker
> +        period, or zero if no flicker at all is detected.
> +
> +        So in the case of 50Hz mains flicker, the value would be 10000
> +        (corresponding to 100Hz), or 8333 (120Hz) for 60Hz mains flicker. But
> +        note that it is not required that any form of scene flicker is detected
> +        at all, so depending on the implementation, this metadata may just
> +        report zero or be entirely absent.

I would be more strict and require platforms that do not support
flicker detection not to report this control at all, instead of
reporting 0

> +
> +        AeFlickerDetected may also report other values for non-standard flicker
> +        periods.
> +
> +        \sa AeFlickerMode
> +
>    - Brightness:
>        type: float
>        description: |
> @@ -843,23 +910,6 @@ controls:
>            value: 1
>            description: The lens shading map mode is available.
>
> -  - SceneFlicker:
> -      type: int32_t
> -      draft: true
> -      description: |
> -       Control to report the detected scene light frequency. Currently
> -       identical to ANDROID_STATISTICS_SCENE_FLICKER.
> -      enum:
> -        - name: SceneFickerOff
> -          value: 0
> -          description: No flickering detected.
> -        - name: SceneFicker50Hz
> -          value: 1
> -          description: 50Hz flickering detected.
> -        - name: SceneFicker60Hz
> -          value: 2
> -          description: 60Hz flickering detected.
> -
>    - PipelineDepth:
>        type: int32_t
>        draft: true
> --
> 2.30.2
>
Nicolas Dufresne April 3, 2023, 7:34 p.m. UTC | #4
Le mardi 28 mars 2023 à 09:55 +0100, David Plowman via libcamera-devel a écrit :
> Flicker is the term used to describe brightness banding or oscillation
> of images caused typically by artificial lighting driven by a 50 or
> 60Hz mains supply. We add three controls intended to be used by
> AEC/AGC algorithms:
> 
> AeFlickerMode to enable flicker avoidance.
> 
> AeFlickerCustom to set custom flicker periods.
> 
> AeFlickerDetected to report any flicker that is currently detected.
> 
> Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
> ---
>  src/libcamera/control_ids.yaml | 84 +++++++++++++++++++++++++++-------
>  1 file changed, 67 insertions(+), 17 deletions(-)
> 
> diff --git a/src/libcamera/control_ids.yaml b/src/libcamera/control_ids.yaml
> index adea5f90..b472050c 100644
> --- a/src/libcamera/control_ids.yaml
> +++ b/src/libcamera/control_ids.yaml
> @@ -156,6 +156,73 @@ controls:
>          control of which features should be automatically adjusted shouldn't
>          better be handled through a separate AE mode control.
>  
> +  - AeFlickerMode:

Maybe loaded question, since it goes beyound the scope of the patch, but how are
we suppose to know which controls is an actual control, and which one are
metadata ? (read-only, possibly on sync with request ?)

> +      type: int32_t
> +      description: |
> +        Set the flicker mode, which determines whether, and how, the AGC/AEC
> +        algorithm attempts to hide flicker effects caused by the duty cycle of
> +        artificial lighting.
> +
> +        Although implementation dependent, many algorithms for "flicker
> +        avoidance" work by restricting this exposure time to integer multiples
> +        of the cycle period, wherever possible.
> +
> +        Implementations may not support all of the flicker modes listed below.
> +
> +      enum:
> +        - name: FlickerOff
> +          value: 0
> +          description: No flicker avoidance is performed.
> +        - name: FlickerFreq50Hz
> +          value: 1
> +          description: 50Hz flicker avoidance.
> +            Suppress flicker effects caused by lighting running with a 100Hz
> +            period (such as that produced by 50Hz mains electricity).
> +        - name: FlickerFreq60Hz
> +          value: 2
> +          description: 60Hz flicker avoidance.
> +            Suppress flicker effects caused by lighting running with a 120Hz
> +            period (such as that produced by 60Hz mains electricity).
> +        - name: FlickerCustom
> +          value: 3
> +          description: Custom flicker avoidance.
> +            Suppress flicker effects caused by lighting running with a period
> +            specified by the AeFlickerCustom control.
> +            \sa AeFlickerCustom
> +        - name: FlickerAuto
> +          value: 4
> +          description: Automatic flicker period detection and avoidance.
> +            The system will automatically determine the most likely value of
> +            flicker period, and avoid flicker of this frequency.
> +
> +  - AeFlickerCustom:
> +      type: int32_t
> +      description: Custom flicker period in microseconds.
> +        This value sets the current flicker period to avoid. It is used when
> +        AeFlickerMode is set to FlickerCustom.
> +
> +        If this control is not available, then the setting of custom flicker
> +        periods is not supported.
> +
> +        \sa AeFlickerMode
> +
> +  - AeFlickerDetected:
> +      type: int32_t
> +      description: Flicker period detected in microseconds.
> +        The value reported here indicates the currently detected flicker
> +        period, or zero if no flicker at all is detected.
> +
> +        So in the case of 50Hz mains flicker, the value would be 10000
> +        (corresponding to 100Hz), or 8333 (120Hz) for 60Hz mains flicker. But
> +        note that it is not required that any form of scene flicker is detected
> +        at all, so depending on the implementation, this metadata may just
> +        report zero or be entirely absent.
> +
> +        AeFlickerDetected may also report other values for non-standard flicker
> +        periods.
> +
> +        \sa AeFlickerMode
> +
>    - Brightness:
>        type: float
>        description: |
> @@ -843,23 +910,6 @@ controls:
>            value: 1
>            description: The lens shading map mode is available.
>  
> -  - SceneFlicker:
> -      type: int32_t
> -      draft: true
> -      description: |
> -       Control to report the detected scene light frequency. Currently
> -       identical to ANDROID_STATISTICS_SCENE_FLICKER.
> -      enum:
> -        - name: SceneFickerOff
> -          value: 0
> -          description: No flickering detected.
> -        - name: SceneFicker50Hz
> -          value: 1
> -          description: 50Hz flickering detected.
> -        - name: SceneFicker60Hz
> -          value: 2
> -          description: 60Hz flickering detected.
> -
>    - PipelineDepth:
>        type: int32_t
>        draft: true
Kieran Bingham April 3, 2023, 8:34 p.m. UTC | #5
Quoting Nicolas Dufresne via libcamera-devel (2023-04-03 20:34:26)
> Le mardi 28 mars 2023 à 09:55 +0100, David Plowman via libcamera-devel a écrit :
> > Flicker is the term used to describe brightness banding or oscillation
> > of images caused typically by artificial lighting driven by a 50 or
> > 60Hz mains supply. We add three controls intended to be used by
> > AEC/AGC algorithms:
> > 
> > AeFlickerMode to enable flicker avoidance.
> > 
> > AeFlickerCustom to set custom flicker periods.
> > 
> > AeFlickerDetected to report any flicker that is currently detected.
> > 
> > Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
> > ---
> >  src/libcamera/control_ids.yaml | 84 +++++++++++++++++++++++++++-------
> >  1 file changed, 67 insertions(+), 17 deletions(-)
> > 
> > diff --git a/src/libcamera/control_ids.yaml b/src/libcamera/control_ids.yaml
> > index adea5f90..b472050c 100644
> > --- a/src/libcamera/control_ids.yaml
> > +++ b/src/libcamera/control_ids.yaml
> > @@ -156,6 +156,73 @@ controls:
> >          control of which features should be automatically adjusted shouldn't
> >          better be handled through a separate AE mode control.
> >  
> > +  - AeFlickerMode:
> 
> Maybe loaded question, since it goes beyound the scope of the patch, but how are
> we suppose to know which controls is an actual control, and which one are
> metadata ? (read-only, possibly on sync with request ?)

a camera exposes a ControlList as camera->controls()
Those (and only those) are the valid controls for that camera.

When a request completes, it can share data in request->metadata() which
is a ControlList of ControlValues that can be interpreted by the
application to see what was, or any control value that the pipeline
handler chooses to put in there really. I think we did look at having
separate 'Metadata' control lists - but it seemed like that would be a
lot of duplication of keys...

Unless you're asking how can we know 'from the documentation'. .. in
which case - currently it's not specifically defined, but we could add a
property to the yaml file?

--
Kieran


> 
> > +      type: int32_t
> > +      description: |
> > +        Set the flicker mode, which determines whether, and how, the AGC/AEC
> > +        algorithm attempts to hide flicker effects caused by the duty cycle of
> > +        artificial lighting.
> > +
> > +        Although implementation dependent, many algorithms for "flicker
> > +        avoidance" work by restricting this exposure time to integer multiples
> > +        of the cycle period, wherever possible.
> > +
> > +        Implementations may not support all of the flicker modes listed below.
> > +
> > +      enum:
> > +        - name: FlickerOff
> > +          value: 0
> > +          description: No flicker avoidance is performed.
> > +        - name: FlickerFreq50Hz
> > +          value: 1
> > +          description: 50Hz flicker avoidance.
> > +            Suppress flicker effects caused by lighting running with a 100Hz
> > +            period (such as that produced by 50Hz mains electricity).
> > +        - name: FlickerFreq60Hz
> > +          value: 2
> > +          description: 60Hz flicker avoidance.
> > +            Suppress flicker effects caused by lighting running with a 120Hz
> > +            period (such as that produced by 60Hz mains electricity).
> > +        - name: FlickerCustom
> > +          value: 3
> > +          description: Custom flicker avoidance.
> > +            Suppress flicker effects caused by lighting running with a period
> > +            specified by the AeFlickerCustom control.
> > +            \sa AeFlickerCustom
> > +        - name: FlickerAuto
> > +          value: 4
> > +          description: Automatic flicker period detection and avoidance.
> > +            The system will automatically determine the most likely value of
> > +            flicker period, and avoid flicker of this frequency.
> > +
> > +  - AeFlickerCustom:
> > +      type: int32_t
> > +      description: Custom flicker period in microseconds.
> > +        This value sets the current flicker period to avoid. It is used when
> > +        AeFlickerMode is set to FlickerCustom.
> > +
> > +        If this control is not available, then the setting of custom flicker
> > +        periods is not supported.
> > +
> > +        \sa AeFlickerMode
> > +
> > +  - AeFlickerDetected:
> > +      type: int32_t
> > +      description: Flicker period detected in microseconds.
> > +        The value reported here indicates the currently detected flicker
> > +        period, or zero if no flicker at all is detected.
> > +
> > +        So in the case of 50Hz mains flicker, the value would be 10000
> > +        (corresponding to 100Hz), or 8333 (120Hz) for 60Hz mains flicker. But
> > +        note that it is not required that any form of scene flicker is detected
> > +        at all, so depending on the implementation, this metadata may just
> > +        report zero or be entirely absent.
> > +
> > +        AeFlickerDetected may also report other values for non-standard flicker
> > +        periods.
> > +
> > +        \sa AeFlickerMode
> > +
> >    - Brightness:
> >        type: float
> >        description: |
> > @@ -843,23 +910,6 @@ controls:
> >            value: 1
> >            description: The lens shading map mode is available.
> >  
> > -  - SceneFlicker:
> > -      type: int32_t
> > -      draft: true
> > -      description: |
> > -       Control to report the detected scene light frequency. Currently
> > -       identical to ANDROID_STATISTICS_SCENE_FLICKER.
> > -      enum:
> > -        - name: SceneFickerOff
> > -          value: 0
> > -          description: No flickering detected.
> > -        - name: SceneFicker50Hz
> > -          value: 1
> > -          description: 50Hz flickering detected.
> > -        - name: SceneFicker60Hz
> > -          value: 2
> > -          description: 60Hz flickering detected.
> > -
> >    - PipelineDepth:
> >        type: int32_t
> >        draft: true
>
Laurent Pinchart April 4, 2023, 5:49 a.m. UTC | #6
On Mon, Apr 03, 2023 at 09:34:28PM +0100, Kieran Bingham via libcamera-devel wrote:
> Quoting Nicolas Dufresne via libcamera-devel (2023-04-03 20:34:26)
> > Le mardi 28 mars 2023 à 09:55 +0100, David Plowman via libcamera-devel a écrit :
> > > Flicker is the term used to describe brightness banding or oscillation
> > > of images caused typically by artificial lighting driven by a 50 or
> > > 60Hz mains supply. We add three controls intended to be used by
> > > AEC/AGC algorithms:
> > > 
> > > AeFlickerMode to enable flicker avoidance.
> > > 
> > > AeFlickerCustom to set custom flicker periods.
> > > 
> > > AeFlickerDetected to report any flicker that is currently detected.
> > > 
> > > Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
> > > ---
> > >  src/libcamera/control_ids.yaml | 84 +++++++++++++++++++++++++++-------
> > >  1 file changed, 67 insertions(+), 17 deletions(-)
> > > 
> > > diff --git a/src/libcamera/control_ids.yaml b/src/libcamera/control_ids.yaml
> > > index adea5f90..b472050c 100644
> > > --- a/src/libcamera/control_ids.yaml
> > > +++ b/src/libcamera/control_ids.yaml
> > > @@ -156,6 +156,73 @@ controls:
> > >          control of which features should be automatically adjusted shouldn't
> > >          better be handled through a separate AE mode control.
> > >  
> > > +  - AeFlickerMode:
> > 
> > Maybe loaded question, since it goes beyound the scope of the patch, but how are
> > we suppose to know which controls is an actual control, and which one are
> > metadata ? (read-only, possibly on sync with request ?)
> 
> a camera exposes a ControlList as camera->controls()
> Those (and only those) are the valid controls for that camera.
> 
> When a request completes, it can share data in request->metadata() which
> is a ControlList of ControlValues that can be interpreted by the
> application to see what was, or any control value that the pipeline
> handler chooses to put in there really. I think we did look at having
> separate 'Metadata' control lists - but it seemed like that would be a
> lot of duplication of keys...
> 
> Unless you're asking how can we know 'from the documentation'. .. in
> which case - currently it's not specifically defined, but we could add a
> property to the yaml file?

I think we should.

> > > +      type: int32_t
> > > +      description: |
> > > +        Set the flicker mode, which determines whether, and how, the AGC/AEC
> > > +        algorithm attempts to hide flicker effects caused by the duty cycle of
> > > +        artificial lighting.
> > > +
> > > +        Although implementation dependent, many algorithms for "flicker
> > > +        avoidance" work by restricting this exposure time to integer multiples
> > > +        of the cycle period, wherever possible.
> > > +
> > > +        Implementations may not support all of the flicker modes listed below.
> > > +
> > > +      enum:
> > > +        - name: FlickerOff
> > > +          value: 0
> > > +          description: No flicker avoidance is performed.
> > > +        - name: FlickerFreq50Hz
> > > +          value: 1
> > > +          description: 50Hz flicker avoidance.
> > > +            Suppress flicker effects caused by lighting running with a 100Hz
> > > +            period (such as that produced by 50Hz mains electricity).
> > > +        - name: FlickerFreq60Hz
> > > +          value: 2
> > > +          description: 60Hz flicker avoidance.
> > > +            Suppress flicker effects caused by lighting running with a 120Hz
> > > +            period (such as that produced by 60Hz mains electricity).
> > > +        - name: FlickerCustom
> > > +          value: 3
> > > +          description: Custom flicker avoidance.
> > > +            Suppress flicker effects caused by lighting running with a period
> > > +            specified by the AeFlickerCustom control.
> > > +            \sa AeFlickerCustom
> > > +        - name: FlickerAuto
> > > +          value: 4
> > > +          description: Automatic flicker period detection and avoidance.
> > > +            The system will automatically determine the most likely value of
> > > +            flicker period, and avoid flicker of this frequency.
> > > +
> > > +  - AeFlickerCustom:
> > > +      type: int32_t
> > > +      description: Custom flicker period in microseconds.
> > > +        This value sets the current flicker period to avoid. It is used when
> > > +        AeFlickerMode is set to FlickerCustom.
> > > +
> > > +        If this control is not available, then the setting of custom flicker
> > > +        periods is not supported.
> > > +
> > > +        \sa AeFlickerMode
> > > +
> > > +  - AeFlickerDetected:
> > > +      type: int32_t
> > > +      description: Flicker period detected in microseconds.
> > > +        The value reported here indicates the currently detected flicker
> > > +        period, or zero if no flicker at all is detected.
> > > +
> > > +        So in the case of 50Hz mains flicker, the value would be 10000
> > > +        (corresponding to 100Hz), or 8333 (120Hz) for 60Hz mains flicker. But
> > > +        note that it is not required that any form of scene flicker is detected
> > > +        at all, so depending on the implementation, this metadata may just
> > > +        report zero or be entirely absent.
> > > +
> > > +        AeFlickerDetected may also report other values for non-standard flicker
> > > +        periods.
> > > +
> > > +        \sa AeFlickerMode
> > > +
> > >    - Brightness:
> > >        type: float
> > >        description: |
> > > @@ -843,23 +910,6 @@ controls:
> > >            value: 1
> > >            description: The lens shading map mode is available.
> > >  
> > > -  - SceneFlicker:
> > > -      type: int32_t
> > > -      draft: true
> > > -      description: |
> > > -       Control to report the detected scene light frequency. Currently
> > > -       identical to ANDROID_STATISTICS_SCENE_FLICKER.
> > > -      enum:
> > > -        - name: SceneFickerOff
> > > -          value: 0
> > > -          description: No flickering detected.
> > > -        - name: SceneFicker50Hz
> > > -          value: 1
> > > -          description: 50Hz flickering detected.
> > > -        - name: SceneFicker60Hz
> > > -          value: 2
> > > -          description: 60Hz flickering detected.
> > > -
> > >    - PipelineDepth:
> > >        type: int32_t
> > >        draft: true
Laurent Pinchart April 4, 2023, 6:06 a.m. UTC | #7
Hi David,

Thank you for the patch.

On Mon, Apr 03, 2023 at 09:20:39AM +0100, David Plowman via libcamera-devel wrote:
> On Mon, 3 Apr 2023 at 08:58, Naushir Patuck wrote:
> > On Tue, 28 Mar 2023 at 09:55, David Plowman via libcamera-devel wrote:
> > >
> > > Flicker is the term used to describe brightness banding or oscillation
> > > of images caused typically by artificial lighting driven by a 50 or
> > > 60Hz mains supply. We add three controls intended to be used by
> > > AEC/AGC algorithms:
> > >
> > > AeFlickerMode to enable flicker avoidance.
> > >
> > > AeFlickerCustom to set custom flicker periods.
> > >
> > > AeFlickerDetected to report any flicker that is currently detected.
> > >
> > > Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
> > > ---
> > >  src/libcamera/control_ids.yaml | 84 +++++++++++++++++++++++++++-------
> > >  1 file changed, 67 insertions(+), 17 deletions(-)
> > >
> > > diff --git a/src/libcamera/control_ids.yaml b/src/libcamera/control_ids.yaml
> > > index adea5f90..b472050c 100644
> > > --- a/src/libcamera/control_ids.yaml
> > > +++ b/src/libcamera/control_ids.yaml
> > > @@ -156,6 +156,73 @@ controls:
> > >          control of which features should be automatically adjusted shouldn't
> > >          better be handled through a separate AE mode control.
> > >
> > > +  - AeFlickerMode:
> > > +      type: int32_t
> > > +      description: |
> > > +        Set the flicker mode, which determines whether, and how, the AGC/AEC
> > > +        algorithm attempts to hide flicker effects caused by the duty cycle of
> > > +        artificial lighting.
> > > +
> > > +        Although implementation dependent, many algorithms for "flicker
> > > +        avoidance" work by restricting this exposure time to integer multiples
> > > +        of the cycle period, wherever possible.
> > > +
> > > +        Implementations may not support all of the flicker modes listed below.
> > > +
> > > +      enum:
> > > +        - name: FlickerOff
> > > +          value: 0
> > > +          description: No flicker avoidance is performed.
> > > +        - name: FlickerFreq50Hz
> > > +          value: 1
> > > +          description: 50Hz flicker avoidance.
> > > +            Suppress flicker effects caused by lighting running with a 100Hz
> > > +            period (such as that produced by 50Hz mains electricity).
> > > +        - name: FlickerFreq60Hz
> > > +          value: 2
> > > +          description: 60Hz flicker avoidance.
> > > +            Suppress flicker effects caused by lighting running with a 120Hz
> > > +            period (such as that produced by 60Hz mains electricity).

Have you considered having 3 values here (off, manual and auto), and a
AeFlickerPeriod control to set the manual value ? 50Hz and 60Hz are
common values, but do they require special cases ?

> > > +        - name: FlickerCustom
> > > +          value: 3
> > > +          description: Custom flicker avoidance.
> > > +            Suppress flicker effects caused by lighting running with a period
> > > +            specified by the AeFlickerCustom control.
> > > +            \sa AeFlickerCustom
> > > +        - name: FlickerAuto
> > > +          value: 4
> > > +          description: Automatic flicker period detection and avoidance.
> > > +            The system will automatically determine the most likely value of
> > > +            flicker period, and avoid flicker of this frequency.
> > > +
> > > +  - AeFlickerCustom:
> > > +      type: int32_t
> > > +      description: Custom flicker period in microseconds.
> > > +        This value sets the current flicker period to avoid. It is used when
> > > +        AeFlickerMode is set to FlickerCustom.
> > > +
> > > +        If this control is not available, then the setting of custom flicker
> > > +        periods is not supported.
> > > +
> > > +        \sa AeFlickerMode
> > > +
> > > +  - AeFlickerDetected:
> > > +      type: int32_t
> > > +      description: Flicker period detected in microseconds.
> > > +        The value reported here indicates the currently detected flicker
> > > +        period, or zero if no flicker at all is detected.
> > > +
> > > +        So in the case of 50Hz mains flicker, the value would be 10000
> > > +        (corresponding to 100Hz), or 8333 (120Hz) for 60Hz mains flicker. But
> > > +        note that it is not required that any form of scene flicker is detected
> > > +        at all, so depending on the implementation, this metadata may just
> > > +        report zero or be entirely absent.
> > > +
> > > +        AeFlickerDetected may also report other values for non-standard flicker
> > > +        periods.
> > > +
> > > +        \sa AeFlickerMode
> >
> > To clarify, AeFlickerDetected ought to be returned unconditionally on every
> > frame, even if AeFlickerMode is set to something other than FlickerAuto, is that
> > correct?
> 
> Actually I think that's a good question. A lot of this stuff _could_
> be quite implementation dependent, I think, and we can't really
> mandate that implementations will do auto-detection in all
> circumstances (some may well not do it al all).
> 
> For example, if we're actually doing flicker avoidance at 50Hz, you
> might expect that it gets tricky to detect 50Hz flicker. So what do we
> report? Maybe we should require that the metadata is absent (or invent
> a "can't tell" value) in these circumstances. We should probably be
> clearer on this, so maybe another version is required

To answer this question I'd like to look at it from an application's
point of view. How do you envision this control being used in
applications ?

> > Other than that:
> >
> > Reviewed-by: Naushir Patuck <naush@raspberrypi.com>
> >
> > > +
> > >    - Brightness:
> > >        type: float
> > >        description: |
> > > @@ -843,23 +910,6 @@ controls:
> > >            value: 1
> > >            description: The lens shading map mode is available.
> > >
> > > -  - SceneFlicker:
> > > -      type: int32_t
> > > -      draft: true
> > > -      description: |
> > > -       Control to report the detected scene light frequency. Currently
> > > -       identical to ANDROID_STATISTICS_SCENE_FLICKER.
> > > -      enum:
> > > -        - name: SceneFickerOff
> > > -          value: 0
> > > -          description: No flickering detected.
> > > -        - name: SceneFicker50Hz
> > > -          value: 1
> > > -          description: 50Hz flickering detected.
> > > -        - name: SceneFicker60Hz
> > > -          value: 2
> > > -          description: 60Hz flickering detected.
> > > -
> > >    - PipelineDepth:
> > >        type: int32_t
> > >        draft: true
David Plowman April 4, 2023, 10:40 a.m. UTC | #8
Hi Laurent

Thanks for the review.

On Tue, 4 Apr 2023 at 07:06, Laurent Pinchart
<laurent.pinchart@ideasonboard.com> wrote:
>
> Hi David,
>
> Thank you for the patch.
>
> On Mon, Apr 03, 2023 at 09:20:39AM +0100, David Plowman via libcamera-devel wrote:
> > On Mon, 3 Apr 2023 at 08:58, Naushir Patuck wrote:
> > > On Tue, 28 Mar 2023 at 09:55, David Plowman via libcamera-devel wrote:
> > > >
> > > > Flicker is the term used to describe brightness banding or oscillation
> > > > of images caused typically by artificial lighting driven by a 50 or
> > > > 60Hz mains supply. We add three controls intended to be used by
> > > > AEC/AGC algorithms:
> > > >
> > > > AeFlickerMode to enable flicker avoidance.
> > > >
> > > > AeFlickerCustom to set custom flicker periods.
> > > >
> > > > AeFlickerDetected to report any flicker that is currently detected.
> > > >
> > > > Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
> > > > ---
> > > >  src/libcamera/control_ids.yaml | 84 +++++++++++++++++++++++++++-------
> > > >  1 file changed, 67 insertions(+), 17 deletions(-)
> > > >
> > > > diff --git a/src/libcamera/control_ids.yaml b/src/libcamera/control_ids.yaml
> > > > index adea5f90..b472050c 100644
> > > > --- a/src/libcamera/control_ids.yaml
> > > > +++ b/src/libcamera/control_ids.yaml
> > > > @@ -156,6 +156,73 @@ controls:
> > > >          control of which features should be automatically adjusted shouldn't
> > > >          better be handled through a separate AE mode control.
> > > >
> > > > +  - AeFlickerMode:
> > > > +      type: int32_t
> > > > +      description: |
> > > > +        Set the flicker mode, which determines whether, and how, the AGC/AEC
> > > > +        algorithm attempts to hide flicker effects caused by the duty cycle of
> > > > +        artificial lighting.
> > > > +
> > > > +        Although implementation dependent, many algorithms for "flicker
> > > > +        avoidance" work by restricting this exposure time to integer multiples
> > > > +        of the cycle period, wherever possible.
> > > > +
> > > > +        Implementations may not support all of the flicker modes listed below.
> > > > +
> > > > +      enum:
> > > > +        - name: FlickerOff
> > > > +          value: 0
> > > > +          description: No flicker avoidance is performed.
> > > > +        - name: FlickerFreq50Hz
> > > > +          value: 1
> > > > +          description: 50Hz flicker avoidance.
> > > > +            Suppress flicker effects caused by lighting running with a 100Hz
> > > > +            period (such as that produced by 50Hz mains electricity).
> > > > +        - name: FlickerFreq60Hz
> > > > +          value: 2
> > > > +          description: 60Hz flicker avoidance.
> > > > +            Suppress flicker effects caused by lighting running with a 120Hz
> > > > +            period (such as that produced by 60Hz mains electricity).
>
> Have you considered having 3 values here (off, manual and auto), and a
> AeFlickerPeriod control to set the manual value ? 50Hz and 60Hz are
> common values, but do they require special cases ?

Interesting thought, maybe I was following Android too slavishly. I
believe it to be the case that almost no one will care about anything
other than 50/60Hz so making those convenient seems helpful, but a
manual mode like this would be fine too. Too many choices... need to
think!

>
> > > > +        - name: FlickerCustom
> > > > +          value: 3
> > > > +          description: Custom flicker avoidance.
> > > > +            Suppress flicker effects caused by lighting running with a period
> > > > +            specified by the AeFlickerCustom control.
> > > > +            \sa AeFlickerCustom
> > > > +        - name: FlickerAuto
> > > > +          value: 4
> > > > +          description: Automatic flicker period detection and avoidance.
> > > > +            The system will automatically determine the most likely value of
> > > > +            flicker period, and avoid flicker of this frequency.
> > > > +
> > > > +  - AeFlickerCustom:
> > > > +      type: int32_t
> > > > +      description: Custom flicker period in microseconds.
> > > > +        This value sets the current flicker period to avoid. It is used when
> > > > +        AeFlickerMode is set to FlickerCustom.
> > > > +
> > > > +        If this control is not available, then the setting of custom flicker
> > > > +        periods is not supported.
> > > > +
> > > > +        \sa AeFlickerMode
> > > > +
> > > > +  - AeFlickerDetected:
> > > > +      type: int32_t
> > > > +      description: Flicker period detected in microseconds.
> > > > +        The value reported here indicates the currently detected flicker
> > > > +        period, or zero if no flicker at all is detected.
> > > > +
> > > > +        So in the case of 50Hz mains flicker, the value would be 10000
> > > > +        (corresponding to 100Hz), or 8333 (120Hz) for 60Hz mains flicker. But
> > > > +        note that it is not required that any form of scene flicker is detected
> > > > +        at all, so depending on the implementation, this metadata may just
> > > > +        report zero or be entirely absent.
> > > > +
> > > > +        AeFlickerDetected may also report other values for non-standard flicker
> > > > +        periods.
> > > > +
> > > > +        \sa AeFlickerMode
> > >
> > > To clarify, AeFlickerDetected ought to be returned unconditionally on every
> > > frame, even if AeFlickerMode is set to something other than FlickerAuto, is that
> > > correct?
> >
> > Actually I think that's a good question. A lot of this stuff _could_
> > be quite implementation dependent, I think, and we can't really
> > mandate that implementations will do auto-detection in all
> > circumstances (some may well not do it al all).
> >
> > For example, if we're actually doing flicker avoidance at 50Hz, you
> > might expect that it gets tricky to detect 50Hz flicker. So what do we
> > report? Maybe we should require that the metadata is absent (or invent
> > a "can't tell" value) in these circumstances. We should probably be
> > clearer on this, so maybe another version is required
>
> To answer this question I'd like to look at it from an application's
> point of view. How do you envision this control being used in
> applications ?

Yes, always a good idea to do this. Let me try and list a number of scenarios:

1. Flicker avoidance permanently off. For users who don't care!

2. A user or application might know what country they're in and set
Flicker avoidance to 50/60Hz immediately, and leave it forever. These
folks would just use the "manual" mode.

3. A user might want to rely on auto-detection to discover 50/60Hz
flicker, then they set flicker avoidance to that frequency and leave
it forever. Here we'd watch the "flicker detected" value, and as soon
as we see 50 or 60Hz, we'd set the flicker avoidance manually and
never touch it again.

After this, we might consider users with non-50/60Hz lights, or where
the flicker period might change. I think these use cases are way more
niche.

4. A user has some special lighting with a known period, and they want
to set it manually. After that they leave it alone. Though somewhat
niche, this sounds plausible to me, and is also something that is
easily implemented so we should probably cover it.

5. User has unknown lighting (that may not be 50/60Hz) and wants to be
told the flicker period so that they can cancel it. Obviously depends
on whether the implementation can support this. Niche certainly, but
perhaps it could happen.

After this I think there are cases where someone might want to detect
flicker even when one particular frequency is being cancelled, perhaps
so that they can change the flicker period. This sounds like the least
realistic end of the spectrum, and will definitely depend a lot on
what the capabilities of the platform are.

I also haven't talked about what "auto" means. There will be a class
of users who just want to set "auto" and forget about it. It may
depend on the implementation what "auto" means, but I would expect
something like case 3 (above) might be an obvious candidate. If a
platform can detect other flicker frequencies than the one it is
cancelling, then "auto" could potentially be cleverer, though most of
the time there's no benefit to this.

Sorry if that's got rather long! I guess the thing that perhaps leaves
me the most questions is exactly what flicker detection means when a
particular period is already being cancelled. Do we need to
distinguish between "I've detected another flicker frequency" from
"I've detected that there is no flicker at all" from "I can't tell" or
even "I can't detect a different frequency but I can't tell if the one
I'm cancelling is still there"?

Thanks
David

>
> > > Other than that:
> > >
> > > Reviewed-by: Naushir Patuck <naush@raspberrypi.com>
> > >
> > > > +
> > > >    - Brightness:
> > > >        type: float
> > > >        description: |
> > > > @@ -843,23 +910,6 @@ controls:
> > > >            value: 1
> > > >            description: The lens shading map mode is available.
> > > >
> > > > -  - SceneFlicker:
> > > > -      type: int32_t
> > > > -      draft: true
> > > > -      description: |
> > > > -       Control to report the detected scene light frequency. Currently
> > > > -       identical to ANDROID_STATISTICS_SCENE_FLICKER.
> > > > -      enum:
> > > > -        - name: SceneFickerOff
> > > > -          value: 0
> > > > -          description: No flickering detected.
> > > > -        - name: SceneFicker50Hz
> > > > -          value: 1
> > > > -          description: 50Hz flickering detected.
> > > > -        - name: SceneFicker60Hz
> > > > -          value: 2
> > > > -          description: 60Hz flickering detected.
> > > > -
> > > >    - PipelineDepth:
> > > >        type: int32_t
> > > >        draft: true
>
> --
> Regards,
>
> Laurent Pinchart
Laurent Pinchart April 5, 2023, 2:06 a.m. UTC | #9
Hi David,

On Tue, Apr 04, 2023 at 11:40:53AM +0100, David Plowman wrote:
> On Tue, 4 Apr 2023 at 07:06, Laurent Pinchart wrote:
> > On Mon, Apr 03, 2023 at 09:20:39AM +0100, David Plowman via libcamera-devel wrote:
> > > On Mon, 3 Apr 2023 at 08:58, Naushir Patuck wrote:
> > > > On Tue, 28 Mar 2023 at 09:55, David Plowman via libcamera-devel wrote:
> > > > >
> > > > > Flicker is the term used to describe brightness banding or oscillation
> > > > > of images caused typically by artificial lighting driven by a 50 or
> > > > > 60Hz mains supply. We add three controls intended to be used by
> > > > > AEC/AGC algorithms:
> > > > >
> > > > > AeFlickerMode to enable flicker avoidance.
> > > > >
> > > > > AeFlickerCustom to set custom flicker periods.
> > > > >
> > > > > AeFlickerDetected to report any flicker that is currently detected.
> > > > >
> > > > > Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
> > > > > ---
> > > > >  src/libcamera/control_ids.yaml | 84 +++++++++++++++++++++++++++-------
> > > > >  1 file changed, 67 insertions(+), 17 deletions(-)
> > > > >
> > > > > diff --git a/src/libcamera/control_ids.yaml b/src/libcamera/control_ids.yaml
> > > > > index adea5f90..b472050c 100644
> > > > > --- a/src/libcamera/control_ids.yaml
> > > > > +++ b/src/libcamera/control_ids.yaml
> > > > > @@ -156,6 +156,73 @@ controls:
> > > > >          control of which features should be automatically adjusted shouldn't
> > > > >          better be handled through a separate AE mode control.
> > > > >
> > > > > +  - AeFlickerMode:
> > > > > +      type: int32_t
> > > > > +      description: |
> > > > > +        Set the flicker mode, which determines whether, and how, the AGC/AEC
> > > > > +        algorithm attempts to hide flicker effects caused by the duty cycle of
> > > > > +        artificial lighting.
> > > > > +
> > > > > +        Although implementation dependent, many algorithms for "flicker
> > > > > +        avoidance" work by restricting this exposure time to integer multiples
> > > > > +        of the cycle period, wherever possible.
> > > > > +
> > > > > +        Implementations may not support all of the flicker modes listed below.
> > > > > +
> > > > > +      enum:
> > > > > +        - name: FlickerOff
> > > > > +          value: 0
> > > > > +          description: No flicker avoidance is performed.
> > > > > +        - name: FlickerFreq50Hz
> > > > > +          value: 1
> > > > > +          description: 50Hz flicker avoidance.
> > > > > +            Suppress flicker effects caused by lighting running with a 100Hz
> > > > > +            period (such as that produced by 50Hz mains electricity).
> > > > > +        - name: FlickerFreq60Hz
> > > > > +          value: 2
> > > > > +          description: 60Hz flicker avoidance.
> > > > > +            Suppress flicker effects caused by lighting running with a 120Hz
> > > > > +            period (such as that produced by 60Hz mains electricity).
> >
> > Have you considered having 3 values here (off, manual and auto), and a
> > AeFlickerPeriod control to set the manual value ? 50Hz and 60Hz are
> > common values, but do they require special cases ?
> 
> Interesting thought, maybe I was following Android too slavishly. I
> believe it to be the case that almost no one will care about anything
> other than 50/60Hz so making those convenient seems helpful, but a
> manual mode like this would be fine too. Too many choices... need to
> think!

With the world (or at least the parts I'm used to) switching to LED
lighting, won't automatic frequency detection become more relevant than
50/60Hz manual selection ?

> > > > > +        - name: FlickerCustom
> > > > > +          value: 3
> > > > > +          description: Custom flicker avoidance.
> > > > > +            Suppress flicker effects caused by lighting running with a period
> > > > > +            specified by the AeFlickerCustom control.
> > > > > +            \sa AeFlickerCustom
> > > > > +        - name: FlickerAuto
> > > > > +          value: 4
> > > > > +          description: Automatic flicker period detection and avoidance.
> > > > > +            The system will automatically determine the most likely value of
> > > > > +            flicker period, and avoid flicker of this frequency.
> > > > > +
> > > > > +  - AeFlickerCustom:
> > > > > +      type: int32_t
> > > > > +      description: Custom flicker period in microseconds.
> > > > > +        This value sets the current flicker period to avoid. It is used when
> > > > > +        AeFlickerMode is set to FlickerCustom.
> > > > > +
> > > > > +        If this control is not available, then the setting of custom flicker
> > > > > +        periods is not supported.
> > > > > +
> > > > > +        \sa AeFlickerMode
> > > > > +
> > > > > +  - AeFlickerDetected:
> > > > > +      type: int32_t
> > > > > +      description: Flicker period detected in microseconds.
> > > > > +        The value reported here indicates the currently detected flicker
> > > > > +        period, or zero if no flicker at all is detected.
> > > > > +
> > > > > +        So in the case of 50Hz mains flicker, the value would be 10000
> > > > > +        (corresponding to 100Hz), or 8333 (120Hz) for 60Hz mains flicker. But
> > > > > +        note that it is not required that any form of scene flicker is detected
> > > > > +        at all, so depending on the implementation, this metadata may just
> > > > > +        report zero or be entirely absent.
> > > > > +
> > > > > +        AeFlickerDetected may also report other values for non-standard flicker
> > > > > +        periods.
> > > > > +
> > > > > +        \sa AeFlickerMode
> > > >
> > > > To clarify, AeFlickerDetected ought to be returned unconditionally on every
> > > > frame, even if AeFlickerMode is set to something other than FlickerAuto, is that
> > > > correct?
> > >
> > > Actually I think that's a good question. A lot of this stuff _could_
> > > be quite implementation dependent, I think, and we can't really
> > > mandate that implementations will do auto-detection in all
> > > circumstances (some may well not do it al all).
> > >
> > > For example, if we're actually doing flicker avoidance at 50Hz, you
> > > might expect that it gets tricky to detect 50Hz flicker. So what do we
> > > report? Maybe we should require that the metadata is absent (or invent
> > > a "can't tell" value) in these circumstances. We should probably be
> > > clearer on this, so maybe another version is required
> >
> > To answer this question I'd like to look at it from an application's
> > point of view. How do you envision this control being used in
> > applications ?
> 
> Yes, always a good idea to do this. Let me try and list a number of scenarios:
> 
> 1. Flicker avoidance permanently off. For users who don't care!

And for controlled environment where the user knows there's no lighting
flicker.

> 2. A user or application might know what country they're in and set
> Flicker avoidance to 50/60Hz immediately, and leave it forever. These
> folks would just use the "manual" mode.
> 
> 3. A user might want to rely on auto-detection to discover 50/60Hz
> flicker, then they set flicker avoidance to that frequency and leave
> it forever. Here we'd watch the "flicker detected" value, and as soon
> as we see 50 or 60Hz, we'd set the flicker avoidance manually and
> never touch it again.

Wouldn't using auto mode be simpler for applications in this case ?

> After this, we might consider users with non-50/60Hz lights, or where
> the flicker period might change. I think these use cases are way more
> niche.

As mentioned in a reply to the cover letter, won't non-50/60Hz
frequencies become increasingly important with LED lighting ?

> 4. A user has some special lighting with a known period, and they want
> to set it manually. After that they leave it alone. Though somewhat
> niche, this sounds plausible to me, and is also something that is
> easily implemented so we should probably cover it.

I agree with both points, it's a bit niche, but I see no reason not to
support it.

> 5. User has unknown lighting (that may not be 50/60Hz) and wants to be
> told the flicker period so that they can cancel it. Obviously depends
> on whether the implementation can support this. Niche certainly, but
> perhaps it could happen.
> 
> After this I think there are cases where someone might want to detect
> flicker even when one particular frequency is being cancelled, perhaps
> so that they can change the flicker period. This sounds like the least
> realistic end of the spectrum, and will definitely depend a lot on
> what the capabilities of the platform are.
> 
> I also haven't talked about what "auto" means. There will be a class
> of users who just want to set "auto" and forget about it. It may
> depend on the implementation what "auto" means, but I would expect
> something like case 3 (above) might be an obvious candidate. If a
> platform can detect other flicker frequencies than the one it is
> cancelling, then "auto" could potentially be cleverer, though most of
> the time there's no benefit to this.

The way you've specified the behaviour of "auto" in your patch seems
fine to me. Implementations can be cleverer than that if they want to.

> Sorry if that's got rather long! I guess the thing that perhaps leaves
> me the most questions is exactly what flicker detection means when a
> particular period is already being cancelled. Do we need to
> distinguish between "I've detected another flicker frequency" from
> "I've detected that there is no flicker at all" from "I can't tell" or
> even "I can't detect a different frequency but I can't tell if the one
> I'm cancelling is still there"?

That's also the part that bothers me a bit, I'm not sure what would be
best for applications. Detecting and handling multiple frequencies seems
to me like an advanced use case that may be best left to applications to
manage.

Regarding reporting no flicker vs. unknown flicker frequency, is that
even something we can do ? If you set the exposure time to a multiple of
the flicker half period, my understanding is that libcamera wouldn't be
able to distinguish the two cases. We should then document that a zero
value for the detected flicker frequency could mean either when in
non-off mode.

> > > > Other than that:
> > > >
> > > > Reviewed-by: Naushir Patuck <naush@raspberrypi.com>
> > > >
> > > > > +
> > > > >    - Brightness:
> > > > >        type: float
> > > > >        description: |
> > > > > @@ -843,23 +910,6 @@ controls:
> > > > >            value: 1
> > > > >            description: The lens shading map mode is available.
> > > > >
> > > > > -  - SceneFlicker:
> > > > > -      type: int32_t
> > > > > -      draft: true
> > > > > -      description: |
> > > > > -       Control to report the detected scene light frequency. Currently
> > > > > -       identical to ANDROID_STATISTICS_SCENE_FLICKER.
> > > > > -      enum:
> > > > > -        - name: SceneFickerOff
> > > > > -          value: 0
> > > > > -          description: No flickering detected.
> > > > > -        - name: SceneFicker50Hz
> > > > > -          value: 1
> > > > > -          description: 50Hz flickering detected.
> > > > > -        - name: SceneFicker60Hz
> > > > > -          value: 2
> > > > > -          description: 60Hz flickering detected.
> > > > > -
> > > > >    - PipelineDepth:
> > > > >        type: int32_t
> > > > >        draft: true
Laurent Pinchart July 3, 2023, 9:18 a.m. UTC | #10
Hi David,

Resurecting this old thread.

On Wed, Apr 05, 2023 at 05:06:56AM +0300, Laurent Pinchart via libcamera-devel wrote:
> On Tue, Apr 04, 2023 at 11:40:53AM +0100, David Plowman wrote:
> > On Tue, 4 Apr 2023 at 07:06, Laurent Pinchart wrote:
> > > On Mon, Apr 03, 2023 at 09:20:39AM +0100, David Plowman via libcamera-devel wrote:
> > > > On Mon, 3 Apr 2023 at 08:58, Naushir Patuck wrote:
> > > > > On Tue, 28 Mar 2023 at 09:55, David Plowman via libcamera-devel wrote:
> > > > > >
> > > > > > Flicker is the term used to describe brightness banding or oscillation
> > > > > > of images caused typically by artificial lighting driven by a 50 or
> > > > > > 60Hz mains supply. We add three controls intended to be used by
> > > > > > AEC/AGC algorithms:
> > > > > >
> > > > > > AeFlickerMode to enable flicker avoidance.
> > > > > >
> > > > > > AeFlickerCustom to set custom flicker periods.
> > > > > >
> > > > > > AeFlickerDetected to report any flicker that is currently detected.
> > > > > >
> > > > > > Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
> > > > > > ---
> > > > > >  src/libcamera/control_ids.yaml | 84 +++++++++++++++++++++++++++-------
> > > > > >  1 file changed, 67 insertions(+), 17 deletions(-)
> > > > > >
> > > > > > diff --git a/src/libcamera/control_ids.yaml b/src/libcamera/control_ids.yaml
> > > > > > index adea5f90..b472050c 100644
> > > > > > --- a/src/libcamera/control_ids.yaml
> > > > > > +++ b/src/libcamera/control_ids.yaml
> > > > > > @@ -156,6 +156,73 @@ controls:
> > > > > >          control of which features should be automatically adjusted shouldn't
> > > > > >          better be handled through a separate AE mode control.
> > > > > >
> > > > > > +  - AeFlickerMode:
> > > > > > +      type: int32_t
> > > > > > +      description: |
> > > > > > +        Set the flicker mode, which determines whether, and how, the AGC/AEC
> > > > > > +        algorithm attempts to hide flicker effects caused by the duty cycle of
> > > > > > +        artificial lighting.
> > > > > > +
> > > > > > +        Although implementation dependent, many algorithms for "flicker
> > > > > > +        avoidance" work by restricting this exposure time to integer multiples
> > > > > > +        of the cycle period, wherever possible.
> > > > > > +
> > > > > > +        Implementations may not support all of the flicker modes listed below.
> > > > > > +
> > > > > > +      enum:
> > > > > > +        - name: FlickerOff
> > > > > > +          value: 0
> > > > > > +          description: No flicker avoidance is performed.
> > > > > > +        - name: FlickerFreq50Hz
> > > > > > +          value: 1
> > > > > > +          description: 50Hz flicker avoidance.
> > > > > > +            Suppress flicker effects caused by lighting running with a 100Hz
> > > > > > +            period (such as that produced by 50Hz mains electricity).
> > > > > > +        - name: FlickerFreq60Hz
> > > > > > +          value: 2
> > > > > > +          description: 60Hz flicker avoidance.
> > > > > > +            Suppress flicker effects caused by lighting running with a 120Hz
> > > > > > +            period (such as that produced by 60Hz mains electricity).
> > >
> > > Have you considered having 3 values here (off, manual and auto), and a
> > > AeFlickerPeriod control to set the manual value ? 50Hz and 60Hz are
> > > common values, but do they require special cases ?
> > 
> > Interesting thought, maybe I was following Android too slavishly. I
> > believe it to be the case that almost no one will care about anything
> > other than 50/60Hz so making those convenient seems helpful, but a
> > manual mode like this would be fine too. Too many choices... need to
> > think!
> 
> With the world (or at least the parts I'm used to) switching to LED
> lighting, won't automatic frequency detection become more relevant than
> 50/60Hz manual selection ?

What are everyone's thoughts on this ?

> > > > > > +        - name: FlickerCustom
> > > > > > +          value: 3
> > > > > > +          description: Custom flicker avoidance.
> > > > > > +            Suppress flicker effects caused by lighting running with a period
> > > > > > +            specified by the AeFlickerCustom control.
> > > > > > +            \sa AeFlickerCustom
> > > > > > +        - name: FlickerAuto
> > > > > > +          value: 4
> > > > > > +          description: Automatic flicker period detection and avoidance.
> > > > > > +            The system will automatically determine the most likely value of
> > > > > > +            flicker period, and avoid flicker of this frequency.
> > > > > > +
> > > > > > +  - AeFlickerCustom:
> > > > > > +      type: int32_t
> > > > > > +      description: Custom flicker period in microseconds.
> > > > > > +        This value sets the current flicker period to avoid. It is used when
> > > > > > +        AeFlickerMode is set to FlickerCustom.
> > > > > > +
> > > > > > +        If this control is not available, then the setting of custom flicker
> > > > > > +        periods is not supported.
> > > > > > +
> > > > > > +        \sa AeFlickerMode
> > > > > > +
> > > > > > +  - AeFlickerDetected:
> > > > > > +      type: int32_t
> > > > > > +      description: Flicker period detected in microseconds.
> > > > > > +        The value reported here indicates the currently detected flicker
> > > > > > +        period, or zero if no flicker at all is detected.
> > > > > > +
> > > > > > +        So in the case of 50Hz mains flicker, the value would be 10000
> > > > > > +        (corresponding to 100Hz), or 8333 (120Hz) for 60Hz mains flicker. But
> > > > > > +        note that it is not required that any form of scene flicker is detected
> > > > > > +        at all, so depending on the implementation, this metadata may just
> > > > > > +        report zero or be entirely absent.
> > > > > > +
> > > > > > +        AeFlickerDetected may also report other values for non-standard flicker
> > > > > > +        periods.
> > > > > > +
> > > > > > +        \sa AeFlickerMode
> > > > >
> > > > > To clarify, AeFlickerDetected ought to be returned unconditionally on every
> > > > > frame, even if AeFlickerMode is set to something other than FlickerAuto, is that
> > > > > correct?
> > > >
> > > > Actually I think that's a good question. A lot of this stuff _could_
> > > > be quite implementation dependent, I think, and we can't really
> > > > mandate that implementations will do auto-detection in all
> > > > circumstances (some may well not do it al all).
> > > >
> > > > For example, if we're actually doing flicker avoidance at 50Hz, you
> > > > might expect that it gets tricky to detect 50Hz flicker. So what do we
> > > > report? Maybe we should require that the metadata is absent (or invent
> > > > a "can't tell" value) in these circumstances. We should probably be
> > > > clearer on this, so maybe another version is required
> > >
> > > To answer this question I'd like to look at it from an application's
> > > point of view. How do you envision this control being used in
> > > applications ?
> > 
> > Yes, always a good idea to do this. Let me try and list a number of scenarios:
> > 
> > 1. Flicker avoidance permanently off. For users who don't care!
> 
> And for controlled environment where the user knows there's no lighting
> flicker.

And for users who don't care, shouldn't the camera default to auto if it
supports that mode ? That's Android's default, and I think it makes
sense for applications that target consumer users. I'd expect
applications that target use cases where it's important to disable
flicker avoidance to be able to do so manually.

> > 2. A user or application might know what country they're in and set
> > Flicker avoidance to 50/60Hz immediately, and leave it forever. These
> > folks would just use the "manual" mode.
> > 
> > 3. A user might want to rely on auto-detection to discover 50/60Hz
> > flicker, then they set flicker avoidance to that frequency and leave
> > it forever. Here we'd watch the "flicker detected" value, and as soon
> > as we see 50 or 60Hz, we'd set the flicker avoidance manually and
> > never touch it again.
> 
> Wouldn't using auto mode be simpler for applications in this case ?

If the IPA has the ability to detect the frequency, I believe we should
require the auto mode to be supported, in which case applications should
should use it.

A posibly more common use case for 50/60Hz detection reported to
applications is users operating in a "standard" 50/60Hz environment who
want to use manual exposure.

> > After this, we might consider users with non-50/60Hz lights, or where
> > the flicker period might change. I think these use cases are way more
> > niche.
> 
> As mentioned in a reply to the cover letter, won't non-50/60Hz
> frequencies become increasingly important with LED lighting ?
> 
> > 4. A user has some special lighting with a known period, and they want
> > to set it manually. After that they leave it alone. Though somewhat
> > niche, this sounds plausible to me, and is also something that is
> > easily implemented so we should probably cover it.
> 
> I agree with both points, it's a bit niche, but I see no reason not to
> support it.
> 
> > 5. User has unknown lighting (that may not be 50/60Hz) and wants to be
> > told the flicker period so that they can cancel it. Obviously depends
> > on whether the implementation can support this. Niche certainly, but
> > perhaps it could happen.
> > 
> > After this I think there are cases where someone might want to detect
> > flicker even when one particular frequency is being cancelled, perhaps
> > so that they can change the flicker period. This sounds like the least
> > realistic end of the spectrum, and will definitely depend a lot on
> > what the capabilities of the platform are.

Agreed.

> > I also haven't talked about what "auto" means. There will be a class
> > of users who just want to set "auto" and forget about it. It may
> > depend on the implementation what "auto" means, but I would expect
> > something like case 3 (above) might be an obvious candidate. If a
> > platform can detect other flicker frequencies than the one it is
> > cancelling, then "auto" could potentially be cleverer, though most of
> > the time there's no benefit to this.
> 
> The way you've specified the behaviour of "auto" in your patch seems
> fine to me. Implementations can be cleverer than that if they want to.
> 
> > Sorry if that's got rather long! I guess the thing that perhaps leaves
> > me the most questions is exactly what flicker detection means when a
> > particular period is already being cancelled. Do we need to
> > distinguish between "I've detected another flicker frequency" from
> > "I've detected that there is no flicker at all" from "I can't tell" or
> > even "I can't detect a different frequency but I can't tell if the one
> > I'm cancelling is still there"?
> 
> That's also the part that bothers me a bit, I'm not sure what would be
> best for applications. Detecting and handling multiple frequencies seems
> to me like an advanced use case that may be best left to applications to
> manage.
> 
> Regarding reporting no flicker vs. unknown flicker frequency, is that
> even something we can do ? If you set the exposure time to a multiple of
> the flicker half period, my understanding is that libcamera wouldn't be
> able to distinguish the two cases. We should then document that a zero
> value for the detected flicker frequency could mean either when in
> non-off mode.
> 
> > > > > Other than that:
> > > > >
> > > > > Reviewed-by: Naushir Patuck <naush@raspberrypi.com>
> > > > >
> > > > > > +
> > > > > >    - Brightness:
> > > > > >        type: float
> > > > > >        description: |
> > > > > > @@ -843,23 +910,6 @@ controls:
> > > > > >            value: 1
> > > > > >            description: The lens shading map mode is available.
> > > > > >
> > > > > > -  - SceneFlicker:
> > > > > > -      type: int32_t
> > > > > > -      draft: true
> > > > > > -      description: |
> > > > > > -       Control to report the detected scene light frequency. Currently
> > > > > > -       identical to ANDROID_STATISTICS_SCENE_FLICKER.
> > > > > > -      enum:
> > > > > > -        - name: SceneFickerOff
> > > > > > -          value: 0
> > > > > > -          description: No flickering detected.
> > > > > > -        - name: SceneFicker50Hz
> > > > > > -          value: 1
> > > > > > -          description: 50Hz flickering detected.
> > > > > > -        - name: SceneFicker60Hz
> > > > > > -          value: 2
> > > > > > -          description: 60Hz flickering detected.
> > > > > > -
> > > > > >    - PipelineDepth:
> > > > > >        type: int32_t
> > > > > >        draft: true

Patch
diff mbox series

diff --git a/src/libcamera/control_ids.yaml b/src/libcamera/control_ids.yaml
index adea5f90..b472050c 100644
--- a/src/libcamera/control_ids.yaml
+++ b/src/libcamera/control_ids.yaml
@@ -156,6 +156,73 @@  controls:
         control of which features should be automatically adjusted shouldn't
         better be handled through a separate AE mode control.
 
+  - AeFlickerMode:
+      type: int32_t
+      description: |
+        Set the flicker mode, which determines whether, and how, the AGC/AEC
+        algorithm attempts to hide flicker effects caused by the duty cycle of
+        artificial lighting.
+
+        Although implementation dependent, many algorithms for "flicker
+        avoidance" work by restricting this exposure time to integer multiples
+        of the cycle period, wherever possible.
+
+        Implementations may not support all of the flicker modes listed below.
+
+      enum:
+        - name: FlickerOff
+          value: 0
+          description: No flicker avoidance is performed.
+        - name: FlickerFreq50Hz
+          value: 1
+          description: 50Hz flicker avoidance.
+            Suppress flicker effects caused by lighting running with a 100Hz
+            period (such as that produced by 50Hz mains electricity).
+        - name: FlickerFreq60Hz
+          value: 2
+          description: 60Hz flicker avoidance.
+            Suppress flicker effects caused by lighting running with a 120Hz
+            period (such as that produced by 60Hz mains electricity).
+        - name: FlickerCustom
+          value: 3
+          description: Custom flicker avoidance.
+            Suppress flicker effects caused by lighting running with a period
+            specified by the AeFlickerCustom control.
+            \sa AeFlickerCustom
+        - name: FlickerAuto
+          value: 4
+          description: Automatic flicker period detection and avoidance.
+            The system will automatically determine the most likely value of
+            flicker period, and avoid flicker of this frequency.
+
+  - AeFlickerCustom:
+      type: int32_t
+      description: Custom flicker period in microseconds.
+        This value sets the current flicker period to avoid. It is used when
+        AeFlickerMode is set to FlickerCustom.
+
+        If this control is not available, then the setting of custom flicker
+        periods is not supported.
+
+        \sa AeFlickerMode
+
+  - AeFlickerDetected:
+      type: int32_t
+      description: Flicker period detected in microseconds.
+        The value reported here indicates the currently detected flicker
+        period, or zero if no flicker at all is detected.
+
+        So in the case of 50Hz mains flicker, the value would be 10000
+        (corresponding to 100Hz), or 8333 (120Hz) for 60Hz mains flicker. But
+        note that it is not required that any form of scene flicker is detected
+        at all, so depending on the implementation, this metadata may just
+        report zero or be entirely absent.
+
+        AeFlickerDetected may also report other values for non-standard flicker
+        periods.
+
+        \sa AeFlickerMode
+
   - Brightness:
       type: float
       description: |
@@ -843,23 +910,6 @@  controls:
           value: 1
           description: The lens shading map mode is available.
 
-  - SceneFlicker:
-      type: int32_t
-      draft: true
-      description: |
-       Control to report the detected scene light frequency. Currently
-       identical to ANDROID_STATISTICS_SCENE_FLICKER.
-      enum:
-        - name: SceneFickerOff
-          value: 0
-          description: No flickering detected.
-        - name: SceneFicker50Hz
-          value: 1
-          description: 50Hz flickering detected.
-        - name: SceneFicker60Hz
-          value: 2
-          description: 60Hz flickering detected.
-
   - PipelineDepth:
       type: int32_t
       draft: true