From patchwork Fri Oct 22 11:55:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 14255 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 EF323BF415 for ; Fri, 22 Oct 2021 11:55:48 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id AE88068F68; Fri, 22 Oct 2021 13:55:48 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=raspberrypi.com header.i=@raspberrypi.com header.b="sK2flQGo"; dkim-atps=neutral Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id ACFBE68F5E for ; Fri, 22 Oct 2021 13:55:44 +0200 (CEST) Received: by mail-wr1-x42e.google.com with SMTP id e12so3577351wra.4 for ; Fri, 22 Oct 2021 04:55:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raspberrypi.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ZtQR4caqAxW5pY0otRC3Bfy56XbPIGt7rkbYnd54QuY=; b=sK2flQGokXoH/h69QXg7khQWxelLq+evFy/zNGXLDZk0jXtxanq630O9d/S3Dp3gNx JOg6AaWBNmtGP/DFroWsokL5lTyRUsgz5UGeM/Wtn5gNtV/Iu/t2w3OCn2Tsp6t4U6Ei uV0+CVSEROl6lyhzJRDYji8ZUsYNwhR5kif1E6iHn/kACcLqMu3JhTOOd4GwVz1BXUpq Iyp9Cwobkr6AIbi2CZIds1UKNPT2x+/eOLbs4j7/EUF6KbfAAEXMx/bZTsqWyHlqNIqu aASPgnRKRIW4Z+HFI5scmg7o6sdj/+jM/AkT891++5oxp4/wgTZ7Q8i8OlopoxPNCptD OIWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ZtQR4caqAxW5pY0otRC3Bfy56XbPIGt7rkbYnd54QuY=; b=aaS9Vrd3oLkCOIADFy0XtcVIiWnIuQmPvWx/YBCQpHo4VXDryFNfCUARCBhMVRvIW/ xl2M8Nda9YT/P1Un5g3j2T3FstQr2IAb3irXObF3h30X4g+Dk39IZwg78VDIxItXvG1b 5urY4GFqnIwV0Y5DgB8H95vt4/AgArsBCXwBQCl19u6SZZwq77x1kV4UIDhPg8praBNh l8lYZf4iAok584xxi832CNTr0e6covaECeoi8miCuU3KQ9l1Ycz8ewPXDN9Yq10TUcXP LRy4gWt0iVyF10PgP5zfFOD6gslrcFoG66XwQ8CQGkktVwc6OpBMymGI0VTr5Hl7u9bZ qPaQ== X-Gm-Message-State: AOAM5322WJcGl1IpmXKer5LicIgJNTYNj3jayB/+0HWl6LTaBQvXiGCD egMJRmdpr5rLpFJueWqdqvQbwTO/lm0DFSeC X-Google-Smtp-Source: ABdhPJxcPJ2GK5LGE3g41fdjDWz3gZ7ecMnIRUqqh2rmsGTvy35gUTQUVMZSkp5MQ4vp12b9uD9kvw== X-Received: by 2002:adf:d1eb:: with SMTP id g11mr14926382wrd.16.1634903744017; Fri, 22 Oct 2021 04:55:44 -0700 (PDT) Received: from naush-laptop.pitowers.org ([2a00:1098:3142:14:f00d:ddf0:61b0:a4cd]) by smtp.gmail.com with ESMTPSA id l5sm7853364wru.24.2021.10.22.04.55.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Oct 2021 04:55:43 -0700 (PDT) From: Naushir Patuck To: libcamera-devel@lists.libcamera.org Date: Fri, 22 Oct 2021 12:55:34 +0100 Message-Id: <20211022115537.2964533-4-naush@raspberrypi.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211022115537.2964533-1-naush@raspberrypi.com> References: <20211022115537.2964533-1-naush@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 3/6] pipeline: raspberrypi: Return a PixelFormat from findBestMode() 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" It is more convenient to return a PixelFormat from findBestMode(), as the conversions from PixelFormat to V4L2SubdeviceFormat and V4L2DeviceFormat are simpler. Add some internal helpers to perform these conversions. Signed-off-by: Naushir Patuck --- .../pipeline/raspberrypi/raspberrypi.cpp | 73 +++++++++++++------ 1 file changed, 52 insertions(+), 21 deletions(-) diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp index 730f1575095c..0f13127a7748 100644 --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp @@ -50,6 +50,7 @@ namespace { /* Map of mbus codes to supported sizes reported by the sensor. */ using SensorFormats = std::map>; +using SensorMode = std::pair; SensorFormats populateSensorFormats(std::unique_ptr &sensor) { @@ -61,6 +62,34 @@ SensorFormats populateSensorFormats(std::unique_ptr &sensor) return formats; } +inline V4L2DeviceFormat toV4L2DeviceFormat(SensorMode &mode) +{ + V4L2DeviceFormat deviceFormat; + + deviceFormat.fourcc = V4L2PixelFormat::fromPixelFormat(mode.first); + deviceFormat.size = mode.second; + return deviceFormat; +} + +inline V4L2DeviceFormat toV4L2DeviceFormat(V4L2SubdeviceFormat &format) +{ + V4L2DeviceFormat deviceFormat; + + deviceFormat.fourcc = BayerFormat::fromMbusCode(format.mbus_code).toV4L2PixelFormat(); + deviceFormat.size = format.size; + return deviceFormat; +} + +inline V4L2SubdeviceFormat toV4L2SubdeviceFormat(SensorMode &mode) +{ + V4L2SubdeviceFormat subdeviceFormat; + V4L2PixelFormat fourcc = V4L2PixelFormat::fromPixelFormat(mode.first); + + subdeviceFormat.mbus_code = BayerFormat::fromV4L2PixelFormat(fourcc).toMbusCode(); + subdeviceFormat.size = mode.second; + return subdeviceFormat; +} + bool isRaw(PixelFormat &pixFmt) { /* @@ -87,10 +116,10 @@ double scoreFormat(double desired, double actual) return score; } -V4L2DeviceFormat findBestMode(const SensorFormats &formatsMap, const Size &req) +SensorMode findBestMode(const SensorFormats &formatsMap, const Size &req) { double bestScore = std::numeric_limits::max(), score; - V4L2DeviceFormat bestMode; + SensorMode bestMode; #define PENALTY_AR 1500.0 #define PENALTY_8BIT 2000.0 @@ -101,8 +130,8 @@ V4L2DeviceFormat findBestMode(const SensorFormats &formatsMap, const Size &req) /* Calculate the closest/best mode from the user requested size. */ for (const auto &iter : formatsMap) { const unsigned int mbus_code = iter.first; - const V4L2PixelFormat v4l2Format = BayerFormat::fromMbusCode(mbus_code).toV4L2PixelFormat(); - const PixelFormatInfo &info = PixelFormatInfo::info(v4l2Format); + const PixelFormat format = BayerFormat::fromMbusCode(mbus_code).toPixelFormat(); + const PixelFormatInfo &info = PixelFormatInfo::info(format); for (const Size &sz : iter.second) { double reqAr = static_cast(req.width) / req.height; @@ -126,12 +155,12 @@ V4L2DeviceFormat findBestMode(const SensorFormats &formatsMap, const Size &req) if (score <= bestScore) { bestScore = score; - bestMode.fourcc = v4l2Format; - bestMode.size = sz; + bestMode.first = format; + bestMode.second = sz; } LOG(RPI, Info) << "Mode: " << sz.width << "x" << sz.height - << " fmt " << v4l2Format.toString() + << " fmt " << format.toString() << " Score: " << score << " (best " << bestScore << ")"; } @@ -364,8 +393,9 @@ CameraConfiguration::Status RPiCameraConfiguration::validate() * Calculate the best sensor mode we can use based on * the user request. */ - V4L2DeviceFormat sensorFormat = findBestMode(data_->sensorFormats_, cfg.size); - int ret = data_->unicam_[Unicam::Image].dev()->tryFormat(&sensorFormat); + SensorMode sensorMode = findBestMode(data_->sensorFormats_, cfg.size); + V4L2DeviceFormat unicamFormat = toV4L2DeviceFormat(sensorMode); + int ret = data_->unicam_[Unicam::Image].dev()->tryFormat(&unicamFormat); if (ret) return Invalid; @@ -377,7 +407,7 @@ CameraConfiguration::Status RPiCameraConfiguration::validate() * fetch the "native" (i.e. untransformed) Bayer order, * because the sensor may currently be flipped! */ - V4L2PixelFormat fourcc = sensorFormat.fourcc; + V4L2PixelFormat fourcc = unicamFormat.fourcc; if (data_->flipsAlterBayerOrder_) { BayerFormat bayer = BayerFormat::fromV4L2PixelFormat(fourcc); bayer.order = data_->nativeBayerOrder_; @@ -386,15 +416,15 @@ CameraConfiguration::Status RPiCameraConfiguration::validate() } PixelFormat sensorPixFormat = fourcc.toPixelFormat(); - if (cfg.size != sensorFormat.size || + if (cfg.size != unicamFormat.size || cfg.pixelFormat != sensorPixFormat) { - cfg.size = sensorFormat.size; + cfg.size = unicamFormat.size; cfg.pixelFormat = sensorPixFormat; status = Adjusted; } - cfg.stride = sensorFormat.planes[0].bpl; - cfg.frameSize = sensorFormat.planes[0].size; + cfg.stride = unicamFormat.planes[0].bpl; + cfg.frameSize = unicamFormat.planes[0].size; rawCount++; } else { @@ -483,7 +513,8 @@ CameraConfiguration *PipelineHandlerRPi::generateConfiguration(Camera *camera, { RPiCameraData *data = cameraData(camera); CameraConfiguration *config = new RPiCameraConfiguration(data); - V4L2DeviceFormat sensorFormat; + V4L2DeviceFormat unicamFormat; + SensorMode sensorMode; unsigned int bufferCount; PixelFormat pixelFormat; V4L2VideoDevice::Formats fmts; @@ -498,8 +529,9 @@ CameraConfiguration *PipelineHandlerRPi::generateConfiguration(Camera *camera, switch (role) { case StreamRole::Raw: size = data->sensor_->resolution(); - sensorFormat = findBestMode(data->sensorFormats_, size); - pixelFormat = sensorFormat.fourcc.toPixelFormat(); + sensorMode = findBestMode(data->sensorFormats_, size); + unicamFormat = toV4L2DeviceFormat(sensorMode); + pixelFormat = sensorMode.first; ASSERT(pixelFormat.isValid()); bufferCount = 2; rawCount++; @@ -609,10 +641,9 @@ int PipelineHandlerRPi::configure(Camera *camera, CameraConfiguration *config) } /* First calculate the best sensor mode we can use based on the user request. */ - V4L2DeviceFormat unicamFormat = findBestMode(data->sensorFormats_, rawStream ? sensorSize : maxSize); - - unsigned int mbus_code = BayerFormat::fromV4L2PixelFormat(unicamFormat.fourcc).toMbusCode(); - V4L2SubdeviceFormat sensorFormat { .mbus_code = mbus_code, .size = unicamFormat.size }; + SensorMode sensorMode = findBestMode(data->sensorFormats_, rawStream ? sensorSize : maxSize); + V4L2SubdeviceFormat sensorFormat = toV4L2SubdeviceFormat(sensorMode); + V4L2DeviceFormat unicamFormat = toV4L2DeviceFormat(sensorMode); ret = data->sensor_->setFormat(&sensorFormat); if (ret)