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
12 local transition_type = 0
13 local transition_src_signal = 0
14 local transition_dst_signal = 0
16 local neutral_colors = {
17 {0.5, 0.5, 0.5}, -- Input 0.
18 {0.5, 0.5, 0.5} -- Input 1.
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 -- Valid values for transition_type. (Cuts are done directly, so they need no entry.)
31 local NO_TRANSITION = 0
32 local ZOOM_TRANSITION = 1 -- Also for slides.
33 local FADE_TRANSITION = 2
35 -- Last width/height/frame rate for each channel, if we have it.
36 -- Note that unlike the values we get from Nageru, the resolution is per
37 -- frame and not per field, since we deinterlace.
38 local last_resolution = {}
40 -- Utility function to help creating many similar chains that can differ
41 -- in a free set of chosen parameters.
42 function make_cartesian_product(parms, callback)
43 return make_cartesian_product_internal(parms, callback, 1, {})
46 function make_cartesian_product_internal(parms, callback, index, args)
47 if index > #parms then
48 return callback(unpack(args))
51 for _, value in ipairs(parms[index]) do
53 ret[value] = make_cartesian_product_internal(parms, callback, index + 1, args)
58 function make_sbs_input(chain, signal, deint, hq)
59 local input = chain:add_live_input(not deint, deint) -- Override bounce only if not deinterlacing.
60 input:connect_signal(signal)
62 local resample_effect = nil
63 local resize_effect = nil
65 resample_effect = chain:add_effect(ResampleEffect.new())
67 resize_effect = chain:add_effect(ResizeEffect.new())
69 local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
71 local padding_effect = chain:add_effect(IntegralPaddingEffect.new())
75 wb_effect = wb_effect,
76 resample_effect = resample_effect,
77 resize_effect = resize_effect,
78 padding_effect = padding_effect
82 -- The main live chain.
83 function make_sbs_chain(input0_type, input1_type, hq)
84 local chain = EffectChain.new(16, 9)
86 local input0 = make_sbs_input(chain, INPUT0_SIGNAL_NUM, input0_type == "livedeint", hq)
87 local input1 = make_sbs_input(chain, INPUT1_SIGNAL_NUM, input1_type == "livedeint", hq)
89 input0.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 1.0)
90 input1.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0)
92 chain:add_effect(OverlayEffect.new(), input0.padding_effect, input1.padding_effect)
102 -- Make all possible combinations of side-by-side chains.
103 local sbs_chains = make_cartesian_product({
104 {"live", "livedeint"}, -- input0_type
105 {"live", "livedeint"}, -- input1_type
107 }, function(input0_type, input1_type, hq)
108 return make_sbs_chain(input0_type, input1_type, hq)
111 function make_fade_input(chain, signal, live, deint, scale)
112 local input, wb_effect, resample_effect, last
114 input = chain:add_live_input(false, deint)
115 input:connect_signal(signal)
118 input = chain:add_effect(ImageInput.new("bg.jpeg"))
122 -- If we cared about this for the non-main inputs, we would have
123 -- checked hq here and invoked ResizeEffect instead.
125 resample_effect = chain:add_effect(ResampleEffect.new())
126 last = resample_effect
129 -- Make sure to put the white balance after the scaling (usually more efficient).
131 wb_effect = chain:add_effect(WhiteBalanceEffect.new())
137 wb_effect = wb_effect,
138 resample_effect = resample_effect,
143 -- A chain to fade between two inputs, of which either can be a picture
144 -- or a live input. In practice only used live, but we still support the
146 function make_fade_chain(input0_live, input0_deint, input0_scale, input1_live, input1_deint, input1_scale, hq)
147 local chain = EffectChain.new(16, 9)
149 local input0 = make_fade_input(chain, INPUT0_SIGNAL_NUM, input0_live, input0_deint, input0_scale)
150 local input1 = make_fade_input(chain, INPUT1_SIGNAL_NUM, input1_live, input1_deint, input1_scale)
152 local mix_effect = chain:add_effect(MixEffect.new(), input0.last, input1.last)
159 mix_effect = mix_effect
163 -- Chains to fade between two inputs, in various configurations.
164 local fade_chains = make_cartesian_product({
165 {"static", "live", "livedeint"}, -- input0_type
166 {true, false}, -- input0_scale
167 {"static", "live", "livedeint"}, -- input1_type
168 {true, false}, -- input1_scale
170 }, function(input0_type, input0_scale, input1_type, input1_scale, hq)
171 local input0_live = (input0_type ~= "static")
172 local input1_live = (input1_type ~= "static")
173 local input0_deint = (input0_type == "livedeint")
174 local input1_deint = (input1_type == "livedeint")
175 return make_fade_chain(input0_live, input0_deint, input0_scale, input1_live, input1_deint, input1_scale, hq)
178 -- A chain to show a single input on screen.
179 function make_simple_chain(input_deint, input_scale, hq)
180 local chain = EffectChain.new(16, 9)
182 local input = chain:add_live_input(false, input_deint)
183 input:connect_signal(0) -- First input card. Can be changed whenever you want.
185 local resample_effect, resize_effect
188 resample_effect = chain:add_effect(ResampleEffect.new())
190 resize_effect = chain:add_effect(ResizeEffect.new())
194 local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
200 wb_effect = wb_effect,
201 resample_effect = resample_effect,
202 resize_effect = resize_effect
206 -- Make all possible combinations of single-input chains.
207 local simple_chains = make_cartesian_product({
208 {"live", "livedeint"}, -- input_type
209 {true, false}, -- input_scale
211 }, function(input_type, input_scale, hq)
212 local input_deint = (input_type == "livedeint")
213 return make_simple_chain(input_deint, input_scale, hq)
216 -- A chain to show a single static picture on screen (HQ version).
217 local static_chain_hq = EffectChain.new(16, 9)
218 local static_chain_hq_input = static_chain_hq:add_effect(ImageInput.new("bg.jpeg"))
219 static_chain_hq:finalize(true)
221 -- A chain to show a single static picture on screen (LQ version).
222 local static_chain_lq = EffectChain.new(16, 9)
223 local static_chain_lq_input = static_chain_lq:add_effect(ImageInput.new("bg.jpeg"))
224 static_chain_lq:finalize(false)
226 -- Used for indexing into the tables of chains.
227 function get_input_type(signals, signal_num)
228 if signal_num == STATIC_SIGNAL_NUM then
230 elseif signals:get_interlaced(signal_num) then
237 function needs_scale(signals, signal_num, width, height)
238 if signal_num == STATIC_SIGNAL_NUM then
239 -- We assume this is already correctly scaled at load time.
242 assert(is_plain_signal(signal_num))
243 return (signals:get_width(signal_num) ~= width or signals:get_height(signal_num) ~= height)
246 function set_scale_parameters_if_needed(chain_or_input, width, height)
247 if chain_or_input.resample_effect then
248 chain_or_input.resample_effect:set_int("width", width)
249 chain_or_input.resample_effect:set_int("height", height)
250 elseif chain_or_input.resize_effect then
251 chain_or_input.resize_effect:set_int("width", width)
252 chain_or_input.resize_effect:set_int("height", height)
257 -- Returns the number of outputs in addition to the live (0) and preview (1).
258 -- Called only once, at the start of the program.
259 function num_channels()
263 function is_plain_signal(num)
264 return num == INPUT0_SIGNAL_NUM or num == INPUT1_SIGNAL_NUM
267 -- Helper function to write e.g. “720p60”. The difference between this
268 -- and get_channel_resolution_raw() is that this one also can say that
269 -- there's no signal.
270 function get_channel_resolution(signal_num)
271 res = last_resolution[signal_num]
272 if (not res) or not res.is_connected then
273 return "disconnected"
275 if res.height <= 0 then
278 if not res.has_signal then
279 if res.height == 525 then
280 -- Special mode for the USB3 cards.
283 return get_channel_resolution_raw(res) .. ", no signal"
285 return get_channel_resolution_raw(res)
289 -- Helper function to write e.g. “60” or “59.94”.
290 function get_frame_rate(res)
291 local nom = res.frame_rate_nom
292 local den = res.frame_rate_den
293 if nom % den == 0 then
296 return string.format("%.2f", nom / den)
300 -- Helper function to write e.g. “720p60”.
301 function get_channel_resolution_raw(res)
302 if res.interlaced then
303 return res.height .. "i" .. get_frame_rate(res)
305 return res.height .. "p" .. get_frame_rate(res)
310 -- Returns the name for each additional channel (starting from 2).
311 -- Called at the start of the program, and then each frame for live
312 -- channels in case they change resolution.
313 function channel_name(channel)
314 local signal_num = channel - 2
315 if is_plain_signal(signal_num) then
316 return "Input " .. (signal_num + 1) .. " (" .. get_channel_resolution(signal_num) .. ")"
317 elseif signal_num == SBS_SIGNAL_NUM then
318 return "Side-by-side"
319 elseif signal_num == STATIC_SIGNAL_NUM then
320 return "Static picture"
325 -- Returns, given a channel number, which signal it corresponds to (starting from 0).
326 -- Should return -1 if the channel does not correspond to a simple signal
327 -- (one connected to a capture card, or a video input). The information is used for
328 -- whether right-click on the channel should bring up a context menu or not,
329 -- typically containing an input selector, resolution menu etc.
331 -- Called once for each channel, at the start of the program.
332 -- Will never be called for live (0) or preview (1).
333 function channel_signal(channel)
336 elseif channel == 3 then
344 -- Called every frame. Returns the color (if any) to paint around the given
345 -- channel. Returns a CSS color (typically to mark live and preview signals);
346 -- "transparent" is allowed.
347 -- Will never be called for live (0) or preview (1).
348 function channel_color(channel)
349 if transition_type ~= NO_TRANSITION then
350 if channel_involved_in(channel, transition_src_signal) or
351 channel_involved_in(channel, transition_dst_signal) then
355 if channel_involved_in(channel, live_signal_num) then
359 if channel_involved_in(channel, preview_signal_num) then
365 function channel_involved_in(channel, signal_num)
366 if is_plain_signal(signal_num) then
367 return channel == (signal_num + 2)
369 if signal_num == SBS_SIGNAL_NUM then
370 return (channel == 2 or channel == 3)
372 if signal_num == STATIC_SIGNAL_NUM then
373 return (channel == 5)
379 -- Returns if a given channel supports setting white balance (starting from 2).
380 -- Called only once for each channel, at the start of the program.
381 function supports_set_wb(channel)
382 return is_plain_signal(channel - 2)
386 -- Gets called with a new gray point when the white balance is changing.
387 -- The color is in linear light (not sRGB gamma).
388 function set_wb(channel, red, green, blue)
389 if is_plain_signal(channel - 2) then
390 neutral_colors[channel - 2 + 1] = { red, green, blue }
394 function finish_transitions(t)
395 if transition_type ~= NO_TRANSITION and t >= transition_end then
396 live_signal_num = transition_dst_signal
397 transition_type = NO_TRANSITION
401 function in_transition(t)
402 return t >= transition_start and t <= transition_end
406 -- Called every frame.
407 function get_transitions(t)
408 if in_transition(t) then
409 -- Transition already in progress, the only thing we can do is really
410 -- cut to the preview. (TODO: Make an “abort” and/or “finish”, too?)
414 finish_transitions(t)
416 if live_signal_num == preview_signal_num then
417 -- No transitions possible.
421 if (is_plain_signal(live_signal_num) or live_signal_num == STATIC_SIGNAL_NUM) and
422 (is_plain_signal(preview_signal_num) or preview_signal_num == STATIC_SIGNAL_NUM) then
423 return {"Cut", "", "Fade"}
427 if live_signal_num == SBS_SIGNAL_NUM and is_plain_signal(preview_signal_num) then
428 return {"Cut", "Zoom in"}
429 elseif is_plain_signal(live_signal_num) and preview_signal_num == SBS_SIGNAL_NUM then
430 return {"Cut", "Zoom out"}
436 function swap_preview_live()
437 local temp = live_signal_num
438 live_signal_num = preview_signal_num
439 preview_signal_num = temp
442 function start_transition(type_, t, duration)
444 transition_end = t + duration
445 transition_type = type_
446 transition_src_signal = live_signal_num
447 transition_dst_signal = preview_signal_num
452 -- Called when the user clicks a transition button.
453 function transition_clicked(num, t)
456 if in_transition(t) then
457 -- Ongoing transition; finish it immediately before the cut.
458 finish_transitions(transition_end)
464 finish_transitions(t)
466 if live_signal_num == preview_signal_num then
471 if is_plain_signal(live_signal_num) and is_plain_signal(preview_signal_num) then
472 -- We can't zoom between these. Just make a cut.
473 io.write("Cutting from " .. live_signal_num .. " to " .. live_signal_num .. "\n")
478 if (live_signal_num == SBS_SIGNAL_NUM and is_plain_signal(preview_signal_num)) or
479 (preview_signal_num == SBS_SIGNAL_NUM and is_plain_signal(live_signal_num)) then
480 start_transition(ZOOM_TRANSITION, t, 1.0)
483 finish_transitions(t)
486 if (live_signal_num ~= preview_signal_num) and
487 (is_plain_signal(live_signal_num) or
488 live_signal_num == STATIC_SIGNAL_NUM) and
489 (is_plain_signal(preview_signal_num) or
490 preview_signal_num == STATIC_SIGNAL_NUM) then
491 start_transition(FADE_TRANSITION, t, 1.0)
493 -- Fades involving SBS are ignored (we have no chain for it).
499 function channel_clicked(num)
500 preview_signal_num = num
503 function get_fade_chain(signals, t, width, height, input_resolution)
504 local input0_type = get_input_type(signals, transition_src_signal)
505 local input0_scale = needs_scale(signals, transition_src_signal, width, height)
506 local input1_type = get_input_type(signals, transition_dst_signal)
507 local input1_scale = needs_scale(signals, transition_dst_signal, width, height)
508 local chain = fade_chains[input0_type][input0_scale][input1_type][input1_scale][true]
510 if input0_type == "live" or input0_type == "livedeint" then
511 chain.input0.input:connect_signal(transition_src_signal)
512 set_neutral_color_from_signal(chain.input0.wb_effect, transition_src_signal)
514 set_scale_parameters_if_needed(chain.input0, width, height)
515 if input1_type == "live" or input1_type == "livedeint" then
516 chain.input1.input:connect_signal(transition_dst_signal)
517 set_neutral_color_from_signal(chain.input1.wb_effect, transition_dst_signal)
519 set_scale_parameters_if_needed(chain.input1, width, height)
520 local tt = calc_fade_progress(t, transition_start, transition_end)
522 chain.mix_effect:set_float("strength_first", 1.0 - tt)
523 chain.mix_effect:set_float("strength_second", tt)
525 return chain.chain, prepare
528 -- SBS code (live_signal_num == SBS_SIGNAL_NUM, or in a transition to/from it).
529 function get_sbs_chain(signals, t, width, height, input_resolution)
530 local input0_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
531 local input1_type = get_input_type(signals, INPUT1_SIGNAL_NUM)
532 return sbs_chains[input0_type][input1_type][true]
536 -- Called every frame. Get the chain for displaying at input <num>,
537 -- where 0 is live, 1 is preview, 2 is the first channel to display
538 -- in the bottom bar, and so on up to num_channels()+1. t is the
539 -- current time in seconds. width and height are the dimensions of
540 -- the output, although you can ignore them if you don't need them
541 -- (they're useful if you want to e.g. know what to resample by).
543 -- <signals> is basically an exposed InputState, which you can use to
544 -- query for information about the signals at the point of the current
545 -- frame. In particular, you can call get_width() and get_height()
546 -- for any signal number, and use that to e.g. assist in chain selection.
548 -- You should return two objects; the chain itself, and then a
549 -- function (taking no parameters) that is run just before rendering.
550 -- The function needs to call connect_signal on any inputs, so that
551 -- it gets updated video data for the given frame. (You are allowed
552 -- to switch which input your input is getting from between frames,
553 -- but not calling connect_signal results in undefined behavior.)
554 -- If you want to change any parameters in the chain, this is also
557 -- NOTE: The chain returned must be finalized with the Y'CbCr flag
558 -- if and only if num==0.
559 function get_chain(num, t, width, height, signals)
560 local input_resolution = {}
561 for signal_num=0,1 do
563 width = signals:get_width(signal_num),
564 height = signals:get_height(signal_num),
565 interlaced = signals:get_interlaced(signal_num),
566 is_connected = signals:get_is_connected(signal_num),
567 has_signal = signals:get_has_signal(signal_num),
568 frame_rate_nom = signals:get_frame_rate_nom(signal_num),
569 frame_rate_den = signals:get_frame_rate_den(signal_num)
572 if res.interlaced then
573 -- Convert height from frame height to field height.
574 -- (Needed for e.g. place_rectangle.)
575 res.height = res.height * 2
577 -- Show field rate instead of frame rate; really for cosmetics only
578 -- (and actually contrary to EBU recommendations, although in line
579 -- with typical user expectations).
580 res.frame_rate_nom = res.frame_rate_nom * 2
583 input_resolution[signal_num] = res
585 last_resolution = input_resolution
587 if num == 0 then -- Live.
588 finish_transitions(t)
589 if transition_type == ZOOM_TRANSITION then
590 -- Transition in or out of SBS.
591 local chain = get_sbs_chain(signals, t, width, height, input_resolution)
593 prepare_sbs_chain(chain, calc_zoom_progress(t), transition_type, transition_src_signal, transition_dst_signal, width, height, input_resolution)
595 return chain.chain, prepare
596 elseif transition_type == NO_TRANSITION and live_signal_num == SBS_SIGNAL_NUM then
598 local chain = get_sbs_chain(signals, t, width, height, input_resolution)
600 prepare_sbs_chain(chain, 0.0, NO_TRANSITION, 0, SBS_SIGNAL_NUM, width, height, input_resolution)
602 return chain.chain, prepare
603 elseif transition_type == FADE_TRANSITION then
604 return get_fade_chain(signals, t, width, height, input_resolution)
605 elseif is_plain_signal(live_signal_num) then
606 local input_type = get_input_type(signals, live_signal_num)
607 local input_scale = needs_scale(signals, live_signal_num, width, height)
608 local chain = simple_chains[input_type][input_scale][true]
610 chain.input:connect_signal(live_signal_num)
611 set_scale_parameters_if_needed(chain, width, height)
612 set_neutral_color_from_signal(chain.wb_effect, live_signal_num)
614 return chain.chain, prepare
615 elseif live_signal_num == STATIC_SIGNAL_NUM then -- Static picture.
618 return static_chain_hq, prepare
623 if num == 1 then -- Preview.
624 num = preview_signal_num + 2
627 -- Individual preview inputs.
628 if is_plain_signal(num - 2) then
629 local signal_num = num - 2
630 local input_type = get_input_type(signals, signal_num)
631 local input_scale = needs_scale(signals, signal_num, width, height)
632 local chain = simple_chains[input_type][input_scale][false]
634 chain.input:connect_signal(signal_num)
635 set_scale_parameters_if_needed(chain, width, height)
636 set_neutral_color(chain.wb_effect, neutral_colors[signal_num + 1])
638 return chain.chain, prepare
640 if num == SBS_SIGNAL_NUM + 2 then
641 local input0_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
642 local input1_type = get_input_type(signals, INPUT1_SIGNAL_NUM)
643 local chain = sbs_chains[input0_type][input1_type][false]
645 prepare_sbs_chain(chain, 0.0, NO_TRANSITION, 0, SBS_SIGNAL_NUM, width, height, input_resolution)
647 return chain.chain, prepare
649 if num == STATIC_SIGNAL_NUM + 2 then
652 return static_chain_lq, prepare
656 function place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height)
662 padding_effect:set_int("width", screen_width)
663 padding_effect:set_int("height", screen_height)
666 if x0 > screen_width or x1 < 0.0 or y0 > screen_height or y1 < 0.0 then
667 if resample_effect ~= nil then
668 resample_effect:set_int("width", 1)
669 resample_effect:set_int("height", 1)
670 resample_effect:set_float("zoom_x", screen_width)
671 resample_effect:set_float("zoom_y", screen_height)
673 resize_effect:set_int("width", 1)
674 resize_effect:set_int("height", 1)
676 padding_effect:set_int("left", screen_width + 100)
677 padding_effect:set_int("top", screen_height + 100)
683 srcx0 = -x0 / (x1 - x0)
687 srcy0 = -y0 / (y1 - y0)
690 if x1 > screen_width then
691 srcx1 = (screen_width - x0) / (x1 - x0)
694 if y1 > screen_height then
695 srcy1 = (screen_height - y0) / (y1 - y0)
699 if resample_effect ~= nil then
700 -- High-quality resampling.
701 local x_subpixel_offset = x0 - math.floor(x0)
702 local y_subpixel_offset = y0 - math.floor(y0)
704 -- Resampling must be to an integral number of pixels. Round up,
705 -- and then add an extra pixel so we have some leeway for the border.
706 local width = math.ceil(x1 - x0) + 1
707 local height = math.ceil(y1 - y0) + 1
708 resample_effect:set_int("width", width)
709 resample_effect:set_int("height", height)
711 -- Correct the discrepancy with zoom. (This will leave a small
712 -- excess edge of pixels and subpixels, which we'll correct for soon.)
713 local zoom_x = (x1 - x0) / (width * (srcx1 - srcx0))
714 local zoom_y = (y1 - y0) / (height * (srcy1 - srcy0))
715 resample_effect:set_float("zoom_x", zoom_x)
716 resample_effect:set_float("zoom_y", zoom_y)
717 resample_effect:set_float("zoom_center_x", 0.0)
718 resample_effect:set_float("zoom_center_y", 0.0)
720 -- Padding must also be to a whole-pixel offset.
721 padding_effect:set_int("left", math.floor(x0))
722 padding_effect:set_int("top", math.floor(y0))
724 -- Correct _that_ discrepancy by subpixel offset in the resampling.
725 resample_effect:set_float("left", srcx0 * input_width - x_subpixel_offset / zoom_x)
726 resample_effect:set_float("top", srcy0 * input_height - y_subpixel_offset / zoom_y)
728 -- Finally, adjust the border so it is exactly where we want it.
729 padding_effect:set_float("border_offset_left", x_subpixel_offset)
730 padding_effect:set_float("border_offset_right", x1 - (math.floor(x0) + width))
731 padding_effect:set_float("border_offset_top", y_subpixel_offset)
732 padding_effect:set_float("border_offset_bottom", y1 - (math.floor(y0) + height))
734 -- Lower-quality simple resizing.
735 local width = round(x1 - x0)
736 local height = round(y1 - y0)
737 resize_effect:set_int("width", width)
738 resize_effect:set_int("height", height)
740 -- Padding must also be to a whole-pixel offset.
741 padding_effect:set_int("left", math.floor(x0))
742 padding_effect:set_int("top", math.floor(y0))
746 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
748 return math.floor(x + 0.5)
751 function lerp(a, b, t)
752 return a + (b - a) * t
755 function lerp_pos(a, b, t)
757 x0 = lerp(a.x0, b.x0, t),
758 y0 = lerp(a.y0, b.y0, t),
759 x1 = lerp(a.x1, b.x1, t),
760 y1 = lerp(a.y1, b.y1, t)
764 function pos_from_top_left(x, y, width, height, screen_width, screen_height)
765 local xs = screen_width / 1280.0
766 local ys = screen_height / 720.0
770 x1 = round(xs * (x + width)),
771 y1 = round(ys * (y + height))
775 function prepare_sbs_chain(chain, t, transition_type, src_signal, dst_signal, screen_width, screen_height, input_resolution)
776 chain.input0.input:connect_signal(0)
777 chain.input1.input:connect_signal(1)
778 set_neutral_color(chain.input0.wb_effect, neutral_colors[1])
779 set_neutral_color(chain.input1.wb_effect, neutral_colors[2])
781 -- First input is positioned (16,48) from top-left.
782 -- Second input is positioned (16,48) from the bottom-right.
783 local pos0 = pos_from_top_left(16, 48, 848, 477, screen_width, screen_height)
784 local pos1 = pos_from_top_left(1280 - 384 - 16, 720 - 216 - 48, 384, 216, screen_width, screen_height)
786 local pos_fs = { x0 = 0, y0 = 0, x1 = screen_width, y1 = screen_height }
788 if transition_type == NO_TRANSITION then
790 affine_param = { sx = 1.0, sy = 1.0, tx = 0.0, ty = 0.0 } -- Identity.
792 -- Zooming to/from SBS view into or out of a single view.
793 assert(transition_type == ZOOM_TRANSITION)
795 if src_signal == SBS_SIGNAL_NUM then
799 assert(dst_signal == SBS_SIGNAL_NUM)
804 if signal == INPUT0_SIGNAL_NUM then
805 affine_param = find_affine_param(pos0, lerp_pos(pos0, pos_fs, real_t))
806 elseif signal == INPUT1_SIGNAL_NUM then
807 affine_param = find_affine_param(pos1, lerp_pos(pos1, pos_fs, real_t))
811 -- NOTE: input_resolution is not 1-indexed, unlike usual Lua arrays.
812 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)
813 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)
816 -- Find the transformation that changes the first rectangle to the second one.
817 function find_affine_param(a, b)
818 local sx = (b.x1 - b.x0) / (a.x1 - a.x0)
819 local sy = (b.y1 - b.y0) / (a.y1 - a.y0)
823 tx = b.x0 - a.x0 * sx,
824 ty = b.y0 - a.y0 * sy
828 function place_rectangle_with_affine(resample_effect, resize_effect, padding_effect, pos, aff, screen_width, screen_height, input_width, input_height)
829 local x0 = pos.x0 * aff.sx + aff.tx
830 local x1 = pos.x1 * aff.sx + aff.tx
831 local y0 = pos.y0 * aff.sy + aff.ty
832 local y1 = pos.y1 * aff.sy + aff.ty
834 place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height, input_width, input_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 is_plain_signal(signal) then
843 set_neutral_color(effect, neutral_colors[signal - INPUT0_SIGNAL_NUM + 1])
847 function calc_zoom_progress(t)
848 if t < transition_start then
850 elseif t > transition_end then
853 local tt = (t - transition_start) / (transition_end - transition_start)
855 return math.sin(tt * 3.14159265358 * 0.5)
859 function calc_fade_progress(t, transition_start, transition_end)
860 local tt = (t - transition_start) / (transition_end - transition_start)
867 -- Make the fade look maybe a tad more natural, by pumping it
868 -- through a sigmoid function.
870 tt = 1.0 / (1.0 + math.exp(-tt))