diff mbox series

[v3,1/3] media: rkvdec: Fix .buf_prepare

Message ID 20200505134110.3435-2-ezequiel@collabora.com (mailing list archive)
State New, archived
Headers show
Series media: rkvdec: Add a VP9 backend | expand

Commit Message

Ezequiel Garcia May 5, 2020, 1:41 p.m. UTC
The driver should only set the payload on .buf_prepare
if the buffer is CAPTURE type, or if an OUTPUT buffer
has a zeroed payload.

Fix it.

Fixes: cd33c830448ba ("media: rkvdec: Add the rkvdec driver")
Signed-off-by: Ezequiel Garcia <ezequiel@collabora.com>
---
 drivers/staging/media/rkvdec/rkvdec.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

Comments

Tomasz Figa May 5, 2020, 1:56 p.m. UTC | #1
Hi Ezequiel,

On Tue, May 5, 2020 at 3:41 PM Ezequiel Garcia <ezequiel@collabora.com> wrote:
>
> The driver should only set the payload on .buf_prepare
> if the buffer is CAPTURE type, or if an OUTPUT buffer
> has a zeroed payload.

Thanks for the patch. Just one question below.

Where does the requirement to set OUTPUT buffer bytesused to sizeimage
if the original bytesused is 0 come from?

Best regards,
Tomasz

>
> Fix it.
>
> Fixes: cd33c830448ba ("media: rkvdec: Add the rkvdec driver")
> Signed-off-by: Ezequiel Garcia <ezequiel@collabora.com>
> ---
>  drivers/staging/media/rkvdec/rkvdec.c | 10 +++++++++-
>  1 file changed, 9 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/staging/media/rkvdec/rkvdec.c b/drivers/staging/media/rkvdec/rkvdec.c
> index 225eeca73356..4df2a248ab96 100644
> --- a/drivers/staging/media/rkvdec/rkvdec.c
> +++ b/drivers/staging/media/rkvdec/rkvdec.c
> @@ -456,7 +456,15 @@ static int rkvdec_buf_prepare(struct vb2_buffer *vb)
>                 if (vb2_plane_size(vb, i) < sizeimage)
>                         return -EINVAL;
>         }
> -       vb2_set_plane_payload(vb, 0, f->fmt.pix_mp.plane_fmt[0].sizeimage);
> +
> +       /*
> +        * Buffer's bytesused is written by the driver for CAPTURE buffers,
> +        * or if the application passed zero bytesused on an OUTPUT buffer.
> +        */
> +       if (!V4L2_TYPE_IS_OUTPUT(vq->type) ||
> +           (V4L2_TYPE_IS_OUTPUT(vq->type) && !vb2_get_plane_payload(vb, 0)))
> +               vb2_set_plane_payload(vb, 0,
> +                                     f->fmt.pix_mp.plane_fmt[0].sizeimage);
>         return 0;
>  }
>
> --
> 2.26.0.rc2
>
Ezequiel Garcia May 5, 2020, 1:59 p.m. UTC | #2
On Tue, 2020-05-05 at 15:56 +0200, Tomasz Figa wrote:
> Hi Ezequiel,
> 
> On Tue, May 5, 2020 at 3:41 PM Ezequiel Garcia <ezequiel@collabora.com> wrote:
> > The driver should only set the payload on .buf_prepare
> > if the buffer is CAPTURE type, or if an OUTPUT buffer
> > has a zeroed payload.
> 
> Thanks for the patch. Just one question below.
> 
> Where does the requirement to set OUTPUT buffer bytesused to sizeimage
> if the original bytesused is 0 come from?
> 

If I'm reading english correctly, it's here:

https://www.kernel.org/doc/html/latest/media/uapi/v4l/buffer.html

