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