libcamera: include/libcamera/internal/clock_recovery.h Source File

libcamera v0.7.2 Supporting cameras in Linux since 2019

Loading...
Searching...
No Matches
clock_recovery.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2/*
3 * Copyright (C) 2024, Raspberry Pi Ltd
4 *
5 * Camera recovery algorithm
6 */
7#pragma once
8
9#include <stdint.h>
10
11namespace libcamera {
12
13 class ClockRecovery
14{
15public:
16 ClockRecovery();
17
18 void configure(unsigned int numSamples = 100, unsigned int maxJitter = 2000,
19 unsigned int minSamples = 10, unsigned int errorThreshold = 50000);
20 void reset();
21
22 void addSample();
23 void addSample(uint64_t input, uint64_t output);
24
25 uint64_t getOutput(uint64_t input);
26
27private:
28 /* Approximate number of samples over which the model state persists. */
29 unsigned int numSamples_;
30 /* Remove any output jitter larger than this immediately. */
31 unsigned int maxJitter_;
32 /* Number of samples required before we start to use model estimates. */
33 unsigned int minSamples_;
34 /* Threshold above which we assume the wallclock has been reset. */
35 unsigned int errorThreshold_;
36
37 /* How many samples seen (up to numSamples_). */
38 unsigned int count_;
39 /* This gets subtracted from all input values, just to make the numbers easier. */
40 uint64_t inputBase_;
41 /* As above, for the output. */
42 uint64_t outputBase_;
43 /* The previous input sample. */
44 uint64_t lastInput_;
45 /* The previous output sample. */
46 uint64_t lastOutput_;
47
48 /* Average x value seen so far. */
49 double xAve_;
50 /* Average y value seen so far */
51 double yAve_;
52 /* Average x^2 value seen so far. */
53 double x2Ave_;
54 /* Average x*y value seen so far. */
55 double xyAve_;
56
57 /*
58 * The latest estimate of linear parameters to derive the output clock
59 * from the input.
60 */
61 double slope_;
62 double offset_;
63
64 /* Use this cumulative error to monitor for spontaneous clock updates. */
65 double error_;
66};
67
68} /* namespace libcamera */
libcamera::ClockRecovery::configure
void configure(unsigned int numSamples=100, unsigned int maxJitter=2000, unsigned int minSamples=10, unsigned int errorThreshold=50000)
Set configuration parameters.
Definition clock_recovery.cpp:66
libcamera::ClockRecovery::getOutput
uint64_t getOutput(uint64_t input)
Calculate the output clock value according to the model from an input clock value.
Definition clock_recovery.cpp:219
libcamera::ClockRecovery::ClockRecovery
ClockRecovery()
Construct a ClockRecovery.
Definition clock_recovery.cpp:49
libcamera::ClockRecovery::reset
void reset()
Reset the clock recovery model and start again from scratch.
Definition clock_recovery.cpp:81
libcamera::ClockRecovery::addSample
void addSample()
Add a sample point to the clock recovery model, for recovering a wall clock value from the internal s...
Definition clock_recovery.cpp:109
libcamera
Top-level libcamera namespace.
Definition backtrace.h:17