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