[{"id":25751,"web_url":"https://patchwork.libcamera.org/comment/25751/","msgid":"<Y2uCMRbHbveCNY3S@pendragon.ideasonboard.com>","date":"2022-11-09T10:34:25","subject":"Re: [libcamera-devel] [PATCH v2 06/11] utils: libtuning:\n\tgenerators: Add raspberrypi output","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Paul,\n\nThank you for the patch.\n\nOn Sat, Oct 22, 2022 at 03:23:05PM +0900, Paul Elder via libcamera-devel wrote:\n> Add a generator to libtuning for writing tuning output to a json file\n> formatted the same way that raspberrypi's ctt formats them.\n> \n> Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>\n> \n> ---\n> Changes 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          | 115 ++++++++++++++++++\n>  2 files changed, 120 insertions(+)\n>  create mode 100644 utils/tuning/libtuning/generators/raspberrypi_output.py\n> \n> diff --git a/utils/tuning/libtuning/generators/__init__.py b/utils/tuning/libtuning/generators/__init__.py\n> index 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\n> diff --git a/utils/tuning/libtuning/generators/raspberrypi_output.py b/utils/tuning/libtuning/generators/raspberrypi_output.py\n> new file mode 100644\n> index 00000000..e06aeddf\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'{module.out_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\nPlease move this class to the end of the file. I would also possibly\nmake the pretty_print function a (private) class member.\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\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)","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 E7B3CBE08B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed,  9 Nov 2022 10:34:46 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 5AA446307E;\n\tWed,  9 Nov 2022 11:34:46 +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 0610461F3F\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed,  9 Nov 2022 11:34:45 +0100 (CET)","from pendragon.ideasonboard.com\n\t(117.145-247-81.adsl-dyn.isp.belgacom.be [81.247.145.117])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 8D2D7896;\n\tWed,  9 Nov 2022 11:34:44 +0100 (CET)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1667990086;\n\tbh=ZRLINKfy6qAqo9FC1XXT/Fj9qg8sUvz3L8vo/Qz4/70=;\n\th=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=BQwA+F8IpiYJBdhKZ3FaO8KkhyhEBivsy1nRlLBSbmSjdbdTYcxpfWDIMO733XYIG\n\tL+KShJOSkhvGQj3GYv0eUD3YG1Fu6EkxPutEP/UyKYTP1x23DV717i5N8FNzyeeenN\n\toFIhtx73i6tG7I+jHPRN5C5jgRy60pHzlP8Ck73odN9cRGExAU+74LIi2SNQ87OeLf\n\tevuECkYA/axil7rlGQYxywA2704n4EdjC5/xc0mYxC4TyJjtankRotrOsHkjDPmAO/\n\ttDQZWXLIYfZlcp5/kN7d4T07B1ypbi/JNjxkwUIKuY9iGqMMv7nyTuc6mEMjWFyQ0e\n\tV4+OxymTSOH3A==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1667990084;\n\tbh=ZRLINKfy6qAqo9FC1XXT/Fj9qg8sUvz3L8vo/Qz4/70=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=Y0bA4t/ZoborXqqtCgjiMijNO51oBUvUe56fKmPBhQiAcZ/jdz2tCGvD0gzH8aIa0\n\tgnBPvLTjGxd7sS5KPcrjso5d8kGSWtjaoSYrpzQlSrNxkrxGxLt38h0t5ndsfa9FOY\n\thVgpHEw30ABlmTFPfjndzNiWSWLFqIK+XFl51G7s="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"Y0bA4t/Z\"; dkim-atps=neutral","Date":"Wed, 9 Nov 2022 12:34:25 +0200","To":"Paul Elder <paul.elder@ideasonboard.com>","Message-ID":"<Y2uCMRbHbveCNY3S@pendragon.ideasonboard.com>","References":"<20221022062310.2545463-1-paul.elder@ideasonboard.com>\n\t<20221022062310.2545463-7-paul.elder@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20221022062310.2545463-7-paul.elder@ideasonboard.com>","Subject":"Re: [libcamera-devel] [PATCH v2 06/11] utils: libtuning:\n\tgenerators: Add 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":"Laurent Pinchart via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]