]> git.sesse.net Git - casparcg/blobdiff - core/producer/stage.cpp
2.1.0: ffmpeg_producer: Preallocate/reserve some frames during construction.
[casparcg] / core / producer / stage.cpp
index 2396e7be6b2368fa75ae80737531b428cdd3ae48..3ac8a983ec97b09f4dc8b3747983a43269a12c64 100644 (file)
 \r
 #include "layer.h"\r
 \r
-#include "frame/basic_frame.h"\r
-#include "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/producer/frame/frame_transform.h>\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 <boost/property_tree/ptree.hpp>\r
-\r
 #include <map>\r
+#include <vector>\r
 \r
 namespace caspar { namespace core {\r
        \r
-template<typename T>\r
-class tweened_transform\r
-{\r
-       T source_;\r
-       T dest_;\r
-       int duration_;\r
-       int time_;\r
-       tweener_t tweener_;\r
-public:        \r
-       tweened_transform()\r
-               : duration_(0)\r
-               , time_(0)\r
-               , tweener_(get_tweener(L"linear")){}\r
-       tweened_transform(const T& source, const T& dest, int duration, const std::wstring& tween = L"linear")\r
-               : source_(source)\r
-               , dest_(dest)\r
-               , duration_(duration)\r
-               , time_(0)\r
-               , tweener_(get_tweener(tween)){}\r
-       \r
-       T fetch()\r
-       {\r
-               return time_ == duration_ ? dest_ : tween(static_cast<double>(time_), source_, dest_, static_cast<double>(duration_), tweener_);\r
-       }\r
-\r
-       T fetch_and_tick(int num)\r
-       {                                               \r
-               time_ = std::min(time_+num, duration_);\r
-               return fetch();\r
-       }\r
-};\r
-\r
-struct stage::implementation : public std::enable_shared_from_this<implementation>\r
-                                                        , boost::noncopyable\r
-{              \r
-       safe_ptr<diagnostics::graph>    graph_;\r
-       safe_ptr<stage::target_t>               target_;\r
-       video_format_desc                               format_desc_;\r
-\r
-       boost::timer                                    produce_timer_;\r
-       boost::timer                                    tick_timer_;\r
-\r
-       std::map<int, layer>                    layers_;        \r
-       std::map<int, tweened_transform<core::frame_transform>> transforms_;    \r
-\r
-       executor                                                executor_;\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
-       implementation(const safe_ptr<diagnostics::graph>& graph, const safe_ptr<stage::target_t>& target, const video_format_desc& format_desc)  \r
-               : graph_(graph)\r
-               , format_desc_(format_desc)\r
-               , target_(target)\r
-               , executor_(L"stage")\r
-       {\r
-               graph_->add_guide("tick-time", 0.5f);   \r
-               graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));   \r
-               graph_->set_color("produce-time", diagnostics::color(0.0f, 1.0f, 0.0f));\r
-       }\r
-\r
-       void spawn_token()\r
+       impl() : executor_(L"stage")\r
        {\r
-               std::weak_ptr<implementation> self = shared_from_this();\r
-               executor_.begin_invoke([=]{tick(self);});\r
        }\r
-                                                       \r
-       void tick(const std::weak_ptr<implementation>& self)\r
+               \r
+       std::map<int, spl::shared_ptr<draw_frame>> operator()(const struct video_format_desc& format_desc)\r
        {               \r
-               try\r
+               return executor_.invoke([=]() -> std::map<int, spl::shared_ptr<draw_frame>>\r
                {\r
-                       produce_timer_.restart();\r
+                       std::map<int, spl::shared_ptr<class draw_frame>> frames;\r
 \r
-                       std::map<int, safe_ptr<basic_frame>> frames;\r
-               \r
-                       BOOST_FOREACH(auto& layer, layers_)                     \r
-                               frames[layer.first] = basic_frame::empty();     \r
+                       try\r
+                       {                                       \r
+                               BOOST_FOREACH(auto& layer, layers_)                     \r
+                                       frames[layer.first] = draw_frame::empty();      \r
 \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
+                               auto format_desc2 = format_desc;\r
 \r
-                               int hints = frame_producer::NO_HINT;\r
-                               if(format_desc_.field_mode != field_mode::progressive)\r
+                               tbb::parallel_for_each(layers_.begin(), layers_.end(), [&](std::map<int, layer>::value_type& layer)\r
                                {\r
-                                       hints |= std::abs(transform.fill_scale[1]  - 1.0) > 0.0001 ? frame_producer::DEINTERLACE_HINT : frame_producer::NO_HINT;\r
-                                       hints |= std::abs(transform.fill_translation[1]) > 0.0001 ? frame_producer::DEINTERLACE_HINT : frame_producer::NO_HINT;\r
-                               }\r
+                                       auto transform = transforms_[layer.first].fetch_and_tick(1);\r
 \r
-                               if(transform.is_key)\r
-                                       hints |= frame_producer::ALPHA_HINT;\r
-\r
-                               auto frame = layer.second.receive(hints);       \r
-                               \r
-                               auto frame1 = make_safe<core::basic_frame>(frame);\r
-                               frame1->get_frame_transform() = transform;\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(format_desc_.field_mode != core::field_mode::progressive)\r
-                               {                               \r
-                                       auto frame2 = make_safe<core::basic_frame>(frame);\r
-                                       frame2->get_frame_transform() = transforms_[layer.first].fetch_and_tick(1);\r
-                                       frame1 = core::basic_frame::interlace(frame1, frame2, format_desc_.field_mode);\r
-                               }\r
+                                       if(transform.is_key)\r
+                                               flags |= frame_producer::flags::alpha_only;\r
 \r
-                               frames[layer.first] = frame1;\r
-                       });\r
-                       \r
-                       graph_->update_value("produce-time", produce_timer_.elapsed()*format_desc_.fps*0.5);\r
-                       \r
-                       std::shared_ptr<void> ticket(nullptr, [self](void*)\r
+                                       auto frame = layer.second.receive(flags);       \r
+                               \r
+                                       auto frame1 = spl::make_shared<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 = spl::make_shared<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
-                               auto self2 = self.lock();\r
-                               if(self2)                               \r
-                                       self2->executor_.begin_invoke([=]{tick(self);});                                \r
-                       });\r
-\r
-                       target_->send(std::make_pair(frames, ticket));\r
+                               layers_.clear();\r
+                               CASPAR_LOG_CURRENT_EXCEPTION();\r
+                       }       \r
 \r
-                       graph_->update_value("tick-time", tick_timer_.elapsed()*format_desc_.fps*0.5);\r
-                       tick_timer_.restart();\r
-               }\r
-               catch(...)\r
-               {\r
-                       layers_.clear();\r
-                       CASPAR_LOG_CURRENT_EXCEPTION();\r
-               }               \r
+                       return frames;\r
+               });\r
        }\r
