]> git.sesse.net Git - ultimatescore/blob - nageru/ultimate.lua
Add the Nageru theme.
[ultimatescore] / nageru / ultimate.lua
1 -- Nageru theme for TFK mini-tournament 2017, based on the default theme.
2
3 local transition_start = -2.0
4 local transition_end = -1.0
5 local transition_type = 0
6 local transition_src_signal = 0
7 local transition_dst_signal = 0
8
9 local neutral_colors = {
10         {0.5, 0.5, 0.5},  -- Input 0.
11         {0.5, 0.5, 0.5},  -- Input 1.
12         {0.5, 0.5, 0.5}   -- Input 2.
13 }
14
15 local overlay_transition_start = -2.0
16 local overlay_transition_end = -1.0
17 local overlay_alpha_src = 0.0
18 local overlay_alpha_dst = 1.0
19 local overlay_enabled = false
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 INPUT2_SIGNAL_NUM = 2
28 local STATIC_SIGNAL_NUM = 3
29
30 -- Preview-only signal showing the current signal with the overlay.
31 -- Not valid for live_signal_num!
32 local OVERLAY_SIGNAL_NUM = 4
33
34 -- Valid values for transition_type. (Cuts are done directly, so they need no entry.)
35 local NO_TRANSITION = 0
36 local FADE_TRANSITION = 2
37
38 -- Last width/height/frame rate for each channel, if we have it.
39 -- Note that unlike the values we get from Nageru, the resolution is per
40 -- frame and not per field, since we deinterlace.
41 local last_resolution = {}
42
43 local caspar_input = VideoInput.new("unix:///tmp/caspar.sock")
44 caspar_input:change_rate(2.0)
45
46 -- Utility function to help creating many similar chains that can differ
47 -- in a free set of chosen parameters.
48 function make_cartesian_product(parms, callback)
49         return make_cartesian_product_internal(parms, callback, 1, {})
50 end
51
52 function make_cartesian_product_internal(parms, callback, index, args)
53         if index > #parms then
54                 return callback(unpack(args))
55         end
56         local ret = {}
57         for _, value in ipairs(parms[index]) do
58                 args[index] = value
59                 ret[value] = make_cartesian_product_internal(parms, callback, index + 1, args)
60         end
61         return ret
62 end
63
64 -- An overlay with variable alpha.
65 function make_overlay(chain, base)
66         local image = chain:add_video_input(caspar_input, false)
67         local multiply_effect = chain:add_effect(MultiplyEffect.new())
68         local overlay_effect = chain:add_effect(OverlayEffect.new(), base, multiply_effect)
69         return {
70                 image = image,
71                 multiply_effect = multiply_effect,
72                 overlay_effect = overlay_effect
73         }
74 end
75
76 function possibly_make_overlay(has_overlay, chain, base)
77         if has_overlay == true then
78                 return make_overlay(chain, base)
79         else
80                 return nil
81         end
82 end
83
84 function make_fade_input(chain, signal, live, deint, scale)
85         local input, wb_effect, resample_effect, last
86         if live then
87                 input = chain:add_live_input(false, deint)
88                 input:connect_signal(signal)
89                 last = input
90         else
91                 input = chain:add_effect(ImageInput.new("tfk_pause.png"))
92                 last = input
93         end
94
95         -- If we cared about this for the non-main inputs, we would have
96         -- checked hq here and invoked ResizeEffect instead.
97         if scale then
98                 resample_effect = chain:add_effect(ResampleEffect.new())
99                 last = resample_effect
100         end
101
102         -- Make sure to put the white balance after the scaling (usually more efficient).
103         if live then
104                 wb_effect = chain:add_effect(WhiteBalanceEffect.new())
105                 last = wb_effect
106         end
107
108         return {
109                 input = input,
110                 wb_effect = wb_effect,
111                 resample_effect = resample_effect,
112                 last = last
113         }
114 end
115
116 -- A chain to fade between two inputs, of which either can be a picture
117 -- or a live input. In practice only used live, but we still support the
118 -- hq parameter.
119 function make_fade_chain(input0_live, input0_deint, input0_scale, input1_live, input1_deint, input1_scale, has_overlay, hq)
120         local chain = EffectChain.new(16, 9)
121
122         local input0 = make_fade_input(chain, INPUT0_SIGNAL_NUM, input0_live, input0_deint, input0_scale)
123         local input1 = make_fade_input(chain, INPUT1_SIGNAL_NUM, input1_live, input1_deint, input1_scale)
124
125         local mix_effect = chain:add_effect(MixEffect.new(), input0.last, input1.last)
126         local overlay = possibly_make_overlay(has_overlay, chain, mix_effect)
127
128         chain:finalize(hq)
129
130         return {
131                 chain = chain,
132                 input0 = input0,
133                 input1 = input1,
134                 mix_effect = mix_effect,
135                 overlay = overlay
136         }
137 end
138
139 -- Chains to fade between two inputs, in various configurations.
140 local fade_chains = make_cartesian_product({
141         {"static", "live", "livedeint"},  -- input0_type
142         {true, false},                    -- input0_scale
143         {"static", "live", "livedeint"},  -- input1_type
144         {true, false},                    -- input1_scale
145         {true, false},                    -- has_overlay
146         {true}                            -- hq
147 }, function(input0_type, input0_scale, input1_type, input1_scale, has_overlay, hq)
148         local input0_live = (input0_type ~= "static")
149         local input1_live = (input1_type ~= "static")
150         local input0_deint = (input0_type == "livedeint")
151         local input1_deint = (input1_type == "livedeint")
152         return make_fade_chain(input0_live, input0_deint, input0_scale, input1_live, input1_deint, input1_scale, has_overlay, hq)
153 end)
154
155 -- A chain to show a single input on screen.
156 function make_simple_chain(input_deint, input_scale, has_overlay, hq)
157         local chain = EffectChain.new(16, 9)
158
159         local input = chain:add_live_input(false, input_deint)
160         input:connect_signal(0)  -- First input card. Can be changed whenever you want.
161
162         local resample_effect, resize_effect
163         if scale then
164                 if hq then
165                         resample_effect = chain:add_effect(ResampleEffect.new())
166                 else
167                         resize_effect = chain:add_effect(ResizeEffect.new())
168                 end
169         end
170
171         local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
172         local overlay = possibly_make_overlay(has_overlay, chain, wb_effect)
173
174         chain:finalize(hq)
175
176         return {
177                 chain = chain,
178                 input = input,
179                 wb_effect = wb_effect,
180                 resample_effect = resample_effect,
181                 resize_effect = resize_effect,
182                 overlay = overlay
183         }
184 end
185
186 -- Make all possible combinations of single-input chains.
187 local simple_chains = make_cartesian_product({
188         {"live", "livedeint"},  -- input_type
189         {true, false},          -- input_scale
190         {true, false},          -- has_overlay
191         {true, false}           -- hq
192 }, function(input_type, input_scale, has_overlay, hq)
193         local input_deint = (input_type == "livedeint")
194         return make_simple_chain(input_deint, input_scale, has_overlay, hq)
195 end)
196
197 -- A chain to show a single static picture on screen.
198 local static_chains = make_cartesian_product({
199         {true, false},           -- has_overlay
200         {true, false}            -- hq
201 }, function(has_overlay, hq)
202         local chain = EffectChain.new(16, 9)
203         local chain_input = chain:add_effect(ImageInput.new("tfk_pause.png"))
204         local overlay = possibly_make_overlay(has_overlay, chain, chain_input)
205
206         chain:finalize(hq)
207         return {
208                 chain = chain,
209                 overlay = overlay
210         }
211 end)
212
213 -- A chain to show the overlay and nothing more. LQ only,
214 -- since it is not a valid live signal.
215 local overlay_chain_lq = EffectChain.new(16, 9)
216 local overlay_chain_lq_input = overlay_chain_lq:add_video_input(caspar_input, false)
217 overlay_chain_lq:finalize(false)
218
219 -- Used for indexing into the tables of chains.
220 function get_input_type(signals, signal_num)
221         if signal_num == STATIC_SIGNAL_NUM then
222                 return "static"
223         elseif signals:get_interlaced(signal_num) then
224                 return "livedeint"
225         else
226                 return "live"
227         end
228 end
229
230 function needs_scale(signals, signal_num, width, height)
231         if signal_num == STATIC_SIGNAL_NUM then
232                 -- We assume this is already correctly scaled at load time.
233                 return false
234         end
235         assert(is_plain_signal(signal_num))
236         return (signals:get_width(signal_num) ~= width or signals:get_height(signal_num) ~= height)
237 end
238
239 function set_scale_parameters_if_needed(chain_or_input, width, height)
240         if chain_or_input.resample_effect then
241                 chain_or_input.resample_effect:set_int("width", width)
242                 chain_or_input.resample_effect:set_int("height", height)
243         elseif chain_or_input.resize_effect then
244                 chain_or_input.resize_effect:set_int("width", width)
245                 chain_or_input.resize_effect:set_int("height", height)
246         end
247 end
248
249 -- API ENTRY POINT
250 -- Returns the number of outputs in addition to the live (0) and preview (1).
251 -- Called only once, at the start of the program.
252 function num_channels()
253         return 5
254 end
255
256 function is_plain_signal(num)
257         return num >= INPUT0_SIGNAL_NUM and num <= INPUT2_SIGNAL_NUM
258 end
259
260 -- Helper function to write e.g. “720p60”. The difference between this
261 -- and get_channel_resolution_raw() is that this one also can say that
262 -- there's no signal.
263 function get_channel_resolution(signal_num)
264         res = last_resolution[signal_num]
265         if (not res) or not res.is_connected then
266                 return "disconnected"
267         end
268         if res.height <= 0 then
269                 return "no signal"
270         end
271         if not res.has_signal then
272                 if res.height == 525 then
273                         -- Special mode for the USB3 cards.
274                         return "no signal"
275                 end
276                 return get_channel_resolution_raw(res) .. ", no signal"
277         else
278                 return get_channel_resolution_raw(res)
279         end
280 end
281
282 -- Helper function to write e.g. “60” or “59.94”.
283 function get_frame_rate(res)
284         local nom = res.frame_rate_nom
285         local den = res.frame_rate_den
286         if nom % den == 0 then
287                 return nom / den
288         else
289                 return string.format("%.2f", nom / den)
290         end
291 end
292
293 -- Helper function to write e.g. “720p60”.
294 function get_channel_resolution_raw(res)
295         if res.interlaced then
296                 return res.height .. "i" .. get_frame_rate(res)
297         else
298                 return res.height .. "p" .. get_frame_rate(res)
299         end
300 end
301
302 -- API ENTRY POINT
303 -- Returns the name for each additional channel (starting from 2).
304 -- Called at the start of the program, and then each frame for live
305 -- channels in case they change resolution.
306 function channel_name(channel)
307         local signal_num = channel - 2
308         if is_plain_signal(signal_num) then
309                 return "Input " .. (signal_num + 1) .. " (" .. get_channel_resolution(signal_num) .. ")"
310         elseif signal_num == STATIC_SIGNAL_NUM then
311                 return "Static picture"
312         elseif signal_num == OVERLAY_SIGNAL_NUM then
313                 return "Overlay"
314         end
315 end
316
317 -- API ENTRY POINT
318 -- Returns, given a channel number, which signal it corresponds to (starting from 0).
319 -- Should return -1 if the channel does not correspond to a simple signal.
320 -- (The information is used for whether right-click on the channel should bring up
321 -- an input selector or not.)
322 -- Called once for each channel, at the start of the program.
323 -- Will never be called for live (0) or preview (1).
324 function channel_signal(channel)
325         if channel == 2 then
326                 return 0
327         elseif channel == 3 then
328                 return 1
329         else
330                 return -1
331         end
332 end
333
334 -- API ENTRY POINT
335 -- Called every frame. Returns the color (if any) to paint around the given
336 -- channel. Returns a CSS color (typically to mark live and preview signals);
337 -- "transparent" is allowed.
338 -- Will never be called for live (0) or preview (1).
339 function channel_color(channel)
340         if transition_type ~= NO_TRANSITION then
341                 if channel_involved_in(channel, transition_src_signal) or
342                    channel_involved_in(channel, transition_dst_signal) then
343                         return "#f00"
344                 end
345         else
346                 if channel_involved_in(channel, live_signal_num) then
347                         return "#f00"
348                 end
349         end
350         if channel_involved_in(channel, preview_signal_num) then
351                 return "#0f0"
352         end
353         return "transparent"
354 end
355
356 function channel_involved_in(channel, signal_num)
357         if is_plain_signal(signal_num) then
358                 return channel == (signal_num + 2)
359         end
360         if signal_num == STATIC_SIGNAL_NUM then
361                 return (channel == 5)
362         end
363         return false
364 end
365
366 -- API ENTRY POINT
367 -- Returns if a given channel supports setting white balance (starting from 2).
368 -- Called only once for each channel, at the start of the program.
369 function supports_set_wb(channel)
370         return is_plain_signal(channel - 2)
371 end
372
373 -- API ENTRY POINT
374 -- Gets called with a new gray point when the white balance is changing.
375 -- The color is in linear light (not sRGB gamma).
376 function set_wb(channel, red, green, blue)
377         if is_plain_signal(channel - 2) then
378                 neutral_colors[channel - 2 + 1] = { red, green, blue }
379         end
380 end
381
382 function finish_transitions(t)
383         if transition_type ~= NO_TRANSITION and t >= transition_end then
384                 live_signal_num = transition_dst_signal
385                 transition_type = NO_TRANSITION
386         end
387
388         -- Disable the overlay if it is no longer visible.
389         if overlay_enabled and t > overlay_transition_end and overlay_alpha_dst == 0.0 then
390                 overlay_enabled = false
391                 print("Turning off overlay")
392         end
393 end
394
395 function in_transition(t)
396         return t >= transition_start and t <= transition_end
397 end
398
399 -- API ENTRY POINT
400 -- Called every frame.
401 function get_transitions(t)
402         if preview_signal_num == OVERLAY_SIGNAL_NUM then
403                 if t < overlay_transition_end then
404                         -- Fade in progress.
405                         return {}
406                 end
407                 if overlay_enabled then
408                         return {"Overlay off", "", "Fade ovl out"}
409                 else
410                         return {"Overlay on", "", "Fade ovl in"}
411                 end
412         end
413
414         if in_transition(t) then
415                 -- Transition already in progress, the only thing we can do is really
416                 -- cut to the preview. (TODO: Make an “abort” and/or “finish”, too?)
417                 return {"Cut"}
418         end
419
420         if live_signal_num == preview_signal_num then
421                 -- No transitions possible.
422                 return {}
423         end
424
425         if (is_plain_signal(live_signal_num) or live_signal_num == STATIC_SIGNAL_NUM) and
426            (is_plain_signal(preview_signal_num) or preview_signal_num == STATIC_SIGNAL_NUM) then
427                 return {"Cut", "", "Fade"}
428         end
429
430         return {"Cut"}
431 end
432
433 function swap_preview_live()
434         local temp = live_signal_num
435         live_signal_num = preview_signal_num
436         preview_signal_num = temp
437 end
438
439 function start_transition(type_, t, duration)
440         transition_start = t
441         transition_end = t + duration
442         transition_type = type_
443         transition_src_signal = live_signal_num
444         transition_dst_signal = preview_signal_num
445         swap_preview_live()
446 end
447
448 -- API ENTRY POINT
449 -- Called when the user clicks a transition button.
450 function transition_clicked(num, t)
451         if preview_signal_num == OVERLAY_SIGNAL_NUM then
452                 if num == 0 then
453                         -- Cut.
454                         overlay_transition_start = -2.0
455                         overlay_transition_end = -1.0
456                         if overlay_enabled then
457                                 overlay_enabled = false
458                                 overlay_alpha_src = 0.0
459                                 overlay_alpha_dst = 0.0
460                         else
461                                 overlay_enabled = true
462                                 overlay_alpha_src = 1.0
463                                 overlay_alpha_dst = 1.0
464                         end
465                 elseif num == 2 then
466                         -- Fade.
467                         overlay_transition_start = t
468                         overlay_transition_end = t + 1.0
469                         if overlay_enabled then
470                                 overlay_alpha_src = 1.0
471                                 overlay_alpha_dst = 0.0
472                         else
473                                 overlay_alpha_src = 0.0
474                                 overlay_alpha_dst = 1.0
475                         end
476                         overlay_enabled = true
477                 end
478                 return
479         end
480
481         if num == 0 then
482                 -- Cut.
483                 if in_transition(t) then
484                         -- Ongoing transition; finish it immediately before the cut.
485                         finish_transitions(transition_end)
486                 end
487
488                 swap_preview_live()
489         elseif num == 1 then
490                 -- Zoom.
491                 finish_transitions(t)
492
493                 if live_signal_num == preview_signal_num then
494                         -- Nothing to do.
495                         return
496                 end
497
498                 if is_plain_signal(live_signal_num) and is_plain_signal(preview_signal_num) then
499                         -- We can't zoom between these. Just make a cut.
500                         io.write("Cutting from " .. live_signal_num .. " to " .. live_signal_num .. "\n")
501                         swap_preview_live()
502                         return
503                 end
504         elseif num == 2 then
505                 finish_transitions(t)
506
507                 -- Fade.
508                 if (live_signal_num ~= preview_signal_num) and
509                    (is_plain_signal(live_signal_num) or
510                     live_signal_num == STATIC_SIGNAL_NUM) and
511                    (is_plain_signal(preview_signal_num) or
512                     preview_signal_num == STATIC_SIGNAL_NUM) then
513                         start_transition(FADE_TRANSITION, t, 1.0)
514                 else
515                         -- Fades involving SBS are ignored (we have no chain for it).
516                 end
517         end
518 end
519
520 -- API ENTRY POINT
521 function channel_clicked(num)
522         preview_signal_num = num
523 end
524
525 function get_fade_chain(signals, t, width, height, input_resolution)
526         local input0_type = get_input_type(signals, transition_src_signal)
527         local input0_scale = needs_scale(signals, transition_src_signal, width, height)
528         local input1_type = get_input_type(signals, transition_dst_signal)
529         local input1_scale = needs_scale(signals, transition_dst_signal, width, height)
530         local chain = fade_chains[input0_type][input0_scale][input1_type][input1_scale][overlay_enabled][true]
531         prepare = function()
532                 if input0_type == "live" or input0_type == "livedeint" then
533                         chain.input0.input:connect_signal(transition_src_signal)
534                         set_neutral_color_from_signal(chain.input0.wb_effect, transition_src_signal)
535                 end
536                 set_scale_parameters_if_needed(chain.input0, width, height)
537                 if input1_type == "live" or input1_type == "livedeint" then
538                         chain.input1.input:connect_signal(transition_dst_signal)
539                         set_neutral_color_from_signal(chain.input1.wb_effect, transition_dst_signal)
540                 end
541                 set_scale_parameters_if_needed(chain.input1, width, height)
542                 local tt = calc_fade_progress(t, transition_start, transition_end)
543
544                 chain.mix_effect:set_float("strength_first", 1.0 - tt)
545                 chain.mix_effect:set_float("strength_second", tt)
546                 prepare_overlay_live(chain, t)
547         end
548         return chain.chain, prepare
549 end
550
551 -- API ENTRY POINT
552 -- Called every frame. Get the chain for displaying at input <num>,
553 -- where 0 is live, 1 is preview, 2 is the first channel to display
554 -- in the bottom bar, and so on up to num_channels()+1. t is the
555 -- current time in seconds. width and height are the dimensions of
556 -- the output, although you can ignore them if you don't need them
557 -- (they're useful if you want to e.g. know what to resample by).
558 --
559 -- <signals> is basically an exposed InputState, which you can use to
560 -- query for information about the signals at the point of the current
561 -- frame. In particular, you can call get_width() and get_height()
562 -- for any signal number, and use that to e.g. assist in chain selection.
563 --
564 -- You should return two objects; the chain itself, and then a
565 -- function (taking no parameters) that is run just before rendering.
566 -- The function needs to call connect_signal on any inputs, so that
567 -- it gets updated video data for the given frame. (You are allowed
568 -- to switch which input your input is getting from between frames,
569 -- but not calling connect_signal results in undefined behavior.)
570 -- If you want to change any parameters in the chain, this is also
571 -- the right place.
572 --
573 -- NOTE: The chain returned must be finalized with the Y'CbCr flag
574 -- if and only if num==0.
575 function get_chain(num, t, width, height, signals)
576         local input_resolution = {}
577         for signal_num=0,2 do
578                 local res = {
579                         width = signals:get_width(signal_num),
580                         height = signals:get_height(signal_num),
581                         interlaced = signals:get_interlaced(signal_num),
582                         has_signal = signals:get_has_signal(signal_num),
583                         is_connected = signals:get_is_connected(signal_num),
584                         frame_rate_nom = signals:get_frame_rate_nom(signal_num),
585                         frame_rate_den = signals:get_frame_rate_den(signal_num)
586                 }
587
588                 if res.interlaced then
589                         -- Convert height from frame height to field height.
590                         -- (Needed for e.g. place_rectangle.)
591                         res.height = res.height * 2
592
593                         -- Show field rate instead of frame rate; really for cosmetics only
594                         -- (and actually contrary to EBU recommendations, although in line
595                         -- with typical user expectations).
596                         res.frame_rate_nom = res.frame_rate_nom * 2
597                 end
598
599                 input_resolution[signal_num] = res
600         end
601         last_resolution = input_resolution
602
603         if num == 0 then  -- Live.
604                 -- See if we're in a transition.
605                 finish_transitions(t)
606                 if transition_type == FADE_TRANSITION then
607                         return get_fade_chain(signals, t, width, height, input_resolution)
608                 elseif is_plain_signal(live_signal_num) then
609                         local input_type = get_input_type(signals, live_signal_num)
610                         local input_scale = needs_scale(signals, live_signal_num, width, height)
611                         local chain = simple_chains[input_type][input_scale][overlay_enabled][true]
612                         prepare = function()
613                                 chain.input:connect_signal(live_signal_num)
614                                 set_scale_parameters_if_needed(chain, width, height)
615                                 set_neutral_color_from_signal(chain.wb_effect, live_signal_num)
616                                 prepare_overlay_live(chain, t)
617                         end
618                         return chain.chain, prepare
619                 elseif live_signal_num == STATIC_SIGNAL_NUM then  -- Static picture.
620                         local chain = static_chains[overlay_enabled][true]
621                         prepare = function()
622                                 prepare_overlay_live(chain, t)
623                         end
624                         return chain.chain, prepare
625                 else
626                         assert(false)
627                 end
628         end
629
630         -- We do not show overlays on the individual preview inputs.
631         -- The M/E preview matches what we'd put live by doing a transition, as always.
632         local show_overlay = false
633         if num == 1 then  -- Preview.
634                 if preview_signal_num == OVERLAY_SIGNAL_NUM then
635                         num = live_signal_num + 2
636                         show_overlay = not overlay_enabled
637
638                         if transition_type ~= NO_TRANSITION then
639                                 num = transition_dst_signal + 2
640                         end
641                 else
642                         num = preview_signal_num + 2
643                         show_overlay = overlay_enabled
644                 end
645         end
646
647         -- Individual preview inputs (usually without overlay).
648         if is_plain_signal(num - 2) then
649                 local signal_num = num - 2
650                 local input_type = get_input_type(signals, signal_num)
651                 local input_scale = needs_scale(signals, signal_num, width, height)
652                 local chain = simple_chains[input_type][input_scale][show_overlay][false]
653                 prepare = function()
654                         chain.input:connect_signal(signal_num)
655                         set_scale_parameters_if_needed(chain, width, height)
656                         set_neutral_color(chain.wb_effect, neutral_colors[signal_num + 1])
657                         prepare_overlay_static(chain, t)
658                 end
659                 return chain.chain, prepare
660         end
661         if num == STATIC_SIGNAL_NUM + 2 then
662                 local chain = static_chains[show_overlay][false]
663                 prepare = function()
664                         prepare_overlay_static(chain, t)
665                 end
666                 return chain.chain, prepare
667         end
668         if num == OVERLAY_SIGNAL_NUM + 2 then
669                 prepare = function()
670 --                      prepare_overlay(overlay_chain_lq, t)
671                 end
672                 return overlay_chain_lq, prepare
673         end
674 end
675
676 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
677 function round(x)
678         return math.floor(x + 0.5)
679 end
680
681 function prepare_overlay_live(chain, t)
682         if chain.overlay then
683                 local tt = calc_fade_progress(t, overlay_transition_start, overlay_transition_end)
684                 overlay_alpha = overlay_alpha_src + tt * (overlay_alpha_dst - overlay_alpha_src)
685                 --print("overlay_alpha=" .. overlay_alpha .. " [" .. overlay_alpha_src .. "," .. overlay_alpha_dst .. "]@" .. tt)
686                 if t > overlay_transition_end and overlay_alpha_dst == 0.0 then
687                         overlay_enabled = false  -- Takes effect next frame.
688         --              print("Turning off overlay")
689                 end
690                 chain.overlay.multiply_effect:set_vec4("factor", overlay_alpha, overlay_alpha, overlay_alpha, overlay_alpha)
691         end
692 end
693
694 function prepare_overlay_static(chain)
695         if chain.overlay then
696                 chain.overlay.multiply_effect:set_vec4("factor", 1.0, 1.0, 1.0, 1.0)
697         end
698 end
699
700 function set_neutral_color(effect, color)
701         effect:set_vec3("neutral_color", color[1], color[2], color[3])
702 end
703
704 function set_neutral_color_from_signal(effect, signal)
705         if is_plain_signal(signal) then
706                 set_neutral_color(effect, neutral_colors[signal - INPUT0_SIGNAL_NUM + 1])
707         end
708 end
709
710 function calc_zoom_progress(t)
711         if t < transition_start then
712                 return 0.0
713         elseif t > transition_end then
714                 return 1.0
715         else
716                 local tt = (t - transition_start) / (transition_end - transition_start)
717                 -- Smooth it a bit.
718                 return math.sin(tt * 3.14159265358 * 0.5)
719         end
720 end
721
722 function calc_fade_progress(t, transition_start, transition_end)
723         local tt = (t - transition_start) / (transition_end - transition_start)
724         if tt < 0.0 then
725                 return 0.0
726         elseif tt > 1.0 then
727                 return 1.0
728         end
729
730         -- Make the fade look maybe a tad more natural, by pumping it
731         -- through a sigmoid function.
732         tt = 10.0 * tt - 5.0
733         tt = 1.0 / (1.0 + math.exp(-tt))
734
735         return tt
736 end