]> 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/composite_frame.h"\r
26 #include "../../processor/transform_frame.h"\r
27 #include "../../processor/frame_processor_device.h"\r
28 \r
29 #include "../../producer/frame_producer_device.h"\r
30 \r
31 #include <boost/range/algorithm/copy.hpp>\r
32 \r
33 namespace caspar { namespace core {     \r
34 \r
35 struct transition_producer::implementation : boost::noncopyable\r
36 {\r
37         implementation(const frame_producer_ptr& dest, const transition_info& info) \r
38                 : current_frame_(0), info_(info), dest_producer_(dest), org_dest_producer_(dest)\r
39         {\r
40                 if(!dest)\r
41                         BOOST_THROW_EXCEPTION(null_argument() << arg_name_info("dest"));\r
42         }\r
43                 \r
44         frame_producer_ptr get_following_producer() const\r
45         {\r
46                 return dest_producer_;\r
47         }\r
48         \r
49         void set_leading_producer(const frame_producer_ptr& producer)\r
50         {\r
51                 org_source_producer_ = source_producer_ = producer;\r
52         }\r
53                 \r
54         gpu_frame_ptr render_frame()\r
55         {\r
56                 if(current_frame_ == 0)\r
57                         CASPAR_LOG(info) << "Transition started.";\r
58 \r
59                 gpu_frame_ptr result = [&]() -> gpu_frame_ptr\r
60                 {\r
61                         if(current_frame_++ >= info_.duration)\r
62                                 return nullptr;\r
63 \r
64                         gpu_frame_ptr source;\r
65                         gpu_frame_ptr dest;\r
66 \r
67                         tbb::parallel_invoke\r
68                         (\r
69                                 [&]{dest   = render_frame(dest_producer_);},\r
70                                 [&]{source = render_frame(source_producer_);}\r
71                         );\r
72 \r
73                         return compose(dest, source);\r
74                 }();\r
75 \r
76                 if(!result)\r
77                         CASPAR_LOG(info) << "Transition ended.";\r
78 \r
79                 return result;\r
80         }\r
81 \r
82         gpu_frame_ptr render_frame(frame_producer_ptr& producer)\r
83         {\r
84                 if(!producer)\r
85                         return nullptr;\r
86 \r
87                 gpu_frame_ptr frame;\r
88                 try\r
89                 {\r
90                         frame = producer->render_frame();\r
91                 }\r
92                 catch(...)\r
93                 {\r
94                         CASPAR_LOG_CURRENT_EXCEPTION();\r
95                         producer = nullptr;\r
96                         CASPAR_LOG(warning) << "Removed producer from transition.";\r
97                 }\r
98 \r
99                 if(frame == nullptr)\r
100                 {\r
101                         if(!producer || !producer->get_following_producer())\r
102                                 return nullptr;\r
103 \r
104                         try\r
105                         {\r
106                                 auto following = producer->get_following_producer();\r
107                                 following->initialize(frame_processor_);\r
108                                 following->set_leading_producer(producer);\r
109                                 producer = following;\r
110                         }\r
111                         catch(...)\r
112                         {\r
113                                 CASPAR_LOG(warning) << "Failed to initialize following producer. Removing it.";\r
114                                 producer = nullptr;\r
115                         }\r
116 \r
117                         return render_frame(producer);\r
118                 }\r
119                 return frame;\r
120         }\r
121                                         \r
122         gpu_frame_ptr compose(gpu_frame_ptr dest_frame, gpu_frame_ptr src_frame) \r
123         {       \r
124                 if(!dest_frame && !src_frame)\r
125                         return nullptr;\r
126 \r
127                 if(info_.type == transition::cut)               \r
128                         return src_frame;\r
129                                                                                 \r
130                 double alpha = static_cast<double>(current_frame_)/static_cast<double>(info_.duration);\r
131                 unsigned char volume = static_cast<unsigned char>(alpha*256.0);\r
132 \r
133                 auto my_src_frame = std::make_shared<transform_frame>(src_frame);\r
134                 auto my_dest_frame = std::make_shared<transform_frame>(dest_frame);\r
135 \r
136                 my_src_frame->audio_volume(255-volume);\r
137                 my_dest_frame->audio_volume(volume);\r
138 \r
139                 double dir = info_.direction == transition_direction::from_left ? 1.0 : -1.0;           \r
140                 \r
141                 if(info_.type == transition::mix)\r
142                         my_dest_frame->alpha(alpha);            \r
143                 else if(info_.type == transition::slide)                        \r
144                         my_dest_frame->translate((-1.0+alpha)*dir, 0.0);                        \r
145                 else if(info_.type == transition::push)\r
146                 {\r
147                         my_dest_frame->translate((-1.0+alpha)*dir, 0.0);\r
148                         my_src_frame->translate((0.0+alpha)*dir, 0.0);\r
149                 }\r
150                 else if(info_.type == transition::wipe)\r
151                 {\r
152                         my_dest_frame->translate((-1.0+alpha)*dir, 0.0);                        \r
153                         my_dest_frame->texcoord((-1.0+alpha)*dir, 1.0, 1.0-(1.0-alpha)*dir, 0.0);                               \r
154                 }\r
155                                                 \r
156                 return std::make_shared<composite_frame>(my_src_frame, my_dest_frame);\r
157         }\r
158                 \r
159         void initialize(const frame_processor_device_ptr& frame_processor)\r
160         {\r
161                 dest_producer_->initialize(frame_processor);\r
162                 frame_processor_ = frame_processor;\r
163         }\r
164 \r
165         std::wstring print() const\r
166         {\r
167                 return L"transition_producer. dest: " + (org_dest_producer_ ? org_dest_producer_->print() : L"empty") + L" src: " + (org_source_producer_ ? org_source_producer_->print() : L"empty");\r
168         }\r
169         \r
170         frame_producer_ptr                      org_source_producer_;\r
171         frame_producer_ptr                      org_dest_producer_;\r
172 \r
173         frame_producer_ptr                      source_producer_;\r
174         frame_producer_ptr                      dest_producer_;\r
175         \r
176         unsigned short                          current_frame_;\r
177         \r
178         const transition_info           info_;\r
179         frame_processor_device_ptr      frame_processor_;\r
180 };\r
181 \r
182 transition_producer::transition_producer(const frame_producer_ptr& dest, const transition_info& info) : impl_(new implementation(dest, info)){}\r
183 gpu_frame_ptr transition_producer::render_frame(){return impl_->render_frame();}\r
184 frame_producer_ptr transition_producer::get_following_producer() const{return impl_->get_following_producer();}\r
185 void transition_producer::set_leading_producer(const frame_producer_ptr& producer) { impl_->set_leading_producer(producer); }\r
186 void transition_producer::initialize(const frame_processor_device_ptr& frame_processor) { impl_->initialize(frame_processor);}\r
187 std::wstring transition_producer::print() const { return impl_->print();}\r
188 \r
189 }}\r
190 \r