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