From patchwork Thu Jan 5 04:37:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 18093 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 B84C6C322E for ; Thu, 5 Jan 2023 04:38:07 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 70FD1625E4; Thu, 5 Jan 2023 05:38:07 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1672893487; bh=APeQXA7xUzqatN2KHkXdArmpDV/MRE+qbbFiX/hrdv8=; 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=23G6OdybxcmqwyYPVYFrt5S9BmPySA+oWuJqcnp1GypJfmOt/kLhnI36HxlX9fdOo sSvH0l5VKfySDju4tSOiVTcLfb1OeX9OtWjexhJoLcKNkCK1pkLnSFTy0u6tTQ0Ckg SrH3OHlypZNfejUs0zUb/u5Utxi3o9jVIBUf/zP4a/GhHeKIedghfn28YRzI0ORsbH zbZBZl7Dd92OjiHs3ILa84bKrApQ590jXvJh56rycfLlXqmiH7SA3gYJ/uIm44DEVF LdvvApU8AMzPGeDQA1qdscAW225wpI+Xo/UrCSJi6fGhUq3s8snuYhuRE3SJBAmsTb UWbDRtSeDNjpw== Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 425AF625E0 for ; Thu, 5 Jan 2023 05:38:03 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="Fi2lJI5k"; dkim-atps=neutral Received: by mail-pl1-x62b.google.com with SMTP id p24so15732750plw.11 for ; Wed, 04 Jan 2023 20:38:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; 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=xZUF8kLRelIKfAN12rHK0VQVbiYV9PuXYXL0Rf+v8Rk=; b=Fi2lJI5kCGbH/7HWTgP9co2eC3tW/3DNU86amBvu+zkExsN3gbWMKYlCznv/PFeFVj lkTeFS6e2NopgqEEoE9YxyTmuCbM5EmjMRI0rWTCY4l5nMFICRjwFyMbJ0X2PQRu6jfC nyc19F77u/u0LV3JMGqZppcbBQbVW0bzhkH9Y= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=xZUF8kLRelIKfAN12rHK0VQVbiYV9PuXYXL0Rf+v8Rk=; b=VQ962qSUziS2oR2KhpQ0HylcN8X95b/I8xaM7XUmegN0zXiO3tl2rTSKrHq3EdptC/ 9aL+qsP3osArZB6734dYCSCrok0MruPLjyPyTS6z5J8p4eU/ZRxkf4aXzn5L2CjHFyfE wt+81IHpFQktTwd0Ty5qCvaTCKPNfhPrX5Rwg+25Y3JV8XOAR1e+uenSqcwrhiCEQLiz 3DPTK4sxw5e/WMlXpCudU3Bcw+xznB4Q/LyK9eO1Er5qpfwFyhf37txyHeNmJSt00tbc swOWZe2LIIVJrmugs4cIHz4rBj8f6WOMknaGPMaGta9cP2RBhzqpX0yp/Yq30qnYwKiP 6jGw== X-Gm-Message-State: AFqh2kp/fNC04++H1oPD9XQcn5GT6IeowYK0BsD971nyNUznZ9Qpn1g2 /2l/V2YBnK8blmpBg8/dZBKvSYX1dfVwNjHH X-Google-Smtp-Source: AMrXdXtErhSQyT6DVZILd0e27UaMzOCyd4pEOUzNGEKs3Diu2aUpVaSg/bPswrloVdWVEXthuPdXOA== X-Received: by 2002:a17:902:ccc1:b0:189:5ff5:eb92 with SMTP id z1-20020a170902ccc100b001895ff5eb92mr66564137ple.39.1672893481480; Wed, 04 Jan 2023 20:38:01 -0800 (PST) Received: from chenghaoyang-low.c.googlers.com.com (46.165.189.35.bc.googleusercontent.com. [35.189.165.46]) by smtp.gmail.com with ESMTPSA id x23-20020a170902b41700b00188c5f0f9e9sm25015200plr.199.2023.01.04.20.38.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Jan 2023 20:38:01 -0800 (PST) X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Date: Thu, 5 Jan 2023 04:37:24 +0000 Message-Id: <20230105043726.679968-8-chenghaoyang@google.com> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog In-Reply-To: <20230105043726.679968-1-chenghaoyang@google.com> References: <20230105043726.679968-1-chenghaoyang@google.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 7/9] 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 | 140 +++++++++++++++++++-- 1 file changed, 132 insertions(+), 8 deletions(-) diff --git a/src/libcamera/pipeline/virtual/virtual.cpp b/src/libcamera/pipeline/virtual/virtual.cpp index ba77d757..ef2901e7 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/media_device_virtual.h" @@ -20,6 +21,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) { @@ -27,15 +33,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 @@ -58,17 +71,56 @@ public: bool match(DeviceEnumerator *enumerator) override; private: + VirtualCameraData *cameraData(Camera *camera) + { + return static_cast(camera->_d()); + } + std::shared_ptr mediaDeviceVirtual_; }; -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) @@ -79,16 +131,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, @@ -124,7 +241,14 @@ bool PipelineHandlerVirtual::match(DeviceEnumerator *enumerator) (void)enumerator; mediaDevices_.push_back(mediaDeviceVirtual_); + // 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";