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