+
+local transition_start = -2.0
+local transition_end = -1.0
+local zoom_src = 0.0
+local zoom_dst = 1.0
+local zoom_poi = 0 -- which input to zoom in on
+local fade_src_signal = 0
+local fade_dst_signal = 0
+
+local input0_neutral_color = {0.5, 0.5, 0.5}
+local input1_neutral_color = {0.5, 0.5, 0.5}
+
+local live_signal_num = 0
+local 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
+
+-- “fake” signal number that signifies that we are fading from one input
+-- to the next.
+local FADE_SIGNAL_NUM = 4
+
+-- Last width/height/resolution for each channel, if we have it.
+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 wb_effect = chain:add_effect(WhiteBalanceEffect.new())
+
+ 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 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_deint, input1_deint, hq)
+ local chain = EffectChain.new(16, 9)
+
+ local input0 = make_sbs_input(chain, INPUT0_SIGNAL_NUM, input0_deint, hq)
+ local input1 = make_sbs_input(chain, INPUT1_SIGNAL_NUM, input1_deint, 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)
+ local input0_deint = (input0_type == "livedeint")
+ local input1_deint = (input1_type == "livedeint")
+ return make_sbs_chain(input0_deint, input1_deint, 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)
+ wb_effect = chain:add_effect(WhiteBalanceEffect.new())
+ input:connect_signal(signal)
+ last = wb_effect
+ 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
+
+ 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)
+
+-- A chain to show a single input on screen.
+function make_simple_chain(input_deint, input_scale, hq)
+ local chain = EffectChain.new(16, 9)
+
+ local input = chain:add_live_input(false, input_deint)
+ input:connect_signal(0) -- First input card. Can be changed whenever you want.
+ local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
+ chain:finalize(hq)
+
+ local resample_effect, resize_effect
+ if scale then
+ if hq then
+ resample_effect = chain:add_effect(ResampleEffect.new())
+ else
+ resize_effect = chain:add_effect(ResizeEffect.new())
+ end
+ end
+
+ return {
+ chain = chain,
+ input = input,
+ wb_effect = wb_effect,
+ resample_effect = resample_effect,
+ resize_effect = resize_effect
+ }
+end
+
+-- Make all possible combinations of single-input chains.
+local simple_chains = make_cartesian_product({
+ {"live", "livedeint"}, -- input_type
+ {true, false}, -- input_scale
+ {true, false} -- hq
+}, function(input_type, input_scale, hq)
+ local input_deint = (input_type == "livedeint")
+ return make_simple_chain(input_deint, input_scale, hq)
+end)
+
+-- A chain to show a single static picture on screen (HQ version).
+local static_chain_hq = EffectChain.new(16, 9)
+local static_chain_hq_input = static_chain_hq:add_effect(ImageInput.new("bg.jpeg"))
+static_chain_hq:finalize(true)
+
+-- A chain to show a single static picture on screen (LQ version).
+local static_chain_lq = EffectChain.new(16, 9)
+local static_chain_lq_input = static_chain_lq:add_effect(ImageInput.new("bg.jpeg"))
+static_chain_lq:finalize(false)
+
+-- Used for indexing into the tables of chains.
+function get_input_type(signals, signal_num)
+ if signal_num == STATIC_SIGNAL_NUM then
+ return "static"
+ elseif signals:get_interlaced(signal_num) then
+ return "livedeint"
+ else
+ return "live"
+ end
+end
+
+function needs_scale(signals, signal_num, width, height)
+ return (signals:get_width(signal_num) ~= width or signals:get_height(signal_num) ~= height)
+end
+
+function set_scale_parameters_if_needed(chain_or_input, width, height)
+ if chain_or_input.resample_effect then
+ chain_or_input.resample_effect:set_int("width", width)
+ chain_or_input.resample_effect:set_int("height", height)
+ elseif chain_or_input.resize_effect then
+ chain_or_input.resize_effect:set_int("width", width)
+ chain_or_input.resize_effect:set_int("height", height)
+ end
+end