]> git.sesse.net Git - casparcg/blob - core/producer/transition/transition_producer.cpp
34a5ef1bc945ec28face70b4b04d5dab29b860ad
[casparcg] / core / producer / transition / transition_producer.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "../../StdAfx.h"
23
24 #include "transition_producer.h"
25
26 #include "../frame_producer.h"
27 #include "../../frame/draw_frame.h"
28 #include "../../frame/frame_transform.h"
29 #include "../../monitor/monitor.h"
30
31 #include <tbb/parallel_invoke.h>
32
33 #include <future>
34
35 namespace caspar { namespace core {     
36
37 class transition_producer : public frame_producer_base
38 {       
39         spl::shared_ptr<monitor::subject>       monitor_subject_;
40         const field_mode                                        mode_;
41         int                                                                     current_frame_          = 0;
42         
43         const transition_info                           info_;
44                 
45         spl::shared_ptr<frame_producer>         dest_producer_;
46         spl::shared_ptr<frame_producer>         source_producer_        = frame_producer::empty();
47
48         bool                                                            paused_                         = false;
49                 
50 public:
51         explicit transition_producer(const field_mode& mode, const spl::shared_ptr<frame_producer>& dest, const transition_info& info) 
52                 : mode_(mode)
53                 , info_(info)
54                 , dest_producer_(dest)
55         {
56                 dest->monitor_output().attach_parent(monitor_subject_);
57
58                 CASPAR_LOG(info) << print() << L" Initialized";
59         }
60         
61         // frame_producer
62                 
63         void leading_producer(const spl::shared_ptr<frame_producer>& producer) override
64         {
65                 source_producer_ = producer;
66         }
67
68         draw_frame receive_impl() override
69         {
70                 if(current_frame_ >= info_.duration)
71                 {
72                         source_producer_ = core::frame_producer::empty();
73                         return dest_producer_->receive();               
74                 }
75
76                 current_frame_ += 1;
77
78                 auto dest = draw_frame::empty();
79                 auto source = draw_frame::empty();
80
81                 tbb::parallel_invoke(
82                 [&]
83                 {
84                         dest = dest_producer_->receive();
85                         if(dest == core::draw_frame::late())
86                                 dest = dest_producer_->last_frame();
87                 },
88                 [&]
89                 {
90                         source = source_producer_->receive();
91                         if(source == core::draw_frame::late())
92                                 source = source_producer_->last_frame();
93                 });                     
94                                                 
95                 *monitor_subject_       << monitor::message("/transition/frame") % current_frame_ % info_.duration
96                                                         << monitor::message("/transition/type") % [&]() -> std::string
97                                                                                                                                 {
98                                                                                                                                         switch(info_.type)
99                                                                                                                                         {
100                                                                                                                                         case transition_type::mix:              return "mix";
101                                                                                                                                         case transition_type::wipe:             return "wipe";
102                                                                                                                                         case transition_type::slide:    return "slide";
103                                                                                                                                         case transition_type::push:             return "push";
104                                                                                                                                         case transition_type::cut:              return "cut";
105                                                                                                                                         default:                                                return "n/a";
106                                                                                                                                         }
107                                                                                                                                 }();
108
109                 return compose(dest, source);
110         }
111
112         draw_frame last_frame() override
113         {
114                 if(current_frame_ >= info_.duration)
115                         return dest_producer_->last_frame();
116
117                 return frame_producer_base::last_frame();
118         }
119
120         constraints& pixel_constraints() override
121         {
122                 return dest_producer_->pixel_constraints();
123         }
124
125         uint32_t nb_frames() const override
126         {
127                 return dest_producer_->nb_frames();
128         }
129
130         std::wstring print() const override
131         {
132                 return L"transition[" + source_producer_->print() + L"=>" + dest_producer_->print() + L"]";
133         }
134
135         std::wstring name() const override
136         {
137                 return L"transition";
138         }
139         
140         boost::property_tree::wptree info() const override
141         {
142                 return dest_producer_->info();
143         }
144         
145         std::future<std::wstring> call(const std::vector<std::wstring>& params) override
146         {
147                 return dest_producer_->call(params);
148         }
149
150         // transition_producer
151                                                 
152         draw_frame compose(draw_frame dest_frame, draw_frame src_frame) const
153         {       
154                 if(info_.type == transition_type::cut)          
155                         return src_frame;
156                                                                                 
157                 const double delta1 = info_.tweener(current_frame_*2-1, 0.0, 1.0, static_cast<double>(info_.duration*2));
158                 const double delta2 = info_.tweener(current_frame_*2,   0.0, 1.0, static_cast<double>(info_.duration*2));  
159
160                 const double dir = info_.direction == transition_direction::from_left ? 1.0 : -1.0;             
161                 
162                 // For interlaced transitions. Seperate fields into seperate frames which are transitioned accordingly.
163                 
164                 src_frame.transform().audio_transform.volume = 1.0-delta2;
165                 auto s_frame1 = src_frame;
166                 auto s_frame2 = src_frame;
167                 
168                 dest_frame.transform().audio_transform.volume = delta2;
169                 auto d_frame1 = dest_frame;
170                 auto d_frame2 = dest_frame;
171                 
172                 if(info_.type == transition_type::mix)
173                 {
174                         d_frame1.transform().image_transform.opacity = delta1;  
175                         d_frame1.transform().image_transform.is_mix = true;
176                         d_frame2.transform().image_transform.opacity = delta2;
177                         d_frame2.transform().image_transform.is_mix = true;
178
179                         s_frame1.transform().image_transform.opacity = 1.0-delta1;      
180                         s_frame1.transform().image_transform.is_mix = true;
181                         s_frame2.transform().image_transform.opacity = 1.0-delta2;      
182                         s_frame2.transform().image_transform.is_mix = true;
183                 }
184                 if(info_.type == transition_type::slide)
185                 {
186                         d_frame1.transform().image_transform.fill_translation[0] = (-1.0+delta1)*dir;   
187                         d_frame2.transform().image_transform.fill_translation[0] = (-1.0+delta2)*dir;           
188                 }
189                 else if(info_.type == transition_type::push)
190                 {
191                         d_frame1.transform().image_transform.fill_translation[0] = (-1.0+delta1)*dir;
192                         d_frame2.transform().image_transform.fill_translation[0] = (-1.0+delta2)*dir;
193
194                         s_frame1.transform().image_transform.fill_translation[0] = (0.0+delta1)*dir;    
195                         s_frame2.transform().image_transform.fill_translation[0] = (0.0+delta2)*dir;            
196                 }
197                 else if(info_.type == transition_type::wipe)            
198                 {
199                         d_frame1.transform().image_transform.clip_scale[0] = delta1;    
200                         d_frame2.transform().image_transform.clip_scale[0] = delta2;                    
201                 }
202                                 
203                 const auto s_frame = s_frame1.transform() == s_frame2.transform() ? s_frame2 : draw_frame::interlace(s_frame1, s_frame2, mode_);
204                 const auto d_frame = d_frame1.transform() == d_frame2.transform() ? d_frame2 : draw_frame::interlace(d_frame1, d_frame2, mode_);
205                 
206                 return draw_frame::over(s_frame, d_frame);
207         }
208
209         monitor::subject& monitor_output()
210         {
211                 return *monitor_subject_;
212         }
213
214         void on_interaction(const interaction_event::ptr& event) override
215         {
216                 dest_producer_->on_interaction(event);
217         }
218
219         bool collides(double x, double y) const override
220         {
221                 return dest_producer_->collides(x, y);
222         }
223 };
224
225 spl::shared_ptr<frame_producer> create_transition_producer(const field_mode& mode, const spl::shared_ptr<frame_producer>& destination, const transition_info& info)
226 {
227         return spl::make_shared<transition_producer>(mode, destination, info);
228 }
229
230 }}
231