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 "../../video_format.h"
\r
25 #include "../../mixer/frame/draw_frame.h"
\r
26 #include "../../mixer/frame_mixer_device.h"
\r
27 #include "../../mixer/image/image_mixer.h"
\r
28 #include "../../mixer/audio/audio_mixer.h"
\r
30 #include <boost/range/algorithm/copy.hpp>
\r
32 namespace caspar { namespace core {
\r
34 struct transition_producer::implementation : boost::noncopyable
\r
36 unsigned short current_frame_;
\r
38 const transition_info info_;
\r
40 safe_ptr<frame_producer> dest_producer_;
\r
41 safe_ptr<frame_producer> source_producer_;
\r
43 std::shared_ptr<frame_factory> frame_factory_;
\r
45 implementation(const safe_ptr<frame_producer>& dest, const transition_info& info)
\r
48 , dest_producer_(dest)
\r
49 , source_producer_(frame_producer::empty()){}
\r
51 safe_ptr<frame_producer> get_following_producer() const
\r
53 return dest_producer_;
\r
56 void set_leading_producer(const safe_ptr<frame_producer>& producer)
\r
58 source_producer_ = producer;
\r
61 safe_ptr<draw_frame> receive()
\r
63 if(current_frame_++ >= info_.duration)
\r
64 return draw_frame::eof();
\r
66 auto source = draw_frame::empty();
\r
67 auto dest = draw_frame::empty();
\r
69 tbb::parallel_invoke
\r
71 [&]{dest = render_sub_frame(dest_producer_);},
\r
72 [&]{source = render_sub_frame(source_producer_);}
\r
75 return compose(dest, source);
\r
78 safe_ptr<draw_frame> render_sub_frame(safe_ptr<frame_producer>& producer)
\r
80 if(producer == frame_producer::empty())
\r
81 return draw_frame::eof();
\r
83 auto frame = draw_frame::eof();
\r
86 frame = producer->receive();
\r
90 CASPAR_LOG_CURRENT_EXCEPTION();
\r
91 producer = frame_producer::empty();
\r
92 CASPAR_LOG(warning) << "Failed to receive frame. Removed producer from transition.";
\r
95 if(frame == draw_frame::eof())
\r
99 auto following = producer->get_following_producer();
\r
100 following->initialize(safe_ptr<frame_factory>(frame_factory_));
\r
101 following->set_leading_producer(producer);
\r
102 producer = std::move(following);
\r
106 CASPAR_LOG_CURRENT_EXCEPTION();
\r
107 producer = frame_producer::empty();
\r
108 CASPAR_LOG(warning) << "Failed to initialize following producer.";
\r
111 return render_sub_frame(producer);
\r
116 safe_ptr<draw_frame> compose(const safe_ptr<draw_frame>& dest_frame, const safe_ptr<draw_frame>& src_frame)
\r
118 if(dest_frame == draw_frame::eof() && src_frame == draw_frame::eof())
\r
119 return draw_frame::eof();
\r
121 if(info_.type == transition::cut)
\r
122 return src_frame != draw_frame::eof() ? src_frame : draw_frame::empty();
\r
124 double alpha = static_cast<double>(current_frame_)/static_cast<double>(info_.duration);
\r
126 auto my_src_frame = draw_frame(src_frame);
\r
127 auto my_dest_frame = draw_frame(dest_frame);
\r
129 my_src_frame.get_audio_transform().gain = 1.0-alpha;
\r
130 my_dest_frame.get_audio_transform().gain = alpha;
\r
132 double dir = info_.direction == transition_direction::from_left ? 1.0 : -1.0;
\r
134 if(info_.type == transition::mix)
\r
135 my_dest_frame.get_image_transform().alpha = alpha;
\r
136 else if(info_.type == transition::slide)
\r
137 my_dest_frame.get_image_transform().pos = boost::make_tuple((-1.0+alpha)*dir, 0.0);
\r
138 else if(info_.type == transition::push)
\r
140 my_dest_frame.get_image_transform().pos = boost::make_tuple((-1.0+alpha)*dir, 0.0);
\r
141 my_src_frame.get_image_transform().pos = boost::make_tuple((0.0+alpha)*dir, 0.0);
\r
143 else if(info_.type == transition::wipe)
\r
145 my_dest_frame.get_image_transform().pos = boost::make_tuple((-1.0+alpha)*dir, 0.0);
\r
146 my_dest_frame.get_image_transform().uv = boost::make_tuple((-1.0+alpha)*dir, 0.0, 0.0-(1.0-alpha)*dir, 0.0);
\r
149 return draw_frame(std::move(my_src_frame), std::move(my_dest_frame));
\r
152 void initialize(const safe_ptr<frame_factory>& frame_factory)
\r
154 dest_producer_->initialize(frame_factory);
\r
155 frame_factory_ = frame_factory;
\r
158 std::wstring print() const
\r
160 return L"transition[" + (source_producer_->print()) + L" -> " + (dest_producer_->print()) + L"]";
\r
164 transition_producer::transition_producer(transition_producer&& other) : impl_(std::move(other.impl_)){}
\r
165 transition_producer::transition_producer(const safe_ptr<frame_producer>& dest, const transition_info& info) : impl_(new implementation(dest, info)){}
\r
166 safe_ptr<draw_frame> transition_producer::receive(){return impl_->receive();}
\r
167 safe_ptr<frame_producer> transition_producer::get_following_producer() const{return impl_->get_following_producer();}
\r
168 void transition_producer::set_leading_producer(const safe_ptr<frame_producer>& producer) { impl_->set_leading_producer(producer); }
\r
169 void transition_producer::initialize(const safe_ptr<frame_factory>& frame_factory) { impl_->initialize(frame_factory);}
\r
170 std::wstring transition_producer::print() const { return impl_->print();}
\r