From patchwork Wed Dec 1 13:40:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Plowman X-Patchwork-Id: 14959 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 46CEABF415 for ; Wed, 1 Dec 2021 13:41:07 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id A489C60724; Wed, 1 Dec 2021 14:41:06 +0100 (CET) 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="ZPg/9Muh"; dkim-atps=neutral Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id B56046011A for ; Wed, 1 Dec 2021 14:41:05 +0100 (CET) Received: by mail-wr1-x429.google.com with SMTP id j3so52381641wrp.1 for ; Wed, 01 Dec 2021 05:41:05 -0800 (PST) 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=+J0tALPcVr3JUOjWtvfRYLeV/hJN8luGThVPxsycQRU=; b=ZPg/9MuhDEesGSD4+0SmuiAxwywxZI79gzQrD1XBIWlqUx7ZHQjM2pOZ0lesX0kLWQ ahfPRC5KpFKZzJuSiIhJWgSX0GOcZHgVcPN5GMpLbUQnWAjfWsV0MY5Ho9BW5NR2IPUv 8OHr44PtwPdEU0CwKQbG8olKsyjxHz9mvpahKlQjvgTAOlmzzHRU2LY7xloHgcp9+mvM vPWxx7UrtGdQnDein0jTjzl9aV28TnzPBN5ZLEki3UVoiFJZagLawrunvNaFTppZa8F4 AkGqou3oAElaJNR5QQh4YdlACaXihvT9O3K1XcV6VkBT/3wUvzCY5b44YO4s3YGhOteN amGQ== 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=+J0tALPcVr3JUOjWtvfRYLeV/hJN8luGThVPxsycQRU=; b=H6uCYsw+DDaDcn5Pea9Bbm01TnY6df6/P4ocyENJcjFYj6v4k6jzfYNDRoBRZaBypG hxMItxttOaN4xIEcRgBhPobycWhHL5VcTEc0k+wJTPNxVuTS19QecNzhgYI7id1NaniH z5I/wvFjKViyOH4ljlB/n2NocDbLIilepGDygX42j/CqcMz0pgGbewKqgMaMDsr54Av8 dine/JunUhTfScOwPkCUe7SJdrzcFahsg6K+93tU49gzJIPhnsXkBCHvxhp1DgDbuqnN yGIXQLxIV82n04enPPmhzDtmRcTHfERyZtXv7LPKabRqOFFg3prD01Cx3EK0sYElu7d8 8TZA== X-Gm-Message-State: AOAM532SCkkZVxaaL5NPp6U35BLTeTra9IYFakoUZRTdx9rpx+ZLAeEa iIsqeH/T6xcjuumRiUCqSTbeLX/QwgRypiB5 X-Google-Smtp-Source: ABdhPJx+HRc6tGiM/8MF7VPSZY3T2cRQhSFBqh/N7iEACul4eJ+RaMTiGw8BwVJvGt8pa+GfkJk/Sw== X-Received: by 2002:adf:fb0c:: with SMTP id c12mr7323368wrr.614.1638366065138; Wed, 01 Dec 2021 05:41:05 -0800 (PST) Received: from pi4-davidp.pitowers.org ([2a00:1098:3142:14:e4a2:3070:eea4:e434]) by smtp.gmail.com with ESMTPSA id a10sm1101384wmq.27.2021.12.01.05.41.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Dec 2021 05:41:04 -0800 (PST) From: David Plowman To: libcamera-devel@lists.libcamera.org Date: Wed, 1 Dec 2021 13:40:48 +0000 Message-Id: <20211201134048.6363-1-david.plowman@raspberrypi.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211201101508.10619-3-david.plowman@raspberrypi.com> References: <20211201101508.10619-3-david.plowman@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3] pipeline: raspberrypi: Choose bit depth and packing according to raw stream 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" When a raw stream is specified, the bit depth and packing requested should influence our choice of camera mode to match (if possible). Signed-off-by: David Plowman Reviewed-by: Kieran Bingham Reviewed-by: Naushir Patuck --- .../pipeline/raspberrypi/raspberrypi.cpp | 38 ++++++++++--------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp index 045725dd..7f387626 100644 --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp @@ -6,6 +6,7 @@ */ #include #include +#include #include #include #include @@ -49,6 +50,8 @@ LOG_DEFINE_CATEGORY(RPI) namespace { +constexpr unsigned int defaultRawBitDepth = 12; + /* Map of mbus codes to supported sizes reported by the sensor. */ using SensorFormats = std::map>; @@ -125,15 +128,13 @@ double scoreFormat(double desired, double actual) return score; } -V4L2SubdeviceFormat findBestFormat(const SensorFormats &formatsMap, const Size &req) +V4L2SubdeviceFormat findBestFormat(const SensorFormats &formatsMap, const Size &req, unsigned int bitDepth) { double bestScore = std::numeric_limits::max(), score; V4L2SubdeviceFormat bestFormat; -#define PENALTY_AR 1500.0 -#define PENALTY_8BIT 2000.0 -#define PENALTY_10BIT 1000.0 -#define PENALTY_12BIT 0.0 + constexpr float penaltyAr = 1500.0; + constexpr float penaltyBitDepth = 500.0; /* Calculate the closest/best mode from the user requested size. */ for (const auto &iter : formatsMap) { @@ -149,15 +150,10 @@ V4L2SubdeviceFormat findBestFormat(const SensorFormats &formatsMap, const Size & /* Score the dimensions for closeness. */ score = scoreFormat(req.width, size.width); score += scoreFormat(req.height, size.height); - score += PENALTY_AR * scoreFormat(reqAr, fmtAr); + score += penaltyAr * scoreFormat(reqAr, fmtAr); /* Add any penalties... this is not an exact science! */ - if (info.bitsPerPixel == 12) - score += PENALTY_12BIT; - else if (info.bitsPerPixel == 10) - score += PENALTY_10BIT; - else if (info.bitsPerPixel == 8) - score += PENALTY_8BIT; + score += std::abs(static_cast(info.bitsPerPixel - bitDepth)) * penaltyBitDepth; if (score <= bestScore) { bestScore = score; @@ -397,9 +393,14 @@ CameraConfiguration::Status RPiCameraConfiguration::validate() * Calculate the best sensor mode we can use based on * the user request. */ - V4L2SubdeviceFormat sensorFormat = findBestFormat(data_->sensorFormats_, cfg.size); + const PixelFormatInfo &info = PixelFormatInfo::info(cfg.pixelFormat); + unsigned int bitDepth = info.isValid() ? info.bitsPerPixel : defaultRawBitDepth; + V4L2SubdeviceFormat sensorFormat = findBestFormat(data_->sensorFormats_, cfg.size, bitDepth); + BayerFormat::Packing packing = BayerFormat::Packing::CSI2; + if (info.isValid() && !info.packed) + packing = BayerFormat::Packing::None; V4L2DeviceFormat unicamFormat = toV4L2DeviceFormat(sensorFormat, - BayerFormat::Packing::CSI2); + packing); int ret = data_->unicam_[Unicam::Image].dev()->tryFormat(&unicamFormat); if (ret) return Invalid; @@ -533,7 +534,7 @@ CameraConfiguration *PipelineHandlerRPi::generateConfiguration(Camera *camera, switch (role) { case StreamRole::Raw: size = data->sensor_->resolution(); - sensorFormat = findBestFormat(data->sensorFormats_, size); + sensorFormat = findBestFormat(data->sensorFormats_, size, defaultRawBitDepth); pixelFormat = mbusCodeToPixelFormat(sensorFormat.mbus_code, BayerFormat::Packing::CSI2); ASSERT(pixelFormat.isValid()); @@ -622,6 +623,7 @@ int PipelineHandlerRPi::configure(Camera *camera, CameraConfiguration *config) Size maxSize, sensorSize; unsigned int maxIndex = 0; bool rawStream = false; + unsigned int bitDepth = defaultRawBitDepth; /* * Look for the RAW stream (if given) size as well as the largest @@ -638,7 +640,9 @@ int PipelineHandlerRPi::configure(Camera *camera, CameraConfiguration *config) sensorSize = cfg.size; rawStream = true; /* Check if the user has explicitly set an unpacked format. */ - packing = BayerFormat::fromPixelFormat(cfg.pixelFormat).packing; + BayerFormat bayerFormat = BayerFormat::fromPixelFormat(cfg.pixelFormat); + packing = bayerFormat.packing; + bitDepth = bayerFormat.bitDepth; } else { if (cfg.size > maxSize) { maxSize = config->at(i).size; @@ -664,7 +668,7 @@ int PipelineHandlerRPi::configure(Camera *camera, CameraConfiguration *config) } /* First calculate the best sensor mode we can use based on the user request. */ - V4L2SubdeviceFormat sensorFormat = findBestFormat(data->sensorFormats_, rawStream ? sensorSize : maxSize); + V4L2SubdeviceFormat sensorFormat = findBestFormat(data->sensorFormats_, rawStream ? sensorSize : maxSize, bitDepth); ret = data->sensor_->setFormat(&sensorFormat); if (ret) return ret;