Skip to content

Commit 4fea2ee

Browse files
njhollinghurstnaushir
authored andcommitted
core: Add HDR option, with a stop-gap implementation using V4L2 control
Signed-off-by: Nick Hollinghurst <nick.hollinghurst@raspberrypi.com>
1 parent bce2788 commit 4fea2ee

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

core/options.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
*
55
* options.cpp - common program options helpers
66
*/
7+
#include <fcntl.h>
8+
#include <linux/v4l2-controls.h>
9+
#include <linux/videodev2.h>
10+
#include <sys/ioctl.h>
711
#include <algorithm>
812
#include <iomanip>
913
#include <iostream>
@@ -47,6 +51,16 @@ std::string Mode::ToString() const
4751
}
4852
}
4953

54+
static int xioctl(int fd, unsigned long ctl, void *arg)
55+
{
56+
int ret, num_tries = 10;
57+
do
58+
{
59+
ret = ioctl(fd, ctl, arg);
60+
} while (ret == -1 && errno == EINTR && num_tries-- > 0);
61+
return ret;
62+
}
63+
5064
bool Options::Parse(int argc, char *argv[])
5165
{
5266
using namespace boost::program_options;
@@ -79,6 +93,27 @@ bool Options::Parse(int argc, char *argv[])
7993
else if (!lens_position_.empty())
8094
throw std::runtime_error("Invalid lens position: " + lens_position_);
8195

96+
// HDR control. Set this before opening or listing any cameras.
97+
// Currently this does not exist in libcamera, so go directly to V4L2
98+
// XXX it's not obvious which v4l2-subdev to use for which camera!
99+
{
100+
bool ok = false;
101+
for (int i = 0; i < 4 && !ok; i++)
102+
{
103+
std::string dev("/dev/v4l-subdev");
104+
dev += (char)('0' + i);
105+
int fd = open(dev.c_str(), O_RDWR, 0);
106+
if (fd < 0)
107+
continue;
108+
109+
v4l2_control ctrl { V4L2_CID_WIDE_DYNAMIC_RANGE, hdr };
110+
ok = !xioctl(fd, VIDIOC_S_CTRL, &ctrl);
111+
close(fd);
112+
}
113+
if (hdr && !ok)
114+
LOG_ERROR("WARNING: Unable to set HDR mode");
115+
}
116+
82117
// Set the verbosity
83118
LibcameraApp::verbosity = verbose;
84119

@@ -349,6 +384,8 @@ void Options::Print() const
349384
<< afWindow_height << std::endl;
350385
if (!lens_position_.empty())
351386
std::cerr << " lens-position: " << lens_position_ << std::endl;
387+
if (hdr)
388+
std::cerr << " hdr: enabled" << hdr << std::endl;
352389
std::cerr << " mode: " << mode.ToString() << std::endl;
353390
std::cerr << " viewfinder-mode: " << viewfinder_mode.ToString() << std::endl;
354391
if (buffer_count > 0)

core/options.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ struct Options
146146
"Sets AfMetering to AfMeteringWindows an set region used, e.g. 0.25,0.25,0.5,0.5")
147147
("lens-position", value<std::string>(&lens_position_)->default_value(""),
148148
"Set the lens to a particular focus position, expressed as a reciprocal distance (0 moves the lens to infinity), or \"default\" for the hyperfocal distance")
149+
("hdr", value<bool>(&hdr)->default_value(false)->implicit_value(true),
150+
"Enable (1) or disable (0) High Dynamic Range, where supported")
149151
("metadata", value<std::string>(&metadata),
150152
"Save captured image metadata to a file or \"-\" for stdout")
151153
("metadata-format", value<std::string>(&metadata_format)->default_value("json"),
@@ -221,6 +223,7 @@ struct Options
221223
bool af_on_capture;
222224
std::string metadata;
223225
std::string metadata_format;
226+
bool hdr;
224227

225228
virtual bool Parse(int argc, char *argv[]);
226229
virtual void Print() const;

0 commit comments

Comments
 (0)