]> git.sesse.net Git - ultimatescore/blob - nageru/ultimate.lua
Make some nice mappings for the Sound Shark.
[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 = 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 local last_alsa_sound_shark_volume = -1   -- Always update on the first frame.
728
729 function update_volume()
730         if Nageru.get_num_audio_buses() < 3 or
731            Nageru.get_audio_bus_name(2) ~= 'Sound Shark' then
732                 -- We haven't loaded the right audio mapping.
733                 return
734         end
735
736         if first_frame then
737                 Nageru.set_audio_bus_fader_level_db(2, -math.huge)
738                 first_frame = false
739         end
740
741         local volume_db = Nageru.get_audio_bus_fader_level_db(2)
742         if Nageru.get_audio_bus_mute(2) or volume_db < -100.0 then
743                 volume_db = -100.0
744         end
745         cef_input:execute_javascript_async("set_sound_shark_volume_db(" .. volume_db .. ")")
746
747         local desired_alsa_sound_shark_volume = (volume_db < -40.0) and 0 or 154  -- 154 corresponds to 70% in alsamixer for this input.
748         if desired_alsa_sound_shark_volume ~= last_alsa_sound_shark_volume then
749                 os.execute("amixer -D hw:CARD=USB sset 'Mix A Input 04' " .. desired_alsa_sound_shark_volume .. " &")
750                 os.execute("amixer -D hw:CARD=USB sset 'Mix B Input 04' " .. desired_alsa_sound_shark_volume .. " &")
751                 last_alsa_sound_shark_volume = desired_alsa_sound_shark_volume
752         end
753 end
754
755 local last_rate = 0.0
756
757 -- API ENTRY POINT
758 -- Called every frame. Get the scene for displaying at input <num>,
759 -- where 0 is live, 1 is preview, 2 is the first channel to display
760 -- in the bottom bar, and so on up to num_channels()+1. t is the
761 -- current time in seconds. width and height are the dimensions of
762 -- the output, although you can ignore them if you don't need them
763 -- (they're useful if you want to e.g. know what to resample by).
764 --
765 -- <signals> is basically an exposed InputState, which you can use to
766 -- query for information about the signals at the point of the current
767 -- frame. In particular, you can call get_width() and get_height()
768 -- for any signal number, and use that to e.g. assist in scene selection.
769 --
770 -- You should return scene to use, after having set any parameters you
771 -- want to set (through set_int() etc.). The parameters will be snapshot
772 -- at return time and used during rendering.
773 function get_scene(num, t, width, height, signals)
774         if num == 0 then
775                 update_volume()
776         end
777
778         local input_resolution = {}
779         for signal_num=0,(NUM_CAMERAS-1) do
780                 input_resolution[signal_num] = fetch_input_resolution(signals, signal_num)
781         end
782         input_resolution[iptv_video:get_signal_num()] = fetch_input_resolution(signals, iptv_video:get_signal_num())
783         last_resolution = input_resolution
784
785         -- Save some CPU time if we're not having SBS on live.
786         local new_rate
787         if state.live_signal_num == SBS_SIGNAL_NUM or
788            state.preview_signal_num == SBS_SIGNAL_NUM or
789            state.transition_type == ZOOM_TRANSITION then
790                 new_rate = 1.0
791         else
792                 new_rate = 0.0001
793         end
794         if new_rate ~= last_rate then
795                 -- Avoid waking up the video thread (which may be sleeping) if the rate is the same.
796                 bg_video:change_rate(new_rate)
797                 last_rate = new_rate
798         end
799
800         if num == 0 and multiviewer_enabled then  -- Live, with debugging on.
801                 return multiview_scene_hq
802         end
803
804         if num == 0 then  -- Live.
805                 -- See if we're in a transition.
806                 finish_transitions(t)
807                 if state.transition_type == ZOOM_TRANSITION then
808                         -- Transition in or out of SBS.
809                         sbs_scene.input0.overlay.overlay_effect:enable_if(state.overlay_enabled)
810                         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)
811                         prepare_overlay_live(state, sbs_scene, t, 1.0)
812                         return sbs_scene.scene
813                 elseif state.transition_type == NO_TRANSITION and state.live_signal_num == SBS_SIGNAL_NUM then
814                         -- Static SBS view.
815                         sbs_scene.input0.overlay.overlay_effect:enable_if(state.overlay_enabled)
816                         prepare_sbs_scene(state, sbs_scene, 0.0, NO_TRANSITION, 0, SBS_SIGNAL_NUM, width, height, input_resolution, true)
817                         prepare_overlay_live(state, sbs_scene, t, 1.0)
818                         return sbs_scene.scene
819                 elseif state.transition_type == FADE_TRANSITION then
820                         return get_fade_scene(state, signals, t, width, height, input_resolution)
821                 elseif is_plain_signal(state.live_signal_num) then
822                         local overlay_really_enabled = state.overlay_enabled and simple_signal_has_overlay(state.live_signal_num)
823                         if state.stinger_in_progress then
824                                 stinger_scene.overlay.overlay_effect:enable_if(overlay_really_enabled)
825                                 if state.live_signal_num == VIDEO_SIGNAL_NUM then
826                                         stinger_scene.input:display(iptv_video)
827                                 else
828                                         stinger_scene.input:display(state.live_signal_num)
829                                 end
830                                 set_scale_parameters_if_needed(stinger_scene, signals, state.live_signal_num, width, height, true)
831
832                                 stinger_scene.stinger_input:display(stinger_images[state.stinger_frame])
833                                 state.stinger_frame = state.stinger_frame + 1
834                                 if state.stinger_frame >= 25 then
835                                         state.stinger_in_progress = false
836                                         state.preview_signal_num = state.stinger_src_signal
837                                         state.live_signal_num = state.stinger_dst_signal
838
839                                         if state.stinger_dst_signal == VIDEO_SIGNAL_NUM then
840                                                 -- Turn off the overlay when playing video.
841                                                 state.stinger_save_overlay = state.overlay_enabled
842                                                 state.overlay_enabled = false
843                                         else
844                                                 -- Restore the state.
845                                                 state.overlay_enabled = state.stinger_save_overlay
846                                         end
847                                 end
848
849                                 if overlay_really_enabled then
850                                         prepare_overlay_live(state, stinger_scene, t, 1.0)
851                                 end
852                                 return stinger_scene.scene
853                         else
854                                 simple_scene.overlay.overlay_effect:enable_if(overlay_really_enabled)
855                                 if state.live_signal_num == VIDEO_SIGNAL_NUM then
856                                         simple_scene.input:display(iptv_video)
857                                 else
858                                         simple_scene.input:display(state.live_signal_num)
859                                 end
860                                 set_scale_parameters_if_needed(simple_scene, signals, state.live_signal_num, width, height, true)
861                                 prepare_overlay_live(state, simple_scene, t, 1.0)
862                                 return simple_scene.scene
863                         end
864                 elseif state.live_signal_num == STATIC_SIGNAL_NUM then  -- Static picture.
865                         return static_scene
866                 else
867                         assert(false)
868                 end
869         end
870
871         -- We do not show overlays on the individual preview inputs.
872         -- The M/E preview matches what we'd put live by doing a transition, as always.
873         local show_overlay = false
874         if num == 1 then  -- Preview.
875                 if state.preview_signal_num == OVERLAY_SIGNAL_NUM then
876                         num = state.live_signal_num + 2
877                         show_overlay = not state.overlay_enabled
878
879                         if state.transition_type ~= NO_TRANSITION then
880                                 num = state.transition_dst_signal + 2
881                         end
882                 else
883                         num = state.preview_signal_num + 2
884                         show_overlay = state.overlay_enabled and simple_signal_has_overlay(state.preview_signal_num)
885                 end
886         end
887
888         -- Individual preview inputs (usually without overlay).
889         if is_plain_signal(num - 2) then
890                 local signal_num = num - 2
891                 simple_scene.overlay.overlay_effect:enable_if(show_overlay)
892                 if signal_num == VIDEO_SIGNAL_NUM then
893                         simple_scene.input:display(iptv_video)
894                 else
895                         simple_scene.input:display(signal_num)
896                 end
897                 set_scale_parameters_if_needed(simple_scene, signals, signal_num, width, height, false)
898                 prepare_overlay_static(simple_scene, t)
899                 return simple_scene.scene
900         end
901         if num == SBS_SIGNAL_NUM + 2 then
902                 sbs_scene.input0.overlay.overlay_effect:enable_if(show_overlay)
903                 prepare_sbs_scene(state, sbs_scene, 0.0, NO_TRANSITION, 0, SBS_SIGNAL_NUM, width, height, input_resolution, false)
904                 return sbs_scene.scene
905         end
906         if num == STATIC_SIGNAL_NUM + 2 then
907                 return static_scene
908         end
909         if num == OVERLAY_SIGNAL_NUM + 2 then
910                 return overlay_scene_lq
911         end
912 end
913
914 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
915 function round(x)
916         return math.floor(x + 0.5)
917 end
918
919 function lerp(a, b, t)
920         return a + (b - a) * t
921 end
922
923 function lerp_pos(a, b, t)
924         return {
925                 x0 = lerp(a.x0, b.x0, t),
926                 y0 = lerp(a.y0, b.y0, t),
927                 x1 = lerp(a.x1, b.x1, t),
928                 y1 = lerp(a.y1, b.y1, t)
929         }
930 end
931
932 function pos_from_top_left(x, y, width, height, screen_width, screen_height)
933         local xs = screen_width / 1280.0
934         local ys = screen_height / 720.0
935         return {
936                 x0 = round(xs * x),
937                 y0 = round(ys * y),
938                 x1 = round(xs * (x + width)),
939                 y1 = round(ys * (y + height))
940         }
941 end
942
943 function prepare_sbs_scene(state, scene, t, transition_type, src_signal, dst_signal, screen_width, screen_height, input_resolution, hq)
944         scene.input0.input:display(0)
945         scene.input1.input:display(5)
946
947         -- Both inputs are the same size (true side-by-side).
948         local pos0 = pos_from_top_left(1280 - 616 - 16, 186, 616, 347, screen_width, screen_height)
949         local pos1 = pos_from_top_left(16, 186, 616, 347, screen_width, screen_height)
950
951         local pos_fs = { x0 = 0, y0 = 0, x1 = screen_width, y1 = screen_height }
952         local affine_param
953         if transition_type == NO_TRANSITION then
954                 -- Static SBS view.
955                 affine_param = { sx = 1.0, sy = 1.0, tx = 0.0, ty = 0.0 }   -- Identity.
956         else
957                 -- Zooming to/from SBS view into or out of a single view.
958                 assert(transition_type == ZOOM_TRANSITION)
959                 local signal, real_t
960                 if src_signal == SBS_SIGNAL_NUM then
961                         signal = dst_signal
962                         real_t = t
963                 else
964                         assert(dst_signal == SBS_SIGNAL_NUM)
965                         signal = src_signal
966                         real_t = 1.0 - t
967                 end
968
969                 if signal == INPUT0_SIGNAL_NUM then
970                         affine_param = find_affine_param(pos0, lerp_pos(pos0, pos_fs, real_t))
971                 elseif signal == INPUT4_SIGNAL_NUM then
972                         affine_param = find_affine_param(pos1, lerp_pos(pos1, pos_fs, real_t))
973                 end
974         end
975
976         -- NOTE: input_resolution is not 1-indexed, unlike usual Lua arrays.
977         place_rectangle_with_affine(scene.input0, pos0, affine_param, screen_width, screen_height, input_resolution[0].width, input_resolution[0].height, hq)
978         place_rectangle_with_affine(scene.input1, pos1, affine_param, screen_width, screen_height, input_resolution[1].width, input_resolution[1].height, hq)
979 end
980
981 -- Find the transformation that changes the first rectangle to the second one.
982 function find_affine_param(a, b)
983         local sx = (b.x1 - b.x0) / (a.x1 - a.x0)
984         local sy = (b.y1 - b.y0) / (a.y1 - a.y0)
985         return {
986                 sx = sx,
987                 sy = sy,
988                 tx = b.x0 - a.x0 * sx,
989                 ty = b.y0 - a.y0 * sy
990         }
991 end
992
993 function place_rectangle_with_affine(input, pos, aff, screen_width, screen_height, input_width, input_height, hq)
994         local x0 = pos.x0 * aff.sx + aff.tx
995         local x1 = pos.x1 * aff.sx + aff.tx
996         local y0 = pos.y0 * aff.sy + aff.ty
997         local y1 = pos.y1 * aff.sy + aff.ty
998
999         place_rectangle(input, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height, hq)
1000 end
1001
1002 function place_rectangle(input, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height, hq)
1003         input.padding_effect:set_int("width", screen_width)
1004         input.padding_effect:set_int("height", screen_height)
1005
1006         -- Cull.
1007         if x0 > screen_width or x1 < 0.0 or y0 > screen_height or y1 < 0.0 then
1008                 input.resample_effect:choose(ResizeEffect)  -- Low-quality resizing.
1009                 input.resample_effect:set_int("width", 1)
1010                 input.resample_effect:set_int("height", 1)
1011                 input.padding_effect:set_int("left", screen_width + 100)
1012                 input.padding_effect:set_int("top", screen_height + 100)
1013                 return
1014         end
1015
1016         local srcx0 = 0.0
1017         local srcx1 = 1.0
1018         local srcy0 = 0.0
1019         local srcy1 = 1.0
1020
1021         -- Clip.
1022         if x0 < 0 then
1023                 srcx0 = -x0 / (x1 - x0)
1024                 x0 = 0
1025         end
1026         if y0 < 0 then
1027                 srcy0 = -y0 / (y1 - y0)
1028                 y0 = 0
1029         end
1030         if x1 > screen_width then
1031                 srcx1 = (screen_width - x0) / (x1 - x0)
1032                 x1 = screen_width
1033         end
1034         if y1 > screen_height then
1035                 srcy1 = (screen_height - y0) / (y1 - y0)
1036                 y1 = screen_height
1037         end
1038
1039         if hq then
1040                 -- High-quality resampling.
1041                 local resample_effect = input.resample_effect:choose(ResampleEffect)
1042
1043                 local x_subpixel_offset = x0 - math.floor(x0)
1044                 local y_subpixel_offset = y0 - math.floor(y0)
1045
1046                 -- Resampling must be to an integral number of pixels. Round up,
1047                 -- and then add an extra pixel so we have some leeway for the border.
1048                 local width = math.ceil(x1 - x0) + 1
1049                 local height = math.ceil(y1 - y0) + 1
1050                 input.resample_effect:set_int("width", width)
1051                 input.resample_effect:set_int("height", height)
1052
1053                 -- Correct the discrepancy with zoom. (This will leave a small
1054                 -- excess edge of pixels and subpixels, which we'll correct for soon.)
1055                 local zoom_x = (x1 - x0) / (width * (srcx1 - srcx0))
1056                 local zoom_y = (y1 - y0) / (height * (srcy1 - srcy0))
1057                 resample_effect:set_float("zoom_x", zoom_x)  -- Use the actual effect specialization; specific to ResampleEffect.
1058                 resample_effect:set_float("zoom_y", zoom_y)
1059                 resample_effect:set_float("zoom_center_x", 0.0)
1060                 resample_effect:set_float("zoom_center_y", 0.0)
1061
1062                 -- Padding must also be to a whole-pixel offset.
1063                 input.padding_effect:set_int("left", math.floor(x0))
1064                 input.padding_effect:set_int("top", math.floor(y0))
1065
1066                 -- Correct _that_ discrepancy by subpixel offset in the resampling.
1067                 resample_effect:set_float("left", srcx0 * input_width - x_subpixel_offset / zoom_x)
1068                 resample_effect:set_float("top", srcy0 * input_height - y_subpixel_offset / zoom_y)
1069
1070                 -- Finally, adjust the border so it is exactly where we want it.
1071                 input.padding_effect:set_float("border_offset_left", x_subpixel_offset)
1072                 input.padding_effect:set_float("border_offset_right", x1 - (math.floor(x0) + width))
1073                 input.padding_effect:set_float("border_offset_top", y_subpixel_offset)
1074                 input.padding_effect:set_float("border_offset_bottom", y1 - (math.floor(y0) + height))
1075         else
1076                 -- Lower-quality simple resizing.
1077                 input.resample_effect:choose(ResizeEffect)
1078
1079                 local width = round(x1 - x0)
1080                 local height = round(y1 - y0)
1081                 input.resample_effect:set_int("width", width)
1082                 input.resample_effect:set_int("height", height)
1083
1084                 -- Padding must also be to a whole-pixel offset.
1085                 input.padding_effect:set_int("left", math.floor(x0))
1086                 input.padding_effect:set_int("top", math.floor(y0))
1087
1088                 -- No subpixel stuff.
1089                 input.padding_effect:set_float("border_offset_left", 0.0)
1090                 input.padding_effect:set_float("border_offset_right", 0.0)
1091                 input.padding_effect:set_float("border_offset_top", 0.0)
1092                 input.padding_effect:set_float("border_offset_bottom", 0.0)
1093         end
1094 end
1095
1096 function prepare_overlay_live(state, scene, t, extra_alpha_factor)
1097         if scene.overlay then
1098                 local tt = calc_fade_progress(t, state.overlay_transition_start, state.overlay_transition_end)
1099                 local overlay_alpha = state.overlay_alpha_src + tt * (state.overlay_alpha_dst - state.overlay_alpha_src)
1100                 overlay_alpha = overlay_alpha * extra_alpha_factor
1101                 --print("overlay_alpha=" .. overlay_alpha .. " [" .. state.overlay_alpha_src .. "," .. state.overlay_alpha_dst .. "]@" .. tt)
1102                 if t > state.overlay_transition_end and state.overlay_alpha_dst == 0.0 then
1103                         state.overlay_enabled = false  -- Takes effect next frame.
1104         --              print("Turning off overlay")
1105                 end
1106                 scene.overlay.multiply_effect:set_vec4("factor", overlay_alpha, overlay_alpha, overlay_alpha, overlay_alpha)
1107         end
1108 end
1109
1110 function prepare_overlay_static(scene)
1111         if scene.overlay then
1112                 scene.overlay.multiply_effect:set_vec4("factor", 1.0, 1.0, 1.0, 1.0)
1113         end
1114 end
1115
1116 function calc_zoom_progress(t)
1117         if t < state.transition_start then
1118                 return 0.0
1119         elseif t > state.transition_end then
1120                 return 1.0
1121         else
1122                 local tt = (t - state.transition_start) / (state.transition_end - state.transition_start)
1123                 -- Smooth it a bit.
1124                 return math.sin(tt * 3.14159265358 * 0.5)
1125         end
1126 end
1127
1128 function calc_fade_progress(t, transition_start, transition_end)
1129         local tt = (t - transition_start) / (transition_end - transition_start)
1130         if tt < 0.0 then
1131                 return 0.0
1132         elseif tt > 1.0 then
1133                 return 1.0
1134         end
1135
1136         -- Make the fade look maybe a tad more natural, by pumping it
1137         -- through a sigmoid function.
1138         tt = 10.0 * tt - 5.0
1139         tt = 1.0 / (1.0 + math.exp(-tt))
1140
1141         return tt
1142 end
1143
1144 ThemeMenu.set(
1145         { "Reload overlay", reload_cef },
1146         { "Disconnect IPTV", disconnect_iptv },
1147         { "Toggle multiviewer", toggle_multiviewer }
1148 )