2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
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.
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.
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/>.
19 * Author: Helge Norberg, helge.norberg@svt.se
22 #include "../../StdAfx.h"
24 #include <common/future.h>
25 #include <common/prec_timer.h>
27 #include <boost/algorithm/string/split.hpp>
28 #include <boost/algorithm/string.hpp>
30 #include "scene_producer.h"
32 #include "../../frame/draw_frame.h"
33 #include "../../interaction/interaction_aggregator.h"
34 #include "../text/text_producer.h"
36 namespace caspar { namespace core { namespace scene {
38 layer::layer(const std::wstring& name, const spl::shared_ptr<frame_producer>& producer)
39 : name(name), producer(producer)
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);
49 adjustments::adjustments()
56 std::map<int64_t, keyframe> keyframes;
58 void on_frame(int64_t frame)
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();
67 if (found_exact_frame)
69 exact_frame->second.on_destination_frame();
71 auto next_frame = ++exact_frame;
73 if (next_frame != keyframes.end() && next_frame->second.on_start_animate)
74 next_frame->second.on_start_animate();
78 int64_t start_frame = 0;
82 start_frame = before->first;
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);
93 mark_action get_mark_action(const std::wstring& name)
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;
104 CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"Invalid mark_action " + name));
110 std::wstring label_argument;
112 marker(mark_action action, const std::wstring& label_argument)
114 , label_argument(label_argument)
119 struct scene_producer::impl
121 std::wstring producer_name_;
122 std::wstring template_name_;
123 constraints pixel_constraints_;
124 video_format_desc format_desc_;
125 std::list<layer> layers_;
126 interaction_aggregator aggregator_;
127 binding<double> frame_number_;
128 binding<int64_t> timeline_frame_number_;
129 binding<double> speed_;
130 mutable tbb::atomic<int64_t> m_x_;
131 mutable tbb::atomic<int64_t> m_y_;
132 binding<int64_t> mouse_x_;
133 binding<int64_t> mouse_y_;
134 double frame_fraction_ = 0.0;
135 std::map<void*, timeline> timelines_;
136 std::map<std::wstring, std::shared_ptr<core::variable>> variables_;
137 std::vector<std::wstring> variable_names_;
138 std::multimap<int64_t, marker> markers_by_frame_;
139 std::vector<std::shared_ptr<void>> task_subscriptions_;
140 monitor::subject monitor_subject_;
142 bool removed_ = false;
143 bool going_to_mark_ = false;
146 std::wstring producer_name,
147 std::wstring template_name,
150 const video_format_desc& format_desc)
151 : producer_name_(std::move(producer_name))
152 , template_name_(std::move(template_name))
153 , format_desc_(format_desc)
154 , aggregator_([=] (double x, double y) { return collission_detect(x, y); })
156 auto speed_variable = std::make_shared<core::variable_impl<double>>(L"1.0", true, 1.0);
157 store_variable(L"scene_speed", speed_variable);
158 speed_ = speed_variable->value();
160 auto frame_variable = std::make_shared<core::variable_impl<double>>(L"-1", true, -1);
161 store_variable(L"frame", frame_variable);
162 frame_number_ = frame_variable->value();
164 auto timeline_frame_variable = std::make_shared<core::variable_impl<int64_t>>(L"-1", false, -1);
165 store_variable(L"timeline_frame", timeline_frame_variable);
166 timeline_frame_number_ = timeline_frame_variable->value();
168 auto mouse_x_variable = std::make_shared<core::variable_impl<int64_t>>(L"0", false, 0);
169 auto mouse_y_variable = std::make_shared<core::variable_impl<int64_t>>(L"0", false, 0);
170 store_variable(L"mouse_x", mouse_x_variable);
171 store_variable(L"mouse_y", mouse_y_variable);
172 mouse_x_ = mouse_x_variable->value();
173 mouse_y_ = mouse_y_variable->value();
177 auto scene_width = std::make_shared<core::variable_impl<double>>(boost::lexical_cast<std::wstring>(width), false, width);
178 auto scene_height = std::make_shared<core::variable_impl<double>>(boost::lexical_cast<std::wstring>(height), false, height);
179 store_variable(L"scene_width", scene_width);
180 store_variable(L"scene_height", scene_height);
181 pixel_constraints_.width = scene_width->value();
182 pixel_constraints_.height = scene_height->value();
186 const spl::shared_ptr<frame_producer>& producer, int x, int y, const std::wstring& name)
188 layer layer(name, producer);
190 layer.position.x.set(x);
191 layer.position.y.set(y);
193 layers_.push_back(layer);
195 return layers_.back();
198 void reverse_layers() {
202 void store_keyframe(void* timeline_identity, const keyframe& k)
204 timelines_[timeline_identity].keyframes.insert(std::make_pair(k.destination_frame, k));
208 const std::wstring& name, const std::shared_ptr<core::variable>& var)
210 variables_.insert(std::make_pair(name, var));
211 variable_names_.push_back(name);
214 void add_mark(int64_t frame, mark_action action, const std::wstring& label)
216 markers_by_frame_.insert(std::make_pair(frame, marker(action, label)));
219 core::variable& get_variable(const std::wstring& name)
221 auto found = variables_.find(name);
223 if (found == variables_.end())
224 CASPAR_THROW_EXCEPTION(user_error() << msg_info(name + L" not found in scene"));
226 return *found->second;
229 const std::vector<std::wstring>& get_variables() const
231 return variable_names_;
234 binding<int64_t> timeline_frame()
236 return timeline_frame_number_;
239 frame_transform get_transform(const layer& layer) const
241 frame_transform transform;
243 auto& anchor = transform.image_transform.anchor;
244 auto& pos = transform.image_transform.fill_translation;
245 auto& scale = transform.image_transform.fill_scale;
246 auto& angle = transform.image_transform.angle;
247 auto& crop = transform.image_transform.crop;
248 auto& pers = transform.image_transform.perspective;
250 anchor[0] = layer.anchor.x.get() / layer.producer.get()->pixel_constraints().width.get();
251 anchor[1] = layer.anchor.y.get() / layer.producer.get()->pixel_constraints().height.get();
252 pos[0] = layer.position.x.get() / pixel_constraints_.width.get();
253 pos[1] = layer.position.y.get() / pixel_constraints_.height.get();
254 scale[0] = layer.producer.get()->pixel_constraints().width.get() / pixel_constraints_.width.get();
255 scale[1] = layer.producer.get()->pixel_constraints().height.get() / pixel_constraints_.height.get();
256 crop.ul[0] = layer.crop.upper_left.x.get() / layer.producer.get()->pixel_constraints().width.get();
257 crop.ul[1] = layer.crop.upper_left.y.get() / layer.producer.get()->pixel_constraints().height.get();
258 crop.lr[0] = layer.crop.lower_right.x.get() / layer.producer.get()->pixel_constraints().width.get();
259 crop.lr[1] = layer.crop.lower_right.y.get() / layer.producer.get()->pixel_constraints().height.get();
260 pers.ul[0] = layer.perspective.upper_left.x.get() / layer.producer.get()->pixel_constraints().width.get();
261 pers.ul[1] = layer.perspective.upper_left.y.get() / layer.producer.get()->pixel_constraints().height.get();
262 pers.ur[0] = layer.perspective.upper_right.x.get() / layer.producer.get()->pixel_constraints().width.get();
263 pers.ur[1] = layer.perspective.upper_right.y.get() / layer.producer.get()->pixel_constraints().height.get();
264 pers.lr[0] = layer.perspective.lower_right.x.get() / layer.producer.get()->pixel_constraints().width.get();
265 pers.lr[1] = layer.perspective.lower_right.y.get() / layer.producer.get()->pixel_constraints().height.get();
266 pers.ll[0] = layer.perspective.lower_left.x.get() / layer.producer.get()->pixel_constraints().width.get();
267 pers.ll[1] = layer.perspective.lower_left.y.get() / layer.producer.get()->pixel_constraints().height.get();
269 static const double PI = 3.141592653589793;
271 angle = layer.rotation.get() * PI / 180.0;
273 transform.image_transform.opacity = layer.adjustments.opacity.get();
274 transform.image_transform.is_key = layer.is_key.get();
275 transform.image_transform.use_mipmap = layer.use_mipmap.get();
276 transform.image_transform.blend_mode = layer.blend_mode.get();
277 transform.image_transform.chroma.enable = layer.chroma_key.enable.get();
278 transform.image_transform.chroma.target_hue = layer.chroma_key.target_hue.get();
279 transform.image_transform.chroma.hue_width = layer.chroma_key.hue_width.get();
280 transform.image_transform.chroma.min_saturation = layer.chroma_key.min_saturation.get();
281 transform.image_transform.chroma.min_brightness = layer.chroma_key.min_brightness.get();
282 transform.image_transform.chroma.softness = layer.chroma_key.softness.get();
283 transform.image_transform.chroma.spill = layer.chroma_key.spill.get();
284 transform.image_transform.chroma.spill_darken = layer.chroma_key.spill_darken.get();
286 // Mark as sublayer, so it will be composited separately by the mixer.
287 transform.image_transform.layer_depth = 1;
292 boost::optional<std::pair<int64_t, marker>> find_first_stop_or_jump_or_remove(int64_t start_frame, int64_t end_frame)
294 auto lower = markers_by_frame_.lower_bound(start_frame);
295 auto upper = markers_by_frame_.upper_bound(end_frame);
297 if (lower == markers_by_frame_.end())
300 for (auto iter = lower; iter != upper; ++iter)
302 auto action = iter->second.action;
304 if (action == mark_action::stop || action == mark_action::jump_to || action == mark_action::remove)
305 return std::make_pair(iter->first, iter->second);
311 boost::optional<std::pair<int64_t, marker>> find_first_start(int64_t start_frame)
313 auto lower = markers_by_frame_.lower_bound(start_frame);
315 if (lower == markers_by_frame_.end())
318 for (auto iter = lower; iter != markers_by_frame_.end(); ++iter)
320 auto action = iter->second.action;
322 if (action == mark_action::start)
323 return std::make_pair(iter->first, iter->second);
329 draw_frame render_frame()
331 if (format_desc_.field_count == 1)
332 return render_progressive_frame();
336 timer.tick_millis(0);
338 auto field1 = render_progressive_frame();
340 timer.tick(0.5 / format_desc_.fps);
342 auto field2 = render_progressive_frame();
344 return draw_frame::interlace(field1, field2, format_desc_.field_mode);
350 frame_fraction_ += speed_.get();
352 if (std::abs(frame_fraction_) >= 1.0)
354 int64_t delta = static_cast<int64_t>(frame_fraction_);
355 auto previous_frame = timeline_frame_number_.get();
356 auto next_frame = timeline_frame_number_.get() + delta;
357 auto marker = find_first_stop_or_jump_or_remove(previous_frame + 1, next_frame);
359 if (marker && marker->second.action == mark_action::remove)
363 if (marker && !going_to_mark_)
365 if (marker->second.action == mark_action::stop)
367 timeline_frame_number_.set(marker->first);
368 frame_fraction_ = 0.0;
371 else if (marker->second.action == mark_action::jump_to)
373 go_to_marker(marker->second.label_argument, 0);
378 timeline_frame_number_.set(next_frame);
379 frame_fraction_ -= delta;
382 going_to_mark_ = false;
386 draw_frame render_progressive_frame()
389 return draw_frame::empty();
397 frame_number_.set(frame_number_.get() + speed_.get());
399 for (auto& timeline : timelines_)
400 timeline.second.on_frame(timeline_frame_number_.get());
402 std::vector<draw_frame> frames;
404 for (auto& layer : layers_)
406 if (layer.hidden.get())
409 draw_frame frame(layer.producer.get()->receive());
410 frame.transform() = get_transform(layer);
411 frames.push_back(frame);
414 return draw_frame(frames);
417 void on_interaction(const interaction_event::ptr& event)
419 aggregator_.translate_and_send(event);
422 bool collides(double x, double y) const
424 m_x_ = static_cast<int64_t>(x * pixel_constraints_.width.get());
425 m_y_ = static_cast<int64_t>(y * pixel_constraints_.height.get());
427 return static_cast<bool>((collission_detect(x, y)));
430 boost::optional<interaction_target> collission_detect(double x, double y) const
432 for (auto& layer : layers_ | boost::adaptors::reversed)
434 if (layer.hidden.get())
437 auto transform = get_transform(layer);
438 auto translated = translate(x, y, transform);
440 if (translated.first >= 0.0
441 && translated.first <= 1.0
442 && translated.second >= 0.0
443 && translated.second <= 1.0
444 && layer.producer.get()->collides(translated.first, translated.second))
446 return std::make_pair(transform, static_cast<interaction_sink*>(layer.producer.get().get()));
450 return boost::optional<interaction_target>();
453 std::future<std::wstring> call(const std::vector<std::wstring>& params)
455 if (!params.empty() && boost::ends_with(params.at(0), L"()"))
456 return make_ready_future(handle_call(params));
458 return make_ready_future(handle_variable_set(params));
461 std::wstring handle_variable_set(const std::vector<std::wstring>& params)
463 for (int i = 0; i + 1 < params.size(); i += 2)
465 auto found = variables_.find(boost::to_lower_copy(params.at(i)));
467 if (found != variables_.end() && found->second->is_public())
468 found->second->from_string(params.at(i + 1));
474 std::wstring handle_call(const std::vector<std::wstring>& params)
476 auto call = params.at(0);
478 if (call == L"play()")
479 go_to_marker(params.at(1), -1);
480 else if (call == L"remove()")
482 else if (call == L"next()")
485 CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"Unknown call " + call));
498 auto marker = find_first_start(timeline_frame_number_.get() + 1);
502 timeline_frame_number_.set(marker->first - 1);
503 frame_fraction_ = 0.0;
505 going_to_mark_ = true;
513 void go_to_marker(const std::wstring& marker_name, int64_t offset)
515 for (auto& marker : markers_by_frame_)
517 if (marker.second.label_argument == marker_name && marker.second.action == mark_action::start)
519 timeline_frame_number_.set(marker.first + offset);
520 frame_fraction_ = 0.0;
522 going_to_mark_ = true;
528 if (marker_name == L"intro")
530 timeline_frame_number_.set(offset);
531 frame_fraction_ = 0.0;
533 going_to_mark_ = true;
535 else if (marker_name == L"outro")
540 CASPAR_LOG(info) << print() << L" no marker called " << marker_name << " found";
543 std::wstring print() const
545 return L"scene[type=" + name() + L" template=" + template_name_ + L"]";
548 std::wstring name() const
550 return producer_name_;
553 boost::property_tree::wptree info() const
555 boost::property_tree::wptree info;
556 info.add(L"type", L"scene");
557 info.add(L"producer-name", name());
558 info.add(L"template-name", template_name_);
559 info.add(L"frame-number", frame_number_.get());
560 info.add(L"timeline-frame-number", timeline_frame_number_.get());
562 for (auto& var : variables_)
564 boost::property_tree::wptree variable_info;
566 variable_info.add(L"name", var.first);
567 variable_info.add(L"public", var.second->is_public());
568 variable_info.add(L"value", var.second->to_string());
570 info.add_child(L"variables.variable", variable_info);
573 for (auto& layer : layers_)
575 boost::property_tree::wptree layer_info;
577 layer_info.add(L"name", layer.name.get());
578 layer_info.add_child(L"producer", layer.producer.get()->info());
579 layer_info.add(L"x", layer.position.x.get());
580 layer_info.add(L"y", layer.position.y.get());
581 layer_info.add(L"width", layer.producer.get()->pixel_constraints().width.get());
582 layer_info.add(L"height", layer.producer.get()->pixel_constraints().height.get());
584 info.add_child(L"layers.layer", layer_info);
590 monitor::subject& monitor_output()
592 return monitor_subject_;
596 scene_producer::scene_producer(std::wstring producer_name, std::wstring template_name, int width, int height, const video_format_desc& format_desc)
597 : impl_(new impl(std::move(producer_name), std::move(template_name), width, height, format_desc))
601 scene_producer::~scene_producer()
605 layer& scene_producer::create_layer(
606 const spl::shared_ptr<frame_producer>& producer, int x, int y, const std::wstring& name)
608 return impl_->create_layer(producer, x, y, name);
611 layer& scene_producer::create_layer(
612 const spl::shared_ptr<frame_producer>& producer, const std::wstring& name)
614 return impl_->create_layer(producer, 0, 0, name);
617 void scene_producer::reverse_layers() {
618 impl_->reverse_layers();
621 binding<int64_t> scene_producer::timeline_frame()
623 return impl_->timeline_frame();
626 draw_frame scene_producer::receive_impl()
628 return impl_->render_frame();
631 constraints& scene_producer::pixel_constraints() { return impl_->pixel_constraints_; }
633 void scene_producer::on_interaction(const interaction_event::ptr& event)
635 impl_->on_interaction(event);
638 bool scene_producer::collides(double x, double y) const
640 return impl_->collides(x, y);
643 std::wstring scene_producer::print() const
645 return impl_->print();
648 std::wstring scene_producer::name() const
650 return impl_->name();
653 boost::property_tree::wptree scene_producer::info() const
655 return impl_->info();
658 std::future<std::wstring> scene_producer::call(const std::vector<std::wstring>& params)
660 return impl_->call(params);
663 monitor::subject& scene_producer::monitor_output()
665 return impl_->monitor_output();
668 void scene_producer::store_keyframe(void* timeline_identity, const keyframe& k)
670 impl_->store_keyframe(timeline_identity, k);
673 void scene_producer::store_variable(
674 const std::wstring& name, const std::shared_ptr<core::variable>& var)
676 impl_->store_variable(name, var);
679 void scene_producer::add_mark(int64_t frame, mark_action action, const std::wstring& label)
681 impl_->add_mark(frame, action, label);
684 core::variable& scene_producer::get_variable(const std::wstring& name)
686 return impl_->get_variable(name);
689 const std::vector<std::wstring>& scene_producer::get_variables() const
691 return impl_->get_variables();