{"id":21803,"url":"https://patchwork.libcamera.org/api/patches/21803/?format=json","web_url":"https://patchwork.libcamera.org/patch/21803/","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":"<20241103152205.29219-7-hdegoede@redhat.com>","date":"2024-11-03T15:22:03","name":"[6/8] libcamera: swstats_cpu: Add support for YUV420","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"548fe88e0395591ae58904cf8c0d73e1f6c6710a","submitter":{"id":102,"url":"https://patchwork.libcamera.org/api/people/102/?format=json","name":"Hans de Goede","email":"hdegoede@redhat.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/21803/mbox/","series":[{"id":4764,"url":"https://patchwork.libcamera.org/api/series/4764/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4764","date":"2024-11-03T15:21:57","name":"libcamera: Add swstats_cpu::processFrame() and atomisp pipeline handler","version":1,"mbox":"https://patchwork.libcamera.org/series/4764/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/21803/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/21803/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 340DDC330D\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSun,  3 Nov 2024 15:22:27 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 4DD4A653CA;\n\tSun,  3 Nov 2024 16:22:26 +0100 (CET)","from us-smtp-delivery-124.mimecast.com\n\t(us-smtp-delivery-124.mimecast.com [170.10.129.124])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 177F8653CF\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun,  3 Nov 2024 16:22:24 +0100 (CET)","from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com\n\t(ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63])\n\tby relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3,\n\tcipher=TLS_AES_256_GCM_SHA384) id us-mta-161-07NtK2A2MqSbyMYNelvKbw-1;\n\tSun, 03 Nov 2024 10:22:21 -0500","from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com\n\t(mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com\n\t[10.30.177.15])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\tkey-exchange X25519 server-signature RSA-PSS (2048 bits)\n\tserver-digest SHA256) (No client certificate requested)\n\tby mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix)\n\twith ESMTPS\n\tid C306B19560B8 for <libcamera-devel@lists.libcamera.org>;\n\tSun,  3 Nov 2024 15:22:20 +0000 (UTC)","from shalem.redhat.com (unknown [10.39.192.77])\n\tby mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix)\n\twith ESMTP id 4FF151956086; Sun,  3 Nov 2024 15:22:19 +0000 (UTC)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=redhat.com header.i=@redhat.com\n\theader.b=\"amxk7oST\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1730647343;\n\th=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n\tto:to:cc:cc:mime-version:mime-version:content-type:content-type:\n\tcontent-transfer-encoding:content-transfer-encoding:\n\tin-reply-to:in-reply-to:references:references;\n\tbh=IXNsfpbzqKwKq9ekhT0U9ueoSskyeTqir9T4wT5jxiw=;\n\tb=amxk7oSTc1fGQ0wiZF5+juAk4HLjPk6DxU1yk9k+DQE6unS1LTWCjQs7T3rGC2Ct8rZbLY\n\t8nJ0QchwhnlTkeXZVzHopwmlLrwD0P8f8uerXlMfd/ZKQIofZX0OLTb3eNFbnC8WW/NUZ9\n\t2yNOfuOtBUc9/IVWp2LdXXxn14JySqs=","X-MC-Unique":"07NtK2A2MqSbyMYNelvKbw-1","From":"Hans de Goede <hdegoede@redhat.com>","To":"libcamera-devel@lists.libcamera.org","Cc":"Milan Zamazal <mzamazal@redhat.com>, Maxime Ripard <mripard@redhat.com>, \n\tHans de Goede <hdegoede@redhat.com>","Subject":"[PATCH 6/8] libcamera: swstats_cpu: Add support for YUV420","Date":"Sun,  3 Nov 2024 16:22:03 +0100","Message-ID":"<20241103152205.29219-7-hdegoede@redhat.com>","In-Reply-To":"<20241103152205.29219-1-hdegoede@redhat.com>","References":"<20241103152205.29219-1-hdegoede@redhat.com>","MIME-Version":"1.0","X-Scanned-By":"MIMEDefang 3.0 on 10.30.177.15","X-Mimecast-Spam-Score":"0","X-Mimecast-Originator":"redhat.com","Content-Transfer-Encoding":"8bit","Content-Type":"text/plain; charset=\"US-ASCII\"; x-default=true","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>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Add support for processing YUV420 data.\n\nSigned-off-by: Hans de Goede <hdegoede@redhat.com>\n---\n .../internal/software_isp/swstats_cpu.h       |  6 ++\n src/libcamera/software_isp/swstats_cpu.cpp    | 89 +++++++++++++++++++\n 2 files changed, 95 insertions(+)","diff":"diff --git a/include/libcamera/internal/software_isp/swstats_cpu.h b/include/libcamera/internal/software_isp/swstats_cpu.h\nindex fa47cec9..a043861c 100644\n--- a/include/libcamera/internal/software_isp/swstats_cpu.h\n+++ b/include/libcamera/internal/software_isp/swstats_cpu.h\n@@ -71,6 +71,7 @@ public:\n private:\n \tusing statsProcessFn = void (SwStatsCpu::*)(const uint8_t *src[]);\n \tusing processFrameFn = void (SwStatsCpu::*)(MappedFrameBuffer &in);\n+\tusing finishFrameFn = void (SwStatsCpu::*)();\n \n \tint setupStandardBayerOrder(BayerFormat::Order order);\n \t/* Bayer 8 bpp unpacked */\n@@ -82,10 +83,15 @@ private:\n \t/* Bayer 10 bpp packed */\n \tvoid statsBGGR10PLine0(const uint8_t *src[]);\n \tvoid statsGBRG10PLine0(const uint8_t *src[]);\n+\t/* YUV420 3 planes */\n+\tvoid statsYUV420Line0(const uint8_t *src[]);\n \n \tvoid processBayerFrame2(MappedFrameBuffer &in);\n+\tvoid processYUV420Frame(MappedFrameBuffer &in);\n+\tvoid finishYUV420Frame();\n \n \tprocessFrameFn processFrame_;\n+\tfinishFrameFn finishFrame_;\n \n \t/* Variables set by configure(), used every line */\n \tstatsProcessFn stats0_;\ndiff --git a/src/libcamera/software_isp/swstats_cpu.cpp b/src/libcamera/software_isp/swstats_cpu.cpp\nindex 1ff15f5b..e81c96a2 100644\n--- a/src/libcamera/software_isp/swstats_cpu.cpp\n+++ b/src/libcamera/software_isp/swstats_cpu.cpp\n@@ -13,6 +13,7 @@\n \n #include <libcamera/base/log.h>\n \n+#include <libcamera/formats.h>\n #include <libcamera/stream.h>\n \n #include \"libcamera/internal/bayer_format.h\"\n@@ -288,6 +289,40 @@ void SwStatsCpu::statsGBRG10PLine0(const uint8_t *src[])\n \tSWSTATS_FINISH_LINE_STATS()\n }\n \n+void SwStatsCpu::statsYUV420Line0(const uint8_t *src[])\n+{\n+\tuint64_t sumY = 0;\n+\tuint64_t sumU = 0;\n+\tuint64_t sumV = 0;\n+\tuint8_t y, u, v;\n+\n+\t/* Adjust src[] for starting at window_.x */\n+\tsrc[0] += window_.x;\n+\tsrc[1] += window_.x / 2;\n+\tsrc[2] += window_.x / 2;\n+\n+\t/* x += 4 sample every other 2x2 block */\n+\tfor (int x = 0; x < (int)window_.width; x += 4) {\n+\t\t/*\n+\t\t * Take y from the top left corner of the 2x2 block instead\n+\t\t * of averaging 4 y-s.\n+\t\t */\n+\t\ty = src[0][x];\n+\t\tu = src[1][x];\n+\t\tv = src[2][x];\n+\n+\t\tsumY += y;\n+\t\tsumU += u;\n+\t\tsumV += v;\n+\n+\t\tstats_.yHistogram[y * SwIspStats::kYHistogramSize / 256]++;\n+\t}\n+\n+\tstats_.sumR_ += sumY;\n+\tstats_.sumG_ += sumU;\n+\tstats_.sumB_ += sumV;\n+}\n+\n /**\n  * \\brief Reset state to start statistics gathering for a new frame\n  *\n@@ -313,6 +348,9 @@ void SwStatsCpu::startFrame(void)\n  */\n void SwStatsCpu::finishFrame(uint32_t frame, uint32_t bufferId)\n {\n+\tif (finishFrame_)\n+\t\t(this->*finishFrame_)();\n+\n \t*sharedStats_ = stats_;\n \tstatsReady.emit(frame, bufferId);\n }\n@@ -362,6 +400,20 @@ int SwStatsCpu::setupStandardBayerOrder(BayerFormat::Order order)\n int SwStatsCpu::configure(const StreamConfiguration &inputCfg)\n {\n \tstride_ = inputCfg.stride;\n+\tfinishFrame_ = NULL;\n+\n+\tif (inputCfg.pixelFormat == formats::YUV420) {\n+\t\tpatternSize_.height = 2;\n+\t\tpatternSize_.width = 2;\n+\t\t/* Skip every 3th and 4th line, sample every other 2x2 block */\n+\t\tySkipMask_ = 0x02;\n+\t\txShift_ = 0;\n+\t\tswapLines_ = false;\n+\t\tstats0_ = &SwStatsCpu::statsYUV420Line0;\n+\t\tprocessFrame_ = &SwStatsCpu::processYUV420Frame;\n+\t\tfinishFrame_ = &SwStatsCpu::finishYUV420Frame;\n+\t\treturn 0;\n+\t}\n \n \tBayerFormat bayerFormat =\n \t\tBayerFormat::fromPixelFormat(inputCfg.pixelFormat);\n@@ -430,6 +482,43 @@ void SwStatsCpu::setWindow(const Rectangle &window)\n \twindow_.height &= ~(patternSize_.height - 1);\n }\n \n+void SwStatsCpu::processYUV420Frame(MappedFrameBuffer &in)\n+{\n+\tconst uint8_t *linePointers[3];\n+\n+\tlinePointers[0] = in.planes()[0].data();\n+\tlinePointers[1] = in.planes()[1].data();\n+\tlinePointers[2] = in.planes()[2].data();\n+\n+\t/* Adjust linePointers for starting at window_.y */\n+\tlinePointers[0] += window_.y * stride_;\n+\tlinePointers[1] += window_.y * stride_ / 4;\n+\tlinePointers[2] += window_.y * stride_ / 4;\n+\n+\tfor (unsigned int y = 0; y < window_.height; y += 2) {\n+\t\tif (!(y & ySkipMask_))\n+\t\t\t(this->*stats0_)(linePointers);\n+\n+\t\tlinePointers[0] += stride_ * 2;\n+\t\tlinePointers[1] += stride_ / 2;\n+\t\tlinePointers[2] += stride_ / 2;\n+\t}\n+}\n+\n+void SwStatsCpu::finishYUV420Frame()\n+{\n+\t/* sumR_ / G_ / B_ contain Y / U / V sums convert this */\n+\tdouble divider = (uint64_t)window_.width * window_.height * 256 / 16;\n+\tdouble Y = (double)stats_.sumR_ / divider;\n+\t/* U and V 0 - 255 values represent -128 - 127 range */\n+\tdouble U = (double)stats_.sumG_ / divider - 0.5;\n+\tdouble V = (double)stats_.sumB_ / divider - 0.5;\n+\n+\tstats_.sumR_ = (Y + 1.140 * V) * divider;\n+\tstats_.sumG_ = (Y - 0.395 * U - 0.581 * V) * divider;\n+\tstats_.sumB_ = (Y + 2.032 * U) * divider;\n+}\n+\n void SwStatsCpu::processBayerFrame2(MappedFrameBuffer &in)\n {\n \tconst uint8_t *src = in.planes()[0].data();\n","prefixes":["6/8"]}