Re: [PATCH v2] media: vimc: Implement debayer control for mean window size

From: Hans Verkuil
Date: Thu Sep 19 2019 - 04:20:44 EST


On 9/18/19 1:12 AM, Shuah Khan wrote:
> On 9/17/19 4:53 PM, Arthur Moraes do Lago wrote:
>> Add mean window size parameter for debayer filter as a control in
>> vimc-debayer.
>>
>> vimc-debayer was patched to allow changing mean windows parameter
>> of the filter without needing to reload the driver. The parameter
>> can now be set using a v4l2-ctl control(mean_window_size).
>>
>> Co-developed-by: LaÃs Pessine do Carmo <laispc19@xxxxxxxxx>
>> Signed-off-by: LaÃs Pessine do Carmo <laispc19@xxxxxxxxx>
>> Signed-off-by: Arthur Moraes do Lago <arthurmoraeslago@xxxxxxxxx>
>>
>> Small fixes to vimc debayer mean window size patch
>> ---
>> Changes in V2:
>> Â - Updated documentation
>> Â - Added v4l2_subev_core_ops to solve errors in v4l2-ctl compliance test
>> Â - Changed control naming to follow English capitalization rules
>> Â - Rebased to Shuah Khan's newest patch series 171283
>> ÂÂÂÂ ("Collapse vimc single monolithic driver")
>> Â - Change maximum value for mean window size
>>
>> We did run streaming tests, the value of 1 for mean window size has a
>> nice side effect of not applying any filter, so we can see the original
>> pattern. The value of 99 for mean window size worked, but was very slow.
>> We wanted to find a way to show that higher values for mean window size
>> yielded very similar results to just blurring the image, but we could
>> not find any way to show this, so we just made the maximum value 25,
>> which runs faster, but is still probably a little high.
>> ---
>> Â Documentation/media/v4l-drivers/vimc.rstÂÂ | 10 +--
>>  drivers/media/platform/vimc/vimc-common.h | 1 +
>> Â drivers/media/platform/vimc/vimc-debayer.c | 89 +++++++++++++++++++---
>> Â 3 files changed, 79 insertions(+), 21 deletions(-)
>>
>> diff --git a/Documentation/media/v4l-drivers/vimc.rst b/Documentation/media/v4l-drivers/vimc.rst
>> index a582af0509ee..91c909904b87 100644
>> --- a/Documentation/media/v4l-drivers/vimc.rst
>> +++ b/Documentation/media/v4l-drivers/vimc.rst
>> @@ -80,9 +80,7 @@ vimc-capture:
>> ÂÂÂÂÂÂÂÂÂ Module options
>> Â ---------------
>> Â -Vimc has a few module parameters to configure the driver.
>> -
>> -ÂÂÂÂÂÂÂ param=value
>> +Vimc has a module parameters to configure the driver.
>
> parameter
>
>> Â Â * ``sca_mult=<unsigned int>``
>> Â @@ -91,12 +89,6 @@ Vimc has a few module parameters to configure the driver.
>> ÂÂÂÂÂÂÂÂÂ original one. Currently, only supports scaling up (the default value
>> ÂÂÂÂÂÂÂÂÂ is 3).
>> Â -* ``deb_mean_win_size=<unsigned int>``
>> -
>> -ÂÂÂÂÂÂÂ Window size to calculate the mean. Note: the window size needs to be an
>> -ÂÂÂÂÂÂÂ odd number, as the main pixel stays in the center of the window,
>> -ÂÂÂÂÂÂÂ otherwise the next odd number is considered (the default value is 3).
>> -
>> Â Source code documentation
>> Â -------------------------
>> Â diff --git a/drivers/media/platform/vimc/vimc-common.h b/drivers/media/platform/vimc/vimc-common.h
>> index 236412ad7548..3a5102ddf794 100644
>> --- a/drivers/media/platform/vimc/vimc-common.h
>> +++ b/drivers/media/platform/vimc/vimc-common.h
>> @@ -19,6 +19,7 @@
>> Â #define VIMC_CID_VIMC_BASEÂÂÂÂÂÂÂ (0x00f00000 | 0xf000)
>> Â #define VIMC_CID_VIMC_CLASSÂÂÂÂÂÂÂ (0x00f00000 | 1)
>> Â #define VIMC_CID_TEST_PATTERNÂÂÂÂÂÂÂ (VIMC_CID_VIMC_BASE + 0)
>> +#define VIMC_CID_MEAN_WIN_SIZEÂÂÂÂÂÂÂ (VIMC_CID_VIMC_BASE + 1)
>> Â Â #define VIMC_FRAME_MAX_WIDTH 4096
>> Â #define VIMC_FRAME_MAX_HEIGHT 2160
>> diff --git a/drivers/media/platform/vimc/vimc-debayer.c b/drivers/media/platform/vimc/vimc-debayer.c
>> index 37f3767db469..76df2ac110c0 100644
>> --- a/drivers/media/platform/vimc/vimc-debayer.c
>> +++ b/drivers/media/platform/vimc/vimc-debayer.c
>> @@ -11,17 +11,12 @@
>> Â #include <linux/platform_device.h>
>> Â #include <linux/vmalloc.h>
>> Â #include <linux/v4l2-mediabus.h>
>> +#include <media/v4l2-ctrls.h>
>> +#include <media/v4l2-event.h>
>> Â #include <media/v4l2-subdev.h>
>> Â Â #include "vimc-common.h"
>> Â -static unsigned int deb_mean_win_size = 3;
>> -module_param(deb_mean_win_size, uint, 0000);
>> -MODULE_PARM_DESC(deb_mean_win_size, " the window size to calculate the mean.\n"
>> -ÂÂÂ "NOTE: the window size needs to be an odd number, as the main pixel "
>> -ÂÂÂ "stays in the center of the window, otherwise the next odd number "
>> -ÂÂÂ "is considered");
>> -
>> Â enum vimc_deb_rgb_colors {
>> ÂÂÂÂÂ VIMC_DEB_RED = 0,
>> ÂÂÂÂÂ VIMC_DEB_GREEN = 1,
>> @@ -46,6 +41,8 @@ struct vimc_deb_device {
>> ÂÂÂÂÂ u8 *src_frame;
>> ÂÂÂÂÂ const struct vimc_deb_pix_map *sink_pix_map;
>> ÂÂÂÂÂ unsigned int sink_bpp;
>> +ÂÂÂ unsigned int mean_win_size;
>> +ÂÂÂ struct v4l2_ctrl_handler hdl;
>> Â };
>> Â Â static const struct v4l2_mbus_framefmt sink_fmt_default = {
>> @@ -346,11 +343,18 @@ static int vimc_deb_s_stream(struct v4l2_subdev *sd, int enable)
>> ÂÂÂÂÂ return 0;
>> Â }
>> Â +static const struct v4l2_subdev_core_ops vimc_deb_core_ops = {
>> +ÂÂÂ .log_status = v4l2_ctrl_subdev_log_status,
>> +ÂÂÂ .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
>> +ÂÂÂ .unsubscribe_event = v4l2_event_subdev_unsubscribe,
>> +};
>> +
>> Â static const struct v4l2_subdev_video_ops vimc_deb_video_ops = {
>> ÂÂÂÂÂ .s_stream = vimc_deb_s_stream,
>> Â };
>> Â Â static const struct v4l2_subdev_ops vimc_deb_ops = {
>> +ÂÂÂ .core = &vimc_deb_core_ops,
>> ÂÂÂÂÂ .pad = &vimc_deb_pad_ops,
>> ÂÂÂÂÂ .video = &vimc_deb_video_ops,
>> Â };
>> @@ -384,7 +388,7 @@ static void vimc_deb_calc_rgb_sink(struct vimc_deb_device *vdeb,
>> ÂÂÂÂÂÂ * the top left corner of the mean window (considering the current
>> ÂÂÂÂÂÂ * pixel as the center)
>> ÂÂÂÂÂÂ */
>> -ÂÂÂ seek = deb_mean_win_size / 2;
>> +ÂÂÂ seek = vdeb->mean_win_size / 2;
>> Â ÂÂÂÂÂ /* Sum the values of the colors in the mean window */
>> Â @@ -474,6 +478,33 @@ static void *vimc_deb_process_frame(struct vimc_ent_device *ved,
>> Â Â }
>> Â +static inline void vimc_deb_s_mean_win_size(struct vimc_deb_device *vdeb,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ unsigned int mean_win_size)
>> +{
>> +ÂÂÂ if (vdeb->mean_win_size == mean_win_size)
>> +ÂÂÂÂÂÂÂ return;
>> +ÂÂÂ vdeb->mean_win_size = mean_win_size;
>> +}
>> +
>> +static int vimc_deb_s_ctrl(struct v4l2_ctrl *ctrl)
>> +{
>> +ÂÂÂ struct vimc_deb_device *vdeb =
>> +ÂÂÂÂÂÂÂ container_of(ctrl->handler, struct vimc_deb_device, hdl);
>> +
>> +ÂÂÂ switch (ctrl->id) {
>> +ÂÂÂ case VIMC_CID_MEAN_WIN_SIZE:
>> +ÂÂÂÂÂÂÂ vimc_deb_s_mean_win_size(vdeb, ctrl->val);
>> +ÂÂÂÂÂÂÂ break;
>> +ÂÂÂ default:
>> +ÂÂÂÂÂÂÂ return -EINVAL;
>> +ÂÂÂ }
>> +ÂÂÂ return 0;
>> +}
>> +
>> +static const struct v4l2_ctrl_ops vimc_deb_ctrl_ops = {
>> +ÂÂÂ .s_ctrl = vimc_deb_s_ctrl,
>> +};
>> +
>> Â static void vimc_deb_release(struct v4l2_subdev *sd)
>> Â {
>> ÂÂÂÂÂ struct vimc_deb_device *vdeb =
>> @@ -495,6 +526,24 @@ void vimc_deb_rm(struct vimc_device *vimc, struct vimc_ent_device *ved)
>> ÂÂÂÂÂ vimc_ent_sd_unregister(ved, &vdeb->sd);
>> Â }
>> Â +static const struct v4l2_ctrl_config vimc_deb_ctrl_class = {
>> +ÂÂÂ .flags = V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_WRITE_ONLY,
>> +ÂÂÂ .id = VIMC_CID_VIMC_CLASS,
>> +ÂÂÂ .name = "VIMC Controls",
>
> Make this VIMC Debayer Controls?

Actually, no. This control class is similar to the "Vivid Controls" in the vivid
driver: it collects all VIMC specific controls. It's only Debayer for now, but others
will be added here as well.

>
>> +ÂÂÂ .type = V4L2_CTRL_TYPE_CTRL_CLASS,
>> +};
>> +
>> +static const struct v4l2_ctrl_config vimc_deb_ctrl_mean_win_size = {
>> +ÂÂÂ .ops = &vimc_deb_ctrl_ops,
>> +ÂÂÂ .id = VIMC_CID_MEAN_WIN_SIZE,
>> +ÂÂÂ .name = "Mean Window Size",

But it would be useful to change this to:

"Debayer Mean Window Size"

Regards,

Hans

>> +ÂÂÂ .type = V4L2_CTRL_TYPE_INTEGER,
>> +ÂÂÂ .min = 1,
>> +ÂÂÂ .max = 25,
>> +ÂÂÂ .step = 2,
>> +ÂÂÂ .def = 3,
>> +};
>> +
>> Â struct vimc_ent_device *vimc_deb_add(struct vimc_device *vimc,
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ const char *vcfg_name)
>> Â {
>> @@ -507,6 +556,16 @@ struct vimc_ent_device *vimc_deb_add(struct vimc_device *vimc,
>> ÂÂÂÂÂ if (!vdeb)
>> ÂÂÂÂÂÂÂÂÂ return NULL;
>> Â +ÂÂÂ /* Create controls: */
>> +ÂÂÂ v4l2_ctrl_handler_init(&vdeb->hdl, 2);
>> +ÂÂÂ v4l2_ctrl_new_custom(&vdeb->hdl, &vimc_deb_ctrl_class, NULL);
>> +ÂÂÂ v4l2_ctrl_new_custom(&vdeb->hdl, &vimc_deb_ctrl_mean_win_size, NULL);
>> +ÂÂÂ vdeb->sd.ctrl_handler = &vdeb->hdl;
>> +ÂÂÂ if (vdeb->hdl.error) {
>> +ÂÂÂÂÂÂÂ ret = vdeb->hdl.error;
>> +ÂÂÂÂÂÂÂ goto err_free_vdeb;
>> +ÂÂÂ }
>> +
>> ÂÂÂÂÂ /* Initialize ved and sd */
>> ÂÂÂÂÂ ret = vimc_ent_sd_register(&vdeb->ved, &vdeb->sd, v4l2_dev,
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ vcfg_name,
>> @@ -514,13 +573,12 @@ struct vimc_ent_device *vimc_deb_add(struct vimc_device *vimc,
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ (const unsigned long[2]) {MEDIA_PAD_FL_SINK,
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ MEDIA_PAD_FL_SOURCE},
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ &vimc_deb_int_ops, &vimc_deb_ops);
>> -ÂÂÂ if (ret) {
>> -ÂÂÂÂÂÂÂ kfree(vdeb);
>> -ÂÂÂÂÂÂÂ return NULL;
>> -ÂÂÂ }
>> +ÂÂÂ if (ret)
>> +ÂÂÂÂÂÂÂ goto err_free_hdl;
>> Â ÂÂÂÂÂ vdeb->ved.process_frame = vimc_deb_process_frame;
>> ÂÂÂÂÂ vdeb->dev = &vimc->pdev.dev;
>> +ÂÂÂ vdeb->mean_win_size = vimc_deb_ctrl_mean_win_size.def;
>> Â ÂÂÂÂÂ /* Initialize the frame format */
>> ÂÂÂÂÂ vdeb->sink_fmt = sink_fmt_default;
>> @@ -534,4 +592,11 @@ struct vimc_ent_device *vimc_deb_add(struct vimc_device *vimc,
>> ÂÂÂÂÂ vdeb->set_rgb_src = vimc_deb_set_rgb_mbus_fmt_rgb888_1x24;
>> Â ÂÂÂÂÂ return &vdeb->ved;
>> +
>> +err_free_hdl:
>> +ÂÂÂ v4l2_ctrl_handler_free(&vdeb->hdl);
>
> Free the handle from vimc_deb_release()
>
>> +err_free_vdeb:
>> +ÂÂÂ kfree(vdeb);
>> +
>> +ÂÂÂ return NULL;
>> Â }
>>
>
> thanks,
> -- Shuah