]> git.sesse.net Git - ultimatescore/blob - nageru/ultimate.lua
Turn off the overlay when in stinger mode.
[ultimatescore] / nageru / ultimate.lua
1 -- Nageru theme for TFK mini-tournament 2017, based on the default theme.
2
3 local state = {
4         transition_start = -2.0,
5         transition_end = -1.0,
6         transition_type = 0,
7         transition_src_signal = 0,
8         transition_dst_signal = 0,
9
10         neutral_colors = {
11                 {0.5, 0.5, 0.5},  -- Input 0.
12                 {0.5, 0.5, 0.5},  -- Input 1.
13                 {0.5, 0.5, 0.5},  -- Input 2.
14                 {0.5, 0.5, 0.5},  -- Input 3.
15                 {0.5, 0.5, 0.5},  -- Input 4.
16                 {0.5, 0.5, 0.5}   -- Input 5.
17                 -- Will also be filled with VIDEO_SIGNAL_NUM below.
18         },
19
20         overlay_transition_start = -2.0,
21         overlay_transition_end = -1.0,
22         overlay_alpha_src = 0.0,
23         overlay_alpha_dst = 1.0,
24         overlay_enabled = false,
25
26         stinger_in_progress = false,
27         stinger_frame = 0,
28         stinger_src_signal = 0,
29         stinger_dst_signal = 0,
30         stinger_save_overlay = false,
31
32         live_signal_num = 0,
33         preview_signal_num = 1
34 }
35 local NUM_CAMERAS = 6  -- Remember to update neutral_colors, too.
36
37 -- Valid values for live_signal_num and preview_signal_num.
38 local INPUT0_SIGNAL_NUM = 0
39 local INPUT1_SIGNAL_NUM = 1
40 local INPUT2_SIGNAL_NUM = 2
41 local INPUT3_SIGNAL_NUM = 3
42 local INPUT4_SIGNAL_NUM = 4
43 local INPUT5_SIGNAL_NUM = 5
44 local SBS_SIGNAL_NUM = NUM_CAMERAS
45 local STATIC_SIGNAL_NUM = NUM_CAMERAS + 1
46 local VIDEO_SIGNAL_NUM = NUM_CAMERAS + 2
47
48 state.neutral_colors[VIDEO_SIGNAL_NUM - INPUT0_SIGNAL_NUM + 1] = {0.5, 0.5, 0.5}
49
50 -- Preview-only signal showing the current signal with the overlay.
51 -- Not valid for live_signal_num!
52 local OVERLAY_SIGNAL_NUM = NUM_CAMERAS + 3
53
54 -- Valid values for transition_type. (Cuts are done directly, so they need no entry.)
55 local NO_TRANSITION = 0
56 local ZOOM_TRANSITION = 1
57 local FADE_TRANSITION = 2
58
59 -- Last width/height/frame rate for each channel, if we have it.
60 -- Note that unlike the values we get from Nageru, the resolution is per
61 -- frame and not per field, since we deinterlace.
62 local last_resolution = {}
63
64 local cef_path = Nageru.THEME_PATH:match("(.*)/nageru/")
65 local cef_input = HTMLInput.new("file://" .. cef_path .. "/score.html")
66 cef_input:execute_javascript_async("play()")
67
68 local bg_video = VideoInput.new(cef_path .. "/flow-720.mp4", Nageru.VIDEO_FORMAT_YCBCR)
69 -- local iptv_video = VideoInput.new("http://10.34.129.69:9060/1/v.flv", Nageru.VIDEO_FORMAT_YCBCR)
70 local iptv_video = VideoInput.new("http://home.samfundet.no/~sesse/videostuff", Nageru.VIDEO_FORMAT_YCBCR)
71
72 function reload_cef()
73         cef_input:reload()
74         cef_input:execute_javascript_async("play()")
75 end
76
77 function disconnect_iptv()
78         iptv_video:disconnect()
79 end
80
81 -- Utility function to help creating many similar chains that can differ
82 -- in a free set of chosen parameters.
83 function make_cartesian_product(parms, callback)
84         return make_cartesian_product_internal(parms, callback, 1, {})
85 end
86
87 function make_cartesian_product_internal(parms, callback, index, args)
88         if index > #parms then
89                 return callback(unpack(args))
90         end
91         local ret = {}
92         for _, value in ipairs(parms[index]) do
93                 args[index] = value
94                 ret[value] = make_cartesian_product_internal(parms, callback, index + 1, args)
95         end
96         return ret
97 end
98
99 -- An overlay with variable alpha.
100 function make_overlay(chain, base)
101         local image = chain:add_html_input(cef_input)
102         local multiply_effect = chain:add_effect(MultiplyEffect.new())
103         local overlay_effect = chain:add_effect(OverlayEffect.new(), base, multiply_effect)
104         return {
105                 image = image,
106                 multiply_effect = multiply_effect,
107                 overlay_effect = overlay_effect
108         }
109 end
110
111 function possibly_make_overlay(has_overlay, chain, base)
112         if has_overlay == true then
113                 return make_overlay(chain, base)
114         else
115                 return nil
116         end
117 end
118
119 function make_fade_input(chain, signal, live, deint, scale, video)
120         local input, wb_effect, resample_effect, last
121         if video then
122                 input = chain:add_video_input(iptv_video, false)
123                 last = input
124         elseif live then
125                 input = chain:add_live_input(false, deint)
126                 input:connect_signal(signal)
127                 last = input
128         else
129                 input = chain:add_effect(ImageInput.new(cef_path .. "/nageru/dsn-bg.png"))
130                 last = input
131         end
132
133         -- If we cared about this for the non-main inputs, we would have
134         -- checked hq here and invoked ResizeEffect instead.
135         if scale then
136                 resample_effect = chain:add_effect(ResampleEffect.new())
137                 last = resample_effect
138         end
139
140         -- Make sure to put the white balance after the scaling (usually more efficient).
141         if live then
142                 wb_effect = chain:add_effect(WhiteBalanceEffect.new())
143                 last = wb_effect
144         end
145
146         return {
147                 input = input,
148                 wb_effect = wb_effect,
149                 resample_effect = resample_effect,
150                 last = last
151         }
152 end
153
154 -- A chain to fade between two inputs, of which either can be a picture
155 -- or a live input. In practice only used live, but we still support the
156 -- hq parameter.
157 function make_fade_chain(input0_live, input0_deint, input0_scale, input0_video, input1_live, input1_deint, input1_scale, input1_video, has_overlay, hq)
158         local chain = EffectChain.new(16, 9)
159
160         local input0 = make_fade_input(chain, INPUT0_SIGNAL_NUM, input0_live, input0_deint, input0_scale, input0_video)
161         local input1 = make_fade_input(chain, INPUT1_SIGNAL_NUM, input1_live, input1_deint, input1_scale, input1_video)
162
163         -- If fading between two live inputs, the overlay is put on top.
164         -- If fading between the static picture and a live input,
165         -- the overlay is put on the live input.
166         local overlay = nil
167         if input0_live and not input1_live then
168                 overlay = possibly_make_overlay(has_overlay, chain, input0.last)
169                 if overlay then
170                         input0.last = overlay.overlay_effect
171                 end
172         elseif input1_live and not input0_live then
173                 overlay = possibly_make_overlay(has_overlay, chain, input1.last)
174                 if overlay then
175                         input1.last = overlay.overlay_effect
176                 end
177         end
178
179         local mix_effect = chain:add_effect(MixEffect.new(), input0.last, input1.last)
180         if input0_live and input1_live then
181                 overlay = possibly_make_overlay(has_overlay, chain, mix_effect)
182         end
183
184         chain:finalize(hq)
185
186         return {
187                 chain = chain,
188                 input0 = input0,
189                 input1 = input1,
190                 mix_effect = mix_effect,
191                 overlay = overlay
192         }
193 end
194
195 -- Chains to fade between two inputs, in various configurations.
196 local fade_chains = make_cartesian_product({
197         {"video", "static", "live", "livedeint"},  -- input0_type
198         {true, false},                    -- input0_scale
199         {"video", "static", "live", "livedeint"},  -- input1_type
200         {true, false},                    -- input1_scale
201         {true, false},                    -- has_overlay
202         {true}                            -- hq
203 }, function(input0_type, input0_scale, input1_type, input1_scale, has_overlay, hq)
204         local input0_live = (input0_type ~= "static")
205         local input1_live = (input1_type ~= "static")
206         local input0_deint = (input0_type == "livedeint")
207         local input1_deint = (input1_type == "livedeint")
208         local input0_video = (input0_type == "video")
209         local input1_video = (input1_type == "video")
210         return make_fade_chain(input0_live, input0_deint, input0_scale, input0_video, input1_live, input1_deint, input1_scale, input1_video, has_overlay, hq)
211 end)
212
213 function make_sbs_input(chain, signal, deint, has_overlay, hq)
214         local input = chain:add_live_input(not deint, deint)  -- Override bounce only if not deinterlacing.
215         input:connect_signal(signal)
216
217         local wb_effect = chain:add_effect(WhiteBalanceEffect.new())  -- Needs to be before the overlay.
218
219         local overlay = nil
220         if has_overlay then
221                 overlay = make_overlay(chain, wb_effect)
222         end
223
224         local resample_effect = nil
225         local resize_effect = nil
226         if (hq) then
227                 resample_effect = chain:add_effect(ResampleEffect.new())
228         else
229                 resize_effect = chain:add_effect(ResizeEffect.new())
230         end
231
232         local padding_effect = chain:add_effect(IntegralPaddingEffect.new())
233
234         return {
235                 input = input,
236                 wb_effect = wb_effect,
237                 resample_effect = resample_effect,
238                 resize_effect = resize_effect,
239                 padding_effect = padding_effect,
240                 overlay = overlay
241         }
242 end
243
244 -- Side-by-side chains.
245 function make_sbs_chain(input0_type, input0_overlay, input1_type, hq)
246         local chain = EffectChain.new(16, 9)
247
248         local bg = chain:add_video_input(bg_video, false)
249
250         local input0 = make_sbs_input(chain, INPUT0_SIGNAL_NUM, input0_type == "livedeint", input0_overlay, hq)
251         local input1 = make_sbs_input(chain, INPUT4_SIGNAL_NUM, input1_type == "livedeint", false, hq)
252
253         input0.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0)
254         input1.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0)
255
256         local i0 = chain:add_effect(OverlayEffect.new(), bg, input0.padding_effect)
257         chain:add_effect(OverlayEffect.new(), i0, input1.padding_effect)
258         chain:finalize(hq)
259
260         return {
261                 chain = chain,
262                 input0 = input0,
263                 input1 = input1,
264                 overlay = input0.overlay  -- May be nil.
265         }
266 end
267
268
269 -- Make all possible combinations of side-by-side chains.
270 local sbs_chains = make_cartesian_product({
271         {"live", "livedeint"},  -- input0_type
272         {true, false},          -- input0_overlay
273         {"live", "livedeint"},  -- input1_type
274         {true, false}           -- hq
275 }, function(input0_type, input0_overlay, input1_type, hq)
276         return make_sbs_chain(input0_type, input0_overlay, input1_type, hq)
277 end)
278
279 function make_simple_chain_no_finalize(input_deint, input_video, input_scale, has_overlay, hq)
280         local chain = EffectChain.new(16, 9)
281
282         local input
283         if input_video then
284                 input = chain:add_video_input(iptv_video, false)
285         else
286                 input = chain:add_live_input(false, input_deint)
287                 input:connect_signal(0)  -- First input card. Can be changed whenever you want.
288         end
289
290         local resample_effect, resize_effect
291         if input_scale then
292                 if hq then
293                         resample_effect = chain:add_effect(ResampleEffect.new())
294                 else
295                         resize_effect = chain:add_effect(ResizeEffect.new())
296                 end
297         end
298
299         local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
300         local overlay = possibly_make_overlay(has_overlay, chain, wb_effect)
301
302         return {
303                 chain = chain,
304                 input = input,
305                 wb_effect = wb_effect,
306                 resample_effect = resample_effect,
307                 resize_effect = resize_effect,
308                 overlay = overlay
309         }
310 end
311
312 -- A chain to show a single input on screen.
313 function make_simple_chain(input_deint, input_video, input_scale, has_overlay, hq)
314         local chain = make_simple_chain_no_finalize(input_deint, input_video, input_scale, has_overlay, hq)
315         chain.chain:finalize(hq)
316         return chain
317 end
318
319 -- Make all possible combinations of single-input chains.
320 local simple_chains = make_cartesian_product({
321         {"video", "live", "livedeint"},  -- input_type
322         {true, false},          -- input_scale
323         {true, false},          -- has_overlay
324         {true, false}           -- hq
325 }, function(input_type, input_scale, has_overlay, hq)
326         local input_deint = (input_type == "livedeint")
327         local input_video = (input_type == "video")
328         return make_simple_chain(input_deint, input_video, input_scale, has_overlay, hq)
329 end)
330
331 -- A chain to show a single input on screen, with a stinger on top.
332 function make_stinger_chain(input_deint, input_video, input_scale, has_overlay, stinger_frame, hq)
333         local chain = make_simple_chain_no_finalize(input_deint, input_video, input_scale, has_overlay, hq)
334         local filename = cef_path .. "/stinger/blur" .. string.rep("0", 3 - string.len(tostring(stinger_frame))) .. stinger_frame .. ".png"
335
336         local last
337         if has_overlay then
338                 last = chain.overlay.overlay_effect
339         else
340                 last = chain.wb_effect
341         end
342         local input = chain.chain:add_effect(ImageInput.new(filename))
343         chain.chain:add_effect(OverlayEffect.new(), last, input)
344         chain.chain:finalize(hq)
345         return chain
346 end
347
348 -- Single-input chains with various stinger frames on top.
349 local stinger_chains = make_cartesian_product({
350         {"video", "live", "livedeint"},  -- input_type
351         {true, false},          -- input_scale
352         {true, false},          -- has_overlay
353         {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24},  -- stinger_frame
354         {true, false}           -- hq
355 }, function(input_type, input_scale, has_overlay, stinger_frame, hq)
356         local input_deint = (input_type == "livedeint")
357         local input_video = (input_type == "video")
358         return make_stinger_chain(input_deint, input_video, input_scale, has_overlay, stinger_frame, hq)
359 end)
360
361 -- A chain to show a single static picture on screen. Never with HTML overlay.
362 local static_chains = make_cartesian_product({
363         {true, false}            -- hq
364 }, function(hq)
365         local chain = EffectChain.new(16, 9)
366         local chain_input = chain:add_effect(ImageInput.new(cef_path .. "/nageru/dsn-bg.png"))
367
368         chain:finalize(hq)
369         return {
370                 chain = chain
371         }
372 end)
373
374 -- A chain to show the overlay and nothing more. LQ only,
375 -- since it is not a valid live signal.
376 local overlay_chain_lq = EffectChain.new(16, 9)
377 local overlay_chain_lq_input = overlay_chain_lq:add_html_input(cef_input)
378 overlay_chain_lq:finalize(false)
379
380 -- Used for indexing into the tables of chains.
381 function get_input_type(signals, signal_num)
382         if signal_num == STATIC_SIGNAL_NUM then
383                 return "static"
384         elseif signal_num == VIDEO_SIGNAL_NUM then
385                 return "video"
386         elseif signals:get_interlaced(signal_num) then
387                 return "livedeint"
388         else
389                 return "live"
390         end
391 end
392
393 function needs_scale(signals, signal_num, width, height)
394         if signal_num == STATIC_SIGNAL_NUM then
395                 -- We assume this is already correctly scaled at load time.
396                 return false
397         end
398         assert(is_plain_signal(signal_num))
399         return (signals:get_width(signal_num) ~= width or signals:get_height(signal_num) ~= height)
400 end
401
402 function set_scale_parameters_if_needed(chain_or_input, width, height)
403         if chain_or_input.resample_effect then
404                 chain_or_input.resample_effect:set_int("width", width)
405                 chain_or_input.resample_effect:set_int("height", height)
406         elseif chain_or_input.resize_effect then
407                 chain_or_input.resize_effect:set_int("width", width)
408                 chain_or_input.resize_effect:set_int("height", height)
409         end
410 end
411
412 -- API ENTRY POINT
413 -- Returns the number of outputs in addition to the live (0) and preview (1).
414 -- Called only once, at the start of the program.
415 function num_channels()
416         return NUM_CAMERAS + 4  -- sbs, static picture, video and overlay
417 end
418
419 function is_plain_signal(num)
420         return (num >= INPUT0_SIGNAL_NUM and num <= INPUT5_SIGNAL_NUM) or (num == VIDEO_SIGNAL_NUM)
421 end
422
423 -- Helper function to write e.g. “720p60”. The difference between this
424 -- and get_channel_resolution_raw() is that this one also can say that
425 -- there's no signal.
426 function get_channel_resolution(signal_num)
427         local res = last_resolution[signal_num]
428         if (not res) or not res.is_connected then
429                 return "disconnected"
430         end
431         if res.height <= 0 then
432                 return "no signal"
433         end
434         if not res.has_signal then
435                 if res.height == 525 then
436                         -- Special mode for the USB3 cards.
437                         return "no signal"
438                 end
439                 return get_channel_resolution_raw(res) .. ", no signal"
440         else
441                 return get_channel_resolution_raw(res)
442         end
443 end
444
445 -- Helper function to write e.g. “60” or “59.94”.
446 function get_frame_rate(res)
447         local nom = res.frame_rate_nom
448         local den = res.frame_rate_den
449         if nom % den == 0 then
450                 return nom / den
451         else
452                 return string.format("%.2f", nom / den)
453         end
454 end
455
456 -- Helper function to write e.g. “720p60”.
457 function get_channel_resolution_raw(res)
458         if res.interlaced then
459                 return res.height .. "i" .. get_frame_rate(res)
460         else
461                 return res.height .. "p" .. get_frame_rate(res)
462         end
463 end
464
465 -- API ENTRY POINT
466 -- Returns the name for each additional channel (starting from 2).
467 -- Called at the start of the program, and then each frame for live
468 -- channels in case they change resolution.
469 function channel_name(channel)
470         local signal_num = channel - 2
471         if signal_num == INPUT0_SIGNAL_NUM then
472                 return "Main (" .. get_channel_resolution(signal_num) .. ")"
473         elseif signal_num == INPUT1_SIGNAL_NUM then
474                 return "Secondary (" .. get_channel_resolution(signal_num) .. ")"
475         elseif signal_num == INPUT2_SIGNAL_NUM then
476                 return "Goal L (" .. get_channel_resolution(signal_num) .. ")"
477         elseif signal_num == INPUT3_SIGNAL_NUM then
478                 return "Goal R (" .. get_channel_resolution(signal_num) .. ")"
479         elseif signal_num == INPUT4_SIGNAL_NUM then
480                 return "Commentators (" .. get_channel_resolution(signal_num) .. ")"
481         elseif signal_num == INPUT5_SIGNAL_NUM then
482                 return "Laptop (" .. get_channel_resolution(signal_num) .. ")"
483         elseif signal_num == SBS_SIGNAL_NUM then
484                 return "Side-by-side"
485         elseif signal_num == STATIC_SIGNAL_NUM then
486                 return "Static picture"
487         elseif signal_num == VIDEO_SIGNAL_NUM then
488                 return "IPTV input"
489         elseif signal_num == OVERLAY_SIGNAL_NUM then
490                 return "Overlay"
491         end
492 end
493
494 -- API ENTRY POINT
495 -- Returns, given a channel number, which signal it corresponds to (starting from 0).
496 -- Should return -1 if the channel does not correspond to a simple signal.
497 -- (The information is used for whether right-click on the channel should bring up
498 -- an input selector or not.)
499 -- Called once for each channel, at the start of the program.
500 -- Will never be called for live (0) or preview (1).
501 function channel_signal(channel)
502         if channel - 2 == VIDEO_SIGNAL_NUM then
503                 return iptv_video:get_signal_num()
504         elseif is_plain_signal(channel - 2) then
505                 return channel - 2
506         else
507                 return -1
508         end
509 end
510
511 -- API ENTRY POINT
512 -- Called every frame. Returns the color (if any) to paint around the given
513 -- channel. Returns a CSS color (typically to mark live and preview signals);
514 -- "transparent" is allowed.
515 -- Will never be called for live (0) or preview (1).
516 function channel_color(channel)
517         if state.transition_type ~= NO_TRANSITION then
518                 if channel_involved_in(channel, state.transition_src_signal) or
519                    channel_involved_in(channel, state.transition_dst_signal) then
520                         return "#f00"
521                 end
522         else
523                 if channel_involved_in(channel, state.live_signal_num) then
524                         return "#f00"
525                 end
526         end
527         if channel_involved_in(channel, state.preview_signal_num) then
528                 return "#0f0"
529         end
530         return "transparent"
531 end
532
533 function channel_involved_in(channel, signal_num)
534         if is_plain_signal(signal_num) then
535                 return channel == (signal_num + 2)
536         end
537         if signal_num == SBS_SIGNAL_NUM then
538                 return is_sbs_participating_signal(channel - 2)
539         end
540         if signal_num == STATIC_SIGNAL_NUM then
541                 return (channel == NUM_CAMERAS)
542         end
543         if signal_num == VIDEO_SIGNAL_NUM then
544                 return (channel == NUM_CAMERAS + 1)
545         end
546         return false
547 end
548
549 -- API ENTRY POINT
550 -- Returns if a given channel supports setting white balance (starting from 2).
551 -- Called only once for each channel, at the start of the program.
552 function supports_set_wb(channel)
553         return is_plain_signal(channel - 2)
554 end
555
556 -- API ENTRY POINT
557 -- Gets called with a new gray point when the white balance is changing.
558 -- The color is in linear light (not sRGB gamma).
559 function set_wb(channel, red, green, blue)
560         if is_plain_signal(channel - 2) then
561                 state.neutral_colors[channel - 2 + 1] = { red, green, blue }
562         end
563 end
564
565 function finish_transitions(t)
566         if state.transition_type ~= NO_TRANSITION and t >= state.transition_end then
567                 state.live_signal_num = state.transition_dst_signal
568                 state.transition_type = NO_TRANSITION
569         end
570
571         -- Disable the overlay if it is no longer visible.
572         if state.overlay_enabled and t > state.overlay_transition_end and state.overlay_alpha_dst == 0.0 then
573                 state.overlay_enabled = false
574                 print("Turning off overlay")
575         end
576 end
577
578 function in_transition(t)
579         return t >= state.transition_start and t <= state.transition_end
580 end
581
582 function is_sbs_participating_signal(signal_num)
583         return signal_num == INPUT0_SIGNAL_NUM or signal_num == INPUT4_SIGNAL_NUM
584 end
585
586 function simple_signal_has_overlay(signal_num)
587         -- The commentator output has no overlay on it.
588         return signal_num ~= INPUT4_SIGNAL_NUM
589 end
590
591 -- API ENTRY POINT
592 -- Called every frame.
593 function get_transitions(t)
594         if state.preview_signal_num == OVERLAY_SIGNAL_NUM then
595                 if t < state.overlay_transition_end then
596                         -- Fade in progress.
597                         return {}
598                 end
599                 if state.overlay_enabled then
600                         return {"Overlay off", "", "Fade ovl out"}
601                 else
602                         return {"Overlay on", "", "Fade ovl in"}
603                 end
604         end
605
606         if in_transition(t) then
607                 -- Transition already in progress, the only thing we can do is really
608                 -- cut to the preview. (TODO: Make an “abort” and/or “finish”, too?)
609                 return {"Cut"}
610         end
611
612         if state.live_signal_num == state.preview_signal_num then
613                 -- No transitions possible.
614                 return {}
615         end
616
617         if (is_plain_signal(state.live_signal_num) and state.preview_signal_num == VIDEO_SIGNAL_NUM) or
618            (is_plain_signal(state.preview_signal_num) and state.live_signal_num == VIDEO_SIGNAL_NUM) then
619                 return {"Cut", "Sting", "Fade"}
620         end
621
622         if (is_plain_signal(state.live_signal_num) or state.live_signal_num == STATIC_SIGNAL_NUM) and
623            (is_plain_signal(state.preview_signal_num) or state.preview_signal_num == STATIC_SIGNAL_NUM) then
624                 return {"Cut", "", "Fade"}
625         end
626
627         -- Various zooms.
628         if state.live_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(state.preview_signal_num) then
629                 return {"Cut", "Zoom in"}
630         elseif is_sbs_participating_signal(state.live_signal_num) and state.preview_signal_num == SBS_SIGNAL_NUM then
631                 return {"Cut", "Zoom out"}
632         end
633
634         return {"Cut"}
635 end
636
637 function swap_preview_live()
638         local temp = state.live_signal_num
639         state.live_signal_num = state.preview_signal_num
640         state.preview_signal_num = temp
641 end
642
643 function start_transition(type_, t, duration)
644         state.transition_start = t
645         state.transition_end = t + duration
646         state.transition_type = type_
647         state.transition_src_signal = state.live_signal_num
648         state.transition_dst_signal = state.preview_signal_num
649         state.stinger_in_progress = false
650         swap_preview_live()
651 end
652
653 -- API ENTRY POINT
654 -- Called when the user clicks a transition button.
655 function transition_clicked(num, t)
656         if state.preview_signal_num == OVERLAY_SIGNAL_NUM then
657                 if num == 0 then
658                         -- Cut.
659                         state.overlay_transition_start = -2.0
660                         state.overlay_transition_end = -1.0
661                         if state.overlay_enabled then
662                                 state.overlay_enabled = false
663                                 state.overlay_alpha_src = 0.0
664                                 state.overlay_alpha_dst = 0.0
665                         else
666                                 state.overlay_enabled = true
667                                 state.overlay_alpha_src = 1.0
668                                 state.overlay_alpha_dst = 1.0
669                         end
670                 elseif num == 2 then
671                         -- Fade.
672                         state.overlay_transition_start = t
673                         state.overlay_transition_end = t + 1.0
674                         if state.overlay_enabled then
675                                 state.overlay_alpha_src = 1.0
676                                 state.overlay_alpha_dst = 0.0
677                         else
678                                 state.overlay_alpha_src = 0.0
679                                 state.overlay_alpha_dst = 1.0
680                         end
681                         state.overlay_enabled = true
682                 end
683                 return
684         end
685
686         if num == 0 then
687                 -- Cut.
688                 if in_transition(t) then
689                         -- Ongoing transition; finish it immediately before the cut.
690                         finish_transitions(state.transition_end)
691                 end
692
693                 swap_preview_live()
694                 state.stinger_in_progress = false
695         elseif num == 1 then
696                 -- Zoom or sting.
697                 finish_transitions(t)
698
699                 if state.live_signal_num == state.preview_signal_num then
700                         -- Nothing to do.
701                         return
702                 end
703
704                 if (is_plain_signal(state.live_signal_num) and state.preview_signal_num == VIDEO_SIGNAL_NUM) or
705                    (is_plain_signal(state.preview_signal_num) and state.live_signal_num == VIDEO_SIGNAL_NUM) then
706                         -- Sting.
707                         if stinger_in_progress then
708                                 return
709                         end
710
711                         state.stinger_in_progress = true
712                         state.stinger_frame = 0
713                         state.stinger_src_signal = state.live_signal_num
714                         state.stinger_dst_signal = state.preview_signal_num
715                         return
716                 end
717
718                 if is_plain_signal(state.live_signal_num) and is_plain_signal(state.preview_signal_num) then
719                         -- We can't zoom between these. Just make a cut.
720                         io.write("Cutting from " .. state.live_signal_num .. " to " .. state.live_signal_num .. "\n")
721                         swap_preview_live()
722                         state.stinger_in_progress = false
723                         return
724                 end
725
726                 if (state.live_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(state.preview_signal_num)) or
727                    (state.preview_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(state.live_signal_num)) then
728                         start_transition(ZOOM_TRANSITION, t, 1.0)
729                 end
730         elseif num == 2 then
731                 finish_transitions(t)
732
733                 -- Fade.
734                 if (state.live_signal_num ~= state.preview_signal_num) and
735                    (is_plain_signal(state.live_signal_num) or
736                     state.live_signal_num == STATIC_SIGNAL_NUM) and
737                    (is_plain_signal(state.preview_signal_num) or
738                     state.preview_signal_num == STATIC_SIGNAL_NUM) then
739                         start_transition(FADE_TRANSITION, t, 1.0)
740                 else
741                         -- Fades involving SBS are ignored (we have no chain for it).
742                 end
743         end
744 end
745
746 -- API ENTRY POINT
747 function channel_clicked(num)
748         state.preview_signal_num = num
749 end
750
751 function get_fade_chain(state, signals, t, width, height, input_resolution)
752         local input0_type = get_input_type(signals, state.transition_src_signal)
753         local input0_scale = needs_scale(signals, state.transition_src_signal, width, height)
754         local input1_type = get_input_type(signals, state.transition_dst_signal)
755         local input1_scale = needs_scale(signals, state.transition_dst_signal, width, height)
756         local chain = fade_chains[input0_type][input0_scale][input1_type][input1_scale][state.overlay_enabled][true]
757         local prepare = function()
758                 if input0_type == "live" or input0_type == "livedeint" then
759                         chain.input0.input:connect_signal(state.transition_src_signal)
760                 end
761                 if input0_type ~= "static" then
762                         set_neutral_color_from_signal(state, chain.input0.wb_effect, state.transition_src_signal)
763                 end
764                 set_scale_parameters_if_needed(chain.input0, width, height)
765                 if input1_type == "live" or input1_type == "livedeint" then
766                         chain.input1.input:connect_signal(state.transition_dst_signal)
767                 end
768                 if input1_type ~= "static" then
769                         set_neutral_color_from_signal(state, chain.input1.wb_effect, state.transition_dst_signal)
770                 end
771                 set_scale_parameters_if_needed(chain.input1, width, height)
772                 local tt = calc_fade_progress(t, state.transition_start, state.transition_end)
773
774                 chain.mix_effect:set_float("strength_first", 1.0 - tt)
775                 chain.mix_effect:set_float("strength_second", tt)
776
777                 -- The commentator output has no overlay on it.
778                 local extra_alpha_factor = 1.0
779                 if not simple_signal_has_overlay(state.transition_src_signal) and
780                    not simple_signal_has_overlay(state.transition_dst_signal) then
781                         extra_alpha_factor = 0.0
782                 elseif not simple_signal_has_overlay(state.transition_src_signal) then
783                         extra_alpha_factor = tt
784                 elseif not simple_signal_has_overlay(state.transition_dst_signal) then
785                         extra_alpha_factor = 1.0 - tt
786                 end
787                 prepare_overlay_live(state, chain, t, extra_alpha_factor)
788         end
789         return chain.chain, prepare
790 end
791
792 -- SBS code (live_signal_num == SBS_SIGNAL_NUM, or in a transition to/from it).
793 function get_sbs_chain(state, signals)
794         local input0_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
795         local input1_type = get_input_type(signals, INPUT4_SIGNAL_NUM)
796         return sbs_chains[input0_type][state.overlay_enabled][input1_type][true]
797 end
798
799 local last_rate = 0.0
800
801 -- API ENTRY POINT
802 -- Called every frame. Get the chain for displaying at input <num>,
803 -- where 0 is live, 1 is preview, 2 is the first channel to display
804 -- in the bottom bar, and so on up to num_channels()+1. t is the
805 -- current time in seconds. width and height are the dimensions of
806 -- the output, although you can ignore them if you don't need them
807 -- (they're useful if you want to e.g. know what to resample by).
808 --
809 -- <signals> is basically an exposed InputState, which you can use to
810 -- query for information about the signals at the point of the current
811 -- frame. In particular, you can call get_width() and get_height()
812 -- for any signal number, and use that to e.g. assist in chain selection.
813 --
814 -- You should return two objects; the chain itself, and then a
815 -- function (taking no parameters) that is run just before rendering.
816 -- The function needs to call connect_signal on any inputs, so that
817 -- it gets updated video data for the given frame. (You are allowed
818 -- to switch which input your input is getting from between frames,
819 -- but not calling connect_signal results in undefined behavior.)
820 -- If you want to change any parameters in the chain, this is also
821 -- the right place.
822 --
823 -- NOTE: The chain returned must be finalized with the Y'CbCr flag
824 -- if and only if num==0.
825 function get_chain(num, t, width, height, signals)
826         local input_resolution = {}
827         for signal_num=0,(NUM_CAMERAS-1) do
828                 local res = {
829                         width = signals:get_width(signal_num),
830                         height = signals:get_height(signal_num),
831                         interlaced = signals:get_interlaced(signal_num),
832                         has_signal = signals:get_has_signal(signal_num),
833                         is_connected = signals:get_is_connected(signal_num),
834                         frame_rate_nom = signals:get_frame_rate_nom(signal_num),
835                         frame_rate_den = signals:get_frame_rate_den(signal_num)
836                 }
837
838                 if res.interlaced then
839                         -- Convert height from frame height to field height.
840                         -- (Needed for e.g. place_rectangle.)
841                         res.height = res.height * 2
842
843                         -- Show field rate instead of frame rate; really for cosmetics only
844                         -- (and actually contrary to EBU recommendations, although in line
845                         -- with typical user expectations).
846                         res.frame_rate_nom = res.frame_rate_nom * 2
847                 end
848
849                 input_resolution[signal_num] = res
850         end
851         last_resolution = input_resolution
852
853         -- Make a (semi-shallow) copy of the current state, so that the returned prepare function
854         -- is unaffected by state changes made by the UI before it is rendered.
855         local state_copy = {}
856         for key, value in pairs(state) do
857                 state_copy[key] = value
858         end
859         for key, value in pairs(state.neutral_colors) do
860                 state_copy.neutral_colors[key] = value
861         end
862
863         -- Save some CPU time if we're not having SBS on live.
864         local new_rate
865         if state.live_signal_num == SBS_SIGNAL_NUM or
866            state.preview_signal_num == SBS_SIGNAL_NUM or
867            state.transition_type == ZOOM_TRANSITION then
868                 new_rate = 1.0
869         else
870                 new_rate = 0.0001
871         end
872         if new_rate ~= last_rate then
873                 -- Avoid waking up the video thread (which may be sleeping) if the rate is the same.
874                 bg_video:change_rate(new_rate)
875                 last_rate = new_rate
876         end
877
878         if num == 0 then  -- Live.
879                 -- See if we're in a transition.
880                 finish_transitions(t)
881                 if state.transition_type == ZOOM_TRANSITION then
882                         -- Transition in or out of SBS.
883                         local chain = get_sbs_chain(state_copy, signals)
884                         local prepare = function()
885                                 prepare_sbs_chain(state_copy, chain, calc_zoom_progress(t), state_copy.transition_type, state_copy.transition_src_signal, state_copy.transition_dst_signal, width, height, input_resolution)
886                                 prepare_overlay_live(state_copy, chain, t, 1.0)
887                         end
888                         return chain.chain, prepare
889                 elseif state.transition_type == NO_TRANSITION and state.live_signal_num == SBS_SIGNAL_NUM then
890                         -- Static SBS view.
891                         local chain = get_sbs_chain(state_copy, signals)
892                         local prepare = function()
893                                 prepare_sbs_chain(state_copy, chain, 0.0, NO_TRANSITION, 0, SBS_SIGNAL_NUM, width, height, input_resolution)
894                                 prepare_overlay_live(state_copy, chain, t, 1.0)
895                         end
896                         return chain.chain, prepare
897                 elseif state.transition_type == FADE_TRANSITION then
898                         return get_fade_chain(state_copy, signals, t, width, height, input_resolution)
899                 elseif is_plain_signal(state.live_signal_num) then
900                         local input_type = get_input_type(signals, state.live_signal_num)
901                         local input_scale = needs_scale(signals, state.live_signal_num, width, height)
902                         local overlay_really_enabled = state.overlay_enabled and simple_signal_has_overlay(state.live_signal_num)
903                         local chain
904                         if state.stinger_in_progress then
905                                 chain = stinger_chains[input_type][input_scale][overlay_really_enabled][state.stinger_frame][true]
906                                 state.stinger_frame = state.stinger_frame + 1
907                                 if state.stinger_frame >= 25 then
908                                         state.stinger_in_progress = false
909                                         state.preview_signal_num = state.stinger_src_signal
910                                         state.live_signal_num = state.stinger_dst_signal
911
912                                         if state.stinger_dst_signal == VIDEO_SIGNAL_NUM then
913                                                 -- Turn off the overlay when playing video.
914                                                 state.stinger_save_overlay = state.overlay_enabled
915                                                 state.overlay_enabled = false
916                                         else
917                                                 -- Restore the state.
918                                                 state.overlay_enabled = state.stinger_save_overlay
919                                         end
920                                 end
921                         else
922                                 chain = simple_chains[input_type][input_scale][overlay_really_enabled][true]
923                         end
924                         local prepare = function()
925                                 if input_type ~= "video" then
926                                         chain.input:connect_signal(state.live_signal_num)
927                                 end
928                                 set_neutral_color_from_signal(state_copy, chain.wb_effect, state.live_signal_num)
929                                 set_scale_parameters_if_needed(chain, width, height)
930                                 prepare_overlay_live(state_copy, chain, t, 1.0)
931                         end
932                         return chain.chain, prepare
933                 elseif state.live_signal_num == STATIC_SIGNAL_NUM then  -- Static picture.
934                         local chain = static_chains[true]
935                         local prepare = function()
936                                 prepare_overlay_live(state_copy, chain, t, 1.0)  -- FIXME: Should this ever be here?
937                         end
938                         return chain.chain, prepare
939                 else
940                         assert(false)
941                 end
942         end
943
944         -- We do not show overlays on the individual preview inputs.
945         -- The M/E preview matches what we'd put live by doing a transition, as always.
946         local show_overlay = false
947         if num == 1 then  -- Preview.
948                 if state.preview_signal_num == OVERLAY_SIGNAL_NUM then
949                         num = state.live_signal_num + 2
950                         show_overlay = not state.overlay_enabled
951
952                         if state.transition_type ~= NO_TRANSITION then
953                                 num = state.transition_dst_signal + 2
954                         end
955                 else
956                         num = state.preview_signal_num + 2
957                         show_overlay = state.overlay_enabled and simple_signal_has_overlay(state.preview_signal_num)
958                 end
959         end
960
961         -- Individual preview inputs (usually without overlay).
962         if is_plain_signal(num - 2) then
963                 local signal_num = num - 2
964                 local input_type = get_input_type(signals, signal_num)
965                 local input_scale = needs_scale(signals, signal_num, width, height)
966                 local chain = simple_chains[input_type][input_scale][show_overlay][false]
967                 local prepare = function()
968                         if input_type ~= "video" then
969                                 chain.input:connect_signal(signal_num)
970                         end
971                         set_neutral_color(chain.wb_effect, state_copy.neutral_colors[signal_num + 1])
972                         set_scale_parameters_if_needed(chain, width, height)
973                         prepare_overlay_static(chain, t)
974                 end
975                 return chain.chain, prepare
976         end
977         if num == SBS_SIGNAL_NUM + 2 then
978                 local input0_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
979                 local input1_type = get_input_type(signals, INPUT4_SIGNAL_NUM)
980                 local chain = sbs_chains[input0_type][show_overlay][input1_type][false]
981                 local prepare = function()
982                         prepare_sbs_chain(state_copy, chain, 0.0, NO_TRANSITION, 0, SBS_SIGNAL_NUM, width, height, input_resolution)
983                 end
984                 return chain.chain, prepare
985         end
986         if num == STATIC_SIGNAL_NUM + 2 then
987                 local chain = static_chains[false]
988                 local prepare = function()
989                         prepare_overlay_static(chain, t)
990                 end
991                 return chain.chain, prepare
992         end
993         if num == OVERLAY_SIGNAL_NUM + 2 then
994                 local prepare = function()
995 --                      prepare_overlay(overlay_chain_lq, t)
996                 end
997                 return overlay_chain_lq, prepare
998         end
999 end
1000
1001 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
1002 function round(x)
1003         return math.floor(x + 0.5)
1004 end
1005
1006 function lerp(a, b, t)
1007         return a + (b - a) * t
1008 end
1009
1010 function lerp_pos(a, b, t)
1011         return {
1012                 x0 = lerp(a.x0, b.x0, t),
1013                 y0 = lerp(a.y0, b.y0, t),
1014                 x1 = lerp(a.x1, b.x1, t),
1015                 y1 = lerp(a.y1, b.y1, t)
1016         }
1017 end
1018
1019 function pos_from_top_left(x, y, width, height, screen_width, screen_height)
1020         local xs = screen_width / 1280.0
1021         local ys = screen_height / 720.0
1022         return {
1023                 x0 = round(xs * x),
1024                 y0 = round(ys * y),
1025                 x1 = round(xs * (x + width)),
1026                 y1 = round(ys * (y + height))
1027         }
1028 end
1029
1030 function prepare_sbs_chain(state, chain, t, transition_type, src_signal, dst_signal, screen_width, screen_height, input_resolution)
1031         chain.input0.input:connect_signal(0)
1032         chain.input1.input:connect_signal(4)
1033         set_neutral_color(chain.input0.wb_effect, state.neutral_colors[1])
1034         set_neutral_color(chain.input1.wb_effect, state.neutral_colors[5])
1035
1036         -- Both inputs are the same size (true side-by-side).
1037         local pos0 = pos_from_top_left(1280 - 616 - 16, 186, 616, 347, screen_width, screen_height)
1038         local pos1 = pos_from_top_left(16, 186, 616, 347, screen_width, screen_height)
1039
1040         local pos_fs = { x0 = 0, y0 = 0, x1 = screen_width, y1 = screen_height }
1041         local affine_param
1042         if transition_type == NO_TRANSITION then
1043                 -- Static SBS view.
1044                 affine_param = { sx = 1.0, sy = 1.0, tx = 0.0, ty = 0.0 }   -- Identity.
1045         else
1046                 -- Zooming to/from SBS view into or out of a single view.
1047                 assert(transition_type == ZOOM_TRANSITION)
1048                 local signal, real_t
1049                 if src_signal == SBS_SIGNAL_NUM then
1050                         signal = dst_signal
1051                         real_t = t
1052                 else
1053                         assert(dst_signal == SBS_SIGNAL_NUM)
1054                         signal = src_signal
1055                         real_t = 1.0 - t
1056                 end
1057
1058                 if signal == INPUT0_SIGNAL_NUM then
1059                         affine_param = find_affine_param(pos0, lerp_pos(pos0, pos_fs, real_t))
1060                 elseif signal == INPUT4_SIGNAL_NUM then
1061                         affine_param = find_affine_param(pos1, lerp_pos(pos1, pos_fs, real_t))
1062                 end
1063         end
1064
1065         -- NOTE: input_resolution is not 1-indexed, unlike usual Lua arrays.
1066         place_rectangle_with_affine(chain.input0.resample_effect, chain.input0.resize_effect, chain.input0.padding_effect, pos0, affine_param, screen_width, screen_height, input_resolution[0].width, input_resolution[0].height)
1067         place_rectangle_with_affine(chain.input1.resample_effect, chain.input1.resize_effect, chain.input1.padding_effect, pos1, affine_param, screen_width, screen_height, input_resolution[1].width, input_resolution[1].height)
1068 end
1069
1070 -- Find the transformation that changes the first rectangle to the second one.
1071 function find_affine_param(a, b)
1072         local sx = (b.x1 - b.x0) / (a.x1 - a.x0)
1073         local sy = (b.y1 - b.y0) / (a.y1 - a.y0)
1074         return {
1075                 sx = sx,
1076                 sy = sy,
1077                 tx = b.x0 - a.x0 * sx,
1078                 ty = b.y0 - a.y0 * sy
1079         }
1080 end
1081
1082 function place_rectangle_with_affine(resample_effect, resize_effect, padding_effect, pos, aff, screen_width, screen_height, input_width, input_height)
1083         local x0 = pos.x0 * aff.sx + aff.tx
1084         local x1 = pos.x1 * aff.sx + aff.tx
1085         local y0 = pos.y0 * aff.sy + aff.ty
1086         local y1 = pos.y1 * aff.sy + aff.ty
1087
1088         place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height)
1089 end
1090
1091 function place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height)
1092         local srcx0 = 0.0
1093         local srcx1 = 1.0
1094         local srcy0 = 0.0
1095         local srcy1 = 1.0
1096
1097         padding_effect:set_int("width", screen_width)
1098         padding_effect:set_int("height", screen_height)
1099
1100         -- Cull.
1101         if x0 > screen_width or x1 < 0.0 or y0 > screen_height or y1 < 0.0 then
1102                 if resample_effect ~= nil then
1103                         resample_effect:set_int("width", 1)
1104                         resample_effect:set_int("height", 1)
1105                         resample_effect:set_float("zoom_x", screen_width)
1106                         resample_effect:set_float("zoom_y", screen_height)
1107                 else
1108                         resize_effect:set_int("width", 1)
1109                         resize_effect:set_int("height", 1)
1110                 end
1111                 padding_effect:set_int("left", screen_width + 100)
1112                 padding_effect:set_int("top", screen_height + 100)
1113                 return
1114         end
1115
1116         -- Clip.
1117         if x0 < 0 then
1118                 srcx0 = -x0 / (x1 - x0)
1119                 x0 = 0
1120         end
1121         if y0 < 0 then
1122                 srcy0 = -y0 / (y1 - y0)
1123                 y0 = 0
1124         end
1125         if x1 > screen_width then
1126                 srcx1 = (screen_width - x0) / (x1 - x0)
1127                 x1 = screen_width
1128         end
1129         if y1 > screen_height then
1130                 srcy1 = (screen_height - y0) / (y1 - y0)
1131                 y1 = screen_height
1132         end
1133
1134         if resample_effect ~= nil then
1135                 -- High-quality resampling.
1136                 local x_subpixel_offset = x0 - math.floor(x0)
1137                 local y_subpixel_offset = y0 - math.floor(y0)
1138
1139                 -- Resampling must be to an integral number of pixels. Round up,
1140                 -- and then add an extra pixel so we have some leeway for the border.
1141                 local width = math.ceil(x1 - x0) + 1
1142                 local height = math.ceil(y1 - y0) + 1
1143                 resample_effect:set_int("width", width)
1144                 resample_effect:set_int("height", height)
1145
1146                 -- Correct the discrepancy with zoom. (This will leave a small
1147                 -- excess edge of pixels and subpixels, which we'll correct for soon.)
1148                 local zoom_x = (x1 - x0) / (width * (srcx1 - srcx0))
1149                 local zoom_y = (y1 - y0) / (height * (srcy1 - srcy0))
1150                 resample_effect:set_float("zoom_x", zoom_x)
1151                 resample_effect:set_float("zoom_y", zoom_y)
1152                 resample_effect:set_float("zoom_center_x", 0.0)
1153                 resample_effect:set_float("zoom_center_y", 0.0)
1154
1155                 -- Padding must also be to a whole-pixel offset.
1156                 padding_effect:set_int("left", math.floor(x0))
1157                 padding_effect:set_int("top", math.floor(y0))
1158
1159                 -- Correct _that_ discrepancy by subpixel offset in the resampling.
1160                 resample_effect:set_float("left", srcx0 * input_width - x_subpixel_offset / zoom_x)
1161                 resample_effect:set_float("top", srcy0 * input_height - y_subpixel_offset / zoom_y)
1162
1163                 -- Finally, adjust the border so it is exactly where we want it.
1164                 padding_effect:set_float("border_offset_left", x_subpixel_offset)
1165                 padding_effect:set_float("border_offset_right", x1 - (math.floor(x0) + width))
1166                 padding_effect:set_float("border_offset_top", y_subpixel_offset)
1167                 padding_effect:set_float("border_offset_bottom", y1 - (math.floor(y0) + height))
1168         else
1169                 -- Lower-quality simple resizing.
1170                 local width = round(x1 - x0)
1171                 local height = round(y1 - y0)
1172                 resize_effect:set_int("width", width)
1173                 resize_effect:set_int("height", height)
1174
1175                 -- Padding must also be to a whole-pixel offset.
1176                 padding_effect:set_int("left", math.floor(x0))
1177                 padding_effect:set_int("top", math.floor(y0))
1178         end
1179 end
1180
1181 function prepare_overlay_live(state, chain, t, extra_alpha_factor)
1182         if chain.overlay then
1183                 local tt = calc_fade_progress(t, state.overlay_transition_start, state.overlay_transition_end)
1184                 local overlay_alpha = state.overlay_alpha_src + tt * (state.overlay_alpha_dst - state.overlay_alpha_src)
1185                 overlay_alpha = overlay_alpha * extra_alpha_factor
1186                 --print("overlay_alpha=" .. overlay_alpha .. " [" .. state.overlay_alpha_src .. "," .. state.overlay_alpha_dst .. "]@" .. tt)
1187                 if t > state.overlay_transition_end and state.overlay_alpha_dst == 0.0 then
1188                         state.overlay_enabled = false  -- Takes effect next frame.
1189         --              print("Turning off overlay")
1190                 end
1191                 chain.overlay.multiply_effect:set_vec4("factor", overlay_alpha, overlay_alpha, overlay_alpha, overlay_alpha)
1192         end
1193 end
1194
1195 function prepare_overlay_static(chain)
1196         if chain.overlay then
1197                 chain.overlay.multiply_effect:set_vec4("factor", 1.0, 1.0, 1.0, 1.0)
1198         end
1199 end
1200
1201 function set_neutral_color(effect, color)
1202         effect:set_vec3("neutral_color", color[1], color[2], color[3])
1203 end
1204
1205 function set_neutral_color_from_signal(state, effect, signal)
1206         if is_plain_signal(signal) then
1207                 set_neutral_color(effect, state.neutral_colors[signal - INPUT0_SIGNAL_NUM + 1])
1208         end
1209 end
1210
1211 function calc_zoom_progress(t)
1212         if t < state.transition_start then
1213                 return 0.0
1214         elseif t > state.transition_end then
1215                 return 1.0
1216         else
1217                 local tt = (t - state.transition_start) / (state.transition_end - state.transition_start)
1218                 -- Smooth it a bit.
1219                 return math.sin(tt * 3.14159265358 * 0.5)
1220         end
1221 end
1222
1223 function calc_fade_progress(t, transition_start, transition_end)
1224         local tt = (t - transition_start) / (transition_end - transition_start)
1225         if tt < 0.0 then
1226                 return 0.0
1227         elseif tt > 1.0 then
1228                 return 1.0
1229         end
1230
1231         -- Make the fade look maybe a tad more natural, by pumping it
1232         -- through a sigmoid function.
1233         tt = 10.0 * tt - 5.0
1234         tt = 1.0 / (1.0 + math.exp(-tt))
1235
1236         return tt
1237 end
1238
1239 ThemeMenu.set(
1240         { "Reload overlay", reload_cef },
1241         { "Disconnect IPTV", disconnect_iptv }
1242 )