[{"id":25196,"web_url":"https://patchwork.libcamera.org/comment/25196/","msgid":"<166456595329.4106166.1506615189253633993@Monstersaurus>","date":"2022-09-30T19:25:53","subject":"Re: [libcamera-devel] [PATCH v2] utils: rkisp1: gen-csc-table: Add\n\tsupport for inverting the CSC","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"Quoting Laurent Pinchart via libcamera-devel (2022-09-30 20:07:11)\n> Add a -i/--invert command line argument to invert the YCbCr encoding and\n> output a YCbCr to RGB matrix.\n> \n> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>\n> ---\n> Changes 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> diff --git a/utils/rkisp1/gen-csc-table.py b/utils/rkisp1/gen-csc-table.py\n> index 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\nThe the\n\nPresumably that should be 'In the', or 'For the'.\n\n\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\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> \n> base-commit: ed591e705c451d0ce14988ae96829a31a2ae2f9a\n> -- \n> Regards,\n> \n> Laurent Pinchart\n>","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 B03F9C0DA4\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 30 Sep 2022 19:25:58 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id D281162840;\n\tFri, 30 Sep 2022 21:25:57 +0200 (CEST)","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 8429F61FB1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 30 Sep 2022 21:25:56 +0200 (CEST)","from pendragon.ideasonboard.com\n\t(cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id EEFFF47C;\n\tFri, 30 Sep 2022 21:25:55 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1664565957;\n\tbh=bQbSKU87vOOWcf0pCxroIk2f5l+AvQbI2LNqeWemJa8=;\n\th=In-Reply-To:References:To:Date:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:\n\tFrom;\n\tb=dh9j0rBTVkDjPrLANVAsR4DlnZgBOXSsK/4t0Y2+jyonU26yC/swg/xbXTANKEhE5\n\tomt7/key2JY4mfKBOzuzEcfd++wozu1zhiX1iAg1hhdZHRdwCjwHuEhcO/2yH3v2RJ\n\tOcv3KFpPRMZmv5eXxa+8QHRcfRq2ZkzUKIglBJmFd20xT5UEFC8VAEo+35kIFDDH+2\n\tqUPX4lwr4IV+4Rl/88PG8U7ROW8gyuIdFS/Sb+/AP/Rf1fD6+LUqqGrepYOtvNsmQc\n\tusoasiMTHnRVdhUrur1O3LC1HVT/Dfobj4dmrchSuNswMMV61S31Bl6UJlncueW87+\n\txNjmBx978Ksig==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1664565956;\n\tbh=bQbSKU87vOOWcf0pCxroIk2f5l+AvQbI2LNqeWemJa8=;\n\th=In-Reply-To:References:Subject:From:To:Date:From;\n\tb=Yogcy7wz0iAH+82+I4h3Qqj3QlTlPWb/eH5QGqHeb3HqEKTnd9pDQPFuSL9Bx4mHt\n\t2f+qqW9bOEGggBuqHXngO6h0knMoA9D1TJbtitmUmQzqQw/7Y7hr1rGHQDZ6IoWDXd\n\tJ9EYtrVOWMCyjLRIbh6NZ9XLTloDxMz05qR2VLEI="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"Yogcy7wz\"; dkim-atps=neutral","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<20220930190711.13834-1-laurent.pinchart@ideasonboard.com>","References":"<20220930190711.13834-1-laurent.pinchart@ideasonboard.com>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>,\n\tlibcamera-devel@lists.libcamera.org","Date":"Fri, 30 Sep 2022 20:25:53 +0100","Message-ID":"<166456595329.4106166.1506615189253633993@Monstersaurus>","User-Agent":"alot/0.10","Subject":"Re: [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":"Kieran Bingham via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]