"""
The number of bytes occupied by the data in the buffer. It depends on the negotiated data format and may change with each buffer for compressed
variable size data like JPEG images. Drivers must set this field when type refers to a capture stream, applications when it refers to an output
stream. If the application sets this to 0 for an output stream, then bytesused will be set to the size of the buffer (see the length field of this
struct) by the driver. For multiplanar formats this field is ignored and the planes pointer is used instead.
"""

Thanks!
Ezequiel

> Best regards,
> Tomasz
> 
> > Fix it.
> > 
> > Fixes: cd33c830448ba ("media: rkvdec: Add the rkvdec driver")
> > Signed-off-by: Ezequiel Garcia <ezequiel@collabora.com>
> > ---
> >  drivers/staging/media/rkvdec/rkvdec.c | 10 +++++++++-
> >  1 file changed, 9 insertions(+), 1 deletion(-)
> > 
> > diff --git a/drivers/staging/media/rkvdec/rkvdec.c b/drivers/staging/media/rkvdec/rkvdec.c
> > index 225eeca73356..4df2a248ab96 100644
> > --- a/drivers/staging/media/rkvdec/rkvdec.c
> > +++ b/drivers/staging/media/rkvdec/rkvdec.c
> > @@ -456,7 +456,15 @@ static int rkvdec_buf_prepare(struct vb2_buffer *vb)
> >                 if (vb2_plane_size(vb, i) < sizeimage)
> >                         return -EINVAL;
> >         }
> > -       vb2_set_plane_payload(vb, 0, f->fmt.pix_mp.plane_fmt[0].sizeimage);
> > +
> > +       /*
> > +        * Buffer's bytesused is written by the driver for CAPTURE buffers,
> > +        * or if the application passed zero bytesused on an OUTPUT buffer.
> > +        */
> > +       if (!V4L2_TYPE_IS_OUTPUT(vq->type) ||
> > +           (V4L2_TYPE_IS_OUTPUT(vq->type) && !vb2_get_plane_payload(vb, 0)))
> > +               vb2_set_plane_payload(vb, 0,
> > +                                     f->fmt.pix_mp.plane_fmt[0].sizeimage);
> >         return 0;
> >  }
> > 
> > --
> > 2.26.0.rc2
> >
Tomasz Figa May 5, 2020, 2:05 p.m. UTC | #3
On Tue, May 5, 2020 at 3:59 PM Ezequiel Garcia <ezequiel@collabora.com> wrote:
>
> On Tue, 2020-05-05 at 15:56 +0200, Tomasz Figa wrote:
> > Hi Ezequiel,
> >
> > On Tue, May 5, 2020 at 3:41 PM Ezequiel Garcia <ezequiel@collabora.com> wrote:
> > > The driver should only set the payload on .buf_prepare
> > > if the buffer is CAPTURE type, or if an OUTPUT buffer
> > > has a zeroed payload.
> >
> > Thanks for the patch. Just one question below.
> >
> > Where does the requirement to set OUTPUT buffer bytesused to sizeimage
> > if the original bytesused is 0 come from?
> >
>
> If I'm reading english correctly, it's here:
>
> https://www.kernel.org/doc/html/latest/media/uapi/v4l/buffer.html
>
> """
> The number of bytes occupied by the data in the buffer. It depends on the negotiated data format and may change with each buffer for compressed
> variable size data like JPEG images. Drivers must set this field when type refers to a capture stream, applications when it refers to an output
> stream. If the application sets this to 0 for an output stream, then bytesused will be set to the size of the buffer (see the length field of this
> struct) by the driver. For multiplanar formats this field is ignored and the planes pointer is used instead.
> """

Okay, thanks. I wonder if this shouldn't be handled by the core,
though. Especially given that the document refers to the length field
specifically and not the sizeimage set in the format.

