oneVPL++
C++ API Prototype for oneVPL
impl_selector.hpp
1 /*############################################################################
2  # Copyright Intel Corporation
3  #
4  # SPDX-License-Identifier: MIT
5  ############################################################################*/
6 
7 #pragma once
8 
9 #include <memory>
10 #include <utility>
11 #include <vector>
12 
13 #include "vpl/preview/detail/sdk_callable.hpp"
14 #include "vpl/preview/exception.hpp"
15 #include "vpl/preview/impl_caps.hpp"
16 #include "vpl/mfxdispatcher.h"
17 #include "vpl/preview/options.hpp"
18 
19 namespace oneapi {
20 namespace vpl {
21 
28 protected:
31  explicit implemetation_selector(std::initializer_list<property> list = {})
32  : opts_(list),
33  format_(MFX_IMPLCAPS_IMPLDESCSTRUCTURE) {}
34 
37  explicit implemetation_selector(const std::vector<property> &list)
38  : opts_(list),
39  format_(MFX_IMPLCAPS_IMPLDESCSTRUCTURE) {}
40 
41 public:
44 
48  auto session() const {
49  mfxStatus sts;
51  auto loader = MFXLoad();
52  // convert options to mfxConfig
53 
54  std::for_each(opts_.begin(), opts_.end(), [&](auto opt) {
55  auto cfg = MFXCreateConfig(loader);
56  detail::c_api_invoker e(detail::default_checker,
57  MFXSetConfigFilterProperty,
58  cfg,
59  (const uint8_t *)opt.get_name().c_str(),
60  opt.GetValue());
61  });
62 
63  uint32_t idx = 0;
64  while (true) {
65  void *h;
66  sts = MFXEnumImplementations(loader, idx, format_, &h);
67  // break if no idx
68  if (sts == MFX_ERR_NOT_FOUND)
69  break;
70  if (sts < 0)
71  throw base_exception(sts);
72 
73  std::shared_ptr<base_implementation_capabilities> caps = factory.create(format_, h);
74 
75  if (this->operator()(caps)) {
76  mfxSession s;
77  detail::c_api_invoker e(detail::default_checker, MFXCreateSession, loader, idx, &s);
78  return std::pair(loader, s);
79  }
80  idx++;
81  }
82  MFXUnload(loader);
83  throw base_exception(MFX_ERR_NOT_INITIALIZED);
84  }
85 
86 protected:
91  virtual bool operator()(std::shared_ptr<base_implementation_capabilities> caps) const = 0;
92 
94  std::vector<property> opts_;
95 
98  mfxImplCapsDeliveryFormat format_;
99 };
100 
103 public:
106  explicit default_selector(std::initializer_list<property> list = {})
107  : implemetation_selector(list) {}
108 
111  explicit default_selector(const std::vector<property> &list) : implemetation_selector(list) {}
112 
115  bool operator()(std::shared_ptr<base_implementation_capabilities>) const override {
116  return true;
117  }
118 };
119 
122 public:
125  : default_selector({ property(property_name("mfxImplDescription") / "Impl",
126  (uint32_t)MFX_IMPL_TYPE_SOFTWARE) }) {}
127 };
128 
131 public:
134  : default_selector({ property(property_name("mfxImplDescription") / "Impl",
135  (uint32_t)MFX_IMPL_TYPE_HARDWARE) }) {}
136 };
137 
138 } // namespace vpl
139 } // namespace oneapi
Forms string at compile time for dispatcher with the property name. String is built from the series o...
Definition: property_name.hpp:139
Base class to represents oneVPL exception (negative) C API error codes.
Definition: exception.hpp:18
default_selector(const std::vector< property > &list)
Protected ctor.
Definition: impl_selector.hpp:111
virtual bool operator()(std::shared_ptr< base_implementation_capabilities > caps) const =0
This operator is applyed to any found oneVPL implementation. If operator returns true, a session based on found implementation is created. Otherwise, search is continued.
Default implemetation selector. It accepts first implementation matching provided properties...
Definition: impl_selector.hpp:102
bool operator()(std::shared_ptr< base_implementation_capabilities >) const override
Acccept first found implementation.
Definition: impl_selector.hpp:115
default_selector(std::initializer_list< property > list={})
Protected ctor.
Definition: impl_selector.hpp:106
gpu_selector()
Default ctor.
Definition: impl_selector.hpp:133
std::shared_ptr< base_implementation_capabilities > create(uint32_t id, void *handle)
Creates instance of implementation capabilities report class based on the format ID.
Definition: impl_caps.hpp:519
Safely calls C functions and throw exception in case of negative error code. User can provide own fun...
Definition: sdk_callable.hpp:56
auto session() const
Creates session which has the requested properties. Session class object calls this method at the cto...
Definition: impl_selector.hpp:48
Represents implementation properties for the dispatcher.
Definition: options.hpp:24
std::vector< property > opts_
List of properties.
Definition: impl_selector.hpp:94
virtual ~implemetation_selector()
dtor
Definition: impl_selector.hpp:43
Default SW based implementation selector. It accepts first implementation with SW based acceleration...
Definition: impl_selector.hpp:121
mfxImplCapsDeliveryFormat format_
Implementation capabilities report format.
Definition: impl_selector.hpp:98
implemetation_selector(const std::vector< property > &list)
Protected ctor.
Definition: impl_selector.hpp:37
Definition: bitstream.hpp:21
Default HW based implementation selector. It accepts first implementation with HW based acceleration...
Definition: impl_selector.hpp:130
Factory class to create implementation capabilities report class based on the format ID...
Definition: impl_caps.hpp:505
implemetation_selector(std::initializer_list< property > list={})
Protected ctor.
Definition: impl_selector.hpp:31
cpu_selector()
Default ctor.
Definition: impl_selector.hpp:124
Selects oneVPL implementation according to the specified properties.
Definition: impl_selector.hpp:27