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