1 -- Nageru theme for TFK mini-tournament 2017, based on the default theme.
3 local futatabi_server = "http://10.42.0.2:9096"
6 transition_start = -2.0,
9 transition_src_signal = 0,
10 transition_dst_signal = 0,
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,
18 stinger_in_progress = false,
20 stinger_src_signal = 0,
21 stinger_dst_signal = 0,
22 stinger_save_overlay = false,
25 preview_signal_num = 1
29 -- Valid values for live_signal_num and preview_signal_num.
30 local INPUT0_SIGNAL_NUM = 0
31 local INPUT1_SIGNAL_NUM = 1
32 local INPUT2_SIGNAL_NUM = 2
33 local INPUT3_SIGNAL_NUM = 3
34 local INPUT4_SIGNAL_NUM = 4
35 local INPUT5_SIGNAL_NUM = 5
36 local VIDEO_SIGNAL_NUM = NUM_CAMERAS
37 local SBS_SIGNAL_NUM = NUM_CAMERAS + 1
38 local STATIC_SIGNAL_NUM = NUM_CAMERAS + 2
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
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
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 = {}
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()")
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)
63 local static_image = ImageInput.new(cef_path .. "/nageru/dsn-bg.png")
64 local first_frame = true
65 local multiviewer_enabled = false
69 cef_input:execute_javascript_async("play()")
72 function disconnect_iptv()
73 iptv_video:disconnect()
76 function toggle_multiviewer()
77 multiviewer_enabled = not multiviewer_enabled
80 -- An overlay with variable alpha.
81 function make_overlay(scene, base)
82 local image = scene:add_input(cef_input)
83 local multiply_effect = scene:add_effect(MultiplyEffect.new())
84 local overlay_effect = scene:add_optional_effect(OverlayEffect.new(), base, multiply_effect)
87 multiply_effect = multiply_effect,
88 overlay_effect = overlay_effect
92 function make_fade_input(scene)
94 input = scene:add_input(),
95 resample_effect = scene:add_optional_effect(ResampleEffect.new()), -- Activated if scaling.
96 wb_effect = scene:add_auto_white_balance()
98 ret.overlay = make_overlay(scene, ret.wb_effect)
102 -- A scene to fade between two inputs, of which either can be a picture
104 function make_fade_scene()
105 local scene = Scene.new(16, 9)
107 -- If fading between two live inputs, the overlay is put on top.
108 -- If fading between the static picture and a live input,
109 -- the overlay is put on the live input.
110 local input0 = make_fade_input(scene)
111 local input1 = make_fade_input(scene)
112 local mix_effect = scene:add_effect(MixEffect.new(), input0.overlay.overlay_effect, input1.overlay.overlay_effect)
113 local overlay = make_overlay(scene, mix_effect)
115 -- At most one overlay can be active at any given time.
116 input0.overlay.overlay_effect:promise_to_disable_if_enabled(input1.overlay.overlay_effect)
117 input0.overlay.overlay_effect:promise_to_disable_if_enabled(overlay.overlay_effect)
118 input1.overlay.overlay_effect:promise_to_disable_if_enabled(input0.overlay.overlay_effect)
119 input1.overlay.overlay_effect:promise_to_disable_if_enabled(overlay.overlay_effect)
120 overlay.overlay_effect:promise_to_disable_if_enabled(input0.overlay.overlay_effect)
121 overlay.overlay_effect:promise_to_disable_if_enabled(input1.overlay.overlay_effect)
123 scene:finalize(true) -- Only used live, don't bother creating LQ versions.
129 mix_effect = mix_effect,
133 fade_scene = make_fade_scene()
135 function make_sbs_input(scene, has_overlay)
136 local input = scene:add_input()
137 local wb_effect = scene:add_auto_white_balance()
138 local overlay = has_overlay and make_overlay(scene, wb_effect) or nil
139 local resample_effect = scene:add_effect({ResampleEffect.new(), ResizeEffect.new()})
140 local padding_effect = scene:add_effect(IntegralPaddingEffect.new())
144 resample_effect = resample_effect,
145 wb_effect = wb_effect,
146 padding_effect = padding_effect
150 -- Side-by-side scene.
151 function make_sbs_scene()
152 local scene = Scene.new(16, 9)
154 local bg = scene:add_input(bg_video)
155 local input0 = make_sbs_input(scene, true)
156 local input1 = make_sbs_input(scene, false)
158 input0.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0)
159 input1.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0)
161 input0.input:display(0)
162 input1.input:display(1)
164 local i0 = scene:add_effect(OverlayEffect.new(), bg, input0.padding_effect)
165 scene:add_effect(OverlayEffect.new(), i0, input1.padding_effect)
172 overlay = input0.overlay -- May be nil.
175 local sbs_scene = make_sbs_scene()
177 function make_simple_scene_no_finalize()
178 local scene = Scene.new(16, 9)
180 local input = scene:add_input()
181 local resample_effect = scene:add_effect({ResampleEffect.new(), ResizeEffect.new(), IdentityEffect.new()})
182 local wb_effect = scene:add_auto_white_balance()
183 local overlay = make_overlay(scene, wb_effect)
188 wb_effect = wb_effect,
189 resample_effect = resample_effect,
194 -- A scene to show a single input on screen.
195 function make_simple_scene()
196 local scene = make_simple_scene_no_finalize()
197 scene.scene:finalize()
200 local simple_scene = make_simple_scene()
202 -- Load all the stinger frames.
203 local stinger_images = {}
205 local filename = cef_path .. "/stinger/esk-blur" .. string.rep("0", 3 - string.len(tostring(idx))) .. idx .. ".png"
206 stinger_images[idx] = ImageInput.new(filename)
209 -- A scene to show a single input on screen, with a stinger on top.
210 function make_stinger_scene()
211 local scene = make_simple_scene_no_finalize()
212 scene.stinger_input = scene.scene:add_input()
213 scene.scene:add_effect(OverlayEffect.new(), scene.overlay.overlay_effect, scene.stinger_input)
214 scene.scene:finalize()
217 local stinger_scene = make_stinger_scene()
219 -- A scene to show a single static picture on screen. Never with HTML overlay.
220 local static_scene = Scene.new(16, 9)
221 static_scene:add_input(static_image)
222 static_scene:finalize()
224 -- A scene to show the overlay and nothing more. LQ only,
225 -- since it is not a valid live signal.
226 local overlay_scene_lq = Scene.new(16, 9)
227 local overlay_scene_lq_input = overlay_scene_lq:add_input(cef_input)
228 overlay_scene_lq:finalize(false)
230 -- A debugging scene to show a simple multiviewer. HQ only, since there is no preview.
231 function make_multiview_scene()
232 local scene = Scene.new(16, 9)
233 local bg = scene:add_input(ImageInput.new(cef_path .. "/nageru/white.png"))
234 local mv_width = 1280 / 3
235 local mv_height = 720 / 3
236 local mv_offset = (720 - mv_height * 2) / 2
239 local input = scene:add_input(input_idx)
241 local resize = scene:add_effect(ResizeEffect.new())
242 resize:set_int("width", mv_width)
243 resize:set_int("height", mv_height)
245 local padding = scene:add_effect(IntegralPaddingEffect.new())
246 padding:set_int("width", 1280)
247 padding:set_int("height", 720)
248 padding:set_int("left", (input_idx % 3) * mv_width)
249 padding:set_int("top", math.floor(input_idx / 3) * mv_height + mv_offset)
251 local overlay = scene:add_effect(OverlayEffect.new(), bg, padding)
258 local multiview_scene_hq = make_multiview_scene()
260 function needs_scale(signals, signal_num, width, height)
261 if signal_num == STATIC_SIGNAL_NUM then
262 -- We assume this is already correctly scaled at load time.
265 assert(is_plain_signal(signal_num))
266 return (signals:get_width(signal_num) ~= width or signals:get_height(signal_num) ~= height)
269 function set_scale_parameters_if_needed(scene, signals, signal_num, width, height, hq)
270 -- Futatabi rescaling will be linear, but original frames come back unscaled;
271 -- make them linear, too, so that we don't get more sharpness on every other
272 -- frame. This won't be right for fading, but it's probably hard to notice.
273 if signal_num == VIDEO_SIGNAL_NUM then
277 if needs_scale(signals, signal_num, width, height) then
279 scene.resample_effect:choose(ResampleEffect) -- High-quality resampling.
281 scene.resample_effect:choose(ResizeEffect) -- Low-quality resampling.
283 scene.resample_effect:set_int("width", width)
284 scene.resample_effect:set_int("height", height)
286 scene.resample_effect:disable()
292 -- Returns the number of outputs in addition to the live (0) and preview (1).
293 -- Called only once, at the start of the program.
294 function num_channels()
295 return NUM_CAMERAS + 4 -- sbs, static picture, video and overlay
298 function is_plain_signal(num)
299 return (num >= INPUT0_SIGNAL_NUM and num <= INPUT5_SIGNAL_NUM) or (num == VIDEO_SIGNAL_NUM)
302 -- Helper function to write e.g. “720p60”. The difference between this
303 -- and get_channel_resolution_raw() is that this one also can say that
304 -- there's no signal.
305 function get_channel_resolution(signal_num)
306 local res = last_resolution[signal_num]
307 if (not res) or not res.is_connected then
308 return "disconnected"
310 if res.height <= 0 then
313 if not res.has_signal then
314 if res.height == 525 then
315 -- Special mode for the USB3 cards.
318 return get_channel_resolution_raw(res) .. ", no signal"
320 return get_channel_resolution_raw(res)
324 -- Helper function to write e.g. “60” or “59.94”.
325 function get_frame_rate(res)
326 local nom = res.frame_rate_nom
327 local den = res.frame_rate_den
328 if nom % den == 0 then
331 return string.format("%.2f", nom / den)
335 -- Helper function to write e.g. “720p60”.
336 function get_channel_resolution_raw(res)
337 if res.interlaced then
338 return res.height .. "i" .. get_frame_rate(res)
340 return res.height .. "p" .. get_frame_rate(res)
344 function get_futatabi_status(str)
347 for word in string.gmatch(str, '([^;]+)') do
348 table.insert(fields, word)
349 num_fields = num_fields + 1
351 if num_fields >= 4 then
359 -- Returns the name for each additional channel (starting from 2).
360 -- Called at the start of the program, and then each frame for live
361 -- channels in case they change resolution.
362 function channel_name(channel)
363 local signal_num = channel - 2
364 if signal_num == INPUT0_SIGNAL_NUM then
365 return "Main (" .. get_channel_resolution(signal_num) .. ")"
366 elseif signal_num == INPUT1_SIGNAL_NUM then
367 return "Secondary (" .. get_channel_resolution(signal_num) .. ")"
368 elseif signal_num == INPUT2_SIGNAL_NUM then
369 return "Goal L (" .. get_channel_resolution(signal_num) .. ")"
370 elseif signal_num == INPUT3_SIGNAL_NUM then
371 return "Goal R (" .. get_channel_resolution(signal_num) .. ")"
372 elseif signal_num == INPUT4_SIGNAL_NUM then
373 return "Ambience (" .. get_channel_resolution(signal_num) .. ")"
374 elseif signal_num == INPUT5_SIGNAL_NUM then
375 return "Commentators (" .. get_channel_resolution(signal_num) .. ")"
376 elseif signal_num == VIDEO_SIGNAL_NUM then
377 local res = last_resolution[iptv_video:get_signal_num()]
378 if (not res) or res.last_subtitle == nil then
381 return "Futatabi (" .. get_futatabi_status(res.last_subtitle) .. ")"
383 elseif signal_num == SBS_SIGNAL_NUM then
384 return "Side-by-side"
385 elseif signal_num == STATIC_SIGNAL_NUM then
386 return "Static picture"
387 elseif signal_num == OVERLAY_SIGNAL_NUM then
393 -- Returns, given a channel number, which signal it corresponds to (starting from 0).
394 -- Should return -1 if the channel does not correspond to a simple signal.
395 -- (The information is used for whether right-click on the channel should bring up
396 -- an input selector or not.)
397 -- Called once for each channel, at the start of the program.
398 -- Will never be called for live (0) or preview (1).
399 function channel_signal(channel)
400 if channel - 2 == VIDEO_SIGNAL_NUM then
401 return iptv_video:get_signal_num()
402 elseif is_plain_signal(channel - 2) then
410 -- Called every frame. Returns the color (if any) to paint around the given
411 -- channel. Returns a CSS color (typically to mark live and preview signals);
412 -- "transparent" is allowed.
413 -- Will never be called for live (0) or preview (1).
414 function channel_color(channel)
415 if state.transition_type ~= NO_TRANSITION then
416 if channel_involved_in(channel, state.transition_src_signal) or
417 channel_involved_in(channel, state.transition_dst_signal) then
421 if channel_involved_in(channel, state.live_signal_num) then
425 if channel_involved_in(channel, state.preview_signal_num) then
431 function channel_involved_in(channel, signal_num)
432 if is_plain_signal(signal_num) then
433 return channel == (signal_num + 2)
435 if signal_num == SBS_SIGNAL_NUM then
436 return is_sbs_participating_signal(channel - 2)
438 if signal_num == STATIC_SIGNAL_NUM then
439 return (channel == NUM_CAMERAS)
441 if signal_num == VIDEO_SIGNAL_NUM then
442 return (channel == NUM_CAMERAS + 1)
448 -- Returns if a given channel supports setting white balance (starting from 2).
449 -- Called only once for each channel, at the start of the program.
450 function supports_set_wb(channel)
451 return is_plain_signal(channel - 2)
454 function finish_transitions(t)
455 if state.transition_type ~= NO_TRANSITION and t >= state.transition_end then
456 state.live_signal_num = state.transition_dst_signal
457 state.transition_type = NO_TRANSITION
460 -- Disable the overlay if it is no longer visible.
461 if state.overlay_enabled and t > state.overlay_transition_end and state.overlay_alpha_dst == 0.0 then
462 state.overlay_enabled = false
463 print("Turning off overlay")
467 function in_transition(t)
468 return t >= state.transition_start and t <= state.transition_end
471 function is_sbs_participating_signal(signal_num)
472 return signal_num == INPUT0_SIGNAL_NUM or signal_num == INPUT4_SIGNAL_NUM
475 function simple_signal_has_overlay(signal_num)
476 -- The commentator output has no overlay on it.
477 return signal_num ~= INPUT4_SIGNAL_NUM
481 -- Called every frame.
482 function get_transitions(t)
483 if state.preview_signal_num == OVERLAY_SIGNAL_NUM then
484 if t < state.overlay_transition_end then
488 if state.overlay_enabled then
489 return {"Overlay off", "", "Fade ovl out"}
491 return {"Overlay on", "", "Fade ovl in"}
495 if in_transition(t) then
496 -- Transition already in progress, the only thing we can do is really
497 -- cut to the preview. (TODO: Make an “abort” and/or “finish”, too?)
501 if state.live_signal_num == state.preview_signal_num then
502 -- No transitions possible.
506 if (is_plain_signal(state.live_signal_num) and state.preview_signal_num == VIDEO_SIGNAL_NUM) or
507 (is_plain_signal(state.preview_signal_num) and state.live_signal_num == VIDEO_SIGNAL_NUM) then
508 return {"Cut", "Sting", "Fade"}
511 if (is_plain_signal(state.live_signal_num) or state.live_signal_num == STATIC_SIGNAL_NUM) and
512 (is_plain_signal(state.preview_signal_num) or state.preview_signal_num == STATIC_SIGNAL_NUM) then
513 return {"Cut", "", "Fade"}
517 if state.live_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(state.preview_signal_num) then
518 return {"Cut", "Zoom in"}
519 elseif is_sbs_participating_signal(state.live_signal_num) and state.preview_signal_num == SBS_SIGNAL_NUM then
520 return {"Cut", "Zoom out"}
526 function swap_preview_live()
527 local temp = state.live_signal_num
528 state.live_signal_num = state.preview_signal_num
529 state.preview_signal_num = temp
532 function start_transition(type_, t, duration)
533 state.transition_start = t
534 state.transition_end = t + duration
535 state.transition_type = type_
536 state.transition_src_signal = state.live_signal_num
537 state.transition_dst_signal = state.preview_signal_num
538 state.stinger_in_progress = false
543 -- Called when the user clicks a transition button.
544 function transition_clicked(num, t)
545 if state.preview_signal_num == OVERLAY_SIGNAL_NUM then
548 state.overlay_transition_start = -2.0
549 state.overlay_transition_end = -1.0
550 if state.overlay_enabled then
551 state.overlay_enabled = false
552 state.overlay_alpha_src = 0.0
553 state.overlay_alpha_dst = 0.0
555 state.overlay_enabled = true
556 state.overlay_alpha_src = 1.0
557 state.overlay_alpha_dst = 1.0
561 state.overlay_transition_start = t
562 state.overlay_transition_end = t + 1.0
563 if state.overlay_enabled then
564 state.overlay_alpha_src = 1.0
565 state.overlay_alpha_dst = 0.0
567 state.overlay_alpha_src = 0.0
568 state.overlay_alpha_dst = 1.0
570 state.overlay_enabled = true
577 if in_transition(t) then
578 -- Ongoing transition; finish it immediately before the cut.
579 finish_transitions(state.transition_end)
583 state.stinger_in_progress = false
586 finish_transitions(t)
588 if state.live_signal_num == state.preview_signal_num then
593 if (is_plain_signal(state.live_signal_num) and state.preview_signal_num == VIDEO_SIGNAL_NUM) or
594 (is_plain_signal(state.preview_signal_num) and state.live_signal_num == VIDEO_SIGNAL_NUM) then
596 if stinger_in_progress then
600 state.stinger_in_progress = true
601 state.stinger_frame = 0
602 state.stinger_src_signal = state.live_signal_num
603 state.stinger_dst_signal = state.preview_signal_num
607 if is_plain_signal(state.live_signal_num) and is_plain_signal(state.preview_signal_num) then
608 -- We can't zoom between these. Just make a cut.
609 io.write("Cutting from " .. state.live_signal_num .. " to " .. state.live_signal_num .. "\n")
611 state.stinger_in_progress = false
615 if (state.live_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(state.preview_signal_num)) or
616 (state.preview_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(state.live_signal_num)) then
617 start_transition(ZOOM_TRANSITION, t, 1.0)
620 finish_transitions(t)
623 if (state.live_signal_num ~= state.preview_signal_num) and
624 (is_plain_signal(state.live_signal_num) or
625 state.live_signal_num == STATIC_SIGNAL_NUM) and
626 (is_plain_signal(state.preview_signal_num) or
627 state.preview_signal_num == STATIC_SIGNAL_NUM) then
628 start_transition(FADE_TRANSITION, t, 1.0)
630 -- Fades involving SBS are ignored (we have no scene for it).
636 function channel_clicked(num)
637 state.preview_signal_num = num
640 function setup_fade_input(state, input, signals, signal_num, width, height)
641 if signal_num == STATIC_SIGNAL_NUM then
642 input.input:display(static_image)
644 -- We assume this is already correctly scaled at load time.
645 input.resample_effect:disable()
647 input.input:display(signal_num)
649 if (signals:get_width(signal_num) ~= width or signals:get_height(signal_num) ~= height) then
650 input.resample_effect:enable()
651 input.resample_effect:set_int("width", width)
652 input.resample_effect:set_int("height", height)
654 input.resample_effect:disable()
659 function get_fade_scene(state, signals, t, width, height, input_resolution)
660 local scene = fade_scene
661 setup_fade_input(state, scene.input0, signals, state.transition_src_signal, width, height)
662 setup_fade_input(state, scene.input1, signals, state.transition_dst_signal, width, height)
664 local tt = calc_fade_progress(t, state.transition_start, state.transition_end)
665 scene.mix_effect:set_float("strength_first", 1.0 - tt)
666 scene.mix_effect:set_float("strength_second", tt)
668 -- The commentator output has no overlay on it.
669 local extra_alpha_factor = 1.0
670 if not simple_signal_has_overlay(state.transition_src_signal) and
671 not simple_signal_has_overlay(state.transition_dst_signal) then
672 extra_alpha_factor = 0.0
673 elseif not simple_signal_has_overlay(state.transition_src_signal) then
674 extra_alpha_factor = tt
675 elseif not simple_signal_has_overlay(state.transition_dst_signal) then
676 extra_alpha_factor = 1.0 - tt
679 -- If fading between two live inputs, the overlay is put on top.
680 -- If fading between the static picture and a live input,
681 -- the overlay is put on the live input.
682 scene.input0.overlay.overlay_effect:disable()
683 scene.input1.overlay.overlay_effect:disable()
684 scene.overlay.overlay_effect:disable()
685 if state.overlay_enabled then
686 local input0_live = (state.transition_src_signal ~= STATIC_SIGNAL_NUM)
687 local input1_live = (state.transition_dst_signal ~= STATIC_SIGNAL_NUM)
688 if input0_live and not input1_live then
689 scene.input0.overlay.overlay_effect:enable()
690 prepare_overlay_live(state, scene.input0, t, extra_alpha_factor)
691 elseif input1_live and not input0_live then
692 scene.input1.overlay.overlay_effect:enable()
693 prepare_overlay_live(state, scene.input1, t, extra_alpha_factor)
695 scene.overlay.overlay_effect:enable()
696 prepare_overlay_live(state, scene, t, extra_alpha_factor)
702 function fetch_input_resolution(signals, signal_num)
704 width = signals:get_width(signal_num),
705 height = signals:get_height(signal_num),
706 interlaced = signals:get_interlaced(signal_num),
707 has_signal = signals:get_has_signal(signal_num),
708 is_connected = signals:get_is_connected(signal_num),
709 frame_rate_nom = signals:get_frame_rate_nom(signal_num),
710 frame_rate_den = signals:get_frame_rate_den(signal_num),
711 last_subtitle = signals:get_last_subtitle(signal_num)
714 if res.interlaced then
715 -- Convert height from frame height to field height.
716 -- (Needed for e.g. place_rectangle.)
717 res.height = res.height * 2
719 -- Show field rate instead of frame rate; really for cosmetics only
720 -- (and actually contrary to EBU recommendations, although in line
721 -- with typical user expectations).
722 res.frame_rate_nom = res.frame_rate_nom * 2
727 function update_volume()
728 if Nageru.get_num_audio_buses() < 3 or
729 Nageru.get_audio_bus_name(2) ~= 'Sound Shark' then
730 -- We haven't loaded the right audio mapping.
735 Nageru.set_audio_bus_fader_level_db(2, -math.huge)
739 local volume_db = Nageru.get_audio_bus_fader_level_db(2)
740 if Nageru.get_audio_bus_mute(2) or volume_db < -100.0 then
743 cef_input:execute_javascript_async("set_sound_shark_volume_db(" .. volume_db .. ")")
746 local last_rate = 0.0
749 -- Called every frame. Get the scene for displaying at input <num>,
750 -- where 0 is live, 1 is preview, 2 is the first channel to display
751 -- in the bottom bar, and so on up to num_channels()+1. t is the
752 -- current time in seconds. width and height are the dimensions of
753 -- the output, although you can ignore them if you don't need them
754 -- (they're useful if you want to e.g. know what to resample by).
756 -- <signals> is basically an exposed InputState, which you can use to
757 -- query for information about the signals at the point of the current
758 -- frame. In particular, you can call get_width() and get_height()
759 -- for any signal number, and use that to e.g. assist in scene selection.
761 -- You should return scene to use, after having set any parameters you
762 -- want to set (through set_int() etc.). The parameters will be snapshot
763 -- at return time and used during rendering.
764 function get_scene(num, t, width, height, signals)
769 local input_resolution = {}
770 for signal_num=0,(NUM_CAMERAS-1) do
771 input_resolution[signal_num] = fetch_input_resolution(signals, signal_num)
773 input_resolution[iptv_video:get_signal_num()] = fetch_input_resolution(signals, iptv_video:get_signal_num())
774 last_resolution = input_resolution
776 -- Save some CPU time if we're not having SBS on live.
778 if state.live_signal_num == SBS_SIGNAL_NUM or
779 state.preview_signal_num == SBS_SIGNAL_NUM or
780 state.transition_type == ZOOM_TRANSITION then
785 if new_rate ~= last_rate then
786 -- Avoid waking up the video thread (which may be sleeping) if the rate is the same.
787 bg_video:change_rate(new_rate)
791 if num == 0 and multiviewer_enabled then -- Live, with debugging on.
792 return multiview_scene_hq
795 if num == 0 then -- Live.
796 -- See if we're in a transition.
797 finish_transitions(t)
798 if state.transition_type == ZOOM_TRANSITION then
799 -- Transition in or out of SBS.
800 sbs_scene.input0.overlay.overlay_effect:enable_if(state.overlay_enabled)
801 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)
802 prepare_overlay_live(state, sbs_scene, t, 1.0)
803 return sbs_scene.scene
804 elseif state.transition_type == NO_TRANSITION and state.live_signal_num == SBS_SIGNAL_NUM then
806 sbs_scene.input0.overlay.overlay_effect:enable_if(state.overlay_enabled)
807 prepare_sbs_scene(state, sbs_scene, 0.0, NO_TRANSITION, 0, SBS_SIGNAL_NUM, width, height, input_resolution, true)
808 prepare_overlay_live(state, sbs_scene, t, 1.0)
809 return sbs_scene.scene
810 elseif state.transition_type == FADE_TRANSITION then
811 return get_fade_scene(state, signals, t, width, height, input_resolution)
812 elseif is_plain_signal(state.live_signal_num) then
813 local overlay_really_enabled = state.overlay_enabled and simple_signal_has_overlay(state.live_signal_num)
814 if state.stinger_in_progress then
815 stinger_scene.overlay.overlay_effect:enable_if(overlay_really_enabled)
816 if state.live_signal_num == VIDEO_SIGNAL_NUM then
817 stinger_scene.input:display(iptv_video)
819 stinger_scene.input:display(state.live_signal_num)
821 set_scale_parameters_if_needed(stinger_scene, signals, state.live_signal_num, width, height, true)
823 stinger_scene.stinger_input:display(stinger_images[state.stinger_frame])
824 state.stinger_frame = state.stinger_frame + 1
825 if state.stinger_frame >= 25 then
826 state.stinger_in_progress = false
827 state.preview_signal_num = state.stinger_src_signal
828 state.live_signal_num = state.stinger_dst_signal
830 if state.stinger_dst_signal == VIDEO_SIGNAL_NUM then
831 -- Turn off the overlay when playing video.
832 state.stinger_save_overlay = state.overlay_enabled
833 state.overlay_enabled = false
835 -- Restore the state.
836 state.overlay_enabled = state.stinger_save_overlay
840 if overlay_really_enabled then
841 prepare_overlay_live(state, stinger_scene, t, 1.0)
843 return stinger_scene.scene
845 simple_scene.overlay.overlay_effect:enable_if(overlay_really_enabled)
846 if state.live_signal_num == VIDEO_SIGNAL_NUM then
847 simple_scene.input:display(iptv_video)
849 simple_scene.input:display(state.live_signal_num)
851 set_scale_parameters_if_needed(simple_scene, signals, state.live_signal_num, width, height, true)
852 prepare_overlay_live(state, simple_scene, t, 1.0)
853 return simple_scene.scene
855 elseif state.live_signal_num == STATIC_SIGNAL_NUM then -- Static picture.
862 -- We do not show overlays on the individual preview inputs.
863 -- The M/E preview matches what we'd put live by doing a transition, as always.
864 local show_overlay = false
865 if num == 1 then -- Preview.
866 if state.preview_signal_num == OVERLAY_SIGNAL_NUM then
867 num = state.live_signal_num + 2
868 show_overlay = not state.overlay_enabled
870 if state.transition_type ~= NO_TRANSITION then
871 num = state.transition_dst_signal + 2
874 num = state.preview_signal_num + 2
875 show_overlay = state.overlay_enabled and simple_signal_has_overlay(state.preview_signal_num)
879 -- Individual preview inputs (usually without overlay).
880 if is_plain_signal(num - 2) then
881 local signal_num = num - 2
882 simple_scene.overlay.overlay_effect:enable_if(show_overlay)
883 if signal_num == VIDEO_SIGNAL_NUM then
884 simple_scene.input:display(iptv_video)
886 simple_scene.input:display(signal_num)
888 set_scale_parameters_if_needed(simple_scene, signals, signal_num, width, height, false)
889 prepare_overlay_static(simple_scene, t)
890 return simple_scene.scene
892 if num == SBS_SIGNAL_NUM + 2 then
893 sbs_scene.input0.overlay.overlay_effect:enable_if(show_overlay)
894 prepare_sbs_scene(state, sbs_scene, 0.0, NO_TRANSITION, 0, SBS_SIGNAL_NUM, width, height, input_resolution, false)
895 return sbs_scene.scene
897 if num == STATIC_SIGNAL_NUM + 2 then
900 if num == OVERLAY_SIGNAL_NUM + 2 then
901 return overlay_scene_lq
905 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
907 return math.floor(x + 0.5)
910 function lerp(a, b, t)
911 return a + (b - a) * t
914 function lerp_pos(a, b, t)
916 x0 = lerp(a.x0, b.x0, t),
917 y0 = lerp(a.y0, b.y0, t),
918 x1 = lerp(a.x1, b.x1, t),
919 y1 = lerp(a.y1, b.y1, t)
923 function pos_from_top_left(x, y, width, height, screen_width, screen_height)
924 local xs = screen_width / 1280.0
925 local ys = screen_height / 720.0
929 x1 = round(xs * (x + width)),
930 y1 = round(ys * (y + height))
934 function prepare_sbs_scene(state, scene, t, transition_type, src_signal, dst_signal, screen_width, screen_height, input_resolution, hq)
935 scene.input0.input:display(0)
936 scene.input1.input:display(5)
938 -- Both inputs are the same size (true side-by-side).
939 local pos0 = pos_from_top_left(1280 - 616 - 16, 186, 616, 347, screen_width, screen_height)
940 local pos1 = pos_from_top_left(16, 186, 616, 347, screen_width, screen_height)
942 local pos_fs = { x0 = 0, y0 = 0, x1 = screen_width, y1 = screen_height }
944 if transition_type == NO_TRANSITION then
946 affine_param = { sx = 1.0, sy = 1.0, tx = 0.0, ty = 0.0 } -- Identity.
948 -- Zooming to/from SBS view into or out of a single view.
949 assert(transition_type == ZOOM_TRANSITION)
951 if src_signal == SBS_SIGNAL_NUM then
955 assert(dst_signal == SBS_SIGNAL_NUM)
960 if signal == INPUT0_SIGNAL_NUM then
961 affine_param = find_affine_param(pos0, lerp_pos(pos0, pos_fs, real_t))
962 elseif signal == INPUT4_SIGNAL_NUM then
963 affine_param = find_affine_param(pos1, lerp_pos(pos1, pos_fs, real_t))
967 -- NOTE: input_resolution is not 1-indexed, unlike usual Lua arrays.
968 place_rectangle_with_affine(scene.input0, pos0, affine_param, screen_width, screen_height, input_resolution[0].width, input_resolution[0].height, hq)
969 place_rectangle_with_affine(scene.input1, pos1, affine_param, screen_width, screen_height, input_resolution[1].width, input_resolution[1].height, hq)
972 -- Find the transformation that changes the first rectangle to the second one.
973 function find_affine_param(a, b)
974 local sx = (b.x1 - b.x0) / (a.x1 - a.x0)
975 local sy = (b.y1 - b.y0) / (a.y1 - a.y0)
979 tx = b.x0 - a.x0 * sx,
980 ty = b.y0 - a.y0 * sy
984 function place_rectangle_with_affine(input, pos, aff, screen_width, screen_height, input_width, input_height, hq)
985 local x0 = pos.x0 * aff.sx + aff.tx
986 local x1 = pos.x1 * aff.sx + aff.tx
987 local y0 = pos.y0 * aff.sy + aff.ty
988 local y1 = pos.y1 * aff.sy + aff.ty
990 place_rectangle(input, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height, hq)
993 function place_rectangle(input, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height, hq)
994 input.padding_effect:set_int("width", screen_width)
995 input.padding_effect:set_int("height", screen_height)
998 if x0 > screen_width or x1 < 0.0 or y0 > screen_height or y1 < 0.0 then
999 input.resample_effect:choose(ResizeEffect) -- Low-quality resizing.
1000 input.resample_effect:set_int("width", 1)
1001 input.resample_effect:set_int("height", 1)
1002 input.padding_effect:set_int("left", screen_width + 100)
1003 input.padding_effect:set_int("top", screen_height + 100)
1014 srcx0 = -x0 / (x1 - x0)
1018 srcy0 = -y0 / (y1 - y0)
1021 if x1 > screen_width then
1022 srcx1 = (screen_width - x0) / (x1 - x0)
1025 if y1 > screen_height then
1026 srcy1 = (screen_height - y0) / (y1 - y0)
1031 -- High-quality resampling.
1032 local resample_effect = input.resample_effect:choose(ResampleEffect)
1034 local x_subpixel_offset = x0 - math.floor(x0)
1035 local y_subpixel_offset = y0 - math.floor(y0)
1037 -- Resampling must be to an integral number of pixels. Round up,
1038 -- and then add an extra pixel so we have some leeway for the border.
1039 local width = math.ceil(x1 - x0) + 1
1040 local height = math.ceil(y1 - y0) + 1
1041 input.resample_effect:set_int("width", width)
1042 input.resample_effect:set_int("height", height)
1044 -- Correct the discrepancy with zoom. (This will leave a small
1045 -- excess edge of pixels and subpixels, which we'll correct for soon.)
1046 local zoom_x = (x1 - x0) / (width * (srcx1 - srcx0))
1047 local zoom_y = (y1 - y0) / (height * (srcy1 - srcy0))
1048 resample_effect:set_float("zoom_x", zoom_x) -- Use the actual effect specialization; specific to ResampleEffect.
1049 resample_effect:set_float("zoom_y", zoom_y)
1050 resample_effect:set_float("zoom_center_x", 0.0)
1051 resample_effect:set_float("zoom_center_y", 0.0)
1053 -- Padding must also be to a whole-pixel offset.
1054 input.padding_effect:set_int("left", math.floor(x0))
1055 input.padding_effect:set_int("top", math.floor(y0))
1057 -- Correct _that_ discrepancy by subpixel offset in the resampling.
1058 resample_effect:set_float("left", srcx0 * input_width - x_subpixel_offset / zoom_x)
1059 resample_effect:set_float("top", srcy0 * input_height - y_subpixel_offset / zoom_y)
1061 -- Finally, adjust the border so it is exactly where we want it.
1062 input.padding_effect:set_float("border_offset_left", x_subpixel_offset)
1063 input.padding_effect:set_float("border_offset_right", x1 - (math.floor(x0) + width))
1064 input.padding_effect:set_float("border_offset_top", y_subpixel_offset)
1065 input.padding_effect:set_float("border_offset_bottom", y1 - (math.floor(y0) + height))
1067 -- Lower-quality simple resizing.
1068 input.resample_effect:choose(ResizeEffect)
1070 local width = round(x1 - x0)
1071 local height = round(y1 - y0)
1072 input.resample_effect:set_int("width", width)
1073 input.resample_effect:set_int("height", height)
1075 -- Padding must also be to a whole-pixel offset.
1076 input.padding_effect:set_int("left", math.floor(x0))
1077 input.padding_effect:set_int("top", math.floor(y0))
1079 -- No subpixel stuff.
1080 input.padding_effect:set_float("border_offset_left", 0.0)
1081 input.padding_effect:set_float("border_offset_right", 0.0)
1082 input.padding_effect:set_float("border_offset_top", 0.0)
1083 input.padding_effect:set_float("border_offset_bottom", 0.0)
1087 function prepare_overlay_live(state, scene, t, extra_alpha_factor)
1088 if scene.overlay then
1089 local tt = calc_fade_progress(t, state.overlay_transition_start, state.overlay_transition_end)
1090 local overlay_alpha = state.overlay_alpha_src + tt * (state.overlay_alpha_dst - state.overlay_alpha_src)
1091 overlay_alpha = overlay_alpha * extra_alpha_factor
1092 --print("overlay_alpha=" .. overlay_alpha .. " [" .. state.overlay_alpha_src .. "," .. state.overlay_alpha_dst .. "]@" .. tt)
1093 if t > state.overlay_transition_end and state.overlay_alpha_dst == 0.0 then
1094 state.overlay_enabled = false -- Takes effect next frame.
1095 -- print("Turning off overlay")
1097 scene.overlay.multiply_effect:set_vec4("factor", overlay_alpha, overlay_alpha, overlay_alpha, overlay_alpha)
1101 function prepare_overlay_static(scene)
1102 if scene.overlay then
1103 scene.overlay.multiply_effect:set_vec4("factor", 1.0, 1.0, 1.0, 1.0)
1107 function calc_zoom_progress(t)
1108 if t < state.transition_start then
1110 elseif t > state.transition_end then
1113 local tt = (t - state.transition_start) / (state.transition_end - state.transition_start)
1115 return math.sin(tt * 3.14159265358 * 0.5)
1119 function calc_fade_progress(t, transition_start, transition_end)
1120 local tt = (t - transition_start) / (transition_end - transition_start)
1123 elseif tt > 1.0 then
1127 -- Make the fade look maybe a tad more natural, by pumping it
1128 -- through a sigmoid function.
1129 tt = 10.0 * tt - 5.0
1130 tt = 1.0 / (1.0 + math.exp(-tt))
1136 { "Reload overlay", reload_cef },
1137 { "Disconnect IPTV", disconnect_iptv },
1138 { "Toggle multiviewer", toggle_multiviewer }