From patchwork Wed Jul 13 09:21:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 16616 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 40C44BD1F1 for ; Wed, 13 Jul 2022 09:22:26 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id EBC5563311; Wed, 13 Jul 2022 11:22:25 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1657704145; bh=6Qi+mMmtUhg+cXIdprUkkvhZHWWZEI1hR3V2T+/VXkk=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=kWkWwLsdU3kLhZ9NlpyobkDLiBzHmRdCqn2taLRvyI2dAX4oSH0SevxpKL8RXZqUq WFEgOiK3GYun+x6S1W9zGA+PND7+O3n5DThwJheDcILsqe9v4jqf+pl4NZwUfHrdOb UOfognHmVAvam51Db2XPygBkFBbq+ol48TcZpE3I6//GQvj5FAZa2HhwGEGMc3irJl oU+8zj4W12X2FKt9Z0mJAucz2ma8hQ3aT6MMSagCFoGeyExC9yj7WOKSKHk0E4edUm jaNOIJb9DuNq7j9vT44PjvGf5yL0G4lvoFWwUaKWtXktsgMH/DN0roPO2zXM5PzWxN 1o0xUTEsGEB4g== Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 78CC86331E for ; Wed, 13 Jul 2022 11:22:20 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=raspberrypi.com header.i=@raspberrypi.com header.b="oVzUy1pk"; dkim-atps=neutral Received: by mail-wr1-x42b.google.com with SMTP id r14so14649175wrg.1 for ; Wed, 13 Jul 2022 02:22:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raspberrypi.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Ji6mFJZozAqosAIowcOQ7v5MRS1VSrt09iAFZRMrQjU=; b=oVzUy1pktxnl2O1cGh4keINqFM66/vvqpYrpLq1o5KX1QW3Mvm7hq/kJSgOTSb/GIR 18HBPZ2p92Shq3cVNgk/76CYum3xAk+4HKSDrcVwJgd2B+tpe7I+Oz6ds7Xz50y9TnfV j7VSWL5ls0TaNN+58AHXWJ6cbYCHMvT5tBlGj0nBV6/NoRzSLD86LaU4Aiw+7SWl//jd jK6SgP+nLzZJURtZP/772DLPprNhoHpoQGH1MM5cpy2My+RaZRnv05FSr7o80Jhq+dIz 0wzgHZLePHloUMBh+aL6+Whbn8l7FTIvSzyFw9s26BMmgnDsZxLVK9Hiqi6QyftWr0UE qSeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Ji6mFJZozAqosAIowcOQ7v5MRS1VSrt09iAFZRMrQjU=; b=NUJBSmvfWoZZ0HxZnIZ13Y9hO0xYtkFUmAW6XayRuInb8Zd1pcz42j6oN+jng5uE23 TuXUB/FttWd59o+W2YweQsXfzvtgi+pkmv1+6g2mBcAuA6+ijNstYIWBjN2EB8MCahQt VFeA/l6QcWxVfaUQjM0q2TDmbmTnfM+794agKspYp3zHwJOvKihHzBmm83v8MdA00jBR nXHAJoLxibfzVRJa14TvtMTP4MSiaRND2FyiP7f6ez5o16NgwmZWXiqtWeGSgfAyGdgl QRw5B/Qz4wQspMvZJV/BOqiKB+YDckIujCfIFha2ZuMTpT9NnHYARHzytN83y0S3716X I4zw== X-Gm-Message-State: AJIora9BQ13GvcmlHDWh+GL1ZbzjzW3E8I2xTUcdCyKMl0ehPjGfnMJJ sZeaNlI5TX+10YNZsX2ks6LLQowvXawPCg== X-Google-Smtp-Source: AGRyM1u+y4ufdkwNC7ybWM1jEf6bz1po4hh/fhL/m98vyMhZ2V23zYhHSmcV+pMxeqStojy8g+vb5Q== X-Received: by 2002:a05:6000:168e:b0:21d:b9bf:5e39 with SMTP id y14-20020a056000168e00b0021db9bf5e39mr2431700wrd.71.1657704139273; Wed, 13 Jul 2022 02:22:19 -0700 (PDT) Received: from naush-laptop.localdomain ([93.93.133.154]) by smtp.gmail.com with ESMTPSA id bd20-20020a05600c1f1400b0039747cf8354sm1514778wmb.39.2022.07.13.02.22.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Jul 2022 02:22:18 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Wed, 13 Jul 2022 10:21:57 +0100 Message-Id: <20220713092159.9014-7-naush@raspberrypi.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220713092159.9014-1-naush@raspberrypi.com> References: <20220713092159.9014-1-naush@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 6/8] utils: raspberrypi: Add tuning file conversion script 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: , X-Patchwork-Original-From: Naushir Patuck via libcamera-devel From: Naushir Patuck Reply-To: Naushir Patuck Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Add a script to convert the Raspberry Pi camera tuning file format from version 1.0 to 2.0. The version 1.0 format was originally used with the boost JSON parser that happen to provided algorithm ordering based on the ordering in the file. The new format provides implicit ordering by having the algorithms listed in an array. This script also adds a root level version key set to 2.0 to the config file, allowing the controller to distinguish between the two formats. Signed-off-by: Naushir Patuck --- utils/raspberrypi/ctt/convert_tuning.py | 94 +++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100755 utils/raspberrypi/ctt/convert_tuning.py diff --git a/utils/raspberrypi/ctt/convert_tuning.py b/utils/raspberrypi/ctt/convert_tuning.py new file mode 100755 index 000000000000..3396bebe8f4e --- /dev/null +++ b/utils/raspberrypi/ctt/convert_tuning.py @@ -0,0 +1,94 @@ +#!/bin/python3 +# Script to convert version 1.0 Raspberry Pi camera tuning files to version 2.0 +# and later. +# +# Copyright 2022 Raspberry Pi Ltd. + +import argparse +import json +import textwrap + + +class Encoder(json.JSONEncoder): + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.indentation_level = 0 + self.soft_break = 80 + self.hard_break = 120 + + def encode(self, o): + if isinstance(o, (list, tuple)): + if not any(isinstance(el, (list, tuple, dict)) for el in o): + s = ', '.join(json.dumps(el) for el in o) + if len(s) > self.soft_break: + self.indentation_level += 1 + t = textwrap.wrap(s, self.hard_break, break_long_words=False, + initial_indent=self.indent_str, subsequent_indent=self.indent_str) + self.indentation_level -= 1 + output = '\n'.join(t) + output = f'\n{self.indent_str}[\n{output}\n{self.indent_str}]' + else: + output = f' [ {s} ]' + return output + else: + self.indentation_level += 1 + output = [self.indent_str + self.encode(el) for el in o] + self.indentation_level -= 1 + output = ',\n'.join(output) + return f' [\n{output}\n{self.indent_str}]' + + elif isinstance(o, dict): + self.indentation_level += 1 + output = [] + for k, v in o.items(): + sep = f'\n{self.indent_str}' if isinstance(v, dict) else '' + output.append(self.indent_str + f'{json.dumps(k)}:{sep}{self.encode(v)}') + self.indentation_level -= 1 + output = ',\n'.join(output) + return f'{{\n{output}\n{self.indent_str}}}' + + else: + return ' ' + json.dumps(o) + + @property + def indent_str(self) -> str: + return ' ' * self.indentation_level * self.indent + + def iterencode(self, o, **kwargs): + return self.encode(o) + + +def convert_v2(in_json): + + ver = 1.0 if 'version' not in in_json.keys() else in_json['version'] + + if ver != 1.0: + print(f'Cannot convert version {ver} format to version 2.0!') + exit(-1) + + converted = {} + converted['version'] = 2.0 + converted['algorithms'] = [] + + for k, v in in_json.items(): + if k == 'version': + continue + converted['algorithms'].append(dict(zip([k], [v]))) + + return json.dumps(converted, cls=Encoder, indent=4, sort_keys=False) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description='Convert the format of the Raspberry Pi camera tuning file from v1 to v2.') + parser.add_argument('input', type=str, nargs=1, help='Input config file') + parser.add_argument('output', type=str, nargs='?', help='Output converted config file', default=None) + args = parser.parse_args() + + with open(args.input[0], 'r') as f: + in_json = json.load(f) + + out_json = convert_v2(in_json) + + with open(args.output if args.output is not None else args.input[0], 'w') as f: + f.write(out_json)