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