{"id":8498,"url":"https://patchwork.libcamera.org/api/patches/8498/?format=json","web_url":"https://patchwork.libcamera.org/patch/8498/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20200629115328.16273-4-jacopo@jmondi.org>","date":"2020-06-29T11:53:28","name":"[libcamera-devel,v3,4/4] libcamera: v4l2_videodevice: Use ImageFormats","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"17ea9e66d5c4a6866c227afa689af5e7f5b8d25b","submitter":{"id":3,"url":"https://patchwork.libcamera.org/api/people/3/?format=json","name":"Jacopo Mondi","email":"jacopo@jmondi.org"},"delegate":{"id":15,"url":"https://patchwork.libcamera.org/api/users/15/?format=json","username":"jmondi","first_name":"Jacopo","last_name":"Mondi","email":"jacopo@jmondi.org"},"mbox":"https://patchwork.libcamera.org/patch/8498/mbox/","series":[{"id":1055,"url":"https://patchwork.libcamera.org/api/series/1055/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1055","date":"2020-06-29T10:50:33","name":"ImageFormat rework","version":3,"mbox":"https://patchwork.libcamera.org/series/1055/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/8498/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/8498/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 3DA93BF415\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 29 Jun 2020 11:50:17 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 199C160A16;\n\tMon, 29 Jun 2020 13:50:17 +0200 (CEST)","from relay11.mail.gandi.net (relay11.mail.gandi.net\n\t[217.70.178.231])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id CEE52609D6\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 29 Jun 2020 13:50:13 +0200 (CEST)","from uno.lan (93-34-118-233.ip49.fastwebnet.it [93.34.118.233])\n\t(Authenticated sender: jacopo@jmondi.org)\n\tby relay11.mail.gandi.net (Postfix) with ESMTPSA id 3461E100013;\n\tMon, 29 Jun 2020 11:50:13 +0000 (UTC)"],"From":"Jacopo Mondi <jacopo@jmondi.org>","To":"libcamera-devel@lists.libcamera.org","Date":"Mon, 29 Jun 2020 13:53:28 +0200","Message-Id":"<20200629115328.16273-4-jacopo@jmondi.org>","X-Mailer":"git-send-email 2.27.0","In-Reply-To":"<20200629115328.16273-1-jacopo@jmondi.org>","References":"<20200629105037.14706-1-jacopo@jmondi.org>\n\t<20200629115328.16273-1-jacopo@jmondi.org>","MIME-Version":"1.0","Subject":"[libcamera-devel] [PATCH v3 4/4] libcamera: v4l2_videodevice: Use\n\tImageFormats","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Content-Type":"text/plain; charset=\"us-ascii\"","Content-Transfer-Encoding":"7bit","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"ImageFormats was meant to be used not only for V4L2Subdevice but\nfor video devices as well. Since the introduction of of V4L2PixelFormat\nthe V4L2VideoDevice class and its users have been using a raw map to\nenumerate and inspect the V4L2VideoDevice formats.\n\nProvide a V4L2VideoDevice::Formats type definition as for V4L2Subdevice\nand use ImageFormats wherever possible in pipeline handlers.\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nSigned-off-by: Jacopo Mondi <jacopo@jmondi.org>\n---\n include/libcamera/internal/v4l2_videodevice.h |  4 +++-\n .../pipeline/raspberrypi/raspberrypi.cpp      | 21 +++++++++----------\n src/libcamera/pipeline/simple/converter.cpp   |  2 +-\n src/libcamera/pipeline/simple/simple.cpp      |  7 +++----\n src/libcamera/pipeline/uvcvideo/uvcvideo.cpp  |  7 +++----\n src/libcamera/v4l2_videodevice.cpp            | 14 +++++++++----\n 6 files changed, 30 insertions(+), 25 deletions(-)\n\n--\n2.27.0","diff":"diff --git a/include/libcamera/internal/v4l2_videodevice.h b/include/libcamera/internal/v4l2_videodevice.h\nindex 4d21f5a01ec8..06c65e863a99 100644\n--- a/include/libcamera/internal/v4l2_videodevice.h\n+++ b/include/libcamera/internal/v4l2_videodevice.h\n@@ -168,6 +168,8 @@ public:\n class V4L2VideoDevice : public V4L2Device\n {\n public:\n+\tusing Formats = ImageFormats<V4L2PixelFormat>;\n+\n \texplicit V4L2VideoDevice(const std::string &deviceNode);\n \texplicit V4L2VideoDevice(const MediaEntity *entity);\n \tV4L2VideoDevice(const V4L2VideoDevice &) = delete;\n@@ -187,7 +189,7 @@ public:\n\n \tint getFormat(V4L2DeviceFormat *format);\n \tint setFormat(V4L2DeviceFormat *format);\n-\tstd::map<V4L2PixelFormat, std::vector<SizeRange>> formats(uint32_t code = 0);\n+\tFormats formats(uint32_t code = 0);\n\n \tint setSelection(unsigned int target, Rectangle *rect);\n\ndiff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\nindex 9d887b706c3f..ffb3c5cc45d9 100644\n--- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n+++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n@@ -37,8 +37,6 @@ namespace libcamera {\n\n LOG_DEFINE_CATEGORY(RPI)\n\n-using V4L2PixFmtMap = std::map<V4L2PixelFormat, std::vector<SizeRange>>;\n-\n namespace {\n\n bool isRaw(PixelFormat &pixFmt)\n@@ -67,7 +65,7 @@ double scoreFormat(double desired, double actual)\n \treturn score;\n }\n\n-V4L2DeviceFormat findBestMode(V4L2PixFmtMap &formatsMap, const Size &req)\n+V4L2DeviceFormat findBestMode(V4L2VideoDevice::Formats &formatsMap, const Size &req)\n {\n \tdouble bestScore = 9e9, score;\n \tV4L2DeviceFormat bestMode = {};\n@@ -79,7 +77,7 @@ V4L2DeviceFormat findBestMode(V4L2PixFmtMap &formatsMap, const Size &req)\n #define PENALTY_UNPACKED\t 500.0\n\n \t/* Calculate the closest/best mode from the user requested size. */\n-\tfor (const auto &iter : formatsMap) {\n+\tfor (const auto &iter : formatsMap.data()) {\n \t\tV4L2PixelFormat v4l2Format = iter.first;\n \t\tPixelFormat pixelFormat = v4l2Format.toPixelFormat();\n \t\tconst PixelFormatInfo &info = PixelFormatInfo::info(pixelFormat);\n@@ -427,7 +425,7 @@ CameraConfiguration::Status RPiCameraConfiguration::validate()\n \t\t\t * Calculate the best sensor mode we can use based on\n \t\t\t * the user request.\n \t\t\t */\n-\t\t\tV4L2PixFmtMap fmts = data_->unicam_[Unicam::Image].dev()->formats();\n+\t\t\tV4L2VideoDevice::Formats fmts = data_->unicam_[Unicam::Image].dev()->formats();\n \t\t\tV4L2DeviceFormat sensorFormat = findBestMode(fmts, cfg.size);\n \t\t\tPixelFormat sensorPixFormat = sensorFormat.fourcc.toPixelFormat();\n \t\t\tif (cfg.size != sensorFormat.size ||\n@@ -481,14 +479,14 @@ CameraConfiguration::Status RPiCameraConfiguration::validate()\n \t\t *\n \t\t */\n \t\tPixelFormat &cfgPixFmt = config_.at(outSize[i].first).pixelFormat;\n-\t\tV4L2PixFmtMap fmts;\n+\t\tV4L2VideoDevice::Formats fmts;\n\n \t\tif (i == maxIndex)\n \t\t\tfmts = data_->isp_[Isp::Output0].dev()->formats();\n \t\telse\n \t\t\tfmts = data_->isp_[Isp::Output1].dev()->formats();\n\n-\t\tif (fmts.find(V4L2PixelFormat::fromPixelFormat(cfgPixFmt, false)) == fmts.end()) {\n+\t\tif (!fmts.contains(V4L2PixelFormat::fromPixelFormat(cfgPixFmt, false))) {\n \t\t\t/* If we cannot find a native format, use a default one. */\n \t\t\tcfgPixFmt = formats::NV12;\n \t\t\tstatus = Adjusted;\n@@ -518,9 +516,9 @@ CameraConfiguration *PipelineHandlerRPi::generateConfiguration(Camera *camera,\n \tRPiCameraData *data = cameraData(camera);\n \tCameraConfiguration *config = new RPiCameraConfiguration(data);\n \tV4L2DeviceFormat sensorFormat;\n+\tV4L2VideoDevice::Formats fmts;\n \tunsigned int bufferCount;\n \tPixelFormat pixelFormat;\n-\tV4L2PixFmtMap fmts;\n \tSize size;\n\n \tif (roles.empty())\n@@ -580,8 +578,9 @@ CameraConfiguration *PipelineHandlerRPi::generateConfiguration(Camera *camera,\n\n \t\t/* Translate the V4L2PixelFormat to PixelFormat. */\n \t\tstd::map<PixelFormat, std::vector<SizeRange>> deviceFormats;\n-\t\tstd::transform(fmts.begin(), fmts.end(), std::inserter(deviceFormats, deviceFormats.end()),\n-\t\t\t       [&](const decltype(fmts)::value_type &format) {\n+\t\tstd::transform(fmts.data().begin(), fmts.data().end(),\n+\t\t\t       std::inserter(deviceFormats, deviceFormats.end()),\n+\t\t\t       [&](const auto &format) {\n \t\t\t\t\treturn decltype(deviceFormats)::value_type{\n \t\t\t\t\t\tformat.first.toPixelFormat(),\n \t\t\t\t\t\tformat.second\n@@ -638,7 +637,7 @@ int PipelineHandlerRPi::configure(Camera *camera, CameraConfiguration *config)\n \t}\n\n \t/* First calculate the best sensor mode we can use based on the user request. */\n-\tV4L2PixFmtMap fmts = data->unicam_[Unicam::Image].dev()->formats();\n+\tV4L2VideoDevice::Formats fmts = data->unicam_[Unicam::Image].dev()->formats();\n \tV4L2DeviceFormat sensorFormat = findBestMode(fmts, rawStream ? sensorSize : maxSize);\n\n \t/*\ndiff --git a/src/libcamera/pipeline/simple/converter.cpp b/src/libcamera/pipeline/simple/converter.cpp\nindex e5e2f0fddb62..701db96138b0 100644\n--- a/src/libcamera/pipeline/simple/converter.cpp\n+++ b/src/libcamera/pipeline/simple/converter.cpp\n@@ -85,7 +85,7 @@ std::vector<PixelFormat> SimpleConverter::formats(PixelFormat input)\n\n \tstd::vector<PixelFormat> pixelFormats;\n\n-\tfor (const auto &format : m2m_->capture()->formats()) {\n+\tfor (const auto &format : m2m_->capture()->formats().data()) {\n \t\tPixelFormat pixelFormat = format.first.toPixelFormat();\n \t\tif (pixelFormat)\n \t\t\tpixelFormats.push_back(pixelFormat);\ndiff --git a/src/libcamera/pipeline/simple/simple.cpp b/src/libcamera/pipeline/simple/simple.cpp\nindex 1ec8d0f7de03..202a7e85375c 100644\n--- a/src/libcamera/pipeline/simple/simple.cpp\n+++ b/src/libcamera/pipeline/simple/simple.cpp\n@@ -275,13 +275,12 @@ int SimpleCameraData::init()\n \t\t\treturn ret;\n \t\t}\n\n-\t\tstd::map<V4L2PixelFormat, std::vector<SizeRange>> videoFormats =\n-\t\t\tvideo_->formats(format.mbus_code);\n+\t\tV4L2VideoDevice::Formats videoFormats = video_->formats(format.mbus_code);\n\n \t\tLOG(SimplePipeline, Debug)\n \t\t\t<< \"Adding configuration for \" << format.size.toString()\n \t\t\t<< \" in pixel formats [ \"\n-\t\t\t<< utils::join(videoFormats, \", \",\n+\t\t\t<< utils::join(videoFormats.data(), \", \",\n \t\t\t\t       [](const auto &f) {\n \t\t\t\t\t       return f.first.toString();\n \t\t\t\t       })\n@@ -294,7 +293,7 @@ int SimpleCameraData::init()\n \t\t * handler currently doesn't care about how a particular\n \t\t * PixelFormat is achieved.\n \t\t */\n-\t\tfor (const auto &videoFormat : videoFormats) {\n+\t\tfor (const auto &videoFormat : videoFormats.data()) {\n \t\t\tPixelFormat pixelFormat = videoFormat.first.toPixelFormat();\n \t\t\tif (!pixelFormat)\n \t\t\t\tcontinue;\ndiff --git a/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp b/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp\nindex 80a0e77ba3fc..4c7812d0fb6b 100644\n--- a/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp\n+++ b/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp\n@@ -158,12 +158,11 @@ CameraConfiguration *PipelineHandlerUVC::generateConfiguration(Camera *camera,\n \tif (roles.empty())\n \t\treturn config;\n\n-\tstd::map<V4L2PixelFormat, std::vector<SizeRange>> v4l2Formats =\n-\t\tdata->video_->formats();\n+\tV4L2VideoDevice::Formats v4l2Formats = data->video_->formats();\n \tstd::map<PixelFormat, std::vector<SizeRange>> deviceFormats;\n-\tstd::transform(v4l2Formats.begin(), v4l2Formats.end(),\n+\tstd::transform(v4l2Formats.data().begin(), v4l2Formats.data().end(),\n \t\t       std::inserter(deviceFormats, deviceFormats.begin()),\n-\t\t       [&](const decltype(v4l2Formats)::value_type &format) {\n+\t\t       [&](const auto &format) {\n \t\t\t       return decltype(deviceFormats)::value_type{\n \t\t\t\t       format.first.toPixelFormat(),\n \t\t\t\t       format.second\ndiff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp\nindex 3614b2ed1cbc..63e09cf9616a 100644\n--- a/src/libcamera/v4l2_videodevice.cpp\n+++ b/src/libcamera/v4l2_videodevice.cpp\n@@ -461,6 +461,12 @@ const std::string V4L2DeviceFormat::toString() const\n  * \\context This class is \\threadbound.\n  */\n\n+/**\n+ * \\typedef V4L2VideoDevice::Formats\n+ * \\brief An ImageFormats specialization mapping V4L2PixelFormat instances to\n+ * image resolutions\n+ */\n+\n /**\n  * \\brief Construct a V4L2VideoDevice\n  * \\param[in] deviceNode The file-system path to the video device node\n@@ -925,23 +931,23 @@ int V4L2VideoDevice::setFormatSingleplane(V4L2DeviceFormat *format)\n  *\n  * \\return A list of the supported video device formats\n  */\n-std::map<V4L2PixelFormat, std::vector<SizeRange>> V4L2VideoDevice::formats(uint32_t code)\n+V4L2VideoDevice::Formats V4L2VideoDevice::formats(uint32_t code)\n {\n-\tstd::map<V4L2PixelFormat, std::vector<SizeRange>> formats;\n+\tFormats formats;\n\n \tfor (V4L2PixelFormat pixelFormat : enumPixelformats(code)) {\n \t\tstd::vector<SizeRange> sizes = enumSizes(pixelFormat);\n \t\tif (sizes.empty())\n \t\t\treturn {};\n\n-\t\tif (formats.find(pixelFormat) != formats.end()) {\n+\t\tif (!formats.contains(pixelFormat)) {\n \t\t\tLOG(V4L2, Error)\n \t\t\t\t<< \"Could not add sizes for pixel format \"\n \t\t\t\t<< pixelFormat;\n \t\t\treturn {};\n \t\t}\n\n-\t\tformats.emplace(pixelFormat, sizes);\n+\t\tformats.addFormat(pixelFormat, sizes);\n \t}\n\n \treturn formats;\n","prefixes":["libcamera-devel","v3","4/4"]}