libcamera v0.8.0+1-3c8337a0-nvm
Supporting cameras in Linux since 2019
Loading...
Searching...
No Matches
agc.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2/*
3 * Copyright (C) 2026 Ideas on Board Oy
4 *
5 * Auto exposure/gain algorithm for implementing the IPA-specific AGC algorithms
6 */
7
8#pragma once
9
10#include <optional>
11#include <stdint.h>
12#include <utility>
13#include <variant>
14#include <vector>
15
16#include <linux/v4l2-controls.h>
17
19
21#include <libcamera/controls.h>
22#include <libcamera/geometry.h>
23
24#include "agc_mean_luminance.h"
25#include "agc_msv.h"
27
28namespace libcamera {
29
30struct IPACameraSensorInfo;
31
32namespace ipa {
33
34class Histogram;
35
36namespace agc {
37
55
77
95
96[[nodiscard]]
97inline std::pair<uint32_t, double>
98extractControls(const ControlList &controls, const CameraSensorHelper *sensor)
99{
100 auto exposure = controls.get(V4L2_CID_EXPOSURE).get<int32_t>();
101 auto gainCode = controls.get(V4L2_CID_ANALOGUE_GAIN).get<int32_t>();
102
103 return {
104 static_cast<uint32_t>(exposure),
105 sensor ? sensor->gain(gainCode) : gainCode,
106 };
107}
108
109inline void
111 const FrameContext &frameContext)
112{
113 controls.set(V4L2_CID_EXPOSURE, static_cast<int32_t>(frameContext.exposure));
114 controls.set(V4L2_CID_ANALOGUE_GAIN,
115 static_cast<int32_t>(sensor
116 ? sensor->gainCode(frameContext.gain)
117 : static_cast<uint32_t>(frameContext.gain)));
118 controls.set(V4L2_CID_VBLANK, static_cast<int32_t>(frameContext.vblank));
119}
120
121} /* namespace agc */
122
124{
125public:
132
136 uint32_t exposure;
137 double gain;
138 std::vector<AgcMeanLuminance::AgcConstraint> &&additionalConstraints = {};
139 double lux = 0;
140 };
141
142 int init(const ValueNode &tuningData, CameraSensorHelper *sensor,
143 const ConfigurationParams &config);
144
145 int configure(agc::Session &session, agc::ActiveState &state,
146 const ConfigurationParams &config);
147
148 void queueRequest(const agc::Session &session, agc::ActiveState &state,
149 agc::FrameContext &frameContext, const ControlList &controls);
150
151 void prepare(agc::ActiveState &state, agc::FrameContext &frameContext);
152
153 void process(const agc::Session &session, agc::ActiveState &state,
154 agc::FrameContext &frameContext, std::optional<ProcessParams> &&params,
155 ControlList &metadata);
156
157private:
158 void processFrameDuration(const agc::Session &session,
159 agc::FrameContext &frameContext);
160 void fillMetadata(const agc::Session &session,
161 const agc::FrameContext &frameContext,
162 ControlList &metadata);
163
164 std::variant<AgcMSV, AgcMeanLuminance> impl_;
165 CameraSensorHelper *sensor_ = nullptr;
166
167 struct {
168 bool hasExposureModes = false;
169 } tuning_;
170};
171
172} /* namespace ipa */
173
174} /* namespace libcamera */
Class implementing mean luminance AEGC.
Helper class that performs sensor-specific parameter computations.
A map of ControlId to ControlInfo.
Definition controls.h:409
std::unordered_map< const ControlId *, ControlInfo > Map
The base std::unsorted_map<> container.
Definition controls.h:411
Associate a list of ControlId with their values for an object.
Definition controls.h:453
Describe a two-dimensional size.
Definition geometry.h:51
A class representing a tree structure of values.
Definition value_node.h:27
libIPA LSC algorithm algorithm
Definition agc.h:124
void process(const agc::Session &session, agc::ActiveState &state, agc::FrameContext &frameContext, std::optional< ProcessParams > &&params, ControlList &metadata)
Process frame statistics.
Definition agc.cpp:767
void queueRequest(const agc::Session &session, agc::ActiveState &state, agc::FrameContext &frameContext, const ControlList &controls)
Queue a request.
Definition agc.cpp:595
int init(const ValueNode &tuningData, CameraSensorHelper *sensor, const ConfigurationParams &config)
Load tuning data and configure.
Definition agc.cpp:327
int configure(agc::Session &session, agc::ActiveState &state, const ConfigurationParams &config)
Initialize the session configuration and active state.
Definition agc.cpp:374
void prepare(agc::ActiveState &state, agc::FrameContext &frameContext)
Prepare a frame.
Definition agc.cpp:711
Base class for computing sensor tuning parameters using sensor-specific constants.
Definition camera_sensor_helper.h:24
virtual uint32_t gainCode(double gain) const
Compute gain code from the analogue gain absolute value.
Definition camera_sensor_helper.cpp:98
virtual double gain(uint32_t gainCode) const
Compute the real gain from the V4L2 subdev control gain code.
Definition camera_sensor_helper.cpp:125
The base class for creating histograms.
Definition histogram.h:23
Helper class from std::chrono::duration that represents a time duration in nanoseconds with double pr...
Definition utils.h:331
Camera controls identifiers.
Framework to manage controls related to an object.
Data structures related to geometric objects.
void prepareControls(ControlList &controls, const CameraSensorHelper *sensor, const FrameContext &frameContext)
Definition agc.h:110
std::pair< uint32_t, double > extractControls(const ControlList &controls, const CameraSensorHelper *sensor)
Definition agc.h:98
AeExposureModeEnum
Supported AeExposureMode values.
Definition control_ids.h:115
AeConstraintModeEnum
Supported AeConstraintMode values.
Definition control_ids.h:105
Top-level libcamera namespace.
Definition backtrace.h:17
Report the image sensor characteristics.
Definition core_ipa_interface.h:31
Parameters for AgcAlgorithm::configure()
Definition agc.h:126
const ControlInfoMap & sensorControls
ControlInfoMap of the sensor.
Definition agc.h:128
ControlInfoMap::Map & ctrlMap
ControlInfoMap::Map to update with controls.
Definition agc.h:129
const IPACameraSensorInfo & sensorInfo
Current configuration of the sensor.
Definition agc.h:127
bool autoAllowed
Whether to enable auto controls.
Definition agc.h:130
Parameters for AgcAlgorithm::process()
Definition agc.h:133
double gain
Effective gain of the frame.
Definition agc.h:137
const AgcMeanLuminance::Traits & traits
Implementation of AgcMeanLuminance::Traits.
Definition agc.h:134
std::vector< AgcMeanLuminance::AgcConstraint > && additionalConstraints
Additional AgcMeanLuminance::AgcConstraints to apply.
Definition agc.h:138
double lux
Effective lux value of the frame.
Definition agc.h:139
const Histogram & yHist
Luminance histogram of the frame.
Definition agc.h:135
uint32_t exposure
Effective exposure of the frame.
Definition agc.h:136
A collection of callbacks.
Definition agc_mean_luminance.h:40
Active state for AgcAlgorithm.
Definition agc.h:56
double exposureValue
Exposure value as set by the ExposureValue control.
Definition agc.h:71
bool autoGainEnabled
Whether automatic gain control is enabled by the AnalogueGainMode control.
Definition agc.h:70
struct libcamera::ipa::agc::ActiveState::@11 automatic
Automatic exposure time and analog gain (computed by the algorithm)
bool autoExposureEnabled
Whether automatic exposure control is enabled by the ExposureTimeMode control.
Definition agc.h:69
double yTarget
Automatically determined luminance target.
Definition agc.h:66
double quantizationGain
Automatic quantization gain multiplier.
Definition agc.h:64
controls::AeExposureModeEnum exposureMode
Exposure mode as set by the AeExposureMode control.
Definition agc.h:73
utils::Duration minFrameDuration
Minimum frame duration as set by the FrameDurationLimits control.
Definition agc.h:74
double digitalGain
Automatic digital gain multiplier.
Definition agc.h:65
uint32_t exposure
Manual exposure time expressed as a number of lines as set by the ExposureTime control.
Definition agc.h:58
struct libcamera::ipa::agc::ActiveState::@10 manual
Manual exposure time and analog gain (set through requests)
utils::Duration maxFrameDuration
Maximum frame duration as set by the FrameDurationLimits control.
Definition agc.h:75
controls::AeConstraintModeEnum constraintMode
Constraint mode as set by the AeConstraintMode control.
Definition agc.h:72
double gain
Manual analogue gain as set by the AnalogueGain control.
Definition agc.h:59
Per-frame context for AgcAlgorithm.
Definition agc.h:78
utils::Duration minFrameDuration
Minimum frame duration as set by the FrameDurationLimits control.
Definition agc.h:89
bool autoGainEnabled
Manual/automatic AGC state (gain) as set by the AnalogueGainMode control.
Definition agc.h:86
controls::AeConstraintModeEnum constraintMode
Constraint mode as set by the AeConstraintMode control.
Definition agc.h:87
double gain
Analogue gain multiplier computed by the algorithm.
Definition agc.h:80
bool autoExposureEnabled
Manual/automatic AGC state (exposure) as set by the ExposureTimeMode control.
Definition agc.h:85
uint32_t exposure
Exposure time expressed as a number of lines computed by the algorithm.
Definition agc.h:79
double quantizationGain
Quantization gain multiplier computed by the algorithm.
Definition agc.h:81
bool autoGainModeChange
Indicate if autoGainEnabled has changed from true in the previous frame to false in the current frame...
Definition agc.h:93
utils::Duration frameDuration
The actual FrameDuration used by the algorithm for the frame.
Definition agc.h:91
controls::AeExposureModeEnum exposureMode
Exposure mode as set by the AeExposureMode control.
Definition agc.h:88
double exposureValue
Exposure value as set by the ExposureValue control.
Definition agc.h:82
bool autoExposureModeChange
Indicate if autoExposureEnabled has changed from true in the previous frame to false in the current f...
Definition agc.h:92
utils::Duration maxFrameDuration
Maximum frame duration as set by the FrameDurationLimits control.
Definition agc.h:90
double yTarget
Luminance target computed by the algorithm.
Definition agc.h:83
uint32_t vblank
Vertical blanking parameter computed by the algorithm.
Definition agc.h:84
Session configuration for AgcAlgorithm.
Definition agc.h:38
utils::Duration minFrameDuration
Minimum frame duration for the streaming session.
Definition agc.h:44
utils::Duration lineDuration
Line duration for the streaming session.
Definition agc.h:46
struct libcamera::ipa::agc::Session::@9 sensor
Details of the sensor configuration.
double minAnalogueGain
Minimum analogue gain for the streaming session.
Definition agc.h:41
uint32_t minExposure
Minimum exposure (in lines) for the streaming session.
Definition agc.h:39
double defAnalogueGain
Default analogue gain of the configured sensor.
Definition agc.h:43
utils::Duration minExposureTime
Minimum exposure time for the streaming session.
Definition agc.h:40
double maxAnalogueGain
Maximum analogue gain for the streaming session.
Definition agc.h:42
uint32_t exposureMargin
Exposure margin of the sensor.
Definition agc.h:50
utils::Duration maxFrameDuration
Maximum frame duration for the streaming session.
Definition agc.h:45
bool autoAllowed
Whether to enable auto controls.
Definition agc.h:53
Size outputSize
Configured output size of the sensor.
Definition agc.h:49
Miscellaneous utility functions.