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