{"id":20219,"url":"https://patchwork.libcamera.org/api/patches/20219/?format=json","web_url":"https://patchwork.libcamera.org/patch/20219/","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":"<20240606101512.375178-6-david.plowman@raspberrypi.com>","date":"2024-06-06T10:15:11","name":"[5/6] utils: raspberrypi: ctt: Add option to convert between vc4/pisp targets","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"c5865d81247687894ae91ee35e112f04795b37c8","submitter":{"id":42,"url":"https://patchwork.libcamera.org/api/people/42/?format=json","name":"David Plowman","email":"david.plowman@raspberrypi.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/20219/mbox/","series":[{"id":4367,"url":"https://patchwork.libcamera.org/api/series/4367/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4367","date":"2024-06-06T10:15:06","name":"Raspberry Pi Camera Tuning Tool updates","version":1,"mbox":"https://patchwork.libcamera.org/series/4367/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/20219/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/20219/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 E1DACC3292\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu,  6 Jun 2024 10:15:42 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id DB48F65464;\n\tThu,  6 Jun 2024 12:15:41 +0200 (CEST)","from mail-ej1-x631.google.com (mail-ej1-x631.google.com\n\t[IPv6:2a00:1450:4864:20::631])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id D39366545D\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu,  6 Jun 2024 12:15:33 +0200 (CEST)","by mail-ej1-x631.google.com with SMTP id\n\ta640c23a62f3a-a6266ffdba8so65596666b.1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 06 Jun 2024 03:15:33 -0700 (PDT)","from pi5-davidp.pitowers.org\n\t([2001:4d4e:300:1f:c732:5d0a:406b:ae46])\n\tby smtp.gmail.com with ESMTPSA id\n\ta640c23a62f3a-a6c805c59a2sm75809866b.50.2024.06.06.03.15.32\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tThu, 06 Jun 2024 03:15:32 -0700 (PDT)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (2048-bit key;\n\tunprotected) header.d=raspberrypi.com header.i=@raspberrypi.com\n\theader.b=\"kezno5NU\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=raspberrypi.com; s=google; t=1717668933; x=1718273733;\n\tdarn=lists.libcamera.org; \n\th=content-transfer-encoding:mime-version:references:in-reply-to\n\t:message-id:date:subject:cc:to:from:from:to:cc:subject:date\n\t:message-id:reply-to;\n\tbh=W06p37Y1xwC67cFH+rZkEwLXRBquD8QbZ03xa8sUGrY=;\n\tb=kezno5NU+jWXfGFph/m6esRZDFiihgpVoN5pVkaSJb+EUGJS+WCVy0ZPXef93O4pua\n\tc4MUOogIAhZsWkqTCQ5Y2UEwPYGXL8FyV32k9kYqY6jh/EjmPdR1Hnqmy68TQHZ1J4IW\n\tEC1/OcilasMicM23aR2fDgf9nEMeBTI8fZnla+YUQVaDltXdNhdEsYk6i/BDwL/oZole\n\tM/3QPG0zK4x1GckgELSIaqq3CztOZkCKbWkUpACpsXQC4EsxhxjWm2pR72MVNqk6vn99\n\tkdAQllRVZSqgsVxIVeilL5SeJ3dux5GQx7zdP2ZyJ8MWbzkQoBIw2EkgG7tAIgRkkWYS\n\teaOA==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20230601; t=1717668933; x=1718273733;\n\th=content-transfer-encoding:mime-version:references:in-reply-to\n\t:message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc\n\t:subject:date:message-id:reply-to;\n\tbh=W06p37Y1xwC67cFH+rZkEwLXRBquD8QbZ03xa8sUGrY=;\n\tb=mC0f7ZlWv7UbsWq/rqxYx65hWY1bwZuTFaLrvETF9sW+9k9XkXnpSGSNHfFJpyTbOU\n\tO7LgsgepeJARCF3AwvyZjApzg7zZ+7PPfK9c++BoUHPsAEsN/e1noK+OfDEdOATg3rRR\n\tpMQygAo0jQpegiPmslpewu+fzWt5Q8eugutYVxHwjPZ6yRHCJ/ryiwW7mJsC1Ggvd30e\n\tctgC0/Wd5OKd4SLaC3Yu02BMcCOF7iTqrA8lFL3tRJdsAlFkWFQtjnMcqJBmRzbB4Q5o\n\tvKquc60J6ZmUb3AH4cWcVXYuYb9m965umdbYk3nKBuVAkZctQm8HXfQKAfWmz2beqpmq\n\tEeDg==","X-Gm-Message-State":"AOJu0Yy6G8+SbVnb5MwmvMM1WnOCWSmWFgnqBTtDSQSPycTZOGBLs5n9\n\ttjFfysrSee+T6oY54CNWZCLP5yAn6y08/t+2BlHS9td6f5hpGJGkFMoH8ffmVAdjogwmC5fpfsM\n\t0","X-Google-Smtp-Source":"AGHT+IHWHGYONi98wQvaj4RBlbLj1FyXAzD9vqQmyNRINQdmqy7gwkR4ajuWomwSCm8U5qCxfOu5zw==","X-Received":"by 2002:a17:906:c059:b0:a6c:702e:73fc with SMTP id\n\ta640c23a62f3a-a6c702e760fmr240859666b.8.1717668932917; \n\tThu, 06 Jun 2024 03:15:32 -0700 (PDT)","From":"David Plowman <david.plowman@raspberrypi.com>","To":"libcamera-devel@lists.libcamera.org","Cc":"Naushir Patuck <naush@raspberrypi.com>","Subject":"[PATCH 5/6] utils: raspberrypi: ctt: Add option to convert between\n\tvc4/pisp targets","Date":"Thu,  6 Jun 2024 11:15:11 +0100","Message-Id":"<20240606101512.375178-6-david.plowman@raspberrypi.com>","X-Mailer":"git-send-email 2.39.2","In-Reply-To":"<20240606101512.375178-1-david.plowman@raspberrypi.com>","References":"<20240606101512.375178-1-david.plowman@raspberrypi.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","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>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"From: Naushir Patuck <naush@raspberrypi.com>\n\nThis change adds functionality to the convert_tuning.py script to\nconvert between vc4 and pisp target tuning files. The conversion is\ndone on a best effort basis, and should provide functional tuning files.\nHowever, a full tuning for the target platform is always preferred.\n\nSigned-off-by: Naushir Patuck <naush@raspberrypi.com>\n---\n utils/raspberrypi/ctt/convert_tuning.py | 98 ++++++++++++++++++++++---\n 1 file changed, 86 insertions(+), 12 deletions(-)","diff":"diff --git a/utils/raspberrypi/ctt/convert_tuning.py b/utils/raspberrypi/ctt/convert_tuning.py\nindex f4504d45..83cf69d4 100755\n--- a/utils/raspberrypi/ctt/convert_tuning.py\n+++ b/utils/raspberrypi/ctt/convert_tuning.py\n@@ -8,30 +8,104 @@\n \n import argparse\n import json\n+import numpy as np\n import sys\n \n from ctt_pretty_print_json import pretty_print\n+from ctt_pisp import grid_size as grid_size_pisp\n+from ctt_pisp import json_template as json_template_pisp\n+from ctt_vc4 import grid_size as grid_size_vc4\n+from ctt_vc4 import json_template as json_template_vc4\n \n \n-def convert_v2(in_json: dict) -> str:\n+def interp_2d(in_ls, src_w, src_h, dst_w, dst_h):\n \n-    if 'version' in in_json.keys() and in_json['version'] != 1.0:\n-        print(f'The JSON config reports version {in_json[\"version\"]} that is incompatible with this tool.')\n-        sys.exit(-1)\n+    out_ls = np.zeros((dst_h, dst_w))\n+    for i in range(src_h):\n+        out_ls[i] = np.interp(np.linspace(0, dst_w - 1, dst_w),\n+                              np.linspace(0, dst_w - 1, src_w),\n+                              in_ls[i])\n+    for i in range(dst_w):\n+        out_ls[:,i] = np.interp(np.linspace(0, dst_h - 1, dst_h),\n+                                np.linspace(0, dst_h - 1, src_h),\n+                                out_ls[:src_h, i])\n+    return out_ls\n \n-    converted = {\n-        'version': 2.0,\n-        'target': 'bcm2835',\n-        'algorithms': [{algo: config} for algo, config in in_json.items()]\n-    }\n \n-    return pretty_print(converted)\n+def convert_target(in_json: dict, target: str):\n+\n+    src_w, src_h = grid_size_pisp if target == 'vc4' else grid_size_vc4\n+    dst_w, dst_h = grid_size_vc4 if target == 'vc4' else grid_size_pisp\n+    json_template = json_template_vc4 if target == 'vc4' else json_template_pisp\n+\n+    # ALSC grid sizes\n+    alsc = next(algo for algo in in_json['algorithms'] if 'rpi.alsc' in algo)['rpi.alsc']\n+    for colour in ['calibrations_Cr', 'calibrations_Cb']:\n+        if colour not in alsc:\n+            continue\n+        for temperature in alsc[colour]:\n+            in_ls = np.reshape(temperature['table'], (src_h, src_w))\n+            out_ls = interp_2d(in_ls, src_w, src_h, dst_w, dst_h)\n+            temperature['table'] = np.round(out_ls.flatten(), 3).tolist()\n+\n+    if 'luminance_lut' in alsc:\n+        in_ls = np.reshape(alsc['luminance_lut'], (src_h, src_w))\n+        out_ls = interp_2d(in_ls, src_w, src_h, dst_w, dst_h)\n+        alsc['luminance_lut'] = np.round(out_ls.flatten(), 3).tolist()\n+\n+    # Denoise blocks\n+    for i, algo in enumerate(in_json['algorithms']):\n+        if list(algo.keys())[0] == 'rpi.sdn':\n+            in_json['algorithms'][i] = {'rpi.denoise': json_template['rpi.sdn'] if target == 'vc4' else json_template['rpi.denoise']}\n+            break\n+\n+    # AGC mode weights\n+    agc = next(algo for algo in in_json['algorithms'] if 'rpi.agc' in algo)['rpi.agc']\n+    if 'channels' in agc:\n+        for i, channel in enumerate(agc['channels']):\n+            target_agc_metering = json_template['rpi.agc']['channels'][i]['metering_modes']\n+            for mode, v in channel['metering_modes'].items():\n+                v['weights'] = target_agc_metering[mode]['weights']\n+    else:\n+        for mode, v in agc[\"metering_modes\"].items():\n+            target_agc_metering = json_template['rpi.agc']['channels'][0]['metering_modes']\n+            v['weights'] = target_agc_metering[mode]['weights']\n+\n+    # HDR\n+    if target == 'pisp':\n+        for i, algo in enumerate(in_json['algorithms']):\n+            if list(algo.keys())[0] == 'rpi.hdr':\n+                in_json['algorithms'][i] = {'rpi.hdr': json_template['rpi.hdr']}\n+\n+    return in_json\n+\n+\n+def convert_v2(in_json: dict, target: str) -> str:\n+\n+    if 'version' in in_json.keys() and in_json['version'] == 1.0:\n+        converted = {\n+            'version': 2.0,\n+            'target': target,\n+            'algorithms': [{algo: config} for algo, config in in_json.items()]\n+        }\n+    else:\n+        converted = in_json\n+\n+    # Convert between vc4 <-> pisp targets. This is a best effort thing.\n+    if converted['target'] != target:\n+        converted = convert_target(converted, target)\n+        converted['target'] = target\n+\n+    grid_size = grid_size_vc4[0] if target == 'vc4' else grid_size_pisp[0]\n+    return pretty_print(converted, custom_elems={'table': grid_size, 'luminance_lut': grid_size})\n \n \n if __name__ == \"__main__\":\n     parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, description=\n-                    'Convert the format of the Raspberry Pi camera tuning file from v1.0 to v2.0.\\n')\n+        'Convert the format of the Raspberry Pi camera tuning file from v1.0 to v2.0 and/or the vc4 <-> pisp targets.\\n')\n     parser.add_argument('input', type=str, help='Input tuning file.')\n+    parser.add_argument('-t', '--target', type=str, help='Target platform.',\n+                        choices=['pisp', 'vc4'], default='vc4')\n     parser.add_argument('output', type=str, nargs='?',\n                         help='Output converted tuning file. If not provided, the input file will be updated in-place.',\n                         default=None)\n@@ -40,7 +114,7 @@ if __name__ == \"__main__\":\n     with open(args.input, 'r') as f:\n         in_json = json.load(f)\n \n-    out_json = convert_v2(in_json)\n+    out_json = convert_v2(in_json, args.target)\n \n     with open(args.output if args.output is not None else args.input, 'w') as f:\n         f.write(out_json)\n","prefixes":["5/6"]}