From patchwork Wed Mar 15 10:22:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 18406 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 892D5C329D for ; Wed, 15 Mar 2023 10:23:17 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 34B8B62717; Wed, 15 Mar 2023 11:23:17 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1678875797; bh=HUipLLAsOu56sfyzx2B4T69Uh/inoCt+MZJMElAJvFU=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=Xhvhune9v+pmKouFAK53auzYrAsCFgoPJdWgFRLYf3tfk+GpMVTDmyImEQZ9nT5Ml F5rfqcJ9tyfFifLBR4NN4L+RKRmlN6Wbzf3LhNuKsv6yRau3ljMeEbWCFEhHrUv1hh SB433WgdajxDshr4zh6zhGSwwmfrOJ7Ol6DnkVm8sqQdkPh57cSRpy6S60MoFY+9sq M8KoXmCxSJxrPpPCMll+T2XR7oJama1ZCtXE9PyYYAVk8GxVPXju85INeqD1VHT9J6 dKncUJszGbOpGPGKtCbmfJHyiZ+dMM0fVR0Sq9/qal3zoLAi0MFCIdrI66/bVK6RxB YMNXuvxSbHe1w== Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 5D6F86270A for ; Wed, 15 Mar 2023 11:23:12 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="eCCOWHrI"; dkim-atps=neutral Received: by mail-pj1-x1036.google.com with SMTP id j3-20020a17090adc8300b0023d09aea4a6so1307923pjv.5 for ; Wed, 15 Mar 2023 03:23:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678875791; 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=7qQMFEtbq07W1ElR7VQSKGYfnRAtU6ZCdE527E1pQg8=; b=eCCOWHrI1DLPvw6F0WHO/eQWFVNpULTR/dT4v7kkgfcGnbGpnGr6G1DC5YzKOTQ87G IVAd1s/fl7+B2soUCzsD9o17Az7DmopgqVtWgd13hCl8Rx8YwBn2ArhgyF9pFpZzGYkY BzkD9nG0KGIANU6F37BKUe2CDRTWoXBm/wQuY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678875791; 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=7qQMFEtbq07W1ElR7VQSKGYfnRAtU6ZCdE527E1pQg8=; b=uHx1ozjlMiMk2KXzdWxJgmTrUxS9ZW+TsYsAjwPC/Zp4eyvRcBpAsYStMs05w+Fiwl A0FxPpswVRvK5cOMK0Z28R+Zw/b/ee3KayFsNEfgGcDJCPz4LOqWJfyjXit3l8/lxUq7 udIoVdJ7N2QtuADxHjou9+BTiqr+sCQHGnpRCqIfyCszpOasdvo+uGTLNk0heG/S3A8q NxUFFidRuKUPpcec8mt/NMUXwF2vvOYNGV1CMtBbPcs4AgaMdBTHclipAfzlupqfP7aS rfBWus7yL/9gguhxwpUUBGXObYGe5E5SbttZapbQOA4Me2nrBoVqTQdiAu9dFMZfaeSO mYqA== X-Gm-Message-State: AO0yUKXy6RcxwP46+CStNHcIgJZW42Ut1VK2TYaXoDafrsLm1NQDWy0/ 8tBxpJQW54ObI5VEmSS0uyi4kwH1HT7zRxoAvh0= X-Google-Smtp-Source: AK7set8VGkp66r87LETsvWbU9qXx9XNAwjNzBZBpyxmmlLIKkkQDbsv9qatDiu7CbuyRUiejFVH3vA== X-Received: by 2002:a17:903:1106:b0:19d:7a4:4063 with SMTP id n6-20020a170903110600b0019d07a44063mr2659643plh.46.1678875790682; Wed, 15 Mar 2023 03:23:10 -0700 (PDT) Received: from chenghaoyang-low.c.googlers.com.com (112.157.221.35.bc.googleusercontent.com. [35.221.157.112]) by smtp.gmail.com with ESMTPSA id f11-20020a17090274cb00b001a0450da45csm3262786plt.185.2023.03.15.03.23.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Mar 2023 03:23:10 -0700 (PDT) X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Date: Wed, 15 Mar 2023 10:22:58 +0000 Message-Id: <20230315102300.2265491-5-chenghaoyang@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog In-Reply-To: <20230315102300.2265491-1-chenghaoyang@google.com> References: <20230315102300.2265491-1-chenghaoyang@google.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 4/6] libcamera: pipeline: virtual: Generate and validate StreamConfigurations 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: , X-Patchwork-Original-From: Harvey Yang via libcamera-devel From: Harvey Yang Reply-To: Harvey Yang Cc: Harvey Yang , Harvey Yang Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Implement the support for Generating and Validating the streams the Camera can provide. Currently use only one camera with fixed configuration. Will add a configuration file to specify virtual cameras. In |PipelineHandlerVirtual::configure|, nothing needs to be done. Test the configurations can be generated and reported with cam -I: """ build/src/apps/cam/cam -c 1 -I [45:19:28.901456135] [2611530] INFO IPAManager ipa_manager.cpp:143 libcamera is not installed. Adding '/usr/local/google/home/chenghaoyang/cros2/src/third_party/libcamera/build/src /ipa' to the IPA search path [45:19:28.904364465] [2611530] INFO Camera camera_manager.cpp:293 libcamera v0.0.1+56-4f4554fa-dirty (2022-12-07T06:55:04+00:00) Using camera Virtual0 as cam0 0: 1920x1080-NV12 * Pixelformat: NV12 (1280x720)-(1920x1080)/(+1,+1) - 1280x720 - 1280x800 - 1360x768 - 1366x768 - 1440x900 - 1280x1024 - 1536x864 - 1280x1080 - 1600x900 - 1400x1050 - 1680x1050 - 1920x1080 """ Signed-off-by: Harvey Yang --- src/libcamera/pipeline/virtual/virtual.cpp | 141 +++++++++++++++++++-- 1 file changed, 133 insertions(+), 8 deletions(-) diff --git a/src/libcamera/pipeline/virtual/virtual.cpp b/src/libcamera/pipeline/virtual/virtual.cpp index 1d66a60e..b0e6de20 100644 --- a/src/libcamera/pipeline/virtual/virtual.cpp +++ b/src/libcamera/pipeline/virtual/virtual.cpp @@ -8,6 +8,7 @@ #include #include +#include #include "libcamera/internal/camera.h" #include "libcamera/internal/pipeline_handler.h" @@ -19,6 +20,11 @@ LOG_DEFINE_CATEGORY(VIRTUAL) class VirtualCameraData : public Camera::Private { public: + struct Resolution { + Size size; + std::vector frame_rates; + std::vector formats; + }; VirtualCameraData(PipelineHandler *pipe) : Camera::Private(pipe) { @@ -26,15 +32,22 @@ public: ~VirtualCameraData() = default; + std::vector supportedResolutions_; + Stream stream_; }; class VirtualCameraConfiguration : public CameraConfiguration { public: - VirtualCameraConfiguration(); + static constexpr unsigned int kBufferCount = 4; // 4~6 + + VirtualCameraConfiguration(VirtualCameraData *data); Status validate() override; + +private: + const VirtualCameraData *data_; }; class PipelineHandlerVirtual : public PipelineHandler @@ -55,16 +68,56 @@ public: int queueRequestDevice(Camera *camera, Request *request) override; bool match(DeviceEnumerator *enumerator) override; + +private: + VirtualCameraData *cameraData(Camera *camera) + { + return static_cast(camera->_d()); + } }; -VirtualCameraConfiguration::VirtualCameraConfiguration() - : CameraConfiguration() +VirtualCameraConfiguration::VirtualCameraConfiguration(VirtualCameraData *data) + : CameraConfiguration(), data_(data) { } CameraConfiguration::Status VirtualCameraConfiguration::validate() { - return Invalid; + Status status = Valid; + + if (config_.empty()) { + LOG(VIRTUAL, Error) << "Empty config"; + return Invalid; + } + + // TODO: check if we should limit |config_.size()| + + Size maxSize; + for (const auto &resolution : data_->supportedResolutions_) + maxSize = std::max(maxSize, resolution.size); + + for (StreamConfiguration &cfg : config_) { + // TODO: check |cfg.pixelFormat|. + + bool found = false; + for (const auto &resolution : data_->supportedResolutions_) { + if (resolution.size.width >= cfg.size.width && resolution.size.height >= cfg.size.height) { + found = true; + break; + } + } + + if (!found) { + cfg.size = maxSize; + status = Adjusted; + } + + cfg.setStream(const_cast(&data_->stream_)); + + cfg.bufferCount = VirtualCameraConfiguration::kBufferCount; + } + + return status; } PipelineHandlerVirtual::PipelineHandlerVirtual(CameraManager *manager) @@ -75,16 +128,81 @@ PipelineHandlerVirtual::PipelineHandlerVirtual(CameraManager *manager) std::unique_ptr PipelineHandlerVirtual::generateConfiguration(Camera *camera, const StreamRoles &roles) { - (void)camera; - (void)roles; - return std::unique_ptr(nullptr); + VirtualCameraData *data = cameraData(camera); + auto config = + std::make_unique(data); + + if (roles.empty()) + return config; + + Size minSize, sensorResolution; + for (const auto &resolution : data->supportedResolutions_) { + if (minSize.isNull() || minSize > resolution.size) + minSize = resolution.size; + + sensorResolution = std::max(sensorResolution, resolution.size); + } + + for (const StreamRole role : roles) { + std::map> streamFormats; + unsigned int bufferCount; + PixelFormat pixelFormat; + + switch (role) { + case StreamRole::StillCapture: + pixelFormat = formats::NV12; + bufferCount = VirtualCameraConfiguration::kBufferCount; + streamFormats[pixelFormat] = { { minSize, sensorResolution } }; + + break; + + case StreamRole::Raw: { + // TODO: check + pixelFormat = formats::SBGGR10; + bufferCount = VirtualCameraConfiguration::kBufferCount; + streamFormats[pixelFormat] = { { minSize, sensorResolution } }; + + break; + } + + case StreamRole::Viewfinder: + case StreamRole::VideoRecording: { + pixelFormat = formats::NV12; + bufferCount = VirtualCameraConfiguration::kBufferCount; + streamFormats[pixelFormat] = { { minSize, sensorResolution } }; + + break; + } + + default: + LOG(VIRTUAL, Error) + << "Requested stream role not supported: " << role; + config.reset(); + return config; + } + + StreamFormats formats(streamFormats); + StreamConfiguration cfg(formats); + cfg.size = sensorResolution; + cfg.pixelFormat = pixelFormat; + cfg.bufferCount = bufferCount; + config->addConfiguration(cfg); + } + + if (config->validate() == CameraConfiguration::Invalid) { + config.reset(); + return config; + } + + return config; } int PipelineHandlerVirtual::configure(Camera *camera, CameraConfiguration *config) { (void)camera; (void)config; - return -1; + // Nothing to be done. + return 0; } int PipelineHandlerVirtual::exportFrameBuffers(Camera *camera, Stream *stream, @@ -119,7 +237,14 @@ bool PipelineHandlerVirtual::match(DeviceEnumerator *enumerator) { (void)enumerator; + // TODO: Add virtual cameras according to a config file. + std::unique_ptr data = std::make_unique(this); + + data->supportedResolutions_.resize(2); + data->supportedResolutions_[0] = { .size = Size(1920, 1080), .frame_rates = { 30 }, .formats = { "YCbCr_420_888" } }; + data->supportedResolutions_[1] = { .size = Size(1280, 720), .frame_rates = { 30, 60 }, .formats = { "YCbCr_420_888" } }; + /* Create and register the camera. */ std::set streams{ &data->stream_ }; const std::string id = "Virtual0";