{"id":3351,"url":"https://patchwork.libcamera.org/api/patches/3351/?format=json","web_url":"https://patchwork.libcamera.org/patch/3351/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/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":"<20200326225844.4117712-8-niklas.soderlund@ragnatech.se>","date":"2020-03-26T22:58:44","name":"[libcamera-devel,v2,7/7] libcamera: ipu3: Add support for a RAW still capture stream","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"754fdae70d800a2c9bd2fca293bf67eeccda604b","submitter":{"id":5,"url":"https://patchwork.libcamera.org/api/people/5/?format=json","name":"Niklas Söderlund","email":"niklas.soderlund@ragnatech.se"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/3351/mbox/","series":[{"id":785,"url":"https://patchwork.libcamera.org/api/series/785/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=785","date":"2020-03-26T22:58:37","name":"libcamera: Add support for a RAW still capture","version":2,"mbox":"https://patchwork.libcamera.org/series/785/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/3351/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/3351/checks/","tags":{},"headers":{"Return-Path":"<niklas.soderlund@ragnatech.se>","Received":["from bin-mail-out-05.binero.net (bin-mail-out-05.binero.net\n\t[195.74.38.228])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id E7AAB62684\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 26 Mar 2020 23:59:01 +0100 (CET)","from bismarck.berto.se (p4fca2392.dip0.t-ipconnect.de\n\t[79.202.35.146]) by bin-vsp-out-02.atm.binero.net (Halon) with ESMTPA\n\tid 60774018-6fb5-11ea-aeed-005056917f90;\n\tThu, 26 Mar 2020 23:58:58 +0100 (CET)"],"X-Halon-ID":"60774018-6fb5-11ea-aeed-005056917f90","Authorized-sender":"niklas@soderlund.pp.se","From":"=?utf-8?q?Niklas_S=C3=B6derlund?= <niklas.soderlund@ragnatech.se>","To":"libcamera-devel@lists.libcamera.org","Date":"Thu, 26 Mar 2020 23:58:44 +0100","Message-Id":"<20200326225844.4117712-8-niklas.soderlund@ragnatech.se>","X-Mailer":"git-send-email 2.25.1","In-Reply-To":"<20200326225844.4117712-1-niklas.soderlund@ragnatech.se>","References":"<20200326225844.4117712-1-niklas.soderlund@ragnatech.se>","MIME-Version":"1.0","Content-Type":"text/plain; charset=UTF-8","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v2 7/7] 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":"Thu, 26 Mar 2020 22:59:03 -0000"},"content":"Allow the RAW buffer cycling between CIO2 and IMGU to be memory copied\nto a new FrameBuffer in a new RAW stream. This allows users to capture\nthe raw Bayer format coming from the sensor.\n\nAs the RAW frame is memory copied queueing requests with the\nStillCaptureRaw stream might impact performance.\n\nSigned-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n---\n* Changes since v1\n- Break out translation of mbus code to pixel format to a static map and\n  make use of it in validate() and generateConfiguration()\n- Move cfg.bufferCount = IPU3_BUFFER_COUNT from adjustStream() to\n  validate()\n- Added comment and updated an error message.\n- Added todo to not configure and start the ImgU if only a single raw\n  stream is requested.\n\n* Changes from RFC\n- Add definition for IPU3_MAX_STREAMS.\n- Deal with all IPU3 Bayer patterns.\n- Rework size logic.\n---\n src/libcamera/pipeline/ipu3/ipu3.cpp | 111 ++++++++++++++++++++++++---\n 1 file changed, 101 insertions(+), 10 deletions(-)","diff":"diff --git a/src/libcamera/pipeline/ipu3/ipu3.cpp b/src/libcamera/pipeline/ipu3/ipu3.cpp\nindex 0933a03c9f718591..30bc662cc8bfa935 100644\n--- a/src/libcamera/pipeline/ipu3/ipu3.cpp\n+++ b/src/libcamera/pipeline/ipu3/ipu3.cpp\n@@ -33,6 +33,13 @@ LOG_DEFINE_CATEGORY(IPU3)\n \n class IPU3CameraData;\n \n+static const std::map<uint32_t, PixelFormat> sensorMbusToPixel = {\n+\t{ MEDIA_BUS_FMT_SBGGR10_1X10, PixelFormat(DRM_FORMAT_SBGGR10, { IPU3_FORMAT_MOD_PACKED }) },\n+\t{ MEDIA_BUS_FMT_SGBRG10_1X10, PixelFormat(DRM_FORMAT_SGBRG10, { IPU3_FORMAT_MOD_PACKED }) },\n+\t{ MEDIA_BUS_FMT_SGRBG10_1X10, PixelFormat(DRM_FORMAT_SGRBG10, { IPU3_FORMAT_MOD_PACKED }) },\n+\t{ MEDIA_BUS_FMT_SRGGB10_1X10, PixelFormat(DRM_FORMAT_SRGGB10, { IPU3_FORMAT_MOD_PACKED }) },\n+};\n+\n class ImgUDevice\n {\n public:\n@@ -140,11 +147,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@@ -166,6 +174,7 @@ public:\n \n \tIPU3Stream outStream_;\n \tIPU3Stream vfStream_;\n+\tIPU3Stream rawStream_;\n };\n \n class IPU3CameraConfiguration : public CameraConfiguration\n@@ -180,6 +189,7 @@ public:\n \n private:\n \tstatic constexpr unsigned int IPU3_BUFFER_COUNT = 4;\n+\tstatic constexpr unsigned int IPU3_MAX_STREAMS = 3;\n \n \tvoid adjustStream(StreamConfiguration &cfg, bool scale);\n \n@@ -291,8 +301,6 @@ void IPU3CameraConfiguration::adjustStream(StreamConfiguration &cfg, bool scale)\n \t\tcfg.size.width &= ~7;\n \t\tcfg.size.height &= ~3;\n \t}\n-\n-\tcfg.bufferCount = IPU3_BUFFER_COUNT;\n }\n \n CameraConfiguration::Status IPU3CameraConfiguration::validate()\n@@ -304,8 +312,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() > IPU3_MAX_STREAMS) {\n+\t\tconfig_.resize(IPU3_MAX_STREAMS);\n \t\tstatus = Adjusted;\n \t}\n \n@@ -345,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@@ -356,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@@ -367,8 +378,20 @@ 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->raw_) {\n+\t\t\tconst auto &itFormat =\n+\t\t\t\tsensorMbusToPixel.find(sensorFormat_.mbus_code);\n+\t\t\tif (itFormat == sensorMbusToPixel.end())\n+\t\t\t\treturn Invalid;\n+\n+\t\t\tcfg.pixelFormat = itFormat->second;\n+\t\t\tcfg.size = sensorFormat_.size;\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\tcfg.bufferCount = IPU3_BUFFER_COUNT;\n \n \t\tif (cfg.pixelFormat != pixelFormat || cfg.size != size) {\n \t\t\tLOG(IPU3, Debug)\n@@ -397,6 +420,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@@ -438,6 +462,29 @@ 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+\t\t\t\tbreak;\n+\t\t\t}\n+\n+\t\t\tstream = &data->rawStream_;\n+\n+\t\t\tcfg.size = data->cio2_.sensor_->resolution();\n+\n+\t\t\tV4L2SubdeviceFormat sensorFormat =\n+\t\t\t\tdata->cio2_.sensor_->getFormat({ MEDIA_BUS_FMT_SBGGR10_1X10,\n+\t\t\t\t\t\t\t\t MEDIA_BUS_FMT_SGBRG10_1X10,\n+\t\t\t\t\t\t\t\t MEDIA_BUS_FMT_SGRBG10_1X10,\n+\t\t\t\t\t\t\t\t MEDIA_BUS_FMT_SRGGB10_1X10 },\n+\t\t\t\t\t\t\t       cfg.size);\n+\t\t\tcfg.pixelFormat =\n+\t\t\t\tsensorMbusToPixel.at(sensorFormat.mbus_code);\n+\t\t\tbreak;\n+\t\t}\n+\n \t\tcase StreamRole::Viewfinder:\n \t\tcase StreamRole::VideoRecording: {\n \t\t\t/*\n@@ -535,6 +582,9 @@ int PipelineHandlerIPU3::configure(Camera *camera, CameraConfiguration *c)\n \t/*\n \t * \\todo: Enable links selectively based on the requested streams.\n \t * As of now, enable all links unconditionally.\n+\t * \\todo Don't configure the ImgU at all if we only have a single\n+\t * stream which is for raw capture, in which case no buffers will\n+\t * never be queued to the ImgU.\n \t */\n \tret = data->imgu_->enableLinks(true);\n \tif (ret)\n@@ -571,6 +621,13 @@ int PipelineHandlerIPU3::configure(Camera *camera, CameraConfiguration *c)\n \t\tstream->active_ = true;\n \t\tcfg.setStream(stream);\n \n+\t\t/*\n+\t\t * The RAW still capture stream just copies buffers from the\n+\t\t * internal queue and doesn't need any specific configuration.\n+\t\t */\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@@ -621,9 +678,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@@ -737,6 +800,10 @@ int PipelineHandlerIPU3::queueRequestDevice(Camera *camera, Request *request)\n \t\tIPU3Stream *stream = static_cast<IPU3Stream *>(it.first);\n \t\tbuffer = it.second;\n \n+\t\t/* Skip raw streams, they are copied from the CIO2 buffer. */\n+\t\tif (stream->raw_)\n+\t\t\tcontinue;\n+\n \t\tint ret = stream->device_->dev->queueBuffer(buffer);\n \t\tif (ret < 0)\n \t\t\terror = ret;\n@@ -831,6 +898,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@@ -852,6 +920,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@@ -941,7 +1011,28 @@ void IPU3CameraData::cio2BufferReady(FrameBuffer *buffer)\n \tif (buffer->metadata().status == FrameMetadata::FrameCancelled)\n \t\treturn;\n \n-\timgu_->input_->queueBuffer(buffer);\n+\tRequest *request = buffer->request();\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 (raw->copyFrom(buffer))\n+\t\tLOG(IPU3, Debug) << \"Copy of FrameBuffer failed\";\n+\n+\tpipe_->completeBuffer(camera_, request, raw);\n+\n+\tif (request->buffers().size() == 1) {\n+\t\tcio2_.putBuffer(buffer);\n+\t\tpipe_->completeRequest(camera_, request);\n+\t}\n }\n \n /* -----------------------------------------------------------------------------\n","prefixes":["libcamera-devel","v2","7/7"]}