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