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/frame.h"
\r
26 #include "../../processor/composite_frame.h"
\r
27 #include "../../processor/frame_processor_device.h"
\r
29 #include "../../producer/frame_producer_device.h"
\r
31 #include <boost/range/algorithm/copy.hpp>
\r
33 namespace caspar { namespace core {
\r
35 struct transition_producer::implementation : boost::noncopyable
\r
37 implementation(const frame_producer_ptr& dest, const transition_info& info)
\r
38 : current_frame_(0), info_(info), dest_producer_(dest)
\r
41 BOOST_THROW_EXCEPTION(null_argument() << arg_name_info("dest"));
\r
44 frame_producer_ptr get_following_producer() const
\r
46 return dest_producer_;
\r
49 void set_leading_producer(const frame_producer_ptr& producer)
\r
51 source_producer_ = producer;
\r
54 frame_ptr render_frame()
\r
56 if(current_frame_ == 0)
\r
57 CASPAR_LOG(info) << "Transition started.";
\r
59 frame_ptr result = [&]() -> frame_ptr
\r
61 if(current_frame_++ >= info_.duration)
\r
67 tbb::parallel_invoke
\r
69 [&]{dest = render_frame(dest_producer_);},
\r
70 [&]{source = render_frame(source_producer_);}
\r
73 return compose(dest, source);
\r
76 if(result == nullptr)
\r
77 CASPAR_LOG(info) << "Transition ended.";
\r
82 frame_ptr render_frame(frame_producer_ptr& producer)
\r
84 if(producer == nullptr)
\r
90 frame = producer->render_frame();
\r
94 CASPAR_LOG_CURRENT_EXCEPTION();
\r
96 CASPAR_LOG(warning) << "Removed producer from transition.";
\r
99 if(frame == nullptr)
\r
101 if(producer == nullptr || producer->get_following_producer() == nullptr)
\r
106 auto following = producer->get_following_producer();
\r
107 following->initialize(frame_processor_);
\r
108 following->set_leading_producer(producer);
\r
109 producer = following;
\r
113 CASPAR_LOG(warning) << "Failed to initialize following producer. Removing it.";
\r
114 producer = nullptr;
\r
117 return render_frame(producer);
\r
122 void set_volume(const frame_ptr& frame, int volume)
\r
124 assert(volume >= 0 && volume <= 256);
\r
125 std::for_each(frame->get_audio_data().begin(), frame->get_audio_data().end(), [&](int16_t& sample)
\r
127 sample = static_cast<short>((static_cast<int>(sample)*volume)>>8);
\r
131 frame_ptr compose(frame_ptr dest_frame, frame_ptr src_frame)
\r
133 if(!dest_frame && !src_frame)
\r
136 if(info_.type == transition::cut)
\r
139 dest_frame = dest_frame ? dest_frame : frame::empty();
\r
140 src_frame = src_frame ? src_frame : frame::empty();
\r
142 double alpha = static_cast<double>(current_frame_)/static_cast<double>(info_.duration);
\r
143 int volume = static_cast<int>(alpha*256.0);
\r
145 tbb::parallel_invoke
\r
147 [&]{set_volume(dest_frame, volume);},
\r
148 [&]{set_volume(src_frame, 256-volume);}
\r
151 double dir = info_.direction == transition_direction::from_left ? 1.0 : -1.0;
\r
153 if(info_.type == transition::mix)
\r
154 dest_frame->get_render_transform().alpha = alpha;
\r
155 else if(info_.type == transition::slide)
\r
156 dest_frame->get_render_transform().pos = boost::make_tuple((-1.0+alpha)*dir, 0.0);
\r
157 else if(info_.type == transition::push)
\r
159 dest_frame->get_render_transform().pos = boost::make_tuple((-1.0+alpha)*dir, 0.0);
\r
160 src_frame->get_render_transform().pos = boost::make_tuple((0.0+alpha)*dir, 0.0);
\r
162 else if(info_.type == transition::wipe)
\r
164 dest_frame->get_render_transform().pos = boost::make_tuple((-1.0+alpha)*dir, 0.0);
\r
165 dest_frame->get_render_transform().uv = boost::make_tuple((-1.0+alpha)*dir, 1.0, 1.0-(1.0-alpha)*dir, 0.0);
\r
168 return std::make_shared<composite_frame>(src_frame, dest_frame);
\r
171 void initialize(const frame_processor_device_ptr& frame_processor)
\r
173 dest_producer_->initialize(frame_processor);
\r
174 frame_processor_ = frame_processor;
\r
177 frame_producer_ptr source_producer_;
\r
178 frame_producer_ptr dest_producer_;
\r
180 unsigned short current_frame_;
\r
182 const transition_info info_;
\r
183 frame_processor_device_ptr frame_processor_;
\r
186 transition_producer::transition_producer(const frame_producer_ptr& dest, const transition_info& info) : impl_(new implementation(dest, info)){}
\r
187 frame_ptr transition_producer::render_frame(){return impl_->render_frame();}
\r
188 frame_producer_ptr transition_producer::get_following_producer() const{return impl_->get_following_producer();}
\r
189 void transition_producer::set_leading_producer(const frame_producer_ptr& producer) { impl_->set_leading_producer(producer); }
\r
190 void transition_producer::initialize(const frame_processor_device_ptr& frame_processor) { impl_->initialize(frame_processor);}
\r