From patchwork Tue Oct 26 09:55:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jean-Michel Hautbois X-Patchwork-Id: 14316 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 0DA01BDB1C for ; Tue, 26 Oct 2021 09:55:55 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 27BF364899; Tue, 26 Oct 2021 11:55:54 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="F2bdonMw"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 77AED60123 for ; Tue, 26 Oct 2021 11:55:45 +0200 (CEST) Received: from tatooine.ideasonboard.com (unknown [IPv6:2a01:e0a:169:7140:dce3:eb54:18d7:6f3d]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 2C9EF3F0; Tue, 26 Oct 2021 11:55:45 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1635242145; bh=aJzcaOw0CLDeD3nP0A2GpJAEE9VikFQMqZLFac3ZMAw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=F2bdonMwxRrRIb1PlDbkJPeIMlxTwbdRsEzHYBx+F3tttCGwBg0KUmGFiImo82xIS qNcgTU7olNrZoNrB4X3CT/osOI9LV2k5GCqoXjg0uPajgospGZYTMi4mQ9+upWkWvj rlQETR8YKD1gJirtuEfsbtWFmHvLV88M3S4x+tRM= From: Jean-Michel Hautbois To: libcamera-devel@lists.libcamera.org Date: Tue, 26 Oct 2021 11:55:20 +0200 Message-Id: <20211026095534.90348-6-jeanmichel.hautbois@ideasonboard.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211026095534.90348-1-jeanmichel.hautbois@ideasonboard.com> References: <20211026095534.90348-1-jeanmichel.hautbois@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 05/19] ipa: ipu3: awb: Add AWB class documentation X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" The AWB algorithm is based on the Grey world algorithm and uses the statistics generated by the ImgU for that. Explain how it uses those, and reference the original algorithm at the same time. Signed-off-by: Jean-Michel Hautbois Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- v3: - move the diagram from Accumulator to AWB - add a word to say green gains are always set to 1 --- src/ipa/ipu3/algorithms/awb.cpp | 110 ++++++++++++++++++++------------ 1 file changed, 69 insertions(+), 41 deletions(-) diff --git a/src/ipa/ipu3/algorithms/awb.cpp b/src/ipa/ipu3/algorithms/awb.cpp index 91364a04..8d22aceb 100644 --- a/src/ipa/ipu3/algorithms/awb.cpp +++ b/src/ipa/ipu3/algorithms/awb.cpp @@ -11,6 +11,10 @@ #include +/** + * \file awb.h + */ + namespace libcamera { namespace ipa::ipu3::algorithms { @@ -42,47 +46,6 @@ static constexpr uint32_t kMinCellsPerZoneRatio = 255 * 90 / 100; * \struct Accumulator * \brief RGB statistics for a given zone * - * - Cells are defined in Pixels - * - Zones are defined in Cells - * - * 80 cells - * /───────────── 1280 pixels ───────────\ - * 16 zones - * 16 - * ┌────┬────┬────┬────┬────┬─ ──────┬────┐ \ - * │Cell│ │ │ │ │ | │ │ │ - * 16 │ px │ │ │ │ │ | │ │ │ - * ├────┼────┼────┼────┼────┼─ ──────┼────┤ │ - * │ │ │ │ │ │ | │ │ - * │ │ │ │ │ │ | │ │ 7 - * │ ── │ ── │ ── │ ── │ ── │ ── ── ─┤ ── │ 1 2 4 - * │ │ │ │ │ │ | │ │ 2 0 5 - * - * │ │ │ │ │ │ | │ │ z p c - * ├────┼────┼────┼────┼────┼─ ──────┼────┤ o i e - * │ │ │ │ │ │ | │ │ n x l - * │ │ | │ │ e e l - * ├─── ───┼─ ──────┼────┤ s l s - * │ │ | │ │ s - * │ │ | │ │ - * ├─── Zone of Cells ───┼─ ──────┼────┤ │ - * │ (5 x 4) │ | │ │ │ - * │ │ | │ │ │ - * ├── ───┼─ ──────┼────┤ │ - * │ │ │ | │ │ │ - * │ │ │ │ │ │ | │ │ │ - * └────┴────┴────┴────┴────┴─ ──────┴────┘ / - * - * - * The algorithm works with a fixed number of zones \a kAwbStatsSizeX x - * \a kAwbStatsSizeY. For example, a frame of 1280x720 is divided into 80x45 - * cells of [16x16] pixels. In the case of \a kAwbStatsSizeX=16 and - * \a kAwbStatsSizeY=12 the zones are made of [5x4] cells. The cells are - * left-aligned and calculated by IPAIPU3::calculateBdsGrid(). - * - * Each statistics cell represents the average value of the pixels in that cell - * split by colour components. - * * The Accumulator structure stores the sum of the average of each cell in a * zone of the image, as well as the number of cells which were unsaturated and * therefore included in the average. @@ -152,6 +115,71 @@ static const struct ipu3_uapi_ccm_mat_config imguCssCcmDefault = { 0, 0, 8191, 0 }; +/** + * \class Awb + * \brief A Grey world white balance correction algorithm + * + * The Grey World algorithm assumes that the scene, in average, is neutral grey. + * Reference: Lam, Edmund & Fung, George. (2008). Automatic White Balancing in + * Digital Photography. 10.1201/9781420054538.ch10. + * + * The IPU3 generates statistics from the Bayer Down Scaler output into a grid + * defined in the ipu3_uapi_awb_config_s structure. + * + * - Cells are defined in Pixels + * - Zones are defined in Cells + * + * 80 cells + * /───────────── 1280 pixels ───────────\ + * 16 zones + * 16 + * ┌────┬────┬────┬────┬────┬─ ──────┬────┐ \ + * │Cell│ │ │ │ │ | │ │ │ + * 16 │ px │ │ │ │ │ | │ │ │ + * ├────┼────┼────┼────┼────┼─ ──────┼────┤ │ + * │ │ │ │ │ │ | │ │ + * │ │ │ │ │ │ | │ │ 7 + * │ ── │ ── │ ── │ ── │ ── │ ── ── ─┤ ── │ 1 2 4 + * │ │ │ │ │ │ | │ │ 2 0 5 + * + * │ │ │ │ │ │ | │ │ z p c + * ├────┼────┼────┼────┼────┼─ ──────┼────┤ o i e + * │ │ │ │ │ │ | │ │ n x l + * │ │ | │ │ e e l + * ├─── ───┼─ ──────┼────┤ s l s + * │ │ | │ │ s + * │ │ | │ │ + * ├─── Zone of Cells ───┼─ ──────┼────┤ │ + * │ (5 x 4) │ | │ │ │ + * │ │ | │ │ │ + * ├── ───┼─ ──────┼────┤ │ + * │ │ │ | │ │ │ + * │ │ │ │ │ │ | │ │ │ + * └────┴────┴────┴────┴────┴─ ──────┴────┘ / + * + * + * In each cell, the ImgU computes for each colour component the average of all + * unsaturated pixels (below a programmable threshold). It also provides the + * ratio of saturated pixels in the cell. + * + * The AWB algorithm operates on a coarser grid, made by grouping cells from the + * hardware grid into zones. The number of zones is fixed to \a kAwbStatsSizeX x + * \a kAwbStatsSizeY. For example, a frame of 1280x720 is divided into 80x45 + * cells of [16x16] pixels and 16x12 zones of [5x4] cells each + * (\a kAwbStatsSizeX=16 and \a kAwbStatsSizeY=12). If the number of cells isn't + * an exact multiple of the number of zones, the right-most and bottom-most + * cells are ignored. The grid configuration is computed by + * IPAIPU3::calculateBdsGrid(). + * + * Before calculating the gains, the algorithm aggregates the cell averages for + * each zone in generateAwbStats(). Cells that have a too high ratio of + * saturated pixels are ignored, and only zones that contain enough + * non-saturated cells are then used by the algorithm. + * + * The Grey World algorithm will then estimate the red and blue gains to apply, and + * store the results in the metadata. The green gain is always set to 1. + */ + Awb::Awb() : Algorithm() {