[libcamera-devel] [PATCH] utils: rkisp1: gen-csc-table: Add support for inverting the CSC

Laurent Pinchart laurent.pinchart at ideasonboard.com
Wed Sep 28 01:27:07 CEST 2022


Add a -i/--invert command line argument to invert the YCbCr encoding and
output a YCbCr to RGB matrix.

Signed-off-by: Laurent Pinchart <laurent.pinchart at ideasonboard.com>
---
 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]
-- 
Regards,

Laurent Pinchart



More information about the libcamera-devel mailing list