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