-               void set_transform(int index, const frame_transform& transform, unsigned int mix_duration, const std::wstring& tween)\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
-                       auto src = transforms_[index].fetch();\r
-                       auto dst = transform;\r
-                       transforms_[index] = tweened_transform<frame_transform>(src, dst, mix_duration, tween);\r
-               }, high_priority);\r
+                       BOOST_FOREACH(auto& transform, transforms)\r
+                       {\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
+               }, task_priority::high_priority);\r
        }\r
-                               \r
-       void apply_transform(int index, const std::function<frame_transform(frame_transform)>& transform, unsigned int mix_duration, const std::wstring& tween)\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
                        auto src = transforms_[index].fetch();\r
                        auto dst = transform(src);\r
-                       transforms_[index] = tweened_transform<frame_transform>(src, dst, mix_duration, tween);\r
-               }, high_priority);\r
+                       transforms_[index] = tweened_transform(src, dst, mix_duration, tween);\r
+               }, task_priority::high_priority);\r
        }\r
 \r
        void clear_transforms(int index)\r
@@ -192,7 +135,7 @@ public:
                executor_.begin_invoke([=]\r
                {\r
                        transforms_.erase(index);\r
-               }, high_priority);\r
+               }, task_priority::high_priority);\r
        }\r
 \r
        void clear_transforms()\r
