mirror of
https://github.com/opencv/opencv.git
synced 2026-07-30 15:53:03 +04:00
Merge pull request #20570 from sivanov-work:vpl_source_data_adapter
G-API: oneVPL (simplification) Add data adapter & Cfg params * Add cfg_param & data_provider * Fix compilation after rebase * Apply some comments * Apply default ctor outside class definition comment * Apply cfg param in source * Fix compilation: add virtual dtor * Move cfg_params in regular gapi src list * Fix compilation: add export.hpp * Add errno.h * Add errno.h * Apply namespace comment * Add several Doxygen & rename cfg_param * Fix build * Update Doxygen docs for onevpl * Fix typo
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2021 Intel Corporation
|
||||
|
||||
#include <opencv2/gapi/util/throw.hpp>
|
||||
|
||||
#include <opencv2/gapi/streaming/onevpl/cfg_params.hpp>
|
||||
|
||||
namespace cv {
|
||||
namespace gapi {
|
||||
namespace wip {
|
||||
namespace onevpl {
|
||||
|
||||
namespace util {
|
||||
struct variant_comparator : cv::util::static_visitor<bool, variant_comparator> {
|
||||
variant_comparator(const CfgParam::value_t& rhs_value) :
|
||||
rhs(rhs_value) {}
|
||||
|
||||
template<typename ValueType>
|
||||
bool visit(const ValueType& lhs) const {
|
||||
return lhs < cv::util::get<ValueType>(rhs);
|
||||
}
|
||||
private:
|
||||
const CfgParam::value_t& rhs;
|
||||
};
|
||||
} // namespace util
|
||||
|
||||
struct CfgParam::Priv {
|
||||
Priv(const std::string& param_name, CfgParam::value_t&& param_value, bool is_major_param) :
|
||||
name(param_name), value(std::forward<value_t>(param_value)), major_flag(is_major_param) {
|
||||
}
|
||||
|
||||
const CfgParam::name_t& get_name_impl() const {
|
||||
return name;
|
||||
}
|
||||
|
||||
const CfgParam::value_t& get_value_impl() const {
|
||||
return value;
|
||||
}
|
||||
|
||||
bool is_major_impl() const {
|
||||
return major_flag;
|
||||
}
|
||||
|
||||
// comparison implementation
|
||||
bool operator< (const Priv& rhs) const {
|
||||
// implement default pair comparison
|
||||
if (get_name_impl() < rhs.get_name_impl()) {
|
||||
return true;
|
||||
} else if (get_name_impl() > rhs.get_name_impl()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
//TODO implement operator < for cv::util::variant
|
||||
const CfgParam::value_t& lvar = get_value_impl();
|
||||
const CfgParam::value_t& rvar = rhs.get_value_impl();
|
||||
if (lvar.index() < rvar.index()) {
|
||||
return true;
|
||||
} else if (lvar.index() > rvar.index()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
util::variant_comparator comp(rvar);
|
||||
return cv::util::visit(comp, lvar);
|
||||
}
|
||||
|
||||
bool operator==(const Priv& rhs) const {
|
||||
return (get_name_impl() == rhs.get_name_impl())
|
||||
&& (get_value_impl() == rhs.get_value_impl());
|
||||
}
|
||||
|
||||
bool operator!=(const Priv& rhs) const {
|
||||
return !(*this == rhs);
|
||||
}
|
||||
|
||||
CfgParam::name_t name;
|
||||
CfgParam::value_t value;
|
||||
bool major_flag;
|
||||
};
|
||||
|
||||
CfgParam::CfgParam (const std::string& param_name, value_t&& param_value, bool is_major_param) :
|
||||
m_priv(new Priv(param_name, std::move(param_value), is_major_param)) {
|
||||
}
|
||||
|
||||
CfgParam::~CfgParam() = default;
|
||||
|
||||
CfgParam& CfgParam::operator=(const CfgParam& src) {
|
||||
if (this != &src) {
|
||||
m_priv = src.m_priv;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
CfgParam& CfgParam::operator=(CfgParam&& src) {
|
||||
if (this != &src) {
|
||||
m_priv = std::move(src.m_priv);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
CfgParam::CfgParam(const CfgParam& src) :
|
||||
m_priv(src.m_priv) {
|
||||
}
|
||||
|
||||
CfgParam::CfgParam(CfgParam&& src) :
|
||||
m_priv(std::move(src.m_priv)) {
|
||||
}
|
||||
|
||||
const CfgParam::name_t& CfgParam::get_name() const {
|
||||
return m_priv->get_name_impl();
|
||||
}
|
||||
|
||||
const CfgParam::value_t& CfgParam::get_value() const {
|
||||
return m_priv->get_value_impl();
|
||||
}
|
||||
|
||||
bool CfgParam::is_major() const {
|
||||
return m_priv->is_major_impl();
|
||||
}
|
||||
|
||||
bool CfgParam::operator< (const CfgParam& rhs) const {
|
||||
return *m_priv < *rhs.m_priv;
|
||||
}
|
||||
|
||||
bool CfgParam::operator==(const CfgParam& rhs) const {
|
||||
return *m_priv == *rhs.m_priv;
|
||||
}
|
||||
|
||||
bool CfgParam::operator!=(const CfgParam& rhs) const {
|
||||
return *m_priv != *rhs.m_priv;
|
||||
}
|
||||
} // namespace onevpl
|
||||
} // namespace wip
|
||||
} // namespace gapi
|
||||
} // namespace cv
|
||||
@@ -0,0 +1,22 @@
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <opencv2/gapi/streaming/onevpl/data_provider_interface.hpp>
|
||||
|
||||
namespace cv {
|
||||
namespace gapi {
|
||||
namespace wip {
|
||||
namespace onevpl {
|
||||
DataProviderSystemErrorException::DataProviderSystemErrorException(int error_code, const std::string& desription) {
|
||||
reason = desription + ", error: " + std::to_string(error_code) + ", desctiption: " + strerror(error_code);
|
||||
}
|
||||
|
||||
DataProviderSystemErrorException::~DataProviderSystemErrorException() = default;
|
||||
|
||||
const char* DataProviderSystemErrorException::what() const noexcept {
|
||||
return reason.c_str();
|
||||
}
|
||||
} // namespace onevpl
|
||||
} // namespace wip
|
||||
} // namespace gapi
|
||||
} // namespace cv
|
||||
@@ -0,0 +1,47 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2021 Intel Corporation
|
||||
#include <errno.h>
|
||||
|
||||
#include "streaming/onevpl/file_data_provider.hpp"
|
||||
|
||||
namespace cv {
|
||||
namespace gapi {
|
||||
namespace wip {
|
||||
namespace onevpl {
|
||||
|
||||
FileDataProvider::FileDataProvider(const std::string& file_path) :
|
||||
source_handle(fopen(file_path.c_str(), "rb"), &fclose) {
|
||||
if (!source_handle) {
|
||||
throw DataProviderSystemErrorException(errno,
|
||||
"FileDataProvider: cannot open source file: " + file_path);
|
||||
}
|
||||
}
|
||||
|
||||
FileDataProvider::~FileDataProvider() = default;
|
||||
|
||||
size_t FileDataProvider::fetch_data(size_t out_data_bytes_size, void* out_data) {
|
||||
if (empty()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t ret = fread(out_data, 1, out_data_bytes_size, source_handle.get());
|
||||
if (ret == 0) {
|
||||
if (feof(source_handle.get())) {
|
||||
source_handle.reset();
|
||||
} else {
|
||||
throw DataProviderSystemErrorException (errno, "FileDataProvider::fetch_data error read");
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool FileDataProvider::empty() const {
|
||||
return !source_handle;
|
||||
}
|
||||
} // namespace onevpl
|
||||
} // namespace wip
|
||||
} // namespace gapi
|
||||
} // namespace cv
|
||||
@@ -0,0 +1,33 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2021 Intel Corporation
|
||||
|
||||
#ifndef GAPI_STREAMING_ONEVPL_ONEVPL_FILE_DATA_PROVIDER_HPP
|
||||
#define GAPI_STREAMING_ONEVPL_ONEVPL_FILE_DATA_PROVIDER_HPP
|
||||
#include <stdio.h>
|
||||
|
||||
#include <opencv2/gapi/streaming/onevpl/data_provider_interface.hpp>
|
||||
|
||||
namespace cv {
|
||||
namespace gapi {
|
||||
namespace wip {
|
||||
namespace onevpl {
|
||||
struct FileDataProvider : public IDataProvider {
|
||||
|
||||
using file_ptr = std::unique_ptr<FILE, decltype(&fclose)>;
|
||||
FileDataProvider(const std::string& file_path);
|
||||
~FileDataProvider();
|
||||
|
||||
size_t fetch_data(size_t out_data_bytes_size, void* out_data) override;
|
||||
bool empty() const override;
|
||||
private:
|
||||
file_ptr source_handle;
|
||||
};
|
||||
} // namespace onevpl
|
||||
} // namespace wip
|
||||
} // namespace gapi
|
||||
} // namespace cv
|
||||
|
||||
#endif // GAPI_STREAMING_ONEVPL_ONEVPL_FILE_DATA_PROVIDER_HPP
|
||||
@@ -1,48 +0,0 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2021 Intel Corporation
|
||||
|
||||
#include <opencv2/gapi/streaming/onevpl/onevpl_source.hpp>
|
||||
|
||||
#include "streaming/onevpl/onevpl_source_priv.hpp"
|
||||
|
||||
namespace cv {
|
||||
namespace gapi {
|
||||
namespace wip {
|
||||
|
||||
#ifdef HAVE_ONEVPL
|
||||
OneVPLSource::OneVPLSource(const std::string& filePath) :
|
||||
OneVPLSource(std::unique_ptr<Priv>(new OneVPLSource::Priv(filePath))) {
|
||||
|
||||
if (filePath.empty()) {
|
||||
util::throw_error(std::logic_error("Cannot create 'OneVPLSource' on empty source file name"));
|
||||
}
|
||||
}
|
||||
#else
|
||||
OneVPLSource::OneVPLSource(const std::string&) {
|
||||
GAPI_Assert(false && "Unsupported: G-API compiled without `WITH_GAPI_ONEVPL=ON`");
|
||||
}
|
||||
#endif
|
||||
|
||||
OneVPLSource::OneVPLSource(std::unique_ptr<Priv>&& impl) :
|
||||
IStreamSource(),
|
||||
m_priv(std::move(impl)) {
|
||||
}
|
||||
|
||||
OneVPLSource::~OneVPLSource() {
|
||||
}
|
||||
|
||||
bool OneVPLSource::pull(cv::gapi::wip::Data& data)
|
||||
{
|
||||
return m_priv->pull(data);
|
||||
}
|
||||
|
||||
GMetaArg OneVPLSource::descr_of() const
|
||||
{
|
||||
return m_priv->descr_of();
|
||||
}
|
||||
} // namespace wip
|
||||
} // namespace gapi
|
||||
} // namespace cv
|
||||
@@ -0,0 +1,58 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2021 Intel Corporation
|
||||
|
||||
#include <opencv2/gapi/streaming/onevpl/source.hpp>
|
||||
|
||||
#include "streaming/onevpl/source_priv.hpp"
|
||||
#include "streaming/onevpl/file_data_provider.hpp"
|
||||
namespace cv {
|
||||
namespace gapi {
|
||||
namespace wip {
|
||||
namespace onevpl {
|
||||
|
||||
#ifdef HAVE_ONEVPL
|
||||
GSource::GSource(const std::string& filePath, const CfgParams& cfg_params) :
|
||||
GSource(std::unique_ptr<Priv>(new GSource::Priv(std::make_shared<FileDataProvider>(filePath),
|
||||
cfg_params))) {
|
||||
|
||||
if (filePath.empty()) {
|
||||
util::throw_error(std::logic_error("Cannot create 'GSource' on empty source file name"));
|
||||
}
|
||||
}
|
||||
|
||||
GSource::GSource(std::shared_ptr<IDataProvider> source, const CfgParams& cfg_params) :
|
||||
GSource(std::unique_ptr<Priv>(new GSource::Priv(source, cfg_params))) {
|
||||
}
|
||||
#else
|
||||
GSource::GSource(const std::string&, const CfgParams&) {
|
||||
GAPI_Assert(false && "Unsupported: G-API compiled without `WITH_GAPI_ONEVPL=ON`");
|
||||
}
|
||||
|
||||
GSource::GSource(std::shared_ptr<IDataProvider>, const CfgParams&) {
|
||||
GAPI_Assert(false && "Unsupported: G-API compiled without `WITH_GAPI_ONEVPL=ON`");
|
||||
}
|
||||
#endif
|
||||
|
||||
GSource::GSource(std::unique_ptr<Priv>&& impl) :
|
||||
IStreamSource(),
|
||||
m_priv(std::move(impl)) {
|
||||
}
|
||||
|
||||
GSource::~GSource() = default;
|
||||
|
||||
bool GSource::pull(cv::gapi::wip::Data& data)
|
||||
{
|
||||
return m_priv->pull(data);
|
||||
}
|
||||
|
||||
GMetaArg GSource::descr_of() const
|
||||
{
|
||||
return m_priv->descr_of();
|
||||
}
|
||||
} // namespace onevpl
|
||||
} // namespace wip
|
||||
} // namespace gapi
|
||||
} // namespace cv
|
||||
+13
-9
@@ -7,19 +7,21 @@
|
||||
#include <algorithm>
|
||||
#include <sstream>
|
||||
|
||||
#include "streaming/onevpl/onevpl_source_priv.hpp"
|
||||
#include "streaming/onevpl/source_priv.hpp"
|
||||
#include "logger.hpp"
|
||||
|
||||
#ifndef HAVE_ONEVPL
|
||||
namespace cv {
|
||||
namespace gapi {
|
||||
namespace wip {
|
||||
bool OneVPLSource::Priv::pull(cv::gapi::wip::Data&) {
|
||||
namespace onevpl {
|
||||
bool GSource::Priv::pull(cv::gapi::wip::Data&) {
|
||||
return true;
|
||||
}
|
||||
GMetaArg OneVPLSource::Priv::descr_of() const {
|
||||
GMetaArg GSource::Priv::descr_of() const {
|
||||
return {};
|
||||
}
|
||||
} // namespace onevpl
|
||||
} // namespace wip
|
||||
} // namespace gapi
|
||||
} // namespace cv
|
||||
@@ -29,33 +31,35 @@ GMetaArg OneVPLSource::Priv::descr_of() const {
|
||||
namespace cv {
|
||||
namespace gapi {
|
||||
namespace wip {
|
||||
OneVPLSource::Priv::Priv() :
|
||||
namespace onevpl {
|
||||
GSource::Priv::Priv() :
|
||||
mfx_handle(MFXLoad())
|
||||
{
|
||||
GAPI_LOG_INFO(nullptr, "Initialized MFX handle: " << mfx_handle);
|
||||
description_is_valid = false;
|
||||
}
|
||||
|
||||
OneVPLSource::Priv::Priv(const std::string&) :
|
||||
OneVPLSource::Priv()
|
||||
GSource::Priv::Priv(std::shared_ptr<IDataProvider>, const std::vector<CfgParam>&) :
|
||||
GSource::Priv()
|
||||
{
|
||||
}
|
||||
|
||||
OneVPLSource::Priv::~Priv()
|
||||
GSource::Priv::~Priv()
|
||||
{
|
||||
GAPI_LOG_INFO(nullptr, "Unload MFX handle: " << mfx_handle);
|
||||
MFXUnload(mfx_handle);
|
||||
}
|
||||
|
||||
bool OneVPLSource::Priv::pull(cv::gapi::wip::Data&)
|
||||
bool GSource::Priv::pull(cv::gapi::wip::Data&)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
GMetaArg OneVPLSource::Priv::descr_of() const
|
||||
GMetaArg GSource::Priv::descr_of() const
|
||||
{
|
||||
return {};
|
||||
}
|
||||
} // namespace onevpl
|
||||
} // namespace wip
|
||||
} // namespace gapi
|
||||
} // namespace cv
|
||||
+9
-4
@@ -14,7 +14,7 @@
|
||||
|
||||
#include <opencv2/gapi/garg.hpp>
|
||||
#include <opencv2/gapi/streaming/meta.hpp>
|
||||
#include <opencv2/gapi/streaming/onevpl/onevpl_source.hpp>
|
||||
#include <opencv2/gapi/streaming/onevpl/source.hpp>
|
||||
|
||||
#ifdef HAVE_ONEVPL
|
||||
#if (MFX_VERSION >= 2000)
|
||||
@@ -28,10 +28,12 @@
|
||||
namespace cv {
|
||||
namespace gapi {
|
||||
namespace wip {
|
||||
namespace onevpl {
|
||||
|
||||
struct OneVPLSource::Priv
|
||||
struct GSource::Priv
|
||||
{
|
||||
explicit Priv(const std::string& file_path);
|
||||
explicit Priv(std::shared_ptr<IDataProvider> provider,
|
||||
const std::vector<CfgParam>& params);
|
||||
~Priv();
|
||||
|
||||
bool pull(cv::gapi::wip::Data& data);
|
||||
@@ -41,6 +43,7 @@ private:
|
||||
mfxLoader mfx_handle;
|
||||
bool description_is_valid;
|
||||
};
|
||||
} // namespace onevpl
|
||||
} // namespace wip
|
||||
} // namespace gapi
|
||||
} // namespace cv
|
||||
@@ -50,11 +53,13 @@ private:
|
||||
namespace cv {
|
||||
namespace gapi {
|
||||
namespace wip {
|
||||
struct OneVPLSource::Priv final
|
||||
namespace onevpl {
|
||||
struct GSource::Priv final
|
||||
{
|
||||
bool pull(cv::gapi::wip::Data&);
|
||||
GMetaArg descr_of() const;
|
||||
};
|
||||
} // namespace onevpl
|
||||
} // namespace wip
|
||||
} // namespace gapi
|
||||
} // namespace cv
|
||||
Reference in New Issue
Block a user