]> git.sesse.net Git - casparcg/blobdiff - core/producer/stage.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / core / producer / stage.cpp
index 432e2591269f99dd0f302631446b47d8eac246ee..40298d5cf43cad33e9926fb5f7c3fc7385ba482c 100644 (file)
@@ -1,21 +1,22 @@
 /*\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
+* Author: Robert Nagy, ronag89@gmail.com\r
 */\r
 \r
 #include "../StdAfx.h"\r
 \r
 #include "layer.h"\r
 \r
-#include "../video_channel_context.h"\r
-\r
-#include <core/producer/frame/basic_frame.h>\r
-#include <core/producer/frame/frame_factory.h>\r
+#include "../frame/draw_frame.h"\r
+#include "../frame/frame_factory.h"\r
 \r
 #include <common/concurrency/executor.h>\r
+#include <common/diagnostics/graph.h>\r
+\r
+#include <core/frame/frame_transform.h>\r
 \r
 #include <boost/foreach.hpp>\r
+#include <boost/timer.hpp>\r
+#include <boost/property_tree/ptree.hpp>\r
+#include <boost/range/algorithm_ext.hpp>\r
+\r
+#include <tbb/parallel_for_each.h>\r
 \r
 #include <map>\r
-#include <set>\r
+#include <vector>\r
 \r
 namespace caspar { namespace core {\r
-               \r
-void destroy_producer(safe_ptr<frame_producer>& producer)\r
-{\r
-       if(!producer.unique())\r
-               CASPAR_LOG(debug) << producer->print() << L" Not destroyed on safe asynchronous destruction thread.";\r
        \r
-       producer = frame_producer::empty();\r
-}\r
-\r
-class destroy_producer_proxy : public frame_producer\r
-{\r
-       safe_ptr<frame_producer> producer_;\r
-       executor& destroy_context_;\r
+struct stage::impl : public std::enable_shared_from_this<impl>\r
+{                      \r
+       std::map<int, layer>                            layers_;        \r
+       std::map<int, tweened_transform>        transforms_;    \r
+       executor                                                        executor_;\r
 public:\r
-       destroy_producer_proxy(executor& destroy_context, const safe_ptr<frame_producer>& producer) \r
-               : producer_(producer)\r
-               , destroy_context_(destroy_context){}\r
-\r
-       ~destroy_producer_proxy()\r
+       impl() : executor_(L"stage")\r
+       {\r
+       }\r
+               \r
+       std::map<int, safe_ptr<draw_frame>> operator()(const struct video_format_desc& format_desc)\r
        {               \r
-               if(destroy_context_.size() > 4)\r
-                       CASPAR_LOG(error) << L" Potential destroyer deadlock.";\r
+               return executor_.invoke([=]() -> std::map<int, safe_ptr<draw_frame>>\r
+               {\r
+                       std::map<int, safe_ptr<class draw_frame>> frames;\r
 \r
-               destroy_context_.begin_invoke(std::bind(&destroy_producer, std::move(producer_)));\r
-       }\r
+                       try\r
+                       {                                       \r
+                               BOOST_FOREACH(auto& layer, layers_)                     \r
+                                       frames[layer.first] = draw_frame::empty();      \r
 \r
-       virtual safe_ptr<basic_frame>           receive(int hints)                                                                                              {return producer_->receive(hints);}\r
-       virtual safe_ptr<basic_frame>           last_frame() const                                                                                              {return producer_->last_frame();}\r
-       virtual std::wstring                            print() const                                                                                                   {return producer_->print();}\r
-       virtual void                                            param(const std::wstring& str)                                                                  {producer_->param(str);}\r
-       virtual safe_ptr<frame_producer>        get_following_producer() const                                                                  {return producer_->get_following_producer();}\r
-       virtual void                                            set_leading_producer(const safe_ptr<frame_producer>& producer)  {producer_->set_leading_producer(producer);}\r
-       virtual int64_t                                         nb_frames() const                                                                                               {return producer_->nb_frames();}\r
-};\r
+                               auto format_desc2 = format_desc;\r
 \r
-struct stage::implementation : boost::noncopyable\r
-{              \r
-       std::map<int, layer>                                            layers_;        \r
-       video_channel_context&                                          channel_;\r
-public:\r
-       implementation(video_channel_context& video_channel)  \r
-               : channel_(video_channel)\r
-       {\r
-       }\r
-                                               \r
-       std::map<int, safe_ptr<basic_frame>> execute()\r
-       {                       \r
-               std::map<int, safe_ptr<basic_frame>> frames;\r
+                               tbb::parallel_for_each(layers_.begin(), layers_.end(), [&](std::map<int, layer>::value_type& layer)\r
+                               {\r
+                                       auto transform = transforms_[layer.first].fetch_and_tick(1);\r
+\r
+                                       frame_producer::flags flags = frame_producer::flags::none;\r
+                                       if(format_desc2.field_mode != field_mode::progressive)\r
+                                       {\r
+                                               flags |= std::abs(transform.fill_scale[1]  - 1.0) > 0.0001 ? frame_producer::flags::deinterlace : frame_producer::flags::none;\r
+                                               flags |= std::abs(transform.fill_translation[1])  > 0.0001 ? frame_producer::flags::deinterlace : frame_producer::flags::none;\r
+                                       }\r
+\r
+                                       if(transform.is_key)\r
+                                               flags |= frame_producer::flags::alpha_only;\r
 \r
-               try\r
+                                       auto frame = layer.second.receive(flags);       \r
+                               \r
+                                       auto frame1 = make_safe<core::draw_frame>(frame);\r
+                                       frame1->get_frame_transform() = transform;\r
+\r
+                                       if(format_desc2.field_mode != core::field_mode::progressive)\r
+                                       {                               \r
+                                               auto frame2 = make_safe<core::draw_frame>(frame);\r
+                                               frame2->get_frame_transform() = transforms_[layer.first].fetch_and_tick(1);\r
+                                               frame1 = core::draw_frame::interlace(frame1, frame2, format_desc2.field_mode);\r
+                                       }\r
+\r
+                                       frames[layer.first] = frame1;\r
+                               });             \r
+                       }\r
+                       catch(...)\r
+                       {\r
+                               layers_.clear();\r
+                               CASPAR_LOG_CURRENT_EXCEPTION();\r
+                       }       \r
+\r
+                       return frames;\r
+               });\r
+       }\r
+               \r
+       void apply_transforms(const std::vector<std::tuple<int, stage::transform_func_t, unsigned int, tweener>>& transforms)\r
+       {\r
+               executor_.begin_invoke([=]\r
                {\r
-                       BOOST_FOREACH(auto& layer, layers_)\r
+                       BOOST_FOREACH(auto& transform, transforms)\r
                        {\r
-                               frames[layer.first] = layer.second.receive();\r
-                               channel_.execution().yield();\r
+                               auto src = transforms_[std::get<0>(transform)].fetch();\r
+                               auto dst = std::get<1>(transform)(src);\r
+                               transforms_[std::get<0>(transform)] = tweened_transform(src, dst, std::get<2>(transform), std::get<3>(transform));\r
                        }\r
-               }\r
-               catch(...)\r
+               }, high_priority);\r
+       }\r
+                                               \r
+       void apply_transform(int index, const stage::transform_func_t& transform, unsigned int mix_duration, const tweener& tween)\r
+       {\r
+               executor_.begin_invoke([=]\r
                {\r
-                       CASPAR_LOG_CURRENT_EXCEPTION();\r
-               }\r
-               \r
-               return frames;\r
+                       auto src = transforms_[index].fetch();\r
+                       auto dst = transform(src);\r
+                       transforms_[index] = tweened_transform(src, dst, mix_duration, tween);\r
+               }, high_priority);\r
+       }\r
+\r
+       void clear_transforms(int index)\r
+       {\r
+               executor_.begin_invoke([=]\r
+               {\r
+                       transforms_.erase(index);\r
+               }, high_priority);\r
        }\r
 \r
-       void load(int index, const safe_ptr<frame_producer>& producer, bool preview, int auto_play_delta)\r
+       void clear_transforms()\r
+       {\r
+               executor_.begin_invoke([=]\r
+               {\r
+                       transforms_.clear();\r
+               }, high_priority);\r
+       }\r
+               \r
+       void load(int index, const safe_ptr<frame_producer>& producer, int auto_play_delta)\r
        {\r
-               channel_.execution().invoke([&]\r
+               executor_.begin_invoke([=]\r
                {\r
-                       layers_[index].load(make_safe<destroy_producer_proxy>(channel_.destruction(), producer), preview, auto_play_delta);\r
+                       layers_[index].load(producer, auto_play_delta);\r
                }, high_priority);\r
        }\r
 \r
        void pause(int index)\r
        {               \r
-               channel_.execution().invoke([&]\r
+               executor_.begin_invoke([=]\r
                {\r
                        layers_[index].pause();\r
                }, high_priority);\r
@@ -120,7 +164,7 @@ public:
 \r
        void play(int index)\r
        {               \r
-               channel_.execution().invoke([&]\r
+               executor_.begin_invoke([=]\r
                {\r
                        layers_[index].play();\r
                }, high_priority);\r
@@ -128,7 +172,7 @@ public:
 \r
        void stop(int index)\r
        {               \r
-               channel_.execution().invoke([&]\r
+               executor_.begin_invoke([=]\r
                {\r
                        layers_[index].stop();\r
                }, high_priority);\r
@@ -136,7 +180,7 @@ public:
 \r
        void clear(int index)\r
        {\r
-               channel_.execution().invoke([&]\r
+               executor_.begin_invoke([=]\r
                {\r
                        layers_.erase(index);\r
                }, high_priority);\r
@@ -144,88 +188,106 @@ public:
                \r
        void clear()\r
        {\r
-               channel_.execution().invoke([&]\r
+               executor_.begin_invoke([=]\r
                {\r
                        layers_.clear();\r
                }, high_priority);\r
        }       \r
-       \r
-       void swap_layer(int index, size_t other_index)\r
+               \r
+       void swap_layers(const safe_ptr<stage>& other)\r
        {\r
-               channel_.execution().invoke([&]\r
+               if(other->impl_.get() == this)\r
+                       return;\r
+               \r
+               auto func = [=]\r
                {\r
-                       layers_[index].swap(layers_[other_index]);\r
+                       std::swap(layers_, other->impl_->layers_);\r
+               };              \r
+               executor_.begin_invoke([=]\r
+               {\r
+                       other->impl_->executor_.invoke(func, high_priority);\r
                }, high_priority);\r
        }\r
 \r
-       void swap_layer(int index, size_t other_index, stage& other)\r
+       void swap_layer(int index, int other_index)\r
        {\r
-               if(other.impl_.get() == this)\r
-                       swap_layer(index, other_index);\r
-               else\r
+               executor_.begin_invoke([=]\r
                {\r
-                       if(channel_.get_format_desc() != other.impl_->channel_.get_format_desc() || &channel_.ogl() != &other.impl_->channel_.ogl())\r
-                               BOOST_THROW_EXCEPTION(not_supported() << msg_info("Cannot swap between incompatible channels."));\r
-\r
-                       auto func = [&]{layers_[index].swap(other.impl_->layers_[other_index]);};\r
-               \r
-                       channel_.execution().invoke([&]{other.impl_->channel_.execution().invoke(func, high_priority);}, high_priority);\r
-               }\r
+                       std::swap(layers_[index], layers_[other_index]);\r
+               }, high_priority);\r
        }\r
 \r
-       void swap(stage& other)\r
+       void swap_layer(int index, int other_index, const safe_ptr<stage>& other)\r
        {\r
-               if(other.impl_.get() == this)\r
-                       return;\r
-               \r
-               if(channel_.get_format_desc() != other.impl_->channel_.get_format_desc() || &channel_.ogl() != &other.impl_->channel_.ogl())\r
-                       BOOST_THROW_EXCEPTION(not_supported() << msg_info("Cannot swap between incompatible channels."));\r
-\r
-               auto func = [&]\r
+               if(other->impl_.get() == this)\r
+                       swap_layer(index, other_index);\r
+               else\r
                {\r
-                       auto sel_first = [](const std::pair<int, layer>& pair){return pair.first;};\r
-\r
-                       std::set<int> indices;\r
-                       auto inserter = std::inserter(indices, indices.begin());\r
-\r
-                       std::transform(layers_.begin(), layers_.end(), inserter, sel_first);\r
-                       std::transform(other.impl_->layers_.begin(), other.impl_->layers_.end(), inserter, sel_first);\r
-\r
-                       BOOST_FOREACH(auto index, indices)\r
-                               layers_[index].swap(other.impl_->layers_[index]);\r
-               };\r
-               \r
-               channel_.execution().invoke([&]{other.impl_->channel_.execution().invoke(func, high_priority);});\r
+                       auto func = [=]\r
+                       {\r
+                               std::swap(layers_[index], other->impl_->layers_[other_index]);\r
+                       };              \r
+                       executor_.begin_invoke([=]\r
+                       {\r
+                               other->impl_->executor_.invoke(func, high_priority);\r
+                       }, high_priority);\r
+               }\r
        }\r
-       \r
+               \r
        boost::unique_future<safe_ptr<frame_producer>> foreground(int index)\r
        {\r
-               return channel_.execution().begin_invoke([=]{return layers_[index].foreground();}, high_priority);\r
+               return executor_.begin_invoke([=]\r
+               {\r
+                       return layers_[index].foreground();\r
+               }, high_priority);\r
        }\r
        \r
        boost::unique_future<safe_ptr<frame_producer>> background(int index)\r
        {\r
-               return channel_.execution().begin_invoke([=]{return layers_[index].background();}, high_priority);\r
+               return executor_.begin_invoke([=]\r
+               {\r
+                       return layers_[index].background();\r
+               }, high_priority);\r
        }\r
 \r
-       std::wstring print() const\r
+       boost::unique_future<boost::property_tree::wptree> info()\r
        {\r
-               return L"stage [" + boost::lexical_cast<std::wstring>(channel_.index()) + L"]";\r
+               return executor_.begin_invoke([this]() -> boost::property_tree::wptree\r
+               {\r
+                       boost::property_tree::wptree info;\r
+                       BOOST_FOREACH(auto& layer, layers_)                     \r
+                               info.add_child(L"layers.layer", layer.second.info())\r
+                                       .add(L"index", layer.first);    \r
+                       return info;\r
+               }, high_priority);\r
        }\r
 \r
+       boost::unique_future<boost::property_tree::wptree> info(int index)\r
+       {\r
+               return executor_.begin_invoke([=]() -> boost::property_tree::wptree\r
+               {\r
+                       return layers_[index].info();\r
+               }, high_priority);\r
+       }               \r
 };\r
 \r
-stage::stage(video_channel_context& video_channel) : impl_(new implementation(video_channel)){}\r
-void stage::swap(stage& other){impl_->swap(other);}\r
-void stage::load(int index, const safe_ptr<frame_producer>& producer, bool preview, int auto_play_delta){impl_->load(index, producer, preview, auto_play_delta);}\r
+stage::stage() : impl_(new impl()){}\r
+void stage::apply_transforms(const std::vector<stage::transform_tuple_t>& transforms){impl_->apply_transforms(transforms);}\r
+void stage::apply_transform(int index, const std::function<core::frame_transform(core::frame_transform)>& transform, unsigned int mix_duration, const tweener& tween){impl_->apply_transform(index, transform, mix_duration, tween);}\r
+void stage::clear_transforms(int index){impl_->clear_transforms(index);}\r
+void stage::clear_transforms(){impl_->clear_transforms();}\r
+void stage::load(int index, const safe_ptr<frame_producer>& producer, int auto_play_delta){impl_->load(index, producer, auto_play_delta);}\r
 void stage::pause(int index){impl_->pause(index);}\r
 void stage::play(int index){impl_->play(index);}\r
 void stage::stop(int index){impl_->stop(index);}\r
 void stage::clear(int index){impl_->clear(index);}\r
 void stage::clear(){impl_->clear();}\r
-void stage::swap_layer(int index, size_t other_index){impl_->swap_layer(index, other_index);}\r
-void stage::swap_layer(int index, size_t other_index, stage& other){impl_->swap_layer(index, other_index, other);}\r
-boost::unique_future<safe_ptr<frame_producer>> stage::foreground(size_t index) {return impl_->foreground(index);}\r
-boost::unique_future<safe_ptr<frame_producer>> stage::background(size_t index) {return impl_->background(index);}\r
-std::map<int, safe_ptr<basic_frame>> stage::execute(){return impl_->execute();}\r
+void stage::swap_layers(const safe_ptr<stage>& other){impl_->swap_layers(other);}\r
+void stage::swap_layer(int index, int other_index){impl_->swap_layer(index, other_index);}\r
+void stage::swap_layer(int index, int other_index, const safe_ptr<stage>& other){impl_->swap_layer(index, other_index, other);}\r
+boost::unique_future<safe_ptr<frame_producer>> stage::foreground(int index) {return impl_->foreground(index);}\r
+boost::unique_future<safe_ptr<frame_producer>> stage::background(int index) {return impl_->background(index);}\r
+boost::unique_future<boost::property_tree::wptree> stage::info() const{return impl_->info();}\r
+boost::unique_future<boost::property_tree::wptree> stage::info(int index) const{return impl_->info(index);}\r
+std::map<int, safe_ptr<class draw_frame>> stage::operator()(const video_format_desc& format_desc){return (*impl_)(format_desc);}\r
 }}
\ No newline at end of file