Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ rust_library(
srcs = [
"src/capture_channel.rs",
"src/control.rs",
"src/controls/exposure_bias.rs",
"src/controls/gain.rs",
"src/controls/lens_facing.rs",
"src/controls/mod.rs",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ impl ControlDescriptor {
self.type_ == bindings::v4l2_ctrl_type_V4L2_CTRL_TYPE_INTEGER64
}

fn is_integer_menu(&self) -> bool {
self.type_ == bindings::v4l2_ctrl_type_V4L2_CTRL_TYPE_INTEGER_MENU
}

fn elem_size(&self) -> u32 {
if self.is_64bit() {
std::mem::size_of::<i64>() as u32
Expand Down Expand Up @@ -111,6 +115,11 @@ pub trait Control: Send + Sync {
None
}

/// Value of integer menu entry `index`, `None` if there is no such entry.
fn menu_value(&self, _index: u32) -> Option<i64> {
None
}

/// Projects the value into `params`.
fn apply(&self, _params: &mut RenderParams) {}
}
Expand Down Expand Up @@ -257,16 +266,20 @@ impl ControlSet {

/// `VIDIOC_QUERYMENU`.
pub fn querymenu(&self, id: u32, index: u32) -> IoctlResult<bindings::v4l2_querymenu> {
let name = self
.find(id)
.and_then(|control| control.menu_name(index))
.ok_or(libc::EINVAL)?;
let control = self.find(id).ok_or(libc::EINVAL)?;
let entry = if control.descriptor().is_integer_menu() {
let value = control.menu_value(index).ok_or(libc::EINVAL)?;
bindings::v4l2_querymenu__bindgen_ty_1 { value }
} else {
let name = control.menu_name(index).ok_or(libc::EINVAL)?;
bindings::v4l2_querymenu__bindgen_ty_1 {
name: ctrl_name(name),
}
};
Ok(bindings::v4l2_querymenu {
id,
index,
__bindgen_anon_1: bindings::v4l2_querymenu__bindgen_ty_1 {
name: ctrl_name(name),
},
__bindgen_anon_1: entry,
..Default::default()
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright 2026, The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! `V4L2_CID_AUTO_EXPOSURE_BIAS`: integer menu of exposure bias values.

use v4l2r::bindings;

use crate::control::{Control, ControlDescriptor};
use crate::render_params::{ExposureBias, RenderParams};

/// Read-write integer menu control backed by [`ExposureBias`].
///
/// Like any menu control its value is an index into the menu; `VIDIOC_QUERYMENU` maps the
/// index to the EV millis of the entry, `ExposureBias::MIN + (index * ExposureBias::STEP)`,
/// as the V4L2 API requires for this control:
///
/// ```text
/// index EV millis EV
/// 0 -3000 -3.0
/// 1 -2500 -2.5
/// 6 0 0.0 (default)
/// 11 2500 2.5
/// 12 3000 3.0
/// ```
#[derive(Debug, Default)]
pub struct ExposureBiasControl {
bias: ExposureBias,
}

impl ExposureBiasControl {
/// Index of the last menu entry.
const MAX_INDEX: i32 = (ExposureBias::MAX - ExposureBias::MIN) / ExposureBias::STEP;
/// Index of the 0 EV entry.
const DEFAULT_INDEX: i32 = (ExposureBias::DEFAULT - ExposureBias::MIN) / ExposureBias::STEP;

pub fn new() -> Self {
Self::default()
}

fn ev_millis(index: i64) -> Result<i32, libc::c_int> {
let index = i32::try_from(index).map_err(|_| libc::ERANGE)?;
if index < 0 || index > Self::MAX_INDEX {
return Err(libc::ERANGE);
}
Ok(ExposureBias::MIN + index * ExposureBias::STEP)
}

fn parse(value: i64) -> Result<ExposureBias, i32> {
ExposureBias::new(Self::ev_millis(value)?)
}

fn index(&self) -> i32 {
(self.bias.ev_millis() - ExposureBias::MIN) / ExposureBias::STEP
}
}

impl Control for ExposureBiasControl {
fn id(&self) -> u32 {
bindings::V4L2_CID_AUTO_EXPOSURE_BIAS
}

fn descriptor(&self) -> ControlDescriptor {
ControlDescriptor {
type_: bindings::v4l2_ctrl_type_V4L2_CTRL_TYPE_INTEGER_MENU,
name: "Auto Exposure, Bias",
minimum: 0,
maximum: Self::MAX_INDEX as i64,
step: 1,
default_value: Self::DEFAULT_INDEX as i64,
flags: 0,
}
}

fn value(&self) -> i64 {
self.index() as i64
}

fn validate(&self, value: i64) -> Result<(), i32> {
Self::parse(value).map(|_| ())
}

fn set(&mut self, value: i64) -> Result<bool, i32> {
let bias = Self::parse(value)?;
let changed = self.bias != bias;
self.bias = bias;
Ok(changed)
}

fn menu_value(&self, index: u32) -> Option<i64> {
Self::ev_millis(index as i64).ok().map(i64::from)
}

fn apply(&self, params: &mut RenderParams) {
params.exposure_bias = self.bias;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

pub mod exposure_bias;
pub mod gain;
pub mod lens_facing;
pub mod test_pattern;

pub use exposure_bias::ExposureBiasControl;
pub use gain::GainControl;
pub use lens_facing::{CID_LENS_FACING, LensFacing, LensFacingControl};
pub use test_pattern::TestPatternControl;
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,15 @@ impl FramePattern for Pipeline {
sink_u: &mut dyn Write,
sink_v: &mut dyn Write,
) -> Result<(), i32> {
// The exposure bias lengthens or shortens the exposure, as auto-exposure would.
let sensor = Sensor {
exposure_s: self.sensor.exposure_s * params.exposure_bias.factor(),
..self.sensor
};
let isp = Isp {
gain: params.gain,
crop: params.crop,
};
isp.process(&self.sensor, iteration, width, height, sink_y, sink_u, sink_v)
isp.process(&sensor, iteration, width, height, sink_y, sink_u, sink_v)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,47 @@ impl Default for Gain {
}
}

/// Exposure bias, also called exposure compensation.
///
/// EV stands for exposure value: +1 EV doubles the light reaching the sensor, -1 EV halves it,
/// 0 EV leaves the exposure as is. The value is stored in EV millis, thousandths of an EV.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ExposureBias(i32);

impl ExposureBias {
/// -3 EV to +3 EV in half EV steps.
pub const MIN: i32 = -3000;
pub const MAX: i32 = 3000;
pub const STEP: i32 = 500;
pub const DEFAULT: i32 = 0;

pub fn new(ev_millis: i32) -> Result<Self, libc::c_int> {
if ev_millis < Self::MIN || ev_millis > Self::MAX || ev_millis % Self::STEP != 0 {
return Err(libc::ERANGE);
}
Ok(ExposureBias(ev_millis))
}

pub fn ev_millis(&self) -> i32 {
self.0
}

pub fn ev(&self) -> f32 {
self.0 as f32 / 1000.0
}

/// Exposure multiplier, 2 to the power of the EV.
pub fn factor(&self) -> f32 {
2f32.powf(self.ev())
}
}

impl Default for ExposureBias {
fn default() -> Self {
ExposureBias(Self::DEFAULT)
}
}

/// Test pattern selectable through `V4L2_CID_TEST_PATTERN`.
///
/// The discriminants double as the menu indices reported by `VIDIOC_QUERYMENU`, so they
Expand Down Expand Up @@ -181,6 +222,8 @@ impl Default for Crop {
pub struct RenderParams {
/// Luma scaling, `Gain::MIN` is 1.0x.
pub gain: Gain,
/// Exposure bias applied by [`crate::device::FrameSource::EmulatedScene`].
pub exposure_bias: ExposureBias,
/// Pattern rendered by [`crate::device::FrameSource::TestPatterns`].
pub test_pattern: TestPattern,
/// Window of the sensor active array rendered by
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod userptr;
use std::path::PathBuf;

use clap::Parser;
use emulated_camera::controls::{GainControl, LensFacingControl};
use emulated_camera::controls::{ExposureBiasControl, GainControl, LensFacingControl};
use emulated_camera::{Control, ControlSet, FrameSource, LensFacing};
use thiserror::Error;

Expand Down Expand Up @@ -77,6 +77,7 @@ fn controls(lens_facing: LensFacing) -> Vec<Box<dyn Control>> {
vec![
Box::new(LensFacingControl::new(lens_facing)),
Box::new(GainControl::new()),
Box::new(ExposureBiasControl::new()),
]
}

Expand Down
Loading