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 "transition_producer.h"
\r
24 #include "../../frame/frame_format.h"
\r
25 #include "../../frame/gpu_frame.h"
\r
26 #include "../../frame/composite_gpu_frame.h"
\r
27 #include "../../frame/frame_factory.h"
\r
29 #include "../../../common/utility/memory.h"
\r
30 #include "../../renderer/render_device.h"
\r
32 #include <boost/range/algorithm/copy.hpp>
\r
36 struct transition_producer::implementation : boost::noncopyable
\r
38 implementation(const frame_producer_ptr& dest, const transition_info& info, const frame_format_desc& format_desc)
\r
39 : current_frame_(0), info_(info), border_color_(0), format_desc_(format_desc), dest_(dest)
\r
42 BOOST_THROW_EXCEPTION(null_argument() << arg_name_info("dest"));
\r
45 frame_producer_ptr get_following_producer() const
\r
50 void set_leading_producer(const frame_producer_ptr& producer)
\r
55 gpu_frame_ptr get_frame()
\r
57 if(++current_frame_ >= info_.duration)
\r
60 return compose(get_producer_frame(dest_), get_producer_frame(source_));
\r
63 gpu_frame_ptr get_producer_frame(frame_producer_ptr& producer)
\r
65 if(producer == nullptr)
\r
67 auto frame = factory_->create_frame(format_desc_);
\r
68 common::clear(frame->data(), frame->size());
\r
72 gpu_frame_ptr frame;
\r
75 frame = producer->get_frame();
\r
79 CASPAR_LOG_CURRENT_EXCEPTION();
\r
80 CASPAR_LOG(warning) << "Removed renderer from transition.";
\r
83 if(frame == nullptr && producer->get_following_producer() != nullptr)
\r
85 auto following = producer->get_following_producer();
\r
86 following->set_leading_producer(producer);
\r
87 producer = following;
\r
88 return get_producer_frame(producer);
\r
93 gpu_frame_ptr compose(const gpu_frame_ptr& dest_frame, const gpu_frame_ptr& src_frame)
\r
98 if(info_.type == transition_type::cut || !dest_frame)
\r
101 int volume = static_cast<int>(static_cast<float>(current_frame_)/static_cast<float>(info_.duration)*256.0f);
\r
103 for(size_t n = 0; n < dest_frame->audio_data().size(); ++n)
\r
104 dest_frame->audio_data()[n] = static_cast<short>((static_cast<int>(dest_frame->audio_data()[n])*volume)>>8);
\r
106 for(size_t n = 0; n < src_frame->audio_data().size(); ++n)
\r
107 src_frame->audio_data()[n] = static_cast<short>((static_cast<int>(src_frame->audio_data()[n])*(256-volume))>>8);
\r
109 float alpha = static_cast<float>(current_frame_)/static_cast<float>(info_.duration);
\r
110 auto composite = std::make_shared<composite_gpu_frame>(format_desc_.width, format_desc_.height);
\r
111 composite->add(src_frame);
\r
112 composite->add(dest_frame);
\r
113 if(info_.type == transition_type::mix)
\r
115 src_frame->alpha(1.0f-alpha);
\r
116 dest_frame->alpha(alpha);
\r
118 else if(info_.type == transition_type::slide)
\r
120 if(info_.direction == transition_direction::from_left)
\r
121 dest_frame->translate(-1.0f+alpha, 0.0f);
\r
122 else if(info_.direction == transition_direction::from_right)
\r
123 dest_frame->translate(1.0f-alpha, 0.0f);
\r
125 else if(info_.type == transition_type::push)
\r
127 if(info_.direction == transition_direction::from_left)
\r
129 dest_frame->translate(-1.0f+alpha, 0.0f);
\r
130 src_frame->translate(0.0f+alpha, 0.0f);
\r
132 else if(info_.direction == transition_direction::from_right)
\r
134 dest_frame->translate(1.0f-alpha, 0.0f);
\r
135 src_frame->translate(0.0f-alpha, 0.0f);
\r
141 void initialize(const frame_factory_ptr& factory)
\r
143 dest_->initialize(factory);
\r
144 factory_ = factory;
\r
147 const frame_format_desc format_desc_;
\r
149 frame_producer_ptr source_;
\r
150 frame_producer_ptr dest_;
\r
152 unsigned short current_frame_;
\r
154 const transition_info info_;
\r
155 const unsigned long border_color_;
\r
156 frame_factory_ptr factory_;
\r
159 transition_producer::transition_producer(const frame_producer_ptr& dest, const transition_info& info, const frame_format_desc& format_desc)
\r
160 : impl_(new implementation(dest, info, format_desc)){}
\r
161 gpu_frame_ptr transition_producer::get_frame(){return impl_->get_frame();}
\r
162 frame_producer_ptr transition_producer::get_following_producer() const{return impl_->get_following_producer();}
\r
163 void transition_producer::set_leading_producer(const frame_producer_ptr& producer) { impl_->set_leading_producer(producer); }
\r
164 const frame_format_desc& transition_producer::get_frame_format_desc() const { return impl_->format_desc_; }
\r
165 void transition_producer::initialize(const frame_factory_ptr& factory) { impl_->initialize(factory);}
\r