]> git.sesse.net Git - casparcg/blob - core/producer/transition/transition_producer.cpp
2.0.0.2: Progress on animated values.
[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/image/image_transform.h>\r
28 #include <mixer/audio/audio_mixer.h>\r
29 #include <mixer/audio/audio_transform.h>\r
30 \r
31 namespace caspar { namespace core {     \r
32 \r
33 struct transition_producer::implementation : boost::noncopyable\r
34 {       \r
35         printer parent_printer_;\r
36         unsigned short                          current_frame_;\r
37         \r
38         const transition_info           info_;\r
39         \r
40         safe_ptr<frame_producer>        dest_producer_;\r
41         safe_ptr<frame_producer>        source_producer_;\r
42 \r
43         std::shared_ptr<frame_factory>  frame_factory_;\r
44         video_format_desc                               format_desc_;\r
45 \r
46         std::vector<safe_ptr<draw_frame>> frame_buffer_;\r
47         \r
48         implementation(const safe_ptr<frame_producer>& dest, const transition_info& info) \r
49                 : current_frame_(0)\r
50                 , info_(info)\r
51                 , dest_producer_(dest)\r
52                 , source_producer_(frame_producer::empty())\r
53         {\r
54                 dest_producer_->set_parent_printer(std::bind(&implementation::dest_print, this));\r
55                 frame_buffer_.push_back(draw_frame::empty());\r
56         }\r
57                                 \r
58         void initialize(const safe_ptr<frame_factory>& frame_factory)\r
59         {\r
60                 dest_producer_->initialize(frame_factory);\r
61                 frame_factory_ = frame_factory;\r
62                 format_desc_ = frame_factory_->get_video_format_desc();\r
63         }\r
64 \r
65         virtual void set_parent_printer(const printer& parent_printer) \r
66         {\r
67                 parent_printer_ = parent_printer;\r
68         }\r
69 \r
70         safe_ptr<frame_producer> get_following_producer() const\r
71         {\r
72                 return dest_producer_;\r
73         }\r
74         \r
75         void set_leading_producer(const safe_ptr<frame_producer>& producer)\r
76         {\r
77                 source_producer_ = producer;\r
78                 source_producer_->set_parent_printer(std::bind(&implementation::source_print, this));\r
79         }\r
80 \r
81         safe_ptr<draw_frame> receive()\r
82         {\r
83                 if(current_frame_++ >= info_.duration)\r
84                         return draw_frame::eof();\r
85 \r
86                 auto source = draw_frame::empty();\r
87                 auto dest = draw_frame::empty();\r
88 \r
89                 tbb::parallel_invoke\r
90                 (\r
91                         [&]{dest   = render_sub_frame(dest_producer_);},\r
92                         [&]{source = render_sub_frame(source_producer_);}\r
93                 );\r
94 \r
95                 return compose(dest, source);\r
96         }\r
97         \r
98         safe_ptr<draw_frame> render_sub_frame(safe_ptr<frame_producer>& producer)\r
99         {\r
100                 if(producer == frame_producer::empty())\r
101                         return draw_frame::eof();\r
102 \r
103                 auto frame = draw_frame::eof();\r
104                 try\r
105                 {\r
106                         frame = producer->receive();\r
107                 }\r
108                 catch(...)\r
109                 {\r
110                         CASPAR_LOG_CURRENT_EXCEPTION();\r
111                         producer = frame_producer::empty();\r
112                         CASPAR_LOG(warning) << print() << " Failed to receive frame. Removed producer from transition.";\r
113                 }\r
114 \r
115                 if(frame == draw_frame::eof())\r
116                 {\r
117                         try\r
118                         {\r
119                                 auto following = producer->get_following_producer();\r
120                                 following->initialize(safe_ptr<frame_factory>(frame_factory_));\r
121                                 following->set_leading_producer(producer);\r
122                                 producer = std::move(following);\r
123                         }\r
124                         catch(...)\r
125                         {\r
126                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
127                                 producer = frame_producer::empty();\r
128                                 CASPAR_LOG(warning) << print() << " Failed to initialize following producer.";\r
129                         }\r
130 \r
131                         return render_sub_frame(producer);\r
132                 }\r
133                 return frame;\r
134         }\r
135                                         \r
136         safe_ptr<draw_frame> compose(const safe_ptr<draw_frame>& dest_frame, const safe_ptr<draw_frame>& src_frame) \r
137         {       \r
138                 if(dest_frame == draw_frame::eof() && src_frame == draw_frame::eof())\r
139                         return draw_frame::eof();\r
140 \r
141                 if(info_.type == transition::cut)               \r
142                         return src_frame != draw_frame::eof() ? src_frame : draw_frame::empty();\r
143                                                                                 \r
144                 double alpha = static_cast<double>(current_frame_)/static_cast<double>(info_.duration);\r
145                 double half_alpha_step = 0.5*1.0/static_cast<double>(info_.duration);\r
146                 \r
147                 double dir = info_.direction == transition_direction::from_left ? 1.0 : -1.0;           \r
148                 \r
149                 // For interalced transitions; seperate fields into seperate frames which are transitioned accordingly.\r
150 \r
151                 auto s_frame1 = make_safe<draw_frame>(src_frame);\r
152                 auto s_frame2 = make_safe<draw_frame>(src_frame);\r
153 \r
154                 s_frame1->get_audio_transform().set_gain(0);\r
155                 s_frame2->get_audio_transform().set_gain(1.0-alpha);\r
156 \r
157                 auto d_frame1 = make_safe<draw_frame>(dest_frame);\r
158                 auto d_frame2 = make_safe<draw_frame>(dest_frame);\r
159                 \r
160                 d_frame1->get_audio_transform().set_gain(0);\r
161                 d_frame2->get_audio_transform().set_gain(alpha);\r
162 \r
163                 if(info_.type == transition::mix)\r
164                 {\r
165                         d_frame1->get_image_transform().set_opacity(alpha-half_alpha_step);     \r
166                         d_frame2->get_image_transform().set_opacity(alpha);     \r
167                 }\r
168                 else if(info_.type == transition::slide)\r
169                 {\r
170                         d_frame1->get_image_transform().set_fill_translation((-1.0+alpha-half_alpha_step)*dir, 0.0);    \r
171                         d_frame2->get_image_transform().set_fill_translation((-1.0+alpha)*dir, 0.0);            \r
172                 }\r
173                 else if(info_.type == transition::push)\r
174                 {\r
175                         d_frame1->get_image_transform().set_fill_translation((-1.0+alpha-half_alpha_step)*dir, 0.0);\r
176                         d_frame2->get_image_transform().set_fill_translation((-1.0+alpha)*dir, 0.0);\r
177 \r
178                         s_frame1->get_image_transform().set_fill_translation((0.0+alpha-half_alpha_step)*dir, 0.0);     \r
179                         s_frame2->get_image_transform().set_fill_translation((0.0+alpha)*dir, 0.0);             \r
180                 }\r
181                 else if(info_.type == transition::wipe)         \r
182                 {\r
183                         d_frame1->get_image_transform().set_key_scale(alpha-half_alpha_step, 1.0);      \r
184                         d_frame2->get_image_transform().set_key_scale(alpha, 1.0);                      \r
185                 }\r
186                 \r
187                 return draw_frame(draw_frame::interlace(s_frame1, s_frame2, format_desc_.mode), draw_frame::interlace(d_frame1, d_frame2, format_desc_.mode));\r
188         }\r
189 \r
190         std::wstring print() const\r
191         {\r
192                 return (parent_printer_ ? parent_printer_() + L"/" : L"") + L"transition[" + info_.name() + L":" + boost::lexical_cast<std::wstring>(info_.duration) + L"]";\r
193         }\r
194 \r
195         std::wstring source_print() const { return print() + L"/source";}\r
196         std::wstring dest_print() const { return print() + L"/dest";}\r
197 };\r
198 \r
199 transition_producer::transition_producer(transition_producer&& other) : impl_(std::move(other.impl_)){}\r
200 transition_producer::transition_producer(const safe_ptr<frame_producer>& dest, const transition_info& info) : impl_(new implementation(dest, info)){}\r
201 safe_ptr<draw_frame> transition_producer::receive(){return impl_->receive();}\r
202 safe_ptr<frame_producer> transition_producer::get_following_producer() const{return impl_->get_following_producer();}\r
203 void transition_producer::set_leading_producer(const safe_ptr<frame_producer>& producer) { impl_->set_leading_producer(producer); }\r
204 void transition_producer::initialize(const safe_ptr<frame_factory>& frame_factory) { impl_->initialize(frame_factory);}\r
205 void transition_producer::set_parent_printer(const printer& parent_printer) {impl_->set_parent_printer(parent_printer);}\r
206 std::wstring transition_producer::print() const { return impl_->print();}\r
207 \r
208 }}\r
209 \r