[PATCH v2 2/2] media: iris: Fix fps calculation

Ricardo Ribalda posted 2 patches 1 month, 3 weeks ago
There is a newer version of this series
[PATCH v2 2/2] media: iris: Fix fps calculation
Posted by Ricardo Ribalda 1 month, 3 weeks ago
iris_venc_s_param() uses do_div to divide two 64 bits operators, this is
wrong. Luckily for us, both of the operators fit in 32 bits, so we can use
a normal division.

Now that we are at it, mark the fps smaller than 1 as invalid, the code
does not seem to handle them properly.

The following cocci warning is fixed with this patch:
./platform/qcom/iris/iris_venc.c:378:1-7: WARNING: do_div() does a 64-by-32 division, please consider using div64_u64 instead

Fixes: 4ff586ff28e3 ("media: iris: Add support for G/S_PARM for encoder video device")
Reviewed-by: Dikshita Agarwal <dikshita.agarwal@oss.qualcomm.com>
Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
---
 drivers/media/platform/qcom/iris/iris_venc.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)

diff --git a/drivers/media/platform/qcom/iris/iris_venc.c b/drivers/media/platform/qcom/iris/iris_venc.c
index 5830eba93c68b27fa9db87bac63a691eaca338d2..3b941aeb55a2f498898a27a5f8cb58cdb26fdfed 100644
--- a/drivers/media/platform/qcom/iris/iris_venc.c
+++ b/drivers/media/platform/qcom/iris/iris_venc.c
@@ -382,8 +382,7 @@ int iris_venc_s_param(struct iris_inst *inst, struct v4l2_streamparm *s_parm)
 	struct v4l2_fract *timeperframe = NULL;
 	u32 default_rate = DEFAULT_FPS;
 	bool is_frame_rate = false;
-	u64 us_per_frame, fps;
-	u32 max_rate;
+	u32 fps, max_rate;
 
 	int ret = 0;
 
@@ -405,23 +404,19 @@ int iris_venc_s_param(struct iris_inst *inst, struct v4l2_streamparm *s_parm)
 			timeperframe->denominator = default_rate;
 	}
 
-	us_per_frame = timeperframe->numerator * (u64)USEC_PER_SEC;
-	do_div(us_per_frame, timeperframe->denominator);
-
-	if (!us_per_frame)
+	fps = timeperframe->numerator / timeperframe->denominator;
+	if (!fps)
 		return -EINVAL;
 
-	fps = (u64)USEC_PER_SEC;
-	do_div(fps, us_per_frame);
 	if (fps > max_rate) {
 		ret = -ENOMEM;
 		goto reset_rate;
 	}
 
 	if (is_frame_rate)
-		inst->frame_rate = (u32)fps;
+		inst->frame_rate = fps;
 	else
-		inst->operating_rate = (u32)fps;
+		inst->operating_rate = fps;
 
 	if ((s_parm->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE && vb2_is_streaming(src_q)) ||
 	    (s_parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE && vb2_is_streaming(dst_q))) {

-- 
2.52.0.239.gd5f0c6e74e-goog
Re: [PATCH v2 2/2] media: iris: Fix fps calculation
Posted by Dikshita Agarwal 1 month, 2 weeks ago

On 12/16/2025 9:12 PM, Ricardo Ribalda wrote:
> iris_venc_s_param() uses do_div to divide two 64 bits operators, this is
> wrong. Luckily for us, both of the operators fit in 32 bits, so we can use
> a normal division.
> 
> Now that we are at it, mark the fps smaller than 1 as invalid, the code
> does not seem to handle them properly.
> 
> The following cocci warning is fixed with this patch:
> ./platform/qcom/iris/iris_venc.c:378:1-7: WARNING: do_div() does a 64-by-32 division, please consider using div64_u64 instead
> 
> Fixes: 4ff586ff28e3 ("media: iris: Add support for G/S_PARM for encoder video device")
> Reviewed-by: Dikshita Agarwal <dikshita.agarwal@oss.qualcomm.com>
> Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
> ---
>  drivers/media/platform/qcom/iris/iris_venc.c | 15 +++++----------
>  1 file changed, 5 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/media/platform/qcom/iris/iris_venc.c b/drivers/media/platform/qcom/iris/iris_venc.c
> index 5830eba93c68b27fa9db87bac63a691eaca338d2..3b941aeb55a2f498898a27a5f8cb58cdb26fdfed 100644
> --- a/drivers/media/platform/qcom/iris/iris_venc.c
> +++ b/drivers/media/platform/qcom/iris/iris_venc.c
> @@ -382,8 +382,7 @@ int iris_venc_s_param(struct iris_inst *inst, struct v4l2_streamparm *s_parm)
>  	struct v4l2_fract *timeperframe = NULL;
>  	u32 default_rate = DEFAULT_FPS;
>  	bool is_frame_rate = false;
> -	u64 us_per_frame, fps;
> -	u32 max_rate;
> +	u32 fps, max_rate;
>  
>  	int ret = 0;
>  
> @@ -405,23 +404,19 @@ int iris_venc_s_param(struct iris_inst *inst, struct v4l2_streamparm *s_parm)
>  			timeperframe->denominator = default_rate;
>  	}
>  
> -	us_per_frame = timeperframe->numerator * (u64)USEC_PER_SEC;
> -	do_div(us_per_frame, timeperframe->denominator);
> -
> -	if (!us_per_frame)
> +	fps = timeperframe->numerator / timeperframe->denominator;

