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