]> git.sesse.net Git - casparcg/blob - core/producer/stage.cpp
2.0.2: auto-deinterlace when using MIXER fill scaling and/or translation.
[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/basic_frame.h"\r
29 #include "frame/frame_factory.h"\r
30 \r
31 #include <common/concurrency/executor.h>\r
32 \r
33 #include <core/producer/frame/frame_transform.h>\r
34 \r
35 #include <boost/foreach.hpp>\r
36 #include <boost/timer.hpp>\r
37 \r
38 #include <tbb/parallel_for_each.h>\r
39 \r
40 #include <boost/property_tree/ptree.hpp>\r
41 \r
42 #include <map>\r
43 \r
44 namespace caspar { namespace core {\r
45         \r
46 template<typename T>\r
47 class tweened_transform\r
48 {\r
49         T source_;\r
50         T dest_;\r
51         int duration_;\r
52         int time_;\r
53         tweener_t tweener_;\r
54 public: \r
55         tweened_transform()\r
56                 : duration_(0)\r
57                 , time_(0)\r
58                 , tweener_(get_tweener(L"linear")){}\r
59         tweened_transform(const T& source, const T& dest, int duration, const std::wstring& tween = L"linear")\r
60                 : source_(source)\r
61                 , dest_(dest)\r
62                 , duration_(duration)\r
63                 , time_(0)\r
64                 , tweener_(get_tweener(tween)){}\r
65         \r
66         T fetch()\r
67         {\r
68                 return time_ == duration_ ? dest_ : tween(static_cast<double>(time_), source_, dest_, static_cast<double>(duration_), tweener_);\r
69         }\r
70 \r
71         T fetch_and_tick(int num)\r
72         {                                               \r
73                 time_ = std::min(time_+num, duration_);\r
74                 return fetch();\r
75         }\r
76 };\r
77 \r
78 struct stage::implementation : public std::enable_shared_from_this<implementation>\r
79                                                          , boost::noncopyable\r
80 {               \r
81         safe_ptr<diagnostics::graph>    graph_;\r
82         safe_ptr<stage::target_t>               target_;\r
83         video_format_desc                               format_desc_;\r
84 \r
85         boost::timer                                    produce_timer_;\r
86         boost::timer                                    tick_timer_;\r
87 \r
88         std::map<int, layer>                    layers_;        \r
89         std::map<int, tweened_transform<core::frame_transform>> transforms_;    \r
90 \r
91         executor                                                executor_;\r
92 public:\r
93         implementation(const safe_ptr<diagnostics::graph>& graph, const safe_ptr<stage::target_t>& target, const video_format_desc& format_desc)  \r
94                 : graph_(graph)\r
95                 , format_desc_(format_desc)\r
96                 , target_(target)\r
97                 , executor_(L"stage")\r
98         {\r
99                 graph_->add_guide("tick-time", 0.5f);   \r
100                 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));   \r
101                 graph_->set_color("produce-time", diagnostics::color(0.0f, 1.0f, 0.0f));\r
102         }\r
103 \r
104         void spawn_token()\r
105         {\r
106                 std::weak_ptr<implementation> self = shared_from_this();\r
107                 executor_.begin_invoke([=]{tick(self);});\r
108         }\r
109                                                         \r
110         void tick(const std::weak_ptr<implementation>& self)\r
111         {               \r
112                 try\r
113                 {\r
114                         produce_timer_.restart();\r
115 \r
116                         std::map<int, safe_ptr<basic_frame>> frames;\r
117                 \r
118                         BOOST_FOREACH(auto& layer, layers_)                     \r
119                                 frames[layer.first] = basic_frame::empty();     \r
120 \r
121                         tbb::parallel_for_each(layers_.begin(), layers_.end(), [&](std::map<int, layer>::value_type& layer) \r
122                         {\r
123                                 auto transform = transforms_[layer.first].fetch_and_tick(1);\r
124 \r
125                                 int hints = frame_producer::NO_HINT;\r
126                                 if(format_desc_.field_mode != field_mode::progressive)\r
127                                 {\r
128                                         hints |= std::abs(transform.fill_scale[1]  - 1.0) > 0.0001 ? frame_producer::DEINTERLACE_HINT : frame_producer::NO_HINT;\r
129                                         hints |= std::abs(transform.fill_translation[1]) > 0.0001 ? frame_producer::DEINTERLACE_HINT : frame_producer::NO_HINT;\r
130                                 }\r
131 \r
132                                 if(transform.is_key)\r
133                                         hints |= frame_producer::ALPHA_HINT;\r
134 \r
135                                 auto frame = layer.second.receive(hints);       \r
136                                 \r
137                                 auto frame1 = make_safe<core::basic_frame>(frame);\r
138                                 frame1->get_frame_transform() = transform;\r
139 \r
140                                 if(format_desc_.field_mode != core::field_mode::progressive)\r
141                                 {                               \r
142                                         auto frame2 = make_safe<core::basic_frame>(frame);\r
143                                         frame2->get_frame_transform() = transforms_[layer.first].fetch_and_tick(1);\r
144                                         frame1 = core::basic_frame::interlace(frame1, frame2, format_desc_.field_mode);\r
145                                 }\r
146 \r
147                                 frames[layer.first] = frame1;\r
148                         });\r
149                         \r
150                         graph_->update_value("produce-time", produce_timer_.elapsed()*format_desc_.fps*0.5);\r
151                         \r
152                         std::shared_ptr<void> ticket(nullptr, [self](void*)\r
153                         {\r
154                                 auto self2 = self.lock();\r
155                                 if(self2)                               \r
156                                         self2->executor_.begin_invoke([=]{tick(self);});                                \r
157                         });\r
158 \r
159                         target_->send(std::make_pair(frames, ticket));\r
160 \r
161                         graph_->update_value("tick-time", tick_timer_.elapsed()*format_desc_.fps*0.5);\r
162                         tick_timer_.restart();\r
163                 }\r
164                 catch(...)\r
165                 {\r
166                         layers_.clear();\r
167                         CASPAR_LOG_CURRENT_EXCEPTION();\r
168                 }               \r
169         }\r
170                 void set_transform(int index, const frame_transform& transform, unsigned int mix_duration, const std::wstring& tween)\r
171         {\r
172                 executor_.begin_invoke([=]\r
173                 {\r
174                         auto src = transforms_[index].fetch();\r
175                         auto dst = transform;\r
176                         transforms_[index] = tweened_transform<frame_transform>(src, dst, mix_duration, tween);\r
177                 }, high_priority);\r
178         }\r
179                                 \r
180         void apply_transform(int index, const std::function<frame_transform(frame_transform)>& transform, unsigned int mix_duration, const std::wstring& tween)\r
181         {\r
182                 executor_.begin_invoke([=]\r
183                 {\r
184                         auto src = transforms_[index].fetch();\r
185                         auto dst = transform(src);\r
186                         transforms_[index] = tweened_transform<frame_transform>(src, dst, mix_duration, tween);\r
187                 }, high_priority);\r
188         }\r
189 \r
190         void clear_transforms(int index)\r
191         {\r
192                 executor_.begin_invoke([=]\r
193                 {\r
194                         transforms_.erase(index);\r
195                 }, high_priority);\r
196         }\r
197 \r
198         void clear_transforms()\r
199         {\r
200                 executor_.begin_invoke([=]\r
201                 {\r
202                         transforms_.clear();\r
203                 }, high_priority);\r
204         }\r
205                 \r
206         void load(int index, const safe_ptr<frame_producer>& producer, bool preview, int auto_play_delta)\r
207         {\r
208                 executor_.begin_invoke([=]\r
209                 {\r
210                         layers_[index].load(producer, preview, auto_play_delta);\r
211                 }, high_priority);\r
212         }\r
213 \r
214         void pause(int index)\r
215         {               \r
216                 executor_.begin_invoke([=]\r
217                 {\r
218                         layers_[index].pause();\r
219                 }, high_priority);\r
220         }\r
221 \r
222         void play(int index)\r
223         {               \r
224                 executor_.begin_invoke([=]\r
225                 {\r
226                         layers_[index].play();\r
227                 }, high_priority);\r
228         }\r
229 \r
230         void stop(int index)\r
231         {               \r
232                 executor_.begin_invoke([=]\r
233                 {\r
234                         layers_[index].stop();\r
235                 }, high_priority);\r
236         }\r
237 \r
238         void clear(int index)\r
239         {\r
240                 executor_.begin_invoke([=]\r
241                 {\r
242                         layers_.erase(index);\r
243                 }, high_priority);\r
244         }\r
245                 \r
246         void clear()\r
247         {\r
248                 executor_.begin_invoke([=]\r
249                 {\r
250                         layers_.clear();\r
251                 }, high_priority);\r
252         }       \r
253         \r
254         boost::unique_future<std::wstring> call(int index, bool foreground, const std::wstring& param)\r
255         {\r
256                 return std::move(*executor_.invoke([=]\r
257                 {\r
258                         return std::make_shared<boost::unique_future<std::wstring>>(std::move(layers_[index].call(foreground, param)));\r
259                 }, high_priority));\r
260         }\r
261         \r
262         void swap_layers(const safe_ptr<stage>& other)\r
263         {\r
264                 if(other->impl_.get() == this)\r
265                         return;\r
266                 \r
267                 auto func = [=]\r
268                 {\r
269                         std::swap(layers_, other->impl_->layers_);\r
270                 };              \r
271                 executor_.begin_invoke([=]\r
272                 {\r
273                         other->impl_->executor_.invoke(func, high_priority);\r
274                 }, high_priority);\r
275         }\r
276 \r
277         void swap_layer(int index, size_t other_index)\r
278         {\r
279                 executor_.begin_invoke([=]\r
280                 {\r
281                         std::swap(layers_[index], layers_[other_index]);\r
282                 }, high_priority);\r
283         }\r
284 \r
285         void swap_layer(int index, size_t other_index, const safe_ptr<stage>& other)\r
286         {\r
287                 if(other->impl_.get() == this)\r
288                         swap_layer(index, other_index);\r
289                 else\r
290                 {\r
291                         auto func = [=]\r
292                         {\r
293                                 std::swap(layers_[index], other->impl_->layers_[other_index]);\r
294                         };              \r
295                         executor_.begin_invoke([=]\r
296                         {\r
297                                 other->impl_->executor_.invoke(func, high_priority);\r
298                         }, high_priority);\r
299                 }\r
300         }\r
301                 \r
302         boost::unique_future<safe_ptr<frame_producer>> foreground(int index)\r
303         {\r
304                 return executor_.begin_invoke([=]\r
305                 {\r
306                         return layers_[index].foreground();\r
307                 }, high_priority);\r
308         }\r
309         \r
310         boost::unique_future<safe_ptr<frame_producer>> background(int index)\r
311         {\r
312                 return executor_.begin_invoke([=]\r
313                 {\r
314                         return layers_[index].background();\r
315                 }, high_priority);\r
316         }\r
317         \r
318         void set_video_format_desc(const video_format_desc& format_desc)\r
319         {\r
320                 executor_.begin_invoke([=]\r
321                 {\r
322                         format_desc_ = format_desc;\r
323                 }, high_priority);\r
324         }\r
325 \r
326         boost::unique_future<boost::property_tree::wptree> info()\r
327         {\r
328                 return std::move(executor_.begin_invoke([&]() -> boost::property_tree::wptree\r
329                 {\r
330                         boost::property_tree::wptree info;\r
331                         BOOST_FOREACH(auto& layer, layers_)                     \r
332                                 info.add_child(L"layers.layer", layer.second.info())\r
333                                         .add(L"index", layer.first);    \r
334                         return info;\r
335                 }, high_priority));\r
336         }\r
337 \r
338         boost::unique_future<boost::property_tree::wptree> info(int index)\r
339         {\r
340                 return std::move(executor_.begin_invoke([&]() -> boost::property_tree::wptree\r
341                 {\r
342                         return layers_[index].info();\r
343                 }, high_priority));\r
344         }\r
345 };\r
346 \r
347 stage::stage(const safe_ptr<diagnostics::graph>& graph, const safe_ptr<target_t>& target, const video_format_desc& format_desc) : impl_(new implementation(graph, target, format_desc)){}\r
348 void stage::set_frame_transform(int index, const core::frame_transform& transform, unsigned int mix_duration, const std::wstring& tween){impl_->set_transform(index, transform, mix_duration, tween);}\r
349 void stage::apply_frame_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
350 void stage::clear_transforms(int index){impl_->clear_transforms(index);}\r
351 void stage::clear_transforms(){impl_->clear_transforms();}\r
352 void stage::spawn_token(){impl_->spawn_token();}\r
353 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
354 void stage::pause(int index){impl_->pause(index);}\r
355 void stage::play(int index){impl_->play(index);}\r
356 void stage::stop(int index){impl_->stop(index);}\r
357 void stage::clear(int index){impl_->clear(index);}\r
358 void stage::clear(){impl_->clear();}\r
359 void stage::swap_layers(const safe_ptr<stage>& other){impl_->swap_layers(other);}\r
360 void stage::swap_layer(int index, size_t other_index){impl_->swap_layer(index, other_index);}\r
361 void stage::swap_layer(int index, size_t other_index, const safe_ptr<stage>& other){impl_->swap_layer(index, other_index, other);}\r
362 boost::unique_future<safe_ptr<frame_producer>> stage::foreground(int index) {return impl_->foreground(index);}\r
363 boost::unique_future<safe_ptr<frame_producer>> stage::background(int index) {return impl_->background(index);}\r
364 boost::unique_future<std::wstring> stage::call(int index, bool foreground, const std::wstring& param){return impl_->call(index, foreground, param);}\r
365 void stage::set_video_format_desc(const video_format_desc& format_desc){impl_->set_video_format_desc(format_desc);}\r
366 boost::unique_future<boost::property_tree::wptree> stage::info() const{return impl_->info();}\r
367 boost::unique_future<boost::property_tree::wptree> stage::info(int index) const{return impl_->info(index);}\r
368 }}