[v5,2/7] libcamera: software_isp: Clarify SwStatsCpu::setWindow use
diff mbox series

Message ID 20250929201924.45019-3-mzamazal@redhat.com
State New
Headers show
Series
  • Fix stats related problems in software ISP
Related show

Commit Message

Milan Zamazal Sept. 29, 2025, 8:19 p.m. UTC
The window coordinates passed to SwStatsCpu::setWindow are confusing.
Let's clarify what the coordinates should be.

A source of confusion is that the specified window is relative to the
processed area.  Debayering adjusts line pointers for its processed area
and this is what's also passed to stats processing.  The window passed
to SwStatsCpu::setWindow should either specify the size of the whole
processed (not image) area, or its cropping in case the stats shouldn't
be gathered over the whole processed area.  This patch should clarify
this in the code.

Reviewed-by: Maciej S. Szmigiero <mail@maciej.szmigiero.name>
Reviewed-by: Hans de Goede <hansg@kernel.org>
Signed-off-by: Milan Zamazal <mzamazal@redhat.com>
---
 src/libcamera/software_isp/debayer_cpu.cpp |  6 +++++-
 src/libcamera/software_isp/swstats_cpu.cpp | 24 ++++++++++++++++++++++
 2 files changed, 29 insertions(+), 1 deletion(-)

Comments

