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