]> git.sesse.net Git - casparcg/blobdiff - core/producer/transition/transition_producer.cpp
set svn:eol-style native on .h and .cpp files
[casparcg] / core / producer / transition / transition_producer.cpp
index e32901e261e919b0087c5717b68bfd8f0dcb75e9..aed74fd4ce52869c2e00ad87b842ee000a5cd664 100644 (file)
-/*\r
-* copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
-*\r
-*  This file is part of CasparCG.\r
-*\r
-*    CasparCG is free software: you can redistribute it and/or modify\r
-*    it under the terms of the GNU General Public License as published by\r
-*    the Free Software Foundation, either version 3 of the License, or\r
-*    (at your option) any later version.\r
-*\r
-*    CasparCG is distributed in the hope that it will be useful,\r
-*    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-*    GNU General Public License for more details.\r
-\r
-*    You should have received a copy of the GNU General Public License\r
-*    along with CasparCG.  If not, see <http://www.gnu.org/licenses/>.\r
-*\r
-*/ \r
-#include "../../stdafx.h"\r
-\r
-#include "transition_producer.h"\r
-\r
-#include <core/video_format.h>\r
-\r
-#include <mixer/frame/draw_frame.h>\r
-#include <mixer/image/image_transform.h>\r
-#include <mixer/audio/audio_mixer.h>\r
-#include <mixer/audio/audio_transform.h>\r
-\r
-namespace caspar { namespace core {    \r
-\r
-struct transition_producer::implementation : boost::noncopyable\r
-{      \r
-       printer parent_printer_;\r
-       unsigned short                          current_frame_;\r
-       \r
-       const transition_info           info_;\r
-       \r
-       safe_ptr<frame_producer>        dest_producer_;\r
-       safe_ptr<frame_producer>        source_producer_;\r
-\r
-       std::shared_ptr<frame_factory>  frame_factory_;\r
-       video_format_desc                               format_desc_;\r
-\r
-       std::vector<safe_ptr<draw_frame>> frame_buffer_;\r
-       \r
-       implementation(const safe_ptr<frame_producer>& dest, const transition_info& info) \r
-               : current_frame_(0)\r
-               , info_(info)\r
-               , dest_producer_(dest)\r
-               , source_producer_(frame_producer::empty())\r
-       {\r
-               dest_producer_->set_parent_printer(std::bind(&implementation::dest_print, this));\r
-               frame_buffer_.push_back(draw_frame::empty());\r
-       }\r
-                               \r
-       void initialize(const safe_ptr<frame_factory>& frame_factory)\r
-       {\r
-               dest_producer_->initialize(frame_factory);\r
-               frame_factory_ = frame_factory;\r
-               format_desc_ = frame_factory_->get_video_format_desc();\r
-       }\r
-\r
-       virtual void set_parent_printer(const printer& parent_printer) \r
-       {\r
-               parent_printer_ = parent_printer;\r
-       }\r
-\r
-       safe_ptr<frame_producer> get_following_producer() const\r
-       {\r
-               return dest_producer_;\r
-       }\r
-       \r
-       void set_leading_producer(const safe_ptr<frame_producer>& producer)\r
-       {\r
-               source_producer_ = producer;\r
-               source_producer_->set_parent_printer(std::bind(&implementation::source_print, this));\r
-       }\r
-\r
-       safe_ptr<draw_frame> receive()\r
-       {\r
-               if(current_frame_++ >= info_.duration)\r
-                       return draw_frame::eof();\r
-\r
-               auto source = draw_frame::empty();\r
-               auto dest = draw_frame::empty();\r
-\r
-               tbb::parallel_invoke\r
-               (\r
-                       [&]{dest   = render_sub_frame(dest_producer_);},\r
-                       [&]{source = render_sub_frame(source_producer_);}\r
-               );\r
-\r
-               return compose(dest, source);\r
-       }\r
-       \r
-       safe_ptr<draw_frame> render_sub_frame(safe_ptr<frame_producer>& producer)\r
-       {\r
-               if(producer == frame_producer::empty())\r
-                       return draw_frame::eof();\r
-\r
-               auto frame = draw_frame::eof();\r
-               try\r
-               {\r
-                       frame = producer->receive();\r
-               }\r
-               catch(...)\r
-               {\r
-                       CASPAR_LOG_CURRENT_EXCEPTION();\r
-                       producer = frame_producer::empty();\r
-                       CASPAR_LOG(warning) << print() << " Failed to receive frame. Removed producer from transition.";\r
-               }\r
-\r
-               if(frame == draw_frame::eof())\r
-               {\r
-                       try\r
-                       {\r
-                               auto following = producer->get_following_producer();\r
-                               following->initialize(safe_ptr<frame_factory>(frame_factory_));\r
-                               following->set_leading_producer(producer);\r
-                               producer = std::move(following);\r
-                       }\r
-                       catch(...)\r
-                       {\r
-                               CASPAR_LOG_CURRENT_EXCEPTION();\r
-                               producer = frame_producer::empty();\r
-                               CASPAR_LOG(warning) << print() << " Failed to initialize following producer.";\r
-                       }\r
-\r
-                       return render_sub_frame(producer);\r
-               }\r
-               return frame;\r
-       }\r
-                                       \r
-       safe_ptr<draw_frame> compose(const safe_ptr<draw_frame>& dest_frame, const safe_ptr<draw_frame>& src_frame) \r
-       {       \r
-               if(dest_frame == draw_frame::eof() && src_frame == draw_frame::eof())\r
-                       return draw_frame::eof();\r
-\r
-               if(info_.type == transition::cut)               \r
-                       return src_frame != draw_frame::eof() ? src_frame : draw_frame::empty();\r
-                                                                               \r
-               double alpha = static_cast<double>(current_frame_)/static_cast<double>(info_.duration);\r
-               double half_alpha_step = 0.5*1.0/static_cast<double>(info_.duration);\r
-               \r
-               double dir = info_.direction == transition_direction::from_left ? 1.0 : -1.0;           \r
-               \r
-               // For interlaced transitions. Seperate fields into seperate frames which are transitioned accordingly.\r
-               \r
-               auto s_frame1 = make_safe<draw_frame>(src_frame);\r
-               auto s_frame2 = make_safe<draw_frame>(src_frame);\r
-\r
-               s_frame1->get_audio_transform().set_has_audio(false);\r
-               s_frame2->get_audio_transform().set_gain(1.0-alpha);\r
-\r
-               auto d_frame1 = make_safe<draw_frame>(dest_frame);\r
-               auto d_frame2 = make_safe<draw_frame>(dest_frame);\r
-               \r
-               d_frame1->get_audio_transform().set_has_audio(false);\r
-               d_frame2->get_audio_transform().set_gain(alpha);\r
-\r
-               if(info_.type == transition::mix)\r
-               {\r
-                       d_frame1->get_image_transform().set_opacity(alpha-half_alpha_step);     \r
-                       d_frame2->get_image_transform().set_opacity(alpha);     \r
-               }\r
-               else if(info_.type == transition::slide)\r
-               {\r
-                       d_frame1->get_image_transform().set_fill_translation((-1.0+alpha-half_alpha_step)*dir, 0.0);    \r
-                       d_frame2->get_image_transform().set_fill_translation((-1.0+alpha)*dir, 0.0);            \r
-               }\r
-               else if(info_.type == transition::push)\r
-               {\r
-                       d_frame1->get_image_transform().set_fill_translation((-1.0+alpha-half_alpha_step)*dir, 0.0);\r
-                       d_frame2->get_image_transform().set_fill_translation((-1.0+alpha)*dir, 0.0);\r
-\r
-                       s_frame1->get_image_transform().set_fill_translation((0.0+alpha-half_alpha_step)*dir, 0.0);     \r
-                       s_frame2->get_image_transform().set_fill_translation((0.0+alpha)*dir, 0.0);             \r
-               }\r
-               else if(info_.type == transition::wipe)         \r
-               {\r
-                       d_frame1->get_image_transform().set_key_scale(alpha-half_alpha_step, 1.0);      \r
-                       d_frame2->get_image_transform().set_key_scale(alpha, 1.0);                      \r
-               }\r
-               \r
-               auto s_frame = s_frame1->get_image_transform() == s_frame2->get_image_transform() ? s_frame2 : draw_frame::interlace(s_frame1, s_frame2, format_desc_.mode);\r
-               auto d_frame = draw_frame::interlace(d_frame1, d_frame2, format_desc_.mode);\r
-\r
-               return draw_frame(s_frame, d_frame);\r
-       }\r
-\r
-       std::wstring print() const\r
-       {\r
-               return (parent_printer_ ? parent_printer_() + L"/" : L"") + L"transition[" + info_.name() + L":" + boost::lexical_cast<std::wstring>(info_.duration) + L"]";\r
-       }\r
-\r
-       std::wstring source_print() const { return print() + L"/source";}\r
-       std::wstring dest_print() const { return print() + L"/dest";}\r
-};\r
-\r
-transition_producer::transition_producer(transition_producer&& other) : impl_(std::move(other.impl_)){}\r
-transition_producer::transition_producer(const safe_ptr<frame_producer>& dest, const transition_info& info) : impl_(new implementation(dest, info)){}\r
-safe_ptr<draw_frame> transition_producer::receive(){return impl_->receive();}\r
-safe_ptr<frame_producer> transition_producer::get_following_producer() const{return impl_->get_following_producer();}\r
-void transition_producer::set_leading_producer(const safe_ptr<frame_producer>& producer) { impl_->set_leading_producer(producer); }\r
-void transition_producer::initialize(const safe_ptr<frame_factory>& frame_factory) { impl_->initialize(frame_factory);}\r
-void transition_producer::set_parent_printer(const printer& parent_printer) {impl_->set_parent_printer(parent_printer);}\r
-std::wstring transition_producer::print() const { return impl_->print();}\r
-\r
-}}\r
-\r
+/*
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
+*
+* 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 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 <http://www.gnu.org/licenses/>.
+*
+* Author: Robert Nagy, ronag89@gmail.com
+*/
+
+#include "../../stdafx.h"
+
+#include "transition_producer.h"
+
+#include "../frame_producer.h"
+#include "../../frame/draw_frame.h"
+#include "../../frame/frame_transform.h"
+#include "../../monitor/monitor.h"
+
+#include <tbb/parallel_invoke.h>
+
+namespace caspar { namespace core {    
+
+class transition_producer : public frame_producer_base
+{      
+       monitor::basic_subject                          event_subject_;
+       const field_mode                                        mode_;
+       int                                                                     current_frame_;
+       
+       const transition_info                           info_;
+               
+       spl::shared_ptr<frame_producer>         dest_producer_;
+       spl::shared_ptr<frame_producer>         source_producer_;
+
+       bool                                                            paused_;
+               
+public:
+       explicit transition_producer(const field_mode& mode, const spl::shared_ptr<frame_producer>& dest, const transition_info& info) 
+               : mode_(mode)
+               , current_frame_(0)
+               , info_(info)
+               , dest_producer_(dest)
+               , source_producer_(frame_producer::empty())
+               , paused_(false)
+       {
+               dest->subscribe(event_subject_);
+
+               CASPAR_LOG(info) << print() << L" Initialized";
+       }
+       
+       // frame_producer
+               
+       void leading_producer(const spl::shared_ptr<frame_producer>& producer) override
+       {
+               source_producer_ = producer;
+       }
+
+       draw_frame receive_impl() override
+       {
+               if(current_frame_ >= info_.duration)
+               {
+                       source_producer_ = core::frame_producer::empty();
+                       return dest_producer_->receive();               
+               }
+
+               current_frame_ += 1;
+
+               auto dest = draw_frame::empty();
+               auto source = draw_frame::empty();
+
+               tbb::parallel_invoke(
+               [&]
+               {
+                       dest = dest_producer_->receive();
+                       if(dest == core::draw_frame::late())
+                               dest = dest_producer_->last_frame();
+               },
+               [&]
+               {
+                       source = source_producer_->receive();
+                       if(source == core::draw_frame::late())
+                               source = source_producer_->last_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";
+                                                                                                                                       }
+                                                                                                                               }();
+
+               return compose(dest, source);
+       }
+
+       draw_frame last_frame() override
+       {
+               if(current_frame_ >= info_.duration)
+                       return dest_producer_->last_frame();
+
+               return frame_producer_base::last_frame();
+       }
+                       
+       uint32_t nb_frames() const override
+       {
+               return dest_producer_->nb_frames();
+       }
+
+       std::wstring print() const override
+       {
+               return L"transition[" + source_producer_->print() + L"=>" + dest_producer_->print() + L"]";
+       }
+
+       std::wstring name() const override
+       {
+               return L"transition";
+       }
+       
+       boost::property_tree::wptree info() const override
+       {
+               return dest_producer_->info();
+       }
+       
+       boost::unique_future<std::wstring> call(const std::wstring& str) override
+       {
+               return dest_producer_->call(str);
+       }
+
+       // transition_producer
+                                               
+       draw_frame compose(draw_frame dest_frame, draw_frame src_frame) const
+       {       
+               if(info_.type == transition_type::cut)          
+                       return src_frame;
+                                                                               
+               const double delta1 = info_.tweener(current_frame_*2-1, 0.0, 1.0, static_cast<double>(info_.duration*2));
+               const double delta2 = info_.tweener(current_frame_*2,   0.0, 1.0, static_cast<double>(info_.duration*2));  
+
+               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.transform().audio_transform.volume = 1.0-delta2;
+               auto s_frame1 = src_frame;
+               auto s_frame2 = src_frame;
+               
+               dest_frame.transform().audio_transform.volume = delta2;
+               auto d_frame1 = dest_frame;
+               auto d_frame2 = dest_frame;
+               
+               if(info_.type == transition_type::mix)
+               {
+                       d_frame1.transform().image_transform.opacity = delta1;  
+                       d_frame1.transform().image_transform.is_mix = true;
+                       d_frame2.transform().image_transform.opacity = delta2;
+                       d_frame2.transform().image_transform.is_mix = true;
+
+                       s_frame1.transform().image_transform.opacity = 1.0-delta1;      
+                       s_frame1.transform().image_transform.is_mix = true;
+                       s_frame2.transform().image_transform.opacity = 1.0-delta2;      
+                       s_frame2.transform().image_transform.is_mix = true;
+               }
+               if(info_.type == transition_type::slide)
+               {
+                       d_frame1.transform().image_transform.fill_translation[0] = (-1.0+delta1)*dir;   
+                       d_frame2.transform().image_transform.fill_translation[0] = (-1.0+delta2)*dir;           
+               }
+               else if(info_.type == transition_type::push)
+               {
+                       d_frame1.transform().image_transform.fill_translation[0] = (-1.0+delta1)*dir;
+                       d_frame2.transform().image_transform.fill_translation[0] = (-1.0+delta2)*dir;
+
+                       s_frame1.transform().image_transform.fill_translation[0] = (0.0+delta1)*dir;    
+                       s_frame2.transform().image_transform.fill_translation[0] = (0.0+delta2)*dir;            
+               }
+               else if(info_.type == transition_type::wipe)            
+               {
+                       d_frame1.transform().image_transform.clip_scale[0] = delta1;    
+                       d_frame2.transform().image_transform.clip_scale[0] = delta2;                    
+               }
+                               
+               const auto s_frame = s_frame1.transform() == s_frame2.transform() ? s_frame2 : draw_frame::interlace(s_frame1, s_frame2, mode_);
+               const auto d_frame = d_frame1.transform() == d_frame2.transform() ? d_frame2 : draw_frame::interlace(d_frame1, d_frame2, mode_);
+               
+               return draw_frame::over(s_frame, d_frame);
+       }
+
+       void subscribe(const monitor::observable::observer_ptr& o) override                                                                                                                     
+       {
+               event_subject_.subscribe(o);
+       }
+
+       void unsubscribe(const monitor::observable::observer_ptr& o) override           
+       {
+               event_subject_.unsubscribe(o);
+       }
+};
+
+spl::shared_ptr<frame_producer> create_transition_producer(const field_mode& mode, const spl::shared_ptr<frame_producer>& destination, const transition_info& info)
+{
+       return spl::make_shared<transition_producer>(mode, destination, info);
+}
+
+}}
+