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 uint32_t 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<basic_frame> last_dest_;
\r
42 safe_ptr<basic_frame> last_source_;
\r
44 explicit transition_producer(const video_mode::type& mode, const safe_ptr<frame_producer>& dest, const transition_info& info)
\r
48 , dest_producer_(dest)
\r
49 , source_producer_(frame_producer::empty())
\r
50 , last_dest_(core::basic_frame::empty())
\r
51 , last_source_(core::basic_frame::empty()){}
\r
55 virtual safe_ptr<frame_producer> get_following_producer() const
\r
57 return dest_producer_;
\r
60 virtual void set_leading_producer(const safe_ptr<frame_producer>& producer)
\r
62 source_producer_ = producer;
\r
65 virtual safe_ptr<basic_frame> receive()
\r
67 if(current_frame_++ >= info_.duration)
\r
68 return basic_frame::eof();
\r
70 tbb::parallel_invoke
\r
72 [&]{last_dest_ = receive_and_follow_w_last(dest_producer_, last_dest_);},
\r
73 [&]{last_source_ = receive_and_follow_w_last(source_producer_, last_source_);}
\r
76 return compose(last_dest_, last_source_);
\r
79 virtual std::wstring print() const
\r
81 return L"transition";
\r
84 safe_ptr<basic_frame> receive_and_follow_w_last(safe_ptr<frame_producer>& producer, safe_ptr<basic_frame> last_frame)
\r
86 auto frame = core::receive_and_follow(producer);
\r
87 if(frame == basic_frame::late())
\r
89 last_frame->get_audio_transform().set_has_audio(false);
\r
95 // transition_producer
\r
97 safe_ptr<basic_frame> compose(const safe_ptr<basic_frame>& dest_frame, const safe_ptr<basic_frame>& src_frame)
\r
99 if(dest_frame == basic_frame::eof() && src_frame == basic_frame::eof())
\r
100 return basic_frame::eof();
\r
102 if(info_.type == transition::cut)
\r
105 double delta1 = info_.tweener(current_frame_*2-1, 0.0, 1.0, info_.duration*2);
\r
106 double delta2 = info_.tweener(current_frame_*2, 0.0, 1.0, info_.duration*2);
\r
108 double dir = info_.direction == transition_direction::from_left ? 1.0 : -1.0;
\r
110 // For interlaced transitions. Seperate fields into seperate frames which are transitioned accordingly.
\r
112 auto s_frame1 = make_safe<basic_frame>(src_frame);
\r
113 auto s_frame2 = make_safe<basic_frame>(src_frame);
\r
115 s_frame1->get_audio_transform().set_has_audio(false);
\r
116 s_frame2->get_audio_transform().set_gain(1.0-delta2);
\r
118 auto d_frame1 = make_safe<basic_frame>(dest_frame);
\r
119 auto d_frame2 = make_safe<basic_frame>(dest_frame);
\r
121 d_frame1->get_audio_transform().set_has_audio(false);
\r
122 d_frame2->get_audio_transform().set_gain(delta2);
\r
124 if(info_.type == transition::mix)
\r
126 d_frame1->get_image_transform().set_opacity(delta1);
\r
127 d_frame2->get_image_transform().set_opacity(delta2);
\r
129 else if(info_.type == transition::slide)
\r
131 d_frame1->get_image_transform().set_fill_translation((-1.0+delta1)*dir, 0.0);
\r
132 d_frame2->get_image_transform().set_fill_translation((-1.0+delta2)*dir, 0.0);
\r
134 else if(info_.type == transition::push)
\r
136 d_frame1->get_image_transform().set_fill_translation((-1.0+delta1)*dir, 0.0);
\r
137 d_frame2->get_image_transform().set_fill_translation((-1.0+delta2)*dir, 0.0);
\r
139 s_frame1->get_image_transform().set_fill_translation((0.0+delta1)*dir, 0.0);
\r
140 s_frame2->get_image_transform().set_fill_translation((0.0+delta2)*dir, 0.0);
\r
142 else if(info_.type == transition::wipe)
\r
144 d_frame1->get_image_transform().set_key_scale(delta1, 1.0);
\r
145 d_frame2->get_image_transform().set_key_scale(delta2, 1.0);
\r
148 auto s_frame = s_frame1->get_image_transform() == s_frame2->get_image_transform() ? s_frame2 : basic_frame::interlace(s_frame1, s_frame2, mode_);
\r
149 auto d_frame = d_frame1->get_image_transform() == d_frame2->get_image_transform() ? d_frame2 : basic_frame::interlace(d_frame1, d_frame2, mode_);
\r
151 return basic_frame::combine(s_frame, d_frame);
\r
155 safe_ptr<frame_producer> create_transition_producer(const video_mode::type& mode, const safe_ptr<frame_producer>& destination, const transition_info& info)
\r
157 return make_safe<transition_producer>(mode, destination, info);
\r