]> git.sesse.net Git - casparcg/blob - core/producer/scene/scene_producer.cpp
* Merged MIXER CROP, MIXER ANCHOR, MIXER ROTATION and MIXER PERSPECTIVE from 2.0
[casparcg] / core / producer / scene / scene_producer.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Helge Norberg, helge.norberg@svt.se
20 */
21
22 #include "../../StdAfx.h"
23
24 #include <common/future.h>
25 #include <boost/algorithm/string/split.hpp>
26 #include <boost/algorithm/string.hpp>
27
28 #include "scene_producer.h"
29
30 #include "../../frame/draw_frame.h"
31 #include "../../interaction/interaction_aggregator.h"
32 #include "../text/text_producer.h"
33
34 namespace caspar { namespace core { namespace scene {
35
36 layer::layer(const std::wstring& name, const spl::shared_ptr<frame_producer>& producer)
37         : name(name), producer(producer)
38 {
39         crop.lower_right.x.bind(producer.get()->pixel_constraints().width);
40         crop.lower_right.y.bind(producer.get()->pixel_constraints().height);
41         perspective.upper_right.x.bind(producer.get()->pixel_constraints().width);
42         perspective.lower_right.x.bind(producer.get()->pixel_constraints().width);
43         perspective.lower_right.y.bind(producer.get()->pixel_constraints().height);
44         perspective.lower_left.y.bind(producer.get()->pixel_constraints().height);
45 }
46
47 adjustments::adjustments()
48         : opacity(1.0)
49 {
50 }
51
52 struct timeline
53 {
54         std::map<int64_t, keyframe> keyframes;
55
56         void on_frame(int64_t frame)
57         {
58                 auto before = --keyframes.upper_bound(frame);
59                 bool found_before = before != keyframes.end() && before->first < frame;
60                 auto after = keyframes.upper_bound(frame);
61                 bool found_after = after != keyframes.end() && after->first > frame;
62                 auto exact_frame = keyframes.find(frame);
63                 bool found_exact_frame = exact_frame != keyframes.end();
64
65                 if (found_exact_frame)
66                 {
67                         exact_frame->second.on_destination_frame();
68
69                         auto next_frame = ++exact_frame;
70
71                         if (next_frame != keyframes.end() && next_frame->second.on_start_animate)
72                                 next_frame->second.on_start_animate();
73                 }
74                 else if (found_after)
75                 {
76                         int64_t start_frame = 0;
77
78                         if (found_before)
79                         {
80                                 start_frame = before->first;
81                         }
82
83                         if (after->second.on_start_animate && frame == 0)
84                                 after->second.on_start_animate();
85                         else if (after->second.on_animate_to)
86                                 after->second.on_animate_to(start_frame, frame);
87                 }
88         }
89 };
90
91 struct scene_producer::impl
92 {
93         constraints pixel_constraints_;
94         std::list<layer> layers_;
95         interaction_aggregator aggregator_;
96         binding<int64_t> frame_number_;
97         binding<double> speed_;
98         double frame_fraction_;
99         std::map<void*, timeline> timelines_;
100         std::map<std::wstring, std::shared_ptr<core::variable>> variables_;
101         std::vector<std::wstring> variable_names_;
102         monitor::subject monitor_subject_;
103
104         impl(int width, int height)
105                 : pixel_constraints_(width, height)
106                 , aggregator_([=] (double x, double y) { return collission_detect(x, y); })
107                 , frame_fraction_(0)
108         {
109                 auto speed_variable = std::make_shared<core::variable_impl<double>>(L"1.0", true, 1.0);
110                 store_variable(L"scene_speed", speed_variable);
111                 speed_ = speed_variable->value();
112                 auto frame_variable = std::make_shared<core::variable_impl<int64_t>>(L"0", true, 0);
113                 store_variable(L"frame", frame_variable);
114                 frame_number_ = frame_variable->value();
115         }
116
117         layer& create_layer(
118                         const spl::shared_ptr<frame_producer>& producer, int x, int y, const std::wstring& name)
119         {
120                 layer layer(name, producer);
121
122                 layer.position.x.set(x);
123                 layer.position.y.set(y);
124
125                 layers_.push_back(layer);
126
127                 return layers_.back();
128         }
129
130         void store_keyframe(void* timeline_identity, const keyframe& k)
131         {
132                 timelines_[timeline_identity].keyframes.insert(std::make_pair(k.destination_frame, k));
133         }
134
135         void store_variable(
136                         const std::wstring& name, const std::shared_ptr<core::variable>& var)
137         {
138                 variables_.insert(std::make_pair(name, var));
139                 variable_names_.push_back(name);
140         }
141
142         core::variable& get_variable(const std::wstring& name)
143         {
144                 auto found = variables_.find(name);
145
146                 if (found == variables_.end())
147                         CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(name + L" not found in scene"));
148
149                 return *found->second;
150         }
151
152         const std::vector<std::wstring>& get_variables() const
153         {
154                 return variable_names_;
155         }
156
157         binding<int64_t> frame()
158         {
159                 return frame_number_;
160         }
161
162         frame_transform get_transform(const layer& layer) const
163         {
164                 frame_transform transform;
165
166                 auto& anchor            = transform.image_transform.anchor;
167                 auto& pos                       = transform.image_transform.fill_translation;
168                 auto& scale                     = transform.image_transform.fill_scale;
169                 auto& angle                     = transform.image_transform.angle;
170                 auto& crop                      = transform.image_transform.crop;
171                 auto& pers                      = transform.image_transform.perspective;
172
173                 anchor[0]       = layer.anchor.x.get()                                                                          / layer.producer.get()->pixel_constraints().width.get();
174                 anchor[1]       = layer.anchor.y.get()                                                                          / layer.producer.get()->pixel_constraints().height.get();
175                 pos[0]          = layer.position.x.get()                                                                        / pixel_constraints_.width.get();
176                 pos[1]          = layer.position.y.get()                                                                        / pixel_constraints_.height.get();
177                 scale[0]        = layer.producer.get()->pixel_constraints().width.get()         / pixel_constraints_.width.get();
178                 scale[1]        = layer.producer.get()->pixel_constraints().height.get()        / pixel_constraints_.height.get();
179                 crop.ul[0]      = layer.crop.upper_left.x.get()                                                         / layer.producer.get()->pixel_constraints().width.get();
180                 crop.ul[1]      = layer.crop.upper_left.y.get()                                                         / layer.producer.get()->pixel_constraints().height.get();
181                 crop.lr[0]      = layer.crop.lower_right.x.get()                                                        / layer.producer.get()->pixel_constraints().width.get();
182                 crop.lr[1]      = layer.crop.lower_right.y.get()                                                        / layer.producer.get()->pixel_constraints().height.get();
183                 pers.ul[0]      = layer.perspective.upper_left.x.get()                                          / layer.producer.get()->pixel_constraints().width.get();
184                 pers.ul[1]      = layer.perspective.upper_left.y.get()                                          / layer.producer.get()->pixel_constraints().height.get();
185                 pers.ur[0]      = layer.perspective.upper_right.x.get()                                         / layer.producer.get()->pixel_constraints().width.get();
186                 pers.ur[1]      = layer.perspective.upper_right.y.get()                                         / layer.producer.get()->pixel_constraints().height.get();
187                 pers.lr[0]      = layer.perspective.lower_right.x.get()                                         / layer.producer.get()->pixel_constraints().width.get();
188                 pers.lr[1]      = layer.perspective.lower_right.y.get()                                         / layer.producer.get()->pixel_constraints().height.get();
189                 pers.ll[0]      = layer.perspective.lower_left.x.get()                                          / layer.producer.get()->pixel_constraints().width.get();
190                 pers.ll[1]      = layer.perspective.lower_left.y.get()                                          / layer.producer.get()->pixel_constraints().height.get();
191
192                 static const double PI = 3.141592653589793;
193
194                 angle           = layer.rotation.get() * PI / 180.0;
195
196                 transform.image_transform.opacity = layer.adjustments.opacity.get();
197                 transform.image_transform.is_key = layer.is_key.get();
198
199                 return transform;
200         }
201
202         draw_frame render_frame()
203         {
204                 for (auto& timeline : timelines_)
205                         timeline.second.on_frame(frame_number_.get());
206
207                 std::vector<draw_frame> frames;
208
209                 for (auto& layer : layers_)
210                 {
211                         if (layer.hidden.get())
212                                 continue;
213
214                         draw_frame frame(layer.producer.get()->receive());
215                         frame.transform() = get_transform(layer);;
216                         frames.push_back(frame);
217                 }
218
219                 frame_fraction_ += speed_.get();
220
221                 if (std::abs(frame_fraction_) >= 1.0)
222                 {
223                         int64_t delta = static_cast<int64_t>(frame_fraction_);
224                         frame_number_.set(frame_number_.get() + delta);
225                         frame_fraction_ -= delta;
226                 }
227
228                 return draw_frame(frames);
229         }
230
231         void on_interaction(const interaction_event::ptr& event)
232         {
233                 aggregator_.translate_and_send(event);
234         }
235
236         bool collides(double x, double y) const
237         {
238                 return static_cast<bool>((collission_detect(x, y)));
239         }
240
241         boost::optional<interaction_target> collission_detect(double x, double y) const
242         {
243                 for (auto& layer : layers_ | boost::adaptors::reversed)
244                 {
245                         if (layer.hidden.get())
246                                 continue;
247
248                         auto transform = get_transform(layer);
249                         auto translated = translate(x, y, transform);
250
251                         if (translated.first >= 0.0
252                                 && translated.first <= 1.0
253                                 && translated.second >= 0.0
254                                 && translated.second <= 1.0
255                                 && layer.producer.get()->collides(translated.first, translated.second))
256                         {
257                                 return std::make_pair(transform, static_cast<interaction_sink*>(layer.producer.get().get()));
258                         }
259                 }
260
261                 return boost::optional<interaction_target>();
262         }
263
264         std::future<std::wstring> call(const std::vector<std::wstring>& params) 
265         {
266                 for (int i = 0; i + 1 < params.size(); i += 2)
267                 {
268                         auto found = variables_.find(boost::to_lower_copy(params[i]));
269
270                         if (found != variables_.end() && found->second->is_public())
271                                 found->second->from_string(params[i + 1]);
272                 }
273
274                 return make_ready_future(std::wstring(L""));
275         }
276
277         std::wstring print() const
278         {
279                 return L"scene[]";
280         }
281
282         std::wstring name() const
283         {
284                 return L"scene";
285         }
286         
287         boost::property_tree::wptree info() const
288         {
289                 boost::property_tree::wptree info;
290                 info.add(L"type", L"scene");
291                 return info;
292         }
293
294         monitor::subject& monitor_output()
295         {
296                 return monitor_subject_;
297         }
298 };
299
300 scene_producer::scene_producer(int width, int height)
301         : impl_(new impl(width, height))
302 {
303 }
304
305 scene_producer::~scene_producer()
306 {
307 }
308
309 layer& scene_producer::create_layer(
310                 const spl::shared_ptr<frame_producer>& producer, int x, int y, const std::wstring& name)
311 {
312         return impl_->create_layer(producer, x, y, name);
313 }
314
315 layer& scene_producer::create_layer(
316                 const spl::shared_ptr<frame_producer>& producer, const std::wstring& name)
317 {
318         return impl_->create_layer(producer, 0, 0, name);
319 }
320
321 binding<int64_t> scene_producer::frame()
322 {
323         return impl_->frame();
324 }
325
326 draw_frame scene_producer::receive_impl()
327 {
328         return impl_->render_frame();
329 }
330
331 constraints& scene_producer::pixel_constraints() { return impl_->pixel_constraints_; }
332
333 void scene_producer::on_interaction(const interaction_event::ptr& event)
334 {
335         impl_->on_interaction(event);
336 }
337
338 bool scene_producer::collides(double x, double y) const
339 {
340         return impl_->collides(x, y);
341 }
342
343 std::wstring scene_producer::print() const
344 {
345         return impl_->print();
346 }
347
348 std::wstring scene_producer::name() const
349 {
350         return impl_->name();
351 }
352
353 boost::property_tree::wptree scene_producer::info() const
354 {
355         return impl_->info();
356 }
357
358 std::future<std::wstring> scene_producer::call(const std::vector<std::wstring>& params) 
359 {
360         return impl_->call(params);
361 }
362
363 monitor::subject& scene_producer::monitor_output()
364 {
365         return impl_->monitor_output();
366 }
367
368 void scene_producer::store_keyframe(void* timeline_identity, const keyframe& k)
369 {
370         impl_->store_keyframe(timeline_identity, k);
371 }
372
373 void scene_producer::store_variable(
374                 const std::wstring& name, const std::shared_ptr<core::variable>& var)
375 {
376         impl_->store_variable(name, var);
377 }
378
379 core::variable& scene_producer::get_variable(const std::wstring& name)
380 {
381         return impl_->get_variable(name);
382 }
383
384 const std::vector<std::wstring>& scene_producer::get_variables() const
385 {
386         return impl_->get_variables();
387 }
388
389 spl::shared_ptr<core::frame_producer> create_dummy_scene_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const video_format_desc& format_desc, const std::vector<std::wstring>& params)
390 {
391         if (params.size() < 1 || !boost::iequals(params.at(0), L"[SCENE]"))
392                 return core::frame_producer::empty();
393
394         auto scene = spl::make_shared<scene_producer>(format_desc.width, format_desc.height);
395
396         text::text_info text_info;
397         text_info.font = L"ArialMT";
398         text_info.size = 62;
399         text_info.color.r = 1;
400         text_info.color.g = 1;
401         text_info.color.b = 1;
402         text_info.color.a = 0.5;
403         auto text_area = text_producer::create(frame_factory, 0, 0, L"a", text_info, 1280, 720, false);
404
405         auto text_width = text_area->pixel_constraints().width;
406         binding<double> padding(1.0);
407         binding<double> panel_width = padding + text_width + padding;
408         binding<double> panel_height = padding + text_area->pixel_constraints().height + padding;
409
410         auto subscription = panel_width.on_change([&]
411         {
412                 CASPAR_LOG(info) << "Panel width: " << panel_width.get();
413         });
414
415         padding.set(2);
416
417         auto create_param = [](std::wstring elem) -> std::vector<std::wstring>
418         {
419                 std::vector<std::wstring> result;
420                 result.push_back(elem);
421                 return result;
422         };
423
424         auto& car_layer = scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"car")), L"car");
425         car_layer.adjustments.opacity.set(0.5);
426         //car_layer.hidden = scene->frame() % 50 > 25 || !(scene->frame() < 1000);
427         std::vector<std::wstring> sub_params;
428         sub_params.push_back(L"[FREEHAND]");
429         sub_params.push_back(L"640");
430         sub_params.push_back(L"360");
431         scene->create_layer(create_producer(frame_factory, format_desc, sub_params), 10, 10, L"freehand");
432         sub_params.clear();
433
434         auto& color_layer = scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"RED")), 110, 10, L"color");
435         color_layer.producer.get()->pixel_constraints().width.set(1000);
436         color_layer.producer.get()->pixel_constraints().height.set(550);
437
438         //scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"SP")), 50, 50);
439
440         auto& upper_left = scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"scene/upper_left")), L"upper_left");
441         auto& upper_right = scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"scene/upper_right")), L"upper_right");
442         auto& lower_left = scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"scene/lower_left")), L"lower_left");
443         auto& lower_right = scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"scene/lower_right")), L"lower_right");
444         auto& text_layer = scene->create_layer(text_area, L"text_area");
445         upper_left.adjustments.opacity.bind(text_layer.adjustments.opacity);
446         upper_right.adjustments.opacity.bind(text_layer.adjustments.opacity);
447         lower_left.adjustments.opacity.bind(text_layer.adjustments.opacity);
448         lower_right.adjustments.opacity.bind(text_layer.adjustments.opacity);
449
450         /*
451         binding<double> panel_x = (scene->frame()
452                         .as<double>()
453                         .transformed([](double v) { return std::sin(v / 20.0); })
454                         * 20.0
455                         + 40.0)
456                         .transformed([](double v) { return std::floor(v); }); // snap to pixels instead of subpixels
457                         */
458         tweener tween(L"easeoutbounce");
459         binding<double> panel_x(0.0);
460
461         scene->add_keyframe(panel_x, -panel_width, 0);
462         scene->add_keyframe(panel_x, 300.0, 50, L"easeinoutsine");
463         scene->add_keyframe(panel_x, 300.0, 50 * 4);
464         scene->add_keyframe(panel_x, 1000.0, 50 * 5, L"easeinoutsine");
465         //panel_x = delay(panel_x, add_tween(panel_x, scene->frame(), 200.0, int64_t(50), L"linear"), scene->frame(), int64_t(100));
466         /*binding<double> panel_x = when(scene->frame() < 50)
467                 .then(scene->frame().as<double>().transformed([tween](double t) { return tween(t, 0.0, 200, 50); }))
468                 .otherwise(200.0);*/
469         //binding<double> panel_y = when(car_layer.hidden).then(500.0).otherwise(-panel_x + 300);
470         binding<double> panel_y(500.0);
471         scene->add_keyframe(panel_y, panel_y.get(), 50 * 4);
472         scene->add_keyframe(panel_y, 720.0, 50 * 5, L"easeinexpo");
473
474         scene->add_keyframe(text_layer.adjustments.opacity, 1.0, 100);
475         scene->add_keyframe(text_layer.adjustments.opacity, 0.0, 125, L"linear");
476         scene->add_keyframe(text_layer.adjustments.opacity, 1.0, 150, L"linear");
477
478         upper_left.position.x = panel_x;
479         upper_left.position.y = panel_y;
480         upper_right.position.x = upper_left.position.x + upper_left.producer.get()->pixel_constraints().width + panel_width;
481         upper_right.position.y = upper_left.position.y;
482         lower_left.position.x = upper_left.position.x;
483         lower_left.position.y = upper_left.position.y + upper_left.producer.get()->pixel_constraints().height + panel_height;
484         lower_right.position.x = upper_right.position.x;
485         lower_right.position.y = lower_left.position.y;
486         text_layer.position.x = upper_left.position.x + upper_left.producer.get()->pixel_constraints().width + padding;
487         text_layer.position.y = upper_left.position.y + upper_left.producer.get()->pixel_constraints().height + padding + text_area->current_bearing_y().as<double>();
488
489         text_area->text().bind(scene->create_variable<std::wstring>(L"text", true));
490
491         auto params2 = params;
492         params2.erase(params2.begin());
493
494         scene->call(params2);
495
496         return scene;
497 }
498
499 }}}