Add standard exposure and gain controls for multiple captures
v2 cover Add standard exposure and gain controls for multiple captures
All controls are in the same class.
They can be set atomically via VIDIOC_S_EXT_CTRLS, this could turn out to be useful in case of sensors with context switching.
Each element of the array will hold an u32 value (exposure or gain) for one capture. The size of the array is up to the sensor driver which will implement the controls and initialize them via v4l2_ctrl_new_custom(). With this approach, the user-space will have to set valid values for all the captures represented in the array. The order of elements inside the array is from the longest to the shortest exposure.
The v4l2-core only supports one scalar min/max/step value for the entire array, and each element is validated and adjusted to be within these bounds in v4l2_ctrl_type_op_validate(). The significance for the maximum value for the exposure control could be "the max value for the long exposure" or "the max value for the sum of all exposures". If none of these is ok, the sensor driver can adjust the values as supported and the user space can use the TRY operation to query the sensor for the minimum or maximum values.
Changes in v2:
- Update documentation according to review comments
- Patch #3 adds v4l2 helpers for backward compatibility with single-capture controls
- Patch #4 uses those helpers in the v4l2 core functions,
to aleviate the backward compatibility task from new drivers implementing only multi-capture controls
- Patch #5 is a less aggresive alternative to patch #4, without v4l2 core changes, but which requires more actions in the drivers, including a s_ctrl implementation for single-capture controls, but using the helpers
- We will have to pick either patch #4 or patch #5
Mirela Rabulea (2):
media: Add exposure and gain controls for multiple captures
Documentation: media: Describe exposure and gain controls for multiple
captures
media: v4l2-ctrls: Add v4l2 helper functions for single-capture
controls
media: v4l2-ctrls: Use the single-capture control helpers in v4l2 core
media: v4l2-ctrls: Add __v4l2_s_ctrl_single_to_multi helper function
.../media/v4l/ext-ctrls-image-source.rst | 21 +++ drivers/media/v4l2-core/v4l2-ctrls-api.c | 28 +++- drivers/media/v4l2-core/v4l2-ctrls-core.c | 120 +++++++++++++++++- drivers/media/v4l2-core/v4l2-ctrls-defs.c | 8 ++ include/media/v4l2-ctrls.h | 93 ++++++++++++++ include/uapi/linux/v4l2-controls.h | 3 + 6 files changed, 268 insertions(+), 5 deletions(-)
Signature · 1 line
2.43.0
RFC v2 1/5 media: Add exposure and gain controls for multiple captures
Add V4L2_CID_EXPOSURE_MULTI, V4L2_CID_AGAIN_MULTI and V4L2_CID_DGAIN_MULTI for exposure and gain control for multiple exposure sensors.
--- drivers/media/v4l2-core/v4l2-ctrls-defs.c | 8 ++++++++ include/uapi/linux/v4l2-controls.h | 3 +++ 2 files changed, 11 insertions(+)
drivers/media/v4l2-core/v4l2-ctrls-defs.c +8 -0
@@ -1155,6 +1155,9 @@ const char *v4l2_ctrl_get_name(u32 id)case V4L2_CID_TEST_PATTERN_BLUE :return "Blue Pixel Value" ;case V4L2_CID_TEST_PATTERN_GREENB :return "Green (Blue) Pixel Value" ;case V4L2_CID_NOTIFY_GAINS :return "Notify Gains" ; +case V4L2_CID_EXPOSURE_MULTI :return "Exposure, Multiple Captures" ; +case V4L2_CID_AGAIN_MULTI :return "Analog Gain, Multiple Captures" ; +case V4L2_CID_DGAIN_MULTI :return "Digital Gain, Multiple Captures" ;/* Image processing controls */ /* Keep the order of the 'case's the same as in v4l2-controls.h! */
@@ -1607,6 +1610,11 @@ void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,case V4L2_CID_COLORIMETRY_HDR10_MASTERING_DISPLAY : *type =V4L2_CTRL_TYPE_HDR10_MASTERING_DISPLAY ;break ; +case V4L2_CID_EXPOSURE_MULTI : +case V4L2_CID_AGAIN_MULTI : +case V4L2_CID_DGAIN_MULTI : + *type =V4L2_CTRL_TYPE_U32 ; +break ;default : *type =V4L2_CTRL_TYPE_INTEGER ;break ;
include/uapi/linux/v4l2-controls.h +3 -0
@@ -1226,6 +1226,9 @@ enum v4l2_jpeg_chroma_subsampling {#define V4L2_CID_TEST_PATTERN_GREENB (V4L2_CID_IMAGE_SOURCE_CLASS_BASE + 7)#define V4L2_CID_UNIT_CELL_SIZE (V4L2_CID_IMAGE_SOURCE_CLASS_BASE + 8)#define V4L2_CID_NOTIFY_GAINS (V4L2_CID_IMAGE_SOURCE_CLASS_BASE + 9) +#define V4L2_CID_EXPOSURE_MULTI (V4L2_CID_IMAGE_SOURCE_CLASS_BASE + 10) +#define V4L2_CID_AGAIN_MULTI (V4L2_CID_IMAGE_SOURCE_CLASS_BASE + 11) +#define V4L2_CID_DGAIN_MULTI (V4L2_CID_IMAGE_SOURCE_CLASS_BASE + 12)/* Image processing controls */
Signature · 1 line
2.43.0
RFC v2 2/5 Documentation: media: Describe exposure and gain controls for multiple captures
In reply to Mirela RabuleaThe standard controls for exposure and gains allow a single value, for a single capture. For sensors with HDR capabilities or context switching, this is not enough, so add new controls that allow multiple values, one for each capture.
---
Changes in v2:
Mention order inside array, from longest to shortest exposure Mention not to implement the -single controls if -multi controls are implemented
.../media/v4l/ext-ctrls-image-source.rst | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+)
Documentation/userspace-api/media/v4l/ext-ctrls-image-source.rst +21 -0
@@ -92,3 +92,24 @@ Image Source Control IDs representing a gain of exactly 1.0. For example, if this default value is reported as being (say) 128, then a value of 192 would represent a gain of exactly 1.5. + +``V4L2_CID_EXPOSURE_MULTI (__u32 array)`` + Same as V4L2_CID_EXPOSURE, but for multiple exposure sensors. Each + element of the array holds the exposure value for one capture, from + the longest to the shortest exposure. Drivers for HDR sensors that + implement the V4L2_CID_EXPOSURE_MULTI control should not implement + V4L2_CID_EXPOSURE. + +``V4L2_CID_AGAIN_MULTI (__u32 array)`` + Same as V4L2_CID_ANALOGUE_GAIN, but for multiple exposure sensors. Each + element of the array holds the analog gain value for one capture, from + the longest to the shortest exposure. Drivers for HDR sensors that + implement the V4L2_CID_AGAIN_MULTI control should not implement + V4L2_CID_ANALOGUE_GAIN. + +``V4L2_CID_DGAIN_MULTI (__u32 array)`` + Same as V4L2_CID_DIGITAL_GAIN, but for multiple exposure sensors. Each + element of the array holds the digital gain value for one capture, from + the longest to the shortest exposure. Drivers for HDR sensors that + implement the V4L2_CID_DGAIN_MULTI control should not implement + V4L2_CID_DIGITAL_GAIN.
Signature · 1 line
2.43.0
RFC v2 3/5 media: v4l2-ctrls: Add v4l2 helper functions for single-capture controls
In reply to Mirela RabuleaAdd v4l2 helper functions to support backward compatibility with older user-space applications that are still using the single-capture exposure and gain controls.
After the addition of multi-capture exposure and gain controls, the following scenarios are possible:
1. Old applications & old sensor drivers
Both the application and the driver are using single-capture controls, no problem here.
2. Old applications & new sensor drivers
The application is using single-capture controls and we want the drivers to implement only the new multi-capture controls. In this case the driver should use the v4l2_ctrl_new_single_cap_ctrls helper to create the old single-capture controls, for backward compatibility.
3. New application & old sensor drivers
The new application can query if the driver supports multi-capture controls, if it doesn't, the application may fallback to single-capture controls.
4. New application & new sensor drivers
Both the application and the driver are using multi-capture controls, again, no problem here.
---
Changes in v2:
New patch
drivers/media/v4l2-core/v4l2-ctrls-core.c | 85 +++++++++++++++++++++++ include/media/v4l2-ctrls.h | 76 ++++++++++++++++++++ 2 files changed, 161 insertions(+)
drivers/media/v4l2-core/v4l2-ctrls-core.c +85 -0
@@ -2719,3 +2719,88 @@ int v4l2_ctrl_new_fwnode_properties(struct v4l2_ctrl_handler *hdl,return hdl->error; }EXPORT_SYMBOL (v4l2_ctrl_new_fwnode_properties); + +int v4l2_ctrl_new_single_cap_ctrls (struct v4l2_ctrl_handler *hdl, +const struct v4l2_ctrl_ops *ctrl_ops) +{ +if (hdl->error) +return hdl->error; + +v4l2_ctrl_new_std (hdl, ctrl_ops,V4L2_CID_EXPOSURE , +0 ,0 ,1 ,0 ); + +v4l2_ctrl_new_std (hdl, ctrl_ops,V4L2_CID_ANALOGUE_GAIN , +0 ,0 ,1 ,0 ); + +v4l2_ctrl_new_std (hdl, ctrl_ops,V4L2_CID_DIGITAL_GAIN , +0 ,0 ,1 ,0 ); + +return hdl->error; +} +EXPORT_SYMBOL (v4l2_ctrl_new_single_cap_ctrls); + +struct v4l2_ctrl *__v4l2_get_multi_ctrl (struct v4l2_ctrl *ctrl_single) +{ +struct v4l2_ctrl_handler *hdl = ctrl_single->handler; +struct v4l2_ctrl_ref *ref; + +switch (ctrl_single->id) { +case V4L2_CID_EXPOSURE : + ref =find_ref (hdl,V4L2_CID_EXPOSURE_MULTI ); +break ; +case V4L2_CID_ANALOGUE_GAIN : + ref =find_ref (hdl,V4L2_CID_AGAIN_MULTI ); +break ; +case V4L2_CID_DIGITAL_GAIN : + ref =find_ref (hdl,V4L2_CID_DGAIN_MULTI ); +break ; +default : +return NULL ; + } + +return ref->ctrl; +} +EXPORT_SYMBOL (__v4l2_get_multi_ctrl); + +struct v4l2_ctrl *__v4l2_get_single_ctrl (struct v4l2_ctrl *ctrl_multi) +{ +struct v4l2_ctrl_handler *hdl = ctrl_multi->handler; +struct v4l2_ctrl_ref *ref; + +switch (ctrl_multi->id) { +case V4L2_CID_EXPOSURE_MULTI : + ref =find_ref (hdl,V4L2_CID_EXPOSURE ); +break ; +case V4L2_CID_AGAIN_MULTI : + ref =find_ref (hdl,V4L2_CID_ANALOGUE_GAIN ); +break ; +case V4L2_CID_DGAIN_MULTI : + ref =find_ref (hdl,V4L2_CID_DIGITAL_GAIN ); +break ; +default : +return NULL ; + } + +return ref->ctrl; +} +EXPORT_SYMBOL (__v4l2_get_single_ctrl); + +int __v4l2_s_ctrl_multi_to_single (struct v4l2_ctrl *ctrl_multi) +{ +struct v4l2_ctrl *ctrl_single =NULL ; + +if (!ctrl_multi) +return -EINVAL ; + + ctrl_single =__v4l2_get_single_ctrl (ctrl_multi); +/* don't fail if no corresponding single control is found */ +if (!ctrl_single) +return 0 ; + +/* update the value of the single control, without calling s_ctrl */ + ctrl_single->cur.val = ctrl_multi->p_cur.p_u32[0 ]; + +return 0 ; +} +EXPORT_SYMBOL (__v4l2_s_ctrl_multi_to_single); +
include/media/v4l2-ctrls.h +76 -0
82 lines not on this page · raw message
Signature · 1 line
2.43.0
RFC v2 4/5 media: v4l2-ctrls: Use the single-capture control helpers in v4l2 core
In reply to Mirela RabuleaUse the following v4l2 helpers to support backward compatibility with single-capture controls, such that new drivers that only implement explicitly the multi-capture control won't have to worry about keeping them in sync with old single capture controls:
__v4l2_get_multi_ctrl
Used by the v2l2 core in try_or_set_cluster and __v4l2_ctrl_handler_setup to redirect s_ctrl towards the multi controls instead of single controls, so that the new sensor drivers do not need to implement the single-capture controls in s_ctrl.__v4l2_get_single_ctrl
Used by the v2l2 core in __v4l2_ctrl_modify_range, to modify also the range of single-capture control when multi-capture control range changes, so drivers won't have to worry about it.__v4l2_s_ctrl_multi_to_single
Used by the v2l2 core in __v4l2_ctrl_s_ctrl_compound, to transparently keep the value of single-capture control in sync with the value of the multi-capture control, so drivers won't have to worry about it. The __v4l2_ctrl_s_ctrl_compound() and __v4l2_ctrl_modify_range() are usually called during sensor mode change to update the exposure according to the current mode, the sensor driver only needs to update the multi-controls, and the v4l2-core will update the single-controls.
---
Changes in v2:
New patch
We need to choose either this patch (do as much as we can in the v4l2 core for backward compatibility for old userspace apps using single-capture controls) or patch #5 (let the new drivers still implement s_ctrl for single-capture controls, but using the helpers, also driver needs to take care to keep ranges and values in sync)
drivers/media/v4l2-core/v4l2-ctrls-api.c | 28 ++++++++++++++++++++--- drivers/media/v4l2-core/v4l2-ctrls-core.c | 23 +++++++++++++++++-- 2 files changed, 46 insertions(+), 5 deletions(-)
drivers/media/v4l2-core/v4l2-ctrls-api.c +25 -3
@@ -947,6 +947,8 @@ EXPORT_SYMBOL(__v4l2_ctrl_s_ctrl_string);int __v4l2_ctrl_s_ctrl_compound (struct v4l2_ctrl *ctrl,enum v4l2_ctrl_type type,const void *p) { +int ret; +lockdep_assert_held (ctrl->handler->lock);/* It's a driver bug if this happens. */
@@ -956,7 +958,14 @@ int __v4l2_ctrl_s_ctrl_compound(struct v4l2_ctrl *ctrl,if (WARN_ON (ctrl->is_dyn_array))return -EINVAL ;memcpy (ctrl->p_new.p, p, ctrl->elems * ctrl->elem_size); -return set_ctrl (NULL , ctrl,0 ); + ret =set_ctrl (NULL , ctrl,0 ); +if (!ret) +return ret; +/* +* this can be removed when backward compatibility with legacy +* single exposure and gain controls is no longer needed +*/ +return __v4l2_s_ctrl_multi_to_single (ctrl); }EXPORT_SYMBOL (__v4l2_ctrl_s_ctrl_compound);
@@ -968,6 +977,7 @@ int __v4l2_ctrl_modify_range(struct v4l2_ctrl *ctrl, {bool value_changed;bool range_changed = false; +struct v4l2_ctrl *ctrl_single =NULL ;int ret;lockdep_assert_held (ctrl->handler->lock);
@@ -982,8 +992,11 @@ int __v4l2_ctrl_modify_range(struct v4l2_ctrl *ctrl,case V4L2_CTRL_TYPE_U8 :case V4L2_CTRL_TYPE_U16 :case V4L2_CTRL_TYPE_U32 : -if (ctrl->is_array) -return -EINVAL ; +if (ctrl->is_array) { + ctrl_single =__v4l2_get_single_ctrl (ctrl); +if (!ctrl_single) +return -EINVAL ; + } ret =check_range (ctrl->type, min, max, step, def);if (ret)return ret;
@@ -1015,6 +1028,15 @@ int __v4l2_ctrl_modify_range(struct v4l2_ctrl *ctrl, ret =set_ctrl (NULL , ctrl,V4L2_EVENT_CTRL_CH_RANGE ); else if (range_changed) send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_RANGE); + if (ret) + return ret; + /* + * this can be removed when backward compatibility with legacy + * single exposure and gain controls is no longer needed + */ + if (ctrl_single) + ret = __v4l2_ctrl_modify_range(ctrl_single, min, max, step, def); + return ret; } EXPORT_SYMBOL(__v4l2_ctrl_modify_range);
drivers/media/v4l2-core/v4l2-ctrls-core.c +21 -2
@@ -2478,6 +2478,7 @@ int try_or_set_cluster(struct v4l2_fh *fh, struct v4l2_ctrl *master,bool update_flag;int ret;int i; +struct v4l2_ctrl *master_multi =NULL ;/* * Go through the cluster and either validate the new value or
@@ -2508,7 +2509,16 @@ int try_or_set_cluster(struct v4l2_fh *fh, struct v4l2_ctrl *master,/* Don't set if there is no change */ if (ret || !set || !cluster_changed (master))return ret; - ret =call_op (master, s_ctrl); + +/* +* for sensor drivers that no longer handle single-capture controls in +* s_ctrl, set the corresponding multi-capture control instead +*/ + master_multi =__v4l2_get_multi_ctrl (master); +if (master_multi) + ret =call_op (master_multi, s_ctrl); +else + ret =call_op (master, s_ctrl);if (ret)return ret;
@@ -2590,6 +2600,7 @@ int __v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)list_for_each_entry (ctrl, &hdl->ctrls, node) {struct v4l2_ctrl *master = ctrl->cluster[0 ]; +struct v4l2_ctrl *master_multi =__v4l2_get_multi_ctrl (master);int i;/* Skip if this control was already handled by a cluster. */
@@ -2605,7 +2616,15 @@ int __v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl) master->cluster[i]->done = true; } } - ret =call_op (master, s_ctrl); +/* +* for sensor drivers that no longer handle single-capture +* controls in s_ctrl, set the corresponding multi-capture +* control instead +*/ +if (master_multi) + ret =call_op (master_multi, s_ctrl); +else + ret =call_op (master, s_ctrl);if (ret)break ; }
Signature · 1 line
2.43.0
RFC v2 5/5 media: v4l2-ctrls: Add __v4l2_s_ctrl_single_to_multi helper function
In reply to Mirela RabuleaHelper function to be used in new sensor drivers to implement s_ctrl for single controls for backward compatibility.
This function finds the corresponding multi-capture control for a given single-capture control, and updates only the value corresponding to the first capture with the same value as the single-capture control value.
---
Changes in v2:
New patch
We need to choose either this patch or patch #4
drivers/media/v4l2-core/v4l2-ctrls-core.c | 12 ++++++++++++ include/media/v4l2-ctrls.h | 17 +++++++++++++++++ 2 files changed, 29 insertions(+)
drivers/media/v4l2-core/v4l2-ctrls-core.c +12 -0
@@ -2823,3 +2823,15 @@ int __v4l2_s_ctrl_multi_to_single(struct v4l2_ctrl *ctrl_multi) }EXPORT_SYMBOL (__v4l2_s_ctrl_multi_to_single); +int __v4l2_s_ctrl_single_to_multi (struct v4l2_ctrl *ctrl_single) +{ +struct v4l2_ctrl *ctrl_multi =__v4l2_get_multi_ctrl (ctrl_single); + +if (!ctrl_multi || !ctrl_single) +return -EINVAL ; + + ctrl_multi->p_cur.p_u32[0 ] = ctrl_single->val; +return __v4l2_ctrl_s_ctrl_compound (ctrl_multi,V4L2_CTRL_TYPE_U32 , + ctrl_multi->p_cur.p_u32); +} +EXPORT_SYMBOL (__v4l2_s_ctrl_single_to_multi);
include/media/v4l2-ctrls.h +17 -0
@@ -1663,6 +1663,23 @@ struct v4l2_ctrl *__v4l2_get_single_ctrl(struct v4l2_ctrl *ctrl_multi);*/ int __v4l2_s_ctrl_multi_to_single (struct v4l2_ctrl *ctrl_multi); +/** +* __v4l2_s_ctrl_single_to_multi() - Set the multi-capture control for a +* given single-capture control +* +* @ctrl_single: pointer to &struct v4l2_ctrl for the single-capture control +* +* This function finds the corresponding multi-capture control for a given +* single-capture control, and updates only the value corresponding to the +* first capture with the same value as the single-capture control value. +* +* This function assumes the control's handler is already locked, +* allowing it to be used from within the &v4l2_ctrl_ops functions. +* +* Return: 0 on success, a negative error code on failure. +*/ +int __v4l2_s_ctrl_single_to_multi (struct v4l2_ctrl *ctrl_single); +/** * v4l2_ctrl_type_op_equal - Default v4l2_ctrl_type_ops equal callback. *
Signature · 1 line
2.43.0