Fix Sonix Technology MJPEG streams
Subsystems (2)
cover Fix Sonix Technology MJPEG streams
Some cameras, such as the Sonix Technology Co. 292A, allow multiple streams to be sent over the same USB device. It was found during testing that occasionally, when one H.264 stream sends a new keyframe during an MJPEG stream, a URB buffer would be lost, and a frame lost in the resulting capture.
This was because some EOF markers in URB buffers are lost. This particular use case is not covered by the uvc_video driver, which causes two consecutive buffers to be erroneously considered to be as part of the same buffer, and lead to the whole erroneous buffer to be dropped. This can be seen when using the current driver, as frames occasionally are double the expected size, containing two images. Furthermore, the interval between the frames was double that of what it should be (~66ms as opposed to the usual ~33ms). Upon further investigation, it was found that the packets within the erroneous buffer were being dropped.
The changes in this series supply a new quirk for the UVC Driver, which cause the buffers of MJPEG streams to be closed when a new JPEG SOI marker is discovered, and be treated as a new buffer. This works around the missing EOF marker and separates the buffers.
Tested on v6.12-rc3
Isaac Scott (3):
media: uvcvideo: Introduce header length
media: uvcvideo: Add new quirk definition for the Sonix Technology Co.
292a camera
media: uvcvideo: Implement dual stream quirk to fix loss of usb
packets
drivers/media/usb/uvc/uvc_driver.c | 9 +++++++++ drivers/media/usb/uvc/uvc_video.c | 27 ++++++++++++++++++++++++++- drivers/media/usb/uvc/uvcvideo.h | 1 + 3 files changed, 36 insertions(+), 1 deletion(-)
Signature · 1 line
2.43.0
PATCH 1/3 media: uvcvideo: Introduce header length
The uvc_video_decode_start function returns the first byte of the header, which is in fact the length of the header. Improve readability by using an appropriately named variable.
--- drivers/media/usb/uvc/uvc_video.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/media/usb/uvc/uvc_video.c +2 -1
@@ -1117,6 +1117,7 @@ static int uvc_video_decode_start(struct uvc_streaming *stream, struct uvc_buffer *buf, const u8 *data, int len) { u8 fid; +u8 header_len = data[0 ];/* * Sanity checks:
@@ -1212,7 +1213,7 @@ static int uvc_video_decode_start(struct uvc_streaming *stream, stream->last_fid = fid; -return data[0 ]; +return header_len; } static inline enum dma_data_direction uvc_stream_dir(
Signature · 1 line
2.43.0
Laurent Pinchart
maintainerRe: [PATCH 1/3] media: uvcvideo: Introduce header length
Hi Isaac,
Thank you for the patch.
Quoted 14 lines · Isaac Scott
The
uvc_video_decode_startfunction returns the first byte of the header, which is in fact the length of the header. Improve readability by using an appropriately named variable.Signed-off-by Isaac Scott <[email protected]>--- drivers/media/usb/uvc/uvc_video.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)drivers/media/usb/uvc/uvc_video.c +0
-0
drivers/media/usb/uvc/uvc_video.c static int uvc_video_decode_start(struct uvc_streaming *stream, · line 1120
Quoted 4 lines
drivers/media/usb/uvc/uvc_video.c +1
-0@@ -1117,6 +1117,7 @@ static int uvc_video_decode_start(struct uvc_streaming *stream, struct uvc_buffer *buf, const u8 *data, int len) { u8 fid; +u8 header_len = data[0 ];
Is there a guarantee at this point, before the sanity checks below, that len is not zero, that is, that it's safe to read data[0] ?
Quoted 12 lines
drivers/media/usb/uvc/uvc_video.c +1
-1/* * Sanity checks: @@ -1212,7 +1213,7 @@ static int uvc_video_decode_start(struct uvc_streaming *stream, stream->last_fid = fid; -return data[0 ]; +return header_len; } static inline enum dma_data_direction uvc_stream_dir(
Signature · 3 lines
Regards, Laurent Pinchart
Re: [PATCH 1/3] media: uvcvideo: Introduce header length
Hi Isaac
nit: If you have to redo the set I would recommend to squash this patch to the next one.
Quoted 46 lines · Laurent Pinchart
Hi Isaac,
Thank you for the patch.
Quoted 14 lines · Isaac Scott
The
uvc_video_decode_startfunction returns the first byte of the header, which is in fact the length of the header. Improve readability by using an appropriately named variable.Signed-off-by Isaac Scott <[email protected]>--- drivers/media/usb/uvc/uvc_video.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)drivers/media/usb/uvc/uvc_video.c +0
-0drivers/media/usb/uvc/uvc_video.c static int uvc_video_decode_start(struct uvc_streaming *stream, · line 1120
Quoted 4 lines
drivers/media/usb/uvc/uvc_video.c +1
-0@@ -1117,6 +1117,7 @@ static int uvc_video_decode_start(struct uvc_streaming *stream, struct uvc_buffer *buf, const u8 *data, int len) { u8 fid; +u8 header_len = data[0 ];Is there a guarantee at this point, before the sanity checks below, that len is not zero, that is, that it's safe to read data[0] ?
drivers/media/usb/uvc/uvc_video.c static int uvc_video_decode_start(struct uvc_streaming *stream, · line 1219
Quoted 12 lines
drivers/media/usb/uvc/uvc_video.c +1
-1/* * Sanity checks: @@ -1212,7 +1213,7 @@ static int uvc_video_decode_start(struct uvc_streaming *stream, stream->last_fid = fid; -return data[0 ]; +return header_len; } static inline enum dma_data_direction uvc_stream_dir(--
Regards,Laurent Pinchart
Signature · 1 line
Ricardo Ribalda
PATCH 2/3 media: uvcvideo: Add new quirk definition for the Sonix Technology Co. 292a camera
In reply to Isaac ScottAdd the definition of a new quirk that supports the Sonix Technology Co. 292A camera, which uses the AR0330 sensor. The camera supports the output of two simultaneous streams, which need to be handled appropriately by the driver.
--- drivers/media/usb/uvc/uvc_driver.c | 9 +++++++++ drivers/media/usb/uvc/uvcvideo.h | 1 + 2 files changed, 10 insertions(+)
drivers/media/usb/uvc/uvc_driver.c +9 -0
@@ -2752,6 +2752,15 @@ static const struct usb_device_id uvc_ids[] = { .bInterfaceSubClass = 1, .bInterfaceProtocol = 0, .driver_info = (kernel_ulong_t)&uvc_quirk_probe_minmax }, +/* Sonix Technology Co. Ltd. - 292A IPC AR0330 */ + { .match_flags = USB_DEVICE_ID_MATCH_DEVICE + | USB_DEVICE_ID_MATCH_INT_INFO, + .idVendor = 0x0c45, + .idProduct = 0x6366, + .bInterfaceClass = USB_CLASS_VIDEO, + .bInterfaceSubClass = 1, + .bInterfaceProtocol = 0, + .driver_info = UVC_INFO_QUIRK(UVC_QUIRK_MJPEG_NO_EOF) }, /* MT6227 */ { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_INT_INFO,
drivers/media/usb/uvc/uvcvideo.h +1 -0
@@ -76,6 +76,7 @@#define UVC_QUIRK_NO_RESET_RESUME 0x00004000#define UVC_QUIRK_DISABLE_AUTOSUSPEND 0x00008000#define UVC_QUIRK_INVALID_DEVICE_SOF 0x00010000 +#define UVC_QUIRK_MJPEG_NO_EOF 0x00020000/* Format flags */ #define UVC_FMT_FLAG_COMPRESSED 0x00000001
Signature · 1 line
2.43.0
Re: [PATCH 2/3] media: uvcvideo: Add new quirk definition for the Sonix Technology Co. 292a camera
HI
I think it makes more sense to swap the order to patches 2 and 3 and move the
#define UVC_QUIRK_MJPEG_NO_EOF 0x00020000
to media: uvcvideo: Implement dual stream quirk to fix loss of usb packets
Also when we send quirks we send to the ML the output of lsusb -v 0c45:6366
Quoted 48 lines · Isaac Scott
Add the definition of a new quirk that supports the Sonix Technology Co. 292A camera, which uses the AR0330 sensor. The camera supports the output of two simultaneous streams, which need to be handled appropriately by the driver.
Signed-off-by Isaac Scott <[email protected]>--- drivers/media/usb/uvc/uvc_driver.c | 9 +++++++++ drivers/media/usb/uvc/uvcvideo.h | 1 + 2 files changed, 10 insertions(+)drivers/media/usb/uvc/uvc_driver.c +9
-0@@ -2752,6 +2752,15 @@ static const struct usb_device_id uvc_ids[] = { .bInterfaceSubClass = 1, .bInterfaceProtocol = 0, .driver_info = (kernel_ulong_t)&uvc_quirk_probe_minmax }, +/* Sonix Technology Co. Ltd. - 292A IPC AR0330 */ + { .match_flags = USB_DEVICE_ID_MATCH_DEVICE + | USB_DEVICE_ID_MATCH_INT_INFO, + .idVendor = 0x0c45, + .idProduct = 0x6366, + .bInterfaceClass = USB_CLASS_VIDEO, + .bInterfaceSubClass = 1, + .bInterfaceProtocol = 0, + .driver_info = UVC_INFO_QUIRK(UVC_QUIRK_MJPEG_NO_EOF) }, /* MT6227 */ { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_INT_INFO,drivers/media/usb/uvc/uvcvideo.h +1
-1@@ -76,6 +76,7 @@#define UVC_QUIRK_NO_RESET_RESUME 0x00004000#define UVC_QUIRK_DISABLE_AUTOSUSPEND 0x00008000#define UVC_QUIRK_INVALID_DEVICE_SOF 0x00010000 +#define UVC_QUIRK_MJPEG_NO_EOF 0x00020000/* Format flags */ #define UVC_FMT_FLAG_COMPRESSED 0x00000001 --2.43.0
Signature · 1 line
Ricardo Ribalda
PATCH 3/3 media: uvcvideo: Implement dual stream quirk to fix loss of usb packets
In reply to Isaac ScottSome cameras, such as the Sonix Technology Co. 292A exhibit issues when running two parallel streams, causing USB packets to be dropped when an H.264 stream posts a keyframe while an MJPEG stream is running simultaneously. This occasionally causes the driver to erroneously output two consecutive JPEG images as a single frame.
To fix this, we inspect the buffer, and trigger a new frame when we find an SOI, inverting the FID to make sure no frames are erroneously dropped.
--- drivers/media/usb/uvc/uvc_video.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
drivers/media/usb/uvc/uvc_video.c +24 -0
@@ -1211,6 +1211,30 @@ static int uvc_video_decode_start(struct uvc_streaming *stream,return -EAGAIN ; } +/* +* Some cameras, such as the Sonix Technology Co. 292A exhibit issues +* when running two parallel streams, causing USB packets to be dropped +* when an H.264 stream posts a keyframe while an MJPEG stream is +* running simultaneously. This occasionally causes the driver to +* erroneously output two consecutive JPEG images as a single frame. +* +* Check the buffer for a new SOI on JPEG streams and complete the +* preceding buffer using EAGAIN, and invert the FID to make sure the +* erroneous frame is not dropped. +*/ +if ((stream->dev->quirks &UVC_QUIRK_MJPEG_NO_EOF ) && + (stream->cur_format->fcc ==V4L2_PIX_FMT_MJPEG || + stream->cur_format->fcc ==V4L2_PIX_FMT_JPEG )) { +const u8 *packet = data + header_len; + +if ((packet[0 ] ==0xff && packet[1 ] ==0xd8 ) && buf->bytesused !=0 ) { + buf->state =UVC_BUF_STATE_READY ; + buf->error =1 ; + stream->last_fid ^=UVC_STREAM_FID ; +return -EAGAIN ; + } + } + stream->last_fid = fid;return header_len;
Signature · 1 line
2.43.0
Re: [PATCH 3/3] media: uvcvideo: Implement dual stream quirk to fix loss of usb packets
Quoted 29 lines · Isaac Scott
Some cameras, such as the Sonix Technology Co. 292A exhibit issues when running two parallel streams, causing USB packets to be dropped when an H.264 stream posts a keyframe while an MJPEG stream is running simultaneously. This occasionally causes the driver to erroneously output two consecutive JPEG images as a single frame.
To fix this, we inspect the buffer, and trigger a new frame when we find an SOI, inverting the FID to make sure no frames are erroneously dropped.
Signed-off-by Isaac Scott <[email protected]>--- drivers/media/usb/uvc/uvc_video.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)drivers/media/usb/uvc/uvc_video.c +5
-0@@ -1211,6 +1211,30 @@ static int uvc_video_decode_start(struct uvc_streaming *stream,return -EAGAIN ; } +/* +* Some cameras, such as the Sonix Technology Co. 292A exhibit issues +* when running two parallel streams, causing USB packets to be dropped +* when an H.264 stream posts a keyframe while an MJPEG stream is +* running simultaneously. This occasionally causes the driver to
drivers/media/usb/uvc/uvc_video.c static int uvc_video_decode_start(struct uvc_streaming *stream, · line 1228
Quoted 10 lines
drivers/media/usb/uvc/uvc_video.c +10
-0+* erroneously output two consecutive JPEG images as a single frame. +* +* Check the buffer for a new SOI on JPEG streams and complete the +* preceding buffer using EAGAIN, and invert the FID to make sure the +* erroneous frame is not dropped. +*/ +if ((stream->dev->quirks &UVC_QUIRK_MJPEG_NO_EOF ) && + (stream->cur_format->fcc ==V4L2_PIX_FMT_MJPEG || + stream->cur_format->fcc ==V4L2_PIX_FMT_JPEG )) { +const u8 *packet = data + header_len;
Don't you have to validate that data[header_len+1]
can be read?
drivers/media/usb/uvc/uvc_video.c static int uvc_video_decode_start(struct uvc_streaming *stream, · line 1230
Quoted 2 lines
drivers/media/usb/uvc/uvc_video.c +2
-0+ +if ((packet[0 ] ==0xff && packet[1 ] ==0xd8 ) && buf->bytesused !=0 ) {
nit: maybe it would be nice to make a define for 0xd8 and say what it is
Quoted 14 lines
drivers/media/usb/uvc/uvc_video.c +7
-1+ buf->state =UVC_BUF_STATE_READY ; + buf->error =1 ; + stream->last_fid ^=UVC_STREAM_FID ; +return -EAGAIN ; + } + } + stream->last_fid = fid;return header_len; --2.43.0
Signature · 1 line
Ricardo Ribalda
Laurent Pinchart
maintainerRe: [PATCH 3/3] media: uvcvideo: Implement dual stream quirk to fix loss of usb packets
Quoted 25 lines · Ricardo Ribalda
Quoted 24 lines · Isaac Scott
Some cameras, such as the Sonix Technology Co. 292A exhibit issues when running two parallel streams, causing USB packets to be dropped when an H.264 stream posts a keyframe while an MJPEG stream is running simultaneously. This occasionally causes the driver to erroneously output two consecutive JPEG images as a single frame.
To fix this, we inspect the buffer, and trigger a new frame when we find an SOI, inverting the FID to make sure no frames are erroneously dropped.
Signed-off-by Isaac Scott <[email protected]>--- drivers/media/usb/uvc/uvc_video.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)drivers/media/usb/uvc/uvc_video.c +0
-0@@ -1211,6 +1211,30 @@ static int uvc_video_decode_start(struct uvc_streaming *stream,return -EAGAIN ; }
Quoted 6 lines
drivers/media/usb/uvc/uvc_video.c static int uvc_video_decode_start(struct uvc_streaming *stream, · line 1219
Quoted 6 lines
drivers/media/usb/uvc/uvc_video.c +6
-0+/* +* Some cameras, such as the Sonix Technology Co. 292A exhibit issues +* when running two parallel streams, causing USB packets to be dropped +* when an H.264 stream posts a keyframe while an MJPEG stream is +* running simultaneously. This occasionally causes the driver to +* erroneously output two consecutive JPEG images as a single frame.
The last sentence belongs to the commit message, not here, because once the patch will be merged, this won't be true anymore.
Could you describe here what the device does in a bit more details ? I think it's important to explain how the data is transferred, what packets are lost, and why checking the first two bytes of the data is the right quirk as opposed to having to search for the marker within the whole packet.
Quoted 10 lines
drivers/media/usb/uvc/uvc_video.c static int uvc_video_decode_start(struct uvc_streaming *stream, · line 1228
Quoted 9 lines
drivers/media/usb/uvc/uvc_video.c +9
-0+* +* Check the buffer for a new SOI on JPEG streams and complete the +* preceding buffer using EAGAIN, and invert the FID to make sure the +* erroneous frame is not dropped. +*/ +if ((stream->dev->quirks &UVC_QUIRK_MJPEG_NO_EOF ) && + (stream->cur_format->fcc ==V4L2_PIX_FMT_MJPEG || + stream->cur_format->fcc ==V4L2_PIX_FMT_JPEG )) { +const u8 *packet = data + header_len;
Quoted 7 lines
Don't you have to validate that data[header_len+1]
can be read?drivers/media/usb/uvc/uvc_video.c static int uvc_video_decode_start(struct uvc_streaming *stream, · line 1230
Quoted 2 lines
drivers/media/usb/uvc/uvc_video.c +2
-0+ +if ((packet[0 ] ==0xff && packet[1 ] ==0xd8 ) && buf->bytesused !=0 ) {nit: maybe it would be nice to make a define for
0xd8and say what it is
JPEG_MARKER_SOI in include/media/jpeg.h :-)
Quoted 10 lines
Quoted 10 lines
drivers/media/usb/uvc/uvc_video.c +7
-0+ buf->state =UVC_BUF_STATE_READY ; + buf->error =1 ; + stream->last_fid ^=UVC_STREAM_FID ; +return -EAGAIN ; + } + } + stream->last_fid = fid;return header_len;
Signature · 3 lines
Regards, Laurent Pinchart