This is wrong, should be timeperframe->denominator/timeperframe->numerator
Also I think it would be better to clamp the fps?

Thanks,
Dikshita

> +	if (!fps)
>  		return -EINVAL;
>  
> -	fps = (u64)USEC_PER_SEC;
> -	do_div(fps, us_per_frame);
>  	if (fps > max_rate) {
>  		ret = -ENOMEM;
>  		goto reset_rate;
>  	}
>  
>  	if (is_frame_rate)
> -		inst->frame_rate = (u32)fps;
> +		inst->frame_rate = fps;
>  	else
> -		inst->operating_rate = (u32)fps;
> +		inst->operating_rate = fps;
>  
>  	if ((s_parm->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE && vb2_is_streaming(src_q)) ||
>  	    (s_parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE && vb2_is_streaming(dst_q))) {
>
Re: [PATCH v2 2/2] media: iris: Fix fps calculation
Posted by Ricardo Ribalda 1 month, 2 weeks ago
On Tue, 23 Dec 2025 at 10:39, Dikshita Agarwal
<dikshita.agarwal@oss.qualcomm.com> wrote:
>
>
>
> On 12/16/2025 9:12 PM, Ricardo Ribalda wrote:
> > iris_venc_s_param() uses do_div to divide two 64 bits operators, this is
> > wrong. Luckily for us, both of the operators fit in 32 bits, so we can use
> > a normal division.
> >
> > Now that we are at it, mark the fps smaller than 1 as invalid, the code
> > does not seem to handle them properly.
> >
> > The following cocci warning is fixed with this patch:
> > ./platform/qcom/iris/iris_venc.c:378:1-7: WARNING: do_div() does a 64-by-32 division, please consider using div64_u64 instead
> >
> > Fixes: 4ff586ff28e3 ("media: iris: Add support for G/S_PARM for encoder video device")
> > Reviewed-by: Dikshita Agarwal <dikshita.agarwal@oss.qualcomm.com>
> > Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
> > ---
> >  drivers/media/platform/qcom/iris/iris_venc.c | 15 +++++----------
> >  1 file changed, 5 insertions(+), 10 deletions(-)
> >
> > diff --git a/drivers/media/platform/qcom/iris/iris_venc.c b/drivers/media/platform/qcom/iris/iris_venc.c
> > index 5830eba93c68b27fa9db87bac63a691eaca338d2..3b941aeb55a2f498898a27a5f8cb58cdb26fdfed 100644
> > --- a/drivers/media/platform/qcom/iris/iris_venc.c
> > +++ b/drivers/media/platform/qcom/iris/iris_venc.c
> > @@ -382,8 +382,7 @@ int iris_venc_s_param(struct iris_inst *inst, struct v4l2_streamparm *s_parm)
> >       struct v4l2_fract *timeperframe = NULL;
> >       u32 default_rate = DEFAULT_FPS;
> >       bool is_frame_rate = false;
> > -     u64 us_per_frame, fps;
> > -     u32 max_rate;
> > +     u32 fps, max_rate;
> >
> >       int ret = 0;
> >
> > @@ -405,23 +404,19 @@ int iris_venc_s_param(struct iris_inst *inst, struct v4l2_streamparm *s_parm)
> >                       timeperframe->denominator = default_rate;
> >       }
> >
> > -     us_per_frame = timeperframe->numerator * (u64)USEC_PER_SEC;
> > -     do_div(us_per_frame, timeperframe->denominator);
> > -
> > -     if (!us_per_frame)
> > +     fps = timeperframe->numerator / timeperframe->denominator;
>
> This is wrong, should be timeperframe->denominator/timeperframe->numerator

Ups, sorry about that :S

> Also I think it would be better to clamp the fps?

Do you mean clamp instead of returning -ENOMEM? I have no strong
opinion about that, but I believe that should be a follow up patch.

>
> Thanks,
> Dikshita
>
> > +     if (!fps)
> >               return -EINVAL;
> >
> > -     fps = (u64)USEC_PER_SEC;
> > -     do_div(fps, us_per_frame);
> >       if (fps > max_rate) {
> >               ret = -ENOMEM;
> >               goto reset_rate;
> >       }
> >
> >       if (is_frame_rate)
> > -             inst->frame_rate = (u32)fps;
> > +             inst->frame_rate = fps;
> >       else
> > -             inst->operating_rate = (u32)fps;
> > +             inst->operating_rate = fps;
> >
> >       if ((s_parm->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE && vb2_is_streaming(src_q)) ||
> >           (s_parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE && vb2_is_streaming(dst_q))) {
> >



-- 
Ricardo Ribalda