[libcamera-devel,2/2] libcamera: Use utils::abs_diff()
diff mbox series

Message ID 20211206152644.4863-3-laurent.pinchart@ideasonboard.com
State Accepted
Commit e7888073718aadd7c35a78f88792a73a4e3a8300
Delegated to: Laurent Pinchart
Headers show
Series
  • libcamera: Introduce utils::abs_diff()
Related show

Commit Message

Laurent Pinchart Dec. 6, 2021, 3:26 p.m. UTC
Use the new utils::abs_diff() function where appropriate to replace
manual implementations.

While at it fix a header ordering issue in
src/libcamera/pipeline/raspberrypi/raspberrypi.cpp.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 src/ipa/ipu3/algorithms/agc.cpp                    | 3 ++-
 src/ipa/ipu3/ipu3.cpp                              | 4 ++--
 src/ipa/rkisp1/algorithms/agc.cpp                  | 3 ++-
 src/libcamera/pipeline/ipu3/imgu.cpp               | 4 ++--
 src/libcamera/pipeline/raspberrypi/raspberrypi.cpp | 5 ++---
 5 files changed, 10 insertions(+), 9 deletions(-)

Comments

Kieran Bingham Dec. 6, 2021, 4:20 p.m. UTC | #1
Quoting Laurent Pinchart (2021-12-06 15:26:44)
> Use the new utils::abs_diff() function where appropriate to replace
> manual implementations.
> 
> While at it fix a header ordering issue in
> src/libcamera/pipeline/raspberrypi/raspberrypi.cpp.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Much tidier, and clearer (and ... safer?!)

Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

