]> git.sesse.net Git - casparcg/blob - core/producer/stage.cpp
2.1.0: -Simplified by removing auto deinterlacing for MIXER transforms.
[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/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 <functional>\r
44 #include <map>\r
45 #include <vector>\r
46 \r
47 namespace caspar { namespace core {\r
48         \r
49 struct stage::impl : public std::enable_shared_from_this<impl>\r
50 {                               \r
51         spl::shared_ptr<diagnostics::graph> graph_;\r
52         monitor::basic_subject                          event_subject_;\r
53         std::map<int, layer>                            layers_;        \r
54         std::map<int, tweened_transform>        tweens_;        \r
55         executor                                                        executor_;\r
56 public:\r
57         impl(spl::shared_ptr<diagnostics::graph> graph) \r
58                 : graph_(std::move(graph))\r
59                 , event_subject_("stage")\r
60                 , executor_(L"stage")\r
61         {\r
62                 graph_->set_color("produce-time", diagnostics::color(0.0f, 1.0f, 0.0f));\r
63         }\r
64                 \r
65         std::map<int, draw_frame> operator()(const struct video_format_desc& format_desc)\r
66         {               \r
67                 return executor_.invoke([=]() -> std::map<int, draw_frame>\r
68                 {\r
69                         boost::timer frame_timer;\r
70 \r
71                         std::map<int, class draw_frame> frames;\r
72                         \r
73                         try\r
74                         {                       \r
75                                 std::vector<int> indices;\r
76 \r
77                                 BOOST_FOREACH(auto& layer, layers_)     \r
78                                 {\r
79                                         frames[layer.first] = draw_frame::empty();      \r
80                                         indices.push_back(layer.first);\r
81                                 }                               \r
82 \r
83                                 // WORKAROUND: Compiler doesn't seem to like lambda.\r
84                                 tbb::parallel_for_each(indices.begin(), indices.end(), std::bind(&stage::impl::draw, this, std::placeholders::_1, std::ref(format_desc), std::ref(frames)));\r
85                         }\r
86                         catch(...)\r
87                         {\r
88                                 layers_.clear();\r
89                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
90                         }       \r
91                         \r
92                         graph_->set_value("produce-time", frame_timer.elapsed()*format_desc.fps*0.5);\r
93                         event_subject_ << monitor::event("profiler/time") % frame_timer.elapsed() % (1.0/format_desc.fps);\r
94 \r
95                         return frames;\r
96                 });\r
97         }\r
98 \r
99         void draw(int index, const video_format_desc& format_desc, std::map<int, draw_frame>& frames)\r
100         {\r
101                 auto& layer             = layers_[index];\r
102                 auto& tween             = tweens_[index];\r
103                 auto transform  = tween.fetch_and_tick(1);\r
104                                 \r
105                 auto frame  = layer.receive(format_desc);                                       \r
106                 auto frame1 = core::draw_frame(frame);\r
107                 frame1.transform() = transform;\r
108 \r
109                 if(format_desc.field_mode != core::field_mode::progressive)\r
110                 {                               \r
111                         auto frame2 = core::draw_frame(frame);\r
112                         frame2.transform() = tween.fetch_and_tick(1);\r
113                         frame1 = core::draw_frame::interlace(frame1, frame2, format_desc.field_mode);\r
114                 }\r
115 \r
116                 frames[index] = frame1;\r
117         }\r
118 \r
119         layer& get_layer(int index)\r
120         {\r
121                 auto it = layers_.find(index);\r
122                 if(it == std::end(layers_))\r
123                 {\r
124                         it = layers_.insert(std::make_pair(index, layer(index))).first;\r
125                         it->second.subscribe(event_subject_);\r
126                 }\r
127                 return it->second;\r
128         }\r
129                 \r
130         void apply_transforms(const std::vector<std::tuple<int, stage::transform_func_t, unsigned int, tweener>>& transforms)\r
131         {\r
132                 executor_.begin_invoke([=]\r
133                 {\r
134                         BOOST_FOREACH(auto& transform, transforms)\r
135                         {\r
136                                 auto src = tweens_[std::get<0>(transform)].fetch();\r
137                                 auto dst = std::get<1>(transform)(src);\r
138                                 tweens_[std::get<0>(transform)] = tweened_transform(src, dst, std::get<2>(transform), std::get<3>(transform));\r
139                         }\r
140                 }, task_priority::high_priority);\r
141         }\r
142                                                 \r
143         void apply_transform(int index, const stage::transform_func_t& transform, unsigned int mix_duration, const tweener& tween)\r
144         {\r
145                 executor_.begin_invoke([=]\r
146                 {\r
147                         auto src = tweens_[index].fetch();\r
148                         auto dst = transform(src);\r
149                         tweens_[index] = tweened_transform(src, dst, mix_duration, tween);\r
150                 }, task_priority::high_priority);\r
151         }\r
152 \r
153         void clear_transforms(int index)\r
154         {\r
155                 executor_.begin_invoke([=]\r
156                 {\r
157                         tweens_.erase(index);\r
158                 }, task_priority::high_priority);\r
159         }\r
160 \r
161         void clear_transforms()\r
162         {\r
163                 executor_.begin_invoke([=]\r
164                 {\r
165                         tweens_.clear();\r
166                 }, task_priority::high_priority);\r
167         }\r
168                 \r
169         void load(int index, const spl::shared_ptr<frame_producer>& producer, bool preview, const boost::optional<int32_t>& auto_play_delta)\r
170         {\r
171                 executor_.begin_invoke([=]\r
172                 {\r
173                         get_layer(index).load(producer, preview, auto_play_delta);                      \r
174                 }, task_priority::high_priority);\r
175         }\r
176 \r
177         void pause(int index)\r
178         {               \r
179                 executor_.begin_invoke([=]\r
180                 {\r
181                         layers_[index].pause();\r
182                 }, task_priority::high_priority);\r
183         }\r
184 \r
185         void play(int index)\r
186         {               \r
187                 executor_.begin_invoke([=]\r
188                 {\r
189                         layers_[index].play();\r
190                 }, task_priority::high_priority);\r
191         }\r
192 \r
193         void stop(int index)\r
194         {               \r
195                 executor_.begin_invoke([=]\r
196                 {\r
197                         layers_[index].stop();\r
198                 }, task_priority::high_priority);\r
199         }\r
200 \r
201         void clear(int index)\r
202         {\r
203                 executor_.begin_invoke([=]\r
204                 {\r
205                         layers_.erase(index);\r
206                 }, task_priority::high_priority);\r
207         }\r
208                 \r
209         void clear()\r
210         {\r
211                 executor_.begin_invoke([=]\r
212                 {\r
213                         layers_.clear();\r
214                 }, task_priority::high_priority);\r
215         }       \r
216                 \r
217         void swap_layers(stage& other)\r
218         {\r
219                 auto other_impl = other.impl_;\r
220 \r
221                 if(other_impl.get() == this)\r
222                         return;\r
223                 \r
224                 auto func = [=]\r
225                 {\r
226                         auto layers                     = layers_ | boost::adaptors::map_values;\r
227                         auto other_layers       = other_impl->layers_ | boost::adaptors::map_values;\r
228 \r
229                         BOOST_FOREACH(auto& layer, layers)\r
230                                 layer.unsubscribe(event_subject_);\r
231                         \r
232                         BOOST_FOREACH(auto& layer, other_layers)\r
233                                 layer.unsubscribe(event_subject_);\r
234                         \r
235                         std::swap(layers_, other_impl->layers_);\r
236                                                 \r
237                         BOOST_FOREACH(auto& layer, layers)\r
238                                 layer.subscribe(event_subject_);\r
239                         \r
240                         BOOST_FOREACH(auto& layer, other_layers)\r
241                                 layer.subscribe(event_subject_);\r
242                 };              \r
243                 executor_.begin_invoke([=]\r
244                 {\r
245                         other_impl->executor_.invoke(func, task_priority::high_priority);\r
246                 }, task_priority::high_priority);\r
247         }\r
248 \r
249         void swap_layer(int index, int other_index)\r
250         {\r
251                 executor_.begin_invoke([=]\r
252                 {\r
253                         std::swap(layers_[index], layers_[other_index]);\r
254                 }, task_priority::high_priority);\r
255         }\r
256 \r
257         void swap_layer(int index, int other_index, stage& other)\r
258         {\r
259                 auto other_impl = other.impl_;\r
260 \r
261                 if(other_impl.get() == this)\r
262                         swap_layer(index, other_index);\r
263                 else\r
264                 {\r
265                         auto func = [=]\r
266                         {\r
267                                 auto& my_layer          = get_layer(index);\r
268                                 auto& other_layer       = other_impl->get_layer(other_index);\r
269 \r
270                                 my_layer.unsubscribe(event_subject_);\r
271                                 other_layer.unsubscribe(other_impl->event_subject_);\r
272 \r
273                                 std::swap(my_layer, other_layer);\r
274 \r
275                                 my_layer.subscribe(event_subject_);\r
276                                 other_layer.subscribe(other_impl->event_subject_);\r
277                         };              \r
278                         executor_.begin_invoke([=]\r
279                         {\r
280                                 other_impl->executor_.invoke(func, task_priority::high_priority);\r
281                         }, task_priority::high_priority);\r
282                 }\r
283         }\r
284                 \r
285         boost::unique_future<spl::shared_ptr<frame_producer>> foreground(int index)\r
286         {\r
287                 return executor_.begin_invoke([=]\r
288                 {\r
289                         return layers_[index].foreground();\r
290                 }, task_priority::high_priority);\r
291         }\r
292         \r
293         boost::unique_future<spl::shared_ptr<frame_producer>> background(int index)\r
294         {\r
295                 return executor_.begin_invoke([=]\r
296                 {\r
297                         return layers_[index].background();\r
298                 }, task_priority::high_priority);\r
299         }\r
300 \r
301         boost::unique_future<boost::property_tree::wptree> info()\r
302         {\r
303                 return executor_.begin_invoke([this]() -> boost::property_tree::wptree\r
304                 {\r
305                         boost::property_tree::wptree info;\r
306                         BOOST_FOREACH(auto& layer, layers_)                     \r
307                                 info.add_child(L"layers.layer", layer.second.info())\r
308                                         .add(L"index", layer.first);    \r
309                         return info;\r
310                 }, task_priority::high_priority);\r
311         }\r
312 \r
313         boost::unique_future<boost::property_tree::wptree> info(int index)\r
314         {\r
315                 return executor_.begin_invoke([=]\r
316                 {\r
317                         return layers_[index].info();\r
318                 }, task_priority::high_priority);\r
319         }               \r
320 };\r
321 \r
322 stage::stage(spl::shared_ptr<diagnostics::graph> graph) : impl_(new impl(std::move(graph))){}\r
323 void stage::apply_transforms(const std::vector<stage::transform_tuple_t>& transforms){impl_->apply_transforms(transforms);}\r
324 void stage::apply_transform(int index, const std::function<core::frame_transform(core::frame_transform)>& transform, unsigned int mix_duration, const tweener& tween){impl_->apply_transform(index, transform, mix_duration, tween);}\r
325 void stage::clear_transforms(int index){impl_->clear_transforms(index);}\r
326 void stage::clear_transforms(){impl_->clear_transforms();}\r
327 void stage::load(int index, const spl::shared_ptr<frame_producer>& producer, bool preview, const boost::optional<int32_t>& auto_play_delta){impl_->load(index, producer, preview, auto_play_delta);}\r
328 void stage::pause(int index){impl_->pause(index);}\r
329 void stage::play(int index){impl_->play(index);}\r
330 void stage::stop(int index){impl_->stop(index);}\r
331 void stage::clear(int index){impl_->clear(index);}\r
332 void stage::clear(){impl_->clear();}\r
333 void stage::swap_layers(stage& other){impl_->swap_layers(other);}\r
334 void stage::swap_layer(int index, int other_index){impl_->swap_layer(index, other_index);}\r
335 void stage::swap_layer(int index, int other_index, stage& other){impl_->swap_layer(index, other_index, other);}\r
336 boost::unique_future<spl::shared_ptr<frame_producer>> stage::foreground(int index) {return impl_->foreground(index);}\r
337 boost::unique_future<spl::shared_ptr<frame_producer>> stage::background(int index) {return impl_->background(index);}\r
338 boost::unique_future<boost::property_tree::wptree> stage::info() const{return impl_->info();}\r
339 boost::unique_future<boost::property_tree::wptree> stage::info(int index) const{return impl_->info(index);}\r
340 std::map<int, class draw_frame> stage::operator()(const video_format_desc& format_desc){return (*impl_)(format_desc);}\r
341 void stage::subscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_.subscribe(o);}\r
342 void stage::unsubscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_.unsubscribe(o);}\r
343 }}