From patchwork Mon Sep 1 09:23:19 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 24275 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 E0A23BEFBE for ; Mon, 1 Sep 2025 09:23:26 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id D636E6931D; Mon, 1 Sep 2025 11:23:25 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="KHi/yhpD"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id C07EC613AC for ; Mon, 1 Sep 2025 11:23:23 +0200 (CEST) Received: from pb-laptop.local (185.221.143.232.nat.pool.zt.hu [185.221.143.232]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 75A9DEFE for ; Mon, 1 Sep 2025 11:22:16 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1756718536; bh=0rxqnRB5u+DCnovjRE0yZuRESlQ+IIZfqZyRqOKW8+8=; h=From:To:Subject:Date:From; b=KHi/yhpD387Y79QB+OfLEczHuJQTRedqoCwyN5WzwHhL0Blbi5to7WA2H2fb/cdeq 6Eg0DdRDex/y30Jg2wBzmjuIlK9va/xQa09xJnhwUiEEBgt8SLGn0y00OcEEAZmDq2 mln3Qo2ShW4wI67hwIMOGVaT1ihJrRdvteSOxGcc= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Subject: [PATCH v1 1/2] utils: codegen: gen-formats.py: Use jinja Date: Mon, 1 Sep 2025 11:23:19 +0200 Message-ID: <20250901092320.44615-1-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.51.0 MIME-Version: 1.0 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: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Currently the gen-formats.py script can only be used to generate C++ code because it hard-codes part of the template. Use jinja to fully remove any such dependency. Signed-off-by: Barnabás Pőcze Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- include/libcamera/formats.h.in | 4 +++- utils/codegen/gen-formats.py | 34 ++++++++++++---------------------- 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/include/libcamera/formats.h.in b/include/libcamera/formats.h.in index 6ae7634fe..5ff9c3bf4 100644 --- a/include/libcamera/formats.h.in +++ b/include/libcamera/formats.h.in @@ -35,7 +35,9 @@ constexpr uint64_t __mod(unsigned int vendor, unsigned int mod) } /* namespace */ -${formats} +{% for f in formats %} +constexpr PixelFormat {{f.name}}(__fourcc({{f.fourcc}}), __mod({{f.mod}})); +{%- endfor %} } /* namespace formats */ diff --git a/utils/codegen/gen-formats.py b/utils/codegen/gen-formats.py index 0c0932a5b..872f3fe34 100755 --- a/utils/codegen/gen-formats.py +++ b/utils/codegen/gen-formats.py @@ -7,6 +7,7 @@ # Generate formats definitions from YAML import argparse +import jinja2 import re import string import sys @@ -52,9 +53,7 @@ class DRMFourCC(object): return self.vendors[vendor], value -def generate_h(formats, drm_fourcc): - template = string.Template('constexpr PixelFormat ${name}{ __fourcc(${fourcc}), __mod(${mod}) };') - +def generate_formats(formats, drm_fourcc): fmts = [] for format in formats: @@ -73,24 +72,17 @@ def generate_h(formats, drm_fourcc): if mod: data['mod'] = '%u, %u' % drm_fourcc.mod(mod) - fmts.append(template.substitute(data)) - - return {'formats': '\n'.join(fmts)} - - -def fill_template(template, data): + fmts.append(data) - template = open(template, 'rb').read() - template = template.decode('utf-8') - template = string.Template(template) - return template.substitute(data) + return fmts def main(argv): # Parse command line arguments parser = argparse.ArgumentParser() - parser.add_argument('-o', dest='output', metavar='file', type=str, + parser.add_argument('-o', dest='output', metavar='file', + type=argparse.FileType('w', encoding='utf-8'), default=sys.stdout, help='Output file name. Defaults to standard output if not specified.') parser.add_argument('input', type=str, help='Input file name.') @@ -104,15 +96,13 @@ def main(argv): formats = yaml.safe_load(data)['formats'] drm_fourcc = DRMFourCC(args.drm_fourcc) - data = generate_h(formats, drm_fourcc) - data = fill_template(args.template, data) + env = jinja2.Environment() + template = env.from_string(open(args.template, 'r', encoding='utf-8').read()) + string = template.render({ + 'formats': generate_formats(formats, drm_fourcc), + }) - if args.output: - output = open(args.output, 'wb') - output.write(data.encode('utf-8')) - output.close() - else: - sys.stdout.write(data) + args.output.write(string) return 0 From patchwork Mon Sep 1 09:23:20 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 24276 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 3AB56BEFBE for ; Mon, 1 Sep 2025 09:23:29 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 75E4669333; Mon, 1 Sep 2025 11:23:26 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="wUURHAgk"; dkim-atps=neutral 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 EDDC46931D for ; Mon, 1 Sep 2025 11:23:23 +0200 (CEST) Received: from pb-laptop.local (185.221.143.232.nat.pool.zt.hu [185.221.143.232]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id B5D3BF52 for ; Mon, 1 Sep 2025 11:22:16 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1756718536; bh=JYdj6/BAO7KfMuGdl9thg4EuFbQ99vcs6KN7hhGUGEY=; h=From:To:Subject:Date:In-Reply-To:References:From; b=wUURHAgknIKRTxJc7pMMl+z/SJZ4hYAG5i6FOVegxhP5oROFqspYYps3WYXGKcZct BikSK8TaTsvPgbRIVU2GvDOgjeiB5nVGSo8C0D7zEigyztHcwSk1miLYJJcvG6n1gn vGO7l8d/oiHp2USNMmvmJSXRXaKQOgOTZ9X7QXm8= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Subject: [PATCH v1 2/2] utils: codegen: gen-formats.py: Fix big endian formats Date: Mon, 1 Sep 2025 11:23:20 +0200 Message-ID: <20250901092320.44615-2-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20250901092320.44615-1-barnabas.pocze@ideasonboard.com> References: <20250901092320.44615-1-barnabas.pocze@ideasonboard.com> MIME-Version: 1.0 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: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" First there is a single big endian format defined in `formats.yaml`: RGB565_BE. However, while the yaml file specifies "big_endian: true", the python script looks for a key named "big-endian". Causing `RGB565{,_BE}` both to be the same. Second, the python script simply appends " | DRM_FORMAT_BIG_ENDIAN" to the fourcc of the format. However, there is no definition of that macro is available in the only user, `formats.h.in`. Fix the first one by checking for "big_endian" in the script as well, and fix the second one by defining a constant with the same value and using that. Signed-off-by: Barnabás Pőcze Fixes: 7c496f1c54dd58 ("utils: gen-formats: Support big-endian DRM formats") Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- include/libcamera/formats.h.in | 4 +++- utils/codegen/gen-formats.py | 3 +-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/include/libcamera/formats.h.in b/include/libcamera/formats.h.in index 5ff9c3bf4..c821ffbb1 100644 --- a/include/libcamera/formats.h.in +++ b/include/libcamera/formats.h.in @@ -33,10 +33,12 @@ constexpr uint64_t __mod(unsigned int vendor, unsigned int mod) (static_cast(mod) << 0); } +constexpr uint32_t kDrmFormatBigEndian = uint32_t(1) << 31; /* DRM_FORMAT_BIG_ENDIAN */ + } /* namespace */ {% for f in formats %} -constexpr PixelFormat {{f.name}}(__fourcc({{f.fourcc}}), __mod({{f.mod}})); +constexpr PixelFormat {{f.name}}(__fourcc({{f.fourcc}}){{ ' | kDrmFormatBigEndian' if f.big_endian }}, __mod({{f.mod}})); {%- endfor %} } /* namespace formats */ diff --git a/utils/codegen/gen-formats.py b/utils/codegen/gen-formats.py index 872f3fe34..01adf5e1f 100755 --- a/utils/codegen/gen-formats.py +++ b/utils/codegen/gen-formats.py @@ -59,13 +59,12 @@ def generate_formats(formats, drm_fourcc): for format in formats: name, format = format.popitem() fourcc = drm_fourcc.fourcc(format['fourcc']) - if format.get('big-endian'): - fourcc += '| DRM_FORMAT_BIG_ENDIAN' data = { 'name': name, 'fourcc': fourcc, 'mod': '0, 0', + 'big_endian': format.get('big_endian') == True, } mod = format.get('mod')