]> 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 "../../frame/frame_format.h"\r
25 #include "../../frame/gpu_frame.h"\r
26 #include "../../frame/gpu_composite_frame.h"\r
27 #include "../../frame/frame_factory.h"\r
28 \r
29 #include "../../../common/utility/memory.h"\r
30 #include "../../renderer/render_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                                         const frame_format_desc& format_desc) \r
40                 : current_frame_(0), info_(info), format_desc_(format_desc), dest_producer_(dest)\r
41         {\r
42                 if(!dest)\r
43                         BOOST_THROW_EXCEPTION(null_argument() << arg_name_info("dest"));\r
44         }\r
45                 \r
46         frame_producer_ptr get_following_producer() const\r
47         {\r
48                 return dest_producer_;\r
49         }\r
50         \r
51         void set_leading_producer(const frame_producer_ptr& producer)\r
52         {\r
53                 source_producer_ = producer;\r
54         }\r
55                 \r
56         gpu_frame_ptr get_frame()\r
57         {\r
58                 if(current_frame_++ >= info_.duration)\r
59                         return nullptr;\r
60 \r
61                 gpu_frame_ptr source;\r
62                 gpu_frame_ptr dest;\r
63 \r
64                 tbb::parallel_invoke\r
65                 (\r
66                         [&]{dest = get_frame(dest_producer_);},\r
67                         [&]{source = get_frame(source_producer_);}\r
68                 );\r
69 \r
70                 return compose(dest, source);\r
71         }\r
72 \r
73         gpu_frame_ptr get_frame(frame_producer_ptr& producer)\r
74         {\r
75                 if(producer == nullptr)\r
76                         return nullptr;\r
77 \r
78                 gpu_frame_ptr frame;\r
79                 try\r
80                 {\r
81                         frame = producer->get_frame();\r
82                 }\r
83                 catch(...)\r
84                 {\r
85                         CASPAR_LOG_CURRENT_EXCEPTION();\r
86                         producer = nullptr;\r
87                         CASPAR_LOG(warning) << "Removed renderer from transition.";\r
88                 }\r
89 \r
90                 if(frame == nullptr && producer != nullptr && producer->get_following_producer() != nullptr)\r
91                 {\r
92                         auto following = producer->get_following_producer();\r
93                         following->initialize(factory_);\r
94                         following->set_leading_producer(producer);\r
95                         producer = following;\r
96                         return get_frame(producer);\r
97                 }\r
98                 return frame;\r
99         }\r
100                         \r
101         void set_volume(const gpu_frame_ptr& frame, int volume)\r
102         {\r
103                 if(!frame)\r
104                         return;\r
105 \r
106                 for(size_t n = 0; n < frame->audio_data().size(); ++n)\r
107                         frame->audio_data()[n] = static_cast<short>((static_cast<int>(frame->audio_data()[n])*volume)>>8);\r
108         }\r
109                         \r
110 \r
111         gpu_frame_ptr compose(const gpu_frame_ptr& dest_frame, gpu_frame_ptr src_frame) \r
112         {       \r
113                 if(info_.type == transition_type::cut)          \r
114                         return src_frame;\r
115 \r
116                 if(!dest_frame)\r
117                         return nullptr;\r
118                                                                 \r
119                 double alpha = static_cast<double>(current_frame_)/static_cast<double>(info_.duration);\r
120                 int volume = static_cast<int>(static_cast<double>(current_frame_)/static_cast<double>(info_.duration)*256.0);\r
121                                 \r
122                 tbb::parallel_invoke\r
123                 (\r
124                         [&]{set_volume(dest_frame, volume);},\r
125                         [&]{set_volume(src_frame, 256-volume);}\r
126                 );\r
127                                 \r
128                 auto composite = std::make_shared<gpu_composite_frame>();\r
129                 if(src_frame)\r
130                         composite->add(src_frame);\r
131                 composite->add(dest_frame);\r
132 \r
133                 switch(info_.type)\r
134                 {\r
135                 case transition_type::mix: \r
136                         dest_frame->alpha(alpha);       \r
137                         break;\r
138                 case transition_type::slide:                    \r
139                         if(info_.direction == transition_direction::from_left)                  \r
140                                 dest_frame->translate(-1.0+alpha, 0.0);                 \r
141                         else if(info_.direction == transition_direction::from_right)\r
142                                 dest_frame->translate(1.0-alpha, 0.0);          \r
143                         break;\r
144                 case transition_type::push:\r
145                         if(info_.direction == transition_direction::from_left)          \r
146                         {\r
147                                 dest_frame->translate(-1.0+alpha, 0.0);\r
148                                 if(src_frame)\r
149                                         src_frame->translate(0.0+alpha, 0.0);\r
150                         }\r
151                         else if(info_.direction == transition_direction::from_right)\r
152                         {\r
153                                 dest_frame->translate(1.0-alpha, 0.0);\r
154                                 if(src_frame)\r
155                                         src_frame->translate(0.0-alpha, 0.0);\r
156                         }\r
157                         break;\r
158                 }\r
159 \r
160                 if(info_.type == transition_type::mix)\r
161                         dest_frame->alpha(alpha);               \r
162                 else if(info_.type == transition_type::slide)\r
163                 {       \r
164                 }\r
165                 else if(info_.type == transition_type::push)\r
166                 {\r
167                 }\r
168                 else if(info_.type == transition_type::wipe)\r
169                 {\r
170                         if(info_.direction == transition_direction::from_left)          \r
171                         {\r
172                                 dest_frame->translate(-1.0+alpha, 0.0);\r
173                                 dest_frame->texcoords(rectangle(-1.0+alpha, 1.0, alpha, 0.0));\r
174                         }\r
175                         else if(info_.direction == transition_direction::from_right)\r
176                         {\r
177                                 dest_frame->translate(1.0-alpha, 0.0);\r
178                                 dest_frame->texcoords(rectangle(1.0-alpha, 1.0, 2.0-alpha, 0.0));\r
179                         }\r
180                 }\r
181 \r
182                 return composite;\r
183         }\r
184                 \r
185         void initialize(const frame_factory_ptr& factory)\r
186         {\r
187                 dest_producer_->initialize(factory);\r
188                 factory_ = factory;\r
189         }\r
190 \r
191         const frame_format_desc         format_desc_;\r
192 \r
193         frame_producer_ptr                      source_producer_;\r
194         frame_producer_ptr                      dest_producer_;\r
195         \r
196         unsigned short                          current_frame_;\r
197         \r
198         const transition_info           info_;\r
199         frame_factory_ptr                       factory_;\r
200 };\r
201 \r
202 transition_producer::transition_producer(const frame_producer_ptr& dest, const transition_info& info, const frame_format_desc& format_desc) \r
203         : impl_(new implementation(dest, info, format_desc)){}\r
204 gpu_frame_ptr transition_producer::get_frame(){return impl_->get_frame();}\r
205 frame_producer_ptr transition_producer::get_following_producer() const{return impl_->get_following_producer();}\r
206 void transition_producer::set_leading_producer(const frame_producer_ptr& producer) { impl_->set_leading_producer(producer); }\r
207 const frame_format_desc& transition_producer::get_frame_format_desc() const { return impl_->format_desc_; } \r
208 void transition_producer::initialize(const frame_factory_ptr& factory) { impl_->initialize(factory);}\r
209 \r
210 }}\r
211 \r