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