{"id":18941,"url":"https://patchwork.libcamera.org/api/1.1/patches/18941/?format=json","web_url":"https://patchwork.libcamera.org/patch/18941/","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":"<20230814112849.176943-5-gabbymg94@gmail.com>","date":"2023-08-14T11:28:48","name":"[libcamera-devel,RFC,4/5] libcamera: pipeline: uvcvideo: Allocate metadata buffers","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"cda3d51d02ca687b61bbfc61911283ef6491f0a3","submitter":{"id":160,"url":"https://patchwork.libcamera.org/api/1.1/people/160/?format=json","name":"Gabrielle George","email":"gabbymg94@gmail.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/18941/mbox/","series":[{"id":4003,"url":"https://patchwork.libcamera.org/api/1.1/series/4003/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4003","date":"2023-08-14T11:28:44","name":"RFC:Add UVC Metadata buffer timestamp support","version":1,"mbox":"https://patchwork.libcamera.org/series/4003/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/18941/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/18941/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 56798C32B1\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 14 Aug 2023 11:29:03 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 2541E628DD;\n\tMon, 14 Aug 2023 13:29:01 +0200 (CEST)","from mail-ot1-x32f.google.com (mail-ot1-x32f.google.com\n\t[IPv6:2607:f8b0:4864:20::32f])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 5A8C7628DB\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 14 Aug 2023 13:28:57 +0200 (CEST)","by mail-ot1-x32f.google.com with SMTP id\n\t46e09a7af769-6bca66e6c44so3735614a34.0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 14 Aug 2023 04:28:57 -0700 (PDT)","from localhost.localdomain (97-115-76-16.ptld.qwest.net.\n\t[97.115.76.16]) by smtp.gmail.com with ESMTPSA id\n\tc3-20020a9d6c83000000b006b92509e76esm4163817otr.32.2023.08.14.04.28.54\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tMon, 14 Aug 2023 04:28:55 -0700 (PDT)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1692012541;\n\tbh=szpRlwvfHuwJi9e/cV+qIhsj0OHhxLHDghQNsEK/8f4=;\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:\n\tFrom;\n\tb=SfK9cHj4V1t5fFQlKzRcROAnFVTPYCYz4Wtg3WmRfMmY8nKhU/Q2fvH9RXGp9/jOU\n\tqrZMbbozEJ4lOqeZv+d6Q/HUsB96GnJJRovTfdYBRmdq3Ani7fAXzAJY/YaujGmJbT\n\tDKJzptm5ROqGErQ/sz/y55oPg3QshEP97+CpXQ8ZSiIC3x85CJ9mmmuPjpfZlL92rk\n\to7/iP7rtiXg/ydePRtfHdMih2hYbmyKMnLY5c2qQI3Hzivr7LOiIB4BNslTTZbBae0\n\tWNjYP2yJbu1+FzHS0BaF50A0Hm9ZK2WPrWhkKTd70Kw6jt8Cavebs2Mcak9WvPbesY\n\t9dpz8/z58w+kQ==","v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=gmail.com; s=20221208; t=1692012536; x=1692617336;\n\th=content-transfer-encoding:mime-version:references:in-reply-to\n\t:message-id:date:subject:to:from:from:to:cc:subject:date:message-id\n\t:reply-to; bh=2paCm8n5nk6/G3ElxO9IuZP3Z4eBx88uZvY2ElGxhDk=;\n\tb=Not+KZnUubJMvjuTOcwvhfnSmib04EyzzXYOXH5+hNJpnH7/Fwbn5RDspj3r5knCrx\n\t6c1wWfb9AUYV3ICVNNbl4fKTBZ0ORdYYvYF72wBEsOrTiEh1Yb/D+eS5pwq5Fc3eviE4\n\tUxt1Saodr1uFX5161fJOXkjNu1TT939Wy/9Ovzl8rjHMdDGwVxnl+b2B2k9iRB6e+2uA\n\tBb1sPUu2MTG+apDsTM/m7YK3VyE2DLspxl+JcWiUmNObTpkGX0rFDIsd8tUq7ACVOHd3\n\tO63s6Yh0cdlKsRBInGzPyIbfCult/SHprM29UC1p+Up6H7hJOSbq85yFlEFHhPqrYwug\n\tBdqQ=="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (2048-bit key; \n\tunprotected) header.d=gmail.com header.i=@gmail.com\n\theader.b=\"Not+KZnU\"; dkim-atps=neutral","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20221208; t=1692012536; x=1692617336;\n\th=content-transfer-encoding:mime-version:references:in-reply-to\n\t:message-id:date:subject:to:from:x-gm-message-state:from:to:cc\n\t:subject:date:message-id:reply-to;\n\tbh=2paCm8n5nk6/G3ElxO9IuZP3Z4eBx88uZvY2ElGxhDk=;\n\tb=FxEu7rJvqKojAaRX89pZ3s9hMU+J7Ckcj4KXNlyAxYwjaaBfdFQmuTJBaMk/kuwN97\n\t3twNjjiHHUNSgQasOKo/3xiVYn38al2/rPEMtabCJBN3epBLeYgobcp4OoygPuFFvHnd\n\tzywzgJBDpXxdTmVh0ISBLWXCorpO7vKIdgcNBDjg8I6IARiMIqGNpsnFcF9GzDhvR0my\n\tshrVVAyz99miZmVafuE+xV66PJlWE/f5UZ6xT9HDEVQXB/GqBepWJhRN0CbxF7ocTqUp\n\tRJp9wlOjgiBXP/3WD03M1Os/NDGbuWrG++C2ymzFUPpRw26x0+n7ow2AeVs55AenYuxm\n\tomhQ==","X-Gm-Message-State":"AOJu0YzX3o3TjJDmA5jZuqSfsF4Ym6ANNNkhTv+yNquESeoLR/xgFvuw\n\tjjI8lE8xx09ajDTrm3x1sJ5W/iprOAOYuQ==","X-Google-Smtp-Source":"AGHT+IHjrbpcs3YSyLE8L1EbRDdGRPuif37IATGwiWpMmjU596ZQCgTiT7+Uf+SEaF7M8L/RYAFczg==","X-Received":"by 2002:a9d:7585:0:b0:6b5:f457:adaa with SMTP id\n\ts5-20020a9d7585000000b006b5f457adaamr8688818otk.29.1692012535912; \n\tMon, 14 Aug 2023 04:28:55 -0700 (PDT)","To":"libcamera-devel@lists.libcamera.org, kieran.bingham@ideasonboard.com,\n\tvedantparanjape160201@gmail.com, gabbymg94@gmail.com","Date":"Mon, 14 Aug 2023 04:28:48 -0700","Message-Id":"<20230814112849.176943-5-gabbymg94@gmail.com>","X-Mailer":"git-send-email 2.34.1","In-Reply-To":"<20230814112849.176943-1-gabbymg94@gmail.com>","References":"<20230814112849.176943-1-gabbymg94@gmail.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [RFC PATCH 4/5] libcamera: pipeline: uvcvideo:\n\tAllocate 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":"Gabby George via libcamera-devel <libcamera-devel@lists.libcamera.org>","Reply-To":"Gabby George <gabbymg94@gmail.com>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Perform the allocation and mapping of metadata buffers into\nlibcamera's virtual address space.  UVC metadata buffers cannot be\nexported as DMA buffer file descriptors, so use the MappedFrameBuffer\nclass to map them into memory directly.  This will give the UVC\npipeline access to buffer data to extract timestamp information.\n\nMetadata buffers are internal to the UVC pipeline, so buffer memory\nshould not be exposed to the user.\n\nSigned-off-by: Gabby George <gabbymg94@gmail.com>\n---\n src/libcamera/pipeline/uvcvideo/uvcvideo.cpp | 97 +++++++++++++++++++-\n 1 file changed, 95 insertions(+), 2 deletions(-)","diff":"diff --git a/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp b/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp\nindex 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 \tstd::unique_ptr<V4L2VideoDevice> video_;\n \tstd::unique_ptr<V4L2VideoDevice> metadata_;\n \tStream stream_;\n+\tstd::vector<std::unique_ptr<FrameBuffer>> metadataBuffers_;\n+\tstd::map<unsigned int, MappedFrameBuffer> mappedMetadataBuffers_;\n+\tbool useMetadataStream_;\n+\n \tstd::map<PixelFormat, std::vector<SizeRange>> formats_;\n \n private:\n \tint initMetadata(MediaDevice *media);\n \n+\tconst unsigned int minLengthHeaderBuf_ = 10;\n+\n \tbool generateId();\n \n \tstd::string id_;\n@@ -96,6 +103,10 @@ private:\n \t\t\t   const ControlValue &value);\n \tint processControls(UVCCameraData *data, Request *request);\n \n+\tint createMetadataBuffers(Camera *camera, unsigned int count);\n+\tint cleanupMetadataBuffers(Camera *camera);\n+\tint cleanup(Camera *camera);\n+\n \tUVCCameraData *cameraData(Camera *camera)\n \t{\n \t\treturn static_cast<UVCCameraData *>(camera->_d());\n@@ -225,10 +236,66 @@ int PipelineHandlerUVC::configure(Camera *camera, CameraConfiguration *config)\n \t\treturn -EINVAL;\n \n \tcfg.setStream(&data->stream_);\n+\treturn 0;\n+}\n \n+int PipelineHandlerUVC::cleanupMetadataBuffers(Camera *camera)\n+{\n+\tint ret = 0;\n+\tUVCCameraData *data = cameraData(camera);\n+\n+\tret = data->metadata_->releaseBuffers();\n+\tdata->metadataBuffers_.clear(); //call the destructor for the frame buffers\n+\tdata->mappedMetadataBuffers_.clear();\n+\tdata->useMetadataStream_ = false;\n+\n+\treturn ret;\n+}\n+\n+int PipelineHandlerUVC::cleanup(Camera *camera)\n+{\n+\tUVCCameraData *data = cameraData(camera);\n+\tcleanupMetadataBuffers(camera);\n+\tdata->video_->releaseBuffers();\n \treturn 0;\n }\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+ */\n+int PipelineHandlerUVC::createMetadataBuffers(Camera *camera, unsigned int count)\n+{\n+\tUVCCameraData *data = cameraData(camera);\n+\tint ret = data->metadata_->allocateBuffers(count, &data->metadataBuffers_);\n+\tif (ret < 0)\n+\t\treturn -EINVAL;\n+\n+\tfor (unsigned int i = 0; i < count; i++) {\n+\t\tMappedFrameBuffer mappedBuffer(data->metadataBuffers_[i].get(),\n+\t\t\t\t\t       MappedFrameBuffer::MapFlag::Read, true);\n+\t\tif (!mappedBuffer.isValid()) {\n+\t\t\tLOG(UVC, Error)\n+\t\t\t\t<< \"Failed to mmap metadata buffer: \"\n+\t\t\t\t<< strerror(mappedBuffer.error());\n+\t\t\treturn mappedBuffer.error();\n+\t\t}\n+\n+\t\tdata->mappedMetadataBuffers_.emplace(i, std::move(mappedBuffer));\n+\t\tdata->metadataBuffers_[i]->setCookie(i);\n+\t}\n+\treturn ret;\n+}\n+\n int PipelineHandlerUVC::exportFrameBuffers(Camera *camera, Stream *stream,\n \t\t\t\t\t   std::vector<std::unique_ptr<FrameBuffer>> *buffers)\n {\n@@ -247,20 +314,46 @@ int PipelineHandlerUVC::start(Camera *camera, [[maybe_unused]] const ControlList\n \tif (ret < 0)\n \t\treturn ret;\n \n+\tif (data->useMetadataStream_) {\n+\t\tif (createMetadataBuffers(camera, count) < 0) {\n+\t\t\tLOG(UVC, Error) << \"Unable to allocate buffers for UVC metadata stream.\";\n+\t\t\tdata->useMetadataStream_ = false;\n+\t\t}\n+\t}\n+\n \tret = data->video_->streamOn();\n \tif (ret < 0) {\n-\t\tdata->video_->releaseBuffers();\n+\t\tcleanup(camera);\n \t\treturn ret;\n \t}\n \n+\tif (data->useMetadataStream_) {\n+\t\tret = data->metadata_->streamOn();\n+\t\tif (ret) {\n+\t\t\tLOG(UVC, Error) << \"Failed to start metadata stream\";\n+\t\t\treturn ret;\n+\t\t}\n+\n+\t\tfor (std::unique_ptr<FrameBuffer> &buf : data->metadataBuffers_) {\n+\t\t\tret = data->metadata_->queueBuffer(buf.get());\n+\t\t\tif (ret < 0) {\n+\t\t\t\tcleanupMetadataBuffers(camera);\n+\t\t\t\treturn ret;\n+\t\t\t}\n+\t\t}\n+\t}\n \treturn 0;\n }\n \n void PipelineHandlerUVC::stopDevice(Camera *camera)\n {\n \tUVCCameraData *data = cameraData(camera);\n+\n \tdata->video_->streamOff();\n-\tdata->video_->releaseBuffers();\n+\n+\tdata->metadata_->streamOff();\n+\n+\tcleanup(camera);\n }\n \n int PipelineHandlerUVC::processControl(ControlList *controls, unsigned int id,\n","prefixes":["libcamera-devel","RFC","4/5"]}