[libcamera-devel,v3,3/5] ipa: rpi: agc: Implementation of multi-channel AGC
diff mbox series

Message ID 20230912102442.169001-4-david.plowman@raspberrypi.com
State Accepted
Headers show
Series
  • Multi-channel AGC
Related show

Commit Message

David Plowman Sept. 12, 2023, 10:24 a.m. UTC
The switchMode, prepare and process methods are updated to implement
multi-channel AGC correctly:

* switchMode now invokes switchMode on all the channels (whether
  active or not).

* prepare must find what channel the current frame is, and run on
  behalf of that channel.

* process updates the most recent DeviceStatus and statistics for the
  channel of the frame that has just arrived, but generates updated
  values working through the active channels in round-robin fashion.

One minor detail in process is that we don't want to change the
DeviceStatus metadata of the current frame, so we now pass this to the
AgcChannel's process method, rather than letting it find the
DeviceStatus in the metadata.

Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
Reviewed-by: Naushir Patuck <naush@raspberrypi.com>
---
 src/ipa/rpi/controller/agc_status.h        |   1 +
 src/ipa/rpi/controller/rpi/agc.cpp         | 108 +++++++++++++++++++--
 src/ipa/rpi/controller/rpi/agc.h           |   3 +
 src/ipa/rpi/controller/rpi/agc_channel.cpp |  13 +--
 src/ipa/rpi/controller/rpi/agc_channel.h   |   4 +-
 5 files changed, 111 insertions(+), 18 deletions(-)

Comments

Jacopo Mondi Sept. 12, 2023, 3:06 p.m. UTC | #1
Hi David

