]> git.sesse.net Git - nageru/blob - nageru/theme.h
37113b2b954d88a607087ca8e691df1aa082a8fd
[nageru] / nageru / theme.h
1 #ifndef _THEME_H
2 #define _THEME_H 1
3
4 #include <lua.hpp>
5 #include <movit/flat_input.h>
6 #include <movit/ycbcr_input.h>
7 #include <stdbool.h>
8 #include <functional>
9 #include <map>
10 #include <mutex>
11 #include <string>
12 #include <unordered_map>
13 #include <vector>
14
15 #include "bmusb/bmusb.h"
16 #include "defs.h"
17 #include "ref_counted_frame.h"
18 #include "tweaked_inputs.h"
19
20 class Scene;
21 class CEFCapture;
22 class FFmpegCapture;
23 class LiveInputWrapper;
24 struct InputState;
25
26 namespace movit {
27 class Effect;
28 class EffectChain;
29 class ResourcePool;
30 }  // namespace movit
31
32 enum EffectType {
33         // LIVE_INPUT_* also covers CEF and video inputs.
34         LIVE_INPUT_YCBCR,
35         LIVE_INPUT_YCBCR_WITH_DEINTERLACE,
36         LIVE_INPUT_YCBCR_PLANAR,
37         LIVE_INPUT_BGRA,
38         IMAGE_INPUT,
39
40         IDENTITY_EFFECT,
41         WHITE_BALANCE_EFFECT,
42         RESAMPLE_EFFECT,
43         PADDING_EFFECT,
44         INTEGRAL_PADDING_EFFECT,
45         OVERLAY_EFFECT,
46         RESIZE_EFFECT,
47         MULTIPLY_EFFECT,
48         MIX_EFFECT,
49         LIFT_GAMMA_GAIN_EFFECT,
50
51         NO_EFFECT_TYPE
52 };
53
54 // An EffectBlueprint refers to an Effect before it's being added to the graph.
55 // It contains enough information to instantiate the effect, including any
56 // parameters that were set before it was added to the graph. Once it is
57 // instantiated, it forwards its calls on to the real Effect instead.
58 struct EffectBlueprint {
59         EffectBlueprint(EffectType effect_type) : effect_type(effect_type) {}
60
61         EffectType effect_type;
62         std::map<std::string, int> int_parameters;
63         std::map<std::string, float> float_parameters;
64         std::map<std::string, std::array<float, 3>> vec3_parameters;
65         std::map<std::string, std::array<float, 4>> vec4_parameters;
66
67         movit::Effect *effect = nullptr;  // Gets filled out when it's instantiated.
68 };
69
70 // Contains basically the same data as InputState, but does not hold on to
71 // a reference to the frames. This is important so that we can release them
72 // without having to wait for Lua's GC.
73 struct InputStateInfo {
74         explicit InputStateInfo(const InputState& input_state);
75
76         unsigned last_width[MAX_VIDEO_CARDS], last_height[MAX_VIDEO_CARDS];
77         bool last_interlaced[MAX_VIDEO_CARDS], last_has_signal[MAX_VIDEO_CARDS], last_is_connected[MAX_VIDEO_CARDS];
78         unsigned last_frame_rate_nom[MAX_VIDEO_CARDS], last_frame_rate_den[MAX_VIDEO_CARDS];
79         bool has_last_subtitle[MAX_VIDEO_CARDS];
80         std::string last_subtitle[MAX_VIDEO_CARDS];
81 };
82
83 class Theme {
84 public:
85         Theme(const std::string &filename, const std::vector<std::string> &search_dirs, movit::ResourcePool *resource_pool, unsigned num_cards);
86         ~Theme();
87
88         struct Chain {
89                 movit::EffectChain *chain;
90                 std::function<void()> setup_chain;
91
92                 // FRAME_HISTORY frames for each input, in order. Will contain duplicates
93                 // for non-interlaced inputs.
94                 std::vector<RefCountedFrame> input_frames;
95         };
96
97         Chain get_chain(unsigned num, float t, unsigned width, unsigned height, const InputState &input_state);
98
99         int get_num_channels() const { return num_channels; }
100         int map_signal(int signal_num);
101         void set_signal_mapping(int signal_num, int card_num);
102         std::string get_channel_name(unsigned channel);
103         int get_channel_signal(unsigned channel);
104         bool get_supports_set_wb(unsigned channel);
105         void set_wb(unsigned channel, double r, double g, double b);
106         std::string get_channel_color(unsigned channel);
107
108         std::vector<std::string> get_transition_names(float t);
109
110         void transition_clicked(int transition_num, float t);
111         void channel_clicked(int preview_num);
112
113         movit::ResourcePool *get_resource_pool() const { return resource_pool; }
114
115         // Should be called as part of VideoInput.new() only.
116         void register_video_input(FFmpegCapture *capture)
117         {
118                 video_inputs.push_back(capture);
119         }
120
121         std::vector<FFmpegCapture *> get_video_inputs() const
122         {
123                 return video_inputs;
124         }
125
126 #ifdef HAVE_CEF
127         // Should be called as part of HTMLInput.new() only.
128         void register_html_input(CEFCapture *capture)
129         {
130                 html_inputs.push_back(capture);
131         }
132
133         std::vector<CEFCapture *> get_html_inputs() const
134         {
135                 return html_inputs;
136         }
137 #endif
138
139         void register_video_signal_connection(movit::EffectChain *chain, LiveInputWrapper *live_input, FFmpegCapture *capture)
140         {
141                 video_signal_connections[chain].emplace_back(VideoSignalConnection { live_input, capture });
142         }
143
144 #ifdef HAVE_CEF
145         void register_html_signal_connection(movit::EffectChain *chain, LiveInputWrapper *live_input, CEFCapture *capture)
146         {
147                 html_signal_connections[chain].emplace_back(CEFSignalConnection { live_input, capture });
148         }
149 #endif
150
151         struct MenuEntry {
152                 MenuEntry(const std::string &text, lua_State *L, int lua_ref, unsigned flags)
153                         : text(text), is_submenu(false), entry{L, lua_ref, flags} {}
154                 MenuEntry(const std::string &text, std::vector<std::unique_ptr<MenuEntry>> submenu)
155                         : text(text), is_submenu(true), submenu(std::move(submenu)) {}
156                 ~MenuEntry();
157
158                 static constexpr unsigned CHECKABLE = 1;
159                 static constexpr unsigned CHECKED = 2;
160
161                 std::string text;
162                 bool is_submenu;
163
164                 union {
165                         // is_submenu = false.
166                         struct {
167                                 lua_State *L;
168                                 int lua_ref;
169                                 unsigned flags;
170                         } entry;
171
172                         // is_submenu = true.
173                         std::vector<std::unique_ptr<MenuEntry>> submenu;
174                 };
175         };
176         MenuEntry *get_theme_menu() { return theme_menu.get(); }  // Can be empty for no menu.
177         void theme_menu_entry_clicked(int lua_ref);
178
179         // Will be invoked every time the theme sets a new menu.
180         // Is not invoked for a menu that exists at the time of the callback.
181         void set_theme_menu_callback(std::function<void()> callback)
182         {
183                 theme_menu_callback = callback;
184         }
185
186         std::string format_status_line(const std::string &disk_space_left_text, double file_length_seconds);
187
188 private:
189         void register_globals();
190         void register_class(const char *class_name, const luaL_Reg *funcs, EffectType effect_type = NO_EFFECT_TYPE);
191         int set_theme_menu(lua_State *L);
192         Chain get_chain_from_effect_chain(movit::EffectChain *effect_chain, unsigned num, const InputState &input_state);
193
194         std::string theme_path;
195
196         std::mutex m;
197         lua_State *L;  // Protected by <m>.
198         const InputState *input_state = nullptr;  // Protected by <m>. Only set temporarily, during chain setup.
199         movit::ResourcePool *resource_pool;
200         int num_channels = -1;
201         unsigned num_cards;
202         bool startup_finished = false;
203
204         std::mutex map_m;
205         std::map<int, int> signal_to_card_mapping;  // Protected by <map_m>.
206
207         std::vector<FFmpegCapture *> video_inputs;
208         struct VideoSignalConnection {
209                 LiveInputWrapper *wrapper;
210                 FFmpegCapture *source;
211         };
212         std::unordered_map<movit::EffectChain *, std::vector<VideoSignalConnection>>
213                  video_signal_connections;
214 #ifdef HAVE_CEF
215         std::vector<CEFCapture *> html_inputs;
216         struct CEFSignalConnection {
217                 LiveInputWrapper *wrapper;
218                 CEFCapture *source;
219         };
220         std::unordered_map<movit::EffectChain *, std::vector<CEFSignalConnection>>
221                 html_signal_connections;
222 #endif
223
224         std::unique_ptr<MenuEntry> theme_menu;
225         std::function<void()> theme_menu_callback;
226
227         std::map<unsigned, std::string> channel_names;  // Set using Nageru.set_channel_name(). Protected by <m>.
228         std::map<unsigned, int> channel_signals;  // Set using Nageru.set_channel_signal(). Protected by <m>.
229         std::map<unsigned, bool> channel_supports_wb;  // Set using Nageru.set_supports_wb(). Protected by <m>.
230
231         friend class LiveInputWrapper;
232         friend class Scene;
233         friend int ThemeMenu_set(lua_State *L);
234         friend int Nageru_set_channel_name(lua_State *L);
235         friend int Nageru_set_num_channels(lua_State *L);
236         friend int Nageru_set_channel_signal(lua_State *L);
237         friend int Nageru_set_supports_wb(lua_State *L);
238 };
239
240 // LiveInputWrapper is a facade on top of an YCbCrInput, exposed to
241 // the Lua code. It contains a function (connect_signal()) intended
242 // to be called during chain setup, that picks out the current frame
243 // (in the form of a set of textures) from the input state given by
244 // the mixer, and communicates that state over to the actual YCbCrInput.
245 class LiveInputWrapper {
246 public:
247         // Note: <override_bounce> is irrelevant for PixelFormat_8BitBGRA.
248         LiveInputWrapper(Theme *theme, movit::EffectChain *chain, bmusb::PixelFormat pixel_format, bool override_bounce, bool deinterlace, bool user_connectable);
249
250         bool connect_signal(int signal_num);  // Must be called with the theme's <m> lock held, since it accesses theme->input_state. Returns false on error.
251         void connect_signal_raw(int signal_num, const InputState &input_state);
252         movit::Effect *get_effect() const
253         {
254                 if (deinterlace) {
255                         return deinterlace_effect;
256                 } else if (pixel_format == bmusb::PixelFormat_8BitBGRA) {
257                         return rgba_inputs[0];
258                 } else {
259                         return ycbcr_inputs[0];
260                 }
261         }
262
263 private:
264         Theme *theme;  // Not owned by us.
265         bmusb::PixelFormat pixel_format;
266         movit::YCbCrFormat input_ycbcr_format;
267         std::vector<movit::YCbCrInput *> ycbcr_inputs;  // Multiple ones if deinterlacing. Owned by the chain.
268         std::vector<movit::FlatInput *> rgba_inputs;  // Multiple ones if deinterlacing. Owned by the chain.
269         movit::Effect *deinterlace_effect = nullptr;  // Owned by the chain.
270         bool deinterlace;
271         bool user_connectable;
272 };
273
274 // Utility functions used by Scene.
275 void add_outputs_and_finalize(movit::EffectChain *chain, bool is_main_chain);
276 Theme *get_theme_updata(lua_State* L);
277 bool checkbool(lua_State* L, int idx);
278 std::string checkstdstring(lua_State *L, int index);
279 movit::Effect *instantiate_effect(movit::EffectChain *chain, EffectType effect_type);
280 void print_warning(lua_State* L, const char *format, ...);
281
282 #endif  // !defined(_THEME_H)