{"id":17779,"url":"https://patchwork.libcamera.org/api/patches/17779/?format=json","web_url":"https://patchwork.libcamera.org/patch/17779/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/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":"<20221110173154.488445-9-paul.elder@ideasonboard.com>","date":"2022-11-10T17:31:50","name":"[libcamera-devel,v3,08/12] utils: libtuning: generators: Add raspberrypi output","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"70ae6b71fcefbb837997553a3711d910e5931c70","submitter":{"id":17,"url":"https://patchwork.libcamera.org/api/people/17/?format=json","name":"Paul Elder","email":"paul.elder@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/17779/mbox/","series":[{"id":3603,"url":"https://patchwork.libcamera.org/api/series/3603/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3603","date":"2022-11-10T17:31:42","name":"utils: tuning: Add a new tuning infrastructure","version":3,"mbox":"https://patchwork.libcamera.org/series/3603/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/17779/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/17779/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 E3851BD16B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 10 Nov 2022 17:32:34 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 9B77E63098;\n\tThu, 10 Nov 2022 18:32:34 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id C8DA66307B\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 10 Nov 2022 18:32:32 +0100 (CET)","from pyrite.tail37cf.ts.net (h175-177-042-159.catv02.itscom.jp\n\t[175.177.42.159])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 1AAA132A;\n\tThu, 10 Nov 2022 18:32:30 +0100 (CET)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1668101554;\n\tbh=CuU1K1m8mrxqBjkaDa8ED9wVDqe4niKSyF1qiHslvto=;\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=Q7As18XXhU/3VqOWRP4TwN5KsqCSt7C3zZZ9YbK8SzQ0pO7PbcuuSsFLPvJbluY64\n\tkRbQppJJTVmorEdDK6xvBEUIgKXJ53UiD0vzFYDCn6r0asLGo0qSQ9uudL3A5UWBdx\n\tA6EUdtcUrUmm/G0r12sMdaDgqE8FC78wM3nTXl/9lep1DUElIbBqxA8uBSg7Lv6Bwx\n\tjbLN8KFGKUG2mhfxkE/O0Kn2E7CWnAPAMFLh6e5RzroLCbonbhByEUlfwKX8Wt/2wy\n\tsDCTm68M7s2K6hQXd2WVzoZCIUuC/bPsNf/IuZRVih494Ha9temtQZMQm1A1BkM++P\n\tJOxDL4zbgEizg==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1668101552;\n\tbh=CuU1K1m8mrxqBjkaDa8ED9wVDqe4niKSyF1qiHslvto=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=PONOyjPvW+hNVxcAR1jKXfiCSCceYAMAzSm/hZfLAW5kyfKGdQeMByDNPZT7JE26x\n\t9tS3+dRp2NA8v+IQ5iNcb+l4c7quGwsuIJ4JBF/+TUU0cdLiMT/rRVp0ES6oW4VRAp\n\tFLbx6O38sJEBRdzK1WiwyICk3gXY1/IiObxgweKY="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"PONOyjPv\"; dkim-atps=neutral","To":"libcamera-devel@lists.libcamera.org","Date":"Fri, 11 Nov 2022 02:31:50 +0900","Message-Id":"<20221110173154.488445-9-paul.elder@ideasonboard.com>","X-Mailer":"git-send-email 2.35.1","In-Reply-To":"<20221110173154.488445-1-paul.elder@ideasonboard.com>","References":"<20221110173154.488445-1-paul.elder@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v3 08/12] utils: libtuning: generators:\n\tAdd raspberrypi 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>\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n---\nChanges in v3:\n- move pretty_print into a private class member\n- move RaspberryPiOutput to end of file\n- add file descriptions\n- remove indirection from fake polymorphism\n\nChanges in v2:\n- add SPDX and copyright\n- fix style\n- move the 'rpi.' prefix of module names in the tuning output from here\n  to into the Modules' out_name field\n---\n utils/tuning/libtuning/generators/__init__.py |   5 +\n .../generators/raspberrypi_output.py          | 114 ++++++++++++++++++\n 2 files changed, 119 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..937aff30 100644\n--- a/utils/tuning/libtuning/generators/__init__.py\n+++ b/utils/tuning/libtuning/generators/__init__.py\n@@ -0,0 +1,5 @@\n+# SPDX-License-Identifier: GPL-2.0-or-later\n+#\n+# Copyright (C) 2022, Paul Elder <paul.elder@ideasonboard.com>\n+\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..813491cd\n--- /dev/null\n+++ b/utils/tuning/libtuning/generators/raspberrypi_output.py\n@@ -0,0 +1,114 @@\n+# SPDX-License-Identifier: BSD-2-Clause\n+#\n+# Copyright 2022 Raspberry Pi Ltd\n+#\n+# raspberrypi_output.py - Generate tuning file in Raspberry Pi's json format\n+#\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 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+class RaspberryPiOutput(Generator):\n+    def __init__(self):\n+        super().__init__()\n+\n+    def _pretty_print(self, 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+\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'{module.out_name}': output_dict[module]} for module in output_order]\n+        }\n+\n+        with open(output_file, 'w') as f:\n+            f.write(self._pretty_print(out_json))\n","prefixes":["libcamera-devel","v3","08/12"]}