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