@@ -200,15 +143,15 @@ public:
                executor_.begin_invoke([=]\r
                {\r
                        transforms_.clear();\r
-               }, high_priority);\r
+               }, task_priority::high_priority);\r
        }\r
                \r
-       void load(int index, const safe_ptr<frame_producer>& producer, bool preview, int auto_play_delta)\r
+       void load(int index, const spl::shared_ptr<frame_producer>& producer, const boost::optional<int32_t>& auto_play_delta)\r
        {\r
                executor_.begin_invoke([=]\r
                {\r
-                       layers_[index].load(producer, preview, auto_play_delta);\r
-               }, high_priority);\r
+                       layers_[index].load(producer, auto_play_delta);\r
+               }, task_priority::high_priority);\r
        }\r
 \r
        void pause(int index)\r
@@ -216,7 +159,7 @@ public:
                executor_.begin_invoke([=]\r
                {\r
                        layers_[index].pause();\r
-               }, high_priority);\r
+               }, task_priority::high_priority);\r
        }\r
 \r
        void play(int index)\r
@@ -224,7 +167,7 @@ public:
                executor_.begin_invoke([=]\r
                {\r
                        layers_[index].play();\r
-               }, high_priority);\r
+               }, task_priority::high_priority);\r
        }\r
 \r
        void stop(int index)\r
@@ -232,7 +175,7 @@ public:
                executor_.begin_invoke([=]\r
                {\r
                        layers_[index].stop();\r
-               }, high_priority);\r
+               }, task_priority::high_priority);\r
        }\r
 \r
        void clear(int index)\r
@@ -240,7 +183,7 @@ public:
                executor_.begin_invoke([=]\r
                {\r
                        layers_.erase(index);\r
-               }, high_priority);\r
+               }, task_priority::high_priority);\r
        }\r
                \r
        void clear()\r
@@ -248,18 +191,10 @@ public:
                executor_.begin_invoke([=]\r
                {\r
                        layers_.clear();\r
-               }, high_priority);\r
+               }, task_priority::high_priority);\r
        }       \r
-       \r
-       boost::unique_future<std::wstring> call(int index, bool foreground, const std::wstring& param)\r
-       {\r
-               return std::move(*executor_.invoke([=]\r
-               {\r
-                       return std::make_shared<boost::unique_future<std::wstring>>(std::move(layers_[index].call(foreground, param)));\r
-               }, high_priority));\r
-       }\r
-       \r
-       void swap_layers(const safe_ptr<stage>& other)\r
+               \r
+       void swap_layers(const spl::shared_ptr<stage>& other)\r
        {\r
                if(other->impl_.get() == this)\r
                        return;\r
@@ -270,19 +205,19 @@ public:
                };              \r
                executor_.begin_invoke([=]\r
                {\r
-                       other->impl_->executor_.invoke(func, high_priority);\r
-               }, high_priority);\r
+                       other->impl_->executor_.invoke(func, task_priority::high_priority);\r
+               }, task_priority::high_priority);\r
        }\r
 \r
-       void swap_layer(int index, size_t other_index)\r
+       void swap_layer(int index, int other_index)\r
        {\r
                executor_.begin_invoke([=]\r
                {\r
                        std::swap(layers_[index], layers_[other_index]);\r
-               }, high_priority);\r
+               }, task_priority::high_priority);\r
        }\r
 \r
