libcamera: Extend u16 control type

V4L2 Controls support a wide variety of types not yet supported by the
ControlValue type system.

Extend the libcamera ControlValue types to support an explicit 16 bit
unsigned integer type, and map that to the corresponding
V4L2_CTRL_TYPE_U16 type within the v4l2_device support class.

It's used on some camera metadata that is of length 16-bits,
for example JPEG metadata headers.

Signed-off-by: Yudhistira Erlandinata <yerlandinata@chromium.org>
Co-developed-by: Harvey Yang <chenghaoyang@chromium.org>
Signed-off-by: Harvey Yang <chenghaoyang@chromium.org>
Reviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
This commit is contained in:
Yudhistira Erlandinata 2024-10-29 16:07:13 +00:00 committed by Kieran Bingham
parent 86902b39d7
commit d711a4c015
4 changed files with 83 additions and 0 deletions

View file

@ -29,6 +29,7 @@ enum ControlType {
ControlTypeNone, ControlTypeNone,
ControlTypeBool, ControlTypeBool,
ControlTypeByte, ControlTypeByte,
ControlTypeUnsigned16,
ControlTypeUnsigned32, ControlTypeUnsigned32,
ControlTypeInteger32, ControlTypeInteger32,
ControlTypeInteger64, ControlTypeInteger64,
@ -63,6 +64,12 @@ struct control_type<uint8_t> {
static constexpr std::size_t size = 0; static constexpr std::size_t size = 0;
}; };
template<>
struct control_type<uint16_t> {
static constexpr ControlType value = ControlTypeUnsigned16;
static constexpr std::size_t size = 0;
};
template<> template<>
struct control_type<uint32_t> { struct control_type<uint32_t> {
static constexpr ControlType value = ControlTypeUnsigned32; static constexpr ControlType value = ControlTypeUnsigned32;

View file

@ -54,6 +54,7 @@ static constexpr size_t ControlValueSize[] = {
[ControlTypeNone] = 0, [ControlTypeNone] = 0,
[ControlTypeBool] = sizeof(bool), [ControlTypeBool] = sizeof(bool),
[ControlTypeByte] = sizeof(uint8_t), [ControlTypeByte] = sizeof(uint8_t),
[ControlTypeUnsigned16] = sizeof(uint16_t),
[ControlTypeUnsigned32] = sizeof(uint32_t), [ControlTypeUnsigned32] = sizeof(uint32_t),
[ControlTypeInteger32] = sizeof(int32_t), [ControlTypeInteger32] = sizeof(int32_t),
[ControlTypeInteger64] = sizeof(int64_t), [ControlTypeInteger64] = sizeof(int64_t),
@ -75,6 +76,8 @@ static constexpr size_t ControlValueSize[] = {
* The control stores a boolean value * The control stores a boolean value
* \var ControlTypeByte * \var ControlTypeByte
* The control stores a byte value as an unsigned 8-bit integer * The control stores a byte value as an unsigned 8-bit integer
* \var ControlTypeUnsigned16
* The control stores an unsigned 16-bit integer value
* \var ControlTypeUnsigned32 * \var ControlTypeUnsigned32
* The control stores an unsigned 32-bit integer value * The control stores an unsigned 32-bit integer value
* \var ControlTypeInteger32 * \var ControlTypeInteger32
@ -233,6 +236,11 @@ std::string ControlValue::toString() const
str += std::to_string(*value); str += std::to_string(*value);
break; break;
} }
case ControlTypeUnsigned16: {
const uint16_t *value = reinterpret_cast<const uint16_t *>(data);
str += std::to_string(*value);
break;
}
case ControlTypeUnsigned32: { case ControlTypeUnsigned32: {
const uint32_t *value = reinterpret_cast<const uint32_t *>(data); const uint32_t *value = reinterpret_cast<const uint32_t *>(data);
str += std::to_string(*value); str += std::to_string(*value);

View file

@ -216,6 +216,13 @@ ControlList V4L2Device::getControls(const std::vector<uint32_t> &ids)
v4l2Ctrl.p_u8 = data.data(); v4l2Ctrl.p_u8 = data.data();
break; break;
case V4L2_CTRL_TYPE_U16:
type = ControlTypeUnsigned16;
value.reserve(type, true, info.elems);
data = value.data();
v4l2Ctrl.p_u16 = reinterpret_cast<uint16_t *>(data.data());
break;
case V4L2_CTRL_TYPE_U32: case V4L2_CTRL_TYPE_U32:
type = ControlTypeUnsigned32; type = ControlTypeUnsigned32;
value.reserve(type, true, info.elems); value.reserve(type, true, info.elems);
@ -307,6 +314,18 @@ int V4L2Device::setControls(ControlList *ctrls)
/* Set the v4l2_ext_control value for the write operation. */ /* Set the v4l2_ext_control value for the write operation. */
ControlValue &value = ctrl->second; ControlValue &value = ctrl->second;
switch (iter->first->type()) { switch (iter->first->type()) {
case ControlTypeUnsigned16: {
if (value.isArray()) {
Span<uint8_t> data = value.data();
v4l2Ctrl.p_u16 = reinterpret_cast<uint16_t *>(data.data());
v4l2Ctrl.size = data.size();
} else {
v4l2Ctrl.value = value.get<uint16_t>();
}
break;
}
case ControlTypeUnsigned32: { case ControlTypeUnsigned32: {
if (value.isArray()) { if (value.isArray()) {
Span<uint8_t> data = value.data(); Span<uint8_t> data = value.data();
@ -508,6 +527,9 @@ ControlType V4L2Device::v4l2CtrlType(uint32_t ctrlType)
case V4L2_CTRL_TYPE_BOOLEAN: case V4L2_CTRL_TYPE_BOOLEAN:
return ControlTypeBool; return ControlTypeBool;
case V4L2_CTRL_TYPE_U16:
return ControlTypeUnsigned16;
case V4L2_CTRL_TYPE_U32: case V4L2_CTRL_TYPE_U32:
return ControlTypeUnsigned32; return ControlTypeUnsigned32;
@ -559,6 +581,11 @@ std::optional<ControlInfo> V4L2Device::v4l2ControlInfo(const v4l2_query_ext_ctrl
static_cast<uint8_t>(ctrl.maximum), static_cast<uint8_t>(ctrl.maximum),
static_cast<uint8_t>(ctrl.default_value)); static_cast<uint8_t>(ctrl.default_value));
case V4L2_CTRL_TYPE_U16:
return ControlInfo(static_cast<uint16_t>(ctrl.minimum),
static_cast<uint16_t>(ctrl.maximum),
static_cast<uint16_t>(ctrl.default_value));
case V4L2_CTRL_TYPE_U32: case V4L2_CTRL_TYPE_U32:
return ControlInfo(static_cast<uint32_t>(ctrl.minimum), return ControlInfo(static_cast<uint32_t>(ctrl.minimum),
static_cast<uint32_t>(ctrl.maximum), static_cast<uint32_t>(ctrl.maximum),
@ -650,6 +677,7 @@ void V4L2Device::listControls()
case V4L2_CTRL_TYPE_BITMASK: case V4L2_CTRL_TYPE_BITMASK:
case V4L2_CTRL_TYPE_INTEGER_MENU: case V4L2_CTRL_TYPE_INTEGER_MENU:
case V4L2_CTRL_TYPE_U8: case V4L2_CTRL_TYPE_U8:
case V4L2_CTRL_TYPE_U16:
case V4L2_CTRL_TYPE_U32: case V4L2_CTRL_TYPE_U32:
break; break;
/* \todo Support other control types. */ /* \todo Support other control types. */

View file

@ -109,6 +109,46 @@ protected:
return TestFail; return TestFail;
} }
/*
* Unsigned Integer16 type.
*/
value.set(static_cast<uint16_t>(42));
if (value.isNone() || value.isArray() ||
value.type() != ControlTypeUnsigned16) {
cerr << "Control type mismatch after setting to uint16_t" << endl;
return TestFail;
}
if (value.get<uint16_t>() != 42) {
cerr << "Control value mismatch after setting to uint16_t" << endl;
return TestFail;
}
if (value.toString() != "42") {
cerr << "Control string mismatch after setting to uint16_t" << endl;
return TestFail;
}
std::array<uint16_t, 4> uint16s{ 3, 14, 15, 9 };
value.set(Span<uint16_t>(uint16s));
if (value.isNone() || !value.isArray() ||
value.type() != ControlTypeUnsigned16) {
cerr << "Control type mismatch after setting to uint16_t array" << endl;
return TestFail;
}
Span<const uint16_t> uint16sResult = value.get<Span<const uint16_t>>();
if (uint16s.size() != uint16sResult.size() ||
!std::equal(uint16s.begin(), uint16s.end(), uint16sResult.begin())) {
cerr << "Control value mismatch after setting to uint16_t array" << endl;
return TestFail;
}
if (value.toString() != "[ 3, 14, 15, 9 ]") {
cerr << "Control string mismatch after setting to uint16_t array" << endl;
return TestFail;
}
/* /*
* Unsigned Integer32 type. * Unsigned Integer32 type.
*/ */