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