umlaeute / v4l2loopback

v4l2-loopback device
GNU General Public License v2.0
3.69k stars 523 forks source link

v4l2loopback: Fixup bytesused field when writer sends a too large value #543

Closed jwrdegoede closed 1 year ago

jwrdegoede commented 1 year ago

Gstreamer's v4l2sink is known to submit buffers with bytesused set to the length of the buffer instead of the size of the actual image-data within the buffer which is typically smaller due to buffer sizes being rounded op to a page-size:

https://gitlab.freedesktop.org/gstreamer/gstreamer/-/issues/2532

Despite this being a long standing issue and their being 2 merge-reqs:

https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/3713 https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/4527

to try and fix this it looks like this is not going to get fixed in gst's v4l2sink anytime soon and even if once it is fixed many users will likely be using an older v4l2sink which still has this bug.

These buffers with a too large bytes used value are causing issues with various apps which reject these buffers when reading from the v4l2loopback device, such as e.g. ffmpeg and firefox.

Add a pix_format_has_valid_sizeimage flag which gets set from vidioc_s_fmt_out() if dev->pix_format.sizeimage is known to have just been set to a valid, fixed size (so this e.g. won't be set for MJPG).

And then fix this issue by making vidioc_qbuf() truncate V4L2_BUF_TYPE_VIDEO_OUTPUT buffer's bytes_used value to dev->pix_format.sizeimage when this flag is set.

Closes #190 Closes #448 Obsoletes #435

jwrdegoede commented 1 year ago

p.s.

Thank you for merging this quickly and thank you for the dprintkrw() fix you added.