Skip to content

Commit 395492a

Browse files
committed
Remove VideoReader::grab()
1 parent 6d155cc commit 395492a

File tree

4 files changed

+15
-81
lines changed

4 files changed

+15
-81
lines changed

modules/cudacodec/include/opencv2/cudacodec.hpp

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,6 @@ struct CV_EXPORTS_W_SIMPLE FormatInfo
336336
/** @brief cv::cudacodec::VideoReader generic properties identifier.
337337
*/
338338
enum class VideoReaderProps {
339-
PROP_DECODED_FRAME_IDX = 0, //!< Index for retrieving the decoded frame using retrieve().
340339
PROP_EXTRA_DATA_INDEX = 1, //!< Index for retrieving the extra data associated with a video source using retrieve().
341340
PROP_RAW_PACKAGES_BASE_INDEX = 2, //!< Base index for retrieving raw encoded data using retrieve().
342341
PROP_NUMBER_OF_RAW_PACKAGES_SINCE_LAST_GRAB = 3, //!< Number of raw packages recieved since the last call to grab().
@@ -379,36 +378,9 @@ class CV_EXPORTS_W VideoReader
379378
*/
380379
CV_WRAP virtual FormatInfo format() const = 0;
381380

382-
/** @brief Grabs the next frame from the video source.
383-
384-
@param stream Stream for the asynchronous version.
385-
@return `true` (non-zero) in the case of success.
386-
387-
The method/function grabs the next frame from video file or camera and returns true (non-zero) in
388-
the case of success.
389-
390-
The primary use of the function is for reading both the encoded and decoded video data when rawMode is enabled. With rawMode enabled
391-
retrieve() can be called following grab() to retrieve all the data associated with the current video source since the last call to grab() or the creation of the VideoReader.
392-
*/
393-
CV_WRAP virtual bool grab(Stream& stream = Stream::Null()) = 0;
394-
395381
/** @brief Returns previously grabbed video data.
396382
397383
@param [out] frame The returned data which depends on the provided idx.
398-
@param idx Determines the returned data inside image. The returned data can be the:
399-
- Decoded frame, idx = get(PROP_DECODED_FRAME_IDX).
400-
- Extra data if available, idx = get(PROP_EXTRA_DATA_INDEX).
401-
- Raw encoded data package. To retrieve package i, idx = get(PROP_RAW_PACKAGES_BASE_INDEX) + i with i < get(PROP_NUMBER_OF_RAW_PACKAGES_SINCE_LAST_GRAB)
402-
@return `false` if no frames have been grabbed
403-
404-
The method returns data associated with the current video source since the last call to grab() or the creation of the VideoReader. If no data is present
405-
the method returns false and the function returns an empty image.
406-
*/
407-
virtual bool retrieve(OutputArray frame, const size_t idx = static_cast<size_t>(VideoReaderProps::PROP_DECODED_FRAME_IDX)) const = 0;
408-
409-
/** @brief Returns previously grabbed encoded video data.
410-
411-
@param [out] frame The encoded video data.
412384
@param idx Determines the returned data inside image. The returned data can be the:
413385
- Extra data if available, idx = get(PROP_EXTRA_DATA_INDEX).
414386
- Raw encoded data package. To retrieve package i, idx = get(PROP_RAW_PACKAGES_BASE_INDEX) + i with i < get(PROP_NUMBER_OF_RAW_PACKAGES_SINCE_LAST_GRAB)
@@ -417,21 +389,7 @@ class CV_EXPORTS_W VideoReader
417389
The method returns data associated with the current video source since the last call to grab() or the creation of the VideoReader. If no data is present
418390
the method returns false and the function returns an empty image.
419391
*/
420-
CV_WRAP inline bool retrieve(CV_OUT Mat& frame, const size_t idx) const {
421-
return retrieve(OutputArray(frame), idx);
422-
}
423-
424-
/** @brief Returns the next video frame.
425-
426-
@param [out] frame The video frame. If grab() has not been called then this will be empty().
427-
@return `false` if no frames have been grabbed
428-
429-
The method returns data associated with the current video source since the last call to grab(). If no data is present
430-
the method returns false and the function returns an empty image.
431-
*/
432-
CV_WRAP inline bool retrieve(CV_OUT GpuMat& frame) const {
433-
return retrieve(OutputArray(frame));
434-
}
392+
CV_WRAP virtual bool retrieve(OutputArray frame, const size_t idx) const = 0;
435393

436394
/** @brief Sets a property in the VideoReader.
437395

modules/cudacodec/misc/python/test/test_cudacodec.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ def test_reader(self):
5555
self.assertTrue(ret and i_base == 2.0)
5656
ret, gpu_mat3 = reader.nextFrame()
5757
self.assertTrue(ret and isinstance(gpu_mat3,cv.cuda.GpuMat) and not gpu_mat3.empty())
58-
ret = reader.retrieve(gpu_mat3)
59-
self.assertTrue(ret and isinstance(gpu_mat3,cv.cuda.GpuMat) and not gpu_mat3.empty())
6058
ret, n_raw_packages_since_last_grab = reader.getVideoReaderProps(cv.cudacodec.VideoReaderProps_PROP_NUMBER_OF_RAW_PACKAGES_SINCE_LAST_GRAB)
6159
self.assertTrue(ret and n_raw_packages_since_last_grab > 0)
6260
self.assertTrue(reader.rawPackageHasKeyFrame(int(i_base)))

modules/cudacodec/src/video_reader.cpp

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,6 @@ namespace
121121

122122
FormatInfo format() const CV_OVERRIDE;
123123

124-
bool grab(Stream& stream) CV_OVERRIDE;
125-
126124
bool retrieve(OutputArray frame, const size_t idx) const CV_OVERRIDE;
127125

128126
bool set(const VideoReaderProps propertyId, const double propertyVal) CV_OVERRIDE;
@@ -136,7 +134,6 @@ namespace
136134
bool rawPackageHasKeyFrame(const int idx) const CV_OVERRIDE;
137135

138136
private:
139-
bool internalGrab(GpuMat& frame, Stream& stream);
140137
void waitForDecoderInit();
141138

142139
Ptr<VideoSource> videoSource_;
@@ -207,7 +204,7 @@ namespace
207204
CUvideoctxlock m_lock;
208205
};
209206

210-
bool VideoReaderImpl::internalGrab(GpuMat& frame, Stream& stream) {
207+
bool VideoReaderImpl::nextFrame(GpuMat& frame, Stream& stream) {
211208
if (videoParser_->hasError())
212209
CV_Error(Error::StsError, "Parsing/Decoding video source failed, check GPU memory is available and GPU supports hardware decoding.");
213210

@@ -275,17 +272,8 @@ namespace
275272
return true;
276273
}
277274

278-
bool VideoReaderImpl::grab(Stream& stream) {
279-
return internalGrab(lastFrame, stream);
280-
}
281-
282275
bool VideoReaderImpl::retrieve(OutputArray frame, const size_t idx) const {
283-
if (idx == decodedFrameIdx) {
284-
if (!frame.isGpuMat())
285-
CV_Error(Error::StsUnsupportedFormat, "Decoded frame is stored on the device and must be retrieved using a cv::cuda::GpuMat");
286-
frame.getGpuMatRef() = lastFrame;
287-
}
288-
else if (idx == extraDataIdx) {
276+
if (idx == extraDataIdx) {
289277
if (!frame.isMat())
290278
CV_Error(Error::StsUnsupportedFormat, "Extra data is stored on the host and must be retrieved using a cv::Mat");
291279
videoSource_->getExtraData(frame.getMatRef());
@@ -338,9 +326,6 @@ namespace
338326
bool VideoReaderImpl::get(const VideoReaderProps propertyId, double& propertyVal) const {
339327
switch (propertyId)
340328
{
341-
case VideoReaderProps::PROP_DECODED_FRAME_IDX:
342-
propertyVal = decodedFrameIdx;
343-
return true;
344329
case VideoReaderProps::PROP_EXTRA_DATA_INDEX:
345330
propertyVal = extraDataIdx;
346331
return true;
@@ -383,13 +368,6 @@ namespace
383368
bool VideoReaderImpl::get(const int propertyId, double& propertyVal) const {
384369
return videoSource_->get(propertyId, propertyVal);
385370
}
386-
387-
bool VideoReaderImpl::nextFrame(GpuMat& frame, Stream& stream)
388-
{
389-
if (!internalGrab(frame, stream))
390-
return false;
391-
return true;
392-
}
393371
}
394372

395373
Ptr<VideoReader> cv::cudacodec::createVideoReader(const String& filename, const std::vector<int>& sourceParams, const VideoReaderInitParams params)

modules/cudacodec/test/test_video.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ CUDA_TEST_P(CheckSet, Reader)
132132
ASSERT_TRUE(reader->get(cv::cudacodec::VideoReaderProps::PROP_RAW_MODE, rawModeVal));
133133
ASSERT_TRUE(rawModeVal);
134134
bool rawPacketsAvailable = false;
135-
while (reader->grab()) {
135+
GpuMat frame;
136+
while (reader->nextFrame(frame)) {
136137
double nRawPackages = -1;
137138
ASSERT_TRUE(reader->get(cv::cudacodec::VideoReaderProps::PROP_NUMBER_OF_RAW_PACKAGES_SINCE_LAST_GRAB, nRawPackages));
138139
if (nRawPackages > 0) {
@@ -159,7 +160,8 @@ CUDA_TEST_P(CheckExtraData, Reader)
159160
double extraDataIdx = -1;
160161
ASSERT_TRUE(reader->get(cv::cudacodec::VideoReaderProps::PROP_EXTRA_DATA_INDEX, extraDataIdx));
161162
ASSERT_EQ(extraDataIdx, 1 );
162-
ASSERT_TRUE(reader->grab());
163+
GpuMat frame;
164+
ASSERT_TRUE(reader->nextFrame(frame));
163165
cv::Mat extraData;
164166
const bool newData = reader->retrieve(extraData, static_cast<size_t>(extraDataIdx));
165167
ASSERT_TRUE((newData && sz) || (!newData && !sz));
@@ -184,8 +186,9 @@ CUDA_TEST_P(CheckKeyFrame, Reader)
184186
ASSERT_EQ(rawIdxBase, 2);
185187
constexpr int maxNPackagesToCheck = 2;
186188
int nPackages = 0;
189+
GpuMat frame;
187190
while (nPackages < maxNPackagesToCheck) {
188-
ASSERT_TRUE(reader->grab());
191+
ASSERT_TRUE(reader->nextFrame(frame));
189192
double N = -1;
190193
ASSERT_TRUE(reader->get(cv::cudacodec::VideoReaderProps::PROP_NUMBER_OF_RAW_PACKAGES_SINCE_LAST_GRAB,N));
191194
for (int i = static_cast<int>(rawIdxBase); i < static_cast<int>(N + rawIdxBase); i++) {
@@ -438,8 +441,7 @@ CUDA_TEST_P(VideoReadRaw, Reader)
438441
cv::cuda::GpuMat frame;
439442
for (int i = 0; i < 100; i++)
440443
{
441-
ASSERT_TRUE(reader->grab());
442-
ASSERT_TRUE(reader->retrieve(frame));
444+
ASSERT_TRUE(reader->nextFrame(frame));
443445
ASSERT_FALSE(frame.empty());
444446
double N = -1;
445447
ASSERT_TRUE(reader->get(cv::cudacodec::VideoReaderProps::PROP_NUMBER_OF_RAW_PACKAGES_SINCE_LAST_GRAB,N));
@@ -459,16 +461,12 @@ CUDA_TEST_P(VideoReadRaw, Reader)
459461
cv::cudacodec::VideoReaderInitParams params;
460462
params.rawMode = true;
461463
cv::Ptr<cv::cudacodec::VideoReader> readerActual = cv::cudacodec::createVideoReader(fileNameOut, {}, params);
462-
double decodedFrameIdx = -1;
463-
ASSERT_TRUE(readerActual->get(cv::cudacodec::VideoReaderProps::PROP_DECODED_FRAME_IDX, decodedFrameIdx));
464-
ASSERT_EQ(decodedFrameIdx, 0);
465464
cv::cuda::GpuMat reference, actual;
466465
cv::Mat referenceHost, actualHost;
467466
for (int i = 0; i < 100; i++)
468467
{
469468
ASSERT_TRUE(readerReference->nextFrame(reference));
470-
ASSERT_TRUE(readerActual->grab());
471-
ASSERT_TRUE(readerActual->retrieve(actual, static_cast<size_t>(decodedFrameIdx)));
469+
ASSERT_TRUE(readerActual->nextFrame(actual));
472470
actual.download(actualHost);
473471
reference.download(referenceHost);
474472
ASSERT_TRUE(cvtest::norm(actualHost, referenceHost, NORM_INF) == 0);
@@ -544,7 +542,8 @@ CUDA_TEST_P(CheckDecodeSurfaces, Reader)
544542
cv::Ptr<cv::cudacodec::VideoReader> reader = cv::cudacodec::createVideoReader(inputFile, {}, params);
545543
cv::cudacodec::FormatInfo fmt = reader->format();
546544
ASSERT_TRUE(fmt.ulNumDecodeSurfaces == ulNumDecodeSurfaces);
547-
for (int i = 0; i < 100; i++) ASSERT_TRUE(reader->grab());
545+
GpuMat frame;
546+
for (int i = 0; i < 100; i++) ASSERT_TRUE(reader->nextFrame(frame));
548547
}
549548

550549
{
@@ -553,7 +552,8 @@ CUDA_TEST_P(CheckDecodeSurfaces, Reader)
553552
cv::Ptr<cv::cudacodec::VideoReader> reader = cv::cudacodec::createVideoReader(inputFile, {}, params);
554553
cv::cudacodec::FormatInfo fmt = reader->format();
555554
ASSERT_TRUE(fmt.ulNumDecodeSurfaces == ulNumDecodeSurfaces + 1);
556-
for (int i = 0; i < 100; i++) ASSERT_TRUE(reader->grab());
555+
GpuMat frame;
556+
for (int i = 0; i < 100; i++) ASSERT_TRUE(reader->nextFrame(frame));
557557
}
558558
}
559559

0 commit comments

Comments
 (0)