X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=core%2Fproducer%2Ftransition%2Ftransition_producer.cpp;h=ec3e5bfa12f5609ab638f3ab03e1f9a3912f76fb;hb=2af2c4bb98e0d68d7128cc3df51dc447fe4ab280;hp=7d71f5f4a3f16b5cb173b8e50ea75c684d76256f;hpb=df0adecdebeaefdb7250df6349af8073d15ecaaa;p=casparcg diff --git a/core/producer/transition/transition_producer.cpp b/core/producer/transition/transition_producer.cpp index 7d71f5f4a..e3056c75f 100644 --- a/core/producer/transition/transition_producer.cpp +++ b/core/producer/transition/transition_producer.cpp @@ -1,154 +1,209 @@ /* -* copyright (c) 2010 Sveriges Television AB +* Copyright (c) 2011 Sveriges Television AB * -* This file is part of CasparCG. +* This file is part of CasparCG (www.casparcg.com). * -* CasparCG is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. +* CasparCG is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. * -* CasparCG is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. - -* You should have received a copy of the GNU General Public License -* along with CasparCG. If not, see . +* CasparCG is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with CasparCG. If not, see . * -*/ +* Author: Robert Nagy, ronag89@gmail.com +*/ + #include "../../stdafx.h" #include "transition_producer.h" -#include "../../frame/frame_format.h" -#include "../../frame/gpu_frame.h" -#include "../../frame/composite_gpu_frame.h" -#include "../../frame/frame_factory.h" +#include "../frame_producer.h" +#include "../../frame/draw_frame.h" +#include "../../frame/frame_transform.h" +#include "../../monitor/monitor.h" -#include "../../../common/image/image.h" -#include "../../renderer/render_device.h" +#include -#include +namespace caspar { namespace core { -namespace caspar{ +class transition_producer : public frame_producer +{ + monitor::basic_subject event_subject_; + const field_mode mode_; + int current_frame_; + + const transition_info info_; -struct transition_producer::implementation : boost::noncopyable -{ - implementation(const frame_producer_ptr& dest, const transition_info& info, const frame_format_desc& format_desc) - : current_frame_(0), info_(info), border_color_(0), format_desc_(format_desc), dest_(dest) - { - if(!dest) - BOOST_THROW_EXCEPTION(null_argument() << arg_name_info("dest")); - } + spl::shared_ptr last_frame_; + + spl::shared_ptr dest_producer_; + spl::shared_ptr source_producer_; - frame_producer_ptr get_following_producer() const +public: + explicit transition_producer(const field_mode& mode, const spl::shared_ptr& dest, const transition_info& info) + : mode_(mode) + , current_frame_(0) + , info_(info) + , last_frame_(draw_frame::empty()) + , dest_producer_(dest) + , source_producer_(frame_producer::empty()) { - return dest_; + dest->subscribe(event_subject_); } - void set_leading_producer(const frame_producer_ptr& producer) + // frame_producer + + virtual void leading_producer(const spl::shared_ptr& producer) override { - source_ = producer; + source_producer_ = producer; } - - gpu_frame_ptr get_frame() + + virtual spl::shared_ptr receive(int flags) override { - if(++current_frame_ >= info_.duration) - return nullptr; + if(current_frame_ >= info_.duration) + return dest_producer_->receive(flags); + + ++current_frame_; + + event_subject_ << monitor::event("transition/frame") % current_frame_ % info_.duration + << monitor::event("transition/type") % [&]() -> std::string + { + switch(info_.type.value()) + { + case transition_type::mix: return "mix"; + case transition_type::wipe: return "wipe"; + case transition_type::slide: return "slide"; + case transition_type::push: return "push"; + case transition_type::cut: return "cut"; + default: return "n/a"; + } + }(); + + auto dest = draw_frame::empty(); + auto source = draw_frame::empty(); + + tbb::parallel_invoke( + [&] + { + dest = dest_producer_->receive(flags); + if(dest == core::draw_frame::late()) + dest = dest_producer_->last_frame(); + }, + [&] + { + source = source_producer_->receive(flags); + if(source == core::draw_frame::late()) + source = source_producer_->last_frame(); + }); - return compose(get_producer_frame(dest_), get_producer_frame(source_)); + return compose(dest, source); } - gpu_frame_ptr get_producer_frame(frame_producer_ptr& producer) + virtual spl::shared_ptr last_frame() const override { - if(producer == nullptr) - { - auto frame = factory_->create_frame(format_desc_); - common::image::clear(frame->data(), frame->size()); - return frame; - } + return dest_producer_->last_frame(); + } + + virtual uint32_t nb_frames() const override + { + return dest_producer_->nb_frames(); + } - gpu_frame_ptr frame; - try - { - frame = producer->get_frame(); - } - catch(...) - { - CASPAR_LOG_CURRENT_EXCEPTION(); - CASPAR_LOG(warning) << "Removed renderer from transition."; - } + virtual std::wstring print() const override + { + return L"transition[" + source_producer_->print() + L"=>" + dest_producer_->print() + L"]"; + } - if(frame == nullptr && producer->get_following_producer() != nullptr) - { - auto following = producer->get_following_producer(); - following->set_leading_producer(producer); - producer = following; - return get_producer_frame(producer); - } - return frame; + virtual std::wstring name() const override + { + return L"transition"; + } + + boost::property_tree::wptree info() const override + { + return dest_producer_->info(); } - - gpu_frame_ptr compose(const gpu_frame_ptr& dest_frame, const gpu_frame_ptr& src_frame) - { + + // transition_producer + + spl::shared_ptr compose(const spl::shared_ptr& dest_frame, const spl::shared_ptr& src_frame) + { if(info_.type == transition_type::cut) return src_frame; - - int volume = static_cast(static_cast(current_frame_)/static_cast(info_.duration)*256.0f); - - for(size_t n = 0; n < dest_frame->audio_data().size(); ++n) - dest_frame->audio_data()[n] = static_cast((static_cast(dest_frame->audio_data()[n])*volume)>>8); + + const double delta1 = info_.tweener(current_frame_*2-1, 0.0, 1.0, static_cast(info_.duration*2)); + const double delta2 = info_.tweener(current_frame_*2, 0.0, 1.0, static_cast(info_.duration*2)); - for(size_t n = 0; n < src_frame->audio_data().size(); ++n) - src_frame->audio_data()[n] = static_cast((static_cast(src_frame->audio_data()[n])*(256-volume))>>8); - - float alpha = static_cast(current_frame_)/static_cast(info_.duration); - auto composite = std::make_shared(format_desc_.width, format_desc_.height); - composite->add(src_frame); - composite->add(dest_frame); + const double dir = info_.direction == transition_direction::from_left ? 1.0 : -1.0; + + // For interlaced transitions. Seperate fields into seperate frames which are transitioned accordingly. + + src_frame->frame_transform().audio_transform.volume = 1.0-delta2; + auto s_frame1 = spl::make_shared(src_frame); + auto s_frame2 = spl::make_shared(src_frame); + + dest_frame->frame_transform().audio_transform.volume = delta2; + auto d_frame1 = spl::make_shared(dest_frame); + auto d_frame2 = spl::make_shared(dest_frame); + if(info_.type == transition_type::mix) { - src_frame->alpha(1.0f-alpha); - dest_frame->alpha(alpha); + d_frame1->frame_transform().image_transform.opacity = delta1; + d_frame1->frame_transform().image_transform.is_mix = true; + d_frame2->frame_transform().image_transform.opacity = delta2; + d_frame2->frame_transform().image_transform.is_mix = true; + + s_frame1->frame_transform().image_transform.opacity = 1.0-delta1; + s_frame1->frame_transform().image_transform.is_mix = true; + s_frame2->frame_transform().image_transform.opacity = 1.0-delta2; + s_frame2->frame_transform().image_transform.is_mix = true; } - else if(info_.type == transition_type::slide) + if(info_.type == transition_type::slide) { - dest_frame->translate(-1.0f+alpha, 0.0f); + d_frame1->frame_transform().image_transform.fill_translation[0] = (-1.0+delta1)*dir; + d_frame2->frame_transform().image_transform.fill_translation[0] = (-1.0+delta2)*dir; } else if(info_.type == transition_type::push) { - dest_frame->translate(-1.0f+alpha, 0.0f); - src_frame->translate(alpha, 0.0f); + d_frame1->frame_transform().image_transform.fill_translation[0] = (-1.0+delta1)*dir; + d_frame2->frame_transform().image_transform.fill_translation[0] = (-1.0+delta2)*dir; + + s_frame1->frame_transform().image_transform.fill_translation[0] = (0.0+delta1)*dir; + s_frame2->frame_transform().image_transform.fill_translation[0] = (0.0+delta2)*dir; } - return composite; - } + else if(info_.type == transition_type::wipe) + { + d_frame1->frame_transform().image_transform.clip_scale[0] = delta1; + d_frame2->frame_transform().image_transform.clip_scale[0] = delta2; + } + + const auto s_frame = s_frame1->frame_transform() == s_frame2->frame_transform() ? s_frame2 : draw_frame::interlace(s_frame1, s_frame2, mode_); + const auto d_frame = d_frame1->frame_transform() == d_frame2->frame_transform() ? d_frame2 : draw_frame::interlace(d_frame1, d_frame2, mode_); - void initialize(const frame_factory_ptr& factory) - { - dest_->initialize(factory); - factory_ = factory; + return draw_frame::over(s_frame, d_frame); } - const frame_format_desc format_desc_; + virtual void subscribe(const monitor::observable::observer_ptr& o) override + { + event_subject_.subscribe(o); + } - frame_producer_ptr source_; - frame_producer_ptr dest_; - - unsigned short current_frame_; - - const transition_info info_; - const unsigned long border_color_; - frame_factory_ptr factory_; + virtual void unsubscribe(const monitor::observable::observer_ptr& o) override + { + event_subject_.unsubscribe(o); + } }; -transition_producer::transition_producer(const frame_producer_ptr& dest, const transition_info& info, const frame_format_desc& format_desc) - : impl_(new implementation(dest, info, format_desc)){} -gpu_frame_ptr transition_producer::get_frame(){return impl_->get_frame();} -frame_producer_ptr transition_producer::get_following_producer() const{return impl_->get_following_producer();} -void transition_producer::set_leading_producer(const frame_producer_ptr& producer) { impl_->set_leading_producer(producer); } -const frame_format_desc& transition_producer::get_frame_format_desc() const { return impl_->format_desc_; } -void transition_producer::initialize(const frame_factory_ptr& factory) { impl_->initialize(factory);} - +spl::shared_ptr create_transition_producer(const field_mode& mode, const spl::shared_ptr& destination, const transition_info& info) +{ + return core::wrap_producer(spl::make_shared(mode, destination, info)); } +}} +