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