From 799982b6465a9389bf525b91dbb100e6dd1913c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A1s=20P=C5=91cze?= Date: Tue, 28 Jan 2025 12:40:56 +0100 Subject: libcamera: pipeline: uvcvideo: Fix `ExposureTimeMode` control setup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `ControlInfo(Span{...})` calls the incorrect constructor of `ControlInfo`. The intended constructor to be called is `ControlInfo(Span, ...)` however that is not called because a span of `const int32_t` is passed. Instead, the constructor `ControlInfo(const ControlValue &min, const ControlValue &max, ...)` will be called. Furthermore, since `values.back()` is used, only the last element of the array is actually set. To fix this, convert the array to contain `ControlValue` objects and use a separate variable to keep track of which element to set next. For each of `ExposureTimeMode{Auto,Manual}` save the V4L2 control value that is to be used when the libcamera control is set. Fixes: bad8d591f8acfa ("libcamera: uvcvideo: Register ExposureTimeMode control") Signed-off-by: Barnabás Pőcze Reviewed-by: Jacopo Mondi --- src/libcamera/pipeline/uvcvideo/uvcvideo.cpp | 90 ++++++++++++++++++++++------ 1 file changed, 71 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp b/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp index 7470b562..3a2a64cf 100644 --- a/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp +++ b/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp @@ -6,10 +6,12 @@ */ #include +#include #include #include #include #include +#include #include #include #include @@ -56,6 +58,9 @@ public: Stream stream_; std::map> formats_; + std::optional autoExposureMode_; + std::optional manualExposureMode_; + private: bool generateId(); @@ -106,6 +111,26 @@ private: } }; +namespace { + +std::optional v4l2ToExposureMode(int32_t x) +{ + using namespace controls; + + switch (x) { + case V4L2_EXPOSURE_AUTO: + case V4L2_EXPOSURE_APERTURE_PRIORITY: + return ExposureTimeModeAuto; + case V4L2_EXPOSURE_MANUAL: + case V4L2_EXPOSURE_SHUTTER_PRIORITY: + return ExposureTimeModeManual; + default: + return {}; + } +} + +} /* namespace */ + UVCCameraConfiguration::UVCCameraConfiguration(UVCCameraData *data) : CameraConfiguration(), data_(data) { @@ -723,25 +748,52 @@ void UVCCameraData::addControl(uint32_t cid, const ControlInfo &v4l2Info, * ExposureTimeModeManual = { V4L2_EXPOSURE_MANUAL, * V4L2_EXPOSURE_SHUTTER_PRIORITY } */ - std::array values{}; - - auto it = std::find_if(v4l2Values.begin(), v4l2Values.end(), - [&](const ControlValue &val) { - return (val.get() == V4L2_EXPOSURE_APERTURE_PRIORITY || - val.get() == V4L2_EXPOSURE_AUTO) ? true : false; - }); - if (it != v4l2Values.end()) - values.back() = static_cast(controls::ExposureTimeModeAuto); - - it = std::find_if(v4l2Values.begin(), v4l2Values.end(), - [&](const ControlValue &val) { - return (val.get() == V4L2_EXPOSURE_SHUTTER_PRIORITY || - val.get() == V4L2_EXPOSURE_MANUAL) ? true : false; - }); - if (it != v4l2Values.end()) - values.back() = static_cast(controls::ExposureTimeModeManual); - - info = ControlInfo{Span{values}, values[0]}; + + std::bitset< + std::max(V4L2_EXPOSURE_AUTO, + std::max(V4L2_EXPOSURE_APERTURE_PRIORITY, + std::max(V4L2_EXPOSURE_MANUAL, + V4L2_EXPOSURE_SHUTTER_PRIORITY))) + 1 + > exposureModes; + std::optional lcDef; + + for (const ControlValue &value : v4l2Values) { + const auto x = value.get(); + + if (0 <= x && static_cast(x) < exposureModes.size()) { + exposureModes[x] = true; + + if (x == def) + lcDef = v4l2ToExposureMode(x); + } + } + + if (exposureModes[V4L2_EXPOSURE_AUTO]) + autoExposureMode_ = V4L2_EXPOSURE_AUTO; + else if (exposureModes[V4L2_EXPOSURE_APERTURE_PRIORITY]) + autoExposureMode_ = V4L2_EXPOSURE_APERTURE_PRIORITY; + + if (exposureModes[V4L2_EXPOSURE_SHUTTER_PRIORITY]) + manualExposureMode_ = V4L2_EXPOSURE_SHUTTER_PRIORITY; + else if (exposureModes[V4L2_EXPOSURE_MANUAL]) + manualExposureMode_ = V4L2_EXPOSURE_MANUAL; + + std::array values; + std::size_t count = 0; + + if (autoExposureMode_) + values[count++] = controls::ExposureTimeModeAuto; + + if (manualExposureMode_) + values[count++] = controls::ExposureTimeModeManual; + + if (count == 0) + return; + + info = ControlInfo{ + Span{ values.data(), count }, + !lcDef ? values.front() : *lcDef, + }; break; } case V4L2_CID_EXPOSURE_ABSOLUTE: -- cgit v1.2.1