From patchwork Fri Jul 26 11:47:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Umang Jain X-Patchwork-Id: 20711 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 AEB9CBDC71 for ; Fri, 26 Jul 2024 11:47:34 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 4E0A963378; Fri, 26 Jul 2024 13:47:34 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="jyTvf4/T"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 8E8466336B for ; Fri, 26 Jul 2024 13:47:29 +0200 (CEST) Received: from localhost.localdomain (unknown [IPv6:2405:201:2015:f873:55f8:639e:8e9f:12ec]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 5D5378CC; Fri, 26 Jul 2024 13:46:44 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1721994405; bh=l+JMY3xVM7qlcAJq3/PvQ9triNKZiGSv4dbVblgMnQ0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jyTvf4/TIWIRRFndX3LgFENhhwhcyL5qqa48vLmQMuZGdykob0GOFGHB0HT5W6IuH ebVS9Rg1xPhxRnmbWktOnVaADCt0FPYiMmf1Z5FN1UQtSzuIghI2yE7om6UCjWSoNH bQZHNHYLAptYUUu62p9+h6hdGtPOn2gD6+2pi1AE= From: Umang Jain To: libcamera-devel@lists.libcamera.org Cc: Umang Jain , Paul Elder Subject: [PATCH v6 2/5] libcamera: converter: Add interface to support cropping capability Date: Fri, 26 Jul 2024 17:17:12 +0530 Message-ID: <20240726114715.226468-3-umang.jain@ideasonboard.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240726114715.226468-1-umang.jain@ideasonboard.com> References: <20240726114715.226468-1-umang.jain@ideasonboard.com> MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" If the converter has cropping capability, the interface should support it by providing appropriate virtual functions. Provide Feature::Crop in Feature enumeration for the same. Provide virtual setCrop() and getCropBounds() interfaces so that the converter can implement their own cropping functionality. The V4L2M2MConverter implements these interfaces of the Converter interface. Not all V4L2M2M converters will have cropping capability, hence Feature::Crop should be set while construction for all those converters. Signed-off-by: Umang Jain Reviewed-by: Paul Elder --- include/libcamera/internal/converter.h | 5 + .../internal/converter/converter_v4l2_m2m.h | 6 ++ src/libcamera/converter.cpp | 52 +++++++++++ .../converter/converter_v4l2_m2m.cpp | 92 +++++++++++++++++++ 4 files changed, 155 insertions(+) diff --git a/include/libcamera/internal/converter.h b/include/libcamera/internal/converter.h index 652ff519..853888f4 100644 --- a/include/libcamera/internal/converter.h +++ b/include/libcamera/internal/converter.h @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -35,6 +36,7 @@ class Converter public: enum class Feature { None = 0, + InputCrop = (1 << 0), }; using Features = Flags; @@ -63,6 +65,9 @@ public: virtual int queueBuffers(FrameBuffer *input, const std::map &outputs) = 0; + virtual int setInputCrop(const Stream *stream, Rectangle *rect); + virtual std::pair inputCropBounds(const Stream *stream); + Signal inputBufferReady; Signal outputBufferReady; diff --git a/include/libcamera/internal/converter/converter_v4l2_m2m.h b/include/libcamera/internal/converter/converter_v4l2_m2m.h index 91701dbe..5b69b14e 100644 --- a/include/libcamera/internal/converter/converter_v4l2_m2m.h +++ b/include/libcamera/internal/converter/converter_v4l2_m2m.h @@ -30,6 +30,7 @@ class Size; class SizeRange; class Stream; struct StreamConfiguration; +class Rectangle; class V4L2M2MDevice; class V4L2M2MConverter : public Converter @@ -57,6 +58,9 @@ public: int queueBuffers(FrameBuffer *input, const std::map &outputs); + int setInputCrop(const Stream *stream, Rectangle *rect); + std::pair inputCropBounds(const Stream *stream); + private: class V4L2M2MStream : protected Loggable { @@ -75,6 +79,8 @@ private: int queueBuffers(FrameBuffer *input, FrameBuffer *output); + int setSelection(unsigned int target, Rectangle *rect); + protected: std::string logPrefix() const override; diff --git a/src/libcamera/converter.cpp b/src/libcamera/converter.cpp index e2dbf5e0..7ab56b4c 100644 --- a/src/libcamera/converter.cpp +++ b/src/libcamera/converter.cpp @@ -11,6 +11,8 @@ #include +#include + #include "libcamera/internal/media_device.h" /** @@ -39,6 +41,8 @@ LOG_DEFINE_CATEGORY(Converter) * \brief Specify the features supported by the converter * \var Converter::Feature::None * \brief No extra features supported by the converter + * \var Converter::Feature::InputCrop + * \brief Cropping capability at input is supported by the converter */ /** @@ -161,6 +165,54 @@ Converter::~Converter() * \return 0 on success or a negative error code otherwise */ +/** + * \brief Set the crop rectangle \a rect for \a stream + * \param[in] stream Pointer to output stream + * \param[inout] rect The crop rectangle to be applied + * + * Set the crop rectangle \a rect for \a stream provided the converter supports + * cropping. The converter should have the Feature::InputCrop flag in this + * case. + * + * \return 0 on success or a negative error code otherwise + */ +int Converter::setInputCrop([[maybe_unused]] const Stream *stream, [[maybe_unused]] Rectangle *rect) +{ + if (!(features() & Feature::InputCrop)) { + LOG(Converter, Error) << "Converter doesn't support cropping capabilities"; + return -ENOTSUP; + } + + return 0; +} + +/** + * \brief Get the crop bounds \a stream + * \param[in] stream Pointer to output stream + * + * Get the minimum and maximum crop bounds for \a stream. The converter + * should supporting cropping (Feature::InputCrop). + * + * \return A std::pair containining minimum and maximum + * crop bound respectively. + */ +std::pair Converter::inputCropBounds([[maybe_unused]] const Stream *stream) +{ + const StreamConfiguration &config = stream->configuration(); + Rectangle rect; + + if (!(features() & Feature::InputCrop)) + LOG(Converter, Error) << "Converter doesn't support cropping capabilities"; + + /* + * This is base implementation for the Converter class, so just return + * the stream configured size as minimum and maximum crop bounds. + */ + rect.size() = config.size; + + return { rect, rect }; +} + /** * \var Converter::inputBufferReady * \brief A signal emitted when the input frame buffer completes diff --git a/src/libcamera/converter/converter_v4l2_m2m.cpp b/src/libcamera/converter/converter_v4l2_m2m.cpp index 4aeb7dd9..3295b988 100644 --- a/src/libcamera/converter/converter_v4l2_m2m.cpp +++ b/src/libcamera/converter/converter_v4l2_m2m.cpp @@ -155,6 +155,15 @@ int V4L2M2MConverter::V4L2M2MStream::queueBuffers(FrameBuffer *input, FrameBuffe return 0; } +int V4L2M2MConverter::V4L2M2MStream::setSelection(unsigned int target, Rectangle *rect) +{ + int ret = m2m_->output()->setSelection(target, rect); + if (ret < 0) + return ret; + + return 0; +} + std::string V4L2M2MConverter::V4L2M2MStream::logPrefix() const { return stream_->configuration().toString(); @@ -375,6 +384,85 @@ int V4L2M2MConverter::exportBuffers(const Stream *stream, unsigned int count, return iter->second->exportBuffers(count, buffers); } +/** + * \copydoc libcamera::Converter::setInputCrop + */ +int V4L2M2MConverter::setInputCrop(const Stream *stream, Rectangle *rect) +{ + if (!(features() & Feature::InputCrop)) + return -ENOTSUP; + + auto iter = streams_.find(stream); + if (iter == streams_.end()) + return -EINVAL; + + return iter->second->setSelection(V4L2_SEL_TGT_CROP, rect); +} + +/** + * \copydoc libcamera::Converter::inputCropBounds + */ +std::pair +V4L2M2MConverter::inputCropBounds(const Stream *stream) +{ + Rectangle minCrop; + Rectangle maxCrop; + int ret; + + if (!(features() & Feature::InputCrop)) { + LOG(Converter, Error) << "Input Crop functionality is not supported"; + return {}; + } + + minCrop.width = 1; + minCrop.height = 1; + maxCrop.width = UINT_MAX; + maxCrop.height = UINT_MAX; + + auto iter = streams_.find(stream); + if (iter == streams_.end()) { + /* + * No streams configured, return minimum and maximum crop + * bounds at initialization. + */ + ret = m2m_->output()->setSelection(V4L2_SEL_TGT_CROP, &minCrop); + if (ret) { + LOG(Converter, Error) << "Failed to query minimum crop bound" + << strerror(-ret); + return {}; + } + + ret = m2m_->output()->setSelection(V4L2_SEL_TGT_CROP, &maxCrop); + if (ret) { + LOG(Converter, Error) << "Failed to query maximum crop bound" + << strerror(-ret); + return {}; + } + + return { minCrop, maxCrop }; + } + + /* + * If the streams are configured, return bounds from according to + * stream configuration. + */ + ret = setInputCrop(stream, &minCrop); + if (ret) { + LOG(Converter, Error) << "Failed to query minimum crop bound" + << strerror(-ret); + return {}; + } + + ret = setInputCrop(stream, &maxCrop); + if (ret) { + LOG(Converter, Error) << "Failed to query maximum crop bound" + << strerror(-ret); + return {}; + } + + return { minCrop, maxCrop }; +} + /** * \copydoc libcamera::Converter::start */ @@ -448,6 +536,10 @@ int V4L2M2MConverter::queueBuffers(FrameBuffer *input, return 0; } +/* + * \todo: This should be extended to include Feature::Flag to denote + * what each converter supports feature-wise. + */ static std::initializer_list compatibles = { "mtk-mdp", "pxp",