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 : public frame_producer
\r
34 const video_mode::type mode_;
\r
35 unsigned int 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
41 safe_ptr<frame_producer> org_dest_producer_;
\r
42 safe_ptr<frame_producer> org_source_producer_;
\r
44 safe_ptr<basic_frame> last_frame_;
\r
46 explicit transition_producer(const video_mode::type& mode, const safe_ptr<frame_producer>& dest, const transition_info& info)
\r
50 , dest_producer_(dest)
\r
51 , org_dest_producer_(dest)
\r
52 , source_producer_(frame_producer::empty())
\r
53 , org_source_producer_(frame_producer::empty())
\r
54 , last_frame_(basic_frame::empty()){}
\r
58 virtual safe_ptr<frame_producer> get_following_producer() const
\r
60 return dest_producer_;
\r
63 virtual void set_leading_producer(const safe_ptr<frame_producer>& producer)
\r
65 source_producer_ = producer;
\r
66 org_source_producer_ = producer;
\r
69 virtual safe_ptr<basic_frame> receive(int hints)
\r
71 if(current_frame_++ >= info_.duration)
\r
72 return basic_frame::eof();
\r
74 auto dest = core::basic_frame::empty();
\r
75 auto source = core::basic_frame::empty();
\r
77 tbb::parallel_invoke
\r
81 dest = receive_and_follow(dest_producer_, hints);
\r
82 if(dest == core::basic_frame::late())
\r
83 dest = dest_producer_->last_frame();
\r
87 source = receive_and_follow(source_producer_, hints);
\r
88 if(source == core::basic_frame::late())
\r
89 source = source_producer_->last_frame();
\r
93 return last_frame_ = compose(dest, source);
\r
96 virtual safe_ptr<core::basic_frame> last_frame() const
\r
101 virtual int64_t nb_frames() const
\r
103 return get_following_producer()->nb_frames();
\r
106 virtual std::wstring print() const
\r
108 return L"transition[" + org_source_producer_->print() + L"|" + org_dest_producer_->print() + L"]";
\r
111 // transition_producer
\r
113 safe_ptr<basic_frame> compose(const safe_ptr<basic_frame>& dest_frame, const safe_ptr<basic_frame>& src_frame)
\r
115 if(info_.type == transition::cut)
\r
118 const double delta1 = info_.tweener(current_frame_*2-1, 0.0, 1.0, info_.duration*2);
\r
119 const double delta2 = info_.tweener(current_frame_*2, 0.0, 1.0, info_.duration*2);
\r
121 const double dir = info_.direction == transition_direction::from_left ? 1.0 : -1.0;
\r
123 // For interlaced transitions. Seperate fields into seperate frames which are transitioned accordingly.
\r
125 auto s_frame1 = make_safe<basic_frame>(src_frame);
\r
126 auto s_frame2 = make_safe<basic_frame>(src_frame);
\r
128 s_frame1->get_audio_transform().set_has_audio(false);
\r
129 s_frame2->get_audio_transform().set_gain(1.0-delta2);
\r
131 auto d_frame1 = make_safe<basic_frame>(dest_frame);
\r
132 auto d_frame2 = make_safe<basic_frame>(dest_frame);
\r
134 d_frame1->get_audio_transform().set_has_audio(false);
\r
135 d_frame2->get_audio_transform().set_gain(delta2);
\r
137 if(info_.type == transition::mix)
\r
139 d_frame1->get_image_transform().set_opacity(delta1);
\r
140 d_frame2->get_image_transform().set_opacity(delta2);
\r
142 else if(info_.type == transition::slide)
\r
144 d_frame1->get_image_transform().set_fill_translation((-1.0+delta1)*dir, 0.0);
\r
145 d_frame2->get_image_transform().set_fill_translation((-1.0+delta2)*dir, 0.0);
\r
147 else if(info_.type == transition::push)
\r
149 d_frame1->get_image_transform().set_fill_translation((-1.0+delta1)*dir, 0.0);
\r
150 d_frame2->get_image_transform().set_fill_translation((-1.0+delta2)*dir, 0.0);
\r
152 s_frame1->get_image_transform().set_fill_translation((0.0+delta1)*dir, 0.0);
\r
153 s_frame2->get_image_transform().set_fill_translation((0.0+delta2)*dir, 0.0);
\r
155 else if(info_.type == transition::wipe)
\r
157 d_frame1->get_image_transform().set_clip_scale(delta1, 1.0);
\r
158 d_frame2->get_image_transform().set_clip_scale(delta2, 1.0);
\r
161 const auto s_frame = s_frame1->get_image_transform() == s_frame2->get_image_transform() ? s_frame2 : basic_frame::interlace(s_frame1, s_frame2, mode_);
\r
162 const auto d_frame = d_frame1->get_image_transform() == d_frame2->get_image_transform() ? d_frame2 : basic_frame::interlace(d_frame1, d_frame2, mode_);
\r
164 return basic_frame::combine(s_frame, d_frame);
\r
168 safe_ptr<frame_producer> create_transition_producer(const video_mode::type& mode, const safe_ptr<frame_producer>& destination, const transition_info& info)
\r
170 return make_safe<transition_producer>(mode, destination, info);
\r