<div dir="ltr"><div dir="ltr">Hi Laurent,<div><br></div><div>Thank you for your review feedback.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, 4 Feb 2021 at 20:59, Laurent Pinchart <<a href="mailto:laurent.pinchart@ideasonboard.com">laurent.pinchart@ideasonboard.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi Naush,<br>
<br>
Thank you for the patch.<br>
<br>
On Fri, Jan 29, 2021 at 11:16:15AM +0000, Naushir Patuck wrote:<br>
> In order to provide an optimal split between shutter speed and gain, the<br>
> AGC must know the maximum allowable shutter speed, as limited by the<br>
> maximum frame duration (either application provided or the default).<br>
> <br>
> Add a new API function, SetMaxShutter, to the AgcAlgorithm class. The<br>
> IPA provides the the maximum shutter speed for AGC calculations.<br>
<br>
s/the the/the/<br>
<br>
> This applies to both the manual and auto AGC modes.<br>
> <br>
> Signed-off-by: Naushir Patuck <<a href="mailto:naush@raspberrypi.com" target="_blank">naush@raspberrypi.com</a>><br>
> Reviewed-by: David Plowman <<a href="mailto:david.plowman@raspberrypi.com" target="_blank">david.plowman@raspberrypi.com</a>><br>
> Reviewed-by: Jacopo Mondi <<a href="mailto:jacopo@jmondi.org" target="_blank">jacopo@jmondi.org</a>><br>
> ---<br>
>  .../raspberrypi/controller/agc_algorithm.hpp  |  1 +<br>
>  src/ipa/raspberrypi/controller/rpi/agc.cpp    | 48 +++++++++++++------<br>
>  src/ipa/raspberrypi/controller/rpi/agc.hpp    |  3 ++<br>
>  src/ipa/raspberrypi/raspberrypi.cpp           | 12 +++++<br>
>  4 files changed, 49 insertions(+), 15 deletions(-)<br>
> <br>
> diff --git a/src/ipa/raspberrypi/controller/agc_algorithm.hpp b/src/ipa/raspberrypi/controller/agc_algorithm.hpp<br>
> index 981f1de2f0e4..f97deb0fca59 100644<br>
> --- a/src/ipa/raspberrypi/controller/agc_algorithm.hpp<br>
> +++ b/src/ipa/raspberrypi/controller/agc_algorithm.hpp<br>
> @@ -19,6 +19,7 @@ public:<br>
>       virtual void SetEv(double ev) = 0;<br>
>       virtual void SetFlickerPeriod(double flicker_period) = 0;<br>
>       virtual void SetFixedShutter(double fixed_shutter) = 0; // microseconds<br>
> +     virtual void SetMaxShutter(double max_shutter) = 0; // microseconds<br>
>       virtual void SetFixedAnalogueGain(double fixed_analogue_gain) = 0;<br>
>       virtual void SetMeteringMode(std::string const &metering_mode_name) = 0;<br>
>       virtual void SetExposureMode(std::string const &exposure_mode_name) = 0;<br>
> diff --git a/src/ipa/raspberrypi/controller/rpi/agc.cpp b/src/ipa/raspberrypi/controller/rpi/agc.cpp<br>
> index eddd1684da12..0023d50029f1 100644<br>
> --- a/src/ipa/raspberrypi/controller/rpi/agc.cpp<br>
> +++ b/src/ipa/raspberrypi/controller/rpi/agc.cpp<br>
> @@ -157,7 +157,7 @@ Agc::Agc(Controller *controller)<br>
>         frame_count_(0), lock_count_(0),<br>
>         last_target_exposure_(0.0),<br>
>         ev_(1.0), flicker_period_(0.0),<br>
> -       fixed_shutter_(0), fixed_analogue_gain_(0.0)<br>
> +       max_shutter_(0), fixed_shutter_(0), fixed_analogue_gain_(0.0)<br>
>  {<br>
>       memset(&awb_, 0, sizeof(awb_));<br>
>       // Setting status_.total_exposure_value_ to zero initially tells us<br>
> @@ -227,11 +227,16 @@ void Agc::SetFlickerPeriod(double flicker_period)<br>
>       flicker_period_ = flicker_period;<br>
>  }<br>
>  <br>
> +void Agc::SetMaxShutter(double max_shutter)<br>
> +{<br>
> +     max_shutter_ = max_shutter;<br>
> +}<br>
> +<br>
>  void Agc::SetFixedShutter(double fixed_shutter)<br>
>  {<br>
>       fixed_shutter_ = fixed_shutter;<br>
>       // Set this in case someone calls Pause() straight after.<br>
> -     status_.shutter_time = fixed_shutter;<br>
> +     status_.shutter_time = clipShutter(fixed_shutter_);<br>
>  }<br>
>  <br>
>  void Agc::SetFixedAnalogueGain(double fixed_analogue_gain)<br>
> @@ -261,7 +266,8 @@ void Agc::SwitchMode([[maybe_unused]] CameraMode const &camera_mode,<br>
>  {<br>
>       housekeepConfig();<br>
>  <br>
> -     if (fixed_shutter_ != 0.0 && fixed_analogue_gain_ != 0.0) {<br>
> +     double fixed_shutter = clipShutter(fixed_shutter_);<br>
> +     if (fixed_shutter != 0.0 && fixed_analogue_gain_ != 0.0) {<br>
>               // We're going to reset the algorithm here with these fixed values.<br>
>  <br>
>               fetchAwbStatus(metadata);<br>
> @@ -269,14 +275,14 @@ void Agc::SwitchMode([[maybe_unused]] CameraMode const &camera_mode,<br>
>               ASSERT(min_colour_gain != 0.0);<br>
>  <br>
>               // This is the equivalent of computeTargetExposure and applyDigitalGain.<br>
> -             target_.total_exposure_no_dg = fixed_shutter_ * fixed_analogue_gain_;<br>
> +             target_.total_exposure_no_dg = fixed_shutter * fixed_analogue_gain_;<br>
>               target_.total_exposure = target_.total_exposure_no_dg / min_colour_gain;<br>
>  <br>
>               // Equivalent of filterExposure. This resets any "history".<br>
>               filtered_ = target_;<br>
>  <br>
>               // Equivalent of divideUpExposure.<br>
> -             filtered_.shutter = fixed_shutter_;<br>
> +             filtered_.shutter = fixed_shutter;<br>
>               filtered_.analogue_gain = fixed_analogue_gain_;<br>
>       } else if (status_.total_exposure_value) {<br>
>               // On a mode switch, it's possible the exposure profile could change,<br>
> @@ -290,7 +296,7 @@ void Agc::SwitchMode([[maybe_unused]] CameraMode const &camera_mode,<br>
>               // for any that weren't set.<br>
>  <br>
>               // Equivalent of divideUpExposure.<br>
> -             filtered_.shutter = fixed_shutter_ ? fixed_shutter_ : config_.default_exposure_time;<br>
> +             filtered_.shutter = fixed_shutter ? fixed_shutter : config_.default_exposure_time;<br>
>               filtered_.analogue_gain = fixed_analogue_gain_ ? fixed_analogue_gain_ : config_.default_analogue_gain;<br>
>       }<br>
>  <br>
> @@ -403,7 +409,8 @@ void Agc::housekeepConfig()<br>
>  {<br>
>       // First fetch all the up-to-date settings, so no one else has to do it.<br>
>       status_.ev = ev_;<br>
> -     status_.fixed_shutter = fixed_shutter_;<br>
> +     double fixed_shutter = clipShutter(fixed_shutter_);<br>
> +     status_.fixed_shutter = fixed_shutter;<br>
<br>
You could write<br>
<br>
        status_.fixed_shutter = clipShutter(fixed_shutter_);<br>
<br>
>       status_.fixed_analogue_gain = fixed_analogue_gain_;<br>
>       status_.flicker_period = flicker_period_;<br>
>       LOG(RPiAgc, Debug) << "ev " << status_.ev << " fixed_shutter "<br>
> @@ -582,13 +589,15 @@ void Agc::computeTargetExposure(double gain)<br>
>               target_.total_exposure = current_.total_exposure_no_dg * gain;<br>
>               // The final target exposure is also limited to what the exposure<br>
>               // mode allows.<br>
> +             double max_shutter = status_.fixed_shutter != 0.0<br>
> +                                          ? status_.fixed_shutter<br>
> +                                          : exposure_mode_->shutter.back();<br>
<br>
The indentation is a bit weird, how about this ?<br>
<br>
                double max_shutter = status_.fixed_shutter != 0.0<br>
                                   ? status_.fixed_shutter<br>
                                   : exposure_mode_->shutter.back();<br>
<br>
Those are minor issues,<br>
<br>
Reviewed-by: Laurent Pinchart <<a href="mailto:laurent.pinchart@ideasonboard.com" target="_blank">laurent.pinchart@ideasonboard.com</a>><br>
<br>
If you're fine with these changes, there's no need to resent, I'll fix<br>
when applying.<br></blockquote><div><br></div><div>Ack all the minors.  Please do fix when applying if it is not too much trouble.</div><div><br></div><div>Regards,</div><div>Naush</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
> +             max_shutter = clipShutter(max_shutter);<br>
>               double max_total_exposure =<br>
> -                     (status_.fixed_shutter != 0.0<br>
> -                      ? status_.fixed_shutter<br>
> -                      : exposure_mode_->shutter.back()) *<br>
> +                     max_shutter *<br>
>                       (status_.fixed_analogue_gain != 0.0<br>
> -                      ? status_.fixed_analogue_gain<br>
> -                      : exposure_mode_->gain.back());<br>
> +                              ? status_.fixed_analogue_gain<br>
> +                              : exposure_mode_->gain.back());<br>
>               target_.total_exposure = std::min(target_.total_exposure,<br>
>                                                 max_total_exposure);<br>
>       }<br>
> @@ -671,6 +680,7 @@ void Agc::divideUpExposure()<br>
>       shutter_time = status_.fixed_shutter != 0.0<br>
>                              ? status_.fixed_shutter<br>
>                              : exposure_mode_->shutter[0];<br>
> +     shutter_time = clipShutter(shutter_time);<br>
>       analogue_gain = status_.fixed_analogue_gain != 0.0<br>
>                               ? status_.fixed_analogue_gain<br>
>                               : exposure_mode_->gain[0];<br>
> @@ -678,14 +688,15 @@ void Agc::divideUpExposure()<br>
>               for (unsigned int stage = 1;<br>
>                    stage < exposure_mode_->gain.size(); stage++) {<br>
>                       if (status_.fixed_shutter == 0.0) {<br>
> -                             if (exposure_mode_->shutter[stage] *<br>
> -                                         analogue_gain >=<br>
> +                             double stage_shutter =<br>
> +                                     clipShutter(exposure_mode_->shutter[stage]);<br>
> +                             if (stage_shutter * analogue_gain >=<br>
>                                   exposure_value) {<br>
>                                       shutter_time =<br>
>                                               exposure_value / analogue_gain;<br>
>                                       break;<br>
>                               }<br>
> -                             shutter_time = exposure_mode_->shutter[stage];<br>
> +                             shutter_time = stage_shutter;<br>
>                       }<br>
>                       if (status_.fixed_analogue_gain == 0.0) {<br>
>                               if (exposure_mode_->gain[stage] *<br>
> @@ -740,6 +751,13 @@ void Agc::writeAndFinish(Metadata *image_metadata, bool desaturate)<br>
>                          << " analogue gain " << filtered_.analogue_gain;<br>
>  }<br>
>  <br>
> +double Agc::clipShutter(double shutter)<br>
> +{<br>
> +     if (max_shutter_)<br>
> +             shutter = std::min(shutter, max_shutter_);<br>
> +     return shutter;<br>
> +}<br>
> +<br>
>  // Register algorithm with the system.<br>
>  static Algorithm *Create(Controller *controller)<br>
>  {<br>
> diff --git a/src/ipa/raspberrypi/controller/rpi/agc.hpp b/src/ipa/raspberrypi/controller/rpi/agc.hpp<br>
> index 05c334e4a1de..0427fb59ec1b 100644<br>
> --- a/src/ipa/raspberrypi/controller/rpi/agc.hpp<br>
> +++ b/src/ipa/raspberrypi/controller/rpi/agc.hpp<br>
> @@ -78,6 +78,7 @@ public:<br>
>       unsigned int GetConvergenceFrames() const override;<br>
>       void SetEv(double ev) override;<br>
>       void SetFlickerPeriod(double flicker_period) override;<br>
> +     void SetMaxShutter(double max_shutter) override; // microseconds<br>
>       void SetFixedShutter(double fixed_shutter) override; // microseconds<br>
>       void SetFixedAnalogueGain(double fixed_analogue_gain) override;<br>
>       void SetMeteringMode(std::string const &metering_mode_name) override;<br>
> @@ -100,6 +101,7 @@ private:<br>
>       void filterExposure(bool desaturate);<br>
>       void divideUpExposure();<br>
>       void writeAndFinish(Metadata *image_metadata, bool desaturate);<br>
> +     double clipShutter(double shutter);<br>
>       AgcMeteringMode *metering_mode_;<br>
>       AgcExposureMode *exposure_mode_;<br>
>       AgcConstraintMode *constraint_mode_;<br>
> @@ -126,6 +128,7 @@ private:<br>
>       std::string constraint_mode_name_;<br>
>       double ev_;<br>
>       double flicker_period_;<br>
> +     double max_shutter_;<br>
>       double fixed_shutter_;<br>
>       double fixed_analogue_gain_;<br>
>  };<br>
> diff --git a/src/ipa/raspberrypi/raspberrypi.cpp b/src/ipa/raspberrypi/raspberrypi.cpp<br>
> index e4911b734e20..8c0e699184f6 100644<br>
> --- a/src/ipa/raspberrypi/raspberrypi.cpp<br>
> +++ b/src/ipa/raspberrypi/raspberrypi.cpp<br>
> @@ -1006,6 +1006,18 @@ void IPARPi::applyFrameDurations(double minFrameDuration, double maxFrameDuratio<br>
>       libcameraMetadata_.set(controls::FrameDurations,<br>
>                              { static_cast<int64_t>(minFrameDuration_),<br>
>                                static_cast<int64_t>(maxFrameDuration_) });<br>
> +<br>
> +     /*<br>
> +      * Calculate the maximum exposure time possible for the AGC to use.<br>
> +      * GetVBlanking() will update maxShutter with the largest exposure<br>
> +      * value possible.<br>
> +      */<br>
> +     double maxShutter = std::numeric_limits<double>::max();<br>
> +     helper_->GetVBlanking(maxShutter, minFrameDuration_, maxFrameDuration_);<br>
> +<br>
> +     RPiController::AgcAlgorithm *agc = dynamic_cast<RPiController::AgcAlgorithm *>(<br>
> +             controller_.GetAlgorithm("agc"));<br>
> +     agc->SetMaxShutter(maxShutter);<br>
>  }<br>
>  <br>
>  void IPARPi::applyAGC(const struct AgcStatus *agcStatus, ControlList &ctrls)<br>
<br>
-- <br>
Regards,<br>
<br>
Laurent Pinchart<br>
</blockquote></div></div>