Files
opencv-MIRROR/modules/videoio/misc/python/test/test_videoio.py
Tianyu Guo 3e1cb2371a Merge pull request #30798 from gty111:fix/python-video-stream-seek
videoio: fix large Python stream seeks and callback failures 🤖🤖🤖 - #30798

Opening an MP4 from a Python stream can segfault when its `moov` box requires an absolute seek beyond 2 GiB. `PythonStreamReader::seek()` narrows the 64-bit offset to `int`; a resulting Python exception is followed by operations on a null callback result. This was reproduced independently of a model runtime and through the unmodified video decoder class from vLLM main.

This patch preserves 64-bit read/seek arguments using the Python C API's `L` format, checks callback and conversion results before using them, and uses `PyEnsureGIL`/`PySafeObject` for cleanup. It also holds the GIL when releasing the stream's last Python reference. FFmpeg read callbacks now return `AVERROR(EIO)` on exceptions: returning zero caused an endless retry loop once callback exceptions were handled safely.

Regression tests cover a virtual MP4 larger than 2 GiB, exceptions and invalid results from read/seek callbacks, oversized reads, overflowing seek results, and releasing the last stream reference. The large-stream test reuses the existing 6.6 KB `highgui/video/sample_23976fps.mp4` fixture and moves its metadata into virtual free space; it does not allocate or add a multi-gigabyte asset.

Related reports: https://github.com/vllm-project/vllm/issues/46589 and https://github.com/vllm-project/vllm/issues/35285. The affected stream interface was introduced in https://github.com/opencv/opencv/pull/25584. Searches for open PythonStreamReader / Python seek fixes did not find another PR addressing this issue.

### Validation

Built unmodified and patched OpenCV from `4.x` at `38af4e35d7e6e001fa89c26b74a24232375afc1e`, using CMake/Ninja with `BUILD_LIST=core,imgproc,imgcodecs,videoio,python3` and FFmpeg enabled.

Environment: Linux aarch64, GCC 13.3, CPython 3.12.3, NumPy 2.2.6, system FFmpeg 6.1.1.

- Added standalone C++ regression `Videoio.stream_capture_ffmpeg_read_exception` next to `stream_capture_ffmpeg`: an `IStreamReader` whose `read()` throws is exercised, and `open()` returns false with `isOpened() == false`. It reuses the neighbouring backend/plugin ABI/API skips and needs neither Python bindings nor video fixtures.
- Built `opencv_test_videoio` with `BUILD_TESTS=ON` and `BUILD_LIST=core,imgproc,imgcodecs,videoio,highgui,ts,python3`. The C++ regression passed with both test-data environment variables unset. With the same test binary loading unpatched `38af4e3` libraries, opening the stream hung until an external 5-second timeout terminated it (exit 124); patched libraries completed immediately. Disabling FFmpeg verified the backend skip.
- Patched `modules/videoio/misc/python/test/test_videoio.py -v`: **8 tests passed**, including all eight invalid-callback subcases.
- Each of the four new test methods, run in a separate process against the unmodified source build, terminated with **SIGSEGV (-11)**. The four existing methods passed against that build.
- vLLM main `c07d58dc82d75f7d1a947b3264b460384ec3e18d`: the unmodified `OpenCVVideoBackendMixin` class was extracted and executed on a generated, valid 2,147,549,184-byte MP4. Unmodified OpenCV segfaulted during open; patched OpenCV opened/decoded a frame, released the capture, and retrieved correct metadata (4 frames, 2 fps, 2 seconds).
- `git diff --check`: passed.

Example test command after configuring/building the Python module:

```sh
PYTHONPATH="$PWD/build/lib/python3:$PWD/modules/python/test" \
OPENCV_TEST_DATA_PATH=/path/to/opencv_extra/testdata \
python3 modules/videoio/misc/python/test/test_videoio.py -v
```

This is decoder-level validation; a full Qwen model server was not started. Windows, macOS and MSMF have not been validated locally.

Prepared with AI assistance (Codex); native builds and the reported tests were run locally.

### Pull Request Readiness Checklist

