{"id":17474,"url":"https://patchwork.libcamera.org/api/patches/17474/?format=json","web_url":"https://patchwork.libcamera.org/patch/17474/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/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":"<20220930190711.13834-1-laurent.pinchart@ideasonboard.com>","date":"2022-09-30T19:07:11","name":"[libcamera-devel,v2] utils: rkisp1: gen-csc-table: Add support for inverting the CSC","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"85134bddcc74f53f7eae701bdb223e992b6594f1","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/17474/mbox/","series":[{"id":3519,"url":"https://patchwork.libcamera.org/api/series/3519/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3519","date":"2022-09-30T19:07:11","name":"[libcamera-devel,v2] utils: rkisp1: gen-csc-table: Add support for inverting the CSC","version":2,"mbox":"https://patchwork.libcamera.org/series/3519/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/17474/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/17474/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 38AD9BD16B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 30 Sep 2022 19:07:16 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 9A84261FB1;\n\tFri, 30 Sep 2022 21:07:15 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id C257561FB1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 30 Sep 2022 21:07:13 +0200 (CEST)","from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi\n\t[62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 3B6654D5\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 30 Sep 2022 21:07:13 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1664564835;\n\tbh=zj4xwm8q2INH9HJx6q/F/R0p16rKF6JZrkj+7MP2P4o=;\n\th=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post:\n\tList-Help:List-Subscribe:From:Reply-To:From;\n\tb=P9mmpQIc2hPq6EJSYgl8u4F96+C+Qkignb7uYSD0wX9RMcqufTiGAsJZzdZVKnh1+\n\tReaiZMyy8Gn4/NB+6r7wUMyRByQwqhkUrCWuEcFazkvJnfIxCgii93sW4j4G04Yovi\n\tZ8027vxUJQFW23cscR+90a2c8ZzHAmCFZrAkXcAXTo5P6VqWPYLne9tR4yu6dBhUf7\n\tIX4cgmgd/lvxwzpWZyRxf3vdbfnvyOMv+b06+jYt4hZI2at75lD1td+jdEqMbZkksR\n\ti3EEPwawXhCI/sMmOSeVGAk3pz/Y4Jl/i7EIfBMmZONQ9PIjIN2jN4PfFViCmc6Y4I\n\tGKN6QBLjpOXzw==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1664564833;\n\tbh=zj4xwm8q2INH9HJx6q/F/R0p16rKF6JZrkj+7MP2P4o=;\n\th=From:To:Subject:Date:From;\n\tb=qazsEk7x9wiI1ixlw6Y2J52ayISQe+febR9Y+kgbyjn7j4a2x1QxYX21F4n1KnEr2\n\t0yZuzi16qHuUKqAZhSZ73GAokbEnn/lohRw1G4wWwLqr7yPeN+Pk6OXBUuvFQL510q\n\tLMWkIdg01hr0brZphCLruTNsPfQZwQIMGvUJ8X1M="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"qazsEk7x\"; dkim-atps=neutral","To":"libcamera-devel@lists.libcamera.org","Date":"Fri, 30 Sep 2022 22:07:11 +0300","Message-Id":"<20220930190711.13834-1-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.35.1","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v2] utils: rkisp1: gen-csc-table: Add\n\tsupport for inverting the CSC","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>","From":"Laurent Pinchart via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Add a -i/--invert command line argument to invert the YCbCr encoding and\noutput a YCbCr to RGB matrix.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nReviewed-by: Jacopo Mondi <jacopo@jmondi.org>\n---\nChanges since v1:\n\n- Fix inverted CSC calculation\n- Perform simple rounding for inverted CSC\n---\n utils/rkisp1/gen-csc-table.py | 31 +++++++++++++++++++++----------\n 1 file changed, 21 insertions(+), 10 deletions(-)\n\n\nbase-commit: ed591e705c451d0ce14988ae96829a31a2ae2f9a","diff":"diff --git a/utils/rkisp1/gen-csc-table.py b/utils/rkisp1/gen-csc-table.py\nindex 2fb702746421..f93e7cfacab6 100755\n--- a/utils/rkisp1/gen-csc-table.py\n+++ b/utils/rkisp1/gen-csc-table.py\n@@ -7,6 +7,7 @@\n \n import argparse\n import enum\n+import numpy as np\n import sys\n \n \n@@ -63,9 +64,8 @@ class Quantization(enum.Enum):\n     LIMITED = 1\n \n \n-def scale_coeff(coeff, quantization, luma, precision):\n-    \"\"\"Scale a coefficient to the output range dictated by the quantization and\n-    the precision.\n+def scale_coeff(coeff, quantization, luma):\n+    \"\"\"Scale a coefficient to the output range dictated by the quantization.\n \n     Parameters\n     ----------\n@@ -75,9 +75,6 @@ def scale_coeff(coeff, quantization, luma, precision):\n         The quantization, either FULL or LIMITED\n     luma : bool\n         True if the coefficient corresponds to a luma value, False otherwise\n-    precision : int\n-        The desired precision for the scaled coefficient as a number of\n-        fractional bits\n     \"\"\"\n \n     # Assume the input range is 8 bits. The output range is set by the\n@@ -91,7 +88,7 @@ def scale_coeff(coeff, quantization, luma, precision):\n     else:\n         out_range = 240 - 16\n \n-    return coeff * out_range / in_range * (1 << precision)\n+    return coeff * out_range / in_range\n \n \n def round_array(values):\n@@ -150,6 +147,8 @@ def main(argv):\n         description='Generate color space conversion table coefficients with '\n         'configurable fixed-point precision.'\n     )\n+    parser.add_argument('--invert', '-i', action='store_true',\n+                        help='Invert the color space conversion (YUV -> RGB)')\n     parser.add_argument('--precision', '-p', default='Q1.7',\n                         help='The output fixed point precision in Q notation (sign bit excluded)')\n     parser.add_argument('--quantization', '-q', choices=['full', 'limited'],\n@@ -171,13 +170,25 @@ def main(argv):\n     luma = True\n     scaled_coeffs = []\n     for line in encoding:\n-        line = [scale_coeff(coeff, quantization, luma, precision.fractional) for coeff in line]\n+        line = [scale_coeff(coeff, quantization, luma) for coeff in line]\n         scaled_coeffs.append(line)\n         luma = False\n \n+    if args.invert:\n+        scaled_coeffs = np.linalg.inv(scaled_coeffs)\n+\n     rounded_coeffs = []\n     for line in scaled_coeffs:\n-        line = round_array(line)\n+        line = [coeff * (1 << precision.fractional) for coeff in line]\n+        # For the RGB to YUV conversion, use a rounding method that preserves\n+        # the rounded sum of each line to avoid biases and overflow, as the sum\n+        # of luma and chroma coefficients should be 1.0 and 0.0 respectively\n+        # (in full range). The the YUV to RGB conversion, there is no such\n+        # constraint, so use simple rounding.\n+        if args.invert:\n+            line = [round(coeff) for coeff in line]\n+        else:\n+            line = round_array(line)\n \n         # Convert coefficients to the number of bits selected by the precision.\n         # Negative values will be turned into positive integers using 2's\n@@ -188,7 +199,7 @@ def main(argv):\n     # Print the result as C code.\n     nbits = 1 << (precision.total - 1).bit_length()\n     nbytes = nbits // 4\n-    print(f'static const u{nbits} rgb2yuv_{args.encoding}_{quantization.name.lower()}_coeffs[] = {{')\n+    print(f'static const u{nbits} {\"yuv2rgb\" if args.invert else \"rgb2yuv\"}_{args.encoding}_{quantization.name.lower()}_coeffs[] = {{')\n \n     for line in rounded_coeffs:\n         line = [f'0x{coeff:0{nbytes}x}' for coeff in line]\n","prefixes":["libcamera-devel","v2"]}