]> git.sesse.net Git - casparcg/blob - core/producer/transition/transition_producer.cpp
2.0.0.2:
[casparcg] / core / producer / transition / transition_producer.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\r
5 *\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
10 *\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
15 \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
18 *\r
19 */ \r
20 #include "../../stdafx.h"\r
21 \r
22 #include "transition_producer.h"\r
23 \r
24 #include "../../format/video_format.h"\r
25 #include "../../processor/frame.h"\r
26 #include "../../processor/composite_frame.h"\r
27 #include "../../processor/frame_processor_device.h"\r
28 \r
29 #include "../../../common/utility/memory.h"\r
30 #include "../../producer/frame_producer_device.h"\r
31 \r
32 #include <boost/range/algorithm/copy.hpp>\r
33 \r
34 namespace caspar { namespace core {     \r
35 \r
36 struct transition_producer::implementation : boost::noncopyable\r
37 {\r
38         implementation(const frame_producer_ptr& dest, const transition_info& info) \r
39                 : current_frame_(0), info_(info), dest_producer_(dest)\r
40         {\r
41                 if(!dest)\r
42                         BOOST_THROW_EXCEPTION(null_argument() << arg_name_info("dest"));\r
43         }\r
44                 \r
45         frame_producer_ptr get_following_producer() const\r
46         {\r
47                 return dest_producer_;\r
48         }\r
49         \r
50         void set_leading_producer(const frame_producer_ptr& producer)\r
51         {\r
52                 source_producer_ = producer;\r
53         }\r
54                 \r
55         frame_ptr render_frame()\r
56         {\r
57                 if(current_frame_ == 0)\r
58                         CASPAR_LOG(info) << "Transition started.";\r
59 \r
60                 frame_ptr result = [&]() -> frame_ptr\r
61                 {\r
62                         if(current_frame_++ >= info_.duration)\r
63                                 return nullptr;\r
64 \r
65                         frame_ptr source;\r
66                         frame_ptr dest;\r
67 \r
68                         tbb::parallel_invoke\r
69                         (\r
70                                 [&]{dest   = render_frame(dest_producer_);},\r
71                                 [&]{source = render_frame(source_producer_);}\r
72                         );\r
73 \r
74                         return compose(dest, source);\r
75                 }();\r
76 \r
77                 if(result == nullptr)\r
78                         CASPAR_LOG(info) << "Transition ended.";\r
79 \r
80                 return result;\r
81         }\r
82 \r
83         frame_ptr render_frame(frame_producer_ptr& producer)\r
84         {\r
85                 if(producer == nullptr)\r
86                         return nullptr;\r
87 \r
88                 frame_ptr frame;\r
89                 try\r
90                 {\r
91                         frame = producer->render_frame();\r
92                 }\r
93                 catch(...)\r
94                 {\r
95                         CASPAR_LOG_CURRENT_EXCEPTION();\r
96                         producer = nullptr;\r
97                         CASPAR_LOG(warning) << "Removed producer from transition.";\r
98                 }\r
99 \r
100                 if(frame == nullptr)\r
101                 {\r
102                         if(producer == nullptr || producer->get_following_producer() == nullptr)\r
103                                 return nullptr;\r
104 \r
105                         try\r
106                         {\r
107                                 auto following = producer->get_following_producer();\r
108                                 following->initialize(frame_processor_);\r
109                                 following->set_leading_producer(producer);\r
110                                 producer = following;\r
111                         }\r
112                         catch(...)\r
113                         {\r
114                                 CASPAR_LOG(warning) << "Failed to initialize following producer. Removing it.";\r
115                                 producer = nullptr;\r
116                         }\r
117 \r
118                         return render_frame(producer);\r
119                 }\r
120                 return frame;\r
121         }\r
122                         \r
123         void set_volume(const frame_ptr& frame, int volume)\r
124         {\r
125                 if(!frame)\r
126                         return;\r
127 \r
128                 for(size_t n = 0; n < frame->audio_data().size(); ++n)\r
129                         frame->audio_data()[n] = static_cast<short>((static_cast<int>(frame->audio_data()[n])*volume)>>8);\r
130         }\r
131                 \r
132         frame_ptr compose(const frame_ptr& dest_frame, frame_ptr src_frame) \r
133         {       \r
134                 if(info_.type == transition::cut)               \r
135                         return src_frame;\r
136 \r
137                 if(!dest_frame)\r
138                         return nullptr;\r
139                                                                 \r
140                 double alpha = static_cast<double>(current_frame_)/static_cast<double>(info_.duration);\r
141                 int volume = static_cast<int>(alpha*256.0);\r
142                                 \r
143                 tbb::parallel_invoke\r
144                 (\r
145                         [&]{set_volume(dest_frame, volume);},\r
146                         [&]{set_volume(src_frame, 256-volume);}\r
147                 );\r
148                 \r
149                 if(info_.type == transition::mix)\r
150                         dest_frame->alpha(alpha);               \r
151                 else if(info_.type == transition::slide)\r
152                 {       \r
153                         if(info_.direction == transition_direction::from_left)                  \r
154                                 dest_frame->translate(-1.0+alpha, 0.0);                 \r
155                         else if(info_.direction == transition_direction::from_right)\r
156                                 dest_frame->translate(1.0-alpha, 0.0);          \r
157                 }\r
158                 else if(info_.type == transition::push)\r
159                 {\r
160                         if(info_.direction == transition_direction::from_left)          \r
161                         {\r
162                                 dest_frame->translate(-1.0+alpha, 0.0);\r
163                                 if(src_frame)\r
164                                         src_frame->translate(0.0+alpha, 0.0);\r
165                         }\r
166                         else if(info_.direction == transition_direction::from_right)\r
167                         {\r
168                                 dest_frame->translate(1.0-alpha, 0.0);\r
169                                 if(src_frame)\r
170                                         src_frame->translate(0.0-alpha, 0.0);\r
171                         }\r
172                 }\r
173                 else if(info_.type == transition::wipe)\r
174                 {\r
175                         if(info_.direction == transition_direction::from_left)          \r
176                         {\r
177                                 dest_frame->translate(-1.0+alpha, 0.0);\r
178                                 dest_frame->texcoords(-1.0+alpha, 1.0, alpha, 0.0);\r
179                         }\r
180                         else if(info_.direction == transition_direction::from_right)\r
181                         {\r
182                                 dest_frame->translate(1.0-alpha, 0.0);\r
183                                 dest_frame->texcoords(1.0-alpha, 1.0, 2.0-alpha, 0.0);\r
184                         }\r
185                 }\r
186                                                 \r
187                 auto composite = std::make_shared<composite_frame>();\r
188                 if(src_frame)\r
189                         composite->add(src_frame);\r
190                 composite->add(dest_frame);\r
191                 return composite;\r
192         }\r
193                 \r
194         void initialize(const frame_processor_device_ptr& frame_processor)\r
195         {\r
196                 dest_producer_->initialize(frame_processor);\r
197                 frame_processor_ = frame_processor;\r
198         }\r
199         \r
200         frame_producer_ptr                      source_producer_;\r
201         frame_producer_ptr                      dest_producer_;\r
202         \r
203         unsigned short                          current_frame_;\r
204         \r
205         const transition_info           info_;\r
206         frame_processor_device_ptr      frame_processor_;\r
207 };\r
208 \r
209 transition_producer::transition_producer(const frame_producer_ptr& dest, const transition_info& info) \r
210         : impl_(new implementation(dest, info)){}\r
211 frame_ptr transition_producer::render_frame(){return impl_->render_frame();}\r
212 frame_producer_ptr transition_producer::get_following_producer() const{return impl_->get_following_producer();}\r
213 void transition_producer::set_leading_producer(const frame_producer_ptr& producer) { impl_->set_leading_producer(producer); }\r
214 void transition_producer::initialize(const frame_processor_device_ptr& frame_processor) { impl_->initialize(frame_processor);}\r
215 \r
216 }}\r
217 \r