- [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 (`4.x`).
- [x] There is a reference to the original bug report and related work.
- [x] Regression tests reuse existing opencv_extra data; no companion data PR is needed.
- [x] The patch builds with CMake. No public API is added.
2026-11-18 14:28:37 +03:00

250 lines
9.6 KiB
Python

#!/usr/bin/env python
from __future__ import print_function
import numpy as np
import cv2 as cv
import io
import struct
import sys
import tempfile
import weakref
from tests_common import NewOpenCVTests
class Bindings(NewOpenCVTests):
def check_name(self, name):
#print(name)
self.assertFalse(name == None)
self.assertFalse(name == "")
def test_registry(self):
self.check_name(cv.videoio_registry.getBackendName(cv.CAP_ANY));
self.check_name(cv.videoio_registry.getBackendName(cv.CAP_FFMPEG))
self.check_name(cv.videoio_registry.getBackendName(cv.CAP_OPENCV_MJPEG))
backends = cv.videoio_registry.getBackends()
for backend in backends:
self.check_name(cv.videoio_registry.getBackendName(backend))
def test_capture_stream_file(self):
if sys.version_info[0] < 3:
raise self.skipTest('Python 3.x required')
api_pref = None
for backend in cv.videoio_registry.getStreamBufferedBackends():
if not cv.videoio_registry.hasBackend(backend):
continue
if not cv.videoio_registry.isBackendBuiltIn(backend):
_, abi, api = cv.videoio_registry.getStreamBufferedBackendPluginVersion(backend)
if (abi < 1 or (abi == 1 and api < 2)):
continue
api_pref = backend
break
if not api_pref:
raise self.skipTest("No available backends")
with open(self.find_file("cv/video/768x576.avi"), "rb") as f:
cap = cv.VideoCapture(f, api_pref, [])
self.assertTrue(cap.isOpened())
hasFrame, frame = cap.read()
self.assertTrue(hasFrame)
self.assertEqual(frame.shape, (576, 768, 3))
def test_capture_stream_buffer(self):
if sys.version_info[0] < 3:
raise self.skipTest('Python 3.x required')
api_pref = None
for backend in cv.videoio_registry.getStreamBufferedBackends():
if not cv.videoio_registry.hasBackend(backend):
continue
if not cv.videoio_registry.isBackendBuiltIn(backend):
_, abi, api = cv.videoio_registry.getStreamBufferedBackendPluginVersion(backend)
if (abi < 1 or (abi == 1 and api < 2)):
continue
api_pref = backend
break
if not api_pref:
raise self.skipTest("No available backends")
class BufferStream(io.BufferedIOBase):
def __init__(self, filepath):
self.f = open(filepath, "rb")
def read(self, size=-1):
return self.f.read(size)
def seek(self, offset, whence):
return self.f.seek(offset, whence)
def __del__(self):
self.f.close()
stream = BufferStream(self.find_file("cv/video/768x576.avi"))
cap = cv.VideoCapture(stream, api_pref, [])
self.assertTrue(cap.isOpened())
hasFrame, frame = cap.read()
self.assertTrue(hasFrame)
self.assertEqual(frame.shape, (576, 768, 3))
def require_ffmpeg_stream(self):
if sys.version_info[0] < 3:
self.skipTest('Python 3.x required')
if (cv.CAP_FFMPEG not in cv.videoio_registry.getStreamBufferedBackends()
or not cv.videoio_registry.hasBackend(cv.CAP_FFMPEG)):
self.skipTest("FFmpeg stream backend required")
if not cv.videoio_registry.isBackendBuiltIn(cv.CAP_FFMPEG):
_, abi, api = cv.videoio_registry.getStreamBufferedBackendPluginVersion(cv.CAP_FFMPEG)
if abi < 1 or (abi == 1 and api < 2):
self.skipTest("FFmpeg stream plugin API required")
def test_capture_stream_large_seek(self):
self.require_ffmpeg_stream()
with open(self.find_file("highgui/video/sample_23976fps.mp4"), "rb") as f:
data = f.read()
class LargeVideoStream(io.BufferedIOBase):
def __init__(self):
moov_offset = data.index(b'moov') - 4
moov_size = struct.unpack_from('>I', data, moov_offset)[0]
self.suffix_offset = (1 << 31) + (1 << 16)
# Preserve the media offsets by replacing the old moov with
# free space, then place it after a second, virtual free box.
self.prefix = (data[:moov_offset + 4] + b'free' + data[moov_offset + 8:]
+ struct.pack('>I4s', self.suffix_offset - len(data), b'free'))
self.suffix = data[moov_offset:moov_offset + moov_size]
self.size = self.suffix_offset + moov_size
self.pos = 0
self.large_seeks = []
def read(self, size=-1):
size = min(size, max(0, self.size - self.pos))
result = bytearray(size)
end = self.pos + size
for start, chunk in ((0, self.prefix), (self.suffix_offset, self.suffix)):
left, right = max(self.pos, start), min(end, start + len(chunk))
if left < right:
result[left - self.pos:right - self.pos] = chunk[left - start:right - start]
self.pos = end
return bytes(result)
def seek(self, offset, whence):
if whence == io.SEEK_SET and offset > (1 << 31) - 1:
self.large_seeks.append(offset)
pos = offset + (0, self.pos, self.size)[whence]
if pos < 0:
raise ValueError("negative seek position")
self.pos = pos
return pos
# A virtual free box moves the MP4 index beyond 2 GiB without
# allocating a large buffer or adding another video fixture.
stream = LargeVideoStream()
cap = cv.VideoCapture(stream, cv.CAP_FFMPEG, [])
try:
self.assertTrue(cap.isOpened())
hasFrame, frame = cap.read()
self.assertTrue(hasFrame)
self.assertEqual(frame.shape, (120, 160, 3))
self.assertTrue(stream.large_seeks)
finally:
cap.release()
def check_capture_stream_callback_error(self, method, error):
with open(self.find_file("cv/video/768x576.avi"), "rb") as f:
data = f.read()
class FailingStream(io.BytesIO):
calls = 0
def fail(self, *args):
self.calls += 1
if error == 'exception':
raise RuntimeError("stream callback failed")
if error == 'oversized':
return b'\0' * (args[0] + 1)
if error == 'overflow':
return 1 << 100
if error == 'wrong_type':
return 'invalid result'
return None
def read(self, size=-1):
return self.fail(size) if method == 'read' else super(FailingStream, self).read(size)
def seek(self, offset, whence=0):
return self.fail(offset, whence) if method == 'seek' else super(FailingStream, self).seek(offset, whence)
stream = FailingStream(data)
cap = cv.VideoCapture()
try:
# The backend may report failure or recover without seeking.
# Neither a native crash nor a pending Python error is acceptable.
try:
cap.open(stream, cv.CAP_FFMPEG, [])
except cv.error:
pass
self.assertGreater(stream.calls, 0)
if method == 'read':
self.assertFalse(cap.isOpened())
finally:
cap.release()
valid_stream = io.BytesIO(data)
cap = cv.VideoCapture(valid_stream, cv.CAP_FFMPEG, [])
try:
self.assertTrue(cap.isOpened())
self.assertTrue(cap.read()[0])
finally:
cap.release()
def test_capture_stream_read_errors(self):
self.require_ffmpeg_stream()
for error in ('exception', 'none', 'wrong_type', 'oversized'):
with self.subTest(error=error):
self.check_capture_stream_callback_error('read', error)
def test_capture_stream_seek_errors(self):
self.require_ffmpeg_stream()
for error in ('exception', 'none', 'wrong_type', 'overflow'):
with self.subTest(error=error):
self.check_capture_stream_callback_error('seek', error)
def test_capture_stream_release_last_reference(self):
self.require_ffmpeg_stream()
with open(self.find_file("cv/video/768x576.avi"), "rb") as f:
stream = io.BytesIO(f.read())
stream_ref = weakref.ref(stream)
cap = cv.VideoCapture(stream, cv.CAP_FFMPEG, [])
try:
self.assertTrue(cap.isOpened())
del stream
self.assertIsNotNone(stream_ref())
self.assertTrue(cap.read()[0])
finally:
cap.release()
self.assertIsNone(stream_ref())
def test_context_manager(self):
video_file = self.find_file("cv/video/768x576.avi")
with cv.VideoCapture(video_file) as cap:
self.assertTrue(cap.isOpened(), "VideoCapture should be opened within context manager")
with tempfile.NamedTemporaryFile(suffix='.avi') as tmp:
with cv.VideoWriter(tmp.name, cv.VideoWriter_fourcc(*'MJPG'), 25, (640, 480)) as writer:
self.assertTrue(isinstance(writer, cv.VideoWriter))
try:
with cv.VideoCapture(video_file) as cap:
self.assertTrue(cap.isOpened())
raise RuntimeError("Testing context manager exception safety")
except RuntimeError:
pass
if __name__ == '__main__':
NewOpenCVTests.bootstrap()