]> git.sesse.net Git - casparcg/blob - core/producer/scene/xml_scene_producer.cpp
* Merged chroma key feature, but removed unsupported parameters and color names....
[casparcg] / core / producer / scene / xml_scene_producer.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author: Helge Norberg, helge.norberg@svt.se
20 */
21
22 #include "../../StdAfx.h"
23
24 #include "xml_scene_producer.h"
25 #include "expression_parser.h"
26
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>
31
32 #include <common/env.h>
33 #include <core/producer/frame_producer.h>
34
35 #include <future>
36
37 #include "scene_producer.h"
38
39 namespace caspar { namespace core { namespace scene {
40
41 void deduce_expression(variable& var, const variable_repository& repo)
42 {
43         auto expr_str = var.original_expr();
44         auto trimmed = boost::trim_copy(expr_str);
45
46         if (boost::starts_with(trimmed, L"${") && boost::ends_with(trimmed, L"}"))
47         {
48                 expr_str = trimmed.substr(2, expr_str.length() - 3);
49
50                 if (var.is<double>())
51                         var.as<double>().bind(parse_expression<double>(expr_str, repo));
52                 else if (var.is<bool>())
53                         var.as<bool>().bind(parse_expression<bool>(expr_str, repo));
54                 else if (var.is<std::wstring>())
55                         var.as<std::wstring>().bind(parse_expression<std::wstring>(expr_str, repo));
56         }
57         else if (!expr_str.empty())
58         {
59                 var.from_string(expr_str);
60         }
61 }
62
63 spl::shared_ptr<core::frame_producer> create_xml_scene_producer(
64                 const spl::shared_ptr<core::frame_factory>& frame_factory,
65                 const core::video_format_desc& format_desc,
66                 const std::vector<std::wstring>& params)
67 {
68         if (params.empty())
69                 return core::frame_producer::empty();
70
71         std::wstring filename = env::media_folder() + L"/" + params[0] + L".xml";
72         
73         if (!boost::filesystem::is_regular_file(boost::filesystem::path(filename)))
74                 return core::frame_producer::empty();
75
76         boost::property_tree::wptree root;
77         boost::filesystem::wifstream file(filename);
78         boost::property_tree::read_xml(
79                         file,
80                         root,
81                         boost::property_tree::xml_parser::trim_whitespace
82                                         | boost::property_tree::xml_parser::no_comments);
83
84         int width = root.get<int>(L"scene.<xmlattr>.width");
85         int height = root.get<int>(L"scene.<xmlattr>.height");
86
87         auto scene = spl::make_shared<scene_producer>(width, height);
88
89         for (auto elem : root.get_child(L"scene.variables"))
90         {
91                 auto type = elem.second.get<std::wstring>(L"<xmlattr>.type");
92                 auto id = elem.second.get<std::wstring>(L"<xmlattr>.id");
93                 auto is_public = elem.second.get(L"<xmlattr>.public", false);
94                 auto expr = elem.second.get_value<std::wstring>();
95
96                 if (!is_public)
97                         id = L"variable." + id;
98
99                 if (type == L"number")
100                         scene->create_variable<double>(id, is_public, expr);
101                 else if (type == L"string")
102                         scene->create_variable<std::wstring>(id, is_public, expr);
103                 else if (type == L"bool")
104                         scene->create_variable<bool>(id, is_public, expr);
105         }
106
107         for (auto& elem : root.get_child(L"scene.layers"))
108         {
109                 auto id = elem.second.get<std::wstring>(L"<xmlattr>.id");
110                 auto producer = create_producer(frame_factory, format_desc, elem.second.get<std::wstring>(L"producer"));
111                 auto& layer = scene->create_layer(producer, 0, 0, id);
112                 auto variable_prefix = L"layer." + id + L".";
113
114                 layer.hidden = scene->create_variable<bool>(variable_prefix + L"hidden", false, elem.second.get(L"hidden", L"false"));
115                 layer.position.x = scene->create_variable<double>(variable_prefix + L"x", false, elem.second.get<std::wstring>(L"x"));
116                 layer.position.y = scene->create_variable<double>(variable_prefix + L"y", false, elem.second.get<std::wstring>(L"y"));
117                 layer.anchor.x = scene->create_variable<double>(variable_prefix + L"anchor_x", false, elem.second.get<std::wstring>(L"anchor_x", L"0.0"));
118                 layer.anchor.y = scene->create_variable<double>(variable_prefix + L"anchor_y", false, elem.second.get<std::wstring>(L"anchor_y", L"0.0"));
119                 layer.rotation = scene->create_variable<double>(variable_prefix + L"rotation", false, elem.second.get<std::wstring>(L"rotation", L"0.0"));
120                 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"));
121                 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"));
122                 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()));
123                 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()));
124                 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"));
125                 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"));
126                 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()));
127                 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"));
128                 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()));
129                 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()));
130                 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"));
131                 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()));
132
133                 layer.adjustments.opacity = scene->create_variable<double>(variable_prefix + L"adjustment.opacity", false, elem.second.get(L"adjustments.opacity", L"1.0"));
134                 layer.is_key = scene->create_variable<bool>(variable_prefix + L"is_key", false, elem.second.get(L"is_key", L"false"));
135                 layer.use_mipmap = scene->create_variable<bool>(variable_prefix + L"use_mipmap", false, elem.second.get(L"use_mipmap", L"false"));
136                 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); });
137                 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); });
138                 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"));
139                 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"));
140                 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"));
141
142                 scene->create_variable<double>(variable_prefix + L"width", false) = layer.producer.get()->pixel_constraints().width;
143                 scene->create_variable<double>(variable_prefix + L"height", false) = layer.producer.get()->pixel_constraints().height;
144
145                 for (auto& var_name : producer->get_variables())
146                 {
147                         auto& var = producer->get_variable(var_name);
148                         auto expr = elem.second.get<std::wstring>(L"parameters." + var_name, L"");
149
150                         if (var.is<double>())
151                                 scene->create_variable<double>(variable_prefix + L"parameter." + var_name, false, expr) = var.as<double>();
152                         else if (var.is<std::wstring>())
153                                 scene->create_variable<std::wstring>(variable_prefix + L"parameter." + var_name, false, expr) = var.as<std::wstring>();
154                         else if (var.is<bool>())
155                                 scene->create_variable<bool>(variable_prefix + L"parameter." + var_name, false, expr) = var.as<bool>();
156                 }
157         }
158
159         for (auto& elem : root.get_child(L"scene.timelines"))
160         {
161                 auto& variable = scene->get_variable(elem.second.get<std::wstring>(L"<xmlattr>.variable"));
162
163                 for (auto& k : elem.second)
164                 {
165                         if (k.first == L"<xmlattr>")
166                                 continue;
167
168                         auto easing = k.second.get(L"<xmlattr>.easing", L"");
169                         auto at = k.second.get<int64_t>(L"<xmlattr>.at");
170
171                         if (variable.is<double>())
172                                 scene->add_keyframe(variable.as<double>(), k.second.get_value<double>(), at, easing);
173                         else if (variable.is<int>())
174                                 scene->add_keyframe(variable.as<int>(), k.second.get_value<int>(), at, easing);
175                 }
176         }
177
178         auto repo = [&scene](const std::wstring& name) -> variable&
179         {
180                 return scene->get_variable(name); 
181         };
182
183         for (auto& var_name : scene->get_variables())
184         {
185                 deduce_expression(scene->get_variable(var_name), repo);
186         }
187
188         auto params2 = params;
189         params2.erase(params2.begin());
190
191         scene->call(params2);
192
193         return scene;
194 }
195
196 }}}