]> git.sesse.net Git - ultimatescore/blob - nageru/ultimate.lua
Make the roster scripts executable.
[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 local static_image = ImageInput.new(cef_path .. "/nageru/dsn-bg.png")
75
76 function reload_cef()
77         cef_input:reload()
78         cef_input:execute_javascript_async("play()")
79 end
80
81 function disconnect_iptv()
82         iptv_video:disconnect()
83 end
84
85 -- An overlay with variable alpha.
86 function make_overlay(scene, base)
87         local image = scene:add_input(cef_input)
88         local multiply_effect = scene:add_effect(MultiplyEffect.new())
89         local overlay_effect = scene:add_optional_effect(OverlayEffect.new(), base, multiply_effect)
90         return {
91                 image = image,
92                 multiply_effect = multiply_effect,
93                 overlay_effect = overlay_effect
94         }
95 end
96
97 function make_fade_input(scene)
98         local ret = {
99                 input = scene:add_input(),
100                 resample_effect = scene:add_optional_effect(ResampleEffect.new()),  -- Activated if scaling.
101                 wb_effect = scene:add_optional_effect(WhiteBalanceEffect.new())  -- Activated for video inputs.
102         }
103         ret.overlay = make_overlay(scene, ret.wb_effect)
104         return ret
105 end
106
107 -- A scene to fade between two inputs, of which either can be a picture
108 -- or a live input.
109 function make_fade_scene()
110         local scene = Scene.new(16, 9)
111
112         -- If fading between two live inputs, the overlay is put on top.
113         -- If fading between the static picture and a live input,
114         -- the overlay is put on the live input.
115         local input0 = make_fade_input(scene)
116         local input1 = make_fade_input(scene)
117         local mix_effect = scene:add_effect(MixEffect.new(), input0.overlay.overlay_effect, input1.overlay.overlay_effect)
118         local overlay = make_overlay(scene, mix_effect)
119
120         -- At most one overlay can be active at any given time.
121         input0.overlay.overlay_effect:promise_to_disable_if_enabled(input1.overlay.overlay_effect)
122         input0.overlay.overlay_effect:promise_to_disable_if_enabled(overlay.overlay_effect)
123         input1.overlay.overlay_effect:promise_to_disable_if_enabled(input0.overlay.overlay_effect)
124         input1.overlay.overlay_effect:promise_to_disable_if_enabled(overlay.overlay_effect)
125         overlay.overlay_effect:promise_to_disable_if_enabled(input0.overlay.overlay_effect)
126         overlay.overlay_effect:promise_to_disable_if_enabled(input1.overlay.overlay_effect)
127
128         scene:finalize(true)  -- Only used live, don't bother creating LQ versions.
129
130         return {
131                 scene = scene,
132                 input0 = input0,
133                 input1 = input1,
134                 mix_effect = mix_effect,
135                 overlay = overlay
136         }
137 end
138 fade_scene = make_fade_scene()
139
140 function make_sbs_input(scene, has_overlay)
141         local input = scene:add_input()
142         return {
143                 input = input,
144                 overlay = has_overlay and make_overlay(scene, input) or nil,
145                 resample_effect = scene:add_effect({ResampleEffect.new(), ResizeEffect.new()}),
146                 wb_effect = scene:add_effect(WhiteBalanceEffect.new()),
147                 padding_effect = scene:add_effect(IntegralPaddingEffect.new())
148         }
149 end
150
151 -- Side-by-side scene.
152 function make_sbs_scene()
153         local scene = Scene.new(16, 9)
154
155         local bg = scene:add_input(bg_video)
156         local input0 = make_sbs_input(scene, true)
157         local input1 = make_sbs_input(scene, false)
158
159         input0.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0)
160         input1.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0)
161
162         input0.input:display(0)
163         input1.input:display(1)
164
165         local i0 = scene:add_effect(OverlayEffect.new(), bg, input0.padding_effect)
166         scene:add_effect(OverlayEffect.new(), i0, input1.padding_effect)
167         scene:finalize()
168
169         return {
170                 scene = scene,
171                 input0 = input0,
172                 input1 = input1,
173                 overlay = input0.overlay  -- May be nil.
174         }
175 end
176 local sbs_scene = make_sbs_scene()
177
178 function make_simple_scene_no_finalize()
179         local scene = Scene.new(16, 9)
180
181         local input = scene:add_input()
182         local resample_effect = scene:add_effect({ResampleEffect.new(), ResizeEffect.new(), IdentityEffect.new()})
183         local wb_effect = scene:add_effect(WhiteBalanceEffect.new())
184         local overlay = make_overlay(scene, wb_effect)
185
186         return {
187                 scene = scene,
188                 input = input,
189                 wb_effect = wb_effect,
190                 resample_effect = resample_effect,
191                 overlay = overlay
192         }
193 end
194
195 -- A scene to show a single input on screen.
196 function make_simple_scene()
197         local scene = make_simple_scene_no_finalize()
198         scene.scene:finalize()
199         return scene
200 end
201
202 local simple_scene = make_simple_scene()
203
204 -- Load all the stinger frames.
205 local stinger_images = {}
206 for idx=0,24 do
207         local filename = cef_path .. "/stinger/blur" .. string.rep("0", 3 - string.len(tostring(idx))) .. idx .. ".png"
208         stinger_images[idx] = ImageInput.new(filename)
209 end
210
211 -- A scene to show a single input on screen, with a stinger on top.
212 function make_stinger_scene()
213         local scene = make_simple_scene_no_finalize()
214         scene.stinger_input = scene.scene:add_input()
215         scene.scene:add_effect(OverlayEffect.new(), scene.overlay.overlay_effect, scene.stinger_input)
216         scene.scene:finalize()
217         return scene
218 end
219 local stinger_scene = make_stinger_scene()
220
221 -- A scene to show a single static picture on screen. Never with HTML overlay.
222 local static_scene = Scene.new(16, 9)
223 static_scene:add_input(static_image)
224 static_scene:finalize()
225
226 -- A scene to show the overlay and nothing more. LQ only,
227 -- since it is not a valid live signal.
228 local overlay_scene_lq = Scene.new(16, 9)
229 local overlay_scene_lq_input = overlay_scene_lq:add_input(cef_input)
230 overlay_scene_lq:finalize(false)
231
232 function needs_scale(signals, signal_num, width, height)
233         if signal_num == STATIC_SIGNAL_NUM then
234                 -- We assume this is already correctly scaled at load time.
235                 return false
236         end
237         assert(is_plain_signal(signal_num))
238         return (signals:get_width(signal_num) ~= width or signals:get_height(signal_num) ~= height)
239 end
240
241 function set_scale_parameters_if_needed(scene, signals, signal_num, width, height, hq)
242         if needs_scale(signals, signal_num, width, height) then
243                 if hq then
244                         scene.resample_effect:choose(ResampleEffect)  -- High-quality resampling.
245                 else
246                         scene.resample_effect:choose(ResizeEffect)  -- Low-quality resampling.
247                 end
248                 scene.resample_effect:set_int("width", width)
249                 scene.resample_effect:set_int("height", height)
250         else
251                 scene.resample_effect:disable()
252         end
253
254 end
255
256 -- API ENTRY POINT
257 -- Returns the number of outputs in addition to the live (0) and preview (1).
258 -- Called only once, at the start of the program.
259 function num_channels()
260         return NUM_CAMERAS + 4  -- sbs, static picture, video and overlay
261 end
262
263 function is_plain_signal(num)
264         return (num >= INPUT0_SIGNAL_NUM and num <= INPUT5_SIGNAL_NUM) or (num == VIDEO_SIGNAL_NUM)
265 end
266
267 -- Helper function to write e.g. “720p60”. The difference between this
268 -- and get_channel_resolution_raw() is that this one also can say that
269 -- there's no signal.
270 function get_channel_resolution(signal_num)
271         local res = last_resolution[signal_num]
272         if (not res) or not res.is_connected then
273                 return "disconnected"
274         end
275         if res.height <= 0 then
276                 return "no signal"
277         end
278         if not res.has_signal then
279                 if res.height == 525 then
280                         -- Special mode for the USB3 cards.
281                         return "no signal"
282                 end
283                 return get_channel_resolution_raw(res) .. ", no signal"
284         else
285                 return get_channel_resolution_raw(res)
286         end
287 end
288
289 -- Helper function to write e.g. “60” or “59.94”.
290 function get_frame_rate(res)
291         local nom = res.frame_rate_nom
292         local den = res.frame_rate_den
293         if nom % den == 0 then
294                 return nom / den
295         else
296                 return string.format("%.2f", nom / den)
297         end
298 end
299
300 -- Helper function to write e.g. “720p60”.
301 function get_channel_resolution_raw(res)
302         if res.interlaced then
303                 return res.height .. "i" .. get_frame_rate(res)
304         else
305                 return res.height .. "p" .. get_frame_rate(res)
306         end
307 end
308
309 function get_futatabi_status(str)
310         local num_fields = 0
311         local fields = {}
312         for word in string.gmatch(str, '([^;]+)') do
313                 table.insert(fields, word)
314                 num_fields = num_fields + 1
315         end
316         if num_fields >= 4 then
317                 return fields[4]
318         else
319                 return "???"
320         end
321 end
322
323 -- API ENTRY POINT
324 -- Returns the name for each additional channel (starting from 2).
325 -- Called at the start of the program, and then each frame for live
326 -- channels in case they change resolution.
327 function channel_name(channel)
328         local signal_num = channel - 2
329         if signal_num == INPUT0_SIGNAL_NUM then
330                 return "Main (" .. get_channel_resolution(signal_num) .. ")"
331         elseif signal_num == INPUT1_SIGNAL_NUM then
332                 return "Secondary (" .. get_channel_resolution(signal_num) .. ")"
333         elseif signal_num == INPUT2_SIGNAL_NUM then
334                 return "Goal L (" .. get_channel_resolution(signal_num) .. ")"
335         elseif signal_num == INPUT3_SIGNAL_NUM then
336                 return "Goal R (" .. get_channel_resolution(signal_num) .. ")"
337         elseif signal_num == INPUT4_SIGNAL_NUM then
338                 return "Commentators (" .. get_channel_resolution(signal_num) .. ")"
339         elseif signal_num == INPUT5_SIGNAL_NUM then
340                 return "Laptop (" .. get_channel_resolution(signal_num) .. ")"
341         elseif signal_num == SBS_SIGNAL_NUM then
342                 return "Side-by-side"
343         elseif signal_num == STATIC_SIGNAL_NUM then
344                 return "Static picture"
345         elseif signal_num == VIDEO_SIGNAL_NUM then
346                 local res = last_resolution[iptv_video:get_signal_num()]
347                 if (not res) or res.last_subtitle == nil then
348                         return "IPTV"
349                 else
350                         return "IPTV (" .. get_futatabi_status(res.last_subtitle) .. ")"
351                 end
352         elseif signal_num == OVERLAY_SIGNAL_NUM then
353                 return "Overlay"
354         end
355 end
356
357 -- API ENTRY POINT
358 -- Returns, given a channel number, which signal it corresponds to (starting from 0).
359 -- Should return -1 if the channel does not correspond to a simple signal.
360 -- (The information is used for whether right-click on the channel should bring up
361 -- an input selector or not.)
362 -- Called once for each channel, at the start of the program.
363 -- Will never be called for live (0) or preview (1).
364 function channel_signal(channel)
365         if channel - 2 == VIDEO_SIGNAL_NUM then
366                 return iptv_video:get_signal_num()
367         elseif is_plain_signal(channel - 2) then
368                 return channel - 2
369         else
370                 return -1
371         end
372 end
373
374 -- API ENTRY POINT
375 -- Called every frame. Returns the color (if any) to paint around the given
376 -- channel. Returns a CSS color (typically to mark live and preview signals);
377 -- "transparent" is allowed.
378 -- Will never be called for live (0) or preview (1).
379 function channel_color(channel)
380         if state.transition_type ~= NO_TRANSITION then
381                 if channel_involved_in(channel, state.transition_src_signal) or
382                    channel_involved_in(channel, state.transition_dst_signal) then
383                         return "#f00"
384                 end
385         else
386                 if channel_involved_in(channel, state.live_signal_num) then
387                         return "#f00"
388                 end
389         end
390         if channel_involved_in(channel, state.preview_signal_num) then
391                 return "#0f0"
392         end
393         return "transparent"
394 end
395
396 function channel_involved_in(channel, signal_num)
397         if is_plain_signal(signal_num) then
398                 return channel == (signal_num + 2)
399         end
400         if signal_num == SBS_SIGNAL_NUM then
401                 return is_sbs_participating_signal(channel - 2)
402         end
403         if signal_num == STATIC_SIGNAL_NUM then
404                 return (channel == NUM_CAMERAS)
405         end
406         if signal_num == VIDEO_SIGNAL_NUM then
407                 return (channel == NUM_CAMERAS + 1)
408         end
409         return false
410 end
411
412 -- API ENTRY POINT
413 -- Returns if a given channel supports setting white balance (starting from 2).
414 -- Called only once for each channel, at the start of the program.
415 function supports_set_wb(channel)
416         return is_plain_signal(channel - 2)
417 end
418
419 -- API ENTRY POINT
420 -- Gets called with a new gray point when the white balance is changing.
421 -- The color is in linear light (not sRGB gamma).
422 function set_wb(channel, red, green, blue)
423         if is_plain_signal(channel - 2) then
424                 state.neutral_colors[channel - 2 + 1] = { red, green, blue }
425         end
426 end
427
428 function finish_transitions(t)
429         if state.transition_type ~= NO_TRANSITION and t >= state.transition_end then
430                 state.live_signal_num = state.transition_dst_signal
431                 state.transition_type = NO_TRANSITION
432         end
433
434         -- Disable the overlay if it is no longer visible.
435         if state.overlay_enabled and t > state.overlay_transition_end and state.overlay_alpha_dst == 0.0 then
436                 state.overlay_enabled = false
437                 print("Turning off overlay")
438         end
439 end
440
441 function in_transition(t)
442         return t >= state.transition_start and t <= state.transition_end
443 end
444
445 function is_sbs_participating_signal(signal_num)
446         return signal_num == INPUT0_SIGNAL_NUM or signal_num == INPUT4_SIGNAL_NUM
447 end
448
449 function simple_signal_has_overlay(signal_num)
450         -- The commentator output has no overlay on it.
451         return signal_num ~= INPUT4_SIGNAL_NUM
452 end
453
454 -- API ENTRY POINT
455 -- Called every frame.
456 function get_transitions(t)
457         if state.preview_signal_num == OVERLAY_SIGNAL_NUM then
458                 if t < state.overlay_transition_end then
459                         -- Fade in progress.
460                         return {}
461                 end
462                 if state.overlay_enabled then
463                         return {"Overlay off", "", "Fade ovl out"}
464                 else
465                         return {"Overlay on", "", "Fade ovl in"}
466                 end
467         end
468
469         if in_transition(t) then
470                 -- Transition already in progress, the only thing we can do is really
471                 -- cut to the preview. (TODO: Make an “abort” and/or “finish”, too?)
472                 return {"Cut"}
473         end
474
475         if state.live_signal_num == state.preview_signal_num then
476                 -- No transitions possible.
477                 return {}
478         end
479
480         if (is_plain_signal(state.live_signal_num) and state.preview_signal_num == VIDEO_SIGNAL_NUM) or
481            (is_plain_signal(state.preview_signal_num) and state.live_signal_num == VIDEO_SIGNAL_NUM) then
482                 return {"Cut", "Sting", "Fade"}
483         end
484
485         if (is_plain_signal(state.live_signal_num) or state.live_signal_num == STATIC_SIGNAL_NUM) and
486            (is_plain_signal(state.preview_signal_num) or state.preview_signal_num == STATIC_SIGNAL_NUM) then
487                 return {"Cut", "", "Fade"}
488         end
489
490         -- Various zooms.
491         if state.live_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(state.preview_signal_num) then
492                 return {"Cut", "Zoom in"}
493         elseif is_sbs_participating_signal(state.live_signal_num) and state.preview_signal_num == SBS_SIGNAL_NUM then
494                 return {"Cut", "Zoom out"}
495         end
496
497         return {"Cut"}
498 end
499
500 function swap_preview_live()
501         local temp = state.live_signal_num
502         state.live_signal_num = state.preview_signal_num
503         state.preview_signal_num = temp
504 end
505
506 function start_transition(type_, t, duration)
507         state.transition_start = t
508         state.transition_end = t + duration
509         state.transition_type = type_
510         state.transition_src_signal = state.live_signal_num
511         state.transition_dst_signal = state.preview_signal_num
512         state.stinger_in_progress = false
513         swap_preview_live()
514 end
515
516 -- API ENTRY POINT
517 -- Called when the user clicks a transition button.
518 function transition_clicked(num, t)
519         if state.preview_signal_num == OVERLAY_SIGNAL_NUM then
520                 if num == 0 then
521                         -- Cut.
522                         state.overlay_transition_start = -2.0
523                         state.overlay_transition_end = -1.0
524                         if state.overlay_enabled then
525                                 state.overlay_enabled = false
526                                 state.overlay_alpha_src = 0.0
527                                 state.overlay_alpha_dst = 0.0
528                         else
529                                 state.overlay_enabled = true
530                                 state.overlay_alpha_src = 1.0
531                                 state.overlay_alpha_dst = 1.0
532                         end
533                 elseif num == 2 then
534                         -- Fade.
535                         state.overlay_transition_start = t
536                         state.overlay_transition_end = t + 1.0
537                         if state.overlay_enabled then
538                                 state.overlay_alpha_src = 1.0
539                                 state.overlay_alpha_dst = 0.0
540                         else
541                                 state.overlay_alpha_src = 0.0
542                                 state.overlay_alpha_dst = 1.0
543                         end
544                         state.overlay_enabled = true
545                 end
546                 return
547         end
548
549         if num == 0 then
550                 -- Cut.
551                 if in_transition(t) then
552                         -- Ongoing transition; finish it immediately before the cut.
553                         finish_transitions(state.transition_end)
554                 end
555
556                 swap_preview_live()
557                 state.stinger_in_progress = false
558         elseif num == 1 then
559                 -- Zoom or sting.
560                 finish_transitions(t)
561
562                 if state.live_signal_num == state.preview_signal_num then
563                         -- Nothing to do.
564                         return
565                 end
566
567                 if (is_plain_signal(state.live_signal_num) and state.preview_signal_num == VIDEO_SIGNAL_NUM) or
568                    (is_plain_signal(state.preview_signal_num) and state.live_signal_num == VIDEO_SIGNAL_NUM) then
569                         -- Sting.
570                         if stinger_in_progress then
571                                 return
572                         end
573
574                         state.stinger_in_progress = true
575                         state.stinger_frame = 0
576                         state.stinger_src_signal = state.live_signal_num
577                         state.stinger_dst_signal = state.preview_signal_num
578                         return
579                 end
580
581                 if is_plain_signal(state.live_signal_num) and is_plain_signal(state.preview_signal_num) then
582                         -- We can't zoom between these. Just make a cut.
583                         io.write("Cutting from " .. state.live_signal_num .. " to " .. state.live_signal_num .. "\n")
584                         swap_preview_live()
585                         state.stinger_in_progress = false
586                         return
587                 end
588
589                 if (state.live_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(state.preview_signal_num)) or
590                    (state.preview_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(state.live_signal_num)) then
591                         start_transition(ZOOM_TRANSITION, t, 1.0)
592                 end
593         elseif num == 2 then
594                 finish_transitions(t)
595
596                 -- Fade.
597                 if (state.live_signal_num ~= state.preview_signal_num) and
598                    (is_plain_signal(state.live_signal_num) or
599                     state.live_signal_num == STATIC_SIGNAL_NUM) and
600                    (is_plain_signal(state.preview_signal_num) or
601                     state.preview_signal_num == STATIC_SIGNAL_NUM) then
602                         start_transition(FADE_TRANSITION, t, 1.0)
603                 else
604                         -- Fades involving SBS are ignored (we have no scene for it).
605                 end
606         end
607 end
608
609 -- API ENTRY POINT
610 function channel_clicked(num)
611         state.preview_signal_num = num
612 end
613
614 function setup_fade_input(state, input, signals, signal_num, width, height)
615         if signal_num == STATIC_SIGNAL_NUM then
616                 input.input:display(static_image)
617                 input.wb_effect:disable()
618
619                 -- We assume this is already correctly scaled at load time.
620                 input.resample_effect:disable()
621         else
622                 input.input:display(signal_num)
623                 input.wb_effect:enable()
624                 set_neutral_color(input.wb_effect, state.neutral_colors[signal_num - INPUT0_SIGNAL_NUM + 1])
625
626                 if (signals:get_width(signal_num) ~= width or signals:get_height(signal_num) ~= height) then
627                         input.resample_effect:enable()
628                         input.resample_effect:set_int("width", width)
629                         input.resample_effect:set_int("height", height)
630                 else
631                         input.resample_effect:disable()
632                 end
633         end
634 end
635
636 function get_fade_scene(state, signals, t, width, height, input_resolution)
637         local scene = fade_scene
638         setup_fade_input(state, scene.input0, signals, state.transition_src_signal, width, height)
639         setup_fade_input(state, scene.input1, signals, state.transition_dst_signal, width, height)
640
641         local tt = calc_fade_progress(t, state.transition_start, state.transition_end)
642         scene.mix_effect:set_float("strength_first", 1.0 - tt)
643         scene.mix_effect:set_float("strength_second", tt)
644
645         -- The commentator output has no overlay on it.
646         local extra_alpha_factor = 1.0
647         if not simple_signal_has_overlay(state.transition_src_signal) and
648            not simple_signal_has_overlay(state.transition_dst_signal) then
649                 extra_alpha_factor = 0.0
650         elseif not simple_signal_has_overlay(state.transition_src_signal) then
651                 extra_alpha_factor = tt
652         elseif not simple_signal_has_overlay(state.transition_dst_signal) then
653                 extra_alpha_factor = 1.0 - tt
654         end
655
656         -- If fading between two live inputs, the overlay is put on top.
657         -- If fading between the static picture and a live input,
658         -- the overlay is put on the live input.
659         scene.input0.overlay.overlay_effect:disable()
660         scene.input1.overlay.overlay_effect:disable()
661         scene.overlay.overlay_effect:disable()
662         if state.overlay_enabled then
663                 local input0_live = (state.transition_src_signal ~= STATIC_SIGNAL_NUM)
664                 local input1_live = (state.transition_dst_signal ~= STATIC_SIGNAL_NUM)
665                 if input0_live and not input1_live then
666                         scene.input0.overlay.overlay_effect:enable()
667                         prepare_overlay_live(state, scene.input0, t, extra_alpha_factor)
668                 elseif input1_live and not input0_live then
669                         scene.input1.overlay.overlay_effect:enable()
670                         prepare_overlay_live(state, scene.input1, t, extra_alpha_factor)
671                 else
672                         scene.overlay.overlay_effect:enable()
673                         prepare_overlay_live(state, scene, t, extra_alpha_factor)
674                 end
675         end
676         return scene.scene
677 end
678
679 function fetch_input_resolution(signals, signal_num)
680         local res = {
681                 width = signals:get_width(signal_num),
682                 height = signals:get_height(signal_num),
683                 interlaced = signals:get_interlaced(signal_num),
684                 has_signal = signals:get_has_signal(signal_num),
685                 is_connected = signals:get_is_connected(signal_num),
686                 frame_rate_nom = signals:get_frame_rate_nom(signal_num),
687                 frame_rate_den = signals:get_frame_rate_den(signal_num),
688                 last_subtitle = signals:get_last_subtitle(signal_num)
689         }
690
691         if res.interlaced then
692                 -- Convert height from frame height to field height.
693                 -- (Needed for e.g. place_rectangle.)
694                 res.height = res.height * 2
695
696                 -- Show field rate instead of frame rate; really for cosmetics only
697                 -- (and actually contrary to EBU recommendations, although in line
698                 -- with typical user expectations).
699                 res.frame_rate_nom = res.frame_rate_nom * 2
700         end
701         return res
702 end
703
704 local last_rate = 0.0
705
706 -- API ENTRY POINT
707 -- Called every frame. Get the scene for displaying at input <num>,
708 -- where 0 is live, 1 is preview, 2 is the first channel to display
709 -- in the bottom bar, and so on up to num_channels()+1. t is the
710 -- current time in seconds. width and height are the dimensions of
711 -- the output, although you can ignore them if you don't need them
712 -- (they're useful if you want to e.g. know what to resample by).
713 --
714 -- <signals> is basically an exposed InputState, which you can use to
715 -- query for information about the signals at the point of the current
716 -- frame. In particular, you can call get_width() and get_height()
717 -- for any signal number, and use that to e.g. assist in scene selection.
718 --
719 -- You should return scene to use, after having set any parameters you
720 -- want to set (through set_int() etc.). The parameters will be snapshot
721 -- at return time and used during rendering.
722 function get_scene(num, t, width, height, signals)
723         local input_resolution = {}
724         for signal_num=0,(NUM_CAMERAS-1) do
725                 input_resolution[signal_num] = fetch_input_resolution(signals, signal_num)
726         end
727         input_resolution[iptv_video:get_signal_num()] = fetch_input_resolution(signals, iptv_video:get_signal_num())
728         last_resolution = input_resolution
729
730         -- Save some CPU time if we're not having SBS on live.
731         local new_rate
732         if state.live_signal_num == SBS_SIGNAL_NUM or
733            state.preview_signal_num == SBS_SIGNAL_NUM or
734            state.transition_type == ZOOM_TRANSITION then
735                 new_rate = 1.0
736         else
737                 new_rate = 0.0001
738         end
739         if new_rate ~= last_rate then
740                 -- Avoid waking up the video thread (which may be sleeping) if the rate is the same.
741                 bg_video:change_rate(new_rate)
742                 last_rate = new_rate
743         end
744
745         if num == 0 then  -- Live.
746                 -- See if we're in a transition.
747                 finish_transitions(t)
748                 if state.transition_type == ZOOM_TRANSITION then
749                         -- Transition in or out of SBS.
750                         sbs_scene.input0.overlay.overlay_effect:enable_if(state.overlay_enabled)
751                         prepare_sbs_scene(state, sbs_scene, calc_zoom_progress(t), state.transition_type, state.transition_src_signal, state.transition_dst_signal, width, height, input_resolution, true)
752                         prepare_overlay_live(state, sbs_scene, t, 1.0)
753                         return sbs_scene.scene
754                 elseif state.transition_type == NO_TRANSITION and state.live_signal_num == SBS_SIGNAL_NUM then
755                         -- Static SBS view.
756                         sbs_scene.input0.overlay.overlay_effect:enable_if(state.overlay_enabled)
757                         prepare_sbs_scene(state, sbs_scene, 0.0, NO_TRANSITION, 0, SBS_SIGNAL_NUM, width, height, input_resolution, true)
758                         prepare_overlay_live(state, sbs_scene, t, 1.0)
759                         return sbs_scene.scene
760                 elseif state.transition_type == FADE_TRANSITION then
761                         return get_fade_scene(state, signals, t, width, height, input_resolution)
762                 elseif is_plain_signal(state.live_signal_num) then
763                         local overlay_really_enabled = state.overlay_enabled and simple_signal_has_overlay(state.live_signal_num)
764                         if state.stinger_in_progress then
765                                 stinger_scene.overlay.overlay_effect:enable_if(overlay_really_enabled)
766                                 stinger_scene.input:display(state.live_signal_num)
767                                 set_scale_parameters_if_needed(stinger_scene, signals, state.live_signal_num, width, height, true)
768                                 set_neutral_color_from_signal(state, stinger_scene.wb_effect, state.live_signal_num)
769
770                                 stinger_scene.stinger_input:display(stinger_images[state.stinger_frame])
771                                 state.stinger_frame = state.stinger_frame + 1
772                                 if state.stinger_frame >= 25 then
773                                         state.stinger_in_progress = false
774                                         state.preview_signal_num = state.stinger_src_signal
775                                         state.live_signal_num = state.stinger_dst_signal
776
777                                         if state.stinger_dst_signal == VIDEO_SIGNAL_NUM then
778                                                 -- Turn off the overlay when playing video.
779                                                 state.stinger_save_overlay = state.overlay_enabled
780                                                 state.overlay_enabled = false
781                                         else
782                                                 -- Restore the state.
783                                                 state.overlay_enabled = state.stinger_save_overlay
784                                         end
785                                 end
786
787                                 if overlay_really_enabled then
788                                         prepare_overlay_live(state, stinger_scene, t, 1.0)
789                                 end
790                                 return stinger_scene.scene
791                         else
792                                 simple_scene.overlay.overlay_effect:enable_if(overlay_really_enabled)
793                                 simple_scene.input:display(state.live_signal_num)
794                                 set_scale_parameters_if_needed(simple_scene, signals, state.live_signal_num, width, height, true)
795                                 set_neutral_color_from_signal(state, simple_scene.wb_effect, state.live_signal_num)
796                                 prepare_overlay_live(state, simple_scene, t, 1.0)
797                                 return simple_scene.scene
798                         end
799                 elseif state.live_signal_num == STATIC_SIGNAL_NUM then  -- Static picture.
800                         return static_scene
801                 else
802                         assert(false)
803                 end
804         end
805
806         -- We do not show overlays on the individual preview inputs.
807         -- The M/E preview matches what we'd put live by doing a transition, as always.
808         local show_overlay = false
809         if num == 1 then  -- Preview.
810                 if state.preview_signal_num == OVERLAY_SIGNAL_NUM then
811                         num = state.live_signal_num + 2
812                         show_overlay = not state.overlay_enabled
813
814                         if state.transition_type ~= NO_TRANSITION then
815                                 num = state.transition_dst_signal + 2
816                         end
817                 else
818                         num = state.preview_signal_num + 2
819                         show_overlay = state.overlay_enabled and simple_signal_has_overlay(state.preview_signal_num)
820                 end
821         end
822
823         -- Individual preview inputs (usually without overlay).
824         if is_plain_signal(num - 2) then
825                 local signal_num = num - 2
826                 simple_scene.overlay.overlay_effect:enable_if(show_overlay)
827                 simple_scene.input:display(signal_num)
828                 set_scale_parameters_if_needed(simple_scene, signals, signal_num, width, height, false)
829                 set_neutral_color_from_signal(state, simple_scene.wb_effect, signal_num)
830                 prepare_overlay_static(simple_scene, t)
831                 return simple_scene.scene
832         end
833         if num == SBS_SIGNAL_NUM + 2 then
834                 sbs_scene.input0.overlay.overlay_effect:enable_if(show_overlay)
835                 prepare_sbs_scene(state, sbs_scene, 0.0, NO_TRANSITION, 0, SBS_SIGNAL_NUM, width, height, input_resolution, false)
836                 return sbs_scene.scene
837         end
838         if num == STATIC_SIGNAL_NUM + 2 then
839                 return static_scene
840         end
841         if num == OVERLAY_SIGNAL_NUM + 2 then
842                 return overlay_scene_lq
843         end
844 end
845
846 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
847 function round(x)
848         return math.floor(x + 0.5)
849 end
850
851 function lerp(a, b, t)
852         return a + (b - a) * t
853 end
854
855 function lerp_pos(a, b, t)
856         return {
857                 x0 = lerp(a.x0, b.x0, t),
858                 y0 = lerp(a.y0, b.y0, t),
859                 x1 = lerp(a.x1, b.x1, t),
860                 y1 = lerp(a.y1, b.y1, t)
861         }
862 end
863
864 function pos_from_top_left(x, y, width, height, screen_width, screen_height)
865         local xs = screen_width / 1280.0
866         local ys = screen_height / 720.0
867         return {
868                 x0 = round(xs * x),
869                 y0 = round(ys * y),
870                 x1 = round(xs * (x + width)),
871                 y1 = round(ys * (y + height))
872         }
873 end
874
875 function prepare_sbs_scene(state, scene, t, transition_type, src_signal, dst_signal, screen_width, screen_height, input_resolution, hq)
876         scene.input0.input:display(0)
877         scene.input1.input:display(4)
878         set_neutral_color(scene.input0.wb_effect, state.neutral_colors[1])
879         set_neutral_color(scene.input1.wb_effect, state.neutral_colors[5])
880
881         -- Both inputs are the same size (true side-by-side).
882         local pos0 = pos_from_top_left(1280 - 616 - 16, 186, 616, 347, screen_width, screen_height)
883         local pos1 = pos_from_top_left(16, 186, 616, 347, screen_width, screen_height)
884
885         local pos_fs = { x0 = 0, y0 = 0, x1 = screen_width, y1 = screen_height }
886         local affine_param
887         if transition_type == NO_TRANSITION then
888                 -- Static SBS view.
889                 affine_param = { sx = 1.0, sy = 1.0, tx = 0.0, ty = 0.0 }   -- Identity.
890         else
891                 -- Zooming to/from SBS view into or out of a single view.
892                 assert(transition_type == ZOOM_TRANSITION)
893                 local signal, real_t
894                 if src_signal == SBS_SIGNAL_NUM then
895                         signal = dst_signal
896                         real_t = t
897                 else
898                         assert(dst_signal == SBS_SIGNAL_NUM)
899                         signal = src_signal
900                         real_t = 1.0 - t
901                 end
902
903                 if signal == INPUT0_SIGNAL_NUM then
904                         affine_param = find_affine_param(pos0, lerp_pos(pos0, pos_fs, real_t))
905                 elseif signal == INPUT4_SIGNAL_NUM then
906                         affine_param = find_affine_param(pos1, lerp_pos(pos1, pos_fs, real_t))
907                 end
908         end
909
910         -- NOTE: input_resolution is not 1-indexed, unlike usual Lua arrays.
911         place_rectangle_with_affine(scene.input0, pos0, affine_param, screen_width, screen_height, input_resolution[0].width, input_resolution[0].height, hq)
912         place_rectangle_with_affine(scene.input1, pos1, affine_param, screen_width, screen_height, input_resolution[1].width, input_resolution[1].height, hq)
913 end
914
915 -- Find the transformation that changes the first rectangle to the second one.
916 function find_affine_param(a, b)
917         local sx = (b.x1 - b.x0) / (a.x1 - a.x0)
918         local sy = (b.y1 - b.y0) / (a.y1 - a.y0)
919         return {
920                 sx = sx,
921                 sy = sy,
922                 tx = b.x0 - a.x0 * sx,
923                 ty = b.y0 - a.y0 * sy
924         }
925 end
926
927 function place_rectangle_with_affine(input, pos, aff, screen_width, screen_height, input_width, input_height, hq)
928         local x0 = pos.x0 * aff.sx + aff.tx
929         local x1 = pos.x1 * aff.sx + aff.tx
930         local y0 = pos.y0 * aff.sy + aff.ty
931         local y1 = pos.y1 * aff.sy + aff.ty
932
933         place_rectangle(input, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height, hq)
934 end
935
936 function place_rectangle(input, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height, hq)
937         input.padding_effect:set_int("width", screen_width)
938         input.padding_effect:set_int("height", screen_height)
939
940         -- Cull.
941         if x0 > screen_width or x1 < 0.0 or y0 > screen_height or y1 < 0.0 then
942                 input.resample_effect:choose(ResizeEffect)  -- Low-quality resizing.
943                 input.resample_effect:set_int("width", 1)
944                 input.resample_effect:set_int("height", 1)
945                 input.padding_effect:set_int("left", screen_width + 100)
946                 input.padding_effect:set_int("top", screen_height + 100)
947                 return
948         end
949
950         local srcx0 = 0.0
951         local srcx1 = 1.0
952         local srcy0 = 0.0
953         local srcy1 = 1.0
954
955         -- Clip.
956         if x0 < 0 then
957                 srcx0 = -x0 / (x1 - x0)
958                 x0 = 0
959         end
960         if y0 < 0 then
961                 srcy0 = -y0 / (y1 - y0)
962                 y0 = 0
963         end
964         if x1 > screen_width then
965                 srcx1 = (screen_width - x0) / (x1 - x0)
966                 x1 = screen_width
967         end
968         if y1 > screen_height then
969                 srcy1 = (screen_height - y0) / (y1 - y0)
970                 y1 = screen_height
971         end
972
973         if hq then
974                 -- High-quality resampling.
975                 local resample_effect = input.resample_effect:choose(ResampleEffect)
976
977                 local x_subpixel_offset = x0 - math.floor(x0)
978                 local y_subpixel_offset = y0 - math.floor(y0)
979
980                 -- Resampling must be to an integral number of pixels. Round up,
981                 -- and then add an extra pixel so we have some leeway for the border.
982                 local width = math.ceil(x1 - x0) + 1
983                 local height = math.ceil(y1 - y0) + 1
984                 input.resample_effect:set_int("width", width)
985                 input.resample_effect:set_int("height", height)
986
987                 -- Correct the discrepancy with zoom. (This will leave a small
988                 -- excess edge of pixels and subpixels, which we'll correct for soon.)
989                 local zoom_x = (x1 - x0) / (width * (srcx1 - srcx0))
990                 local zoom_y = (y1 - y0) / (height * (srcy1 - srcy0))
991                 resample_effect:set_float("zoom_x", zoom_x)  -- Use the actual effect specialization; specific to ResampleEffect.
992                 resample_effect:set_float("zoom_y", zoom_y)
993                 resample_effect:set_float("zoom_center_x", 0.0)
994                 resample_effect:set_float("zoom_center_y", 0.0)
995
996                 -- Padding must also be to a whole-pixel offset.
997                 input.padding_effect:set_int("left", math.floor(x0))
998                 input.padding_effect:set_int("top", math.floor(y0))
999
1000                 -- Correct _that_ discrepancy by subpixel offset in the resampling.
1001                 resample_effect:set_float("left", srcx0 * input_width - x_subpixel_offset / zoom_x)
1002                 resample_effect:set_float("top", srcy0 * input_height - y_subpixel_offset / zoom_y)
1003
1004                 -- Finally, adjust the border so it is exactly where we want it.
1005                 input.padding_effect:set_float("border_offset_left", x_subpixel_offset)
1006                 input.padding_effect:set_float("border_offset_right", x1 - (math.floor(x0) + width))
1007                 input.padding_effect:set_float("border_offset_top", y_subpixel_offset)
1008                 input.padding_effect:set_float("border_offset_bottom", y1 - (math.floor(y0) + height))
1009         else
1010                 -- Lower-quality simple resizing.
1011                 input.resample_effect:choose(ResizeEffect)
1012
1013                 local width = round(x1 - x0)
1014                 local height = round(y1 - y0)
1015                 input.resample_effect:set_int("width", width)
1016                 input.resample_effect:set_int("height", height)
1017
1018                 -- Padding must also be to a whole-pixel offset.
1019                 input.padding_effect:set_int("left", math.floor(x0))
1020                 input.padding_effect:set_int("top", math.floor(y0))
1021
1022                 -- No subpixel stuff.
1023                 input.padding_effect:set_float("border_offset_left", 0.0)
1024                 input.padding_effect:set_float("border_offset_right", 0.0)
1025                 input.padding_effect:set_float("border_offset_top", 0.0)
1026                 input.padding_effect:set_float("border_offset_bottom", 0.0)
1027         end
1028 end
1029
1030 function prepare_overlay_live(state, scene, t, extra_alpha_factor)
1031         if scene.overlay then
1032                 local tt = calc_fade_progress(t, state.overlay_transition_start, state.overlay_transition_end)
1033                 local overlay_alpha = state.overlay_alpha_src + tt * (state.overlay_alpha_dst - state.overlay_alpha_src)
1034                 overlay_alpha = overlay_alpha * extra_alpha_factor
1035                 --print("overlay_alpha=" .. overlay_alpha .. " [" .. state.overlay_alpha_src .. "," .. state.overlay_alpha_dst .. "]@" .. tt)
1036                 if t > state.overlay_transition_end and state.overlay_alpha_dst == 0.0 then
1037                         state.overlay_enabled = false  -- Takes effect next frame.
1038         --              print("Turning off overlay")
1039                 end
1040                 scene.overlay.multiply_effect:set_vec4("factor", overlay_alpha, overlay_alpha, overlay_alpha, overlay_alpha)
1041         end
1042 end
1043
1044 function prepare_overlay_static(scene)
1045         if scene.overlay then
1046                 scene.overlay.multiply_effect:set_vec4("factor", 1.0, 1.0, 1.0, 1.0)
1047         end
1048 end
1049
1050 function set_neutral_color(effect, color)
1051         effect:set_vec3("neutral_color", color[1], color[2], color[3])
1052 end
1053
1054 function set_neutral_color_from_signal(state, effect, signal)
1055         if is_plain_signal(signal) then
1056                 set_neutral_color(effect, state.neutral_colors[signal - INPUT0_SIGNAL_NUM + 1])
1057         end
1058 end
1059
1060 function calc_zoom_progress(t)
1061         if t < state.transition_start then
1062                 return 0.0
1063         elseif t > state.transition_end then
1064                 return 1.0
1065         else
1066                 local tt = (t - state.transition_start) / (state.transition_end - state.transition_start)
1067                 -- Smooth it a bit.
1068                 return math.sin(tt * 3.14159265358 * 0.5)
1069         end
1070 end
1071
1072 function calc_fade_progress(t, transition_start, transition_end)
1073         local tt = (t - transition_start) / (transition_end - transition_start)
1074         if tt < 0.0 then
1075                 return 0.0
1076         elseif tt > 1.0 then
1077                 return 1.0
1078         end
1079
1080         -- Make the fade look maybe a tad more natural, by pumping it
1081         -- through a sigmoid function.
1082         tt = 10.0 * tt - 5.0
1083         tt = 1.0 / (1.0 + math.exp(-tt))
1084
1085         return tt
1086 end
1087
1088 ThemeMenu.set(
1089         { "Reload overlay", reload_cef },
1090         { "Disconnect IPTV", disconnect_iptv }
1091 )