From patchwork Tue Nov 17 10:10:36 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 10443 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 E4C9CBE081 for ; Tue, 17 Nov 2020 10:10:43 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id B095263321; Tue, 17 Nov 2020 11:10:43 +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="ax1N0y2t"; dkim-atps=neutral Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id AB6CC63282 for ; Tue, 17 Nov 2020 11:10:41 +0100 (CET) Received: by mail-wm1-x334.google.com with SMTP id 19so2671775wmf.1 for ; Tue, 17 Nov 2020 02:10:41 -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=jI7Up8GugWwQ85gC2Q/wVLikYmCjlJVPHbKnNY3L5mA=; b=ax1N0y2tWIyo38Jiz/sqyFbECJfsulWSEvKxZbs9FPk2yfAlz0YoiY3qnVseA38e/l tfFngT2RRPHi6brhwQBcYJGOTwnL4vXRq4krZi3Eb/PEWpdxgb2pHScomNRnrHfWo1oO 2N09AQ0aTUFdVMScQ+bOKjRY7oerBuKGttTIfzrHpi2WU/Tki1ayQwwFKXZuc5MaTLyV Ysgyrp9Z7tHVvNdLBrsW2r1Rd8lGWD1jTJfKxktRnyboNeK2ygCfSnHFtVGKJ+Me4Iay odCdyxiklkG70viMCkycbY3eIK6NxLIyIlI67pQOs3ccJXuKn/Avli5NYqMzw2BdnPRp 8/yQ== 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=jI7Up8GugWwQ85gC2Q/wVLikYmCjlJVPHbKnNY3L5mA=; b=PFGPXLfVgWssUa+7otKeT2YPisl0CBMHwUD5FW9b21O7fW7AaGAXjRu2WzjhHDicK5 Z7OynjdYrqzyWAFzl5BeQKBv77wbxYXR+RK9rbirhR0rlh18C9J3FMaAxvkd9rieNAdP qsClFGSLaBMP/zzid422w2gXAZqpoFIObU5OKfBEOU8d8/TYC6iWcBErzwUpfLsqu8CM AWq/1SsLFOeu+yJD565VXT801byFVpdzSvFx1Sc6ss1+k2jRTMxNUylRCDUQQICE/M1b 7XRvHXo2k0V6i/DIYaw8G2e3Dl3Fzs9NP013pzFcr6N1GuppB+7zTOrxoKoEXdcRpbBu MzPQ== X-Gm-Message-State: AOAM530mOqQ0RlW5RF6MuRjE1kGUsSmfT6dnvRzFto0PUynqYg24E9zI 2g3xL59meW3OldO1VIOxHtq5BR/3Gbf37g== X-Google-Smtp-Source: ABdhPJwJxVSNiTj2+jH8yL3p/Yb+GOepAf4y5NUZZHmUaRMYx+Hp+VkF2uDgoFbZTKPhH2N4Bf44ag== X-Received: by 2002:a1c:bd08:: with SMTP id n8mr3473873wmf.136.1605607840950; Tue, 17 Nov 2020 02:10:40 -0800 (PST) Received: from naushir-VirtualBox.patuck.local ([88.97.76.4]) by smtp.gmail.com with ESMTPSA id u8sm2874971wmg.6.2020.11.17.02.10.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Nov 2020 02:10:40 -0800 (PST) From: Naushir Patuck To: libcamera-devel@lists.libcamera.org Date: Tue, 17 Nov 2020 10:10:36 +0000 Message-Id: <20201117101036.166323-1-naush@raspberrypi.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2] 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..7271573a 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) { + /* We pop the item from the queue lower down. */ + 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)