On Tue, Sep 12, 2023 at 11:24:40AM +0100, David Plowman via libcamera-devel wrote:
> The switchMode, prepare and process methods are updated to implement
> multi-channel AGC correctly:
>
> * switchMode now invokes switchMode on all the channels (whether
>   active or not).
>
> * prepare must find what channel the current frame is, and run on
>   behalf of that channel.
>
> * process updates the most recent DeviceStatus and statistics for the
>   channel of the frame that has just arrived, but generates updated
>   values working through the active channels in round-robin fashion.
>
> One minor detail in process is that we don't want to change the
> DeviceStatus metadata of the current frame, so we now pass this to the
> AgcChannel's process method, rather than letting it find the
> DeviceStatus in the metadata.
>
> Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
> Reviewed-by: Naushir Patuck <naush@raspberrypi.com>
> ---
>  src/ipa/rpi/controller/agc_status.h        |   1 +
>  src/ipa/rpi/controller/rpi/agc.cpp         | 108 +++++++++++++++++++--
>  src/ipa/rpi/controller/rpi/agc.h           |   3 +
>  src/ipa/rpi/controller/rpi/agc_channel.cpp |  13 +--
>  src/ipa/rpi/controller/rpi/agc_channel.h   |   4 +-
>  5 files changed, 111 insertions(+), 18 deletions(-)
>
> diff --git a/src/ipa/rpi/controller/agc_status.h b/src/ipa/rpi/controller/agc_status.h
> index 597eddd7..e5c4ee22 100644
> --- a/src/ipa/rpi/controller/agc_status.h
> +++ b/src/ipa/rpi/controller/agc_status.h
> @@ -36,6 +36,7 @@ struct AgcStatus {
>  	int floatingRegionEnable;
>  	libcamera::utils::Duration fixedShutter;
>  	double fixedAnalogueGain;
> +	unsigned int channel;
>  };
>
>  struct AgcPrepareStatus {
> diff --git a/src/ipa/rpi/controller/rpi/agc.cpp b/src/ipa/rpi/controller/rpi/agc.cpp
> index 598fc890..58ba8839 100644
> --- a/src/ipa/rpi/controller/rpi/agc.cpp
> +++ b/src/ipa/rpi/controller/rpi/agc.cpp
> @@ -22,7 +22,8 @@ LOG_DEFINE_CATEGORY(RPiAgc)
>
>  Agc::Agc(Controller *controller)
>  	: AgcAlgorithm(controller),
> -	  activeChannels_({ 0 })
> +	  activeChannels_({ 0 }),
> +	  index_(0)

Both initializers fits on a single line

>  {
>  }
>
> @@ -205,20 +206,113 @@ void Agc::setActiveChannels(const std::vector<unsigned int> &activeChannels)
>  void Agc::switchMode(CameraMode const &cameraMode,
>  		     Metadata *metadata)
>  {
> -	LOG(RPiAgc, Debug) << "switchMode for channel 0";
> -	channelData_[0].channel.switchMode(cameraMode, metadata);
> +	/*
> +	 * We run switchMode on every channel, and then we're going to start over
> +	 * with the first active channel again which means that this channel's
> +	 * status needs to be the one we leave in the metadata.
> +	 */
> +	AgcStatus status;
> +
> +	for (unsigned int channelIndex = 0; channelIndex < channelData_.size(); channelIndex++) {
> +		LOG(RPiAgc, Debug) << "switchMode for channel " << channelIndex;
> +		channelData_[channelIndex].channel.switchMode(cameraMode, metadata);
> +		if (channelIndex == activeChannels_[0])
> +			metadata->get("agc.status", status);
> +	}
> +
> +	status.channel = activeChannels_[0];
> +	metadata->set("agc.status", status);
> +	index_ = 0;
> +}
> +
> +static void getChannelIndex(Metadata *metadata, const char *message, unsigned int &channelIndex)
> +{
> +	std::unique_lock<RPiController::Metadata> lock(*metadata);
> +	AgcStatus *status = metadata->getLocked<AgcStatus>("agc.delayed_status");
> +	if (status)
> +		channelIndex = status->channel;
> +	else
> +		/* This does happen at startup, otherwise it would be a Warning or Error. */
> +		LOG(RPiAgc, Debug) << message;
> +}
> +
> +static void setChannelIndex(Metadata *metadata, const char *message, unsigned int channelIndex)
> +{
> +	std::unique_lock<RPiController::Metadata> lock(*metadata);
> +	AgcStatus *status = metadata->getLocked<AgcStatus>("agc.status");
> +	if (status)
> +		status->channel = channelIndex;
> +	else
> +		/* This does happen at startup, otherwise it would be a Warning or Error. */
> +		LOG(RPiAgc, Debug) << message;
>  }

As a nit, get and set channel index operates on the "previous" and
"current" channel respectively. The function name could convey that

>
>  void Agc::prepare(Metadata *imageMetadata)
>  {
> -	LOG(RPiAgc, Debug) << "prepare for channel 0";
> -	channelData_[0].channel.prepare(imageMetadata);
> +	/*
> +	 * The DeviceStatus in the metadata should be correct for the image we
> +	 * are processing. THe delayed status should tell us what channel this frame
> +	 * was from, so we will use that channel's prepare method.

s/THe/The/

> +	 *
> +	 * \todo To be honest, there's not much that's stateful in the prepare methods
> +	 * so we should perhaps re-evaluate whether prepare even needs to be done
> +	 * "per channel".
> +	 */
> +	unsigned int channelIndex = activeChannels_[0];
> +	getChannelIndex(imageMetadata, "prepare: no delayed status", channelIndex);
> +
> +	LOG(RPiAgc, Debug) << "prepare for channel " << channelIndex;
> +	channelData_[channelIndex].channel.prepare(imageMetadata);
>  }
>
>  void Agc::process(StatisticsPtr &stats, Metadata *imageMetadata)
>  {
> -	LOG(RPiAgc, Debug) << "process for channel 0";
> -	channelData_[0].channel.process(stats, imageMetadata);
> +	/*
> +	 * We want to generate values for the next channel in round robin fashion
> +	 * (i.e. the channel at location index_ in the activeChannel list), even though
> +	 * the statistics we have will be for a different channel (which we find
> +	 * again from the delayed status).
> +	 */
> +
> +	/* Generate updated AGC values for this channel: */

s/:/.

You use "this channel" in all comments, but it's not that helpful to
understand which channel you're dealing with

	/* Generate updated AGC values for the currently active channel. */

> +	unsigned int channelIndex = activeChannels_[index_];
> +	AgcChannelData &channelData = channelData_[channelIndex];
> +	/* Stats are from this channel: */

	/* Stats are from the previously active channel: */

> +	unsigned int statsIndex = 0;
> +	getChannelIndex(imageMetadata, "process: no delayed status for stats", statsIndex);
> +	LOG(RPiAgc, Debug) << "process for channel " << channelIndex;
> +
> +	/*
> +	 * We keep a cache of the most recent DeviceStatus and stats for each channel,
> +	 * so that we can invoke the next channel's process method with the most up to date
> +	 * values.
> +	 */
> +	LOG(RPiAgc, Debug) << "Save DeviceStatus and stats for channel " << statsIndex;
> +	DeviceStatus deviceStatus;
> +	if (imageMetadata->get<DeviceStatus>("device.status", deviceStatus) == 0)
> +		channelData_[statsIndex].deviceStatus = deviceStatus;

Where is the deviceStatus of the "previous" channel read ?

It seems to me this function only reads the "current" one below

		deviceStatus = *channelData.deviceStatus;

> +	else
> +		/* Every frame should have a DeviceStatus. */
> +		LOG(RPiAgc, Error) << "process: no device status found";
> +	channelData_[statsIndex].statistics = stats;
> +
> +	/*
> +	 * Finally fetch the most recent DeviceStatus and stats for this channel, if both

         * Finally fetch the most recent DeviceStatus and stats for
         * the current channel channel, if both

> +	 * exist, and call process(). We must make the agc.status metadata record correctly
> +	 * which channel this is.
> +	 */
> +	if (channelData.statistics && channelData.deviceStatus) {
> +		deviceStatus = *channelData.deviceStatus;
> +		stats = channelData.statistics;
> +	} else
> +		/* Can also happen when new channels start. */
> +		LOG(RPiAgc, Debug) << "process: channel " << channelIndex << " not seen yet";

code style would require {} here, but this is IPA code..

> +
> +	channelData.channel.process(stats, &deviceStatus, imageMetadata);

const variable passed by pointer
modifiable variable passed by reference

It's really hard by reading the function signature understanding what
is going to be modified and what won't

Up to you

All minors/questions

Reviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>

Thanks
  j

> +	setChannelIndex(imageMetadata, "process: no AGC status found", channelIndex);
> +
> +	/* And onto the next channel for the next call. */
> +	index_ = (index_ + 1) % activeChannels_.size();
>  }
>
>  /* Register algorithm with the system. */
> diff --git a/src/ipa/rpi/controller/rpi/agc.h b/src/ipa/rpi/controller/rpi/agc.h
> index 24f0a271..ee85c693 100644
> --- a/src/ipa/rpi/controller/rpi/agc.h
> +++ b/src/ipa/rpi/controller/rpi/agc.h
> @@ -18,6 +18,8 @@ namespace RPiController {
>
>  struct AgcChannelData {
>  	AgcChannel channel;
> +	std::optional<DeviceStatus> deviceStatus;
> +	StatisticsPtr statistics;
>  };
>
>  class Agc : public AgcAlgorithm
> @@ -52,6 +54,7 @@ private:
>  	int checkChannel(unsigned int channel) const;
>  	std::vector<AgcChannelData> channelData_;
>  	std::vector<unsigned int> activeChannels_;
> +	unsigned int index_; /* index into the activeChannels_ */
>  };
>
>  } /* namespace RPiController */
> diff --git a/src/ipa/rpi/controller/rpi/agc_channel.cpp b/src/ipa/rpi/controller/rpi/agc_channel.cpp
> index 7c1aba81..f1f19598 100644
> --- a/src/ipa/rpi/controller/rpi/agc_channel.cpp
> +++ b/src/ipa/rpi/controller/rpi/agc_channel.cpp
> @@ -444,7 +444,7 @@ void AgcChannel::prepare(Metadata *imageMetadata)
>  	}
>  }
>
> -void AgcChannel::process(StatisticsPtr &stats, Metadata *imageMetadata)
> +void AgcChannel::process(StatisticsPtr &stats, const DeviceStatus *deviceStatus, Metadata *imageMetadata)
>  {
>  	frameCount_++;
>  	/*
> @@ -455,7 +455,7 @@ void AgcChannel::process(StatisticsPtr &stats, Metadata *imageMetadata)
>  	/* Fetch the AWB status immediately, so that we can assume it's there. */
>  	fetchAwbStatus(imageMetadata);
>  	/* Get the current exposure values for the frame that's just arrived. */
> -	fetchCurrentExposure(imageMetadata);
> +	fetchCurrentExposure(deviceStatus);
>  	/* Compute the total gain we require relative to the current exposure. */
>  	double gain, targetY;
>  	computeGain(stats, imageMetadata, gain, targetY);
> @@ -567,18 +567,13 @@ void AgcChannel::housekeepConfig()
>  			   << meteringModeName_;
>  }
>
> -void AgcChannel::fetchCurrentExposure(Metadata *imageMetadata)
> +void AgcChannel::fetchCurrentExposure(const DeviceStatus *deviceStatus)
>  {
> -	std::unique_lock<Metadata> lock(*imageMetadata);
> -	DeviceStatus *deviceStatus =
> -		imageMetadata->getLocked<DeviceStatus>("device.status");
>  	if (!deviceStatus)
>  		LOG(RPiAgc, Fatal) << "No device metadata";
>  	current_.shutter = deviceStatus->shutterSpeed;
>  	current_.analogueGain = deviceStatus->analogueGain;
> -	AgcStatus *agcStatus =
> -		imageMetadata->getLocked<AgcStatus>("agc.status");
> -	current_.totalExposure = agcStatus ? agcStatus->totalExposureValue : 0s;
> +	current_.totalExposure = 0s; /* this value is unused */
>  	current_.totalExposureNoDG = current_.shutter * current_.analogueGain;
>  }
>
> diff --git a/src/ipa/rpi/controller/rpi/agc_channel.h b/src/ipa/rpi/controller/rpi/agc_channel.h
> index d5a5cf3a..24ee3491 100644
> --- a/src/ipa/rpi/controller/rpi/agc_channel.h
> +++ b/src/ipa/rpi/controller/rpi/agc_channel.h
> @@ -85,13 +85,13 @@ public:
>  	void disableAuto();
>  	void switchMode(CameraMode const &cameraMode, Metadata *metadata);
>  	void prepare(Metadata *imageMetadata);
> -	void process(StatisticsPtr &stats, Metadata *imageMetadata);
> +	void process(StatisticsPtr &stats, const DeviceStatus *deviceStatus, Metadata *imageMetadata);
>
>  private:
>  	bool updateLockStatus(DeviceStatus const &deviceStatus);
>  	AgcConfig config_;
>  	void housekeepConfig();
> -	void fetchCurrentExposure(Metadata *imageMetadata);
> +	void fetchCurrentExposure(const DeviceStatus *deviceStatus);
>  	void fetchAwbStatus(Metadata *imageMetadata);
>  	void computeGain(StatisticsPtr &statistics, Metadata *imageMetadata,
>  			 double &gain, double &targetY);
> --
> 2.30.2
>
David Plowman Sept. 15, 2023, 9 a.m. UTC | #2
Hi Jacopo

