]> git.sesse.net Git - casparcg/blob - core/producer/scene/scene_producer.cpp
mouse hover position exposed as mouse_x and mouse_y variable in scene_producers for...
[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 <common/prec_timer.h>
26
27 #include <boost/algorithm/string/split.hpp>
28 #include <boost/algorithm/string.hpp>
29
30 #include "scene_producer.h"
31
32 #include "../../frame/draw_frame.h"
33 #include "../../interaction/interaction_aggregator.h"
34 #include "../text/text_producer.h"
35
36 namespace caspar { namespace core { namespace scene {
37
38 layer::layer(const std::wstring& name, const spl::shared_ptr<frame_producer>& producer)
39         : name(name), producer(producer)
40 {
41         crop.lower_right.x.bind(producer.get()->pixel_constraints().width);
42         crop.lower_right.y.bind(producer.get()->pixel_constraints().height);
43         perspective.upper_right.x.bind(producer.get()->pixel_constraints().width);
44         perspective.lower_right.x.bind(producer.get()->pixel_constraints().width);
45         perspective.lower_right.y.bind(producer.get()->pixel_constraints().height);
46         perspective.lower_left.y.bind(producer.get()->pixel_constraints().height);
47 }
48
49 adjustments::adjustments()
50         : opacity(1.0)
51 {
52 }
53
54 struct timeline
55 {
56         std::map<int64_t, keyframe> keyframes;
57
58         void on_frame(int64_t frame)
59         {
60                 auto before = --keyframes.upper_bound(frame);
61                 bool found_before = before != keyframes.end() && before->first < frame;
62                 auto after = keyframes.upper_bound(frame);
63                 bool found_after = after != keyframes.end() && after->first > frame;
64                 auto exact_frame = keyframes.find(frame);
65                 bool found_exact_frame = exact_frame != keyframes.end();
66
67                 if (found_exact_frame)
68                 {
69                         exact_frame->second.on_destination_frame();
70
71                         auto next_frame = ++exact_frame;
72
73                         if (next_frame != keyframes.end() && next_frame->second.on_start_animate)
74                                 next_frame->second.on_start_animate();
75                 }
76                 else if (found_after)
77                 {
78                         int64_t start_frame = 0;
79
80                         if (found_before)
81                         {
82                                 start_frame = before->first;
83                         }
84
85                         if (after->second.on_start_animate && frame == 0)
86                                 after->second.on_start_animate();
87                         else if (after->second.on_animate_to)
88                                 after->second.on_animate_to(start_frame, frame);
89                 }
90         }
91 };
92
93 mark_action get_mark_action(const std::wstring& name)
94 {
95         if (name == L"start")
96                 return mark_action::start;
97         else if (name == L"stop")
98                 return mark_action::stop;
99         else if (name == L"jump_to")
100                 return mark_action::jump_to;
101         else if (name == L"remove")
102                 return mark_action::remove;
103         else
104                 CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"Invalid mark_action " + name));
105 }
106
107 struct marker
108 {
109         mark_action             action;
110         std::wstring    label_argument;
111
112         marker(mark_action action, const std::wstring& label_argument)
113                 : action(action)
114                 , label_argument(label_argument)
115         {
116         }
117 };
118
119 struct scene_producer::impl
120 {
121         std::wstring                                                                                    producer_name_;
122         constraints                                                                                             pixel_constraints_;
123         video_format_desc                                                                               format_desc_;
124         std::list<layer>                                                                                layers_;
125         interaction_aggregator                                                                  aggregator_;
126         binding<int64_t>                                                                                frame_number_;
127         binding<double>                                                                                 speed_;
128         mutable tbb::atomic<int64_t>                                                    m_x_;
129         mutable tbb::atomic<int64_t>                                                    m_y_;
130         binding<int64_t>                                                                                mouse_x_;
131         binding<int64_t>                                                                                mouse_y_;
132         double                                                                                                  frame_fraction_         = 0.0;
133         std::map<void*, timeline>                                                               timelines_;
134         std::map<std::wstring, std::shared_ptr<core::variable>> variables_;
135         std::vector<std::wstring>                                                               variable_names_;
136         std::multimap<int64_t, marker>                                                  markers_by_frame_;
137         monitor::subject                                                                                monitor_subject_;
138         bool                                                                                                    paused_                         = true;
139         bool                                                                                                    removed_                        = false;
140         bool                                                                                                    going_to_mark_          = false;
141
142         impl(std::wstring producer_name, int width, int height, const video_format_desc& format_desc)
143                 : producer_name_(std::move(producer_name))
144                 , pixel_constraints_(width, height)
145                 , format_desc_(format_desc)
146                 , aggregator_([=] (double x, double y) { return collission_detect(x, y); })
147         {
148                 auto speed_variable = std::make_shared<core::variable_impl<double>>(L"1.0", true, 1.0);
149                 store_variable(L"scene_speed", speed_variable);
150                 speed_ = speed_variable->value();
151                 
152                 auto frame_variable = std::make_shared<core::variable_impl<int64_t>>(L"-1", true, -1);
153                 store_variable(L"frame", frame_variable);
154                 frame_number_ = frame_variable->value();
155
156                 auto mouse_x_variable = std::make_shared<core::variable_impl<int64_t>>(L"0", false, 0);
157                 auto mouse_y_variable = std::make_shared<core::variable_impl<int64_t>>(L"0", false, 0);
158                 store_variable(L"mouse_x", mouse_x_variable);
159                 store_variable(L"mouse_y", mouse_y_variable);
160                 mouse_x_ = mouse_x_variable->value();
161                 mouse_y_ = mouse_y_variable->value();
162                 m_x_ = 0;
163                 m_y_ = 0;
164         }
165
166         layer& create_layer(
167                         const spl::shared_ptr<frame_producer>& producer, int x, int y, const std::wstring& name)
168         {
169                 layer layer(name, producer);
170
171                 layer.position.x.set(x);
172                 layer.position.y.set(y);
173
174                 layers_.push_back(layer);
175
176                 return layers_.back();
177         }
178
179         void reverse_layers() {
180                 layers_.reverse();
181         }
182
183         void store_keyframe(void* timeline_identity, const keyframe& k)
184         {
185                 timelines_[timeline_identity].keyframes.insert(std::make_pair(k.destination_frame, k));
186         }
187
188         void store_variable(
189                         const std::wstring& name, const std::shared_ptr<core::variable>& var)
190         {
191                 variables_.insert(std::make_pair(name, var));
192                 variable_names_.push_back(name);
193         }
194
195         void add_mark(int64_t frame, mark_action action, const std::wstring& label)
196         {
197                 markers_by_frame_.insert(std::make_pair(frame, marker(action, label)));
198         }
199
200         core::variable& get_variable(const std::wstring& name)
201         {
202                 auto found = variables_.find(name);
203
204                 if (found == variables_.end())
205                         CASPAR_THROW_EXCEPTION(user_error() << msg_info(name + L" not found in scene"));
206
207                 return *found->second;
208         }
209
210         const std::vector<std::wstring>& get_variables() const
211         {
212                 return variable_names_;
213         }
214
215         binding<int64_t> frame()
216         {
217                 return frame_number_;
218         }
219
220         frame_transform get_transform(const layer& layer) const
221         {
222                 frame_transform transform;
223
224                 auto& anchor            = transform.image_transform.anchor;
225                 auto& pos                       = transform.image_transform.fill_translation;
226                 auto& scale                     = transform.image_transform.fill_scale;
227                 auto& angle                     = transform.image_transform.angle;
228                 auto& crop                      = transform.image_transform.crop;
229                 auto& pers                      = transform.image_transform.perspective;
230
231                 anchor[0]       = layer.anchor.x.get()                                                                          / layer.producer.get()->pixel_constraints().width.get();
232                 anchor[1]       = layer.anchor.y.get()                                                                          / layer.producer.get()->pixel_constraints().height.get();
233                 pos[0]          = layer.position.x.get()                                                                        / pixel_constraints_.width.get();
234                 pos[1]          = layer.position.y.get()                                                                        / pixel_constraints_.height.get();
235                 scale[0]        = layer.producer.get()->pixel_constraints().width.get()         / pixel_constraints_.width.get();
236                 scale[1]        = layer.producer.get()->pixel_constraints().height.get()        / pixel_constraints_.height.get();
237                 crop.ul[0]      = layer.crop.upper_left.x.get()                                                         / layer.producer.get()->pixel_constraints().width.get();
238                 crop.ul[1]      = layer.crop.upper_left.y.get()                                                         / layer.producer.get()->pixel_constraints().height.get();
239                 crop.lr[0]      = layer.crop.lower_right.x.get()                                                        / layer.producer.get()->pixel_constraints().width.get();
240                 crop.lr[1]      = layer.crop.lower_right.y.get()                                                        / layer.producer.get()->pixel_constraints().height.get();
241                 pers.ul[0]      = layer.perspective.upper_left.x.get()                                          / layer.producer.get()->pixel_constraints().width.get();
242                 pers.ul[1]      = layer.perspective.upper_left.y.get()                                          / layer.producer.get()->pixel_constraints().height.get();
243                 pers.ur[0]      = layer.perspective.upper_right.x.get()                                         / layer.producer.get()->pixel_constraints().width.get();
244                 pers.ur[1]      = layer.perspective.upper_right.y.get()                                         / layer.producer.get()->pixel_constraints().height.get();
245                 pers.lr[0]      = layer.perspective.lower_right.x.get()                                         / layer.producer.get()->pixel_constraints().width.get();
246                 pers.lr[1]      = layer.perspective.lower_right.y.get()                                         / layer.producer.get()->pixel_constraints().height.get();
247                 pers.ll[0]      = layer.perspective.lower_left.x.get()                                          / layer.producer.get()->pixel_constraints().width.get();
248                 pers.ll[1]      = layer.perspective.lower_left.y.get()                                          / layer.producer.get()->pixel_constraints().height.get();
249
250                 static const double PI = 3.141592653589793;
251
252                 angle           = layer.rotation.get() * PI / 180.0;
253
254                 transform.image_transform.opacity                       = layer.adjustments.opacity.get();
255                 transform.image_transform.is_key                        = layer.is_key.get();
256                 transform.image_transform.use_mipmap            = layer.use_mipmap.get();
257                 transform.image_transform.blend_mode            = layer.blend_mode.get();
258                 transform.image_transform.chroma.key            = layer.chroma_key.key.get();
259                 transform.image_transform.chroma.threshold      = layer.chroma_key.threshold.get();
260                 transform.image_transform.chroma.softness       = layer.chroma_key.softness.get();
261                 transform.image_transform.chroma.spill          = layer.chroma_key.spill.get();
262
263                 // Mark as sublayer, so it will be composited separately by the mixer.
264                 transform.image_transform.layer_depth = 1;
265
266                 return transform;
267         }
268
269         boost::optional<std::pair<int64_t, marker>> find_first_stop_or_jump_or_remove(int64_t start_frame, int64_t end_frame)
270         {
271                 auto lower = markers_by_frame_.lower_bound(start_frame);
272                 auto upper = markers_by_frame_.upper_bound(end_frame);
273
274                 if (lower == markers_by_frame_.end())
275                         return boost::none;
276
277                 for (auto iter = lower; iter != upper; ++iter)
278                 {
279                         auto action = iter->second.action;
280
281                         if (action == mark_action::stop || action == mark_action::jump_to || action == mark_action::remove)
282                                 return std::make_pair(iter->first, iter->second);
283                 }
284
285                 return boost::none;
286         }
287
288         boost::optional<std::pair<int64_t, marker>> find_first_start(int64_t start_frame)
289         {
290                 auto lower = markers_by_frame_.lower_bound(start_frame);
291
292                 if (lower == markers_by_frame_.end())
293                         return boost::none;
294
295                 for (auto iter = lower; iter != markers_by_frame_.end(); ++iter)
296                 {
297                         auto action = iter->second.action;
298
299                         if (action == mark_action::start)
300                                 return std::make_pair(iter->first, iter->second);
301                 }
302
303                 return boost::none;
304         }
305
306         draw_frame render_frame()
307         {
308                 if (format_desc_.field_count == 1)
309                         return render_progressive_frame();
310                 else
311                 {
312                         prec_timer timer;
313                         timer.tick_millis(0);
314                         
315                         auto field1 = render_progressive_frame();
316                         
317                         timer.tick(0.5 / format_desc_.fps);
318
319                         auto field2 = render_progressive_frame();
320
321                         return draw_frame::interlace(field1, field2, format_desc_.field_mode);
322                 }
323         }
324
325         void advance()
326         {
327                 frame_fraction_ += speed_.get();
328
329                 if (std::abs(frame_fraction_) >= 1.0)
330                 {
331                         int64_t delta = static_cast<int64_t>(frame_fraction_);
332                         auto previous_frame = frame_number_.get();
333                         auto next_frame = frame_number_.get() + delta;
334                         auto marker = find_first_stop_or_jump_or_remove(previous_frame + 1, next_frame);
335
336                         if (marker && marker->second.action == mark_action::remove)
337                         {
338                                 remove();
339                         }
340                         if (marker && !going_to_mark_)
341                         {
342                                 if (marker->second.action == mark_action::stop)
343                                 {
344                                         frame_number_.set(marker->first);
345                                         frame_fraction_ = 0.0;
346                                         paused_ = true;
347                                 }
348                                 else if (marker->second.action == mark_action::jump_to)
349                                 {
350                                         go_to_marker(marker->second.label_argument, 0);
351                                 }
352                         }
353                         else
354                         {
355                                 frame_number_.set(next_frame);
356                                 frame_fraction_ -= delta;
357                         }
358
359                         going_to_mark_ = false;
360                 }
361         }
362
363         draw_frame render_progressive_frame()
364         {
365                 if (removed_)
366                         return draw_frame::empty();
367
368                 if (!paused_)
369                         advance();
370
371                 for (auto& timeline : timelines_)
372                         timeline.second.on_frame(frame_number_.get());
373
374                 mouse_x_.set(m_x_);
375                 mouse_y_.set(m_y_);
376
377                 std::vector<draw_frame> frames;
378
379                 for (auto& layer : layers_)
380                 {
381                         if (layer.hidden.get())
382                                 continue;
383
384                         draw_frame frame(layer.producer.get()->receive());
385                         frame.transform() = get_transform(layer);
386                         frames.push_back(frame);
387                 }
388
389                 return draw_frame(frames);
390         }
391
392         void on_interaction(const interaction_event::ptr& event)
393         {
394                 aggregator_.translate_and_send(event);
395         }
396
397         bool collides(double x, double y) const
398         {
399                 m_x_ = static_cast<int64_t>(x * pixel_constraints_.width.get());
400                 m_y_ = static_cast<int64_t>(y * pixel_constraints_.height.get());
401
402                 return static_cast<bool>((collission_detect(x, y)));
403         }
404
405         boost::optional<interaction_target> collission_detect(double x, double y) const
406         {
407                 for (auto& layer : layers_ | boost::adaptors::reversed)
408                 {
409                         if (layer.hidden.get())
410                                 continue;
411
412                         auto transform = get_transform(layer);
413                         auto translated = translate(x, y, transform);
414
415                         if (translated.first >= 0.0
416                                 && translated.first <= 1.0
417                                 && translated.second >= 0.0
418                                 && translated.second <= 1.0
419                                 && layer.producer.get()->collides(translated.first, translated.second))
420                         {
421                                 return std::make_pair(transform, static_cast<interaction_sink*>(layer.producer.get().get()));
422                         }
423                 }
424
425                 return boost::optional<interaction_target>();
426         }
427
428         std::future<std::wstring> call(const std::vector<std::wstring>& params) 
429         {
430                 if (!params.empty() && boost::ends_with(params.at(0), L"()"))
431                         return make_ready_future(handle_call(params));
432                 else
433                         return make_ready_future(handle_variable_set(params));
434         }
435
436         std::wstring handle_variable_set(const std::vector<std::wstring>& params)
437         {
438                 for (int i = 0; i + 1 < params.size(); i += 2)
439                 {
440                         auto found = variables_.find(boost::to_lower_copy(params.at(i)));
441
442                         if (found != variables_.end() && found->second->is_public())
443                                 found->second->from_string(params.at(i + 1));
444                 }
445
446                 return L"";
447         }
448
449         std::wstring handle_call(const std::vector<std::wstring>& params)
450         {
451                 auto call = params.at(0);
452
453                 if (call == L"play()")
454                         go_to_marker(params.at(1), -1);
455                 else if (call == L"remove()")
456                         remove();
457                 else if (call == L"next()")
458                         next();
459                 else
460                         CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"Unknown call " + call));
461
462                 return L"";
463         }
464
465         void remove()
466         {
467                 removed_ = true;
468                 layers_.clear();
469         }
470
471         void next()
472         {
473                 auto marker = find_first_start(frame_number_.get() + 1);
474
475                 if (marker)
476                 {
477                         frame_number_.set(marker->first - 1);
478                         frame_fraction_ = 0.0;
479                         paused_ = false;
480                         going_to_mark_ = true;
481                 }
482                 else
483                 {
484                         remove();
485                 }
486         }
487
488         void go_to_marker(const std::wstring& marker_name, int64_t offset)
489         {
490                 for (auto& marker : markers_by_frame_)
491                 {
492                         if (marker.second.label_argument == marker_name && marker.second.action == mark_action::start)
493                         {
494                                 frame_number_.set(marker.first + offset);
495                                 frame_fraction_ = 0.0;
496                                 paused_ = false;
497                                 going_to_mark_ = true;
498
499                                 return;
500                         }
501                 }
502
503                 if (marker_name == L"intro")
504                 {
505                         frame_number_.set(offset);
506                         frame_fraction_ = 0.0;
507                         paused_ = false;
508                         going_to_mark_ = true;
509                 }
510                 else if (marker_name == L"outro")
511                 {
512                         remove();
513                 }
514                 else
515                         CASPAR_LOG(info) << print() << L" no marker called " << marker_name << " found";
516         }
517
518         std::wstring print() const
519         {
520                 return L"scene[type=" + name() + L"]";
521         }
522
523         std::wstring name() const
524         {
525                 return producer_name_;
526         }
527         
528         boost::property_tree::wptree info() const
529         {
530                 boost::property_tree::wptree info;
531                 info.add(L"type", L"scene");
532                 info.add(L"producer-name", name());
533                 info.add(L"frame-number", frame_number_.get());
534
535                 for (auto& var : variables_)
536                 {
537                         boost::property_tree::wptree variable_info;
538
539                         variable_info.add(L"name", var.first);
540                         variable_info.add(L"public", var.second->is_public());
541                         variable_info.add(L"value", var.second->to_string());
542
543                         info.add_child(L"variables.variable", variable_info);
544                 }
545
546                 for (auto& layer : layers_)
547                 {
548                         boost::property_tree::wptree layer_info;
549
550                         layer_info.add(L"name", layer.name.get());
551                         layer_info.add_child(L"producer", layer.producer.get()->info());
552                         layer_info.add(L"x", layer.position.x.get());
553                         layer_info.add(L"y", layer.position.y.get());
554                         layer_info.add(L"width", layer.producer.get()->pixel_constraints().width.get());
555                         layer_info.add(L"height", layer.producer.get()->pixel_constraints().height.get());
556
557                         info.add_child(L"layers.layer", layer_info);
558                 }
559
560                 return info;
561         }
562
563         monitor::subject& monitor_output()
564         {
565                 return monitor_subject_;
566         }
567 };
568
569 scene_producer::scene_producer(std::wstring producer_name, int width, int height, const video_format_desc& format_desc)
570         : impl_(new impl(std::move(producer_name), width, height, format_desc))
571 {
572 }
573
574 scene_producer::~scene_producer()
575 {
576 }
577
578 layer& scene_producer::create_layer(
579                 const spl::shared_ptr<frame_producer>& producer, int x, int y, const std::wstring& name)
580 {
581         return impl_->create_layer(producer, x, y, name);
582 }
583
584 layer& scene_producer::create_layer(
585                 const spl::shared_ptr<frame_producer>& producer, const std::wstring& name)
586 {
587         return impl_->create_layer(producer, 0, 0, name);
588 }
589
590 void scene_producer::reverse_layers() {
591         impl_->reverse_layers();
592 }
593
594 binding<int64_t> scene_producer::frame()
595 {
596         return impl_->frame();
597 }
598
599 draw_frame scene_producer::receive_impl()
600 {
601         return impl_->render_frame();
602 }
603
604 constraints& scene_producer::pixel_constraints() { return impl_->pixel_constraints_; }
605
606 void scene_producer::on_interaction(const interaction_event::ptr& event)
607 {
608         impl_->on_interaction(event);
609 }
610
611 bool scene_producer::collides(double x, double y) const
612 {
613         return impl_->collides(x, y);
614 }
615
616 std::wstring scene_producer::print() const
617 {
618         return impl_->print();
619 }
620
621 std::wstring scene_producer::name() const
622 {
623         return impl_->name();
624 }
625
626 boost::property_tree::wptree scene_producer::info() const
627 {
628         return impl_->info();
629 }
630
631 std::future<std::wstring> scene_producer::call(const std::vector<std::wstring>& params) 
632 {
633         return impl_->call(params);
634 }
635
636 monitor::subject& scene_producer::monitor_output()
637 {
638         return impl_->monitor_output();
639 }
640
641 void scene_producer::store_keyframe(void* timeline_identity, const keyframe& k)
642 {
643         impl_->store_keyframe(timeline_identity, k);
644 }
645
646 void scene_producer::store_variable(
647                 const std::wstring& name, const std::shared_ptr<core::variable>& var)
648 {
649         impl_->store_variable(name, var);
650 }
651
652 void scene_producer::add_mark(int64_t frame, mark_action action, const std::wstring& label)
653 {
654         impl_->add_mark(frame, action, label);
655 }
656
657 core::variable& scene_producer::get_variable(const std::wstring& name)
658 {
659         return impl_->get_variable(name);
660 }
661
662 const std::vector<std::wstring>& scene_producer::get_variables() const
663 {
664         return impl_->get_variables();
665 }
666
667 }}}