]> git.sesse.net Git - casparcg/blob - core/producer/transition/transition_producer.cpp
2.0.0.2: Mayor flash producer readability and maintainability improvements.
[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/draw_frame.h"\r
26 #include "../../processor/composite_frame.h"\r
27 #include "../../processor/transform_frame.h"\r
28 #include "../../processor/frame_processor_device.h"\r
29 \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 safe_ptr<frame_producer>& dest, const transition_info& info) : current_frame_(0), info_(info), \r
39                 dest_producer_(dest), source_producer_(frame_producer::empty())\r
40         {}\r
41                 \r
42         safe_ptr<frame_producer> get_following_producer() const\r
43         {\r
44                 return dest_producer_;\r
45         }\r
46         \r
47         void set_leading_producer(const safe_ptr<frame_producer>& producer)\r
48         {\r
49                 source_producer_ = producer;\r
50         }\r
51                 \r
52         safe_ptr<draw_frame> receive()\r
53         {\r
54                 if(current_frame_++ >= info_.duration)\r
55                         return draw_frame::eof();\r
56 \r
57                 auto source = draw_frame::empty();\r
58                 auto dest = draw_frame::empty();\r
59 \r
60                 tbb::parallel_invoke\r
61                 (\r
62                         [&]{dest   = receive(dest_producer_);},\r
63                         [&]{source = receive(source_producer_);}\r
64                 );\r
65 \r
66                 return compose(dest, source);\r
67         }\r
68 \r
69         safe_ptr<draw_frame> receive(safe_ptr<frame_producer>& producer)\r
70         {\r
71                 if(producer == frame_producer::empty())\r
72                         return draw_frame::eof();\r
73 \r
74                 auto frame = draw_frame::eof();\r
75                 try\r
76                 {\r
77                         frame = producer->receive();\r
78                 }\r
79                 catch(...)\r
80                 {\r
81                         CASPAR_LOG_CURRENT_EXCEPTION();\r
82                         CASPAR_LOG(warning) << "Failed to receive frame. Removed producer from transition.";\r
83                 }\r
84 \r
85                 if(frame == draw_frame::eof())\r
86                 {\r
87                         try\r
88                         {\r
89                                 auto following = producer->get_following_producer();\r
90                                 following->initialize(safe_ptr<frame_processor_device>(frame_processor_));\r
91                                 following->set_leading_producer(producer);\r
92                                 producer = std::move(following);\r
93                         }\r
94                         catch(...)\r
95                         {\r
96                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
97                                 CASPAR_LOG(warning) << "Failed to initialize following producer.";\r
98                         }\r
99 \r
100                         return receive(producer);\r
101                 }\r
102                 return frame;\r
103         }\r
104                                         \r
105         safe_ptr<draw_frame> compose(const safe_ptr<draw_frame>& dest_frame, const safe_ptr<draw_frame>& src_frame) \r
106         {       \r
107                 if(dest_frame == draw_frame::eof() && src_frame == draw_frame::eof())\r
108                         return draw_frame::eof();\r
109 \r
110                 if(info_.type == transition::cut)               \r
111                         return src_frame != draw_frame::eof() ? src_frame : draw_frame::empty();\r
112                                                                                 \r
113                 double alpha = static_cast<double>(current_frame_)/static_cast<double>(info_.duration);\r
114 \r
115                 auto my_src_frame = transform_frame(src_frame);\r
116                 auto my_dest_frame = transform_frame(dest_frame);\r
117 \r
118                 my_src_frame.audio_volume(1.0-alpha);\r
119                 my_dest_frame.audio_volume(alpha);\r
120 \r
121                 double dir = info_.direction == transition_direction::from_left ? 1.0 : -1.0;           \r
122                 \r
123                 if(info_.type == transition::mix)\r
124                         my_dest_frame.alpha(alpha);             \r
125                 else if(info_.type == transition::slide)                        \r
126                         my_dest_frame.translate((-1.0+alpha)*dir, 0.0);                 \r
127                 else if(info_.type == transition::push)\r
128                 {\r
129                         my_dest_frame.translate((-1.0+alpha)*dir, 0.0);\r
130                         my_src_frame.translate((0.0+alpha)*dir, 0.0);\r
131                 }\r
132                 else if(info_.type == transition::wipe)\r
133                 {\r
134                         my_dest_frame.translate((-1.0+alpha)*dir, 0.0);                 \r
135                         my_dest_frame.texcoord((-1.0+alpha)*dir, 0.0, 0.0-(1.0-alpha)*dir, 0.0);                                \r
136                 }\r
137 \r
138                 return composite_frame(std::move(my_src_frame), std::move(my_dest_frame));\r
139         }\r
140                 \r
141         void initialize(const safe_ptr<frame_processor_device>& frame_processor)\r
142         {\r
143                 dest_producer_->initialize(frame_processor);\r
144                 frame_processor_ = frame_processor;\r
145         }\r
146 \r
147         std::wstring print() const\r
148         {\r
149                 return L"transition[" + (source_producer_->print()) + L" -> " + (dest_producer_->print()) + L"]";\r
150         }\r
151         \r
152         safe_ptr<frame_producer>        source_producer_;\r
153         safe_ptr<frame_producer>        dest_producer_;\r
154         \r
155         unsigned short                          current_frame_;\r
156         \r
157         const transition_info           info_;\r
158         std::shared_ptr<frame_processor_device> frame_processor_;\r
159 };\r
160 \r
161 transition_producer::transition_producer(transition_producer&& other) : impl_(std::move(other.impl_)){}\r
162 transition_producer::transition_producer(const safe_ptr<frame_producer>& dest, const transition_info& info) : impl_(new implementation(dest, info)){}\r
163 safe_ptr<draw_frame> transition_producer::receive(){return impl_->receive();}\r
164 safe_ptr<frame_producer> transition_producer::get_following_producer() const{return impl_->get_following_producer();}\r
165 void transition_producer::set_leading_producer(const safe_ptr<frame_producer>& producer) { impl_->set_leading_producer(producer); }\r
166 void transition_producer::initialize(const safe_ptr<frame_processor_device>& frame_processor) { impl_->initialize(frame_processor);}\r
167 std::wstring transition_producer::print() const { return impl_->print();}\r
168 \r
169 }}\r
170 \r