]> git.sesse.net Git - casparcg/blobdiff - core/producer/transition/transition_producer.cpp
2.1.0: -frame_transform: Split frame_transform into image and audio-transforms.
[casparcg] / core / producer / transition / transition_producer.cpp
index 62232be6351f931caf017dc65fc43e6fd06064dc..e3056c75f668e6d18106ded08c8628d7ac354eb7 100644 (file)
 /*\r
-* copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
 *\r
-*  This file is part of CasparCG.\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
+* 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
+* 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
+* Author: Robert Nagy, ronag89@gmail.com\r
+*/\r
+\r
 #include "../../stdafx.h"\r
 \r
 #include "transition_producer.h"\r
 \r
-#include "../../frame/frame_format.h"\r
-#include "../../frame/gpu_frame.h"\r
-#include "../../frame/composite_gpu_frame.h"\r
-#include "../../frame/frame_factory.h"\r
-\r
-#include "../../../common/utility/memory.h"\r
-#include "../../renderer/render_device.h"\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 <boost/range/algorithm/copy.hpp>\r
+#include <tbb/parallel_invoke.h>\r
 \r
 namespace caspar { namespace core {    \r
 \r
-struct transition_producer::implementation : boost::noncopyable\r
-{\r
-       implementation(const frame_producer_ptr& dest, const transition_info& info, const frame_format_desc& format_desc) \r
-               : current_frame_(0), info_(info), format_desc_(format_desc), dest_(dest)\r
-       {\r
-               if(!dest)\r
-                       BOOST_THROW_EXCEPTION(null_argument() << arg_name_info("dest"));\r
-       }\r
+class transition_producer : public frame_producer\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<draw_frame>                     last_frame_;\r
+       \r
+       spl::shared_ptr<frame_producer>         dest_producer_;\r
+       spl::shared_ptr<frame_producer>         source_producer_;\r
                \r
-       frame_producer_ptr get_following_producer() const\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
+               , last_frame_(draw_frame::empty())\r
+               , dest_producer_(dest)\r
+               , source_producer_(frame_producer::empty())\r
        {\r
-               return dest_;\r
+               dest->subscribe(event_subject_);\r
        }\r
        \r
-       void set_leading_producer(const frame_producer_ptr& producer)\r
-       {\r
-               source_ = producer;\r
-       }\r
+       // frame_producer\r
                \r
-       gpu_frame_ptr get_frame()\r
+       virtual void leading_producer(const spl::shared_ptr<frame_producer>& producer) override\r
        {\r
-               return ++current_frame_ >= info_.duration ? nullptr : compose(get_producer_frame(dest_), get_producer_frame(source_));\r
+               source_producer_ = producer;\r
        }\r
 \r
-       gpu_frame_ptr get_producer_frame(frame_producer_ptr& producer)\r
+       virtual spl::shared_ptr<draw_frame> receive(int flags) override\r
        {\r
-               if(producer == nullptr)\r
-               {       \r
-                       auto frame = factory_->create_frame(format_desc_);\r
-                       common::clear(frame->data(), frame->size());\r
-                       return frame;\r
-               }\r
-\r
-               gpu_frame_ptr frame;\r
-               try\r
+               if(current_frame_ >= info_.duration)\r
+                       return dest_producer_->receive(flags);\r
+               \r
+               ++current_frame_;\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
+               auto dest = draw_frame::empty();\r
+               auto source = draw_frame::empty();\r
+\r
+               tbb::parallel_invoke(\r
+               [&]\r
                {\r
-                       frame = producer->get_frame();\r
-               }\r
-               catch(...)\r
+                       dest = dest_producer_->receive(flags);\r
+                       if(dest == core::draw_frame::late())\r
+                               dest = dest_producer_->last_frame();\r
+               },\r
+               [&]\r
                {\r
-                       CASPAR_LOG_CURRENT_EXCEPTION();\r
-                       producer = nullptr;\r
-                       CASPAR_LOG(warning) << "Removed renderer from transition.";\r
-               }\r
+                       source = source_producer_->receive(flags);\r
+                       if(source == core::draw_frame::late())\r
+                               source = source_producer_->last_frame();\r
+               });             \r
 \r
-               if(frame == nullptr && producer != nullptr && producer->get_following_producer() != nullptr)\r
-               {\r
-                       auto following = producer->get_following_producer();\r
-                       following->initialize(factory_);\r
-                       following->set_leading_producer(producer);\r
-                       producer = following;\r
-                       return get_producer_frame(producer);\r
-               }\r
-               return frame;\r
+               return compose(dest, source);\r
+       }\r
+\r
+       virtual spl::shared_ptr<draw_frame> last_frame() const override\r
+       {\r
+               return dest_producer_->last_frame();\r
+       }\r
+       \r
+       virtual uint32_t nb_frames() const override\r
+       {\r
+               return dest_producer_->nb_frames();\r
+       }\r
+\r
+       virtual std::wstring print() const override\r
+       {\r
+               return L"transition[" + source_producer_->print() + L"=>" + dest_producer_->print() + L"]";\r
+       }\r
+\r
+       virtual 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
-       gpu_frame_ptr compose(const gpu_frame_ptr& dest_frame, const gpu_frame_ptr& src_frame) \r
-       {       \r
-               if(!src_frame)\r
-                       return dest_frame;\r
 \r
-               if(info_.type == transition_type::cut || !dest_frame)           \r
+       // transition_producer\r
+                                               \r
+       spl::shared_ptr<draw_frame> compose(const spl::shared_ptr<draw_frame>& dest_frame, const spl::shared_ptr<draw_frame>& src_frame) \r
+       {       \r
+               if(info_.type == transition_type::cut)          \r
                        return src_frame;\r
-               \r
-               int volume = static_cast<int>(static_cast<double>(current_frame_)/static_cast<double>(info_.duration)*256.0);\r
-                               \r
-               for(size_t n = 0; n < dest_frame->audio_data().size(); ++n)\r
-                       dest_frame->audio_data()[n] = static_cast<short>((static_cast<int>(dest_frame->audio_data()[n])*volume)>>8);\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
-               for(size_t n = 0; n < src_frame->audio_data().size(); ++n)\r
-                       src_frame->audio_data()[n] = static_cast<short>((static_cast<int>(src_frame->audio_data()[n])*(256-volume))>>8);\r
-                               \r
-               float alpha = static_cast<float>(current_frame_)/static_cast<float>(info_.duration);\r
-               auto composite = std::make_shared<composite_gpu_frame>(format_desc_.width, format_desc_.height);\r
-               composite->add(src_frame);\r
-               composite->add(dest_frame);\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->frame_transform().audio_transform.volume = 1.0-delta2;\r
+               auto s_frame1 = spl::make_shared<draw_frame>(src_frame);\r
+               auto s_frame2 = spl::make_shared<draw_frame>(src_frame);\r
+               \r
+               dest_frame->frame_transform().audio_transform.volume = delta2;\r
+               auto d_frame1 = spl::make_shared<draw_frame>(dest_frame);\r
+               auto d_frame2 = spl::make_shared<draw_frame>(dest_frame);\r
+               \r
                if(info_.type == transition_type::mix)\r
                {\r
-                       src_frame->alpha(1.0f-alpha);\r
-                       dest_frame->alpha(alpha);\r
+                       d_frame1->frame_transform().image_transform.opacity = delta1;   \r
+                       d_frame1->frame_transform().image_transform.is_mix = true;\r
+                       d_frame2->frame_transform().image_transform.opacity = delta2;\r
+                       d_frame2->frame_transform().image_transform.is_mix = true;\r
+\r
+                       s_frame1->frame_transform().image_transform.opacity = 1.0-delta1;       \r
+                       s_frame1->frame_transform().image_transform.is_mix = true;\r
+                       s_frame2->frame_transform().image_transform.opacity = 1.0-delta2;       \r
+                       s_frame2->frame_transform().image_transform.is_mix = true;\r
                }\r
-               else if(info_.type == transition_type::slide)\r
+               if(info_.type == transition_type::slide)\r
                {\r
-                       if(info_.direction == transition_direction::from_left)                  \r
-                               dest_frame->translate(-1.0f+alpha, 0.0f);                       \r
-                       else if(info_.direction == transition_direction::from_right)\r
-                               dest_frame->translate(1.0f-alpha, 0.0f);                        \r
+                       d_frame1->frame_transform().image_transform.fill_translation[0] = (-1.0+delta1)*dir;    \r
+                       d_frame2->frame_transform().image_transform.fill_translation[0] = (-1.0+delta2)*dir;            \r
                }\r
                else if(info_.type == transition_type::push)\r
                {\r
-                       if(info_.direction == transition_direction::from_left)          \r
-                       {\r
-                               dest_frame->translate(-1.0f+alpha, 0.0f);\r
-                               src_frame->translate(0.0f+alpha, 0.0f);\r
-                       }\r
-                       else if(info_.direction == transition_direction::from_right)\r
-                       {\r
-                               dest_frame->translate(1.0f-alpha, 0.0f);\r
-                               src_frame->translate(0.0f-alpha, 0.0f);\r
-                       }\r
+                       d_frame1->frame_transform().image_transform.fill_translation[0] = (-1.0+delta1)*dir;\r
+                       d_frame2->frame_transform().image_transform.fill_translation[0] = (-1.0+delta2)*dir;\r
+\r
+                       s_frame1->frame_transform().image_transform.fill_translation[0] = (0.0+delta1)*dir;     \r
+                       s_frame2->frame_transform().image_transform.fill_translation[0] = (0.0+delta2)*dir;             \r
                }\r
-               return composite;\r
-       }\r
+               else if(info_.type == transition_type::wipe)            \r
+               {\r
+                       d_frame1->frame_transform().image_transform.clip_scale[0] = delta1;     \r
+                       d_frame2->frame_transform().image_transform.clip_scale[0] = delta2;                     \r
+               }\r
+                               \r
+               const auto s_frame = s_frame1->frame_transform() == s_frame2->frame_transform() ? s_frame2 : draw_frame::interlace(s_frame1, s_frame2, mode_);\r
+               const auto d_frame = d_frame1->frame_transform() == d_frame2->frame_transform() ? d_frame2 : draw_frame::interlace(d_frame1, d_frame2, mode_);\r
                \r
-       void initialize(const frame_factory_ptr& factory)\r
-       {\r
-               dest_->initialize(factory);\r
-               factory_ = factory;\r
+               return draw_frame::over(s_frame, d_frame);\r
        }\r
 \r
-       const frame_format_desc format_desc_;\r
+       virtual void subscribe(const monitor::observable::observer_ptr& o) override                                                                                                                     \r
+       {\r
+               event_subject_.subscribe(o);\r
+       }\r
 \r
-       frame_producer_ptr              source_;\r
-       frame_producer_ptr              dest_;\r
-       \r
-       unsigned short                  current_frame_;\r
-       \r
-       const transition_info   info_;\r
-       frame_factory_ptr               factory_;\r
+       virtual void unsubscribe(const monitor::observable::observer_ptr& o) override           \r
+       {\r
+               event_subject_.unsubscribe(o);\r
+       }\r
 };\r
 \r
-transition_producer::transition_producer(const frame_producer_ptr& dest, const transition_info& info, const frame_format_desc& format_desc) \r
-       : impl_(new implementation(dest, info, format_desc)){}\r
-gpu_frame_ptr transition_producer::get_frame(){return impl_->get_frame();}\r
-frame_producer_ptr transition_producer::get_following_producer() const{return impl_->get_following_producer();}\r
-void transition_producer::set_leading_producer(const frame_producer_ptr& producer) { impl_->set_leading_producer(producer); }\r
-const frame_format_desc& transition_producer::get_frame_format_desc() const { return impl_->format_desc_; } \r
-void transition_producer::initialize(const frame_factory_ptr& factory) { impl_->initialize(factory);}\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 core::wrap_producer(spl::make_shared<transition_producer>(mode, destination, info));\r
+}\r
 \r
 }}\r
 \r