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
This commit is contained in:
Lazizbek Ergashev
2026-11-07 18:28:12 +05:00
committed by GitHub
parent 43f8b1f5da
commit e2c04b9cac

View File

@@ -1456,7 +1456,11 @@ void CvCaptureCAM_V4L::convertToRgb(const Buffer &currentBuffer)
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<IVideoCapture> create_V4L_capture_cam(int index)