Barnabás Pőcze Sept. 30, 2025, 8:40 a.m. UTC | #1
2025. 09. 29. 22:19 keltezéssel, Milan Zamazal írta:
> The window coordinates passed to SwStatsCpu::setWindow are confusing.
> Let's clarify what the coordinates should be.
> 
> A source of confusion is that the specified window is relative to the
> processed area.  Debayering adjusts line pointers for its processed area
> and this is what's also passed to stats processing.  The window passed
> to SwStatsCpu::setWindow should either specify the size of the whole
> processed (not image) area, or its cropping in case the stats shouldn't
> be gathered over the whole processed area.  This patch should clarify
> this in the code.
> 
> Reviewed-by: Maciej S. Szmigiero <mail@maciej.szmigiero.name>
> Reviewed-by: Hans de Goede <hansg@kernel.org>
> Signed-off-by: Milan Zamazal <mzamazal@redhat.com>
> ---
>   src/libcamera/software_isp/debayer_cpu.cpp |  6 +++++-
>   src/libcamera/software_isp/swstats_cpu.cpp | 24 ++++++++++++++++++++++
>   2 files changed, 29 insertions(+), 1 deletion(-)
> 
> diff --git a/src/libcamera/software_isp/debayer_cpu.cpp b/src/libcamera/software_isp/debayer_cpu.cpp
> index 2dc85e5e0..bcaaa5dee 100644
> --- a/src/libcamera/software_isp/debayer_cpu.cpp
> +++ b/src/libcamera/software_isp/debayer_cpu.cpp
> @@ -554,7 +554,11 @@ int DebayerCpu::configure(const StreamConfiguration &inputCfg,
>   	window_.width = outputCfg.size.width;
>   	window_.height = outputCfg.size.height;
> 
> -	/* Don't pass x,y since process() already adjusts src before passing it */
> +	/*
> +	 * Set the stats window to the whole processed window. Its coordinates are
> +	 * relative to the debayered area since debayering passes only the part of
> +	 * data to be processed to the stats; see SwStatsCpu::setWindow.
> +	 */
>   	stats_->setWindow(Rectangle(window_.size()));
> 
>   	/* pad with patternSize.Width on both left and right side */
> diff --git a/src/libcamera/software_isp/swstats_cpu.cpp b/src/libcamera/software_isp/swstats_cpu.cpp
> index e8a1d52f2..4031c1a9e 100644
> --- a/src/libcamera/software_isp/swstats_cpu.cpp
> +++ b/src/libcamera/software_isp/swstats_cpu.cpp
> @@ -416,9 +416,33 @@ int SwStatsCpu::configure(const StreamConfiguration &inputCfg)
>   /**
>    * \brief Specify window coordinates over which to gather statistics
>    * \param[in] window The window object.
> + *
> + * This method specifies the image area over which to gather the statistics.
> + * It must be called to set the area, otherwise the default zero-sized
> + * \a Rectangle is used and no statistics is gathered.
> + *
> + * The specified \a window is relative to what is passed to the processLine*
> + * methods. For example, if statistics are to be gathered from the entire
> + * processed area, then \a window should be a rectangle with the top-left corner
> + * of (0,0) and the same size as the processed area. If only a part of the
> + * processed area (e.g. its centre) is to be considered for statistics, then
> + * \a window should specify such a restriction accordingly.
> + *
> + * It is the responsibility of the callers to provide sensible \a window values,
> + * most notably not exceeding the original image boundaries. This means, among
> + * other, that neither coordinate of the top-left corner shall be negative.
> + *
> + * Due to limitations of the implementation, the method may adjust the window
> + * slightly if it is not aligned according to the bayer pattern determined in
> + * \a SwStatsCpu::configure(). In that case the window will be modified such that
> + * the sides are no larger than the original, and that the new bottom-left

Ahhhhh.... it should be "bottom-right" sorry.


Reviewed-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>


> + * corner will be no further from (0,0) (along either axis) than the original
> + * was.
>    */
>   void SwStatsCpu::setWindow(const Rectangle &window)
>   {
> +	ASSERT(window.x >= 0 && window.y >= 0);
> +
>   	window_ = window;
> 
>   	window_.x &= ~(patternSize_.width - 1);
> --
> 2.51.0
>

Patch
diff mbox series

diff --git a/src/libcamera/software_isp/debayer_cpu.cpp b/src/libcamera/software_isp/debayer_cpu.cpp
index 2dc85e5e0..bcaaa5dee 100644
--- a/src/libcamera/software_isp/debayer_cpu.cpp
+++ b/src/libcamera/software_isp/debayer_cpu.cpp
@@ -554,7 +554,11 @@  int DebayerCpu::configure(const StreamConfiguration &inputCfg,
 	window_.width = outputCfg.size.width;
 	window_.height = outputCfg.size.height;
 
-	/* Don't pass x,y since process() already adjusts src before passing it */
+	/*
+	 * Set the stats window to the whole processed window. Its coordinates are
+	 * relative to the debayered area since debayering passes only the part of
+	 * data to be processed to the stats; see SwStatsCpu::setWindow.
+	 */
 	stats_->setWindow(Rectangle(window_.size()));
 
 	/* pad with patternSize.Width on both left and right side */
diff --git a/src/libcamera/software_isp/swstats_cpu.cpp b/src/libcamera/software_isp/swstats_cpu.cpp
index e8a1d52f2..4031c1a9e 100644
--- a/src/libcamera/software_isp/swstats_cpu.cpp
+++ b/src/libcamera/software_isp/swstats_cpu.cpp
@@ -416,9 +416,33 @@  int SwStatsCpu::configure(const StreamConfiguration &inputCfg)
 /**
  * \brief Specify window coordinates over which to gather statistics
  * \param[in] window The window object.
+ *
+ * This method specifies the image area over which to gather the statistics.
+ * It must be called to set the area, otherwise the default zero-sized
+ * \a Rectangle is used and no statistics is gathered.
+ *
+ * The specified \a window is relative to what is passed to the processLine*
+ * methods. For example, if statistics are to be gathered from the entire
+ * processed area, then \a window should be a rectangle with the top-left corner
+ * of (0,0) and the same size as the processed area. If only a part of the
+ * processed area (e.g. its centre) is to be considered for statistics, then
+ * \a window should specify such a restriction accordingly.
+ *
+ * It is the responsibility of the callers to provide sensible \a window values,
+ * most notably not exceeding the original image boundaries. This means, among
+ * other, that neither coordinate of the top-left corner shall be negative.
+ *
+ * Due to limitations of the implementation, the method may adjust the window
+ * slightly if it is not aligned according to the bayer pattern determined in
+ * \a SwStatsCpu::configure(). In that case the window will be modified such that
+ * the sides are no larger than the original, and that the new bottom-left
+ * corner will be no further from (0,0) (along either axis) than the original
+ * was.
  */
 void SwStatsCpu::setWindow(const Rectangle &window)
 {
+	ASSERT(window.x >= 0 && window.y >= 0);
+
 	window_ = window;
 
 	window_.x &= ~(patternSize_.width - 1);