Best regards,
Tomasz
Ezequiel Garcia May 5, 2020, 2:27 p.m. UTC | #4
On Tue, 2020-05-05 at 16:05 +0200, Tomasz Figa wrote:
> On Tue, May 5, 2020 at 3:59 PM Ezequiel Garcia <ezequiel@collabora.com> wrote:
> > On Tue, 2020-05-05 at 15:56 +0200, Tomasz Figa wrote:
> > > Hi Ezequiel,
> > > 
> > > On Tue, May 5, 2020 at 3:41 PM Ezequiel Garcia <ezequiel@collabora.com> wrote:
> > > > The driver should only set the payload on .buf_prepare
> > > > if the buffer is CAPTURE type, or if an OUTPUT buffer
> > > > has a zeroed payload.
> > > 
> > > Thanks for the patch. Just one question below.
> > > 
> > > Where does the requirement to set OUTPUT buffer bytesused to sizeimage
> > > if the original bytesused is 0 come from?
> > > 
> > 
> > If I'm reading english correctly, it's here:
> > 
> > https://www.kernel.org/doc/html/latest/media/uapi/v4l/buffer.html
> > 
> > """
> > The number of bytes occupied by the data in the buffer. It depends on the negotiated data format and may change with each buffer for compressed
> > variable size data like JPEG images. Drivers must set this field when type refers to a capture stream, applications when it refers to an output
> > stream. If the application sets this to 0 for an output stream, then bytesused will be set to the size of the buffer (see the length field of this
> > struct) by the driver. For multiplanar formats this field is ignored and the planes pointer is used instead.
> > """
> 
> Okay, thanks. I wonder if this shouldn't be handled by the core,
> though. Especially given that the document refers to the length field
> specifically and not the sizeimage set in the format.
> 

Yes, either core or helper, this definitely calls for a generic solution.

Ezequiel
Nicolas Dufresne May 13, 2020, 11:38 p.m. UTC | #5
Le mardi 05 mai 2020 à 11:27 -0300, Ezequiel Garcia a écrit :
> On Tue, 2020-05-05 at 16:05 +0200, Tomasz Figa wrote:
> > On Tue, May 5, 2020 at 3:59 PM Ezequiel Garcia <ezequiel@collabora.com> wrote:
> > > On Tue, 2020-05-05 at 15:56 +0200, Tomasz Figa wrote:
> > > > Hi Ezequiel,
> > > > 
> > > > On Tue, May 5, 2020 at 3:41 PM Ezequiel Garcia <ezequiel@collabora.com> wrote:
> > > > > The driver should only set the payload on .buf_prepare
> > > > > if the buffer is CAPTURE type, or if an OUTPUT buffer
> > > > > has a zeroed payload.
> > > > 
> > > > Thanks for the patch. Just one question below.
> > > > 
> > > > Where does the requirement to set OUTPUT buffer bytesused to sizeimage
> > > > if the original bytesused is 0 come from?
> > > > 
> > > 
> > > If I'm reading english correctly, it's here:
> > > 
> > > https://www.kernel.org/doc/html/latest/media/uapi/v4l/buffer.html
> > > 
> > > """
> > > The number of bytes occupied by the data in the buffer. It depends on the negotiated data format and may change with each buffer for compressed
> > > variable size data like JPEG images. Drivers must set this field when type refers to a capture stream, applications when it refers to an output
> > > stream. If the application sets this to 0 for an output stream, then bytesused will be set to the size of the buffer (see the length field of this
> > > struct) by the driver. For multiplanar formats this field is ignored and the planes pointer is used instead.
> > > """
> > 
> > Okay, thanks. I wonder if this shouldn't be handled by the core,
> > though. Especially given that the document refers to the length field
> > specifically and not the sizeimage set in the format.
> > 
> 
> Yes, either core or helper, this definitely calls for a generic solution.

For the context, this is for backward compatibility. I'm not certain it
make sense for new driver interface like RKVDEC. Specially that if the
user did pass an empty buffer by accident, this will push garbage into
the driver. That being said, it seems to match the spec.

