<div dir="ltr"><div dir="ltr">Hi David,<div><br></div><div>Thank you for your work.</div><div><br></div></div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, 27 May 2021 at 09:45, David Plowman <<a href="mailto:david.plowman@raspberrypi.com">david.plowman@raspberrypi.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">When the sensor is switched to a mode with a different sensitivity,<br>
the target exposure values need to be adjusted proportionately to<br>
maintain the same image brightness.<br>
<br>
Signed-off-by: David Plowman <<a href="mailto:david.plowman@raspberrypi.com" target="_blank">david.plowman@raspberrypi.com</a>><br></blockquote><div><br></div><div>These changes look sensible to me.</div><div><br></div><div>Reviewed-by: Naushir Patuck <<a href="mailto:naush@raspberrpi.com">naush@raspberrpi.com</a>></div><div> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
---<br>
 src/ipa/raspberrypi/controller/rpi/agc.cpp | 25 +++++++++++++++++-----<br>
 src/ipa/raspberrypi/controller/rpi/agc.hpp |  1 +<br>
 2 files changed, 21 insertions(+), 5 deletions(-)<br>
<br>
diff --git a/src/ipa/raspberrypi/controller/rpi/agc.cpp b/src/ipa/raspberrypi/controller/rpi/agc.cpp<br>
index f4cd5d26..6f0e9702 100644<br>
--- a/src/ipa/raspberrypi/controller/rpi/agc.cpp<br>
+++ b/src/ipa/raspberrypi/controller/rpi/agc.cpp<br>
@@ -155,7 +155,7 @@ Agc::Agc(Controller *controller)<br>
        : AgcAlgorithm(controller), metering_mode_(nullptr),<br>
          exposure_mode_(nullptr), constraint_mode_(nullptr),<br>
          frame_count_(0), lock_count_(0),<br>
-         last_target_exposure_(0.0),<br>
+         last_target_exposure_(0.0), last_sensitivity_(0.0),<br>
          ev_(1.0), flicker_period_(0.0),<br>
          max_shutter_(0), fixed_shutter_(0), fixed_analogue_gain_(0.0)<br>
 {<br>
@@ -261,7 +261,7 @@ void Agc::SetConstraintMode(std::string const &constraint_mode_name)<br>
        constraint_mode_name_ = constraint_mode_name;<br>
 }<br>
<br>
-void Agc::SwitchMode([[maybe_unused]] CameraMode const &camera_mode,<br>
+void Agc::SwitchMode(CameraMode const &camera_mode,<br>
                     Metadata *metadata)<br>
 {<br>
        housekeepConfig();<br>
@@ -285,9 +285,20 @@ void Agc::SwitchMode([[maybe_unused]] CameraMode const &camera_mode,<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>
-               // or a fixed exposure/gain might be set so we divide up the exposure/<br>
-               // gain again, but we don't change any target values.<br>
+               // On a mode switch, various things could happen:<br>
+               // - the exposure profile might change<br>
+               // - a fixed exposure or gain might be set<br>
+               // - the new mode's sensitivity might be different<br>
+               // We cope with the last of these by scaling the target values. After<br>
+               // that we just need to re-divide the exposure/gain according to the<br>
+               // current exposure profile, which takes care of everything else.<br>
+<br>
+               double ratio = last_sensitivity_ / camera_mode.sensitivity;<br>
+               target_.total_exposure_no_dg *= ratio;<br>
+               target_.total_exposure *= ratio;<br>
+               filtered_.total_exposure_no_dg *= ratio;<br>
+               filtered_.total_exposure *= ratio;<br>
+<br>
                divideUpExposure();<br>
        } else {<br>
                // We come through here on startup, when at least one of the shutter<br>
@@ -301,6 +312,10 @@ void Agc::SwitchMode([[maybe_unused]] CameraMode const &camera_mode,<br>
        }<br>
<br>
        writeAndFinish(metadata, false);<br>
+<br>
+       // We must remember the sensitivity of this mode for the next SwitchMode.<br>
+       ASSERT(camera_mode.sensitivity != 0.0);<br>
+       last_sensitivity_ = camera_mode.sensitivity;<br>
 }<br>
<br>
 void Agc::Prepare(Metadata *image_metadata)<br>
diff --git a/src/ipa/raspberrypi/controller/rpi/agc.hpp b/src/ipa/raspberrypi/controller/rpi/agc.hpp<br>
index 0427fb59..025878a2 100644<br>
--- a/src/ipa/raspberrypi/controller/rpi/agc.hpp<br>
+++ b/src/ipa/raspberrypi/controller/rpi/agc.hpp<br>
@@ -122,6 +122,7 @@ private:<br>
        int lock_count_;<br>
        DeviceStatus last_device_status_;<br>
        double last_target_exposure_;<br>
+       double last_sensitivity_; // sensitivity of the previous camera mode<br>
        // Below here the "settings" that applications can change.<br>
        std::string metering_mode_name_;<br>
        std::string exposure_mode_name_;<br>
-- <br>
2.20.1<br>
<br>
</blockquote></div></div>