2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG.
\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
20 #include "../../stdafx.h"
\r
22 #include "transition_producer.h"
\r
24 #include <core/video_format.h>
\r
26 #include <core/producer/frame/basic_frame.h>
\r
27 #include <core/producer/frame/image_transform.h>
\r
28 #include <core/producer/frame/audio_transform.h>
\r
30 namespace caspar { namespace core {
\r
32 struct transition_producer::implementation : boost::noncopyable
\r
34 const transition_producer* self_;
\r
35 unsigned short current_frame_;
\r
37 const transition_info info_;
\r
39 safe_ptr<frame_producer> dest_producer_;
\r
40 safe_ptr<frame_producer> source_producer_;
\r
42 std::shared_ptr<frame_factory> frame_factory_;
\r
44 std::vector<safe_ptr<basic_frame>> frame_buffer_;
\r
46 implementation(const transition_producer* self, const safe_ptr<frame_producer>& dest, const transition_info& info)
\r
50 , dest_producer_(dest)
\r
51 , source_producer_(frame_producer::empty())
\r
53 dest_producer_->set_parent(self_);
\r
54 frame_buffer_.push_back(basic_frame::empty());
\r
57 void set_frame_factory(const safe_ptr<frame_factory>& frame_factory)
\r
59 dest_producer_->set_frame_factory(frame_factory);
\r
60 frame_factory_ = frame_factory;
\r
63 safe_ptr<frame_producer> get_following_producer() const
\r
65 return dest_producer_;
\r
68 void set_leading_producer(const safe_ptr<frame_producer>& producer)
\r
70 source_producer_ = producer;
\r
71 source_producer_->set_parent(self_);
\r
74 safe_ptr<basic_frame> receive()
\r
76 if(current_frame_++ >= info_.duration)
\r
77 return basic_frame::eof();
\r
79 auto source = basic_frame::empty();
\r
80 auto dest = basic_frame::empty();
\r
82 tbb::parallel_invoke
\r
84 [&]{dest = render_sub_frame(dest_producer_);},
\r
85 [&]{source = render_sub_frame(source_producer_);}
\r
88 return compose(dest, source);
\r
91 safe_ptr<basic_frame> render_sub_frame(safe_ptr<frame_producer>& producer)
\r
93 if(producer == frame_producer::empty())
\r
94 return basic_frame::eof();
\r
96 auto frame = basic_frame::eof();
\r
99 frame = producer->receive();
\r
103 CASPAR_LOG_CURRENT_EXCEPTION();
\r
104 producer = frame_producer::empty();
\r
105 CASPAR_LOG(warning) << self_->print() << " Failed to receive frame. Removed producer from transition.";
\r
108 if(frame == basic_frame::eof())
\r
112 auto following = producer->get_following_producer();
\r
113 following->set_frame_factory(safe_ptr<frame_factory>(frame_factory_));
\r
114 following->set_leading_producer(producer);
\r
115 following->set_parent(self_);
\r
116 producer = std::move(following);
\r
120 CASPAR_LOG_CURRENT_EXCEPTION();
\r
121 producer = frame_producer::empty();
\r
122 CASPAR_LOG(warning) << self_->print() << " Failed to initialize following producer.";
\r
125 return render_sub_frame(producer);
\r
130 safe_ptr<basic_frame> compose(const safe_ptr<basic_frame>& dest_frame, const safe_ptr<basic_frame>& src_frame)
\r
132 if(dest_frame == basic_frame::eof() && src_frame == basic_frame::eof())
\r
133 return basic_frame::eof();
\r
135 if(info_.type == transition::cut)
\r
136 return src_frame != basic_frame::eof() ? src_frame : basic_frame::empty();
\r
138 double delta1 = info_.tweener(current_frame_*2-1, 0.0, 1.0, info_.duration*2);
\r
139 double delta2 = info_.tweener(current_frame_*2, 0.0, 1.0, info_.duration*2);
\r
141 double dir = info_.direction == transition_direction::from_left ? 1.0 : -1.0;
\r
143 // For interlaced transitions. Seperate fields into seperate frames which are transitioned accordingly.
\r
145 auto s_frame1 = make_safe<basic_frame>(src_frame);
\r
146 auto s_frame2 = make_safe<basic_frame>(src_frame);
\r
148 s_frame1->get_audio_transform().set_has_audio(false);
\r
149 s_frame2->get_audio_transform().set_gain(1.0-delta2);
\r
151 auto d_frame1 = make_safe<basic_frame>(dest_frame);
\r
152 auto d_frame2 = make_safe<basic_frame>(dest_frame);
\r
154 d_frame1->get_audio_transform().set_has_audio(false);
\r
155 d_frame2->get_audio_transform().set_gain(delta2);
\r
157 if(info_.type == transition::mix)
\r
159 d_frame1->get_image_transform().set_opacity(delta1);
\r
160 d_frame2->get_image_transform().set_opacity(delta2);
\r
162 else if(info_.type == transition::slide)
\r
164 d_frame1->get_image_transform().set_fill_translation((-1.0+delta1)*dir, 0.0);
\r
165 d_frame2->get_image_transform().set_fill_translation((-1.0+delta2)*dir, 0.0);
\r
167 else if(info_.type == transition::push)
\r
169 d_frame1->get_image_transform().set_fill_translation((-1.0+delta1)*dir, 0.0);
\r
170 d_frame2->get_image_transform().set_fill_translation((-1.0+delta2)*dir, 0.0);
\r
172 s_frame1->get_image_transform().set_fill_translation((0.0+delta1)*dir, 0.0);
\r
173 s_frame2->get_image_transform().set_fill_translation((0.0+delta2)*dir, 0.0);
\r
175 else if(info_.type == transition::wipe)
\r
177 d_frame1->get_image_transform().set_key_scale(delta1, 1.0);
\r
178 d_frame2->get_image_transform().set_key_scale(delta2, 1.0);
\r
181 auto mode = frame_factory_->get_video_format_desc().mode;
\r
182 auto s_frame = s_frame1->get_image_transform() == s_frame2->get_image_transform() ? s_frame2 : basic_frame::interlace(s_frame1, s_frame2, mode);
\r
183 auto d_frame = basic_frame::interlace(d_frame1, d_frame2, mode);
\r
185 return basic_frame(s_frame, d_frame);
\r
188 virtual std::wstring print() const
\r
190 return L"transition[" + info_.name() + L":" + boost::lexical_cast<std::wstring>(info_.duration) + L"]";
\r
193 std::wstring source_print() const { return print() + L"/source";}
\r
194 std::wstring dest_print() const { return print() + L"/dest";}
\r
197 transition_producer::transition_producer(const safe_ptr<frame_producer>& dest, const transition_info& info) : impl_(new implementation(this, dest, info)){}
\r
198 transition_producer::transition_producer(transition_producer&& other) : impl_(std::move(other.impl_)){}
\r
199 safe_ptr<basic_frame> transition_producer::receive(){return impl_->receive();}
\r
200 safe_ptr<frame_producer> transition_producer::get_following_producer() const{return impl_->get_following_producer();}
\r
201 void transition_producer::set_leading_producer(const safe_ptr<frame_producer>& producer) { impl_->set_leading_producer(producer); }
\r
202 void transition_producer::set_frame_factory(const safe_ptr<frame_factory>& frame_factory) { impl_->set_frame_factory(frame_factory);}
\r
203 std::wstring transition_producer::print() const { return frame_producer::print();}// + impl_->print();}
\r