mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Balakrishnan Sambath <balakrishnan.s@microchip.com>
To: <ehristev@kernel.org>
Cc: <linux-kernel@vger.kernel.org>,
	Balakrishnan Sambath <balakrishnan.s@microchip.com>
Subject: [PATCH 10/15] media: microchip-isc: trim outliers in the AWB average
Date: Wed, 2 Dec 2026 12:46:16 +0530	[thread overview]
Message-ID: <20261202-isc-series-v1-v1-10-4e2f17c0271c@microchip.com> (raw)
In-Reply-To: <20261202-isc-series-v1-v1-0-4e2f17c0271c@microchip.com>

Skip the darkest and brightest 2% of each channel histogram
(ISC_AWB_OUTLIER_DIV) when picking min and max, so a few hot or dark
pixels no longer set the stretch range.

Keep the mean over the remaining bins in ctrls->channel_avg[] in place
of the raw sum, hist_count[]. The pixel count left after the trim
differs per channel, so the raw sums no longer compare across channels.
An empty histogram reports a zero mean, so the null green check in
isc_wb_update() still returns early and the gains stay as they are.
The trim counts bins 0 to 510 only, the last bin stays out as before, so
the outlier budget is taken from the same count. When the bins above min
fit in the budget, max settles on min and the mean is that bin.

Signed-off-by: Balakrishnan Sambath <balakrishnan.s@microchip.com>
---
 .../media/platform/microchip/microchip-isc-base.c  | 78 +++++++++++++++-------
 drivers/media/platform/microchip/microchip-isc.h   |  2 +-
 2 files changed, 54 insertions(+), 26 deletions(-)

diff --git a/drivers/media/platform/microchip/microchip-isc-base.c b/drivers/media/platform/microchip/microchip-isc-base.c
index b2c19879e1c6..2787de606b4a 100644
--- a/drivers/media/platform/microchip/microchip-isc-base.c
+++ b/drivers/media/platform/microchip/microchip-isc-base.c
@@ -40,6 +40,9 @@
 	(((mbus_code) == MEDIA_BUS_FMT_Y10_1X10) | \
 	(((mbus_code) == MEDIA_BUS_FMT_Y8_1X8)))
 
