]> git.sesse.net Git - casparcg/blob - core/producer/transition/transition_producer.cpp
- Implemented real-time state notification using OSC-UDP.
[casparcg] / core / producer / transition / transition_producer.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\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 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "../../stdafx.h"\r
23 \r
24 #include "transition_producer.h"\r
25 \r
26 #include <core/video_format.h>\r
27 \r
28 #include <core/producer/frame/basic_frame.h>\r
29 #include <core/producer/frame/frame_transform.h>\r
30 \r
31 #include <tbb/parallel_invoke.h>\r
32 \r
33 #include <boost/assign.hpp>\r
34 \r
35 using namespace boost::assign;\r
36 \r
37 namespace caspar { namespace core {     \r
38 \r
39 struct transition_producer : public frame_producer\r
40 {       \r
41         monitor::subject                        monitor_subject_;\r
42 \r
43         const field_mode::type          mode_;\r
44         unsigned int                            current_frame_;\r
45         \r
46         const transition_info           info_;\r
47         \r
48         safe_ptr<frame_producer>        dest_producer_;\r
49         safe_ptr<frame_producer>        source_producer_;\r
50 \r
51         safe_ptr<basic_frame>           last_frame_;\r
52                 \r
53         explicit transition_producer(const field_mode::type& mode, const safe_ptr<frame_producer>& dest, const transition_info& info) \r
54                 : mode_(mode)\r
55                 , current_frame_(0)\r
56                 , info_(info)\r
57                 , dest_producer_(dest)\r
58                 , source_producer_(frame_producer::empty())\r
59                 , last_frame_(basic_frame::empty())\r
60         {\r
61                 dest->monitor_output().link_target(&monitor_subject_);\r
62         }\r
63         \r
64         // frame_producer\r
65 \r
66         virtual safe_ptr<frame_producer> get_following_producer() const override\r
67         {\r
68                 return dest_producer_;\r
69         }\r
70         \r
71         virtual void set_leading_producer(const safe_ptr<frame_producer>& producer) override\r
72         {\r
73                 source_producer_ = producer;\r
74         }\r
75 \r
76         virtual boost::unique_future<std::wstring> call(const std::wstring& params) override\r
77         {\r
78                 return dest_producer_->call(params);\r
79         }\r
80 \r
81         virtual safe_ptr<basic_frame> receive(int hints) override\r
82         {\r
83                 if(++current_frame_ >= info_.duration)\r
84                         return basic_frame::eof();\r
85                 \r
86                 auto dest = basic_frame::empty();\r
87                 auto source = basic_frame::empty();\r
88 \r
89                 tbb::parallel_invoke(\r
90                 [&]\r
91                 {\r
92                         dest = receive_and_follow(dest_producer_, hints);\r
93                         if(dest == core::basic_frame::late())\r
94                                 dest = dest_producer_->last_frame();\r
95                 },\r
96                 [&]\r
97                 {\r
98                         source = receive_and_follow(source_producer_, hints);\r
99                         if(source == core::basic_frame::late())\r
100                                 source = source_producer_->last_frame();\r
101                 });\r
102 \r
103                 monitor_subject_ << monitor::message("/transition/frame") % static_cast<std::int32_t>(current_frame_) % static_cast<std::int32_t>(info_.duration)\r
104                                                  << monitor::message("/transition/type") % [&]() -> std::string\r
105                                                                                                                                 {\r
106                                                                                                                                         switch(info_.type)\r
107                                                                                                                                         {\r
108                                                                                                                                         case transition::mix:   return "mix";\r
109                                                                                                                                         case transition::wipe:  return "wipe";\r
110                                                                                                                                         case transition::slide: return "slide";\r
111                                                                                                                                         case transition::push:  return "push";\r
112                                                                                                                                         case transition::cut:   return "cut";\r
113                                                                                                                                         default:                                return "n/a";\r
114                                                                                                                                         }\r
115                                                                                                                                 }();\r
116 \r
117                 return compose(dest, source);\r
118         }\r
119 \r
120         virtual safe_ptr<core::basic_frame> last_frame() const override\r
121         {\r
122                 return disable_audio(last_frame_);\r
123         }\r
124 \r
125         virtual uint32_t nb_frames() const override\r
126         {\r
127                 return get_following_producer()->nb_frames();\r
128         }\r
129 \r
130         virtual std::wstring print() const override\r
131         {\r
132                 return L"transition[" + source_producer_->print() + L"=>" + dest_producer_->print() + L"]";\r
133         }\r
134         \r
135         boost::property_tree::wptree info() const override\r
136         {\r
137                 boost::property_tree::wptree info;\r
138                 info.add(L"type", L"transition-producer");\r
139                 info.add_child(L"source.producer",         source_producer_->info());\r
140                 info.add_child(L"destination.producer", dest_producer_->info());\r
141                 return info;\r
142         }\r
143 \r
144         // transition_producer\r
145                                                 \r
146         safe_ptr<basic_frame> compose(const safe_ptr<basic_frame>& dest_frame, const safe_ptr<basic_frame>& src_frame) \r
147         {       \r
148                 if(info_.type == transition::cut)               \r
149                         return src_frame;\r
150                                                                                 \r
151                 const double delta1 = info_.tweener(current_frame_*2-1, 0.0, 1.0, info_.duration*2);\r
152                 const double delta2 = info_.tweener(current_frame_*2, 0.0, 1.0, info_.duration*2);  \r
153 \r
154                 const double dir = info_.direction == transition_direction::from_left ? 1.0 : -1.0;             \r
155                 \r
156                 // For interlaced transitions. Seperate fields into seperate frames which are transitioned accordingly.\r
157                 \r
158                 auto s_frame1 = make_safe<basic_frame>(src_frame);\r
159                 auto s_frame2 = make_safe<basic_frame>(src_frame);\r
160 \r
161                 s_frame1->get_frame_transform().volume = 0.0;\r
162                 s_frame2->get_frame_transform().volume = 1.0-delta2;\r
163 \r
164                 auto d_frame1 = make_safe<basic_frame>(dest_frame);\r
165                 auto d_frame2 = make_safe<basic_frame>(dest_frame);\r
166                 \r
167                 d_frame1->get_frame_transform().volume = 0.0;\r
168                 d_frame2->get_frame_transform().volume = delta2;\r
169 \r
170                 if(info_.type == transition::mix)\r
171                 {\r
172                         d_frame1->get_frame_transform().opacity = delta1;       \r
173                         d_frame1->get_frame_transform().is_mix = true;\r
174                         d_frame2->get_frame_transform().opacity = delta2;\r
175                         d_frame2->get_frame_transform().is_mix = true;\r
176 \r
177                         s_frame1->get_frame_transform().opacity = 1.0-delta1;   \r
178                         s_frame1->get_frame_transform().is_mix = true;\r
179                         s_frame2->get_frame_transform().opacity = 1.0-delta2;   \r
180                         s_frame2->get_frame_transform().is_mix = true;\r
181                 }\r
182                 if(info_.type == transition::slide)\r
183                 {\r
184                         d_frame1->get_frame_transform().fill_translation[0] = (-1.0+delta1)*dir;        \r
185                         d_frame2->get_frame_transform().fill_translation[0] = (-1.0+delta2)*dir;                \r
186                 }\r
187                 else if(info_.type == transition::push)\r
188                 {\r
189                         d_frame1->get_frame_transform().fill_translation[0] = (-1.0+delta1)*dir;\r
190                         d_frame2->get_frame_transform().fill_translation[0] = (-1.0+delta2)*dir;\r
191 \r
192                         s_frame1->get_frame_transform().fill_translation[0] = (0.0+delta1)*dir; \r
193                         s_frame2->get_frame_transform().fill_translation[0] = (0.0+delta2)*dir;         \r
194                 }\r
195                 else if(info_.type == transition::wipe)         \r
196                 {\r
197                         d_frame1->get_frame_transform().clip_scale[0] = delta1; \r
198                         d_frame2->get_frame_transform().clip_scale[0] = delta2;                 \r
199                 }\r
200                                 \r
201                 const auto s_frame = s_frame1->get_frame_transform() == s_frame2->get_frame_transform() ? s_frame2 : basic_frame::interlace(s_frame1, s_frame2, mode_);\r
202                 const auto d_frame = d_frame1->get_frame_transform() == d_frame2->get_frame_transform() ? d_frame2 : basic_frame::interlace(d_frame1, d_frame2, mode_);\r
203                 \r
204                 last_frame_ = basic_frame::combine(s_frame2, d_frame2);\r
205 \r
206                 return basic_frame::combine(s_frame, d_frame);\r
207         }\r
208 \r
209         monitor::source& monitor_output()\r
210         {\r
211                 return monitor_subject_;\r
212         }\r
213 };\r
214 \r
215 safe_ptr<frame_producer> create_transition_producer(const field_mode::type& mode, const safe_ptr<frame_producer>& destination, const transition_info& info)\r
216 {\r
217         return create_producer_print_proxy(\r
218                         make_safe<transition_producer>(mode, destination, info));\r
219 }\r
220 \r
221 }}\r
222 \r