From patchwork Mon Aug 5 13:48:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 20784 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 16E17C324E for ; Mon, 5 Aug 2024 13:51:26 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 8BD0F63387; Mon, 5 Aug 2024 15:51:25 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="AOE4YFC+"; dkim-atps=neutral Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id BACD96338B for ; Mon, 5 Aug 2024 15:51:10 +0200 (CEST) Received: by mail-wm1-x32d.google.com with SMTP id 5b1f17b1804b1-4266f3e0df8so68982295e9.2 for ; Mon, 05 Aug 2024 06:51:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1722865870; x=1723470670; darn=lists.libcamera.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=qlFjvnTzNyCv9wRreQFusaLyvxzJN6jzcsPPAHNtxA8=; b=AOE4YFC+iVBV/Ok9lbNp6QRN1WHG/BvgFI1W2PwF0KCn1oHyQgZoZEoISsYUIO6HYx gw2mCGFxFVDD7FkMYRtlbbbL5u2D3VnlqwYnXjJ/vZYaCloE4iJNduu18xyijgLUyxTD fCYy5rYvv7VLF5gewwgq1AEfo1bHxKY9n3ALk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722865870; x=1723470670; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qlFjvnTzNyCv9wRreQFusaLyvxzJN6jzcsPPAHNtxA8=; b=JGwRXnT59s0JdWS0vCC7AivaZJ3ndSN3guZFlrUvsKS9EmQfgSk78yEDVGiUIkGIe4 HhrnR38SM24mvRPrBDoRg3Dq4AigyAhHvUrzWzEAnVVqMikvzp4hr603IVNZBUCuSEFP Dk7gVmOVqL08Lz+Yf1m37sVpyo2zPGYJuOJGzhTj5VQC56JRrvmTJHb7cY/uXRFmoTdy uQhcCZbzvLYR8YNvSfq36meQ88J2VLUCouc1IhrCYndsM40n1Ql58wUbmy08dB2rOSdQ fDOtEF9QzCR7XxMY2jjQ24QM2bzZsMG7h6fYlcM0Hwj2/fNQnuS4sMhr17KxuQqyxcBu x8yg== X-Gm-Message-State: AOJu0YwFHbwtGKwQDT3zDk2/JyfR/qyhdf/7Zzw87crq2MOClWEP2tVG lUwf8X378XqjqCy5CJFtVJz7NnKRclMqJFddu6Te6kO+Fyaie4pv+aRHGdkvTARVPXyKt/CIqdh OgamPGmQ= X-Google-Smtp-Source: AGHT+IGCsw5kV30Q1kBTcrysetRki6END2k/hq7wlVm7/4/EqalelzS2D/p1adRVQ3w1OSvkjPn+PQ== X-Received: by 2002:a05:600c:3b19:b0:426:61e8:fb35 with SMTP id 5b1f17b1804b1-428e6af2e9amr78111425e9.4.1722865869778; Mon, 05 Aug 2024 06:51:09 -0700 (PDT) Received: from chenghaoyang-germany.c.googlers.com.com (49.222.77.34.bc.googleusercontent.com. [34.77.222.49]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-428e8d6555fsm137146675e9.26.2024.08.05.06.51.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Aug 2024 06:51:09 -0700 (PDT) From: Harvey Yang X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Konami Shu , Harvey Yang , Yunke Cao , Tomasz Figa Subject: [PATCH v8 7/8] libcamera: pipeline: Load images Date: Mon, 5 Aug 2024 13:48:38 +0000 Message-ID: <20240805135104.139932-8-chenghaoyang@google.com> X-Mailer: git-send-email 2.46.0.rc2.264.g509ed76dc8-goog In-Reply-To: <20240805135104.139932-1-chenghaoyang@google.com> References: <20240805135104.139932-1-chenghaoyang@google.com> MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" From: Konami Shu - Refactor ImageFrameGenerator and TestPatternGenerator - Extend the config file to have section to choose to use images or test pattern - Extend Parser to parse the configuration - Add ImageFrameGenerator which uses images to provide frames Patchset1->2 - Extend the parser to accept files with ".jpeg" - Heap allocate some buffers in ImageFrameGenerator::generateFrame so the buffers won't cause stack over flow Patchset5->6 - Move static factory function from the interface class to the derived classes Patchset6->8 - create FrameGenerator before starting the camera Signed-off-by: Konami Shu Co-developed-by: Harvey Yang Co-developed-by: Yunke Cao Co-developed-by: Tomasz Figa --- src/libcamera/pipeline/virtual/README.md | 18 +- .../pipeline/virtual/common_functions.cpp | 27 +++ .../pipeline/virtual/common_functions.h | 18 ++ .../pipeline/virtual/frame_generator.h | 2 +- .../virtual/image_frame_generator.cpp | 154 ++++++++++++++++++ .../pipeline/virtual/image_frame_generator.h | 65 ++++++++ src/libcamera/pipeline/virtual/meson.build | 6 +- src/libcamera/pipeline/virtual/parser.cpp | 77 +++++++-- src/libcamera/pipeline/virtual/parser.h | 5 +- .../virtual/test_pattern_generator.cpp | 4 +- .../pipeline/virtual/test_pattern_generator.h | 2 +- src/libcamera/pipeline/virtual/virtual.cpp | 37 +++-- src/libcamera/pipeline/virtual/virtual.h | 20 ++- 13 files changed, 390 insertions(+), 45 deletions(-) create mode 100644 src/libcamera/pipeline/virtual/common_functions.cpp create mode 100644 src/libcamera/pipeline/virtual/common_functions.h create mode 100644 src/libcamera/pipeline/virtual/image_frame_generator.cpp create mode 100644 src/libcamera/pipeline/virtual/image_frame_generator.h diff --git a/src/libcamera/pipeline/virtual/README.md b/src/libcamera/pipeline/virtual/README.md index 27d6283d..5e21ce74 100644 --- a/src/libcamera/pipeline/virtual/README.md +++ b/src/libcamera/pipeline/virtual/README.md @@ -15,8 +15,13 @@ Each camera block is a dictionary, containing the following keys: - `supported_formats` (list of `VirtualCameraData::Resolution`, optional) : List of supported resolution and frame rates of the emulated camera - `width` (`unsigned int`, default=1920): Width of the window resolution. This needs to be even. - `height` (`unsigned int`, default=1080): Height of the window resolution. - - `frame_rates` (list of `int`, default=`[30,60]` ): Range of the frame rate. The list has to be two values of the lower bound and the upper bound of the frame rate. -- `test_pattern` (`string`, default="bars"): Which test pattern to use as frames. The options are "bars", "lines". + - `frame_rates` (list of `int`, default=`[30,60]` ): Range of the frame rate. The list has to be two values of the lower bound and the upper bound of the frame rate. This does not affect the frame rate for now. +- `frames` (dictionary): + - `path` (`string`, default="bars"): Name of a test pattern, path to an image, or path to a directory of a series of images. + - The test patterns are "bars" which means color bars, and "lines" which means diagonal lines. + - The path to an image has ".jpg" extension. + - The path to a directory ends with "/". The name of the images in the directory are "{n}.jpg" with {n} is the sequence of images starting with 0. + - `scale_mode`(`string`, default="fill"): Scale mode when the frames are images. The scale modes are "fill", "contain", and "cover". This does not matter when frames is a test pattern. This does not affect the scale mode for now. - `location` (`string`, default="front"): The location of the camera. Support "front" and "back". This is displayed in qcam camera selection window but this does not change the output. - `model` (`string`, default="Unknown"): The model name of the camera. This is displayed in qcam camera selection window but this does not change the output. @@ -35,13 +40,16 @@ A sample config file: frame_rates: - 70 - 80 - test_pattern: "bars" + frames: + path: "lines" location: "front" model: "Virtual Video Device" "Virtual1": supported_formats: - width: 800 - test_pattern: "lines" + frames: + path: "path/to/directory_of_images/" + scale_mode: "contain" location: "back" model: "Virtual Video Device1" "Virtual2": @@ -61,7 +69,7 @@ This is the procedure of the Parser class: - If the config file contains invalid configuration, this method returns nullptr. The camera will be skipped. 3. Parse each property and register the data. - `parseSupportedFormats()`: Parses `supported_formats` in the config, which contains resolutions and frame rates. - - `parseTestPattern()`: Parses `test_pattern` in the config. + - `parseFrame()`: Parses `frames` in the config. - `parseLocation()`: Parses `location` in the config. - `parseModel()`: Parses `model` in the config. 4. Back to `parseConfigFile()` and append the camera configuration. diff --git a/src/libcamera/pipeline/virtual/common_functions.cpp b/src/libcamera/pipeline/virtual/common_functions.cpp new file mode 100644 index 00000000..207827ee --- /dev/null +++ b/src/libcamera/pipeline/virtual/common_functions.cpp @@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2023, Google Inc. + * + * common_functions.cpp - Helper that do not depend on any class + */ + +#include "common_functions.h" + +namespace libcamera { + +std::string getExtension(const std::string &path) +{ + size_t i = path.find("."); + if (i != std::string::npos) { + return path.substr(i); + } + return ""; +} + +std::size_t numberOfFilesInDirectory(std::filesystem::path path) +{ + using std::filesystem::directory_iterator; + return std::distance(directory_iterator(path), directory_iterator{}); +} + +} // namespace libcamera diff --git a/src/libcamera/pipeline/virtual/common_functions.h b/src/libcamera/pipeline/virtual/common_functions.h new file mode 100644 index 00000000..4203f950 --- /dev/null +++ b/src/libcamera/pipeline/virtual/common_functions.h @@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2023, Google Inc. + * + * common_functions.h - Helper that do not depend on any class + */ + +#pragma once + +#include + +namespace libcamera { + +std::string getExtension(const std::string &path); + +std::size_t numberOfFilesInDirectory(std::filesystem::path path); + +} // namespace libcamera diff --git a/src/libcamera/pipeline/virtual/frame_generator.h b/src/libcamera/pipeline/virtual/frame_generator.h index 9699af7a..f69576b3 100644 --- a/src/libcamera/pipeline/virtual/frame_generator.h +++ b/src/libcamera/pipeline/virtual/frame_generator.h @@ -23,7 +23,7 @@ public: /** Fill the output frame buffer. * Use the frame at the frameCount of image frames */ - virtual void generateFrame(const Size &size, + virtual void generateFrame(unsigned int &frameCount, const Size &size, const FrameBuffer *buffer) = 0; protected: diff --git a/src/libcamera/pipeline/virtual/image_frame_generator.cpp b/src/libcamera/pipeline/virtual/image_frame_generator.cpp new file mode 100644 index 00000000..d374877f --- /dev/null +++ b/src/libcamera/pipeline/virtual/image_frame_generator.cpp @@ -0,0 +1,154 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2023, Google Inc. + * + * image_frame_generator.cpp - Derived class of FrameGenerator for + * generating frames from images + */ + +#include "image_frame_generator.h" + +#include +#include +#include + +#include +#include + +#include + +#include "libcamera/internal/mapped_framebuffer.h" + +#include "libyuv/convert.h" +#include "libyuv/scale.h" +namespace libcamera { + +LOG_DECLARE_CATEGORY(Virtual) + +std::unique_ptr ImageFrameGenerator::create( + ImageFrames &imageFrames) +{ + std::unique_ptr imageFrameGenerator = + std::make_unique(); + imageFrameGenerator->imageFrames_ = &imageFrames; + + /** For each file in the directory + * load the image, convert it to NV12, and store the pointer + */ + for (unsigned int i = 0; i < imageFrames.number.value_or(1); i++) { + std::string path; + if (!imageFrames.number.has_value()) { + /* If the path is to an image */ + path = imageFrames.path; + } else { + /* If the path is to a directory */ + path = constructPath(imageFrames.path, i); + } + + File file(path); + bool isOpen = file.open(File::OpenModeFlag::ReadOnly); + if (!isOpen) { + LOG(Virtual, Error) << "Failed to open image file: " << file.fileName(); + return nullptr; + } + + /* Read the image file to data */ + uint8_t buffer[file.size()]; + Span data{ buffer, (unsigned long)file.size() }; + long dataSize = file.read(data); + + /* Get the width and height of the image */ + int width, height; + if (libyuv::MJPGSize(data.data(), dataSize, &width, &height)) { + LOG(Virtual, Error) << "Failed to get the size of the image file: " + << file.fileName(); + return nullptr; + } + + /* Convert to NV12 and write the data to tmpY and tmpUV */ + int halfWidth = (width + 1) / 2; + int halfHeight = (height + 1) / 2; + std::unique_ptr dstY = + std::make_unique(width * height); + std::unique_ptr dstUV = + std::make_unique(halfWidth * halfHeight * 2); + int ret = libyuv::MJPGToNV12(data.data(), dataSize, + dstY.get(), width, dstUV.get(), + width, width, height, width, height); + if (ret != 0) { + LOG(Virtual, Error) << "MJPGToNV12() failed with " << ret; + } + + imageFrameGenerator->imageFrameDatas_.emplace_back( + ImageFrameData{ std::move(dstY), std::move(dstUV), + Size(width, height) }); + } + return imageFrameGenerator; +} + +std::string ImageFrameGenerator::constructPath(std::string &name, unsigned int &i) +{ + return name + std::to_string(i) + ".jpg"; +} + +void ImageFrameGenerator::configure(const Size &size) +{ + for (unsigned int i = 0; i < imageFrames_->number.value_or(1); i++) { + /* Scale the imageFrameDatas_ to scaledY and scaledUV */ + int halfSizeWidth = (size.width + 1) / 2; + int halfSizeHeight = (size.height + 1) / 2; + std::unique_ptr scaledY = + std::make_unique(size.width * size.height); + std::unique_ptr scaledUV = + std::make_unique(halfSizeWidth * halfSizeHeight * 2); + auto &src = imageFrameDatas_[i]; + + /* + * \todo Implement "contain" & "cover", based on + * |imageFrames_[i].scaleMode|. + */ + + /* + * \todo Some platforms might enforce stride due to GPU, like + * ChromeOS ciri (64). The weight needs to be a multiple of + * the stride to work properly for now. + */ + libyuv::NV12Scale(src.Y.get(), src.size.width, + src.UV.get(), src.size.width, + src.size.width, src.size.height, + scaledY.get(), size.width, scaledUV.get(), size.width, + size.width, size.height, libyuv::FilterMode::kFilterBilinear); + + /* Store the pointers to member variable */ + scaledFrameDatas_.emplace_back( + ImageFrameData{ std::move(scaledY), std::move(scaledUV), size }); + } +} + +void ImageFrameGenerator::generateFrame(unsigned int &frameCount, const Size &size, const FrameBuffer *buffer) +{ + /* Don't do anything when the list of buffers is empty*/ + if (scaledFrameDatas_.size() == 0) + return; + + MappedFrameBuffer mappedFrameBuffer(buffer, MappedFrameBuffer::MapFlag::Write); + + auto planes = mappedFrameBuffer.planes(); + + /* Make sure the frameCount does not over the number of images */ + frameCount %= imageFrames_->number.value_or(1); + + /* Write the scaledY and scaledUV to the mapped frame buffer */ + libyuv::NV12Copy(scaledFrameDatas_[frameCount].Y.get(), size.width, + scaledFrameDatas_[frameCount].UV.get(), size.width, planes[0].begin(), + size.width, planes[1].begin(), size.width, + size.width, size.height); + + /* proceed an image every 4 frames */ + /* \todo read the parameter_ from the configuration file? */ + parameter_++; + if (parameter_ % 4 == 0) + frameCount++; +} + +} // namespace libcamera diff --git a/src/libcamera/pipeline/virtual/image_frame_generator.h b/src/libcamera/pipeline/virtual/image_frame_generator.h new file mode 100644 index 00000000..74468e07 --- /dev/null +++ b/src/libcamera/pipeline/virtual/image_frame_generator.h @@ -0,0 +1,65 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2023, Google Inc. + * + * image_frame_generator.h - Derived class of FrameGenerator for + * generating frames from images + */ + +#pragma once + +#include +#include +#include +#include + +#include "frame_generator.h" + +namespace libcamera { + +enum class ScaleMode : char { + Fill = 0, + Contain = 1, + Cover = 2, +}; + +/* Frame configuration provided by the config file */ +struct ImageFrames { + std::string path; + ScaleMode scaleMode; + std::optional number; +}; + +class ImageFrameGenerator : public FrameGenerator +{ +public: + /** Factory function to create an ImageFrameGenerator object. + * Read the images and convert them to buffers in NV12 format. + * Store the pointers to the buffers to a list (imageFrameDatas) + */ + static std::unique_ptr create(ImageFrames &imageFrames); + +private: + struct ImageFrameData { + std::unique_ptr Y; + std::unique_ptr UV; + Size size; + }; + + /* Scale the buffers for image frames. */ + void configure(const Size &size) override; + void generateFrame(unsigned int &frameCount, const Size &size, const FrameBuffer *buffer) override; + + static std::string constructPath(std::string &name, unsigned int &i); + + /* List of pointers to the not scaled image buffers */ + std::vector imageFrameDatas_; + /* List of pointers to the scaled image buffers */ + std::vector scaledFrameDatas_; + /* Pointer to the imageFrames_ in VirtualCameraData */ + ImageFrames *imageFrames_; + /* Speed parameter. Change to the next image every parameter_ frames. */ + int parameter_; +}; + +} /* namespace libcamera */ diff --git a/src/libcamera/pipeline/virtual/meson.build b/src/libcamera/pipeline/virtual/meson.build index 2e82e64c..d56aedec 100644 --- a/src/libcamera/pipeline/virtual/meson.build +++ b/src/libcamera/pipeline/virtual/meson.build @@ -2,11 +2,14 @@ libcamera_sources += files([ 'virtual.cpp', - 'test_pattern_generator.cpp', 'parser.cpp', + 'test_pattern_generator.cpp', + 'image_frame_generator.cpp', + 'common_functions.cpp', ]) libyuv_dep = dependency('libyuv', required : false) +libjpeg = dependency('libjpeg', required : false) # Fallback to a subproject if libyuv isn't found, as it's typically not # provided by distributions. @@ -26,3 +29,4 @@ if not libyuv_dep.found() endif libcamera_deps += [libyuv_dep] +libcamera_deps += [libjpeg] diff --git a/src/libcamera/pipeline/virtual/parser.cpp b/src/libcamera/pipeline/virtual/parser.cpp index 032c0cd9..46d1ab18 100644 --- a/src/libcamera/pipeline/virtual/parser.cpp +++ b/src/libcamera/pipeline/virtual/parser.cpp @@ -18,6 +18,7 @@ #include "libcamera/internal/pipeline_handler.h" #include "libcamera/internal/yaml_parser.h" +#include "common_functions.h" #include "virtual.h" namespace libcamera { @@ -52,12 +53,12 @@ std::vector> Parser::parseConfigFile( continue; } - data->id_ = cameraId; + data->config_.id = cameraId; ControlInfoMap::Map controls; /* todo: Check which resolution's frame rate to be reported */ controls[&controls::FrameDurationLimits] = - ControlInfo(int64_t(1000 / data->supportedResolutions_[0].frameRates[1]), - int64_t(1000 / data->supportedResolutions_[0].frameRates[0])); + ControlInfo(int64_t(1000 / data->config_.resolutions[0].frameRates[1]), + int64_t(1000 / data->config_.resolutions[0].frameRates[0])); data->controlInfo_ = ControlInfoMap(std::move(controls), controls::controls); configurations.push_back(std::move(data)); } @@ -72,7 +73,7 @@ std::unique_ptr Parser::parseCameraConfigData( if (parseSupportedFormats(cameraConfigData, data.get())) return nullptr; - if (parseTestPattern(cameraConfigData, data.get())) + if (parseFrame(cameraConfigData, data.get())) return nullptr; if (parseLocation(cameraConfigData, data.get())) @@ -122,14 +123,14 @@ int Parser::parseSupportedFormats( frameRates.push_back(60); } - data->supportedResolutions_.emplace_back( + data->config_.resolutions.emplace_back( VirtualCameraData::Resolution{ Size{ width, height }, frameRates }); activeResolution = std::max(activeResolution, Size{ width, height }); } } else { - data->supportedResolutions_.emplace_back( + data->config_.resolutions.emplace_back( VirtualCameraData::Resolution{ Size{ 1920, 1080 }, { 30, 60 } }); activeResolution = Size(1920, 1080); @@ -141,21 +142,65 @@ int Parser::parseSupportedFormats( return 0; } -int Parser::parseTestPattern( +int Parser::parseFrame( const YamlObject &cameraConfigData, VirtualCameraData *data) { - std::string testPattern = cameraConfigData["test_pattern"].get().value(); + const YamlObject &frames = cameraConfigData["frames"]; + /* When there is no frames provided in the config file, use color bar test pattern */ + if (frames.size() == 0) { + data->config_.frame = TestPattern::ColorBars; + return 0; + } + + if (!frames.isDictionary()) { + LOG(Virtual, Error) << "'frames' is not a dictionary."; + return -EINVAL; + } + + std::string path = frames["path"].get().value(); + + if (auto ext = getExtension(path); ext == ".jpg" || ext == ".jpeg") { + ScaleMode scaleMode; + if (parseScaleMode(frames, &scaleMode)) + return -EINVAL; + data->config_.frame = ImageFrames{ path, scaleMode, std::nullopt }; + } else if (path.back() == '/') { + ScaleMode scaleMode; + if (parseScaleMode(frames, &scaleMode)) + return -EINVAL; + data->config_.frame = ImageFrames{ path, scaleMode, + numberOfFilesInDirectory(path) }; + } else if (path == "bars" || path == "") { + /* Default value is "bars" */ + data->config_.frame = TestPattern::ColorBars; + } else if (path == "lines") { + data->config_.frame = TestPattern::DiagonalLines; + } else { + LOG(Virtual, Error) << "Frame: " << path + << " is not supported"; + return -EINVAL; + } + return 0; +} - /* Default value is "bars" */ - if (testPattern == "bars" || testPattern == "") { - data->testPattern_ = TestPattern::ColorBars; - } else if (testPattern == "lines") { - data->testPattern_ = TestPattern::DiagonalLines; +int Parser::parseScaleMode( + const YamlObject &framesConfigData, ScaleMode *scaleMode) +{ + std::string mode = framesConfigData["scale_mode"].get().value(); + + /* Default value is fill */ + if (mode == "fill" || mode == "") { + *scaleMode = ScaleMode::Fill; + } else if (mode == "contain") { + *scaleMode = ScaleMode::Contain; + } else if (mode == "cover") { + *scaleMode = ScaleMode::Cover; } else { - LOG(Virtual, Error) << "Test pattern: " << testPattern - << "is not supported"; + LOG(Virtual, Error) << "scaleMode: " << mode + << " is not supported"; return -EINVAL; } + return 0; } @@ -195,4 +240,4 @@ int Parser::parseModel( return 0; } -} /* namespace libcamera */ +} // namespace libcamera diff --git a/src/libcamera/pipeline/virtual/parser.h b/src/libcamera/pipeline/virtual/parser.h index a377d8aa..38ea460d 100644 --- a/src/libcamera/pipeline/virtual/parser.h +++ b/src/libcamera/pipeline/virtual/parser.h @@ -34,12 +34,15 @@ private: int parseSupportedFormats( const YamlObject &cameraConfigData, VirtualCameraData *data); - int parseTestPattern( + int parseFrame( const YamlObject &cameraConfigData, VirtualCameraData *data); int parseLocation( const YamlObject &cameraConfigData, VirtualCameraData *data); int parseModel( const YamlObject &cameraConfigData, VirtualCameraData *data); + + int parseScaleMode( + const YamlObject &framesConfigData, ScaleMode *scaleMode); }; } // namespace libcamera diff --git a/src/libcamera/pipeline/virtual/test_pattern_generator.cpp b/src/libcamera/pipeline/virtual/test_pattern_generator.cpp index 6df9b31e..844dffd4 100644 --- a/src/libcamera/pipeline/virtual/test_pattern_generator.cpp +++ b/src/libcamera/pipeline/virtual/test_pattern_generator.cpp @@ -20,7 +20,7 @@ LOG_DECLARE_CATEGORY(Virtual) static const unsigned int kARGBSize = 4; void TestPatternGenerator::generateFrame( - const Size &size, + [[maybe_unused]] unsigned int &frameCount, const Size &size, const FrameBuffer *buffer) { MappedFrameBuffer mappedFrameBuffer(buffer, @@ -29,7 +29,7 @@ void TestPatternGenerator::generateFrame( auto planes = mappedFrameBuffer.planes(); /* TODO: select whether to do shifting or not */ - shiftLeft(size); + // shiftLeft(size); /* Convert the template_ to the frame buffer */ int ret = libyuv::ARGBToNV12( diff --git a/src/libcamera/pipeline/virtual/test_pattern_generator.h b/src/libcamera/pipeline/virtual/test_pattern_generator.h index ed8d4e43..cebdd014 100644 --- a/src/libcamera/pipeline/virtual/test_pattern_generator.h +++ b/src/libcamera/pipeline/virtual/test_pattern_generator.h @@ -25,7 +25,7 @@ enum class TestPattern : char { class TestPatternGenerator : public FrameGenerator { private: - void generateFrame(const Size &size, + void generateFrame(unsigned int &frameCount, const Size &size, const FrameBuffer *buffer) override; protected: diff --git a/src/libcamera/pipeline/virtual/virtual.cpp b/src/libcamera/pipeline/virtual/virtual.cpp index 0fe471f0..2412af70 100644 --- a/src/libcamera/pipeline/virtual/virtual.cpp +++ b/src/libcamera/pipeline/virtual/virtual.cpp @@ -20,9 +20,13 @@ #include "libcamera/internal/pipeline_handler.h" #include "libcamera/internal/yaml_parser.h" -#include "frame_generator.h" #include "parser.h" +#define ifTestPattern(v) std::holds_alternative(v) +#define getTestPattern(v) std::get(v) +#define ifImageFrames(v) std::holds_alternative(v) +#define getImageFrames(v) std::get(v) + namespace libcamera { LOG_DEFINE_CATEGORY(Virtual) @@ -63,12 +67,12 @@ CameraConfiguration::Status VirtualCameraConfiguration::validate() } Size maxSize; - for (const auto &resolution : data_->supportedResolutions_) + for (const auto &resolution : data_->config_.resolutions) maxSize = std::max(maxSize, resolution.size); for (StreamConfiguration &cfg : config_) { bool found = false; - for (const auto &resolution : data_->supportedResolutions_) { + for (const auto &resolution : data_->config_.resolutions) { if (resolution.size.width == cfg.size.width && resolution.size.height == cfg.size.height) { found = true; @@ -110,7 +114,7 @@ PipelineHandlerVirtual::generateConfiguration(Camera *camera, return config; Size minSize, sensorResolution; - for (const auto &resolution : data->supportedResolutions_) { + for (const auto &resolution : data->config_.resolutions) { if (minSize.isNull() || minSize > resolution.size) minSize = resolution.size; @@ -199,7 +203,7 @@ int PipelineHandlerVirtual::exportFrameBuffers( int PipelineHandlerVirtual::start(Camera *camera, [[maybe_unused]] const ControlList *controls) { - /* \todo Start reading the virtual video if any. */ + /* Start reading the images/generating test patterns */ VirtualCameraData *data = cameraData(camera); data->frameGenerator_->configure(data->stream_.configuration().size); @@ -219,8 +223,8 @@ int PipelineHandlerVirtual::queueRequestDevice([[maybe_unused]] Camera *camera, /* \todo Read from the virtual video if any. */ for (auto const &[stream, buffer] : request->buffers()) { - /* map buffer and fill test patterns */ - data->frameGenerator_->generateFrame(stream->configuration().size, buffer); + /* Map buffer. Fill test patterns or images */ + data->frameGenerator_->generateFrame(data->frameCount_, stream->configuration().size, buffer); completeBuffer(request, buffer); } @@ -250,9 +254,10 @@ bool PipelineHandlerVirtual::match([[maybe_unused]] DeviceEnumerator *enumerator /* Configure and register cameras with configData */ for (auto &data : configData) { std::set streams{ &data->stream_ }; - std::string id = data->id_; + std::string id = data->config_.id; std::shared_ptr camera = Camera::create(std::move(data), id, streams); + /* Initialize FrameGenerator*/ initFrameGenerator(camera.get()); registerCamera(std::move(camera)); @@ -264,13 +269,19 @@ bool PipelineHandlerVirtual::match([[maybe_unused]] DeviceEnumerator *enumerator void PipelineHandlerVirtual::initFrameGenerator(Camera *camera) { auto data = cameraData(camera); - if (data->testPattern_ == TestPattern::DiagonalLines) { - data->frameGenerator_ = DiagonalLinesGenerator::create(); - } else { - data->frameGenerator_ = ColorBarsGenerator::create(); + auto &frame = data->config_.frame; + if (ifTestPattern(frame)) { + TestPattern &testPattern = getTestPattern(frame); + if (testPattern == TestPattern::DiagonalLines) { + data->frameGenerator_ = DiagonalLinesGenerator::create(); + } else { + data->frameGenerator_ = ColorBarsGenerator::create(); + } + } else if (ifImageFrames(frame)) { + data->frameGenerator_ = ImageFrameGenerator::create(getImageFrames(frame)); } } REGISTER_PIPELINE_HANDLER(PipelineHandlerVirtual, "virtual") -} /* namespace libcamera */ +} // namespace libcamera diff --git a/src/libcamera/pipeline/virtual/virtual.h b/src/libcamera/pipeline/virtual/virtual.h index c1ac4eb9..f41a4a90 100644 --- a/src/libcamera/pipeline/virtual/virtual.h +++ b/src/libcamera/pipeline/virtual/virtual.h @@ -7,16 +7,22 @@ #pragma once +#include + #include #include "libcamera/internal/camera.h" #include "libcamera/internal/dma_buf_allocator.h" #include "libcamera/internal/pipeline_handler.h" +#include "frame_generator.h" +#include "image_frame_generator.h" #include "test_pattern_generator.h" namespace libcamera { +using VirtualFrame = std::variant; + class VirtualCameraData : public Camera::Private { public: @@ -24,6 +30,13 @@ public: Size size; std::vector frameRates; }; + /* The config file is parsed to the Configuration struct */ + struct Configuration { + std::string id; + std::vector resolutions; + VirtualFrame frame; + }; + VirtualCameraData(PipelineHandler *pipe) : Camera::Private(pipe) { @@ -31,12 +44,9 @@ public: ~VirtualCameraData() = default; - std::string id_; - std::vector supportedResolutions_; - TestPattern testPattern_; - + unsigned int frameCount_ = 0; + Configuration config_; Stream stream_; - std::unique_ptr frameGenerator_; };