]> git.sesse.net Git - casparcg/blob - core/producer/scene/scene_producer.h
[scene] Added missing mixer features
[casparcg] / core / producer / scene / scene_producer.h
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 #pragma once
23
24 #include <common/log.h>
25
26 #include "../frame_producer.h"
27 #include "../../fwd.h"
28
29 #include "../binding.h"
30 #include "../variable.h"
31
32 namespace caspar { namespace core { namespace scene {
33
34 struct coord
35 {
36         binding<double> x;
37         binding<double> y;
38 };
39
40 struct rect
41 {
42         coord upper_left;
43         coord lower_right;
44 };
45
46 struct corners
47 {
48         coord upper_left;
49         coord upper_right;
50         coord lower_right;
51         coord lower_left;
52 };
53
54 struct adjustments
55 {
56         binding<double> opacity;
57         binding<double> contrast;
58         binding<double> saturation;
59         binding<double> brightness;
60
61         adjustments();
62 };
63
64 struct levels
65 {
66         binding<double> min_input;
67         binding<double> max_input;
68         binding<double> gamma;
69         binding<double> min_output;
70         binding<double> max_output;
71
72         levels();
73 };
74
75 struct chroma_key
76 {
77         binding<bool>   enable;
78         binding<double> target_hue;
79         binding<double> hue_width;
80         binding<double> min_saturation;
81         binding<double> min_brightness;
82         binding<double> softness;
83         binding<double> spill_suppress;
84         binding<double> spill_suppress_saturation;
85 };
86
87 struct layer
88 {
89         binding<std::wstring>                                           name;
90         scene::coord                                                            anchor;
91         scene::coord                                                            position;
92         scene::rect                                                                     crop;
93         scene::corners                                                          perspective;
94         scene::rect                                                                     clip;
95         binding<double>                                                         rotation;
96         scene::adjustments                                                      adjustments;
97         scene::levels                                                           levels;
98         binding<spl::shared_ptr<frame_producer>>        producer;
99         binding<bool>                                                           hidden;
100         binding<bool>                                                           is_key;
101         binding<bool>                                                           use_mipmap;
102         binding<core::blend_mode>                                       blend_mode;
103         scene::chroma_key                                                       chroma_key;
104         binding<double>                                                         volume;
105
106         explicit layer(const std::wstring& name, const spl::shared_ptr<frame_producer>& producer);
107 };
108
109 struct keyframe
110 {
111         std::function<void ()>                                                                                          on_start_animate;
112         std::function<void (int64_t start_frame, int64_t current_frame)>        on_animate_to;
113         std::function<void ()>                                                                                          on_destination_frame;
114         int64_t                                                                                                                         destination_frame;
115 public:
116         keyframe(int64_t destination_frame)
117                 : destination_frame(destination_frame)
118         {
119         }
120 };
121
122 enum class mark_action
123 {
124         start,
125         stop,
126         jump_to,
127         remove
128 };
129
130 mark_action get_mark_action(const std::wstring& name);
131
132 class scene_producer : public frame_producer_base
133 {
134 public:
135         scene_producer(std::wstring producer_name, std::wstring template_name, int width, int height, const video_format_desc& format_desc);
136         ~scene_producer();
137
138         draw_frame receive_impl() override;
139         constraints& pixel_constraints() override;
140         void on_interaction(const interaction_event::ptr& event) override;
141         bool collides(double x, double y) const override;
142         std::wstring print() const override;
143         std::wstring name() const override;
144         std::future<std::wstring> call(const std::vector<std::wstring>& params) override;
145         boost::property_tree::wptree info() const override;
146         monitor::subject& monitor_output();
147
148         layer& create_layer(
149                         const spl::shared_ptr<frame_producer>& producer, int x, int y, const std::wstring& name);
150         layer& create_layer(
151                         const spl::shared_ptr<frame_producer>& producer, const std::wstring& name);
152         void reverse_layers();
153         layer& get_layer(const std::wstring& name);
154
155         binding<int64_t> timeline_frame();
156         binding<double> speed();
157
158         template<typename T> binding<T>& create_variable(
159                         const std::wstring& name, bool is_public, const std::wstring& expr = L"")
160         {
161                 std::shared_ptr<core::variable> var =
162                                 std::make_shared<core::variable_impl<T>>(expr, is_public);
163
164                 store_variable(name, var);
165
166                 return var->as<T>();
167         }
168
169         template<typename T>
170         void add_keyframe(
171                         binding<T>& to_affect,
172                         T destination_value,
173                         int64_t at_frame,
174                         const std::wstring& easing)
175         {
176                 add_keyframe(to_affect, binding<T>(destination_value), at_frame, easing);
177         }
178
179         template<typename T>
180         void add_keyframe(
181                         binding<T>& to_affect,
182                         const binding<T>& destination_value,
183                         int64_t at_frame,
184                         const std::wstring& easing)
185         {
186                 if (easing.empty())
187                 {
188                         add_keyframe(to_affect, destination_value, at_frame);
189                         return;
190                 }
191
192                 tweener tween(easing);
193                 keyframe k(at_frame);
194
195                 std::shared_ptr<T> start_value(new T);
196
197                 k.on_start_animate = [=]() mutable
198                 {
199                         *start_value = to_affect.get();
200                         to_affect.unbind();
201                 };
202
203                 k.on_destination_frame = [=]() mutable
204                 {
205                         to_affect.bind(destination_value);
206                 };
207
208                 k.on_animate_to =
209                                 [=](int64_t start_frame, int64_t current_frame) mutable
210                                 {
211                                         auto relative_frame = current_frame - start_frame;
212                                         auto duration = at_frame - start_frame;
213                                         auto tweened = static_cast<T>(tween(
214                                                         static_cast<double>(relative_frame),
215                                                         *start_value,
216                                                         destination_value.get() - *start_value,
217                                                         static_cast<double>(duration)));
218
219                                         to_affect.set(tweened);
220
221                                         //CASPAR_LOG(info) << relative_frame << L" " << *start_value << L" " << duration << L" " << tweened;
222                                 };
223
224                 store_keyframe(to_affect.identity(), k);
225         }
226
227         template<typename T>
228         void add_keyframe(binding<T>& to_affect, T set_value, int64_t at_frame)
229         {
230                 add_keyframe(to_affect, binding<T>(set_value), at_frame);
231         }
232
233         template<typename T>
234         void add_keyframe(binding<T>& to_affect, const binding<T>& set_value, int64_t at_frame)
235         {
236                 keyframe k(at_frame);
237
238                 k.on_destination_frame = [=]() mutable
239                 {
240                         to_affect.bind(set_value);
241                 };
242
243                 store_keyframe(to_affect.identity(), k);
244         }
245
246         void add_mark(int64_t at_frame, mark_action action, const std::wstring& label);
247         void add_task(binding<bool> when, std::function<void ()> task);
248
249         core::variable& get_variable(const std::wstring& name) override;
250         const std::vector<std::wstring>& get_variables() const override;
251 private:
252         void store_keyframe(void* timeline_identity, const keyframe& k);
253         void store_variable(
254                         const std::wstring& name, const std::shared_ptr<core::variable>& var);
255
256         struct impl;
257         std::unique_ptr<impl> impl_;
258 };
259
260 }}}