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 const field_mode::type mode_;
\r
42 unsigned int current_frame_;
\r
44 const transition_info info_;
\r
46 safe_ptr<frame_producer> dest_producer_;
\r
47 safe_ptr<frame_producer> source_producer_;
\r
49 safe_ptr<basic_frame> last_frame_;
\r
51 explicit transition_producer(const field_mode::type& mode, const safe_ptr<frame_producer>& dest, const transition_info& info)
\r
55 , dest_producer_(dest)
\r
56 , source_producer_(frame_producer::empty())
\r
57 , last_frame_(basic_frame::empty()){}
\r
61 virtual safe_ptr<frame_producer> get_following_producer() const override
\r
63 return dest_producer_;
\r
66 virtual void set_leading_producer(const safe_ptr<frame_producer>& producer) override
\r
68 source_producer_ = producer;
\r
71 virtual boost::unique_future<std::wstring> call(const std::wstring& params) override
\r
73 return dest_producer_->call(params);
\r
76 virtual safe_ptr<basic_frame> receive(int hints) override
\r
78 if(++current_frame_ >= info_.duration)
\r
79 return basic_frame::eof();
\r
81 auto dest = basic_frame::empty();
\r
82 auto source = basic_frame::empty();
\r
84 tbb::parallel_invoke(
\r
87 dest = receive_and_follow(dest_producer_, hints);
\r
88 if(dest == core::basic_frame::late())
\r
89 dest = dest_producer_->last_frame();
\r
93 source = receive_and_follow(source_producer_, hints);
\r
94 if(source == core::basic_frame::late())
\r
95 source = source_producer_->last_frame();
\r
98 return compose(dest, source);
\r
101 virtual safe_ptr<core::basic_frame> last_frame() const override
\r
103 return disable_audio(last_frame_);
\r
106 virtual uint32_t nb_frames() const override
\r
108 return get_following_producer()->nb_frames();
\r
111 virtual std::wstring print() const override
\r
113 return L"transition[" + source_producer_->print() + L"=>" + dest_producer_->print() + L"]";
\r
116 boost::property_tree::wptree info() const override
\r
118 boost::property_tree::wptree info;
\r
119 info.add(L"type", L"transition-producer");
\r
120 info.add_child(L"source.producer", source_producer_->info());
\r
121 info.add_child(L"destination.producer", dest_producer_->info());
\r
125 // transition_producer
\r
127 safe_ptr<basic_frame> compose(const safe_ptr<basic_frame>& dest_frame, const safe_ptr<basic_frame>& src_frame)
\r
129 if(info_.type == transition::cut)
\r
132 const double delta1 = info_.tweener(current_frame_*2-1, 0.0, 1.0, info_.duration*2);
\r
133 const double delta2 = info_.tweener(current_frame_*2, 0.0, 1.0, info_.duration*2);
\r
135 const double dir = info_.direction == transition_direction::from_left ? 1.0 : -1.0;
\r
137 // For interlaced transitions. Seperate fields into seperate frames which are transitioned accordingly.
\r
139 auto s_frame1 = make_safe<basic_frame>(src_frame);
\r
140 auto s_frame2 = make_safe<basic_frame>(src_frame);
\r
142 s_frame1->get_frame_transform().volume = 0.0;
\r
143 s_frame2->get_frame_transform().volume = 1.0-delta2;
\r
145 auto d_frame1 = make_safe<basic_frame>(dest_frame);
\r
146 auto d_frame2 = make_safe<basic_frame>(dest_frame);
\r
148 d_frame1->get_frame_transform().volume = 0.0;
\r
149 d_frame2->get_frame_transform().volume = delta2;
\r
151 if(info_.type == transition::mix)
\r
153 d_frame1->get_frame_transform().opacity = delta1;
\r
154 d_frame1->get_frame_transform().is_mix = true;
\r
155 d_frame2->get_frame_transform().opacity = delta2;
\r
156 d_frame2->get_frame_transform().is_mix = true;
\r
158 s_frame1->get_frame_transform().opacity = 1.0-delta1;
\r
159 s_frame1->get_frame_transform().is_mix = true;
\r
160 s_frame2->get_frame_transform().opacity = 1.0-delta2;
\r
161 s_frame2->get_frame_transform().is_mix = true;
\r
163 if(info_.type == transition::slide)
\r
165 d_frame1->get_frame_transform().fill_translation[0] = (-1.0+delta1)*dir;
\r
166 d_frame2->get_frame_transform().fill_translation[0] = (-1.0+delta2)*dir;
\r
168 else if(info_.type == transition::push)
\r
170 d_frame1->get_frame_transform().fill_translation[0] = (-1.0+delta1)*dir;
\r
171 d_frame2->get_frame_transform().fill_translation[0] = (-1.0+delta2)*dir;
\r
173 s_frame1->get_frame_transform().fill_translation[0] = (0.0+delta1)*dir;
\r
174 s_frame2->get_frame_transform().fill_translation[0] = (0.0+delta2)*dir;
\r
176 else if(info_.type == transition::wipe)
\r
178 d_frame1->get_frame_transform().clip_scale[0] = delta1;
\r
179 d_frame2->get_frame_transform().clip_scale[0] = delta2;
\r
182 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
183 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
185 last_frame_ = basic_frame::combine(s_frame2, d_frame2);
\r
187 return basic_frame::combine(s_frame, d_frame);
\r
191 safe_ptr<frame_producer> create_transition_producer(const field_mode::type& mode, const safe_ptr<frame_producer>& destination, const transition_info& info)
\r
193 return create_producer_print_proxy(
\r
194 make_safe<transition_producer>(mode, destination, info));
\r