+/* Skip the darkest and brightest 2% of the histogram as outliers */
+#define ISC_AWB_OUTLIER_DIV	50
+
 static inline void isc_update_v4l2_ctrls(struct isc_device *isc)
 {
 	struct isc_ctrls *ctrls = &isc->ctrls;
@@ -1275,41 +1278,66 @@ static void isc_hist_count(struct isc_device *isc, u32 *min, u32 *max)
 {
 	struct regmap *regmap = isc->regmap;
 	struct isc_ctrls *ctrls = &isc->ctrls;
-	u32 *hist_count = &ctrls->hist_count[ctrls->hist_id];
 	u32 *hist_entry = &ctrls->hist_entry[0];
-	bool seen = false;
+	u64 weighted_sum = 0;
+	u32 cumulative, outlier;
+	u32 total_pixels = 0;
+	u32 pixels = 0;
 	u32 i;
 
-	*min = 0;
-	*max = HIST_ENTRIES;
-
 	regmap_bulk_read(regmap, ISC_HIS_ENTRY + isc->offsets.his_entry,
 			 hist_entry, HIST_ENTRIES);
 
-	*hist_count = 0;
-	/*
-	 * we deliberately ignore the end of the histogram,
-	 * the most white pixels
-	 */
+	/* The last bin, the most white pixels, is deliberately left out */
+	for (i = 0; i < HIST_ENTRIES - 1; i++)
+		total_pixels += hist_entry[i];
+
+	/* No pixels, a zero average leaves the gains alone */
+	if (!total_pixels) {
+		ctrls->channel_avg[ctrls->hist_id] = 0;
+		*min = 0;
+		*max = HIST_ENTRIES - 2;
+		return;
+	}
+
+	outlier = total_pixels / ISC_AWB_OUTLIER_DIV;
+
+	/* The effective minimum skips the dark outliers */
+	cumulative = 0;
 	for (i = 0; i < HIST_ENTRIES - 1; i++) {
-		if (!hist_entry[i])
-			continue;
-		if (!seen) {
-			*min = i;
-			seen = true;
-		}
-		*max = i;
-		*hist_count += i * hist_entry[i];
+		cumulative += hist_entry[i];
+		if (cumulative > outlier)
+			break;
+	}
+	*min = i;
+
+	/* The maximum skips the bright ones and stops at the minimum */
+	cumulative = 0;
+	for (i = HIST_ENTRIES - 2; i > *min; i--) {
+		cumulative += hist_entry[i];
+		if (cumulative > outlier)
+			break;
 	}
+	*max = i;
 
-	dev_dbg(isc->dev, "isc wb: hist_id %u, hist_count %u",
-		ctrls->hist_id, *hist_count);
+	for (i = *min; i <= *max; i++) {
+		pixels += hist_entry[i];
+		weighted_sum += (u64)i * hist_entry[i];
+	}
+
+	/* The minimum bin is occupied, so pixels is never zero */
+	ctrls->channel_avg[ctrls->hist_id] = div_u64(weighted_sum, pixels);
+
+	dev_dbg(isc->dev,
+		"isc wb: hist_id %u, avg %u, range [%u,%u], total %u\n",
+		ctrls->hist_id, ctrls->channel_avg[ctrls->hist_id],
+		*min, *max, total_pixels);
 }
 
 static void isc_wb_update(struct isc_ctrls *ctrls)
 {
 	struct isc_device *isc = container_of(ctrls, struct isc_device, ctrls);
-	u32 *hist_count = &ctrls->hist_count[0];
+	u32 *channel_avg = &ctrls->channel_avg[0];
 	u32 c, offset[4];
 	u64 avg = 0;
 	/* We compute two gains, stretch gain and grey world gain */
@@ -1321,8 +1349,8 @@ static void isc_wb_update(struct isc_ctrls *ctrls)
 	 * Thus we want to keep Green as fixed and adjust only Red/Blue
 	 * Compute the average of the both green channels first
 	 */
-	avg = (u64)hist_count[ISC_HIS_CFG_MODE_GR] +
-		(u64)hist_count[ISC_HIS_CFG_MODE_GB];
+	avg = (u64)channel_avg[ISC_HIS_CFG_MODE_GR] +
+		(u64)channel_avg[ISC_HIS_CFG_MODE_GB];
 	avg >>= 1;
 
 	dev_dbg(isc->dev, "isc wb: green components average %llu\n", avg);
@@ -1372,8 +1400,8 @@ static void isc_wb_update(struct isc_ctrls *ctrls)
 		 * If it happens that the component is zero, use the
 		 * fixed point value : 1.0 gain.
 		 */
-		if (hist_count[c])
-			gw_gain[c] = div_u64(avg << 9, hist_count[c]);
+		if (channel_avg[c])
+			gw_gain[c] = div_u64(avg << 9, channel_avg[c]);
 		else
 			gw_gain[c] = 1 << 9;
 
diff --git a/drivers/media/platform/microchip/microchip-isc.h b/drivers/media/platform/microchip/microchip-isc.h
index ccb566e5e627..1784c0089296 100644
--- a/drivers/media/platform/microchip/microchip-isc.h
+++ b/drivers/media/platform/microchip/microchip-isc.h
@@ -154,12 +154,12 @@ struct isc_ctrls {
 	s32 offset[HIST_BAYER];
 
 	u32 hist_entry[HIST_ENTRIES];
-	u32 hist_count[HIST_BAYER];
 	u8 hist_id;
 	u8 hist_stat;
 #define HIST_MIN_INDEX		0
 #define HIST_MAX_INDEX		1
 	u32 hist_minmax[HIST_BAYER][2];
+	u32 channel_avg[HIST_BAYER];
 };
 
 #define ISC_PIPE_LINE_NODE_NUM	15

-- 
2.34.1


  parent reply	other threads:[~2026-12-02  7:16 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  7:16 [PATCH 00/15] media: microchip-isc: fix SAMA7G5 image pipeline and AWB Balakrishnan Sambath
2026-12-02  7:16 ` [PATCH 01/15] dt-bindings: media: microchip,xisc: add SAM9X7 compatible Balakrishnan Sambath
2026-12-02  7:16 ` [PATCH 02/15] media: microchip-isc: set SAM9X7 maximum resolution per SoC Balakrishnan Sambath
2026-12-02  7:16 ` [PATCH 03/15] media: microchip-isc: fix DPC black level scale Balakrishnan Sambath
2026-12-02  7:16 ` [PATCH 04/15] media: microchip-isc: enable black level correction on SAMA7G5 Balakrishnan Sambath
2026-12-02  7:16 ` [PATCH 05/15] media: microchip-isc: bypass SAMA7G5 pipeline for Bayer output Balakrishnan Sambath
2026-12-02  7:16 ` [PATCH 06/15] media: microchip-isc: add gamma 1.8 and 2.4 curves for SAMA7G5 Balakrishnan Sambath
2026-12-02  7:16 ` [PATCH 07/15] media: microchip-isc: fix CBHS register names Balakrishnan Sambath
2026-12-02  7:16 ` [PATCH 08/15] media: microchip-isc: add SAMA7G5 hue and saturation controls Balakrishnan Sambath
2026-12-02  7:16 ` [PATCH 09/15] media: microchip-isc: fix histogram bin index for AWB Balakrishnan Sambath
2026-12-02  7:16 ` Balakrishnan Sambath [this message]
2026-12-02  7:16 ` [PATCH 11/15] media: microchip-isc: skip AWB on near-empty channels Balakrishnan Sambath
2026-12-02  7:16 ` [PATCH 12/15] media: microchip-isc: do the AWB gain product in 64 bits Balakrishnan Sambath
2026-12-02  7:16 ` [PATCH 13/15] media: microchip-isc: smooth AWB gains with an EMA filter Balakrishnan Sambath
2026-12-02  7:16 ` [PATCH 14/15] media: microchip-isc: use an unsigned contrast range Balakrishnan Sambath
2026-12-02  7:16 ` [PATCH 15/15] media: microchip-isc: add driver documentation Balakrishnan Sambath

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261202-isc-series-v1-v1-10-4e2f17c0271c@microchip.com \
    --to=balakrishnan.s@microchip.com \
    --cc=ehristev@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®