Thanks for the comments.

On Tue, 12 Sept 2023 at 16:06, Jacopo Mondi
<jacopo.mondi@ideasonboard.com> wrote:
>
> Hi David
>
> On Tue, Sep 12, 2023 at 11:24:40AM +0100, David Plowman via libcamera-devel wrote:
> > The switchMode, prepare and process methods are updated to implement
> > multi-channel AGC correctly:
> >
> > * switchMode now invokes switchMode on all the channels (whether
> >   active or not).
> >
> > * prepare must find what channel the current frame is, and run on
> >   behalf of that channel.
> >
> > * process updates the most recent DeviceStatus and statistics for the
> >   channel of the frame that has just arrived, but generates updated
> >   values working through the active channels in round-robin fashion.
> >
> > One minor detail in process is that we don't want to change the
> > DeviceStatus metadata of the current frame, so we now pass this to the
> > AgcChannel's process method, rather than letting it find the
> > DeviceStatus in the metadata.
> >
> > Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
> > Reviewed-by: Naushir Patuck <naush@raspberrypi.com>
> > ---
> >  src/ipa/rpi/controller/agc_status.h        |   1 +
> >  src/ipa/rpi/controller/rpi/agc.cpp         | 108 +++++++++++++++++++--
> >  src/ipa/rpi/controller/rpi/agc.h           |   3 +
> >  src/ipa/rpi/controller/rpi/agc_channel.cpp |  13 +--
> >  src/ipa/rpi/controller/rpi/agc_channel.h   |   4 +-
> >  5 files changed, 111 insertions(+), 18 deletions(-)
> >
> > diff --git a/src/ipa/rpi/controller/agc_status.h b/src/ipa/rpi/controller/agc_status.h
> > index 597eddd7..e5c4ee22 100644
> > --- a/src/ipa/rpi/controller/agc_status.h
> > +++ b/src/ipa/rpi/controller/agc_status.h
> > @@ -36,6 +36,7 @@ struct AgcStatus {
> >       int floatingRegionEnable;
> >       libcamera::utils::Duration fixedShutter;
> >       double fixedAnalogueGain;
> > +     unsigned int channel;
> >  };
> >
> >  struct AgcPrepareStatus {
> > diff --git a/src/ipa/rpi/controller/rpi/agc.cpp b/src/ipa/rpi/controller/rpi/agc.cpp
> > index 598fc890..58ba8839 100644
> > --- a/src/ipa/rpi/controller/rpi/agc.cpp
> > +++ b/src/ipa/rpi/controller/rpi/agc.cpp
> > @@ -22,7 +22,8 @@ LOG_DEFINE_CATEGORY(RPiAgc)
> >
> >  Agc::Agc(Controller *controller)
> >       : AgcAlgorithm(controller),
> > -       activeChannels_({ 0 })
> > +       activeChannels_({ 0 }),
> > +       index_(0)
>
> Both initializers fits on a single line

Yes, I can do that.

>
> >  {
> >  }
> >
> > @@ -205,20 +206,113 @@ void Agc::setActiveChannels(const std::vector<unsigned int> &activeChannels)
> >  void Agc::switchMode(CameraMode const &cameraMode,
> >                    Metadata *metadata)
> >  {
> > -     LOG(RPiAgc, Debug) << "switchMode for channel 0";
> > -     channelData_[0].channel.switchMode(cameraMode, metadata);
> > +     /*
> > +      * We run switchMode on every channel, and then we're going to start over
> > +      * with the first active channel again which means that this channel's
> > +      * status needs to be the one we leave in the metadata.
> > +      */
> > +     AgcStatus status;
> > +
> > +     for (unsigned int channelIndex = 0; channelIndex < channelData_.size(); channelIndex++) {
> > +             LOG(RPiAgc, Debug) << "switchMode for channel " << channelIndex;
> > +             channelData_[channelIndex].channel.switchMode(cameraMode, metadata);
> > +             if (channelIndex == activeChannels_[0])
> > +                     metadata->get("agc.status", status);
> > +     }
> > +
> > +     status.channel = activeChannels_[0];
> > +     metadata->set("agc.status", status);
> > +     index_ = 0;
> > +}
> > +
> > +static void getChannelIndex(Metadata *metadata, const char *message, unsigned int &channelIndex)
> > +{
> > +     std::unique_lock<RPiController::Metadata> lock(*metadata);
> > +     AgcStatus *status = metadata->getLocked<AgcStatus>("agc.delayed_status");
> > +     if (status)
> > +             channelIndex = status->channel;
> > +     else
> > +             /* This does happen at startup, otherwise it would be a Warning or Error. */
> > +             LOG(RPiAgc, Debug) << message;
> > +}
> > +
> > +static void setChannelIndex(Metadata *metadata, const char *message, unsigned int channelIndex)
> > +{
> > +     std::unique_lock<RPiController::Metadata> lock(*metadata);
> > +     AgcStatus *status = metadata->getLocked<AgcStatus>("agc.status");
> > +     if (status)
> > +             status->channel = channelIndex;
> > +     else
> > +             /* This does happen at startup, otherwise it would be a Warning or Error. */
> > +             LOG(RPiAgc, Debug) << message;
> >  }
>
> As a nit, get and set channel index operates on the "previous" and
> "current" channel respectively. The function name could convey that

Yes, I'll improve the names.

>
> >
> >  void Agc::prepare(Metadata *imageMetadata)
> >  {
> > -     LOG(RPiAgc, Debug) << "prepare for channel 0";
> > -     channelData_[0].channel.prepare(imageMetadata);
> > +     /*
> > +      * The DeviceStatus in the metadata should be correct for the image we
> > +      * are processing. THe delayed status should tell us what channel this frame
> > +      * was from, so we will use that channel's prepare method.
>
> s/THe/The/

Yep!

>
> > +      *
> > +      * \todo To be honest, there's not much that's stateful in the prepare methods
> > +      * so we should perhaps re-evaluate whether prepare even needs to be done
> > +      * "per channel".
> > +      */
> > +     unsigned int channelIndex = activeChannels_[0];
> > +     getChannelIndex(imageMetadata, "prepare: no delayed status", channelIndex);
> > +
> > +     LOG(RPiAgc, Debug) << "prepare for channel " << channelIndex;
> > +     channelData_[channelIndex].channel.prepare(imageMetadata);
> >  }
> >
> >  void Agc::process(StatisticsPtr &stats, Metadata *imageMetadata)
> >  {
> > -     LOG(RPiAgc, Debug) << "process for channel 0";
> > -     channelData_[0].channel.process(stats, imageMetadata);
> > +     /*
> > +      * We want to generate values for the next channel in round robin fashion
> > +      * (i.e. the channel at location index_ in the activeChannel list), even though
> > +      * the statistics we have will be for a different channel (which we find
> > +      * again from the delayed status).
> > +      */
> > +
> > +     /* Generate updated AGC values for this channel: */
>
> s/:/.

Will do.

>
> You use "this channel" in all comments, but it's not that helpful to
> understand which channel you're dealing with
>
>         /* Generate updated AGC values for the currently active channel. */

I'll try to improve this, thanks.

>
> > +     unsigned int channelIndex = activeChannels_[index_];
> > +     AgcChannelData &channelData = channelData_[channelIndex];
> > +     /* Stats are from this channel: */
>
>         /* Stats are from the previously active channel: */

Here too.

>
> > +     unsigned int statsIndex = 0;
> > +     getChannelIndex(imageMetadata, "process: no delayed status for stats", statsIndex);
> > +     LOG(RPiAgc, Debug) << "process for channel " << channelIndex;
> > +
> > +     /*
> > +      * We keep a cache of the most recent DeviceStatus and stats for each channel,
> > +      * so that we can invoke the next channel's process method with the most up to date
> > +      * values.
> > +      */
> > +     LOG(RPiAgc, Debug) << "Save DeviceStatus and stats for channel " << statsIndex;
> > +     DeviceStatus deviceStatus;
> > +     if (imageMetadata->get<DeviceStatus>("device.status", deviceStatus) == 0)
> > +             channelData_[statsIndex].deviceStatus = deviceStatus;
>
> Where is the deviceStatus of the "previous" channel read ?
>
> It seems to me this function only reads the "current" one below
>
>                 deviceStatus = *channelData.deviceStatus;

So that happens just below where it says "deviceStatus =
*channelData.deviceStatus;"

We have to save the statistics that have just arrived into the
"channel data" that was used to produce that corresponding frame
(which has also just arrived). But then we must use the most recent
statistics and device status from the last time we saw a frame from
the channel for which we're now calculating values.

If that makes sense. Yes, my head hurts a bit!

>
> > +     else
> > +             /* Every frame should have a DeviceStatus. */
> > +             LOG(RPiAgc, Error) << "process: no device status found";
> > +     channelData_[statsIndex].statistics = stats;
> > +
> > +     /*
> > +      * Finally fetch the most recent DeviceStatus and stats for this channel, if both
>
>          * Finally fetch the most recent DeviceStatus and stats for
>          * the current channel channel, if both
>
> > +      * exist, and call process(). We must make the agc.status metadata record correctly
> > +      * which channel this is.
> > +      */
> > +     if (channelData.statistics && channelData.deviceStatus) {
> > +             deviceStatus = *channelData.deviceStatus;
> > +             stats = channelData.statistics;
> > +     } else
> > +             /* Can also happen when new channels start. */
> > +             LOG(RPiAgc, Debug) << "process: channel " << channelIndex << " not seen yet";
>
> code style would require {} here, but this is IPA code..

Indeed. In fact I noticed a couple of other places, so I'll fix those too.

>
> > +
> > +     channelData.channel.process(stats, &deviceStatus, imageMetadata);
>
> const variable passed by pointer
> modifiable variable passed by reference
>
> It's really hard by reading the function signature understanding what
> is going to be modified and what won't
>
> Up to you

Yes, true. Both deviceStatus and imageMetadata are pointers, but maybe
the deviceStatus could be a reference as I think folks prefer const
references and pointers to mutable things?? agc_channel.cpp does
actually check for a non-NULL deviceStatus pointer, but seeing as
agc.cpp can never pass a NULL pointer it seems safe to remove that. So
I'll change it.

>
> All minors/questions
>
> Reviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>

Thanks again for all the reviewing!

David

>
> Thanks
>   j
>
> > +     setChannelIndex(imageMetadata, "process: no AGC status found", channelIndex);
> > +
> > +     /* And onto the next channel for the next call. */
> > +     index_ = (index_ + 1) % activeChannels_.size();
> >  }
> >
> >  /* Register algorithm with the system. */
> > diff --git a/src/ipa/rpi/controller/rpi/agc.h b/src/ipa/rpi/controller/rpi/agc.h
> > index 24f0a271..ee85c693 100644
> > --- a/src/ipa/rpi/controller/rpi/agc.h
> > +++ b/src/ipa/rpi/controller/rpi/agc.h
> > @@ -18,6 +18,8 @@ namespace RPiController {
> >
> >  struct AgcChannelData {
> >       AgcChannel channel;
> > +     std::optional<DeviceStatus> deviceStatus;
> > +     StatisticsPtr statistics;
> >  };
> >
> >  class Agc : public AgcAlgorithm
> > @@ -52,6 +54,7 @@ private:
> >       int checkChannel(unsigned int channel) const;
> >       std::vector<AgcChannelData> channelData_;
> >       std::vector<unsigned int> activeChannels_;
> > +     unsigned int index_; /* index into the activeChannels_ */
> >  };
> >
> >  } /* namespace RPiController */
> > diff --git a/src/ipa/rpi/controller/rpi/agc_channel.cpp b/src/ipa/rpi/controller/rpi/agc_channel.cpp
> > index 7c1aba81..f1f19598 100644
> > --- a/src/ipa/rpi/controller/rpi/agc_channel.cpp
> > +++ b/src/ipa/rpi/controller/rpi/agc_channel.cpp
> > @@ -444,7 +444,7 @@ void AgcChannel::prepare(Metadata *imageMetadata)
> >       }
> >  }
> >
> > -void AgcChannel::process(StatisticsPtr &stats, Metadata *imageMetadata)
> > +void AgcChannel::process(StatisticsPtr &stats, const DeviceStatus *deviceStatus, Metadata *imageMetadata)
> >  {
> >       frameCount_++;
> >       /*
> > @@ -455,7 +455,7 @@ void AgcChannel::process(StatisticsPtr &stats, Metadata *imageMetadata)
> >       /* Fetch the AWB status immediately, so that we can assume it's there. */
> >       fetchAwbStatus(imageMetadata);
> >       /* Get the current exposure values for the frame that's just arrived. */
> > -     fetchCurrentExposure(imageMetadata);
> > +     fetchCurrentExposure(deviceStatus);
> >       /* Compute the total gain we require relative to the current exposure. */
> >       double gain, targetY;
> >       computeGain(stats, imageMetadata, gain, targetY);
> > @@ -567,18 +567,13 @@ void AgcChannel::housekeepConfig()
> >                          << meteringModeName_;
> >  }
> >
> > -void AgcChannel::fetchCurrentExposure(Metadata *imageMetadata)
> > +void AgcChannel::fetchCurrentExposure(const DeviceStatus *deviceStatus)
> >  {
> > -     std::unique_lock<Metadata> lock(*imageMetadata);
> > -     DeviceStatus *deviceStatus =
> > -             imageMetadata->getLocked<DeviceStatus>("device.status");
> >       if (!deviceStatus)
> >               LOG(RPiAgc, Fatal) << "No device metadata";
> >       current_.shutter = deviceStatus->shutterSpeed;
> >       current_.analogueGain = deviceStatus->analogueGain;
> > -     AgcStatus *agcStatus =
> > -             imageMetadata->getLocked<AgcStatus>("agc.status");
> > -     current_.totalExposure = agcStatus ? agcStatus->totalExposureValue : 0s;
> > +     current_.totalExposure = 0s; /* this value is unused */
> >       current_.totalExposureNoDG = current_.shutter * current_.analogueGain;
> >  }
> >
> > diff --git a/src/ipa/rpi/controller/rpi/agc_channel.h b/src/ipa/rpi/controller/rpi/agc_channel.h
> > index d5a5cf3a..24ee3491 100644
> > --- a/src/ipa/rpi/controller/rpi/agc_channel.h
> > +++ b/src/ipa/rpi/controller/rpi/agc_channel.h
> > @@ -85,13 +85,13 @@ public:
> >       void disableAuto();
> >       void switchMode(CameraMode const &cameraMode, Metadata *metadata);
> >       void prepare(Metadata *imageMetadata);
> > -     void process(StatisticsPtr &stats, Metadata *imageMetadata);
> > +     void process(StatisticsPtr &stats, const DeviceStatus *deviceStatus, Metadata *imageMetadata);
> >
> >  private:
> >       bool updateLockStatus(DeviceStatus const &deviceStatus);
> >       AgcConfig config_;
> >       void housekeepConfig();
> > -     void fetchCurrentExposure(Metadata *imageMetadata);
> > +     void fetchCurrentExposure(const DeviceStatus *deviceStatus);
> >       void fetchAwbStatus(Metadata *imageMetadata);
> >       void computeGain(StatisticsPtr &statistics, Metadata *imageMetadata,
> >                        double &gain, double &targetY);
> > --
> > 2.30.2
> >

