{"id":17537,"url":"https://patchwork.libcamera.org/api/1.1/patches/17537/?format=json","web_url":"https://patchwork.libcamera.org/patch/17537/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20221006120105.3861831-5-paul.elder@ideasonboard.com>","date":"2022-10-06T12:01:02","name":"[libcamera-devel,4/7] utils: libtuning: generators: Add raspberrypi output","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"ff9776fdb19f79c010243140a10fda5e7d5faaf7","submitter":{"id":17,"url":"https://patchwork.libcamera.org/api/1.1/people/17/?format=json","name":"Paul Elder","email":"paul.elder@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/17537/mbox/","series":[{"id":3536,"url":"https://patchwork.libcamera.org/api/1.1/series/3536/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3536","date":"2022-10-06T12:00:58","name":"utils: tuning: Add a new tuning infrastructure","version":1,"mbox":"https://patchwork.libcamera.org/series/3536/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/17537/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/17537/checks/","tags":{},"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 E467BC0DA4\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu,  6 Oct 2022 12:01:27 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id A004962CFE;\n\tThu,  6 Oct 2022 14:01:27 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 143A962CF7\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu,  6 Oct 2022 14:01:26 +0200 (CEST)","from pyrite.rasen.tech (h175-177-042-159.catv02.itscom.jp\n\t[175.177.42.159])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 8FDF5A42;\n\tThu,  6 Oct 2022 14:01:24 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1665057687;\n\tbh=K74rGkxTIsp5mWnp6ujEQtDW4zStQUuyvk9gON1M/c4=;\n\th=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:\n\tFrom;\n\tb=FjLkX/M6Xcl5IlS02BeB14WkXs7/DgnbYgHMzw5lECuH+l0BwVj0P+RoBYtg4J9Lx\n\txTy/YbFAgXiAqIH/DkplhYdz+dYINcxhL4lRHe40i7lZfi4qdFOAzdaPTBZp7gZ59H\n\t08hnqzm53nGYRW7C6sxi8DDFAcwk4yR9q86K31pRaU7XoIe10M+HdEHqupAs9od7l6\n\t8U7Dg/bvceJHWku6X0Z5IP11ySjm4eq0zEqUTHQx+pKI2fetH+IIuuWvhSWAXdRsTw\n\tqa4JEjBZnoDCujeEKZmA6FHgg8CDzE08JBsdZAHqj9O8HFwBO+epQKFPd91KoQV3x0\n\texBoKgijntpYQ==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1665057685;\n\tbh=K74rGkxTIsp5mWnp6ujEQtDW4zStQUuyvk9gON1M/c4=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=nvWu2JuLLDleu1q9MGBvazPbXVdShHfI9beOpdbHfk3S66h1nvuZ3JimW1xX5D2Ts\n\tftlbOl8az5qWmCtmPaMjAb3J2tMOrqzBMvFYfVmw0Di+2EwI3L1OqWG72KVC+qqttK\n\tLWNL6RkDSLvMlpGlEf4cJZ5LKmS6XJY2ZjIYPv/k="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"nvWu2JuL\"; dkim-atps=neutral","To":"libcamera-devel@lists.libcamera.org","Date":"Thu,  6 Oct 2022 21:01:02 +0900","Message-Id":"<20221006120105.3861831-5-paul.elder@ideasonboard.com>","X-Mailer":"git-send-email 2.30.2","In-Reply-To":"<20221006120105.3861831-1-paul.elder@ideasonboard.com>","References":"<20221006120105.3861831-1-paul.elder@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH 4/7] utils: libtuning: generators: Add\n\traspberrypi output","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":"Paul Elder via libcamera-devel <libcamera-devel@lists.libcamera.org>","Reply-To":"Paul Elder <paul.elder@ideasonboard.com>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Add a generator to libtuning for writing tuning output to a json file\nformatted the same way that raspberrypi's ctt formats them.\n\nSigned-off-by: Paul Elder <paul.elder@ideasonboard.com>\n---\n utils/tuning/libtuning/generators/__init__.py |   1 +\n .../generators/raspberrypi_output.py          | 115 ++++++++++++++++++\n 2 files changed, 116 insertions(+)\n create mode 100644 utils/tuning/libtuning/generators/raspberrypi_output.py","diff":"diff --git a/utils/tuning/libtuning/generators/__init__.py b/utils/tuning/libtuning/generators/__init__.py\nindex e69de29b..50064196 100644\n--- a/utils/tuning/libtuning/generators/__init__.py\n+++ b/utils/tuning/libtuning/generators/__init__.py\n@@ -0,0 +1 @@\n+from libtuning.generators.raspberrypi_output import RaspberryPiOutput\ndiff --git a/utils/tuning/libtuning/generators/raspberrypi_output.py b/utils/tuning/libtuning/generators/raspberrypi_output.py\nnew file mode 100644\nindex 00000000..d5cc0c48\n--- /dev/null\n+++ b/utils/tuning/libtuning/generators/raspberrypi_output.py\n@@ -0,0 +1,115 @@\n+# SPDX-License-Identifier: BSD-2-Clause\n+#\n+# Copyright 2022 Raspberry Pi Ltd\n+#\n+# Script to pretty print a Raspberry Pi tuning config JSON structure in\n+# version 2.0 and later formats.\n+# (Copied from ctt_pretty_print_json.py)\n+\n+from .generator import Generator\n+\n+import json\n+from pathlib import Path\n+import textwrap\n+\n+\n+class RaspberryPiOutput(Generator):\n+    def __init__(self):\n+        super().__init__()\n+\n+    def __write__(self, output_file: Path, output_dict: dict, output_order: list):\n+        # Write json dictionary to file using ctt's version 2 format\n+        out_json = {\n+            \"version\": 2.0,\n+            'target': 'bcm2835',\n+            \"algorithms\": [{f'rpi.{module.name}': output_dict[module]} for module in output_order]\n+        }\n+\n+        with open(output_file, 'w') as f:\n+            f.write(pretty_print(out_json))\n+\n+\n+class Encoder(json.JSONEncoder):\n+\n+    def __init__(self, *args, **kwargs):\n+        super().__init__(*args, **kwargs)\n+        self.indentation_level = 0\n+        self.hard_break = 120\n+        self.custom_elems = {\n+            'table': 16,\n+            'luminance_lut': 16,\n+            'ct_curve': 3,\n+            'ccm': 3,\n+            'gamma_curve': 2,\n+            'y_target': 2,\n+            'prior': 2\n+        }\n+\n+    def encode(self, o, node_key=None):\n+        if isinstance(o, (list, tuple)):\n+            # Check if we are a flat list of numbers.\n+            if not any(isinstance(el, (list, tuple, dict)) for el in o):\n+                s = ', '.join(json.dumps(el) for el in o)\n+                if node_key in self.custom_elems.keys():\n+                    # Special case handling to specify number of elements in a row for tables, ccm, etc.\n+                    self.indentation_level += 1\n+                    sl = s.split(', ')\n+                    num = self.custom_elems[node_key]\n+                    chunk = [self.indent_str + ', '.join(sl[x:x + num]) for x in range(0, len(sl), num)]\n+                    t = ',\\n'.join(chunk)\n+                    self.indentation_level -= 1\n+                    output = f'\\n{self.indent_str}[\\n{t}\\n{self.indent_str}]'\n+                elif len(s) > self.hard_break - len(self.indent_str):\n+                    # Break a long list with wraps.\n+                    self.indentation_level += 1\n+                    t = textwrap.fill(s, self.hard_break, break_long_words=False,\n+                                      initial_indent=self.indent_str, subsequent_indent=self.indent_str)\n+                    self.indentation_level -= 1\n+                    output = f'\\n{self.indent_str}[\\n{t}\\n{self.indent_str}]'\n+                else:\n+                    # Smaller lists can remain on a single line.\n+                    output = f' [ {s} ]'\n+                return output\n+            else:\n+                # Sub-structures in the list case.\n+                self.indentation_level += 1\n+                output = [self.indent_str + self.encode(el) for el in o]\n+                self.indentation_level -= 1\n+                output = ',\\n'.join(output)\n+                return f' [\\n{output}\\n{self.indent_str}]'\n+\n+        elif isinstance(o, dict):\n+            self.indentation_level += 1\n+            output = []\n+            for k, v in o.items():\n+                if isinstance(v, dict) and len(v) == 0:\n+                    # Empty config block special case.\n+                    output.append(self.indent_str + f'{json.dumps(k)}: {{ }}')\n+                else:\n+                    # Only linebreak if the next node is a config block.\n+                    sep = f'\\n{self.indent_str}' if isinstance(v, dict) else ''\n+                    output.append(self.indent_str + f'{json.dumps(k)}:{sep}{self.encode(v, k)}')\n+            output = ',\\n'.join(output)\n+            self.indentation_level -= 1\n+            return f'{{\\n{output}\\n{self.indent_str}}}'\n+\n+        else:\n+            return ' ' + json.dumps(o)\n+\n+    @property\n+    def indent_str(self) -> str:\n+        return ' ' * self.indentation_level * self.indent\n+\n+    def iterencode(self, o, **kwargs):\n+        return self.encode(o)\n+\n+\n+def pretty_print(in_json: dict) -> str:\n+\n+    if 'version' not in in_json or \\\n+       'target' not in in_json or \\\n+       'algorithms' not in in_json or \\\n+       in_json['version'] < 2.0:\n+        raise RuntimeError('Incompatible JSON dictionary has been provided')\n+\n+    return json.dumps(in_json, cls=Encoder, indent=4, sort_keys=False)\n","prefixes":["libcamera-devel","4/7"]}