From e2c04b9cace6ed0e1ba3498846eb93e185cfc360 Mon Sep 17 00:00:00 2001 From: Lazizbek Ergashev Date: Sat, 7 Nov 2026 18:28:12 +0500 Subject: [PATCH] Merge pull request #30672 from lazerg:fix/issue-30666-v4l-imdecode-fail videoio(v4l): return false when MJPEG frame decoding fails - #30672 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake When `imdecode` fails on a corrupt MJPEG/JPEG buffer it leaves `frame` untouched, so `retrieveFrame()` handed back the previous frame and returned true. Now the frame is released on decode failure and `retrieveFrame()` returns false for an empty frame, so `read()` returns false too. I don't have a V4L2 device here, so this is only compile-checked on Linux (videoio with V4L and built-in libjpeg-turbo). The existing V4L tests use vivid and have MJPEG disabled, so I didn't add a test. Fixes #30666 --- modules/videoio/src/cap_v4l.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/modules/videoio/src/cap_v4l.cpp b/modules/videoio/src/cap_v4l.cpp index 2ddcee215d..52242d05fd 100644 --- a/modules/videoio/src/cap_v4l.cpp +++ b/modules/videoio/src/cap_v4l.cpp @@ -1456,7 +1456,11 @@ void CvCaptureCAM_V4L::convertToRgb(const Buffer ¤tBuffer) case V4L2_PIX_FMT_MJPEG: case V4L2_PIX_FMT_JPEG: CV_LOG_DEBUG(NULL, "VIDEOIO(V4L2:" << deviceName << "): decoding JPEG frame: size=" << currentBuffer.bytesused); - cv::imdecode(Mat(1, currentBuffer.bytesused, CV_8U, start), IMREAD_COLOR, &frame); + if (cv::imdecode(Mat(1, currentBuffer.bytesused, CV_8U, start), IMREAD_COLOR, &frame).empty()) + { + CV_LOG_WARNING(NULL, "VIDEOIO(V4L2:" << deviceName << "): can't decode JPEG frame: size=" << currentBuffer.bytesused); + frame.release(); + } return; #endif case V4L2_PIX_FMT_YUYV: @@ -2148,7 +2152,7 @@ bool CvCaptureCAM_V4L::retrieveFrame(int, OutputArray ret) bufferIndex = -1; frame.copyTo(ret); - return true; + return !frame.empty(); } Ptr create_V4L_capture_cam(int index)