From patchwork Tue Sep 27 23:27:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 17446 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 E12DAC0DA4 for ; Tue, 27 Sep 2022 23:27:11 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 5063D62278; Wed, 28 Sep 2022 01:27:11 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1664321231; bh=R5bt5TXxHrzzh1Qg+LdKepAPSBvgqbe2AZ1TEbyWkgY=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=pMO9pbXv9PojHxBSreBCyVSToaTDrO83f8H6oI+BlulpzfttefF7od+Xy4aErxCT+ x3asEYiwq+Hk7YxU2xdkU6u7gLUNqOyvSrEDaGuKlUVyZgJN1jamP9TyRBvv+zWcFq kR9ATUp9k/ZxEUOCQUgeAHIvFRP7NpkRODnZqiognK4S04PnM76C5kq0sDwaOOA+2E YIpNE5Yz0yf4Txj5EZRnQV+OqPbQ8MJE3SI+4R2MpCrVg0Pzp1NvVrW8dcPY7iTbIh mPdjfN9b8BbnbXRY/+y44nvmNi9JFAYr6JSI1FSbCilaZ3VEovGpAT9LeRpm6uoyeN AO9f9kPWknD8w== 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 36D2E61F78 for ; Wed, 28 Sep 2022 01:27:09 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="EdBriv0v"; 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 B549447C for ; Wed, 28 Sep 2022 01:27:08 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1664321228; bh=R5bt5TXxHrzzh1Qg+LdKepAPSBvgqbe2AZ1TEbyWkgY=; h=From:To:Subject:Date:From; b=EdBriv0vcoaoNXHSOpHfYTsWdTEoSxXRixOXaX2yKdLIDtBrta1QZJFxHzKP0eSWD dINhXwnRgD4drcwo2v286+uREfqhWk9Tb/Vyb/H3oCvjQPdlG/wmZh7xFwrOi/jAC2 sCVWdoiB3hSO4XyTF5q+nVXKOevZxzvwuFvjKGyU= To: libcamera-devel@lists.libcamera.org Date: Wed, 28 Sep 2022 02:27:07 +0300 Message-Id: <20220927232707.7596-1-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH] 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 --- utils/rkisp1/gen-csc-table.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/utils/rkisp1/gen-csc-table.py b/utils/rkisp1/gen-csc-table.py index 2fb702746421..83aea357966a 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,7 +64,7 @@ class Quantization(enum.Enum): LIMITED = 1 -def scale_coeff(coeff, quantization, luma, precision): +def scale_coeff(coeff, quantization, luma, precision, invert): """Scale a coefficient to the output range dictated by the quantization and the precision. @@ -91,6 +92,9 @@ def scale_coeff(coeff, quantization, luma, precision): else: out_range = 240 - 16 + if invert: + in_range, out_range = out_range, in_range + return coeff * out_range / in_range * (1 << precision) @@ -150,6 +154,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'], @@ -166,12 +172,15 @@ def main(argv): encoding = encodings[args.encoding] quantization = Quantization[args.quantization.upper()] + if args.invert: + encoding = np.linalg.inv(encoding) + # Scale and round the encoding coefficients based on the precision and # quantization range. 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, precision.fractional, args.invert) for coeff in line] scaled_coeffs.append(line) luma = False @@ -188,7 +197,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]