Skip to content

Commit a6f7dca

Browse files
committed
Merge pull request opencv#19027 from alalek:videoio_plugins_api_versioning
2 parents 282605e + 9e6b7f5 commit a6f7dca

File tree

7 files changed

+182
-141
lines changed

7 files changed

+182
-141
lines changed

modules/videoio/src/backend_plugin.cpp

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ class PluginBackend: public IBackend
224224
return;
225225
}
226226
#ifdef HAVE_FFMPEG_WRAPPER
227-
if (plugin_api_->captureAPI == CAP_FFMPEG)
227+
if (plugin_api_->v0.captureAPI == CAP_FFMPEG)
228228
{
229229
// no checks for OpenCV minor version
230230
}
@@ -409,11 +409,11 @@ void PluginBackendFactory::loadPlugin()
409409
Ptr<PluginBackend> pluginBackend = makePtr<PluginBackend>(lib);
410410
if (pluginBackend && pluginBackend->plugin_api_)
411411
{
412-
if (pluginBackend->plugin_api_->captureAPI != id_)
412+
if (pluginBackend->plugin_api_->v0.captureAPI != id_)
413413
{
414414
CV_LOG_ERROR(NULL, "Video I/O: plugin '" << pluginBackend->plugin_api_->api_header.api_description <<
415415
"': unexpected backend ID: " <<
416-
pluginBackend->plugin_api_->captureAPI << " vs " << (int)id_ << " (expected)");
416+
pluginBackend->plugin_api_->v0.captureAPI << " vs " << (int)id_ << " (expected)");
417417
}
418418
else
419419
{
@@ -444,10 +444,10 @@ class PluginCapture : public cv::IVideoCapture
444444
{
445445
CV_Assert(plugin_api);
446446
CvPluginCapture capture = NULL;
447-
if (plugin_api->Capture_open)
447+
if (plugin_api->v0.Capture_open)
448448
{
449-
CV_Assert(plugin_api->Capture_release);
450-
if (CV_ERROR_OK == plugin_api->Capture_open(filename.empty() ? 0 : filename.c_str(), camera, &capture))
449+
CV_Assert(plugin_api->v0.Capture_release);
450+
if (CV_ERROR_OK == plugin_api->v0.Capture_open(filename.empty() ? 0 : filename.c_str(), camera, &capture))
451451
{
452452
CV_Assert(capture);
453453
return makePtr<PluginCapture>(plugin_api, capture);
@@ -464,30 +464,30 @@ class PluginCapture : public cv::IVideoCapture
464464

465465
~PluginCapture()
466466
{
467-
CV_DbgAssert(plugin_api_->Capture_release);
468-
if (CV_ERROR_OK != plugin_api_->Capture_release(capture_))
467+
CV_DbgAssert(plugin_api_->v0.Capture_release);
468+
if (CV_ERROR_OK != plugin_api_->v0.Capture_release(capture_))
469469
CV_LOG_ERROR(NULL, "Video I/O: Can't release capture by plugin '" << plugin_api_->api_header.api_description << "'");
470470
capture_ = NULL;
471471
}
472472
double getProperty(int prop) const CV_OVERRIDE
473473
{
474474
double val = -1;
475-
if (plugin_api_->Capture_getProperty)
476-
if (CV_ERROR_OK != plugin_api_->Capture_getProperty(capture_, prop, &val))
475+
if (plugin_api_->v0.Capture_getProperty)
476+
if (CV_ERROR_OK != plugin_api_->v0.Capture_getProperty(capture_, prop, &val))
477477
val = -1;
478478
return val;
479479
}
480480
bool setProperty(int prop, double val) CV_OVERRIDE
481481
{
482-
if (plugin_api_->Capture_setProperty)
483-
if (CV_ERROR_OK == plugin_api_->Capture_setProperty(capture_, prop, val))
482+
if (plugin_api_->v0.Capture_setProperty)
483+
if (CV_ERROR_OK == plugin_api_->v0.Capture_setProperty(capture_, prop, val))
484484
return true;
485485
return false;
486486
}
487487
bool grabFrame() CV_OVERRIDE
488488
{
489-
if (plugin_api_->Capture_grab)
490-
if (CV_ERROR_OK == plugin_api_->Capture_grab(capture_))
489+
if (plugin_api_->v0.Capture_grab)
490+
if (CV_ERROR_OK == plugin_api_->v0.Capture_grab(capture_))
491491
return true;
492492
return false;
493493
}
@@ -503,8 +503,8 @@ class PluginCapture : public cv::IVideoCapture
503503
bool retrieveFrame(int idx, cv::OutputArray img) CV_OVERRIDE
504504
{
505505
bool res = false;
506-
if (plugin_api_->Capture_retreive)
507-
if (CV_ERROR_OK == plugin_api_->Capture_retreive(capture_, idx, retrieve_callback, (cv::_OutputArray*)&img))
506+
if (plugin_api_->v0.Capture_retreive)
507+
if (CV_ERROR_OK == plugin_api_->v0.Capture_retreive(capture_, idx, retrieve_callback, (cv::_OutputArray*)&img))
508508
res = true;
509509
return res;
510510
}
@@ -514,7 +514,7 @@ class PluginCapture : public cv::IVideoCapture
514514
}
515515
int getCaptureDomain() CV_OVERRIDE
516516
{
517-
return plugin_api_->captureAPI;
517+
return plugin_api_->v0.captureAPI;
518518
}
519519
};
520520

@@ -534,23 +534,23 @@ class PluginWriter : public cv::IVideoWriter
534534
{
535535
CV_Assert(plugin_api);
536536
CvPluginWriter writer = NULL;
537-
if (plugin_api->api_header.api_version >= 1 && plugin_api->Writer_open_with_params)
537+
if (plugin_api->api_header.api_version >= 1 && plugin_api->v1.Writer_open_with_params)
538538
{
539-
CV_Assert(plugin_api->Writer_release);
539+
CV_Assert(plugin_api->v0.Writer_release);
540540
CV_Assert(!filename.empty());
541541
std::vector<int> vint_params = params.getIntVector();
542542
int* c_params = &vint_params[0];
543543
unsigned n_params = (unsigned)(vint_params.size() / 2);
544544

545-
if (CV_ERROR_OK == plugin_api->Writer_open_with_params(filename.c_str(), fourcc, fps, sz.width, sz.height, c_params, n_params, &writer))
545+
if (CV_ERROR_OK == plugin_api->v1.Writer_open_with_params(filename.c_str(), fourcc, fps, sz.width, sz.height, c_params, n_params, &writer))
546546
{
547547
CV_Assert(writer);
548548
return makePtr<PluginWriter>(plugin_api, writer);
549549
}
550550
}
551-
else if (plugin_api->Writer_open)
551+
else if (plugin_api->v0.Writer_open)
552552
{
553-
CV_Assert(plugin_api->Writer_release);
553+
CV_Assert(plugin_api->v0.Writer_release);
554554
CV_Assert(!filename.empty());
555555
const bool isColor = params.get(VIDEOWRITER_PROP_IS_COLOR, true);
556556
const int depth = params.get(VIDEOWRITER_PROP_DEPTH, CV_8U);
@@ -559,7 +559,7 @@ class PluginWriter : public cv::IVideoWriter
559559
CV_LOG_WARNING(NULL, "Video I/O plugin doesn't support (due to lower API level) creation of VideoWriter with depth != CV_8U");
560560
return Ptr<PluginWriter>();
561561
}
562-
if (CV_ERROR_OK == plugin_api->Writer_open(filename.c_str(), fourcc, fps, sz.width, sz.height, isColor, &writer))
562+
if (CV_ERROR_OK == plugin_api->v0.Writer_open(filename.c_str(), fourcc, fps, sz.width, sz.height, isColor, &writer))
563563
{
564564
CV_Assert(writer);
565565
return makePtr<PluginWriter>(plugin_api, writer);
@@ -576,23 +576,23 @@ class PluginWriter : public cv::IVideoWriter
576576

577577
~PluginWriter()
578578
{
579-
CV_DbgAssert(plugin_api_->Writer_release);
580-
if (CV_ERROR_OK != plugin_api_->Writer_release(writer_))
579+
CV_DbgAssert(plugin_api_->v0.Writer_release);
580+
if (CV_ERROR_OK != plugin_api_->v0.Writer_release(writer_))
581581
CV_LOG_ERROR(NULL, "Video I/O: Can't release writer by plugin '" << plugin_api_->api_header.api_description << "'");
582582
writer_ = NULL;
583583
}
584584
double getProperty(int prop) const CV_OVERRIDE
585585
{
586586
double val = -1;
587-
if (plugin_api_->Writer_getProperty)
588-
if (CV_ERROR_OK != plugin_api_->Writer_getProperty(writer_, prop, &val))
587+
if (plugin_api_->v0.Writer_getProperty)
588+
if (CV_ERROR_OK != plugin_api_->v0.Writer_getProperty(writer_, prop, &val))
589589
val = -1;
590590
return val;
591591
}
592592
bool setProperty(int prop, double val) CV_OVERRIDE
593593
{
594-
if (plugin_api_->Writer_setProperty)
595-
if (CV_ERROR_OK == plugin_api_->Writer_setProperty(writer_, prop, val))
594+
if (plugin_api_->v0.Writer_setProperty)
595+
if (CV_ERROR_OK == plugin_api_->v0.Writer_setProperty(writer_, prop, val))
596596
return true;
597597
return false;
598598
}
@@ -604,16 +604,16 @@ class PluginWriter : public cv::IVideoWriter
604604
{
605605
cv::Mat img = arr.getMat();
606606
CV_DbgAssert(writer_);
607-
CV_Assert(plugin_api_->Writer_write);
608-
if (CV_ERROR_OK != plugin_api_->Writer_write(writer_, img.data, (int)img.step[0], img.cols, img.rows, img.channels()))
607+
CV_Assert(plugin_api_->v0.Writer_write);
608+
if (CV_ERROR_OK != plugin_api_->v0.Writer_write(writer_, img.data, (int)img.step[0], img.cols, img.rows, img.channels()))
609609
{
610610
CV_LOG_DEBUG(NULL, "Video I/O: Can't write frame by plugin '" << plugin_api_->api_header.api_description << "'");
611611
}
612612
// TODO return bool result?
613613
}
614614
int getCaptureDomain() const CV_OVERRIDE
615615
{
616-
return plugin_api_->captureAPI;
616+
return plugin_api_->v0.captureAPI;
617617
}
618618
};
619619

modules/videoio/src/cap_ffmpeg.cpp

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ cv::Ptr<cv::IVideoWriter> cvCreateVideoWriter_FFMPEG_proxy(const std::string& fi
217217

218218
#if defined(BUILD_PLUGIN)
219219

220+
#define ABI_VERSION 0
221+
#define API_VERSION 0
220222
#include "plugin_api.hpp"
221223

222224
namespace cv {
@@ -393,37 +395,36 @@ CvResult CV_API_CALL cv_writer_write(CvPluginWriter handle, const unsigned char
393395
}
394396
}
395397

396-
static const OpenCV_VideoIO_Plugin_API_preview plugin_api_v0 =
398+
static const OpenCV_VideoIO_Plugin_API_preview plugin_api =
397399
{
398400
{
399-
sizeof(OpenCV_VideoIO_Plugin_API_preview), ABI_VERSION, 0/*API_VERSION*/,
401+
sizeof(OpenCV_VideoIO_Plugin_API_preview), ABI_VERSION, API_VERSION,
400402
CV_VERSION_MAJOR, CV_VERSION_MINOR, CV_VERSION_REVISION, CV_VERSION_STATUS,
401403
"FFmpeg OpenCV Video I/O plugin"
402404
},
403-
/* 1*/CAP_FFMPEG,
404-
/* 2*/cv_capture_open,
405-
/* 3*/cv_capture_release,
406-
/* 4*/cv_capture_get_prop,
407-
/* 5*/cv_capture_set_prop,
408-
/* 6*/cv_capture_grab,
409-
/* 7*/cv_capture_retrieve,
410-
/* 8*/cv_writer_open,
411-
/* 9*/cv_writer_release,
412-
/* 10*/cv_writer_get_prop,
413-
/* 11*/cv_writer_set_prop,
414-
/* 12*/cv_writer_write,
415-
/* 13 Writer_open_with_params*/NULL
405+
{
406+
/* 1*/CAP_FFMPEG,
407+
/* 2*/cv_capture_open,
408+
/* 3*/cv_capture_release,
409+
/* 4*/cv_capture_get_prop,
410+
/* 5*/cv_capture_set_prop,
411+
/* 6*/cv_capture_grab,
412+
/* 7*/cv_capture_retrieve,
413+
/* 8*/cv_writer_open,
414+
/* 9*/cv_writer_release,
415+
/* 10*/cv_writer_get_prop,
416+
/* 11*/cv_writer_set_prop,
417+
/* 12*/cv_writer_write
418+
}
416419
};
417420

418421
} // namespace
419422

420423
const OpenCV_VideoIO_Plugin_API_preview* opencv_videoio_plugin_init_v0(int requested_abi_version, int requested_api_version, void* /*reserved=NULL*/) CV_NOEXCEPT
421424
{
422-
if (requested_abi_version != 0)
423-
return NULL;
424-
if (requested_api_version != 0)
425-
return NULL;
426-
return &cv::plugin_api_v0;
425+
if (requested_abi_version == ABI_VERSION && requested_api_version <= API_VERSION)
426+
return &cv::plugin_api;
427+
return NULL;
427428
}
428429

429430
#endif // BUILD_PLUGIN

modules/videoio/src/cap_gstreamer.cpp

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,6 +1846,8 @@ void handleMessage(GstElement * pipeline)
18461846

18471847
#if defined(BUILD_PLUGIN)
18481848

1849+
#define ABI_VERSION 0
1850+
#define API_VERSION 1
18491851
#include "plugin_api.hpp"
18501852

18511853
namespace cv {
@@ -2061,37 +2063,39 @@ CvResult CV_API_CALL cv_writer_write(CvPluginWriter handle, const unsigned char
20612063
}
20622064
}
20632065

2064-
static const OpenCV_VideoIO_Plugin_API_preview plugin_api_v0 =
2066+
static const OpenCV_VideoIO_Plugin_API_preview plugin_api =
20652067
{
20662068
{
20672069
sizeof(OpenCV_VideoIO_Plugin_API_preview), ABI_VERSION, API_VERSION,
20682070
CV_VERSION_MAJOR, CV_VERSION_MINOR, CV_VERSION_REVISION, CV_VERSION_STATUS,
20692071
"GStreamer OpenCV Video I/O plugin"
20702072
},
2071-
/* 1*/CAP_GSTREAMER,
2072-
/* 2*/cv_capture_open,
2073-
/* 3*/cv_capture_release,
2074-
/* 4*/cv_capture_get_prop,
2075-
/* 5*/cv_capture_set_prop,
2076-
/* 6*/cv_capture_grab,
2077-
/* 7*/cv_capture_retrieve,
2078-
/* 8*/cv_writer_open,
2079-
/* 9*/cv_writer_release,
2080-
/* 10*/cv_writer_get_prop,
2081-
/* 11*/cv_writer_set_prop,
2082-
/* 12*/cv_writer_write,
2083-
/* 13*/cv_writer_open_with_params
2073+
{
2074+
/* 1*/CAP_GSTREAMER,
2075+
/* 2*/cv_capture_open,
2076+
/* 3*/cv_capture_release,
2077+
/* 4*/cv_capture_get_prop,
2078+
/* 5*/cv_capture_set_prop,
2079+
/* 6*/cv_capture_grab,
2080+
/* 7*/cv_capture_retrieve,
2081+
/* 8*/cv_writer_open,
2082+
/* 9*/cv_writer_release,
2083+
/* 10*/cv_writer_get_prop,
2084+
/* 11*/cv_writer_set_prop,
2085+
/* 12*/cv_writer_write
2086+
},
2087+
{
2088+
/* 13*/cv_writer_open_with_params
2089+
}
20842090
};
20852091

20862092
} // namespace
20872093

20882094
const OpenCV_VideoIO_Plugin_API_preview* opencv_videoio_plugin_init_v0(int requested_abi_version, int requested_api_version, void* /*reserved=NULL*/) CV_NOEXCEPT
20892095
{
2090-
if (requested_abi_version != 0)
2091-
return NULL;
2092-
if (requested_api_version != 0 && requested_api_version != 1)
2093-
return NULL;
2094-
return &cv::plugin_api_v0;
2096+
if (requested_abi_version == ABI_VERSION && requested_api_version <= API_VERSION)
2097+
return &cv::plugin_api;
2098+
return NULL;
20952099
}
20962100

20972101
#endif // BUILD_PLUGIN

modules/videoio/src/cap_mfx_plugin.cpp

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -185,37 +185,36 @@ CvResult CV_API_CALL cv_writer_write(CvPluginWriter handle, const unsigned char
185185
}
186186
}
187187

188-
static const OpenCV_VideoIO_Plugin_API_preview plugin_api_v0 =
188+
static const OpenCV_VideoIO_Plugin_API_preview plugin_api =
189189
{
190190
{
191-
sizeof(OpenCV_VideoIO_Plugin_API_preview), ABI_VERSION, 0/*API_VERSION*/,
191+
sizeof(OpenCV_VideoIO_Plugin_API_preview), ABI_VERSION, API_VERSION,
192192
CV_VERSION_MAJOR, CV_VERSION_MINOR, CV_VERSION_REVISION, CV_VERSION_STATUS,
193193
"MediaSDK OpenCV Video I/O plugin"
194194
},
195-
/* 1*/CAP_INTEL_MFX,
196-
/* 2*/cv_capture_open,
197-
/* 3*/cv_capture_release,
198-
/* 4*/cv_capture_get_prop,
199-
/* 5*/cv_capture_set_prop,
200-
/* 6*/cv_capture_grab,
201-
/* 7*/cv_capture_retrieve,
202-
/* 8*/cv_writer_open,
203-
/* 9*/cv_writer_release,
204-
/* 10*/cv_writer_get_prop,
205-
/* 11*/cv_writer_set_prop,
206-
/* 12*/cv_writer_write,
207-
/* 13 Writer_open_with_params*/NULL
195+
{
196+
/* 1*/CAP_INTEL_MFX,
197+
/* 2*/cv_capture_open,
198+
/* 3*/cv_capture_release,
199+
/* 4*/cv_capture_get_prop,
200+
/* 5*/cv_capture_set_prop,
201+
/* 6*/cv_capture_grab,
202+
/* 7*/cv_capture_retrieve,
203+
/* 8*/cv_writer_open,
204+
/* 9*/cv_writer_release,
205+
/* 10*/cv_writer_get_prop,
206+
/* 11*/cv_writer_set_prop,
207+
/* 12*/cv_writer_write
208+
}
208209
};
209210

210211
} // namespace
211212

212213
const OpenCV_VideoIO_Plugin_API_preview* opencv_videoio_plugin_init_v0(int requested_abi_version, int requested_api_version, void* /*reserved=NULL*/) CV_NOEXCEPT
213214
{
214-
if (requested_abi_version != 0)
215-
return NULL;
216-
if (requested_api_version != 0)
217-
return NULL;
218-
return &cv::plugin_api_v0;
215+
if (requested_abi_version == ABI_VERSION && requested_api_version <= API_VERSION)
216+
return &cv::plugin_api;
217+
return NULL;
219218
}
220219

221220
#endif // BUILD_PLUGIN

0 commit comments

Comments
 (0)