]> git.sesse.net Git - nageru/blob - nageru/scene.h
Make it possible for auto white balance to be controlled by another input.
[nageru] / nageru / scene.h
1 #ifndef _SCENE_H
2 #define _SCENE_H 1
3
4 // A Scene is an equivalent of an EffectChain, but each part is not a single
5 // Effect. (The name itself does not carry any specific meaning above that
6 // of what an EffectChain is; it was just chosen as a more intuitive name than
7 // an EffectChain when we had to change anyway.) Instead, it is a “block”,
8 // which can hold one or more effect alternatives, e.g., one block could hold
9 // ResizeEffect or IdentityEffect (effectively doing nothing), or many
10 // different input types. On finalization, every different combination of
11 // block alternatives are tried, and one EffectChain is generated for each.
12 // This also goes for whether the scene is destined for preview outputs
13 // (directly to screen, RGBA) or live (Y'CbCr output).
14
15 #include <stddef.h>
16 #include <bitset>
17 #include <functional>
18 #include <map>
19 #include <memory>
20 #include <string>
21 #include <vector>
22
23 class CEFCapture;
24 struct EffectBlueprint;
25 class FFmpegCapture;
26 class ImageInput;
27 struct InputState;
28 class LiveInputWrapper;
29 class Theme;
30 struct lua_State;
31
32 namespace movit {
33 class Effect;
34 class EffectChain;
35 class ResourcePool;
36 }  // namespace movit
37
38 struct Block {
39         // Index into the parent Scene's list of blocks.
40         using Index = size_t;
41         Index idx = 0;
42
43         // Each instantiation is indexed by the chosen alternative for each block.
44         // These are combined into one big variable-base number, ranging from 0
45         // to (B_0*B_1*B_2*...*B_n)-1, where B_i is the number of alternatives for
46         // block number i and n is the index of the last block.
47         //
48         // The actual index, given alternatives A_0, A_1, A_2, ..., is given as
49         //
50         //   A_0 + B_0 * (A_1 + B_1 * (A_2 + B_2 * (...)))
51         //
52         // where each A_i can of course range from 0 to B_i-1. In other words,
53         // the first block gets the lowest “bits” (or trits, or quats...) of the
54         // index number, the second block gets the ones immediately above,
55         // and so on. Thus, there are no holes in the sequence.
56         //
57         // Expanding the formula above gives the equivalent index
58         //
59         //   A_0 + A_1 * B_0 + A_2 * B_0 * B_1 + A_3 * ...
60         //
61         // or
62         //
63         //   A_0 * C_0 + A_1 * C_1 + A_2 * C_2 + A_3 * ...
64         //
65         // where C_0 = 0 and C_(i+1) = C_i * B_i. In other words, C_i is
66         // the product of the cardinalities of each previous effect; if we
67         // are e.g. at the third index and there have been C_2 = 3 * 5 = 15
68         // different alternatives for constructing the scene so far
69         // (with possible indexes 0..14), it is only logical that if we
70         // want three new options (B_2 = 3), we must add 0, 15 or 30 to
71         // the index. (Then the local possible indexes become 0..44 and
72         // C_3 = 45, of course.) Given an index number k, we can then get our
73         // own local “bits” of the index, giving the alternative for this
74         // block, by doing (k / 15) % 3.
75         //
76         // This specific member contains the value of C_i for this block.
77         // (B_i is alternatives.size().) Not set before finalize() has run.
78         size_t cardinality_base = 0;
79
80         // Find the chosen alternative for this block in a given instance.
81         int chosen_alternative(size_t chain_idx) const {
82                 if (chain_idx == size_t(-1)) {
83                         return currently_chosen_alternative;
84                 } else {
85                         return (chain_idx / cardinality_base) % alternatives.size();
86                 }
87         }
88
89         std::vector<EffectBlueprint *> alternatives;  // Must all have the same amount of inputs. Pointers to make things easier for Lua.
90         std::vector<Index> inputs;  // One for each input of alternatives[0] (ie., typically 0 or 1, occasionally 2).
91
92         // If any of these effects are disabled (IdentityEffect chosen)
93         // or enabled (not chosen) as determined by <condition>, so should this one.
94         struct Disabler {
95                 Index block_idx;
96                 enum {
97                         DISABLE_IF_OTHER_DISABLED,
98
99                         // This a promise from the user; ie., we don't disable automatically
100                         // (see comments in find_disabled_blocks()).
101                         DISABLE_IF_OTHER_ENABLED
102                 } condition;
103                 std::string declaration_point;  // For error messages.
104         };
105         std::vector<Disabler> disablers;
106         int currently_chosen_alternative = 0;
107         // What alternative to use if the block is disabled.
108         // Points to an alternative with IDENTITY_EFFECT if it exists
109         // (to disable as much as possible), otherwise 0.
110         int canonical_alternative = 0;
111         bool is_input = false;
112
113         // For LIVE_INPUT* only. We can't just always populate signal_to_connect,
114         // since when we set this, CEF and video signals may not have numbers yet.
115         // FIXME: Perhaps it would be simpler if they just did?
116         enum { CONNECT_NONE, CONNECT_SIGNAL, CONNECT_CEF, CONNECT_VIDEO } signal_type_to_connect = CONNECT_NONE;
117         int signal_to_connect = 0;  // For CONNECT_SIGNAL.
118 #ifdef HAVE_CEF
119         CEFCapture *cef_to_connect = nullptr;  // For CONNECT_CEF.
120 #endif
121         FFmpegCapture *video_to_connect = nullptr;  // For CONNECT_VIDEO.
122
123         std::string pathname;  // For IMAGE_INPUT only.
124
125         // Parameters to set on the effect prior to render.
126         // Will be set _before_ the ones from the EffectBlueprint, so that
127         // the latter takes priority.
128         std::map<std::string, int> int_parameters;
129         std::map<std::string, float> float_parameters;
130         std::map<std::string, std::array<float, 3>> vec3_parameters;
131         std::map<std::string, std::array<float, 4>> vec4_parameters;
132
133         std::string declaration_point;  // For error messages.
134
135         // Only for AUTO_WHITE_BALANCE_EFFECT. Points to the parent block with is_input = true,
136         // so that we know which signal to get the white balance from.
137         const Block *white_balance_controller_block = nullptr;
138 };
139
140 int Block_display(lua_State* L);
141 int Block_choose(lua_State* L);
142 int Block_enable(lua_State *L);
143 int Block_enable_if(lua_State *L);
144 int Block_disable(lua_State *L);
145 int Block_always_disable_if_disabled(lua_State *L);
146 int Block_promise_to_disable_if_enabled(lua_State *L);
147 int Block_set_int(lua_State *L);
148 int Block_set_float(lua_State *L);
149 int Block_set_vec3(lua_State *L);
150 int Block_set_vec4(lua_State *L);
151
152 class Scene {
153 private:
154         std::vector<Block *> blocks;  // The last one represents the output node (after finalization). Pointers to make things easier for Lua.
155         struct Instantiation {
156                 std::unique_ptr<movit::EffectChain> chain;
157                 std::map<Block::Index, movit::Effect *> effects;  // So that we can set parameters.
158                 std::map<Block::Index, LiveInputWrapper *> inputs;  // So that we can connect signals.
159                 std::map<Block::Index, ImageInput *> image_inputs;  // So that we can connect signals.
160         };
161         std::vector<Instantiation> chains;  // Indexed by combination of each block's chosen alternative. See Block for information.
162
163         Theme *theme;
164         float aspect_nom, aspect_denom;
165         movit::ResourcePool *resource_pool;
166
167         movit::Effect *instantiate_effects(const Block *block, size_t chain_idx, Instantiation *instantiation);
168         size_t compute_chain_number_for_block(size_t block_idx, const std::bitset<256> &disabled) const;
169         static void find_inputs_for_block(lua_State *L, Scene *scene, Block *block, int first_input_idx = 3);
170         static Block *find_block_from_arg(lua_State *L, Scene *scene, int idx);
171
172         // Find out which blocks (indexed by position in the “blocks” array),
173         // if any, are disabled in a given instantiation. A disabled block is
174         // one that will not be instantiated at all, because it is a secondary
175         // (ie., not the first) input of some multi-input effect that was replaced
176         // with IdentityEffect in the given instantiation.
177         //
178         // Set chain_idx to size_t(-1) to use whatever is in each block's
179         // currently_chosen_alternative.
180         std::bitset<256> find_disabled_blocks(size_t chain_idx) const;
181         void find_disabled_blocks(size_t chain_idx, size_t block_idx, bool currently_disabled, std::bitset<256> *disabled) const;
182
183         // If a block is disabled, it should always have canonical_alternative chosen,
184         // so that we don't instantiate a bunch of irrelevant duplicates that
185         // differ only in disabled blocks. You can check this property with
186         // is_noncanonical_chain() and then avoid instantiating the ones where
187         // it returns true.
188         bool is_noncanonical_chain(size_t chain_idx) const;
189
190         // For a given block, find any parents it may have that are inputs.
191         // If there is more than one, throws an error. If there are zero,
192         // returns nullptr (should probably also be an error).
193         const Block *find_root_input_block(lua_State *L, const Block *block);
194
195 public:
196         Scene(Theme *theme, float aspect_nom, float aspect_denom);
197         size_t compute_chain_number(bool is_main_chain) const;
198
199         std::pair<movit::EffectChain *, std::function<void()>>
200         get_chain(Theme *theme, lua_State *L, unsigned num, const InputState &input_state);
201
202         static int add_input(lua_State *L);
203         static int add_effect(lua_State *L);
204         static int add_optional_effect(lua_State *L);
205         static int add_auto_white_balance(lua_State *L);
206         static int finalize(lua_State *L);
207 };
208
209 #endif   // !defined(_SCENE_H)