]> git.sesse.net Git - casparcg/blob - core/producer/stage.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[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 struct stage::impl : public std::enable_shared_from_this<impl>\r
49 {                       \r
50         std::map<int, layer>                            layers_;        \r
51         std::map<int, tweened_transform>        tweens_;        \r
52         executor                                                        executor_;\r
53 public:\r
54         impl() : executor_(L"stage")\r
55         {\r
56         }\r
57                 \r
58         std::map<int, spl::shared_ptr<draw_frame>> operator()(const struct video_format_desc& format_desc)\r
59         {               \r
60                 return executor_.invoke([=]() -> std::map<int, spl::shared_ptr<draw_frame>>\r
61                 {\r
62                         std::map<int, spl::shared_ptr<class draw_frame>> frames;\r
63 \r
64                         try\r
65                         {                                       \r
66                                 BOOST_FOREACH(auto& layer, layers_)                     \r
67                                         frames[layer.first] = draw_frame::empty();      \r
68 \r
69                                 auto format_desc2 = format_desc;\r
70 \r
71                                 tbb::parallel_for_each(layers_.begin(), layers_.end(), [&](std::map<int, layer>::value_type& layer)\r
72                                 {\r
73                                         auto& tween             = tweens_[layer.first];\r
74                                         auto transform  = tween.fetch_and_tick(1);\r
75 \r
76                                         frame_producer::flags flags = frame_producer::flags::none;\r
77                                         if(format_desc2.field_mode != field_mode::progressive)\r
78                                         {\r
79                                                 flags |= std::abs(transform.fill_scale[1]  - 1.0) > 0.0001 ? frame_producer::flags::deinterlace : frame_producer::flags::none;\r
80                                                 flags |= std::abs(transform.fill_translation[1])  > 0.0001 ? frame_producer::flags::deinterlace : frame_producer::flags::none;\r
81                                         }\r
82 \r
83                                         if(transform.is_key)\r
84                                                 flags |= frame_producer::flags::alpha_only;\r
85 \r
86                                         auto frame = layer.second.receive(flags);       \r
87                                 \r
88                                         auto frame1 = spl::make_shared<core::draw_frame>(frame);\r
89                                         frame1->get_frame_transform() = transform;\r
90 \r
91                                         if(format_desc2.field_mode != core::field_mode::progressive)\r
92                                         {                               \r
93                                                 auto frame2 = spl::make_shared<core::draw_frame>(frame);\r
94                                                 frame2->get_frame_transform() = tween.fetch_and_tick(1);\r
95                                                 frame1 = core::draw_frame::interlace(frame1, frame2, format_desc2.field_mode);\r
96                                         }\r
97 \r
98                                         frames[layer.first] = frame1;\r
99                                 });             \r
100                         }\r
101                         catch(...)\r
102                         {\r
103                                 layers_.clear();\r
104                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
105                         }       \r
106 \r
107                         return frames;\r
108                 });\r
109         }\r
110                 \r
111         void apply_transforms(const std::vector<std::tuple<int, stage::transform_func_t, unsigned int, tweener>>& transforms)\r
112         {\r
113                 executor_.begin_invoke([=]\r
114                 {\r
115                         BOOST_FOREACH(auto& transform, transforms)\r
116                         {\r
117                                 auto src = tweens_[std::get<0>(transform)].fetch();\r
118                                 auto dst = std::get<1>(transform)(src);\r
119                                 tweens_[std::get<0>(transform)] = tweened_transform(src, dst, std::get<2>(transform), std::get<3>(transform));\r
120                         }\r
121                 }, task_priority::high_priority);\r
122         }\r
123                                                 \r
124         void apply_transform(int index, const stage::transform_func_t& transform, unsigned int mix_duration, const tweener& tween)\r
125         {\r
126                 executor_.begin_invoke([=]\r
127                 {\r
128                         auto src = tweens_[index].fetch();\r
129                         auto dst = transform(src);\r
130                         tweens_[index] = tweened_transform(src, dst, mix_duration, tween);\r
131                 }, task_priority::high_priority);\r
132         }\r
133 \r
134         void clear_transforms(int index)\r
135         {\r
136                 executor_.begin_invoke([=]\r
137                 {\r
138                         tweens_.erase(index);\r
139                 }, task_priority::high_priority);\r
140         }\r
141 \r
142         void clear_transforms()\r
143         {\r
144                 executor_.begin_invoke([=]\r
145                 {\r
146                         tweens_.clear();\r
147                 }, task_priority::high_priority);\r
148         }\r
149                 \r
150         void load(int index, const spl::shared_ptr<frame_producer>& producer, const boost::optional<int32_t>& auto_play_delta)\r
151         {\r
152                 executor_.begin_invoke([=]\r
153                 {\r
154                         layers_[index].load(producer, auto_play_delta);\r
155                 }, task_priority::high_priority);\r
156         }\r
157 \r
158         void pause(int index)\r
159         {               \r
160                 executor_.begin_invoke([=]\r
161                 {\r
162                         layers_[index].pause();\r
163                 }, task_priority::high_priority);\r
164         }\r
165 \r
166         void play(int index)\r
167         {               \r
168                 executor_.begin_invoke([=]\r
169                 {\r
170                         layers_[index].play();\r
171                 }, task_priority::high_priority);\r
172         }\r
173 \r
174         void stop(int index)\r
175         {               \r
176                 executor_.begin_invoke([=]\r
177                 {\r
178                         layers_[index].stop();\r
179                 }, task_priority::high_priority);\r
180         }\r
181 \r
182         void clear(int index)\r
183         {\r
184                 executor_.begin_invoke([=]\r
185                 {\r
186                         layers_.erase(index);\r
187                 }, task_priority::high_priority);\r
188         }\r
189                 \r
190         void clear()\r
191         {\r
192                 executor_.begin_invoke([=]\r
193                 {\r
194                         layers_.clear();\r
195                 }, task_priority::high_priority);\r
196         }       \r
197                 \r
198         void swap_layers(const spl::shared_ptr<stage>& other)\r
199         {\r
200                 if(other->impl_.get() == this)\r
201                         return;\r
202                 \r
203                 auto func = [=]\r
204                 {\r
205                         std::swap(layers_, other->impl_->layers_);\r
206                 };              \r
207                 executor_.begin_invoke([=]\r
208                 {\r
209                         other->impl_->executor_.invoke(func, task_priority::high_priority);\r
210                 }, task_priority::high_priority);\r
211         }\r
212 \r
213         void swap_layer(int index, int other_index)\r
214         {\r
215                 executor_.begin_invoke([=]\r
216                 {\r
217                         std::swap(layers_[index], layers_[other_index]);\r
218                 }, task_priority::high_priority);\r
219         }\r
220 \r
221         void swap_layer(int index, int other_index, const spl::shared_ptr<stage>& other)\r
222         {\r
223                 if(other->impl_.get() == this)\r
224                         swap_layer(index, other_index);\r
225                 else\r
226                 {\r
227                         auto func = [=]\r
228                         {\r
229                                 std::swap(layers_[index], other->impl_->layers_[other_index]);\r
230                         };              \r
231                         executor_.begin_invoke([=]\r
232                         {\r
233                                 other->impl_->executor_.invoke(func, task_priority::high_priority);\r
234                         }, task_priority::high_priority);\r
235                 }\r
236         }\r
237                 \r
238         boost::unique_future<spl::shared_ptr<frame_producer>> foreground(int index)\r
239         {\r
240                 return executor_.begin_invoke([=]\r
241                 {\r
242                         return layers_[index].foreground();\r
243                 }, task_priority::high_priority);\r
244         }\r
245         \r
246         boost::unique_future<spl::shared_ptr<frame_producer>> background(int index)\r
247         {\r
248                 return executor_.begin_invoke([=]\r
249                 {\r
250                         return layers_[index].background();\r
251                 }, task_priority::high_priority);\r
252         }\r
253 \r
254         boost::unique_future<boost::property_tree::wptree> info()\r
255         {\r
256                 return executor_.begin_invoke([this]() -> boost::property_tree::wptree\r
257                 {\r
258                         boost::property_tree::wptree info;\r
259                         BOOST_FOREACH(auto& layer, layers_)                     \r
260                                 info.add_child(L"layers.layer", layer.second.info())\r
261                                         .add(L"index", layer.first);    \r
262                         return info;\r
263                 }, task_priority::high_priority);\r
264         }\r
265 \r
266         boost::unique_future<boost::property_tree::wptree> info(int index)\r
267         {\r
268                 return executor_.begin_invoke([=]\r
269                 {\r
270                         return layers_[index].info();\r
271                 }, task_priority::high_priority);\r
272         }               \r
273 };\r
274 \r
275 stage::stage() : impl_(new impl()){}\r
276 void stage::apply_transforms(const std::vector<stage::transform_tuple_t>& transforms){impl_->apply_transforms(transforms);}\r
277 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
278 void stage::clear_transforms(int index){impl_->clear_transforms(index);}\r
279 void stage::clear_transforms(){impl_->clear_transforms();}\r
280 void stage::load(int index, const spl::shared_ptr<frame_producer>& producer, const boost::optional<int32_t>& auto_play_delta){impl_->load(index, producer, auto_play_delta);}\r
281 void stage::pause(int index){impl_->pause(index);}\r
282 void stage::play(int index){impl_->play(index);}\r
283 void stage::stop(int index){impl_->stop(index);}\r
284 void stage::clear(int index){impl_->clear(index);}\r
285 void stage::clear(){impl_->clear();}\r
286 void stage::swap_layers(const spl::shared_ptr<stage>& other){impl_->swap_layers(other);}\r
287 void stage::swap_layer(int index, int other_index){impl_->swap_layer(index, other_index);}\r
288 void stage::swap_layer(int index, int other_index, const spl::shared_ptr<stage>& other){impl_->swap_layer(index, other_index, other);}\r
289 boost::unique_future<spl::shared_ptr<frame_producer>> stage::foreground(int index) {return impl_->foreground(index);}\r
290 boost::unique_future<spl::shared_ptr<frame_producer>> stage::background(int index) {return impl_->background(index);}\r
291 boost::unique_future<boost::property_tree::wptree> stage::info() const{return impl_->info();}\r
292 boost::unique_future<boost::property_tree::wptree> stage::info(int index) const{return impl_->info(index);}\r
293 std::map<int, spl::shared_ptr<class draw_frame>> stage::operator()(const video_format_desc& format_desc){return (*impl_)(format_desc);}\r
294 }}