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