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