Patch Detail
Show a patch.
GET /api/patches/19035/?format=api
{ "id": 19035, "url": "https://patchwork.libcamera.org/api/patches/19035/?format=api", "web_url": "https://patchwork.libcamera.org/patch/19035/", "project": { "id": 1, "url": "https://patchwork.libcamera.org/api/projects/1/?format=api", "name": "libcamera", "link_name": "libcamera", "list_id": "libcamera_core", "list_email": "libcamera-devel@lists.libcamera.org", "web_url": "", "scm_url": "", "webscm_url": "" }, "msgid": "<20230915155844.2388-4-david.plowman@raspberrypi.com>", "date": "2023-09-15T15:58:42", "name": "[libcamera-devel,v5,3/5] ipa: rpi: agc: Implementation of multi-channel AGC", "commit_ref": "beab3a229fa18379e65274aecab69a5d0c8b60a5", "pull_url": null, "state": "accepted", "archived": false, "hash": "8e085940e4af2317f9a703ae7902f548283f5c40", "submitter": { "id": 42, "url": "https://patchwork.libcamera.org/api/people/42/?format=api", "name": "David Plowman", "email": "david.plowman@raspberrypi.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/19035/mbox/", "series": [ { "id": 4026, "url": "https://patchwork.libcamera.org/api/series/4026/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=4026", "date": "2023-09-15T15:58:39", "name": "Multi-channel AGC", "version": 5, "mbox": "https://patchwork.libcamera.org/series/4026/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/19035/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/19035/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 31F0CBD160\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 15 Sep 2023 15:58:57 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id EA48A62921;\n\tFri, 15 Sep 2023 17:58:56 +0200 (CEST)", "from mail-wm1-x333.google.com (mail-wm1-x333.google.com\n\t[IPv6:2a00:1450:4864:20::333])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id EB73662911\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 15 Sep 2023 17:58:54 +0200 (CEST)", "by mail-wm1-x333.google.com with SMTP id\n\t5b1f17b1804b1-403012f276dso25195645e9.0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 15 Sep 2023 08:58:54 -0700 (PDT)", "from localhost.localdomain ([195.180.61.40])\n\tby smtp.gmail.com with ESMTPSA id\n\tl13-20020a7bc44d000000b00401c595fcc7sm7810618wmi.11.2023.09.15.08.58.53\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tFri, 15 Sep 2023 08:58:53 -0700 (PDT)" ], "DKIM-Signature": [ "v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1694793536;\n\tbh=kDvFivpZq9caH0degRJnhUg+rglJdU5NoAnVjqFM6TM=;\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:Cc:\n\tFrom;\n\tb=u0rZwK5OAp38pZmAIgX6HB9SnBLMbg3wKDbBDU+0PmuAIL3LPOJDIdj+EIdS1zXJW\n\tjaoHeDqvNNolOc3wHdHvVZQItQ0cynrkEbSkXc4oK9kYLT8jHljX2gD7BO/JS+6lFz\n\tqG8KF0OkGERromZWI47PB19KOvTGiLVds2iup5RYREeHMgRSVnXY6Ofp0EwiPOTGEb\n\tvVn3W+FGtY4rGoRuMykX+MJ0yyn/I1Qeaj3Q3y3u9H5p2ngE1Br+PlykNw5lz4MAbJ\n\tFvN9LKj0LbtjetbKvYALGiVvjDYrHiP+y5j/dG4aZVIaKkPSu8yZFZifP3J3bxS2fc\n\tzKZiVaYzQTtng==", "v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=raspberrypi.com; s=google; t=1694793534; x=1695398334;\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=U/rHJIkqU9vIBuZ09yYrNreBBtCiFbyfxMTz9Zj09+Q=;\n\tb=snj5xgpf1QCLLXl3lo2eWZJVOulRN4JRPW9cF3obdvla32Bn/Pt/G9H+mb3HUXE4N8\n\tH0owOpmV/sCXf6L7xoDD0yC22FOuwxMpWsBQKAdLGOdeUCuDKMAkYEl8oom7kLqZsrvb\n\toKFOM9AzYRSZl7PHeRrj9OYF5qzZFc6P3MhnCxKzE9brxufYG4HrGtf3EgEhf6x/Tw7k\n\t/mBS17LzBnv5J2r15ABunCuTcEDg05ejZ3OKCAZVFGAFKgEd1t7Pcurlc5hml4qwqQGv\n\tAlj+Wq1O3qqXmbPdjKhPDYOB7XU8wADOmJq+RO6s+c4pal9c5jx0MRdHw61/brzn809M\n\tB7ag==" ], "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (2048-bit key; \n\tunprotected) header.d=raspberrypi.com\n\theader.i=@raspberrypi.com\n\theader.b=\"snj5xgpf\"; dkim-atps=neutral", "X-Google-DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20230601; t=1694793534; x=1695398334;\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=U/rHJIkqU9vIBuZ09yYrNreBBtCiFbyfxMTz9Zj09+Q=;\n\tb=NSeQJar8zCzF1F+p0TGq5yK656IwhJp9cJRp43Qd8wdYnoAWYVkFVaDo4odaiRfJSu\n\tRlFoOiusbAoAmPTLOrjlUE/kXQBcJzzAvoemjuVSi9azk1+ccaKv2AYu23xS6+PBd/vf\n\taKEg22m6I+LlwI9Zw4RdzX8B8nV93a7EUcS2DK7aov3mSEQM2psBrvfch1BkLcGbu5/i\n\teXgCzTnvE0Ylm/M58D4fRJUGBqwjWsafjep9RqygV42DMF0QgyUkKPzBTxwxfV54NIR6\n\tVfUk2OJm9thkIxeNu+G0FCoJSKJ1/abKglv2c1YXv38yoRdST360wNhDkvRwKcdmTJWQ\n\t3HBw==", "X-Gm-Message-State": "AOJu0YyoWcj0c5dgbN9G9yEGF8gUsYjje1naQ9iFTYJPNAjfUCOURqGw\n\ta65feaeWoZjBMIUXY94zcB05AHjSol2l+IzRFPQ=", "X-Google-Smtp-Source": "AGHT+IFYQeXhnPuOezE/u7DJXxRMy919Y4mshKkkWSLtF31KF23BpAgbbFRf2rKhhpuv7N3lzUyw/w==", "X-Received": "by 2002:a05:600c:21cb:b0:401:bf87:9895 with SMTP id\n\tx11-20020a05600c21cb00b00401bf879895mr1973502wmj.22.1694793533946; \n\tFri, 15 Sep 2023 08:58:53 -0700 (PDT)", "To": "libcamera-devel@lists.libcamera.org", "Date": "Fri, 15 Sep 2023 16:58:42 +0100", "Message-Id": "<20230915155844.2388-4-david.plowman@raspberrypi.com>", "X-Mailer": "git-send-email 2.30.2", "In-Reply-To": "<20230915155844.2388-1-david.plowman@raspberrypi.com>", "References": "<20230915155844.2388-1-david.plowman@raspberrypi.com>", "MIME-Version": "1.0", "Content-Transfer-Encoding": "8bit", "Subject": "[libcamera-devel] [PATCH v5 3/5] ipa: rpi: agc: Implementation of\n\tmulti-channel AGC", "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": "David Plowman via libcamera-devel <libcamera-devel@lists.libcamera.org>", "Reply-To": "David Plowman <david.plowman@raspberrypi.com>", "Cc": "Jacopo Mondi <jacopo.mondi@ideasonboard.com>", "Errors-To": "libcamera-devel-bounces@lists.libcamera.org", "Sender": "\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>" }, "content": "The switchMode, prepare and process methods are updated to implement\nmulti-channel AGC correctly:\n\n* switchMode now invokes switchMode on all the channels (whether\n active or not).\n\n* prepare must find what channel the current frame is, and run on\n behalf of that channel.\n\n* process updates the most recent DeviceStatus and statistics for the\n channel of the frame that has just arrived, but generates updated\n values working through the active channels in round-robin fashion.\n\nOne minor detail in process is that we don't want to change the\nDeviceStatus metadata of the current frame, so we now pass this to the\nAgcChannel's process method, rather than letting it find the\nDeviceStatus in the metadata.\n\nSigned-off-by: David Plowman <david.plowman@raspberrypi.com>\nReviewed-by: Naushir Patuck <naush@raspberrypi.com>\nReviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\n---\n src/ipa/rpi/controller/agc_status.h | 1 +\n src/ipa/rpi/controller/rpi/agc.cpp | 110 +++++++++++++++++++--\n src/ipa/rpi/controller/rpi/agc.h | 4 +\n src/ipa/rpi/controller/rpi/agc_channel.cpp | 19 ++--\n src/ipa/rpi/controller/rpi/agc_channel.h | 4 +-\n 5 files changed, 116 insertions(+), 22 deletions(-)", "diff": "diff --git a/src/ipa/rpi/controller/agc_status.h b/src/ipa/rpi/controller/agc_status.h\nindex 597eddd7..e5c4ee22 100644\n--- a/src/ipa/rpi/controller/agc_status.h\n+++ b/src/ipa/rpi/controller/agc_status.h\n@@ -36,6 +36,7 @@ struct AgcStatus {\n \tint floatingRegionEnable;\n \tlibcamera::utils::Duration fixedShutter;\n \tdouble fixedAnalogueGain;\n+\tunsigned int channel;\n };\n \n struct AgcPrepareStatus {\ndiff --git a/src/ipa/rpi/controller/rpi/agc.cpp b/src/ipa/rpi/controller/rpi/agc.cpp\nindex 598fc890..8b9d2026 100644\n--- a/src/ipa/rpi/controller/rpi/agc.cpp\n+++ b/src/ipa/rpi/controller/rpi/agc.cpp\n@@ -22,7 +22,7 @@ LOG_DEFINE_CATEGORY(RPiAgc)\n \n Agc::Agc(Controller *controller)\n \t: AgcAlgorithm(controller),\n-\t activeChannels_({ 0 })\n+\t activeChannels_({ 0 }), index_(0)\n {\n }\n \n@@ -205,20 +205,116 @@ void Agc::setActiveChannels(const std::vector<unsigned int> &activeChannels)\n void Agc::switchMode(CameraMode const &cameraMode,\n \t\t Metadata *metadata)\n {\n-\tLOG(RPiAgc, Debug) << \"switchMode for channel 0\";\n-\tchannelData_[0].channel.switchMode(cameraMode, metadata);\n+\t/*\n+\t * We run switchMode on every channel, and then we're going to start over\n+\t * with the first active channel again which means that this channel's\n+\t * status needs to be the one we leave in the metadata.\n+\t */\n+\tAgcStatus status;\n+\n+\tfor (unsigned int channelIndex = 0; channelIndex < channelData_.size(); channelIndex++) {\n+\t\tLOG(RPiAgc, Debug) << \"switchMode for channel \" << channelIndex;\n+\t\tchannelData_[channelIndex].channel.switchMode(cameraMode, metadata);\n+\t\tif (channelIndex == activeChannels_[0])\n+\t\t\tmetadata->get(\"agc.status\", status);\n+\t}\n+\n+\tstatus.channel = activeChannels_[0];\n+\tmetadata->set(\"agc.status\", status);\n+\tindex_ = 0;\n+}\n+\n+static void getDelayedChannelIndex(Metadata *metadata, const char *message, unsigned int &channelIndex)\n+{\n+\tstd::unique_lock<RPiController::Metadata> lock(*metadata);\n+\tAgcStatus *status = metadata->getLocked<AgcStatus>(\"agc.delayed_status\");\n+\tif (status)\n+\t\tchannelIndex = status->channel;\n+\telse {\n+\t\t/* This does happen at startup, otherwise it would be a Warning or Error. */\n+\t\tLOG(RPiAgc, Debug) << message;\n+\t}\n+}\n+\n+static void setCurrentChannelIndex(Metadata *metadata, const char *message, unsigned int channelIndex)\n+{\n+\tstd::unique_lock<RPiController::Metadata> lock(*metadata);\n+\tAgcStatus *status = metadata->getLocked<AgcStatus>(\"agc.status\");\n+\tif (status)\n+\t\tstatus->channel = channelIndex;\n+\telse {\n+\t\t/* This does happen at startup, otherwise it would be a Warning or Error. */\n+\t\tLOG(RPiAgc, Debug) << message;\n+\t}\n }\n \n void Agc::prepare(Metadata *imageMetadata)\n {\n-\tLOG(RPiAgc, Debug) << \"prepare for channel 0\";\n-\tchannelData_[0].channel.prepare(imageMetadata);\n+\t/*\n+\t * The DeviceStatus in the metadata should be correct for the image we\n+\t * are processing. The delayed status should tell us what channel this frame\n+\t * was from, so we will use that channel's prepare method.\n+\t *\n+\t * \\todo To be honest, there's not much that's stateful in the prepare methods\n+\t * so we should perhaps re-evaluate whether prepare even needs to be done\n+\t * \"per channel\".\n+\t */\n+\tunsigned int channelIndex = activeChannels_[0];\n+\tgetDelayedChannelIndex(imageMetadata, \"prepare: no delayed status\", channelIndex);\n+\n+\tLOG(RPiAgc, Debug) << \"prepare for channel \" << channelIndex;\n+\tchannelData_[channelIndex].channel.prepare(imageMetadata);\n }\n \n void Agc::process(StatisticsPtr &stats, Metadata *imageMetadata)\n {\n-\tLOG(RPiAgc, Debug) << \"process for channel 0\";\n-\tchannelData_[0].channel.process(stats, imageMetadata);\n+\t/*\n+\t * We want to generate values for the next channel in round robin fashion\n+\t * (i.e. the channel at location index_ in the activeChannel list), even though\n+\t * the statistics we have will be for a different channel (which we find\n+\t * again from the delayed status).\n+\t */\n+\n+\t/* Generate updated AGC values for channel for new channel that we are requesting. */\n+\tunsigned int channelIndex = activeChannels_[index_];\n+\tAgcChannelData &channelData = channelData_[channelIndex];\n+\t/* The stats that arrived with this image correspond to the following channel. */\n+\tunsigned int statsIndex = 0;\n+\tgetDelayedChannelIndex(imageMetadata, \"process: no delayed status for stats\", statsIndex);\n+\tLOG(RPiAgc, Debug) << \"process for channel \" << channelIndex;\n+\n+\t/*\n+\t * We keep a cache of the most recent DeviceStatus and stats for each channel,\n+\t * so that we can invoke the next channel's process method with the most up to date\n+\t * values.\n+\t */\n+\tLOG(RPiAgc, Debug) << \"Save DeviceStatus and stats for channel \" << statsIndex;\n+\tDeviceStatus deviceStatus;\n+\tif (imageMetadata->get<DeviceStatus>(\"device.status\", deviceStatus) == 0)\n+\t\tchannelData_[statsIndex].deviceStatus = deviceStatus;\n+\telse\n+\t\t/* Every frame should have a DeviceStatus. */\n+\t\tLOG(RPiAgc, Error) << \"process: no device status found\";\n+\tchannelData_[statsIndex].statistics = stats;\n+\n+\t/*\n+\t * Finally fetch the most recent DeviceStatus and stats for the new channel, if both\n+\t * exist, and call process(). We must make the agc.status metadata record correctly\n+\t * which channel this is.\n+\t */\n+\tif (channelData.statistics && channelData.deviceStatus) {\n+\t\tdeviceStatus = *channelData.deviceStatus;\n+\t\tstats = channelData.statistics;\n+\t} else {\n+\t\t/* Can also happen when new channels start. */\n+\t\tLOG(RPiAgc, Debug) << \"process: channel \" << channelIndex << \" not seen yet\";\n+\t}\n+\n+\tchannelData.channel.process(stats, deviceStatus, imageMetadata);\n+\tsetCurrentChannelIndex(imageMetadata, \"process: no AGC status found\", channelIndex);\n+\n+\t/* And onto the next channel for the next call. */\n+\tindex_ = (index_ + 1) % activeChannels_.size();\n }\n \n /* Register algorithm with the system. */\ndiff --git a/src/ipa/rpi/controller/rpi/agc.h b/src/ipa/rpi/controller/rpi/agc.h\nindex a476ea70..ee85c693 100644\n--- a/src/ipa/rpi/controller/rpi/agc.h\n+++ b/src/ipa/rpi/controller/rpi/agc.h\n@@ -6,6 +6,7 @@\n */\n #pragma once\n \n+#include <optional>\n #include <string>\n #include <vector>\n \n@@ -17,6 +18,8 @@ namespace RPiController {\n \n struct AgcChannelData {\n \tAgcChannel channel;\n+\tstd::optional<DeviceStatus> deviceStatus;\n+\tStatisticsPtr statistics;\n };\n \n class Agc : public AgcAlgorithm\n@@ -51,6 +54,7 @@ private:\n \tint checkChannel(unsigned int channel) const;\n \tstd::vector<AgcChannelData> channelData_;\n \tstd::vector<unsigned int> activeChannels_;\n+\tunsigned int index_; /* index into the activeChannels_ */\n };\n \n } /* namespace RPiController */\ndiff --git a/src/ipa/rpi/controller/rpi/agc_channel.cpp b/src/ipa/rpi/controller/rpi/agc_channel.cpp\nindex f180b848..84eafe7d 100644\n--- a/src/ipa/rpi/controller/rpi/agc_channel.cpp\n+++ b/src/ipa/rpi/controller/rpi/agc_channel.cpp\n@@ -444,7 +444,7 @@ void AgcChannel::prepare(Metadata *imageMetadata)\n \t}\n }\n \n-void AgcChannel::process(StatisticsPtr &stats, Metadata *imageMetadata)\n+void AgcChannel::process(StatisticsPtr &stats, DeviceStatus const &deviceStatus, Metadata *imageMetadata)\n {\n \tframeCount_++;\n \t/*\n@@ -455,7 +455,7 @@ void AgcChannel::process(StatisticsPtr &stats, Metadata *imageMetadata)\n \t/* Fetch the AWB status immediately, so that we can assume it's there. */\n \tfetchAwbStatus(imageMetadata);\n \t/* Get the current exposure values for the frame that's just arrived. */\n-\tfetchCurrentExposure(imageMetadata);\n+\tfetchCurrentExposure(deviceStatus);\n \t/* Compute the total gain we require relative to the current exposure. */\n \tdouble gain, targetY;\n \tcomputeGain(stats, imageMetadata, gain, targetY);\n@@ -567,18 +567,11 @@ void AgcChannel::housekeepConfig()\n \t\t\t << meteringModeName_;\n }\n \n-void AgcChannel::fetchCurrentExposure(Metadata *imageMetadata)\n+void AgcChannel::fetchCurrentExposure(DeviceStatus const &deviceStatus)\n {\n-\tstd::unique_lock<Metadata> lock(*imageMetadata);\n-\tDeviceStatus *deviceStatus =\n-\t\timageMetadata->getLocked<DeviceStatus>(\"device.status\");\n-\tif (!deviceStatus)\n-\t\tLOG(RPiAgc, Fatal) << \"No device metadata\";\n-\tcurrent_.shutter = deviceStatus->shutterSpeed;\n-\tcurrent_.analogueGain = deviceStatus->analogueGain;\n-\tAgcStatus *agcStatus =\n-\t\timageMetadata->getLocked<AgcStatus>(\"agc.status\");\n-\tcurrent_.totalExposure = agcStatus ? agcStatus->totalExposureValue : 0s;\n+\tcurrent_.shutter = deviceStatus.shutterSpeed;\n+\tcurrent_.analogueGain = deviceStatus.analogueGain;\n+\tcurrent_.totalExposure = 0s; /* this value is unused */\n \tcurrent_.totalExposureNoDG = current_.shutter * current_.analogueGain;\n }\n \ndiff --git a/src/ipa/rpi/controller/rpi/agc_channel.h b/src/ipa/rpi/controller/rpi/agc_channel.h\nindex aca79ef2..b2f554db 100644\n--- a/src/ipa/rpi/controller/rpi/agc_channel.h\n+++ b/src/ipa/rpi/controller/rpi/agc_channel.h\n@@ -85,13 +85,13 @@ public:\n \tvoid disableAuto();\n \tvoid switchMode(CameraMode const &cameraMode, Metadata *metadata);\n \tvoid prepare(Metadata *imageMetadata);\n-\tvoid process(StatisticsPtr &stats, Metadata *imageMetadata);\n+\tvoid process(StatisticsPtr &stats, DeviceStatus const &deviceStatus, Metadata *imageMetadata);\n \n private:\n \tbool updateLockStatus(DeviceStatus const &deviceStatus);\n \tAgcConfig config_;\n \tvoid housekeepConfig();\n-\tvoid fetchCurrentExposure(Metadata *imageMetadata);\n+\tvoid fetchCurrentExposure(DeviceStatus const &deviceStatus);\n \tvoid fetchAwbStatus(Metadata *imageMetadata);\n \tvoid computeGain(StatisticsPtr &statistics, Metadata *imageMetadata,\n \t\t\t double &gain, double &targetY);\n", "prefixes": [ "libcamera-devel", "v5", "3/5" ] }