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