Re: [PATCH v11 13/15] media: vivid: add fixed point test controls

From: Hans Verkuil
Date: Wed Jan 17 2024 - 05:18:43 EST


On 22/11/2023 08:23, Shengjiu Wang wrote:
> Add fixed point test controls, one is for Q4.16 format
> another one is for Q63 format.
>
> Signed-off-by: Shengjiu Wang <shengjiu.wang@xxxxxxx>
> ---
> drivers/media/test-drivers/vivid/vivid-core.h | 2 ++
> .../media/test-drivers/vivid/vivid-ctrls.c | 26 +++++++++++++++++++
> include/media/v4l2-ctrls.h | 3 +++
> 3 files changed, 31 insertions(+)
>
> diff --git a/drivers/media/test-drivers/vivid/vivid-core.h b/drivers/media/test-drivers/vivid/vivid-core.h
> index cfb8e66083f6..f65465191bc9 100644
> --- a/drivers/media/test-drivers/vivid/vivid-core.h
> +++ b/drivers/media/test-drivers/vivid/vivid-core.h
> @@ -222,6 +222,8 @@ struct vivid_dev {
> struct v4l2_ctrl *boolean;
> struct v4l2_ctrl *int32;
> struct v4l2_ctrl *int64;
> + struct v4l2_ctrl *int32_q16;
> + struct v4l2_ctrl *int64_q63;
> struct v4l2_ctrl *menu;
> struct v4l2_ctrl *string;
> struct v4l2_ctrl *bitmask;
> diff --git a/drivers/media/test-drivers/vivid/vivid-ctrls.c b/drivers/media/test-drivers/vivid/vivid-ctrls.c
> index f2b20e25a7a4..2444ea95b285 100644
> --- a/drivers/media/test-drivers/vivid/vivid-ctrls.c
> +++ b/drivers/media/test-drivers/vivid/vivid-ctrls.c
> @@ -38,6 +38,8 @@
> #define VIVID_CID_U8_PIXEL_ARRAY (VIVID_CID_CUSTOM_BASE + 14)
> #define VIVID_CID_S32_ARRAY (VIVID_CID_CUSTOM_BASE + 15)
> #define VIVID_CID_S64_ARRAY (VIVID_CID_CUSTOM_BASE + 16)
> +#define VIVID_CID_INT_Q4_16 (VIVID_CID_CUSTOM_BASE + 17)
> +#define VIVID_CID_INT64_Q63 (VIVID_CID_CUSTOM_BASE + 18)
>
> #define VIVID_CID_VIVID_BASE (0x00f00000 | 0xf000)
> #define VIVID_CID_VIVID_CLASS (0x00f00000 | 1)
> @@ -182,6 +184,28 @@ static const struct v4l2_ctrl_config vivid_ctrl_int64 = {
> .step = 1,
> };
>
> +static const struct v4l2_ctrl_config vivid_ctrl_int32_q16 = {
> + .ops = &vivid_user_gen_ctrl_ops,
> + .id = VIVID_CID_INT_Q4_16,
> + .name = "Integer 32 Bits Q4.16",
> + .type = V4L2_CTRL_TYPE_INTEGER,
> + .min = v4l2_ctrl_fp_compose(-16, 0, 16),
> + .max = v4l2_ctrl_fp_compose(15, 0xffff, 16),
> + .step = 1,
> + .fraction_bits = 16,
> +};
> +
> +static const struct v4l2_ctrl_config vivid_ctrl_int64_q63 = {
> + .ops = &vivid_user_gen_ctrl_ops,
> + .id = VIVID_CID_INT64_Q63,
> + .name = "Integer 64 Bits Q63",
> + .type = V4L2_CTRL_TYPE_INTEGER64,
> + .min = v4l2_ctrl_fp_compose(-1, 0, 63),
> + .max = v4l2_ctrl_fp_compose(0, LLONG_MAX, 63),
> + .step = 1,
> + .fraction_bits = 63,
> +};
> +
> static const struct v4l2_ctrl_config vivid_ctrl_u32_array = {
> .ops = &vivid_user_gen_ctrl_ops,
> .id = VIVID_CID_U32_ARRAY,
> @@ -1670,6 +1694,8 @@ int vivid_create_controls(struct vivid_dev *dev, bool show_ccs_cap,
> dev->button = v4l2_ctrl_new_custom(hdl_user_gen, &vivid_ctrl_button, NULL);
> dev->int32 = v4l2_ctrl_new_custom(hdl_user_gen, &vivid_ctrl_int32, NULL);
> dev->int64 = v4l2_ctrl_new_custom(hdl_user_gen, &vivid_ctrl_int64, NULL);
> + dev->int32_q16 = v4l2_ctrl_new_custom(hdl_user_gen, &vivid_ctrl_int32_q16, NULL);
> + dev->int64_q63 = v4l2_ctrl_new_custom(hdl_user_gen, &vivid_ctrl_int64_q63, NULL);
> dev->boolean = v4l2_ctrl_new_custom(hdl_user_gen, &vivid_ctrl_boolean, NULL);
> dev->menu = v4l2_ctrl_new_custom(hdl_user_gen, &vivid_ctrl_menu, NULL);
> dev->string = v4l2_ctrl_new_custom(hdl_user_gen, &vivid_ctrl_string, NULL);
> diff --git a/include/media/v4l2-ctrls.h b/include/media/v4l2-ctrls.h
> index c35514c5bf88..d18fd116238b 100644
> --- a/include/media/v4l2-ctrls.h
> +++ b/include/media/v4l2-ctrls.h
> @@ -1593,4 +1593,7 @@ void v4l2_ctrl_type_op_log(const struct v4l2_ctrl *ctrl);
> */
> int v4l2_ctrl_type_op_validate(const struct v4l2_ctrl *ctrl, union v4l2_ctrl_ptr ptr);
>
> +/* Composite function for integer and fractional bits */

This comment needs to be expanded a bit:

/*
* Fixed point compose helper define. This helper maps to the value
* i + f / (1 << fraction_bits).
*/

> +#define v4l2_ctrl_fp_compose(i, f, fraction_bits) (((s64)(i) << fraction_bits) + (f))
> +
> #endif

Regards,

Hans