]> git.sesse.net Git - casparcg/blob - core/producer/stage.cpp
2.1.0: Restructured "frame" folder.
[casparcg] / core / producer / stage.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\r
6 * CasparCG is free software: you can redistribute it and/or modify\r
7 * it under the terms of the GNU General Public License as published by\r
8 * the Free Software Foundation, either version 3 of the License, or\r
9 * (at your option) any later version.\r
10 *\r
11 * CasparCG is distributed in the hope that it will be useful,\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 * GNU General Public License for more details.\r
15 *\r
16 * You should have received a copy of the GNU General Public License\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "../StdAfx.h"\r
23 \r
24 #include "stage.h"\r
25 \r
26 #include "layer.h"\r
27 \r
28 #include "../frame/draw_frame.h"\r
29 #include "../frame/frame_factory.h"\r
30 \r
31 #include <common/concurrency/executor.h>\r
32 #include <common/diagnostics/graph.h>\r
33 \r
34 #include <core/frame/frame_transform.h>\r
35 \r
36 #include <boost/foreach.hpp>\r
37 #include <boost/timer.hpp>\r
38 #include <boost/property_tree/ptree.hpp>\r
39 #include <boost/range/algorithm_ext.hpp>\r
40 \r
41 #include <tbb/parallel_for_each.h>\r
42 \r
43 #include <map>\r
44 #include <vector>\r
45 \r
46 namespace caspar { namespace core {\r
47         \r
48 template<typename T>\r
49 class tweened_transform\r
50 {\r
51         T source_;\r
52         T dest_;\r
53         int duration_;\r
54         int time_;\r
55         tweener_t tweener_;\r
56 public: \r
57         tweened_transform()\r
58                 : duration_(0)\r
59                 , time_(0)\r
60                 , tweener_(get_tweener(L"linear")){}\r
61         tweened_transform(const T& source, const T& dest, int duration, const std::wstring& tween = L"linear")\r
62                 : source_(source)\r
63                 , dest_(dest)\r
64                 , duration_(duration)\r
65                 , time_(0)\r
66                 , tweener_(get_tweener(tween)){}\r
67         \r
68         T fetch()\r
69         {\r
70                 return time_ == duration_ ? dest_ : tween(static_cast<double>(time_), source_, dest_, static_cast<double>(duration_), tweener_);\r
71         }\r
72 \r
73         T fetch_and_tick(int num)\r
74         {                                               \r
75                 time_ = std::min(time_+num, duration_);\r
76                 return fetch();\r
77         }\r
78 };\r
79 \r
80 struct stage::impl : public std::enable_shared_from_this<impl>\r
81                                    , boost::noncopyable\r
82 {                       \r
83         std::map<int, layer>                                                                    layers_;        \r
84         std::map<int, tweened_transform<core::frame_transform>> transforms_;    \r
85 \r
86         executor                                                                                                executor_;\r
87 public:\r
88         impl()  \r
89                 : executor_(L"stage")\r
90         {\r
91         }\r
92                 \r
93         std::map<int, safe_ptr<draw_frame>> operator()(const struct video_format_desc& format_desc)\r
94         {               \r
95                 return executor_.invoke([=]() -> std::map<int, safe_ptr<draw_frame>>\r
96                 {\r
97                         std::map<int, safe_ptr<class draw_frame>> result;\r
98 \r
99                         try\r
100                         {                                       \r
101                                 BOOST_FOREACH(auto& layer, layers_)                     \r
102                                         result[layer.first] = draw_frame::empty();      \r
103 \r
104                                 auto format_desc2 = format_desc;\r
105 \r
106                                 tbb::parallel_for_each(layers_.begin(), layers_.end(), [&](std::map<int, layer>::value_type& layer)\r
107                                 {\r
108                                         auto transform = transforms_[layer.first].fetch_and_tick(1);\r
109 \r
110                                         int flags = frame_producer::flags::none;\r
111                                         if(format_desc2.field_mode != field_mode::progressive)\r
112                                         {\r
113                                                 flags |= std::abs(transform.fill_scale[1]  - 1.0) > 0.0001 ? frame_producer::flags::deinterlace : frame_producer::flags::none;\r
114                                                 flags |= std::abs(transform.fill_translation[1])  > 0.0001 ? frame_producer::flags::deinterlace : frame_producer::flags::none;\r
115                                         }\r
116 \r
117                                         if(transform.is_key)\r
118                                                 flags |= frame_producer::flags::alpha_only;\r
119 \r
120                                         auto frame = layer.second.receive(flags);       \r
121                                 \r
122                                         auto frame1 = make_safe<core::draw_frame>(frame);\r
123                                         frame1->get_frame_transform() = transform;\r
124 \r
125                                         if(format_desc2.field_mode != core::field_mode::progressive)\r
126                                         {                               \r
127                                                 auto frame2 = make_safe<core::draw_frame>(frame);\r
128                                                 frame2->get_frame_transform() = transforms_[layer.first].fetch_and_tick(1);\r
129                                                 frame1 = core::draw_frame::interlace(frame1, frame2, format_desc2.field_mode);\r
130                                         }\r
131 \r
132                                         result[layer.first] = frame1;\r
133                                 });             \r
134                         }\r
135                         catch(...)\r
136                         {\r
137                                 layers_.clear();\r
138                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
139                         }       \r
140 \r
141                         return result;\r
142                 });\r
143         }\r
144                 \r
145         void apply_transforms(const std::vector<std::tuple<int, stage::transform_func_t, unsigned int, std::wstring>>& transforms)\r
146         {\r
147                 executor_.begin_invoke([=]\r
148                 {\r
149                         BOOST_FOREACH(auto& transform, transforms)\r
150                         {\r
151                                 auto src = transforms_[std::get<0>(transform)].fetch();\r
152                                 auto dst = std::get<1>(transform)(src);\r
153                                 transforms_[std::get<0>(transform)] = tweened_transform<frame_transform>(src, dst, std::get<2>(transform), std::get<3>(transform));\r
154                         }\r
155                 }, high_priority);\r
156         }\r
157                                                 \r
158         void apply_transform(int index, const stage::transform_func_t& transform, unsigned int mix_duration, const std::wstring& tween)\r
159         {\r
160                 executor_.begin_invoke([=]\r
161                 {\r
162                         auto src = transforms_[index].fetch();\r
163                         auto dst = transform(src);\r
164                         transforms_[index] = tweened_transform<frame_transform>(src, dst, mix_duration, tween);\r
165                 }, high_priority);\r
166         }\r
167 \r
168         void clear_transforms(int index)\r
169         {\r
170                 executor_.begin_invoke([=]\r
171                 {\r
172                         transforms_.erase(index);\r
173                 }, high_priority);\r
174         }\r
175 \r
176         void clear_transforms()\r
177         {\r
178                 executor_.begin_invoke([=]\r
179                 {\r
180                         transforms_.clear();\r
181                 }, high_priority);\r
182         }\r
183                 \r
184         void load(int index, const safe_ptr<frame_producer>& producer, bool preview, int auto_play_delta)\r
185         {\r
186                 executor_.begin_invoke([=]\r
187                 {\r
188                         layers_[index].load(producer, preview, auto_play_delta);\r
189                 }, high_priority);\r
190         }\r
191 \r
192         void pause(int index)\r
193         {               \r
194                 executor_.begin_invoke([=]\r
195                 {\r
196                         layers_[index].pause();\r
197                 }, high_priority);\r
198         }\r
199 \r
200         void play(int index)\r
201         {               \r
202                 executor_.begin_invoke([=]\r
203                 {\r
204                         layers_[index].play();\r
205                 }, high_priority);\r
206         }\r
207 \r
208         void stop(int index)\r
209         {               \r
210                 executor_.begin_invoke([=]\r
211                 {\r
212                         layers_[index].stop();\r
213                 }, high_priority);\r
214         }\r
215 \r
216         void clear(int index)\r
217         {\r
218                 executor_.begin_invoke([=]\r
219                 {\r
220                         layers_.erase(index);\r
221                 }, high_priority);\r
222         }\r
223                 \r
224         void clear()\r
225         {\r
226                 executor_.begin_invoke([=]\r
227                 {\r
228                         layers_.clear();\r
229                 }, high_priority);\r
230         }       \r
231         \r
232         boost::unique_future<std::wstring> call(int index, bool foreground, const std::wstring& param)\r
233         {\r
234                 return std::move(*executor_.invoke([=]\r
235                 {\r
236                         return std::make_shared<boost::unique_future<std::wstring>>(std::move(layers_[index].call(foreground, param)));\r
237                 }, high_priority));\r
238         }\r
239         \r
240         void swap_layers(const safe_ptr<stage>& other)\r
241         {\r
242                 if(other->impl_.get() == this)\r
243                         return;\r
244                 \r
245                 auto func = [=]\r
246                 {\r
247                         std::swap(layers_, other->impl_->layers_);\r
248                 };              \r
249                 executor_.begin_invoke([=]\r
250                 {\r
251                         other->impl_->executor_.invoke(func, high_priority);\r
252                 }, high_priority);\r
253         }\r
254 \r
255         void swap_layer(int index, int other_index)\r
256         {\r
257                 executor_.begin_invoke([=]\r
258                 {\r
259                         std::swap(layers_[index], layers_[other_index]);\r
260                 }, high_priority);\r
261         }\r
262 \r
263         void swap_layer(int index, int other_index, const safe_ptr<stage>& other)\r
264         {\r
265                 if(other->impl_.get() == this)\r
266                         swap_layer(index, other_index);\r
267                 else\r
268                 {\r
269                         auto func = [=]\r
270                         {\r
271                                 std::swap(layers_[index], other->impl_->layers_[other_index]);\r
272                         };              \r
273                         executor_.begin_invoke([=]\r
274                         {\r
275                                 other->impl_->executor_.invoke(func, high_priority);\r
276                         }, high_priority);\r
277                 }\r
278         }\r
279                 \r
280         boost::unique_future<safe_ptr<frame_producer>> foreground(int index)\r
281         {\r
282                 return executor_.begin_invoke([=]\r
283                 {\r
284                         return layers_[index].foreground();\r
285                 }, high_priority);\r
286         }\r
287         \r
288         boost::unique_future<safe_ptr<frame_producer>> background(int index)\r
289         {\r
290                 return executor_.begin_invoke([=]\r
291                 {\r
292                         return layers_[index].background();\r
293                 }, high_priority);\r
294         }\r
295 \r
296         boost::unique_future<boost::property_tree::wptree> info()\r
297         {\r
298                 return std::move(executor_.begin_invoke([this]() -> boost::property_tree::wptree\r
299                 {\r
300                         boost::property_tree::wptree info;\r
301                         BOOST_FOREACH(auto& layer, layers_)                     \r
302                                 info.add_child(L"layers.layer", layer.second.info())\r
303                                         .add(L"index", layer.first);    \r
304                         return info;\r
305                 }, high_priority));\r
306         }\r
307 \r
308         boost::unique_future<boost::property_tree::wptree> info(int index)\r
309         {\r
310                 return std::move(executor_.begin_invoke([=]() -> boost::property_tree::wptree\r
311                 {\r
312                         return layers_[index].info();\r
313                 }, high_priority));\r
314         }               \r
315 };\r
316 \r
317 stage::stage() : impl_(new impl()){}\r
318 void stage::apply_transforms(const std::vector<stage::transform_tuple_t>& transforms){impl_->apply_transforms(transforms);}\r
319 void stage::apply_transform(int index, const std::function<core::frame_transform(core::frame_transform)>& transform, unsigned int mix_duration, const std::wstring& tween){impl_->apply_transform(index, transform, mix_duration, tween);}\r
320 void stage::clear_transforms(int index){impl_->clear_transforms(index);}\r
321 void stage::clear_transforms(){impl_->clear_transforms();}\r
322 void stage::load(int index, const safe_ptr<frame_producer>& producer, bool preview, int auto_play_delta){impl_->load(index, producer, preview, auto_play_delta);}\r
323 void stage::pause(int index){impl_->pause(index);}\r
324 void stage::play(int index){impl_->play(index);}\r
325 void stage::stop(int index){impl_->stop(index);}\r
326 void stage::clear(int index){impl_->clear(index);}\r
327 void stage::clear(){impl_->clear();}\r
328 void stage::swap_layers(const safe_ptr<stage>& other){impl_->swap_layers(other);}\r
329 void stage::swap_layer(int index, int other_index){impl_->swap_layer(index, other_index);}\r
330 void stage::swap_layer(int index, int other_index, const safe_ptr<stage>& other){impl_->swap_layer(index, other_index, other);}\r
331 boost::unique_future<safe_ptr<frame_producer>> stage::foreground(int index) {return impl_->foreground(index);}\r
332 boost::unique_future<safe_ptr<frame_producer>> stage::background(int index) {return impl_->background(index);}\r
333 boost::unique_future<std::wstring> stage::call(int index, bool foreground, const std::wstring& param){return impl_->call(index, foreground, param);}\r
334 boost::unique_future<boost::property_tree::wptree> stage::info() const{return impl_->info();}\r
335 boost::unique_future<boost::property_tree::wptree> stage::info(int index) const{return impl_->info(index);}\r
336 std::map<int, safe_ptr<class draw_frame>> stage::operator()(const video_format_desc& format_desc){return (*impl_)(format_desc);}\r
337 }}