]> git.sesse.net Git - nageru/blob - theme.lua
Reduce the code duplication in the plain inputs (backport from Solskogen theme).
[nageru] / theme.lua
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.
5 --
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.
9
10 local transition_start = -2.0
11 local transition_end = -1.0
12 local zoom_src = 0.0
13 local zoom_dst = 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
17
18 local neutral_colors = {
19         {0.5, 0.5, 0.5},  -- Input 0.
20         {0.5, 0.5, 0.5}   -- Input 1.
21 }
22
23 local live_signal_num = 0
24 local preview_signal_num = 1
25
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
31
32 -- “fake” signal number that signifies that we are fading from one input
33 -- to the next.
34 local FADE_SIGNAL_NUM = 4
35
36 -- Last width/height/frame rate for each channel, if we have it.
37 -- Note that unlike the values we get from Nageru, the resolution is per
38 -- frame and not per field, since we deinterlace.
39 local last_resolution = {}
40
41 -- Utility function to help creating many similar chains that can differ
42 -- in a free set of chosen parameters.
43 function make_cartesian_product(parms, callback)
44         return make_cartesian_product_internal(parms, callback, 1, {})
45 end
46
47 function make_cartesian_product_internal(parms, callback, index, args)
48         if index > #parms then
49                 return callback(unpack(args))
50         end
51         local ret = {}
52         for _, value in ipairs(parms[index]) do
53                 args[index] = value
54                 ret[value] = make_cartesian_product_internal(parms, callback, index + 1, args)
55         end
56         return ret
57 end
58
59 function make_sbs_input(chain, signal, deint, hq)
60         local input = chain:add_live_input(not deint, deint)  -- Override bounce only if not deinterlacing.
61         input:connect_signal(signal)
62
63         local resample_effect = nil
64         local resize_effect = nil
65         if (hq) then
66                 resample_effect = chain:add_effect(ResampleEffect.new())
67         else
68                 resize_effect = chain:add_effect(ResizeEffect.new())
69         end
70         local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
71
72         local padding_effect = chain:add_effect(IntegralPaddingEffect.new())
73
74         return {
75                 input = input,
76                 wb_effect = wb_effect,
77                 resample_effect = resample_effect,
78                 resize_effect = resize_effect,
79                 padding_effect = padding_effect
80         }
81 end
82
83 -- The main live chain.
84 function make_sbs_chain(input0_deint, input1_deint, hq)
85         local chain = EffectChain.new(16, 9)
86
87         local input0 = make_sbs_input(chain, INPUT0_SIGNAL_NUM, input0_deint, hq)
88         local input1 = make_sbs_input(chain, INPUT1_SIGNAL_NUM, input1_deint, hq)
89
90         input0.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 1.0)
91         input1.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0)
92
93         chain:add_effect(OverlayEffect.new(), input0.padding_effect, input1.padding_effect)
94         chain:finalize(hq)
95
96         return {
97                 chain = chain,
98                 input0 = input0,
99                 input1 = input1
100         }
101 end
102
103 -- Make all possible combinations of side-by-side chains.
104 local sbs_chains = make_cartesian_product({
105         {"live", "livedeint"},  -- input0_type
106         {"live", "livedeint"},  -- input1_type
107         {true, false}           -- hq
108 }, function(input0_type, input1_type, hq)
109         local input0_deint = (input0_type == "livedeint")
110         local input1_deint = (input1_type == "livedeint")
111         return make_sbs_chain(input0_deint, input1_deint, hq)
112 end)
113
114 function make_fade_input(chain, signal, live, deint, scale)
115         local input, wb_effect, resample_effect, last
116         if live then
117                 input = chain:add_live_input(false, deint)
118                 input:connect_signal(signal)
119                 last = input
120         else
121                 input = chain:add_effect(ImageInput.new("bg.jpeg"))
122                 last = input
123         end
124
125         -- If we cared about this for the non-main inputs, we would have
126         -- checked hq here and invoked ResizeEffect instead.
127         if scale then
128                 resample_effect = chain:add_effect(ResampleEffect.new())
129                 last = resample_effect
130         end
131
132         -- Make sure to put the white balance after the scaling (usually more efficient).
133         if live then
134                 wb_effect = chain:add_effect(WhiteBalanceEffect.new())
135                 last = wb_effect
136         end
137
138         return {
139                 input = input,
140                 wb_effect = wb_effect,
141                 resample_effect = resample_effect,
142                 last = last
143         }
144 end
145
146 -- A chain to fade between two inputs, of which either can be a picture
147 -- or a live input. In practice only used live, but we still support the
148 -- hq parameter.
149 function make_fade_chain(input0_live, input0_deint, input0_scale, input1_live, input1_deint, input1_scale, hq)
150         local chain = EffectChain.new(16, 9)
151
152         local input0 = make_fade_input(chain, INPUT0_SIGNAL_NUM, input0_live, input0_deint, input0_scale)
153         local input1 = make_fade_input(chain, INPUT1_SIGNAL_NUM, input1_live, input1_deint, input1_scale)
154
155         local mix_effect = chain:add_effect(MixEffect.new(), input0.last, input1.last)
156         chain:finalize(hq)
157
158         return {
159                 chain = chain,
160                 input0 = input0,
161                 input1 = input1,
162                 mix_effect = mix_effect
163         }
164 end
165
166 -- Chains to fade between two inputs, in various configurations.
167 local fade_chains = make_cartesian_product({
168         {"static", "live", "livedeint"},  -- input0_type
169         {true, false},                    -- input0_scale
170         {"static", "live", "livedeint"},  -- input1_type
171         {true, false},                    -- input1_scale
172         {true}                            -- hq
173 }, function(input0_type, input0_scale, input1_type, input1_scale, hq)
174         local input0_live = (input0_type ~= "static")
175         local input1_live = (input1_type ~= "static")
176         local input0_deint = (input0_type == "livedeint")
177         local input1_deint = (input1_type == "livedeint")
178         return make_fade_chain(input0_live, input0_deint, input0_scale, input1_live, input1_deint, input1_scale, hq)
179 end)
180
181 -- A chain to show a single input on screen.
182 function make_simple_chain(input_deint, input_scale, hq)
183         local chain = EffectChain.new(16, 9)
184
185         local input = chain:add_live_input(false, input_deint)
186         input:connect_signal(0)  -- First input card. Can be changed whenever you want.
187
188         local resample_effect, resize_effect
189         if scale then
190                 if hq then
191                         resample_effect = chain:add_effect(ResampleEffect.new())
192                 else
193                         resize_effect = chain:add_effect(ResizeEffect.new())
194                 end
195         end
196
197         local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
198         chain:finalize(hq)
199
200         return {
201                 chain = chain,
202                 input = input,
203                 wb_effect = wb_effect,
204                 resample_effect = resample_effect,
205                 resize_effect = resize_effect
206         }
207 end
208
209 -- Make all possible combinations of single-input chains.
210 local simple_chains = make_cartesian_product({
211         {"live", "livedeint"},  -- input_type
212         {true, false},          -- input_scale
213         {true, false}           -- hq
214 }, function(input_type, input_scale, hq)
215         local input_deint = (input_type == "livedeint")
216         return make_simple_chain(input_deint, input_scale, hq)
217 end)
218
219 -- A chain to show a single static picture on screen (HQ version).
220 local static_chain_hq = EffectChain.new(16, 9)
221 local static_chain_hq_input = static_chain_hq:add_effect(ImageInput.new("bg.jpeg"))
222 static_chain_hq:finalize(true)
223
224 -- A chain to show a single static picture on screen (LQ version).
225 local static_chain_lq = EffectChain.new(16, 9)
226 local static_chain_lq_input = static_chain_lq:add_effect(ImageInput.new("bg.jpeg"))
227 static_chain_lq:finalize(false)
228
229 -- Used for indexing into the tables of chains.
230 function get_input_type(signals, signal_num)
231         if signal_num == STATIC_SIGNAL_NUM then
232                 return "static"
233         elseif signals:get_interlaced(signal_num) then
234                 return "livedeint"
235         else
236                 return "live"
237         end
238 end
239
240 function needs_scale(signals, signal_num, width, height)
241         if signal_num == STATIC_SIGNAL_NUM then
242                 -- We assume this is already correctly scaled at load time.
243                 return false
244         end
245         assert(is_plain_signal(signal_num))
246         return (signals:get_width(signal_num) ~= width or signals:get_height(signal_num) ~= height)
247 end
248
249 function set_scale_parameters_if_needed(chain_or_input, width, height)
250         if chain_or_input.resample_effect then
251                 chain_or_input.resample_effect:set_int("width", width)
252                 chain_or_input.resample_effect:set_int("height", height)
253         elseif chain_or_input.resize_effect then
254                 chain_or_input.resize_effect:set_int("width", width)
255                 chain_or_input.resize_effect:set_int("height", height)
256         end
257 end
258
259 -- API ENTRY POINT
260 -- Returns the number of outputs in addition to the live (0) and preview (1).
261 -- Called only once, at the start of the program.
262 function num_channels()
263         return 4
264 end
265
266 function is_plain_signal(num)
267         return num == INPUT0_SIGNAL_NUM or num == INPUT1_SIGNAL_NUM
268 end
269
270 -- Helper function to write e.g. “720p60”. The difference between this
271 -- and get_channel_resolution_raw() is that this one also can say that
272 -- there's no signal.
273 function get_channel_resolution(signal_num)
274         res = last_resolution[signal_num]
275         if (not res) or res.height <= 0 then
276                 return "no signal"
277         end
278         if not res.has_signal then
279                 if res.height == 525 then
280                         -- Special mode for the USB3 cards.
281                         return "no signal"
282                 end
283                 return get_channel_resolution_raw(res) .. ", no signal"
284         else
285                 return get_channel_resolution_raw(res)
286         end
287 end
288
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
294                 return nom / den
295         else
296                 return string.format("%.2f", nom / den)
297         end
298 end
299
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)
304         else
305                 return res.height .. "p" .. get_frame_rate(res)
306         end
307 end
308
309 -- API ENTRY POINT
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"
321         end
322 end
323
324 -- API ENTRY POINT
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 -- (The information is used for whether right-click on the channel should bring up
328 -- an input selector or not.)
329 -- Called once for each channel, at the start of the program.
330 -- Will never be called for live (0) or preview (1).
331 function channel_signal(channel)
332         if channel == 2 then
333                 return 0
334         elseif channel == 3 then
335                 return 1
336         else
337                 return -1
338         end
339 end
340
341 -- API ENTRY POINT
342 -- Called every frame. Returns the color (if any) to paint around the given
343 -- channel. Returns a CSS color (typically to mark live and preview signals);
344 -- "transparent" is allowed.
345 -- Will never be called for live (0) or preview (1).
346 function channel_color(channel)
347         if channel_involved_in(channel, live_signal_num) then
348                 return "#f00"
349         end
350         if channel_involved_in(channel, preview_signal_num) then
351                 return "#0f0"
352         end
353         return "transparent"
354 end
355
356 function channel_involved_in(channel, signal_num)
357         if is_plain_signal(signal_num) then
358                 return channel == (signal_num + 2)
359         end
360         if signal_num == SBS_SIGNAL_NUM then
361                 return (channel == 2 or channel == 3)
362         end
363         if signal_num == STATIC_SIGNAL_NUM then
364                 return (channel == 5)
365         end
366         if signal_num == FADE_SIGNAL_NUM then
367                 return (channel_involved_in(channel, fade_src_signal) or
368                         channel_involved_in(channel, fade_dst_signal))
369         end
370         return false
371 end
372
373 -- API ENTRY POINT
374 -- Returns if a given channel supports setting white balance (starting from 2).
375 -- Called only once for each channel, at the start of the program.
376 function supports_set_wb(channel)
377         return is_plain_signal(channel - 2)
378 end
379
380 -- API ENTRY POINT
381 -- Gets called with a new gray point when the white balance is changing.
382 -- The color is in linear light (not sRGB gamma).
383 function set_wb(channel, red, green, blue)
384         if is_plain_signal(channel - 2) then
385                 neutral_colors[channel - 2 + 1] = { red, green, blue }
386         end
387 end
388
389 function finish_transitions(t)
390         -- If live is SBS but de-facto single, make it so.
391         if live_signal_num == SBS_SIGNAL_NUM and t >= transition_end and zoom_dst == 1.0 then
392                 live_signal_num = zoom_poi
393         end
394
395         -- If live is fade but de-facto single, make it so.
396         if live_signal_num == FADE_SIGNAL_NUM and t >= transition_end then
397                 live_signal_num = fade_dst_signal
398         end
399 end
400
401 -- API ENTRY POINT
402 -- Called every frame.
403 function get_transitions(t)
404         finish_transitions(t)
405
406         if live_signal_num == preview_signal_num then
407                 -- No transitions possible.
408                 return {}
409         end
410
411         if live_signal_num == SBS_SIGNAL_NUM and t >= transition_start and t <= transition_end then
412                 -- Zoom in progress.
413                 return {"Cut"}
414         end
415
416         if (is_plain_signal(live_signal_num) or live_signal_num == STATIC_SIGNAL_NUM) and
417            (is_plain_signal(preview_signal_num) or preview_signal_num == STATIC_SIGNAL_NUM) then
418                 return {"Cut", "", "Fade"}
419         end
420
421         -- Various zooms.
422         if live_signal_num == SBS_SIGNAL_NUM and is_plain_signal(preview_signal_num) then
423                 return {"Cut", "Zoom in"}
424         elseif is_plain_signal(live_signal_num) and preview_signal_num == SBS_SIGNAL_NUM then
425                 return {"Cut", "Zoom out"}
426         end
427
428         return {"Cut"}
429 end
430
431 -- API ENTRY POINT
432 -- Called when the user clicks a transition button.
433 function transition_clicked(num, t)
434         if num == 0 then
435                 -- Cut.
436                 if live_signal_num == FADE_SIGNAL_NUM then
437                         -- Ongoing fade; finish it immediately.
438                         finish_transitions(transition_end)
439                 end
440
441                 local temp = live_signal_num
442                 live_signal_num = preview_signal_num
443                 preview_signal_num = temp
444
445                 if live_signal_num == SBS_SIGNAL_NUM then
446                         -- Just cut to SBS, we need to reset any zooms.
447                         zoom_src = 1.0
448                         zoom_dst = 0.0
449                         transition_start = -2.0
450                         transition_end = -1.0
451                 end
452         elseif num == 1 then
453                 -- Zoom.
454
455                 finish_transitions(t)
456
457                 if live_signal_num == preview_signal_num then
458                         -- Nothing to do.
459                         return
460                 end
461
462                 if (is_plain_signal(live_signal_num) and is_plain_signal(preview_signal_num)) then
463                         -- We can't zoom between these. Just make a cut.
464                         io.write("Cutting from " .. live_signal_num .. " to " .. live_signal_num .. "\n")
465                         local temp = live_signal_num
466                         live_signal_num = preview_signal_num
467                         preview_signal_num = temp
468                         return
469                 end
470
471                 if live_signal_num == SBS_SIGNAL_NUM and is_plain_signal(preview_signal_num) then
472                         -- Zoom in from SBS to single.
473                         transition_start = t
474                         transition_end = t + 1.0
475                         zoom_src = 0.0
476                         zoom_dst = 1.0
477                         zoom_poi = preview_signal_num
478                         preview_signal_num = SBS_SIGNAL_NUM
479                 elseif is_plain_signal(live_signal_num) and preview_signal_num == SBS_SIGNAL_NUM then
480                         -- Zoom out from single to SBS.
481                         transition_start = t
482                         transition_end = t + 1.0
483                         zoom_src = 1.0
484                         zoom_dst = 0.0
485                         preview_signal_num = live_signal_num
486                         zoom_poi = live_signal_num
487                         live_signal_num = SBS_SIGNAL_NUM
488                 end
489         elseif num == 2 then
490                 finish_transitions(t)
491
492                 -- Fade.
493                 if (live_signal_num ~= preview_signal_num) and
494                    (is_plain_signal(live_signal_num) or
495                     live_signal_num == STATIC_SIGNAL_NUM) and
496                    (is_plain_signal(preview_signal_num) or
497                     preview_signal_num == STATIC_SIGNAL_NUM) then
498                         transition_start = t
499                         transition_end = t + 1.0
500                         fade_src_signal = live_signal_num
501                         fade_dst_signal = preview_signal_num
502                         preview_signal_num = live_signal_num
503                         live_signal_num = FADE_SIGNAL_NUM
504                 else
505                         -- Fades involving SBS are ignored (we have no chain for it).
506                 end
507         end
508 end
509
510 -- API ENTRY POINT
511 function channel_clicked(num)
512         preview_signal_num = num
513 end
514
515 -- API ENTRY POINT
516 -- Called every frame. Get the chain for displaying at input <num>,
517 -- where 0 is live, 1 is preview, 2 is the first channel to display
518 -- in the bottom bar, and so on up to num_channels()+1. t is the
519 -- current time in seconds. width and height are the dimensions of
520 -- the output, although you can ignore them if you don't need them
521 -- (they're useful if you want to e.g. know what to resample by).
522 --
523 -- <signals> is basically an exposed InputState, which you can use to
524 -- query for information about the signals at the point of the current
525 -- frame. In particular, you can call get_width() and get_height()
526 -- for any signal number, and use that to e.g. assist in chain selection.
527 --
528 -- You should return two objects; the chain itself, and then a
529 -- function (taking no parameters) that is run just before rendering.
530 -- The function needs to call connect_signal on any inputs, so that
531 -- it gets updated video data for the given frame. (You are allowed
532 -- to switch which input your input is getting from between frames,
533 -- but not calling connect_signal results in undefined behavior.)
534 -- If you want to change any parameters in the chain, this is also
535 -- the right place.
536 --
537 -- NOTE: The chain returned must be finalized with the Y'CbCr flag
538 -- if and only if num==0.
539 function get_chain(num, t, width, height, signals)
540         local input_resolution = {}
541         for signal_num=0,1 do
542                 local res = {
543                         width = signals:get_width(signal_num),
544                         height = signals:get_height(signal_num),
545                         interlaced = signals:get_interlaced(signal_num),
546                         has_signal = signals:get_has_signal(signal_num),
547                         frame_rate_nom = signals:get_frame_rate_nom(signal_num),
548                         frame_rate_den = signals:get_frame_rate_den(signal_num)
549                 }
550
551                 if res.interlaced then
552                         -- Convert height from frame height to field height.
553                         -- (Needed for e.g. place_rectangle.)
554                         res.height = res.height * 2
555
556                         -- Show field rate instead of frame rate; really for cosmetics only
557                         -- (and actually contrary to EBU recommendations, although in line
558                         -- with typical user expectations).
559                         res.frame_rate_nom = res.frame_rate_nom * 2
560                 end
561
562                 input_resolution[signal_num] = res
563         end
564         last_resolution = input_resolution
565
566         if num == 0 then  -- Live.
567                 if is_plain_signal(live_signal_num) then  -- Plain inputs.
568                         local input_type = get_input_type(signals, live_signal_num)
569                         local input_scale = needs_scale(signals, live_signal_num, width, height)
570                         local chain = simple_chains[input_type][input_scale][true]
571                         prepare = function()
572                                 chain.input:connect_signal(live_signal_num)
573                                 set_scale_parameters_if_needed(chain, width, height)
574                                 set_neutral_color_from_signal(chain.wb_effect, live_signal_num)
575                         end
576                         return chain.chain, prepare
577                 elseif live_signal_num == STATIC_SIGNAL_NUM then  -- Static picture.
578                         prepare = function()
579                         end
580                         return static_chain_hq, prepare
581                 elseif live_signal_num == FADE_SIGNAL_NUM then  -- Fade.
582                         local input0_type = get_input_type(signals, fade_src_signal)
583                         local input0_scale = needs_scale(signals, fade_src_signal, width, height)
584                         local input1_type = get_input_type(signals, fade_dst_signal)
585                         local input1_scale = needs_scale(signals, fade_dst_signal, width, height)
586                         local chain = fade_chains[input0_type][input0_scale][input1_type][input1_scale][true]
587                         prepare = function()
588                                 if input0_type == "live" or input0_type == "livedeint" then
589                                         chain.input0.input:connect_signal(fade_src_signal)
590                                         set_neutral_color_from_signal(chain.input0.wb_effect, fade_src_signal)
591                                 end
592                                 set_scale_parameters_if_needed(chain.input0, width, height)
593                                 if input1_type == "live" or input1_type == "livedeint" then
594                                         chain.input1.input:connect_signal(fade_dst_signal)
595                                         set_neutral_color_from_signal(chain.input1.wb_effect, fade_dst_signal)
596                                 end
597                                 set_scale_parameters_if_needed(chain.input1, width, height)
598                                 local tt = calc_fade_progress(t, transition_start, transition_end)
599
600                                 chain.mix_effect:set_float("strength_first", 1.0 - tt)
601                                 chain.mix_effect:set_float("strength_second", tt)
602                         end
603                         return chain.chain, prepare
604                 end
605
606                 -- SBS code (live_signal_num == SBS_SIGNAL_NUM).
607                 local input0_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
608                 local input1_type = get_input_type(signals, INPUT1_SIGNAL_NUM)
609                 if t > transition_end and zoom_dst == 1.0 then
610                         -- Special case: Show only the single image on screen.
611                         local input0_scale = needs_scale(signals, fade_src_signal, width, height)
612                         local chain = simple_chains[input0_type][input0_scale][true]
613                         prepare = function()
614                                 chain.input:connect_signal(INPUT0_SIGNAL_NUM)
615                                 set_scale_parameters_if_needed(chain, width, height)
616                                 set_neutral_color(chain.wb_effect, neutral_colors[1])
617                         end
618                         return chain.chain, prepare
619                 end
620                 local chain = sbs_chains[input0_type][input1_type][true]
621                 prepare = function()
622                         if t < transition_start then
623                                 prepare_sbs_chain(chain, zoom_src, width, height, input_resolution)
624                         elseif t > transition_end then
625                                 prepare_sbs_chain(chain, zoom_dst, width, height, input_resolution)
626                         else
627                                 local tt = (t - transition_start) / (transition_end - transition_start)
628                                 -- Smooth it a bit.
629                                 tt = math.sin(tt * 3.14159265358 * 0.5)
630                                 prepare_sbs_chain(chain, zoom_src + (zoom_dst - zoom_src) * tt, width, height, input_resolution)
631                         end
632                 end
633                 return chain.chain, prepare
634         end
635         if num == 1 then  -- Preview.
636                 num = preview_signal_num + 2
637         end
638
639         -- Individual preview inputs.
640         if is_plain_signal(num - 2) then
641                 local signal_num = num - 2
642                 local input_type = get_input_type(signals, signal_num)
643                 local input_scale = needs_scale(signals, signal_num, width, height)
644                 local chain = simple_chains[input_type][input_scale][false]
645                 prepare = function()
646                         chain.input:connect_signal(signal_num)
647                         set_scale_parameters_if_needed(chain, width, height)
648                         set_neutral_color(chain.wb_effect, neutral_colors[signal_num + 1])
649                 end
650                 return chain.chain, prepare
651         end
652         if num == SBS_SIGNAL_NUM + 2 then
653                 local input0_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
654                 local input1_type = get_input_type(signals, INPUT1_SIGNAL_NUM)
655                 local chain = sbs_chains[input0_type][input1_type][false]
656                 prepare = function()
657                         prepare_sbs_chain(chain, 0.0, width, height, input_resolution)
658                 end
659                 return chain.chain, prepare
660         end
661         if num == STATIC_SIGNAL_NUM + 2 then
662                 prepare = function()
663                 end
664                 return static_chain_lq, prepare
665         end
666 end
667
668 function place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height)
669         local srcx0 = 0.0
670         local srcx1 = 1.0
671         local srcy0 = 0.0
672         local srcy1 = 1.0
673
674         padding_effect:set_int("width", screen_width)
675         padding_effect:set_int("height", screen_height)
676
677         -- Cull.
678         if x0 > screen_width or x1 < 0.0 or y0 > screen_height or y1 < 0.0 then
679                 if resample_effect ~= nil then
680                         resample_effect:set_int("width", 1)
681                         resample_effect:set_int("height", 1)
682                         resample_effect:set_float("zoom_x", screen_width)
683                         resample_effect:set_float("zoom_y", screen_height)
684                 else
685                         resize_effect:set_int("width", 1)
686                         resize_effect:set_int("height", 1)
687                 end
688                 padding_effect:set_int("left", screen_width + 100)
689                 padding_effect:set_int("top", screen_height + 100)
690                 return
691         end
692
693         -- Clip.
694         if x0 < 0 then
695                 srcx0 = -x0 / (x1 - x0)
696                 x0 = 0
697         end
698         if y0 < 0 then
699                 srcy0 = -y0 / (y1 - y0)
700                 y0 = 0
701         end
702         if x1 > screen_width then
703                 srcx1 = (screen_width - x0) / (x1 - x0)
704                 x1 = screen_width
705         end
706         if y1 > screen_height then
707                 srcy1 = (screen_height - y0) / (y1 - y0)
708                 y1 = screen_height
709         end
710
711         if resample_effect ~= nil then
712                 -- High-quality resampling.
713                 local x_subpixel_offset = x0 - math.floor(x0)
714                 local y_subpixel_offset = y0 - math.floor(y0)
715
716                 -- Resampling must be to an integral number of pixels. Round up,
717                 -- and then add an extra pixel so we have some leeway for the border.
718                 local width = math.ceil(x1 - x0) + 1
719                 local height = math.ceil(y1 - y0) + 1
720                 resample_effect:set_int("width", width)
721                 resample_effect:set_int("height", height)
722
723                 -- Correct the discrepancy with zoom. (This will leave a small
724                 -- excess edge of pixels and subpixels, which we'll correct for soon.)
725                 local zoom_x = (x1 - x0) / (width * (srcx1 - srcx0))
726                 local zoom_y = (y1 - y0) / (height * (srcy1 - srcy0))
727                 resample_effect:set_float("zoom_x", zoom_x)
728                 resample_effect:set_float("zoom_y", zoom_y)
729                 resample_effect:set_float("zoom_center_x", 0.0)
730                 resample_effect:set_float("zoom_center_y", 0.0)
731
732                 -- Padding must also be to a whole-pixel offset.
733                 padding_effect:set_int("left", math.floor(x0))
734                 padding_effect:set_int("top", math.floor(y0))
735
736                 -- Correct _that_ discrepancy by subpixel offset in the resampling.
737                 resample_effect:set_float("left", srcx0 * input_width - x_subpixel_offset / zoom_x)
738                 resample_effect:set_float("top", srcy0 * input_height - y_subpixel_offset / zoom_y)
739
740                 -- Finally, adjust the border so it is exactly where we want it.
741                 padding_effect:set_float("border_offset_left", x_subpixel_offset)
742                 padding_effect:set_float("border_offset_right", x1 - (math.floor(x0) + width))
743                 padding_effect:set_float("border_offset_top", y_subpixel_offset)
744                 padding_effect:set_float("border_offset_bottom", y1 - (math.floor(y0) + height))
745         else
746                 -- Lower-quality simple resizing.
747                 local width = round(x1 - x0)
748                 local height = round(y1 - y0)
749                 resize_effect:set_int("width", width)
750                 resize_effect:set_int("height", height)
751
752                 -- Padding must also be to a whole-pixel offset.
753                 padding_effect:set_int("left", math.floor(x0))
754                 padding_effect:set_int("top", math.floor(y0))
755         end
756 end
757
758 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
759 function round(x)
760         return math.floor(x + 0.5)
761 end
762
763 function lerp(a, b, t)
764         return a + (b - a) * t
765 end
766
767 function prepare_sbs_chain(chain, t, screen_width, screen_height, input_resolution)
768         chain.input0.input:connect_signal(0)
769         chain.input1.input:connect_signal(1)
770         set_neutral_color(chain.input0.wb_effect, neutral_colors[1])
771         set_neutral_color(chain.input1.wb_effect, neutral_colors[2])
772
773         -- First input is positioned (16,48) from top-left.
774         local width0 = round(848 * screen_width/1280.0)
775         local height0 = round(width0 * 9.0 / 16.0)
776
777         local top0 = 48 * screen_height/720.0
778         local left0 = 16 * screen_width/1280.0
779         local bottom0 = top0 + height0
780         local right0 = left0 + width0
781
782         -- Second input is positioned (16,48) from the bottom-right.
783         local width1 = round(384 * screen_width/1280.0)
784         local height1 = round(216 * screen_height/720.0)
785
786         local bottom1 = screen_height - 48 * screen_height/720.0
787         local right1 = screen_width - 16 * screen_width/1280.0
788         local top1 = bottom1 - height1
789         local left1 = right1 - width1
790
791         -- Interpolate between the fullscreen and side-by-side views.
792         local scale0, tx0, tx0
793         if zoom_poi == INPUT0_SIGNAL_NUM then
794                 local new_left0 = lerp(left0, 0, t)
795                 local new_right0 = lerp(right0, screen_width, t)
796                 local new_top0 = lerp(top0, 0, t)
797                 local new_bottom0 = lerp(bottom0, screen_height, t)
798
799                 scale0 = (new_right0 - new_left0) / width0  -- Same vertically and horizonally.
800                 tx0 = new_left0 - left0 * scale0
801                 ty0 = new_top0 - top0 * scale0
802         else
803                 local new_left1 = lerp(left1, 0, t)
804                 local new_right1 = lerp(right1, screen_width, t)
805                 local new_top1 = lerp(top1, 0, t)
806                 local new_bottom1 = lerp(bottom1, screen_height, t)
807
808                 scale0 = (new_right1 - new_left1) / width1  -- Same vertically and horizonally.
809                 tx0 = new_left1 - left1 * scale0
810                 ty0 = new_top1 - top1 * scale0
811         end
812
813         top0 = top0 * scale0 + ty0
814         bottom0 = bottom0 * scale0 + ty0
815         left0 = left0 * scale0 + tx0
816         right0 = right0 * scale0 + tx0
817
818         top1 = top1 * scale0 + ty0
819         bottom1 = bottom1 * scale0 + ty0
820         left1 = left1 * scale0 + tx0
821         right1 = right1 * scale0 + tx0
822         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)
823         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)
824 end
825
826 function set_neutral_color(effect, color)
827         effect:set_vec3("neutral_color", color[1], color[2], color[3])
828 end
829
830 function set_neutral_color_from_signal(effect, signal)
831         if is_plain_signal(signal) then
832                 set_neutral_color(effect, neutral_colors[signal - INPUT0_SIGNAL_NUM + 1])
833         end
834 end
835
836 function calc_fade_progress(t, transition_start, transition_end)
837         local tt = (t - transition_start) / (transition_end - transition_start)
838         if tt < 0.0 then
839                 return 0.0
840         elseif tt > 1.0 then
841                 return 1.0
842         end
843
844         -- Make the fade look maybe a tad more natural, by pumping it
845         -- through a sigmoid function.
846         tt = 10.0 * tt - 5.0
847         tt = 1.0 / (1.0 + math.exp(-tt))
848
849         return tt
850 end