2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Helge Norberg, helge.norberg@svt.se
22 #include "../../StdAfx.h"
24 #include "xml_scene_producer.h"
25 #include "expression_parser.h"
27 #include <boost/filesystem.hpp>
28 #include <boost/filesystem/fstream.hpp>
29 #include <boost/property_tree/ptree.hpp>
30 #include <boost/property_tree/xml_parser.hpp>
32 #include <common/env.h>
33 #include <core/producer/frame_producer.h>
37 #include "scene_producer.h"
38 #include "scene_cg_proxy.h"
40 namespace caspar { namespace core { namespace scene {
42 void deduce_expression(variable& var, const variable_repository& repo)
44 auto expr_str = var.original_expr();
45 auto trimmed = boost::trim_copy(expr_str);
47 if (boost::starts_with(trimmed, L"${") && boost::ends_with(trimmed, L"}"))
49 expr_str = trimmed.substr(2, expr_str.length() - 3);
52 var.as<double>().bind(parse_expression<double>(expr_str, repo));
53 else if (var.is<bool>())
54 var.as<bool>().bind(parse_expression<bool>(expr_str, repo));
55 else if (var.is<std::wstring>())
56 var.as<std::wstring>().bind(parse_expression<std::wstring>(expr_str, repo));
58 else if (!expr_str.empty())
60 var.from_string(expr_str);
64 void init(module_dependencies dependencies)
66 dependencies.producer_registry->register_producer_factory(create_xml_scene_producer);
67 dependencies.cg_registry->register_cg_producer(
70 [](const std::wstring& filename) { return ""; },
71 [](const spl::shared_ptr<core::frame_producer>& producer)
73 return spl::make_shared<core::scene::scene_cg_proxy>(producer);
76 const core::frame_producer_dependencies& dependencies,
77 const std::wstring& filename)
79 return create_xml_scene_producer(dependencies, { filename });
84 spl::shared_ptr<core::frame_producer> create_xml_scene_producer(
85 const core::frame_producer_dependencies& dependencies,
86 const std::vector<std::wstring>& params)
89 return core::frame_producer::empty();
91 std::wstring filename = env::template_folder() + L"/" + params.at(0) + L".scene";
93 if (!boost::filesystem::is_regular_file(boost::filesystem::path(filename)))
94 return core::frame_producer::empty();
96 boost::property_tree::wptree root;
97 boost::filesystem::wifstream file(filename);
98 boost::property_tree::read_xml(
101 boost::property_tree::xml_parser::trim_whitespace
102 | boost::property_tree::xml_parser::no_comments);
104 int width = root.get<int>(L"scene.<xmlattr>.width");
105 int height = root.get<int>(L"scene.<xmlattr>.height");
107 auto scene = spl::make_shared<scene_producer>(width, height, dependencies.format_desc);
109 for (auto elem : root.get_child(L"scene.variables"))
111 auto type = elem.second.get<std::wstring>(L"<xmlattr>.type");
112 auto id = elem.second.get<std::wstring>(L"<xmlattr>.id");
113 auto is_public = elem.second.get(L"<xmlattr>.public", false);
114 auto expr = elem.second.get_value<std::wstring>();
117 id = L"variable." + id;
119 if (type == L"number")
120 scene->create_variable<double>(id, is_public, expr);
121 else if (type == L"string")
122 scene->create_variable<std::wstring>(id, is_public, expr);
123 else if (type == L"bool")
124 scene->create_variable<bool>(id, is_public, expr);
127 for (auto& elem : root.get_child(L"scene.layers"))
129 auto id = elem.second.get<std::wstring>(L"<xmlattr>.id");
130 auto producer = dependencies.producer_registry->create_producer(dependencies, elem.second.get<std::wstring>(L"producer"));
131 auto& layer = scene->create_layer(producer, 0, 0, id);
132 auto variable_prefix = L"layer." + id + L".";
134 layer.hidden = scene->create_variable<bool>(variable_prefix + L"hidden", false, elem.second.get(L"hidden", L"false"));
135 layer.position.x = scene->create_variable<double>(variable_prefix + L"x", false, elem.second.get<std::wstring>(L"x"));
136 layer.position.y = scene->create_variable<double>(variable_prefix + L"y", false, elem.second.get<std::wstring>(L"y"));
137 layer.anchor.x = scene->create_variable<double>(variable_prefix + L"anchor_x", false, elem.second.get<std::wstring>(L"anchor_x", L"0.0"));
138 layer.anchor.y = scene->create_variable<double>(variable_prefix + L"anchor_y", false, elem.second.get<std::wstring>(L"anchor_y", L"0.0"));
139 layer.rotation = scene->create_variable<double>(variable_prefix + L"rotation", false, elem.second.get<std::wstring>(L"rotation", L"0.0"));
140 layer.crop.upper_left.x = scene->create_variable<double>(variable_prefix + L"crop_upper_left_x", false, elem.second.get<std::wstring>(L"crop_upper_left_x", L"0.0"));
141 layer.crop.upper_left.y = scene->create_variable<double>(variable_prefix + L"crop_upper_left_y", false, elem.second.get<std::wstring>(L"crop_upper_left_y", L"0.0"));
142 layer.crop.lower_right.x = scene->create_variable<double>(variable_prefix + L"crop_lower_right_x", false, elem.second.get<std::wstring>(L"crop_lower_right_x", layer.producer.get()->pixel_constraints().width.as<std::wstring>().get()));
143 layer.crop.lower_right.y = scene->create_variable<double>(variable_prefix + L"crop_lower_right_y", false, elem.second.get<std::wstring>(L"crop_lower_right_y", layer.producer.get()->pixel_constraints().height.as<std::wstring>().get()));
144 layer.perspective.upper_left.x = scene->create_variable<double>(variable_prefix + L"perspective_upper_left_x", false, elem.second.get<std::wstring>(L"perspective_upper_left_x", L"0.0"));
145 layer.perspective.upper_left.y = scene->create_variable<double>(variable_prefix + L"perspective_upper_left_y", false, elem.second.get<std::wstring>(L"perspective_upper_left_y", L"0.0"));
146 layer.perspective.upper_right.x = scene->create_variable<double>(variable_prefix + L"perspective_upper_right_x", false, elem.second.get<std::wstring>(L"perspective_upper_right_x", layer.producer.get()->pixel_constraints().width.as<std::wstring>().get()));
147 layer.perspective.upper_right.y = scene->create_variable<double>(variable_prefix + L"perspective_upper_right_y", false, elem.second.get<std::wstring>(L"perspective_upper_right_y", L"0.0"));
148 layer.perspective.lower_right.x = scene->create_variable<double>(variable_prefix + L"perspective_lower_right_x", false, elem.second.get<std::wstring>(L"perspective_lower_right_x", layer.producer.get()->pixel_constraints().width.as<std::wstring>().get()));
149 layer.perspective.lower_right.y = scene->create_variable<double>(variable_prefix + L"perspective_lower_right_y", false, elem.second.get<std::wstring>(L"perspective_lower_right_y", layer.producer.get()->pixel_constraints().height.as<std::wstring>().get()));
150 layer.perspective.lower_left.x = scene->create_variable<double>(variable_prefix + L"perspective_lower_left_x", false, elem.second.get<std::wstring>(L"perspective_lower_left_x", L"0.0"));
151 layer.perspective.lower_left.y = scene->create_variable<double>(variable_prefix + L"perspective_lower_left_y", false, elem.second.get<std::wstring>(L"perspective_lower_left_y", layer.producer.get()->pixel_constraints().height.as<std::wstring>().get()));
153 layer.adjustments.opacity = scene->create_variable<double>(variable_prefix + L"adjustment.opacity", false, elem.second.get(L"adjustments.opacity", L"1.0"));
154 layer.is_key = scene->create_variable<bool>(variable_prefix + L"is_key", false, elem.second.get(L"is_key", L"false"));
155 layer.use_mipmap = scene->create_variable<bool>(variable_prefix + L"use_mipmap", false, elem.second.get(L"use_mipmap", L"false"));
156 layer.blend_mode = scene->create_variable<std::wstring>(variable_prefix + L"blend_mode", false, elem.second.get(L"blend_mode", L"normal")).transformed([](const std::wstring& b) { return get_blend_mode(b); });
157 layer.chroma_key.key = scene->create_variable<std::wstring>(variable_prefix + L"chroma_key.key", false, elem.second.get(L"chroma_key.key", L"none")).transformed([](const std::wstring& k) { return get_chroma_mode(k); });
158 layer.chroma_key.threshold = scene->create_variable<double>(variable_prefix + L"chroma_key.threshold", false, elem.second.get(L"chroma_key.threshold", L"0.0"));
159 layer.chroma_key.softness = scene->create_variable<double>(variable_prefix + L"chroma_key.softness", false, elem.second.get(L"chroma_key.softness", L"0.0"));
160 layer.chroma_key.spill = scene->create_variable<double>(variable_prefix + L"chroma_key.spill", false, elem.second.get(L"chroma_key.spill", L"0.0"));
162 scene->create_variable<double>(variable_prefix + L"width", false) = layer.producer.get()->pixel_constraints().width;
163 scene->create_variable<double>(variable_prefix + L"height", false) = layer.producer.get()->pixel_constraints().height;
165 for (auto& var_name : producer->get_variables())
167 auto& var = producer->get_variable(var_name);
168 auto expr = elem.second.get<std::wstring>(L"parameters." + var_name, L"");
170 if (var.is<double>())
171 scene->create_variable<double>(variable_prefix + L"parameter." + var_name, false, expr) = var.as<double>();
172 else if (var.is<std::wstring>())
173 scene->create_variable<std::wstring>(variable_prefix + L"parameter." + var_name, false, expr) = var.as<std::wstring>();
174 else if (var.is<bool>())
175 scene->create_variable<bool>(variable_prefix + L"parameter." + var_name, false, expr) = var.as<bool>();
179 if (root.get_child_optional(L"scene.marks"))
181 for (auto& mark : root.get_child(L"scene.marks"))
183 auto at = mark.second.get<int64_t>(L"<xmlattr>.at");
184 auto type = get_mark_action(mark.second.get<std::wstring>(L"<xmlattr>.type"));
185 auto label = mark.second.get(L"<xmlattr>.label", L"");
187 scene->add_mark(at, type, label);
191 for (auto& elem : root.get_child(L"scene.timelines"))
193 auto& variable = scene->get_variable(elem.second.get<std::wstring>(L"<xmlattr>.variable"));
195 for (auto& k : elem.second)
197 if (k.first == L"<xmlattr>")
200 auto easing = k.second.get(L"<xmlattr>.easing", L"");
201 auto at = k.second.get<int64_t>(L"<xmlattr>.at");
203 if (variable.is<double>())
204 scene->add_keyframe(variable.as<double>(), k.second.get_value<double>(), at, easing);
205 else if (variable.is<int>())
206 scene->add_keyframe(variable.as<int>(), k.second.get_value<int>(), at, easing);
210 auto repo = [&scene](const std::wstring& name) -> variable&
212 return scene->get_variable(name);
215 for (auto& var_name : scene->get_variables())
217 deduce_expression(scene->get_variable(var_name), repo);
220 auto params2 = params;
221 params2.erase(params2.begin());
223 scene->call(params2);