]> git.sesse.net Git - casparcg/blobdiff - modules/psd/psd_scene_producer.cpp
* Merged layer_producer and channel_producer from 2.0 to the reroute module (replacin...
[casparcg] / modules / psd / psd_scene_producer.cpp
index c8411fd8e21cdaad8d7b31b2f1a7198c8764801a..64079f002681312eaab794403c9b47c8395bd128 100644 (file)
 */
 
 #include "psd_scene_producer.h"
+#include "psd_document.h"
 #include "layer.h"
-#include "doc.h"
 
 #include <core/frame/pixel_format.h>
 #include <core/frame/frame_factory.h>
 #include <core/producer/frame_producer.h>
 #include <core/producer/color/color_producer.h>
 #include <core/producer/text/text_producer.h>
+#include <core/producer/scene/scene_cg_proxy.h>
 #include <core/producer/scene/scene_producer.h>
 #include <core/producer/scene/const_producer.h>
 #include <core/producer/scene/hotswap_producer.h>
+#include <core/producer/media_info/media_info.h>
 #include <core/frame/draw_frame.h>
 
 #include <common/env.h>
 #include <common/memory.h>
 #include <common/log.h>
+#include <common/os/filesystem.h>
 
 #include <boost/filesystem.hpp>
-#include <boost/foreach.hpp>
 #include <boost/thread/future.hpp>
 #include <boost/algorithm/string.hpp>
 #include <boost/rational.hpp>
 
-namespace caspar { namespace psd {
+#include <future>
 
-void init()
-{
-       core::register_producer_factory(create_psd_scene_producer);
-}
+namespace caspar { namespace psd {
 
 core::text::text_info get_text_info(const boost::property_tree::wptree& ptree)
 {
@@ -91,6 +90,7 @@ core::text::text_info get_text_info(const boost::property_tree::wptree& ptree)
        return result;
 }
 
+
        class layer_link_constructor
        {
                struct linked_layer_record
@@ -132,7 +132,7 @@ core::text::text_info get_text_info(const boost::property_tree::wptree& ptree)
                        {
                                auto& master = (*it);
                                //std::for_each(layers.begin(), layers.end(), [&master](linked_layer_record &r) mutable { 
-                               BOOST_FOREACH(auto &r, layers) {
+                               for (auto &r : layers) {
                                        if(r.link_id == master.link_id && r.layer != master.layer)
                                        {
                                                {       //x-coords
@@ -203,6 +203,60 @@ boost::rational<int> get_rational(const boost::property_tree::wptree& node)
                        node.get<int>(L"numerator"), node.get<int>(L"denominator"));
 }
 
+void create_marks_from_comments(
+               const spl::shared_ptr<core::scene::scene_producer>& scene,
+               const core::video_format_desc& format_desc,
+               const boost::property_tree::wptree& comment_track)
+{
+       auto keylist = comment_track.get_child_optional(L"keyList");
+
+       if (!keylist)
+               return;
+
+       for (auto& key : *keylist)
+       {
+               auto time = get_rational(key.second.get_child(L"time"));
+               auto frame = get_frame_number(format_desc, time);
+               auto text = key.second.get<std::wstring>(L"animKey.Vl  ");
+               std::vector<std::wstring> marks;
+               boost::split(marks, text, boost::is_any_of(","));
+
+               for (auto& mark : marks)
+               {
+                       boost::trim_if(mark, [](wchar_t c) { return isspace(c) || c == 0; });
+
+                       std::vector<std::wstring> args;
+                       boost::split(args, mark, boost::is_any_of(" \t"), boost::algorithm::token_compress_on);
+
+                       scene->add_mark(frame, core::scene::get_mark_action(args.at(0)), args.size() == 1 ? L"" : args.at(1));
+               }
+       }
+}
+
+void create_marks(
+               const spl::shared_ptr<core::scene::scene_producer>& scene,
+               const core::video_format_desc& format_desc,
+               const boost::property_tree::wptree& global_timeline)
+{
+       auto time = get_rational(global_timeline.get_child(L"duration"));
+       auto remove_at_frame = get_frame_number(format_desc, time);
+
+       scene->add_mark(remove_at_frame, core::scene::mark_action::remove, L"");
+
+       auto tracklist = global_timeline.get_child_optional(L"globalTrackList");
+
+       if (!tracklist)
+               return;
+
+       for (auto& track : *tracklist)
+       {
+               auto track_id = track.second.get<std::wstring>(L"stdTrackID");
+
+               if (track_id == L"globalCommentTrack")
+                       create_marks_from_comments(scene, format_desc, track.second);
+       }
+}
+
 void create_timelines(
                const spl::shared_ptr<core::scene::scene_producer>& scene,
                const core::video_format_desc& format_desc,
@@ -228,13 +282,13 @@ void create_timelines(
        double original_pos_x = psd_layer->location().x;
        double original_pos_y = psd_layer->location().y;
 
-       BOOST_FOREACH(auto& track, *tracklist)
+       for (auto& track : *tracklist)
        {
                auto track_id = track.second.get<std::wstring>(L"stdTrackID");
 
                if (track_id == L"sheetPositionTrack")
                {
-                       BOOST_FOREACH(auto& key, track.second.get_child(L"keyList"))
+                       for (auto& key : track.second.get_child(L"keyList"))
                        {
                                bool tween = key.second.get<std::wstring>(L"animInterpStyle")
                                                == L"Lnr ";
@@ -269,7 +323,7 @@ void create_timelines(
                {
                        auto& opacity = layer.adjustments.opacity;
 
-                       BOOST_FOREACH(auto& key, track.second.get_child(L"keyList"))
+                       for (auto& key : track.second.get_child(L"keyList"))
                        {
                                bool tween = key.second.get<std::wstring>(L"animInterpStyle")
                                                == L"Lnr ";
@@ -291,17 +345,18 @@ void create_timelines(
        }
 }
 
-spl::shared_ptr<core::frame_producer> create_psd_scene_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const core::video_format_desc& format_desc, const std::vector<std::wstring>& params)
+spl::shared_ptr<core::frame_producer> create_psd_scene_producer(const core::frame_producer_dependencies& dependencies, const std::vector<std::wstring>& params)
 {
-       std::wstring filename = env::media_folder() + L"\\" + params[0] + L".psd";
-       if(!boost::filesystem::is_regular_file(boost::filesystem::path(filename)))
+       std::wstring filename = env::template_folder() + params.at(0) + L".psd";
+       auto found_file = find_case_insensitive(filename);
+
+       if (!found_file)
                return core::frame_producer::empty();
 
        psd_document doc;
-       if(!doc.parse(filename))
-               return core::frame_producer::empty();
+       doc.parse(*found_file);
 
-       spl::shared_ptr<core::scene::scene_producer> root(spl::make_shared<core::scene::scene_producer>(doc.width(), doc.height()));
+       spl::shared_ptr<core::scene::scene_producer> root(spl::make_shared<core::scene::scene_producer>(doc.width(), doc.height(), dependencies.format_desc));
 
        layer_link_constructor link_constructor;
 
@@ -319,7 +374,7 @@ spl::shared_ptr<core::frame_producer> create_psd_scene_producer(const spl::share
                                core::text::text_info text_info(std::move(get_text_info((*it)->text_data())));
                                text_info.size *= (*it)->text_scale();
 
-                               auto text_producer = core::text_producer::create(frame_factory, 0, 0, str, text_info, doc.width(), doc.height());
+                               auto text_producer = core::text_producer::create(dependencies.frame_factory, 0, 0, str, text_info, doc.width(), doc.height());
                        
                                core::text::string_metrics metrics = text_producer->measure_string(str);
                        
@@ -330,7 +385,7 @@ spl::shared_ptr<core::frame_producer> create_psd_scene_producer(const spl::share
                                new_layer.hidden.set(!(*it)->is_visible());
 
                                if ((*it)->has_timeline())
-                                       create_timelines(root, format_desc, new_layer, (*it), adjustment_x, adjustment_y);
+                                       create_timelines(root, dependencies.format_desc, new_layer, (*it), adjustment_x, adjustment_y);
 
                                if((*it)->link_group_id() != 0)
                                        link_constructor.add(&new_layer, (*it)->link_group_id(), (*it)->is_position_protected(), -adjustment_x, -adjustment_y);
@@ -343,7 +398,7 @@ spl::shared_ptr<core::frame_producer> create_psd_scene_producer(const spl::share
                                std::shared_ptr<core::frame_producer> layer_producer;
                                if((*it)->is_solid())
                                {
-                                       layer_producer = core::create_const_producer(core::create_color_frame(it->get(), frame_factory, (*it)->solid_color().to_uint32()), (*it)->bitmap()->width(), (*it)->bitmap()->height());
+                                       layer_producer = core::create_const_producer(core::create_color_frame(it->get(), dependencies.frame_factory, (*it)->solid_color().to_uint32()), (*it)->bitmap()->width(), (*it)->bitmap()->height());
                                }
                                else if((*it)->bitmap())
                                {
@@ -358,7 +413,7 @@ spl::shared_ptr<core::frame_producer> create_psd_scene_producer(const spl::share
                                                core::pixel_format_desc pfd(core::pixel_format::bgra);
                                                pfd.planes.push_back(core::pixel_format_desc::plane((*it)->bitmap()->width(), (*it)->bitmap()->height(), 4));
 
-                                               auto frame = frame_factory->create_frame(it->get(), pfd);
+                                               auto frame = dependencies.frame_factory->create_frame(it->get(), pfd);
                                                memcpy(frame.image_data().data(), (*it)->bitmap()->data(), frame.image_data().size());
 
                                                layer_producer = core::create_const_producer(core::draw_frame(std::move(frame)), (*it)->bitmap()->width(), (*it)->bitmap()->height());
@@ -372,7 +427,7 @@ spl::shared_ptr<core::frame_producer> create_psd_scene_producer(const spl::share
                                        new_layer.hidden.set(!(*it)->is_visible());
 
                                        if ((*it)->has_timeline())
-                                               create_timelines(root, format_desc, new_layer, (*it), 0, 0);
+                                               create_timelines(root, dependencies.format_desc, new_layer, (*it), 0, 0);
 
                                        if((*it)->link_group_id() != 0)
                                                link_constructor.add(&new_layer, (*it)->link_group_id(), (*it)->is_position_protected(), 0, 0);
@@ -384,15 +439,50 @@ spl::shared_ptr<core::frame_producer> create_psd_scene_producer(const spl::share
        link_constructor.calculate();
 
        // Reset all dynamic text fields to empty strings and expose them as a scene parameter.
-       BOOST_FOREACH(auto& text_layer, text_producers_by_layer_name)
+       for (auto& text_layer : text_producers_by_layer_name)
                text_layer.second->text().bind(root->create_variable<std::wstring>(boost::to_lower_copy(text_layer.first), true, L""));
 
+       if (doc.has_timeline())
+               create_marks(root, dependencies.format_desc, doc.timeline());
+
        auto params2 = params;
-       params2.erase(params2.cbegin());
+       params2.erase(params2.begin());
 
        root->call(params2);
 
        return root;
 }
 
-}}
\ No newline at end of file
+void init(core::module_dependencies dependencies)
+{
+       core::register_producer_factory(create_psd_scene_producer);
+       dependencies.media_info_repo->register_extractor(
+                       [](const std::wstring& file, const std::wstring& upper_case_extension, core::media_info& info)
+                       {
+                               if (upper_case_extension == L".PSD")
+                               {
+                                       info.clip_type = L"STILL";
+
+                                       return true;
+                               }
+
+                               return false;
+                       });
+       dependencies.cg_registry->register_cg_producer(
+                       L"scene",
+                       { L".psd" },
+                       [](const std::wstring& filename) { return ""; },
+                       [](const spl::shared_ptr<core::frame_producer>& producer)
+                       {
+                               return spl::make_shared<core::scene::scene_cg_proxy>(producer);
+                       },
+                       [](
+                                       const core::frame_producer_dependencies& dependencies,
+                                       const std::wstring& filename)
+                       {
+                               return create_psd_scene_producer(dependencies, { filename });
+                       },
+                       false);
+}
+
+}}