{"id":21211,"url":"https://patchwork.libcamera.org/api/1.1/patches/21211/?format=json","web_url":"https://patchwork.libcamera.org/patch/21211/","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":"<20240910044834.2477701-6-chenghaoyang@google.com>","date":"2024-09-10T04:40:18","name":"[v12,5/7] libcamera: virtual: Add ImageFrameGenerator","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"8a6c4fa6130841ed3ee4309d9baf2ff8f8619efd","submitter":{"id":117,"url":"https://patchwork.libcamera.org/api/1.1/people/117/?format=json","name":"Cheng-Hao Yang","email":"chenghaoyang@chromium.org"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/21211/mbox/","series":[{"id":4572,"url":"https://patchwork.libcamera.org/api/1.1/series/4572/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4572","date":"2024-09-10T04:40:13","name":"Add VirtualPipelineHandler","version":12,"mbox":"https://patchwork.libcamera.org/series/4572/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/21211/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/21211/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 BBA98C324C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 10 Sep 2024 04:48:56 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 2819363507;\n\tTue, 10 Sep 2024 06:48:56 +0200 (CEST)","from mail-pf1-x42d.google.com (mail-pf1-x42d.google.com\n\t[IPv6:2607:f8b0:4864:20::42d])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id CC93B634FE\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 10 Sep 2024 06:48:49 +0200 (CEST)","by mail-pf1-x42d.google.com with SMTP id\n\td2e1a72fcca58-718e5edf270so2004949b3a.1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 09 Sep 2024 21:48:49 -0700 (PDT)","from chenghaoyang-low.c.googlers.com.com\n\t(0.223.81.34.bc.googleusercontent.com. [34.81.223.0])\n\tby smtp.gmail.com with ESMTPSA id\n\td2e1a72fcca58-71909092522sm530734b3a.111.2024.09.09.21.48.46\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tMon, 09 Sep 2024 21:48:47 -0700 (PDT)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=chromium.org header.i=@chromium.org\n\theader.b=\"YGxvSXNA\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=chromium.org; s=google; t=1725943728; x=1726548528;\n\tdarn=lists.libcamera.org; \n\th=content-transfer-encoding:mime-version:references:in-reply-to\n\t:message-id:date:subject:cc:to:from:from:to:cc:subject:date\n\t:message-id:reply-to;\n\tbh=uXYXgYmLTI1Lhg/uIXDRU7yOl7xGH0tTcq15d4KTqKk=;\n\tb=YGxvSXNAgPPlQFBVV01pDRsgOroB40ny9S25KRGo7agwMQb+hp0Wf2HvmiktYSiGE+\n\tM6hJI6ArNZzcDiZV/T//wtMX3zVOoWSi5F7ffLCTfDo9tvCYsnN5pf7djvBXk8yZXrWE\n\tAfLMmYpKCRkEK4Xt65r81w+8fc1jE/4a2vxRA=","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20230601; t=1725943728; x=1726548528;\n\th=content-transfer-encoding:mime-version:references:in-reply-to\n\t:message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc\n\t:subject:date:message-id:reply-to;\n\tbh=uXYXgYmLTI1Lhg/uIXDRU7yOl7xGH0tTcq15d4KTqKk=;\n\tb=U1w9F9RFt1eL5Em3DllXG+FHVbxl0YTDSC+7YtuzFJ3pI/xKFTG6tke1VnxKFeMcWP\n\tQbQKMJurROW/Gj9pdqvhLUMaNX8NwTxSXCscuR8TY23RTogjK5rU++yZQxty51gNkWBO\n\t5Cn+tuBu7tOf431odlFA7JZV3W+HyIgrFPhZDflUgdzyC2Ahtw4EgmG0uv54sx2gR2ri\n\tJNGDgavwbd6CVW3RUfsVM26S7khu8CFFHkpydstWQMA23T6kSlgHSWc0Jo5YPqlAT/kE\n\tGjsvBC9KDMVK8P4D4kNJyM3hsVCbSxFiZXuIsdJhnFSPr72pb3QDd5RWtA/ymUbFmH5s\n\t20iQ==","X-Gm-Message-State":"AOJu0YyRm5WfZtDDzNcl3OWwOzenxZGVrO18AKnG+3IhxqYkIZLX//A1\n\tB4hIy5Sdv/UjKVp9g0ZMKl4ZL85R7v+zwnFA2xs0ZRugQpF0Vqx8XPvBjJ6ihjEQt1yzabrpbFg\n\t=","X-Google-Smtp-Source":"AGHT+IE6RZEq4GhOPdLJvatlpfENIpdh9V+pQwyrOG5ah70wP/l6zf/Tr5g0epxrq477H7YJH/gDnA==","X-Received":"by 2002:a05:6a00:3e1a:b0:717:e01d:312f with SMTP id\n\td2e1a72fcca58-718d5f67bf4mr11914199b3a.27.1725943727921; \n\tMon, 09 Sep 2024 21:48:47 -0700 (PDT)","From":"Harvey Yang <chenghaoyang@chromium.org>","X-Google-Original-From":"Harvey Yang <chenghaoyang@google.com>","To":"libcamera-devel@lists.libcamera.org","Cc":"Harvey Yang <chenghaoyang@google.com>, Konami Shu <konamiz@google.com>, \n\tHarvey Yang <chenghaoyang@chromium.org>, Yunke Cao <yunkec@chromium.org>,\n\tTomasz Figa <tfiga@chromium.org>","Subject":"[PATCH v12 5/7] libcamera: virtual: Add ImageFrameGenerator","Date":"Tue, 10 Sep 2024 04:40:18 +0000","Message-ID":"<20240910044834.2477701-6-chenghaoyang@google.com>","X-Mailer":"git-send-email 2.46.0.598.g6f2099f65c-goog","In-Reply-To":"<20240910044834.2477701-1-chenghaoyang@google.com>","References":"<20240910044834.2477701-1-chenghaoyang@google.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","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>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Besides TestPatternGenerator, this patch adds ImageFrameGenerator that\nloads real images (jpg / jpeg for now) as the source and generates\nscaled frames.\n\nSigned-off-by: Konami Shu <konamiz@google.com>\nCo-developed-by: Harvey Yang <chenghaoyang@chromium.org>\nCo-developed-by: Yunke Cao <yunkec@chromium.org>\nCo-developed-by: Tomasz Figa <tfiga@chromium.org>\n---\n .../virtual/image_frame_generator.cpp         | 175 ++++++++++++++++++\n .../pipeline/virtual/image_frame_generator.h  |  51 +++++\n src/libcamera/pipeline/virtual/meson.build    |   4 +\n 3 files changed, 230 insertions(+)\n create mode 100644 src/libcamera/pipeline/virtual/image_frame_generator.cpp\n create mode 100644 src/libcamera/pipeline/virtual/image_frame_generator.h","diff":"diff --git a/src/libcamera/pipeline/virtual/image_frame_generator.cpp b/src/libcamera/pipeline/virtual/image_frame_generator.cpp\nnew file mode 100644\nindex 00000000..80df9d6b\n--- /dev/null\n+++ b/src/libcamera/pipeline/virtual/image_frame_generator.cpp\n@@ -0,0 +1,175 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2024, Google Inc.\n+ *\n+ * image_frame_generator.cpp - Derived class of FrameGenerator for\n+ * generating frames from images\n+ */\n+\n+#include \"image_frame_generator.h\"\n+\n+#include <filesystem>\n+#include <memory>\n+#include <string>\n+\n+#include <libcamera/base/file.h>\n+#include <libcamera/base/log.h>\n+\n+#include <libcamera/framebuffer.h>\n+\n+#include \"libcamera/internal/mapped_framebuffer.h\"\n+\n+#include \"libyuv/convert.h\"\n+#include \"libyuv/scale.h\"\n+\n+namespace libcamera {\n+\n+LOG_DECLARE_CATEGORY(Virtual)\n+\n+/*\n+ * Factory function to create an ImageFrameGenerator object.\n+ * Read the images and convert them to buffers in NV12 format.\n+ * Store the pointers to the buffers to a list (imageFrameDatas)\n+ */\n+std::unique_ptr<ImageFrameGenerator>\n+ImageFrameGenerator::create(ImageFrames &imageFrames)\n+{\n+\tstd::unique_ptr<ImageFrameGenerator> imageFrameGenerator =\n+\t\tstd::make_unique<ImageFrameGenerator>();\n+\timageFrameGenerator->imageFrames_ = &imageFrames;\n+\n+\t/*\n+\t * For each file in the directory, load the image,\n+\t * convert it to NV12, and store the pointer.\n+\t */\n+\tfor (unsigned int i = 0; i < imageFrames.number.value_or(1); i++) {\n+\t\tstd::filesystem::path path;\n+\t\tif (!imageFrames.number)\n+\t\t\t/* If the path is to an image */\n+\t\t\tpath = imageFrames.path;\n+\t\telse\n+\t\t\t/* If the path is to a directory */\n+\t\t\tpath = imageFrames.path / (std::to_string(i) + \".jpg\");\n+\n+\t\tFile file(path);\n+\t\tif (!file.open(File::OpenModeFlag::ReadOnly)) {\n+\t\t\tLOG(Virtual, Error) << \"Failed to open image file \" << file.fileName()\n+\t\t\t\t\t    << \": \" << strerror(file.error());\n+\t\t\treturn nullptr;\n+\t\t}\n+\n+\t\t/* Read the image file to data */\n+\t\tauto fileSize = file.size();\n+\t\tauto buffer = std::make_unique<uint8_t[]>(file.size());\n+\t\tif (file.read({ buffer.get(), static_cast<size_t>(fileSize) }) != fileSize) {\n+\t\t\tLOG(Virtual, Error) << \"Failed to read file \" << file.fileName()\n+\t\t\t\t\t    << \": \" << strerror(file.error());\n+\t\t\treturn nullptr;\n+\t\t}\n+\n+\t\t/* Get the width and height of the image */\n+\t\tint width, height;\n+\t\tif (libyuv::MJPGSize(buffer.get(), fileSize, &width, &height)) {\n+\t\t\tLOG(Virtual, Error) << \"Failed to get the size of the image file: \"\n+\t\t\t\t\t    << file.fileName();\n+\t\t\treturn nullptr;\n+\t\t}\n+\n+\t\tstd::unique_ptr<uint8_t[]> dstY =\n+\t\t\tstd::make_unique<uint8_t[]>(width * height);\n+\t\tstd::unique_ptr<uint8_t[]> dstUV =\n+\t\t\tstd::make_unique<uint8_t[]>(width * height / 2);\n+\t\tint ret = libyuv::MJPGToNV12(buffer.get(), fileSize,\n+\t\t\t\t\t     dstY.get(), width, dstUV.get(),\n+\t\t\t\t\t     width, width, height, width, height);\n+\t\tif (ret != 0)\n+\t\t\tLOG(Virtual, Error) << \"MJPGToNV12() failed with \" << ret;\n+\n+\t\timageFrameGenerator->imageFrameDatas_.emplace_back(\n+\t\t\tImageFrameData{ std::move(dstY), std::move(dstUV),\n+\t\t\t\t\tSize(width, height) });\n+\t}\n+\n+\treturn imageFrameGenerator;\n+}\n+\n+/* Scale the buffers for image frames. */\n+void ImageFrameGenerator::configure(const Size &size)\n+{\n+\t/* Reset the source images to prevent multiple configuration calls */\n+\tscaledFrameDatas_.clear();\n+\tframeCount_ = 0;\n+\tparameter_ = 0;\n+\n+\tfor (unsigned int i = 0; i < imageFrames_->number.value_or(1); i++) {\n+\t\t/* Scale the imageFrameDatas_ to scaledY and scaledUV */\n+\t\tunsigned int halfSizeWidth = (size.width + 1) / 2;\n+\t\tunsigned int halfSizeHeight = (size.height + 1) / 2;\n+\t\tstd::unique_ptr<uint8_t[]> scaledY =\n+\t\t\tstd::make_unique<uint8_t[]>(size.width * size.height);\n+\t\tstd::unique_ptr<uint8_t[]> scaledUV =\n+\t\t\tstd::make_unique<uint8_t[]>(halfSizeWidth * halfSizeHeight * 2);\n+\t\tauto &src = imageFrameDatas_[i];\n+\n+\t\t/*\n+\t\t * \\todo Some platforms might enforce stride due to GPU, like\n+\t\t * ChromeOS ciri (64). The weight needs to be a multiple of\n+\t\t * the stride to work properly for now.\n+\t\t */\n+\t\tlibyuv::NV12Scale(src.Y.get(), src.size.width,\n+\t\t\t\t  src.UV.get(), src.size.width,\n+\t\t\t\t  src.size.width, src.size.height,\n+\t\t\t\t  scaledY.get(), size.width, scaledUV.get(), size.width,\n+\t\t\t\t  size.width, size.height, libyuv::FilterMode::kFilterBilinear);\n+\n+\t\tscaledFrameDatas_.emplace_back(\n+\t\t\tImageFrameData{ std::move(scaledY), std::move(scaledUV), size });\n+\t}\n+}\n+\n+void ImageFrameGenerator::generateFrame(const Size &size, const FrameBuffer *buffer)\n+{\n+\t/* Don't do anything when the list of buffers is empty*/\n+\tASSERT(!scaledFrameDatas_.empty());\n+\n+\tMappedFrameBuffer mappedFrameBuffer(buffer, MappedFrameBuffer::MapFlag::Write);\n+\n+\tauto planes = mappedFrameBuffer.planes();\n+\n+\t/* Make sure the frameCount does not over the number of images */\n+\tframeCount_ %= imageFrames_->number.value_or(1);\n+\n+\t/* Write the scaledY and scaledUV to the mapped frame buffer */\n+\tlibyuv::NV12Copy(scaledFrameDatas_[frameCount_].Y.get(), size.width,\n+\t\t\t scaledFrameDatas_[frameCount_].UV.get(), size.width, planes[0].begin(),\n+\t\t\t size.width, planes[1].begin(), size.width,\n+\t\t\t size.width, size.height);\n+\n+\t/* Proceed an image every 4 frames */\n+\t/* \\todo Consider setting the proceed interval in the config file  */\n+\tparameter_++;\n+\tif (parameter_ % frameInterval == 0)\n+\t\tframeCount_++;\n+}\n+\n+/*\n+ * \\var ImageFrameGenerator::imageFrameDatas_\n+ * \\brief List of pointers to the not scaled image buffers\n+ */\n+\n+/*\n+ * \\var ImageFrameGenerator::scaledFrameDatas_\n+ * \\brief List of pointers to the scaled image buffers\n+ */\n+\n+/*\n+ * \\var ImageFrameGenerator::imageFrames_\n+ * \\brief Pointer to the imageFrames_ in VirtualCameraData\n+ */\n+\n+/*\n+ * \\var ImageFrameGenerator::parameter_\n+ * \\brief Speed parameter. Change to the next image every parameter_ frames\n+ */\n+\n+} /* namespace libcamera */\ndiff --git a/src/libcamera/pipeline/virtual/image_frame_generator.h b/src/libcamera/pipeline/virtual/image_frame_generator.h\nnew file mode 100644\nindex 00000000..bbbe77c8\n--- /dev/null\n+++ b/src/libcamera/pipeline/virtual/image_frame_generator.h\n@@ -0,0 +1,51 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2024, Google Inc.\n+ *\n+ * image_frame_generator.h - Derived class of FrameGenerator for\n+ * generating frames from images\n+ */\n+\n+#pragma once\n+\n+#include <filesystem>\n+#include <memory>\n+#include <optional>\n+#include <stdint.h>\n+#include <sys/types.h>\n+\n+#include \"frame_generator.h\"\n+\n+namespace libcamera {\n+\n+/* Frame configuration provided by the config file */\n+struct ImageFrames {\n+\tstd::filesystem::path path;\n+\tstd::optional<unsigned int> number;\n+};\n+\n+class ImageFrameGenerator : public FrameGenerator\n+{\n+public:\n+\tstatic std::unique_ptr<ImageFrameGenerator> create(ImageFrames &imageFrames);\n+\n+private:\n+\tstatic constexpr unsigned int frameInterval = 4;\n+\n+\tstruct ImageFrameData {\n+\t\tstd::unique_ptr<uint8_t[]> Y;\n+\t\tstd::unique_ptr<uint8_t[]> UV;\n+\t\tSize size;\n+\t};\n+\n+\tvoid configure(const Size &size) override;\n+\tvoid generateFrame(const Size &size, const FrameBuffer *buffer) override;\n+\n+\tstd::vector<ImageFrameData> imageFrameDatas_;\n+\tstd::vector<ImageFrameData> scaledFrameDatas_;\n+\tImageFrames *imageFrames_;\n+\tunsigned int frameCount_;\n+\tunsigned int parameter_;\n+};\n+\n+} /* namespace libcamera */\ndiff --git a/src/libcamera/pipeline/virtual/meson.build b/src/libcamera/pipeline/virtual/meson.build\nindex 0c537777..73d10cc3 100644\n--- a/src/libcamera/pipeline/virtual/meson.build\n+++ b/src/libcamera/pipeline/virtual/meson.build\n@@ -1,8 +1,12 @@\n # SPDX-License-Identifier: CC0-1.0\n \n libcamera_internal_sources += files([\n+    'image_frame_generator.cpp',\n     'test_pattern_generator.cpp',\n     'virtual.cpp',\n ])\n \n+libjpeg = dependency('libjpeg', required : false)\n+\n libcamera_deps += [libyuv_dep]\n+libcamera_deps += [libjpeg]\n","prefixes":["v12","5/7"]}