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