-io.write("hello from lua\n");
-
-local live_signal_num = 0;
-local preview_signal_num = 1;
-
--- The main live chain. Currently just about input 0 with some color correction.
-local main_chain = EffectChain.new(16, 9);
-local input0 = main_chain:add_live_input();
-input0:connect_signal(0);
-local resample_effect = main_chain:add_effect(ResampleEffect.new(), input0);
-resample_effect:set_int("width", 320);
-resample_effect:set_int("height", 180);
-local padding_effect = main_chain:add_effect(IntegralPaddingEffect.new(), resample_effect);
-padding_effect:set_int("width", 1280);
-padding_effect:set_int("height", 720);
-padding_effect:set_int("left", 30);
-padding_effect:set_int("top", 60);
-local input1 = main_chain:add_live_input();
-input1:connect_signal(1);
-local wb_effect = main_chain:add_effect(WhiteBalanceEffect.new(), input1);
-local overlay_effect = main_chain:add_effect(OverlayEffect.new(), wb_effect, padding_effect);
-main_chain:finalize(true);
+
+local state = {
+ transition_start = -2.0,
+ transition_end = -1.0,
+ transition_type = 0,
+ transition_src_signal = 0,
+ transition_dst_signal = 0,
+
+ neutral_colors = {
+ {0.5, 0.5, 0.5}, -- Input 0.
+ {0.5, 0.5, 0.5} -- Input 1.
+ },
+
+ live_signal_num = 0,
+ preview_signal_num = 1
+}
+
+-- Valid values for live_signal_num and preview_signal_num.
+local INPUT0_SIGNAL_NUM = 0
+local INPUT1_SIGNAL_NUM = 1
+local SBS_SIGNAL_NUM = 2
+local STATIC_SIGNAL_NUM = 3
+
+-- Valid values for transition_type. (Cuts are done directly, so they need no entry.)
+local NO_TRANSITION = 0
+local ZOOM_TRANSITION = 1 -- Also for slides.
+local FADE_TRANSITION = 2
+
+-- Last width/height/frame rate for each channel, if we have it.
+-- Note that unlike the values we get from Nageru, the resolution is per
+-- frame and not per field, since we deinterlace.
+local last_resolution = {}
+
+-- Utility function to help creating many similar chains that can differ
+-- in a free set of chosen parameters.
+function make_cartesian_product(parms, callback)
+ return make_cartesian_product_internal(parms, callback, 1, {})
+end
+
+function make_cartesian_product_internal(parms, callback, index, args)
+ if index > #parms then
+ return callback(unpack(args))
+ end
+ local ret = {}
+ for _, value in ipairs(parms[index]) do
+ args[index] = value
+ ret[value] = make_cartesian_product_internal(parms, callback, index + 1, args)
+ end
+ return ret
+end
+
+function make_sbs_input(chain, signal, deint, hq)
+ local input = chain:add_live_input(not deint, deint) -- Override bounce only if not deinterlacing.
+ input:connect_signal(signal)
+
+ local resample_effect = nil
+ local resize_effect = nil
+ if (hq) then
+ resample_effect = chain:add_effect(ResampleEffect.new())
+ else
+ resize_effect = chain:add_effect(ResizeEffect.new())
+ end
+ local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
+
+ local padding_effect = chain:add_effect(IntegralPaddingEffect.new())
+
+ return {
+ input = input,
+ wb_effect = wb_effect,
+ resample_effect = resample_effect,
+ resize_effect = resize_effect,
+ padding_effect = padding_effect
+ }
+end
+
+-- The main live chain.
+function make_sbs_chain(input0_type, input1_type, hq)
+ local chain = EffectChain.new(16, 9)
+
+ local input0 = make_sbs_input(chain, INPUT0_SIGNAL_NUM, input0_type == "livedeint", hq)
+ local input1 = make_sbs_input(chain, INPUT1_SIGNAL_NUM, input1_type == "livedeint", hq)
+
+ input0.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 1.0)
+ input1.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0)
+
+ chain:add_effect(OverlayEffect.new(), input0.padding_effect, input1.padding_effect)
+ chain:finalize(hq)
+
+ return {
+ chain = chain,
+ input0 = input0,
+ input1 = input1
+ }
+end
+
+-- Make all possible combinations of side-by-side chains.
+local sbs_chains = make_cartesian_product({
+ {"live", "livedeint"}, -- input0_type
+ {"live", "livedeint"}, -- input1_type
+ {true, false} -- hq
+}, function(input0_type, input1_type, hq)
+ return make_sbs_chain(input0_type, input1_type, hq)
+end)
+
+function make_fade_input(chain, signal, live, deint, scale)
+ local input, wb_effect, resample_effect, last
+ if live then
+ input = chain:add_live_input(false, deint)
+ input:connect_signal(signal)
+ last = input
+ else
+ input = chain:add_effect(ImageInput.new("bg.jpeg"))
+ last = input
+ end
+
+ -- If we cared about this for the non-main inputs, we would have
+ -- checked hq here and invoked ResizeEffect instead.
+ if scale then
+ resample_effect = chain:add_effect(ResampleEffect.new())
+ last = resample_effect
+ end
+
+ -- Make sure to put the white balance after the scaling (usually more efficient).
+ if live then
+ wb_effect = chain:add_effect(WhiteBalanceEffect.new())
+ last = wb_effect
+ end
+
+ return {
+ input = input,
+ wb_effect = wb_effect,
+ resample_effect = resample_effect,
+ last = last
+ }
+end
+
+-- A chain to fade between two inputs, of which either can be a picture
+-- or a live input. In practice only used live, but we still support the
+-- hq parameter.
+function make_fade_chain(input0_live, input0_deint, input0_scale, input1_live, input1_deint, input1_scale, hq)
+ local chain = EffectChain.new(16, 9)
+
+ local input0 = make_fade_input(chain, INPUT0_SIGNAL_NUM, input0_live, input0_deint, input0_scale)
+ local input1 = make_fade_input(chain, INPUT1_SIGNAL_NUM, input1_live, input1_deint, input1_scale)
+
+ local mix_effect = chain:add_effect(MixEffect.new(), input0.last, input1.last)
+ chain:finalize(hq)
+
+ return {
+ chain = chain,
+ input0 = input0,
+ input1 = input1,
+ mix_effect = mix_effect
+ }
+end
+
+-- Chains to fade between two inputs, in various configurations.
+local fade_chains = make_cartesian_product({
+ {"static", "live", "livedeint"}, -- input0_type
+ {true, false}, -- input0_scale
+ {"static", "live", "livedeint"}, -- input1_type
+ {true, false}, -- input1_scale
+ {true} -- hq
+}, function(input0_type, input0_scale, input1_type, input1_scale, hq)
+ local input0_live = (input0_type ~= "static")
+ local input1_live = (input1_type ~= "static")
+ local input0_deint = (input0_type == "livedeint")
+ local input1_deint = (input1_type == "livedeint")
+ return make_fade_chain(input0_live, input0_deint, input0_scale, input1_live, input1_deint, input1_scale, hq)
+end)