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