1 -- The theme is what decides what's actually shown on screen, what kind of
2 -- transitions are available (if any), and what kind of inputs there are,
3 -- if any. In general, it drives the entire display logic by creating Movit
4 -- chains, setting their parameters and then deciding which to show when.
6 -- Themes are written in Lua, which reflects a simplified form of the Movit API
7 -- where all the low-level details (such as texture formats) are handled by the
8 -- C++ side and you generally just build chains.
11 transition_start = -2.0,
12 transition_end = -1.0,
14 transition_src_signal = 0,
15 transition_dst_signal = 0,
18 {0.5, 0.5, 0.5}, -- Input 0.
19 {0.5, 0.5, 0.5} -- Input 1.
23 preview_signal_num = 1
26 -- Valid values for live_signal_num and preview_signal_num.
27 local INPUT0_SIGNAL_NUM = 0
28 local INPUT1_SIGNAL_NUM = 1
29 local SBS_SIGNAL_NUM = 2
30 local STATIC_SIGNAL_NUM = 3
32 -- Valid values for transition_type. (Cuts are done directly, so they need no entry.)
33 local NO_TRANSITION = 0
34 local ZOOM_TRANSITION = 1 -- Also for slides.
35 local FADE_TRANSITION = 2
37 -- Last width/height/frame rate for each channel, if we have it.
38 -- Note that unlike the values we get from Nageru, the resolution is per
39 -- frame and not per field, since we deinterlace.
40 local last_resolution = {}
42 -- Utility function to help creating many similar chains that can differ
43 -- in a free set of chosen parameters.
44 function make_cartesian_product(parms, callback)
45 return make_cartesian_product_internal(parms, callback, 1, {})
48 function make_cartesian_product_internal(parms, callback, index, args)
49 if index > #parms then
50 return callback(unpack(args))
53 for _, value in ipairs(parms[index]) do
55 ret[value] = make_cartesian_product_internal(parms, callback, index + 1, args)
60 function make_sbs_input(chain, signal, deint, hq)
61 local input = chain:add_live_input(not deint, deint) -- Override bounce only if not deinterlacing.
62 input:connect_signal(signal)
64 local resample_effect = nil
65 local resize_effect = nil
67 resample_effect = chain:add_effect(ResampleEffect.new())
69 resize_effect = chain:add_effect(ResizeEffect.new())
71 local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
73 local padding_effect = chain:add_effect(IntegralPaddingEffect.new())
77 wb_effect = wb_effect,
78 resample_effect = resample_effect,
79 resize_effect = resize_effect,
80 padding_effect = padding_effect
84 -- The main live chain.
85 function make_sbs_chain(input0_type, input1_type, hq)
86 local chain = EffectChain.new(16, 9)
88 local input0 = make_sbs_input(chain, INPUT0_SIGNAL_NUM, input0_type == "livedeint", hq)
89 local input1 = make_sbs_input(chain, INPUT1_SIGNAL_NUM, input1_type == "livedeint", hq)
91 input0.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 1.0)
92 input1.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0)
94 chain:add_effect(OverlayEffect.new(), input0.padding_effect, input1.padding_effect)
104 -- Make all possible combinations of side-by-side chains.
105 local sbs_chains = make_cartesian_product({
106 {"live", "livedeint"}, -- input0_type
107 {"live", "livedeint"}, -- input1_type
109 }, function(input0_type, input1_type, hq)
110 return make_sbs_chain(input0_type, input1_type, hq)
113 function make_fade_input(chain, signal, live, deint, scale)
114 local input, wb_effect, resample_effect, last
116 input = chain:add_live_input(false, deint)
117 input:connect_signal(signal)
120 input = chain:add_effect(ImageInput.new("bg.jpeg"))
124 -- If we cared about this for the non-main inputs, we would have
125 -- checked hq here and invoked ResizeEffect instead.
127 resample_effect = chain:add_effect(ResampleEffect.new())
128 last = resample_effect
131 -- Make sure to put the white balance after the scaling (usually more efficient).
133 wb_effect = chain:add_effect(WhiteBalanceEffect.new())
139 wb_effect = wb_effect,
140 resample_effect = resample_effect,
145 -- A chain to fade between two inputs, of which either can be a picture
146 -- or a live input. In practice only used live, but we still support the
148 function make_fade_chain(input0_live, input0_deint, input0_scale, input1_live, input1_deint, input1_scale, hq)
149 local chain = EffectChain.new(16, 9)
151 local input0 = make_fade_input(chain, INPUT0_SIGNAL_NUM, input0_live, input0_deint, input0_scale)
152 local input1 = make_fade_input(chain, INPUT1_SIGNAL_NUM, input1_live, input1_deint, input1_scale)
154 local mix_effect = chain:add_effect(MixEffect.new(), input0.last, input1.last)
161 mix_effect = mix_effect
165 -- Chains to fade between two inputs, in various configurations.
166 local fade_chains = make_cartesian_product({
167 {"static", "live", "livedeint"}, -- input0_type
168 {true, false}, -- input0_scale
169 {"static", "live", "livedeint"}, -- input1_type
170 {true, false}, -- input1_scale
172 }, function(input0_type, input0_scale, input1_type, input1_scale, hq)
173 local input0_live = (input0_type ~= "static")
174 local input1_live = (input1_type ~= "static")
175 local input0_deint = (input0_type == "livedeint")
176 local input1_deint = (input1_type == "livedeint")
177 return make_fade_chain(input0_live, input0_deint, input0_scale, input1_live, input1_deint, input1_scale, hq)
180 -- A chain to show a single input on screen.
181 function make_simple_chain(input_deint, input_scale, hq)
182 local chain = EffectChain.new(16, 9)
184 local input = chain:add_live_input(false, input_deint)
185 input:connect_signal(0) -- First input card. Can be changed whenever you want.
187 local resample_effect, resize_effect
190 resample_effect = chain:add_effect(ResampleEffect.new())
192 resize_effect = chain:add_effect(ResizeEffect.new())
196 local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
202 wb_effect = wb_effect,
203 resample_effect = resample_effect,
204 resize_effect = resize_effect
208 -- Make all possible combinations of single-input chains.
209 local simple_chains = make_cartesian_product({
210 {"live", "livedeint"}, -- input_type
211 {true, false}, -- input_scale
213 }, function(input_type, input_scale, hq)
214 local input_deint = (input_type == "livedeint")
215 return make_simple_chain(input_deint, input_scale, hq)
218 -- A chain to show a single static picture on screen (HQ version).
219 local static_chain_hq = EffectChain.new(16, 9)
220 local static_chain_hq_input = static_chain_hq:add_effect(ImageInput.new("bg.jpeg"))
221 static_chain_hq:finalize(true)
223 -- A chain to show a single static picture on screen (LQ version).
224 local static_chain_lq = EffectChain.new(16, 9)
225 local static_chain_lq_input = static_chain_lq:add_effect(ImageInput.new("bg.jpeg"))
226 static_chain_lq:finalize(false)
228 -- Used for indexing into the tables of chains.
229 function get_input_type(signals, signal_num)
230 if signal_num == STATIC_SIGNAL_NUM then
232 elseif signals:get_interlaced(signal_num) then
239 function needs_scale(signals, signal_num, width, height)
240 if signal_num == STATIC_SIGNAL_NUM then
241 -- We assume this is already correctly scaled at load time.
244 assert(is_plain_signal(signal_num))
245 return (signals:get_width(signal_num) ~= width or signals:get_height(signal_num) ~= height)
248 function set_scale_parameters_if_needed(chain_or_input, width, height)
249 if chain_or_input.resample_effect then
250 chain_or_input.resample_effect:set_int("width", width)
251 chain_or_input.resample_effect:set_int("height", height)
252 elseif chain_or_input.resize_effect then
253 chain_or_input.resize_effect:set_int("width", width)
254 chain_or_input.resize_effect:set_int("height", height)
259 -- Returns the number of outputs in addition to the live (0) and preview (1).
260 -- Called only once, at the start of the program.
261 function num_channels()
265 function is_plain_signal(num)
266 return num == INPUT0_SIGNAL_NUM or num == INPUT1_SIGNAL_NUM
269 -- Helper function to write e.g. “720p60”. The difference between this
270 -- and get_channel_resolution_raw() is that this one also can say that
271 -- there's no signal.
272 function get_channel_resolution(signal_num)
273 local res = last_resolution[signal_num]
274 if (not res) or not res.is_connected then
275 return "disconnected"
277 if res.height <= 0 then
280 if not res.has_signal then
281 if res.height == 525 then
282 -- Special mode for the USB3 cards.
285 return get_channel_resolution_raw(res) .. ", no signal"
287 return get_channel_resolution_raw(res)
291 -- Helper function to write e.g. “60” or “59.94”.
292 function get_frame_rate(res)
293 local nom = res.frame_rate_nom
294 local den = res.frame_rate_den
295 if nom % den == 0 then
298 return string.format("%.2f", nom / den)
302 -- Helper function to write e.g. “720p60”.
303 function get_channel_resolution_raw(res)
304 if res.interlaced then
305 return res.height .. "i" .. get_frame_rate(res)
307 return res.height .. "p" .. get_frame_rate(res)
312 -- Returns the name for each additional channel (starting from 2).
313 -- Called at the start of the program, and then each frame for live
314 -- channels in case they change resolution.
315 function channel_name(channel)
316 local signal_num = channel - 2
317 if is_plain_signal(signal_num) then
318 return "Input " .. (signal_num + 1) .. " (" .. get_channel_resolution(signal_num) .. ")"
319 elseif signal_num == SBS_SIGNAL_NUM then
320 return "Side-by-side"
321 elseif signal_num == STATIC_SIGNAL_NUM then
322 return "Static picture"
327 -- Returns, given a channel number, which signal it corresponds to (starting from 0).
328 -- Should return -1 if the channel does not correspond to a simple signal
329 -- (one connected to a capture card, or a video input). The information is used for
330 -- whether right-click on the channel should bring up a context menu or not,
331 -- typically containing an input selector, resolution menu etc.
333 -- Called once for each channel, at the start of the program.
334 -- Will never be called for live (0) or preview (1).
335 function channel_signal(channel)
338 elseif channel == 3 then
346 -- Called every frame. Returns the color (if any) to paint around the given
347 -- channel. Returns a CSS color (typically to mark live and preview signals);
348 -- "transparent" is allowed.
349 -- Will never be called for live (0) or preview (1).
350 function channel_color(channel)
351 if state.transition_type ~= NO_TRANSITION then
352 if channel_involved_in(channel, state.transition_src_signal) or
353 channel_involved_in(channel, state.transition_dst_signal) then
357 if channel_involved_in(channel, state.live_signal_num) then
361 if channel_involved_in(channel, state.preview_signal_num) then
367 function channel_involved_in(channel, signal_num)
368 if is_plain_signal(signal_num) then
369 return channel == (signal_num + 2)
371 if signal_num == SBS_SIGNAL_NUM then
372 return (channel == 2 or channel == 3)
374 if signal_num == STATIC_SIGNAL_NUM then
375 return (channel == 5)
381 -- Returns if a given channel supports setting white balance (starting from 2).
382 -- Called only once for each channel, at the start of the program.
383 function supports_set_wb(channel)
384 return is_plain_signal(channel - 2)
388 -- Gets called with a new gray point when the white balance is changing.
389 -- The color is in linear light (not sRGB gamma).
390 function set_wb(channel, red, green, blue)
391 if is_plain_signal(channel - 2) then
392 state.neutral_colors[channel - 2 + 1] = { red, green, blue }
396 function finish_transitions(t)
397 if state.transition_type ~= NO_TRANSITION and t >= state.transition_end then
398 state.live_signal_num = state.transition_dst_signal
399 state.transition_type = NO_TRANSITION
403 function in_transition(t)
404 return t >= state.transition_start and t <= state.transition_end
408 -- Called every frame.
409 function get_transitions(t)
410 if in_transition(t) then
411 -- Transition already in progress, the only thing we can do is really
412 -- cut to the preview. (TODO: Make an “abort” and/or “finish”, too?)
416 finish_transitions(t)
418 if state.live_signal_num == state.preview_signal_num then
419 -- No transitions possible.
423 if (is_plain_signal(state.live_signal_num) or state.live_signal_num == STATIC_SIGNAL_NUM) and
424 (is_plain_signal(state.preview_signal_num) or state.preview_signal_num == STATIC_SIGNAL_NUM) then
425 return {"Cut", "", "Fade"}
429 if state.live_signal_num == SBS_SIGNAL_NUM and is_plain_signal(state.preview_signal_num) then
430 return {"Cut", "Zoom in"}
431 elseif is_plain_signal(state.live_signal_num) and state.preview_signal_num == SBS_SIGNAL_NUM then
432 return {"Cut", "Zoom out"}
438 function swap_preview_live()
439 local temp = state.live_signal_num
440 state.live_signal_num = state.preview_signal_num
441 state.preview_signal_num = temp
444 function start_transition(type_, t, duration)
445 state.transition_start = t
446 state.transition_end = t + duration
447 state.transition_type = type_
448 state.transition_src_signal = state.live_signal_num
449 state.transition_dst_signal = state.preview_signal_num
454 -- Called when the user clicks a transition button.
455 function transition_clicked(num, t)
458 if in_transition(t) then
459 -- Ongoing transition; finish it immediately before the cut.
460 finish_transitions(state.transition_end)
466 finish_transitions(t)
468 if state.live_signal_num == state.preview_signal_num then
473 if is_plain_signal(state.live_signal_num) and is_plain_signal(state.preview_signal_num) then
474 -- We can't zoom between these. Just make a cut.
475 io.write("Cutting from " .. state.live_signal_num .. " to " .. state.live_signal_num .. "\n")
480 if (state.live_signal_num == SBS_SIGNAL_NUM and is_plain_signal(state.preview_signal_num)) or
481 (state.preview_signal_num == SBS_SIGNAL_NUM and is_plain_signal(state.live_signal_num)) then
482 start_transition(ZOOM_TRANSITION, t, 1.0)
485 finish_transitions(t)
488 if (state.live_signal_num ~= state.preview_signal_num) and
489 (is_plain_signal(state.live_signal_num) or
490 state.live_signal_num == STATIC_SIGNAL_NUM) and
491 (is_plain_signal(state.preview_signal_num) or
492 state.preview_signal_num == STATIC_SIGNAL_NUM) then
493 start_transition(FADE_TRANSITION, t, 1.0)
495 -- Fades involving SBS are ignored (we have no chain for it).
501 function channel_clicked(num)
502 state.preview_signal_num = num
505 function get_fade_chain(state, signals, t, width, height, input_resolution)
506 local input0_type = get_input_type(signals, state.transition_src_signal)
507 local input0_scale = needs_scale(signals, state.transition_src_signal, width, height)
508 local input1_type = get_input_type(signals, state.transition_dst_signal)
509 local input1_scale = needs_scale(signals, state.transition_dst_signal, width, height)
510 local chain = fade_chains[input0_type][input0_scale][input1_type][input1_scale][true]
511 local prepare = function()
512 if input0_type == "live" or input0_type == "livedeint" then
513 chain.input0.input:connect_signal(state.transition_src_signal)
514 set_neutral_color_from_signal(state, chain.input0.wb_effect, state.transition_src_signal)
516 set_scale_parameters_if_needed(chain.input0, width, height)
517 if input1_type == "live" or input1_type == "livedeint" then
518 chain.input1.input:connect_signal(state.transition_dst_signal)
519 set_neutral_color_from_signal(state, chain.input1.wb_effect, state.transition_dst_signal)
521 set_scale_parameters_if_needed(chain.input1, width, height)
522 local tt = calc_fade_progress(t, state.transition_start, state.transition_end)
524 chain.mix_effect:set_float("strength_first", 1.0 - tt)
525 chain.mix_effect:set_float("strength_second", tt)
527 return chain.chain, prepare
530 -- SBS code (live_signal_num == SBS_SIGNAL_NUM, or in a transition to/from it).
531 function get_sbs_chain(signals, t, width, height, input_resolution)
532 local input0_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
533 local input1_type = get_input_type(signals, INPUT1_SIGNAL_NUM)
534 return sbs_chains[input0_type][input1_type][true]
538 -- Called every frame. Get the chain for displaying at input <num>,
539 -- where 0 is live, 1 is preview, 2 is the first channel to display
540 -- in the bottom bar, and so on up to num_channels()+1. t is the
541 -- current time in seconds. width and height are the dimensions of
542 -- the output, although you can ignore them if you don't need them
543 -- (they're useful if you want to e.g. know what to resample by).
545 -- <signals> is basically an exposed InputState, which you can use to
546 -- query for information about the signals at the point of the current
547 -- frame. In particular, you can call get_width() and get_height()
548 -- for any signal number, and use that to e.g. assist in chain selection.
550 -- You should return two objects; the chain itself, and then a
551 -- function (taking no parameters) that is run just before rendering.
552 -- The function needs to call connect_signal on any inputs, so that
553 -- it gets updated video data for the given frame. (You are allowed
554 -- to switch which input your input is getting from between frames,
555 -- but not calling connect_signal results in undefined behavior.)
556 -- If you want to change any parameters in the chain, this is also
559 -- NOTE: The chain returned must be finalized with the Y'CbCr flag
560 -- if and only if num==0.
561 function get_chain(num, t, width, height, signals)
562 local input_resolution = {}
563 for signal_num=0,1 do
565 width = signals:get_width(signal_num),
566 height = signals:get_height(signal_num),
567 interlaced = signals:get_interlaced(signal_num),
568 is_connected = signals:get_is_connected(signal_num),
569 has_signal = signals:get_has_signal(signal_num),
570 frame_rate_nom = signals:get_frame_rate_nom(signal_num),
571 frame_rate_den = signals:get_frame_rate_den(signal_num)
574 if res.interlaced then
575 -- Convert height from frame height to field height.
576 -- (Needed for e.g. place_rectangle.)
577 res.height = res.height * 2
579 -- Show field rate instead of frame rate; really for cosmetics only
580 -- (and actually contrary to EBU recommendations, although in line
581 -- with typical user expectations).
582 res.frame_rate_nom = res.frame_rate_nom * 2
585 input_resolution[signal_num] = res
587 last_resolution = input_resolution
589 -- Make a (semi-shallow) copy of the current state, so that the returned prepare function
590 -- is unaffected by state changes made by the UI before it is rendered.
591 local state_copy = {}
592 for key, value in pairs(state) do
593 state_copy[key] = value
595 state_copy.neutral_colors = { unpack(state.neutral_colors) }
597 if num == 0 then -- Live.
598 finish_transitions(t)
599 if state.transition_type == ZOOM_TRANSITION then
600 -- Transition in or out of SBS.
601 local chain = get_sbs_chain(signals, t, width, height, input_resolution)
602 local prepare = function()
603 prepare_sbs_chain(state_copy, chain, calc_zoom_progress(state_copy, t), state_copy.transition_type, state_copy.transition_src_signal, state_copy.transition_dst_signal, width, height, input_resolution)
605 return chain.chain, prepare
606 elseif state.transition_type == NO_TRANSITION and state.live_signal_num == SBS_SIGNAL_NUM then
608 local chain = get_sbs_chain(signals, t, width, height, input_resolution)
609 local prepare = function()
610 prepare_sbs_chain(state_copy, chain, 0.0, NO_TRANSITION, 0, SBS_SIGNAL_NUM, width, height, input_resolution)
612 return chain.chain, prepare
613 elseif state.transition_type == FADE_TRANSITION then
614 return get_fade_chain(state_copy, signals, t, width, height, input_resolution)
615 elseif is_plain_signal(state.live_signal_num) then
616 local input_type = get_input_type(signals, state.live_signal_num)
617 local input_scale = needs_scale(signals, state.live_signal_num, width, height)
618 local chain = simple_chains[input_type][input_scale][true]
619 local prepare = function()
620 chain.input:connect_signal(state_copy.live_signal_num)
621 set_scale_parameters_if_needed(chain, width, height)
622 set_neutral_color_from_signal(state_copy, chain.wb_effect, state_copy.live_signal_num)
624 return chain.chain, prepare
625 elseif state.live_signal_num == STATIC_SIGNAL_NUM then -- Static picture.
626 local prepare = function()
628 return static_chain_hq, prepare
633 if num == 1 then -- Preview.
634 num = state.preview_signal_num + 2
637 -- Individual preview inputs.
638 if is_plain_signal(num - 2) then
639 local signal_num = num - 2
640 local input_type = get_input_type(signals, signal_num)
641 local input_scale = needs_scale(signals, signal_num, width, height)
642 local chain = simple_chains[input_type][input_scale][false]
643 local prepare = function()
644 chain.input:connect_signal(signal_num)
645 set_scale_parameters_if_needed(chain, width, height)
646 set_neutral_color(chain.wb_effect, state_copy.neutral_colors[signal_num + 1])
648 return chain.chain, prepare
650 if num == SBS_SIGNAL_NUM + 2 then
651 local input0_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
652 local input1_type = get_input_type(signals, INPUT1_SIGNAL_NUM)
653 local chain = sbs_chains[input0_type][input1_type][false]
654 local prepare = function()
655 prepare_sbs_chain(state_copy, chain, 0.0, NO_TRANSITION, 0, SBS_SIGNAL_NUM, width, height, input_resolution)
657 return chain.chain, prepare
659 if num == STATIC_SIGNAL_NUM + 2 then
660 local prepare = function()
662 return static_chain_lq, prepare
666 function place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height)
672 padding_effect:set_int("width", screen_width)
673 padding_effect:set_int("height", screen_height)
676 if x0 > screen_width or x1 < 0.0 or y0 > screen_height or y1 < 0.0 then
677 if resample_effect ~= nil then
678 resample_effect:set_int("width", 1)
679 resample_effect:set_int("height", 1)
680 resample_effect:set_float("zoom_x", screen_width)
681 resample_effect:set_float("zoom_y", screen_height)
683 resize_effect:set_int("width", 1)
684 resize_effect:set_int("height", 1)
686 padding_effect:set_int("left", screen_width + 100)
687 padding_effect:set_int("top", screen_height + 100)
693 srcx0 = -x0 / (x1 - x0)
697 srcy0 = -y0 / (y1 - y0)
700 if x1 > screen_width then
701 srcx1 = (screen_width - x0) / (x1 - x0)
704 if y1 > screen_height then
705 srcy1 = (screen_height - y0) / (y1 - y0)
709 if resample_effect ~= nil then
710 -- High-quality resampling.
711 local x_subpixel_offset = x0 - math.floor(x0)
712 local y_subpixel_offset = y0 - math.floor(y0)
714 -- Resampling must be to an integral number of pixels. Round up,
715 -- and then add an extra pixel so we have some leeway for the border.
716 local width = math.ceil(x1 - x0) + 1
717 local height = math.ceil(y1 - y0) + 1
718 resample_effect:set_int("width", width)
719 resample_effect:set_int("height", height)
721 -- Correct the discrepancy with zoom. (This will leave a small
722 -- excess edge of pixels and subpixels, which we'll correct for soon.)
723 local zoom_x = (x1 - x0) / (width * (srcx1 - srcx0))
724 local zoom_y = (y1 - y0) / (height * (srcy1 - srcy0))
725 resample_effect:set_float("zoom_x", zoom_x)
726 resample_effect:set_float("zoom_y", zoom_y)
727 resample_effect:set_float("zoom_center_x", 0.0)
728 resample_effect:set_float("zoom_center_y", 0.0)
730 -- Padding must also be to a whole-pixel offset.
731 padding_effect:set_int("left", math.floor(x0))
732 padding_effect:set_int("top", math.floor(y0))
734 -- Correct _that_ discrepancy by subpixel offset in the resampling.
735 resample_effect:set_float("left", srcx0 * input_width - x_subpixel_offset / zoom_x)
736 resample_effect:set_float("top", srcy0 * input_height - y_subpixel_offset / zoom_y)
738 -- Finally, adjust the border so it is exactly where we want it.
739 padding_effect:set_float("border_offset_left", x_subpixel_offset)
740 padding_effect:set_float("border_offset_right", x1 - (math.floor(x0) + width))
741 padding_effect:set_float("border_offset_top", y_subpixel_offset)
742 padding_effect:set_float("border_offset_bottom", y1 - (math.floor(y0) + height))
744 -- Lower-quality simple resizing.
745 local width = round(x1 - x0)
746 local height = round(y1 - y0)
747 resize_effect:set_int("width", width)
748 resize_effect:set_int("height", height)
750 -- Padding must also be to a whole-pixel offset.
751 padding_effect:set_int("left", math.floor(x0))
752 padding_effect:set_int("top", math.floor(y0))
756 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
758 return math.floor(x + 0.5)
761 function lerp(a, b, t)
762 return a + (b - a) * t
765 function lerp_pos(a, b, t)
767 x0 = lerp(a.x0, b.x0, t),
768 y0 = lerp(a.y0, b.y0, t),
769 x1 = lerp(a.x1, b.x1, t),
770 y1 = lerp(a.y1, b.y1, t)
774 function pos_from_top_left(x, y, width, height, screen_width, screen_height)
775 local xs = screen_width / 1280.0
776 local ys = screen_height / 720.0
780 x1 = round(xs * (x + width)),
781 y1 = round(ys * (y + height))
785 function prepare_sbs_chain(state, chain, t, transition_type, src_signal, dst_signal, screen_width, screen_height, input_resolution)
786 chain.input0.input:connect_signal(0)
787 chain.input1.input:connect_signal(1)
788 set_neutral_color(chain.input0.wb_effect, state.neutral_colors[1])
789 set_neutral_color(chain.input1.wb_effect, state.neutral_colors[2])
791 -- First input is positioned (16,48) from top-left.
792 -- Second input is positioned (16,48) from the bottom-right.
793 local pos0 = pos_from_top_left(16, 48, 848, 477, screen_width, screen_height)
794 local pos1 = pos_from_top_left(1280 - 384 - 16, 720 - 216 - 48, 384, 216, screen_width, screen_height)
796 local pos_fs = { x0 = 0, y0 = 0, x1 = screen_width, y1 = screen_height }
798 if transition_type == NO_TRANSITION then
800 affine_param = { sx = 1.0, sy = 1.0, tx = 0.0, ty = 0.0 } -- Identity.
802 -- Zooming to/from SBS view into or out of a single view.
803 assert(transition_type == ZOOM_TRANSITION)
805 if src_signal == SBS_SIGNAL_NUM then
809 assert(dst_signal == SBS_SIGNAL_NUM)
814 if signal == INPUT0_SIGNAL_NUM then
815 affine_param = find_affine_param(pos0, lerp_pos(pos0, pos_fs, real_t))
816 elseif signal == INPUT1_SIGNAL_NUM then
817 affine_param = find_affine_param(pos1, lerp_pos(pos1, pos_fs, real_t))
821 -- NOTE: input_resolution is not 1-indexed, unlike usual Lua arrays.
822 place_rectangle_with_affine(chain.input0.resample_effect, chain.input0.resize_effect, chain.input0.padding_effect, pos0, affine_param, screen_width, screen_height, input_resolution[0].width, input_resolution[0].height)
823 place_rectangle_with_affine(chain.input1.resample_effect, chain.input1.resize_effect, chain.input1.padding_effect, pos1, affine_param, screen_width, screen_height, input_resolution[1].width, input_resolution[1].height)
826 -- Find the transformation that changes the first rectangle to the second one.
827 function find_affine_param(a, b)
828 local sx = (b.x1 - b.x0) / (a.x1 - a.x0)
829 local sy = (b.y1 - b.y0) / (a.y1 - a.y0)
833 tx = b.x0 - a.x0 * sx,
834 ty = b.y0 - a.y0 * sy
838 function place_rectangle_with_affine(resample_effect, resize_effect, padding_effect, pos, aff, screen_width, screen_height, input_width, input_height)
839 local x0 = pos.x0 * aff.sx + aff.tx
840 local x1 = pos.x1 * aff.sx + aff.tx
841 local y0 = pos.y0 * aff.sy + aff.ty
842 local y1 = pos.y1 * aff.sy + aff.ty
844 place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height)
847 function set_neutral_color(effect, color)
848 effect:set_vec3("neutral_color", color[1], color[2], color[3])
851 function set_neutral_color_from_signal(state, effect, signal)
852 if is_plain_signal(signal) then
853 set_neutral_color(effect, state.neutral_colors[signal - INPUT0_SIGNAL_NUM + 1])
857 function calc_zoom_progress(state, t)
858 if t < state.transition_start then
860 elseif t > state.transition_end then
863 local tt = (t - state.transition_start) / (state.transition_end - state.transition_start)
865 return math.sin(tt * 3.14159265358 * 0.5)
869 function calc_fade_progress(t, transition_start, transition_end)
870 local tt = (t - transition_start) / (transition_end - transition_start)
877 -- Make the fade look maybe a tad more natural, by pumping it
878 -- through a sigmoid function.
880 tt = 1.0 / (1.0 + math.exp(-tt))