{"id":19048,"url":"https://patchwork.libcamera.org/api/1.1/patches/19048/?format=json","web_url":"https://patchwork.libcamera.org/patch/19048/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/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":"<20230916121930.29490-11-jacopo.mondi@ideasonboard.com>","date":"2023-09-16T12:19:28","name":"[libcamera-devel,v4,10/12] libcamera: rpi: Simplify validate() and configure() for RAW streams","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"9b50d5c0bd84dd8237c7527a636285530c4b3d9c","submitter":{"id":143,"url":"https://patchwork.libcamera.org/api/1.1/people/143/?format=json","name":"Jacopo Mondi","email":"jacopo.mondi@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/19048/mbox/","series":[{"id":4027,"url":"https://patchwork.libcamera.org/api/1.1/series/4027/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4027","date":"2023-09-16T12:19:18","name":"libcamera: Introduce SensorConfiguration","version":4,"mbox":"https://patchwork.libcamera.org/series/4027/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/19048/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/19048/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 B4356C3260\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSat, 16 Sep 2023 12:19:50 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 68E5162922;\n\tSat, 16 Sep 2023 14:19:50 +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 9A633628E9\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat, 16 Sep 2023 14:19:41 +0200 (CEST)","from uno.LocalDomain (93-61-96-190.ip145.fastwebnet.it\n\t[93.61.96.190])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 85D2F8BE;\n\tSat, 16 Sep 2023 14:18:07 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1694866790;\n\tbh=joF5SwOXNjXn9yy67Ol4wUMncn/0xR5d/g+WVYRbqCc=;\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=4ZXo51oIKOqOEk8XS7pqVnzUM9y8W02xex4WKdki92RrNvyU2PgJqtcJWdPPFeClz\n\tJg6Peqane45z15peNehU1T/kSERzXbDozgjBH++uNdywBKKEbQfgRHUITfFOm3m0oJ\n\tdp/ZvYvW3lt97/kvSZ0Pzt2NEVeU+xeWE+0yeMbqkHjWjCjgl3oo32hzkinaQQdUVi\n\tDBvvG71ay4aQ69ULPnO5wLYUySwJVp4VMYlPxd+cS8R86YnmjMLLLzfrYK/3EQMuy3\n\tPU9JrOQyWUlme/e4X4/BW+yaximLRGZfeSPRPQ2S82t38wT+4q+bDkR4Tvxa9L6UOW\n\tspLZGqdhciLmQ==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1694866687;\n\tbh=joF5SwOXNjXn9yy67Ol4wUMncn/0xR5d/g+WVYRbqCc=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=gVHP36gjQaRgdyqb93iQRQtCwkomkTd39/Q168g/65Wl8OAfuZ3skRZ6xyAnYGJcL\n\t5HasBl371kAAt4ajnnOVSutegKJG9P84KT/R00dxEJ4llGU4nrbIsKur0mIcce1cGt\n\t45VhTxGApBlaVJqiyPsbfKgVnr+NoxMQzlOUMLK0="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"gVHP36gj\"; dkim-atps=neutral","To":"libcamera-devel@lists.libcamera.org","Date":"Sat, 16 Sep 2023 14:19:28 +0200","Message-ID":"<20230916121930.29490-11-jacopo.mondi@ideasonboard.com>","X-Mailer":"git-send-email 2.42.0","In-Reply-To":"<20230916121930.29490-1-jacopo.mondi@ideasonboard.com>","References":"<20230916121930.29490-1-jacopo.mondi@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v4 10/12] libcamera: rpi: Simplify\n\tvalidate() and configure() for RAW streams","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":"Jacopo Mondi via libcamera-devel <libcamera-devel@lists.libcamera.org>","Reply-To":"Jacopo Mondi <jacopo.mondi@ideasonboard.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":"From: Naushir Patuck <naush@raspberrypi.com>\n\nThis commit simplifies the validate() and configure() calls in the\npipeline handler in a number of ways:\n\n- Only pass the RPiCameraConfiguration structure into platformValidate()\n  and platformConfigure().\n\n- Determine the V4L2DeviceFormat structure fields for all streams in\n  validate(), cache them and reuse in configure() instead of\n  re-generating this structure multiple times.\n\n- Use the recently added updateStreamConfig() and toV4L2DeviceFormat()\n  helpers to populate fields in the V4L2DeviceFormat and\n  StreamConfiguration structures to reduce code duplication.\n\nSigned-off-by: Naushir Patuck <naush@raspberrypi.com>\nReviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\nSigned-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\n---\n .../pipeline/rpi/common/pipeline_base.cpp     | 48 +++++--------------\n .../pipeline/rpi/common/pipeline_base.h       |  5 +-\n src/libcamera/pipeline/rpi/vc4/vc4.cpp        | 35 ++++++++------\n 3 files changed, 34 insertions(+), 54 deletions(-)","diff":"diff --git a/src/libcamera/pipeline/rpi/common/pipeline_base.cpp b/src/libcamera/pipeline/rpi/common/pipeline_base.cpp\nindex 93779556fd81..9e4411f22902 100644\n--- a/src/libcamera/pipeline/rpi/common/pipeline_base.cpp\n+++ b/src/libcamera/pipeline/rpi/common/pipeline_base.cpp\n@@ -234,16 +234,10 @@ CameraConfiguration::Status RPiCameraConfiguration::validate()\n \n \t/* Further fixups on the RAW streams. */\n \tfor (auto &raw : rawStreams_) {\n-\t\tStreamConfiguration &cfg = config_.at(raw.index);\n-\n-\t\tV4L2DeviceFormat rawFormat;\n-\t\trawFormat.fourcc = raw.dev->toV4L2PixelFormat(cfg.pixelFormat);\n-\t\trawFormat.size = cfg.size;\n-\t\trawFormat.colorSpace = cfg.colorSpace;\n-\n-\t\tint ret = raw.dev->tryFormat(&rawFormat);\n+\t\tint ret = raw.dev->tryFormat(&raw.format);\n \t\tif (ret)\n \t\t\treturn Invalid;\n+\n \t\t/*\n \t\t * Some sensors change their Bayer order when they are h-flipped\n \t\t * or v-flipped, according to the transform. If this one does, we\n@@ -251,23 +245,15 @@ CameraConfiguration::Status RPiCameraConfiguration::validate()\n \t\t * Note how we must fetch the \"native\" (i.e. untransformed) Bayer\n \t\t * order, because the sensor may currently be flipped!\n \t\t */\n-\t\tV4L2PixelFormat fourcc = rawFormat.fourcc;\n+\t\tBayerFormat bayer = BayerFormat::fromPixelFormat(raw.cfg->pixelFormat);\n \t\tif (data_->flipsAlterBayerOrder_) {\n-\t\t\tBayerFormat bayer = BayerFormat::fromV4L2PixelFormat(fourcc);\n \t\t\tbayer.order = data_->nativeBayerOrder_;\n \t\t\tbayer = bayer.transform(combinedTransform_);\n-\t\t\tfourcc = bayer.toV4L2PixelFormat();\n \t\t}\n+\t\traw.cfg->pixelFormat = bayer.toPixelFormat();\n \n-\t\tPixelFormat inputPixFormat = fourcc.toPixelFormat();\n-\t\tif (raw.cfg->size != rawFormat.size || raw.cfg->pixelFormat != inputPixFormat) {\n-\t\t\traw.cfg->size = rawFormat.size;\n-\t\t\traw.cfg->pixelFormat = inputPixFormat;\n+\t\tif (RPi::PipelineHandlerBase::updateStreamConfig(raw.cfg, raw.format))\n \t\t\tstatus = Adjusted;\n-\t\t}\n-\n-\t\traw.cfg->stride = rawFormat.planes[0].bpl;\n-\t\traw.cfg->frameSize = rawFormat.planes[0].size;\n \t}\n \n \t/* Further fixups on the ISP output streams. */\n@@ -544,35 +530,25 @@ int PipelineHandlerBase::configure(Camera *camera, CameraConfiguration *config)\n \t * If the application has provided a sensor configuration apply it\n \t * instead of just applying a format.\n \t */\n-\tconst RPiCameraConfiguration *rpiConfig =\n-\t\t\t\tstatic_cast<const RPiCameraConfiguration *>(config);\n-\tV4L2SubdeviceFormat sensorFormat;\n+\tRPiCameraConfiguration *rpiConfig = static_cast<RPiCameraConfiguration *>(config);\n+\tV4L2SubdeviceFormat *sensorFormat = &rpiConfig->sensorFormat_;\n \n \tif (rpiConfig->sensorConfig.populated()) {\n \t\tret = data->sensor_->applyConfiguration(rpiConfig->sensorConfig,\n \t\t\t\t\t\t\trpiConfig->combinedTransform_,\n-\t\t\t\t\t\t\t&sensorFormat);\n+\t\t\t\t\t\t\tsensorFormat);\n \t} else {\n-\t\tsensorFormat = rpiConfig->sensorFormat_;\n-\t\tret = data->sensor_->setFormat(&sensorFormat,\n+\t\tret = data->sensor_->setFormat(sensorFormat,\n \t\t\t\t\t       rpiConfig->combinedTransform_);\n \t}\n \tif (ret)\n \t\treturn ret;\n \n-\t/* Use the user requested packing/bit-depth. */\n-\tstd::optional<BayerFormat::Packing> packing;\n-\tif (!rpiConfig->rawStreams_.empty()) {\n-\t\tBayerFormat bayerFormat =\n-\t\t\tBayerFormat::fromPixelFormat(rpiConfig->rawStreams_[0].cfg->pixelFormat);\n-\t\tpacking = bayerFormat.packing;\n-\t}\n-\n \t/*\n \t * Platform specific internal stream configuration. This also assigns\n \t * external streams which get configured below.\n \t */\n-\tret = data->platformConfigure(sensorFormat, packing, rpiConfig);\n+\tret = data->platformConfigure(rpiConfig);\n \tif (ret)\n \t\treturn ret;\n \n@@ -636,11 +612,11 @@ int PipelineHandlerBase::configure(Camera *camera, CameraConfiguration *config)\n \t\t */\n \t\tlink->setEnabled(true);\n \t\tconst MediaPad *sinkPad = link->sink();\n-\t\tret = device->setFormat(sinkPad->index(), &sensorFormat);\n+\t\tret = device->setFormat(sinkPad->index(), sensorFormat);\n \t\tif (ret) {\n \t\t\tLOG(RPI, Error) << \"Failed to set format on \" << device->entity()->name()\n \t\t\t\t\t<< \" pad \" << sinkPad->index()\n-\t\t\t\t\t<< \" with format  \" << sensorFormat\n+\t\t\t\t\t<< \" with format  \" << *sensorFormat\n \t\t\t\t\t<< \": \" << ret;\n \t\t\treturn ret;\n \t\t}\ndiff --git a/src/libcamera/pipeline/rpi/common/pipeline_base.h b/src/libcamera/pipeline/rpi/common/pipeline_base.h\nindex 491c5e98c4a1..0c4a59b7f590 100644\n--- a/src/libcamera/pipeline/rpi/common/pipeline_base.h\n+++ b/src/libcamera/pipeline/rpi/common/pipeline_base.h\n@@ -58,9 +58,7 @@ public:\n \t}\n \n \tvirtual CameraConfiguration::Status platformValidate(RPiCameraConfiguration *rpiConfig) const = 0;\n-\tvirtual int platformConfigure(const V4L2SubdeviceFormat &sensorFormat,\n-\t\t\t\t      std::optional<BayerFormat::Packing> packing,\n-\t\t\t\t      const RPiCameraConfiguration *rpiConfig) = 0;\n+\tvirtual int platformConfigure(const RPiCameraConfiguration *rpiConfig) = 0;\n \tvirtual void platformStart() = 0;\n \tvirtual void platformStop() = 0;\n \n@@ -269,6 +267,7 @@ public:\n \t\tunsigned int index;\n \t\tStreamConfiguration *cfg;\n \t\tV4L2VideoDevice *dev;\n+\t\tV4L2DeviceFormat format;\n \t};\n \n \tstd::vector<StreamParams> rawStreams_;\ndiff --git a/src/libcamera/pipeline/rpi/vc4/vc4.cpp b/src/libcamera/pipeline/rpi/vc4/vc4.cpp\nindex 2308577a613b..1a03c88d7ee3 100644\n--- a/src/libcamera/pipeline/rpi/vc4/vc4.cpp\n+++ b/src/libcamera/pipeline/rpi/vc4/vc4.cpp\n@@ -115,9 +115,7 @@ private:\n \t\tisp_[Isp::Input].dev()->setSelection(V4L2_SEL_TGT_CROP, &ispCrop_);\n \t}\n \n-\tint platformConfigure(const V4L2SubdeviceFormat &sensorFormat,\n-\t\t\t      std::optional<BayerFormat::Packing> packing,\n-\t\t\t      const RPi::RPiCameraConfiguration *rpiConfig) override;\n+\tint platformConfigure(const RPi::RPiCameraConfiguration *rpiConfig) override;\n \tint platformConfigureIpa(ipa::RPi::ConfigParams &params) override;\n \n \tint platformInitIpa([[maybe_unused]] ipa::RPi::InitParams &params) override\n@@ -416,6 +414,9 @@ CameraConfiguration::Status Vc4CameraData::platformValidate(RPi::RPiCameraConfig\n \t\t/* Handle flips to make sure to match the RAW stream format. */\n \t\tif (flipsAlterBayerOrder_)\n \t\t\trawBayer = rawBayer.transform(rpiConfig->combinedTransform_);\n+\n+\t\t/* Apply the user requested packing. */\n+\t\trawBayer.packing = BayerFormat::fromPixelFormat(rawStream->pixelFormat).packing;\n \t\tPixelFormat rawFormat = rawBayer.toPixelFormat();\n \n \t\tif (rawStream->pixelFormat != rawFormat ||\n@@ -425,6 +426,9 @@ CameraConfiguration::Status Vc4CameraData::platformValidate(RPi::RPiCameraConfig\n \n \t\t\tstatus = CameraConfiguration::Adjusted;\n \t\t}\n+\n+\t\trawStreams[0].format =\n+\t\t\tRPi::PipelineHandlerBase::toV4L2DeviceFormat(unicam_[Unicam::Image].dev(), rawStream);\n \t}\n \n \t/*\n@@ -500,23 +504,14 @@ int Vc4CameraData::platformPipelineConfigure(const std::unique_ptr<YamlObject> &\n \treturn 0;\n }\n \n-int Vc4CameraData::platformConfigure(const V4L2SubdeviceFormat &sensorFormat,\n-\t\t\t\t     std::optional<BayerFormat::Packing> packing,\n-\t\t\t\t     const RPi::RPiCameraConfiguration *rpiConfig)\n+int Vc4CameraData::platformConfigure(const RPi::RPiCameraConfiguration *rpiConfig)\n {\n \tconst std::vector<StreamParams> &rawStreams = rpiConfig->rawStreams_;\n \tconst std::vector<StreamParams> &outStreams = rpiConfig->outStreams_;\n \tint ret;\n \n-\tif (!packing)\n-\t\tpacking = BayerFormat::Packing::CSI2;\n-\n \tV4L2VideoDevice *unicam = unicam_[Unicam::Image].dev();\n-\tV4L2DeviceFormat unicamFormat = RPi::PipelineHandlerBase::toV4L2DeviceFormat(unicam, sensorFormat, *packing);\n-\n-\tret = unicam->setFormat(&unicamFormat);\n-\tif (ret)\n-\t\treturn ret;\n+\tV4L2DeviceFormat unicamFormat;\n \n \t/*\n \t * See which streams are requested, and route the user\n@@ -525,14 +520,24 @@ int Vc4CameraData::platformConfigure(const V4L2SubdeviceFormat &sensorFormat,\n \tif (!rawStreams.empty()) {\n \t\trawStreams[0].cfg->setStream(&unicam_[Unicam::Image]);\n \t\tunicam_[Unicam::Image].setFlags(StreamFlag::External);\n+\t\tunicamFormat = rawStreams[0].format;\n+\t} else {\n+\t\tunicamFormat =\n+\t\t\tRPi::PipelineHandlerBase::toV4L2DeviceFormat(unicam,\n+\t\t\t\t\t\t\t\t     rpiConfig->sensorFormat_,\n+\t\t\t\t\t\t\t\t     BayerFormat::Packing::CSI2);\n \t}\n \n+\tret = unicam->setFormat(&unicamFormat);\n+\tif (ret)\n+\t\treturn ret;\n+\n \tret = isp_[Isp::Input].dev()->setFormat(&unicamFormat);\n \tif (ret)\n \t\treturn ret;\n \n \tLOG(RPI, Info) << \"Sensor: \" << sensor_->id()\n-\t\t       << \" - Selected sensor format: \" << sensorFormat\n+\t\t       << \" - Selected sensor format: \" << rpiConfig->sensorFormat_\n \t\t       << \" - Selected unicam format: \" << unicamFormat;\n \n \t/* Use a sensible small default size if no output streams are configured. */\n","prefixes":["libcamera-devel","v4","10/12"]}