> ---
>  src/ipa/ipu3/algorithms/agc.cpp                    | 3 ++-
>  src/ipa/ipu3/ipu3.cpp                              | 4 ++--
>  src/ipa/rkisp1/algorithms/agc.cpp                  | 3 ++-
>  src/libcamera/pipeline/ipu3/imgu.cpp               | 4 ++--
>  src/libcamera/pipeline/raspberrypi/raspberrypi.cpp | 5 ++---
>  5 files changed, 10 insertions(+), 9 deletions(-)
> 
> diff --git a/src/ipa/ipu3/algorithms/agc.cpp b/src/ipa/ipu3/algorithms/agc.cpp
> index 582f0ae1b54b..8d6f18f60aed 100644
> --- a/src/ipa/ipu3/algorithms/agc.cpp
> +++ b/src/ipa/ipu3/algorithms/agc.cpp
> @@ -12,6 +12,7 @@
>  #include <cmath>
>  
>  #include <libcamera/base/log.h>
> +#include <libcamera/base/utils.h>
>  
>  #include <libcamera/ipa/core_ipa_interface.h>
>  
> @@ -188,7 +189,7 @@ void Agc::computeExposure(IPAFrameContext &frameContext, double yGain,
>         double evGain = std::max(yGain, iqMeanGain);
>  
>         /* Consider within 1% of the target as correctly exposed */
> -       if (std::abs(evGain - 1.0) < 0.01)
> +       if (utils::abs_diff(evGain, 1.0) < 0.01)
>                 LOG(IPU3Agc, Debug) << "We are well exposed (evGain = "
>                                     << evGain << ")";
>  
> diff --git a/src/ipa/ipu3/ipu3.cpp b/src/ipa/ipu3/ipu3.cpp
> index 7618258725a8..3d30770867b9 100644
> --- a/src/ipa/ipu3/ipu3.cpp
> +++ b/src/ipa/ipu3/ipu3.cpp
> @@ -354,7 +354,7 @@ void IPAIPU3::calculateBdsGrid(const Size &bdsOutputSize)
>                                             kMaxGridWidth);
>  
>                 width = width << shift;
> -               uint32_t error = std::abs(static_cast<int>(width - bdsOutputSize.width));
> +               uint32_t error = utils::abs_diff(width, bdsOutputSize.width);
>                 if (error >= minError)
>                         continue;
>  
> @@ -370,7 +370,7 @@ void IPAIPU3::calculateBdsGrid(const Size &bdsOutputSize)
>                                              kMaxGridHeight);
>  
>                 height = height << shift;
> -               uint32_t error = std::abs(static_cast<int>(height - bdsOutputSize.height));
> +               uint32_t error = utils::abs_diff(height, bdsOutputSize.height);
>                 if (error >= minError)
>                         continue;
>  
> diff --git a/src/ipa/rkisp1/algorithms/agc.cpp b/src/ipa/rkisp1/algorithms/agc.cpp
> index d6abdc310f35..dd97afc00813 100644
> --- a/src/ipa/rkisp1/algorithms/agc.cpp
> +++ b/src/ipa/rkisp1/algorithms/agc.cpp
> @@ -12,6 +12,7 @@
>  #include <cmath>
>  
>  #include <libcamera/base/log.h>
> +#include <libcamera/base/utils.h>
>  
>  #include <libcamera/ipa/core_ipa_interface.h>
>  
> @@ -145,7 +146,7 @@ void Agc::computeExposure(IPAContext &context, double yGain)
>                                           kMaxAnalogueGain);
>  
>         /* Consider within 1% of the target as correctly exposed. */
> -       if (std::abs(yGain - 1.0) < 0.01)
> +       if (utils::abs_diff(yGain, 1.0) < 0.01)
>                 return;
>  
>         /* extracted from Rpi::Agc::computeTargetExposure. */
> diff --git a/src/libcamera/pipeline/ipu3/imgu.cpp b/src/libcamera/pipeline/ipu3/imgu.cpp
> index 3ef0ef144301..e5bbc3829c68 100644
> --- a/src/libcamera/pipeline/ipu3/imgu.cpp
> +++ b/src/libcamera/pipeline/ipu3/imgu.cpp
> @@ -81,7 +81,7 @@ float findScaleFactor(float sf, const std::vector<float> &range,
>         float bestDiff = std::numeric_limits<float>::max();
>         unsigned int index = 0;
>         for (unsigned int i = 0; i < range.size(); ++i) {
> -               float diff = std::abs(sf - range[i]);
> +               float diff = utils::abs_diff(sf, range[i]);
>                 if (diff < bestDiff) {
>                         bestDiff = diff;
>                         index = i;
> @@ -99,7 +99,7 @@ bool isSameRatio(const Size &in, const Size &out)
>         float inRatio = static_cast<float>(in.width) / in.height;
>         float outRatio = static_cast<float>(out.width) / out.height;
>  
> -       if (std::abs(inRatio - outRatio) > 0.1)
> +       if (utils::abs_diff(inRatio, outRatio) > 0.1)
>                 return false;
>  
>         return true;
> diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp
> index 321b72adbbf7..2cb0267ccf4c 100644
> --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp
> +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp
> @@ -14,6 +14,7 @@
>  #include <unordered_set>
>  
>  #include <libcamera/base/shared_fd.h>
> +#include <libcamera/base/utils.h>
>  
>  #include <libcamera/camera.h>
>  #include <libcamera/control_ids.h>
> @@ -25,8 +26,6 @@
>  #include <libcamera/property_ids.h>
>  #include <libcamera/request.h>
>  
> -#include <libcamera/base/utils.h>
> -
>  #include <linux/bcm2835-isp.h>
>  #include <linux/media-bus-format.h>
>  #include <linux/videodev2.h>
> @@ -154,7 +153,7 @@ V4L2SubdeviceFormat findBestFormat(const SensorFormats &formatsMap, const Size &
>                         score += penaltyAr * scoreFormat(reqAr, fmtAr);
>  
>                         /* Add any penalties... this is not an exact science! */
> -                       score += std::abs(static_cast<int>(info.bitsPerPixel - bitDepth)) * penaltyBitDepth;
> +                       score += utils::abs_diff(info.bitsPerPixel, bitDepth) * penaltyBitDepth;
>  
>                         if (score <= bestScore) {
>                                 bestScore = score;
> -- 
> Regards,
> 
> Laurent Pinchart
>
Umang Jain Dec. 7, 2021, 4:11 p.m. UTC | #2
Hi Laurent,

Thank you for the patch

On 12/6/21 8:56 PM, Laurent Pinchart wrote:
> Use the new utils::abs_diff() function where appropriate to replace
> manual implementations.
>
> While at it fix a header ordering issue in
> src/libcamera/pipeline/raspberrypi/raspberrypi.cpp.
>
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>


Reviewed-by: Umang Jain <umang.jain@ideasonboard.com>

> ---
>   src/ipa/ipu3/algorithms/agc.cpp                    | 3 ++-
>   src/ipa/ipu3/ipu3.cpp                              | 4 ++--
>   src/ipa/rkisp1/algorithms/agc.cpp                  | 3 ++-
>   src/libcamera/pipeline/ipu3/imgu.cpp               | 4 ++--
>   src/libcamera/pipeline/raspberrypi/raspberrypi.cpp | 5 ++---
>   5 files changed, 10 insertions(+), 9 deletions(-)
>
> diff --git a/src/ipa/ipu3/algorithms/agc.cpp b/src/ipa/ipu3/algorithms/agc.cpp
> index 582f0ae1b54b..8d6f18f60aed 100644
> --- a/src/ipa/ipu3/algorithms/agc.cpp
> +++ b/src/ipa/ipu3/algorithms/agc.cpp
> @@ -12,6 +12,7 @@
>   #include <cmath>
>   
>   #include <libcamera/base/log.h>
> +#include <libcamera/base/utils.h>
>   
>   #include <libcamera/ipa/core_ipa_interface.h>
>   
> @@ -188,7 +189,7 @@ void Agc::computeExposure(IPAFrameContext &frameContext, double yGain,
>   	double evGain = std::max(yGain, iqMeanGain);
>   
>   	/* Consider within 1% of the target as correctly exposed */
> -	if (std::abs(evGain - 1.0) < 0.01)
> +	if (utils::abs_diff(evGain, 1.0) < 0.01)
>   		LOG(IPU3Agc, Debug) << "We are well exposed (evGain = "
>   				    << evGain << ")";
>   
> diff --git a/src/ipa/ipu3/ipu3.cpp b/src/ipa/ipu3/ipu3.cpp
> index 7618258725a8..3d30770867b9 100644
> --- a/src/ipa/ipu3/ipu3.cpp
> +++ b/src/ipa/ipu3/ipu3.cpp
> @@ -354,7 +354,7 @@ void IPAIPU3::calculateBdsGrid(const Size &bdsOutputSize)
>   					    kMaxGridWidth);
>   
>   		width = width << shift;
> -		uint32_t error = std::abs(static_cast<int>(width - bdsOutputSize.width));
> +		uint32_t error = utils::abs_diff(width, bdsOutputSize.width);
>   		if (error >= minError)
>   			continue;
>   
> @@ -370,7 +370,7 @@ void IPAIPU3::calculateBdsGrid(const Size &bdsOutputSize)
>   					     kMaxGridHeight);
>   
>   		height = height << shift;
> -		uint32_t error = std::abs(static_cast<int>(height - bdsOutputSize.height));
> +		uint32_t error = utils::abs_diff(height, bdsOutputSize.height);
>   		if (error >= minError)
>   			continue;
>   
> diff --git a/src/ipa/rkisp1/algorithms/agc.cpp b/src/ipa/rkisp1/algorithms/agc.cpp
> index d6abdc310f35..dd97afc00813 100644
> --- a/src/ipa/rkisp1/algorithms/agc.cpp
> +++ b/src/ipa/rkisp1/algorithms/agc.cpp
> @@ -12,6 +12,7 @@
>   #include <cmath>
>   
>   #include <libcamera/base/log.h>
> +#include <libcamera/base/utils.h>
>   
>   #include <libcamera/ipa/core_ipa_interface.h>
>   
> @@ -145,7 +146,7 @@ void Agc::computeExposure(IPAContext &context, double yGain)
>   					  kMaxAnalogueGain);
>   
>   	/* Consider within 1% of the target as correctly exposed. */
> -	if (std::abs(yGain - 1.0) < 0.01)
> +	if (utils::abs_diff(yGain, 1.0) < 0.01)
>   		return;
>   
>   	/* extracted from Rpi::Agc::computeTargetExposure. */
> diff --git a/src/libcamera/pipeline/ipu3/imgu.cpp b/src/libcamera/pipeline/ipu3/imgu.cpp
> index 3ef0ef144301..e5bbc3829c68 100644
> --- a/src/libcamera/pipeline/ipu3/imgu.cpp
> +++ b/src/libcamera/pipeline/ipu3/imgu.cpp
> @@ -81,7 +81,7 @@ float findScaleFactor(float sf, const std::vector<float> &range,
>   	float bestDiff = std::numeric_limits<float>::max();
>   	unsigned int index = 0;
>   	for (unsigned int i = 0; i < range.size(); ++i) {
> -		float diff = std::abs(sf - range[i]);
> +		float diff = utils::abs_diff(sf, range[i]);
>   		if (diff < bestDiff) {
>   			bestDiff = diff;
>   			index = i;
> @@ -99,7 +99,7 @@ bool isSameRatio(const Size &in, const Size &out)
>   	float inRatio = static_cast<float>(in.width) / in.height;
>   	float outRatio = static_cast<float>(out.width) / out.height;
>   
> -	if (std::abs(inRatio - outRatio) > 0.1)
> +	if (utils::abs_diff(inRatio, outRatio) > 0.1)
>   		return false;
>   
>   	return true;
> diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp
> index 321b72adbbf7..2cb0267ccf4c 100644
> --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp
> +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp
> @@ -14,6 +14,7 @@
>   #include <unordered_set>
>   
>   #include <libcamera/base/shared_fd.h>
> +#include <libcamera/base/utils.h>
>   
>   #include <libcamera/camera.h>
>   #include <libcamera/control_ids.h>
> @@ -25,8 +26,6 @@
>   #include <libcamera/property_ids.h>
>   #include <libcamera/request.h>
>   
> -#include <libcamera/base/utils.h>
> -
>   #include <linux/bcm2835-isp.h>
>   #include <linux/media-bus-format.h>
>   #include <linux/videodev2.h>
> @@ -154,7 +153,7 @@ V4L2SubdeviceFormat findBestFormat(const SensorFormats &formatsMap, const Size &
>   			score += penaltyAr * scoreFormat(reqAr, fmtAr);
>   
>   			/* Add any penalties... this is not an exact science! */
> -			score += std::abs(static_cast<int>(info.bitsPerPixel - bitDepth)) * penaltyBitDepth;
> +			score += utils::abs_diff(info.bitsPerPixel, bitDepth) * penaltyBitDepth;
>   
>   			if (score <= bestScore) {
>   				bestScore = score;

Patch
diff mbox series

diff --git a/src/ipa/ipu3/algorithms/agc.cpp b/src/ipa/ipu3/algorithms/agc.cpp
index 582f0ae1b54b..8d6f18f60aed 100644
--- a/src/ipa/ipu3/algorithms/agc.cpp
+++ b/src/ipa/ipu3/algorithms/agc.cpp
@@ -12,6 +12,7 @@ 
 #include <cmath>
 
 #include <libcamera/base/log.h>
+#include <libcamera/base/utils.h>
 
 #include <libcamera/ipa/core_ipa_interface.h>
 
@@ -188,7 +189,7 @@  void Agc::computeExposure(IPAFrameContext &frameContext, double yGain,
 	double evGain = std::max(yGain, iqMeanGain);
 
 	/* Consider within 1% of the target as correctly exposed */
-	if (std::abs(evGain - 1.0) < 0.01)
+	if (utils::abs_diff(evGain, 1.0) < 0.01)
 		LOG(IPU3Agc, Debug) << "We are well exposed (evGain = "
 				    << evGain << ")";
 
diff --git a/src/ipa/ipu3/ipu3.cpp b/src/ipa/ipu3/ipu3.cpp
index 7618258725a8..3d30770867b9 100644
--- a/src/ipa/ipu3/ipu3.cpp
+++ b/src/ipa/ipu3/ipu3.cpp
@@ -354,7 +354,7 @@  void IPAIPU3::calculateBdsGrid(const Size &bdsOutputSize)
 					    kMaxGridWidth);
 
 		width = width << shift;
-		uint32_t error = std::abs(static_cast<int>(width - bdsOutputSize.width));
+		uint32_t error = utils::abs_diff(width, bdsOutputSize.width);
 		if (error >= minError)
 			continue;
 
@@ -370,7 +370,7 @@  void IPAIPU3::calculateBdsGrid(const Size &bdsOutputSize)
 					     kMaxGridHeight);
 
 		height = height << shift;
-		uint32_t error = std::abs(static_cast<int>(height - bdsOutputSize.height));
+		uint32_t error = utils::abs_diff(height, bdsOutputSize.height);
 		if (error >= minError)
 			continue;
 
diff --git a/src/ipa/rkisp1/algorithms/agc.cpp b/src/ipa/rkisp1/algorithms/agc.cpp
index d6abdc310f35..dd97afc00813 100644
--- a/src/ipa/rkisp1/algorithms/agc.cpp
+++ b/src/ipa/rkisp1/algorithms/agc.cpp
@@ -12,6 +12,7 @@ 
 #include <cmath>
 
 #include <libcamera/base/log.h>
+#include <libcamera/base/utils.h>
 
 #include <libcamera/ipa/core_ipa_interface.h>
 
@@ -145,7 +146,7 @@  void Agc::computeExposure(IPAContext &context, double yGain)
 					  kMaxAnalogueGain);
 
 	/* Consider within 1% of the target as correctly exposed. */
-	if (std::abs(yGain - 1.0) < 0.01)
+	if (utils::abs_diff(yGain, 1.0) < 0.01)
 		return;
 
 	/* extracted from Rpi::Agc::computeTargetExposure. */
diff --git a/src/libcamera/pipeline/ipu3/imgu.cpp b/src/libcamera/pipeline/ipu3/imgu.cpp
index 3ef0ef144301..e5bbc3829c68 100644
--- a/src/libcamera/pipeline/ipu3/imgu.cpp
+++ b/src/libcamera/pipeline/ipu3/imgu.cpp
@@ -81,7 +81,7 @@  float findScaleFactor(float sf, const std::vector<float> &range,
 	float bestDiff = std::numeric_limits<float>::max();
 	unsigned int index = 0;
 	for (unsigned int i = 0; i < range.size(); ++i) {
-		float diff = std::abs(sf - range[i]);
+		float diff = utils::abs_diff(sf, range[i]);
 		if (diff < bestDiff) {
 			bestDiff = diff;
 			index = i;
@@ -99,7 +99,7 @@  bool isSameRatio(const Size &in, const Size &out)
 	float inRatio = static_cast<float>(in.width) / in.height;
 	float outRatio = static_cast<float>(out.width) / out.height;
 
-	if (std::abs(inRatio - outRatio) > 0.1)
+	if (utils::abs_diff(inRatio, outRatio) > 0.1)
 		return false;
 
 	return true;
diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp
index 321b72adbbf7..2cb0267ccf4c 100644
--- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp
+++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp
@@ -14,6 +14,7 @@ 
 #include <unordered_set>
 
 #include <libcamera/base/shared_fd.h>
+#include <libcamera/base/utils.h>
 
 #include <libcamera/camera.h>
 #include <libcamera/control_ids.h>
@@ -25,8 +26,6 @@ 
 #include <libcamera/property_ids.h>
 #include <libcamera/request.h>
 
-#include <libcamera/base/utils.h>
-
 #include <linux/bcm2835-isp.h>
 #include <linux/media-bus-format.h>
 #include <linux/videodev2.h>
@@ -154,7 +153,7 @@  V4L2SubdeviceFormat findBestFormat(const SensorFormats &formatsMap, const Size &
 			score += penaltyAr * scoreFormat(reqAr, fmtAr);
 
 			/* Add any penalties... this is not an exact science! */
-			score += std::abs(static_cast<int>(info.bitsPerPixel - bitDepth)) * penaltyBitDepth;
+			score += utils::abs_diff(info.bitsPerPixel, bitDepth) * penaltyBitDepth;
 
 			if (score <= bestScore) {
 				bestScore = score;