]> git.sesse.net Git - casparcg/blob - core/producer/transition/transition_producer.cpp
2.1.0: -transition_producer: Fixed problem with unpausing paused source clip.
[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 class transition_producer : public frame_producer_base\r
36 {       \r
37         monitor::basic_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 public:\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         {\r
55                 dest->subscribe(event_subject_);\r
56 \r
57                 CASPAR_LOG(info) << print() << L" Initialized";\r
58         }\r
59         \r
60         // frame_producer\r
61                 \r
62         void leading_producer(const spl::shared_ptr<frame_producer>& producer) override\r
63         {\r
64                 source_producer_ = producer;\r
65         }\r
66 \r
67         draw_frame receive_impl() override\r
68         {\r
69                 if(current_frame_ >= info_.duration)\r
70                 {\r
71                         source_producer_ = core::frame_producer::empty();\r
72                         return dest_producer_->receive();\r
73                 }\r
74 \r
75                 ++current_frame_;\r
76 \r
77                 auto dest = draw_frame::empty();\r
78                 auto source = draw_frame::empty();\r
79 \r
80                 tbb::parallel_invoke(\r
81                 [&]\r
82                 {\r
83                         dest = dest_producer_->receive();\r
84                         if(dest == core::draw_frame::late())\r
85                                 dest = dest_producer_->last_frame();\r
86                 },\r
87                 [&]\r
88                 {\r
89                         source = source_producer_->receive();\r
90                         if(source == core::draw_frame::late())\r
91                                 source = source_producer_->last_frame();\r
92                 });                             \r
93                 \r
94                 event_subject_  << monitor::event("transition/frame") % current_frame_ % info_.duration\r
95                                                 << monitor::event("transition/type") % [&]() -> std::string\r
96                                                                                                                                 {\r
97                                                                                                                                         switch(info_.type.value())\r
98                                                                                                                                         {\r
99                                                                                                                                         case transition_type::mix:              return "mix";\r
100                                                                                                                                         case transition_type::wipe:             return "wipe";\r
101                                                                                                                                         case transition_type::slide:    return "slide";\r
102                                                                                                                                         case transition_type::push:             return "push";\r
103                                                                                                                                         case transition_type::cut:              return "cut";\r
104                                                                                                                                         default:                                                return "n/a";\r
105                                                                                                                                         }\r
106                                                                                                                                 }();\r
107 \r
108                 return compose(dest, source);\r
109         }\r
110         \r
111         void paused(bool value) override\r
112         {\r
113                 dest_producer_->paused(value);\r
114         }\r
115                 \r
116         uint32_t nb_frames() const override\r
117         {\r
118                 return dest_producer_->nb_frames();\r
119         }\r
120 \r
121         std::wstring print() const override\r
122         {\r
123                 return L"transition[" + source_producer_->print() + L"=>" + dest_producer_->print() + L"]";\r
124         }\r
125 \r
126         std::wstring name() const override\r
127         {\r
128                 return L"transition";\r
129         }\r
130         \r
131         boost::property_tree::wptree info() const override\r
132         {\r
133                 return dest_producer_->info();\r
134         }\r
135         \r
136         boost::unique_future<std::wstring> call(const std::wstring& str) override\r
137         {\r
138                 return dest_producer_->call(str);\r
139         }\r
140 \r
141         // transition_producer\r
142                                                 \r
143         draw_frame compose(draw_frame dest_frame, draw_frame src_frame) \r
144         {       \r
145                 if(info_.type == transition_type::cut)          \r
146                         return src_frame;\r
147                                                                                 \r
148                 const double delta1 = info_.tweener(current_frame_*2-1, 0.0, 1.0, static_cast<double>(info_.duration*2));\r
149                 const double delta2 = info_.tweener(current_frame_*2,   0.0, 1.0, static_cast<double>(info_.duration*2));  \r
150 \r
151                 const double dir = info_.direction == transition_direction::from_left ? 1.0 : -1.0;             \r
152                 \r
153                 // For interlaced transitions. Seperate fields into seperate frames which are transitioned accordingly.\r
154                 \r
155                 src_frame.transform().audio_transform.volume = 1.0-delta2;\r
156                 auto s_frame1 = src_frame;\r
157                 auto s_frame2 = src_frame;\r
158                 \r
159                 dest_frame.transform().audio_transform.volume = delta2;\r
160                 auto d_frame1 = dest_frame;\r
161                 auto d_frame2 = dest_frame;\r
162                 \r
163                 if(info_.type == transition_type::mix)\r
164                 {\r
165                         d_frame1.transform().image_transform.opacity = delta1;  \r
166                         d_frame1.transform().image_transform.is_mix = true;\r
167                         d_frame2.transform().image_transform.opacity = delta2;\r
168                         d_frame2.transform().image_transform.is_mix = true;\r
169 \r
170                         s_frame1.transform().image_transform.opacity = 1.0-delta1;      \r
171                         s_frame1.transform().image_transform.is_mix = true;\r
172                         s_frame2.transform().image_transform.opacity = 1.0-delta2;      \r
173                         s_frame2.transform().image_transform.is_mix = true;\r
174                 }\r
175                 if(info_.type == transition_type::slide)\r
176                 {\r
177                         d_frame1.transform().image_transform.fill_translation[0] = (-1.0+delta1)*dir;   \r
178                         d_frame2.transform().image_transform.fill_translation[0] = (-1.0+delta2)*dir;           \r
179                 }\r
180                 else if(info_.type == transition_type::push)\r
181                 {\r
182                         d_frame1.transform().image_transform.fill_translation[0] = (-1.0+delta1)*dir;\r
183                         d_frame2.transform().image_transform.fill_translation[0] = (-1.0+delta2)*dir;\r
184 \r
185                         s_frame1.transform().image_transform.fill_translation[0] = (0.0+delta1)*dir;    \r
186                         s_frame2.transform().image_transform.fill_translation[0] = (0.0+delta2)*dir;            \r
187                 }\r
188                 else if(info_.type == transition_type::wipe)            \r
189                 {\r
190                         d_frame1.transform().image_transform.clip_scale[0] = delta1;    \r
191                         d_frame2.transform().image_transform.clip_scale[0] = delta2;                    \r
192                 }\r
193                                 \r
194                 const auto s_frame = s_frame1.transform() == s_frame2.transform() ? s_frame2 : draw_frame::interlace(s_frame1, s_frame2, mode_);\r
195                 const auto d_frame = d_frame1.transform() == d_frame2.transform() ? d_frame2 : draw_frame::interlace(d_frame1, d_frame2, mode_);\r
196                 \r
197                 return draw_frame::over(s_frame, d_frame);\r
198         }\r
199 \r
200         void subscribe(const monitor::observable::observer_ptr& o) override                                                                                                                     \r
201         {\r
202                 event_subject_.subscribe(o);\r
203         }\r
204 \r
205         void unsubscribe(const monitor::observable::observer_ptr& o) override           \r
206         {\r
207                 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 spl::make_shared<transition_producer>(mode, destination, info);\r
214 }\r
215 \r
216 }}\r
217 \r