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: Robert Nagy, ronag89@gmail.com
22 #include "../../stdafx.h"
24 #include "transition_producer.h"
26 #include "../frame_producer.h"
27 #include "../../frame/draw_frame.h"
28 #include "../../frame/frame_transform.h"
29 #include "../../monitor/monitor.h"
31 #include <tbb/parallel_invoke.h>
35 namespace caspar { namespace core {
37 class transition_producer : public frame_producer_base
39 spl::shared_ptr<monitor::subject> monitor_subject_;
40 const field_mode mode_;
43 const transition_info info_;
45 spl::shared_ptr<frame_producer> dest_producer_;
46 spl::shared_ptr<frame_producer> source_producer_;
51 explicit transition_producer(const field_mode& mode, const spl::shared_ptr<frame_producer>& dest, const transition_info& info)
55 , dest_producer_(dest)
56 , source_producer_(frame_producer::empty())
59 dest->monitor_output().attach_parent(monitor_subject_);
61 CASPAR_LOG(info) << print() << L" Initialized";
66 void leading_producer(const spl::shared_ptr<frame_producer>& producer) override
68 source_producer_ = producer;
71 draw_frame receive_impl() override
73 if(current_frame_ >= info_.duration)
75 source_producer_ = core::frame_producer::empty();
76 return dest_producer_->receive();
81 auto dest = draw_frame::empty();
82 auto source = draw_frame::empty();
87 dest = dest_producer_->receive();
88 if(dest == core::draw_frame::late())
89 dest = dest_producer_->last_frame();
93 source = source_producer_->receive();
94 if(source == core::draw_frame::late())
95 source = source_producer_->last_frame();
98 *monitor_subject_ << monitor::message("/transition/frame") % current_frame_ % info_.duration
99 << monitor::message("/transition/type") % [&]() -> std::string
103 case transition_type::mix: return "mix";
104 case transition_type::wipe: return "wipe";
105 case transition_type::slide: return "slide";
106 case transition_type::push: return "push";
107 case transition_type::cut: return "cut";
108 default: return "n/a";
112 return compose(dest, source);
115 draw_frame last_frame() override
117 if(current_frame_ >= info_.duration)
118 return dest_producer_->last_frame();
120 return frame_producer_base::last_frame();
123 constraints& pixel_constraints() override
125 return dest_producer_->pixel_constraints();
128 uint32_t nb_frames() const override
130 return dest_producer_->nb_frames();
133 std::wstring print() const override
135 return L"transition[" + source_producer_->print() + L"=>" + dest_producer_->print() + L"]";
138 std::wstring name() const override
140 return L"transition";
143 boost::property_tree::wptree info() const override
145 return dest_producer_->info();
148 std::future<std::wstring> call(const std::vector<std::wstring>& params) override
150 return dest_producer_->call(params);
153 // transition_producer
155 draw_frame compose(draw_frame dest_frame, draw_frame src_frame) const
157 if(info_.type == transition_type::cut)
160 const double delta1 = info_.tweener(current_frame_*2-1, 0.0, 1.0, static_cast<double>(info_.duration*2));
161 const double delta2 = info_.tweener(current_frame_*2, 0.0, 1.0, static_cast<double>(info_.duration*2));
163 const double dir = info_.direction == transition_direction::from_left ? 1.0 : -1.0;
165 // For interlaced transitions. Seperate fields into seperate frames which are transitioned accordingly.
167 src_frame.transform().audio_transform.volume = 1.0-delta2;
168 auto s_frame1 = src_frame;
169 auto s_frame2 = src_frame;
171 dest_frame.transform().audio_transform.volume = delta2;
172 auto d_frame1 = dest_frame;
173 auto d_frame2 = dest_frame;
175 if(info_.type == transition_type::mix)
177 d_frame1.transform().image_transform.opacity = delta1;
178 d_frame1.transform().image_transform.is_mix = true;
179 d_frame2.transform().image_transform.opacity = delta2;
180 d_frame2.transform().image_transform.is_mix = true;
182 s_frame1.transform().image_transform.opacity = 1.0-delta1;
183 s_frame1.transform().image_transform.is_mix = true;
184 s_frame2.transform().image_transform.opacity = 1.0-delta2;
185 s_frame2.transform().image_transform.is_mix = true;
187 if(info_.type == transition_type::slide)
189 d_frame1.transform().image_transform.fill_translation[0] = (-1.0+delta1)*dir;
190 d_frame2.transform().image_transform.fill_translation[0] = (-1.0+delta2)*dir;
192 else if(info_.type == transition_type::push)
194 d_frame1.transform().image_transform.fill_translation[0] = (-1.0+delta1)*dir;
195 d_frame2.transform().image_transform.fill_translation[0] = (-1.0+delta2)*dir;
197 s_frame1.transform().image_transform.fill_translation[0] = (0.0+delta1)*dir;
198 s_frame2.transform().image_transform.fill_translation[0] = (0.0+delta2)*dir;
200 else if(info_.type == transition_type::wipe)
202 d_frame1.transform().image_transform.clip_scale[0] = delta1;
203 d_frame2.transform().image_transform.clip_scale[0] = delta2;
206 const auto s_frame = s_frame1.transform() == s_frame2.transform() ? s_frame2 : draw_frame::interlace(s_frame1, s_frame2, mode_);
207 const auto d_frame = d_frame1.transform() == d_frame2.transform() ? d_frame2 : draw_frame::interlace(d_frame1, d_frame2, mode_);
209 return draw_frame::over(s_frame, d_frame);
212 monitor::subject& monitor_output()
214 return *monitor_subject_;
217 void on_interaction(const interaction_event::ptr& event) override
219 dest_producer_->on_interaction(event);
222 bool collides(double x, double y) const override
224 return dest_producer_->collides(x, y);
228 spl::shared_ptr<frame_producer> create_transition_producer(const field_mode& mode, const spl::shared_ptr<frame_producer>& destination, const transition_info& info)
230 return spl::make_shared<transition_producer>(mode, destination, info);