[{"id":27614,"web_url":"https://patchwork.libcamera.org/comment/27614/","msgid":"<CAEmqJPpDFVL59JVgdkY0amSigWcWKFb5MWmWPk30Dsq-nkRS0g@mail.gmail.com>","date":"2023-07-26T09:14:33","subject":"Re: [libcamera-devel] [PATCH 3/4] libcamera: camera_sensor: Add\n\tfunction to apply a config","submitter":{"id":34,"url":"https://patchwork.libcamera.org/api/people/34/","name":"Naushir Patuck","email":"naush@raspberrypi.com"},"content":"Hi Jacopo,\n\nThank you for your work.\n\nOn Mon, 24 Jul 2023 at 13:39, Jacopo Mondi via libcamera-devel\n<libcamera-devel@lists.libcamera.org> wrote:\n>\n> Add to the CameraSensor class a function to apply to the sensor\n> a full configuration.\n>\n> The configuration shall be fully populated and shall apply without\n> modifications to the sensor.\n>\n> Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\n\nReviewed-by: Naushir Patuck <naush@raspberrypi.com>\n\n> ---\n>  include/libcamera/internal/camera_sensor.h |  5 ++\n>  src/libcamera/camera_sensor.cpp            | 86 ++++++++++++++++++++++\n>  2 files changed, 91 insertions(+)\n>\n> diff --git a/include/libcamera/internal/camera_sensor.h b/include/libcamera/internal/camera_sensor.h\n> index 02c77ab037da..06791c3c6425 100644\n> --- a/include/libcamera/internal/camera_sensor.h\n> +++ b/include/libcamera/internal/camera_sensor.h\n> @@ -29,6 +29,7 @@ namespace libcamera {\n>  class BayerFormat;\n>  class CameraLens;\n>  class MediaEntity;\n> +class SensorConfiguration;\n>\n>  struct CameraSensorProperties;\n>\n> @@ -58,6 +59,10 @@ public:\n>                       Transform transform = Transform::Identity);\n>         int tryFormat(V4L2SubdeviceFormat *format) const;\n>\n> +       int applyConfiguration(const SensorConfiguration &config,\n> +                              Transform transform = Transform::Identity,\n> +                              V4L2SubdeviceFormat *sensorFormat = nullptr);\n> +\n>         const ControlInfoMap &controls() const;\n>         ControlList getControls(const std::vector<uint32_t> &ids);\n>         int setControls(ControlList *ctrls);\n> diff --git a/src/libcamera/camera_sensor.cpp b/src/libcamera/camera_sensor.cpp\n> index f3a5aa37149f..96b618572a3d 100644\n> --- a/src/libcamera/camera_sensor.cpp\n> +++ b/src/libcamera/camera_sensor.cpp\n> @@ -15,6 +15,7 @@\n>  #include <math.h>\n>  #include <string.h>\n>\n> +#include <libcamera/camera.h>\n>  #include <libcamera/property_ids.h>\n>\n>  #include <libcamera/base/utils.h>\n> @@ -822,6 +823,91 @@ int CameraSensor::tryFormat(V4L2SubdeviceFormat *format) const\n>                                   V4L2Subdevice::Whence::TryFormat);\n>  }\n>\n> +/**\n> + * \\brief Apply a sensor configuration to the camera sensor\n> + * \\param[in] config The sensor configuration\n> + * \\param[in] transform The transform to be applied on the sensor.\n> + * Defaults to Identity\n> + * \\param[out] sensorFormat Optional output parameter where the format\n> + * actually applied to the sensor is returned to the caller\n> + *\n> + * Apply to the camera sensor the configuration \\a config.\n> + *\n> + * \\todo The configuration shall be fully populated and if any of the fields\n> + * there specified cannot be applied as it is, an error code is returned.\n> + *\n> + * \\return 0 if \\a config is applied to the camera sensor, a negative error code\n> + * otherwise\n> + */\n> +int CameraSensor::applyConfiguration(const SensorConfiguration &config,\n> +                                    Transform transform,\n> +                                    V4L2SubdeviceFormat *sensorFormat)\n> +{\n> +       if (!config) {\n> +               LOG(CameraSensor, Error) << \"Invalid sensor configuration\";\n> +               return -EINVAL;\n> +       }\n> +\n> +       std::vector<unsigned int> filteredCodes;\n> +       std::copy_if(mbusCodes_.begin(), mbusCodes_.end(),\n> +                    std::back_inserter(filteredCodes),\n> +                    [&config](unsigned int mbusCode) {\n> +                            BayerFormat bayer = BayerFormat::fromMbusCode(mbusCode);\n> +                            if (bayer.bitDepth == config.bitDepth)\n> +                                    return true;\n> +                            return false;\n> +                    });\n> +       if (filteredCodes.empty()) {\n> +               LOG(CameraSensor, Error)\n> +                       << \"Cannot find any format with bit depth \"\n> +                       << config.bitDepth;\n> +               return -EINVAL;\n> +       }\n> +\n> +       /*\n> +        * Compute the sensor's data frame size by applying the cropping\n> +        * rectangle, subsampling and output crop to the sensor's pixel array\n> +        * size.\n> +        *\n> +        * \\todo The actual size computation is for now ignored and only the\n> +        * output size is considered. This implies that resolutions obtained\n> +        * with two different cropping/subsampling will look identical and\n> +        * only the first found one will be considered.\n> +        */\n> +       V4L2SubdeviceFormat subdevFormat = {};\n> +       for (unsigned int code : filteredCodes) {\n> +               for (const Size &size : sizes(code)) {\n> +                       if (size.width != config.outputSize.width ||\n> +                           size.height != config.outputSize.height)\n> +                               continue;\n> +\n> +                       subdevFormat.mbus_code = code;\n> +                       subdevFormat.size = size;\n> +                       break;\n> +               }\n> +       }\n> +       if (!subdevFormat.mbus_code) {\n> +               LOG(CameraSensor, Error) << \"Invalid output size in sensor configuration\";\n> +               return -EINVAL;\n> +       }\n> +\n> +       int ret = setFormat(&subdevFormat, transform);\n> +       if (ret)\n> +               return ret;\n> +\n> +       /*\n> +        * Return to the caller the format actually applied to the sensor.\n> +        * This is relevant if transform has changed the bayer pattern order.\n> +        */\n> +       if (sensorFormat)\n> +               *sensorFormat = subdevFormat;\n> +\n> +       /* \\todo Handle AnalogCrop. Most sensors do not support set_selection */\n> +       /* \\todo Handle scaling in the digital domain. */\n> +\n> +       return 0;\n> +}\n> +\n>  /**\n>   * \\brief Retrieve the supported V4L2 controls and their information\n>   *\n> --\n> 2.40.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 F112BBDB13\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 26 Jul 2023 09:14:50 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 7451A628C0;\n\tWed, 26 Jul 2023 11:14:50 +0200 (CEST)","from mail-yw1-x1136.google.com (mail-yw1-x1136.google.com\n\t[IPv6:2607:f8b0:4864:20::1136])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id AF1CF628BA\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 26 Jul 2023 11:14:49 +0200 (CEST)","by mail-yw1-x1136.google.com with SMTP id\n\t00721157ae682-579de633419so73716187b3.3\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 26 Jul 2023 02:14:49 -0700 (PDT)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1690362890;\n\tbh=bd4wYLf6kgMfSr4grN4WHqINVsQhvdcSpNeN6fNwfZk=;\n\th=References:In-Reply-To:Date:To:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=ZsQydktByEhmi3nLpuRxrjJRwoTbVD1G/ELYx+lhrvD2JjNn/GbLRqGPdPhAaAt7x\n\t3x16TqM4oTiYxOJh0Mi1L2LSQPNxi/7v20e2hXpt9UEY3KEGkPbf/gsTBpKK4bd3b4\n\tekUVu9f67wQnqLalmSMxdax7++calQV93dvllEvgUziVVJG3ZzuOWIww986NCH7dpC\n\t+Y2NnHGNVnMRlBdo39tFFQ9OstRjNPDgqjKyKeskrvM2nzwfnTUU0RkyyCf5juNOII\n\tpUNXEu/SE1WB7o6R1laE6ILqLN7zjsXjZmRBeQ+SRjF+e9VXC7f1rj3pfxDrfpA7Qo\n\tFhDuinB6MoTkw==","v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=raspberrypi.com; s=google; t=1690362888; x=1690967688;\n\th=cc:to:subject:message-id:date:from:in-reply-to:references\n\t:mime-version:from:to:cc:subject:date:message-id:reply-to;\n\tbh=c6E4Y2oroHwL5oCIje4YWc3WwEnXjahJzrKIgwkvL/0=;\n\tb=f7d6GKNcxbX78y8Dj10F7Aew9k/biRyYUJImTzGLDQqcGRNG01m2jGhwMHs/Cm0kKl\n\tvWwaAVqDbEbt6+YbThGIbgG+4lYikKbVnP0frxrjhHmA48wXChdkp6/tp1/6W9S/ZbAt\n\tGXYzq8PhDZIlb4lixVMESQES8RrOEts95tq8zRrfCr+OgSX3+T7EaZ8cTHyYTmWeEWLY\n\t6tfe9/1G4UDMTKWoknzUn/lhWFp7Q2cKfvvpFq32oGEiDPqcyGjZmlq0wg9n1DOBUcfy\n\tMaBCzNq7neIJrEYbC9CVOrjj8M/Y27oln8pgs2+sBmEnCFPzF68dvgG/gbp7Xg9xepWa\n\tFtlw=="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (2048-bit key; \n\tunprotected) header.d=raspberrypi.com\n\theader.i=@raspberrypi.com\n\theader.b=\"f7d6GKNc\"; dkim-atps=neutral","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20221208; t=1690362888; x=1690967688;\n\th=cc:to:subject:message-id:date:from:in-reply-to:references\n\t:mime-version:x-gm-message-state:from:to:cc:subject:date:message-id\n\t:reply-to;\n\tbh=c6E4Y2oroHwL5oCIje4YWc3WwEnXjahJzrKIgwkvL/0=;\n\tb=Ganncq8Y3EYGBmpJ3Fj+uL1DZU1+52qXY4mm9018RF5UnAVon7XCewJCo9CrJZ6CJ+\n\tA9Ob9hAxK8F45en32mbKZFOr7vUyPmbujOGrY2e7GNprHGbRLZ1twsnUqriA24m8o8sS\n\tzV58EOCUrtv1m74Tdz3EhDdqARwnaNIw0Sw1yzyznRItjHPfJnWe4uxtk6L8UItZBahj\n\tAAX1MGSmYeCi7pGF+Z/Qwx6uuaKG6h2j+x54xB4rmmsHwHmbr7e/2UBgRwb7F6ourz5X\n\to7oNOVEGGzGl1mgVlcLW2j5Ot20k43v65kqzw8rUM7xcLnHdLhYu86KGPYJcQsze1xGO\n\txMlA==","X-Gm-Message-State":"ABy/qLZM4ktA3HRLBGv4vYHEmQErwgpN5wdMFpG8IJ5+QfGu7QV/Cvfe\n\tNxipNduPGZFReu6XLqBORykqS9dxLs4dqjde9fhlEUBKJa4SbR3skVQ=","X-Google-Smtp-Source":"APBJJlEqrUJDBRcuoL0F3S8fsv6N6CgOpKJaKIn1R8G2Y9seyHkOwKL8BPeFkRHFu6ZXg4IWYg5SuhaVVRNRa0L0Exc=","X-Received":"by 2002:a0d:f001:0:b0:57a:2cd2:fdb9 with SMTP id\n\tz1-20020a0df001000000b0057a2cd2fdb9mr1386596ywe.18.1690362888478;\n\tWed, 26 Jul 2023 02:14:48 -0700 (PDT)","MIME-Version":"1.0","References":"<20230724123907.29086-1-jacopo.mondi@ideasonboard.com>\n\t<20230724123907.29086-4-jacopo.mondi@ideasonboard.com>","In-Reply-To":"<20230724123907.29086-4-jacopo.mondi@ideasonboard.com>","Date":"Wed, 26 Jul 2023 10:14:33 +0100","Message-ID":"<CAEmqJPpDFVL59JVgdkY0amSigWcWKFb5MWmWPk30Dsq-nkRS0g@mail.gmail.com>","To":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>","Content-Type":"text/plain; charset=\"UTF-8\"","Subject":"Re: [libcamera-devel] [PATCH 3/4] libcamera: camera_sensor: Add\n\tfunction to apply a config","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":"Naushir Patuck via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Naushir Patuck <naush@raspberrypi.com>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]