]> git.sesse.net Git - nageru/blob - nageru/scene.cpp
Support optional effects with multiple inputs.
[nageru] / nageru / scene.cpp
1 #include <assert.h>
2 extern "C" {
3 #include <lauxlib.h>
4 #include <lua.hpp>
5 }
6
7 #ifdef HAVE_CEF
8 #include "cef_capture.h"
9 #endif
10 #include "ffmpeg_capture.h"
11 #include "flags.h"
12 #include "image_input.h"
13 #include "input_state.h"
14 #include "lua_utils.h"
15 #include "scene.h"
16 #include "theme.h"
17
18 using namespace movit;
19 using namespace std;
20
21 bool display(Block *block, lua_State *L, int idx);
22
23 EffectType current_type(const Block *block)
24 {
25         return block->alternatives[block->currently_chosen_alternative]->effect_type;
26 }
27
28 int find_index_of(const Block *block, EffectType val)
29 {
30         for (size_t idx = 0; idx < block->alternatives.size(); ++idx) {
31                 if (block->alternatives[idx]->effect_type == val) {
32                         return idx;
33                 }
34         }
35         return -1;
36 }
37
38 Scene::Scene(Theme *theme, float aspect_nom, float aspect_denom)
39         : theme(theme), aspect_nom(aspect_nom), aspect_denom(aspect_denom), resource_pool(theme->get_resource_pool()) {}
40
41 size_t Scene::compute_chain_number(bool is_main_chain) const
42 {
43         assert(chains.size() > 0);
44         assert(chains.size() % 2 == 0);
45         bitset<256> disabled = find_disabled_blocks(size_t(-1));
46
47         size_t chain_number = compute_chain_number_for_block(blocks.size() - 1, disabled);
48         assert(chain_number < chains.size() / 2);
49         if (is_main_chain) {
50                 chain_number += chains.size() / 2;
51         }
52         return chain_number;
53 }
54
55 size_t Scene::compute_chain_number_for_block(size_t block_idx, const bitset<256> &disabled) const
56 {
57         Block *block = blocks[block_idx];
58         size_t chain_number;
59
60         size_t currently_chosen_alternative;
61         if (disabled.test(block_idx)) {
62                 // It doesn't matter, so pick 0 as the canonical choice
63                 // (this is the only one that is actually instantiated).
64                 currently_chosen_alternative = 0;
65         } else {
66                 currently_chosen_alternative = block->currently_chosen_alternative;
67         }
68         assert(currently_chosen_alternative < block->alternatives.size());
69
70         if (block_idx == 0) {
71                 assert(block->cardinality_base == 1);
72                 chain_number = currently_chosen_alternative;
73         } else {
74                 chain_number = compute_chain_number_for_block(block_idx - 1, disabled) + block->cardinality_base * currently_chosen_alternative;
75         }
76         return chain_number;
77 }
78
79 bitset<256> Scene::find_disabled_blocks(size_t chain_idx) const
80 {
81         assert(blocks.size() < 256);
82
83         bitset<256> ret;
84         find_disabled_blocks(chain_idx, blocks.size() - 1, /*currently_disabled=*/false, &ret);
85         return ret;
86 }
87
88 void Scene::find_disabled_blocks(size_t chain_idx, size_t block_idx, bool currently_disabled, bitset<256> *disabled) const
89 {
90         if (currently_disabled) {
91                 disabled->set(block_idx);
92         }
93         Block *block = blocks[block_idx];
94         EffectType chosen_type = block->alternatives[block->chosen_alternative(chain_idx)]->effect_type;
95         for (size_t input_idx = 0; input_idx < block->inputs.size(); ++input_idx) {
96                 if (chosen_type == IDENTITY_EFFECT && input_idx > 0) {
97                         // Multi-input effect that has been replaced by
98                         // IdentityEffect, so every effect but the first are
99                         // disabled and will not participate in the chain.
100                         find_disabled_blocks(chain_idx, block->inputs[input_idx], /*currently_disabled=*/true, disabled);
101                 } else {
102                         // Just keep on recursing down.
103                         find_disabled_blocks(chain_idx, block->inputs[input_idx], currently_disabled, disabled);
104                 }
105         }
106 }
107
108 bool Scene::is_noncanonical_chain(size_t chain_idx) const
109 {
110         bitset<256> disabled = find_disabled_blocks(chain_idx);
111         if (disabled.none()) {
112                 return false;
113         }
114         assert(blocks.size() < 256);
115         for (size_t block_idx = 0; block_idx < blocks.size(); ++block_idx) {
116                 if (disabled.test(block_idx) && blocks[block_idx]->chosen_alternative(chain_idx) != 0) {
117                         return true;
118                 }
119         }
120         return false;
121 }
122
123 int Scene::add_input(lua_State* L)
124 {
125         assert(lua_gettop(L) == 1 || lua_gettop(L) == 2);
126         Scene *scene = (Scene *)luaL_checkudata(L, 1, "Scene");
127
128         Block *block = new Block;
129         block->idx = scene->blocks.size();
130         if (lua_gettop(L) == 1) {
131                 // No parameter given, so a flexible input.
132                 block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_YCBCR));
133                 block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_YCBCR_WITH_DEINTERLACE));
134                 block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_YCBCR_PLANAR));
135                 block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_BGRA));
136                 block->alternatives.emplace_back(new EffectBlueprint(IMAGE_INPUT));
137         } else {
138                 // Input of a given type. We'll specialize it here, plus connect the input as given.
139                 if (lua_isnumber(L, 2)) {
140                         block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_YCBCR));
141                         block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_YCBCR_WITH_DEINTERLACE));
142 #ifdef HAVE_CEF
143                 } else if (luaL_testudata(L, 2, "HTMLInput")) {
144                         block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_BGRA));
145 #endif
146                 } else if (luaL_testudata(L, 2, "VideoInput")) {
147                         FFmpegCapture *capture = *(FFmpegCapture **)luaL_checkudata(L, 2, "VideoInput");
148                         if (capture->get_current_pixel_format() == bmusb::PixelFormat_8BitYCbCrPlanar) {
149                                 block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_YCBCR_PLANAR));
150                         } else {
151                                 assert(capture->get_current_pixel_format() == bmusb::PixelFormat_8BitBGRA);
152                                 block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_BGRA));
153                         }
154                 } else if (luaL_testudata(L, 2, "ImageInput")) {
155                         block->alternatives.emplace_back(new EffectBlueprint(IMAGE_INPUT));
156                 } else {
157                         luaL_error(L, "add_input() called with something that's not a signal (a signal number, a HTML input, or a VideoInput)");
158                 }
159                 bool ok = display(block, L, 2);
160                 assert(ok);
161         }
162         block->is_input = true;
163         scene->blocks.push_back(block);
164
165         return wrap_lua_existing_object_nonowned<Block>(L, "Block", block);
166 }
167
168 void Scene::find_inputs_for_block(lua_State *L, Scene *scene, Block *block)
169 {
170         if (lua_gettop(L) == 2) {
171                 // Implicitly the last added effect.
172                 assert(!scene->blocks.empty());
173                 block->inputs.push_back(scene->blocks.size() - 1);
174                 return;
175         }
176
177         for (int idx = 3; idx <= lua_gettop(L); ++idx) {
178                 Block *input_block = nullptr;
179                 if (luaL_testudata(L, idx, "Block")) {
180                         input_block = *(Block **)luaL_checkudata(L, idx, "Block");
181                 } else {
182                         EffectBlueprint *blueprint = *(EffectBlueprint **)luaL_checkudata(L, idx, "EffectBlueprint");
183
184                         // Search through all the blocks to figure out which one contains this effect.
185                         for (Block *block : scene->blocks) {
186                                 if (find(block->alternatives.begin(), block->alternatives.end(), blueprint) != block->alternatives.end()) {
187                                         input_block = block;
188                                         break;
189                                 }
190                         }
191                         if (input_block == nullptr) {
192                                 luaL_error(L, "Input effect in parameter #%d has not been added to this scene", idx - 1);
193                         }
194                 }
195                 block->inputs.push_back(input_block->idx);
196         }
197 }
198
199 int Scene::add_effect(lua_State* L)
200 {
201         assert(lua_gettop(L) >= 2);
202         Scene *scene = (Scene *)luaL_checkudata(L, 1, "Scene");
203
204         Block *block = new Block;
205         block->idx = scene->blocks.size();
206
207         if (lua_istable(L, 2)) {
208                 size_t len = lua_objlen(L, 2);
209                 for (size_t i = 0; i < len; ++i) {
210                         lua_rawgeti(L, 2, i + 1);
211                         EffectBlueprint *blueprint = *(EffectBlueprint **)luaL_checkudata(L, -1, "EffectBlueprint");
212                         block->alternatives.push_back(blueprint);
213                         lua_settop(L, -2);
214                 }
215         } else {
216                 EffectBlueprint *blueprint = *(EffectBlueprint **)luaL_checkudata(L, 2, "EffectBlueprint");
217                 block->alternatives.push_back(blueprint);
218         }
219
220         find_inputs_for_block(L, scene, block);
221         scene->blocks.push_back(block);
222
223         return wrap_lua_existing_object_nonowned<Block>(L, "Block", block);
224 }
225
226 int Scene::add_optional_effect(lua_State* L)
227 {
228         assert(lua_gettop(L) >= 2);
229         Scene *scene = (Scene *)luaL_checkudata(L, 1, "Scene");
230
231         Block *block = new Block;
232         block->idx = scene->blocks.size();
233
234         EffectBlueprint *blueprint = *(EffectBlueprint **)luaL_checkudata(L, 2, "EffectBlueprint");
235         block->alternatives.push_back(blueprint);
236
237         // An IdentityEffect will be the alternative for when the effect is disabled.
238         block->alternatives.push_back(new EffectBlueprint(IDENTITY_EFFECT));
239
240         find_inputs_for_block(L, scene, block);
241         scene->blocks.push_back(block);
242
243         return wrap_lua_existing_object_nonowned<Block>(L, "Block", block);
244 }
245
246 Effect *Scene::instantiate_effects(const Block *block, size_t chain_idx, Scene::Instantiation *instantiation)
247 {
248         // Find the chosen alternative for this block in this instance.
249         EffectType chosen_type = block->alternatives[block->chosen_alternative(chain_idx)]->effect_type;
250
251         vector<Effect *> inputs;
252         for (size_t input_idx : block->inputs) {
253                 inputs.push_back(instantiate_effects(blocks[input_idx], chain_idx, instantiation));
254
255                 // As a special case, we allow IdentityEffect to take only one input
256                 // even if the other alternative (or alternatives) is multi-input.
257                 // Thus, even if there are more than one inputs, instantiate only
258                 // the first one.
259                 if (chosen_type == IDENTITY_EFFECT) {
260                         break;
261                 }
262         }
263
264         Effect *effect;
265         switch (chosen_type) {
266         case LIVE_INPUT_YCBCR:
267         case LIVE_INPUT_YCBCR_WITH_DEINTERLACE:
268         case LIVE_INPUT_YCBCR_PLANAR:
269         case LIVE_INPUT_BGRA: {
270                 bool deinterlace = (chosen_type == LIVE_INPUT_YCBCR_WITH_DEINTERLACE);
271                 bool override_bounce = !deinterlace;  // For most chains, this will be fine. Reconsider if we see real problems somewhere; it's better than having the user try to understand it.
272                 bmusb::PixelFormat pixel_format;
273                 if (chosen_type == LIVE_INPUT_BGRA) {
274                         pixel_format = bmusb::PixelFormat_8BitBGRA;
275                 } else if (chosen_type == LIVE_INPUT_YCBCR_PLANAR) {
276                         pixel_format = bmusb::PixelFormat_8BitYCbCrPlanar;
277                 } else if (global_flags.ten_bit_input) {
278                         pixel_format = bmusb::PixelFormat_10BitYCbCr;
279                 } else {
280                         pixel_format = bmusb::PixelFormat_8BitYCbCr;
281                 }
282                 LiveInputWrapper *input = new LiveInputWrapper(theme, instantiation->chain.get(), pixel_format, override_bounce, deinterlace, /*user_connectable=*/true);
283                 effect = input->get_effect();  // Adds itself to the chain, so no need to call add_effect().
284                 instantiation->inputs.emplace(block->idx, input);
285                 break;
286         }
287         case IMAGE_INPUT: {
288                 ImageInput *input = new ImageInput;
289                 instantiation->chain->add_input(input);
290                 instantiation->image_inputs.emplace(block->idx, input);
291                 effect = input;
292                 break;
293         }
294         default:
295                 effect = instantiate_effect(instantiation->chain.get(), chosen_type);
296                 instantiation->chain->add_effect(effect, inputs);
297                 break;
298         }
299         instantiation->effects.emplace(block->idx, effect);
300         return effect;
301 }
302
303 int Scene::finalize(lua_State* L)
304 {
305         bool only_one_mode = false;
306         bool chosen_mode = false;
307         if (lua_gettop(L) == 2) {
308                 only_one_mode = true;
309                 chosen_mode = checkbool(L, 2);
310         } else {
311                 assert(lua_gettop(L) == 1);
312         }
313         Scene *scene = (Scene *)luaL_checkudata(L, 1, "Scene");
314         Theme *theme = get_theme_updata(L);
315
316         size_t base = 1;
317         for (Block *block : scene->blocks) {
318                 block->cardinality_base = base;
319                 base *= block->alternatives.size();
320         }
321
322         const size_t cardinality = base;
323         size_t real_cardinality = 0;
324         for (size_t chain_idx = 0; chain_idx < cardinality; ++chain_idx) {
325                 if (!scene->is_noncanonical_chain(chain_idx)) {
326                         ++real_cardinality;
327                 }
328         }
329         const size_t total_cardinality = real_cardinality * (only_one_mode ? 1 : 2);
330         if (total_cardinality > 200) {
331                 print_warning(L, "The given Scene will instantiate %zu different versions. This will take a lot of time and RAM to compile; see if you could limit some options by e.g. locking the input type in some cases (by giving a fixed input to add_input()).\n",
332                         total_cardinality);
333         }
334
335         Block *output_block = scene->blocks.back();
336         for (bool is_main_chain : { false, true }) {
337                 for (size_t chain_idx = 0; chain_idx < cardinality; ++chain_idx) {
338                         if ((only_one_mode && is_main_chain != chosen_mode) ||
339                             scene->is_noncanonical_chain(chain_idx)) {
340                                 scene->chains.emplace_back();
341                                 continue;
342                         }
343
344                         Scene::Instantiation instantiation;
345                         instantiation.chain.reset(new EffectChain(scene->aspect_nom, scene->aspect_denom, theme->get_resource_pool()));
346                         scene->instantiate_effects(output_block, chain_idx, &instantiation);
347
348                         add_outputs_and_finalize(instantiation.chain.get(), is_main_chain);
349                         scene->chains.emplace_back(move(instantiation));
350                 }
351         }
352         return 0;
353 }
354
355 std::pair<movit::EffectChain *, std::function<void()>>
356 Scene::get_chain(Theme *theme, lua_State *L, unsigned num, const InputState &input_state)
357 {
358         // For video inputs, pick the right interlaced/progressive version
359         // based on the current state of the signals.
360         InputStateInfo info(input_state);
361         for (Block *block : blocks) {
362                 if (block->is_input && block->signal_type_to_connect == Block::CONNECT_SIGNAL) {
363                         EffectType chosen_type = current_type(block);
364                         assert(chosen_type == LIVE_INPUT_YCBCR || chosen_type == LIVE_INPUT_YCBCR_WITH_DEINTERLACE);
365                         if (info.last_interlaced[block->signal_to_connect]) {
366                                 block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_YCBCR_WITH_DEINTERLACE);
367                         } else {
368                                 block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_YCBCR);
369                         }
370                 }
371         }
372
373         // Pick out the right chain based on the current selections,
374         // and snapshot all the set variables so that we can set them
375         // in the prepare function even if they're being changed by
376         // the Lua code later.
377         bool is_main_chain = (num == 0);
378         size_t chain_idx = compute_chain_number(is_main_chain);
379         const Scene::Instantiation &instantiation = chains[chain_idx];
380         EffectChain *effect_chain = instantiation.chain.get();
381
382         map<LiveInputWrapper *, int> signals_to_connect;
383         map<ImageInput *, string> images_to_select;
384         map<pair<Effect *, string>, int> int_to_set;
385         map<pair<Effect *, string>, float> float_to_set;
386         map<pair<Effect *, string>, array<float, 3>> vec3_to_set;
387         map<pair<Effect *, string>, array<float, 4>> vec4_to_set;
388         for (const auto &index_and_input : instantiation.inputs) {
389                 Block *block = blocks[index_and_input.first];
390                 EffectType chosen_type = current_type(block);
391                 LiveInputWrapper *input = index_and_input.second;
392                 if (chosen_type == LIVE_INPUT_YCBCR ||
393                     chosen_type == LIVE_INPUT_YCBCR_WITH_DEINTERLACE ||
394                     chosen_type == LIVE_INPUT_YCBCR_PLANAR ||
395                     chosen_type == LIVE_INPUT_BGRA) {
396                         if (block->signal_type_to_connect == Block::CONNECT_SIGNAL) {
397                                 signals_to_connect.emplace(input, block->signal_to_connect);
398 #ifdef HAVE_CEF
399                         } else if (block->signal_type_to_connect == Block::CONNECT_CEF) {
400                                 signals_to_connect.emplace(input, block->cef_to_connect->get_card_index());
401 #endif
402                         } else if (block->signal_type_to_connect == Block::CONNECT_VIDEO) {
403                                 signals_to_connect.emplace(input, block->video_to_connect->get_card_index());
404                         } else if (block->signal_type_to_connect == Block::CONNECT_NONE) {
405                                 luaL_error(L, "An input in a scene was not connected to anything (forgot to call display())");
406                         } else {
407                                 assert(false);
408                         }
409                 }
410         }
411         for (const auto &index_and_input : instantiation.image_inputs) {
412                 Block *block = blocks[index_and_input.first];
413                 ImageInput *input = index_and_input.second;
414                 if (current_type(block) == IMAGE_INPUT) {
415                         images_to_select.emplace(input, block->pathname);
416                 }
417         }
418         for (const auto &index_and_effect : instantiation.effects) {
419                 Block *block = blocks[index_and_effect.first];
420                 Effect *effect = index_and_effect.second;
421
422                 // Get the effects currently set on the block.
423                 if (current_type(block) != IDENTITY_EFFECT) {  // Ignore settings on optional effects.
424                         for (const auto &key_and_tuple : block->int_parameters) {
425                                 int_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
426                         }
427                         for (const auto &key_and_tuple : block->float_parameters) {
428                                 float_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
429                         }
430                         for (const auto &key_and_tuple : block->vec3_parameters) {
431                                 vec3_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
432                         }
433                         for (const auto &key_and_tuple : block->vec4_parameters) {
434                                 vec4_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
435                         }
436                 }
437
438                 // Parameters set on the blueprint itself override those that are set for the block,
439                 // so they are set afterwards.
440                 if (!block->alternatives.empty()) {
441                         EffectBlueprint *blueprint = block->alternatives[block->currently_chosen_alternative];
442                         for (const auto &key_and_tuple : blueprint->int_parameters) {
443                                 int_to_set[make_pair(effect, key_and_tuple.first)] = key_and_tuple.second;
444                         }
445                         for (const auto &key_and_tuple : blueprint->float_parameters) {
446                                 float_to_set[make_pair(effect, key_and_tuple.first)] = key_and_tuple.second;
447                         }
448                         for (const auto &key_and_tuple : blueprint->vec3_parameters) {
449                                 vec3_to_set[make_pair(effect, key_and_tuple.first)] = key_and_tuple.second;
450                         }
451                         for (const auto &key_and_tuple : blueprint->vec4_parameters) {
452                                 vec4_to_set[make_pair(effect, key_and_tuple.first)] = key_and_tuple.second;
453                         }
454                 }
455         }
456
457         lua_pop(L, 1);
458
459         auto setup_chain = [L, theme, signals_to_connect, images_to_select, int_to_set, float_to_set, vec3_to_set, vec4_to_set, input_state]{
460                 lock_guard<mutex> lock(theme->m);
461
462                 // Set up state, including connecting signals.
463                 for (const auto &input_and_signal : signals_to_connect) {
464                         LiveInputWrapper *input = input_and_signal.first;
465                         input->connect_signal_raw(input_and_signal.second, input_state);
466                 }
467                 for (const auto &input_and_filename : images_to_select) {
468                         input_and_filename.first->switch_image(input_and_filename.second);
469                 }
470                 for (const auto &effect_and_key_and_value : int_to_set) {
471                         Effect *effect = effect_and_key_and_value.first.first;
472                         const string &key = effect_and_key_and_value.first.second;
473                         const int value = effect_and_key_and_value.second;
474                         if (!effect->set_int(key, value)) {
475                                 luaL_error(L, "Effect refused set_int(\"%s\", %d) (invalid key?)", key.c_str(), value);
476                         }
477                 }
478                 for (const auto &effect_and_key_and_value : float_to_set) {
479                         Effect *effect = effect_and_key_and_value.first.first;
480                         const string &key = effect_and_key_and_value.first.second;
481                         const float value = effect_and_key_and_value.second;
482                         if (!effect->set_float(key, value)) {
483                                 luaL_error(L, "Effect refused set_float(\"%s\", %f) (invalid key?)", key.c_str(), value);
484                         }
485                 }
486                 for (const auto &effect_and_key_and_value : vec3_to_set) {
487                         Effect *effect = effect_and_key_and_value.first.first;
488                         const string &key = effect_and_key_and_value.first.second;
489                         const float *value = effect_and_key_and_value.second.data();
490                         if (!effect->set_vec3(key, value)) {
491                                 luaL_error(L, "Effect refused set_vec3(\"%s\", %f, %f, %f) (invalid key?)", key.c_str(),
492                                                 value[0], value[1], value[2]);
493                         }
494                 }
495                 for (const auto &effect_and_key_and_value : vec4_to_set) {
496                         Effect *effect = effect_and_key_and_value.first.first;
497                         const string &key = effect_and_key_and_value.first.second;
498                         const float *value = effect_and_key_and_value.second.data();
499                         if (!effect->set_vec4(key, value)) {
500                                 luaL_error(L, "Effect refused set_vec4(\"%s\", %f, %f, %f, %f) (invalid key?)", key.c_str(),
501                                                 value[0], value[1], value[2], value[3]);
502                         }
503                 }
504         };
505         return make_pair(effect_chain, move(setup_chain));
506 }
507
508 bool display(Block *block, lua_State *L, int idx)
509 {
510         if (lua_isnumber(L, idx)) {
511                 Theme *theme = get_theme_updata(L);
512                 int signal_idx = luaL_checknumber(L, idx);
513                 block->signal_type_to_connect = Block::CONNECT_SIGNAL;
514                 block->signal_to_connect = theme->map_signal(signal_idx);
515                 block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_YCBCR);  // Will be changed to deinterlaced at get_chain() time if needed.
516                 return true;
517 #ifdef HAVE_CEF
518         } else if (luaL_testudata(L, idx, "HTMLInput")) {
519                 CEFCapture *capture = *(CEFCapture **)luaL_checkudata(L, idx, "HTMLInput");
520                 block->signal_type_to_connect = Block::CONNECT_CEF;
521                 block->cef_to_connect = capture;
522                 block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_BGRA);
523                 assert(capture->get_current_pixel_format() == bmusb::PixelFormat_8BitBGRA);
524                 return true;
525 #endif
526         } else if (luaL_testudata(L, idx, "VideoInput")) {
527                 FFmpegCapture *capture = *(FFmpegCapture **)luaL_checkudata(L, idx, "VideoInput");
528                 block->signal_type_to_connect = Block::CONNECT_VIDEO;
529                 block->video_to_connect = capture;
530                 if (capture->get_current_pixel_format() == bmusb::PixelFormat_8BitYCbCrPlanar) {
531                         block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_YCBCR_PLANAR);
532                 } else {
533                         assert(capture->get_current_pixel_format() == bmusb::PixelFormat_8BitBGRA);
534                         block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_BGRA);
535                 }
536                 return true;
537         } else if (luaL_testudata(L, idx, "ImageInput")) {
538                 ImageInput *image = *(ImageInput **)luaL_checkudata(L, idx, "ImageInput");
539                 block->signal_type_to_connect = Block::CONNECT_NONE;
540                 block->currently_chosen_alternative = find_index_of(block, IMAGE_INPUT);
541                 block->pathname = image->get_pathname();
542                 return true;
543         } else {
544                 return false;
545         }
546 }
547
548 int Block_display(lua_State* L)
549 {
550         assert(lua_gettop(L) == 2);
551         Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
552         if (!block->is_input) {
553                 luaL_error(L, "display() called on something that isn't an input");
554         }
555
556         bool ok = display(block, L, 2);
557         if (!ok) {
558                 luaL_error(L, "display() called with something that's not a signal (a signal number, a HTML input, or a VideoInput)");
559         }
560
561         if (block->currently_chosen_alternative == -1) {
562                 luaL_error(L, "display() called on an input whose type was fixed at construction time, with a signal of different type");
563         }
564
565         return 0;
566 }
567
568 int Block_choose(lua_State* L)
569 {
570         assert(lua_gettop(L) == 2);
571         Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
572         int alternative_idx = -1;
573         if (lua_isnumber(L, 2)) {
574                 alternative_idx = luaL_checknumber(L, 2);
575         } else if (lua_istable(L, 2)) {
576                 // See if it's an Effect metatable (e.g. foo:choose(ResampleEffect))
577                 lua_getfield(L, 2, "__effect_type_id");
578                 if (lua_isnumber(L, -1)) {
579                         EffectType effect_type = EffectType(luaL_checknumber(L, -1));
580                         alternative_idx = find_index_of(block, effect_type);
581                 }
582                 lua_pop(L, 1);
583         }
584
585         if (alternative_idx == -1) {
586                 luaL_error(L, "choose() called with something that was not an index or an effect type (e.g. ResampleEffect) that was part of the alternatives");
587         }
588
589         assert(alternative_idx >= 0);
590         assert(size_t(alternative_idx) < block->alternatives.size());
591         block->currently_chosen_alternative = alternative_idx;
592
593         return wrap_lua_existing_object_nonowned<EffectBlueprint>(L, "EffectBlueprint", block->alternatives[alternative_idx]);
594 }
595
596 int Block_enable(lua_State *L)
597 {
598         assert(lua_gettop(L) == 1);
599         Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
600
601         if (block->alternatives.size() != 2 ||
602             block->alternatives[1]->effect_type != IDENTITY_EFFECT) {
603                 luaL_error(L, "enable() called on something that wasn't added with add_optional_effect()");
604         }
605         block->currently_chosen_alternative = 0;  // The actual effect.
606         return 0;
607 }
608
609 int Block_enable_if(lua_State *L)
610 {
611         assert(lua_gettop(L) == 2);
612         Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
613
614         if (block->alternatives.size() != 2 ||
615             block->alternatives[1]->effect_type != IDENTITY_EFFECT) {
616                 luaL_error(L, "enable_if() called on something that wasn't added with add_optional_effect()");
617         }
618         bool enabled = checkbool(L, 2);
619         block->currently_chosen_alternative = enabled ? 0 : 1;
620         return 0;
621 }
622
623 int Block_disable(lua_State *L)
624 {
625         assert(lua_gettop(L) == 1);
626         Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
627
628         block->currently_chosen_alternative = find_index_of(block, IDENTITY_EFFECT);
629         if (block->currently_chosen_alternative == -1) {
630                 luaL_error(L, "disable() called on something that didn't have an IdentityEffect fallback (try add_optional_effect())");
631         }
632         assert(block->currently_chosen_alternative != -1);
633         return 0;
634 }
635
636 int Block_set_int(lua_State *L)
637 {
638         assert(lua_gettop(L) == 3);
639         Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
640         string key = checkstdstring(L, 2);
641         float value = luaL_checknumber(L, 3);
642
643         // TODO: check validity already here, if possible?
644         block->int_parameters[key] = value;
645
646         return 0;
647 }
648
649 int Block_set_float(lua_State *L)
650 {
651         assert(lua_gettop(L) == 3);
652         Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
653         string key = checkstdstring(L, 2);
654         float value = luaL_checknumber(L, 3);
655
656         // TODO: check validity already here, if possible?
657         block->float_parameters[key] = value;
658
659         return 0;
660 }
661
662 int Block_set_vec3(lua_State *L)
663 {
664         assert(lua_gettop(L) == 5);
665         Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
666         string key = checkstdstring(L, 2);
667         array<float, 3> v;
668         v[0] = luaL_checknumber(L, 3);
669         v[1] = luaL_checknumber(L, 4);
670         v[2] = luaL_checknumber(L, 5);
671
672         // TODO: check validity already here, if possible?
673         block->vec3_parameters[key] = v;
674
675         return 0;
676 }
677
678 int Block_set_vec4(lua_State *L)
679 {
680         assert(lua_gettop(L) == 6);
681         Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
682         string key = checkstdstring(L, 2);
683         array<float, 4> v;
684         v[0] = luaL_checknumber(L, 3);
685         v[1] = luaL_checknumber(L, 4);
686         v[2] = luaL_checknumber(L, 5);
687         v[3] = luaL_checknumber(L, 6);
688
689         // TODO: check validity already here, if possible?
690         block->vec4_parameters[key] = v;
691
692         return 0;
693 }
694