[{"id":28963,"web_url":"https://patchwork.libcamera.org/comment/28963/","msgid":"<20240314154910.d6hmp42h5fiafluj@jasper>","date":"2024-03-14T15:49:10","subject":"Re: [PATCH v5 15/18] libcamera: debayer_cpu: Add BGR888 output\n\tsupport","submitter":{"id":184,"url":"https://patchwork.libcamera.org/api/people/184/","name":"Stefan Klug","email":"stefan.klug@ideasonboard.com"},"content":"Hi Hans,\n\nthanks you for the patch.\n\nOn Mon, Mar 11, 2024 at 03:15:19PM +0100, Hans de Goede wrote:\n> BGR888 is RGB888 with the red and blue pixels swapped, adjust\n> the debayering to swap the red and blue pixels in the bayer pattern\n> to add support for writing formats::BGR888.\n> \n> Tested-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org> # sc8280xp Lenovo x13s\n> Tested-by: Pavel Machek <pavel@ucw.cz>\n> Reviewed-by: Milan Zamazal <mzamazal@redhat.com>\n> Signed-off-by: Hans de Goede <hdegoede@redhat.com>\n> ---\n>  src/libcamera/software_isp/debayer_cpu.cpp | 42 +++++++++++++++++++---\n>  src/libcamera/software_isp/debayer_cpu.h   |  1 +\n>  2 files changed, 38 insertions(+), 5 deletions(-)\n> \n> diff --git a/src/libcamera/software_isp/debayer_cpu.cpp b/src/libcamera/software_isp/debayer_cpu.cpp\n> index eb1c2718..a1692693 100644\n> --- a/src/libcamera/software_isp/debayer_cpu.cpp\n> +++ b/src/libcamera/software_isp/debayer_cpu.cpp\n> @@ -268,7 +268,7 @@ int DebayerCpu::getInputConfig(PixelFormat inputFormat, DebayerInputConfig &conf\n>  \t\tconfig.bpp = (bayerFormat.bitDepth + 7) & ~7;\n>  \t\tconfig.patternSize.width = 2;\n>  \t\tconfig.patternSize.height = 2;\n> -\t\tconfig.outputFormats = std::vector<PixelFormat>({ formats::RGB888 });\n> +\t\tconfig.outputFormats = std::vector<PixelFormat>({ formats::RGB888, formats::BGR888 });\n>  \t\treturn 0;\n>  \t}\n>  \n> @@ -278,7 +278,7 @@ int DebayerCpu::getInputConfig(PixelFormat inputFormat, DebayerInputConfig &conf\n>  \t\tconfig.bpp = 10;\n>  \t\tconfig.patternSize.width = 4; /* 5 bytes per *4* pixels */\n>  \t\tconfig.patternSize.height = 2;\n> -\t\tconfig.outputFormats = std::vector<PixelFormat>({ formats::RGB888 });\n> +\t\tconfig.outputFormats = std::vector<PixelFormat>({ formats::RGB888, formats::BGR888 });\n>  \t\treturn 0;\n>  \t}\n>  \n> @@ -289,7 +289,7 @@ int DebayerCpu::getInputConfig(PixelFormat inputFormat, DebayerInputConfig &conf\n>  \n>  int DebayerCpu::getOutputConfig(PixelFormat outputFormat, DebayerOutputConfig &config)\n>  {\n> -\tif (outputFormat == formats::RGB888) {\n> +\tif (outputFormat == formats::RGB888 || outputFormat == formats::BGR888) {\n>  \t\tconfig.bpp = 24;\n>  \t\treturn 0;\n>  \t}\n> @@ -325,13 +325,41 @@ int DebayerCpu::setupStandardBayerOrder(BayerFormat::Order order)\n>  \treturn 0;\n>  }\n>  \n> -/* TODO: this ignores outputFormat since there is only 1 supported outputFormat for now */\n> -int DebayerCpu::setDebayerFunctions(PixelFormat inputFormat, [[maybe_unused]] PixelFormat outputFormat)\n> +int DebayerCpu::setDebayerFunctions(PixelFormat inputFormat, PixelFormat outputFormat)\n>  {\n>  \tBayerFormat bayerFormat =\n>  \t\tBayerFormat::fromPixelFormat(inputFormat);\n>  \n>  \txShift_ = 0;\n> +\tswapRedBlueGains_ = false;\n> +\n> +\tswitch (outputFormat) {\n> +\tcase formats::RGB888:\n> +\t\tbreak;\n> +\tcase formats::BGR888:\n> +\t\t/* Swap R and B in bayer order to generate BGR888 instead of RGB888 */\n> +\t\tswapRedBlueGains_ = true;\n> +\n> +\t\tswitch (bayerFormat.order) {\n> +\t\tcase BayerFormat::BGGR:\n> +\t\t\tbayerFormat.order = BayerFormat::RGGB;\n> +\t\t\tbreak;\n> +\t\tcase BayerFormat::GBRG:\n> +\t\t\tbayerFormat.order = BayerFormat::GRBG;\n> +\t\t\tbreak;\n> +\t\tcase BayerFormat::GRBG:\n> +\t\t\tbayerFormat.order = BayerFormat::GBRG;\n> +\t\t\tbreak;\n> +\t\tcase BayerFormat::RGGB:\n> +\t\t\tbayerFormat.order = BayerFormat::BGGR;\n> +\t\t\tbreak;\n> +\t\tdefault:\n> +\t\t\tgoto invalid_fmt;\n\nAs Milan already mentioned, this looks quite scary. Still I see that\nit's a valid trick to get the expeced output without touching all the\nother logic. To my understanding it's completely local to this class.\n\nReviewed-by: Stefan Klug <stefan.klug@ideasonboard.com> \n\nCheers,\nStefan\n\n> +\t\t}\n> +\t\tbreak;\n> +\tdefault:\n> +\t\tgoto invalid_fmt;\n> +\t}\n>  \n>  \tif ((bayerFormat.bitDepth == 8 || bayerFormat.bitDepth == 10 || bayerFormat.bitDepth == 12) &&\n>  \t    bayerFormat.packing == BayerFormat::Packing::None &&\n> @@ -378,6 +406,7 @@ int DebayerCpu::setDebayerFunctions(PixelFormat inputFormat, [[maybe_unused]] Pi\n>  \t\t}\n>  \t}\n>  \n> +invalid_fmt:\n>  \tLOG(Debayer, Error) << \"Unsupported input output format combination\";\n>  \treturn -EINVAL;\n>  }\n> @@ -661,6 +690,9 @@ void DebayerCpu::process(FrameBuffer *input, FrameBuffer *output, DebayerParams\n>  \t\tgamma_correction_ = params.gamma;\n>  \t}\n>  \n> +\tif (swapRedBlueGains_)\n> +\t\tstd::swap(params.gainR, params.gainB);\n> +\n>  \tfor (unsigned int i = 0; i < kRGBLookupSize; i++) {\n>  \t\tconstexpr unsigned int div =\n>  \t\t\tkRGBLookupSize * DebayerParams::kGain10 / kGammaLookupSize;\n> diff --git a/src/libcamera/software_isp/debayer_cpu.h b/src/libcamera/software_isp/debayer_cpu.h\n> index fd1fa180..5f44fc65 100644\n> --- a/src/libcamera/software_isp/debayer_cpu.h\n> +++ b/src/libcamera/software_isp/debayer_cpu.h\n> @@ -145,6 +145,7 @@ private:\n>  \tunsigned int lineBufferIndex_;\n>  \tunsigned int xShift_; /* Offset of 0/1 applied to window_.x */\n>  \tbool enableInputMemcpy_;\n> +\tbool swapRedBlueGains_;\n>  \tfloat gamma_correction_;\n>  \tunsigned int measuredFrames_;\n>  \tint64_t frameProcessTime_;\n> -- \n> 2.44.0\n>","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 D36A0BD808\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 14 Mar 2024 15:49:15 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 0BAE06286C;\n\tThu, 14 Mar 2024 16:49:15 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 8928A61C65\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 14 Mar 2024 16:49:13 +0100 (CET)","from ideasonboard.com (unknown\n\t[IPv6:2a00:6020:448c:6c00:c467:7cd1:67a9:bc4c])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 2511D675;\n\tThu, 14 Mar 2024 16:48:50 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"jlmO2pdF\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1710431330;\n\tbh=wO5wmGPG96GJ40PZ3K4GEGpGi8A7yVe/1qppJn5F9Qc=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=jlmO2pdFhc/ZqbPgusgp8DQp/kvEWVAY1S7gAAcxn+6cdpr4qnoWxDeodPR11rnuZ\n\tPnl/YD75reVX67yJ8SzJ2tpImsy5hlomi6Avf0sqnp633Um4Kx3pZV3QsVrFJgp96L\n\tvPQa2i0bZJL03vAlr6AMrWJDqONZj4LwP9oYrr0o=","Date":"Thu, 14 Mar 2024 16:49:10 +0100","From":"Stefan Klug <stefan.klug@ideasonboard.com>","To":"Hans de Goede <hdegoede@redhat.com>","Subject":"Re: [PATCH v5 15/18] libcamera: debayer_cpu: Add BGR888 output\n\tsupport","Message-ID":"<20240314154910.d6hmp42h5fiafluj@jasper>","References":"<20240311141524.27192-1-hdegoede@redhat.com>\n\t<20240311141524.27192-16-hdegoede@redhat.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20240311141524.27192-16-hdegoede@redhat.com>","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>","Cc":"Bryan O'Donoghue <bryan.odonoghue@linaro.org>,\n\tlibcamera-devel@lists.libcamera.org, Maxime Ripard <mripard@redhat.com>, \n\tPavel Machek <pavel@ucw.cz>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]