]> git.sesse.net Git - casparcg/blobdiff - core/producer/transition/transition_producer.cpp
#467 [framerate_producer] Fixed nb_frames() and frame_number() calculation
[casparcg] / core / producer / transition / transition_producer.cpp
index 7b5be01417178fd21380be962d1ee998af5bdd6d..590aca44d248fc02d3c27c9e4179ffb1ca86512c 100644 (file)
-/*\r
-* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
-*\r
-* This file is part of CasparCG (www.casparcg.com).\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
-* Author: Robert Nagy, ronag89@gmail.com\r
-*/\r
-\r
-#include "../../stdafx.h"\r
-\r
-#include "transition_producer.h"\r
-\r
-#include "../frame_producer.h"\r
-#include "../../frame/draw_frame.h"\r
-#include "../../frame/frame_transform.h"\r
-#include "../../monitor/monitor.h"\r
-\r
-#include <tbb/parallel_invoke.h>\r
-\r
-namespace caspar { namespace core {    \r
-\r
-class transition_producer : public frame_producer_base\r
-{      \r
-       monitor::basic_subject                          event_subject_;\r
-       const field_mode                                        mode_;\r
-       int                                                                     current_frame_;\r
-       \r
-       const transition_info                           info_;\r
-               \r
-       spl::shared_ptr<frame_producer>         dest_producer_;\r
-       spl::shared_ptr<frame_producer>         source_producer_;\r
-\r
-       bool                                                            paused_;\r
-               \r
-public:\r
-       explicit transition_producer(const field_mode& mode, const spl::shared_ptr<frame_producer>& dest, const transition_info& info) \r
-               : mode_(mode)\r
-               , current_frame_(0)\r
-               , info_(info)\r
-               , dest_producer_(dest)\r
-               , source_producer_(frame_producer::empty())\r
-               , paused_(false)\r
-       {\r
-               dest->subscribe(event_subject_);\r
-\r
-               CASPAR_LOG(info) << print() << L" Initialized";\r
-       }\r
-       \r
-       // frame_producer\r
-               \r
-       void leading_producer(const spl::shared_ptr<frame_producer>& producer) override\r
-       {\r
-               source_producer_ = producer;\r
-       }\r
-\r
-       draw_frame receive_impl() override\r
-       {\r
-               if(++current_frame_ >= info_.duration)\r
-               {\r
-                       source_producer_ = core::frame_producer::empty();\r
-                       return dest_producer_->receive();               \r
-               }\r
-\r
-               auto dest = draw_frame::empty();\r
-               auto source = draw_frame::empty();\r
-\r
-               tbb::parallel_invoke(\r
-               [&]\r
-               {\r
-                       dest = dest_producer_->receive();\r
-                       if(dest == core::draw_frame::late())\r
-                               dest = dest_producer_->last_frame();\r
-               },\r
-               [&]\r
-               {\r
-                       source = source_producer_->receive();\r
-                       if(source == core::draw_frame::late())\r
-                               source = source_producer_->last_frame();\r
-               });                     \r
-                                               \r
-               event_subject_  << monitor::event("transition/frame") % current_frame_ % info_.duration\r
-                                               << monitor::event("transition/type") % [&]() -> std::string\r
-                                                                                                                               {\r
-                                                                                                                                       switch(info_.type.value())\r
-                                                                                                                                       {\r
-                                                                                                                                       case transition_type::mix:              return "mix";\r
-                                                                                                                                       case transition_type::wipe:             return "wipe";\r
-                                                                                                                                       case transition_type::slide:    return "slide";\r
-                                                                                                                                       case transition_type::push:             return "push";\r
-                                                                                                                                       case transition_type::cut:              return "cut";\r
-                                                                                                                                       default:                                                return "n/a";\r
-                                                                                                                                       }\r
-                                                                                                                               }();\r
-\r
-               return compose(dest, source);\r
-       }\r
-\r
-       draw_frame last_frame() override\r
-       {\r
-               if(current_frame_ >= info_.duration)\r
-                       return dest_producer_->last_frame();\r
-\r
-               return frame_producer_base::last_frame();\r
-       }\r
-                       \r
-       uint32_t nb_frames() const override\r
-       {\r
-               return dest_producer_->nb_frames();\r
-       }\r
-\r
-       std::wstring print() const override\r
-       {\r
-               return L"transition[" + source_producer_->print() + L"=>" + dest_producer_->print() + L"]";\r
-       }\r
-\r
-       std::wstring name() const override\r
-       {\r
-               return L"transition";\r
-       }\r
-       \r
-       boost::property_tree::wptree info() const override\r
-       {\r
-               return dest_producer_->info();\r
-       }\r
-       \r
-       boost::unique_future<std::wstring> call(const std::wstring& str) override\r
-       {\r
-               return dest_producer_->call(str);\r
-       }\r
-\r
-       // transition_producer\r
-                                               \r
-       draw_frame compose(draw_frame dest_frame, draw_frame src_frame) const\r
-       {       \r
-               if(info_.type == transition_type::cut)          \r
-                       return src_frame;\r
-                                                                               \r
-               const double delta1 = info_.tweener(current_frame_*2-1, 0.0, 1.0, static_cast<double>(info_.duration*2));\r
-               const double delta2 = info_.tweener(current_frame_*2,   0.0, 1.0, static_cast<double>(info_.duration*2));  \r
-\r
-               const 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
-               src_frame.transform().audio_transform.volume = 1.0-delta2;\r
-               auto s_frame1 = src_frame;\r
-               auto s_frame2 = src_frame;\r
-               \r
-               dest_frame.transform().audio_transform.volume = delta2;\r
-               auto d_frame1 = dest_frame;\r
-               auto d_frame2 = dest_frame;\r
-               \r
-               if(info_.type == transition_type::mix)\r
-               {\r
-                       d_frame1.transform().image_transform.opacity = delta1;  \r
-                       d_frame1.transform().image_transform.is_mix = true;\r
-                       d_frame2.transform().image_transform.opacity = delta2;\r
-                       d_frame2.transform().image_transform.is_mix = true;\r
-\r
-                       s_frame1.transform().image_transform.opacity = 1.0-delta1;      \r
-                       s_frame1.transform().image_transform.is_mix = true;\r
-                       s_frame2.transform().image_transform.opacity = 1.0-delta2;      \r
-                       s_frame2.transform().image_transform.is_mix = true;\r
-               }\r
-               if(info_.type == transition_type::slide)\r
-               {\r
-                       d_frame1.transform().image_transform.fill_translation[0] = (-1.0+delta1)*dir;   \r
-                       d_frame2.transform().image_transform.fill_translation[0] = (-1.0+delta2)*dir;           \r
-               }\r
-               else if(info_.type == transition_type::push)\r
-               {\r
-                       d_frame1.transform().image_transform.fill_translation[0] = (-1.0+delta1)*dir;\r
-                       d_frame2.transform().image_transform.fill_translation[0] = (-1.0+delta2)*dir;\r
-\r
-                       s_frame1.transform().image_transform.fill_translation[0] = (0.0+delta1)*dir;    \r
-                       s_frame2.transform().image_transform.fill_translation[0] = (0.0+delta2)*dir;            \r
-               }\r
-               else if(info_.type == transition_type::wipe)            \r
-               {\r
-                       d_frame1.transform().image_transform.clip_scale[0] = delta1;    \r
-                       d_frame2.transform().image_transform.clip_scale[0] = delta2;                    \r
-               }\r
-                               \r
-               const auto s_frame = s_frame1.transform() == s_frame2.transform() ? s_frame2 : draw_frame::interlace(s_frame1, s_frame2, mode_);\r
-               const auto d_frame = d_frame1.transform() == d_frame2.transform() ? d_frame2 : draw_frame::interlace(d_frame1, d_frame2, mode_);\r
-               \r
-               return draw_frame::over(s_frame, d_frame);\r
-       }\r
-\r
-       void subscribe(const monitor::observable::observer_ptr& o) override                                                                                                                     \r
-       {\r
-               event_subject_.subscribe(o);\r
-       }\r
-\r
-       void unsubscribe(const monitor::observable::observer_ptr& o) override           \r
-       {\r
-               event_subject_.unsubscribe(o);\r
-       }\r
-};\r
-\r
-spl::shared_ptr<frame_producer> create_transition_producer(const field_mode& mode, const spl::shared_ptr<frame_producer>& destination, const transition_info& info)\r
-{\r
-       return spl::make_shared<transition_producer>(mode, destination, info);\r
-}\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>
+
+#include <future>
+
+namespace caspar { namespace core {    
+
+class transition_producer : public frame_producer_base
+{      
+       spl::shared_ptr<monitor::subject>       monitor_subject_;
+       const field_mode                                        mode_;
+       int                                                                     current_frame_          = 0;
+       
+       const transition_info                           info_;
+               
+       spl::shared_ptr<frame_producer>         dest_producer_;
+       spl::shared_ptr<frame_producer>         source_producer_        = frame_producer::empty();
+
+       bool                                                            paused_                         = false;
+               
+public:
+       explicit transition_producer(const field_mode& mode, const spl::shared_ptr<frame_producer>& dest, const transition_info& info) 
+               : mode_(mode)
+               , info_(info)
+               , dest_producer_(dest)
+       {
+               dest->monitor_output().attach_parent(monitor_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();
+               });                     
+                                               
+               *monitor_subject_       << monitor::message("/transition/frame") % current_frame_ % info_.duration
+                                                       << monitor::message("/transition/type") % [&]() -> std::string
+                                                                                                                               {
+                                                                                                                                       switch(info_.type)
+                                                                                                                                       {
+                                                                                                                                       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();
+       }
+
+       constraints& pixel_constraints() override
+       {
+               return dest_producer_->pixel_constraints();
+       }
+
+       uint32_t nb_frames() const override
+       {
+               return dest_producer_->nb_frames();
+       }
+
+       uint32_t frame_number() const override
+       {
+               return dest_producer_->frame_number();
+       }
+
+       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();
+       }
+       
+       std::future<std::wstring> call(const std::vector<std::wstring>& params) override
+       {
+               return dest_producer_->call(params);
+       }
+
+       // 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);
+       }
+
+       monitor::subject& monitor_output()
+       {
+               return *monitor_subject_;
+       }
+
+       void on_interaction(const interaction_event::ptr& event) override
+       {
+               dest_producer_->on_interaction(event);
+       }
+
+       bool collides(double x, double y) const override
+       {
+               return dest_producer_->collides(x, y);
+       }
+};
+
+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);
+}
+
+}}
+