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