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