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 INPUT6_SIGNAL_NUM = 6
37 local VIDEO_SIGNAL_NUM = NUM_CAMERAS
38 local SBS_SIGNAL_NUM = NUM_CAMERAS + 1
39 local STATIC_SIGNAL_NUM = NUM_CAMERAS + 2
41 -- Preview-only signal showing the current signal with the overlay.
42 -- Not valid for live_signal_num!
43 local OVERLAY_SIGNAL_NUM = NUM_CAMERAS + 3
45 -- Valid values for transition_type. (Cuts are done directly, so they need no entry.)
46 local NO_TRANSITION = 0
47 local ZOOM_TRANSITION = 1
48 local FADE_TRANSITION = 2
50 -- Last width/height/frame rate for each channel, if we have it.
51 -- Note that unlike the values we get from Nageru, the resolution is per
52 -- frame and not per field, since we deinterlace.
53 local last_resolution = {}
55 local cef_path = Nageru.THEME_PATH:match("(.*)/nageru/")
56 local cef_input = HTMLInput.new("file://" .. cef_path .. "/score.html")
57 cef_input:execute_javascript_async("play()")
59 local bg_video = VideoInput.new(cef_path .. "/flow-720.mp4", Nageru.VIDEO_FORMAT_YCBCR)
60 -- local iptv_video = VideoInput.new("http://10.34.129.69:9060/1/v.flv", Nageru.VIDEO_FORMAT_YCBCR)
61 local iptv_video = VideoInput.new(futatabi_server, Nageru.VIDEO_FORMAT_YCBCR)
62 iptv_video:change_rate(10.0)
64 local static_image = ImageInput.new(cef_path .. "/nageru/tfk_pause.png")
65 local first_frame = true
66 local multiviewer_enabled = false
70 cef_input:execute_javascript_async("play()")
73 function disconnect_iptv()
74 iptv_video:disconnect()
77 function toggle_multiviewer()
78 multiviewer_enabled = not multiviewer_enabled
81 -- An overlay with variable alpha.
82 function make_overlay(scene, base)
83 local image = scene:add_input(cef_input)
84 local multiply_effect = scene:add_effect(MultiplyEffect.new())
85 local overlay_effect = scene:add_optional_effect(OverlayEffect.new(), base, multiply_effect)
88 multiply_effect = multiply_effect,
89 overlay_effect = overlay_effect
93 function make_fade_input(scene)
95 input = scene:add_input(),
96 resample_effect = scene:add_optional_effect(ResampleEffect.new()), -- Activated if scaling.
97 wb_effect = scene:add_white_balance()
99 ret.overlay = make_overlay(scene, ret.wb_effect)
103 -- A scene to fade between two inputs, of which either can be a picture
105 function make_fade_scene()
106 local scene = Scene.new(16, 9)
108 -- If fading between two live inputs, the overlay is put on top.
109 -- If fading between the static picture and a live input,
110 -- the overlay is put on the live input.
111 local input0 = make_fade_input(scene)
112 local input1 = make_fade_input(scene)
113 local mix_effect = scene:add_effect(MixEffect.new(), input0.overlay.overlay_effect, input1.overlay.overlay_effect)
114 local overlay = make_overlay(scene, mix_effect)
116 -- At most one overlay can be active at any given time.
117 input0.overlay.overlay_effect:promise_to_disable_if_enabled(input1.overlay.overlay_effect)
118 input0.overlay.overlay_effect:promise_to_disable_if_enabled(overlay.overlay_effect)
119 input1.overlay.overlay_effect:promise_to_disable_if_enabled(input0.overlay.overlay_effect)
120 input1.overlay.overlay_effect:promise_to_disable_if_enabled(overlay.overlay_effect)
121 overlay.overlay_effect:promise_to_disable_if_enabled(input0.overlay.overlay_effect)
122 overlay.overlay_effect:promise_to_disable_if_enabled(input1.overlay.overlay_effect)
124 scene:finalize(true) -- Only used live, don't bother creating LQ versions.
130 mix_effect = mix_effect,
134 fade_scene = make_fade_scene()
136 function make_sbs_input(scene, has_overlay)
137 local input = scene:add_input()
138 local wb_effect = scene:add_white_balance()
139 local overlay = has_overlay and make_overlay(scene, wb_effect) or nil
140 local resample_effect = scene:add_effect({ResampleEffect.new(), ResizeEffect.new()})
141 local padding_effect = scene:add_effect(IntegralPaddingEffect.new())
145 resample_effect = resample_effect,
146 wb_effect = wb_effect,
147 padding_effect = padding_effect
151 -- Side-by-side scene.
152 function make_sbs_scene()
153 local scene = Scene.new(16, 9)
155 local bg = scene:add_input(bg_video)
156 local input0 = make_sbs_input(scene, true)
157 local input1 = make_sbs_input(scene, false)
159 input0.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0)
160 input1.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0)
162 input0.input:display(0)
163 input1.input:display(1)
165 local i0 = scene:add_effect(OverlayEffect.new(), bg, input0.padding_effect)
166 scene:add_effect(OverlayEffect.new(), i0, input1.padding_effect)
173 overlay = input0.overlay -- May be nil.
176 local sbs_scene = make_sbs_scene()
178 function make_simple_scene_no_finalize()
179 local scene = Scene.new(16, 9)
181 local input = scene:add_input()
182 local resample_effect = scene:add_effect({ResampleEffect.new(), ResizeEffect.new(), IdentityEffect.new()})
183 local wb_effect = scene:add_white_balance()
184 local overlay = make_overlay(scene, wb_effect)
189 wb_effect = wb_effect,
190 resample_effect = resample_effect,
195 -- A scene to show a single input on screen.
196 function make_simple_scene()
197 local scene = make_simple_scene_no_finalize()
198 scene.scene:finalize()
201 local simple_scene = make_simple_scene()
203 -- Load all the stinger frames.
204 local stinger_images = {}
206 local filename = cef_path .. "/stinger/blur" .. string.rep("0", 3 - string.len(tostring(idx))) .. idx .. ".png"
207 stinger_images[idx] = ImageInput.new(filename)
210 -- A scene to show a single input on screen, with a stinger on top.
211 function make_stinger_scene()
212 local scene = make_simple_scene_no_finalize()
213 scene.stinger_input = scene.scene:add_input()
214 scene.scene:add_effect(OverlayEffect.new(), scene.overlay.overlay_effect, scene.stinger_input)
215 scene.scene:finalize()
218 local stinger_scene = make_stinger_scene()
220 -- A scene to show a single static picture on screen. Never with HTML overlay.
221 local static_scene = Scene.new(16, 9)
222 static_scene:add_input(static_image)
223 static_scene:finalize()
225 -- A scene to show the overlay and nothing more. LQ only,
226 -- since it is not a valid live signal.
227 local overlay_scene_lq = Scene.new(16, 9)
228 local overlay_scene_lq_input = overlay_scene_lq:add_input(cef_input)
229 overlay_scene_lq:finalize(false)
231 -- A debugging scene to show a simple multiviewer. HQ only, since there is no preview.
232 function make_multiview_scene()
233 local scene = Scene.new(16, 9)
234 local bg = scene:add_input(ImageInput.new(cef_path .. "/nageru/white.png"))
235 local mv_width = 1280 / 3
236 local mv_height = 720 / 3
237 local mv_offset = (720 - mv_height * 2) / 2
240 local input = scene:add_input(input_idx)
242 local resize = scene:add_effect(ResizeEffect.new())
243 resize:set_int("width", mv_width)
244 resize:set_int("height", mv_height)
246 local padding = scene:add_effect(IntegralPaddingEffect.new())
247 padding:set_int("width", 1280)
248 padding:set_int("height", 720)
249 padding:set_int("left", (input_idx % 3) * mv_width)
250 padding:set_int("top", math.floor(input_idx / 3) * mv_height + mv_offset)
252 local overlay = scene:add_effect(OverlayEffect.new(), bg, padding)
259 local multiview_scene_hq = make_multiview_scene()
261 function needs_scale(signals, signal_num, width, height)
262 if signal_num == STATIC_SIGNAL_NUM then
263 -- We assume this is already correctly scaled at load time.
266 assert(is_plain_signal(signal_num))
267 return (signals:get_width(signal_num) ~= width or signals:get_height(signal_num) ~= height)
270 function set_scale_parameters_if_needed(scene, signals, signal_num, width, height, hq)
271 -- Futatabi rescaling will be linear, but original frames come back unscaled;
272 -- make them linear, too, so that we don't get more sharpness on every other
273 -- frame. This won't be right for fading, but it's probably hard to notice.
274 if signal_num == VIDEO_SIGNAL_NUM then
278 if needs_scale(signals, signal_num, width, height) then
280 scene.resample_effect:choose(ResampleEffect) -- High-quality resampling.
282 scene.resample_effect:choose(ResizeEffect) -- Low-quality resampling.
284 scene.resample_effect:set_int("width", width)
285 scene.resample_effect:set_int("height", height)
287 scene.resample_effect:disable()
293 -- Returns the number of outputs in addition to the live (0) and preview (1).
294 -- Called only once, at the start of the program.
295 function num_channels()
296 return NUM_CAMERAS + 4 -- sbs, static picture, video and overlay
299 function is_plain_signal(num)
300 return (num >= INPUT0_SIGNAL_NUM and num <= INPUT6_SIGNAL_NUM) or (num == VIDEO_SIGNAL_NUM)
303 -- Helper function to write e.g. “720p60”. The difference between this
304 -- and get_channel_resolution_raw() is that this one also can say that
305 -- there's no signal.
306 function get_channel_resolution(signal_num)
307 local res = last_resolution[signal_num]
308 if (not res) or not res.is_connected then
309 return "disconnected"
311 if res.height <= 0 then
314 if not res.has_signal then
315 if res.height == 525 then
316 -- Special mode for the USB3 cards.
319 return get_channel_resolution_raw(res) .. ", no signal"
321 return get_channel_resolution_raw(res)
325 -- Helper function to write e.g. “60” or “59.94”.
326 function get_frame_rate(res)
327 local nom = res.frame_rate_nom
328 local den = res.frame_rate_den
329 if nom % den == 0 then
332 return string.format("%.2f", nom / den)
336 -- Helper function to write e.g. “720p60”.
337 function get_channel_resolution_raw(res)
338 if res.interlaced then
339 return res.height .. "i" .. get_frame_rate(res)
341 return res.height .. "p" .. get_frame_rate(res)
345 function get_futatabi_status(str)
348 for word in string.gmatch(str, '([^;]+)') do
349 table.insert(fields, word)
350 num_fields = num_fields + 1
352 if num_fields >= 4 then
360 -- Returns the name for each additional channel (starting from 2).
361 -- Called at the start of the program, and then each frame for live
362 -- channels in case they change resolution.
363 function channel_name(channel)
364 local signal_num = channel - 2
365 if signal_num == INPUT0_SIGNAL_NUM then
366 return "Main (" .. get_channel_resolution(signal_num) .. ")"
367 elseif signal_num == INPUT1_SIGNAL_NUM then
368 return "Secondary (" .. get_channel_resolution(signal_num) .. ")"
369 elseif signal_num == INPUT2_SIGNAL_NUM then
370 return "Goal L (" .. get_channel_resolution(signal_num) .. ")"
371 elseif signal_num == INPUT3_SIGNAL_NUM then
372 return "Goal R (" .. get_channel_resolution(signal_num) .. ")"
373 elseif signal_num == INPUT4_SIGNAL_NUM then
374 return "Commentators (" .. get_channel_resolution(signal_num) .. ")"
375 elseif signal_num == INPUT5_SIGNAL_NUM then
376 return "Hall A2 (" .. get_channel_resolution(signal_num) .. ")"
377 elseif signal_num == INPUT6_SIGNAL_NUM then
378 return "Hall A1 (" .. get_channel_resolution(signal_num) .. ")"
379 elseif signal_num == VIDEO_SIGNAL_NUM then
380 local res = last_resolution[iptv_video:get_signal_num()]
381 if (not res) or res.last_subtitle == nil then
384 return "Futatabi (" .. get_futatabi_status(res.last_subtitle) .. ")"
386 elseif signal_num == SBS_SIGNAL_NUM then
387 return "Side-by-side"
388 elseif signal_num == STATIC_SIGNAL_NUM then
389 return "Static picture"
390 elseif signal_num == OVERLAY_SIGNAL_NUM then
396 -- Returns, given a channel number, which signal it corresponds to (starting from 0).
397 -- Should return -1 if the channel does not correspond to a simple signal.
398 -- (The information is used for whether right-click on the channel should bring up
399 -- an input selector or not.)
400 -- Called once for each channel, at the start of the program.
401 -- Will never be called for live (0) or preview (1).
402 function channel_signal(channel)
403 if channel - 2 == VIDEO_SIGNAL_NUM then
404 return iptv_video:get_signal_num()
405 elseif is_plain_signal(channel - 2) then
413 -- Called every frame. Returns the color (if any) to paint around the given
414 -- channel. Returns a CSS color (typically to mark live and preview signals);
415 -- "transparent" is allowed.
416 -- Will never be called for live (0) or preview (1).
417 function channel_color(channel)
418 if state.transition_type ~= NO_TRANSITION then
419 if channel_involved_in(channel, state.transition_src_signal) or
420 channel_involved_in(channel, state.transition_dst_signal) then
424 if channel_involved_in(channel, state.live_signal_num) then
428 if channel_involved_in(channel, state.preview_signal_num) then
434 function channel_involved_in(channel, signal_num)
435 if is_plain_signal(signal_num) then
436 return channel == (signal_num + 2)
438 if signal_num == SBS_SIGNAL_NUM then
439 return is_sbs_participating_signal(channel - 2)
441 if signal_num == STATIC_SIGNAL_NUM then
442 return (channel == NUM_CAMERAS)
444 if signal_num == VIDEO_SIGNAL_NUM then
445 return (channel == NUM_CAMERAS + 1)
451 -- Returns if a given channel supports setting white balance (starting from 2).
452 -- Called only once for each channel, at the start of the program.
453 function supports_set_wb(channel)
454 return is_plain_signal(channel - 2)
457 function finish_transitions(t)
458 if state.transition_type ~= NO_TRANSITION and t >= state.transition_end then
459 state.live_signal_num = state.transition_dst_signal
460 state.transition_type = NO_TRANSITION
463 -- Disable the overlay if it is no longer visible.
464 if state.overlay_enabled and t > state.overlay_transition_end and state.overlay_alpha_dst == 0.0 then
465 state.overlay_enabled = false
466 print("Turning off overlay")
470 function in_transition(t)
471 return t >= state.transition_start and t <= state.transition_end
474 function is_sbs_participating_signal(signal_num)
475 return signal_num == INPUT0_SIGNAL_NUM or signal_num == INPUT4_SIGNAL_NUM
478 function simple_signal_has_overlay(signal_num)
479 -- The commentator output has no overlay on it.
480 return signal_num ~= INPUT4_SIGNAL_NUM
484 -- Called every frame.
485 function get_transitions(t)
486 if state.preview_signal_num == OVERLAY_SIGNAL_NUM then
487 if t < state.overlay_transition_end then
491 if state.overlay_enabled then
492 return {"Overlay off", "", "Fade ovl out"}
494 return {"Overlay on", "", "Fade ovl in"}
498 if in_transition(t) then
499 -- Transition already in progress, the only thing we can do is really
500 -- cut to the preview. (TODO: Make an “abort” and/or “finish”, too?)
504 if state.live_signal_num == state.preview_signal_num then
505 -- No transitions possible.
509 if (is_plain_signal(state.live_signal_num) and state.preview_signal_num == VIDEO_SIGNAL_NUM) or
510 (is_plain_signal(state.preview_signal_num) and state.live_signal_num == VIDEO_SIGNAL_NUM) then
511 return {"Cut", "Sting", "Fade"}
514 if (is_plain_signal(state.live_signal_num) or state.live_signal_num == STATIC_SIGNAL_NUM) and
515 (is_plain_signal(state.preview_signal_num) or state.preview_signal_num == STATIC_SIGNAL_NUM) then
516 return {"Cut", "", "Fade"}
520 if state.live_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(state.preview_signal_num) then
521 return {"Cut", "Zoom in"}
522 elseif is_sbs_participating_signal(state.live_signal_num) and state.preview_signal_num == SBS_SIGNAL_NUM then
523 return {"Cut", "Zoom out"}
529 function swap_preview_live()
530 local temp = state.live_signal_num
531 state.live_signal_num = state.preview_signal_num
532 state.preview_signal_num = temp
535 function start_transition(type_, t, duration)
536 state.transition_start = t
537 state.transition_end = t + duration
538 state.transition_type = type_
539 state.transition_src_signal = state.live_signal_num
540 state.transition_dst_signal = state.preview_signal_num
541 state.stinger_in_progress = false
546 -- Called when the user clicks a transition button.
547 function transition_clicked(num, t)
548 if state.preview_signal_num == OVERLAY_SIGNAL_NUM then
551 state.overlay_transition_start = -2.0
552 state.overlay_transition_end = -1.0
553 if state.overlay_enabled then
554 state.overlay_enabled = false
555 state.overlay_alpha_src = 0.0
556 state.overlay_alpha_dst = 0.0
558 state.overlay_enabled = true
559 state.overlay_alpha_src = 1.0
560 state.overlay_alpha_dst = 1.0
564 state.overlay_transition_start = t
565 state.overlay_transition_end = t + 1.0
566 if state.overlay_enabled then
567 state.overlay_alpha_src = 1.0
568 state.overlay_alpha_dst = 0.0
570 state.overlay_alpha_src = 0.0
571 state.overlay_alpha_dst = 1.0
573 state.overlay_enabled = true
580 if in_transition(t) then
581 -- Ongoing transition; finish it immediately before the cut.
582 finish_transitions(state.transition_end)
586 state.stinger_in_progress = false
589 finish_transitions(t)
591 if state.live_signal_num == state.preview_signal_num then
596 if (is_plain_signal(state.live_signal_num) and state.preview_signal_num == VIDEO_SIGNAL_NUM) or
597 (is_plain_signal(state.preview_signal_num) and state.live_signal_num == VIDEO_SIGNAL_NUM) then
599 if stinger_in_progress then
603 state.stinger_in_progress = true
604 state.stinger_frame = 0
605 state.stinger_src_signal = state.live_signal_num
606 state.stinger_dst_signal = state.preview_signal_num
610 if is_plain_signal(state.live_signal_num) and is_plain_signal(state.preview_signal_num) then
611 -- We can't zoom between these. Just make a cut.
612 io.write("Cutting from " .. state.live_signal_num .. " to " .. state.live_signal_num .. "\n")
614 state.stinger_in_progress = false
618 if (state.live_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(state.preview_signal_num)) or
619 (state.preview_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(state.live_signal_num)) then
620 start_transition(ZOOM_TRANSITION, t, 1.0)
623 finish_transitions(t)
626 if (state.live_signal_num ~= state.preview_signal_num) and
627 (is_plain_signal(state.live_signal_num) or
628 state.live_signal_num == STATIC_SIGNAL_NUM) and
629 (is_plain_signal(state.preview_signal_num) or
630 state.preview_signal_num == STATIC_SIGNAL_NUM) then
631 start_transition(FADE_TRANSITION, t, 1.0)
633 -- Fades involving SBS are ignored (we have no scene for it).
639 function channel_clicked(num)
640 state.preview_signal_num = num
643 function setup_fade_input(state, input, signals, signal_num, width, height)
644 if signal_num == STATIC_SIGNAL_NUM then
645 input.input:display(static_image)
647 -- We assume this is already correctly scaled at load time.
648 input.resample_effect:disable()
650 input.input:display(signal_num)
652 if (signals:get_width(signal_num) ~= width or signals:get_height(signal_num) ~= height) then
653 input.resample_effect:enable()
654 input.resample_effect:set_int("width", width)
655 input.resample_effect:set_int("height", height)
657 input.resample_effect:disable()
662 function get_fade_scene(state, signals, t, width, height, input_resolution)
663 local scene = fade_scene
664 setup_fade_input(state, scene.input0, signals, state.transition_src_signal, width, height)
665 setup_fade_input(state, scene.input1, signals, state.transition_dst_signal, width, height)
667 local tt = calc_fade_progress(t, state.transition_start, state.transition_end)
668 scene.mix_effect:set_float("strength_first", 1.0 - tt)
669 scene.mix_effect:set_float("strength_second", tt)
671 -- The commentator output has no overlay on it.
672 local extra_alpha_factor = 1.0
673 if not simple_signal_has_overlay(state.transition_src_signal) and
674 not simple_signal_has_overlay(state.transition_dst_signal) then
675 extra_alpha_factor = 0.0
676 elseif not simple_signal_has_overlay(state.transition_src_signal) then
677 extra_alpha_factor = tt
678 elseif not simple_signal_has_overlay(state.transition_dst_signal) then
679 extra_alpha_factor = 1.0 - tt
682 -- If fading between two live inputs, the overlay is put on top.
683 -- If fading between the static picture and a live input,
684 -- the overlay is put on the live input.
685 scene.input0.overlay.overlay_effect:disable()
686 scene.input1.overlay.overlay_effect:disable()
687 scene.overlay.overlay_effect:disable()
688 if state.overlay_enabled then
689 local input0_live = (state.transition_src_signal ~= STATIC_SIGNAL_NUM)
690 local input1_live = (state.transition_dst_signal ~= STATIC_SIGNAL_NUM)
691 if input0_live and not input1_live then
692 scene.input0.overlay.overlay_effect:enable()
693 prepare_overlay_live(state, scene.input0, t, extra_alpha_factor)
694 elseif input1_live and not input0_live then
695 scene.input1.overlay.overlay_effect:enable()
696 prepare_overlay_live(state, scene.input1, t, extra_alpha_factor)
698 scene.overlay.overlay_effect:enable()
699 prepare_overlay_live(state, scene, t, extra_alpha_factor)
705 function fetch_input_resolution(signals, signal_num)
707 width = signals:get_width(signal_num),
708 height = signals:get_height(signal_num),
709 interlaced = signals:get_interlaced(signal_num),
710 has_signal = signals:get_has_signal(signal_num),
711 is_connected = signals:get_is_connected(signal_num),
712 frame_rate_nom = signals:get_frame_rate_nom(signal_num),
713 frame_rate_den = signals:get_frame_rate_den(signal_num),
714 last_subtitle = signals:get_last_subtitle(signal_num)
717 if res.interlaced then
718 -- Convert height from frame height to field height.
719 -- (Needed for e.g. place_rectangle.)
720 res.height = res.height * 2
722 -- Show field rate instead of frame rate; really for cosmetics only
723 -- (and actually contrary to EBU recommendations, although in line
724 -- with typical user expectations).
725 res.frame_rate_nom = res.frame_rate_nom * 2
730 local last_alsa_sound_shark_volume = -1 -- Always update on the first frame.
732 function update_volume()
733 if Nageru.get_num_audio_buses() < 3 or
734 Nageru.get_audio_bus_name(2) ~= 'Sound Shark' then
735 -- We haven't loaded the right audio mapping.
740 Nageru.set_audio_bus_fader_level_db(2, -math.huge)
744 local volume_db = Nageru.get_audio_bus_fader_level_db(2)
745 if Nageru.get_audio_bus_mute(2) or volume_db < -100.0 then
748 cef_input:execute_javascript_async("set_sound_shark_volume_db(" .. volume_db .. ")")
750 local desired_alsa_sound_shark_volume = (volume_db < -40.0) and 0 or 154 -- 154 corresponds to 70% in alsamixer for this input.
751 if desired_alsa_sound_shark_volume ~= last_alsa_sound_shark_volume then
752 os.execute("amixer -D hw:CARD=USB sset 'Mix A Input 04' " .. desired_alsa_sound_shark_volume .. " &")
753 os.execute("amixer -D hw:CARD=USB sset 'Mix B Input 04' " .. desired_alsa_sound_shark_volume .. " &")
754 last_alsa_sound_shark_volume = desired_alsa_sound_shark_volume
758 local last_rate = 0.0
761 -- Called every frame. Get the scene for displaying at input <num>,
762 -- where 0 is live, 1 is preview, 2 is the first channel to display
763 -- in the bottom bar, and so on up to num_channels()+1. t is the
764 -- current time in seconds. width and height are the dimensions of
765 -- the output, although you can ignore them if you don't need them
766 -- (they're useful if you want to e.g. know what to resample by).
768 -- <signals> is basically an exposed InputState, which you can use to
769 -- query for information about the signals at the point of the current
770 -- frame. In particular, you can call get_width() and get_height()
771 -- for any signal number, and use that to e.g. assist in scene selection.
773 -- You should return scene to use, after having set any parameters you
774 -- want to set (through set_int() etc.). The parameters will be snapshot
775 -- at return time and used during rendering.
776 function get_scene(num, t, width, height, signals)
781 local input_resolution = {}
782 for signal_num=0,(NUM_CAMERAS-1) do
783 input_resolution[signal_num] = fetch_input_resolution(signals, signal_num)
785 input_resolution[iptv_video:get_signal_num()] = fetch_input_resolution(signals, iptv_video:get_signal_num())
786 last_resolution = input_resolution
788 -- Save some CPU time if we're not having SBS on live.
790 if state.live_signal_num == SBS_SIGNAL_NUM or
791 state.preview_signal_num == SBS_SIGNAL_NUM or
792 state.transition_type == ZOOM_TRANSITION then
797 if new_rate ~= last_rate then
798 -- Avoid waking up the video thread (which may be sleeping) if the rate is the same.
799 bg_video:change_rate(new_rate)
803 if num == 0 and multiviewer_enabled then -- Live, with debugging on.
804 return multiview_scene_hq
807 if num == 0 then -- Live.
808 -- See if we're in a transition.
809 finish_transitions(t)
810 if state.transition_type == ZOOM_TRANSITION then
811 -- Transition in or out of SBS.
812 sbs_scene.input0.overlay.overlay_effect:enable_if(state.overlay_enabled)
813 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)
814 prepare_overlay_live(state, sbs_scene, t, 1.0)
815 return sbs_scene.scene
816 elseif state.transition_type == NO_TRANSITION and state.live_signal_num == SBS_SIGNAL_NUM then
818 sbs_scene.input0.overlay.overlay_effect:enable_if(state.overlay_enabled)
819 prepare_sbs_scene(state, sbs_scene, 0.0, NO_TRANSITION, 0, SBS_SIGNAL_NUM, width, height, input_resolution, true)
820 prepare_overlay_live(state, sbs_scene, t, 1.0)
821 return sbs_scene.scene
822 elseif state.transition_type == FADE_TRANSITION then
823 return get_fade_scene(state, signals, t, width, height, input_resolution)
824 elseif is_plain_signal(state.live_signal_num) then
825 local overlay_really_enabled = state.overlay_enabled and simple_signal_has_overlay(state.live_signal_num)
826 if state.stinger_in_progress then
827 stinger_scene.overlay.overlay_effect:enable_if(overlay_really_enabled)
828 if state.live_signal_num == VIDEO_SIGNAL_NUM then
829 stinger_scene.input:display(iptv_video)
831 stinger_scene.input:display(state.live_signal_num)
833 set_scale_parameters_if_needed(stinger_scene, signals, state.live_signal_num, width, height, true)
835 stinger_scene.stinger_input:display(stinger_images[state.stinger_frame])
836 state.stinger_frame = state.stinger_frame + 1
837 if state.stinger_frame >= 25 then
838 state.stinger_in_progress = false
839 state.preview_signal_num = state.stinger_src_signal
840 state.live_signal_num = state.stinger_dst_signal
842 if state.stinger_dst_signal == VIDEO_SIGNAL_NUM then
843 -- Turn off the overlay when playing video.
844 state.stinger_save_overlay = state.overlay_enabled
845 state.overlay_enabled = false
847 -- Restore the state.
848 state.overlay_enabled = state.stinger_save_overlay
852 if overlay_really_enabled then
853 prepare_overlay_live(state, stinger_scene, t, 1.0)
855 return stinger_scene.scene
857 simple_scene.overlay.overlay_effect:enable_if(overlay_really_enabled)
858 if state.live_signal_num == VIDEO_SIGNAL_NUM then
859 simple_scene.input:display(iptv_video)
861 simple_scene.input:display(state.live_signal_num)
863 set_scale_parameters_if_needed(simple_scene, signals, state.live_signal_num, width, height, true)
864 prepare_overlay_live(state, simple_scene, t, 1.0)
865 return simple_scene.scene
867 elseif state.live_signal_num == STATIC_SIGNAL_NUM then -- Static picture.
874 -- We do not show overlays on the individual preview inputs.
875 -- The M/E preview matches what we'd put live by doing a transition, as always.
876 local show_overlay = false
877 if num == 1 then -- Preview.
878 if state.preview_signal_num == OVERLAY_SIGNAL_NUM then
879 num = state.live_signal_num + 2
880 show_overlay = not state.overlay_enabled
882 if state.transition_type ~= NO_TRANSITION then
883 num = state.transition_dst_signal + 2
886 num = state.preview_signal_num + 2
887 show_overlay = state.overlay_enabled and simple_signal_has_overlay(state.preview_signal_num)
891 -- Individual preview inputs (usually without overlay).
892 if is_plain_signal(num - 2) then
893 local signal_num = num - 2
894 simple_scene.overlay.overlay_effect:enable_if(show_overlay)
895 if signal_num == VIDEO_SIGNAL_NUM then
896 simple_scene.input:display(iptv_video)
898 simple_scene.input:display(signal_num)
900 set_scale_parameters_if_needed(simple_scene, signals, signal_num, width, height, false)
901 prepare_overlay_static(simple_scene, t)
902 return simple_scene.scene
904 if num == SBS_SIGNAL_NUM + 2 then
905 sbs_scene.input0.overlay.overlay_effect:enable_if(show_overlay)
906 prepare_sbs_scene(state, sbs_scene, 0.0, NO_TRANSITION, 0, SBS_SIGNAL_NUM, width, height, input_resolution, false)
907 return sbs_scene.scene
909 if num == STATIC_SIGNAL_NUM + 2 then
912 if num == OVERLAY_SIGNAL_NUM + 2 then
913 return overlay_scene_lq
917 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
919 return math.floor(x + 0.5)
922 function lerp(a, b, t)
923 return a + (b - a) * t
926 function lerp_pos(a, b, t)
928 x0 = lerp(a.x0, b.x0, t),
929 y0 = lerp(a.y0, b.y0, t),
930 x1 = lerp(a.x1, b.x1, t),
931 y1 = lerp(a.y1, b.y1, t)
935 function pos_from_top_left(x, y, width, height, screen_width, screen_height)
936 local xs = screen_width / 1280.0
937 local ys = screen_height / 720.0
941 x1 = round(xs * (x + width)),
942 y1 = round(ys * (y + height))
946 function prepare_sbs_scene(state, scene, t, transition_type, src_signal, dst_signal, screen_width, screen_height, input_resolution, hq)
947 scene.input0.input:display(0)
948 scene.input1.input:display(5)
950 -- Both inputs are the same size (true side-by-side).
951 local pos0 = pos_from_top_left(1280 - 616 - 16, 186, 616, 347, screen_width, screen_height)
952 local pos1 = pos_from_top_left(16, 186, 616, 347, screen_width, screen_height)
954 local pos_fs = { x0 = 0, y0 = 0, x1 = screen_width, y1 = screen_height }
956 if transition_type == NO_TRANSITION then
958 affine_param = { sx = 1.0, sy = 1.0, tx = 0.0, ty = 0.0 } -- Identity.
960 -- Zooming to/from SBS view into or out of a single view.
961 assert(transition_type == ZOOM_TRANSITION)
963 if src_signal == SBS_SIGNAL_NUM then
967 assert(dst_signal == SBS_SIGNAL_NUM)
972 if signal == INPUT0_SIGNAL_NUM then
973 affine_param = find_affine_param(pos0, lerp_pos(pos0, pos_fs, real_t))
974 elseif signal == INPUT4_SIGNAL_NUM then
975 affine_param = find_affine_param(pos1, lerp_pos(pos1, pos_fs, real_t))
979 -- NOTE: input_resolution is not 1-indexed, unlike usual Lua arrays.
980 place_rectangle_with_affine(scene.input0, pos0, affine_param, screen_width, screen_height, input_resolution[0].width, input_resolution[0].height, hq)
981 place_rectangle_with_affine(scene.input1, pos1, affine_param, screen_width, screen_height, input_resolution[1].width, input_resolution[1].height, hq)
984 -- Find the transformation that changes the first rectangle to the second one.
985 function find_affine_param(a, b)
986 local sx = (b.x1 - b.x0) / (a.x1 - a.x0)
987 local sy = (b.y1 - b.y0) / (a.y1 - a.y0)
991 tx = b.x0 - a.x0 * sx,
992 ty = b.y0 - a.y0 * sy
996 function place_rectangle_with_affine(input, pos, aff, screen_width, screen_height, input_width, input_height, hq)
997 local x0 = pos.x0 * aff.sx + aff.tx
998 local x1 = pos.x1 * aff.sx + aff.tx
999 local y0 = pos.y0 * aff.sy + aff.ty
1000 local y1 = pos.y1 * aff.sy + aff.ty
1002 place_rectangle(input, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height, hq)
1005 function place_rectangle(input, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height, hq)
1006 input.padding_effect:set_int("width", screen_width)
1007 input.padding_effect:set_int("height", screen_height)
1010 if x0 > screen_width or x1 < 0.0 or y0 > screen_height or y1 < 0.0 then
1011 input.resample_effect:choose(ResizeEffect) -- Low-quality resizing.
1012 input.resample_effect:set_int("width", 1)
1013 input.resample_effect:set_int("height", 1)
1014 input.padding_effect:set_int("left", screen_width + 100)
1015 input.padding_effect:set_int("top", screen_height + 100)
1026 srcx0 = -x0 / (x1 - x0)
1030 srcy0 = -y0 / (y1 - y0)
1033 if x1 > screen_width then
1034 srcx1 = (screen_width - x0) / (x1 - x0)
1037 if y1 > screen_height then
1038 srcy1 = (screen_height - y0) / (y1 - y0)
1043 -- High-quality resampling.
1044 local resample_effect = input.resample_effect:choose(ResampleEffect)
1046 local x_subpixel_offset = x0 - math.floor(x0)
1047 local y_subpixel_offset = y0 - math.floor(y0)
1049 -- Resampling must be to an integral number of pixels. Round up,
1050 -- and then add an extra pixel so we have some leeway for the border.
1051 local width = math.ceil(x1 - x0) + 1
1052 local height = math.ceil(y1 - y0) + 1
1053 input.resample_effect:set_int("width", width)
1054 input.resample_effect:set_int("height", height)
1056 -- Correct the discrepancy with zoom. (This will leave a small
1057 -- excess edge of pixels and subpixels, which we'll correct for soon.)
1058 local zoom_x = (x1 - x0) / (width * (srcx1 - srcx0))
1059 local zoom_y = (y1 - y0) / (height * (srcy1 - srcy0))
1060 resample_effect:set_float("zoom_x", zoom_x) -- Use the actual effect specialization; specific to ResampleEffect.
1061 resample_effect:set_float("zoom_y", zoom_y)
1062 resample_effect:set_float("zoom_center_x", 0.0)
1063 resample_effect:set_float("zoom_center_y", 0.0)
1065 -- Padding must also be to a whole-pixel offset.
1066 input.padding_effect:set_int("left", math.floor(x0))
1067 input.padding_effect:set_int("top", math.floor(y0))
1069 -- Correct _that_ discrepancy by subpixel offset in the resampling.
1070 resample_effect:set_float("left", srcx0 * input_width - x_subpixel_offset / zoom_x)
1071 resample_effect:set_float("top", srcy0 * input_height - y_subpixel_offset / zoom_y)
1073 -- Finally, adjust the border so it is exactly where we want it.
1074 input.padding_effect:set_float("border_offset_left", x_subpixel_offset)
1075 input.padding_effect:set_float("border_offset_right", x1 - (math.floor(x0) + width))
1076 input.padding_effect:set_float("border_offset_top", y_subpixel_offset)
1077 input.padding_effect:set_float("border_offset_bottom", y1 - (math.floor(y0) + height))
1079 -- Lower-quality simple resizing.
1080 input.resample_effect:choose(ResizeEffect)
1082 local width = round(x1 - x0)
1083 local height = round(y1 - y0)
1084 input.resample_effect:set_int("width", width)
1085 input.resample_effect:set_int("height", height)
1087 -- Padding must also be to a whole-pixel offset.
1088 input.padding_effect:set_int("left", math.floor(x0))
1089 input.padding_effect:set_int("top", math.floor(y0))
1091 -- No subpixel stuff.
1092 input.padding_effect:set_float("border_offset_left", 0.0)
1093 input.padding_effect:set_float("border_offset_right", 0.0)
1094 input.padding_effect:set_float("border_offset_top", 0.0)
1095 input.padding_effect:set_float("border_offset_bottom", 0.0)
1099 function prepare_overlay_live(state, scene, t, extra_alpha_factor)
1100 if scene.overlay then
1101 local tt = calc_fade_progress(t, state.overlay_transition_start, state.overlay_transition_end)
1102 local overlay_alpha = state.overlay_alpha_src + tt * (state.overlay_alpha_dst - state.overlay_alpha_src)
1103 overlay_alpha = overlay_alpha * extra_alpha_factor
1104 --print("overlay_alpha=" .. overlay_alpha .. " [" .. state.overlay_alpha_src .. "," .. state.overlay_alpha_dst .. "]@" .. tt)
1105 if t > state.overlay_transition_end and state.overlay_alpha_dst == 0.0 then
1106 state.overlay_enabled = false -- Takes effect next frame.
1107 -- print("Turning off overlay")
1109 scene.overlay.multiply_effect:set_vec4("factor", overlay_alpha, overlay_alpha, overlay_alpha, overlay_alpha)
1113 function prepare_overlay_static(scene)
1114 if scene.overlay then
1115 scene.overlay.multiply_effect:set_vec4("factor", 1.0, 1.0, 1.0, 1.0)
1119 function calc_zoom_progress(t)
1120 if t < state.transition_start then
1122 elseif t > state.transition_end then
1125 local tt = (t - state.transition_start) / (state.transition_end - state.transition_start)
1127 return math.sin(tt * 3.14159265358 * 0.5)
1131 function calc_fade_progress(t, transition_start, transition_end)
1132 local tt = (t - transition_start) / (transition_end - transition_start)
1135 elseif tt > 1.0 then
1139 -- Make the fade look maybe a tad more natural, by pumping it
1140 -- through a sigmoid function.
1141 tt = 10.0 * tt - 5.0
1142 tt = 1.0 / (1.0 + math.exp(-tt))
1148 { "Reload overlay", reload_cef },
1149 { "Disconnect IPTV", disconnect_iptv },
1150 { "Toggle multiviewer", toggle_multiviewer }