From patchwork Mon Nov 16 12:02:56 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 10426 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 89E3CBE081 for ; Mon, 16 Nov 2020 12:05:36 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 1B868632C3; Mon, 16 Nov 2020 13:05:36 +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="kLA7XOZ6"; dkim-atps=neutral Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 70745631B5 for ; Mon, 16 Nov 2020 13:05:34 +0100 (CET) Received: by mail-wm1-x330.google.com with SMTP id 23so11886915wmg.1 for ; Mon, 16 Nov 2020 04:05:34 -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:mime-version :content-transfer-encoding; bh=qy10y6LWOCrKeIBP9nu8EToJY6Yojyfq2Ud7V0kTQ6A=; b=kLA7XOZ60gE6ZHBn0zedmIgesKF67fxdzvcOMdhhOWb1/2z9Nv9tTKqbobT4FPQ0/z I1BNdKtHuhXRywxIuz0iWdlmfQjKffZ9qLwViuuLE0F+4KybJSxOfBbDbpo6c4WpV8dn fILAztOcoJ1XKI69gMb+qJMtSA5OLqjGFu63NC1LAW0Te9vh9u5SMyQki+xHR34Cbbg4 hFlyAhQDFJCz59ZQ12HgCpzdyrPebUZr11HZvSc3YdYFmiXKjBdXK6Gfo+31AvcYlOd/ zTCP5A/eTYsN+HLCUf3Q6hxl8aSWmg5VM0qxtiY5bSA9CP0CoK7WlYmgo99a9Zt72k5b b0mQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=qy10y6LWOCrKeIBP9nu8EToJY6Yojyfq2Ud7V0kTQ6A=; b=c5Jd1GS152CTOu4OvXBEIXlQ0KdJOPdtormiN0ni2dx1v4D6e3/6yS52N3lciNN2mi tBK4rbWJAR7psERaB5+N3lnxbk+5pkEh17Rh3t8qW3qDMXO0Q5+Bg/tt7Usa1IeH64HV P4WJeoJke3a+7Xq7pRzXiEy7KpFfOdeB6t6nkfxZ/GumP7MgTMU1VDxC9ZKUDQ93Kz4+ gqSI/JBy45z6jUWqjlFJQixrpYk4zNn4CalU8o6/16afGPvty79qQQiOV4pt3u88uAR/ TP/rSa9QPp5+mldrD2WIgJZOOUTgzxvPfDPcSH6ocqnuDuIo1gquUJ3bq7GZNNhEUKXq Ltog== X-Gm-Message-State: AOAM533WDcPmxhDTS8otUQhtAy9Ms8pwjOB0gCfjbmNjXmCQW+KrZNzM iErKJbtcx+hGGtNiMVO8Uo9d09EtQ0iAQw== X-Google-Smtp-Source: ABdhPJzbNVSzARHykkqX5koURB4Nsq1npoinLGJur331YiAJEihFdh1OzZBAgou1+RGsKsq3p9A2lg== X-Received: by 2002:a1c:4957:: with SMTP id w84mr15018456wma.84.1605528333666; Mon, 16 Nov 2020 04:05:33 -0800 (PST) Received: from naushir-VirtualBox.patuck.local ([88.97.76.4]) by smtp.gmail.com with ESMTPSA id g11sm22763112wrq.7.2020.11.16.04.05.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Nov 2020 04:05:33 -0800 (PST) From: Naushir Patuck 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 bayer/embedded data buffer matching 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" There is a condition that would cause the buffers to never be in sync when we using only a single buffer in the bayer and embedded data streams. This occurred because even though both streams would get flushed to resync, one stream's only buffer was already queued in the device, and would end up never matching. Rework the buffer matching logic by combining updateQueue() and tryFlushQueue() into a single function findMatchingBuffers(). This would allow us to flush the queues at the same time as we match buffers, avoiding the the above condition. Signed-off-by: Naushir Patuck Tested-by: David Plowman Acked-by: Laurent Pinchart --- .../pipeline/raspberrypi/raspberrypi.cpp | 176 +++++++++--------- 1 file changed, 88 insertions(+), 88 deletions(-) diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp index 7ad66f21..26c6731c 100644 --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp @@ -205,9 +205,7 @@ public: private: void checkRequestCompleted(); void tryRunPipeline(); - void tryFlushQueues(); - FrameBuffer *updateQueue(std::queue &q, uint64_t timestamp, - RPi::Stream *stream); + bool findMatchingBuffers(FrameBuffer *&bayerBuffer, FrameBuffer *&embeddedBuffer); unsigned int ispOutputCount_; }; @@ -1537,7 +1535,6 @@ void RPiCameraData::handleState() case State::Idle: tryRunPipeline(); - tryFlushQueues(); break; } } @@ -1588,41 +1585,8 @@ void RPiCameraData::tryRunPipeline() bayerQueue_.empty() || embeddedQueue_.empty()) return; - /* Start with the front of the bayer buffer queue. */ - bayerBuffer = bayerQueue_.front(); - - /* - * Find the embedded data buffer with a matching timestamp to pass to - * the IPA. Any embedded buffers with a timestamp lower than the - * current bayer buffer will be removed and re-queued to the driver. - */ - embeddedBuffer = updateQueue(embeddedQueue_, bayerBuffer->metadata().timestamp, - &unicam_[Unicam::Embedded]); - - if (!embeddedBuffer) { - LOG(RPI, Debug) << "Could not find matching embedded buffer"; - - /* - * Look the other way, try to match a bayer buffer with the - * first embedded buffer in the queue. This will also do some - * housekeeping on the bayer image queue - clear out any - * buffers that are older than the first buffer in the embedded - * queue. - * - * But first check if the embedded queue has emptied out. - */ - if (embeddedQueue_.empty()) - return; - - embeddedBuffer = embeddedQueue_.front(); - bayerBuffer = updateQueue(bayerQueue_, embeddedBuffer->metadata().timestamp, - &unicam_[Unicam::Image]); - - if (!bayerBuffer) { - LOG(RPI, Debug) << "Could not find matching bayer buffer - ending."; - return; - } - } + if (!findMatchingBuffers(bayerBuffer, embeddedBuffer)) + return; /* Take the first request from the queue and action the IPA. */ Request *request = requestQueue_.front(); @@ -1665,10 +1629,6 @@ void RPiCameraData::tryRunPipeline() op.controls = { request->controls() }; ipa_->processEvent(op); - /* Ready to use the buffers, pop them off the queue. */ - bayerQueue_.pop(); - embeddedQueue_.pop(); - /* Set our state to say the pipeline is active. */ state_ = State::Busy; @@ -1685,62 +1645,102 @@ void RPiCameraData::tryRunPipeline() ipa_->processEvent(op); } -void RPiCameraData::tryFlushQueues() +bool RPiCameraData::findMatchingBuffers(FrameBuffer *&bayerBuffer, FrameBuffer *&embeddedBuffer) { - /* - * It is possible for us to end up in a situation where all available - * Unicam buffers have been dequeued but do not match. This can happen - * when the system is heavily loaded and we get out of lock-step with - * the two channels. - * - * In such cases, the best thing to do is the re-queue all the buffers - * and give a chance for the hardware to return to lock-step. We do have - * to drop all interim frames. - */ - if (unicam_[Unicam::Image].getBuffers().size() == bayerQueue_.size() && - unicam_[Unicam::Embedded].getBuffers().size() == embeddedQueue_.size()) { - /* This cannot happen when Unicam streams are external. */ - assert(!unicam_[Unicam::Image].isExternal()); + unsigned int embeddedRequeueCount = 0, bayerRequeueCount = 0; - LOG(RPI, Warning) << "Flushing all buffer queues!"; - - while (!bayerQueue_.empty()) { - unicam_[Unicam::Image].queueBuffer(bayerQueue_.front()); - bayerQueue_.pop(); - } + /* Loop until we find a matching bayer and embedded data buffer. */ + while (!bayerQueue_.empty()) { + /* Start with the front of the bayer queue. */ + bayerBuffer = bayerQueue_.front(); + /* + * Find the embedded data buffer with a matching timestamp to pass to + * the IPA. Any embedded buffers with a timestamp lower than the + * current bayer buffer will be removed and re-queued to the driver. + */ + uint64_t ts = bayerBuffer->metadata().timestamp; + embeddedBuffer = nullptr; while (!embeddedQueue_.empty()) { - unicam_[Unicam::Embedded].queueBuffer(embeddedQueue_.front()); - embeddedQueue_.pop(); + FrameBuffer *b = embeddedQueue_.front(); + if (!unicam_[Unicam::Embedded].isExternal() && b->metadata().timestamp < ts) { + embeddedQueue_.pop(); + unicam_[Unicam::Embedded].queueBuffer(b); + embeddedRequeueCount++; + LOG(RPI, Warning) << "Dropping unmatched input frame in stream " + << unicam_[Unicam::Embedded].name(); + } else if (unicam_[Unicam::Embedded].isExternal() || b->metadata().timestamp == ts) { + /* The calling function will pop the item from the queue. */ + embeddedBuffer = b; + break; + } else { + break; /* Only higher timestamps from here. */ + } } - } -} -FrameBuffer *RPiCameraData::updateQueue(std::queue &q, uint64_t timestamp, - RPi::Stream *stream) -{ - /* - * If the unicam streams are external (both have be to the same), then we - * can only return out the top buffer in the queue, and assume they have - * been synced by queuing at the same time. We cannot drop these frames, - * as they may have been provided externally. - */ - while (!q.empty()) { - FrameBuffer *b = q.front(); - if (!stream->isExternal() && b->metadata().timestamp < timestamp) { - q.pop(); - stream->queueBuffer(b); + if (!embeddedBuffer) { + LOG(RPI, Debug) << "Could not find matching embedded buffer"; + + /* + * If we have requeued all available embedded data buffers in this loop, + * then we are fully out of sync, so might as well requeue all the pending + * bayer buffers. + */ + if (embeddedRequeueCount == unicam_[Unicam::Embedded].getBuffers().size()) { + /* The embedded queue must be empty at this point! */ + ASSERT(embeddedQueue_.empty()); + + LOG(RPI, Warning) << "Flushing bayer stream!"; + while (!bayerQueue_.empty()) { + unicam_[Unicam::Image].queueBuffer(bayerQueue_.front()); + bayerQueue_.pop(); + } + return false; + } + + /* + * Not found a matching embedded buffer for the bayer buffer in + * the front of the queue. This buffer is now orphaned, so requeue + * it back to the device. + */ + unicam_[Unicam::Image].queueBuffer(bayerQueue_.front()); + bayerQueue_.pop(); + bayerRequeueCount++; LOG(RPI, Warning) << "Dropping unmatched input frame in stream " - << stream->name(); - } else if (stream->isExternal() || b->metadata().timestamp == timestamp) { - /* The calling function will pop the item from the queue. */ - return b; + << unicam_[Unicam::Image].name(); + + /* + * Similar to the above, if we have requeued all available bayer buffers in + * the loop, then we are fully out of sync, so might as well requeue all the + * pending embedded data buffers. + */ + if (bayerRequeueCount == unicam_[Unicam::Image].getBuffers().size()) { + /* The embedded queue must be empty at this point! */ + ASSERT(bayerQueue_.empty()); + + LOG(RPI, Warning) << "Flushing embedded data stream!"; + while (!embeddedQueue_.empty()) { + unicam_[Unicam::Embedded].queueBuffer(embeddedQueue_.front()); + embeddedQueue_.pop(); + } + return false; + } + + /* If the embedded queue has become empty, we cannot do any more. */ + if (embeddedQueue_.empty()) + return false; } else { - break; /* Only higher timestamps from here. */ + /* + * We have found a matching bayer and embedded data buffer, so + * nothing more to do apart from popping the buffers from the queue. + */ + bayerQueue_.pop(); + embeddedQueue_.pop(); + return true; } } - return nullptr; + return false; } REGISTER_PIPELINE_HANDLER(PipelineHandlerRPi)