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