Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: Better handling of IMX708 sensor HDR #704

Merged
merged 1 commit into from
Aug 13, 2024
Merged
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
72 changes: 46 additions & 26 deletions core/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
#include <algorithm>
#include <fcntl.h>
#include <filesystem>
#include <iomanip>
#include <iostream>
#include <linux/v4l2-controls.h>
Expand All @@ -20,6 +21,8 @@

#include "core/options.hpp"

namespace fs = std::filesystem;

static const std::map<int, std::string> cfa_map =
{
{ properties::draft::ColorFilterArrangementEnum::RGGB, "RGGB" },
Expand Down Expand Up @@ -104,32 +107,42 @@ static int xioctl(int fd, unsigned long ctl, void *arg)
return ret;
}

static bool set_subdev_hdr_ctrl(int en)
static bool set_imx708_subdev_hdr_ctrl(int en, const std::string &cam_id)
{
bool changed = false;
// Currently this does not exist in libcamera, so go directly to V4L2
// XXX it's not obvious which v4l2-subdev to use for which camera!
for (int i = 0; i < 8; i++)
for (unsigned int i = 0; i < 16; i++)
{
std::string dev("/dev/v4l-subdev");
dev += (char)('0' + i);
int fd = open(dev.c_str(), O_RDWR, 0);
if (fd < 0)
continue;
const fs::path test_dir { "/sys/class/video4linux/v4l-subdev" + std::to_string(i) + "/device" };
const fs::path module_dir { test_dir.string() + "/driver/module" };
const fs::path id_dir { test_dir.string() + "/of_node" };

v4l2_control ctrl { V4L2_CID_WIDE_DYNAMIC_RANGE, en };
if (!xioctl(fd, VIDIOC_G_CTRL, &ctrl) && ctrl.value != en)
if (fs::exists(module_dir) && fs::is_symlink(module_dir))
{
ctrl.value = en;
if (!xioctl(fd, VIDIOC_S_CTRL, &ctrl))
changed = true;
fs::path ln = fs::read_symlink(module_dir);
if (ln.string().find("imx708") != std::string::npos &&
fs::is_symlink(id_dir) && fs::read_symlink(id_dir).string().find(cam_id) != std::string::npos)
{
const std::string dev_node { "/dev/v4l-subdev" + std::to_string(i) };
int fd = open(dev_node.c_str(), O_RDONLY, 0);
if (fd < 0)
continue;

v4l2_control ctrl { V4L2_CID_WIDE_DYNAMIC_RANGE, en };
if (!xioctl(fd, VIDIOC_G_CTRL, &ctrl) && ctrl.value != en)
{
ctrl.value = en;
if (!xioctl(fd, VIDIOC_S_CTRL, &ctrl))
{
close(fd);
return true;
}
}
close(fd);
}
}
close(fd);
}
return changed;
return false;
}


Platform get_platform()
{
bool unknown = false;
Expand Down Expand Up @@ -364,10 +377,6 @@ bool Options::Parse(int argc, char *argv[])
if (!verbose || list_cameras)
libcamera::logSetTarget(libcamera::LoggingTargetNone);

// HDR control. Set the sensor control before opening or listing any cameras.
// Start by disabling HDR unconditionally. Reset the camera manager if we have
// actually switched the value of the control.
set_subdev_hdr_ctrl(0);
app_->initCameraManager();

bool log_env_set = getenv("LIBCAMERA_LOG_LEVELS");
Expand All @@ -379,16 +388,27 @@ bool Options::Parse(int argc, char *argv[])
if (camera < cameras.size())
{
const std::string cam_id = *cameras[camera]->properties().get(libcamera::properties::Model);
if ((hdr == "sensor" || hdr == "auto") && cam_id.find("imx708") != std::string::npos)

if (cam_id.find("imx708") != std::string::npos)
{
// Turn on sensor HDR. Reset the camera manager if we have switched the value of the control.
if (set_subdev_hdr_ctrl(1))
// HDR control. Set the sensor control before opening or listing any cameras.
// Start by disabling HDR unconditionally. Reset the camera manager if we have
// actually switched the value of the control
bool changed = set_imx708_subdev_hdr_ctrl(0, cameras[camera]->id());

if (hdr == "sensor" || hdr == "auto")
{
// Turn on sensor HDR. Reset the camera manager if we have switched the value of the control.
changed |= set_imx708_subdev_hdr_ctrl(1, cameras[camera]->id());
hdr = "sensor";
}

if (changed)
{
cameras.clear();
app_->initCameraManager();
cameras = app_->GetCameras();
}
hdr = "sensor";
}
}

Expand Down
Loading