diff --git a/base/cvd/cuttlefish/host/commands/vhost_user_media/emulated_camera/BUILD.bazel b/base/cvd/cuttlefish/host/commands/vhost_user_media/emulated_camera/BUILD.bazel index 8ea101c2324..ae4d45afa80 100644 --- a/base/cvd/cuttlefish/host/commands/vhost_user_media/emulated_camera/BUILD.bazel +++ b/base/cvd/cuttlefish/host/commands/vhost_user_media/emulated_camera/BUILD.bazel @@ -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", diff --git a/base/cvd/cuttlefish/host/commands/vhost_user_media/emulated_camera/src/control.rs b/base/cvd/cuttlefish/host/commands/vhost_user_media/emulated_camera/src/control.rs index f91b71a980d..c4721e129d2 100644 --- a/base/cvd/cuttlefish/host/commands/vhost_user_media/emulated_camera/src/control.rs +++ b/base/cvd/cuttlefish/host/commands/vhost_user_media/emulated_camera/src/control.rs @@ -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::() as u32 @@ -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 { + None + } + /// Projects the value into `params`. fn apply(&self, _params: &mut RenderParams) {} } @@ -257,16 +266,20 @@ impl ControlSet { /// `VIDIOC_QUERYMENU`. pub fn querymenu(&self, id: u32, index: u32) -> IoctlResult { - 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() }) } diff --git a/base/cvd/cuttlefish/host/commands/vhost_user_media/emulated_camera/src/controls/exposure_bias.rs b/base/cvd/cuttlefish/host/commands/vhost_user_media/emulated_camera/src/controls/exposure_bias.rs new file mode 100644 index 00000000000..954cc3f0b58 --- /dev/null +++ b/base/cvd/cuttlefish/host/commands/vhost_user_media/emulated_camera/src/controls/exposure_bias.rs @@ -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 { + 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::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 { + let bias = Self::parse(value)?; + let changed = self.bias != bias; + self.bias = bias; + Ok(changed) + } + + fn menu_value(&self, index: u32) -> Option { + Self::ev_millis(index as i64).ok().map(i64::from) + } + + fn apply(&self, params: &mut RenderParams) { + params.exposure_bias = self.bias; + } +} diff --git a/base/cvd/cuttlefish/host/commands/vhost_user_media/emulated_camera/src/controls/mod.rs b/base/cvd/cuttlefish/host/commands/vhost_user_media/emulated_camera/src/controls/mod.rs index 85ab9b4ed2a..2d4a44b025f 100644 --- a/base/cvd/cuttlefish/host/commands/vhost_user_media/emulated_camera/src/controls/mod.rs +++ b/base/cvd/cuttlefish/host/commands/vhost_user_media/emulated_camera/src/controls/mod.rs @@ -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; diff --git a/base/cvd/cuttlefish/host/commands/vhost_user_media/emulated_camera/src/pipeline.rs b/base/cvd/cuttlefish/host/commands/vhost_user_media/emulated_camera/src/pipeline.rs index cfbf4b2f2f8..0e518de1aef 100644 --- a/base/cvd/cuttlefish/host/commands/vhost_user_media/emulated_camera/src/pipeline.rs +++ b/base/cvd/cuttlefish/host/commands/vhost_user_media/emulated_camera/src/pipeline.rs @@ -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) } } diff --git a/base/cvd/cuttlefish/host/commands/vhost_user_media/emulated_camera/src/render_params.rs b/base/cvd/cuttlefish/host/commands/vhost_user_media/emulated_camera/src/render_params.rs index 348385477f6..354fc4e41b5 100644 --- a/base/cvd/cuttlefish/host/commands/vhost_user_media/emulated_camera/src/render_params.rs +++ b/base/cvd/cuttlefish/host/commands/vhost_user_media/emulated_camera/src/render_params.rs @@ -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 { + 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 @@ -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 diff --git a/base/cvd/cuttlefish/host/commands/vhost_user_media/emulated_camera_mplane/src/main.rs b/base/cvd/cuttlefish/host/commands/vhost_user_media/emulated_camera_mplane/src/main.rs index cf3feb8a55e..e015d125cac 100644 --- a/base/cvd/cuttlefish/host/commands/vhost_user_media/emulated_camera_mplane/src/main.rs +++ b/base/cvd/cuttlefish/host/commands/vhost_user_media/emulated_camera_mplane/src/main.rs @@ -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; @@ -77,6 +77,7 @@ fn controls(lens_facing: LensFacing) -> Vec> { vec![ Box::new(LensFacingControl::new(lens_facing)), Box::new(GainControl::new()), + Box::new(ExposureBiasControl::new()), ] }