From: Balamanikandan Gunasundar <balamanikandan.gunasundar@microchip.com>
To: Mauro Carvalho Chehab <mchehab@kernel.org>
Cc: Eugen Hristev <eugen.hristev@linaro.org>,
Chas Williams <3chas3@gmail.com>,
Nicolas Ferre <nicolas.ferre@microchip.com>,
"Alexandre Belloni" <alexandre.belloni@bootlin.com>,
Claudiu Beznea <claudiu.beznea@tuxon.dev>,
Balakrishnan Sambath <balakrishnan.s@microchip.com>,
Hans Verkuil <hverkuil@kernel.org>,
"Ricardo Ribalda" <ribalda@chromium.org>,
Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>,
Jacopo Mondi <jacopo.mondi@ideasonboard.com>,
Daniel Scally <dan.scally+renesas@ideasonboard.com>,
Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>,
<linux-kernel@vger.kernel.org>, <linux-media@vger.kernel.org>,
<linux-atm-general@lists.sourceforge.net>,
<netdev@vger.kernel.org>, <linux-arm-kernel@lists.infradead.org>
Subject: [PATCH 04/18] media: microchip-isc: Improve histogram calculation with outlier rejection
Date: Thu, 9 Oct 2025 21:22:37 +0530 [thread overview]
Message-ID: <20251009155251.102472-5-balamanikandan.gunasundar@microchip.com> (raw)
In-Reply-To: <20251009155251.102472-1-balamanikandan.gunasundar@microchip.com>
From: Balakrishnan Sambath <balakrishnan.s@microchip.com>
Replace simple min/max detection with smart outlier rejection that skips
bottom/top 2% of histogram to avoid noise and saturation. Add channel
average calculation using weighted pixel intensity instead of simple
pixel counting for more accurate color analysis.
Signed-off-by: Balakrishnan Sambath <balakrishnan.s@microchip.com>
---
.../platform/microchip/microchip-isc-base.c | 83 ++++++++++++++++---
.../media/platform/microchip/microchip-isc.h | 2 +
2 files changed, 75 insertions(+), 10 deletions(-)
diff --git a/drivers/media/platform/microchip/microchip-isc-base.c b/drivers/media/platform/microchip/microchip-isc-base.c
index c138e92a1aca..956bdea830e3 100644
--- a/drivers/media/platform/microchip/microchip-isc-base.c
+++ b/drivers/media/platform/microchip/microchip-isc-base.c
@@ -262,6 +262,10 @@ static void isc_set_histogram(struct isc_device *isc, bool enable)
struct isc_ctrls *ctrls = &isc->ctrls;
if (enable) {
+ /* Initialize histogram data storage for clean start */
+ memset(ctrls->total_pixels, 0, sizeof(ctrls->total_pixels));
+ memset(ctrls->hist_minmax, 0, sizeof(ctrls->hist_minmax));
+
regmap_write(regmap, ISC_HIS_CFG + isc->offsets.his,
ISC_HIS_CFG_MODE_GR |
(isc->config.sd_format->cfa_baycfg
@@ -1231,24 +1235,83 @@ static void isc_hist_count(struct isc_device *isc, u32 *min, u32 *max)
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
- */
+ /* Calculate total pixels */
+ u32 total_pixels = 0;
+
+ for (i = 0; i < HIST_ENTRIES; i++)
+ total_pixels += hist_entry[i];
+
+ /* Handle empty histogram case */
+ if (total_pixels == 0) {
+ *hist_count = 0;
+ ctrls->channel_avg[ctrls->hist_id] = 256; /* Default middle value */
+ ctrls->total_pixels[ctrls->hist_id] = 0;
+ *min = 1;
+ *max = HIST_ENTRIES - 1;
+ dev_dbg(isc->dev, "isc wb: no pixels in histogram for channel %u", ctrls->hist_id);
+ return;
+ }
+
+ /* Smart outlier rejection - skip bottom/top 2% */
+ u32 dark_threshold = total_pixels / 50; /* Bottom 2% */
+ u32 bright_threshold = total_pixels / 50; /* Top 2% */
+ u32 cumulative = 0;
+
+ /* Find effective minimum (skip dark noise) */
+ *min = 1;
for (i = 1; i < HIST_ENTRIES; i++) {
- if (*hist_entry && !*min)
+ cumulative += hist_entry[i];
+ if (cumulative > dark_threshold) {
*min = i;
- if (*hist_entry)
+ break;
+ }
+ }
+
+ /* Find effective maximum (skip bright saturation) */
+ cumulative = 0;
+ *max = HIST_ENTRIES - 1;
+ for (i = HIST_ENTRIES - 1; i > *min; i--) {
+ cumulative += hist_entry[i];
+ if (cumulative > bright_threshold) {
*max = i;
- *hist_count += i * (*hist_entry++);
+ break;
+ }
}
+ /* Ensure reasonable range */
+ if (*max <= *min) {
+ *min = HIST_ENTRIES / 4;
+ *max = (HIST_ENTRIES * 3) / 4;
+ }
+
+ /* Calculate both pixel count and weighted average for useful range */
+ *hist_count = 0;
+ u64 weighted_sum = 0;
+
+ for (i = *min; i <= *max; i++) {
+ u32 pixel_count = hist_entry[i];
+ *hist_count += pixel_count;
+ weighted_sum += (u64)i * pixel_count;
+ }
+
+ /* Store total useful pixels for this channel */
+ ctrls->total_pixels[ctrls->hist_id] = *hist_count;
+
+ /* Calculate channel average */
+ if (*hist_count > 0)
+ ctrls->channel_avg[ctrls->hist_id] =
+ div64_u64(weighted_sum, *hist_count);
+ else
+ /* Default middle value */
+ ctrls->channel_avg[ctrls->hist_id] = 256;
+
if (!*min)
*min = 1;
- dev_dbg(isc->dev, "isc wb: hist_id %u, hist_count %u",
- ctrls->hist_id, *hist_count);
+ dev_dbg(isc->dev,
+ "isc wb: hist_id %u, avg %u, count %u, range [%u,%u], total %u",
+ ctrls->hist_id, ctrls->channel_avg[ctrls->hist_id],
+ *hist_count, *min, *max, total_pixels);
}
static void isc_wb_update(struct isc_ctrls *ctrls)
diff --git a/drivers/media/platform/microchip/microchip-isc.h b/drivers/media/platform/microchip/microchip-isc.h
index ad4e98a1dd8f..bd75ff4f109b 100644
--- a/drivers/media/platform/microchip/microchip-isc.h
+++ b/drivers/media/platform/microchip/microchip-isc.h
@@ -156,6 +156,8 @@ struct isc_ctrls {
#define HIST_MIN_INDEX 0
#define HIST_MAX_INDEX 1
u32 hist_minmax[HIST_BAYER][2];
+ u32 channel_avg[HIST_BAYER]; /* Average pixel intensity per channel */
+ u32 total_pixels[HIST_BAYER]; /* Total pixels per channel */
};
#define ISC_PIPE_LINE_NODE_NUM 15
--
2.34.1
next prev parent reply other threads:[~2025-10-09 15:54 UTC|newest]
Thread overview: 66+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-09 15:52 [PATCH 00/18] media: microchip-isc: Color correction and histogram stats Balamanikandan Gunasundar
2025-10-09 15:52 ` [PATCH 01/18] media: platform: microchip: set maximum resolution for sam9x7 Balamanikandan Gunasundar
2025-10-09 15:52 ` [PATCH 02/18] media: platform: microchip: Include DPC modules flags in pipeline Balamanikandan Gunasundar
2025-10-09 15:52 ` [PATCH 03/18] media: microchip-isc: Enable GDC and CBC module flags for RGB formats Balamanikandan Gunasundar
2025-10-09 15:52 ` Balamanikandan Gunasundar [this message]
2025-10-09 15:52 ` [PATCH 05/18] media: microchip-isc: Use channel averages for Grey World AWB Balamanikandan Gunasundar
2025-10-09 15:52 ` [PATCH 06/18] media: microchip-isc: Add range based black level correction Balamanikandan Gunasundar
2025-10-09 15:52 ` [PATCH 07/18] media: platform: microchip: Extend gamma table and control range Balamanikandan Gunasundar
2025-10-09 15:52 ` [PATCH 08/18] media: platform: microchip: Add new histogram submodule Balamanikandan Gunasundar
2025-11-10 8:41 ` Hans Verkuil
2025-11-10 8:55 ` Hans Verkuil
2025-10-09 15:52 ` [PATCH 09/18] media: microchip-isc: Register and unregister statistics device Balamanikandan Gunasundar
2025-10-09 15:52 ` [PATCH 10/18] media: microchip-isc: Always enable histogram for all RAW formats Balamanikandan Gunasundar
2025-10-09 15:52 ` [PATCH 11/18] media: microchip-isc: expose hue and saturation as v4l2 controls Balamanikandan Gunasundar
2025-10-09 15:52 ` [PATCH 12/18] media: microchip-isc: Rename CBC to CBHS Balamanikandan Gunasundar
2025-10-09 15:52 ` [PATCH 13/18] media: microchip-isc: Store histogram data of all channels Balamanikandan Gunasundar
2025-10-09 15:52 ` [PATCH 14/18] media: microchip-isc: fix histogram state initialization order Balamanikandan Gunasundar
2025-10-09 15:52 ` [PATCH 15/18] media: microchip-isc: decouple histogram cycling from AWB mode Balamanikandan Gunasundar
2025-10-09 15:52 ` [PATCH 16/18] media: microchip-isc: enable userspace histogram statistics export Balamanikandan Gunasundar
2025-10-09 15:52 ` [PATCH 17/18] media: videodev2.h, v4l2-ioctl: Add microchip statistics format Balamanikandan Gunasundar
2025-11-10 8:49 ` Hans Verkuil
2025-10-09 15:52 ` [PATCH 18/18] media: microchip-isc: expose color correction registers as v4l2 controls Balamanikandan Gunasundar
2025-10-09 20:11 ` [PATCH 00/18] media: microchip-isc: Color correction and histogram stats Eugen Hristev
2025-10-10 8:21 ` Kieran Bingham
2025-10-10 8:27 ` Laurent Pinchart
2025-10-15 6:05 ` Balamanikandan.Gunasundar
2025-10-15 6:45 ` Balamanikandan.Gunasundar
2025-10-15 6:33 ` Balamanikandan.Gunasundar
2025-10-15 5:26 ` Balamanikandan.Gunasundar
2025-11-10 9:01 ` Hans Verkuil
2025-11-10 9:26 ` Hans Verkuil
2026-05-12 15:43 ` [PATCH v2 00/15] media: microchip-isc: fixes and enhancements Balakrishnan Sambath
2026-05-12 15:43 ` [PATCH v2 01/15] media: microchip-isc: fix SBGGR10 Bayer pattern Balakrishnan Sambath
2026-05-12 15:43 ` [PATCH v2 02/15] media: microchip-isc: mask WB offset and gain register fields Balakrishnan Sambath
2026-05-12 15:43 ` [PATCH v2 03/15] media: microchip-isc: fix race condition on stream stop Balakrishnan Sambath
2026-05-12 15:43 ` [PATCH v2 04/15] media: microchip-isc: fix PM runtime leak in AWB work handler Balakrishnan Sambath
2026-05-12 15:43 ` [PATCH v2 05/15] media: microchip-isc: add driver documentation Balakrishnan Sambath
2026-05-12 15:43 ` [PATCH v2 06/15] media: microchip-isc: set SAM9X7 maximum resolution to 2560x1920 Balakrishnan Sambath
2026-05-12 15:43 ` [PATCH v2 07/15] media: microchip-isc: configure DPC and pipeline for SAMA7G5 Balakrishnan Sambath
2026-05-12 15:43 ` [PATCH v2 08/15] media: microchip-isc: add gamma 1.8 and 2.4 correction curves Balakrishnan Sambath
2026-05-12 15:43 ` [PATCH v2 09/15] media: microchip-isc: add SAMA7G5 hue and saturation controls Balakrishnan Sambath
2026-05-12 15:43 ` [PATCH v2 10/15] media: microchip-isc: expose color correction matrix as V4L2 controls Balakrishnan Sambath
2026-05-12 15:43 ` [PATCH v2 11/15] media: microchip-isc: add per-channel gamma LUT controls Balakrishnan Sambath
2026-05-15 10:26 ` Sakari Ailus
2026-05-18 7:25 ` Balakrishnan.S
2026-05-12 15:43 ` [PATCH v2 12/15] media: microchip-isc: reset pipeline state on kernel AWB enable Balakrishnan Sambath
2026-05-12 15:43 ` [PATCH v2 13/15] media: microchip-isc: use weighted averages for Grey World AWB Balakrishnan Sambath
2026-05-12 15:43 ` [PATCH v2 14/15] media: microchip-isc: smooth AWB gains with EMA filter Balakrishnan Sambath
2026-05-12 15:43 ` [PATCH v2 15/15] media: microchip-isc: scale DPC black level to sensor bit depth Balakrishnan Sambath
2026-05-13 7:17 ` [PATCH v3 00/15] media: microchip-isc: fixes and enhancements Balakrishnan Sambath
2026-05-13 7:17 ` [PATCH v3 01/15] media: microchip-isc: fix SBGGR10 Bayer pattern Balakrishnan Sambath
2026-05-13 7:17 ` [PATCH v3 02/15] media: microchip-isc: mask WB offset and gain register fields Balakrishnan Sambath
2026-05-13 7:17 ` [PATCH v3 03/15] media: microchip-isc: fix race condition on stream stop Balakrishnan Sambath
2026-05-13 7:17 ` [PATCH v3 04/15] media: microchip-isc: fix PM runtime leak in AWB work handler Balakrishnan Sambath
2026-05-13 7:17 ` [PATCH v3 05/15] media: microchip-isc: add driver documentation Balakrishnan Sambath
2026-05-13 7:17 ` [PATCH v3 06/15] media: microchip-isc: set SAM9X7 maximum resolution to 2560x1920 Balakrishnan Sambath
2026-05-13 7:17 ` [PATCH v3 07/15] media: microchip-isc: configure DPC and pipeline for SAMA7G5 Balakrishnan Sambath
2026-05-13 7:17 ` [PATCH v3 08/15] media: microchip-isc: add gamma 1.8 and 2.4 correction curves Balakrishnan Sambath
2026-05-13 7:17 ` [PATCH v3 09/15] media: microchip-isc: add SAMA7G5 hue and saturation controls Balakrishnan Sambath
2026-05-13 7:17 ` [PATCH v3 10/15] media: microchip-isc: expose color correction matrix as V4L2 controls Balakrishnan Sambath
2026-05-13 7:17 ` [PATCH v3 11/15] media: microchip-isc: add per-channel gamma LUT controls Balakrishnan Sambath
2026-05-13 7:17 ` [PATCH v3 12/15] media: microchip-isc: reset pipeline state on kernel AWB enable Balakrishnan Sambath
2026-05-13 7:17 ` [PATCH v3 13/15] media: microchip-isc: use weighted averages for Grey World AWB Balakrishnan Sambath
2026-05-13 7:17 ` [PATCH v3 14/15] media: microchip-isc: smooth AWB gains with EMA filter Balakrishnan Sambath
2026-05-13 7:17 ` [PATCH v3 15/15] media: microchip-isc: scale DPC black level to sensor bit depth Balakrishnan Sambath
2026-05-15 10:27 ` [PATCH v3 00/15] media: microchip-isc: fixes and enhancements Sakari Ailus
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=20251009155251.102472-5-balamanikandan.gunasundar@microchip.com \
--to=balamanikandan.gunasundar@microchip.com \
--cc=3chas3@gmail.com \
--cc=alexandre.belloni@bootlin.com \
--cc=balakrishnan.s@microchip.com \
--cc=claudiu.beznea@tuxon.dev \
--cc=dan.scally+renesas@ideasonboard.com \
--cc=eugen.hristev@linaro.org \
--cc=hverkuil@kernel.org \
--cc=jacopo.mondi@ideasonboard.com \
--cc=laurent.pinchart+renesas@ideasonboard.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-atm-general@lists.sourceforge.net \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=mchehab@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=nicolas.ferre@microchip.com \
--cc=ribalda@chromium.org \
--cc=tomi.valkeinen@ideasonboard.com \
/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®