{"id":25906,"url":"https://patchwork.libcamera.org/api/1.1/patches/25906/?format=json","web_url":"https://patchwork.libcamera.org/patch/25906/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20260121133910.34925-1-stefan.klug@ideasonboard.com>","date":"2026-01-21T13:39:07","name":"[v2] ipa: rkisp1: cproc: Fix brightness and contrast value handling","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"39a2e24e5799784c077d377f9150551d0277d4c1","submitter":{"id":184,"url":"https://patchwork.libcamera.org/api/1.1/people/184/?format=json","name":"Stefan Klug","email":"stefan.klug@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/25906/mbox/","series":[{"id":5729,"url":"https://patchwork.libcamera.org/api/1.1/series/5729/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=5729","date":"2026-01-21T13:39:07","name":"[v2] ipa: rkisp1: cproc: Fix brightness and contrast value handling","version":2,"mbox":"https://patchwork.libcamera.org/series/5729/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/25906/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/25906/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id B22DDC3220\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 21 Jan 2026 13:39:22 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id D555E61FC4;\n\tWed, 21 Jan 2026 14:39:21 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 368F161F9F\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 21 Jan 2026 14:39:20 +0100 (CET)","from ideasonboard.com (unknown\n\t[IPv6:2a00:6020:448c:6c00:8b90:6146:64f5:1ba3])\n\tby perceval.ideasonboard.com (Postfix) with UTF8SMTPSA id 2C834B3;\n\tWed, 21 Jan 2026 14:38:48 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"NQMxdnn2\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1769002728;\n\tbh=hEqb5IxHKUxrHhm4fXa+wP+tHGIv6yqW/0VBYCFTJ/0=;\n\th=From:To:Cc:Subject:Date:From;\n\tb=NQMxdnn2JuM7KRRKEVV0XGSBeLZpzEonN3fuUS8C8TAWOZ1aBGMnAClJ1PnM9Y0zz\n\tD1FnOCAQtT0TOlkU1By64Jh7nqqaeA1J04u2cOYDuzFUXyfyOWCDBvHCXhMgwhAlWt\n\t6YOEOPqD6WZR+syud38jcPhHKXQvz0/OS9VFGaTQ=","From":"Stefan Klug <stefan.klug@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Cc":"Stefan Klug <stefan.klug@ideasonboard.com>","Subject":"[PATCH v2] ipa: rkisp1: cproc: Fix brightness and contrast value\n\thandling","Date":"Wed, 21 Jan 2026 14:39:07 +0100","Message-ID":"<20260121133910.34925-1-stefan.klug@ideasonboard.com>","X-Mailer":"git-send-email 2.51.0","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"By reverse engineering it was discovered that the transfer curve applied\nby the CPROC module is\n\nYout = Yin * contrast + brightness/2.\n\nHowever the most common expectation on contrast is to not change the\nmiddle gray of an image. So the expected curve is:\n\nYout = (Yin - 0.5) * contrast + 0.5 + brightness\n\nMap the user expectation to the hardware by changing the brightness\nvalue written to the hardware. This also includes multiplying the\nrequested brightness value by two.\n\nNote: The limits of the brightness control are left as is even though\nthey can not be reached without changing the contrast. But limiting them\nto [0.5, 0.5] would cut off parts of the combined control space of\ncontrast + brightness.\n\nSigned-off-by: Stefan Klug <stefan.klug@ideasonboard.com>\n\n---\n\nThis patch needs to be applied on top of the quantization series:\nhttps://patchwork.libcamera.org/project/libcamera/list/?series=5708\n\nChanges in v2:\n- Moved calculation of brightness into helper function, to be able to\n  reuse it inside configure()\n- Moved calculation of actualBrightness from process() to the helper\n  function\n- Added documentation for the new context variables\n- Removed unnecessary curly braces\n- Improved comment\n---\n src/ipa/rkisp1/algorithms/cproc.cpp | 58 ++++++++++++++++++++++-------\n src/ipa/rkisp1/ipa_context.cpp      | 17 +++++++--\n src/ipa/rkisp1/ipa_context.h        |  3 ++\n 3 files changed, 61 insertions(+), 17 deletions(-)","diff":"diff --git a/src/ipa/rkisp1/algorithms/cproc.cpp b/src/ipa/rkisp1/algorithms/cproc.cpp\nindex 7484e4780094..e0f6ac9816f7 100644\n--- a/src/ipa/rkisp1/algorithms/cproc.cpp\n+++ b/src/ipa/rkisp1/algorithms/cproc.cpp\n@@ -46,6 +46,33 @@ constexpr float kDefaultSaturation = 1.0f;\n  */\n constexpr float kHueScale = -90.0f;\n \n+void applyRequestedBrightness(IPAActiveState &activeState)\n+{\n+\tauto &cproc = activeState.cproc;\n+\n+\t/*\n+\t * The CPROC module applies the transfer function\n+\t *\n+\t * Yout = Yin * contrast + brightness/2\n+\t *\n+\t * The calculations are done one bit wider than the input data to\n+\t * account for the maximum contrast of 2x without clamping. Brightness\n+\t * is applied after and therefore affects the output only with half the\n+\t * value.\n+\t *\n+\t * Most users expect that changing contrast doesn't change the middle\n+\t * gray and that the brightness value is normalized to one. So they\n+\t * expect a transfer function of\n+\t *\n+\t * Yout = (Yin - 0.5) * contrast + 0.5 + brightness\n+\t *\n+\t * To implement above formula with the given hardware we need adjust the\n+\t * hardware brightness value depending on the contrast.\n+\t */\n+\tcproc.brightness = (1 - cproc.contrast.value()) + 2 * cproc.requestedBrightness;\n+\tcproc.actualBrightness = (cproc.brightness.value() - (1 - cproc.contrast.value())) / 2;\n+}\n+\n } /* namespace */\n \n /**\n@@ -76,8 +103,10 @@ int ColorProcessing::configure(IPAContext &context,\n {\n \tauto &cproc = context.activeState.cproc;\n \n-\tcproc.brightness = BrightnessQ(kDefaultBrightness);\n+\tcproc.requestedBrightness = kDefaultBrightness;\n \tcproc.contrast = ContrastQ(kDefaultContrast);\n+\tapplyRequestedBrightness(context.activeState);\n+\n \tcproc.hue = HueQ(kDefaultHue);\n \tcproc.saturation = SaturationQ(kDefaultSaturation);\n \n@@ -98,17 +127,6 @@ void ColorProcessing::queueRequest(IPAContext &context,\n \tif (frame == 0)\n \t\tupdate = true;\n \n-\tconst auto &brightness = controls.get(controls::Brightness);\n-\tif (brightness) {\n-\t\tBrightnessQ value = *brightness;\n-\t\tif (cproc.brightness != value) {\n-\t\t\tcproc.brightness = value;\n-\t\t\tupdate = true;\n-\t\t}\n-\n-\t\tLOG(RkISP1CProc, Debug) << \"Set brightness to \" << value;\n-\t}\n-\n \tconst auto &contrast = controls.get(controls::Contrast);\n \tif (contrast) {\n \t\tContrastQ value = *contrast;\n@@ -120,6 +138,19 @@ void ColorProcessing::queueRequest(IPAContext &context,\n \t\tLOG(RkISP1CProc, Debug) << \"Set contrast to \" << value;\n \t}\n \n+\tconst auto &brightness = controls.get(controls::Brightness);\n+\tif (brightness)\n+\t\tcproc.requestedBrightness = *brightness;\n+\n+\tif (update || brightness) {\n+\t\tBrightnessQ old = cproc.brightness;\n+\t\tapplyRequestedBrightness(context.activeState);\n+\t\tif (cproc.brightness != old)\n+\t\t\tupdate = true;\n+\n+\t\tLOG(RkISP1CProc, Debug) << \"Set brightness to \" << cproc.actualBrightness;\n+\t}\n+\n \tconst auto &hue = controls.get(controls::Hue);\n \tif (hue) {\n \t\t/* Scale the Hue from ]-90, +90] */\n@@ -144,6 +175,7 @@ void ColorProcessing::queueRequest(IPAContext &context,\n \t}\n \n \tframeContext.cproc.brightness = cproc.brightness;\n+\tframeContext.cproc.actualBrightness = cproc.actualBrightness;\n \tframeContext.cproc.contrast = cproc.contrast;\n \tframeContext.cproc.hue = cproc.hue;\n \tframeContext.cproc.saturation = cproc.saturation;\n@@ -179,7 +211,7 @@ void ColorProcessing::process([[maybe_unused]] IPAContext &context,\n \t\t\t      [[maybe_unused]] const rkisp1_stat_buffer *stats,\n \t\t\t      ControlList &metadata)\n {\n-\tmetadata.set(controls::Brightness, frameContext.cproc.brightness.value());\n+\tmetadata.set(controls::Brightness, frameContext.cproc.actualBrightness);\n \tmetadata.set(controls::Contrast, frameContext.cproc.contrast.value());\n \tmetadata.set(controls::Hue, frameContext.cproc.hue.value() * kHueScale);\n \tmetadata.set(controls::Saturation, frameContext.cproc.saturation.value());\ndiff --git a/src/ipa/rkisp1/ipa_context.cpp b/src/ipa/rkisp1/ipa_context.cpp\nindex 15cb0afe9fe8..f9d0aff058bc 100644\n--- a/src/ipa/rkisp1/ipa_context.cpp\n+++ b/src/ipa/rkisp1/ipa_context.cpp\n@@ -232,9 +232,15 @@ namespace libcamera::ipa::rkisp1 {\n /**\n  * \\var IPAActiveState::cproc\n  * \\brief State for the Color Processing algorithm\n+  *\n+ * \\var IPAActiveState::cproc.requestedBrightness\n+ * \\brief Brightness level requested by the user\n+  *\n+ * \\var IPAActiveState::cproc.actualBrightness\n+ * \\brief Brightness level after contrast compensation\n  *\n- * \\struct IPAActiveState::cproc.brightness\n- * \\brief Brightness level\n+ * \\var IPAActiveState::cproc.brightness\n+ * \\brief Hardware brightness level\n  *\n  * \\var IPAActiveState::cproc.contrast\n  * \\brief Contrast level\n@@ -400,8 +406,11 @@ namespace libcamera::ipa::rkisp1 {\n  * \\var IPAFrameContext::cproc\n  * \\brief Color Processing parameters for this frame\n  *\n- * \\struct IPAFrameContext::cproc.brightness\n- * \\brief Brightness level\n+ * \\var IPAFrameContext::cproc.actualBrightness\n+ * \\brief Brightness level after contrast compensation\n+ *\n+ * \\var IPAFrameContext::cproc.brightness\n+ * \\brief Hardware brightness level\n  *\n  * \\var IPAFrameContext::cproc.contrast\n  * \\brief Contrast level\ndiff --git a/src/ipa/rkisp1/ipa_context.h b/src/ipa/rkisp1/ipa_context.h\nindex 80b035044cda..72ec64532dd8 100644\n--- a/src/ipa/rkisp1/ipa_context.h\n+++ b/src/ipa/rkisp1/ipa_context.h\n@@ -122,6 +122,8 @@ struct IPAActiveState {\n \t} ccm;\n \n \tstruct {\n+\t\tfloat requestedBrightness;\n+\t\tfloat actualBrightness;\n \t\tBrightnessQ brightness;\n \t\tContrastQ contrast;\n \t\tHueQ hue;\n@@ -181,6 +183,7 @@ struct IPAFrameContext : public FrameContext {\n \t} awb;\n \n \tstruct {\n+\t\tfloat actualBrightness;\n \t\tBrightnessQ brightness;\n \t\tContrastQ contrast;\n \t\tHueQ hue;\n","prefixes":["v2"]}