Patch
diff mbox series

diff --git a/src/ipa/rpi/controller/agc_status.h b/src/ipa/rpi/controller/agc_status.h
index 597eddd7..e5c4ee22 100644
--- a/src/ipa/rpi/controller/agc_status.h
+++ b/src/ipa/rpi/controller/agc_status.h
@@ -36,6 +36,7 @@  struct AgcStatus {
 	int floatingRegionEnable;
 	libcamera::utils::Duration fixedShutter;
 	double fixedAnalogueGain;
+	unsigned int channel;
 };
 
 struct AgcPrepareStatus {
diff --git a/src/ipa/rpi/controller/rpi/agc.cpp b/src/ipa/rpi/controller/rpi/agc.cpp
index 598fc890..58ba8839 100644
--- a/src/ipa/rpi/controller/rpi/agc.cpp
+++ b/src/ipa/rpi/controller/rpi/agc.cpp
@@ -22,7 +22,8 @@  LOG_DEFINE_CATEGORY(RPiAgc)
 
 Agc::Agc(Controller *controller)
 	: AgcAlgorithm(controller),
-	  activeChannels_({ 0 })
+	  activeChannels_({ 0 }),
+	  index_(0)
 {
 }
 
@@ -205,20 +206,113 @@  void Agc::setActiveChannels(const std::vector<unsigned int> &activeChannels)
 void Agc::switchMode(CameraMode const &cameraMode,
 		     Metadata *metadata)
 {
-	LOG(RPiAgc, Debug) << "switchMode for channel 0";
-	channelData_[0].channel.switchMode(cameraMode, metadata);
+	/*
+	 * We run switchMode on every channel, and then we're going to start over
+	 * with the first active channel again which means that this channel's
+	 * status needs to be the one we leave in the metadata.
+	 */
+	AgcStatus status;
+
+	for (unsigned int channelIndex = 0; channelIndex < channelData_.size(); channelIndex++) {
+		LOG(RPiAgc, Debug) << "switchMode for channel " << channelIndex;
+		channelData_[channelIndex].channel.switchMode(cameraMode, metadata);
+		if (channelIndex == activeChannels_[0])
+			metadata->get("agc.status", status);
+	}
+
+	status.channel = activeChannels_[0];
+	metadata->set("agc.status", status);
+	index_ = 0;
+}
+
+static void getChannelIndex(Metadata *metadata, const char *message, unsigned int &channelIndex)
+{
+	std::unique_lock<RPiController::Metadata> lock(*metadata);
+	AgcStatus *status = metadata->getLocked<AgcStatus>("agc.delayed_status");
+	if (status)
+		channelIndex = status->channel;
+	else
+		/* This does happen at startup, otherwise it would be a Warning or Error. */
+		LOG(RPiAgc, Debug) << message;
+}
+
+static void setChannelIndex(Metadata *metadata, const char *message, unsigned int channelIndex)
+{
+	std::unique_lock<RPiController::Metadata> lock(*metadata);
+	AgcStatus *status = metadata->getLocked<AgcStatus>("agc.status");
+	if (status)
+		status->channel = channelIndex;
+	else
+		/* This does happen at startup, otherwise it would be a Warning or Error. */
+		LOG(RPiAgc, Debug) << message;
 }
 
 void Agc::prepare(Metadata *imageMetadata)
 {
-	LOG(RPiAgc, Debug) << "prepare for channel 0";
-	channelData_[0].channel.prepare(imageMetadata);
+	/*
+	 * The DeviceStatus in the metadata should be correct for the image we
+	 * are processing. THe delayed status should tell us what channel this frame
+	 * was from, so we will use that channel's prepare method.
+	 *
+	 * \todo To be honest, there's not much that's stateful in the prepare methods
+	 * so we should perhaps re-evaluate whether prepare even needs to be done
+	 * "per channel".
+	 */
+	unsigned int channelIndex = activeChannels_[0];
+	getChannelIndex(imageMetadata, "prepare: no delayed status", channelIndex);
+
+	LOG(RPiAgc, Debug) << "prepare for channel " << channelIndex;
+	channelData_[channelIndex].channel.prepare(imageMetadata);
 }
 
 void Agc::process(StatisticsPtr &stats, Metadata *imageMetadata)
 {
-	LOG(RPiAgc, Debug) << "process for channel 0";
-	channelData_[0].channel.process(stats, imageMetadata);
+	/*
+	 * We want to generate values for the next channel in round robin fashion
+	 * (i.e. the channel at location index_ in the activeChannel list), even though
+	 * the statistics we have will be for a different channel (which we find
+	 * again from the delayed status).
+	 */
+
+	/* Generate updated AGC values for this channel: */
+	unsigned int channelIndex = activeChannels_[index_];
+	AgcChannelData &channelData = channelData_[channelIndex];
+	/* Stats are from this channel: */
+	unsigned int statsIndex = 0;
+	getChannelIndex(imageMetadata, "process: no delayed status for stats", statsIndex);
+	LOG(RPiAgc, Debug) << "process for channel " << channelIndex;
+
+	/*
+	 * We keep a cache of the most recent DeviceStatus and stats for each channel,
+	 * so that we can invoke the next channel's process method with the most up to date
+	 * values.
+	 */
+	LOG(RPiAgc, Debug) << "Save DeviceStatus and stats for channel " << statsIndex;
+	DeviceStatus deviceStatus;
+	if (imageMetadata->get<DeviceStatus>("device.status", deviceStatus) == 0)
+		channelData_[statsIndex].deviceStatus = deviceStatus;
+	else
+		/* Every frame should have a DeviceStatus. */
+		LOG(RPiAgc, Error) << "process: no device status found";
+	channelData_[statsIndex].statistics = stats;
+
+	/*
+	 * Finally fetch the most recent DeviceStatus and stats for this channel, if both
+	 * exist, and call process(). We must make the agc.status metadata record correctly
+	 * which channel this is.
+	 */
+	if (channelData.statistics && channelData.deviceStatus) {
+		deviceStatus = *channelData.deviceStatus;
+		stats = channelData.statistics;
+	} else
+		/* Can also happen when new channels start. */
+		LOG(RPiAgc, Debug) << "process: channel " << channelIndex << " not seen yet";
+
+	channelData.channel.process(stats, &deviceStatus, imageMetadata);
+	setChannelIndex(imageMetadata, "process: no AGC status found", channelIndex);
+
+	/* And onto the next channel for the next call. */
+	index_ = (index_ + 1) % activeChannels_.size();
 }
 
 /* Register algorithm with the system. */
diff --git a/src/ipa/rpi/controller/rpi/agc.h b/src/ipa/rpi/controller/rpi/agc.h
index 24f0a271..ee85c693 100644
--- a/src/ipa/rpi/controller/rpi/agc.h
+++ b/src/ipa/rpi/controller/rpi/agc.h
@@ -18,6 +18,8 @@  namespace RPiController {
 
 struct AgcChannelData {
 	AgcChannel channel;
+	std::optional<DeviceStatus> deviceStatus;
+	StatisticsPtr statistics;
 };
 
 class Agc : public AgcAlgorithm
@@ -52,6 +54,7 @@  private:
 	int checkChannel(unsigned int channel) const;
 	std::vector<AgcChannelData> channelData_;
 	std::vector<unsigned int> activeChannels_;
+	unsigned int index_; /* index into the activeChannels_ */
 };
 
 } /* namespace RPiController */
