<div dir="ltr"><div dir="ltr">Hi Laurent,<div><br></div><div>Thank you for your feedback.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, 8 May 2021 at 01:30, 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, May 07, 2021 at 09:40:42AM +0100, Naushir Patuck wrote:<br>
> The controller algorithms currently run on every frame provided to the<br>
> IPA by the pipeline handler. This may be undesirable for very fast fps<br>
> operating modes where it could significantly increase the computation<br>
> cycles (per unit time) without providing any significant changes to the<br>
> IQ parameters. The added latencies could also cause dropped frames.<br>
> <br>
> Pass the FrameBuffer timestamp to the IPA through the controls. This<br>
> timestamp will be used to rate-limit the controller algorithms to run<br>
> with a minimum inter-frame time given by a compile time constant,<br>
> currently set to 16.66ms. On startup, we don't rate-limit the algorithms<br>
> until after the number of frames required for convergence.<br>
> <br>
> Signed-off-by: Naushir Patuck <<a href="mailto:naush@raspberrypi.com" target="_blank">naush@raspberrypi.com</a>><br>
> ---<br>
> src/ipa/raspberrypi/raspberrypi.cpp | 48 +++++++++++++++++--<br>
> .../pipeline/raspberrypi/raspberrypi.cpp | 5 ++<br>
> 2 files changed, 49 insertions(+), 4 deletions(-)<br>
> <br>
> diff --git a/src/ipa/raspberrypi/raspberrypi.cpp b/src/ipa/raspberrypi/raspberrypi.cpp<br>
> index f6d1ab16a290..57f5497a6e6b 100644<br>
> --- a/src/ipa/raspberrypi/raspberrypi.cpp<br>
> +++ b/src/ipa/raspberrypi/raspberrypi.cpp<br>
> @@ -61,6 +61,14 @@ constexpr unsigned int DefaultExposureTime = 20000;<br>
> constexpr double defaultMinFrameDuration = 1e6 / 30.0;<br>
> constexpr double defaultMaxFrameDuration = 1e6 / 0.01;<br>
> <br>
> +/*<br>
> + * Determine the minimum allowable inter-frame duration (in us) to run the<br>
> + * controller algorithms. If the pipeline handler provider frames at a rate<br>
> + * higher than this, we rate-limit the controller Prepare() and Process() calls<br>
> + * to lower than or equal to this rate.<br>
> + */<br>
> +constexpr double controllerMinFrameDuration = 1e6 / 60.0;<br>
> +<br>
> LOG_DEFINE_CATEGORY(IPARPI)<br>
> <br>
> class IPARPi : public ipa::RPi::IPARPiInterface<br>
> @@ -68,7 +76,7 @@ class IPARPi : public ipa::RPi::IPARPiInterface<br>
> public:<br>
> IPARPi()<br>
> : controller_(), frameCount_(0), checkCount_(0), mistrustCount_(0),<br>
> - lsTable_(nullptr), firstStart_(true)<br>
> + lastRunTimestamp_(0), lsTable_(nullptr), firstStart_(true)<br>
> {<br>
> }<br>
> <br>
> @@ -146,6 +154,12 @@ private:<br>
> /* Number of frames that need to be dropped on startup. */<br>
> unsigned int dropFrameCount_;<br>
> <br>
> + /* Frame timestamp for the last run of the controller. */<br>
> + uint64_t lastRunTimestamp_;<br>
> +<br>
> + /* Do we run a Controller::process() for this frame? */<br>
> + bool processPending_;<br>
> +<br>
> /* LS table allocation passed in from the pipeline handler. */<br>
> FileDescriptor lsTableHandle_;<br>
> void *lsTable_;<br>
> @@ -262,6 +276,7 @@ void IPARPi::start(const ControlList &controls, ipa::RPi::StartConfig *startConf<br>
> startConfig->dropFrameCount = dropFrameCount_;<br>
> <br>
> firstStart_ = false;<br>
> + lastRunTimestamp_ = 0;<br>
> }<br>
> <br>
> void IPARPi::setMode(const CameraSensorInfo &sensorInfo)<br>
> @@ -406,7 +421,7 @@ void IPARPi::signalStatReady(uint32_t bufferId)<br>
> {<br>
> if (++checkCount_ != frameCount_) /* assert here? */<br>
> LOG(IPARPI, Error) << "WARNING: Prepare/Process mismatch!!!";<br>
> - if (frameCount_ > mistrustCount_)<br>
> + if (processPending_ && frameCount_ > mistrustCount_)<br>
> processStats(bufferId);<br>
> <br>
> reportMetadata();<br>
> @@ -894,10 +909,11 @@ void IPARPi::returnEmbeddedBuffer(unsigned int bufferId)<br>
> <br>
> void IPARPi::prepareISP(const ipa::RPi::ISPConfig &data)<br>
> {<br>
> + int64_t frameTimestamp = data.controls.get(controls::draft::SensorTimestamp);<br>
> + RPiController::Metadata lastMetadata;<br>
> Span<uint8_t> embeddedBuffer;<br>
> <br>
> - rpiMetadata_.Clear();<br>
> -<br>
> + lastMetadata = std::move(rpiMetadata_);<br>
> fillDeviceStatus(data.controls);<br>
> <br>
> if (data.embeddedBufferPresent) {<br>
> @@ -920,6 +936,30 @@ void IPARPi::prepareISP(const ipa::RPi::ISPConfig &data)<br>
> if (data.embeddedBufferPresent)<br>
> returnEmbeddedBuffer(data.embeddedBufferId);<br>
> <br>
> + /* Allow a 10% margin on the comparison below. */<br>
> + constexpr double eps = controllerMinFrameDuration * 1e3 * 0.1;<br>
> + if (lastRunTimestamp_ && frameCount_ > dropFrameCount_ &&<br>
> + frameTimestamp - lastRunTimestamp_ + eps < controllerMinFrameDuration * 1e3) {<br>
<br>
I would likely have written this<br>
<br>
if (lastRunTimestamp_ && frameCount_ > dropFrameCount_ &&<br>
frameTimestamp - lastRunTimestamp_ + eps < controllerMinFrameDuration * 1e3 * 0.9) {<br>
<br>
but it doesn't matter much.<br>
<br>
> + /*<br>
> + * Ensure we update the controller metadata with the new frame's<br>
> + * exposure/gain values so that the correct values are returned<br>
> + * out in libcamera metadata later on. Any other bits of metadata<br>
> + * that were added in helper_->Prepare() will also be moved across.<br>
> + * All other metadata values must remain the same as the last frame.<br>
> + *<br>
> + * We do this by overwriting the current frame's metadata into the<br>
> + * previous frame's metadata object, and then swapping the latter<br>
<br>
"overwriting ... into" sounds weird. If you can provide an updated text,<br>
I can fix when applying.<br>
<br>
> + * with the former.<br>
> + */<br>
> + lastMetadata.Overwrite(rpiMetadata_);<br>
> + std::swap(rpiMetadata_, lastMetadata);<br>
<br>
This could have been written<br>
<br>
rpiMetadata_.Merge(lastMetadata);<br>
<br>
without a swap, with std::map::merge semantics for Metadata::Merge() :-)<br>
The double swap seems a bit cumbersome.<br>
<br>
If you're happy with the current implementation though, that's fine with<br>
me. In either case,<br></blockquote><div><br></div><div>You are right, a Metadata::Merge() would be a less cumbersome way to do this.</div><div>I will rework this (and the previous) patch to add that and resubmit a v7.</div><div><br></div><div>Thanks,</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>
Reviewed-by: Laurent Pinchart <<a href="mailto:laurent.pinchart@ideasonboard.com" target="_blank">laurent.pinchart@ideasonboard.com</a>><br>
<br>
If you think Merge() would be cleaner, I'll merge v7.<br>
<br>
> + processPending_ = false;<br>
> + return;<br>
> + }<br>
> +<br>
> + lastRunTimestamp_ = frameTimestamp;<br>
> + processPending_ = true;<br>
> +<br>
> ControlList ctrls(ispCtrls_);<br>
> <br>
> controller_.Prepare(&rpiMetadata_);<br>
> diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp<br>
> index 2a917455500f..9cf9c8c6cebd 100644<br>
> --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp<br>
> +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp<br>
> @@ -1414,6 +1414,11 @@ void RPiCameraData::unicamBufferDequeue(FrameBuffer *buffer)<br>
> * DelayedControl and queue them along with the frame buffer.<br>
> */<br>
> ControlList ctrl = delayedCtrls_->get(buffer->metadata().sequence);<br>
> + /*<br>
> + * Add the frame timestamp to the ControlList for the IPA to use<br>
> + * as it does not receive the FrameBuffer object.<br>
> + */<br>
> + ctrl.set(controls::draft::SensorTimestamp, buffer->metadata().timestamp);<br>
> bayerQueue_.push({ buffer, std::move(ctrl) });<br>
> } else {<br>
> embeddedQueue_.push(buffer);<br>
<br>
-- <br>
Regards,<br>
<br>
Laurent Pinchart<br>
</blockquote></div></div>