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/draw_frame.h"
\r
26 #include "../../processor/composite_frame.h"
\r
27 #include "../../processor/transform_frame.h"
\r
28 #include "../../processor/frame_processor_device.h"
\r
30 #include "../../producer/frame_producer_device.h"
\r
32 #include <boost/range/algorithm/copy.hpp>
\r
34 namespace caspar { namespace core {
\r
36 struct transition_producer::implementation : boost::noncopyable
\r
38 implementation(const safe_ptr<frame_producer>& dest, const transition_info& info) : current_frame_(0), info_(info),
\r
39 dest_producer_(dest), org_dest_producer_(dest), org_source_producer_(frame_producer::empty()), source_producer_(frame_producer::empty())
\r
42 safe_ptr<frame_producer> get_following_producer() const
\r
44 return dest_producer_;
\r
47 void set_leading_producer(const safe_ptr<frame_producer>& producer)
\r
49 org_source_producer_ = source_producer_ = producer;
\r
52 safe_ptr<draw_frame> receive()
\r
54 safe_ptr<draw_frame> result = [&]() -> safe_ptr<draw_frame>
\r
56 if(current_frame_++ >= info_.duration)
\r
57 return draw_frame::eof();
\r
59 safe_ptr<draw_frame> source(draw_frame::empty());
\r
60 safe_ptr<draw_frame> dest(draw_frame::empty());
\r
62 tbb::parallel_invoke
\r
64 [&]{dest = receive(dest_producer_);},
\r
65 [&]{source = receive(source_producer_);}
\r
68 return compose(dest, source);
\r
74 safe_ptr<draw_frame> receive(safe_ptr<frame_producer>& producer)
\r
76 if(producer == frame_producer::empty())
\r
77 return draw_frame::eof();
\r
79 auto frame = draw_frame::eof();
\r
82 frame = producer->receive();
\r
86 CASPAR_LOG_CURRENT_EXCEPTION();
\r
87 producer = frame_producer::empty();
\r
88 CASPAR_LOG(warning) << "Failed to receive frame. Removed producer from transition.";
\r
91 if(frame == draw_frame::eof())
\r
95 auto following = producer->get_following_producer();
\r
96 following->initialize(frame_processor_);
\r
97 following->set_leading_producer(producer);
\r
98 producer = following;
\r
102 CASPAR_LOG_CURRENT_EXCEPTION();
\r
103 producer = frame_producer::empty();
\r
104 CASPAR_LOG(warning) << "Failed to initialize following producer. Removed producer from transition.";
\r
107 return receive(producer);
\r
112 safe_ptr<draw_frame> compose(safe_ptr<draw_frame> dest_frame, safe_ptr<draw_frame> src_frame)
\r
114 if(dest_frame == draw_frame::eof() && src_frame == draw_frame::eof())
\r
115 return draw_frame::eof();
\r
117 if(info_.type == transition::cut)
\r
120 double alpha = static_cast<double>(current_frame_)/static_cast<double>(info_.duration);
\r
121 unsigned char volume = static_cast<unsigned char>(alpha*256.0);
\r
123 auto my_src_frame = transform_frame(src_frame);
\r
124 auto my_dest_frame = transform_frame(dest_frame);
\r
126 my_src_frame.audio_volume(255-volume);
\r
127 my_dest_frame.audio_volume(volume);
\r
129 double dir = info_.direction == transition_direction::from_left ? 1.0 : -1.0;
\r
131 if(info_.type == transition::mix)
\r
132 my_dest_frame.alpha(alpha);
\r
133 else if(info_.type == transition::slide)
\r
134 my_dest_frame.translate((-1.0+alpha)*dir, 0.0);
\r
135 else if(info_.type == transition::push)
\r
137 my_dest_frame.translate((-1.0+alpha)*dir, 0.0);
\r
138 my_src_frame.translate((0.0+alpha)*dir, 0.0);
\r
140 else if(info_.type == transition::wipe)
\r
142 my_dest_frame.translate((-1.0+alpha)*dir, 0.0);
\r
143 my_dest_frame.texcoord((-1.0+alpha)*dir, 1.0, 1.0-(1.0-alpha)*dir, 0.0);
\r
146 return composite_frame(std::move(my_src_frame), std::move(my_dest_frame));
\r
149 void initialize(const frame_processor_device_ptr& frame_processor)
\r
151 dest_producer_->initialize(frame_processor);
\r
152 frame_processor_ = frame_processor;
\r
155 std::wstring print() const
\r
157 return L"transition_producer. dest: " + (org_dest_producer_->print()) + L" src: " + (org_source_producer_->print());
\r
160 safe_ptr<frame_producer> org_source_producer_;
\r
161 safe_ptr<frame_producer> org_dest_producer_;
\r
163 safe_ptr<frame_producer> source_producer_;
\r
164 safe_ptr<frame_producer> dest_producer_;
\r
166 unsigned short current_frame_;
\r
168 const transition_info info_;
\r
169 frame_processor_device_ptr frame_processor_;
\r
172 transition_producer::transition_producer(const safe_ptr<frame_producer>& dest, const transition_info& info) : impl_(new implementation(dest, info)){}
\r
173 safe_ptr<draw_frame> transition_producer::receive(){return impl_->receive();}
\r
174 safe_ptr<frame_producer> transition_producer::get_following_producer() const{return impl_->get_following_producer();}
\r
175 void transition_producer::set_leading_producer(const safe_ptr<frame_producer>& producer) { impl_->set_leading_producer(producer); }
\r
176 void transition_producer::initialize(const frame_processor_device_ptr& frame_processor) { impl_->initialize(frame_processor);}
\r
177 std::wstring transition_producer::print() const { return impl_->print();}
\r