diff --git a/src/ipa/rpi/controller/rpi/agc_channel.cpp b/src/ipa/rpi/controller/rpi/agc_channel.cpp
index 7c1aba81..f1f19598 100644
--- a/src/ipa/rpi/controller/rpi/agc_channel.cpp
+++ b/src/ipa/rpi/controller/rpi/agc_channel.cpp
@@ -444,7 +444,7 @@  void AgcChannel::prepare(Metadata *imageMetadata)
 	}
 }
 
-void AgcChannel::process(StatisticsPtr &stats, Metadata *imageMetadata)
+void AgcChannel::process(StatisticsPtr &stats, const DeviceStatus *deviceStatus, Metadata *imageMetadata)
 {
 	frameCount_++;
 	/*
@@ -455,7 +455,7 @@  void AgcChannel::process(StatisticsPtr &stats, Metadata *imageMetadata)
 	/* Fetch the AWB status immediately, so that we can assume it's there. */
 	fetchAwbStatus(imageMetadata);
 	/* Get the current exposure values for the frame that's just arrived. */
-	fetchCurrentExposure(imageMetadata);
+	fetchCurrentExposure(deviceStatus);
 	/* Compute the total gain we require relative to the current exposure. */
 	double gain, targetY;
 	computeGain(stats, imageMetadata, gain, targetY);
@@ -567,18 +567,13 @@  void AgcChannel::housekeepConfig()
 			   << meteringModeName_;
 }
 
