5 #include <movit/effect.h>
6 #include <movit/flat_input.h>
7 #include <movit/ycbcr_input.h>
13 #include <unordered_map>
16 #include "bmusb/bmusb.h"
18 #include "ref_counted_frame.h"
19 #include "tweaked_inputs.h"
24 class LiveInputWrapper;
34 // LIVE_INPUT_* also covers CEF and video inputs.
36 LIVE_INPUT_YCBCR_WITH_DEINTERLACE,
37 LIVE_INPUT_YCBCR_PLANAR,
43 AUTO_WHITE_BALANCE_EFFECT, // Same as WHITE_BALANCE_EFFECT, but sets its value automatically.
46 INTEGRAL_PADDING_EFFECT,
51 LIFT_GAMMA_GAIN_EFFECT,
58 // An EffectBlueprint refers to an Effect before it's being added to the graph.
59 // It contains enough information to instantiate the effect, including any
60 // parameters that were set before it was added to the graph. Once it is
61 // instantiated, it forwards its calls on to the real Effect instead.
62 struct EffectBlueprint {
63 EffectBlueprint(EffectType effect_type) : effect_type(effect_type) {}
65 EffectType effect_type;
66 std::map<std::string, int> int_parameters;
67 std::map<std::string, float> float_parameters;
68 std::map<std::string, std::array<float, 3>> vec3_parameters;
69 std::map<std::string, std::array<float, 4>> vec4_parameters;
71 movit::Effect *effect = nullptr; // Gets filled out when it's instantiated.
74 // Contains basically the same data as InputState, but does not hold on to
75 // a reference to the frames. This is important so that we can release them
76 // without having to wait for Lua's GC.
77 struct InputStateInfo {
78 explicit InputStateInfo(const InputState& input_state);
80 unsigned last_width[MAX_VIDEO_CARDS], last_height[MAX_VIDEO_CARDS];
81 bool last_interlaced[MAX_VIDEO_CARDS], last_has_signal[MAX_VIDEO_CARDS], last_is_connected[MAX_VIDEO_CARDS];
82 unsigned last_frame_rate_nom[MAX_VIDEO_CARDS], last_frame_rate_den[MAX_VIDEO_CARDS];
83 bmusb::PixelFormat last_pixel_format[MAX_VIDEO_CARDS];
84 bool has_last_subtitle[MAX_VIDEO_CARDS];
85 std::string last_subtitle[MAX_VIDEO_CARDS];
90 Theme(const std::string &filename, const std::vector<std::string> &search_dirs, movit::ResourcePool *resource_pool);
94 movit::EffectChain *chain;
95 std::function<void()> setup_chain;
97 // FRAME_HISTORY frames for each input, in order. Will contain duplicates
98 // for non-interlaced inputs.
99 std::vector<RefCountedFrame> input_frames;
102 Chain get_chain(unsigned num, float t, unsigned width, unsigned height, const InputState &input_state);
104 int get_num_channels() const { return num_channels; }
105 int map_signal_to_card(int signal_num);
106 void set_signal_mapping(int signal_num, int card_idx);
107 std::string get_channel_name(unsigned channel);
108 int map_channel_to_signal(unsigned channel);
109 bool get_supports_set_wb(unsigned channel);
110 void set_wb(unsigned channel, float r, float g, float b);
111 void set_wb_for_card(int card_idx, float r, float g, float b);
112 movit::RGBTriplet get_white_balance_for_card(int card_idx);
113 std::string get_channel_color(unsigned channel);
115 std::unordered_map<int, movit::RGBTriplet> white_balance_for_card;
117 std::vector<std::string> get_transition_names(float t);
119 void transition_clicked(int transition_num, float t);
120 void channel_clicked(int preview_num);
122 movit::ResourcePool *get_resource_pool() const { return resource_pool; }
124 // Should be called as part of VideoInput.new() only.
125 void register_video_input(FFmpegCapture *capture)
127 video_inputs.push_back(capture);
130 std::vector<FFmpegCapture *> get_video_inputs() const
136 // Should be called as part of HTMLInput.new() only.
137 void register_html_input(CEFCapture *capture)
139 html_inputs.push_back(capture);
142 std::vector<CEFCapture *> get_html_inputs() const
148 void register_video_signal_connection(movit::EffectChain *chain, LiveInputWrapper *live_input, FFmpegCapture *capture)
150 video_signal_connections[chain].emplace_back(VideoSignalConnection { live_input, capture });
154 void register_html_signal_connection(movit::EffectChain *chain, LiveInputWrapper *live_input, CEFCapture *capture)
156 html_signal_connections[chain].emplace_back(CEFSignalConnection { live_input, capture });
161 MenuEntry(const std::string &text, lua_State *L, int lua_ref, unsigned flags)
162 : text(text), is_submenu(false), entry{L, lua_ref, flags} {}
163 MenuEntry(const std::string &text, std::vector<std::unique_ptr<MenuEntry>> submenu)
164 : text(text), is_submenu(true), submenu(std::move(submenu)) {}
167 static constexpr unsigned CHECKABLE = 1;
168 static constexpr unsigned CHECKED = 2;
174 // is_submenu = false.
181 // is_submenu = true.
182 std::vector<std::unique_ptr<MenuEntry>> submenu;
185 MenuEntry *get_theme_menu() { return theme_menu.get(); } // Can be empty for no menu.
186 void theme_menu_entry_clicked(int lua_ref);
188 // Will be invoked every time the theme sets a new menu.
189 // Is not invoked for a menu that exists at the time of the callback.
190 void set_theme_menu_callback(std::function<void()> callback)
192 theme_menu_callback = callback;
195 std::string format_status_line(const std::string &disk_space_left_text, double file_length_seconds);
197 // Signal that the given card is going away and will not be replaced
198 // with a fake capture card, so remove all connections to it so that
199 // they don't automatically come back on the next frame.
200 void remove_card(unsigned card_index);
203 void register_globals();
204 void register_class(const char *class_name, const luaL_Reg *funcs, EffectType effect_type = NO_EFFECT_TYPE);
205 int set_theme_menu(lua_State *L);
206 Chain get_chain_from_effect_chain(movit::EffectChain *effect_chain, unsigned num, const InputState &input_state);
207 void call_lua_wb_callback(unsigned channel, float r, float g, float b);
209 std::string theme_path;
212 lua_State *L; // Protected by <m>.
213 const InputState *input_state = nullptr; // Protected by <m>. Only set temporarily, during chain setup.
214 movit::ResourcePool *resource_pool;
215 int num_channels = -1;
216 bool startup_finished = false;
219 std::map<int, int> signal_to_card_mapping; // Protected by <map_m>.
221 std::vector<FFmpegCapture *> video_inputs;
222 struct VideoSignalConnection {
223 LiveInputWrapper *wrapper;
224 FFmpegCapture *source;
226 std::unordered_map<movit::EffectChain *, std::vector<VideoSignalConnection>>
227 video_signal_connections;
229 std::vector<CEFCapture *> html_inputs;
230 struct CEFSignalConnection {
231 LiveInputWrapper *wrapper;
234 std::unordered_map<movit::EffectChain *, std::vector<CEFSignalConnection>>
235 html_signal_connections;
238 std::unique_ptr<MenuEntry> theme_menu;
239 std::function<void()> theme_menu_callback;
241 std::map<unsigned, std::string> channel_names; // Set using Nageru.set_channel_name(). Protected by <m>.
242 std::map<unsigned, int> channel_signals; // Set using Nageru.set_channel_signal(). Protected by <m>.
243 std::map<unsigned, bool> channel_supports_wb; // Set using Nageru.set_supports_wb(). Protected by <m>.
245 friend class LiveInputWrapper;
247 friend int ThemeMenu_set(lua_State *L);
248 friend int Nageru_set_channel_name(lua_State *L);
249 friend int Nageru_set_num_channels(lua_State *L);
250 friend int Nageru_set_channel_signal(lua_State *L);
251 friend int Nageru_set_supports_wb(lua_State *L);
254 // LiveInputWrapper is a facade on top of an YCbCrInput, exposed to
255 // the Lua code. It contains a function (connect_signal()) intended
256 // to be called during chain setup, that picks out the current frame
257 // (in the form of a set of textures) from the input state given by
258 // the mixer, and communicates that state over to the actual YCbCrInput.
259 class LiveInputWrapper {
261 // Note: <override_bounce> is irrelevant for PixelFormat_8BitBGRA.
262 LiveInputWrapper(Theme *theme, movit::EffectChain *chain, bmusb::PixelFormat pixel_format, bool override_bounce, bool deinterlace, bool user_connectable);
264 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.
265 void connect_card(int signal_num, const InputState &input_state);
266 movit::Effect *get_effect() const
269 return deinterlace_effect;
270 } else if (pixel_format == bmusb::PixelFormat_8BitBGRA) {
271 return rgba_inputs[0];
273 return ycbcr_inputs[0];
278 Theme *theme; // Not owned by us.
279 bmusb::PixelFormat pixel_format;
280 movit::YCbCrFormat input_ycbcr_format;
281 std::vector<movit::YCbCrInput *> ycbcr_inputs; // Multiple ones if deinterlacing. Owned by the chain.
282 std::vector<movit::FlatInput *> rgba_inputs; // Multiple ones if deinterlacing. Owned by the chain.
283 movit::Effect *deinterlace_effect = nullptr; // Owned by the chain.
285 bool user_connectable;
288 // Utility functions used by Scene.
289 void add_outputs_and_finalize(movit::EffectChain *chain, bool is_main_chain);
290 Theme *get_theme_updata(lua_State* L);
291 bool checkbool(lua_State* L, int idx);
292 std::string checkstdstring(lua_State *L, int index);
293 movit::Effect *instantiate_effect(movit::EffectChain *chain, EffectType effect_type);
294 void print_warning(lua_State* L, const char *format, ...);
296 #endif // !defined(_THEME_H)