8 #include "cef_capture.h"
10 #include "ffmpeg_capture.h"
12 #include "image_input.h"
13 #include "input_state.h"
14 #include "lua_utils.h"
18 using namespace movit;
21 bool display(Block *block, lua_State *L, int idx);
23 EffectType current_type(const Block *block)
25 return block->alternatives[block->currently_chosen_alternative]->effect_type;
28 int find_index_of(const Block *block, EffectType val)
30 for (size_t idx = 0; idx < block->alternatives.size(); ++idx) {
31 if (block->alternatives[idx]->effect_type == val) {
38 string get_declaration_point(lua_State *L)
41 lua_getstack(L, 1, &ar);
42 lua_getinfo(L, "nSl", &ar);
44 snprintf(buf, sizeof(buf), "%s:%d", ar.source, ar.currentline);
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()) {}
51 size_t Scene::compute_chain_number(bool is_main_chain) const
53 assert(chains.size() > 0);
54 assert(chains.size() % 2 == 0);
55 bitset<256> disabled = find_disabled_blocks(size_t(-1));
57 size_t chain_number = compute_chain_number_for_block(blocks.size() - 1, disabled);
58 assert(chain_number < chains.size() / 2);
60 chain_number += chains.size() / 2;
65 size_t Scene::compute_chain_number_for_block(size_t block_idx, const bitset<256> &disabled) const
67 Block *block = blocks[block_idx];
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;
76 currently_chosen_alternative = block->currently_chosen_alternative;
78 assert(currently_chosen_alternative < block->alternatives.size());
81 assert(block->cardinality_base == 1);
82 chain_number = currently_chosen_alternative;
84 chain_number = compute_chain_number_for_block(block_idx - 1, disabled) + block->cardinality_base * currently_chosen_alternative;
89 bitset<256> Scene::find_disabled_blocks(size_t chain_idx) const
91 assert(blocks.size() < 256);
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;
98 find_disabled_blocks(chain_idx, blocks.size() - 1, /*currently_disabled=*/false, &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.
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) {
113 } while (prev != ret);
117 void Scene::find_disabled_blocks(size_t chain_idx, size_t block_idx, bool currently_disabled, bitset<256> *disabled) const
119 if (currently_disabled) {
120 disabled->set(block_idx);
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);
131 // Just keep on recursing down.
132 find_disabled_blocks(chain_idx, block->inputs[input_idx], currently_disabled, disabled);
137 bool Scene::is_noncanonical_chain(size_t chain_idx) const
139 bitset<256> disabled = find_disabled_blocks(chain_idx);
140 if (disabled.none()) {
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) {
153 int Scene::add_input(lua_State* L)
155 assert(lua_gettop(L) == 1 || lua_gettop(L) == 2);
156 Scene *scene = (Scene *)luaL_checkudata(L, 1, "Scene");
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));
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));
174 } else if (luaL_testudata(L, 2, "HTMLInput")) {
175 block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_BGRA));
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));
182 assert(capture->get_current_pixel_format() == bmusb::PixelFormat_8BitBGRA);
183 block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_BGRA));
185 } else if (luaL_testudata(L, 2, "ImageInput")) {
186 block->alternatives.emplace_back(new EffectBlueprint(IMAGE_INPUT));
188 luaL_error(L, "add_input() called with something that's not a signal (a signal number, a HTML input, or a VideoInput)");
190 bool ok = display(block, L, 2);
193 block->is_input = true;
194 scene->blocks.push_back(block);
196 return wrap_lua_existing_object_nonowned<Block>(L, "Block", block);
199 void Scene::find_inputs_for_block(lua_State *L, Scene *scene, Block *block)
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);
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");
213 EffectBlueprint *blueprint = *(EffectBlueprint **)luaL_checkudata(L, idx, "EffectBlueprint");
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()) {
222 if (input_block == nullptr) {
223 luaL_error(L, "Input effect in parameter #%d has not been added to this scene", idx - 1);
226 block->inputs.push_back(input_block->idx);
230 int Scene::add_effect(lua_State* L)
232 assert(lua_gettop(L) >= 2);
233 Scene *scene = (Scene *)luaL_checkudata(L, 1, "Scene");
235 Block *block = new Block;
236 block->declaration_point = get_declaration_point(L);
237 block->idx = scene->blocks.size();
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);
248 EffectBlueprint *blueprint = *(EffectBlueprint **)luaL_checkudata(L, 2, "EffectBlueprint");
249 block->alternatives.push_back(blueprint);
252 int identity_index = find_index_of(block, IDENTITY_EFFECT);
253 if (identity_index == -1) {
254 block->canonical_alternative = 0;
256 // Pick the IdentityEffect as the canonical alternative, in case it
257 // helps us disable more stuff.
258 block->canonical_alternative = identity_index;
261 find_inputs_for_block(L, scene, block);
262 scene->blocks.push_back(block);
264 return wrap_lua_existing_object_nonowned<Block>(L, "Block", block);
267 int Scene::add_optional_effect(lua_State* L)
269 assert(lua_gettop(L) >= 2);
270 Scene *scene = (Scene *)luaL_checkudata(L, 1, "Scene");
272 Block *block = new Block;
273 block->declaration_point = get_declaration_point(L);
274 block->idx = scene->blocks.size();
276 EffectBlueprint *blueprint = *(EffectBlueprint **)luaL_checkudata(L, 2, "EffectBlueprint");
277 block->alternatives.push_back(blueprint);
279 // An IdentityEffect will be the alternative for when the effect is disabled.
280 block->alternatives.push_back(new EffectBlueprint(IDENTITY_EFFECT));
282 block->canonical_alternative = 1;
284 find_inputs_for_block(L, scene, block);
285 scene->blocks.push_back(block);
287 return wrap_lua_existing_object_nonowned<Block>(L, "Block", block);
290 Effect *Scene::instantiate_effects(const Block *block, size_t chain_idx, Scene::Instantiation *instantiation)
292 // Find the chosen alternative for this block in this instance.
293 EffectType chosen_type = block->alternatives[block->chosen_alternative(chain_idx)]->effect_type;
295 vector<Effect *> inputs;
296 for (size_t input_idx : block->inputs) {
297 inputs.push_back(instantiate_effects(blocks[input_idx], chain_idx, instantiation));
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
303 if (chosen_type == IDENTITY_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;
324 pixel_format = bmusb::PixelFormat_8BitYCbCr;
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);
332 ImageInput *input = new ImageInput;
333 instantiation->chain->add_input(input);
334 instantiation->image_inputs.emplace(block->idx, input);
339 effect = instantiate_effect(instantiation->chain.get(), chosen_type);
340 instantiation->chain->add_effect(effect, inputs);
343 instantiation->effects.emplace(block->idx, effect);
347 int Scene::finalize(lua_State* L)
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);
355 assert(lua_gettop(L) == 1);
357 Scene *scene = (Scene *)luaL_checkudata(L, 1, "Scene");
358 Theme *theme = get_theme_updata(L);
361 for (Block *block : scene->blocks) {
362 block->cardinality_base = base;
363 base *= block->alternatives.size();
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)) {
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",
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();
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);
392 add_outputs_and_finalize(instantiation.chain.get(), is_main_chain);
393 scene->chains.emplace_back(move(instantiation));
399 std::pair<movit::EffectChain *, std::function<void()>>
400 Scene::get_chain(Theme *theme, lua_State *L, unsigned num, const InputState &input_state)
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);
412 block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_YCBCR);
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();
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);
443 } else if (block->signal_type_to_connect == Block::CONNECT_CEF) {
444 signals_to_connect.emplace(input, block->cef_to_connect->get_card_index());
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())");
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);
462 for (const auto &index_and_effect : instantiation.effects) {
463 Block *block = blocks[index_and_effect.first];
464 Effect *effect = index_and_effect.second;
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;
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;
476 if (block->int_parameters.count("height") && block->int_parameters["height"] > 0) {
477 missing_height = false;
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);
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);
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);
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);
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;
500 if (blueprint->int_parameters.count("height") && blueprint->int_parameters["height"] > 0) {
501 missing_height = false;
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;
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;
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;
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;
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;
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);
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);
536 for (const auto &input_and_filename : images_to_select) {
537 input_and_filename.first->switch_image(input_and_filename.second);
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);
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);
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]);
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]);
574 return make_pair(effect_chain, move(setup_chain));
577 bool display(Block *block, lua_State *L, int idx)
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.
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);
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);
602 assert(capture->get_current_pixel_format() == bmusb::PixelFormat_8BitBGRA);
603 block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_BGRA);
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();
617 int Block_display(lua_State* L)
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");
625 bool ok = display(block, L, 2);
627 luaL_error(L, "display() called with something that's not a signal (a signal number, a HTML input, or a VideoInput)");
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");
637 int Block_choose(lua_State* L)
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);
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");
658 assert(alternative_idx >= 0);
659 assert(size_t(alternative_idx) < block->alternatives.size());
660 block->currently_chosen_alternative = alternative_idx;
662 return wrap_lua_existing_object_nonowned<EffectBlueprint>(L, "EffectBlueprint", block->alternatives[alternative_idx]);
665 int Block_enable(lua_State *L)
667 assert(lua_gettop(L) == 1);
668 Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
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()");
674 block->currently_chosen_alternative = 0; // The actual effect.
678 int Block_enable_if(lua_State *L)
680 assert(lua_gettop(L) == 2);
681 Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
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()");
687 bool enabled = checkbool(L, 2);
688 block->currently_chosen_alternative = enabled ? 0 : 1;
692 int Block_disable(lua_State *L)
694 assert(lua_gettop(L) == 1);
695 Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
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())");
701 assert(block->currently_chosen_alternative != -1);
705 int Block_always_disable_if_disabled(lua_State *L)
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");
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())");
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())");
720 block->disablers.push_back(disabler_block->idx);
726 int Block_set_int(lua_State *L)
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);
733 // TODO: check validity already here, if possible?
734 block->int_parameters[key] = value;
739 int Block_set_float(lua_State *L)
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);
746 // TODO: check validity already here, if possible?
747 block->float_parameters[key] = value;
752 int Block_set_vec3(lua_State *L)
754 assert(lua_gettop(L) == 5);
755 Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
756 string key = checkstdstring(L, 2);
758 v[0] = luaL_checknumber(L, 3);
759 v[1] = luaL_checknumber(L, 4);
760 v[2] = luaL_checknumber(L, 5);
762 // TODO: check validity already here, if possible?
763 block->vec3_parameters[key] = v;
768 int Block_set_vec4(lua_State *L)
770 assert(lua_gettop(L) == 6);
771 Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
772 string key = checkstdstring(L, 2);
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);
779 // TODO: check validity already here, if possible?
780 block->vec4_parameters[key] = v;