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