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