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 <core/video_format.h>
\r
28 #include <core/producer/frame/basic_frame.h>
\r
29 #include <core/producer/frame/frame_transform.h>
\r
31 #include <tbb/parallel_invoke.h>
\r
33 #include <boost/assign.hpp>
\r
35 using namespace boost::assign;
\r
37 namespace caspar { namespace core {
\r
39 struct transition_producer : public frame_producer
\r
41 monitor::subject monitor_subject_;
\r
43 const field_mode::type mode_;
\r
44 unsigned int current_frame_;
\r
46 const transition_info info_;
\r
48 safe_ptr<frame_producer> dest_producer_;
\r
49 safe_ptr<frame_producer> source_producer_;
\r
51 safe_ptr<basic_frame> last_frame_;
\r
53 explicit transition_producer(const field_mode::type& mode, const safe_ptr<frame_producer>& dest, const transition_info& info)
\r
57 , dest_producer_(dest)
\r
58 , source_producer_(frame_producer::empty())
\r
59 , last_frame_(basic_frame::empty())
\r
61 dest->monitor_output().link_target(&monitor_subject_);
\r
66 virtual safe_ptr<frame_producer> get_following_producer() const override
\r
68 return dest_producer_;
\r
71 virtual void set_leading_producer(const safe_ptr<frame_producer>& producer) override
\r
73 source_producer_ = producer;
\r
76 virtual boost::unique_future<std::wstring> call(const std::wstring& params) override
\r
78 return dest_producer_->call(params);
\r
81 virtual safe_ptr<basic_frame> receive(int hints) override
\r
83 if(++current_frame_ >= info_.duration)
\r
84 return basic_frame::eof();
\r
86 auto dest = basic_frame::empty();
\r
87 auto source = basic_frame::empty();
\r
89 tbb::parallel_invoke(
\r
92 dest = receive_and_follow(dest_producer_, hints);
\r
93 if(dest == core::basic_frame::late())
\r
94 dest = dest_producer_->last_frame();
\r
98 source = receive_and_follow(source_producer_, hints);
\r
99 if(source == core::basic_frame::late())
\r
100 source = source_producer_->last_frame();
\r
103 monitor_subject_ << monitor::message("/transition/frame") % static_cast<std::int32_t>(current_frame_) % static_cast<std::int32_t>(info_.duration)
\r
104 << monitor::message("/transition/type") % [&]() -> std::string
\r
108 case transition::mix: return "mix";
\r
109 case transition::wipe: return "wipe";
\r
110 case transition::slide: return "slide";
\r
111 case transition::push: return "push";
\r
112 case transition::cut: return "cut";
\r
113 default: return "n/a";
\r
117 return compose(dest, source);
\r
120 virtual safe_ptr<core::basic_frame> last_frame() const override
\r
122 return disable_audio(last_frame_);
\r
125 virtual uint32_t nb_frames() const override
\r
127 return get_following_producer()->nb_frames();
\r
130 virtual std::wstring print() const override
\r
132 return L"transition[" + source_producer_->print() + L"=>" + dest_producer_->print() + L"]";
\r
135 boost::property_tree::wptree info() const override
\r
137 boost::property_tree::wptree info;
\r
138 info.add(L"type", L"transition-producer");
\r
139 info.add_child(L"source.producer", source_producer_->info());
\r
140 info.add_child(L"destination.producer", dest_producer_->info());
\r
144 // transition_producer
\r
146 safe_ptr<basic_frame> compose(const safe_ptr<basic_frame>& dest_frame, const safe_ptr<basic_frame>& src_frame)
\r
148 if(info_.type == transition::cut)
\r
151 const double delta1 = info_.tweener(current_frame_*2-1, 0.0, 1.0, info_.duration*2);
\r
152 const double delta2 = info_.tweener(current_frame_*2, 0.0, 1.0, info_.duration*2);
\r
154 const double dir = info_.direction == transition_direction::from_left ? 1.0 : -1.0;
\r
156 // For interlaced transitions. Seperate fields into seperate frames which are transitioned accordingly.
\r
158 auto s_frame1 = make_safe<basic_frame>(src_frame);
\r
159 auto s_frame2 = make_safe<basic_frame>(src_frame);
\r
161 s_frame1->get_frame_transform().volume = 0.0;
\r
162 s_frame2->get_frame_transform().volume = 1.0-delta2;
\r
164 auto d_frame1 = make_safe<basic_frame>(dest_frame);
\r
165 auto d_frame2 = make_safe<basic_frame>(dest_frame);
\r
167 d_frame1->get_frame_transform().volume = 0.0;
\r
168 d_frame2->get_frame_transform().volume = delta2;
\r
170 if(info_.type == transition::mix)
\r
172 d_frame1->get_frame_transform().opacity = delta1;
\r
173 d_frame1->get_frame_transform().is_mix = true;
\r
174 d_frame2->get_frame_transform().opacity = delta2;
\r
175 d_frame2->get_frame_transform().is_mix = true;
\r
177 s_frame1->get_frame_transform().opacity = 1.0-delta1;
\r
178 s_frame1->get_frame_transform().is_mix = true;
\r
179 s_frame2->get_frame_transform().opacity = 1.0-delta2;
\r
180 s_frame2->get_frame_transform().is_mix = true;
\r
182 if(info_.type == transition::slide)
\r
184 d_frame1->get_frame_transform().fill_translation[0] = (-1.0+delta1)*dir;
\r
185 d_frame2->get_frame_transform().fill_translation[0] = (-1.0+delta2)*dir;
\r
187 else if(info_.type == transition::push)
\r
189 d_frame1->get_frame_transform().fill_translation[0] = (-1.0+delta1)*dir;
\r
190 d_frame2->get_frame_transform().fill_translation[0] = (-1.0+delta2)*dir;
\r
192 s_frame1->get_frame_transform().fill_translation[0] = (0.0+delta1)*dir;
\r
193 s_frame2->get_frame_transform().fill_translation[0] = (0.0+delta2)*dir;
\r
195 else if(info_.type == transition::wipe)
\r
197 d_frame1->get_frame_transform().clip_scale[0] = delta1;
\r
198 d_frame2->get_frame_transform().clip_scale[0] = delta2;
\r
201 const auto s_frame = s_frame1->get_frame_transform() == s_frame2->get_frame_transform() ? s_frame2 : basic_frame::interlace(s_frame1, s_frame2, mode_);
\r
202 const auto d_frame = d_frame1->get_frame_transform() == d_frame2->get_frame_transform() ? d_frame2 : basic_frame::interlace(d_frame1, d_frame2, mode_);
\r
204 last_frame_ = basic_frame::combine(s_frame2, d_frame2);
\r
206 return basic_frame::combine(s_frame, d_frame);
\r
209 monitor::source& monitor_output()
\r
211 return monitor_subject_;
\r
215 safe_ptr<frame_producer> create_transition_producer(const field_mode::type& mode, const safe_ptr<frame_producer>& destination, const transition_info& info)
\r
217 return create_producer_print_proxy(
\r
218 make_safe<transition_producer>(mode, destination, info));
\r