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