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