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
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 "../video_channel_context.h"
\r
32 #include <common/exception/exceptions.h>
\r
33 #include <common/concurrency/executor.h>
\r
34 #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
49 #include <tbb/parallel_invoke.h>
\r
51 #include <unordered_map>
\r
53 namespace caspar { namespace core {
\r
55 template<typename T>
\r
56 class tweened_transform
\r
67 , tweener_(get_tweener(L"linear")){}
\r
68 tweened_transform(const T& source, const T& dest, int duration, const std::wstring& tween = L"linear")
\r
71 , duration_(duration)
\r
73 , tweener_(get_tweener(tween)){}
\r
77 return time_ == duration_ ? dest_ : tween(static_cast<double>(time_), source_, dest_, static_cast<double>(duration_), tweener_);
\r
80 T fetch_and_tick(int num)
\r
82 time_ = std::min(time_+num, duration_);
\r
87 struct mixer::implementation : boost::noncopyable
\r
89 video_channel_context& channel_;
\r
91 audio_mixer audio_mixer_;
\r
92 image_mixer image_mixer_;
\r
94 typedef std::unordered_map<int, tweened_transform<core::image_transform>> image_transforms;
\r
95 typedef std::unordered_map<int, tweened_transform<core::audio_transform>> audio_transforms;
\r
97 boost::fusion::map<boost::fusion::pair<core::image_transform, image_transforms>,
\r
98 boost::fusion::pair<core::audio_transform, audio_transforms>> transforms_;
\r
101 implementation(video_channel_context& video_channel)
\r
102 : channel_(video_channel)
\r
103 , audio_mixer_(channel_.get_format_desc())
\r
104 , image_mixer_(channel_)
\r
106 CASPAR_LOG(info) << print() << L" Successfully initialized.";
\r
109 safe_ptr<read_frame> execute(const std::map<int, safe_ptr<core::basic_frame>>& frames)
\r
113 decltype(mix_image(frames)) image;
\r
114 decltype(mix_audio(frames)) audio;
\r
116 tbb::parallel_invoke(
\r
117 [&]{image = mix_image(frames);},
\r
118 [&]{audio = mix_audio(frames);});
\r
120 return make_safe<read_frame>(channel_.ogl(), std::move(image.get()), std::move(audio));
\r
124 channel_.ogl().gc().wait();
\r
125 image_mixer_ = image_mixer(channel_);
\r
126 audio_mixer_ = audio_mixer(channel_.get_format_desc());
\r
127 channel_.ogl().gc().wait();
\r
129 CASPAR_LOG_CURRENT_EXCEPTION();
\r
130 return make_safe<read_frame>();
\r
134 safe_ptr<core::write_frame> create_frame(const void* tag, const core::pixel_format_desc& desc)
\r
136 return image_mixer_.create_frame(tag, desc);
\r
139 void reset_transforms()
\r
141 channel_.execution().invoke([&]
\r
143 boost::fusion::at_key<image_transform>(transforms_).clear();
\r
144 boost::fusion::at_key<audio_transform>(transforms_).clear();
\r
148 template<typename T>
\r
149 void set_transform(int index, const T& transform, unsigned int mix_duration, const std::wstring& tween)
\r
151 channel_.execution().invoke([&]
\r
153 auto& transforms = boost::fusion::at_key<T>(transforms_);
\r
155 auto src = transforms[index].fetch();
\r
156 auto dst = transform;
\r
157 transforms[index] = tweened_transform<T>(src, dst, mix_duration, tween);
\r
161 template<typename T>
\r
162 void apply_transform(int index, const std::function<T(T)>& transform, unsigned int mix_duration, const std::wstring& tween)
\r
164 channel_.execution().invoke([&]
\r
166 auto& transforms = boost::fusion::at_key<T>(transforms_);
\r
168 auto src = transforms[index].fetch();
\r
169 auto dst = transform(src);
\r
170 transforms[index] = tweened_transform<T>(src, dst, mix_duration, tween);
\r
174 std::wstring print() const
\r
181 boost::unique_future<safe_ptr<host_buffer>> mix_image(std::map<int, safe_ptr<core::basic_frame>> frames)
\r
183 auto& image_transforms = boost::fusion::at_key<core::image_transform>(transforms_);
\r
185 BOOST_FOREACH(auto& frame, frames)
\r
187 image_mixer_.begin_layer();
\r
189 auto frame1 = make_safe<core::basic_frame>(frame.second);
\r
190 frame1->get_image_transform() = image_transforms[frame.first].fetch_and_tick(1);
\r
192 if(channel_.get_format_desc().mode != core::video_mode::progressive)
\r
194 auto frame2 = make_safe<core::basic_frame>(frame.second);
\r
195 frame2->get_image_transform() = image_transforms[frame.first].fetch_and_tick(1);
\r
196 frame1 = core::basic_frame::interlace(frame1, frame2, channel_.get_format_desc().mode); // image_mixer optimizes away unecessary interlacing. No need to worry about it here.
\r
199 frame1->accept(image_mixer_);
\r
201 image_mixer_.end_layer();
\r
204 return image_mixer_.render();
\r
207 std::vector<int16_t> mix_audio(const std::map<int, safe_ptr<core::basic_frame>>& frames)
\r
209 auto& audio_transforms = boost::fusion::at_key<core::audio_transform>(transforms_);
\r
211 BOOST_FOREACH(auto& frame, frames)
\r
213 const unsigned int num = channel_.get_format_desc().mode == core::video_mode::progressive ? 1 : 2;
\r
215 auto frame1 = make_safe<core::basic_frame>(frame.second);
\r
216 frame1->get_audio_transform() = audio_transforms[frame.first].fetch_and_tick(num);
\r
217 frame1->accept(audio_mixer_);
\r
220 return audio_mixer_.mix();
\r
224 mixer::mixer(video_channel_context& video_channel) : impl_(new implementation(video_channel)){}
\r
225 safe_ptr<core::read_frame> mixer::execute(const std::map<int, safe_ptr<core::basic_frame>>& frames){ return impl_->execute(frames);}
\r
226 core::video_format_desc mixer::get_video_format_desc() const { return impl_->channel_.get_format_desc(); }
\r
227 safe_ptr<core::write_frame> mixer::create_frame(const void* tag, const core::pixel_format_desc& desc){ return impl_->create_frame(tag, desc); }
\r
228 safe_ptr<core::write_frame> mixer::create_frame(const void* tag, size_t width, size_t height, core::pixel_format::type pix_fmt)
\r
230 // Create bgra frame
\r
231 core::pixel_format_desc desc;
\r
232 desc.pix_fmt = pix_fmt;
\r
233 desc.planes.push_back( core::pixel_format_desc::plane(width, height, 4));
\r
234 return create_frame(tag, desc);
\r
236 void mixer::reset_transforms(){impl_->reset_transforms();}
\r
237 void mixer::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
238 void mixer::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
239 void mixer::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
240 void mixer::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