-void AgcChannel::fetchCurrentExposure(Metadata *imageMetadata)
+void AgcChannel::fetchCurrentExposure(const DeviceStatus *deviceStatus)
 {
-	std::unique_lock<Metadata> lock(*imageMetadata);
-	DeviceStatus *deviceStatus =
-		imageMetadata->getLocked<DeviceStatus>("device.status");
 	if (!deviceStatus)
 		LOG(RPiAgc, Fatal) << "No device metadata";
 	current_.shutter = deviceStatus->shutterSpeed;
 	current_.analogueGain = deviceStatus->analogueGain;
-	AgcStatus *agcStatus =
-		imageMetadata->getLocked<AgcStatus>("agc.status");
-	current_.totalExposure = agcStatus ? agcStatus->totalExposureValue : 0s;
+	current_.totalExposure = 0s; /* this value is unused */
 	current_.totalExposureNoDG = current_.shutter * current_.analogueGain;
 }
 
diff --git a/src/ipa/rpi/controller/rpi/agc_channel.h b/src/ipa/rpi/controller/rpi/agc_channel.h
index d5a5cf3a..24ee3491 100644
--- a/src/ipa/rpi/controller/rpi/agc_channel.h
+++ b/src/ipa/rpi/controller/rpi/agc_channel.h
@@ -85,13 +85,13 @@  public:
 	void disableAuto();
 	void switchMode(CameraMode const &cameraMode, Metadata *metadata);
 	void prepare(Metadata *imageMetadata);
-	void process(StatisticsPtr &stats, Metadata *imageMetadata);
+	void process(StatisticsPtr &stats, const DeviceStatus *deviceStatus, Metadata *imageMetadata);
 
 private:
 	bool updateLockStatus(DeviceStatus const &deviceStatus);
 	AgcConfig config_;
 	void housekeepConfig();
-	void fetchCurrentExposure(Metadata *imageMetadata);
+	void fetchCurrentExposure(const DeviceStatus *deviceStatus);
 	void fetchAwbStatus(Metadata *imageMetadata);
 	void computeGain(StatisticsPtr &statistics, Metadata *imageMetadata,
 			 double &gain, double &targetY);