2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG (www.casparcg.com).
\r
6 * CasparCG is free software: you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation, either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * CasparCG is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
\r
19 * Author: Robert Nagy, ronag89@gmail.com
\r
22 #include "../../stdafx.h"
\r
24 #include "transition_producer.h"
\r
26 #include "../frame_producer.h"
\r
27 #include "../../frame/draw_frame.h"
\r
28 #include "../../frame/frame_transform.h"
\r
29 #include "../../monitor/monitor.h"
\r
31 #include <tbb/parallel_invoke.h>
\r
33 namespace caspar { namespace core {
\r
35 class transition_producer : public frame_producer
\r
37 monitor::basic_subject event_subject_;
\r
38 const field_mode mode_;
\r
41 const transition_info info_;
\r
43 spl::shared_ptr<draw_frame> last_frame_;
\r
45 spl::shared_ptr<frame_producer> dest_producer_;
\r
46 spl::shared_ptr<frame_producer> source_producer_;
\r
49 explicit transition_producer(const field_mode& mode, const spl::shared_ptr<frame_producer>& dest, const transition_info& info)
\r
53 , last_frame_(draw_frame::empty())
\r
54 , dest_producer_(dest)
\r
55 , source_producer_(frame_producer::empty())
\r
57 dest->subscribe(event_subject_);
\r
62 virtual void leading_producer(const spl::shared_ptr<frame_producer>& producer) override
\r
64 source_producer_ = producer;
\r
67 virtual spl::shared_ptr<draw_frame> receive(int flags) override
\r
69 if(current_frame_ >= info_.duration)
\r
70 return dest_producer_->receive(flags);
\r
74 event_subject_ << monitor::event("transition/frame") % current_frame_ % info_.duration
\r
75 << monitor::event("transition/type") % [&]() -> std::string
\r
77 switch(info_.type.value())
\r
79 case transition_type::mix: return "mix";
\r
80 case transition_type::wipe: return "wipe";
\r
81 case transition_type::slide: return "slide";
\r
82 case transition_type::push: return "push";
\r
83 case transition_type::cut: return "cut";
\r
84 default: return "n/a";
\r
88 auto dest = draw_frame::empty();
\r
89 auto source = draw_frame::empty();
\r
91 tbb::parallel_invoke(
\r
94 dest = dest_producer_->receive(flags);
\r
95 if(dest == core::draw_frame::late())
\r
96 dest = dest_producer_->last_frame();
\r
100 source = source_producer_->receive(flags);
\r
101 if(source == core::draw_frame::late())
\r
102 source = source_producer_->last_frame();
\r
105 return compose(dest, source);
\r
108 virtual spl::shared_ptr<draw_frame> last_frame() const override
\r
110 return dest_producer_->last_frame();
\r
113 virtual uint32_t nb_frames() const override
\r
115 return dest_producer_->nb_frames();
\r
118 virtual std::wstring print() const override
\r
120 return L"transition[" + source_producer_->print() + L"=>" + dest_producer_->print() + L"]";
\r
123 virtual std::wstring name() const override
\r
125 return L"transition";
\r
128 boost::property_tree::wptree info() const override
\r
130 return dest_producer_->info();
\r
133 // transition_producer
\r
135 spl::shared_ptr<draw_frame> compose(const spl::shared_ptr<draw_frame>& dest_frame, const spl::shared_ptr<draw_frame>& src_frame)
\r
137 if(info_.type == transition_type::cut)
\r
140 const double delta1 = info_.tweener(current_frame_*2-1, 0.0, 1.0, static_cast<double>(info_.duration*2));
\r
141 const double delta2 = info_.tweener(current_frame_*2, 0.0, 1.0, static_cast<double>(info_.duration*2));
\r
143 const double dir = info_.direction == transition_direction::from_left ? 1.0 : -1.0;
\r
145 // For interlaced transitions. Seperate fields into seperate frames which are transitioned accordingly.
\r
147 src_frame->get_frame_transform().volume = 1.0-delta2;
\r
148 auto s_frame1 = spl::make_shared<draw_frame>(src_frame);
\r
149 auto s_frame2 = spl::make_shared<draw_frame>(src_frame);
\r
151 dest_frame->get_frame_transform().volume = delta2;
\r
152 auto d_frame1 = spl::make_shared<draw_frame>(dest_frame);
\r
153 auto d_frame2 = spl::make_shared<draw_frame>(dest_frame);
\r
155 if(info_.type == transition_type::mix)
\r
157 d_frame1->get_frame_transform().opacity = delta1;
\r
158 d_frame1->get_frame_transform().is_mix = true;
\r
159 d_frame2->get_frame_transform().opacity = delta2;
\r
160 d_frame2->get_frame_transform().is_mix = true;
\r
162 s_frame1->get_frame_transform().opacity = 1.0-delta1;
\r
163 s_frame1->get_frame_transform().is_mix = true;
\r
164 s_frame2->get_frame_transform().opacity = 1.0-delta2;
\r
165 s_frame2->get_frame_transform().is_mix = true;
\r
167 if(info_.type == transition_type::slide)
\r
169 d_frame1->get_frame_transform().fill_translation[0] = (-1.0+delta1)*dir;
\r
170 d_frame2->get_frame_transform().fill_translation[0] = (-1.0+delta2)*dir;
\r
172 else if(info_.type == transition_type::push)
\r
174 d_frame1->get_frame_transform().fill_translation[0] = (-1.0+delta1)*dir;
\r
175 d_frame2->get_frame_transform().fill_translation[0] = (-1.0+delta2)*dir;
\r
177 s_frame1->get_frame_transform().fill_translation[0] = (0.0+delta1)*dir;
\r
178 s_frame2->get_frame_transform().fill_translation[0] = (0.0+delta2)*dir;
\r
180 else if(info_.type == transition_type::wipe)
\r
182 d_frame1->get_frame_transform().clip_scale[0] = delta1;
\r
183 d_frame2->get_frame_transform().clip_scale[0] = delta2;
\r
186 const auto s_frame = s_frame1->get_frame_transform() == s_frame2->get_frame_transform() ? s_frame2 : draw_frame::interlace(s_frame1, s_frame2, mode_);
\r
187 const auto d_frame = d_frame1->get_frame_transform() == d_frame2->get_frame_transform() ? d_frame2 : draw_frame::interlace(d_frame1, d_frame2, mode_);
\r
189 return draw_frame::over(s_frame, d_frame);
\r
192 virtual void subscribe(const monitor::observable::observer_ptr& o) override
\r
194 event_subject_.subscribe(o);
\r
197 virtual void unsubscribe(const monitor::observable::observer_ptr& o) override
\r
199 event_subject_.unsubscribe(o);
\r
203 spl::shared_ptr<frame_producer> create_transition_producer(const field_mode& mode, const spl::shared_ptr<frame_producer>& destination, const transition_info& info)
\r
205 return core::wrap_producer(spl::make_shared<transition_producer>(mode, destination, info));
\r