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/basic_frame.h"
\r
28 #include "../frame/frame_transform.h"
\r
30 #include <tbb/parallel_invoke.h>
\r
32 #include <boost/assign.hpp>
\r
34 using namespace boost::assign;
\r
36 namespace caspar { namespace core {
\r
38 struct transition_producer : public frame_producer
\r
40 const field_mode::type mode_;
\r
41 unsigned int current_frame_;
\r
43 const transition_info info_;
\r
45 safe_ptr<frame_producer> dest_producer_;
\r
46 safe_ptr<frame_producer> source_producer_;
\r
48 safe_ptr<basic_frame> last_frame_;
\r
50 explicit transition_producer(const field_mode::type& mode, const safe_ptr<frame_producer>& dest, const transition_info& info)
\r
54 , dest_producer_(dest)
\r
55 , source_producer_(frame_producer::empty())
\r
56 , last_frame_(basic_frame::empty()){}
\r
60 virtual safe_ptr<frame_producer> get_following_producer() const override
\r
62 return dest_producer_;
\r
65 virtual void set_leading_producer(const safe_ptr<frame_producer>& producer) override
\r
67 source_producer_ = producer;
\r
70 virtual safe_ptr<basic_frame> receive(int flags) override
\r
72 if(++current_frame_ >= info_.duration)
\r
73 return basic_frame::eof();
\r
75 auto dest = basic_frame::empty();
\r
76 auto source = basic_frame::empty();
\r
78 tbb::parallel_invoke(
\r
81 dest = receive_and_follow(dest_producer_, flags);
\r
82 if(dest == core::basic_frame::late())
\r
83 dest = dest_producer_->last_frame();
\r
87 source = receive_and_follow(source_producer_, flags);
\r
88 if(source == core::basic_frame::late())
\r
89 source = source_producer_->last_frame();
\r
92 return compose(dest, source);
\r
95 virtual safe_ptr<core::basic_frame> last_frame() const override
\r
100 virtual uint32_t nb_frames() const override
\r
102 return get_following_producer()->nb_frames();
\r
105 virtual std::wstring print() const override
\r
107 return L"transition[" + source_producer_->print() + L"=>" + dest_producer_->print() + L"]";
\r
110 boost::property_tree::wptree info() const override
\r
112 boost::property_tree::wptree info;
\r
113 info.add(L"type", L"transition-producer");
\r
114 info.add_child(L"source.producer", source_producer_->info());
\r
115 info.add_child(L"destination.producer", dest_producer_->info());
\r
119 // transition_producer
\r
121 safe_ptr<basic_frame> compose(const safe_ptr<basic_frame>& dest_frame, const safe_ptr<basic_frame>& src_frame)
\r
123 if(info_.type == transition::cut)
\r
126 const double delta1 = info_.tweener(current_frame_*2-1, 0.0, 1.0, static_cast<double>(info_.duration*2));
\r
127 const double delta2 = info_.tweener(current_frame_*2, 0.0, 1.0, static_cast<double>(info_.duration*2));
\r
129 const double dir = info_.direction == transition_direction::from_left ? 1.0 : -1.0;
\r
131 // For interlaced transitions. Seperate fields into seperate frames which are transitioned accordingly.
\r
133 auto s_frame1 = make_safe<basic_frame>(src_frame);
\r
134 auto s_frame2 = make_safe<basic_frame>(src_frame);
\r
136 s_frame1->get_frame_transform().volume = 0.0;
\r
137 s_frame2->get_frame_transform().volume = 1.0-delta2;
\r
139 auto d_frame1 = make_safe<basic_frame>(dest_frame);
\r
140 auto d_frame2 = make_safe<basic_frame>(dest_frame);
\r
142 d_frame1->get_frame_transform().volume = 0.0;
\r
143 d_frame2->get_frame_transform().volume = delta2;
\r
145 if(info_.type == transition::mix)
\r
147 d_frame1->get_frame_transform().opacity = delta1;
\r
148 d_frame1->get_frame_transform().is_mix = true;
\r
149 d_frame2->get_frame_transform().opacity = delta2;
\r
150 d_frame2->get_frame_transform().is_mix = true;
\r
152 s_frame1->get_frame_transform().opacity = 1.0-delta1;
\r
153 s_frame1->get_frame_transform().is_mix = true;
\r
154 s_frame2->get_frame_transform().opacity = 1.0-delta2;
\r
155 s_frame2->get_frame_transform().is_mix = true;
\r
157 if(info_.type == transition::slide)
\r
159 d_frame1->get_frame_transform().fill_translation[0] = (-1.0+delta1)*dir;
\r
160 d_frame2->get_frame_transform().fill_translation[0] = (-1.0+delta2)*dir;
\r
162 else if(info_.type == transition::push)
\r
164 d_frame1->get_frame_transform().fill_translation[0] = (-1.0+delta1)*dir;
\r
165 d_frame2->get_frame_transform().fill_translation[0] = (-1.0+delta2)*dir;
\r
167 s_frame1->get_frame_transform().fill_translation[0] = (0.0+delta1)*dir;
\r
168 s_frame2->get_frame_transform().fill_translation[0] = (0.0+delta2)*dir;
\r
170 else if(info_.type == transition::wipe)
\r
172 d_frame1->get_frame_transform().clip_scale[0] = delta1;
\r
173 d_frame2->get_frame_transform().clip_scale[0] = delta2;
\r
176 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
177 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
179 last_frame_ = basic_frame::combine(s_frame2, d_frame2);
\r
181 return basic_frame::combine(s_frame, d_frame);
\r
185 safe_ptr<frame_producer> create_transition_producer(const field_mode::type& mode, const safe_ptr<frame_producer>& destination, const transition_info& info)
\r
187 return make_safe<transition_producer>(mode, destination, info);
\r