mirror of
https://github.com/opencv/opencv.git
synced 2026-07-30 15:53:03 +04:00
Merge pull request #25584 from dkurt:videocapture_from_buffer
Open VideoCapture from data stream #25584 ### Pull Request Readiness Checklist Add VideoCapture option to read a raw binary video data from `std::streambuf`. There are multiple motivations: 1. Avoid disk file creation in case of video already in memory (received by network or from database). 2. Streaming mode. Frames decoding starts during sequential file transfer by chunks. Suppoted backends: * FFmpeg * MSMF (no streaming mode) Supporter interfaces: * C++ (std::streambuf) * Python (io.BufferedIOBase) resolves https://github.com/opencv/opencv/issues/24400 - [x] test h264 - [x] test IP camera like approach with no metadata but key frame only? - [x] C API plugin 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 - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
@@ -719,6 +719,25 @@ enum VideoCaptureOBSensorProperties{
|
||||
|
||||
//! @} videoio_flags_others
|
||||
|
||||
/** @brief Read data stream interface
|
||||
*/
|
||||
class CV_EXPORTS_W IStreamReader
|
||||
{
|
||||
public:
|
||||
virtual ~IStreamReader();
|
||||
|
||||
/** @brief Read bytes from stream */
|
||||
virtual long long read(char* buffer, long long size) = 0;
|
||||
|
||||
/** @brief Sets the stream position
|
||||
*
|
||||
* @param offset Seek offset
|
||||
* @param origin SEEK_SET / SEEK_END / SEEK_CUR
|
||||
*
|
||||
* @see fseek
|
||||
*/
|
||||
virtual long long seek(long long offset, int origin) = 0;
|
||||
};
|
||||
|
||||
class IVideoCapture;
|
||||
//! @cond IGNORED
|
||||
@@ -798,6 +817,14 @@ public:
|
||||
*/
|
||||
CV_WRAP explicit VideoCapture(int index, int apiPreference, const std::vector<int>& params);
|
||||
|
||||
/** @overload
|
||||
@brief Opens a video using data stream.
|
||||
|
||||
The `params` parameter allows to specify extra parameters encoded as pairs `(paramId_1, paramValue_1, paramId_2, paramValue_2, ...)`.
|
||||
See cv::VideoCaptureProperties
|
||||
*/
|
||||
CV_WRAP VideoCapture(const Ptr<IStreamReader>& source, int apiPreference, const std::vector<int>& params);
|
||||
|
||||
/** @brief Default destructor
|
||||
|
||||
The method first calls VideoCapture::release to close the already opened file or camera.
|
||||
@@ -852,6 +879,19 @@ public:
|
||||
*/
|
||||
CV_WRAP virtual bool open(int index, int apiPreference, const std::vector<int>& params);
|
||||
|
||||
/** @brief Opens a video using data stream.
|
||||
|
||||
@overload
|
||||
|
||||
The `params` parameter allows to specify extra parameters encoded as pairs `(paramId_1, paramValue_1, paramId_2, paramValue_2, ...)`.
|
||||
See cv::VideoCaptureProperties
|
||||
|
||||
@return `true` if the file has been successfully opened
|
||||
|
||||
The method first calls VideoCapture::release to close the already opened file or camera.
|
||||
*/
|
||||
CV_WRAP virtual bool open(const Ptr<IStreamReader>& source, int apiPreference, const std::vector<int>& params);
|
||||
|
||||
/** @brief Returns true if video capturing has been initialized already.
|
||||
|
||||
If the previous call to VideoCapture constructor or VideoCapture::open() succeeded, the method returns
|
||||
|
||||
@@ -35,6 +35,9 @@ CV_EXPORTS_W std::vector<VideoCaptureAPIs> getCameraBackends();
|
||||
/** @brief Returns list of available backends which works via `cv::VideoCapture(filename)` */
|
||||
CV_EXPORTS_W std::vector<VideoCaptureAPIs> getStreamBackends();
|
||||
|
||||
/** @brief Returns list of available backends which works via `cv::VideoCapture(buffer)` */
|
||||
CV_EXPORTS_W std::vector<VideoCaptureAPIs> getStreamBufferedBackends();
|
||||
|
||||
/** @brief Returns list of available backends which works via `cv::VideoWriter()` */
|
||||
CV_EXPORTS_W std::vector<VideoCaptureAPIs> getWriterBackends();
|
||||
|
||||
@@ -58,6 +61,13 @@ CV_EXPORTS_W std::string getStreamBackendPluginVersion(
|
||||
CV_OUT int& version_API
|
||||
);
|
||||
|
||||
/** @brief Returns description and ABI/API version of videoio plugin's buffer capture interface */
|
||||
CV_EXPORTS_W std::string getStreamBufferedBackendPluginVersion(
|
||||
VideoCaptureAPIs api,
|
||||
CV_OUT int& version_ABI,
|
||||
CV_OUT int& version_API
|
||||
);
|
||||
|
||||
/** @brief Returns description and ABI/API version of videoio plugin's writer interface */
|
||||
CV_EXPORTS_W std::string getWriterBackendPluginVersion(
|
||||
VideoCaptureAPIs api,
|
||||
|
||||
@@ -10,6 +10,37 @@
|
||||
|
||||
namespace cv {
|
||||
CV_EXPORTS std::string icvExtractPattern(const std::string& filename, unsigned *offset);
|
||||
|
||||
class PluginStreamReader : public IStreamReader
|
||||
{
|
||||
public:
|
||||
PluginStreamReader(void* _opaque,
|
||||
long long (*_read)(void* opaque, char* buffer, long long size),
|
||||
long long (*_seek)(void* opaque, long long offset, int way))
|
||||
{
|
||||
opaque = _opaque;
|
||||
readCallback = _read;
|
||||
seekCallback = _seek;
|
||||
}
|
||||
|
||||
virtual ~PluginStreamReader() {}
|
||||
|
||||
long long read(char* buffer, long long size) override
|
||||
{
|
||||
return readCallback(opaque, buffer, size);
|
||||
}
|
||||
|
||||
long long seek(long long offset, int way) override
|
||||
{
|
||||
return seekCallback(opaque, offset, way);
|
||||
}
|
||||
|
||||
private:
|
||||
void* opaque;
|
||||
long long (*readCallback)(void* opaque, char* buffer, long long size);
|
||||
long long (*seekCallback)(void* opaque, long long offset, int way);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // OPENCV_VIDEOIO_UTILS_PRIVATE_HPP
|
||||
|
||||
Reference in New Issue
Block a user