From patchwork Fri Jul 26 09:54:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 20702 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 E4957BDC71 for ; Fri, 26 Jul 2024 09:56:40 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id B744463373; Fri, 26 Jul 2024 11:56:36 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="hjDYzAmq"; dkim-atps=neutral Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id C9F6A61994 for ; Fri, 26 Jul 2024 11:56:33 +0200 (CEST) Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1fc587361b6so4101725ad.2 for ; Fri, 26 Jul 2024 02:56:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1721987792; x=1722592592; 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=92eGQpIxfgbeaJOI/KHsvmgmx/r97AIsQENTaLGeeDQ=; b=hjDYzAmq3wjtPEoA2Zmv4CgpDjlSzlFmFGQ8glCOji7N4zrzsFL0O3mv8d8IthdfzR Pu8u5I64sBBbBC7vAr/OQZw70EznGgLL3eK3plxGNdHi/0JrZJ201c34zrAuoQ7RavTA HsnzitZS+ctw4Yvc5uYL7h1uDi3FCuW3jCeYM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721987792; x=1722592592; 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=92eGQpIxfgbeaJOI/KHsvmgmx/r97AIsQENTaLGeeDQ=; b=o5W159/Ks/JJ44CQ4BBDYIEdJ5L/xYr/j13qskY+WPqV3hT3X8Ej22Mik+Atu0xms9 aTe3PWU7FgBykQt5KmiZn6TLib/DgVL4y3mWxXNaq/jJkDqaHiivc2mCTRRkejseBwwB eNgL7WbLlNYK+lujiyGIoe2G9B64uIjohKEAkXbf6lViwtj18FbUr0ZePhtlFHCZOvjX Uxmp0zR1Hrl20B3cvPiTgGO46pXczZtZyz26+JCZ/RUzRyPyrBXSCYoe9nooom9As/Wd Rj36CB2+XH8vBo7r5qhUMDPbOux9aDV0RX9w6ZtwEelw15Z07/mjxUqtDEwBc6SOC5vG jQ/g== X-Gm-Message-State: AOJu0YzVifsEbBKEpxJrL7C69KTEp3JyavVMUusHVIcflNtfYtnDpOSo rmyCT/NbGt8dvACU2lW5cSGEu6fFJq8DVWWJex4NHdbannCxpgYuvYN4UGo64N8uwKQDzUbSBxl v8Q== X-Google-Smtp-Source: AGHT+IH84Yf4ZXnaYFaUuZpLvEgTgj9LaBY99bOsQtsBMrgf2k8tEzCNzob5U5mcl4x32vF4sa0jfw== X-Received: by 2002:a17:902:c409:b0:1fd:8c42:61ca with SMTP id d9443c01a7336-1fed9255795mr53627135ad.25.1721987791931; Fri, 26 Jul 2024 02:56:31 -0700 (PDT) Received: from chenghaoyang-low.c.googlers.com.com (148.175.199.104.bc.googleusercontent.com. [104.199.175.148]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fed7c7f63asm28396665ad.46.2024.07.26.02.56.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jul 2024 02:56:31 -0700 (PDT) From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Harvey Yang , Harvey Yang Subject: [PATCH v6 1/7] Add a helper function exportFrameBuffers in DmaBufAllocator to make it easier to use. Date: Fri, 26 Jul 2024 09:54:22 +0000 Message-ID: <20240726095626.3632402-2-chenghaoyang@chromium.org> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog In-Reply-To: <20240726095626.3632402-1-chenghaoyang@chromium.org> References: <20240726095626.3632402-1-chenghaoyang@chromium.org> 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: Harvey Yang It'll be used in Virtual Pipeline Handler specifically. Signed-off-by: Harvey Yang Change-Id: Ieac63dc7b2aef7dda525e59cce232dd63d316417 --- .../libcamera/internal/dma_buf_allocator.h | 10 ++++ src/libcamera/dma_buf_allocator.cpp | 57 ++++++++++++++++++- 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/include/libcamera/internal/dma_buf_allocator.h b/include/libcamera/internal/dma_buf_allocator.h index 36ec1696b..dd2cc2372 100644 --- a/include/libcamera/internal/dma_buf_allocator.h +++ b/include/libcamera/internal/dma_buf_allocator.h @@ -8,12 +8,16 @@ #pragma once #include +#include #include #include namespace libcamera { +class FrameBuffer; +struct StreamConfiguration; + class DmaBufAllocator { public: @@ -30,7 +34,13 @@ public: bool isValid() const { return providerHandle_.isValid(); } UniqueFD alloc(const char *name, std::size_t size); + int exportFrameBuffers( + const StreamConfiguration &config, + std::vector> *buffers); + private: + std::unique_ptr createBuffer(const StreamConfiguration &config); + UniqueFD allocFromHeap(const char *name, std::size_t size); UniqueFD allocFromUDmaBuf(const char *name, std::size_t size); UniqueFD providerHandle_; diff --git a/src/libcamera/dma_buf_allocator.cpp b/src/libcamera/dma_buf_allocator.cpp index c06eca7d0..04079b682 100644 --- a/src/libcamera/dma_buf_allocator.cpp +++ b/src/libcamera/dma_buf_allocator.cpp @@ -23,6 +23,11 @@ #include +#include +#include + +#include "libcamera/internal/formats.h" + /** * \file dma_buf_allocator.cpp * \brief dma-buf allocator @@ -130,8 +135,8 @@ DmaBufAllocator::~DmaBufAllocator() = default; /* uClibc doesn't provide the file sealing API. */ #ifndef __DOXYGEN__ #if not HAVE_FILE_SEALS -#define F_ADD_SEALS 1033 -#define F_SEAL_SHRINK 0x0002 +#define F_ADD_SEALS 1033 +#define F_SEAL_SHRINK 0x0002 #endif #endif @@ -243,4 +248,52 @@ UniqueFD DmaBufAllocator::alloc(const char *name, std::size_t size) return allocFromHeap(name, size); } +int DmaBufAllocator::exportFrameBuffers( + const StreamConfiguration &config, + std::vector> *buffers) +{ + unsigned int count = config.bufferCount; + + for (unsigned i = 0; i < count; ++i) { + std::unique_ptr buffer = createBuffer(config); + if (!buffer) { + LOG(DmaBufAllocator, Error) << "Unable to create buffer"; + + buffers->clear(); + return -EINVAL; + } + + buffers->push_back(std::move(buffer)); + } + + return count; +} + +std::unique_ptr DmaBufAllocator::createBuffer( + const StreamConfiguration &config) +{ + std::vector planes; + + auto info = PixelFormatInfo::info(config.pixelFormat); + for (size_t i = 0; i < info.planes.size(); ++i) { + unsigned int planeSize = info.planeSize(config.size, i); + if (planeSize == 0) + continue; + + UniqueFD fd = alloc("FrameBuffer", planeSize); + if (!fd.isValid()) + return nullptr; + + SharedFD sharedFd(std::move(fd)); + + FrameBuffer::Plane plane; + plane.fd = sharedFd; + plane.offset = 0; + plane.length = planeSize; + planes.push_back(std::move(plane)); + } + + return std::make_unique(planes); +} + } /* namespace libcamera */ From patchwork Fri Jul 26 09:54:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 20703 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 EED4CBDC71 for ; Fri, 26 Jul 2024 09:56:42 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id EAEDB63378; Fri, 26 Jul 2024 11:56:38 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="c5UrKc1t"; dkim-atps=neutral Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 416A363371 for ; Fri, 26 Jul 2024 11:56:35 +0200 (CEST) Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-1fc49c0aaffso4053035ad.3 for ; Fri, 26 Jul 2024 02:56:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1721987793; x=1722592593; 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=3h1FiPDzN3Wef2W7iR2p7iICFPCXweUh/ZRBntn8Ej0=; b=c5UrKc1tBDuoMyH5rC1keymd8NdmchGmOEnCLa1VpE15F27NCN/BhL9s8SOfgoCcAh G3pohYjKusyNxxHxu4bnzxNnTr+941wkC20lw6gzdGKfuNE1zF2RZna6lcpsVwu31C0x lSBCgFwjB2u6oIbvCaSrCVkZcQKno6Ykq2yxA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721987793; x=1722592593; 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=3h1FiPDzN3Wef2W7iR2p7iICFPCXweUh/ZRBntn8Ej0=; b=VdagbzPYxRn45FeGAuEMu+/a6fyhvOcKovF7yU79UP7tV6BfbCySQ2ALxVAp269kFR qVZTs+o1K6Z3p8Sa4BZFoyW8QqNzHNnq5TWtijvq+c7N6ahDwXcOoO1OQrsPMJu0DUJX 4PJXFY4L+oI9L+flgXEP5moMQAsagXKk+WicJPScJGin0+0V5xMxHxeIcv0WP6nknF7S XAv0j1LcItAxESHedHTfwMLyG3usPZ2e+AHvAyJK9DNZAQbgOBvslXIFN8GTHiEcWxtC xofgM8aulS6mVMZvrYuXb65nF2pqaG1ipgYwREBZhewhJ72hMzFyQprng7a9aEGHoMuk 49RA== X-Gm-Message-State: AOJu0Yxmsxre3Aw0GhAB5rA559WlqHw7XtfN6L3rUZDkv09o1L5BkeM3 n+b2hMQ9WArvHqB8JonAuPwnigHIdkpOnK4ceSHi2qHHd+wL6SdTimy8wzxSWU2ZJr6KY4SB9E1 1+A== X-Google-Smtp-Source: AGHT+IHLMdiZMFyDLTyoQkFfAW0FpjHRPs4Mb/1O+U02VLyi0docnbUDw5R8gtSQg3a1s1qLek1dRA== X-Received: by 2002:a17:902:dac9:b0:1fc:692a:ba46 with SMTP id d9443c01a7336-1fed9337017mr46895325ad.65.1721987793416; Fri, 26 Jul 2024 02:56:33 -0700 (PDT) Received: from chenghaoyang-low.c.googlers.com.com (148.175.199.104.bc.googleusercontent.com. [104.199.175.148]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fed7c7f63asm28396665ad.46.2024.07.26.02.56.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jul 2024 02:56:33 -0700 (PDT) From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Harvey Yang , Harvey Yang Subject: [PATCH v6 2/7] The Fatal check of having at least one MediaDevice was to prevent pipeline handler implementations searching and owning media devices with custom conventions, instead of using the base function |acquireMediaDevice|. It also has the assumption that there's at least one media device to make a camera work. Date: Fri, 26 Jul 2024 09:54:23 +0000 Message-ID: <20240726095626.3632402-3-chenghaoyang@chromium.org> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog In-Reply-To: <20240726095626.3632402-1-chenghaoyang@chromium.org> References: <20240726095626.3632402-1-chenghaoyang@chromium.org> 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: Harvey Yang Now that the assumption will be broken by the virtual pipeline handler added in the following patches, and developers should be aware of the available functions in the base class to handle media devices, the Fatal check is no longer needed. Change-Id: I76b4b74ebb2fa982136a9f9ab1c8a0a98b518c3a Signed-off-by: Harvey Yang --- src/libcamera/pipeline_handler.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/libcamera/pipeline_handler.cpp b/src/libcamera/pipeline_handler.cpp index 5ea2ca780..125cf9d5e 100644 --- a/src/libcamera/pipeline_handler.cpp +++ b/src/libcamera/pipeline_handler.cpp @@ -605,9 +605,14 @@ void PipelineHandler::registerCamera(std::shared_ptr camera) { cameras_.push_back(camera); - if (mediaDevices_.empty()) - LOG(Pipeline, Fatal) - << "Registering camera with no media devices!"; + if (mediaDevices_.empty()) { + /* + * For virtual devices with no MediaDevice, there are no system + * devices to register. + */ + manager_->_d()->addCamera(std::move(camera)); + return; + } /* * Walk the entity list and map the devnums of all capture video nodes From patchwork Fri Jul 26 09:54:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 20704 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 BABE3BDC71 for ; Fri, 26 Jul 2024 09:56:44 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 291426337A; Fri, 26 Jul 2024 11:56:44 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="Kaufsj4x"; dkim-atps=neutral Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id EBA7A63375 for ; Fri, 26 Jul 2024 11:56:36 +0200 (CEST) Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-1fc611a0f8cso3973675ad.2 for ; Fri, 26 Jul 2024 02:56:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1721987795; x=1722592595; 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=U7UDgPSResdxH60CPhSb69JHSSLCWSM4C/bJvUhU+YA=; b=Kaufsj4xkzlwsTmR0gBH/POup+Q67o3wgigFhBZIFoqC6GfnaNgWy+2Dz5Dtwgm1hl N1GG5EeWQuD1l4kWYW1VIGEaVX8v81bpjQou0hoTOWvqa4Fbpldd9ERmeSrdt5LJsn+5 cD0BW06CBWTOlr4xXRb71loy9An85K48f8aRw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721987795; x=1722592595; 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=U7UDgPSResdxH60CPhSb69JHSSLCWSM4C/bJvUhU+YA=; b=GSaUZqlntHe1jfFd17V+xylVevMPaj1fvv5h3IBlRGRH6Lg/O3AEmeWUQQbogedbnl 8m/1uDeSFznhENRspW2Noh+NsWjsmJkUkpe/OlqDEM6oCK1iYKg+dlle6f5AJcD8Xi9y CNtRzjcB0pO+z2RwV6Mcqkx7D+ydfUzk7W4cnqK5U2kO7+PZocuKEHVBvF4DmmsDmYaB MdOH69+QukS32Ppl3s5dEJDZ8HOvsw8rTtoAHw0k/lllv82FVUkzV6rdmBx/VAkuTcDB frQGiW+2S4uCudmbNZHXAovxBkFQrM34vD78wcdzNk5A5Gi6r19sBybPDk7w3VY87dRt VfdA== X-Gm-Message-State: AOJu0Yyx4eXl+eZ8dQ6+PYolm59+pxhBXMvLcLYZQ67b04aOTmAFx3Gh uKzdRkh6DUV1QmiDDAtFRRqzlR8sivCU4pPGPFuoMqcJJlEm0DrYmstiNwhqT6TxyVsJQSZYxSP /Zg== X-Google-Smtp-Source: AGHT+IGseH6Sbg8cSj+TKnUD9oMS6+zGDQyhQeQNn6Zc2IuiLDMZ5dJwzZG1EC0xesnaF1xvNeyF4w== X-Received: by 2002:a17:902:d2cc:b0:1fd:a980:5b77 with SMTP id d9443c01a7336-1fed9280406mr60411475ad.36.1721987794944; Fri, 26 Jul 2024 02:56:34 -0700 (PDT) Received: from chenghaoyang-low.c.googlers.com.com (148.175.199.104.bc.googleusercontent.com. [104.199.175.148]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fed7c7f63asm28396665ad.46.2024.07.26.02.56.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jul 2024 02:56:34 -0700 (PDT) From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Harvey Yang Subject: [PATCH v6 3/7] libcamera: pipeline: Add VirtualPipelineHandler Date: Fri, 26 Jul 2024 09:54:24 +0000 Message-ID: <20240726095626.3632402-4-chenghaoyang@chromium.org> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog In-Reply-To: <20240726095626.3632402-1-chenghaoyang@chromium.org> References: <20240726095626.3632402-1-chenghaoyang@chromium.org> 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" Add VirtualPipelineHandler for more unit tests and verfiy libcamera infrastructure works on devices without using hardware cameras. Signed-off-by: Harvey Yang Change-Id: I0850d67e0a36bd3d1ba66f46edeac333633df6f7 --- meson.build | 1 + meson_options.txt | 3 +- src/libcamera/pipeline/virtual/meson.build | 5 + src/libcamera/pipeline/virtual/virtual.cpp | 243 +++++++++++++++++++++ src/libcamera/pipeline/virtual/virtual.h | 78 +++++++ 5 files changed, 329 insertions(+), 1 deletion(-) create mode 100644 src/libcamera/pipeline/virtual/meson.build create mode 100644 src/libcamera/pipeline/virtual/virtual.cpp create mode 100644 src/libcamera/pipeline/virtual/virtual.h diff --git a/meson.build b/meson.build index 2acd8c3e8..b5475fc74 100644 --- a/meson.build +++ b/meson.build @@ -213,6 +213,7 @@ pipelines_support = { 'simple': arch_arm, 'uvcvideo': ['any'], 'vimc': ['test'], + 'virtual': ['test'], } if pipelines.contains('all') diff --git a/meson_options.txt b/meson_options.txt index 7aa412491..c91cd241a 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -53,7 +53,8 @@ option('pipelines', 'rpi/vc4', 'simple', 'uvcvideo', - 'vimc' + 'vimc', + 'virtual' ], description : 'Select which pipeline handlers to build. If this is set to "auto", all the pipelines applicable to the target architecture will be built. If this is set to "all", all the pipelines will be built. If both are selected then "all" will take precedence.') diff --git a/src/libcamera/pipeline/virtual/meson.build b/src/libcamera/pipeline/virtual/meson.build new file mode 100644 index 000000000..ba7ff754e --- /dev/null +++ b/src/libcamera/pipeline/virtual/meson.build @@ -0,0 +1,5 @@ +# SPDX-License-Identifier: CC0-1.0 + +libcamera_sources += files([ + 'virtual.cpp', +]) diff --git a/src/libcamera/pipeline/virtual/virtual.cpp b/src/libcamera/pipeline/virtual/virtual.cpp new file mode 100644 index 000000000..8abb74b48 --- /dev/null +++ b/src/libcamera/pipeline/virtual/virtual.cpp @@ -0,0 +1,243 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2023, Google Inc. + * + * virtual.cpp - Pipeline handler for virtual cameras + */ + +#include "virtual.h" + +#include + +#include +#include +#include +#include +#include + +#include "libcamera/internal/camera.h" +#include "libcamera/internal/formats.h" +#include "libcamera/internal/pipeline_handler.h" + +namespace libcamera { + +LOG_DEFINE_CATEGORY(Virtual) + +namespace { + +uint64_t currentTimestamp() +{ + struct timespec ts; + if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) { + LOG(Virtual, Error) << "Get clock time fails"; + return 0; + } + + return ts.tv_sec * 1'000'000'000LL + ts.tv_nsec; +} + +} // namespace + +VirtualCameraConfiguration::VirtualCameraConfiguration(VirtualCameraData *data) + : CameraConfiguration(), data_(data) +{ +} + +CameraConfiguration::Status VirtualCameraConfiguration::validate() +{ + Status status = Valid; + + if (config_.empty()) { + LOG(Virtual, Error) << "Empty config"; + return Invalid; + } + + /* Currently only one stream is supported */ + if (config_.size() > 1) { + config_.resize(1); + status = Adjusted; + } + + Size maxSize; + for (const auto &resolution : data_->supportedResolutions_) + maxSize = std::max(maxSize, resolution.size); + + for (StreamConfiguration &cfg : config_) { + 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; + } + + const PixelFormatInfo &info = PixelFormatInfo::info(cfg.pixelFormat); + cfg.stride = info.stride(cfg.size.width, 0, 1); + cfg.frameSize = info.frameSize(cfg.size, 1); + + cfg.setStream(const_cast(&data_->stream_)); + + cfg.bufferCount = VirtualCameraConfiguration::kBufferCount; + } + + return status; +} + +PipelineHandlerVirtual::PipelineHandlerVirtual(CameraManager *manager) + : PipelineHandler(manager) +{ +} + +std::unique_ptr +PipelineHandlerVirtual::generateConfiguration(Camera *camera, + Span roles) +{ + 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; +} + +int PipelineHandlerVirtual::configure( + [[maybe_unused]] Camera *camera, + [[maybe_unused]] CameraConfiguration *config) +{ + // Nothing to be done. + return 0; +} + +int PipelineHandlerVirtual::exportFrameBuffers( + [[maybe_unused]] Camera *camera, + Stream *stream, + std::vector> *buffers) +{ + if (!dmaBufAllocator_.isValid()) + return -ENOBUFS; + + return dmaBufAllocator_.exportFrameBuffers(stream->configuration(), buffers); +} + +int PipelineHandlerVirtual::start([[maybe_unused]] Camera *camera, + [[maybe_unused]] const ControlList *controls) +{ + /* \todo Start reading the virtual video if any. */ + return 0; +} + +void PipelineHandlerVirtual::stopDevice([[maybe_unused]] Camera *camera) +{ + /* \todo Reset the virtual video if any. */ +} + +int PipelineHandlerVirtual::queueRequestDevice([[maybe_unused]] Camera *camera, + Request *request) +{ + /* \todo Read from the virtual video if any. */ + for (auto it : request->buffers()) + completeBuffer(request, it.second); + + request->metadata().set(controls::SensorTimestamp, currentTimestamp()); + completeRequest(request); + + return 0; +} + +bool PipelineHandlerVirtual::match([[maybe_unused]] DeviceEnumerator *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 } }; + data->supportedResolutions_[1] = { .size = Size(1280, 720), .frame_rates = { 30, 60 } }; + + data->properties_.set(properties::Location, properties::CameraLocationFront); + data->properties_.set(properties::Model, "Virtual Video Device"); + data->properties_.set(properties::PixelArrayActiveAreas, { Rectangle(Size(1920, 1080)) }); + + /* \todo Set FrameDurationLimits based on config. */ + ControlInfoMap::Map controls; + int64_t min_frame_duration = 30, max_frame_duration = 60; + controls[&controls::FrameDurationLimits] = ControlInfo(min_frame_duration, max_frame_duration); + data->controlInfo_ = ControlInfoMap(std::move(controls), controls::controls); + + /* Create and register the camera. */ + std::set streams{ &data->stream_ }; + const std::string id = "Virtual0"; + std::shared_ptr camera = Camera::create(std::move(data), id, streams); + registerCamera(std::move(camera)); + + return false; // Prevent infinite loops for now +} + +REGISTER_PIPELINE_HANDLER(PipelineHandlerVirtual, "virtual") + +} /* namespace libcamera */ diff --git a/src/libcamera/pipeline/virtual/virtual.h b/src/libcamera/pipeline/virtual/virtual.h new file mode 100644 index 000000000..6fc6b34d8 --- /dev/null +++ b/src/libcamera/pipeline/virtual/virtual.h @@ -0,0 +1,78 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2023, Google Inc. + * + * virtual.h - Pipeline handler for virtual cameras + */ + +#pragma once + +#include + +#include "libcamera/internal/camera.h" +#include "libcamera/internal/dma_buf_allocator.h" +#include "libcamera/internal/pipeline_handler.h" + +namespace libcamera { + +class VirtualCameraData : public Camera::Private +{ +public: + struct Resolution { + Size size; + std::vector frame_rates; + }; + VirtualCameraData(PipelineHandler *pipe) + : Camera::Private(pipe) + { + } + + ~VirtualCameraData() = default; + + std::vector supportedResolutions_; + + Stream stream_; +}; + +class VirtualCameraConfiguration : public CameraConfiguration +{ +public: + static constexpr unsigned int kBufferCount = 4; + + VirtualCameraConfiguration(VirtualCameraData *data); + + Status validate() override; + +private: + const VirtualCameraData *data_; +}; + +class PipelineHandlerVirtual : public PipelineHandler +{ +public: + PipelineHandlerVirtual(CameraManager *manager); + + std::unique_ptr generateConfiguration(Camera *camera, + Span roles) override; + int configure(Camera *camera, CameraConfiguration *config) override; + + int exportFrameBuffers(Camera *camera, Stream *stream, + std::vector> *buffers) override; + + int start(Camera *camera, const ControlList *controls) override; + void stopDevice(Camera *camera) override; + + int queueRequestDevice(Camera *camera, Request *request) override; + + bool match(DeviceEnumerator *enumerator) override; + +private: + VirtualCameraData *cameraData(Camera *camera) + { + return static_cast(camera->_d()); + } + + DmaBufAllocator dmaBufAllocator_; +}; + +} // namespace libcamera From patchwork Fri Jul 26 09:54:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 20705 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 47E1FBDC71 for ; Fri, 26 Jul 2024 09:56:47 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id DF5C86337F; Fri, 26 Jul 2024 11:56:46 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="NbW4IHSu"; dkim-atps=neutral Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 91B9761994 for ; Fri, 26 Jul 2024 11:56:38 +0200 (CEST) Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-1fc658b6b2eso4032825ad.0 for ; Fri, 26 Jul 2024 02:56:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1721987797; x=1722592597; 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=LnScMZl9xDDlUx3VD4jbBUFtWNG/mFUavpE5NoHjgHU=; b=NbW4IHSu7q9kkl5ltQxHga6Sbi7gAJxH/Hy+CCakIkl0DeYMtQEZu9vkKGIYLnSVhE KAYST/+Hq9ugdXniWQE9QfX0Bo/Jm/gPw5oevI+SrsZYa7O/6mfTk+82ZI0NqWexLe5h UynxwMf27AqlZQf1AZuRGRE3HZv98uc3GWKck= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721987797; x=1722592597; 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=LnScMZl9xDDlUx3VD4jbBUFtWNG/mFUavpE5NoHjgHU=; b=XqLokYgdGpGUegKqPfJB2ACpW16nY8JJ8gtCvtlupyiFxLr1dbNuuREDKwhDzPOMRT ZNtt/OAY2PQGDcIzuT6ZGG4rJMd4qaTmfqpBm7l7hqSpLWGZADPxWS8Ox3ir9xPVzkQi 7CzLbLXDvbi13epOZMy3iF2iOJvX21SsGvUk6ZwcoMZks4MB/bsLXxdXA0xn7kpsACuK A6HeRzW12foMNZPa7cLlcKpx3y9JWD9GiG4Ikwfqj5mpvTOkYjLNTAP6xgx3i8UVM92C o3b6kc+xu8W/lAd9SgXW44QluRBKgkCS0iHAv5DXBfzBj1KzFSaFxEspNyeyojE3TGo5 Tb8Q== X-Gm-Message-State: AOJu0YzJ2Mw/jOEBEVFS/TXZ/T9pI4HBhnM61QkkQxJCMIiG1JkJXPkl BwEJut7nIrHhfceUc61nw4fHPE1+XnxbiyUOKc6pEWWqT7J6zT0xS4D9BCHZSG1Fn+1sJArvcSH 8lw== X-Google-Smtp-Source: AGHT+IEg0MKYW+UUsafBW7/mkc6rKt+AUXxKQl8rRyYKNXEjIrrslufmXFm+ggEZ0hT6+DRHECdTcw== X-Received: by 2002:a17:903:22c4:b0:1f7:37f:728d with SMTP id d9443c01a7336-1fed3870eb3mr75232585ad.10.1721987796761; Fri, 26 Jul 2024 02:56:36 -0700 (PDT) Received: from chenghaoyang-low.c.googlers.com.com (148.175.199.104.bc.googleusercontent.com. [104.199.175.148]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fed7c7f63asm28396665ad.46.2024.07.26.02.56.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jul 2024 02:56:36 -0700 (PDT) From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Konami Shu , Harvey Yang , Yunke Cao , Tomasz Figa Subject: [PATCH v6 4/7] libcamera: pipeline: Add test pattern for VirtualPipelineHandler Date: Fri, 26 Jul 2024 09:54:25 +0000 Message-ID: <20240726095626.3632402-5-chenghaoyang@chromium.org> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog In-Reply-To: <20240726095626.3632402-1-chenghaoyang@chromium.org> References: <20240726095626.3632402-1-chenghaoyang@chromium.org> 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 - There are two test patterns: color bars and diagonal lines - Add class for generating test patterns - Add libyuv to build dependencies - Make VirtualPipelineHandler show the test pattern - Format the code - Rename test_pattern to frame_generator - reflect comment - Fix const variable name - Use #pragma once - Make configure() private Signed-off-by: Konami Shu Co-developed-by: Harvey Yang Co-developed-by: Yunke Cao Co-developed-by: Tomasz Figa Change-Id: I744706c9b86539594751fcdd0b57412238f027ef --- .../pipeline/virtual/frame_generator.h | 33 ++++++ src/libcamera/pipeline/virtual/meson.build | 22 ++++ .../virtual/test_pattern_generator.cpp | 112 ++++++++++++++++++ .../pipeline/virtual/test_pattern_generator.h | 58 +++++++++ src/libcamera/pipeline/virtual/virtual.cpp | 28 ++++- src/libcamera/pipeline/virtual/virtual.h | 8 ++ 6 files changed, 258 insertions(+), 3 deletions(-) create mode 100644 src/libcamera/pipeline/virtual/frame_generator.h create mode 100644 src/libcamera/pipeline/virtual/test_pattern_generator.cpp create mode 100644 src/libcamera/pipeline/virtual/test_pattern_generator.h diff --git a/src/libcamera/pipeline/virtual/frame_generator.h b/src/libcamera/pipeline/virtual/frame_generator.h new file mode 100644 index 000000000..9699af7a4 --- /dev/null +++ b/src/libcamera/pipeline/virtual/frame_generator.h @@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2023, Google Inc. + * + * frame_generator.h - Virtual cameras helper to generate frames + */ + +#pragma once + +#include +#include + +namespace libcamera { + +class FrameGenerator +{ +public: + virtual ~FrameGenerator() = default; + + /* Create buffers for using them in `generateFrame` */ + virtual void configure(const Size &size) = 0; + + /** Fill the output frame buffer. + * Use the frame at the frameCount of image frames + */ + virtual void generateFrame(const Size &size, + const FrameBuffer *buffer) = 0; + +protected: + FrameGenerator() {} +}; + +} /* namespace libcamera */ diff --git a/src/libcamera/pipeline/virtual/meson.build b/src/libcamera/pipeline/virtual/meson.build index ba7ff754e..e1e65e68d 100644 --- a/src/libcamera/pipeline/virtual/meson.build +++ b/src/libcamera/pipeline/virtual/meson.build @@ -2,4 +2,26 @@ libcamera_sources += files([ 'virtual.cpp', + 'test_pattern_generator.cpp', ]) + +libyuv_dep = dependency('libyuv', required : false) + +# Fallback to a subproject if libyuv isn't found, as it's typically not +# provided by distributions. +if not libyuv_dep.found() + cmake = import('cmake') + + libyuv_vars = cmake.subproject_options() + libyuv_vars.add_cmake_defines({'CMAKE_POSITION_INDEPENDENT_CODE': 'ON'}) + libyuv_vars.set_override_option('cpp_std', 'c++17') + libyuv_vars.append_compile_args('cpp', + '-Wno-sign-compare', + '-Wno-unused-variable', + '-Wno-unused-parameter') + libyuv_vars.append_link_args('-ljpeg') + libyuv = cmake.subproject('libyuv', options : libyuv_vars) + libyuv_dep = libyuv.dependency('yuv') +endif + +libcamera_deps += [libyuv_dep] diff --git a/src/libcamera/pipeline/virtual/test_pattern_generator.cpp b/src/libcamera/pipeline/virtual/test_pattern_generator.cpp new file mode 100644 index 000000000..b142b522c --- /dev/null +++ b/src/libcamera/pipeline/virtual/test_pattern_generator.cpp @@ -0,0 +1,112 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2023, Google Inc. + * + * test_pattern_generator.cpp - Derived class of FrameGenerator for + * generating test patterns + */ + +#include "test_pattern_generator.h" + +#include + +#include "libcamera/internal/mapped_framebuffer.h" + +#include "libyuv/convert_from_argb.h" +namespace libcamera { + +LOG_DECLARE_CATEGORY(Virtual) + +static const unsigned int kARGBSize = 4; + +void TestPatternGenerator::generateFrame( + const Size &size, + const FrameBuffer *buffer) +{ + MappedFrameBuffer mappedFrameBuffer(buffer, + MappedFrameBuffer::MapFlag::Write); + + auto planes = mappedFrameBuffer.planes(); + + /* Convert the template_ to the frame buffer */ + int ret = libyuv::ARGBToNV12( + template_.get(), /*src_stride_argb=*/size.width * kARGBSize, + planes[0].begin(), size.width, + planes[1].begin(), size.width, + size.width, size.height); + if (ret != 0) { + LOG(Virtual, Error) << "ARGBToNV12() failed with " << ret; + } +} + +std::unique_ptr ColorBarsGenerator::create() +{ + return std::make_unique(); +} + +void ColorBarsGenerator::configure(const Size &size) +{ + constexpr uint8_t kColorBar[8][3] = { + // R, G, B + { 0xFF, 0xFF, 0xFF }, // White + { 0xFF, 0xFF, 0x00 }, // Yellow + { 0x00, 0xFF, 0xFF }, // Cyan + { 0x00, 0xFF, 0x00 }, // Green + { 0xFF, 0x00, 0xFF }, // Magenta + { 0xFF, 0x00, 0x00 }, // Red + { 0x00, 0x00, 0xFF }, // Blue + { 0x00, 0x00, 0x00 }, // Black + }; + + template_ = std::make_unique( + size.width * size.height * kARGBSize); + + unsigned int colorBarWidth = size.width / std::size(kColorBar); + + uint8_t *buf = template_.get(); + for (size_t h = 0; h < size.height; h++) { + for (size_t w = 0; w < size.width; w++) { + // repeat when the width is exceed + int index = (w / colorBarWidth) % std::size(kColorBar); + + *buf++ = kColorBar[index][2]; // B + *buf++ = kColorBar[index][1]; // G + *buf++ = kColorBar[index][0]; // R + *buf++ = 0x00; // A + } + } +} + +std::unique_ptr DiagonalLinesGenerator::create() +{ + return std::make_unique(); +} + +void DiagonalLinesGenerator::configure(const Size &size) +{ + constexpr uint8_t kColorBar[8][3] = { + // R, G, B + { 0xFF, 0xFF, 0xFF }, // White + { 0x00, 0x00, 0x00 }, // Black + }; + + template_ = std::make_unique( + size.width * size.height * kARGBSize); + + unsigned int lineWidth = size.width / 10; + + uint8_t *buf = template_.get(); + for (size_t h = 0; h < size.height; h++) { + for (size_t w = 0; w < size.width; w++) { + // repeat when the width is exceed + int index = ((w + h) / lineWidth) % 2; + + *buf++ = kColorBar[index][2]; // B + *buf++ = kColorBar[index][1]; // G + *buf++ = kColorBar[index][0]; // R + *buf++ = 0x00; // A + } + } +} + +} /* namespace libcamera */ diff --git a/src/libcamera/pipeline/virtual/test_pattern_generator.h b/src/libcamera/pipeline/virtual/test_pattern_generator.h new file mode 100644 index 000000000..ed8d4e43b --- /dev/null +++ b/src/libcamera/pipeline/virtual/test_pattern_generator.h @@ -0,0 +1,58 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2023, Google Inc. + * + * test_pattern_generator.h - Derived class of FrameGenerator for + * generating test patterns + */ + +#pragma once + +#include + +#include +#include + +#include "frame_generator.h" + +namespace libcamera { + +enum class TestPattern : char { + ColorBars = 0, + DiagonalLines = 1, +}; + +class TestPatternGenerator : public FrameGenerator +{ +private: + void generateFrame(const Size &size, + const FrameBuffer *buffer) override; + +protected: + /* Shift the buffer by 1 pixel left each frame */ + void shiftLeft(const Size &size); + /* Buffer of test pattern template */ + std::unique_ptr template_; +}; + +class ColorBarsGenerator : public TestPatternGenerator +{ +public: + static std::unique_ptr create(); + +private: + /* Generate a template buffer of the color bar test pattern. */ + void configure(const Size &size) override; +}; + +class DiagonalLinesGenerator : public TestPatternGenerator +{ +public: + static std::unique_ptr create(); + +private: + /* Generate a template buffer of the diagonal lines test pattern. */ + void configure(const Size &size) override; +}; + +} /* namespace libcamera */ diff --git a/src/libcamera/pipeline/virtual/virtual.cpp b/src/libcamera/pipeline/virtual/virtual.cpp index 8abb74b48..a36ffd42b 100644 --- a/src/libcamera/pipeline/virtual/virtual.cpp +++ b/src/libcamera/pipeline/virtual/virtual.cpp @@ -184,10 +184,14 @@ int PipelineHandlerVirtual::exportFrameBuffers( return dmaBufAllocator_.exportFrameBuffers(stream->configuration(), buffers); } -int PipelineHandlerVirtual::start([[maybe_unused]] Camera *camera, +int PipelineHandlerVirtual::start(Camera *camera, [[maybe_unused]] const ControlList *controls) { /* \todo Start reading the virtual video if any. */ + VirtualCameraData *data = cameraData(camera); + + data->frameGenerator_->configure(data->stream_.configuration().size); + return 0; } @@ -199,9 +203,14 @@ void PipelineHandlerVirtual::stopDevice([[maybe_unused]] Camera *camera) int PipelineHandlerVirtual::queueRequestDevice([[maybe_unused]] Camera *camera, Request *request) { + VirtualCameraData *data = cameraData(camera); + /* \todo Read from the virtual video if any. */ - for (auto it : request->buffers()) - completeBuffer(request, it.second); + for (auto const &[stream, buffer] : request->buffers()) { + /* map buffer and fill test patterns */ + data->frameGenerator_->generateFrame(stream->configuration().size, buffer); + completeBuffer(request, buffer); + } request->metadata().set(controls::SensorTimestamp, currentTimestamp()); completeRequest(request); @@ -233,11 +242,24 @@ bool PipelineHandlerVirtual::match([[maybe_unused]] DeviceEnumerator *enumerator std::set streams{ &data->stream_ }; const std::string id = "Virtual0"; std::shared_ptr camera = Camera::create(std::move(data), id, streams); + + initFrameGenerator(camera.get()); + registerCamera(std::move(camera)); return false; // Prevent infinite loops for now } +void PipelineHandlerVirtual::initFrameGenerator(Camera *camera) +{ + auto data = cameraData(camera); + if (data->testPattern_ == TestPattern::DiagonalLines) { + data->frameGenerator_ = DiagonalLinesGenerator::create(); + } else { + data->frameGenerator_ = ColorBarsGenerator::create(); + } +} + REGISTER_PIPELINE_HANDLER(PipelineHandlerVirtual, "virtual") } /* namespace libcamera */ diff --git a/src/libcamera/pipeline/virtual/virtual.h b/src/libcamera/pipeline/virtual/virtual.h index 6fc6b34d8..fecd9fa6f 100644 --- a/src/libcamera/pipeline/virtual/virtual.h +++ b/src/libcamera/pipeline/virtual/virtual.h @@ -13,6 +13,8 @@ #include "libcamera/internal/dma_buf_allocator.h" #include "libcamera/internal/pipeline_handler.h" +#include "test_pattern_generator.h" + namespace libcamera { class VirtualCameraData : public Camera::Private @@ -29,9 +31,13 @@ public: ~VirtualCameraData() = default; + TestPattern testPattern_; + std::vector supportedResolutions_; Stream stream_; + + std::unique_ptr frameGenerator_; }; class VirtualCameraConfiguration : public CameraConfiguration @@ -72,6 +78,8 @@ private: return static_cast(camera->_d()); } + void initFrameGenerator(Camera *camera); + DmaBufAllocator dmaBufAllocator_; }; From patchwork Fri Jul 26 09:54:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 20706 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 3FEDABDC71 for ; Fri, 26 Jul 2024 09:56:50 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id AAA8C6337F; Fri, 26 Jul 2024 11:56:49 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="ng63tnoU"; dkim-atps=neutral Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id CD4DF6336D for ; Fri, 26 Jul 2024 11:56:40 +0200 (CEST) Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-1fc566ac769so4048515ad.1 for ; Fri, 26 Jul 2024 02:56:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1721987799; x=1722592599; 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=EhpPiLHnLHb4kvcO5QY0MVzu9cMTTnTFFGiNRM6KewI=; b=ng63tnoUV+jlnVhl+aHnv5CYhXNbV6FVejCpJPFyX+uQmDYY+qtEtajyjroDnkQkwn 86NxlKta9j0MfoPzYJIG7f1+X9+u48h8S+dcKYVPiLfUOWy3s9SyqbyUmHr7nRaSbwWl TnBNwyJV0dVv/6TCw1bZW+4jPuVP4pMOvStjU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721987799; x=1722592599; 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=EhpPiLHnLHb4kvcO5QY0MVzu9cMTTnTFFGiNRM6KewI=; b=PP6oO5JHg7eQw7NScu+h7Hop6l33NF4fAqdqgZBZuM5DiVDdCwWPimS3OpQ+L6X4ja qQeeLdk86JGHj+DJ08gzcgOwIpHw3wmKQROxXKV2TuN28SH3xMxJ2HEvy62L9Zw7rM6D axrQcqRqPRtLQ1aoqDM8WZxGAKy8t8qWzvn70jdbFsLUBVFU7EXLSCgZ8cdImCl0yREy 8DJnH8UaCRUSFzh4hEfk7AsaO+2fXSSimu1udDshg+dZ3FjtKAPbx3l+7Au8JLYze+Gf 04DMlgxSnYNVzswESbpqbedZTe49q/Hkq14RZeSeXj/kOWQqJLbTOS95yqqvjusEqamp IzIQ== X-Gm-Message-State: AOJu0YweaRS1uebHU0QOL4kiJoNsEwfBBYV9KKYpJSQ09SOpL+cHYstx u2iQxfebhTkdu5g5bvAiWKynRb1m7HRYnZ4s9PWjD4WZFdJpEQU/lXb/t8/L2kHYFaEL6t/LsuE qMg== X-Google-Smtp-Source: AGHT+IFCP9OYkxTMKsTbeKroTp7qRT2UGoEfAANGlpeN4buh1g69beCSNVdkZ9G2NOEI1UaKORKq8w== X-Received: by 2002:a17:902:ce84:b0:1fb:779e:4fca with SMTP id d9443c01a7336-1fed384829emr63299395ad.14.1721987798612; Fri, 26 Jul 2024 02:56:38 -0700 (PDT) Received: from chenghaoyang-low.c.googlers.com.com (148.175.199.104.bc.googleusercontent.com. [104.199.175.148]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fed7c7f63asm28396665ad.46.2024.07.26.02.56.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jul 2024 02:56:38 -0700 (PDT) From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Konami Shu , Harvey Yang , Yunke Cao , Tomasz Figa Subject: [PATCH v6 5/7] libcamera: pipeline: Read config and register cameras based on the config Date: Fri, 26 Jul 2024 09:54:26 +0000 Message-ID: <20240726095626.3632402-6-chenghaoyang@chromium.org> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog In-Reply-To: <20240726095626.3632402-1-chenghaoyang@chromium.org> References: <20240726095626.3632402-1-chenghaoyang@chromium.org> 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 - Use Yaml::Parser to parse the config - Add config file at "virtual/data/virtual.yaml" - README.md contains the documentation for the format of config file and the implementation of Parser class. - Add Parser class to parse config file in Virtual pipeline handler - Add header file of virtual.cpp to use VirtualCameraData class in Parser class - Parse test patterns - Raise Error when the width of a resolution is an odd number Signed-off-by: Konami Shu Co-developed-by: Harvey Yang Co-developed-by: Yunke Cao Co-developed-by: Tomasz Figa Change-Id: I7f8c375e9a2dc4d06a94c06ddc0e8bc3a432dc2f --- src/libcamera/pipeline/virtual/README.md | 68 ++++++ .../pipeline/virtual/data/virtual.yaml | 49 +++++ src/libcamera/pipeline/virtual/meson.build | 1 + src/libcamera/pipeline/virtual/parser.cpp | 198 ++++++++++++++++++ src/libcamera/pipeline/virtual/parser.h | 45 ++++ src/libcamera/pipeline/virtual/virtual.cpp | 47 +++-- src/libcamera/pipeline/virtual/virtual.h | 6 +- 7 files changed, 389 insertions(+), 25 deletions(-) create mode 100644 src/libcamera/pipeline/virtual/README.md create mode 100644 src/libcamera/pipeline/virtual/data/virtual.yaml create mode 100644 src/libcamera/pipeline/virtual/parser.cpp create mode 100644 src/libcamera/pipeline/virtual/parser.h diff --git a/src/libcamera/pipeline/virtual/README.md b/src/libcamera/pipeline/virtual/README.md new file mode 100644 index 000000000..27d6283df --- /dev/null +++ b/src/libcamera/pipeline/virtual/README.md @@ -0,0 +1,68 @@ +# Virtual Pipeline Handler + +Virtual pipeline handler emulates fake external camera(s) on ChromeOS for testing. + +## Parse config file and register cameras + +- The config file is located at `src/libcamera/pipeline/virtual/data/virtual.yaml` + +### Config File Format +The config file contains the information about cameras' properties to register. +The config file should be a yaml file with dictionary of the cameraIds +associated with their properties as top level. The default value will be applied when any property is empty. + +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". +- `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. + +A sample config file: +``` +--- +"Virtual0": + supported_formats: + - width: 1920 + height: 1080 + frame_rates: + - 30 + - 60 + - width: 1680 + height: 1050 + frame_rates: + - 70 + - 80 + test_pattern: "bars" + location: "front" + model: "Virtual Video Device" +"Virtual1": + supported_formats: + - width: 800 + test_pattern: "lines" + location: "back" + model: "Virtual Video Device1" +"Virtual2": +``` + +### Implementation + +`Parser` class provides methods to parse the config file to register cameras +in Virtual Pipeline Handler. `parseConfigFile()` is exposed to use in +Virtual Pipeline Handler. + +This is the procedure of the Parser class: +1. `parseConfigFile()` parses the config file to `YamlObject` using `YamlParser::parse()`. + - Parse the top level of config file which are the camera ids and look into each camera properties. +2. For each camera, `parseCameraConfigData()` returns a camera with the configuration. + - The methods in the next step fill the data with the pointer to the Camera object. + - 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. + - `parseLocation()`: Parses `location` in the config. + - `parseModel()`: Parses `model` in the config. +4. Back to `parseConfigFile()` and append the camera configuration. +5. Returns a list of camera configurations. diff --git a/src/libcamera/pipeline/virtual/data/virtual.yaml b/src/libcamera/pipeline/virtual/data/virtual.yaml new file mode 100644 index 000000000..4eb239e24 --- /dev/null +++ b/src/libcamera/pipeline/virtual/data/virtual.yaml @@ -0,0 +1,49 @@ +# SPDX-License-Identifier: CC0-1.0 +%YAML 1.1 +--- +"Virtual0": + supported_formats: + - width: 1920 + height: 1080 + frame_rates: + - 30 + - 60 + - width: 1680 + height: 1050 + frame_rates: + - 70 + - 80 + test_pattern: "lines" + location: "front" + model: "Virtual Video Device" +"Virtual1": + supported_formats: + - width: 800 + height: 600 + frame_rates: + - 30 + - 60 + test_pattern: + location: "back" + model: "Virtual Video Device1" +"Virtual2": + supported_formats: + - width: 100 + height: 100 + test_pattern: "lines" + location: "front" + model: "Virtual Video Device2" +"Virtual3": + supported_formats: + - width: 100 + height: 100 + - width: 800 + height: 600 + - width: 1920 + height: 1080 + frame_rates: + - 20 + - 30 + location: "a" + model: "Virtual Video Device3" +"Virtual4": diff --git a/src/libcamera/pipeline/virtual/meson.build b/src/libcamera/pipeline/virtual/meson.build index e1e65e68d..2e82e64cb 100644 --- a/src/libcamera/pipeline/virtual/meson.build +++ b/src/libcamera/pipeline/virtual/meson.build @@ -3,6 +3,7 @@ libcamera_sources += files([ 'virtual.cpp', 'test_pattern_generator.cpp', + 'parser.cpp', ]) libyuv_dep = dependency('libyuv', required : false) diff --git a/src/libcamera/pipeline/virtual/parser.cpp b/src/libcamera/pipeline/virtual/parser.cpp new file mode 100644 index 000000000..032c0cd9d --- /dev/null +++ b/src/libcamera/pipeline/virtual/parser.cpp @@ -0,0 +1,198 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2023, Google Inc. + * + * parser.cpp - Virtual cameras helper to parse config file + */ + +#include "parser.h" + +#include +#include + +#include + +#include +#include + +#include "libcamera/internal/pipeline_handler.h" +#include "libcamera/internal/yaml_parser.h" + +#include "virtual.h" + +namespace libcamera { + +LOG_DECLARE_CATEGORY(Virtual) + +std::vector> Parser::parseConfigFile( + File &file, PipelineHandler *pipe) +{ + std::vector> configurations; + + std::unique_ptr cameras = YamlParser::parse(file); + if (!cameras) { + LOG(Virtual, Error) << "Failed to pass config file."; + return configurations; + } + + if (!cameras->isDictionary()) { + LOG(Virtual, Error) << "Config file is not a dictionary at the top level."; + return configurations; + } + + /* Look into the configuration of each camera */ + for (const auto &[cameraId, cameraConfigData] : cameras->asDict()) { + std::unique_ptr data = + parseCameraConfigData(cameraConfigData, pipe); + /* Parse configData to data*/ + if (!data) { + /* Skip the camera if it has invalid config */ + LOG(Virtual, Error) << "Failed to parse config of the camera: " + << cameraId; + continue; + } + + data->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])); + data->controlInfo_ = ControlInfoMap(std::move(controls), controls::controls); + configurations.push_back(std::move(data)); + } + return configurations; +} + +std::unique_ptr Parser::parseCameraConfigData( + const YamlObject &cameraConfigData, PipelineHandler *pipe) +{ + std::unique_ptr data = std::make_unique(pipe); + + if (parseSupportedFormats(cameraConfigData, data.get())) + return nullptr; + + if (parseTestPattern(cameraConfigData, data.get())) + return nullptr; + + if (parseLocation(cameraConfigData, data.get())) + return nullptr; + + if (parseModel(cameraConfigData, data.get())) + return nullptr; + + return data; +} + +int Parser::parseSupportedFormats( + const YamlObject &cameraConfigData, VirtualCameraData *data) +{ + Size activeResolution{ 0, 0 }; + if (cameraConfigData.contains("supported_formats")) { + const YamlObject &supportedResolutions = cameraConfigData["supported_formats"]; + + for (const YamlObject &supportedResolution : supportedResolutions.asList()) { + unsigned int width = supportedResolution["width"].get(1920); + unsigned int height = supportedResolution["height"].get(1080); + if (width <= 0 || height <= 0) { + LOG(Virtual, Error) << "Invalid width or/and height"; + return -EINVAL; + } + if (width % 2 != 0) { + LOG(Virtual, Error) << "Invalid width: width needs to be even"; + return -EINVAL; + } + + std::vector frameRates; + if (supportedResolution.contains("frame_rates")) { + auto frameRatesList = + supportedResolution["frame_rates"].getList().value(); + if (frameRatesList.size() != 2) { + LOG(Virtual, Error) << "frame_rates needs to be the two edge values of a range"; + return -EINVAL; + } + if (frameRatesList[0] > frameRatesList[1]) { + LOG(Virtual, Error) << "frame_rates's first value(lower bound) is higher than the second value(upper bound)"; + return -EINVAL; + } + frameRates.push_back(frameRatesList[0]); + frameRates.push_back(frameRatesList[1]); + } else { + frameRates.push_back(30); + frameRates.push_back(60); + } + + data->supportedResolutions_.emplace_back( + VirtualCameraData::Resolution{ Size{ width, height }, + frameRates }); + + activeResolution = std::max(activeResolution, Size{ width, height }); + } + } else { + data->supportedResolutions_.emplace_back( + VirtualCameraData::Resolution{ Size{ 1920, 1080 }, + { 30, 60 } }); + activeResolution = Size(1920, 1080); + } + + data->properties_.set(properties::PixelArrayActiveAreas, + { Rectangle(activeResolution) }); + + return 0; +} + +int Parser::parseTestPattern( + const YamlObject &cameraConfigData, VirtualCameraData *data) +{ + std::string testPattern = cameraConfigData["test_pattern"].get().value(); + + /* Default value is "bars" */ + if (testPattern == "bars" || testPattern == "") { + data->testPattern_ = TestPattern::ColorBars; + } else if (testPattern == "lines") { + data->testPattern_ = TestPattern::DiagonalLines; + } else { + LOG(Virtual, Error) << "Test pattern: " << testPattern + << "is not supported"; + return -EINVAL; + } + return 0; +} + +int Parser::parseLocation( + const YamlObject &cameraConfigData, VirtualCameraData *data) +{ + std::string location = cameraConfigData["location"].get().value(); + + /* Default value is properties::CameraLocationFront */ + if (location == "front" || location == "") { + data->properties_.set(properties::Location, + properties::CameraLocationFront); + } else if (location == "back") { + data->properties_.set(properties::Location, + properties::CameraLocationBack); + } else { + LOG(Virtual, Error) << "location: " << location + << " is not supported"; + return -EINVAL; + } + + return 0; +} + +int Parser::parseModel( + const YamlObject &cameraConfigData, VirtualCameraData *data) +{ + std::string model = + cameraConfigData["model"].get().value(); + + /* Default value is "Unknown" */ + if (model == "") + data->properties_.set(properties::Model, "Unknown"); + else + data->properties_.set(properties::Model, model); + + return 0; +} + +} /* namespace libcamera */ diff --git a/src/libcamera/pipeline/virtual/parser.h b/src/libcamera/pipeline/virtual/parser.h new file mode 100644 index 000000000..a377d8aa1 --- /dev/null +++ b/src/libcamera/pipeline/virtual/parser.h @@ -0,0 +1,45 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2023, Google Inc. + * + * parser.h - Virtual cameras helper to parse config file + */ + +#pragma once + +#include +#include + +#include + +#include "libcamera/internal/pipeline_handler.h" +#include "libcamera/internal/yaml_parser.h" + +#include "virtual.h" + +namespace libcamera { + +class Parser +{ +public: + Parser() {} + ~Parser() = default; + + std::vector> + parseConfigFile(File &file, PipelineHandler *pipe); + +private: + std::unique_ptr parseCameraConfigData( + const YamlObject &cameraConfigData, PipelineHandler *pipe); + + int parseSupportedFormats( + const YamlObject &cameraConfigData, VirtualCameraData *data); + int parseTestPattern( + const YamlObject &cameraConfigData, VirtualCameraData *data); + int parseLocation( + const YamlObject &cameraConfigData, VirtualCameraData *data); + int parseModel( + const YamlObject &cameraConfigData, VirtualCameraData *data); +}; + +} // namespace libcamera diff --git a/src/libcamera/pipeline/virtual/virtual.cpp b/src/libcamera/pipeline/virtual/virtual.cpp index a36ffd42b..5563c0f66 100644 --- a/src/libcamera/pipeline/virtual/virtual.cpp +++ b/src/libcamera/pipeline/virtual/virtual.cpp @@ -18,6 +18,10 @@ #include "libcamera/internal/camera.h" #include "libcamera/internal/formats.h" #include "libcamera/internal/pipeline_handler.h" +#include "libcamera/internal/yaml_parser.h" + +#include "frame_generator.h" +#include "parser.h" namespace libcamera { @@ -220,32 +224,31 @@ int PipelineHandlerVirtual::queueRequestDevice([[maybe_unused]] Camera *camera, bool PipelineHandlerVirtual::match([[maybe_unused]] DeviceEnumerator *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 } }; - data->supportedResolutions_[1] = { .size = Size(1280, 720), .frame_rates = { 30, 60 } }; - - data->properties_.set(properties::Location, properties::CameraLocationFront); - data->properties_.set(properties::Model, "Virtual Video Device"); - data->properties_.set(properties::PixelArrayActiveAreas, { Rectangle(Size(1920, 1080)) }); + File file(configurationFile("virtual", "virtual.yaml")); + bool isOpen = file.open(File::OpenModeFlag::ReadOnly); + if (!isOpen) { + LOG(Virtual, Error) << "Failed to open config file: " << file.fileName(); + return false; + } - /* \todo Set FrameDurationLimits based on config. */ - ControlInfoMap::Map controls; - int64_t min_frame_duration = 30, max_frame_duration = 60; - controls[&controls::FrameDurationLimits] = ControlInfo(min_frame_duration, max_frame_duration); - data->controlInfo_ = ControlInfoMap(std::move(controls), controls::controls); + Parser parser; + auto configData = parser.parseConfigFile(file, this); + if (configData.size() == 0) { + LOG(Virtual, Error) << "Failed to parse any cameras from the config file: " + << file.fileName(); + return false; + } - /* Create and register the camera. */ - std::set streams{ &data->stream_ }; - const std::string id = "Virtual0"; - std::shared_ptr camera = Camera::create(std::move(data), id, streams); + /* Configure and register cameras with configData */ + for (auto &data : configData) { + std::set streams{ &data->stream_ }; + std::string id = data->id_; + std::shared_ptr camera = Camera::create(std::move(data), id, streams); - initFrameGenerator(camera.get()); + initFrameGenerator(camera.get()); - registerCamera(std::move(camera)); + registerCamera(std::move(camera)); + } return false; // Prevent infinite loops for now } diff --git a/src/libcamera/pipeline/virtual/virtual.h b/src/libcamera/pipeline/virtual/virtual.h index fecd9fa6f..c1ac4eb90 100644 --- a/src/libcamera/pipeline/virtual/virtual.h +++ b/src/libcamera/pipeline/virtual/virtual.h @@ -22,7 +22,7 @@ class VirtualCameraData : public Camera::Private public: struct Resolution { Size size; - std::vector frame_rates; + std::vector frameRates; }; VirtualCameraData(PipelineHandler *pipe) : Camera::Private(pipe) @@ -31,9 +31,9 @@ public: ~VirtualCameraData() = default; - TestPattern testPattern_; - + std::string id_; std::vector supportedResolutions_; + TestPattern testPattern_; Stream stream_; From patchwork Fri Jul 26 09:54:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 20707 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 582DEBDC71 for ; Fri, 26 Jul 2024 09:56:52 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id AF50A6337B; Fri, 26 Jul 2024 11:56:51 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="TPrh4kjT"; dkim-atps=neutral Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 1B84A63371 for ; Fri, 26 Jul 2024 11:56:42 +0200 (CEST) Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-70d1c8d7d95so516130b3a.2 for ; Fri, 26 Jul 2024 02:56:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1721987800; x=1722592600; 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=+L09Rrpm/0DeMUNffdDZiAx9/frA5c+esuyG5D4svek=; b=TPrh4kjTXHbg6Naq0qI3JNGW+feBh5HKea9PVLngA8JAHDt+4eH2Ek8IGm+qwzW8l7 e0jqlZ/ThYmcr91MuEaPGNFel8PYkLxETzubn/UqDagHXwZ3mOBdJKG8oDuJKiKpe3Cu wNgLsu7HBuKyHD8t5tNMVL6r2YU4WAhED+j9g= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721987800; x=1722592600; 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=+L09Rrpm/0DeMUNffdDZiAx9/frA5c+esuyG5D4svek=; b=mFiz7amjWzkyFcoL+XO7ZIZTedV/saoXKl9kAx9sirdymR0MGE8W8VSAYnqxUlhPpA v4FiWQrB8qFI5uhTbhVKfEnl4Q21atVkWL5DlzF9C9mOELPh56e3NnYItD4r9YEGlX5L aLhJXSzzbcp4aP9B6C0/0ebGy9k7ABR6zfFc3HG48vkFJ1Q46uH43D5ifwQn8w3la4NN 7llHwV1Nfc8GX0IfzsCZ+VcUUJPnC/7YoZ+YYTmgwlMudjGohQ1kn1kv7W7UMOk1f1W0 2fAmj90PMplNajTGieHdTCH6cMHfw5Bx05w4bLJUfOvl5WwAkWR+fK6N9mONxtgK7x36 PIDA== X-Gm-Message-State: AOJu0Yy8mG+T06CejcM4d6BQRXMgzd0HOdZsRYA4yuLANjn/JgADWvg5 q7GW5pYuC+Ye/P3re5mjBFYpis6uvZL3n4fF0Vo97lkXRRqCKWVJb5dqzbTWEqAuJpJtE9rLNu9 mnA== X-Google-Smtp-Source: AGHT+IElzz+mOcRObOIuIAJd8AEYYHXvsBa2sm1UALwBK53eOsXXaeRcTNFWm01RB0+tBAQMZPPN4A== X-Received: by 2002:a05:6a20:2452:b0:1c4:936e:b8a2 with SMTP id adf61e73a8af0-1c4936f4f36mr895565637.27.1721987800315; Fri, 26 Jul 2024 02:56:40 -0700 (PDT) Received: from chenghaoyang-low.c.googlers.com.com (148.175.199.104.bc.googleusercontent.com. [104.199.175.148]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fed7c7f63asm28396665ad.46.2024.07.26.02.56.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jul 2024 02:56:39 -0700 (PDT) From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Konami Shu , Harvey Yang , Yunke Cao , Tomasz Figa Subject: [PATCH v6 6/7] libcamera: pipeline: Shift test pattern by 1 pixel left every frame Date: Fri, 26 Jul 2024 09:54:27 +0000 Message-ID: <20240726095626.3632402-7-chenghaoyang@chromium.org> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog In-Reply-To: <20240726095626.3632402-1-chenghaoyang@chromium.org> References: <20240726095626.3632402-1-chenghaoyang@chromium.org> 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 - This write the buffer every frame - Shifting makes the frame rate dropped from about 160 to 40 Patchset1->2 - Use constant instead of using a magic number Patchset2->3 - Make shiftLeft() private Signed-off-by: Konami Shu Co-developed-by: Harvey Yang Co-developed-by: Yunke Cao Co-developed-by: Tomasz Figa Change-Id: Ief9c1e520a4ffb4cf7d47d0b37ad756be40330ea --- .../pipeline/virtual/data/virtual.yaml | 4 +-- .../virtual/test_pattern_generator.cpp | 36 +++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/libcamera/pipeline/virtual/data/virtual.yaml b/src/libcamera/pipeline/virtual/data/virtual.yaml index 4eb239e24..b8956f2d4 100644 --- a/src/libcamera/pipeline/virtual/data/virtual.yaml +++ b/src/libcamera/pipeline/virtual/data/virtual.yaml @@ -28,8 +28,8 @@ model: "Virtual Video Device1" "Virtual2": supported_formats: - - width: 100 - height: 100 + - width: 400 + height: 300 test_pattern: "lines" location: "front" model: "Virtual Video Device2" diff --git a/src/libcamera/pipeline/virtual/test_pattern_generator.cpp b/src/libcamera/pipeline/virtual/test_pattern_generator.cpp index b142b522c..d685a4903 100644 --- a/src/libcamera/pipeline/virtual/test_pattern_generator.cpp +++ b/src/libcamera/pipeline/virtual/test_pattern_generator.cpp @@ -28,6 +28,9 @@ void TestPatternGenerator::generateFrame( auto planes = mappedFrameBuffer.planes(); + /* TODO: select whether to do shifting or not */ + shiftLeft(size); + /* Convert the template_ to the frame buffer */ int ret = libyuv::ARGBToNV12( template_.get(), /*src_stride_argb=*/size.width * kARGBSize, @@ -39,6 +42,39 @@ void TestPatternGenerator::generateFrame( } } +void TestPatternGenerator::shiftLeft(const Size &size) +{ + /* Store the first column temporarily */ + uint8_t firstColumn[size.height * kARGBSize]; + for (size_t h = 0; h < size.height; h++) { + unsigned int index = h * size.width * kARGBSize; + unsigned int index1 = h * kARGBSize; + firstColumn[index1] = template_[index]; + firstColumn[index1 + 1] = template_[index + 1]; + firstColumn[index1 + 2] = template_[index + 2]; + firstColumn[index1 + 3] = 0x00; + } + + /* Overwrite template_ */ + uint8_t *buf = template_.get(); + for (size_t h = 0; h < size.height; h++) { + for (size_t w = 0; w < size.width - 1; w++) { + /* Overwrite with the pixel on the right */ + unsigned int index = (h * size.width + w + 1) * kARGBSize; + *buf++ = template_[index]; // B + *buf++ = template_[index + 1]; // G + *buf++ = template_[index + 2]; // R + *buf++ = 0x00; // A + } + /* Overwrite the new last column with the original first column */ + unsigned int index1 = h * kARGBSize; + *buf++ = firstColumn[index1]; // B + *buf++ = firstColumn[index1 + 1]; // G + *buf++ = firstColumn[index1 + 2]; // R + *buf++ = 0x00; // A + } +} + std::unique_ptr ColorBarsGenerator::create() { return std::make_unique(); From patchwork Fri Jul 26 09:54:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 20708 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 991B3BDC71 for ; Fri, 26 Jul 2024 09:56:54 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 062D46337F; Fri, 26 Jul 2024 11:56:54 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="BGYI4Rpv"; dkim-atps=neutral Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id C743B63370 for ; Fri, 26 Jul 2024 11:56:44 +0200 (CEST) Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-1fc66fc35f2so3374985ad.0 for ; Fri, 26 Jul 2024 02:56:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1721987802; x=1722592602; 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=9A8+l53+qqbrN07C+fkaL32gTTEThCdVZP3ptj3Pu+Y=; b=BGYI4RpvruLsO+iKY6HtVtTNAxK+VEH0ZBm2+wh3QtEe0LirNbq88yVznaq6P2QFYh UvxQC4maM0rGwfdIQIKDixPnlQlFiGt3JbreS5llsd/CSB7ambtbSHp78QqEibI/4DOV f7XxzsEgKVsJN4yK4YKHyI+XW/O9QJX9QjR/Q= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721987802; x=1722592602; 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=9A8+l53+qqbrN07C+fkaL32gTTEThCdVZP3ptj3Pu+Y=; b=STsBqv3C9GTlUf4eus/06JxWDdnnpUm3LXuR2+dZykxn7IxldSBg4eCAp4iL/xbkUK dnnWn7Om4m2lOEsvvlJDtadefjVsXkj07PQj+IgqYgOWlPSNYwjMCVqe7Hfl8gyxOvon lMaeiPx6DJ722ZKu/ApVyD0AmMkeBfvfkmLvgQz3J0HurmP3D0KgJssRhS9AJBzVQWhN 8Mtm81BCB7lnb2gbywfipkJ1IMbFy7OWBRJyLs+NpbUttUB2aSFJnuawIzq9sTdkBI0Z X3jwHJTn2vtO24Hk6rwCpOZQz4r/8uLobbkEb9oZghlsih6vV3rnCLc1VQDCmhuWL7k+ BxLw== X-Gm-Message-State: AOJu0YxAPKWOVX/jldUZmyYPseVQOC7XqI+DgaJYfvTLq6M3BaE8dJY9 BkiXTL2nt1JNkuUMid6J2287ahLJ+jh7F5BFRRPiRIKn1c+z5ziyVzoKBGD9c9JbhJJHAzj99wO goQ== X-Google-Smtp-Source: AGHT+IFc48n9I0NoJRbU0vU3/6LcAeRZ/BxRwO7JXYDegDN87GzF5HfRCw2UvwzmUUzVW4XhOQrkeQ== X-Received: by 2002:a17:902:ea02:b0:1f4:a04e:8713 with SMTP id d9443c01a7336-1fed28728a6mr99994065ad.28.1721987802079; Fri, 26 Jul 2024 02:56:42 -0700 (PDT) Received: from chenghaoyang-low.c.googlers.com.com (148.175.199.104.bc.googleusercontent.com. [104.199.175.148]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fed7c7f63asm28396665ad.46.2024.07.26.02.56.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jul 2024 02:56:41 -0700 (PDT) From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Konami Shu , Harvey Yang , Yunke Cao , Tomasz Figa Subject: [PATCH v6 7/7] libcamera: pipeline: Load images Date: Fri, 26 Jul 2024 09:54:28 +0000 Message-ID: <20240726095626.3632402-8-chenghaoyang@chromium.org> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog In-Reply-To: <20240726095626.3632402-1-chenghaoyang@chromium.org> References: <20240726095626.3632402-1-chenghaoyang@chromium.org> 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 Change-Id: Iffb4fafeb926d70104c66ae0201435b465bc45e4 --- src/libcamera/pipeline/virtual/README.md | 18 +- .../pipeline/virtual/common_functions.cpp | 27 +++ .../pipeline/virtual/common_functions.h | 18 ++ .../pipeline/virtual/data/virtual.yaml | 30 ++-- .../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 ++- 14 files changed, 406 insertions(+), 59 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 27d6283df..5e21ce74a 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 000000000..207827ee0 --- /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 000000000..4203f9505 --- /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/data/virtual.yaml b/src/libcamera/pipeline/virtual/data/virtual.yaml index b8956f2d4..cb120a06b 100644 --- a/src/libcamera/pipeline/virtual/data/virtual.yaml +++ b/src/libcamera/pipeline/virtual/data/virtual.yaml @@ -13,7 +13,9 @@ frame_rates: - 70 - 80 - test_pattern: "lines" + frames: + path: "files/libcamera_short_anime/" + scale_mode: "fill" location: "front" model: "Virtual Video Device" "Virtual1": @@ -23,27 +25,27 @@ frame_rates: - 30 - 60 - test_pattern: + frames: + path: "files/bike-min.jpeg" + scale_mode: "contain" location: "back" model: "Virtual Video Device1" "Virtual2": supported_formats: - width: 400 height: 300 - test_pattern: "lines" + frames: + path: "lines" + scale_mode: "cover" location: "front" model: "Virtual Video Device2" "Virtual3": - supported_formats: - - width: 100 - height: 100 - - width: 800 - height: 600 - - width: 1920 - height: 1080 - frame_rates: - - 20 - - 30 location: "a" - model: "Virtual Video Device3" "Virtual4": + supported_formats: + - width: 100 + height: 101 +"Virtual5": + frames: + path: "files/chrome_anniversary.jpg" + scale_mode: "contain" diff --git a/src/libcamera/pipeline/virtual/frame_generator.h b/src/libcamera/pipeline/virtual/frame_generator.h index 9699af7a4..f69576b36 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 000000000..d374877ff --- /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 000000000..74468e075 --- /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 2e82e64cb..d56aedec4 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 032c0cd9d..46d1ab181 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 a377d8aa1..38ea460d5 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 d685a4903..8e18c97a7 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 ed8d4e43b..cebdd0141 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 5563c0f66..64732a485 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; @@ -191,7 +195,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); @@ -211,8 +215,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); } @@ -242,9 +246,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)); @@ -256,13 +261,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 c1ac4eb90..f41a4a906 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_; };