-       void swap_layer(int index, size_t other_index, const safe_ptr<stage>& other)\r
+       void swap_layer(int index, int other_index, const spl::shared_ptr<stage>& other)\r
        {\r
                if(other->impl_.get() == this)\r
                        swap_layer(index, other_index);\r
@@ -294,75 +229,65 @@ public:
                        };              \r
                        executor_.begin_invoke([=]\r
                        {\r
-                               other->impl_->executor_.invoke(func, high_priority);\r
-                       }, high_priority);\r
+                               other->impl_->executor_.invoke(func, task_priority::high_priority);\r
+                       }, task_priority::high_priority);\r
                }\r
        }\r
                \r
-       boost::unique_future<safe_ptr<frame_producer>> foreground(int index)\r
+       boost::unique_future<spl::shared_ptr<frame_producer>> foreground(int index)\r
        {\r
                return executor_.begin_invoke([=]\r
                {\r
                        return layers_[index].foreground();\r
-               }, high_priority);\r
+               }, task_priority::high_priority);\r
        }\r
        \r
-       boost::unique_future<safe_ptr<frame_producer>> background(int index)\r
+       boost::unique_future<spl::shared_ptr<frame_producer>> background(int index)\r
        {\r
                return executor_.begin_invoke([=]\r
                {\r
                        return layers_[index].background();\r
-               }, high_priority);\r
-       }\r
-       \r
-       void set_video_format_desc(const video_format_desc& format_desc)\r
-       {\r
-               executor_.begin_invoke([=]\r
-               {\r
-                       format_desc_ = format_desc;\r
-               }, high_priority);\r
+               }, task_priority::high_priority);\r
        }\r
 \r
        boost::unique_future<boost::property_tree::wptree> info()\r
        {\r
-               return std::move(executor_.begin_invoke([&]() -> boost::property_tree::wptree\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
+               }, task_priority::high_priority);\r
        }\r
 \r
        boost::unique_future<boost::property_tree::wptree> info(int index)\r
        {\r
-               return std::move(executor_.begin_invoke([&]() -> boost::property_tree::wptree\r
+               return executor_.begin_invoke([=]\r
                {\r
                        return layers_[index].info();\r
-               }, high_priority));\r
-       }\r
+               }, task_priority::high_priority);\r
+       }               \r
 };\r
 \r
-stage::stage(const safe_ptr<diagnostics::graph>& graph, const safe_ptr<target_t>& target, const video_format_desc& format_desc) : impl_(new implementation(graph, target, format_desc)){}\r
-void stage::set_frame_transform(int index, const core::frame_transform& transform, unsigned int mix_duration, const std::wstring& tween){impl_->set_transform(index, transform, mix_duration, tween);}\r
-void stage::apply_frame_transform(int index, const std::function<core::frame_transform(core::frame_transform)>& transform, unsigned int mix_duration, const std::wstring& tween){impl_->apply_transform(index, transform, mix_duration, tween);}\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::spawn_token(){impl_->spawn_token();}\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
+void stage::load(int index, const spl::shared_ptr<frame_producer>& producer, const boost::optional<int32_t>& 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_layers(const safe_ptr<stage>& other){impl_->swap_layers(other);}\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, 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<std::wstring> stage::call(int index, bool foreground, const std::wstring& param){return impl_->call(index, foreground, param);}\r
-void stage::set_video_format_desc(const video_format_desc& format_desc){impl_->set_video_format_desc(format_desc);}\r
+void stage::swap_layers(const spl::shared_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 spl::shared_ptr<stage>& other){impl_->swap_layer(index, other_index, other);}\r
+boost::unique_future<spl::shared_ptr<frame_producer>> stage::foreground(int index) {return impl_->foreground(index);}\r
+boost::unique_future<spl::shared_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, spl::shared_ptr<class draw_frame>> stage::operator()(const video_format_desc& format_desc){return (*impl_)(format_desc);}\r
 }}
\ No newline at end of file