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.
10 local transition_start = -2.0
11 local transition_end = -1.0
14 local zoom_poi = 0 -- which input to zoom in on
15 local fade_src_signal = 0
16 local fade_dst_signal = 0
18 local input0_neutral_color = {0.5, 0.5, 0.5}
19 local input1_neutral_color = {0.5, 0.5, 0.5}
21 local live_signal_num = 0
22 local preview_signal_num = 1
24 -- Valid values for live_signal_num and preview_signal_num.
25 local INPUT0_SIGNAL_NUM = 0
26 local INPUT1_SIGNAL_NUM = 1
27 local SBS_SIGNAL_NUM = 2
28 local STATIC_SIGNAL_NUM = 3
30 -- “fake” signal number that signifies that we are fading from one input
32 local FADE_SIGNAL_NUM = 4
34 -- Last width/height/frame rate for each channel, if we have it.
35 -- Note that unlike the values we get from Nageru, the resolution is per
36 -- frame and not per field, since we deinterlace.
37 local last_resolution = {}
39 -- Utility function to help creating many similar chains that can differ
40 -- in a free set of chosen parameters.
41 function make_cartesian_product(parms, callback)
42 return make_cartesian_product_internal(parms, callback, 1, {})
45 function make_cartesian_product_internal(parms, callback, index, args)
46 if index > #parms then
47 return callback(unpack(args))
50 for _, value in ipairs(parms[index]) do
52 ret[value] = make_cartesian_product_internal(parms, callback, index + 1, args)
57 function make_sbs_input(chain, signal, deint, hq)
58 local input = chain:add_live_input(not deint, deint) -- Override bounce only if not deinterlacing.
59 input:connect_signal(signal)
61 local resample_effect = nil
62 local resize_effect = nil
64 resample_effect = chain:add_effect(ResampleEffect.new())
66 resize_effect = chain:add_effect(ResizeEffect.new())
68 local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
70 local padding_effect = chain:add_effect(IntegralPaddingEffect.new())
74 wb_effect = wb_effect,
75 resample_effect = resample_effect,
76 resize_effect = resize_effect,
77 padding_effect = padding_effect
81 -- The main live chain.
82 function make_sbs_chain(input0_deint, input1_deint, hq)
83 local chain = EffectChain.new(16, 9)
85 local input0 = make_sbs_input(chain, INPUT0_SIGNAL_NUM, input0_deint, hq)
86 local input1 = make_sbs_input(chain, INPUT1_SIGNAL_NUM, input1_deint, hq)
88 input0.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 1.0)
89 input1.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0)
91 chain:add_effect(OverlayEffect.new(), input0.padding_effect, input1.padding_effect)
101 -- Make all possible combinations of side-by-side chains.
102 local sbs_chains = make_cartesian_product({
103 {"live", "livedeint"}, -- input0_type
104 {"live", "livedeint"}, -- input1_type
106 }, function(input0_type, input1_type, hq)
107 local input0_deint = (input0_type == "livedeint")
108 local input1_deint = (input1_type == "livedeint")
109 return make_sbs_chain(input0_deint, input1_deint, hq)
112 function make_fade_input(chain, signal, live, deint, scale)
113 local input, wb_effect, resample_effect, last
115 input = chain:add_live_input(false, deint)
116 input:connect_signal(signal)
119 input = chain:add_effect(ImageInput.new("bg.jpeg"))
123 -- If we cared about this for the non-main inputs, we would have
124 -- checked hq here and invoked ResizeEffect instead.
126 resample_effect = chain:add_effect(ResampleEffect.new())
127 last = resample_effect
130 -- Make sure to put the white balance after the scaling (usually more efficient).
132 wb_effect = chain:add_effect(WhiteBalanceEffect.new())
138 wb_effect = wb_effect,
139 resample_effect = resample_effect,
144 -- A chain to fade between two inputs, of which either can be a picture
145 -- or a live input. In practice only used live, but we still support the
147 function make_fade_chain(input0_live, input0_deint, input0_scale, input1_live, input1_deint, input1_scale, hq)
148 local chain = EffectChain.new(16, 9)
150 local input0 = make_fade_input(chain, INPUT0_SIGNAL_NUM, input0_live, input0_deint, input0_scale)
151 local input1 = make_fade_input(chain, INPUT1_SIGNAL_NUM, input1_live, input1_deint, input1_scale)
153 local mix_effect = chain:add_effect(MixEffect.new(), input0.last, input1.last)
160 mix_effect = mix_effect
164 -- Chains to fade between two inputs, in various configurations.
165 local fade_chains = make_cartesian_product({
166 {"static", "live", "livedeint"}, -- input0_type
167 {true, false}, -- input0_scale
168 {"static", "live", "livedeint"}, -- input1_type
169 {true, false}, -- input1_scale
171 }, function(input0_type, input0_scale, input1_type, input1_scale, hq)
172 local input0_live = (input0_type ~= "static")
173 local input1_live = (input1_type ~= "static")
174 local input0_deint = (input0_type == "livedeint")
175 local input1_deint = (input1_type == "livedeint")
176 return make_fade_chain(input0_live, input0_deint, input0_scale, input1_live, input1_deint, input1_scale, hq)
179 -- A chain to show a single input on screen.
180 function make_simple_chain(input_deint, input_scale, hq)
181 local chain = EffectChain.new(16, 9)
183 local input = chain:add_live_input(false, input_deint)
184 input:connect_signal(0) -- First input card. Can be changed whenever you want.
186 local resample_effect, resize_effect
189 resample_effect = chain:add_effect(ResampleEffect.new())
191 resize_effect = chain:add_effect(ResizeEffect.new())
195 local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
201 wb_effect = wb_effect,
202 resample_effect = resample_effect,
203 resize_effect = resize_effect
207 -- Make all possible combinations of single-input chains.
208 local simple_chains = make_cartesian_product({
209 {"live", "livedeint"}, -- input_type
210 {true, false}, -- input_scale
212 }, function(input_type, input_scale, hq)
213 local input_deint = (input_type == "livedeint")
214 return make_simple_chain(input_deint, input_scale, hq)
217 -- A chain to show a single static picture on screen (HQ version).
218 local static_chain_hq = EffectChain.new(16, 9)
219 local static_chain_hq_input = static_chain_hq:add_effect(ImageInput.new("bg.jpeg"))
220 static_chain_hq:finalize(true)
222 -- A chain to show a single static picture on screen (LQ version).
223 local static_chain_lq = EffectChain.new(16, 9)
224 local static_chain_lq_input = static_chain_lq:add_effect(ImageInput.new("bg.jpeg"))
225 static_chain_lq:finalize(false)
227 -- Used for indexing into the tables of chains.
228 function get_input_type(signals, signal_num)
229 if signal_num == STATIC_SIGNAL_NUM then
231 elseif signals:get_interlaced(signal_num) then
238 function needs_scale(signals, signal_num, width, height)
239 if signal_num == STATIC_SIGNAL_NUM then
240 -- We assume this is already correctly scaled at load time.
243 assert(is_plain_signal(signal_num))
244 return (signals:get_width(signal_num) ~= width or signals:get_height(signal_num) ~= height)
247 function set_scale_parameters_if_needed(chain_or_input, width, height)
248 if chain_or_input.resample_effect then
249 chain_or_input.resample_effect:set_int("width", width)
250 chain_or_input.resample_effect:set_int("height", height)
251 elseif chain_or_input.resize_effect then
252 chain_or_input.resize_effect:set_int("width", width)
253 chain_or_input.resize_effect:set_int("height", height)
258 -- Returns the number of outputs in addition to the live (0) and preview (1).
259 -- Called only once, at the start of the program.
260 function num_channels()
264 function is_plain_signal(num)
265 return num == INPUT0_SIGNAL_NUM or num == INPUT1_SIGNAL_NUM
268 -- Helper function to write e.g. “720p60”. The difference between this
269 -- and get_channel_resolution_raw() is that this one also can say that
270 -- there's no signal.
271 function get_channel_resolution(signal_num)
272 res = last_resolution[signal_num]
273 if (not res) or res.height <= 0 then
276 if not res.has_signal then
277 if res.height == 525 then
278 -- Special mode for the USB3 cards.
281 return get_channel_resolution_raw(res) .. ", no signal"
283 return get_channel_resolution_raw(res)
287 -- Helper function to write e.g. “60” or “59.94”.
288 function get_frame_rate(res)
289 local nom = res.frame_rate_nom
290 local den = res.frame_rate_den
291 if nom % den == 0 then
294 return string.format("%.2f", nom / den)
298 -- Helper function to write e.g. “720p60”.
299 function get_channel_resolution_raw(res)
300 if res.interlaced then
301 return res.height .. "i" .. get_frame_rate(res)
303 return res.height .. "p" .. get_frame_rate(res)
308 -- Returns the name for each additional channel (starting from 2).
309 -- Called at the start of the program, and then each frame for live
310 -- channels in case they change resolution.
311 function channel_name(channel)
313 return "Input 1 (" .. get_channel_resolution(0) .. ")"
314 elseif channel == 3 then
315 return "Input 2 (" .. get_channel_resolution(1) .. ")"
316 elseif channel == 4 then
317 return "Side-by-side"
318 elseif channel == 5 then
319 return "Static picture"
324 -- Returns, given a channel number, which signal it corresponds to (starting from 0).
325 -- Should return -1 if the channel does not correspond to a simple signal.
326 -- (The information is used for whether right-click on the channel should bring up
327 -- an input selector or not.)
328 -- Called once for each channel, at the start of the program.
329 -- Will never be called for live (0) or preview (1).
330 function channel_signal(channel)
333 elseif channel == 3 then
341 -- Called every frame. Returns the color (if any) to paint around the given
342 -- channel. Returns a CSS color (typically to mark live and preview signals);
343 -- "transparent" is allowed.
344 -- Will never be called for live (0) or preview (1).
345 function channel_color(channel)
346 if channel_involved_in(channel, live_signal_num) then
349 if channel_involved_in(channel, preview_signal_num) then
355 function channel_involved_in(channel, signal_num)
356 if is_plain_signal(signal_num) then
357 return channel == (signal_num + 2)
359 if signal_num == SBS_SIGNAL_NUM then
360 return (channel == 2 or channel == 3)
362 if signal_num == STATIC_SIGNAL_NUM then
363 return (channel == 5)
365 if signal_num == FADE_SIGNAL_NUM then
366 return (channel_involved_in(channel, fade_src_signal) or
367 channel_involved_in(channel, fade_dst_signal))
373 -- Returns if a given channel supports setting white balance (starting from 2).
374 -- Called only once for each channel, at the start of the program.
375 function supports_set_wb(channel)
376 return channel == 2 or channel == 3
380 -- Gets called with a new gray point when the white balance is changing.
381 -- The color is in linear light (not sRGB gamma).
382 function set_wb(channel, red, green, blue)
384 input0_neutral_color = { red, green, blue }
385 elseif channel == 3 then
386 input1_neutral_color = { red, green, blue }
390 function finish_transitions(t)
391 -- If live is SBS but de-facto single, make it so.
392 if live_signal_num == SBS_SIGNAL_NUM and t >= transition_end and zoom_dst == 1.0 then
393 live_signal_num = zoom_poi
396 -- If live is fade but de-facto single, make it so.
397 if live_signal_num == FADE_SIGNAL_NUM and t >= transition_end then
398 live_signal_num = fade_dst_signal
403 -- Called every frame.
404 function get_transitions(t)
405 finish_transitions(t)
407 if live_signal_num == preview_signal_num then
408 -- No transitions possible.
412 if live_signal_num == SBS_SIGNAL_NUM and t >= transition_start and t <= transition_end then
417 if (is_plain_signal(live_signal_num) or live_signal_num == STATIC_SIGNAL_NUM) and
418 (is_plain_signal(preview_signal_num) or preview_signal_num == STATIC_SIGNAL_NUM) then
419 return {"Cut", "", "Fade"}
423 if live_signal_num == SBS_SIGNAL_NUM and is_plain_signal(preview_signal_num) then
424 return {"Cut", "Zoom in"}
425 elseif is_plain_signal(live_signal_num) and preview_signal_num == SBS_SIGNAL_NUM then
426 return {"Cut", "Zoom out"}
433 -- Called when the user clicks a transition button.
434 function transition_clicked(num, t)
437 if live_signal_num == FADE_SIGNAL_NUM then
438 -- Ongoing fade; finish it immediately.
439 finish_transitions(transition_end)
442 local temp = live_signal_num
443 live_signal_num = preview_signal_num
444 preview_signal_num = temp
446 if live_signal_num == SBS_SIGNAL_NUM then
447 -- Just cut to SBS, we need to reset any zooms.
450 transition_start = -2.0
451 transition_end = -1.0
456 finish_transitions(t)
458 if live_signal_num == preview_signal_num then
463 if (is_plain_signal(live_signal_num) and is_plain_signal(preview_signal_num)) then
464 -- We can't zoom between these. Just make a cut.
465 io.write("Cutting from " .. live_signal_num .. " to " .. live_signal_num .. "\n")
466 local temp = live_signal_num
467 live_signal_num = preview_signal_num
468 preview_signal_num = temp
472 if live_signal_num == SBS_SIGNAL_NUM and is_plain_signal(preview_signal_num) then
473 -- Zoom in from SBS to single.
475 transition_end = t + 1.0
478 zoom_poi = preview_signal_num
479 preview_signal_num = SBS_SIGNAL_NUM
480 elseif is_plain_signal(live_signal_num) and preview_signal_num == SBS_SIGNAL_NUM then
481 -- Zoom out from single to SBS.
483 transition_end = t + 1.0
486 preview_signal_num = live_signal_num
487 zoom_poi = live_signal_num
488 live_signal_num = SBS_SIGNAL_NUM
491 finish_transitions(t)
494 if (live_signal_num ~= preview_signal_num) and
495 (is_plain_signal(live_signal_num) or
496 live_signal_num == STATIC_SIGNAL_NUM) and
497 (is_plain_signal(preview_signal_num) or
498 preview_signal_num == STATIC_SIGNAL_NUM) then
500 transition_end = t + 1.0
501 fade_src_signal = live_signal_num
502 fade_dst_signal = preview_signal_num
503 preview_signal_num = live_signal_num
504 live_signal_num = FADE_SIGNAL_NUM
506 -- Fades involving SBS are ignored (we have no chain for it).
512 function channel_clicked(num)
513 preview_signal_num = num
517 -- Called every frame. Get the chain for displaying at input <num>,
518 -- where 0 is live, 1 is preview, 2 is the first channel to display
519 -- in the bottom bar, and so on up to num_channels()+1. t is the
520 -- current time in seconds. width and height are the dimensions of
521 -- the output, although you can ignore them if you don't need them
522 -- (they're useful if you want to e.g. know what to resample by).
524 -- <signals> is basically an exposed InputState, which you can use to
525 -- query for information about the signals at the point of the current
526 -- frame. In particular, you can call get_width() and get_height()
527 -- for any signal number, and use that to e.g. assist in chain selection.
529 -- You should return two objects; the chain itself, and then a
530 -- function (taking no parameters) that is run just before rendering.
531 -- The function needs to call connect_signal on any inputs, so that
532 -- it gets updated video data for the given frame. (You are allowed
533 -- to switch which input your input is getting from between frames,
534 -- but not calling connect_signal results in undefined behavior.)
535 -- If you want to change any parameters in the chain, this is also
538 -- NOTE: The chain returned must be finalized with the Y'CbCr flag
539 -- if and only if num==0.
540 function get_chain(num, t, width, height, signals)
541 local input_resolution = {}
542 for signal_num=0,1 do
544 width = signals:get_width(signal_num),
545 height = signals:get_height(signal_num),
546 interlaced = signals:get_interlaced(signal_num),
547 has_signal = signals:get_has_signal(signal_num),
548 frame_rate_nom = signals:get_frame_rate_nom(signal_num),
549 frame_rate_den = signals:get_frame_rate_den(signal_num)
552 if res.interlaced then
553 -- Convert height from frame height to field height.
554 -- (Needed for e.g. place_rectangle.)
555 res.height = res.height * 2
557 -- Show field rate instead of frame rate; really for cosmetics only
558 -- (and actually contrary to EBU recommendations, although in line
559 -- with typical user expectations).
560 res.frame_rate_nom = res.frame_rate_nom * 2
563 input_resolution[signal_num] = res
565 last_resolution = input_resolution
567 if num == 0 then -- Live.
568 if is_plain_signal(live_signal_num) then -- Plain inputs.
569 local input_type = get_input_type(signals, live_signal_num)
570 local input_scale = needs_scale(signals, live_signal_num, width, height)
571 local chain = simple_chains[input_type][input_scale][true]
573 chain.input:connect_signal(live_signal_num)
574 set_scale_parameters_if_needed(chain, width, height)
575 set_neutral_color_from_signal(chain.wb_effect, live_signal_num)
577 return chain.chain, prepare
578 elseif live_signal_num == STATIC_SIGNAL_NUM then -- Static picture.
581 return static_chain_hq, prepare
582 elseif live_signal_num == FADE_SIGNAL_NUM then -- Fade.
583 local input0_type = get_input_type(signals, fade_src_signal)
584 local input0_scale = needs_scale(signals, fade_src_signal, width, height)
585 local input1_type = get_input_type(signals, fade_dst_signal)
586 local input1_scale = needs_scale(signals, fade_dst_signal, width, height)
587 local chain = fade_chains[input0_type][input0_scale][input1_type][input1_scale][true]
589 if input0_type == "live" or input0_type == "livedeint" then
590 chain.input0.input:connect_signal(fade_src_signal)
591 set_neutral_color_from_signal(chain.input0.wb_effect, fade_src_signal)
593 set_scale_parameters_if_needed(chain.input0, width, height)
594 if input1_type == "live" or input1_type == "livedeint" then
595 chain.input1.input:connect_signal(fade_dst_signal)
596 set_neutral_color_from_signal(chain.input1.wb_effect, fade_dst_signal)
598 set_scale_parameters_if_needed(chain.input1, width, height)
599 local tt = calc_fade_progress(t, transition_start, transition_end)
601 chain.mix_effect:set_float("strength_first", 1.0 - tt)
602 chain.mix_effect:set_float("strength_second", tt)
604 return chain.chain, prepare
607 -- SBS code (live_signal_num == SBS_SIGNAL_NUM).
608 local input0_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
609 local input1_type = get_input_type(signals, INPUT1_SIGNAL_NUM)
610 if t > transition_end and zoom_dst == 1.0 then
611 -- Special case: Show only the single image on screen.
612 local input0_scale = needs_scale(signals, fade_src_signal, width, height)
613 local chain = simple_chains[input0_type][input0_scale][true]
615 chain.input:connect_signal(INPUT0_SIGNAL_NUM)
616 set_scale_parameters_if_needed(chain, width, height)
617 set_neutral_color(chain.wb_effect, input0_neutral_color)
619 return chain.chain, prepare
621 local chain = sbs_chains[input0_type][input1_type][true]
623 if t < transition_start then
624 prepare_sbs_chain(chain, zoom_src, width, height, input_resolution)
625 elseif t > transition_end then
626 prepare_sbs_chain(chain, zoom_dst, width, height, input_resolution)
628 local tt = (t - transition_start) / (transition_end - transition_start)
630 tt = math.sin(tt * 3.14159265358 * 0.5)
631 prepare_sbs_chain(chain, zoom_src + (zoom_dst - zoom_src) * tt, width, height, input_resolution)
634 return chain.chain, prepare
636 if num == 1 then -- Preview.
637 num = preview_signal_num + 2
640 -- Individual preview inputs.
641 if num == INPUT0_SIGNAL_NUM + 2 then
642 local input_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
643 local input_scale = needs_scale(signals, INPUT0_SIGNAL_NUM, width, height)
644 local chain = simple_chains[input_type][input_scale][false]
646 chain.input:connect_signal(INPUT0_SIGNAL_NUM)
647 set_scale_parameters_if_needed(chain, width, height)
648 set_neutral_color(chain.wb_effect, input0_neutral_color)
650 return chain.chain, prepare
652 if num == INPUT1_SIGNAL_NUM + 2 then
653 local input_type = get_input_type(signals, INPUT1_SIGNAL_NUM)
654 local input_scale = needs_scale(signals, INPUT1_SIGNAL_NUM, width, height)
655 local chain = simple_chains[input_type][input_scale][false]
657 chain.input:connect_signal(INPUT1_SIGNAL_NUM)
658 set_scale_parameters_if_needed(chain, width, height)
659 set_neutral_color(chain.wb_effect, input1_neutral_color)
661 return chain.chain, prepare
663 if num == SBS_SIGNAL_NUM + 2 then
664 local input0_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
665 local input1_type = get_input_type(signals, INPUT1_SIGNAL_NUM)
666 local chain = sbs_chains[input0_type][input1_type][false]
668 prepare_sbs_chain(chain, 0.0, width, height, input_resolution)
670 return chain.chain, prepare
672 if num == STATIC_SIGNAL_NUM + 2 then
675 return static_chain_lq, prepare
679 function place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height)
685 padding_effect:set_int("width", screen_width)
686 padding_effect:set_int("height", screen_height)
689 if x0 > screen_width or x1 < 0.0 or y0 > screen_height or y1 < 0.0 then
690 if resample_effect ~= nil then
691 resample_effect:set_int("width", 1)
692 resample_effect:set_int("height", 1)
693 resample_effect:set_float("zoom_x", screen_width)
694 resample_effect:set_float("zoom_y", screen_height)
696 resize_effect:set_int("width", 1)
697 resize_effect:set_int("height", 1)
699 padding_effect:set_int("left", screen_width + 100)
700 padding_effect:set_int("top", screen_height + 100)
706 srcx0 = -x0 / (x1 - x0)
710 srcy0 = -y0 / (y1 - y0)
713 if x1 > screen_width then
714 srcx1 = (screen_width - x0) / (x1 - x0)
717 if y1 > screen_height then
718 srcy1 = (screen_height - y0) / (y1 - y0)
722 if resample_effect ~= nil then
723 -- High-quality resampling.
724 local x_subpixel_offset = x0 - math.floor(x0)
725 local y_subpixel_offset = y0 - math.floor(y0)
727 -- Resampling must be to an integral number of pixels. Round up,
728 -- and then add an extra pixel so we have some leeway for the border.
729 local width = math.ceil(x1 - x0) + 1
730 local height = math.ceil(y1 - y0) + 1
731 resample_effect:set_int("width", width)
732 resample_effect:set_int("height", height)
734 -- Correct the discrepancy with zoom. (This will leave a small
735 -- excess edge of pixels and subpixels, which we'll correct for soon.)
736 local zoom_x = (x1 - x0) / (width * (srcx1 - srcx0))
737 local zoom_y = (y1 - y0) / (height * (srcy1 - srcy0))
738 resample_effect:set_float("zoom_x", zoom_x)
739 resample_effect:set_float("zoom_y", zoom_y)
740 resample_effect:set_float("zoom_center_x", 0.0)
741 resample_effect:set_float("zoom_center_y", 0.0)
743 -- Padding must also be to a whole-pixel offset.
744 padding_effect:set_int("left", math.floor(x0))
745 padding_effect:set_int("top", math.floor(y0))
747 -- Correct _that_ discrepancy by subpixel offset in the resampling.
748 resample_effect:set_float("left", srcx0 * input_width - x_subpixel_offset / zoom_x)
749 resample_effect:set_float("top", srcy0 * input_height - y_subpixel_offset / zoom_y)
751 -- Finally, adjust the border so it is exactly where we want it.
752 padding_effect:set_float("border_offset_left", x_subpixel_offset)
753 padding_effect:set_float("border_offset_right", x1 - (math.floor(x0) + width))
754 padding_effect:set_float("border_offset_top", y_subpixel_offset)
755 padding_effect:set_float("border_offset_bottom", y1 - (math.floor(y0) + height))
757 -- Lower-quality simple resizing.
758 local width = round(x1 - x0)
759 local height = round(y1 - y0)
760 resize_effect:set_int("width", width)
761 resize_effect:set_int("height", height)
763 -- Padding must also be to a whole-pixel offset.
764 padding_effect:set_int("left", math.floor(x0))
765 padding_effect:set_int("top", math.floor(y0))
769 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
771 return math.floor(x + 0.5)
774 function lerp(a, b, t)
775 return a + (b - a) * t
778 function prepare_sbs_chain(chain, t, screen_width, screen_height, input_resolution)
779 chain.input0.input:connect_signal(0)
780 chain.input1.input:connect_signal(1)
781 set_neutral_color(chain.input0.wb_effect, input0_neutral_color)
782 set_neutral_color(chain.input1.wb_effect, input1_neutral_color)
784 -- First input is positioned (16,48) from top-left.
785 local width0 = round(848 * screen_width/1280.0)
786 local height0 = round(width0 * 9.0 / 16.0)
788 local top0 = 48 * screen_height/720.0
789 local left0 = 16 * screen_width/1280.0
790 local bottom0 = top0 + height0
791 local right0 = left0 + width0
793 -- Second input is positioned (16,48) from the bottom-right.
794 local width1 = round(384 * screen_width/1280.0)
795 local height1 = round(216 * screen_height/720.0)
797 local bottom1 = screen_height - 48 * screen_height/720.0
798 local right1 = screen_width - 16 * screen_width/1280.0
799 local top1 = bottom1 - height1
800 local left1 = right1 - width1
802 -- Interpolate between the fullscreen and side-by-side views.
803 local scale0, tx0, tx0
804 if zoom_poi == INPUT0_SIGNAL_NUM then
805 local new_left0 = lerp(left0, 0, t)
806 local new_right0 = lerp(right0, screen_width, t)
807 local new_top0 = lerp(top0, 0, t)
808 local new_bottom0 = lerp(bottom0, screen_height, t)
810 scale0 = (new_right0 - new_left0) / width0 -- Same vertically and horizonally.
811 tx0 = new_left0 - left0 * scale0
812 ty0 = new_top0 - top0 * scale0
814 local new_left1 = lerp(left1, 0, t)
815 local new_right1 = lerp(right1, screen_width, t)
816 local new_top1 = lerp(top1, 0, t)
817 local new_bottom1 = lerp(bottom1, screen_height, t)
819 scale0 = (new_right1 - new_left1) / width1 -- Same vertically and horizonally.
820 tx0 = new_left1 - left1 * scale0
821 ty0 = new_top1 - top1 * scale0
824 top0 = top0 * scale0 + ty0
825 bottom0 = bottom0 * scale0 + ty0
826 left0 = left0 * scale0 + tx0
827 right0 = right0 * scale0 + tx0
829 top1 = top1 * scale0 + ty0
830 bottom1 = bottom1 * scale0 + ty0
831 left1 = left1 * scale0 + tx0
832 right1 = right1 * scale0 + tx0
833 place_rectangle(chain.input0.resample_effect, chain.input0.resize_effect, chain.input0.padding_effect, left0, top0, right0, bottom0, screen_width, screen_height, input_resolution[0].width, input_resolution[0].height)
834 place_rectangle(chain.input1.resample_effect, chain.input1.resize_effect, chain.input1.padding_effect, left1, top1, right1, bottom1, screen_width, screen_height, input_resolution[1].width, input_resolution[1].height)
837 function set_neutral_color(effect, color)
838 effect:set_vec3("neutral_color", color[1], color[2], color[3])
841 function set_neutral_color_from_signal(effect, signal)
842 if signal == INPUT0_SIGNAL_NUM then
843 set_neutral_color(effect, input0_neutral_color)
845 set_neutral_color(effect, input1_neutral_color)
849 function calc_fade_progress(t, transition_start, transition_end)
850 local tt = (t - transition_start) / (transition_end - transition_start)
857 -- Make the fade look maybe a tad more natural, by pumping it
858 -- through a sigmoid function.
860 tt = 1.0 / (1.0 + math.exp(-tt))