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