]> git.sesse.net Git - casparcg/blobdiff - core/producer/transition/transition_producer.cpp
2.0. Fixed mix transition.
[casparcg] / core / producer / transition / transition_producer.cpp
index 1cb6b00f5e3b9bf14afe570f26f72c59bf06f8fa..24cf9287533810a4854657b8f5cab816ac344fb8 100644 (file)
 \r
 #include "transition_producer.h"\r
 \r
-#include "../../format/video_format.h"\r
-#include "../../processor/frame.h"\r
-#include "../../processor/composite_frame.h"\r
-#include "../../processor/frame_processor_device.h"\r
+#include <core/video_format.h>\r
 \r
-#include "../../producer/frame_producer_device.h"\r
+#include <core/producer/frame/basic_frame.h>\r
+#include <core/producer/frame/frame_transform.h>\r
 \r
-#include <boost/range/algorithm/copy.hpp>\r
+#include <tbb/parallel_invoke.h>\r
+\r
+#include <boost/assign.hpp>\r
+\r
+using namespace boost::assign;\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) \r
-               : current_frame_(0), info_(info), dest_producer_(dest)\r
-       {\r
-               if(!dest)\r
-                       BOOST_THROW_EXCEPTION(null_argument() << arg_name_info("dest"));\r
-       }\r
+struct transition_producer : public frame_producer\r
+{      \r
+       const field_mode::type          mode_;\r
+       unsigned int                            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
+       safe_ptr<basic_frame>           last_frame_;\r
                \r
-       frame_producer_ptr get_following_producer() const\r
+       explicit transition_producer(const field_mode::type& mode, const safe_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
+               , last_frame_(basic_frame::empty()){}\r
+       \r
+       // frame_producer\r
+\r
+       virtual safe_ptr<frame_producer> get_following_producer() const\r
        {\r
                return dest_producer_;\r
        }\r
        \r
-       void set_leading_producer(const frame_producer_ptr& producer)\r
+       virtual void set_leading_producer(const safe_ptr<frame_producer>& producer)\r
        {\r
                source_producer_ = producer;\r
        }\r
-               \r
-       frame_ptr render_frame()\r
+\r
+       virtual safe_ptr<basic_frame> receive(int hints)\r
        {\r
-               if(current_frame_ == 0)\r
-                       CASPAR_LOG(info) << "Transition started.";\r
+               if(++current_frame_ >= info_.duration)\r
+                       return basic_frame::eof();\r
+               \r
+               auto dest = basic_frame::empty();\r
+               auto source = basic_frame::empty();\r
 \r
-               frame_ptr result = [&]() -> frame_ptr\r
+               tbb::parallel_invoke(\r
+               [&]\r
                {\r
-                       if(current_frame_++ >= info_.duration)\r
-                               return nullptr;\r
-\r
-                       frame_ptr source;\r
-                       frame_ptr dest;\r
-\r
-                       tbb::parallel_invoke\r
-                       (\r
-                               [&]{dest   = render_frame(dest_producer_);},\r
-                               [&]{source = render_frame(source_producer_);}\r
-                       );\r
-\r
-                       return compose(dest, source);\r
-               }();\r
-\r
-               if(result == nullptr)\r
-                       CASPAR_LOG(info) << "Transition ended.";\r
+                       dest = receive_and_follow(dest_producer_, hints);\r
+                       if(dest == core::basic_frame::late())\r
+                               dest = dest_producer_->last_frame();\r
+               },\r
+               [&]\r
+               {\r
+                       source = receive_and_follow(source_producer_, hints);\r
+                       if(source == core::basic_frame::late())\r
+                               source = source_producer_->last_frame();\r
+               });\r
 \r
-               return result;\r
+               return compose(dest, source);\r
        }\r
 \r
-       frame_ptr render_frame(frame_producer_ptr& producer)\r
+       virtual safe_ptr<core::basic_frame> last_frame() const\r
        {\r
-               if(producer == nullptr)\r
-                       return nullptr;\r
-\r
-               frame_ptr frame;\r
-               try\r
-               {\r
-                       frame = producer->render_frame();\r
-               }\r
-               catch(...)\r
-               {\r
-                       CASPAR_LOG_CURRENT_EXCEPTION();\r
-                       producer = nullptr;\r
-                       CASPAR_LOG(warning) << "Removed producer from transition.";\r
-               }\r
-\r
-               if(frame == nullptr)\r
-               {\r
-                       if(producer == nullptr || producer->get_following_producer() == nullptr)\r
-                               return nullptr;\r
-\r
-                       try\r
-                       {\r
-                               auto following = producer->get_following_producer();\r
-                               following->initialize(frame_processor_);\r
-                               following->set_leading_producer(producer);\r
-                               producer = following;\r
-                       }\r
-                       catch(...)\r
-                       {\r
-                               CASPAR_LOG(warning) << "Failed to initialize following producer. Removing it.";\r
-                               producer = nullptr;\r
-                       }\r
-\r
-                       return render_frame(producer);\r
-               }\r
-               return frame;\r
+               return disable_audio(last_frame_);\r
        }\r
-                       \r
-       void set_volume(const frame_ptr& frame, int volume)\r
+\r
+       virtual int64_t nb_frames() const \r
        {\r
-               if(!frame)\r
-                       return;\r
+               return get_following_producer()->nb_frames();\r
+       }\r
 \r
-               for(size_t n = 0; n < frame->get_audio_data().size(); ++n)\r
-                       frame->get_audio_data()[n] = static_cast<short>((static_cast<int>(frame->get_audio_data()[n])*volume)>>8);\r
+       virtual std::wstring print() const\r
+       {\r
+               return L"transition[" + source_producer_->print() + L"|" + dest_producer_->print() + L"]";\r
        }\r
-               \r
-       frame_ptr compose(const frame_ptr& dest_frame, frame_ptr src_frame) \r
+       \r
+       // transition_producer\r
+                                               \r
+       safe_ptr<basic_frame> compose(const safe_ptr<basic_frame>& dest_frame, const safe_ptr<basic_frame>& src_frame) \r
        {       \r
                if(info_.type == transition::cut)               \r
                        return src_frame;\r
+                                                                               \r
+               const double delta1 = info_.tweener(current_frame_*2-1, 0.0, 1.0, info_.duration*2);\r
+               const double delta2 = info_.tweener(current_frame_*2, 0.0, 1.0, info_.duration*2);  \r
 \r
-               if(!dest_frame)\r
-                       return nullptr;\r
-                                                               \r
-               double alpha = static_cast<double>(current_frame_)/static_cast<double>(info_.duration);\r
-               int volume = static_cast<int>(alpha*256.0);\r
-                               \r
-               tbb::parallel_invoke\r
-               (\r
-                       [&]{set_volume(dest_frame, volume);},\r
-                       [&]{set_volume(src_frame, 256-volume);}\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
+               auto s_frame1 = make_safe<basic_frame>(src_frame);\r
+               auto s_frame2 = make_safe<basic_frame>(src_frame);\r
+\r
+               s_frame1->get_frame_transform().volume = 0.0;\r
+               s_frame2->get_frame_transform().volume = 1.0-delta2;\r
+\r
+               auto d_frame1 = make_safe<basic_frame>(dest_frame);\r
+               auto d_frame2 = make_safe<basic_frame>(dest_frame);\r
+               \r
+               d_frame1->get_frame_transform().volume = 0.0;\r
+               d_frame2->get_frame_transform().volume = delta2;\r
+\r
                if(info_.type == transition::mix)\r
-                       dest_frame->get_render_transform().alpha = alpha;               \r
-               else if(info_.type == transition::slide)\r
-               {       \r
-                       if(info_.direction == transition_direction::from_left)                  \r
-                               dest_frame->get_render_transform().pos = boost::make_tuple(-1.0+alpha, 0.0);                    \r
-                       else if(info_.direction == transition_direction::from_right)\r
-                               dest_frame->get_render_transform().pos = boost::make_tuple(1.0-alpha, 0.0);             \r
+               {\r
+                       d_frame1->get_frame_transform().opacity = delta1;       \r
+                       d_frame1->get_frame_transform().is_mix = true;\r
+                       d_frame2->get_frame_transform().opacity = delta2;\r
+                       d_frame2->get_frame_transform().is_mix = true;\r
+\r
+                       s_frame1->get_frame_transform().opacity = 1.0-delta1;   \r
+                       s_frame1->get_frame_transform().is_mix = true;\r
+                       s_frame2->get_frame_transform().opacity = 1.0-delta2;   \r
+                       s_frame2->get_frame_transform().is_mix = true;\r
+               }\r
+               if(info_.type == transition::slide)\r
+               {\r
+                       d_frame1->get_frame_transform().fill_translation[0] = (-1.0+delta1)*dir;        \r
+                       d_frame2->get_frame_transform().fill_translation[0] = (-1.0+delta2)*dir;                \r
                }\r
                else if(info_.type == transition::push)\r
                {\r
-                       if(info_.direction == transition_direction::from_left)          \r
-                       {\r
-                               dest_frame->get_render_transform().pos = boost::make_tuple(-1.0+alpha, 0.0);\r
-                               if(src_frame)\r
-                                       src_frame->get_render_transform().pos = boost::make_tuple(0.0+alpha, 0.0);\r
-                       }\r
-                       else if(info_.direction == transition_direction::from_right)\r
-                       {\r
-                               dest_frame->get_render_transform().pos = boost::make_tuple(1.0-alpha, 0.0);\r
-                               if(src_frame)\r
-                                       src_frame->get_render_transform().pos = boost::make_tuple(0.0-alpha, 0.0);\r
-                       }\r
+                       d_frame1->get_frame_transform().fill_translation[0] = (-1.0+delta1)*dir;\r
+                       d_frame2->get_frame_transform().fill_translation[0] = (-1.0+delta2)*dir;\r
+\r
+                       s_frame1->get_frame_transform().fill_translation[0] = (0.0+delta1)*dir; \r
+                       s_frame2->get_frame_transform().fill_translation[0] = (0.0+delta2)*dir;         \r
                }\r
-               else if(info_.type == transition::wipe)\r
+               else if(info_.type == transition::wipe)         \r
                {\r
-                       if(info_.direction == transition_direction::from_left)          \r
-                       {\r
-                               dest_frame->get_render_transform().pos = boost::make_tuple(-1.0+alpha, 0.0);\r
-                               dest_frame->get_render_transform().uv = boost::make_tuple(-1.0+alpha, 1.0, alpha, 0.0);\r
-                       }\r
-                       else if(info_.direction == transition_direction::from_right)\r
-                       {\r
-                               dest_frame->get_render_transform().pos = boost::make_tuple(1.0-alpha, 0.0);\r
-                               dest_frame->get_render_transform().uv = boost::make_tuple(1.0-alpha, 1.0, 2.0-alpha, 0.0);\r
-                       }\r
+                       d_frame1->get_frame_transform().clip_scale[0] = delta1; \r
+                       d_frame2->get_frame_transform().clip_scale[0] = delta2;                 \r
                }\r
-                                               \r
-               return std::make_shared<composite_frame>(src_frame, dest_frame);\r
-       }\r
+                               \r
+               const auto s_frame = s_frame1->get_frame_transform() == s_frame2->get_frame_transform() ? s_frame2 : basic_frame::interlace(s_frame1, s_frame2, mode_);\r
+               const auto d_frame = d_frame1->get_frame_transform() == d_frame2->get_frame_transform() ? d_frame2 : basic_frame::interlace(d_frame1, d_frame2, mode_);\r
                \r
-       void initialize(const frame_processor_device_ptr& frame_processor)\r
-       {\r
-               dest_producer_->initialize(frame_processor);\r
-               frame_processor_ = frame_processor;\r
+               last_frame_ = basic_frame::combine(s_frame2, d_frame2);\r
+\r
+               return basic_frame::combine(s_frame, d_frame);\r
        }\r
-       \r
-       frame_producer_ptr                      source_producer_;\r
-       frame_producer_ptr                      dest_producer_;\r
-       \r
-       unsigned short                          current_frame_;\r
-       \r
-       const transition_info           info_;\r
-       frame_processor_device_ptr      frame_processor_;\r
 };\r
 \r
-transition_producer::transition_producer(const frame_producer_ptr& dest, const transition_info& info) \r
-       : impl_(new implementation(dest, info)){}\r
-frame_ptr transition_producer::render_frame(){return impl_->render_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
-void transition_producer::initialize(const frame_processor_device_ptr& frame_processor) { impl_->initialize(frame_processor);}\r
+safe_ptr<frame_producer> create_transition_producer(const field_mode::type& mode, const safe_ptr<frame_producer>& destination, const transition_info& info)\r
+{\r
+       return make_safe<transition_producer>(mode, destination, info);\r
+}\r
 \r
 }}\r
 \r