G-API: Integrated cv::MediaFrame as I/O type + CPU backend
[platform/upstream/opencv.git] / modules / gapi / include / opencv2 / gapi / media.hpp
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
17 namespace cv {
18
19 class GAPI_EXPORTS MediaFrame {
20 public:
21     enum class Access { R, W };
22     class IAdapter;
23     class View;
24     using AdapterPtr = std::unique_ptr<IAdapter>;
25
26     MediaFrame();
27     explicit MediaFrame(AdapterPtr &&);
28     template<class T, class... Args> static cv::MediaFrame Create(Args&&...);
29
30     View access(Access) const;
31     cv::GFrameDesc desc() const;
32
33 private:
34     struct Priv;
35     std::shared_ptr<Priv> m;
36 };
37
38 template<class T, class... Args>
39 inline cv::MediaFrame cv::MediaFrame::Create(Args&&... args) {
40     std::unique_ptr<T> ptr(new T(std::forward<Args>(args)...));
41     return cv::MediaFrame(std::move(ptr));
42 }
43
44 class GAPI_EXPORTS MediaFrame::View final {
45 public:
46     static constexpr const size_t MAX_PLANES = 4;
47     using Ptrs     = std::array<void*, MAX_PLANES>;
48     using Strides  = std::array<std::size_t, MAX_PLANES>; // in bytes
49     using Callback = std::function<void()>;
50
51     View(Ptrs&& ptrs, Strides&& strs, Callback &&cb = [](){});
52     View(const View&) = delete;
53     View(View&&) = default;
54     ~View();
55
56     Ptrs    ptr;
57     Strides stride;
58
59 private:
60     Callback m_cb;
61 };
62
63 class GAPI_EXPORTS MediaFrame::IAdapter {
64 public:
65     virtual ~IAdapter() = 0;
66     virtual cv::GFrameDesc meta() const = 0;
67     virtual MediaFrame::View access(MediaFrame::Access) = 0;
68 };
69
70 } //namespace cv
71
72 #endif // OPENCV_GAPI_MEDIA_HPP