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 <core/producer/frame/basic_frame.h>
\r
27 #include <core/producer/frame/image_transform.h>
\r
28 #include <core/producer/frame/audio_transform.h>
\r
30 namespace caspar { namespace core {
\r
32 struct transition_producer::implementation : boost::noncopyable
\r
34 printer parent_printer_;
\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<basic_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 dest_producer_->set_parent_printer(std::bind(&implementation::dest_print, this));
\r
54 frame_buffer_.push_back(basic_frame::empty());
\r
57 void initialize(const safe_ptr<frame_factory>& frame_factory)
\r
59 dest_producer_->initialize(frame_factory);
\r
60 frame_factory_ = frame_factory;
\r
61 format_desc_ = frame_factory_->get_video_format_desc();
\r
64 virtual void set_parent_printer(const printer& parent_printer)
\r
66 parent_printer_ = parent_printer;
\r
69 safe_ptr<frame_producer> get_following_producer() const
\r
71 return dest_producer_;
\r
74 void set_leading_producer(const safe_ptr<frame_producer>& producer)
\r
76 source_producer_ = producer;
\r
77 source_producer_->set_parent_printer(std::bind(&implementation::source_print, this));
\r
80 safe_ptr<basic_frame> receive()
\r
82 if(current_frame_++ >= info_.duration)
\r
83 return basic_frame::eof();
\r
85 auto source = basic_frame::empty();
\r
86 auto dest = basic_frame::empty();
\r
88 tbb::parallel_invoke
\r
90 [&]{dest = render_sub_frame(dest_producer_);},
\r
91 [&]{source = render_sub_frame(source_producer_);}
\r
94 return compose(dest, source);
\r
97 safe_ptr<basic_frame> render_sub_frame(safe_ptr<frame_producer>& producer)
\r
99 if(producer == frame_producer::empty())
\r
100 return basic_frame::eof();
\r
102 auto frame = basic_frame::eof();
\r
105 frame = producer->receive();
\r
109 CASPAR_LOG_CURRENT_EXCEPTION();
\r
110 producer = frame_producer::empty();
\r
111 CASPAR_LOG(warning) << print() << " Failed to receive frame. Removed producer from transition.";
\r
114 if(frame == basic_frame::eof())
\r
118 auto following = producer->get_following_producer();
\r
119 following->initialize(safe_ptr<frame_factory>(frame_factory_));
\r
120 following->set_leading_producer(producer);
\r
121 producer = std::move(following);
\r
125 CASPAR_LOG_CURRENT_EXCEPTION();
\r
126 producer = frame_producer::empty();
\r
127 CASPAR_LOG(warning) << print() << " Failed to initialize following producer.";
\r
130 return render_sub_frame(producer);
\r
135 safe_ptr<basic_frame> compose(const safe_ptr<basic_frame>& dest_frame, const safe_ptr<basic_frame>& src_frame)
\r
137 if(dest_frame == basic_frame::eof() && src_frame == basic_frame::eof())
\r
138 return basic_frame::eof();
\r
140 if(info_.type == transition::cut)
\r
141 return src_frame != basic_frame::eof() ? src_frame : basic_frame::empty();
\r
143 double delta1 = info_.tweener(current_frame_*2-1, 0.0, 1.0, info_.duration*2);
\r
144 double delta2 = info_.tweener(current_frame_*2, 0.0, 1.0, info_.duration*2);
\r
146 double dir = info_.direction == transition_direction::from_left ? 1.0 : -1.0;
\r
148 // For interlaced transitions. Seperate fields into seperate frames which are transitioned accordingly.
\r
150 auto s_frame1 = make_safe<basic_frame>(src_frame);
\r
151 auto s_frame2 = make_safe<basic_frame>(src_frame);
\r
153 s_frame1->get_audio_transform().set_has_audio(false);
\r
154 s_frame2->get_audio_transform().set_gain(1.0-delta2);
\r
156 auto d_frame1 = make_safe<basic_frame>(dest_frame);
\r
157 auto d_frame2 = make_safe<basic_frame>(dest_frame);
\r
159 d_frame1->get_audio_transform().set_has_audio(false);
\r
160 d_frame2->get_audio_transform().set_gain(delta2);
\r
162 if(info_.type == transition::mix)
\r
164 d_frame1->get_image_transform().set_opacity(delta1);
\r
165 d_frame2->get_image_transform().set_opacity(delta2);
\r
167 else if(info_.type == transition::slide)
\r
169 d_frame1->get_image_transform().set_fill_translation((-1.0+delta1)*dir, 0.0);
\r
170 d_frame2->get_image_transform().set_fill_translation((-1.0+delta2)*dir, 0.0);
\r
172 else if(info_.type == transition::push)
\r
174 d_frame1->get_image_transform().set_fill_translation((-1.0+delta1)*dir, 0.0);
\r
175 d_frame2->get_image_transform().set_fill_translation((-1.0+delta2)*dir, 0.0);
\r
177 s_frame1->get_image_transform().set_fill_translation((0.0+delta1)*dir, 0.0);
\r
178 s_frame2->get_image_transform().set_fill_translation((0.0+delta2)*dir, 0.0);
\r
180 else if(info_.type == transition::wipe)
\r
182 d_frame1->get_image_transform().set_key_scale(delta1, 1.0);
\r
183 d_frame2->get_image_transform().set_key_scale(delta2, 1.0);
\r
186 auto s_frame = s_frame1->get_image_transform() == s_frame2->get_image_transform() ? s_frame2 : basic_frame::interlace(s_frame1, s_frame2, format_desc_.mode);
\r
187 auto d_frame = basic_frame::interlace(d_frame1, d_frame2, format_desc_.mode);
\r
189 return basic_frame(s_frame, d_frame);
\r
192 std::wstring print() const
\r
194 return (parent_printer_ ? parent_printer_() + L"/" : L"") + L"transition[" + info_.name() + L":" + boost::lexical_cast<std::wstring>(info_.duration) + L"]";
\r
197 std::wstring source_print() const { return print() + L"/source";}
\r
198 std::wstring dest_print() const { return print() + L"/dest";}
\r
201 transition_producer::transition_producer(transition_producer&& other) : impl_(std::move(other.impl_)){}
\r
202 transition_producer::transition_producer(const safe_ptr<frame_producer>& dest, const transition_info& info) : impl_(new implementation(dest, info)){}
\r
203 safe_ptr<basic_frame> transition_producer::receive(){return impl_->receive();}
\r
204 safe_ptr<frame_producer> transition_producer::get_following_producer() const{return impl_->get_following_producer();}
\r
205 void transition_producer::set_leading_producer(const safe_ptr<frame_producer>& producer) { impl_->set_leading_producer(producer); }
\r
206 void transition_producer::initialize(const safe_ptr<frame_factory>& frame_factory) { impl_->initialize(frame_factory);}
\r
207 void transition_producer::set_parent_printer(const printer& parent_printer) {impl_->set_parent_printer(parent_printer);}
\r
208 std::wstring transition_producer::print() const { return impl_->print();}
\r