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 <core/video_format.h>
\r
26 #include <mixer/frame/draw_frame.h>
\r
27 #include <mixer/image/image_transform.h>
\r
28 #include <mixer/audio/audio_mixer.h>
\r
29 #include <mixer/audio/audio_transform.h>
\r
31 namespace caspar { namespace core {
\r
33 struct transition_producer::implementation : boost::noncopyable
\r
35 printer parent_printer_;
\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
44 video_format_desc format_desc_;
\r
46 std::vector<safe_ptr<draw_frame>> frame_buffer_;
\r
48 implementation(const safe_ptr<frame_producer>& dest, const transition_info& info)
\r
51 , dest_producer_(dest)
\r
52 , source_producer_(frame_producer::empty())
\r
54 dest_producer_->set_parent_printer(std::bind(&implementation::dest_print, this));
\r
55 frame_buffer_.push_back(draw_frame::empty());
\r
58 void initialize(const safe_ptr<frame_factory>& frame_factory)
\r
60 dest_producer_->initialize(frame_factory);
\r
61 frame_factory_ = frame_factory;
\r
62 format_desc_ = frame_factory_->get_video_format_desc();
\r
65 virtual void set_parent_printer(const printer& parent_printer)
\r
67 parent_printer_ = parent_printer;
\r
70 safe_ptr<frame_producer> get_following_producer() const
\r
72 return dest_producer_;
\r
75 void set_leading_producer(const safe_ptr<frame_producer>& producer)
\r
77 source_producer_ = producer;
\r
78 source_producer_->set_parent_printer(std::bind(&implementation::source_print, this));
\r
81 safe_ptr<draw_frame> receive()
\r
83 if(current_frame_++ >= info_.duration)
\r
84 return draw_frame::eof();
\r
86 auto source = draw_frame::empty();
\r
87 auto dest = draw_frame::empty();
\r
89 tbb::parallel_invoke
\r
91 [&]{dest = render_sub_frame(dest_producer_);},
\r
92 [&]{source = render_sub_frame(source_producer_);}
\r
95 return compose(dest, source);
\r
98 safe_ptr<draw_frame> render_sub_frame(safe_ptr<frame_producer>& producer)
\r
100 if(producer == frame_producer::empty())
\r
101 return draw_frame::eof();
\r
103 auto frame = draw_frame::eof();
\r
106 frame = producer->receive();
\r
110 CASPAR_LOG_CURRENT_EXCEPTION();
\r
111 producer = frame_producer::empty();
\r
112 CASPAR_LOG(warning) << print() << " Failed to receive frame. Removed producer from transition.";
\r
115 if(frame == draw_frame::eof())
\r
119 auto following = producer->get_following_producer();
\r
120 following->initialize(safe_ptr<frame_factory>(frame_factory_));
\r
121 following->set_leading_producer(producer);
\r
122 producer = std::move(following);
\r
126 CASPAR_LOG_CURRENT_EXCEPTION();
\r
127 producer = frame_producer::empty();
\r
128 CASPAR_LOG(warning) << print() << " Failed to initialize following producer.";
\r
131 return render_sub_frame(producer);
\r
136 safe_ptr<draw_frame> compose(const safe_ptr<draw_frame>& dest_frame, const safe_ptr<draw_frame>& src_frame)
\r
138 if(dest_frame == draw_frame::eof() && src_frame == draw_frame::eof())
\r
139 return draw_frame::eof();
\r
141 if(info_.type == transition::cut)
\r
142 return src_frame != draw_frame::eof() ? src_frame : draw_frame::empty();
\r
144 double alpha = static_cast<double>(current_frame_)/static_cast<double>(info_.duration);
\r
145 double half_alpha_step = 0.5*1.0/static_cast<double>(info_.duration);
\r
147 double dir = info_.direction == transition_direction::from_left ? 1.0 : -1.0;
\r
149 // For interalced transitions; seperate fields into seperate frames which are transitioned accordingly.
\r
151 auto s_frame1 = make_safe<draw_frame>(src_frame);
\r
152 auto s_frame2 = make_safe<draw_frame>(src_frame);
\r
154 s_frame1->get_audio_transform().set_gain(0);
\r
155 s_frame2->get_audio_transform().set_gain(1.0-alpha);
\r
157 auto d_frame1 = make_safe<draw_frame>(dest_frame);
\r
158 auto d_frame2 = make_safe<draw_frame>(dest_frame);
\r
160 d_frame1->get_audio_transform().set_gain(0);
\r
161 d_frame2->get_audio_transform().set_gain(alpha);
\r
163 if(info_.type == transition::mix)
\r
165 d_frame1->get_image_transform().set_opacity(alpha-half_alpha_step);
\r
166 d_frame2->get_image_transform().set_opacity(alpha);
\r
168 else if(info_.type == transition::slide)
\r
170 d_frame1->get_image_transform().set_fill_translation((-1.0+alpha-half_alpha_step)*dir, 0.0);
\r
171 d_frame2->get_image_transform().set_fill_translation((-1.0+alpha)*dir, 0.0);
\r
173 else if(info_.type == transition::push)
\r
175 d_frame1->get_image_transform().set_fill_translation((-1.0+alpha-half_alpha_step)*dir, 0.0);
\r
176 d_frame2->get_image_transform().set_fill_translation((-1.0+alpha)*dir, 0.0);
\r
178 s_frame1->get_image_transform().set_fill_translation((0.0+alpha-half_alpha_step)*dir, 0.0);
\r
179 s_frame2->get_image_transform().set_fill_translation((0.0+alpha)*dir, 0.0);
\r
181 else if(info_.type == transition::wipe)
\r
183 d_frame1->get_image_transform().set_key_scale(alpha-half_alpha_step, 1.0);
\r
184 d_frame2->get_image_transform().set_key_scale(alpha, 1.0);
\r
187 return draw_frame(draw_frame::interlace(s_frame1, s_frame2, format_desc_.mode), draw_frame::interlace(d_frame1, d_frame2, format_desc_.mode));
\r
190 std::wstring print() const
\r
192 return (parent_printer_ ? parent_printer_() + L"/" : L"") + L"transition[" + info_.name() + L":" + boost::lexical_cast<std::wstring>(info_.duration) + L"]";
\r
195 std::wstring source_print() const { return print() + L"/source";}
\r
196 std::wstring dest_print() const { return print() + L"/dest";}
\r
199 transition_producer::transition_producer(transition_producer&& other) : impl_(std::move(other.impl_)){}
\r
200 transition_producer::transition_producer(const safe_ptr<frame_producer>& dest, const transition_info& info) : impl_(new implementation(dest, info)){}
\r
201 safe_ptr<draw_frame> transition_producer::receive(){return impl_->receive();}
\r
202 safe_ptr<frame_producer> transition_producer::get_following_producer() const{return impl_->get_following_producer();}
\r
203 void transition_producer::set_leading_producer(const safe_ptr<frame_producer>& producer) { impl_->set_leading_producer(producer); }
\r
204 void transition_producer::initialize(const safe_ptr<frame_factory>& frame_factory) { impl_->initialize(frame_factory);}
\r
205 void transition_producer::set_parent_printer(const printer& parent_printer) {impl_->set_parent_printer(parent_printer);}
\r
206 std::wstring transition_producer::print() const { return impl_->print();}
\r