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