[{"id":27665,"web_url":"https://patchwork.libcamera.org/comment/27665/","msgid":"<169222123253.695434.10047930417440014774@ping.linuxembedded.co.uk>","date":"2023-08-16T21:27:12","subject":"Re: [libcamera-devel] [RFC PATCH 4/5] libcamera: pipeline:\n\tuvcvideo: Allocate metadata buffers","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"Quoting Gabby George (2023-08-14 12:28:48)\n> Perform the allocation and mapping of metadata buffers into\n> libcamera's virtual address space.  UVC metadata buffers cannot be\n> exported as DMA buffer file descriptors, so use the MappedFrameBuffer\n> class to map them into memory directly.  This will give the UVC\n> pipeline access to buffer data to extract timestamp information.\n> \n> Metadata buffers are internal to the UVC pipeline, so buffer memory\n> should not be exposed to the user.\n\nSounds reasonable.\n\n\n> \n> Signed-off-by: Gabby George <gabbymg94@gmail.com>\n> ---\n>  src/libcamera/pipeline/uvcvideo/uvcvideo.cpp | 97 +++++++++++++++++++-\n>  1 file changed, 95 insertions(+), 2 deletions(-)\n> \n> diff --git a/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp b/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp\n> index 4470d8a2..51f30187 100644\n> --- a/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp\n> +++ b/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp\n> @@ -24,6 +24,7 @@\n>  \n>  #include \"libcamera/internal/camera.h\"\n>  #include \"libcamera/internal/device_enumerator.h\"\n> +#include \"libcamera/internal/mapped_framebuffer.h\"\n>  #include \"libcamera/internal/media_device.h\"\n>  #include \"libcamera/internal/pipeline_handler.h\"\n>  #include \"libcamera/internal/sysfs.h\"\n> @@ -51,11 +52,17 @@ public:\n>         std::unique_ptr<V4L2VideoDevice> video_;\n>         std::unique_ptr<V4L2VideoDevice> metadata_;\n>         Stream stream_;\n> +       std::vector<std::unique_ptr<FrameBuffer>> metadataBuffers_;\n> +       std::map<unsigned int, MappedFrameBuffer> mappedMetadataBuffers_;\n> +       bool useMetadataStream_;\n\nI wonder if we'll need this bool. Just from reading these 6 lines, I\ncould probably assume we could replace anything that has \n\tif (useMoetadataStream_)\nwith\n\tif (metadata_)\n\n\n\n> +\n>         std::map<PixelFormat, std::vector<SizeRange>> formats_;\n>  \n>  private:\n>         int initMetadata(MediaDevice *media);\n>  \n> +       const unsigned int minLengthHeaderBuf_ = 10;\n> +\n>         bool generateId();\n>  \n>         std::string id_;\n> @@ -96,6 +103,10 @@ private:\n>                            const ControlValue &value);\n>         int processControls(UVCCameraData *data, Request *request);\n>  \n> +       int createMetadataBuffers(Camera *camera, unsigned int count);\n> +       int cleanupMetadataBuffers(Camera *camera);\n> +       int cleanup(Camera *camera);\n> +\n>         UVCCameraData *cameraData(Camera *camera)\n>         {\n>                 return static_cast<UVCCameraData *>(camera->_d());\n> @@ -225,10 +236,66 @@ int PipelineHandlerUVC::configure(Camera *camera, CameraConfiguration *config)\n>                 return -EINVAL;\n>  \n>         cfg.setStream(&data->stream_);\n> +       return 0;\n> +}\n>  \n> +int PipelineHandlerUVC::cleanupMetadataBuffers(Camera *camera)\n> +{\n> +       int ret = 0;\n> +       UVCCameraData *data = cameraData(camera);\n> +\n> +       ret = data->metadata_->releaseBuffers();\n> +       data->metadataBuffers_.clear(); //call the destructor for the frame buffers\n\nI think we can remove the comment on that line.\n\n> +       data->mappedMetadataBuffers_.clear();\n> +       data->useMetadataStream_ = false;\n> +\n> +       return ret;\n> +}\n> +\n> +int PipelineHandlerUVC::cleanup(Camera *camera)\n> +{\n> +       UVCCameraData *data = cameraData(camera);\n> +       cleanupMetadataBuffers(camera);\n> +       data->video_->releaseBuffers();\n>         return 0;\n>  }\n>  \n> +/*\n\nOpen a block comment with\n \t/**\n\nif it's a documentation comment for doxygen.\n\n\n> + * UVC Metadata stream does not support exporting buffers via EXPBUF,\n> + * so it is necessary to create and store mmap-ed addresses.\n> + * Metadata buffers are internal to libcamera. They are not, and\n> + * cannot be, exposed to the user.\n> + *\n> + * Returns the number of buffers allocated and mapped.\n> + *\n> + * \\return The number of buffers allocated, or a negative error code if\n> + * the number of buffers allocated was not equal to \"count\"\n> + * \\retval -EINVAL if \"count\" buffers were not successfully allocated.\n> + * \\retval -ENOMEM if mmap failed.\n\nHrm ... Have you checked if this is what MappedFrameBuffer.error() will\nreturn? Maybe this should refer to saying 'MappedFrameBuffer::error()'\nif the Frame could not be mapped successfully ...\n\n> + */\n> +int PipelineHandlerUVC::createMetadataBuffers(Camera *camera, unsigned int count)\n> +{\n> +       UVCCameraData *data = cameraData(camera);\n> +       int ret = data->metadata_->allocateBuffers(count, &data->metadataBuffers_);\n> +       if (ret < 0)\n> +               return -EINVAL;\n> +\n> +       for (unsigned int i = 0; i < count; i++) {\n> +               MappedFrameBuffer mappedBuffer(data->metadataBuffers_[i].get(),\n> +                                              MappedFrameBuffer::MapFlag::Read, true);\n> +               if (!mappedBuffer.isValid()) {\n> +                       LOG(UVC, Error)\n> +                               << \"Failed to mmap metadata buffer: \"\n> +                               << strerror(mappedBuffer.error());\n> +                       return mappedBuffer.error();\n> +               }\n> +\n> +               data->mappedMetadataBuffers_.emplace(i, std::move(mappedBuffer));\n> +               data->metadataBuffers_[i]->setCookie(i);\n> +       }\n> +       return ret;\n> +}\n> +\n>  int PipelineHandlerUVC::exportFrameBuffers(Camera *camera, Stream *stream,\n>                                            std::vector<std::unique_ptr<FrameBuffer>> *buffers)\n>  {\n> @@ -247,20 +314,46 @@ int PipelineHandlerUVC::start(Camera *camera, [[maybe_unused]] const ControlList\n>         if (ret < 0)\n>                 return ret;\n>  \n> +       if (data->useMetadataStream_) {\n> +               if (createMetadataBuffers(camera, count) < 0) {\n> +                       LOG(UVC, Error) << \"Unable to allocate buffers for UVC metadata stream.\";\n> +                       data->useMetadataStream_ = false;\n> +               }\n> +       }\n> +\n>         ret = data->video_->streamOn();\n>         if (ret < 0) {\n> -               data->video_->releaseBuffers();\n> +               cleanup(camera);\n>                 return ret;\n>         }\n>  \n> +       if (data->useMetadataStream_) {\n> +               ret = data->metadata_->streamOn();\n> +               if (ret) {\n> +                       LOG(UVC, Error) << \"Failed to start metadata stream\";\n> +                       return ret;\n> +               }\n> +\n> +               for (std::unique_ptr<FrameBuffer> &buf : data->metadataBuffers_) {\n> +                       ret = data->metadata_->queueBuffer(buf.get());\n> +                       if (ret < 0) {\n> +                               cleanupMetadataBuffers(camera);\n> +                               return ret;\n> +                       }\n> +               }\n\nCan all of this be done before video_->streamOn() to avoid having two\nconditionals that check if we useMetadataStream_ ?\n\n\n> +       }\n>         return 0;\n>  }\n>  \n>  void PipelineHandlerUVC::stopDevice(Camera *camera)\n>  {\n>         UVCCameraData *data = cameraData(camera);\n> +\n>         data->video_->streamOff();\n> -       data->video_->releaseBuffers();\n> +\n> +       data->metadata_->streamOff();\n> +\n> +       cleanup(camera);\n>  }\n>  \n>  int PipelineHandlerUVC::processControl(ControlList *controls, unsigned int id,\n> -- \n> 2.34.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 53220BE08A\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 16 Aug 2023 21:27:18 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id AD5DA628D8;\n\tWed, 16 Aug 2023 23:27:17 +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 20D94628D3\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 16 Aug 2023 23:27:16 +0200 (CEST)","from pendragon.ideasonboard.com\n\t(aztw-30-b2-v4wan-166917-cust845.vm26.cable.virginm.net\n\t[82.37.23.78])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 9CC6F2CF;\n\tWed, 16 Aug 2023 23:26:02 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1692221237;\n\tbh=M2mpNQd6ifyV/H6zBCqLDV1h8CFqs3hkk88W95nTmDI=;\n\th=In-Reply-To:References:To:Date:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:\n\tFrom;\n\tb=AE6Z0NeC8nWegP2NnPEgNv3gmVNodFZT9WdwZrSQMPmgUlwDHvy3GPPo9q8lTfgro\n\tTtEYhAEM2uAjfGRAMxTA+2ybtZ0V1G0oc63DttfPQ+QspZd9WKfMBdLWK6TIheJOuv\n\tTvrtgz7GSzITmrGBhH9plryHQHXeNsaQ6ldkFhjJIP7Kw1Gs5HV6TIO5j4QdU8AETQ\n\tNvSEwL/XN6NzydNvESCTyNVcWy0rShOLm/icNsLZ/r33XnhxbkkCgk5KVwnDbZDvtN\n\tLYjvIlFxywwBW40OjkkUdExcNQSJP+N3YyQMhh3525zQv3LNkieF+Fj/ISzX/hOaVG\n\txi8wumpeovIWg==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1692221162;\n\tbh=M2mpNQd6ifyV/H6zBCqLDV1h8CFqs3hkk88W95nTmDI=;\n\th=In-Reply-To:References:Subject:From:To:Date:From;\n\tb=juCjgGHXfWjoykhLIk+OzqhvgrLPDm6+4RA6c20vk3NC+AMBssHg0tm6MSCQ6LMp1\n\tjOcN+jq+AlAHXcR2xGNB/QAw2KXVVgcAMHhgLoX5T8L/woAJBAF/LfqECaxPPKsN7u\n\tglG/m8jhKfsRldbpD1Cp22A69Bb437jBPR8gJPbo="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"juCjgGHX\"; dkim-atps=neutral","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<20230814112849.176943-5-gabbymg94@gmail.com>","References":"<20230814112849.176943-1-gabbymg94@gmail.com>\n\t<20230814112849.176943-5-gabbymg94@gmail.com>","To":"gabbymg94@gmail.com, libcamera-devel@lists.libcamera.org,\n\tvedantparanjape160201@gmail.com","Date":"Wed, 16 Aug 2023 22:27:12 +0100","Message-ID":"<169222123253.695434.10047930417440014774@ping.linuxembedded.co.uk>","User-Agent":"alot/0.10","Subject":"Re: [libcamera-devel] [RFC PATCH 4/5] libcamera: pipeline:\n\tuvcvideo: Allocate metadata buffers","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":"Kieran Bingham via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]