]> git.sesse.net Git - casparcg/blob - core/producer/transition/transition_producer.cpp
Merged most recent OSC changes
[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 namespace caspar { namespace core {     
34
35 class transition_producer : public frame_producer_base
36 {       
37         spl::shared_ptr<monitor::subject>       monitor_subject_;
38         const field_mode                                        mode_;
39         int                                                                     current_frame_;
40         
41         const transition_info                           info_;
42                 
43         spl::shared_ptr<frame_producer>         dest_producer_;
44         spl::shared_ptr<frame_producer>         source_producer_;
45
46         bool                                                            paused_;
47                 
48 public:
49         explicit transition_producer(const field_mode& mode, const spl::shared_ptr<frame_producer>& dest, const transition_info& info) 
50                 : mode_(mode)
51                 , current_frame_(0)
52                 , info_(info)
53                 , dest_producer_(dest)
54                 , source_producer_(frame_producer::empty())
55                 , paused_(false)
56         {
57                 dest->monitor_output().attach_parent(monitor_subject_);
58
59                 CASPAR_LOG(info) << print() << L" Initialized";
60         }
61         
62         // frame_producer
63                 
64         void leading_producer(const spl::shared_ptr<frame_producer>& producer) override
65         {
66                 source_producer_ = producer;
67         }
68
69         draw_frame receive_impl() override
70         {
71                 if(current_frame_ >= info_.duration)
72                 {
73                         source_producer_ = core::frame_producer::empty();
74                         return dest_producer_->receive();               
75                 }
76
77                 current_frame_ += 1;
78
79                 auto dest = draw_frame::empty();
80                 auto source = draw_frame::empty();
81
82                 tbb::parallel_invoke(
83                 [&]
84                 {
85                         dest = dest_producer_->receive();
86                         if(dest == core::draw_frame::late())
87                                 dest = dest_producer_->last_frame();
88                 },
89                 [&]
90                 {
91                         source = source_producer_->receive();
92                         if(source == core::draw_frame::late())
93                                 source = source_producer_->last_frame();
94                 });                     
95                                                 
96                 *monitor_subject_       << monitor::message("/transition/frame") % current_frame_ % info_.duration
97                                                         << monitor::message("/transition/type") % [&]() -> std::string
98                                                                                                                                 {
99                                                                                                                                         switch(info_.type.value())
100                                                                                                                                         {
101                                                                                                                                         case transition_type::mix:              return "mix";
102                                                                                                                                         case transition_type::wipe:             return "wipe";
103                                                                                                                                         case transition_type::slide:    return "slide";
104                                                                                                                                         case transition_type::push:             return "push";
105                                                                                                                                         case transition_type::cut:              return "cut";
106                                                                                                                                         default:                                                return "n/a";
107                                                                                                                                         }
108                                                                                                                                 }();
109
110                 return compose(dest, source);
111         }
112
113         draw_frame last_frame() override
114         {
115                 if(current_frame_ >= info_.duration)
116                         return dest_producer_->last_frame();
117
118                 return frame_producer_base::last_frame();
119         }
120
121         constraints& pixel_constraints() override
122         {
123                 return dest_producer_->pixel_constraints();
124         }
125
126         uint32_t nb_frames() const override
127         {
128                 return dest_producer_->nb_frames();
129         }
130
131         std::wstring print() const override
132         {
133                 return L"transition[" + source_producer_->print() + L"=>" + dest_producer_->print() + L"]";
134         }
135
136         std::wstring name() const override
137         {
138                 return L"transition";
139         }
140         
141         boost::property_tree::wptree info() const override
142         {
143                 return dest_producer_->info();
144         }
145         
146         boost::unique_future<std::wstring> call(const std::vector<std::wstring>& params) override
147         {
148                 return dest_producer_->call(params);
149         }
150
151         // transition_producer
152                                                 
153         draw_frame compose(draw_frame dest_frame, draw_frame src_frame) const
154         {       
155                 if(info_.type == transition_type::cut)          
156                         return src_frame;
157                                                                                 
158                 const double delta1 = info_.tweener(current_frame_*2-1, 0.0, 1.0, static_cast<double>(info_.duration*2));
159                 const double delta2 = info_.tweener(current_frame_*2,   0.0, 1.0, static_cast<double>(info_.duration*2));  
160
161                 const double dir = info_.direction == transition_direction::from_left ? 1.0 : -1.0;             
162                 
163                 // For interlaced transitions. Seperate fields into seperate frames which are transitioned accordingly.
164                 
165                 src_frame.transform().audio_transform.volume = 1.0-delta2;
166                 auto s_frame1 = src_frame;
167                 auto s_frame2 = src_frame;
168                 
169                 dest_frame.transform().audio_transform.volume = delta2;
170                 auto d_frame1 = dest_frame;
171                 auto d_frame2 = dest_frame;
172                 
173                 if(info_.type == transition_type::mix)
174                 {
175                         d_frame1.transform().image_transform.opacity = delta1;  
176                         d_frame1.transform().image_transform.is_mix = true;
177                         d_frame2.transform().image_transform.opacity = delta2;
178                         d_frame2.transform().image_transform.is_mix = true;
179
180                         s_frame1.transform().image_transform.opacity = 1.0-delta1;      
181                         s_frame1.transform().image_transform.is_mix = true;
182                         s_frame2.transform().image_transform.opacity = 1.0-delta2;      
183                         s_frame2.transform().image_transform.is_mix = true;
184                 }
185                 if(info_.type == transition_type::slide)
186                 {
187                         d_frame1.transform().image_transform.fill_translation[0] = (-1.0+delta1)*dir;   
188                         d_frame2.transform().image_transform.fill_translation[0] = (-1.0+delta2)*dir;           
189                 }
190                 else if(info_.type == transition_type::push)
191                 {
192                         d_frame1.transform().image_transform.fill_translation[0] = (-1.0+delta1)*dir;
193                         d_frame2.transform().image_transform.fill_translation[0] = (-1.0+delta2)*dir;
194
195                         s_frame1.transform().image_transform.fill_translation[0] = (0.0+delta1)*dir;    
196                         s_frame2.transform().image_transform.fill_translation[0] = (0.0+delta2)*dir;            
197                 }
198                 else if(info_.type == transition_type::wipe)            
199                 {
200                         d_frame1.transform().image_transform.clip_scale[0] = delta1;    
201                         d_frame2.transform().image_transform.clip_scale[0] = delta2;                    
202                 }
203                                 
204                 const auto s_frame = s_frame1.transform() == s_frame2.transform() ? s_frame2 : draw_frame::interlace(s_frame1, s_frame2, mode_);
205                 const auto d_frame = d_frame1.transform() == d_frame2.transform() ? d_frame2 : draw_frame::interlace(d_frame1, d_frame2, mode_);
206                 
207                 return draw_frame::over(s_frame, d_frame);
208         }
209
210         monitor::subject& monitor_output()
211         {
212                 return *monitor_subject_;
213         }
214
215         void on_interaction(const interaction_event::ptr& event) override
216         {
217                 dest_producer_->on_interaction(event);
218         }
219
220         bool collides(double x, double y) const override
221         {
222                 return dest_producer_->collides(x, y);
223         }
224 };
225
226 spl::shared_ptr<frame_producer> create_transition_producer(const field_mode& mode, const spl::shared_ptr<frame_producer>& destination, const transition_info& info)
227 {
228         return spl::make_shared<transition_producer>(mode, destination, info);
229 }
230
231 }}
232