> 
> Ezequiel
>
Nicolas Dufresne May 14, 2020, 2:50 p.m. UTC | #6
Le mardi 05 mai 2020 à 10:41 -0300, Ezequiel Garcia a écrit :
> The driver should only set the payload on .buf_prepare
> if the buffer is CAPTURE type, or if an OUTPUT buffer
> has a zeroed payload.
> 
> Fix it.
> 
> Fixes: cd33c830448ba ("media: rkvdec: Add the rkvdec driver")
> Signed-off-by: Ezequiel Garcia <ezequiel@collabora.com>
> ---
>  drivers/staging/media/rkvdec/rkvdec.c | 10 +++++++++-
>  1 file changed, 9 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/staging/media/rkvdec/rkvdec.c
> b/drivers/staging/media/rkvdec/rkvdec.c
> index 225eeca73356..4df2a248ab96 100644
> --- a/drivers/staging/media/rkvdec/rkvdec.c
> +++ b/drivers/staging/media/rkvdec/rkvdec.c
> @@ -456,7 +456,15 @@ static int rkvdec_buf_prepare(struct vb2_buffer *vb)
>  		if (vb2_plane_size(vb, i) < sizeimage)
>  			return -EINVAL;
>  	}
> -	vb2_set_plane_payload(vb, 0, f->fmt.pix_mp.plane_fmt[0].sizeimage);
> +
> +	/*
> +	 * Buffer's bytesused is written by the driver for CAPTURE buffers,
> +	 * or if the application passed zero bytesused on an OUTPUT buffer.
> +	 */
> +	if (!V4L2_TYPE_IS_OUTPUT(vq->type) ||
> +	    (V4L2_TYPE_IS_OUTPUT(vq->type) && !vb2_get_plane_payload(vb, 0)))
> +		vb2_set_plane_payload(vb, 0,
> +				      f->fmt.pix_mp.plane_fmt[0].sizeimage);

I believe the spec lacks a bit of a clarification. Converting from 0 to
sizeimage should only be allowed for RAW images. So I would like to suggest to
change this fix into:

-	vb2_set_plane_payload(vb, 0, f->fmt.pix_mp.plane_fmt[0].sizeimage);
+
+	/* Buffer's bytesused is written by the driver for CAPTURE buffers */
+	if (!V4L2_TYPE_IS_OUTPUT(vq->type))
+		vb2_set_plane_payload(vb, 0,
+				      f->fmt.pix_mp.plane_fmt[0].sizeimage);

And then we can fix the spec accordingly. Note that neighter FFMPEG or GStreamer
will pass empty (zero sized) payload at the moment, and if it did, it would be a
bug, and the payload should instead be ignored.

>  	return 0;
>  }
>  
> -- 
> 2.26.0.rc2
> 
>
diff mbox series

Patch

diff --git a/drivers/staging/media/rkvdec/rkvdec.c b/drivers/staging/media/rkvdec/rkvdec.c
index 225eeca73356..4df2a248ab96 100644
--- a/drivers/staging/media/rkvdec/rkvdec.c
+++ b/drivers/staging/media/rkvdec/rkvdec.c
@@ -456,7 +456,15 @@  static int rkvdec_buf_prepare(struct vb2_buffer *vb)
 		if (vb2_plane_size(vb, i) < sizeimage)
 			return -EINVAL;
 	}
-	vb2_set_plane_payload(vb, 0, f->fmt.pix_mp.plane_fmt[0].sizeimage);
+
+	/*
+	 * Buffer's bytesused is written by the driver for CAPTURE buffers,
+	 * or if the application passed zero bytesused on an OUTPUT buffer.
+	 */
+	if (!V4L2_TYPE_IS_OUTPUT(vq->type) ||
+	    (V4L2_TYPE_IS_OUTPUT(vq->type) && !vb2_get_plane_payload(vb, 0)))
+		vb2_set_plane_payload(vb, 0,
+				      f->fmt.pix_mp.plane_fmt[0].sizeimage);
 	return 0;
 }