2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG.
\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
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
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
20 #include "../StdAfx.h"
\r
22 #include "frame_mixer_device.h"
\r
24 #include "read_frame.h"
\r
25 #include "write_frame.h"
\r
27 #include "audio/audio_mixer.h"
\r
28 #include "image/image_mixer.h"
\r
30 #include <common/exception/exceptions.h>
\r
31 #include <common/concurrency/executor.h>
\r
32 #include <common/diagnostics/graph.h>
\r
33 #include <common/utility/tweener.h>
\r
36 #include <core/mixer/read_frame.h>
\r
37 #include <core/mixer/write_frame.h>
\r
38 #include <core/producer/frame/basic_frame.h>
\r
39 #include <core/producer/frame/frame_factory.h>
\r
40 #include <core/producer/frame/pixel_format.h>
\r
41 #include <core/producer/frame/audio_transform.h>
\r
42 #include <core/producer/frame/image_transform.h>
\r
44 #include <core/video_format.h>
\r
46 #include <boost/fusion/container/map.hpp>
\r
47 #include <boost/fusion/include/at_key.hpp>
\r
48 #include <boost/timer.hpp>
\r
50 #include <unordered_map>
\r
52 namespace caspar { namespace core {
\r
54 template<typename T>
\r
55 class tweened_transform
\r
66 , tweener_(get_tweener(L"linear")){}
\r
67 tweened_transform(const T& source, const T& dest, int duration, const std::wstring& tween = L"linear")
\r
70 , duration_(duration)
\r
72 , tweener_(get_tweener(tween)){}
\r
76 return time_ == duration_ ? dest_ : tween(static_cast<double>(time_), source_, dest_, static_cast<double>(duration_), tweener_);
\r
79 T fetch_and_tick(int num)
\r
81 time_ = std::min(time_+num, duration_);
\r
86 struct frame_mixer_device::implementation : boost::noncopyable
\r
88 const core::video_format_desc format_desc_;
\r
90 safe_ptr<diagnostics::graph> diag_;
\r
91 boost::timer frame_timer_;
\r
92 boost::timer tick_timer_;
\r
94 audio_mixer audio_mixer_;
\r
95 image_mixer image_mixer_;
\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
102 boost::fusion::map<boost::fusion::pair<core::image_transform, image_transforms>,
\r
103 boost::fusion::pair<core::audio_transform, audio_transforms>> transforms_;
\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
108 executor executor_;
\r
110 implementation(const core::video_format_desc& format_desc)
\r
111 : format_desc_(format_desc)
\r
112 , diag_(diagnostics::create_graph(narrow(print())))
\r
113 , image_mixer_(format_desc)
\r
114 , executor_(L"frame_mixer_device", true)
\r
116 diag_->add_guide("frame-time", 0.5f);
\r
117 diag_->set_color("frame-time", diagnostics::color(1.0f, 0.0f, 0.0f));
\r
118 diag_->set_color("tick-time", diagnostics::color(0.1f, 0.7f, 0.8f));
\r
119 diag_->set_color("input-buffer", diagnostics::color(1.0f, 1.0f, 0.0f));
\r
120 executor_.set_capacity(1);
\r
121 executor_.begin_invoke([]
\r
123 SetThreadPriority(GetCurrentThread(), ABOVE_NORMAL_PRIORITY_CLASS);
\r
126 CASPAR_LOG(info) << print() << L" Successfully initialized.";
\r
129 boost::signals2::connection connect(const output_t::slot_type& subscriber)
\r
131 return output_.connect(subscriber);
\r
134 boost::unique_future<safe_ptr<const host_buffer>> mix_image(std::map<int, safe_ptr<core::basic_frame>> frames)
\r
136 auto& root_image_transform = boost::fusion::at_key<core::image_transform>(root_transforms_);
\r
137 auto& image_transforms = boost::fusion::at_key<core::image_transform>(transforms_);
\r
139 BOOST_FOREACH(auto& frame, frames)
\r
141 image_mixer_.begin_layer();
\r
143 if(format_desc_.mode != core::video_mode::progressive)
\r
145 auto frame1 = make_safe<core::basic_frame>(frame.second);
\r
146 auto frame2 = make_safe<core::basic_frame>(frame.second);
\r
148 frame1->get_image_transform() = root_image_transform.fetch_and_tick(1)*image_transforms[frame.first].fetch_and_tick(1);
\r
149 frame2->get_image_transform() = root_image_transform.fetch_and_tick(1)*image_transforms[frame.first].fetch_and_tick(1);
\r
151 if(frame1->get_image_transform() != frame2->get_image_transform())
\r
152 core::basic_frame::interlace(frame1, frame2, format_desc_.mode)->accept(image_mixer_);
\r
154 frame2->accept(image_mixer_);
\r
158 auto frame1 = make_safe<core::basic_frame>(frame.second);
\r
159 frame1->get_image_transform() = root_image_transform.fetch_and_tick(1)*image_transforms[frame.first].fetch_and_tick(1);
\r
160 frame1->accept(image_mixer_);
\r
163 image_mixer_.end_layer();
\r
166 return image_mixer_.render();
\r
169 std::vector<short> mix_audio(const std::map<int, safe_ptr<core::basic_frame>>& frames)
\r
171 auto& root_audio_transform = boost::fusion::at_key<core::audio_transform>(root_transforms_);
\r
172 auto& audio_transforms = boost::fusion::at_key<core::audio_transform>(transforms_);
\r
174 auto audio = audio_mixer_.begin_pass();
\r
175 BOOST_FOREACH(auto& frame, frames)
\r
177 int num = format_desc_.mode == core::video_mode::progressive ? 1 : 2;
\r
179 auto frame1 = make_safe<core::basic_frame>(frame.second);
\r
180 frame1->get_audio_transform() = root_audio_transform.fetch_and_tick(num)*audio_transforms[frame.first].fetch_and_tick(num);
\r
181 frame1->accept(audio_mixer_);
\r
183 audio_mixer_.end_pass();
\r
187 void send(const std::map<int, safe_ptr<core::basic_frame>>& frames)
\r
189 executor_.invoke([=]
\r
191 diag_->set_value("input-buffer", static_cast<float>(executor_.size())/static_cast<float>(executor_.capacity()));
\r
192 frame_timer_.restart();
\r
194 auto image = mix_image(frames);
\r
195 auto audio = mix_audio(frames);
\r
197 diag_->update_value("frame-time", static_cast<float>(frame_timer_.elapsed()*format_desc_.fps*0.5));
\r
199 output_(make_safe<const read_frame>(std::move(image), std::move(audio)));
\r
201 diag_->update_value("tick-time", static_cast<float>(tick_timer_.elapsed()*format_desc_.fps*0.5));
\r
202 tick_timer_.restart();
\r
204 diag_->set_value("input-buffer", static_cast<float>(executor_.size())/static_cast<float>(executor_.capacity()));
\r
207 safe_ptr<core::write_frame> create_frame(void* tag, const core::pixel_format_desc& desc)
\r
209 return make_safe<write_frame>(reinterpret_cast<int>(tag), desc, image_mixer_.create_buffers(desc));
\r
212 template<typename T>
\r
213 void set_transform(const T& transform, int mix_duration, const std::wstring& tween)
\r
215 executor_.invoke([&]
\r
217 auto& root = boost::fusion::at_key<T>(root_transforms_);
\r
219 auto src = root.fetch();
\r
220 auto dst = transform;
\r
221 root = tweened_transform<T>(src, dst, mix_duration, tween);
\r
225 template<typename T>
\r
226 void set_transform(int index, const T& transform, int mix_duration, const std::wstring& tween)
\r
228 executor_.invoke([&]
\r
230 auto& transforms = boost::fusion::at_key<T>(transforms_);
\r
232 auto src = transforms[index].fetch();
\r
233 auto dst = transform;
\r
234 transforms[index] = tweened_transform<T>(src, dst, mix_duration, tween);
\r
238 template<typename T>
\r
239 void apply_transform(const std::function<T(const T&)>& transform, int mix_duration, const std::wstring& tween)
\r
241 return executor_.invoke([&]
\r
243 auto& root = boost::fusion::at_key<T>(root_transforms_);
\r
245 auto src = root.fetch();
\r
246 auto dst = transform(src);
\r
247 root = tweened_transform<T>(src, dst, mix_duration, tween);
\r
251 template<typename T>
\r
252 void apply_transform(int index, const std::function<T(T)>& transform, int mix_duration, const std::wstring& tween)
\r
254 executor_.invoke([&]
\r
256 auto& transforms = boost::fusion::at_key<T>(transforms_);
\r
258 auto src = transforms[index].fetch();
\r
259 auto dst = transform(src);
\r
260 transforms[index] = tweened_transform<T>(src, dst, mix_duration, tween);
\r
264 template<typename T>
\r
265 void reset_transform(int mix_duration, const std::wstring& tween)
\r
267 executor_.invoke([&]
\r
269 auto& transforms = boost::fusion::at_key<T>(transforms_);
\r
271 BOOST_FOREACH(auto& t, transforms)
\r
272 t.second = tweened_transform<T>(t.second.fetch(), T(), mix_duration, tween);
\r
273 set_transform(T(), mix_duration, tween);
\r
277 template<typename T>
\r
278 void reset_transform(int index, int mix_duration, const std::wstring& tween)
\r
280 executor_.invoke([&]
\r
282 set_transform(T(), mix_duration, tween);
\r
286 std::wstring print() const
\r
288 return L"frame_mixer_device";
\r
292 frame_mixer_device::frame_mixer_device(const core::video_format_desc& format_desc) : impl_(new implementation(format_desc)){}
\r
293 frame_mixer_device::frame_mixer_device(frame_mixer_device&& other) : impl_(std::move(other.impl_)){}
\r
294 boost::signals2::connection frame_mixer_device::connect(const output_t::slot_type& subscriber){return impl_->connect(subscriber);}
\r
295 void frame_mixer_device::send(const std::map<int, safe_ptr<core::basic_frame>>& frames){impl_->send(frames);}
\r
296 const core::video_format_desc& frame_mixer_device::get_video_format_desc() const { return impl_->format_desc_; }
\r
297 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
298 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
300 // Create bgra frame
\r
301 core::pixel_format_desc desc;
\r
302 desc.pix_fmt = pix_fmt;
\r
303 desc.planes.push_back( core::pixel_format_desc::plane(width, height, 4));
\r
304 return create_frame(tag, desc);
\r
307 safe_ptr<core::write_frame> frame_mixer_device::create_frame(void* tag, core::pixel_format::type pix_fmt)
\r
309 // Create bgra frame with output resolution
\r
310 core::pixel_format_desc desc;
\r
311 desc.pix_fmt = pix_fmt;
\r
312 desc.planes.push_back( core::pixel_format_desc::plane(get_video_format_desc().width, get_video_format_desc().height, 4));
\r
313 return create_frame(tag, desc);
\r
315 void frame_mixer_device::set_image_transform(const core::image_transform& transform, int mix_duration, const std::wstring& tween){impl_->set_transform<core::image_transform>(transform, mix_duration, tween);}
\r
316 void frame_mixer_device::set_image_transform(int index, const core::image_transform& transform, int mix_duration, const std::wstring& tween){impl_->set_transform<core::image_transform>(index, transform, mix_duration, tween);}
\r
317 void frame_mixer_device::set_audio_transform(const core::audio_transform& transform, int mix_duration, const std::wstring& tween){impl_->set_transform<core::audio_transform>(transform, mix_duration, tween);}
\r
318 void frame_mixer_device::set_audio_transform(int index, const core::audio_transform& transform, int mix_duration, const std::wstring& tween){impl_->set_transform<core::audio_transform>(index, transform, mix_duration, tween);}
\r
319 void frame_mixer_device::apply_image_transform(const std::function<core::image_transform(core::image_transform)>& transform, int mix_duration, const std::wstring& tween){impl_->apply_transform<core::image_transform>(transform, mix_duration, tween);}
\r
320 void frame_mixer_device::apply_image_transform(int index, const std::function<core::image_transform(core::image_transform)>& transform, int mix_duration, const std::wstring& tween){impl_->apply_transform<core::image_transform>(index, transform, mix_duration, tween);}
\r
321 void frame_mixer_device::apply_audio_transform(const std::function<core::audio_transform(core::audio_transform)>& transform, int mix_duration, const std::wstring& tween){impl_->apply_transform<core::audio_transform>(transform, mix_duration, tween);}
\r
322 void frame_mixer_device::apply_audio_transform(int index, const std::function<core::audio_transform(core::audio_transform)>& transform, int mix_duration, const std::wstring& tween){impl_->apply_transform<core::audio_transform>(index, transform, mix_duration, tween);}
\r
323 void frame_mixer_device::reset_image_transform(int mix_duration, const std::wstring& tween){impl_->reset_transform<core::image_transform>(mix_duration, tween);}
\r
324 void frame_mixer_device::reset_image_transform(int index, int mix_duration, const std::wstring& tween){impl_->reset_transform<core::image_transform>(index, mix_duration, tween);}
\r
325 void frame_mixer_device::reset_audio_transform(int mix_duration, const std::wstring& tween){impl_->reset_transform<core::audio_transform>(mix_duration, tween);}
\r
326 void frame_mixer_device::reset_audio_transform(int index, int mix_duration, const std::wstring& tween){impl_->reset_transform<core::audio_transform>(index, mix_duration, tween);}
\r