libcamera v0.7.2+1-aebe4861-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
54
76
94
95[[nodiscard]]
96inline std::pair<uint32_t, double>
97extractControls(const ControlList &controls, const CameraSensorHelper *sensor)
98{
99 auto exposure = controls.get(V4L2_CID_EXPOSURE).get<int32_t>();
100 auto gainCode = controls.get(V4L2_CID_ANALOGUE_GAIN).get<int32_t>();
101
102 return {
103 static_cast<uint32_t>(exposure),
104 sensor ? sensor->gain(gainCode) : gainCode,
105 };
106}
107
108inline void
110 uint32_t exposure, double gain)
111{
112 controls.set(V4L2_CID_EXPOSURE, static_cast<int32_t>(exposure));
113 controls.set(V4L2_CID_ANALOGUE_GAIN,
114 static_cast<int32_t>(sensor
115 ? sensor->gainCode(gain)
116 : static_cast<uint32_t>(gain)));
117}
118
119} /* namespace agc */
120
122{
123public:
130
134 uint32_t exposure;
135 double gain;
136 std::vector<AgcMeanLuminance::AgcConstraint> &&additionalConstraints = {};
137 double lux = 0;
138 };
139
140 int init(const ValueNode &tuningData, CameraSensorHelper *sensor,
141 const ConfigurationParams &config);
142
143 int configure(agc::Session &session, agc::ActiveState &state,
144 const ConfigurationParams &config);
145
146 void queueRequest(const agc::Session &session, agc::ActiveState &state,
147 agc::FrameContext &frameContext, const ControlList &controls);
148
149 void prepare(agc::ActiveState &state, agc::FrameContext &frameContext);
150
151 void process(const agc::Session &session, agc::ActiveState &state,
152 agc::FrameContext &frameContext, std::optional<ProcessParams> &&params,
153 ControlList &metadata);
154
155private:
156 void processFrameDuration(const agc::Session &session,
157 agc::FrameContext &frameContext,
158 utils::Duration frameDuration);
159 void fillMetadata(const agc::Session &session,
160 const agc::FrameContext &frameContext,
161 ControlList &metadata);
162
163 std::variant<AgcMSV, AgcMeanLuminance> impl_;
164 CameraSensorHelper *sensor_ = nullptr;
165};
166
167} /* namespace ipa */
168
169} /* 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:122
void process(const agc::Session &session, agc::ActiveState &state, agc::FrameContext &frameContext, std::optional< ProcessParams > &&params, ControlList &metadata)
Process frame statistics.
Definition agc.cpp:722
void queueRequest(const agc::Session &session, agc::ActiveState &state, agc::FrameContext &frameContext, const ControlList &controls)
Queue a request.
Definition agc.cpp:550
int init(const ValueNode &tuningData, CameraSensorHelper *sensor, const ConfigurationParams &config)
Load tuning data and configure.
Definition agc.cpp:309
int configure(agc::Session &session, agc::ActiveState &state, const ConfigurationParams &config)
Initialize the session configuration and active state.
Definition agc.cpp:354
void prepare(agc::ActiveState &state, agc::FrameContext &frameContext)
Prepare a frame.
Definition agc.cpp:666
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:88
virtual double gain(uint32_t gainCode) const
Compute the real gain from the V4L2 subdev control gain code.
Definition camera_sensor_helper.cpp:115
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.
std::pair< uint32_t, double > extractControls(const ControlList &controls, const CameraSensorHelper *sensor)
Definition agc.h:97
void prepareControls(ControlList &controls, const CameraSensorHelper *sensor, uint32_t exposure, double gain)
Definition agc.h:109
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:124
const ControlInfoMap & sensorControls
ControlInfoMap of the sensor.
Definition agc.h:126
ControlInfoMap::Map & ctrlMap
ControlInfoMap::Map to update with controls.
Definition agc.h:127
const IPACameraSensorInfo & sensorInfo
Current configuration of the sensor.
Definition agc.h:125
bool autoAllowed
Whether to enable auto controls.
Definition agc.h:128
Parameters for AgcAlgorithm::process()
Definition agc.h:131
double gain
Effective gain of the frame.
Definition agc.h:135
const AgcMeanLuminance::Traits & traits
Implementation of AgcMeanLuminance::Traits.
Definition agc.h:132
std::vector< AgcMeanLuminance::AgcConstraint > && additionalConstraints
Additional AgcMeanLuminance::AgcConstraints to apply.
Definition agc.h:136
double lux
Effective lux value of the frame.
Definition agc.h:137
const Histogram & yHist
Luminance histogram of the frame.
Definition agc.h:133
uint32_t exposure
Effective exposure of the frame.
Definition agc.h:134
A collection of callbacks.
Definition agc_mean_luminance.h:43
Active state for AgcAlgorithm.
Definition agc.h:55
double exposureValue
Exposure value as set by the ExposureValue control.
Definition agc.h:70
bool autoGainEnabled
Whether automatic gain control is enabled by the AnalogueGainMode control.
Definition agc.h:69
struct libcamera::ipa::agc::ActiveState::@14 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:68
double yTarget
Automatically determined luminance target.
Definition agc.h:65
double quantizationGain
Automatic quantization gain multiplier.
Definition agc.h:63
controls::AeExposureModeEnum exposureMode
Exposure mode as set by the AeExposureMode control.
Definition agc.h:72
utils::Duration minFrameDuration
Minimum frame duration as set by the FrameDurationLimits control.
Definition agc.h:73
double digitalGain
Automatic digital gain multiplier.
Definition agc.h:64
uint32_t exposure
Manual exposure time expressed as a number of lines as set by the ExposureTime control.
Definition agc.h:57
struct libcamera::ipa::agc::ActiveState::@13 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:74
controls::AeConstraintModeEnum constraintMode
Constraint mode as set by the AeConstraintMode control.
Definition agc.h:71
double gain
Manual analogue gain as set by the AnalogueGain control.
Definition agc.h:58
Per-frame context for AgcAlgorithm.
Definition agc.h:77
utils::Duration minFrameDuration
Minimum frame duration as set by the FrameDurationLimits control.
Definition agc.h:88
bool autoGainEnabled
Manual/automatic AGC state (gain) as set by the AnalogueGainMode control.
Definition agc.h:85
controls::AeConstraintModeEnum constraintMode
Constraint mode as set by the AeConstraintMode control.
Definition agc.h:86
double gain
Analogue gain multiplier computed by the algorithm.
Definition agc.h:79
bool autoExposureEnabled
Manual/automatic AGC state (exposure) as set by the ExposureTimeMode control.
Definition agc.h:84
uint32_t exposure
Exposure time expressed as a number of lines computed by the algorithm.
Definition agc.h:78
double quantizationGain
Quantization gain multiplier computed by the algorithm.
Definition agc.h:80
bool autoGainModeChange
Indicate if autoGainEnabled has changed from true in the previous frame to false in the current frame...
Definition agc.h:92
utils::Duration frameDuration
The actual FrameDuration used by the algorithm for the frame.
Definition agc.h:90
controls::AeExposureModeEnum exposureMode
Exposure mode as set by the AeExposureMode control.
Definition agc.h:87
double exposureValue
Exposure value as set by the ExposureValue control.
Definition agc.h:81
bool autoExposureModeChange
Indicate if autoExposureEnabled has changed from true in the previous frame to false in the current f...
Definition agc.h:91
utils::Duration maxFrameDuration
Maximum frame duration as set by the FrameDurationLimits control.
Definition agc.h:89
double yTarget
Luminance target computed by the algorithm.
Definition agc.h:82
uint32_t vblank
Vertical blanking parameter computed by the algorithm.
Definition agc.h:83
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::@12 sensor
Details of the sensor configuration.
double minAnalogueGain
Minimum analogue gain for the streaming session.
Definition agc.h:41
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:39
double maxAnalogueGain
Maximum analogue gain for the streaming session.
Definition agc.h:42
utils::Duration maxExposureTime
Maximum exposure time for the streaming session.
Definition agc.h:40
utils::Duration maxFrameDuration
Maximum frame duration for the streaming session.
Definition agc.h:45
bool autoAllowed
Whether to enable auto controls.
Definition agc.h:52
Size outputSize
Configured output size of the sensor.
Definition agc.h:49
Miscellaneous utility functions.