From patchwork Fri Sep 30 19:07:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 17474 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 38AD9BD16B for ; Fri, 30 Sep 2022 19:07:16 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 9A84261FB1; Fri, 30 Sep 2022 21:07:15 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1664564835; bh=zj4xwm8q2INH9HJx6q/F/R0p16rKF6JZrkj+7MP2P4o=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=P9mmpQIc2hPq6EJSYgl8u4F96+C+Qkignb7uYSD0wX9RMcqufTiGAsJZzdZVKnh1+ ReaiZMyy8Gn4/NB+6r7wUMyRByQwqhkUrCWuEcFazkvJnfIxCgii93sW4j4G04Yovi Z8027vxUJQFW23cscR+90a2c8ZzHAmCFZrAkXcAXTo5P6VqWPYLne9tR4yu6dBhUf7 IX4cgmgd/lvxwzpWZyRxf3vdbfnvyOMv+b06+jYt4hZI2at75lD1td+jdEqMbZkksR i3EEPwawXhCI/sMmOSeVGAk3pz/Y4Jl/i7EIfBMmZONQ9PIjIN2jN4PfFViCmc6Y4I GKN6QBLjpOXzw== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id C257561FB1 for ; Fri, 30 Sep 2022 21:07:13 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="qazsEk7x"; dkim-atps=neutral Received: from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 3B6654D5 for ; Fri, 30 Sep 2022 21:07:13 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1664564833; bh=zj4xwm8q2INH9HJx6q/F/R0p16rKF6JZrkj+7MP2P4o=; h=From:To:Subject:Date:From; b=qazsEk7x9wiI1ixlw6Y2J52ayISQe+febR9Y+kgbyjn7j4a2x1QxYX21F4n1KnEr2 0yZuzi16qHuUKqAZhSZ73GAokbEnn/lohRw1G4wWwLqr7yPeN+Pk6OXBUuvFQL510q LMWkIdg01hr0brZphCLruTNsPfQZwQIMGvUJ8X1M= 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 Subject: [libcamera-devel] [PATCH v2] utils: rkisp1: gen-csc-table: Add support for inverting the CSC 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: , X-Patchwork-Original-From: Laurent Pinchart via libcamera-devel From: Laurent Pinchart Reply-To: Laurent Pinchart Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Add a -i/--invert command line argument to invert the YCbCr encoding and output a YCbCr to RGB matrix. Signed-off-by: Laurent Pinchart Reviewed-by: Jacopo Mondi Reviewed-by: Kieran Bingham --- Changes since v1: - Fix inverted CSC calculation - Perform simple rounding for inverted CSC --- utils/rkisp1/gen-csc-table.py | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) base-commit: ed591e705c451d0ce14988ae96829a31a2ae2f9a diff --git a/utils/rkisp1/gen-csc-table.py b/utils/rkisp1/gen-csc-table.py index 2fb702746421..f93e7cfacab6 100755 --- a/utils/rkisp1/gen-csc-table.py +++ b/utils/rkisp1/gen-csc-table.py @@ -7,6 +7,7 @@ import argparse import enum +import numpy as np import sys @@ -63,9 +64,8 @@ class Quantization(enum.Enum): LIMITED = 1 -def scale_coeff(coeff, quantization, luma, precision): - """Scale a coefficient to the output range dictated by the quantization and - the precision. +def scale_coeff(coeff, quantization, luma): + """Scale a coefficient to the output range dictated by the quantization. Parameters ---------- @@ -75,9 +75,6 @@ def scale_coeff(coeff, quantization, luma, precision): The quantization, either FULL or LIMITED luma : bool True if the coefficient corresponds to a luma value, False otherwise - precision : int - The desired precision for the scaled coefficient as a number of - fractional bits """ # Assume the input range is 8 bits. The output range is set by the @@ -91,7 +88,7 @@ def scale_coeff(coeff, quantization, luma, precision): else: out_range = 240 - 16 - return coeff * out_range / in_range * (1 << precision) + return coeff * out_range / in_range def round_array(values): @@ -150,6 +147,8 @@ def main(argv): description='Generate color space conversion table coefficients with ' 'configurable fixed-point precision.' ) + parser.add_argument('--invert', '-i', action='store_true', + help='Invert the color space conversion (YUV -> RGB)') parser.add_argument('--precision', '-p', default='Q1.7', help='The output fixed point precision in Q notation (sign bit excluded)') parser.add_argument('--quantization', '-q', choices=['full', 'limited'], @@ -171,13 +170,25 @@ def main(argv): luma = True scaled_coeffs = [] for line in encoding: - line = [scale_coeff(coeff, quantization, luma, precision.fractional) for coeff in line] + line = [scale_coeff(coeff, quantization, luma) for coeff in line] scaled_coeffs.append(line) luma = False + if args.invert: + scaled_coeffs = np.linalg.inv(scaled_coeffs) + rounded_coeffs = [] for line in scaled_coeffs: - line = round_array(line) + line = [coeff * (1 << precision.fractional) for coeff in line] + # For the RGB to YUV conversion, use a rounding method that preserves + # the rounded sum of each line to avoid biases and overflow, as the sum + # of luma and chroma coefficients should be 1.0 and 0.0 respectively + # (in full range). The the YUV to RGB conversion, there is no such + # constraint, so use simple rounding. + if args.invert: + line = [round(coeff) for coeff in line] + else: + line = round_array(line) # Convert coefficients to the number of bits selected by the precision. # Negative values will be turned into positive integers using 2's @@ -188,7 +199,7 @@ def main(argv): # Print the result as C code. nbits = 1 << (precision.total - 1).bit_length() nbytes = nbits // 4 - print(f'static const u{nbits} rgb2yuv_{args.encoding}_{quantization.name.lower()}_coeffs[] = {{') + print(f'static const u{nbits} {"yuv2rgb" if args.invert else "rgb2yuv"}_{args.encoding}_{quantization.name.lower()}_coeffs[] = {{') for line in rounded_coeffs: line = [f'0x{coeff:0{nbytes}x}' for coeff in line]