github.com/kaydxh/golang@v0.0.131/pkg/gocv/cgo/third_path/opencv4/include/opencv2/gapi/media.hpp (about) 1 // This file is part of OpenCV project. 2 // It is subject to the license terms in the LICENSE file found in the top-level directory 3 // of this distribution and at http://opencv.org/license.html. 4 // 5 // Copyright (C) 2020 Intel Corporation 6 7 #ifndef OPENCV_GAPI_MEDIA_HPP 8 #define OPENCV_GAPI_MEDIA_HPP 9 10 #include <memory> // unique_ptr<>, shared_ptr<> 11 #include <array> // array<> 12 #include <functional> // function<> 13 #include <utility> // forward<>() 14 15 #include <opencv2/gapi/gframe.hpp> 16 #include <opencv2/gapi/util/any.hpp> 17 18 namespace cv { 19 20 /** \addtogroup gapi_data_structures 21 * @{ 22 * 23 * @brief Extra G-API data structures used to pass input/output data 24 * to the graph for processing. 25 */ 26 /** 27 * @brief cv::MediaFrame class represents an image/media frame 28 * obtained from an external source. 29 * 30 * cv::MediaFrame represents image data as specified in 31 * cv::MediaFormat. cv::MediaFrame is designed to be a thin wrapper over some 32 * external memory of buffer; the class itself provides an uniform 33 * interface over such types of memory. cv::MediaFrame wraps data from 34 * a camera driver or from a media codec and provides an abstraction 35 * layer over this memory to G-API. MediaFrame defines a compact interface 36 * to access and manage the underlying data; the implementation is 37 * fully defined by the associated Adapter (which is usually 38 * user-defined). 39 * 40 * @sa cv::RMat 41 */ 42 class GAPI_EXPORTS MediaFrame { 43 public: 44 /// This enum defines different types of cv::MediaFrame provided 45 /// access to the underlying data. Note that different flags can't 46 /// be combined in this version. 47 enum class Access { 48 R, ///< Access data for reading 49 W, ///< Access data for writing 50 }; 51 class IAdapter; 52 class View; 53 using AdapterPtr = std::unique_ptr<IAdapter>; 54 55 /** 56 * @brief Constructs an empty MediaFrame 57 * 58 * The constructed object has no any data associated with it. 59 */ 60 MediaFrame(); 61 62 /** 63 * @brief Constructs a MediaFrame with the given 64 * Adapter. MediaFrame takes ownership over the passed adapter. 65 * 66 * @param p an unique pointer to instance of IAdapter derived class. 67 */ 68 explicit MediaFrame(AdapterPtr &&p); 69 70 /** 71 * @overload 72 * @brief Constructs a MediaFrame with the given parameters for 73 * the Adapter. The adapter of type `T` is costructed on the fly. 74 * 75 * @param args list of arguments to construct an adapter of type 76 * `T`. 77 */ 78 template<class T, class... Args> static cv::MediaFrame Create(Args&&... args); 79 80 /** 81 * @brief Obtain access to the underlying data with the given 82 * mode. 83 * 84 * Depending on the associated Adapter and the data wrapped, this 85 * method may be cheap (e.g., the underlying memory is local) or 86 * costly (if the underlying memory is external or device 87 * memory). 88 * 89 * @param mode an access mode flag 90 * @return a MediaFrame::View object. The views should be handled 91 * carefully, refer to the MediaFrame::View documentation for details. 92 */ 93 View access(Access mode) const; 94 95 /** 96 * @brief Returns a media frame descriptor -- the information 97 * about the media format, dimensions, etc. 98 * @return a cv::GFrameDesc 99 */ 100 cv::GFrameDesc desc() const; 101 102 // FIXME: design a better solution 103 // Should be used only if the actual adapter provides implementation 104 /// @private -- exclude from the OpenCV documentation for now. 105 cv::util::any blobParams() const; 106 107 /** 108 * @brief Casts and returns the associated MediaFrame adapter to 109 * the particular adapter type `T`, returns nullptr if the type is 110 * different. 111 * 112 * This method may be useful if the adapter type is known by the 113 * caller, and some lower level access to the memory is required. 114 * Depending on the memory type, it may be more efficient than 115 * access(). 116 * 117 * @return a pointer to the adapter object, nullptr if the adapter 118 * type is different. 119 */ 120 template<typename T> T* get() const { 121 static_assert(std::is_base_of<IAdapter, T>::value, 122 "T is not derived from cv::MediaFrame::IAdapter!"); 123 auto* adapter = getAdapter(); 124 GAPI_Assert(adapter != nullptr); 125 return dynamic_cast<T*>(adapter); 126 } 127 128 private: 129 struct Priv; 130 std::shared_ptr<Priv> m; 131 IAdapter* getAdapter() const; 132 }; 133 134 template<class T, class... Args> 135 inline cv::MediaFrame cv::MediaFrame::Create(Args&&... args) { 136 std::unique_ptr<T> ptr(new T(std::forward<Args>(args)...)); 137 return cv::MediaFrame(std::move(ptr)); 138 } 139 140 /** 141 * @brief Provides access to the MediaFrame's underlying data. 142 * 143 * This object contains the necessary information to access the pixel 144 * data of the associated MediaFrame: arrays of pointers and strides 145 * (distance between every plane row, in bytes) for every image 146 * plane, as defined in cv::MediaFormat. 147 * There may be up to four image planes in MediaFrame. 148 * 149 * Depending on the MediaFrame::Access flag passed in 150 * MediaFrame::access(), a MediaFrame::View may be read- or 151 * write-only. 152 * 153 * Depending on the MediaFrame::IAdapter implementation associated 154 * with the parent MediaFrame, writing to memory with 155 * MediaFrame::Access::R flag may have no effect or lead to 156 * undefined behavior. Same applies to reading the memory with 157 * MediaFrame::Access::W flag -- again, depending on the IAdapter 158 * implementation, the host-side buffer the view provides access to 159 * may have no current data stored in (so in-place editing of the 160 * buffer contents may not be possible). 161 * 162 * MediaFrame::View objects must be handled carefully, as an external 163 * resource associated with MediaFrame may be locked for the time the 164 * MediaFrame::View object exists. Obtaining MediaFrame::View should 165 * be seen as "map" and destroying it as "unmap" in the "map/unmap" 166 * idiom (applicable to OpenCL, device memory, remote 167 * memory). 168 * 169 * When a MediaFrame buffer is accessed for writing, and the memory 170 * under MediaFrame::View::Ptrs is altered, the data synchronization 171 * of a host-side and device/remote buffer is not guaranteed until the 172 * MediaFrame::View is destroyed. In other words, the real data on the 173 * device or in a remote target may be updated at the MediaFrame::View 174 * destruction only -- but it depends on the associated 175 * MediaFrame::IAdapter implementation. 176 */ 177 class GAPI_EXPORTS MediaFrame::View final { 178 public: 179 static constexpr const size_t MAX_PLANES = 4; 180 using Ptrs = std::array<void*, MAX_PLANES>; 181 using Strides = std::array<std::size_t, MAX_PLANES>; // in bytes 182 using Callback = std::function<void()>; 183 184 /// @private 185 View(Ptrs&& ptrs, Strides&& strs, Callback &&cb = [](){}); 186 187 /// @private 188 View(const View&) = delete; 189 190 /// @private 191 View(View&&) = default; 192 193 /// @private 194 View& operator = (const View&) = delete; 195 196 ~View(); 197 198 Ptrs ptr; ///< Array of image plane pointers 199 Strides stride; ///< Array of image plane strides, in bytes. 200 201 private: 202 Callback m_cb; 203 }; 204 205 /** 206 * @brief An interface class for MediaFrame data adapters. 207 * 208 * Implement this interface to wrap media data in the MediaFrame. It 209 * makes sense to implement this class if there is a custom 210 * cv::gapi::wip::IStreamSource defined -- in this case, a stream 211 * source can produce MediaFrame objects with this adapter and the 212 * media data may be passed to graph without any copy. For example, a 213 * GStreamer-based stream source can implement an adapter over 214 * `GstBuffer` and G-API will transparently use it in the graph. 215 */ 216 class GAPI_EXPORTS MediaFrame::IAdapter { 217 public: 218 virtual ~IAdapter() = 0; 219 virtual cv::GFrameDesc meta() const = 0; 220 virtual MediaFrame::View access(MediaFrame::Access) = 0; 221 // FIXME: design a better solution 222 // The default implementation does nothing 223 virtual cv::util::any blobParams() const; 224 }; 225 /** @} */ 226 227 } //namespace cv 228 229 #endif // OPENCV_GAPI_MEDIA_HPP