From patchwork Fri Feb 28 03:29:08 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Niklas_S=C3=B6derlund?= X-Patchwork-Id: 2911 Return-Path: Received: from bin-mail-out-05.binero.net (bin-mail-out-05.binero.net [195.74.38.228]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id E377760428 for ; Fri, 28 Feb 2020 04:29:35 +0100 (CET) X-Halon-ID: 8a5a0988-59da-11ea-9f85-005056917a89 Authorized-sender: niklas@soderlund.pp.se Received: from bismarck.berto.se (p4fca2392.dip0.t-ipconnect.de [79.202.35.146]) by bin-vsp-out-01.atm.binero.net (Halon) with ESMTPA id 8a5a0988-59da-11ea-9f85-005056917a89; Fri, 28 Feb 2020 04:29:34 +0100 (CET) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Fri, 28 Feb 2020 04:29:08 +0100 Message-Id: <20200228032913.497826-2-niklas.soderlund@ragnatech.se> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200228032913.497826-1-niklas.soderlund@ragnatech.se> References: <20200228032913.497826-1-niklas.soderlund@ragnatech.se> MIME-Version: 1.0 Subject: [libcamera-devel] [RFC 1/6] libcamera: Use PixelFormat instead of unsigned int where appropriate X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 28 Feb 2020 03:29:36 -0000 Use the PixelFormat instead of unsigned int where a pixel format is to be used. PixelFormat is defined as an unsigned int but is about to be turned into a class to add functionality. There is no functional change in this patch. Signed-off-by: Niklas Söderlund Reviewed-by: Laurent Pinchart --- src/cam/main.cpp | 2 +- src/libcamera/pipeline/ipu3/ipu3.cpp | 2 +- src/libcamera/pipeline/uvcvideo.cpp | 4 ++-- src/libcamera/pipeline/vimc.cpp | 2 +- src/qcam/format_converter.cpp | 2 +- src/qcam/format_converter.h | 6 ++++-- src/qcam/viewfinder.cpp | 2 +- src/qcam/viewfinder.h | 6 ++++-- 8 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/cam/main.cpp b/src/cam/main.cpp index a38cca959aca05ff..ad4be55fc114fe22 100644 --- a/src/cam/main.cpp +++ b/src/cam/main.cpp @@ -281,7 +281,7 @@ int CamApp::infoConfiguration() std::cout << index << ": " << cfg.toString() << std::endl; const StreamFormats &formats = cfg.formats(); - for (unsigned int pixelformat : formats.pixelformats()) { + for (PixelFormat pixelformat : formats.pixelformats()) { std::cout << " * Pixelformat: 0x" << std::hex << std::setw(8) << pixelformat << " " << formats.range(pixelformat).toString() diff --git a/src/libcamera/pipeline/ipu3/ipu3.cpp b/src/libcamera/pipeline/ipu3/ipu3.cpp index 387bb070b5050e77..33f97b340716abd0 100644 --- a/src/libcamera/pipeline/ipu3/ipu3.cpp +++ b/src/libcamera/pipeline/ipu3/ipu3.cpp @@ -349,7 +349,7 @@ CameraConfiguration::Status IPU3CameraConfiguration::validate() for (unsigned int i = 0; i < config_.size(); ++i) { StreamConfiguration &cfg = config_[i]; - const unsigned int pixelFormat = cfg.pixelFormat; + const PixelFormat pixelFormat = cfg.pixelFormat; const Size size = cfg.size; const IPU3Stream *stream; diff --git a/src/libcamera/pipeline/uvcvideo.cpp b/src/libcamera/pipeline/uvcvideo.cpp index 29afb121aa46d5d4..cf419e8b25a87389 100644 --- a/src/libcamera/pipeline/uvcvideo.cpp +++ b/src/libcamera/pipeline/uvcvideo.cpp @@ -107,10 +107,10 @@ CameraConfiguration::Status UVCCameraConfiguration::validate() StreamConfiguration &cfg = config_[0]; const StreamFormats &formats = cfg.formats(); - const unsigned int pixelFormat = cfg.pixelFormat; + const PixelFormat pixelFormat = cfg.pixelFormat; const Size size = cfg.size; - const std::vector pixelFormats = formats.pixelformats(); + const std::vector pixelFormats = formats.pixelformats(); auto iter = std::find(pixelFormats.begin(), pixelFormats.end(), pixelFormat); if (iter == pixelFormats.end()) { cfg.pixelFormat = pixelFormats.front(); diff --git a/src/libcamera/pipeline/vimc.cpp b/src/libcamera/pipeline/vimc.cpp index 5d3d12fef30b01b5..93d89729e1faa29f 100644 --- a/src/libcamera/pipeline/vimc.cpp +++ b/src/libcamera/pipeline/vimc.cpp @@ -177,7 +177,7 @@ CameraConfiguration *PipelineHandlerVimc::generateConfiguration(Camera *camera, ImageFormats formats; - for (unsigned int pixelformat : pixelformats) { + for (PixelFormat pixelformat : pixelformats) { /* The scaler hardcodes a x3 scale-up ratio. */ std::vector sizes{ SizeRange{ 48, 48, 4096, 2160 } diff --git a/src/qcam/format_converter.cpp b/src/qcam/format_converter.cpp index 383d482231400a44..368cb43fbf17ae4d 100644 --- a/src/qcam/format_converter.cpp +++ b/src/qcam/format_converter.cpp @@ -27,7 +27,7 @@ #define CLIP(x) CLAMP(x,0,255) #endif -int FormatConverter::configure(unsigned int format, unsigned int width, +int FormatConverter::configure(libcamera::PixelFormat format, unsigned int width, unsigned int height) { switch (format) { diff --git a/src/qcam/format_converter.h b/src/qcam/format_converter.h index 391e6a44d4ba7d4b..ff488b994ade3c3e 100644 --- a/src/qcam/format_converter.h +++ b/src/qcam/format_converter.h @@ -9,12 +9,14 @@ #include +#include + class QImage; class FormatConverter { public: - int configure(unsigned int format, unsigned int width, + int configure(libcamera::PixelFormat format, unsigned int width, unsigned int height); void convert(const unsigned char *src, size_t size, QImage *dst); @@ -31,7 +33,7 @@ private: void convertRGB(const unsigned char *src, unsigned char *dst); void convertYUV(const unsigned char *src, unsigned char *dst); - unsigned int format_; + libcamera::PixelFormat format_; unsigned int width_; unsigned int height_; diff --git a/src/qcam/viewfinder.cpp b/src/qcam/viewfinder.cpp index d51eebb10aef8663..0ebb8edd49efd1b1 100644 --- a/src/qcam/viewfinder.cpp +++ b/src/qcam/viewfinder.cpp @@ -44,7 +44,7 @@ QImage ViewFinder::getCurrentImage() return image_->copy(); } -int ViewFinder::setFormat(unsigned int format, unsigned int width, +int ViewFinder::setFormat(libcamera::PixelFormat format, unsigned int width, unsigned int height) { int ret; diff --git a/src/qcam/viewfinder.h b/src/qcam/viewfinder.h index 2ba28b60345b0cb3..2668aa4457657ef9 100644 --- a/src/qcam/viewfinder.h +++ b/src/qcam/viewfinder.h @@ -10,6 +10,8 @@ #include #include +#include + #include "format_converter.h" class QImage; @@ -20,7 +22,7 @@ public: ViewFinder(QWidget *parent); ~ViewFinder(); - int setFormat(unsigned int format, unsigned int width, + int setFormat(libcamera::PixelFormat format, unsigned int width, unsigned int height); void display(const unsigned char *rgb, size_t size); @@ -31,7 +33,7 @@ protected: QSize sizeHint() const override; private: - unsigned int format_; + libcamera::PixelFormat format_; unsigned int width_; unsigned int height_; From patchwork Fri Feb 28 03:29:09 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Niklas_S=C3=B6derlund?= X-Patchwork-Id: 2912 Return-Path: Received: from vsp-unauthed02.binero.net (vsp-unauthed02.binero.net [195.74.38.227]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id B87BE62734 for ; Fri, 28 Feb 2020 04:29:37 +0100 (CET) X-Halon-ID: 8ac78155-59da-11ea-9f85-005056917a89 Authorized-sender: niklas@soderlund.pp.se Received: from bismarck.berto.se (p4fca2392.dip0.t-ipconnect.de [79.202.35.146]) by bin-vsp-out-01.atm.binero.net (Halon) with ESMTPA id 8ac78155-59da-11ea-9f85-005056917a89; Fri, 28 Feb 2020 04:29:36 +0100 (CET) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Fri, 28 Feb 2020 04:29:09 +0100 Message-Id: <20200228032913.497826-3-niklas.soderlund@ragnatech.se> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200228032913.497826-1-niklas.soderlund@ragnatech.se> References: <20200228032913.497826-1-niklas.soderlund@ragnatech.se> MIME-Version: 1.0 Subject: [libcamera-devel] [RFC 2/6] libcamera: formats: Turn ImageFormats into a template X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 28 Feb 2020 03:29:38 -0000 The ImageFormats class is used to carry both V4L2 pixelformats and mbus codes as keys to lookup different supported sizes. The reason for this is that both V4L2 pixelformats and mbus codes are unsigned integers and the keyword PixelFormat is currently defined as such. Going forward PixelFormat will be reworked into a class so their is a need to have ImageFormats instances with two different key types, turn it into an template and update all sites to use unsigned int where a mbus code is the key and PixelFormat otherwise. Signed-off-by: Niklas Söderlund --- src/libcamera/camera_sensor.cpp | 2 +- src/libcamera/formats.cpp | 19 +++++++++++++------ src/libcamera/include/formats.h | 11 ++++++----- src/libcamera/include/v4l2_subdevice.h | 2 +- src/libcamera/include/v4l2_videodevice.h | 2 +- src/libcamera/pipeline/uvcvideo.cpp | 2 +- src/libcamera/pipeline/vimc.cpp | 2 +- src/libcamera/v4l2_subdevice.cpp | 4 ++-- src/libcamera/v4l2_videodevice.cpp | 4 ++-- test/v4l2_subdevice/list_formats.cpp | 2 +- 10 files changed, 29 insertions(+), 21 deletions(-) diff --git a/src/libcamera/camera_sensor.cpp b/src/libcamera/camera_sensor.cpp index 2219a43074362834..23fdafb690117d91 100644 --- a/src/libcamera/camera_sensor.cpp +++ b/src/libcamera/camera_sensor.cpp @@ -131,7 +131,7 @@ int CameraSensor::init() properties_.set(properties::Rotation, propertyValue); /* Enumerate and cache media bus codes and sizes. */ - const ImageFormats formats = subdev_->formats(0); + const ImageFormats formats = subdev_->formats(0); if (formats.isEmpty()) { LOG(CameraSensor, Error) << "No image format found"; return -EINVAL; diff --git a/src/libcamera/formats.cpp b/src/libcamera/formats.cpp index 5f6552a4e06c5231..98817deee2b54c84 100644 --- a/src/libcamera/formats.cpp +++ b/src/libcamera/formats.cpp @@ -39,7 +39,8 @@ namespace libcamera { * \return 0 on success or a negative error code otherwise * \retval -EEXIST The format is already described */ -int ImageFormats::addFormat(unsigned int format, const std::vector &sizes) +template +int ImageFormats::addFormat(T format, const std::vector &sizes) { if (data_.find(format) != data_.end()) return -EEXIST; @@ -53,7 +54,8 @@ int ImageFormats::addFormat(unsigned int format, const std::vector &s * \brief Check if the list of devices supported formats is empty * \return True if the list of supported formats is empty */ -bool ImageFormats::isEmpty() const +template +bool ImageFormats::isEmpty() const { return data_.empty(); } @@ -62,9 +64,10 @@ bool ImageFormats::isEmpty() const * \brief Retrieve a list of all supported image formats * \return List of pixel formats or media bus codes */ -std::vector ImageFormats::formats() const +template +std::vector ImageFormats::formats() const { - std::vector formats; + std::vector formats; formats.reserve(data_.size()); /* \todo: Should this be cached instead of computed each time? */ @@ -84,7 +87,8 @@ std::vector ImageFormats::formats() const * \return The list of image sizes supported for \a format, or an empty list if * the format is not supported */ -const std::vector &ImageFormats::sizes(unsigned int format) const +template +const std::vector &ImageFormats::sizes(T format) const { static const std::vector empty; @@ -99,9 +103,12 @@ const std::vector &ImageFormats::sizes(unsigned int format) const * \brief Retrieve the map that associates formats to image sizes * \return The map that associates formats to image sizes */ -const std::map> &ImageFormats::data() const +template +const std::map> &ImageFormats::data() const { return data_; } +template class ImageFormats; + } /* namespace libcamera */ diff --git a/src/libcamera/include/formats.h b/src/libcamera/include/formats.h index f43bc8c004f690b4..566be9c09550a830 100644 --- a/src/libcamera/include/formats.h +++ b/src/libcamera/include/formats.h @@ -15,18 +15,19 @@ namespace libcamera { +template class ImageFormats { public: - int addFormat(unsigned int format, const std::vector &sizes); + int addFormat(T format, const std::vector &sizes); bool isEmpty() const; - std::vector formats() const; - const std::vector &sizes(unsigned int format) const; - const std::map> &data() const; + std::vector formats() const; + const std::vector &sizes(T format) const; + const std::map> &data() const; private: - std::map> data_; + std::map> data_; }; } /* namespace libcamera */ diff --git a/src/libcamera/include/v4l2_subdevice.h b/src/libcamera/include/v4l2_subdevice.h index 9c077674f997dae6..53c73617efe4b860 100644 --- a/src/libcamera/include/v4l2_subdevice.h +++ b/src/libcamera/include/v4l2_subdevice.h @@ -44,7 +44,7 @@ public: int setCrop(unsigned int pad, Rectangle *rect); int setCompose(unsigned int pad, Rectangle *rect); - ImageFormats formats(unsigned int pad); + ImageFormats formats(unsigned int pad); int getFormat(unsigned int pad, V4L2SubdeviceFormat *format); int setFormat(unsigned int pad, V4L2SubdeviceFormat *format); diff --git a/src/libcamera/include/v4l2_videodevice.h b/src/libcamera/include/v4l2_videodevice.h index fcf072641420dacf..982df9d2f918e49c 100644 --- a/src/libcamera/include/v4l2_videodevice.h +++ b/src/libcamera/include/v4l2_videodevice.h @@ -180,7 +180,7 @@ public: int getFormat(V4L2DeviceFormat *format); int setFormat(V4L2DeviceFormat *format); - ImageFormats formats(); + ImageFormats formats(); int setCrop(Rectangle *rect); int setCompose(Rectangle *rect); diff --git a/src/libcamera/pipeline/uvcvideo.cpp b/src/libcamera/pipeline/uvcvideo.cpp index cf419e8b25a87389..8efd188e75a3d135 100644 --- a/src/libcamera/pipeline/uvcvideo.cpp +++ b/src/libcamera/pipeline/uvcvideo.cpp @@ -155,7 +155,7 @@ CameraConfiguration *PipelineHandlerUVC::generateConfiguration(Camera *camera, if (roles.empty()) return config; - ImageFormats v4l2formats = data->video_->formats(); + ImageFormats v4l2formats = data->video_->formats(); StreamFormats formats(v4l2formats.data()); StreamConfiguration cfg(formats); diff --git a/src/libcamera/pipeline/vimc.cpp b/src/libcamera/pipeline/vimc.cpp index 93d89729e1faa29f..9fbe33c626e327d4 100644 --- a/src/libcamera/pipeline/vimc.cpp +++ b/src/libcamera/pipeline/vimc.cpp @@ -175,7 +175,7 @@ CameraConfiguration *PipelineHandlerVimc::generateConfiguration(Camera *camera, if (roles.empty()) return config; - ImageFormats formats; + ImageFormats formats; for (PixelFormat pixelformat : pixelformats) { /* The scaler hardcodes a x3 scale-up ratio. */ diff --git a/src/libcamera/v4l2_subdevice.cpp b/src/libcamera/v4l2_subdevice.cpp index f2bcd7f73c5c75a1..bec14f80c960d854 100644 --- a/src/libcamera/v4l2_subdevice.cpp +++ b/src/libcamera/v4l2_subdevice.cpp @@ -155,9 +155,9 @@ int V4L2Subdevice::setCompose(unsigned int pad, Rectangle *rect) * * \return A list of the supported device formats */ -ImageFormats V4L2Subdevice::formats(unsigned int pad) +ImageFormats V4L2Subdevice::formats(unsigned int pad) { - ImageFormats formats; + ImageFormats formats; if (pad >= entity_->pads().size()) { LOG(V4L2, Error) << "Invalid pad: " << pad; diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp index 99470ce11421c77c..f84bd00570afa38c 100644 --- a/src/libcamera/v4l2_videodevice.cpp +++ b/src/libcamera/v4l2_videodevice.cpp @@ -837,9 +837,9 @@ int V4L2VideoDevice::setFormatSingleplane(V4L2DeviceFormat *format) * * \return A list of the supported video device formats */ -ImageFormats V4L2VideoDevice::formats() +ImageFormats V4L2VideoDevice::formats() { - ImageFormats formats; + ImageFormats formats; for (unsigned int pixelformat : enumPixelformats()) { std::vector sizes = enumSizes(pixelformat); diff --git a/test/v4l2_subdevice/list_formats.cpp b/test/v4l2_subdevice/list_formats.cpp index 067dc5ed30f4edd9..201a294c72a09b4d 100644 --- a/test/v4l2_subdevice/list_formats.cpp +++ b/test/v4l2_subdevice/list_formats.cpp @@ -47,7 +47,7 @@ void ListFormatsTest::printFormats(unsigned int pad, int ListFormatsTest::run() { /* List all formats available on existing "Scaler" pads. */ - ImageFormats formats; + ImageFormats formats; formats = scaler_->formats(0); if (formats.isEmpty()) { From patchwork Fri Feb 28 03:29:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Niklas_S=C3=B6derlund?= X-Patchwork-Id: 2913 Return-Path: Received: from bin-mail-out-06.binero.net (bin-mail-out-06.binero.net [195.74.38.229]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 414246271F for ; Fri, 28 Feb 2020 04:29:39 +0100 (CET) X-Halon-ID: 8bdcdf91-59da-11ea-9f85-005056917a89 Authorized-sender: niklas@soderlund.pp.se Received: from bismarck.berto.se (p4fca2392.dip0.t-ipconnect.de [79.202.35.146]) by bin-vsp-out-01.atm.binero.net (Halon) with ESMTPA id 8bdcdf91-59da-11ea-9f85-005056917a89; Fri, 28 Feb 2020 04:29:37 +0100 (CET) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Fri, 28 Feb 2020 04:29:10 +0100 Message-Id: <20200228032913.497826-4-niklas.soderlund@ragnatech.se> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200228032913.497826-1-niklas.soderlund@ragnatech.se> References: <20200228032913.497826-1-niklas.soderlund@ragnatech.se> MIME-Version: 1.0 Subject: [libcamera-devel] [RFC 3/6] libcamera: PixelFormat: Turn into a class X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 28 Feb 2020 03:29:39 -0000 Create a class to represent the pixel formats. This is done to prepare to add support for modifiers for the formats. One bonus with this change it that it's allows it to make it impossible for other then designated classes (V4L2VideoDevice and V4L2CameraProxy) to create PixelFormat instances from a V4L2 fourcc limiting the risk of users of the class to mix the two fourcc namespaces unintentionally. The patch is unfortunately quiet large as it have to touch a lot of different ares of the code to simultaneously switch to the new class based PixelFormat implementation. Signed-off-by: Niklas Söderlund --- include/libcamera/pixelformats.h | 40 +++++++- src/cam/main.cpp | 4 +- src/libcamera/formats.cpp | 3 + src/libcamera/pipeline/ipu3/ipu3.cpp | 6 +- src/libcamera/pipeline/rkisp1/rkisp1.cpp | 22 ++-- src/libcamera/pipeline/uvcvideo.cpp | 4 +- src/libcamera/pipeline/vimc.cpp | 12 +-- src/libcamera/pixelformats.cpp | 124 +++++++++++++++++++++++ src/libcamera/stream.cpp | 6 +- src/libcamera/v4l2_videodevice.cpp | 101 +----------------- src/qcam/format_converter.cpp | 2 +- src/qcam/viewfinder.cpp | 2 +- src/v4l2/v4l2_camera_proxy.cpp | 49 ++++----- test/stream/stream_formats.cpp | 33 +++--- 14 files changed, 236 insertions(+), 172 deletions(-) diff --git a/include/libcamera/pixelformats.h b/include/libcamera/pixelformats.h index 6e25b8d8b76e5961..f0951e983192d5e8 100644 --- a/include/libcamera/pixelformats.h +++ b/include/libcamera/pixelformats.h @@ -7,11 +7,49 @@ #ifndef __LIBCAMERA_PIXEL_FORMATS_H__ #define __LIBCAMERA_PIXEL_FORMATS_H__ +#include #include +#include + +/* Needs to be forward declared in the global namespace. */ +class V4L2CameraProxy; namespace libcamera { -using PixelFormat = uint32_t; +struct PixelFormatEntry; + +class PixelFormat +{ +public: + PixelFormat(); + PixelFormat(const PixelFormat &other); + explicit PixelFormat(uint32_t drm_fourcc, const std::set &drm_modifiers); + + PixelFormat &operator=(const PixelFormat &other); + + bool operator==(const PixelFormat &other) const; + bool operator!=(const PixelFormat &other) const; + bool operator<(const PixelFormat &other) const; + + uint32_t v4l2() const; + uint32_t fourcc() const; + const std::set &modifiers() const; + +protected: + /* Needed so V4L2VideoDevice can create PixelFormat from V4L2 fourcc. */ + friend class V4L2VideoDevice; + + /* Needed so V4L2CameraProxy can create PixelFormat from V4L2 fourcc. */ + friend V4L2CameraProxy; + + explicit PixelFormat(uint32_t v4l2_fourcc); + +private: + const PixelFormatEntry *fromDRM(uint32_t drm_fourcc, const std::set &drm_modifiers) const; + const PixelFormatEntry *fromV4L2(uint32_t v4l2_fourcc) const; + + const PixelFormatEntry *format_; +}; } /* namespace libcamera */ diff --git a/src/cam/main.cpp b/src/cam/main.cpp index ad4be55fc114fe22..c8ef79daea37d8b6 100644 --- a/src/cam/main.cpp +++ b/src/cam/main.cpp @@ -249,7 +249,7 @@ int CamApp::prepareConfig() /* TODO: Translate 4CC string to ID. */ if (opt.isSet("pixelformat")) - cfg.pixelFormat = opt["pixelformat"]; + cfg.pixelFormat = PixelFormat(opt["pixelformat"], {}); } } @@ -283,7 +283,7 @@ int CamApp::infoConfiguration() const StreamFormats &formats = cfg.formats(); for (PixelFormat pixelformat : formats.pixelformats()) { std::cout << " * Pixelformat: 0x" << std::hex - << std::setw(8) << pixelformat << " " + << std::setw(8) << pixelformat.fourcc() << " " << formats.range(pixelformat).toString() << std::endl; diff --git a/src/libcamera/formats.cpp b/src/libcamera/formats.cpp index 98817deee2b54c84..e3a89121e3c60151 100644 --- a/src/libcamera/formats.cpp +++ b/src/libcamera/formats.cpp @@ -9,6 +9,8 @@ #include +#include + /** * \file formats.h * \brief Types and helper methods to handle libcamera image formats @@ -110,5 +112,6 @@ const std::map> &ImageFormats::data() const } template class ImageFormats; +template class ImageFormats; } /* namespace libcamera */ diff --git a/src/libcamera/pipeline/ipu3/ipu3.cpp b/src/libcamera/pipeline/ipu3/ipu3.cpp index 33f97b340716abd0..71ac0ae33921f548 100644 --- a/src/libcamera/pipeline/ipu3/ipu3.cpp +++ b/src/libcamera/pipeline/ipu3/ipu3.cpp @@ -247,7 +247,7 @@ IPU3CameraConfiguration::IPU3CameraConfiguration(Camera *camera, void IPU3CameraConfiguration::adjustStream(StreamConfiguration &cfg, bool scale) { /* The only pixel format the driver supports is NV12. */ - cfg.pixelFormat = DRM_FORMAT_NV12; + cfg.pixelFormat = PixelFormat(DRM_FORMAT_NV12, {}); if (scale) { /* @@ -402,7 +402,7 @@ CameraConfiguration *PipelineHandlerIPU3::generateConfiguration(Camera *camera, StreamConfiguration cfg = {}; IPU3Stream *stream = nullptr; - cfg.pixelFormat = DRM_FORMAT_NV12; + cfg.pixelFormat = PixelFormat(DRM_FORMAT_NV12, {}); switch (role) { case StreamRole::StillCapture: @@ -1142,7 +1142,7 @@ int ImgUDevice::configureOutput(ImgUOutput *output, return 0; V4L2DeviceFormat outputFormat = {}; - outputFormat.fourcc = dev->toV4L2Fourcc(DRM_FORMAT_NV12); + outputFormat.fourcc = PixelFormat(DRM_FORMAT_NV12, {}).v4l2(); outputFormat.size = cfg.size; outputFormat.planesCount = 2; diff --git a/src/libcamera/pipeline/rkisp1/rkisp1.cpp b/src/libcamera/pipeline/rkisp1/rkisp1.cpp index 13433b216747cb8b..323fa3596c6ee242 100644 --- a/src/libcamera/pipeline/rkisp1/rkisp1.cpp +++ b/src/libcamera/pipeline/rkisp1/rkisp1.cpp @@ -433,14 +433,14 @@ RkISP1CameraConfiguration::RkISP1CameraConfiguration(Camera *camera, CameraConfiguration::Status RkISP1CameraConfiguration::validate() { - static const std::array formats{ - DRM_FORMAT_YUYV, - DRM_FORMAT_YVYU, - DRM_FORMAT_VYUY, - DRM_FORMAT_NV16, - DRM_FORMAT_NV61, - DRM_FORMAT_NV21, - DRM_FORMAT_NV12, + static const std::array formats{ + PixelFormat(DRM_FORMAT_YUYV, {}), + PixelFormat(DRM_FORMAT_YVYU, {}), + PixelFormat(DRM_FORMAT_VYUY, {}), + PixelFormat(DRM_FORMAT_NV16, {}), + PixelFormat(DRM_FORMAT_NV61, {}), + PixelFormat(DRM_FORMAT_NV21, {}), + PixelFormat(DRM_FORMAT_NV12, {}), /* \todo Add support for 8-bit greyscale to DRM formats */ }; @@ -462,7 +462,7 @@ CameraConfiguration::Status RkISP1CameraConfiguration::validate() if (std::find(formats.begin(), formats.end(), cfg.pixelFormat) == formats.end()) { LOG(RkISP1, Debug) << "Adjusting format to NV12"; - cfg.pixelFormat = DRM_FORMAT_NV12, + cfg.pixelFormat = PixelFormat(DRM_FORMAT_NV12, {}); status = Adjusted; } @@ -541,7 +541,7 @@ CameraConfiguration *PipelineHandlerRkISP1::generateConfiguration(Camera *camera return config; StreamConfiguration cfg{}; - cfg.pixelFormat = DRM_FORMAT_NV12; + cfg.pixelFormat = PixelFormat(DRM_FORMAT_NV12, {}); cfg.size = data->sensor_->resolution(); config->addConfiguration(cfg); @@ -796,7 +796,7 @@ int PipelineHandlerRkISP1::start(Camera *camera) /* Inform IPA of stream configuration and sensor controls. */ std::map streamConfig; streamConfig[0] = { - .pixelFormat = data->stream_.configuration().pixelFormat, + .pixelFormat = data->stream_.configuration().pixelFormat.fourcc(), .size = data->stream_.configuration().size, }; diff --git a/src/libcamera/pipeline/uvcvideo.cpp b/src/libcamera/pipeline/uvcvideo.cpp index 8efd188e75a3d135..5f3e52f691aaeae4 100644 --- a/src/libcamera/pipeline/uvcvideo.cpp +++ b/src/libcamera/pipeline/uvcvideo.cpp @@ -115,8 +115,8 @@ CameraConfiguration::Status UVCCameraConfiguration::validate() if (iter == pixelFormats.end()) { cfg.pixelFormat = pixelFormats.front(); LOG(UVC, Debug) - << "Adjusting pixel format from " << pixelFormat - << " to " << cfg.pixelFormat; + << "Adjusting pixel format from " << pixelFormat.fourcc() + << " to " << cfg.pixelFormat.fourcc(); status = Adjusted; } diff --git a/src/libcamera/pipeline/vimc.cpp b/src/libcamera/pipeline/vimc.cpp index 9fbe33c626e327d4..a591c424919b0783 100644 --- a/src/libcamera/pipeline/vimc.cpp +++ b/src/libcamera/pipeline/vimc.cpp @@ -106,10 +106,10 @@ private: namespace { -constexpr std::array pixelformats{ - DRM_FORMAT_RGB888, - DRM_FORMAT_BGR888, - DRM_FORMAT_BGRA8888, +static const std::array pixelformats{ + PixelFormat(DRM_FORMAT_RGB888, {}), + PixelFormat(DRM_FORMAT_BGR888, {}), + PixelFormat(DRM_FORMAT_BGRA8888, {}), }; } /* namespace */ @@ -138,7 +138,7 @@ CameraConfiguration::Status VimcCameraConfiguration::validate() if (std::find(pixelformats.begin(), pixelformats.end(), cfg.pixelFormat) == pixelformats.end()) { LOG(VIMC, Debug) << "Adjusting format to RGB24"; - cfg.pixelFormat = DRM_FORMAT_BGR888; + cfg.pixelFormat = PixelFormat(DRM_FORMAT_BGR888, {}); status = Adjusted; } @@ -187,7 +187,7 @@ CameraConfiguration *PipelineHandlerVimc::generateConfiguration(Camera *camera, StreamConfiguration cfg(formats.data()); - cfg.pixelFormat = DRM_FORMAT_BGR888; + cfg.pixelFormat = PixelFormat(DRM_FORMAT_BGR888, {}); cfg.size = { 1920, 1080 }; cfg.bufferCount = 4; diff --git a/src/libcamera/pixelformats.cpp b/src/libcamera/pixelformats.cpp index c03335400b709d9b..b2aacbc39b9ca16a 100644 --- a/src/libcamera/pixelformats.cpp +++ b/src/libcamera/pixelformats.cpp @@ -7,6 +7,13 @@ #include +#include + +#include +#include + +#include "log.h" + /** * \file pixelformats.h * \brief libcamera pixel formats @@ -14,6 +21,8 @@ namespace libcamera { +LOG_DEFINE_CATEGORY(PixelFormats) + /** * \typedef PixelFormat * \brief libcamera image pixel format @@ -25,4 +34,119 @@ namespace libcamera { * \todo Add support for format modifiers */ +struct PixelFormatEntry { + uint32_t v4l2; + uint32_t drm; + std::set modifiers; +}; + +static const std::vector pixelFormats = { + /* Invalid format, important to be first in list. */ + { .v4l2 = 0, .drm = DRM_FORMAT_INVALID, .modifiers = {} }, + /* RGB formats. */ + { .v4l2 = V4L2_PIX_FMT_RGB24, .drm = DRM_FORMAT_BGR888, .modifiers = {} }, + { .v4l2 = V4L2_PIX_FMT_BGR24, .drm = DRM_FORMAT_RGB888, .modifiers = {} }, + { .v4l2 = V4L2_PIX_FMT_ARGB32, .drm = DRM_FORMAT_BGRA8888, .modifiers = {} }, + /* YUV packed formats. */ + { .v4l2 = V4L2_PIX_FMT_YUYV, .drm = DRM_FORMAT_YUYV, .modifiers = {} }, + { .v4l2 = V4L2_PIX_FMT_YVYU, .drm = DRM_FORMAT_YVYU, .modifiers = {} }, + { .v4l2 = V4L2_PIX_FMT_UYVY, .drm = DRM_FORMAT_UYVY, .modifiers = {} }, + { .v4l2 = V4L2_PIX_FMT_VYUY, .drm = DRM_FORMAT_VYUY, .modifiers = {} }, + /* YUY planar formats. */ + { .v4l2 = V4L2_PIX_FMT_NV16, .drm = DRM_FORMAT_NV16, .modifiers = {} }, + { .v4l2 = V4L2_PIX_FMT_NV16M, .drm = DRM_FORMAT_NV16, .modifiers = {} }, + { .v4l2 = V4L2_PIX_FMT_NV61, .drm = DRM_FORMAT_NV61, .modifiers = {} }, + { .v4l2 = V4L2_PIX_FMT_NV61M, .drm = DRM_FORMAT_NV61, .modifiers = {} }, + { .v4l2 = V4L2_PIX_FMT_NV12, .drm = DRM_FORMAT_NV12, .modifiers = {} }, + { .v4l2 = V4L2_PIX_FMT_NV12M, .drm = DRM_FORMAT_NV12, .modifiers = {} }, + { .v4l2 = V4L2_PIX_FMT_NV21, .drm = DRM_FORMAT_NV21, .modifiers = {} }, + { .v4l2 = V4L2_PIX_FMT_NV21M, .drm = DRM_FORMAT_NV21, .modifiers = {} }, + /* Compressed formats. */ + { .v4l2 = V4L2_PIX_FMT_MJPEG, .drm = DRM_FORMAT_MJPEG, .modifiers = {} }, +}; + +PixelFormat::PixelFormat() + : format_(&pixelFormats[0]) +{ +} + +PixelFormat::PixelFormat(const PixelFormat &other) + : format_(other.format_) +{ +} + +PixelFormat::PixelFormat(uint32_t drm_fourcc, const std::set &drm_modifiers) + : format_(fromDRM(drm_fourcc, drm_modifiers)) +{ +} + +PixelFormat::PixelFormat(uint32_t v4l2_fourcc) + : format_(fromV4L2(v4l2_fourcc)) +{ +} + +PixelFormat &PixelFormat::operator=(const PixelFormat &other) +{ + format_ = other.format_; + + return *this; +} + +bool PixelFormat::operator==(const PixelFormat &other) const +{ + return format_ == other.format_; +} + +bool PixelFormat::operator!=(const PixelFormat &other) const +{ + return format_ != other.format_; +} + +bool PixelFormat::operator<(const PixelFormat &other) const +{ + return format_ > other.format_; +} + +uint32_t PixelFormat::v4l2() const +{ + return format_->v4l2; +} + +uint32_t PixelFormat::fourcc() const +{ + return format_->drm; +} + +const std::set &PixelFormat::modifiers() const +{ + return format_->modifiers; +} + +const PixelFormatEntry *PixelFormat::fromDRM(uint32_t drm_fourcc, const std::set &drm_modifiers) const +{ + for (const PixelFormatEntry &entry : pixelFormats) + if (entry.drm == drm_fourcc && + entry.modifiers == drm_modifiers) + return &entry; + + LOG(PixelFormats, Error) + << "Unsupported DRM pixel format " + << utils::hex(drm_fourcc); + + return &pixelFormats[0]; +} + +const PixelFormatEntry *PixelFormat::fromV4L2(uint32_t v4l2_fourcc) const +{ + for (const PixelFormatEntry &entry : pixelFormats) + if (entry.v4l2 == v4l2_fourcc) + return &entry; + + LOG(PixelFormats, Error) + << "Unsupported V4L2 pixel format " + << utils::hex(v4l2_fourcc); + + return &pixelFormats[0]; +} + } /* namespace libcamera */ diff --git a/src/libcamera/stream.cpp b/src/libcamera/stream.cpp index 13789e9eb344f95c..dbce550ca8d0b7b1 100644 --- a/src/libcamera/stream.cpp +++ b/src/libcamera/stream.cpp @@ -279,7 +279,7 @@ SizeRange StreamFormats::range(PixelFormat pixelformat) const * handlers provied StreamFormats. */ StreamConfiguration::StreamConfiguration() - : pixelFormat(0), stream_(nullptr) + : pixelFormat(), stream_(nullptr) { } @@ -287,7 +287,7 @@ StreamConfiguration::StreamConfiguration() * \brief Construct a configuration with stream formats */ StreamConfiguration::StreamConfiguration(const StreamFormats &formats) - : pixelFormat(0), stream_(nullptr), formats_(formats) + : pixelFormat(), stream_(nullptr), formats_(formats) { } @@ -348,7 +348,7 @@ StreamConfiguration::StreamConfiguration(const StreamFormats &formats) std::string StreamConfiguration::toString() const { std::stringstream ss; - ss << size.toString() << "-" << utils::hex(pixelFormat); + ss << size.toString() << "-" << utils::hex(pixelFormat.fourcc()); return ss.str(); } diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp index f84bd00570afa38c..e9d3e60198e140a0 100644 --- a/src/libcamera/v4l2_videodevice.cpp +++ b/src/libcamera/v4l2_videodevice.cpp @@ -846,7 +846,7 @@ ImageFormats V4L2VideoDevice::formats() if (sizes.empty()) return {}; - if (formats.addFormat(pixelformat, sizes)) { + if (formats.addFormat(PixelFormat(pixelformat), sizes)) { LOG(V4L2, Error) << "Could not add sizes for pixel format " << pixelformat; @@ -1417,56 +1417,7 @@ V4L2VideoDevice *V4L2VideoDevice::fromEntityName(const MediaDevice *media, */ PixelFormat V4L2VideoDevice::toPixelFormat(uint32_t v4l2Fourcc) { - switch (v4l2Fourcc) { - /* RGB formats. */ - case V4L2_PIX_FMT_RGB24: - return DRM_FORMAT_BGR888; - case V4L2_PIX_FMT_BGR24: - return DRM_FORMAT_RGB888; - case V4L2_PIX_FMT_ARGB32: - return DRM_FORMAT_BGRA8888; - - /* YUV packed formats. */ - case V4L2_PIX_FMT_YUYV: - return DRM_FORMAT_YUYV; - case V4L2_PIX_FMT_YVYU: - return DRM_FORMAT_YVYU; - case V4L2_PIX_FMT_UYVY: - return DRM_FORMAT_UYVY; - case V4L2_PIX_FMT_VYUY: - return DRM_FORMAT_VYUY; - - /* YUY planar formats. */ - case V4L2_PIX_FMT_NV16: - case V4L2_PIX_FMT_NV16M: - return DRM_FORMAT_NV16; - case V4L2_PIX_FMT_NV61: - case V4L2_PIX_FMT_NV61M: - return DRM_FORMAT_NV61; - case V4L2_PIX_FMT_NV12: - case V4L2_PIX_FMT_NV12M: - return DRM_FORMAT_NV12; - case V4L2_PIX_FMT_NV21: - case V4L2_PIX_FMT_NV21M: - return DRM_FORMAT_NV21; - - /* Compressed formats. */ - case V4L2_PIX_FMT_MJPEG: - return DRM_FORMAT_MJPEG; - - /* V4L2 formats not yet supported by DRM. */ - case V4L2_PIX_FMT_GREY: - default: - /* - * \todo We can't use LOG() in a static method of a Loggable - * class. Until we fix the logger, work around it. - */ - libcamera::_log(__FILE__, __LINE__, _LOG_CATEGORY(V4L2)(), - LogError).stream() - << "Unsupported V4L2 pixel format " - << utils::hex(v4l2Fourcc); - return 0; - } + return PixelFormat(v4l2Fourcc); } /** @@ -1500,53 +1451,7 @@ uint32_t V4L2VideoDevice::toV4L2Fourcc(PixelFormat pixelFormat) */ uint32_t V4L2VideoDevice::toV4L2Fourcc(PixelFormat pixelFormat, bool multiplanar) { - switch (pixelFormat) { - /* RGB formats. */ - case DRM_FORMAT_BGR888: - return V4L2_PIX_FMT_RGB24; - case DRM_FORMAT_RGB888: - return V4L2_PIX_FMT_BGR24; - case DRM_FORMAT_BGRA8888: - return V4L2_PIX_FMT_ARGB32; - - /* YUV packed formats. */ - case DRM_FORMAT_YUYV: - return V4L2_PIX_FMT_YUYV; - case DRM_FORMAT_YVYU: - return V4L2_PIX_FMT_YVYU; - case DRM_FORMAT_UYVY: - return V4L2_PIX_FMT_UYVY; - case DRM_FORMAT_VYUY: - return V4L2_PIX_FMT_VYUY; - - /* - * YUY planar formats. - * \todo Add support for non-contiguous memory planes - * \todo Select the format variant not only based on \a multiplanar but - * also take into account the formats supported by the device. - */ - case DRM_FORMAT_NV16: - return V4L2_PIX_FMT_NV16; - case DRM_FORMAT_NV61: - return V4L2_PIX_FMT_NV61; - case DRM_FORMAT_NV12: - return V4L2_PIX_FMT_NV12; - case DRM_FORMAT_NV21: - return V4L2_PIX_FMT_NV21; - - /* Compressed formats. */ - case DRM_FORMAT_MJPEG: - return V4L2_PIX_FMT_MJPEG; - } - - /* - * \todo We can't use LOG() in a static method of a Loggable - * class. Until we fix the logger, work around it. - */ - libcamera::_log(__FILE__, __LINE__, _LOG_CATEGORY(V4L2)(), LogError).stream() - << "Unsupported V4L2 pixel format " - << utils::hex(pixelFormat); - return 0; + return pixelFormat.v4l2(); } /** diff --git a/src/qcam/format_converter.cpp b/src/qcam/format_converter.cpp index 368cb43fbf17ae4d..c071ea9b4022750c 100644 --- a/src/qcam/format_converter.cpp +++ b/src/qcam/format_converter.cpp @@ -30,7 +30,7 @@ int FormatConverter::configure(libcamera::PixelFormat format, unsigned int width, unsigned int height) { - switch (format) { + switch (format.fourcc()) { case DRM_FORMAT_NV12: formatFamily_ = NV; horzSubSample_ = 2; diff --git a/src/qcam/viewfinder.cpp b/src/qcam/viewfinder.cpp index 0ebb8edd49efd1b1..d86c97dab8374d5e 100644 --- a/src/qcam/viewfinder.cpp +++ b/src/qcam/viewfinder.cpp @@ -14,7 +14,7 @@ #include "viewfinder.h" ViewFinder::ViewFinder(QWidget *parent) - : QWidget(parent), format_(0), width_(0), height_(0), image_(nullptr) + : QWidget(parent), format_(), width_(0), height_(0), image_(nullptr) { } diff --git a/src/v4l2/v4l2_camera_proxy.cpp b/src/v4l2/v4l2_camera_proxy.cpp index 622520479be01f58..e9d7bfd8ee243b78 100644 --- a/src/v4l2/v4l2_camera_proxy.cpp +++ b/src/v4l2/v4l2_camera_proxy.cpp @@ -525,7 +525,6 @@ struct PixelFormatPlaneInfo { }; struct PixelFormatInfo { - PixelFormat format; uint32_t v4l2Format; unsigned int numPlanes; std::array planes; @@ -533,24 +532,24 @@ struct PixelFormatInfo { namespace { -constexpr std::array pixelFormatInfo = {{ +static const std::array pixelFormatInfo = { { /* RGB formats. */ - { DRM_FORMAT_RGB888, V4L2_PIX_FMT_BGR24, 1, {{ { 24, 1, 1 }, { 0, 0, 0 }, { 0, 0, 0 } }} }, - { DRM_FORMAT_BGR888, V4L2_PIX_FMT_RGB24, 1, {{ { 24, 1, 1 }, { 0, 0, 0 }, { 0, 0, 0 } }} }, - { DRM_FORMAT_BGRA8888, V4L2_PIX_FMT_ARGB32, 1, {{ { 32, 1, 1 }, { 0, 0, 0 }, { 0, 0, 0 } }} }, + { V4L2_PIX_FMT_BGR24, 1, {{ { 24, 1, 1 }, { 0, 0, 0 }, { 0, 0, 0 } }} }, + { V4L2_PIX_FMT_RGB24, 1, {{ { 24, 1, 1 }, { 0, 0, 0 }, { 0, 0, 0 } }} }, + { V4L2_PIX_FMT_ARGB32, 1, {{ { 32, 1, 1 }, { 0, 0, 0 }, { 0, 0, 0 } }} }, /* YUV packed formats. */ - { DRM_FORMAT_UYVY, V4L2_PIX_FMT_UYVY, 1, {{ { 16, 1, 1 }, { 0, 0, 0 }, { 0, 0, 0 } }} }, - { DRM_FORMAT_VYUY, V4L2_PIX_FMT_VYUY, 1, {{ { 16, 1, 1 }, { 0, 0, 0 }, { 0, 0, 0 } }} }, - { DRM_FORMAT_YUYV, V4L2_PIX_FMT_YUYV, 1, {{ { 16, 1, 1 }, { 0, 0, 0 }, { 0, 0, 0 } }} }, - { DRM_FORMAT_YVYU, V4L2_PIX_FMT_YVYU, 1, {{ { 16, 1, 1 }, { 0, 0, 0 }, { 0, 0, 0 } }} }, + { V4L2_PIX_FMT_UYVY, 1, {{ { 16, 1, 1 }, { 0, 0, 0 }, { 0, 0, 0 } }} }, + { V4L2_PIX_FMT_VYUY, 1, {{ { 16, 1, 1 }, { 0, 0, 0 }, { 0, 0, 0 } }} }, + { V4L2_PIX_FMT_YUYV, 1, {{ { 16, 1, 1 }, { 0, 0, 0 }, { 0, 0, 0 } }} }, + { V4L2_PIX_FMT_YVYU, 1, {{ { 16, 1, 1 }, { 0, 0, 0 }, { 0, 0, 0 } }} }, /* YUY planar formats. */ - { DRM_FORMAT_NV12, V4L2_PIX_FMT_NV12, 2, {{ { 8, 1, 1 }, { 16, 2, 2 }, { 0, 0, 0 } }} }, - { DRM_FORMAT_NV21, V4L2_PIX_FMT_NV21, 2, {{ { 8, 1, 1 }, { 16, 2, 2 }, { 0, 0, 0 } }} }, - { DRM_FORMAT_NV16, V4L2_PIX_FMT_NV16, 2, {{ { 8, 1, 1 }, { 16, 2, 1 }, { 0, 0, 0 } }} }, - { DRM_FORMAT_NV61, V4L2_PIX_FMT_NV61, 2, {{ { 8, 1, 1 }, { 16, 2, 1 }, { 0, 0, 0 } }} }, - { DRM_FORMAT_NV24, V4L2_PIX_FMT_NV24, 2, {{ { 8, 1, 1 }, { 16, 2, 1 }, { 0, 0, 0 } }} }, - { DRM_FORMAT_NV42, V4L2_PIX_FMT_NV42, 2, {{ { 8, 1, 1 }, { 16, 1, 1 }, { 0, 0, 0 } }} }, -}}; + { V4L2_PIX_FMT_NV12, 2, {{ { 8, 1, 1 }, { 16, 2, 2 }, { 0, 0, 0 } }} }, + { V4L2_PIX_FMT_NV21, 2, {{ { 8, 1, 1 }, { 16, 2, 2 }, { 0, 0, 0 } }} }, + { V4L2_PIX_FMT_NV16, 2, {{ { 8, 1, 1 }, { 16, 2, 1 }, { 0, 0, 0 } }} }, + { V4L2_PIX_FMT_NV61, 2, {{ { 8, 1, 1 }, { 16, 2, 1 }, { 0, 0, 0 } }} }, + { V4L2_PIX_FMT_NV24, 2, {{ { 8, 1, 1 }, { 16, 2, 1 }, { 0, 0, 0 } }} }, + { V4L2_PIX_FMT_NV42, 2, {{ { 8, 1, 1 }, { 16, 1, 1 }, { 0, 0, 0 } }} }, +} }; } /* namespace */ @@ -588,24 +587,10 @@ unsigned int V4L2CameraProxy::imageSize(uint32_t format, unsigned int width, PixelFormat V4L2CameraProxy::v4l2ToDrm(uint32_t format) { - auto info = std::find_if(pixelFormatInfo.begin(), pixelFormatInfo.end(), - [format](const PixelFormatInfo &info) { - return info.v4l2Format == format; - }); - if (info == pixelFormatInfo.end()) - return format; - - return info->format; + return PixelFormat(format); } uint32_t V4L2CameraProxy::drmToV4L2(PixelFormat format) { - auto info = std::find_if(pixelFormatInfo.begin(), pixelFormatInfo.end(), - [format](const PixelFormatInfo &info) { - return info.format == format; - }); - if (info == pixelFormatInfo.end()) - return format; - - return info->v4l2Format; + return format.v4l2(); } diff --git a/test/stream/stream_formats.cpp b/test/stream/stream_formats.cpp index a391f5cd087d3872..a904d681c1691889 100644 --- a/test/stream/stream_formats.cpp +++ b/test/stream/stream_formats.cpp @@ -7,6 +7,8 @@ #include +#include + #include #include @@ -53,42 +55,49 @@ protected: int run() { + std::vector pixelFormats = { + PixelFormat(DRM_FORMAT_YUYV, {}), + PixelFormat(DRM_FORMAT_YVYU, {}), + PixelFormat(DRM_FORMAT_UYVY, {}), + PixelFormat(DRM_FORMAT_VYUY, {}), + }; + /* Test discrete sizes */ StreamFormats discrete({ - { 1, { SizeRange(100, 100), SizeRange(200, 200) } }, - { 2, { SizeRange(300, 300), SizeRange(400, 400) } }, + { pixelFormats[0], { SizeRange(100, 100), SizeRange(200, 200) } }, + { pixelFormats[1], { SizeRange(300, 300), SizeRange(400, 400) } }, }); - if (testSizes("discrete 1", discrete.sizes(1), + if (testSizes("discrete 1", discrete.sizes(pixelFormats[0]), { Size(100, 100), Size(200, 200) })) return TestFail; - if (testSizes("discrete 2", discrete.sizes(2), + if (testSizes("discrete 2", discrete.sizes(pixelFormats[1]), { Size(300, 300), Size(400, 400) })) return TestFail; /* Test range sizes */ StreamFormats range({ - { 1, { SizeRange(640, 480, 640, 480) } }, - { 2, { SizeRange(640, 480, 800, 600, 8, 8) } }, - { 3, { SizeRange(640, 480, 800, 600, 16, 16) } }, - { 4, { SizeRange(128, 128, 4096, 4096, 128, 128) } }, + { pixelFormats[0], { SizeRange(640, 480, 640, 480) } }, + { pixelFormats[1], { SizeRange(640, 480, 800, 600, 8, 8) } }, + { pixelFormats[2], { SizeRange(640, 480, 800, 600, 16, 16) } }, + { pixelFormats[3], { SizeRange(128, 128, 4096, 4096, 128, 128) } }, }); - if (testSizes("range 1", range.sizes(1), { Size(640, 480) })) + if (testSizes("range 1", range.sizes(pixelFormats[0]), { Size(640, 480) })) return TestFail; - if (testSizes("range 2", range.sizes(2), { + if (testSizes("range 2", range.sizes(pixelFormats[1]), { Size(640, 480), Size(720, 480), Size(720, 576), Size(768, 480), Size(800, 600) })) return TestFail; - if (testSizes("range 3", range.sizes(3), { + if (testSizes("range 3", range.sizes(pixelFormats[2]), { Size(640, 480), Size(720, 480), Size(720, 576), Size(768, 480) })) return TestFail; - if (testSizes("range 4", range.sizes(4), { + if (testSizes("range 4", range.sizes(pixelFormats[3]), { Size(1024, 768), Size(1280, 1024), Size(2048, 1152), Size(2048, 1536), Size(2560, 2048), Size(3200, 2048), })) From patchwork Fri Feb 28 03:29:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Niklas_S=C3=B6derlund?= X-Patchwork-Id: 2914 Return-Path: Received: from bin-mail-out-06.binero.net (bin-mail-out-06.binero.net [195.74.38.229]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 0CD826271F for ; Fri, 28 Feb 2020 04:29:40 +0100 (CET) X-Halon-ID: 8cd1328a-59da-11ea-9f85-005056917a89 Authorized-sender: niklas@soderlund.pp.se Received: from bismarck.berto.se (p4fca2392.dip0.t-ipconnect.de [79.202.35.146]) by bin-vsp-out-01.atm.binero.net (Halon) with ESMTPA id 8cd1328a-59da-11ea-9f85-005056917a89; Fri, 28 Feb 2020 04:29:39 +0100 (CET) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Fri, 28 Feb 2020 04:29:11 +0100 Message-Id: <20200228032913.497826-5-niklas.soderlund@ragnatech.se> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200228032913.497826-1-niklas.soderlund@ragnatech.se> References: <20200228032913.497826-1-niklas.soderlund@ragnatech.se> MIME-Version: 1.0 Subject: [libcamera-devel] [RFC 4/6] libcamera: PixelFormat: Add operations to operate on names X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 28 Feb 2020 03:29:40 -0000 Add a name to each pixel format and extend PixelFormat to be constructed from a name and to retrieve the name for printing. Make use of the new functionality to demonstrate it. - Update the cam utility to read and print the pixel format name instead of the fourcc integer number. - Update log messages to print the name instead of the fourcc integer number. Signed-off-by: Niklas Söderlund --- include/libcamera/pixelformats.h | 3 ++ src/cam/main.cpp | 9 ++--- src/libcamera/pipeline/uvcvideo.cpp | 4 +- src/libcamera/pixelformats.cpp | 58 ++++++++++++++++++++--------- src/libcamera/stream.cpp | 2 +- 5 files changed, 51 insertions(+), 25 deletions(-) diff --git a/include/libcamera/pixelformats.h b/include/libcamera/pixelformats.h index f0951e983192d5e8..8cea3a90ef2cc7ae 100644 --- a/include/libcamera/pixelformats.h +++ b/include/libcamera/pixelformats.h @@ -24,6 +24,7 @@ public: PixelFormat(); PixelFormat(const PixelFormat &other); explicit PixelFormat(uint32_t drm_fourcc, const std::set &drm_modifiers); + explicit PixelFormat(const std::string &name); PixelFormat &operator=(const PixelFormat &other); @@ -31,6 +32,7 @@ public: bool operator!=(const PixelFormat &other) const; bool operator<(const PixelFormat &other) const; + const std::string &name() const; uint32_t v4l2() const; uint32_t fourcc() const; const std::set &modifiers() const; @@ -47,6 +49,7 @@ protected: private: const PixelFormatEntry *fromDRM(uint32_t drm_fourcc, const std::set &drm_modifiers) const; const PixelFormatEntry *fromV4L2(uint32_t v4l2_fourcc) const; + const PixelFormatEntry *fromName(const std::string &name) const; const PixelFormatEntry *format_; }; diff --git a/src/cam/main.cpp b/src/cam/main.cpp index c8ef79daea37d8b6..0a08e362294fc9ee 100644 --- a/src/cam/main.cpp +++ b/src/cam/main.cpp @@ -159,7 +159,7 @@ int CamApp::parseOptions(int argc, char *argv[]) ArgumentRequired); streamKeyValue.addOption("height", OptionInteger, "Height in pixels", ArgumentRequired); - streamKeyValue.addOption("pixelformat", OptionInteger, "Pixel format", + streamKeyValue.addOption("pixelformat", OptionString, "Pixel format", ArgumentRequired); OptionsParser parser; @@ -247,9 +247,9 @@ int CamApp::prepareConfig() if (opt.isSet("height")) cfg.size.height = opt["height"]; - /* TODO: Translate 4CC string to ID. */ if (opt.isSet("pixelformat")) - cfg.pixelFormat = PixelFormat(opt["pixelformat"], {}); + cfg.pixelFormat = + PixelFormat(opt["pixelformat"].toString()); } } @@ -282,8 +282,7 @@ int CamApp::infoConfiguration() const StreamFormats &formats = cfg.formats(); for (PixelFormat pixelformat : formats.pixelformats()) { - std::cout << " * Pixelformat: 0x" << std::hex - << std::setw(8) << pixelformat.fourcc() << " " + std::cout << " * Pixelformat: " << pixelformat.name() << " " << formats.range(pixelformat).toString() << std::endl; diff --git a/src/libcamera/pipeline/uvcvideo.cpp b/src/libcamera/pipeline/uvcvideo.cpp index 5f3e52f691aaeae4..75fbfe1eb9145424 100644 --- a/src/libcamera/pipeline/uvcvideo.cpp +++ b/src/libcamera/pipeline/uvcvideo.cpp @@ -115,8 +115,8 @@ CameraConfiguration::Status UVCCameraConfiguration::validate() if (iter == pixelFormats.end()) { cfg.pixelFormat = pixelFormats.front(); LOG(UVC, Debug) - << "Adjusting pixel format from " << pixelFormat.fourcc() - << " to " << cfg.pixelFormat.fourcc(); + << "Adjusting pixel format from " << pixelFormat.name() + << " to " << cfg.pixelFormat.name(); status = Adjusted; } diff --git a/src/libcamera/pixelformats.cpp b/src/libcamera/pixelformats.cpp index b2aacbc39b9ca16a..70f41d86a23baceb 100644 --- a/src/libcamera/pixelformats.cpp +++ b/src/libcamera/pixelformats.cpp @@ -35,6 +35,7 @@ LOG_DEFINE_CATEGORY(PixelFormats) */ struct PixelFormatEntry { + std::string name; uint32_t v4l2; uint32_t drm; std::set modifiers; @@ -42,27 +43,27 @@ struct PixelFormatEntry { static const std::vector pixelFormats = { /* Invalid format, important to be first in list. */ - { .v4l2 = 0, .drm = DRM_FORMAT_INVALID, .modifiers = {} }, + { .name = "INVALID", .v4l2 = 0, .drm = DRM_FORMAT_INVALID, .modifiers = {} }, /* RGB formats. */ - { .v4l2 = V4L2_PIX_FMT_RGB24, .drm = DRM_FORMAT_BGR888, .modifiers = {} }, - { .v4l2 = V4L2_PIX_FMT_BGR24, .drm = DRM_FORMAT_RGB888, .modifiers = {} }, - { .v4l2 = V4L2_PIX_FMT_ARGB32, .drm = DRM_FORMAT_BGRA8888, .modifiers = {} }, + { .name = "RGR888", .v4l2 = V4L2_PIX_FMT_RGB24, .drm = DRM_FORMAT_BGR888, .modifiers = {} }, + { .name = "RGB888", .v4l2 = V4L2_PIX_FMT_BGR24, .drm = DRM_FORMAT_RGB888, .modifiers = {} }, + { .name = "BGRA8888", .v4l2 = V4L2_PIX_FMT_ARGB32, .drm = DRM_FORMAT_BGRA8888, .modifiers = {} }, /* YUV packed formats. */ - { .v4l2 = V4L2_PIX_FMT_YUYV, .drm = DRM_FORMAT_YUYV, .modifiers = {} }, - { .v4l2 = V4L2_PIX_FMT_YVYU, .drm = DRM_FORMAT_YVYU, .modifiers = {} }, - { .v4l2 = V4L2_PIX_FMT_UYVY, .drm = DRM_FORMAT_UYVY, .modifiers = {} }, - { .v4l2 = V4L2_PIX_FMT_VYUY, .drm = DRM_FORMAT_VYUY, .modifiers = {} }, + { .name = "YUYV", .v4l2 = V4L2_PIX_FMT_YUYV, .drm = DRM_FORMAT_YUYV, .modifiers = {} }, + { .name = "YVYU", .v4l2 = V4L2_PIX_FMT_YVYU, .drm = DRM_FORMAT_YVYU, .modifiers = {} }, + { .name = "UYVY", .v4l2 = V4L2_PIX_FMT_UYVY, .drm = DRM_FORMAT_UYVY, .modifiers = {} }, + { .name = "VYUY", .v4l2 = V4L2_PIX_FMT_VYUY, .drm = DRM_FORMAT_VYUY, .modifiers = {} }, /* YUY planar formats. */ - { .v4l2 = V4L2_PIX_FMT_NV16, .drm = DRM_FORMAT_NV16, .modifiers = {} }, - { .v4l2 = V4L2_PIX_FMT_NV16M, .drm = DRM_FORMAT_NV16, .modifiers = {} }, - { .v4l2 = V4L2_PIX_FMT_NV61, .drm = DRM_FORMAT_NV61, .modifiers = {} }, - { .v4l2 = V4L2_PIX_FMT_NV61M, .drm = DRM_FORMAT_NV61, .modifiers = {} }, - { .v4l2 = V4L2_PIX_FMT_NV12, .drm = DRM_FORMAT_NV12, .modifiers = {} }, - { .v4l2 = V4L2_PIX_FMT_NV12M, .drm = DRM_FORMAT_NV12, .modifiers = {} }, - { .v4l2 = V4L2_PIX_FMT_NV21, .drm = DRM_FORMAT_NV21, .modifiers = {} }, - { .v4l2 = V4L2_PIX_FMT_NV21M, .drm = DRM_FORMAT_NV21, .modifiers = {} }, + { .name = "NV16", .v4l2 = V4L2_PIX_FMT_NV16, .drm = DRM_FORMAT_NV16, .modifiers = {} }, + { .name = "NV16M", .v4l2 = V4L2_PIX_FMT_NV16M, .drm = DRM_FORMAT_NV16, .modifiers = {} }, + { .name = "NV61", .v4l2 = V4L2_PIX_FMT_NV61, .drm = DRM_FORMAT_NV61, .modifiers = {} }, + { .name = "NV61M", .v4l2 = V4L2_PIX_FMT_NV61M, .drm = DRM_FORMAT_NV61, .modifiers = {} }, + { .name = "NV12", .v4l2 = V4L2_PIX_FMT_NV12, .drm = DRM_FORMAT_NV12, .modifiers = {} }, + { .name = "NV12M", .v4l2 = V4L2_PIX_FMT_NV12M, .drm = DRM_FORMAT_NV12, .modifiers = {} }, + { .name = "NV21", .v4l2 = V4L2_PIX_FMT_NV21, .drm = DRM_FORMAT_NV21, .modifiers = {} }, + { .name = "NV21M", .v4l2 = V4L2_PIX_FMT_NV21M, .drm = DRM_FORMAT_NV21, .modifiers = {} }, /* Compressed formats. */ - { .v4l2 = V4L2_PIX_FMT_MJPEG, .drm = DRM_FORMAT_MJPEG, .modifiers = {} }, + { .name = "MJPEG", .v4l2 = V4L2_PIX_FMT_MJPEG, .drm = DRM_FORMAT_MJPEG, .modifiers = {} }, }; PixelFormat::PixelFormat() @@ -85,6 +86,11 @@ PixelFormat::PixelFormat(uint32_t v4l2_fourcc) { } +PixelFormat::PixelFormat(const std::string &name) + : format_(fromName(name)) +{ +} + PixelFormat &PixelFormat::operator=(const PixelFormat &other) { format_ = other.format_; @@ -107,6 +113,11 @@ bool PixelFormat::operator<(const PixelFormat &other) const return format_ > other.format_; } +const std::string &PixelFormat::name() const +{ + return format_->name; +} + uint32_t PixelFormat::v4l2() const { return format_->v4l2; @@ -149,4 +160,17 @@ const PixelFormatEntry *PixelFormat::fromV4L2(uint32_t v4l2_fourcc) const return &pixelFormats[0]; } +const PixelFormatEntry *PixelFormat::fromName(const std::string &name) const +{ + for (const PixelFormatEntry &entry : pixelFormats) + if (entry.name == name) + return &entry; + + LOG(PixelFormats, Error) + << "Unsupported name for pixel format " + << name; + + return &pixelFormats[0]; +} + } /* namespace libcamera */ diff --git a/src/libcamera/stream.cpp b/src/libcamera/stream.cpp index dbce550ca8d0b7b1..e82ffd8f3b211925 100644 --- a/src/libcamera/stream.cpp +++ b/src/libcamera/stream.cpp @@ -348,7 +348,7 @@ StreamConfiguration::StreamConfiguration(const StreamFormats &formats) std::string StreamConfiguration::toString() const { std::stringstream ss; - ss << size.toString() << "-" << utils::hex(pixelFormat.fourcc()); + ss << size.toString() << "-" << pixelFormat.name(); return ss.str(); } From patchwork Fri Feb 28 03:29:12 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Niklas_S=C3=B6derlund?= X-Patchwork-Id: 2915 Return-Path: Received: from vsp-unauthed02.binero.net (vsp-unauthed02.binero.net [195.74.38.227]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id C475A6271F for ; Fri, 28 Feb 2020 04:29:40 +0100 (CET) X-Halon-ID: 8d4fcf04-59da-11ea-9f85-005056917a89 Authorized-sender: niklas@soderlund.pp.se Received: from bismarck.berto.se (p4fca2392.dip0.t-ipconnect.de [79.202.35.146]) by bin-vsp-out-01.atm.binero.net (Halon) with ESMTPA id 8d4fcf04-59da-11ea-9f85-005056917a89; Fri, 28 Feb 2020 04:29:39 +0100 (CET) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Fri, 28 Feb 2020 04:29:12 +0100 Message-Id: <20200228032913.497826-6-niklas.soderlund@ragnatech.se> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200228032913.497826-1-niklas.soderlund@ragnatech.se> References: <20200228032913.497826-1-niklas.soderlund@ragnatech.se> MIME-Version: 1.0 Subject: [libcamera-devel] [RFC 5/6] libcamera: v4l2_videodevice: Remove pixel format translators X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 28 Feb 2020 03:29:41 -0000 Switch to use PixelFormat to translate between DRM and V4L2 instead of having a proxy implementation inside V4L2VideoDevice. Signed-off-by: Niklas Söderlund --- src/libcamera/include/v4l2_videodevice.h | 4 --- src/libcamera/pipeline/rkisp1/rkisp1.cpp | 4 +-- src/libcamera/pipeline/uvcvideo.cpp | 4 +-- src/libcamera/pipeline/vimc.cpp | 4 +-- src/libcamera/v4l2_videodevice.cpp | 44 ------------------------ test/camera/buffer_import.cpp | 2 +- 6 files changed, 7 insertions(+), 55 deletions(-) diff --git a/src/libcamera/include/v4l2_videodevice.h b/src/libcamera/include/v4l2_videodevice.h index 982df9d2f918e49c..9666a4dd2438a5ee 100644 --- a/src/libcamera/include/v4l2_videodevice.h +++ b/src/libcamera/include/v4l2_videodevice.h @@ -199,10 +199,6 @@ public: static V4L2VideoDevice *fromEntityName(const MediaDevice *media, const std::string &entity); - static PixelFormat toPixelFormat(uint32_t v4l2Fourcc); - uint32_t toV4L2Fourcc(PixelFormat pixelFormat); - static uint32_t toV4L2Fourcc(PixelFormat pixelFormat, bool multiplanar); - protected: std::string logPrefix() const; diff --git a/src/libcamera/pipeline/rkisp1/rkisp1.cpp b/src/libcamera/pipeline/rkisp1/rkisp1.cpp index 323fa3596c6ee242..f1ffb7acded5f4ee 100644 --- a/src/libcamera/pipeline/rkisp1/rkisp1.cpp +++ b/src/libcamera/pipeline/rkisp1/rkisp1.cpp @@ -629,7 +629,7 @@ int PipelineHandlerRkISP1::configure(Camera *camera, CameraConfiguration *c) LOG(RkISP1, Debug) << "Resizer output pad configured with " << format.toString(); V4L2DeviceFormat outputFormat = {}; - outputFormat.fourcc = video_->toV4L2Fourcc(cfg.pixelFormat); + outputFormat.fourcc = cfg.pixelFormat.v4l2(); outputFormat.size = cfg.size; outputFormat.planesCount = 2; @@ -638,7 +638,7 @@ int PipelineHandlerRkISP1::configure(Camera *camera, CameraConfiguration *c) return ret; if (outputFormat.size != cfg.size || - outputFormat.fourcc != video_->toV4L2Fourcc(cfg.pixelFormat)) { + outputFormat.fourcc != cfg.pixelFormat.v4l2()) { LOG(RkISP1, Error) << "Unable to configure capture in " << cfg.toString(); return -EINVAL; diff --git a/src/libcamera/pipeline/uvcvideo.cpp b/src/libcamera/pipeline/uvcvideo.cpp index 75fbfe1eb9145424..89c47b99df22395b 100644 --- a/src/libcamera/pipeline/uvcvideo.cpp +++ b/src/libcamera/pipeline/uvcvideo.cpp @@ -177,7 +177,7 @@ int PipelineHandlerUVC::configure(Camera *camera, CameraConfiguration *config) int ret; V4L2DeviceFormat format = {}; - format.fourcc = data->video_->toV4L2Fourcc(cfg.pixelFormat); + format.fourcc = cfg.pixelFormat.v4l2(); format.size = cfg.size; ret = data->video_->setFormat(&format); @@ -185,7 +185,7 @@ int PipelineHandlerUVC::configure(Camera *camera, CameraConfiguration *config) return ret; if (format.size != cfg.size || - format.fourcc != data->video_->toV4L2Fourcc(cfg.pixelFormat)) + format.fourcc != cfg.pixelFormat.v4l2()) return -EINVAL; cfg.setStream(&data->stream_); diff --git a/src/libcamera/pipeline/vimc.cpp b/src/libcamera/pipeline/vimc.cpp index a591c424919b0783..f9e24f288d732f74 100644 --- a/src/libcamera/pipeline/vimc.cpp +++ b/src/libcamera/pipeline/vimc.cpp @@ -232,7 +232,7 @@ int PipelineHandlerVimc::configure(Camera *camera, CameraConfiguration *config) return ret; V4L2DeviceFormat format = {}; - format.fourcc = data->video_->toV4L2Fourcc(cfg.pixelFormat); + format.fourcc = cfg.pixelFormat.v4l2(); format.size = cfg.size; ret = data->video_->setFormat(&format); @@ -240,7 +240,7 @@ int PipelineHandlerVimc::configure(Camera *camera, CameraConfiguration *config) return ret; if (format.size != cfg.size || - format.fourcc != data->video_->toV4L2Fourcc(cfg.pixelFormat)) + format.fourcc != cfg.pixelFormat.v4l2()) return -EINVAL; /* diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp index e9d3e60198e140a0..d3d5f73193e3abef 100644 --- a/src/libcamera/v4l2_videodevice.cpp +++ b/src/libcamera/v4l2_videodevice.cpp @@ -1410,50 +1410,6 @@ V4L2VideoDevice *V4L2VideoDevice::fromEntityName(const MediaDevice *media, return new V4L2VideoDevice(mediaEntity); } -/** - * \brief Convert a \a v4l2Fourcc to the corresponding PixelFormat - * \param[in] v4l2Fourcc The V4L2 pixel format (V4L2_PIX_FORMAT_*) - * \return The PixelFormat corresponding to \a v4l2Fourcc - */ -PixelFormat V4L2VideoDevice::toPixelFormat(uint32_t v4l2Fourcc) -{ - return PixelFormat(v4l2Fourcc); -} - -/** - * \brief Convert \a PixelFormat to its corresponding V4L2 FourCC - * \param[in] pixelFormat The PixelFormat to convert - * - * For multiplanar formats, the V4L2 format variant (contiguous or - * non-contiguous planes) is selected automatically based on the capabilities - * of the video device. If the video device supports the V4L2 multiplanar API, - * non-contiguous formats are preferred. - * - * \return The V4L2_PIX_FMT_* pixel format code corresponding to \a pixelFormat - */ -uint32_t V4L2VideoDevice::toV4L2Fourcc(PixelFormat pixelFormat) -{ - return V4L2VideoDevice::toV4L2Fourcc(pixelFormat, caps_.isMultiplanar()); -} - -/** - * \brief Convert \a pixelFormat to its corresponding V4L2 FourCC - * \param[in] pixelFormat The PixelFormat to convert - * \param[in] multiplanar V4L2 Multiplanar API support flag - * - * Multiple V4L2 formats may exist for one PixelFormat when the format uses - * multiple planes, as V4L2 defines separate 4CCs for contiguous and separate - * planes formats. Set the \a multiplanar parameter to false to select a format - * with contiguous planes, or to true to select a format with non-contiguous - * planes. - * - * \return The V4L2_PIX_FMT_* pixel format code corresponding to \a pixelFormat - */ -uint32_t V4L2VideoDevice::toV4L2Fourcc(PixelFormat pixelFormat, bool multiplanar) -{ - return pixelFormat.v4l2(); -} - /** * \class V4L2M2MDevice * \brief Memory-to-Memory video device diff --git a/test/camera/buffer_import.cpp b/test/camera/buffer_import.cpp index ab6e74bd1671e6f5..d47a9d8516c58375 100644 --- a/test/camera/buffer_import.cpp +++ b/test/camera/buffer_import.cpp @@ -89,7 +89,7 @@ public: } format.size = config.size; - format.fourcc = V4L2VideoDevice::toV4L2Fourcc(config.pixelFormat, false); + format.fourcc = config.pixelFormat.v4l2(); if (video_->setFormat(&format)) { std::cout << "Failed to set format on output device" << std::endl; return TestFail; From patchwork Fri Feb 28 03:29:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Niklas_S=C3=B6derlund?= X-Patchwork-Id: 2916 Return-Path: Received: from vsp-unauthed02.binero.net (vsp-unauthed02.binero.net [195.74.38.227]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 667CC6271F for ; Fri, 28 Feb 2020 04:29:41 +0100 (CET) X-Halon-ID: 8dbb4e34-59da-11ea-9f85-005056917a89 Authorized-sender: niklas@soderlund.pp.se Received: from bismarck.berto.se (p4fca2392.dip0.t-ipconnect.de [79.202.35.146]) by bin-vsp-out-01.atm.binero.net (Halon) with ESMTPA id 8dbb4e34-59da-11ea-9f85-005056917a89; Fri, 28 Feb 2020 04:29:40 +0100 (CET) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Fri, 28 Feb 2020 04:29:13 +0100 Message-Id: <20200228032913.497826-7-niklas.soderlund@ragnatech.se> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200228032913.497826-1-niklas.soderlund@ragnatech.se> References: <20200228032913.497826-1-niklas.soderlund@ragnatech.se> MIME-Version: 1.0 Subject: [libcamera-devel] [RFC 6/6] v4l2: camera_proxy: Switch to PixelFormat X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 28 Feb 2020 03:29:41 -0000 Remove proxies for PixelFormat and use the interface directly. Signed-off-by: Niklas Söderlund --- src/v4l2/v4l2_camera_proxy.cpp | 28 +++++++++------------------- src/v4l2/v4l2_camera_proxy.h | 3 --- 2 files changed, 9 insertions(+), 22 deletions(-) diff --git a/src/v4l2/v4l2_camera_proxy.cpp b/src/v4l2/v4l2_camera_proxy.cpp index e9d7bfd8ee243b78..7ea3039e0455c3f9 100644 --- a/src/v4l2/v4l2_camera_proxy.cpp +++ b/src/v4l2/v4l2_camera_proxy.cpp @@ -141,7 +141,7 @@ void V4L2CameraProxy::setFmtFromConfig(StreamConfiguration &streamConfig) { curV4L2Format_.fmt.pix.width = streamConfig.size.width; curV4L2Format_.fmt.pix.height = streamConfig.size.height; - curV4L2Format_.fmt.pix.pixelformat = drmToV4L2(streamConfig.pixelFormat); + curV4L2Format_.fmt.pix.pixelformat = streamConfig.pixelFormat.v4l2(); curV4L2Format_.fmt.pix.field = V4L2_FIELD_NONE; curV4L2Format_.fmt.pix.bytesperline = bplMultiplier(curV4L2Format_.fmt.pix.pixelformat) * @@ -159,7 +159,7 @@ unsigned int V4L2CameraProxy::calculateSizeImage(StreamConfiguration &streamConf * \todo Merge this method with setFmtFromConfig (need imageSize to * support all libcamera formats first, or filter out MJPEG for now). */ - return imageSize(drmToV4L2(streamConfig.pixelFormat), + return imageSize(streamConfig.pixelFormat.v4l2(), streamConfig.size.width, streamConfig.size.height); } @@ -229,7 +229,7 @@ int V4L2CameraProxy::vidioc_enum_fmt(struct v4l2_fmtdesc *arg) /* \todo Add map from format to description. */ utils::strlcpy(reinterpret_cast(arg->description), "Video Format Description", sizeof(arg->description)); - arg->pixelformat = drmToV4L2(streamConfig_.formats().pixelformats()[arg->index]); + arg->pixelformat = streamConfig_.formats().pixelformats()[arg->index].v4l2(); return 0; } @@ -249,7 +249,7 @@ int V4L2CameraProxy::vidioc_g_fmt(struct v4l2_format *arg) void V4L2CameraProxy::tryFormat(struct v4l2_format *arg) { - PixelFormat format = v4l2ToDrm(arg->fmt.pix.pixelformat); + PixelFormat format = PixelFormat(arg->fmt.pix.pixelformat); const std::vector &formats = streamConfig_.formats().pixelformats(); if (std::find(formats.begin(), formats.end(), format) == formats.end()) @@ -262,11 +262,11 @@ void V4L2CameraProxy::tryFormat(struct v4l2_format *arg) arg->fmt.pix.width = size.width; arg->fmt.pix.height = size.height; - arg->fmt.pix.pixelformat = drmToV4L2(format); + arg->fmt.pix.pixelformat = format.v4l2(); arg->fmt.pix.field = V4L2_FIELD_NONE; - arg->fmt.pix.bytesperline = bplMultiplier(drmToV4L2(format)) * + arg->fmt.pix.bytesperline = bplMultiplier(format.v4l2()) * arg->fmt.pix.width; - arg->fmt.pix.sizeimage = imageSize(drmToV4L2(format), + arg->fmt.pix.sizeimage = imageSize(format.v4l2(), arg->fmt.pix.width, arg->fmt.pix.height); arg->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB; @@ -283,7 +283,7 @@ int V4L2CameraProxy::vidioc_s_fmt(struct v4l2_format *arg) Size size(arg->fmt.pix.width, arg->fmt.pix.height); int ret = vcam_->configure(&streamConfig_, size, - v4l2ToDrm(arg->fmt.pix.pixelformat), + PixelFormat(arg->fmt.pix.pixelformat), bufferCount_); if (ret < 0) return -EINVAL; @@ -345,7 +345,7 @@ int V4L2CameraProxy::vidioc_reqbufs(struct v4l2_requestbuffers *arg) Size size(curV4L2Format_.fmt.pix.width, curV4L2Format_.fmt.pix.height); ret = vcam_->configure(&streamConfig_, size, - v4l2ToDrm(curV4L2Format_.fmt.pix.pixelformat), + PixelFormat(curV4L2Format_.fmt.pix.pixelformat), arg->count); if (ret < 0) return -EINVAL; @@ -584,13 +584,3 @@ unsigned int V4L2CameraProxy::imageSize(uint32_t format, unsigned int width, return width * height * multiplier / 8; } - -PixelFormat V4L2CameraProxy::v4l2ToDrm(uint32_t format) -{ - return PixelFormat(format); -} - -uint32_t V4L2CameraProxy::drmToV4L2(PixelFormat format) -{ - return format.v4l2(); -} diff --git a/src/v4l2/v4l2_camera_proxy.h b/src/v4l2/v4l2_camera_proxy.h index c8e61adf80f1b93b..03495d6266ef610a 100644 --- a/src/v4l2/v4l2_camera_proxy.h +++ b/src/v4l2/v4l2_camera_proxy.h @@ -59,9 +59,6 @@ private: static unsigned int imageSize(uint32_t format, unsigned int width, unsigned int height); - static PixelFormat v4l2ToDrm(uint32_t format); - static uint32_t drmToV4L2(PixelFormat format); - unsigned int refcount_; unsigned int index_; bool nonBlocking_;