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 unsigned short current_frame_;
\r
37 const transition_info info_;
\r
39 safe_ptr<frame_producer> dest_producer_;
\r
40 safe_ptr<frame_producer> source_producer_;
\r
42 std::shared_ptr<frame_factory> frame_factory_;
\r
43 video_format_desc format_desc_;
\r
45 std::vector<safe_ptr<draw_frame>> frame_buffer_;
\r
47 implementation(const safe_ptr<frame_producer>& dest, const transition_info& info)
\r
50 , dest_producer_(dest)
\r
51 , source_producer_(frame_producer::empty())
\r
53 frame_buffer_.push_back(draw_frame::empty());
\r
56 void initialize(const safe_ptr<frame_factory>& frame_factory)
\r
58 dest_producer_->initialize(frame_factory);
\r
59 frame_factory_ = frame_factory;
\r
60 format_desc_ = frame_factory_->get_video_format_desc();
\r
63 safe_ptr<frame_producer> get_following_producer() const
\r
65 return dest_producer_;
\r
68 void set_leading_producer(const safe_ptr<frame_producer>& producer)
\r
70 source_producer_ = producer;
\r
73 safe_ptr<draw_frame> receive()
\r
75 if(current_frame_++ >= info_.duration)
\r
76 return draw_frame::eof();
\r
78 auto source = draw_frame::empty();
\r
79 auto dest = draw_frame::empty();
\r
81 tbb::parallel_invoke
\r
83 [&]{dest = render_sub_frame(dest_producer_);},
\r
84 [&]{source = render_sub_frame(source_producer_);}
\r
87 return compose(dest, source);
\r
90 safe_ptr<draw_frame> render_sub_frame(safe_ptr<frame_producer>& producer)
\r
92 if(producer == frame_producer::empty())
\r
93 return draw_frame::eof();
\r
95 auto frame = draw_frame::eof();
\r
98 frame = producer->receive();
\r
102 CASPAR_LOG_CURRENT_EXCEPTION();
\r
103 producer = frame_producer::empty();
\r
104 CASPAR_LOG(warning) << print() << " Failed to receive frame. Removed producer from transition.";
\r
107 if(frame == draw_frame::eof())
\r
111 auto following = producer->get_following_producer();
\r
112 following->initialize(safe_ptr<frame_factory>(frame_factory_));
\r
113 following->set_leading_producer(producer);
\r
114 producer = std::move(following);
\r
118 CASPAR_LOG_CURRENT_EXCEPTION();
\r
119 producer = frame_producer::empty();
\r
120 CASPAR_LOG(warning) << print() << " Failed to initialize following producer.";
\r
123 return render_sub_frame(producer);
\r
128 safe_ptr<draw_frame> compose(const safe_ptr<draw_frame>& dest_frame, const safe_ptr<draw_frame>& src_frame)
\r
130 if(dest_frame == draw_frame::eof() && src_frame == draw_frame::eof())
\r
131 return draw_frame::eof();
\r
133 if(info_.type == transition::cut)
\r
134 return src_frame != draw_frame::eof() ? src_frame : draw_frame::empty();
\r
136 double alpha = static_cast<double>(current_frame_)/static_cast<double>(info_.duration);
\r
137 double half_alpha_step = 0.5*1.0/static_cast<double>(info_.duration);
\r
139 double dir = info_.direction == transition_direction::from_left ? 1.0 : -1.0;
\r
141 // For interalced transitions; seperate fields into seperate frames which are transitioned accordingly.
\r
143 auto s_frame1 = make_safe<draw_frame>(src_frame);
\r
144 auto s_frame2 = make_safe<draw_frame>(src_frame);
\r
146 s_frame1->get_audio_transform().set_gain(0);
\r
147 s_frame2->get_audio_transform().set_gain(1.0-alpha);
\r
149 auto d_frame1 = make_safe<draw_frame>(dest_frame);
\r
150 auto d_frame2 = make_safe<draw_frame>(dest_frame);
\r
152 d_frame1->get_audio_transform().set_gain(0);
\r
153 d_frame2->get_audio_transform().set_gain(alpha);
\r
155 if(info_.type == transition::mix)
\r
157 d_frame1->get_image_transform().set_opacity(alpha-half_alpha_step);
\r
158 d_frame2->get_image_transform().set_opacity(alpha);
\r
160 else if(info_.type == transition::slide)
\r
162 d_frame1->get_image_transform().set_image_translation((-1.0+alpha-half_alpha_step)*dir, 0.0);
\r
163 d_frame2->get_image_transform().set_image_translation((-1.0+alpha)*dir, 0.0);
\r
165 else if(info_.type == transition::push)
\r
167 d_frame1->get_image_transform().set_image_translation((-1.0+alpha-half_alpha_step)*dir, 0.0);
\r
168 d_frame2->get_image_transform().set_image_translation((-1.0+alpha)*dir, 0.0);
\r
170 s_frame1->get_image_transform().set_image_translation((0.0+alpha-half_alpha_step)*dir, 0.0);
\r
171 s_frame2->get_image_transform().set_image_translation((0.0+alpha)*dir, 0.0);
\r
173 else if(info_.type == transition::wipe)
\r
175 d_frame1->get_image_transform().set_mask_scale(alpha-half_alpha_step, 1.0);
\r
176 d_frame2->get_image_transform().set_mask_scale(alpha, 1.0);
\r
179 return draw_frame(draw_frame::interlace(s_frame1, s_frame2, format_desc_.mode), draw_frame::interlace(d_frame1, d_frame2, format_desc_.mode));
\r
182 std::wstring print() const
\r
184 return L"transition[" + source_producer_->print() + L"->" + dest_producer_->print() + L"]";
\r
188 transition_producer::transition_producer(transition_producer&& other) : impl_(std::move(other.impl_)){}
\r
189 transition_producer::transition_producer(const safe_ptr<frame_producer>& dest, const transition_info& info) : impl_(new implementation(dest, info)){}
\r
190 safe_ptr<draw_frame> transition_producer::receive(){return impl_->receive();}
\r
191 safe_ptr<frame_producer> transition_producer::get_following_producer() const{return impl_->get_following_producer();}
\r
192 void transition_producer::set_leading_producer(const safe_ptr<frame_producer>& producer) { impl_->set_leading_producer(producer); }
\r
193 void transition_producer::initialize(const safe_ptr<frame_factory>& frame_factory) { impl_->initialize(frame_factory);}
\r
194 std::wstring transition_producer::print() const { return impl_->print();}
\r