From patchwork Wed Mar 25 15:13:55 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Klug X-Patchwork-Id: 26365 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 DD8B1BE086 for ; Wed, 25 Mar 2026 15:15:47 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 7977862CC9; Wed, 25 Mar 2026 16:15:47 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="TX4+K4z4"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 4773462C44 for ; Wed, 25 Mar 2026 16:15:46 +0100 (CET) Received: from ideasonboard.com (unknown [IPv6:2a00:6020:448c:6c00:b16a:5ed9:4ada:a95a]) by perceval.ideasonboard.com (Postfix) with UTF8SMTPSA id 4E3C41B98; Wed, 25 Mar 2026 16:14:28 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1774451668; bh=3OpmvkR+XHCPZ8nSjonhVBpIOe4SDmuym/y2EZWkYfU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TX4+K4z4CoxWSUeO1VCvhq2cIpBnOP/d+RBJiCDITDckef1ilNkCOoAcnQ18vgJtS Dj33LQvp8id40op2cYBtUoVl1U6JNtmnbggalY+7dbFg7/0+BDe4PDyUPfQTC0T60q NycLa5/szAk7QCC3cOq+HIUhQz4iAcnt+knErECM= From: Stefan Klug To: libcamera-devel@lists.libcamera.org Cc: Stefan Klug Subject: [PATCH v2 23/32] libcamera: internal: Add SequenceSyncHelper class Date: Wed, 25 Mar 2026 16:13:55 +0100 Message-ID: <20260325151416.2114564-24-stefan.klug@ideasonboard.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260325151416.2114564-1-stefan.klug@ideasonboard.com> References: <20260325151416.2114564-1-stefan.klug@ideasonboard.com> MIME-Version: 1.0 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" On a V4L2 buffer the assigned sequence is not known until the buffer is dequeued. But for per frame controls we have to prepare other data like sensor controls and ISP params in advance. So we try to anticipate the sequence number a given buffer will be. In a perfect world this works well as long as the initial sequence is assigned correctly. But it breaks as soon as things like running out of buffers or incomplete images happen. To make things even more complicated, in most cases more than one buffer is queued to the kernel at a time. So as soon as a sequence number doesn't match the expected one after dequeuing, most likely all the already queued buffers will be dequeued with the same error. It is not sufficient to simply add the correction after dequeuing because the error on all queued frames would accumulate and the whole system starts to oscillate. To work around that add a SequenceSyncHelper class that tracks the expected error and allows to easily query the necessary correction when queuing new buffers. Signed-off-by: Stefan Klug --- Todo: - Add class documentation Changes in v2: - Moved files to man src dir, to be able to reuse it in other pipelines - Added cancel() function. --- include/libcamera/internal/meson.build | 1 + .../libcamera/internal/sequence_sync_helper.h | 76 +++++++++++++++++++ src/libcamera/meson.build | 1 + src/libcamera/sequence_sync_helper.cpp | 21 +++++ 4 files changed, 99 insertions(+) create mode 100644 include/libcamera/internal/sequence_sync_helper.h create mode 100644 src/libcamera/sequence_sync_helper.cpp diff --git a/include/libcamera/internal/meson.build b/include/libcamera/internal/meson.build index 4d2a09bd7041..80c425e13ccd 100644 --- a/include/libcamera/internal/meson.build +++ b/include/libcamera/internal/meson.build @@ -40,6 +40,7 @@ libcamera_internal_headers = files([ 'process.h', 'pub_key.h', 'request.h', + 'sequence_sync_helper.h', 'shared_mem_object.h', 'source_paths.h', 'sysfs.h', diff --git a/include/libcamera/internal/sequence_sync_helper.h b/include/libcamera/internal/sequence_sync_helper.h new file mode 100644 index 000000000000..407c6383dca6 --- /dev/null +++ b/include/libcamera/internal/sequence_sync_helper.h @@ -0,0 +1,76 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2025, Ideas on Board + * + * Sequence sync helper + */ + +#pragma once + +#include + +#include + +namespace libcamera { + +LOG_DECLARE_CATEGORY(SequenceSyncHelper) + +class SequenceSyncHelper +{ +public: + int gotFrame(size_t expectedSequence, size_t actualSequence) + { + ASSERT(!corrections_.empty()); + int diff = actualSequence - expectedSequence; + int corr = corrections_.front(); + corrections_.pop(); + expectedOffset_ -= corr; + int necessaryCorrection = diff - expectedOffset_; + correctionToApply_ += necessaryCorrection; + + LOG(SequenceSyncHelper, Debug) << "Sync frame " + << "expected: " << expectedSequence + << " actual: " << actualSequence + << " correction: " << corr + << " expectedOffset: " << expectedOffset_ + << " correctionToApply " << correctionToApply_; + + expectedOffset_ += necessaryCorrection; + return necessaryCorrection; + } + + void cancelFrame() + { + int corr = corrections_.front(); + corrections_.pop(); + expectedOffset_ -= corr; + } + + /* Value to be added to the source sequence */ + int correction() + { + return correctionToApply_; + } + + void pushCorrection(int correction) + { + corrections_.push(correction); + correctionToApply_ -= correction; + LOG(SequenceSyncHelper, Debug) + << "Push correction " << correction + << " correctionToApply " << correctionToApply_; + } + + void reset() + { + corrections_ = {}; + correctionToApply_ = 0; + expectedOffset_ = 0; + } + + std::queue corrections_; + int correctionToApply_ = 0; + int expectedOffset_ = 0; +}; + +} /* namespace libcamera */ diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build index d15943586300..8b82555a5e81 100644 --- a/src/libcamera/meson.build +++ b/src/libcamera/meson.build @@ -49,6 +49,7 @@ libcamera_internal_sources = files([ 'pipeline_handler.cpp', 'process.cpp', 'pub_key.cpp', + 'sequence_sync_helper.cpp', 'shared_mem_object.cpp', 'source_paths.cpp', 'sysfs.cpp', diff --git a/src/libcamera/sequence_sync_helper.cpp b/src/libcamera/sequence_sync_helper.cpp new file mode 100644 index 000000000000..7f0b9c9111a7 --- /dev/null +++ b/src/libcamera/sequence_sync_helper.cpp @@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2026, Ideas on Board. + * + * Helper to easily record debug metadata inside libcamera. + */ + +#include "libcamera/internal/sequence_sync_helper.h" + +#include + +namespace libcamera { + +LOG_DEFINE_CATEGORY(SequenceSyncHelper) + +/** + * \file debug_controls.h + * \brief Helper to synchronize buffer sequences + */ + +} /* namespace libcamera */