]> git.sesse.net Git - casparcg/blob - core/frame/frame.h
### Mayor refactoring. Simplified frame handling and image_mixer. Separated video...
[casparcg] / core / frame / frame.h
1 #pragma once\r
2 \r
3 #include "../video_format.h"\r
4 \r
5 #include <common/spl/memory.h>\r
6 #include <common/forward.h>\r
7 \r
8 #include <boost/range.hpp>\r
9 #include <boost/any.hpp>\r
10 \r
11 #include <tbb/cache_aligned_allocator.h>\r
12 \r
13 #include <cstddef>\r
14 #include <stdint.h>\r
15 \r
16 FORWARD1(boost, template<typename> class shared_future);\r
17 \r
18 namespace caspar { namespace core {\r
19 \r
20 class const_array\r
21 {\r
22 public:\r
23 \r
24         // Static Members\r
25 \r
26         // Constructors\r
27 \r
28         template<typename T>\r
29         explicit const_array(const uint8_t* ptr, std::size_t size, T&& storage)\r
30                 : ptr_(ptr)\r
31                 , size_(size)\r
32                 , storage_(new boost::any(std::forward<T>(storage)))\r
33         {\r
34         }\r
35         \r
36         const_array(const const_array& other)\r
37                 : ptr_(other.ptr_)\r
38                 , size_(other.size_)\r
39                 , storage_(other.storage_)\r
40         {\r
41         }\r
42 \r
43         const_array(const_array&& other)\r
44                 : ptr_(other.ptr_)\r
45                 , size_(other.size_)\r
46                 , storage_(std::move(other.storage_))\r
47         {\r
48         }\r
49 \r
50         // Methods\r
51 \r
52         const_array& operator=(const_array other)\r
53         {\r
54                 other.swap(*this);\r
55                 return *this;\r
56         }\r
57 \r
58         void swap(const_array& other)\r
59         {\r
60                 std::swap(ptr_, other.ptr_);\r
61                 std::swap(size_, other.size_);\r
62                 std::swap(storage_, other.storage_);\r
63         }\r
64 \r
65         // Properties\r
66                 \r
67         const uint8_t* begin() const    {return ptr_;}          \r
68         const uint8_t* data() const             {return ptr_;}\r
69         const uint8_t* end() const              {return ptr_ + size_;}\r
70         std::size_t size() const                {return size_;}\r
71         bool empty() const                              {return size() == 0;}\r
72                 \r
73 private:\r
74         const uint8_t*  ptr_;\r
75         std::size_t             size_;\r
76         spl::shared_ptr<boost::any>     storage_;\r
77 };\r
78 \r
79 class mutable_array\r
80 {\r
81         mutable_array(const mutable_array&);\r
82         mutable_array& operator=(const mutable_array&);\r
83 public:\r
84 \r
85         // Static Members\r
86 \r
87         // Constructors\r
88         \r
89         template<typename T>\r
90         explicit mutable_array(uint8_t* ptr, std::size_t size, T&& storage)\r
91                 : ptr_(ptr)\r
92                 , size_(size)\r
93                 , storage_(new boost::any(std::forward<T>(storage)))\r
94         {\r
95         }\r
96 \r
97         mutable_array(mutable_array&& other)\r
98                 : ptr_(other.ptr_)\r
99                 , size_(other.size_)\r
100                 , storage_(std::move(other.storage_))\r
101         {\r
102         }\r
103 \r
104         // Methods\r
105 \r
106         mutable_array& operator=(mutable_array&& other)\r
107         {\r
108                 ptr_            = other.ptr_;\r
109                 size_           = other.size_;\r
110                 storage_        = std::move(other.storage_);\r
111                 return *this;\r
112         }\r
113 \r
114         // Properties\r
115         \r
116         uint8_t* begin()                                        {return ptr_;}          \r
117         uint8_t* data()                                         {return ptr_;}\r
118         uint8_t* end()                                          {return ptr_ + size_;}  \r
119         const uint8_t* begin() const            {return ptr_;}          \r
120         const uint8_t* data() const                     {return ptr_;}\r
121         const uint8_t* end() const                      {return ptr_ + size_;}\r
122         std::size_t size() const                        {return size_;}\r
123         bool empty() const                                      {return size() == 0;}\r
124         const boost::any& storage() const       {return *storage_;}\r
125 private:\r
126         uint64_t        id_;\r
127         uint8_t*        ptr_;\r
128         std::size_t     size_;\r
129         spl::unique_ptr<boost::any>     storage_;\r
130 };\r
131 \r
132 typedef std::vector<int32_t, tbb::cache_aligned_allocator<int32_t>> audio_buffer;\r
133 \r
134 class const_frame sealed\r
135 {\r
136 public: \r
137 \r
138         // Static Members\r
139 \r
140         static const const_frame& empty();\r
141 \r
142         // Constructors\r
143 \r
144         explicit const_frame(const void* tag = nullptr);\r
145         explicit const_frame(boost::shared_future<const_array> image, \r
146                                                 audio_buffer audio_buffer, \r
147                                                 const void* tag, \r
148                                                 const struct pixel_format_desc& desc, \r
149                                                 double frame_rate, \r
150                                                 core::field_mode field_mode);\r
151         ~const_frame();\r
152 \r
153         // Methods\r
154 \r
155         const_frame(const_frame&& other);\r
156         const_frame& operator=(const_frame&& other);\r
157         const_frame(const const_frame&);\r
158         const_frame& operator=(const const_frame& other);\r
159                                 \r
160         // Properties\r
161                         \r
162         const struct pixel_format_desc& pixel_format_desc() const;\r
163 \r
164         const_array image_data() const;\r
165         const core::audio_buffer& audio_data() const;\r
166                 \r
167         double frame_rate() const;\r
168         core::field_mode field_mode() const;\r
169 \r
170         std::size_t width() const;\r
171         std::size_t height() const;\r
172         std::size_t size() const;\r
173                                                                 \r
174         const void* tag() const;\r
175 \r
176         bool operator==(const const_frame& other);\r
177         bool operator!=(const const_frame& other);\r
178                         \r
179 private:\r
180         struct impl;\r
181         spl::shared_ptr<impl> impl_;\r
182 };\r
183 \r
184 class mutable_frame sealed\r
185 {\r
186         mutable_frame(const mutable_frame&);\r
187         mutable_frame& operator=(const mutable_frame&);\r
188 public: \r
189 \r
190         // Static Members\r
191 \r
192         // Constructors\r
193 \r
194         explicit mutable_frame(std::vector<mutable_array> image_buffers, \r
195                                                 audio_buffer audio_buffer, \r
196                                                 const void* tag, \r
197                                                 const struct pixel_format_desc& desc, \r
198                                                 double frame_rate, \r
199                                                 core::field_mode field_mode);\r
200         ~mutable_frame();\r
201 \r
202         // Methods\r
203 \r
204         mutable_frame(mutable_frame&& other);\r
205         mutable_frame& operator=(mutable_frame&& other);\r
206 \r
207         void swap(mutable_frame& other);\r
208                         \r
209         // Properties\r
210                         \r
211         const struct pixel_format_desc& pixel_format_desc() const;\r
212 \r
213         const mutable_array& image_data(std::size_t index = 0) const;\r
214         const core::audio_buffer& audio_data() const;\r
215 \r
216         mutable_array& image_data(std::size_t index = 0);\r
217         core::audio_buffer& audio_data();\r
218         \r
219         double frame_rate() const;\r
220         core::field_mode field_mode() const;\r
221 \r
222         std::size_t width() const;\r
223         std::size_t height() const;\r
224                                                                 \r
225         const void* tag() const;\r
226                         \r
227 private:\r
228         struct impl;\r
229         spl::unique_ptr<impl> impl_;\r
230 };\r
231 }}