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/producer_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 frame_producer_ptr& dest, const transition_info& info)
\r
39 : current_frame_(0), info_(info), dest_producer_(dest), org_dest_producer_(dest)
\r
42 BOOST_THROW_EXCEPTION(null_argument() << arg_name_info("dest"));
\r
45 frame_producer_ptr get_following_producer() const
\r
47 return dest_producer_;
\r
50 void set_leading_producer(const frame_producer_ptr& producer)
\r
52 org_source_producer_ = source_producer_ = producer;
\r
55 producer_frame receive()
\r
57 if(current_frame_ == 0)
\r
58 CASPAR_LOG(info) << "Transition started.";
\r
60 producer_frame result = [&]() -> producer_frame
\r
62 if(current_frame_++ >= info_.duration)
\r
63 return producer_frame::eof();
\r
65 producer_frame source;
\r
66 producer_frame dest;
\r
68 tbb::parallel_invoke
\r
70 [&]{dest = receive(dest_producer_);},
\r
71 [&]{source = receive(source_producer_);}
\r
74 return compose(dest, source);
\r
77 if(result == producer_frame::eof())
\r
78 CASPAR_LOG(info) << "Transition ended.";
\r
83 producer_frame receive(frame_producer_ptr& producer)
\r
86 return producer_frame::eof();
\r
88 producer_frame frame = producer_frame::eof();
\r
91 frame = producer->receive();
\r
95 CASPAR_LOG_CURRENT_EXCEPTION();
\r
97 CASPAR_LOG(warning) << "Removed producer from transition.";
\r
100 if(frame == producer_frame::eof())
\r
102 if(!producer || !producer->get_following_producer())
\r
103 return producer_frame::eof();
\r
107 auto following = producer->get_following_producer();
\r
108 following->initialize(frame_processor_);
\r
109 following->set_leading_producer(producer);
\r
110 producer = following;
\r
114 CASPAR_LOG(warning) << "Failed to initialize following producer. Removing it.";
\r
115 producer = nullptr;
\r
118 return receive(producer);
\r
123 producer_frame compose(producer_frame dest_frame, producer_frame src_frame)
\r
125 if(dest_frame == producer_frame::eof() && src_frame == producer_frame::eof())
\r
126 return producer_frame::eof();
\r
128 if(info_.type == transition::cut)
\r
131 double alpha = static_cast<double>(current_frame_)/static_cast<double>(info_.duration);
\r
132 unsigned char volume = static_cast<unsigned char>(alpha*256.0);
\r
134 auto my_src_frame = std::make_shared<transform_frame>(src_frame);
\r
135 auto my_dest_frame = std::make_shared<transform_frame>(dest_frame);
\r
137 my_src_frame->audio_volume(255-volume);
\r
138 my_dest_frame->audio_volume(volume);
\r
140 double dir = info_.direction == transition_direction::from_left ? 1.0 : -1.0;
\r
142 if(info_.type == transition::mix)
\r
143 my_dest_frame->alpha(alpha);
\r
144 else if(info_.type == transition::slide)
\r
145 my_dest_frame->translate((-1.0+alpha)*dir, 0.0);
\r
146 else if(info_.type == transition::push)
\r
148 my_dest_frame->translate((-1.0+alpha)*dir, 0.0);
\r
149 my_src_frame->translate((0.0+alpha)*dir, 0.0);
\r
151 else if(info_.type == transition::wipe)
\r
153 my_dest_frame->translate((-1.0+alpha)*dir, 0.0);
\r
154 my_dest_frame->texcoord((-1.0+alpha)*dir, 1.0, 1.0-(1.0-alpha)*dir, 0.0);
\r
157 return std::make_shared<composite_frame>(my_src_frame, my_dest_frame);
\r
160 void initialize(const frame_processor_device_ptr& frame_processor)
\r
162 dest_producer_->initialize(frame_processor);
\r
163 frame_processor_ = frame_processor;
\r
166 std::wstring print() const
\r
168 return L"transition_producer. dest: " + (org_dest_producer_ ? org_dest_producer_->print() : L"empty") + L" src: " + (org_source_producer_ ? org_source_producer_->print() : L"empty");
\r
171 frame_producer_ptr org_source_producer_;
\r
172 frame_producer_ptr org_dest_producer_;
\r
174 frame_producer_ptr source_producer_;
\r
175 frame_producer_ptr dest_producer_;
\r
177 unsigned short current_frame_;
\r
179 const transition_info info_;
\r
180 frame_processor_device_ptr frame_processor_;
\r
183 transition_producer::transition_producer(const frame_producer_ptr& dest, const transition_info& info) : impl_(new implementation(dest, info)){}
\r
184 producer_frame transition_producer::receive(){return impl_->receive();}
\r
185 frame_producer_ptr transition_producer::get_following_producer() const{return impl_->get_following_producer();}
\r
186 void transition_producer::set_leading_producer(const frame_producer_ptr& producer) { impl_->set_leading_producer(producer); }
\r
187 void transition_producer::initialize(const frame_processor_device_ptr& frame_processor) { impl_->initialize(frame_processor);}
\r
188 std::wstring transition_producer::print() const { return impl_->print();}
\r