]> git.sesse.net Git - casparcg/blob - core/mixer/frame_mixer_device.cpp
2.0.0.2:
[casparcg] / core / mixer / frame_mixer_device.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 "frame_mixer_device.h"\r
23 \r
24 #include "read_frame.h"\r
25 #include "write_frame.h"\r
26 \r
27 #include "audio/audio_mixer.h"\r
28 #include "image/image_mixer.h"\r
29 \r
30 #include "../video_channel_context.h"\r
31 \r
32 #include <common/exception/exceptions.h>\r
33 #include <common/concurrency/executor.h>\r
34 #include <common/diagnostics/graph.h>\r
35 #include <common/utility/tweener.h>\r
36 \r
37 \r
38 #include <core/mixer/read_frame.h>\r
39 #include <core/mixer/write_frame.h>\r
40 #include <core/producer/frame/basic_frame.h>\r
41 #include <core/producer/frame/frame_factory.h>\r
42 #include <core/producer/frame/pixel_format.h>\r
43 #include <core/producer/frame/audio_transform.h>\r
44 #include <core/producer/frame/image_transform.h>\r
45 \r
46 #include <core/video_format.h>\r
47 \r
48 #include <boost/fusion/container/map.hpp>\r
49 #include <boost/fusion/include/at_key.hpp>\r
50 #include <boost/timer.hpp>\r
51 \r
52 #include <unordered_map>\r
53 \r
54 namespace caspar { namespace core {\r
55                 \r
56 template<typename T>\r
57 class tweened_transform\r
58 {\r
59         T source_;\r
60         T dest_;\r
61         int duration_;\r
62         int time_;\r
63         tweener_t tweener_;\r
64 public: \r
65         tweened_transform()\r
66                 : duration_(0)\r
67                 , time_(0)\r
68                 , tweener_(get_tweener(L"linear")){}\r
69         tweened_transform(const T& source, const T& dest, int duration, const std::wstring& tween = L"linear")\r
70                 : source_(source)\r
71                 , dest_(dest)\r
72                 , duration_(duration)\r
73                 , time_(0)\r
74                 , tweener_(get_tweener(tween)){}\r
75         \r
76         T fetch()\r
77         {\r
78                 return time_ == duration_ ? dest_ : tween(static_cast<double>(time_), source_, dest_, static_cast<double>(duration_), tweener_);\r
79         }\r
80 \r
81         T fetch_and_tick(int num)\r
82         {                                               \r
83                 time_ = std::min(time_+num, duration_);\r
84                 return fetch();\r
85         }\r
86 };\r
87 \r
88 struct frame_mixer_device::implementation : boost::noncopyable\r
89 {               \r
90         video_channel_context& channel_;\r
91 \r
92         safe_ptr<diagnostics::graph> diag_;\r
93         boost::timer frame_timer_;\r
94         boost::timer tick_timer_;\r
95 \r
96         audio_mixer     audio_mixer_;\r
97         image_mixer image_mixer_;\r
98         \r
99         typedef std::unordered_map<int, tweened_transform<core::image_transform>> image_transforms;\r
100         typedef std::unordered_map<int, tweened_transform<core::audio_transform>> audio_transforms;\r
101 \r
102         boost::fusion::map<boost::fusion::pair<core::image_transform, image_transforms>,\r
103                                         boost::fusion::pair<core::audio_transform, audio_transforms>> transforms_;\r
104         \r
105         boost::fusion::map<boost::fusion::pair<core::image_transform, tweened_transform<core::image_transform>>,\r
106                                         boost::fusion::pair<core::audio_transform, tweened_transform<core::audio_transform>>> root_transforms_;\r
107 public:\r
108         implementation(video_channel_context& video_channel) \r
109                 : channel_(video_channel)\r
110                 , diag_(diagnostics::create_graph(narrow(print())))\r
111                 , image_mixer_(channel_)\r
112         {\r
113                 diag_->add_guide("frame-time", 0.5f);   \r
114                 diag_->set_color("frame-time", diagnostics::color(1.0f, 0.0f, 0.0f));\r
115                 diag_->set_color("tick-time", diagnostics::color(0.1f, 0.7f, 0.8f));\r
116         \r
117                 CASPAR_LOG(info) << print() << L" Successfully initialized.";   \r
118         }\r
119                         \r
120         safe_ptr<read_frame> operator()(const std::map<int, safe_ptr<core::basic_frame>>& frames)\r
121         {                               \r
122                 frame_timer_.restart();\r
123 \r
124                 auto image = mix_image(frames);\r
125                 auto audio = mix_audio(frames);\r
126                         \r
127                 diag_->update_value("frame-time", frame_timer_.elapsed()*channel_.format_desc.fps*0.5);\r
128                 \r
129                 diag_->update_value("tick-time", tick_timer_.elapsed()*channel_.format_desc.fps*0.5);\r
130                 tick_timer_.restart();\r
131 \r
132                 return make_safe<read_frame>(std::move(image), std::move(audio));\r
133         }\r
134                         \r
135         safe_ptr<core::write_frame> create_frame(void* tag, const core::pixel_format_desc& desc)\r
136         {               \r
137                 return image_mixer_.create_frame(tag, desc);\r
138         }\r
139                         \r
140         template<typename T>    \r
141         void set_transform(const T& transform, unsigned int mix_duration, const std::wstring& tween)\r
142         {\r
143                 channel_.execution.invoke([&]\r
144                 {\r
145                         auto& root = boost::fusion::at_key<T>(root_transforms_);\r
146 \r
147                         auto src = root.fetch();\r
148                         auto dst = transform;\r
149                         root = tweened_transform<T>(src, dst, mix_duration, tween);\r
150                 });\r
151         }\r
152                 \r
153         template<typename T>\r
154         void set_transform(int index, const T& transform, unsigned int mix_duration, const std::wstring& tween)\r
155         {\r
156                 channel_.execution.invoke([&]\r
157                 {\r
158                         auto& transforms = boost::fusion::at_key<T>(transforms_);\r
159 \r
160                         auto src = transforms[index].fetch();\r
161                         auto dst = transform;\r
162                         transforms[index] = tweened_transform<T>(src, dst, mix_duration, tween);\r
163                 });\r
164         }\r
165                 \r
166         template<typename T>\r
167         void apply_transform(const std::function<T(const T&)>& transform, unsigned int mix_duration, const std::wstring& tween)\r
168         {\r
169                 return channel_.execution.invoke([&]\r
170                 {\r
171                         auto& root = boost::fusion::at_key<T>(root_transforms_);\r
172 \r
173                         auto src = root.fetch();\r
174                         auto dst = transform(src);\r
175                         root = tweened_transform<T>(src, dst, mix_duration, tween);\r
176                 });\r
177         }\r
178                 \r
179         template<typename T>\r
180         void apply_transform(int index, const std::function<T(T)>& transform, unsigned int mix_duration, const std::wstring& tween)\r
181         {\r
182                 channel_.execution.invoke([&]\r
183                 {\r
184                         auto& transforms = boost::fusion::at_key<T>(transforms_);\r
185 \r
186                         auto src = transforms[index].fetch();\r
187                         auto dst = transform(src);\r
188                         transforms[index] = tweened_transform<T>(src, dst, mix_duration, tween);\r
189                 });\r
190         }\r
191 \r
192         template<typename T>\r
193         void reset_transform(unsigned int mix_duration, const std::wstring& tween)\r
194         {\r
195                 channel_.execution.invoke([&]\r
196                 {\r
197                         auto& transforms = boost::fusion::at_key<T>(transforms_);\r
198 \r
199                         BOOST_FOREACH(auto& t, transforms)                      \r
200                                  t.second = tweened_transform<T>(t.second.fetch(), T(), mix_duration, tween);                   \r
201                         set_transform(T(), mix_duration, tween);\r
202                 });\r
203         }\r
204 \r
205         template<typename T>\r
206         void reset_transform(int index, unsigned int mix_duration, const std::wstring& tween)\r
207         {\r
208                 channel_.execution.invoke([&]\r
209                 {               \r
210                         set_transform(T(), mix_duration, tween);\r
211                 });\r
212         }\r
213         \r
214         std::wstring print() const\r
215         {\r
216                 return L"frame_mixer_device";\r
217         }\r
218 \r
219 private:\r
220                 \r
221         safe_ptr<host_buffer> mix_image(std::map<int, safe_ptr<core::basic_frame>> frames)\r
222         {               \r
223                 auto& root_image_transform = boost::fusion::at_key<core::image_transform>(root_transforms_);\r
224                 auto& image_transforms = boost::fusion::at_key<core::image_transform>(transforms_);\r
225                 \r
226                 BOOST_FOREACH(auto& frame, frames)\r
227                 {\r
228                         image_mixer_.begin_layer();\r
229                         \r
230                         if(channel_.format_desc.mode != core::video_mode::progressive)\r
231                         {\r
232                                 auto frame1 = make_safe<core::basic_frame>(frame.second);\r
233                                 auto frame2 = make_safe<core::basic_frame>(frame.second);\r
234                                 \r
235                                 frame1->get_image_transform() = root_image_transform.fetch_and_tick(1)*image_transforms[frame.first].fetch_and_tick(1);\r
236                                 frame2->get_image_transform() = root_image_transform.fetch_and_tick(1)*image_transforms[frame.first].fetch_and_tick(1);\r
237 \r
238                                 if(frame1->get_image_transform() != frame2->get_image_transform())\r
239                                         core::basic_frame::interlace(frame1, frame2, channel_.format_desc.mode)->accept(image_mixer_);\r
240                                 else\r
241                                         frame2->accept(image_mixer_);\r
242                         }\r
243                         else\r
244                         {\r
245                                 auto frame1 = make_safe<core::basic_frame>(frame.second);\r
246                                 frame1->get_image_transform() = root_image_transform.fetch_and_tick(1)*image_transforms[frame.first].fetch_and_tick(1);\r
247                                 frame1->accept(image_mixer_);\r
248                         }\r
249 \r
250                         image_mixer_.end_layer();\r
251                 }\r
252 \r
253                 return image_mixer_.render();\r
254         }\r
255 \r
256         std::vector<int16_t> mix_audio(const std::map<int, safe_ptr<core::basic_frame>>& frames)\r
257         {\r
258                 auto& root_audio_transform = boost::fusion::at_key<core::audio_transform>(root_transforms_);\r
259                 auto& audio_transforms = boost::fusion::at_key<core::audio_transform>(transforms_);\r
260 \r
261                 BOOST_FOREACH(auto& frame, frames)\r
262                 {\r
263                         const unsigned int num = channel_.format_desc.mode == core::video_mode::progressive ? 1 : 2;\r
264 \r
265                         auto frame1 = make_safe<core::basic_frame>(frame.second);\r
266                         frame1->get_audio_transform() = root_audio_transform.fetch_and_tick(num)*audio_transforms[frame.first].fetch_and_tick(num);\r
267                         frame1->accept(audio_mixer_);\r
268                 }\r
269 \r
270                 return audio_mixer_.mix();\r
271         }\r
272 };\r
273         \r
274 frame_mixer_device::frame_mixer_device(video_channel_context& video_channel) : impl_(new implementation(video_channel)){}\r
275 safe_ptr<core::read_frame> frame_mixer_device::operator()(const std::map<int, safe_ptr<core::basic_frame>>& frames){ return (*impl_)(frames);}\r
276 const core::video_format_desc& frame_mixer_device::get_video_format_desc() const { return impl_->channel_.format_desc; }\r
277 safe_ptr<core::write_frame> frame_mixer_device::create_frame(void* tag, const core::pixel_format_desc& desc){ return impl_->create_frame(tag, desc); }          \r
278 safe_ptr<core::write_frame> frame_mixer_device::create_frame(void* tag, size_t width, size_t height, core::pixel_format::type pix_fmt)\r
279 {\r
280         // Create bgra frame\r
281         core::pixel_format_desc desc;\r
282         desc.pix_fmt = pix_fmt;\r
283         desc.planes.push_back( core::pixel_format_desc::plane(width, height, 4));\r
284         return create_frame(tag, desc);\r
285 }\r
286 void frame_mixer_device::set_image_transform(const core::image_transform& transform, unsigned int mix_duration, const std::wstring& tween){impl_->set_transform<core::image_transform>(transform, mix_duration, tween);}\r
287 void frame_mixer_device::set_image_transform(int index, const core::image_transform& transform, unsigned int mix_duration, const std::wstring& tween){impl_->set_transform<core::image_transform>(index, transform, mix_duration, tween);}\r
288 void frame_mixer_device::set_audio_transform(const core::audio_transform& transform, unsigned int mix_duration, const std::wstring& tween){impl_->set_transform<core::audio_transform>(transform, mix_duration, tween);}\r
289 void frame_mixer_device::set_audio_transform(int index, const core::audio_transform& transform, unsigned int mix_duration, const std::wstring& tween){impl_->set_transform<core::audio_transform>(index, transform, mix_duration, tween);}\r
290 void frame_mixer_device::apply_image_transform(const std::function<core::image_transform(core::image_transform)>& transform, unsigned int mix_duration, const std::wstring& tween){impl_->apply_transform<core::image_transform>(transform, mix_duration, tween);}\r
291 void frame_mixer_device::apply_image_transform(int index, const std::function<core::image_transform(core::image_transform)>& transform, unsigned int mix_duration, const std::wstring& tween){impl_->apply_transform<core::image_transform>(index, transform, mix_duration, tween);}\r
292 void frame_mixer_device::apply_audio_transform(const std::function<core::audio_transform(core::audio_transform)>& transform, unsigned int mix_duration, const std::wstring& tween){impl_->apply_transform<core::audio_transform>(transform, mix_duration, tween);}\r
293 void frame_mixer_device::apply_audio_transform(int index, const std::function<core::audio_transform(core::audio_transform)>& transform, unsigned int mix_duration, const std::wstring& tween){impl_->apply_transform<core::audio_transform>(index, transform, mix_duration, tween);}\r
294 void frame_mixer_device::reset_image_transform(unsigned int mix_duration, const std::wstring& tween){impl_->reset_transform<core::image_transform>(mix_duration, tween);}\r
295 void frame_mixer_device::reset_image_transform(int index, unsigned int mix_duration, const std::wstring& tween){impl_->reset_transform<core::image_transform>(index, mix_duration, tween);}\r
296 void frame_mixer_device::reset_audio_transform(unsigned int mix_duration, const std::wstring& tween){impl_->reset_transform<core::audio_transform>(mix_duration, tween);}\r
297 void frame_mixer_device::reset_audio_transform(int index, unsigned int mix_duration, const std::wstring& tween){impl_->reset_transform<core::audio_transform>(index, mix_duration, tween);}\r
298 }}