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 "../../format/video_format.h"
\r
25 #include "../../processor/composite_frame.h"
\r
26 #include "../../processor/transform_frame.h"
\r
27 #include "../../processor/frame_processor_device.h"
\r
29 #include <boost/range/algorithm/copy.hpp>
\r
31 namespace caspar { namespace core {
\r
33 struct transition_producer::implementation : boost::noncopyable
\r
35 implementation(const safe_ptr<frame_producer>& dest, const transition_info& info) : current_frame_(0), info_(info),
\r
36 dest_producer_(dest), source_producer_(frame_producer::empty())
\r
39 safe_ptr<frame_producer> get_following_producer() const
\r
41 return dest_producer_;
\r
44 void set_leading_producer(const safe_ptr<frame_producer>& producer)
\r
46 source_producer_ = producer;
\r
49 safe_ptr<draw_frame> receive()
\r
51 if(current_frame_++ >= info_.duration)
\r
52 return draw_frame::eof();
\r
54 auto source = draw_frame::empty();
\r
55 auto dest = draw_frame::empty();
\r
57 tbb::parallel_invoke
\r
59 [&]{dest = receive(dest_producer_);},
\r
60 [&]{source = receive(source_producer_);}
\r
63 return compose(dest, source);
\r
66 safe_ptr<draw_frame> receive(safe_ptr<frame_producer>& producer)
\r
68 if(producer == frame_producer::empty())
\r
69 return draw_frame::eof();
\r
71 auto frame = draw_frame::eof();
\r
74 frame = producer->receive();
\r
78 CASPAR_LOG_CURRENT_EXCEPTION();
\r
79 CASPAR_LOG(warning) << "Failed to receive frame. Removed producer from transition.";
\r
82 if(frame == draw_frame::eof())
\r
86 auto following = producer->get_following_producer();
\r
87 following->initialize(safe_ptr<frame_processor_device>(frame_processor_));
\r
88 following->set_leading_producer(producer);
\r
89 producer = std::move(following);
\r
93 CASPAR_LOG_CURRENT_EXCEPTION();
\r
94 CASPAR_LOG(warning) << "Failed to initialize following producer.";
\r
97 return receive(producer);
\r
102 safe_ptr<draw_frame> compose(const safe_ptr<draw_frame>& dest_frame, const safe_ptr<draw_frame>& src_frame)
\r
104 if(dest_frame == draw_frame::eof() && src_frame == draw_frame::eof())
\r
105 return draw_frame::eof();
\r
107 if(info_.type == transition::cut)
\r
108 return src_frame != draw_frame::eof() ? src_frame : draw_frame::empty();
\r
110 double alpha = static_cast<double>(current_frame_)/static_cast<double>(info_.duration);
\r
112 auto my_src_frame = transform_frame(src_frame);
\r
113 auto my_dest_frame = transform_frame(dest_frame);
\r
115 my_src_frame.audio_volume(1.0-alpha);
\r
116 my_dest_frame.audio_volume(alpha);
\r
118 double dir = info_.direction == transition_direction::from_left ? 1.0 : -1.0;
\r
120 if(info_.type == transition::mix)
\r
121 my_dest_frame.alpha(alpha);
\r
122 else if(info_.type == transition::slide)
\r
123 my_dest_frame.translate((-1.0+alpha)*dir, 0.0);
\r
124 else if(info_.type == transition::push)
\r
126 my_dest_frame.translate((-1.0+alpha)*dir, 0.0);
\r
127 my_src_frame.translate((0.0+alpha)*dir, 0.0);
\r
129 else if(info_.type == transition::wipe)
\r
131 my_dest_frame.translate((-1.0+alpha)*dir, 0.0);
\r
132 my_dest_frame.texcoord((-1.0+alpha)*dir, 0.0, 0.0-(1.0-alpha)*dir, 0.0);
\r
135 return composite_frame(std::move(my_src_frame), std::move(my_dest_frame));
\r
138 void initialize(const safe_ptr<frame_processor_device>& frame_processor)
\r
140 dest_producer_->initialize(frame_processor);
\r
141 frame_processor_ = frame_processor;
\r
144 std::wstring print() const
\r
146 return L"transition[" + (source_producer_->print()) + L" -> " + (dest_producer_->print()) + L"]";
\r
149 safe_ptr<frame_producer> source_producer_;
\r
150 safe_ptr<frame_producer> dest_producer_;
\r
152 unsigned short current_frame_;
\r
154 const transition_info info_;
\r
155 std::shared_ptr<frame_processor_device> frame_processor_;
\r
158 transition_producer::transition_producer(transition_producer&& other) : impl_(std::move(other.impl_)){}
\r
159 transition_producer::transition_producer(const safe_ptr<frame_producer>& dest, const transition_info& info) : impl_(new implementation(dest, info)){}
\r
160 safe_ptr<draw_frame> transition_producer::receive(){return impl_->receive();}
\r
161 safe_ptr<frame_producer> transition_producer::get_following_producer() const{return impl_->get_following_producer();}
\r
162 void transition_producer::set_leading_producer(const safe_ptr<frame_producer>& producer) { impl_->set_leading_producer(producer); }
\r
163 void transition_producer::initialize(const safe_ptr<frame_processor_device>& frame_processor) { impl_->initialize(frame_processor);}
\r
164 std::wstring transition_producer::print() const { return impl_->print();}
\r