]> 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, const frame_format_desc& format_desc) \r
39                 : current_frame_(0), info_(info), format_desc_(format_desc), dest_(dest)\r
40         {\r
41                 if(!dest)\r
42                         BOOST_THROW_EXCEPTION(null_argument() << arg_name_info("dest"));\r
43         }\r
44                 \r
45         frame_producer_ptr get_following_producer() const\r
46         {\r
47                 return dest_;\r
48         }\r
49         \r
50         void set_leading_producer(const frame_producer_ptr& producer)\r
51         {\r
52                 source_ = producer;\r
53         }\r
54                 \r
55         gpu_frame_ptr get_frame()\r
56         {\r
57                 return ++current_frame_ >= info_.duration ? nullptr : compose(get_producer_frame(dest_), get_producer_frame(source_));\r
58         }\r
59 \r
60         gpu_frame_ptr get_producer_frame(frame_producer_ptr& producer)\r
61         {\r
62                 if(producer == nullptr)\r
63                 {       \r
64                         auto frame = factory_->create_frame(format_desc_);\r
65                         common::clear(frame->data(), frame->size());\r
66                         return frame;\r
67                 }\r
68 \r
69                 gpu_frame_ptr frame;\r
70                 try\r
71                 {\r
72                         frame = producer->get_frame();\r
73                 }\r
74                 catch(...)\r
75                 {\r
76                         CASPAR_LOG_CURRENT_EXCEPTION();\r
77                         producer = nullptr;\r
78                         CASPAR_LOG(warning) << "Removed renderer from transition.";\r
79                 }\r
80 \r
81                 if(frame == nullptr && producer != nullptr && producer->get_following_producer() != nullptr)\r
82                 {\r
83                         auto following = producer->get_following_producer();\r
84                         following->initialize(factory_);\r
85                         following->set_leading_producer(producer);\r
86                         producer = following;\r
87                         return get_producer_frame(producer);\r
88                 }\r
89                 return frame;\r
90         }\r
91                         \r
92         gpu_frame_ptr compose(const gpu_frame_ptr& dest_frame, const gpu_frame_ptr& src_frame) \r
93         {       \r
94                 if(!src_frame)\r
95                         return dest_frame;\r
96 \r
97                 if(info_.type == transition_type::cut || !dest_frame)           \r
98                         return src_frame;\r
99                 \r
100                 int volume = static_cast<int>(static_cast<double>(current_frame_)/static_cast<double>(info_.duration)*256.0);\r
101                                 \r
102                 for(size_t n = 0; n < dest_frame->audio_data().size(); ++n)\r
103                         dest_frame->audio_data()[n] = static_cast<short>((static_cast<int>(dest_frame->audio_data()[n])*volume)>>8);\r
104 \r
105                 for(size_t n = 0; n < src_frame->audio_data().size(); ++n)\r
106                         src_frame->audio_data()[n] = static_cast<short>((static_cast<int>(src_frame->audio_data()[n])*(256-volume))>>8);\r
107                                 \r
108                 float alpha = static_cast<float>(current_frame_)/static_cast<float>(info_.duration);\r
109                 auto composite = std::make_shared<gpu_composite_frame>();\r
110                 composite->add(src_frame);\r
111                 composite->add(dest_frame);\r
112                 if(info_.type == transition_type::mix)\r
113                 {\r
114                         src_frame->alpha(1.0f-alpha);\r
115                         dest_frame->alpha(alpha);\r
116                 }\r
117                 else if(info_.type == transition_type::slide)\r
118                 {\r
119                         if(info_.direction == transition_direction::from_left)                  \r
120                                 dest_frame->translate(-1.0f+alpha, 0.0f);                       \r
121                         else if(info_.direction == transition_direction::from_right)\r
122                                 dest_frame->translate(1.0f-alpha, 0.0f);                        \r
123                 }\r
124                 else if(info_.type == transition_type::push)\r
125                 {\r
126                         if(info_.direction == transition_direction::from_left)          \r
127                         {\r
128                                 dest_frame->translate(-1.0f+alpha, 0.0f);\r
129                                 src_frame->translate(0.0f+alpha, 0.0f);\r
130                         }\r
131                         else if(info_.direction == transition_direction::from_right)\r
132                         {\r
133                                 dest_frame->translate(1.0f-alpha, 0.0f);\r
134                                 src_frame->translate(0.0f-alpha, 0.0f);\r
135                         }\r
136                 }\r
137                 return composite;\r
138         }\r
139                 \r
140         void initialize(const frame_factory_ptr& factory)\r
141         {\r
142                 dest_->initialize(factory);\r
143                 factory_ = factory;\r
144         }\r
145 \r
146         const frame_format_desc format_desc_;\r
147 \r
148         frame_producer_ptr              source_;\r
149         frame_producer_ptr              dest_;\r
150         \r
151         unsigned short                  current_frame_;\r
152         \r
153         const transition_info   info_;\r
154         frame_factory_ptr               factory_;\r
155 };\r
156 \r
157 transition_producer::transition_producer(const frame_producer_ptr& dest, const transition_info& info, const frame_format_desc& format_desc) \r
158         : impl_(new implementation(dest, info, format_desc)){}\r
159 gpu_frame_ptr transition_producer::get_frame(){return impl_->get_frame();}\r
160 frame_producer_ptr transition_producer::get_following_producer() const{return impl_->get_following_producer();}\r
161 void transition_producer::set_leading_producer(const frame_producer_ptr& producer) { impl_->set_leading_producer(producer); }\r
162 const frame_format_desc& transition_producer::get_frame_format_desc() const { return impl_->format_desc_; } \r
163 void transition_producer::initialize(const frame_factory_ptr& factory) { impl_->initialize(factory);}\r
164 \r
165 }}\r
166 \r