[{"id":27391,"web_url":"https://patchwork.libcamera.org/comment/27391/","msgid":"<168717292958.3575403.15830550906932199161@Monstersaurus>","date":"2023-06-19T11:08:49","subject":"Re: [libcamera-devel] [PATCH v2] ipa: rpi: cam_helper: PDAF support\n\tfor IMX519","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"Hi John, Lee,\n\nQuoting Umang Jain via libcamera-devel (2023-06-19 07:02:42)\n> From: john <john@arducam.com>\n> \n> Adds the PDAF support for IMX519 camera module by Arducam.\n> \n> Signed-off-by: Umang Jain <umang.jain@ideasonboard.com>\n> Tested-by: Umang Jain <umang.jain@ideasonboard.com>\n> ---\n> Changes in v2:\n> - Add author's Name \"john <john@arducam.com>\"\n\n\nTo be able to integrate support for the IMX519 in libcamera we need to\nbe able to certify the origin of the code, as we document here:\n\n  https://libcamera.org/contributing.html#developer-s-certificate-of-origin\n\nThis follows the https://developercertificate.org/ and ensures that we\ncan legally attibute the authorship of every line of code within the\nproject and that the code has been contributed to open source and\nprotects against issues of copyright claims.\n\nCould we get a fully formed Signed-off-by: tag for this code from\nArducam please?\n\nThis should be in the following form:\n\n  Signed-off-by: Legal Name <valid@email.address>\n\nFor example, I would sign with:\n\n  Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\nProviding this for the code can be done simply by replying to this\nmail/patch with the tag specified to state that the code supplied below\nwas authored by you, and is suitable for integration. \n\n\nRegards\n--\nKieran\n\n\n\n> - Add Tested-by: tag\n> \n> Question: S-o-B by author is still missing on origin commit, should I\n> externally add it? \n> ---\n>  src/ipa/rpi/cam_helper/cam_helper_imx519.cpp | 50 ++++++++++++++++++++\n>  src/ipa/rpi/vc4/data/imx519.json             | 40 ++++++++++++++++\n>  2 files changed, 90 insertions(+)\n> \n> diff --git a/src/ipa/rpi/cam_helper/cam_helper_imx519.cpp b/src/ipa/rpi/cam_helper/cam_helper_imx519.cpp\n> index c7262aa0..6d032082 100644\n> --- a/src/ipa/rpi/cam_helper/cam_helper_imx519.cpp\n> +++ b/src/ipa/rpi/cam_helper/cam_helper_imx519.cpp\n> @@ -15,9 +15,13 @@\n>  \n>  #include <libcamera/base/log.h>\n>  \n> +#include \"controller/pdaf_data.h\"\n> +\n>  #include \"cam_helper.h\"\n>  #include \"md_parser.h\"\n>  \n> +#define ALIGN_UP(x, a) (((x) + (a)-1) & ~(a - 1))\n> +\n>  using namespace RPiController;\n>  using namespace libcamera;\n>  using libcamera::utils::Duration;\n> @@ -66,8 +70,13 @@ private:\n>         /* Largest long exposure scale factor given as a left shift on the frame length. */\n>         static constexpr int longExposureShiftMax = 7;\n>  \n> +       static constexpr int pdafStatsRows = 12;\n> +       static constexpr int pdafStatsCols = 16;\n> +\n>         void populateMetadata(const MdParser::RegisterMap &registers,\n>                               Metadata &metadata) const override;\n> +       static bool parsePdafData(const uint8_t *ptr, size_t len, unsigned bpp,\n> +                                 PdafRegions &pdaf);\n>  };\n>  \n>  CamHelperImx519::CamHelperImx519()\n> @@ -90,6 +99,11 @@ void CamHelperImx519::prepare(libcamera::Span<const uint8_t> buffer, Metadata &m\n>         MdParser::RegisterMap registers;\n>         DeviceStatus deviceStatus;\n>  \n> +       LOG(IPARPI, Debug) << \"Embedded buffer size: \" << buffer.size();\n> +\n> +       size_t bytesPerLine = (mode_.width * mode_.bitdepth) >> 3;\n> +       bytesPerLine = ALIGN_UP(bytesPerLine, 16);\n> +\n>         if (metadata.get(\"device.status\", deviceStatus)) {\n>                 LOG(IPARPI, Error) << \"DeviceStatus not found from DelayedControls\";\n>                 return;\n> @@ -97,6 +111,14 @@ void CamHelperImx519::prepare(libcamera::Span<const uint8_t> buffer, Metadata &m\n>  \n>         parseEmbeddedData(buffer, metadata);\n>  \n> +       if (buffer.size() > 2 * bytesPerLine) {\n> +               PdafRegions pdaf;\n> +               parsePdafData(&buffer[2 * bytesPerLine],\n> +                             buffer.size() - 2 * bytesPerLine,\n> +                             mode_.bitdepth, pdaf);\n> +               metadata.set(\"pdaf.data\", pdaf);\n> +       }\n> +\n>         /*\n>          * The DeviceStatus struct is first populated with values obtained from\n>          * DelayedControls. If this reports frame length is > frameLengthMax,\n> @@ -188,6 +210,34 @@ void CamHelperImx519::populateMetadata(const MdParser::RegisterMap &registers,\n>         metadata.set(\"device.status\", deviceStatus);\n>  }\n>  \n> +bool CamHelperImx519::parsePdafData(const uint8_t *ptr, size_t len,\n> +                                   unsigned bpp, PdafRegions &pdaf)\n> +{\n> +       size_t step = bpp >> 1; /* bytes per PDAF grid entry */\n> +\n> +       if (bpp < 10 || bpp > 12 || len < 194 * step || ptr[0] != 0 || ptr[1] >= 0x40) {\n> +               LOG(IPARPI, Error) << \"PDAF data in unsupported format\";\n> +               return false;\n> +       }\n> +\n> +       pdaf.init({ pdafStatsCols, pdafStatsRows });\n> +\n> +       ptr += 2 * step;\n> +       for (unsigned i = 0; i < pdafStatsRows; ++i) {\n> +               for (unsigned j = 0; j < pdafStatsCols; ++j) {\n> +                       unsigned c = (ptr[0] << 3) | (ptr[1] >> 5);\n> +                       int p = (((ptr[1] & 0x0F) - (ptr[1] & 0x10)) << 6) | (ptr[2] >> 2);\n> +                       PdafData pdafData;\n> +                       pdafData.conf = c;\n> +                       pdafData.phase = c ? p : 0;\n> +                       pdaf.set(libcamera::Point(j, i), { pdafData, 1, 0 });\n> +                       ptr += step;\n> +               }\n> +       }\n> +\n> +       return true;\n> +}\n> +\n>  static CamHelper *create()\n>  {\n>         return new CamHelperImx519();\n> diff --git a/src/ipa/rpi/vc4/data/imx519.json b/src/ipa/rpi/vc4/data/imx519.json\n> index 1b0a7747..0733d97e 100644\n> --- a/src/ipa/rpi/vc4/data/imx519.json\n> +++ b/src/ipa/rpi/vc4/data/imx519.json\n> @@ -350,6 +350,46 @@\n>                  ]\n>              }\n>          },\n> +        {\n> +            \"rpi.af\":\n> +            {\n> +                \"ranges\":\n> +                {\n> +                    \"normal\":\n> +                    {\n> +                        \"min\": 0.0,\n> +                        \"max\": 12.0,\n> +                        \"default\": 1.0\n> +                    },\n> +                    \"macro\":\n> +                    {\n> +                        \"min\": 3.0,\n> +                        \"max\": 15.0,\n> +                        \"default\": 4.0\n> +                    }\n> +                },\n> +                \"speeds\":\n> +                {\n> +                    \"normal\":\n> +                    {\n> +                        \"step_coarse\": 1.0,\n> +                        \"step_fine\": 0.25,\n> +                        \"contrast_ratio\": 0.75,\n> +                        \"pdaf_gain\": -0.02,\n> +                        \"pdaf_squelch\": 0.125,\n> +                        \"max_slew\": 2.0,\n> +                        \"pdaf_frames\": 20,\n> +                        \"dropout_frames\": 6,\n> +                        \"step_frames\": 4\n> +                    }\n> +                },\n> +                \"conf_epsilon\": 8,\n> +                \"conf_thresh\": 16,\n> +                \"conf_clip\": 512,\n> +                \"skip_frames\": 5,\n> +                \"map\": [ 0.0, 0.0, 15.0, 4095 ]\n> +            }\n> +        },\n>          {\n>              \"rpi.ccm\":\n>              {\n> -- \n> 2.39.1\n>","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 16B99C3237\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 19 Jun 2023 11:08:55 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 67C2F628C1;\n\tMon, 19 Jun 2023 13:08:54 +0200 (CEST)","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 7BD77614FD\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 19 Jun 2023 13:08:52 +0200 (CEST)","from pendragon.ideasonboard.com\n\t(aztw-30-b2-v4wan-166917-cust845.vm26.cable.virginm.net\n\t[82.37.23.78])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 3064C800;\n\tMon, 19 Jun 2023 13:08:18 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1687172934;\n\tbh=lK3BObFGKLoDpw8vAPUMjC6ofjreFrgpomd9L/gnV24=;\n\th=In-Reply-To:References:To:Date:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=bXEw/EoWPKUayuvIs+VqBUc6hHwlOjGb7ZsxjOQBOt7AGH4m67lpMeGv0JaLcyzXb\n\tyM9TQXvJHPeQK4D6omAAiCrfFwZYHas72Z/WisfJAN6NbK0cGUBz2D56x4VQ6HuDgS\n\tkRPZ7kwdZ7CubTGZbQGh75/R9pQCo9lb69qog3yr4VmMhUqZWbao5+MlrtE0zMhHK3\n\ttvSawsX9pa2zcJLV4UhG1ZrA+KPtqvxpdLausoAV7L8qWfJj0r243j2gx+6/SvEyQV\n\tthaIhIzVXdgUl4V/wkXwJH8SoYc1/5iS7woBkHB1fMpU2sday1RxtccjvUmbyfCL0Z\n\tPq+VHcn26zmLg==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1687172898;\n\tbh=lK3BObFGKLoDpw8vAPUMjC6ofjreFrgpomd9L/gnV24=;\n\th=In-Reply-To:References:Subject:From:Cc:To:Date:From;\n\tb=YeBgKQEhy3rPIuduzCz6UWjcDNUbVG2grCMaW30QUw8ppVoP+4FPHq+jcZZNYeGZH\n\trV2INSrdnFw3M/hfP2aBiaKyavalWR1NUCQecvND0MHts6Xqgr+vwmFZF4j2d5IDCf\n\tPwUecfFCTerxYygE5IcDnve7D718TJZFcEysBoZ4="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"YeBgKQEh\"; dkim-atps=neutral","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<20230619060242.37020-1-umang.jain@ideasonboard.com>","References":"<20230619060242.37020-1-umang.jain@ideasonboard.com>","To":"john <john@arducam.com>, lee.jackson@arducam.com, Lee <sales@arducam.com>,","Date":"Mon, 19 Jun 2023 12:08:49 +0100","Message-ID":"<168717292958.3575403.15830550906932199161@Monstersaurus>","User-Agent":"alot/0.10","Subject":"Re: [libcamera-devel] [PATCH v2] ipa: rpi: cam_helper: PDAF support\n\tfor IMX519","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":"Kieran Bingham via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Kieran Bingham <kieran.bingham@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>"}}]