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