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