[{"id":4199,"web_url":"https://patchwork.libcamera.org/comment/4199/","msgid":"<20200323133256.GG4768@pendragon.ideasonboard.com>","date":"2020-03-23T13:32:56","subject":"Re: [libcamera-devel] [RFC 6/6] libcamera: ipu3: Add support for a\n\tRAW still capture stream","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Niklas,\n\nThank you for the patch.\n\nOn Mon, Mar 16, 2020 at 03:41:46AM +0100, Niklas Söderlund wrote:\n> Allow the RAW buffer cycling between CIO2 and IMGU to be memory copied\n> to a new FrameBuffer in a new RAW stream. This allows users to capture\n> the raw Bayer format coming from the sensor.\n> \n> As the RAW frame is memory copied queueing requests with the\n> StillCaptureRaw stream might impact performance.\n> \n> Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n> ---\n>  src/libcamera/pipeline/ipu3/ipu3.cpp | 83 +++++++++++++++++++++++++---\n>  1 file changed, 75 insertions(+), 8 deletions(-)\n> \n> diff --git a/src/libcamera/pipeline/ipu3/ipu3.cpp b/src/libcamera/pipeline/ipu3/ipu3.cpp\n> index dbb0c4328aa0efe5..18d9ad4d7318a034 100644\n> --- a/src/libcamera/pipeline/ipu3/ipu3.cpp\n> +++ b/src/libcamera/pipeline/ipu3/ipu3.cpp\n> @@ -142,11 +142,12 @@ class IPU3Stream : public Stream\n>  {\n>  public:\n>  \tIPU3Stream()\n> -\t\t: active_(false), device_(nullptr)\n> +\t\t: active_(false), raw_(false), device_(nullptr)\n>  \t{\n>  \t}\n>  \n>  \tbool active_;\n> +\tbool raw_;\n>  \tstd::string name_;\n>  \tImgUDevice::ImgUOutput *device_;\n>  };\n> @@ -170,6 +171,7 @@ public:\n>  \n>  \tIPU3Stream outStream_;\n>  \tIPU3Stream vfStream_;\n> +\tIPU3Stream rawStream_;\n>  };\n>  \n>  class IPU3CameraConfiguration : public CameraConfiguration\n> @@ -308,8 +310,8 @@ CameraConfiguration::Status IPU3CameraConfiguration::validate()\n>  \t\treturn Invalid;\n>  \n>  \t/* Cap the number of entries to the available streams. */\n> -\tif (config_.size() > 2) {\n> -\t\tconfig_.resize(2);\n> +\tif (config_.size() > 3) {\n\nTime to add a constexpr unsigned int MAX_STREAMS = 3 ?\n\n> +\t\tconfig_.resize(3);\n>  \t\tstatus = Adjusted;\n>  \t}\n>  \n> @@ -322,6 +324,8 @@ CameraConfiguration::Status IPU3CameraConfiguration::validate()\n>  \tSize size = {};\n>  \n>  \tfor (const StreamConfiguration &cfg : config_) {\n> +\t\tif (cfg.pixelFormat.modifiers().count(IPU3_FORMAT_MOD_PACKED))\n> +\t\t\tcontinue;\n\nThis calls for some comments I think, I'm not sure to understand the\nlogic. The validate() function is getting hard to read, I think we'll\nhave to refactor it in the near future.\n\n>  \t\tif (cfg.size.width > size.width)\n>  \t\t\tsize.width = cfg.size.width;\n>  \t\tif (cfg.size.height > size.height)\n> @@ -349,6 +353,7 @@ CameraConfiguration::Status IPU3CameraConfiguration::validate()\n>  \tstd::set<const IPU3Stream *> availableStreams = {\n>  \t\t&data_->outStream_,\n>  \t\t&data_->vfStream_,\n> +\t\t&data_->rawStream_,\n>  \t};\n>  \n>  \tstreams_.clear();\n> @@ -360,7 +365,9 @@ CameraConfiguration::Status IPU3CameraConfiguration::validate()\n>  \t\tconst Size size = cfg.size;\n>  \t\tconst IPU3Stream *stream;\n>  \n> -\t\tif (cfg.size == sensorFormat_.size)\n> +\t\tif (cfg.pixelFormat.modifiers().count(IPU3_FORMAT_MOD_PACKED))\n> +\t\t\tstream = &data_->rawStream_;\n> +\t\telse if (cfg.size == sensorFormat_.size)\n>  \t\t\tstream = &data_->outStream_;\n>  \t\telse\n>  \t\t\tstream = &data_->vfStream_;\n> @@ -371,8 +378,15 @@ CameraConfiguration::Status IPU3CameraConfiguration::validate()\n>  \t\tLOG(IPU3, Debug)\n>  \t\t\t<< \"Assigned '\" << stream->name_ << \"' to stream \" << i;\n>  \n> -\t\tbool scale = stream == &data_->vfStream_;\n> -\t\tadjustStream(config_[i], scale);\n> +\t\tif (stream == &data_->rawStream_) {\n\nMaybe if stream->raw_ ? Or maybe it make no difference.\n\n> +\t\t\tcfg.pixelFormat = PixelFormat(DRM_FORMAT_SRGGB10,\n\nWhat if the sensor produces a different Bayer pattern ?\n\n> +\t\t\t\t\t\t      { IPU3_FORMAT_MOD_PACKED });\n> +\t\t\tcfg.size = sensor->resolution();\n> +\t\t\tcfg.bufferCount = IPU3_BUFFER_COUNT;\n> +\t\t} else {\n> +\t\t\tbool scale = stream == &data_->vfStream_;\n> +\t\t\tadjustStream(config_[i], scale);\n> +\t\t}\n>  \n>  \t\tif (cfg.pixelFormat != pixelFormat || cfg.size != size) {\n>  \t\t\tLOG(IPU3, Debug)\n> @@ -401,6 +415,7 @@ CameraConfiguration *PipelineHandlerIPU3::generateConfiguration(Camera *camera,\n>  \tstd::set<IPU3Stream *> streams = {\n>  \t\t&data->outStream_,\n>  \t\t&data->vfStream_,\n> +\t\t&data->rawStream_,\n>  \t};\n>  \n>  \tconfig = new IPU3CameraConfiguration(camera, data);\n> @@ -442,6 +457,21 @@ CameraConfiguration *PipelineHandlerIPU3::generateConfiguration(Camera *camera,\n>  \n>  \t\t\tbreak;\n>  \n> +\t\tcase StreamRole::StillCaptureRaw: {\n> +\t\t\tif (streams.find(&data->rawStream_) == streams.end()) {\n> +\t\t\t\tLOG(IPU3, Error)\n> +\t\t\t\t\t<< \"No stream available for requested role \"\n> +\t\t\t\t\t<< role;\n\nCan this happen ? Maybe if the raw role is specified multiple times ?\n\n> +\t\t\t\tbreak;\n> +\t\t\t}\n> +\n> +\t\t\tstream = &data->rawStream_;\n> +\n> +\t\t\tcfg.pixelFormat = PixelFormat(DRM_FORMAT_SRGGB10, { IPU3_FORMAT_MOD_PACKED });\n> +\t\t\tcfg.size = data->cio2_.sensor_->resolution();\n> +\t\t\tbreak;\n> +\t\t}\n> +\n>  \t\tcase StreamRole::Viewfinder:\n>  \t\tcase StreamRole::VideoRecording: {\n>  \t\t\t/*\n> @@ -575,6 +605,9 @@ int PipelineHandlerIPU3::configure(Camera *camera, CameraConfiguration *c)\n>  \t\tstream->active_ = true;\n>  \t\tcfg.setStream(stream);\n>  \n> +\t\tif (stream->raw_)\n> +\t\t\tcontinue;\n> +\n>  \t\tret = imgu->configureOutput(stream->device_, cfg);\n>  \t\tif (ret)\n>  \t\t\treturn ret;\n> @@ -625,9 +658,15 @@ int PipelineHandlerIPU3::configure(Camera *camera, CameraConfiguration *c)\n>  int PipelineHandlerIPU3::exportFrameBuffers(Camera *camera, Stream *stream,\n>  \t\t\t\t\t    std::vector<std::unique_ptr<FrameBuffer>> *buffers)\n>  {\n> +\tIPU3CameraData *data = cameraData(camera);\n>  \tIPU3Stream *ipu3stream = static_cast<IPU3Stream *>(stream);\n> -\tV4L2VideoDevice *video = ipu3stream->device_->dev;\n>  \tunsigned int count = stream->configuration().bufferCount;\n> +\tV4L2VideoDevice *video;\n> +\n> +\tif (ipu3stream->raw_)\n> +\t\tvideo = data->cio2_.output_;\n> +\telse\n> +\t\tvideo = ipu3stream->device_->dev;\n>  \n>  \treturn video->exportBuffers(count, buffers);\n>  }\n> @@ -740,6 +779,9 @@ int PipelineHandlerIPU3::queueRequestDevice(Camera *camera, Request *request)\n>  \t\tIPU3Stream *stream = static_cast<IPU3Stream *>(it.first);\n>  \t\tbuffer = it.second;\n>  \n> +\t\tif (stream->raw_)\n> +\t\t\tcontinue;\n\nThis needs a comment too. Overall we need to start documenting this code\nproperly, it's becoming unreadable :-S\n\n> +\n>  \t\tint ret = stream->device_->dev->queueBuffer(buffer);\n>  \t\tif (ret < 0)\n>  \t\t\terror = ret;\n> @@ -834,6 +876,7 @@ int PipelineHandlerIPU3::registerCameras()\n>  \t\tstd::set<Stream *> streams = {\n>  \t\t\t&data->outStream_,\n>  \t\t\t&data->vfStream_,\n> +\t\t\t&data->rawStream_,\n>  \t\t};\n>  \t\tCIO2Device *cio2 = &data->cio2_;\n>  \n> @@ -855,6 +898,8 @@ int PipelineHandlerIPU3::registerCameras()\n>  \t\tdata->outStream_.name_ = \"output\";\n>  \t\tdata->vfStream_.device_ = &data->imgu_->viewfinder_;\n>  \t\tdata->vfStream_.name_ = \"viewfinder\";\n> +\t\tdata->rawStream_.raw_ = true;\n> +\t\tdata->rawStream_.name_ = \"raw\";\n>  \n>  \t\t/*\n>  \t\t * Connect video devices' 'bufferReady' signals to their\n> @@ -946,7 +991,29 @@ void IPU3CameraData::cio2BufferReady(FrameBuffer *buffer)\n>  \tif (buffer->metadata().status == FrameMetadata::FrameCancelled)\n>  \t\treturn;\n>  \n> -\timgu_->input_->queueBuffer(buffer);\n> +\tRequest *request = cio2ToRequest_[buffer];\n> +\tFrameBuffer *raw = request->findBuffer(&rawStream_);\n> +\n> +\tif (!raw) {\n> +\t\t/* No RAW buffers present, just queue to IMGU. */\n> +\t\timgu_->input_->queueBuffer(buffer);\n> +\t\treturn;\n> +\t}\n> +\n> +\t/* RAW buffers present, special care is needed. */\n> +\tif (request->buffers().size() > 1)\n> +\t\timgu_->input_->queueBuffer(buffer);\n> +\n> +\tif (FrameBufferMemCopy(raw, buffer))\n> +\t\tLOG(IPU3, Debug) << \"Memcopy of FrameBuffer Failed\";\n\nI suppose that's OK for now, but it's a costly operation, we'll likely\nhave to defer this to a separate thread.\n\nShouldn't you set the raw buffer status to error if the copy fails ?\n\n> +\n> +\tpipe_->completeBuffer(camera_, request, raw);\n> +\n> +\tif (request->buffers().size() == 1) {\n> +\t\tcio2ToRequest_.erase(buffer);\n> +\t\tcio2_.putBuffer(buffer);\n> +\t\tpipe_->completeRequest(camera_, request);\n> +\t}\n>  }\n>  \n>  /* -----------------------------------------------------------------------------","headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 8822A6041A\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 23 Mar 2020 14:33:06 +0100 (CET)","from pendragon.ideasonboard.com (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id EA4C1308;\n\tMon, 23 Mar 2020 14:33:05 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1584970386;\n\tbh=vQGmuwbGDrpxbmQIEyB3WtlKQIlYErap/s78vm/bT1Y=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=KBbCUyjvY3/LXNjnUbkXGZ06DH5GD3tvTjFWFKLmhsntmSQB2jaDzFnYFodlWgbza\n\tE4Ip6roNoG0kfhJjpXSzWKS2tP0dHtPZEeAvpcQG0rShmaZR3ij6XJvueC7NvtDtr7\n\tp8HPULOhttuORtzxrYEa6FX0DfPzVlI13jbTFkWw=","Date":"Mon, 23 Mar 2020 15:32:56 +0200","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Niklas =?utf-8?q?S=C3=B6derlund?= <niklas.soderlund@ragnatech.se>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20200323133256.GG4768@pendragon.ideasonboard.com>","References":"<20200316024146.2474424-1-niklas.soderlund@ragnatech.se>\n\t<20200316024146.2474424-7-niklas.soderlund@ragnatech.se>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20200316024146.2474424-7-niklas.soderlund@ragnatech.se>","User-Agent":"Mutt/1.10.1 (2018-07-13)","Subject":"Re: [libcamera-devel] [RFC 6/6] libcamera: ipu3: Add support for a\n\tRAW still capture stream","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>","X-List-Received-Date":"Mon, 23 Mar 2020 13:33:06 -0000"}}]