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