{"id":19029,"url":"https://patchwork.libcamera.org/api/1.1/patches/19029/?format=json","web_url":"https://patchwork.libcamera.org/patch/19029/","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":"<20230915130650.35691-11-jacopo.mondi@ideasonboard.com>","date":"2023-09-15T13:06:48","name":"[libcamera-devel,v3,10/12] libcamera: rpi: Simplify validate() and configure() for RAW streams","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"9b4a04a9b434aa73a2508d78758dca24fd2ca4c3","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/19029/mbox/","series":[{"id":4025,"url":"https://patchwork.libcamera.org/api/1.1/series/4025/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4025","date":"2023-09-15T13:06:38","name":"libcamera: Introduce SensorConfiguration","version":3,"mbox":"https://patchwork.libcamera.org/series/4025/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/19029/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/19029/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 4372BBD160\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 15 Sep 2023 13:07:21 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id D4E5962925;\n\tFri, 15 Sep 2023 15:07:20 +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 2D4FF6291C\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 15 Sep 2023 15:07:12 +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 BA109208C;\n\tFri, 15 Sep 2023 15:05:38 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1694783240;\n\tbh=iLQ8LBHmiC3945zZsyxvl/bmfpMvg4wmJfLfordAR+I=;\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=O9I4lwUaFkC0F9/nwDEXjpe4NNqosSmtXKckAtoD3fXYhVeJxXQPi4f0xI0Yd3ZSV\n\tgG/jgdG+r1N/AK2AKSRakoUaNM/6PE4hgvJuY1XSB5r5tFDD08ExXE9YM+0pc2dujB\n\tlrYuv3ToyRegd7MNxghyTM9p/g8de6fF1zDb4hllBWuD1VgIa5i9EjyjuXgT97vLI1\n\tyzHsf/2+7nOc0qSidvnOx6Sf4Vx6WuHCVwmmUpeEViBOpKieFxVksGwuF0nP3yGBhO\n\tBJN/I70Aco+b1h6DD9OKDpxqzLSH2scGAUxq3gA14jSd+TmQiznkmJn0GPcUWvrQOL\n\tzVqy4xgcUoY0w==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1694783139;\n\tbh=iLQ8LBHmiC3945zZsyxvl/bmfpMvg4wmJfLfordAR+I=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=vtXD1oCH4SAffQSKc1HYGGxDlIkmd+2XBg2Vf/kQcNkj0enNDHHkyJYqc0WHUHsbl\n\toWnJ4hZ/GxVpgGd3uPKvHKaiXZSwpvVBdmFR+9LE5NfBd8NcrnTa6WG1uq03XiPuqt\n\t0TrkHuuQXdjx7mpA4rT3jUPw8SGKQPsc1OTEo39g="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"vtXD1oCH\"; dkim-atps=neutral","To":"libcamera-devel@lists.libcamera.org","Date":"Fri, 15 Sep 2023 15:06:48 +0200","Message-ID":"<20230915130650.35691-11-jacopo.mondi@ideasonboard.com>","X-Mailer":"git-send-email 2.42.0","In-Reply-To":"<20230915130650.35691-1-jacopo.mondi@ideasonboard.com>","References":"<20230915130650.35691-1-jacopo.mondi@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v3 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 b523d560420a..d83811801460 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) {\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","v3","10/12"]}