{"id":10426,"url":"https://patchwork.libcamera.org/api/1.1/patches/10426/?format=json","web_url":"https://patchwork.libcamera.org/patch/10426/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/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":"<20201116120256.149789-1-naush@raspberrypi.com>","date":"2020-11-16T12:02:56","name":"[libcamera-devel] pipeline: raspberrypi: Rework bayer/embedded data buffer matching","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"7a8f21e9478b524496379677eabfb152536ef020","submitter":{"id":34,"url":"https://patchwork.libcamera.org/api/1.1/people/34/?format=json","name":"Naushir Patuck","email":"naush@raspberrypi.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/10426/mbox/","series":[{"id":1462,"url":"https://patchwork.libcamera.org/api/1.1/series/1462/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1462","date":"2020-11-16T12:02:56","name":"[libcamera-devel] pipeline: raspberrypi: Rework bayer/embedded data buffer matching","version":1,"mbox":"https://patchwork.libcamera.org/series/1462/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/10426/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/10426/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 89E3CBE081\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 16 Nov 2020 12:05:36 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 1B868632C3;\n\tMon, 16 Nov 2020 13:05:36 +0100 (CET)","from mail-wm1-x330.google.com (mail-wm1-x330.google.com\n\t[IPv6:2a00:1450:4864:20::330])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 70745631B5\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 16 Nov 2020 13:05:34 +0100 (CET)","by mail-wm1-x330.google.com with SMTP id 23so11886915wmg.1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 16 Nov 2020 04:05:34 -0800 (PST)","from naushir-VirtualBox.patuck.local ([88.97.76.4])\n\tby smtp.gmail.com with ESMTPSA id\n\tg11sm22763112wrq.7.2020.11.16.04.05.32\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tMon, 16 Nov 2020 04:05:33 -0800 (PST)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (2048-bit key;\n\tunprotected) header.d=raspberrypi.com header.i=@raspberrypi.com\n\theader.b=\"kLA7XOZ6\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=raspberrypi.com; s=google;\n\th=from:to:cc:subject:date:message-id:mime-version\n\t:content-transfer-encoding;\n\tbh=qy10y6LWOCrKeIBP9nu8EToJY6Yojyfq2Ud7V0kTQ6A=;\n\tb=kLA7XOZ60gE6ZHBn0zedmIgesKF67fxdzvcOMdhhOWb1/2z9Nv9tTKqbobT4FPQ0/z\n\tI1BNdKtHuhXRywxIuz0iWdlmfQjKffZ9qLwViuuLE0F+4KybJSxOfBbDbpo6c4WpV8dn\n\tfILAztOcoJ1XKI69gMb+qJMtSA5OLqjGFu63NC1LAW0Te9vh9u5SMyQki+xHR34Cbbg4\n\thFlyAhQDFJCz59ZQ12HgCpzdyrPebUZr11HZvSc3YdYFmiXKjBdXK6Gfo+31AvcYlOd/\n\tzTCP5A/eTYsN+HLCUf3Q6hxl8aSWmg5VM0qxtiY5bSA9CP0CoK7WlYmgo99a9Zt72k5b\n\tb0mQ==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version\n\t:content-transfer-encoding;\n\tbh=qy10y6LWOCrKeIBP9nu8EToJY6Yojyfq2Ud7V0kTQ6A=;\n\tb=c5Jd1GS152CTOu4OvXBEIXlQ0KdJOPdtormiN0ni2dx1v4D6e3/6yS52N3lciNN2mi\n\ttBK4rbWJAR7psERaB5+N3lnxbk+5pkEh17Rh3t8qW3qDMXO0Q5+Bg/tt7Usa1IeH64HV\n\tP4WJeoJke3a+7Xq7pRzXiEy7KpFfOdeB6t6nkfxZ/GumP7MgTMU1VDxC9ZKUDQ93Kz4+\n\tgqSI/JBy45z6jUWqjlFJQixrpYk4zNn4CalU8o6/16afGPvty79qQQiOV4pt3u88uAR/\n\tTP/rSa9QPp5+mldrD2WIgJZOOUTgzxvPfDPcSH6ocqnuDuIo1gquUJ3bq7GZNNhEUKXq\n\tLtog==","X-Gm-Message-State":"AOAM533WDcPmxhDTS8otUQhtAy9Ms8pwjOB0gCfjbmNjXmCQW+KrZNzM\n\tiErKJbtcx+hGGtNiMVO8Uo9d09EtQ0iAQw==","X-Google-Smtp-Source":"ABdhPJzbNVSzARHykkqX5koURB4Nsq1npoinLGJur331YiAJEihFdh1OzZBAgou1+RGsKsq3p9A2lg==","X-Received":"by 2002:a1c:4957:: with SMTP id\n\tw84mr15018456wma.84.1605528333666; \n\tMon, 16 Nov 2020 04:05:33 -0800 (PST)","From":"Naushir Patuck <naush@raspberrypi.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Mon, 16 Nov 2020 12:02:56 +0000","Message-Id":"<20201116120256.149789-1-naush@raspberrypi.com>","X-Mailer":"git-send-email 2.25.1","MIME-Version":"1.0","Subject":"[libcamera-devel] [PATCH] pipeline: raspberrypi: Rework\n\tbayer/embedded data buffer matching","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":"There is a condition that would cause the buffers to never be in sync\nwhen we using only a single buffer in the bayer and embedded data streams.\nThis occurred because even though both streams would get flushed to resync,\none stream's only buffer was already queued in the device, and would end\nup never matching.\n\nRework the buffer matching logic by combining updateQueue() and\ntryFlushQueue() into a single function findMatchingBuffers(). This would\nallow us to flush the queues at the same time as we match buffers, avoiding\nthe the above condition.\n\nSigned-off-by: Naushir Patuck <naush@raspberrypi.com>\n---\n .../pipeline/raspberrypi/raspberrypi.cpp      | 176 +++++++++---------\n 1 file changed, 88 insertions(+), 88 deletions(-)","diff":"diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\nindex 7ad66f21..26c6731c 100644\n--- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n+++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n@@ -205,9 +205,7 @@ public:\n private:\n \tvoid checkRequestCompleted();\n \tvoid tryRunPipeline();\n-\tvoid tryFlushQueues();\n-\tFrameBuffer *updateQueue(std::queue<FrameBuffer *> &q, uint64_t timestamp,\n-\t\t\t\t RPi::Stream *stream);\n+\tbool findMatchingBuffers(FrameBuffer *&bayerBuffer, FrameBuffer *&embeddedBuffer);\n \n \tunsigned int ispOutputCount_;\n };\n@@ -1537,7 +1535,6 @@ void RPiCameraData::handleState()\n \n \tcase State::Idle:\n \t\ttryRunPipeline();\n-\t\ttryFlushQueues();\n \t\tbreak;\n \t}\n }\n@@ -1588,41 +1585,8 @@ void RPiCameraData::tryRunPipeline()\n \t    bayerQueue_.empty() || embeddedQueue_.empty())\n \t\treturn;\n \n-\t/* Start with the front of the bayer buffer queue. */\n-\tbayerBuffer = bayerQueue_.front();\n-\n-\t/*\n-\t * Find the embedded data buffer with a matching timestamp to pass to\n-\t * the IPA. Any embedded buffers with a timestamp lower than the\n-\t * current bayer buffer will be removed and re-queued to the driver.\n-\t */\n-\tembeddedBuffer = updateQueue(embeddedQueue_, bayerBuffer->metadata().timestamp,\n-\t\t\t\t     &unicam_[Unicam::Embedded]);\n-\n-\tif (!embeddedBuffer) {\n-\t\tLOG(RPI, Debug) << \"Could not find matching embedded buffer\";\n-\n-\t\t/*\n-\t\t * Look the other way, try to match a bayer buffer with the\n-\t\t * first embedded buffer in the queue. This will also do some\n-\t\t * housekeeping on the bayer image queue - clear out any\n-\t\t * buffers that are older than the first buffer in the embedded\n-\t\t * queue.\n-\t\t *\n-\t\t * But first check if the embedded queue has emptied out.\n-\t\t */\n-\t\tif (embeddedQueue_.empty())\n-\t\t\treturn;\n-\n-\t\tembeddedBuffer = embeddedQueue_.front();\n-\t\tbayerBuffer = updateQueue(bayerQueue_, embeddedBuffer->metadata().timestamp,\n-\t\t\t\t\t  &unicam_[Unicam::Image]);\n-\n-\t\tif (!bayerBuffer) {\n-\t\t\tLOG(RPI, Debug) << \"Could not find matching bayer buffer - ending.\";\n-\t\t\treturn;\n-\t\t}\n-\t}\n+\tif (!findMatchingBuffers(bayerBuffer, embeddedBuffer))\n+\t\treturn;\n \n \t/* Take the first request from the queue and action the IPA. */\n \tRequest *request = requestQueue_.front();\n@@ -1665,10 +1629,6 @@ void RPiCameraData::tryRunPipeline()\n \top.controls = { request->controls() };\n \tipa_->processEvent(op);\n \n-\t/* Ready to use the buffers, pop them off the queue. */\n-\tbayerQueue_.pop();\n-\tembeddedQueue_.pop();\n-\n \t/* Set our state to say the pipeline is active. */\n \tstate_ = State::Busy;\n \n@@ -1685,62 +1645,102 @@ void RPiCameraData::tryRunPipeline()\n \tipa_->processEvent(op);\n }\n \n-void RPiCameraData::tryFlushQueues()\n+bool RPiCameraData::findMatchingBuffers(FrameBuffer *&bayerBuffer, FrameBuffer *&embeddedBuffer)\n {\n-\t/*\n-\t * It is possible for us to end up in a situation where all available\n-\t * Unicam buffers have been dequeued but do not match. This can happen\n-\t * when the system is heavily loaded and we get out of lock-step with\n-\t * the two channels.\n-\t *\n-\t * In such cases, the best thing to do is the re-queue all the buffers\n-\t * and give a chance for the hardware to return to lock-step. We do have\n-\t * to drop all interim frames.\n-\t */\n-\tif (unicam_[Unicam::Image].getBuffers().size() == bayerQueue_.size() &&\n-\t    unicam_[Unicam::Embedded].getBuffers().size() == embeddedQueue_.size()) {\n-\t\t/* This cannot happen when Unicam streams are external. */\n-\t\tassert(!unicam_[Unicam::Image].isExternal());\n+\tunsigned int embeddedRequeueCount = 0, bayerRequeueCount = 0;\n \n-\t\tLOG(RPI, Warning) << \"Flushing all buffer queues!\";\n-\n-\t\twhile (!bayerQueue_.empty()) {\n-\t\t\tunicam_[Unicam::Image].queueBuffer(bayerQueue_.front());\n-\t\t\tbayerQueue_.pop();\n-\t\t}\n+\t/* Loop until we find a matching bayer and embedded data buffer. */\n+\twhile (!bayerQueue_.empty()) {\n+\t\t/* Start with the front of the bayer queue. */\n+\t\tbayerBuffer = bayerQueue_.front();\n \n+\t\t/*\n+\t\t* Find the embedded data buffer with a matching timestamp to pass to\n+\t\t* the IPA. Any embedded buffers with a timestamp lower than the\n+\t\t* current bayer buffer will be removed and re-queued to the driver.\n+\t\t*/\n+\t\tuint64_t ts = bayerBuffer->metadata().timestamp;\n+\t\tembeddedBuffer = nullptr;\n \t\twhile (!embeddedQueue_.empty()) {\n-\t\t\tunicam_[Unicam::Embedded].queueBuffer(embeddedQueue_.front());\n-\t\t\tembeddedQueue_.pop();\n+\t\t\tFrameBuffer *b = embeddedQueue_.front();\n+\t\t\tif (!unicam_[Unicam::Embedded].isExternal() && b->metadata().timestamp < ts) {\n+\t\t\t\tembeddedQueue_.pop();\n+\t\t\t\tunicam_[Unicam::Embedded].queueBuffer(b);\n+\t\t\t\tembeddedRequeueCount++;\n+\t\t\t\tLOG(RPI, Warning) << \"Dropping unmatched input frame in stream \"\n+\t\t\t\t\t\t  << unicam_[Unicam::Embedded].name();\n+\t\t\t} else if (unicam_[Unicam::Embedded].isExternal() || b->metadata().timestamp == ts) {\n+\t\t\t\t/* The calling function will pop the item from the queue. */\n+\t\t\t\tembeddedBuffer = b;\n+\t\t\t\tbreak;\n+\t\t\t} else {\n+\t\t\t\tbreak; /* Only higher timestamps from here. */\n+\t\t\t}\n \t\t}\n-\t}\n-}\n \n-FrameBuffer *RPiCameraData::updateQueue(std::queue<FrameBuffer *> &q, uint64_t timestamp,\n-\t\t\t\t\tRPi::Stream *stream)\n-{\n-\t/*\n-\t * If the unicam streams are external (both have be to the same), then we\n-\t * can only return out the top buffer in the queue, and assume they have\n-\t * been synced by queuing at the same time. We cannot drop these frames,\n-\t * as they may have been provided externally.\n-\t */\n-\twhile (!q.empty()) {\n-\t\tFrameBuffer *b = q.front();\n-\t\tif (!stream->isExternal() && b->metadata().timestamp < timestamp) {\n-\t\t\tq.pop();\n-\t\t\tstream->queueBuffer(b);\n+\t\tif (!embeddedBuffer) {\n+\t\t\tLOG(RPI, Debug) << \"Could not find matching embedded buffer\";\n+\n+\t\t\t/*\n+\t\t\t * If we have requeued all available embedded data buffers in this loop,\n+\t\t\t * then we are fully out of sync, so might as well requeue all the pending\n+\t\t\t * bayer buffers.\n+\t\t\t */\n+\t\t\tif (embeddedRequeueCount == unicam_[Unicam::Embedded].getBuffers().size()) {\n+\t\t\t\t/* The embedded queue must be empty at this point! */\n+\t\t\t\tASSERT(embeddedQueue_.empty());\n+\n+\t\t\t\tLOG(RPI, Warning) << \"Flushing bayer stream!\";\n+\t\t\t\twhile (!bayerQueue_.empty()) {\n+\t\t\t\t\tunicam_[Unicam::Image].queueBuffer(bayerQueue_.front());\n+\t\t\t\t\tbayerQueue_.pop();\n+\t\t\t\t}\n+\t\t\t\treturn false;\n+\t\t\t}\n+\n+\t\t\t/*\n+\t\t\t * Not found a matching embedded buffer for the bayer buffer in\n+\t\t\t * the front of the queue. This buffer is now orphaned, so requeue\n+\t\t\t * it back to the device.\n+\t\t\t */\n+\t\t\tunicam_[Unicam::Image].queueBuffer(bayerQueue_.front());\n+\t\t\tbayerQueue_.pop();\n+\t\t\tbayerRequeueCount++;\n \t\t\tLOG(RPI, Warning) << \"Dropping unmatched input frame in stream \"\n-\t\t\t\t\t  << stream->name();\n-\t\t} else if (stream->isExternal() || b->metadata().timestamp == timestamp) {\n-\t\t\t/* The calling function will pop the item from the queue. */\n-\t\t\treturn b;\n+\t\t\t\t\t  << unicam_[Unicam::Image].name();\n+\n+\t\t\t/*\n+\t\t\t * Similar to the above, if we have requeued all available bayer buffers in\n+\t\t\t * the loop, then we are fully out of sync, so might as well requeue all the\n+\t\t\t * pending embedded data buffers.\n+\t\t\t */\n+\t\t\tif (bayerRequeueCount == unicam_[Unicam::Image].getBuffers().size()) {\n+\t\t\t\t/* The embedded queue must be empty at this point! */\n+\t\t\t\tASSERT(bayerQueue_.empty());\n+\n+\t\t\t\tLOG(RPI, Warning) << \"Flushing embedded data stream!\";\n+\t\t\t\twhile (!embeddedQueue_.empty()) {\n+\t\t\t\t\tunicam_[Unicam::Embedded].queueBuffer(embeddedQueue_.front());\n+\t\t\t\t\tembeddedQueue_.pop();\n+\t\t\t\t}\n+\t\t\t\treturn false;\n+\t\t\t}\n+\n+\t\t\t/* If the embedded queue has become empty, we cannot do any more. */\n+\t\t\tif (embeddedQueue_.empty())\n+\t\t\t\treturn false;\n \t\t} else {\n-\t\t\tbreak; /* Only higher timestamps from here. */\n+\t\t\t/*\n+\t\t\t * We have found a matching bayer and embedded data buffer, so\n+\t\t\t * nothing more to do apart from popping the buffers from the queue.\n+\t\t\t */\n+\t\t\tbayerQueue_.pop();\n+\t\t\tembeddedQueue_.pop();\n+\t\t\treturn true;\n \t\t}\n \t}\n \n-\treturn nullptr;\n+\treturn false;\n }\n \n REGISTER_PIPELINE_HANDLER(PipelineHandlerRPi)\n","prefixes":["libcamera-devel"]}