-- Nageru theme for TFK mini-tournament 2017, based on the default theme. local futatabi_server = "http://10.42.0.1:9096" local state = { transition_start = -2.0, transition_end = -1.0, transition_type = 0, transition_src_signal = 0, transition_dst_signal = 0, overlay_transition_start = -2.0, overlay_transition_end = -1.0, overlay_alpha_src = 0.0, overlay_alpha_dst = 1.0, overlay_enabled = false, stinger_in_progress = false, stinger_frame = 0, stinger_src_signal = 0, stinger_dst_signal = 0, stinger_save_overlay = false, live_signal_num = 0, preview_signal_num = 1 } local NUM_CAMERAS = 7 -- Valid values for live_signal_num and preview_signal_num. local INPUT0_SIGNAL_NUM = 0 local INPUT1_SIGNAL_NUM = 1 local INPUT2_SIGNAL_NUM = 2 local INPUT3_SIGNAL_NUM = 3 local INPUT4_SIGNAL_NUM = 4 local INPUT5_SIGNAL_NUM = 5 local INPUT6_SIGNAL_NUM = 6 local COMMENTATOR_SIGNAL_NUM = 4 local VIDEO_SIGNAL_NUM = NUM_CAMERAS local SBS_SIGNAL_NUM = NUM_CAMERAS + 1 local STATIC_SIGNAL_NUM = NUM_CAMERAS + 2 -- Preview-only signal showing the current signal with the overlay. -- Not valid for live_signal_num! local OVERLAY_SIGNAL_NUM = NUM_CAMERAS + 3 -- Valid values for transition_type. (Cuts are done directly, so they need no entry.) local NO_TRANSITION = 0 local ZOOM_TRANSITION = 1 local FADE_TRANSITION = 2 -- Last width/height/frame rate for each channel, if we have it. -- Note that unlike the values we get from Nageru, the resolution is per -- frame and not per field, since we deinterlace. local last_resolution = {} local cef_path = Nageru.THEME_PATH:match("(.*)/nageru/") local cef_input = HTMLInput.new("file://" .. cef_path .. "/score.html") cef_input:execute_javascript_async("play()") local bg_video = VideoInput.new(cef_path .. "/flow-720.mp4", Nageru.VIDEO_FORMAT_YCBCR) -- local iptv_video = VideoInput.new("http://10.34.129.69:9060/1/v.flv", Nageru.VIDEO_FORMAT_YCBCR) local iptv_video = VideoInput.new(futatabi_server, Nageru.VIDEO_FORMAT_YCBCR) iptv_video:change_rate(10.0) local static_image = ImageInput.new(cef_path .. "/nageru/tfk_pause.png") local first_frame = true local multiviewer_enabled = false function reload_cef() cef_input:reload() cef_input:execute_javascript_async("play()") end function disconnect_iptv() iptv_video:disconnect() end function toggle_multiviewer() multiviewer_enabled = not multiviewer_enabled end -- An overlay with variable alpha. function make_overlay(scene, base) local image = scene:add_input(cef_input) local multiply_effect = scene:add_effect(MultiplyEffect.new()) local overlay_effect = scene:add_optional_effect(OverlayEffect.new(), base, multiply_effect) return { image = image, multiply_effect = multiply_effect, overlay_effect = overlay_effect } end function make_fade_input(scene) local ret = { input = scene:add_input(), resample_effect = scene:add_optional_effect(ResampleEffect.new()), -- Activated if scaling. wb_effect = scene:add_white_balance() } ret.overlay = make_overlay(scene, ret.wb_effect) return ret end -- A scene to fade between two inputs, of which either can be a picture -- or a live input. function make_fade_scene() local scene = Scene.new(16, 9) -- If fading between two live inputs, the overlay is put on top. -- If fading between the static picture and a live input, -- the overlay is put on the live input. local input0 = make_fade_input(scene) local input1 = make_fade_input(scene) local mix_effect = scene:add_effect(MixEffect.new(), input0.overlay.overlay_effect, input1.overlay.overlay_effect) local overlay = make_overlay(scene, mix_effect) -- At most one overlay can be active at any given time. input0.overlay.overlay_effect:promise_to_disable_if_enabled(input1.overlay.overlay_effect) input0.overlay.overlay_effect:promise_to_disable_if_enabled(overlay.overlay_effect) input1.overlay.overlay_effect:promise_to_disable_if_enabled(input0.overlay.overlay_effect) input1.overlay.overlay_effect:promise_to_disable_if_enabled(overlay.overlay_effect) overlay.overlay_effect:promise_to_disable_if_enabled(input0.overlay.overlay_effect) overlay.overlay_effect:promise_to_disable_if_enabled(input1.overlay.overlay_effect) scene:finalize(true) -- Only used live, don't bother creating LQ versions. return { scene = scene, input0 = input0, input1 = input1, mix_effect = mix_effect, overlay = overlay } end fade_scene = make_fade_scene() function make_sbs_input(scene, has_overlay) local input = scene:add_input() local wb_effect = scene:add_white_balance() local overlay = has_overlay and make_overlay(scene, wb_effect) or nil local resample_effect = scene:add_effect({ResampleEffect.new(), ResizeEffect.new()}) local padding_effect = scene:add_effect(IntegralPaddingEffect.new()) return { input = input, overlay = overlay, resample_effect = resample_effect, wb_effect = wb_effect, padding_effect = padding_effect } end -- Side-by-side scene. function make_sbs_scene() local scene = Scene.new(16, 9) local bg = scene:add_input(bg_video) local input0 = make_sbs_input(scene, true) local input1 = make_sbs_input(scene, false) input0.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0) input1.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0) input0.input:display(0) input1.input:display(1) local i0 = scene:add_effect(OverlayEffect.new(), bg, input0.padding_effect) scene:add_effect(OverlayEffect.new(), i0, input1.padding_effect) scene:finalize() return { scene = scene, input0 = input0, input1 = input1, overlay = input0.overlay -- May be nil. } end local sbs_scene = make_sbs_scene() function make_simple_scene_no_finalize() local scene = Scene.new(16, 9) local input = scene:add_input() local resample_effect = scene:add_effect({ResampleEffect.new(), ResizeEffect.new(), IdentityEffect.new()}) local wb_effect = scene:add_white_balance() local overlay = make_overlay(scene, wb_effect) return { scene = scene, input = input, wb_effect = wb_effect, resample_effect = resample_effect, overlay = overlay } end -- A scene to show a single input on screen. function make_simple_scene() local scene = make_simple_scene_no_finalize() scene.scene:finalize() return scene end local simple_scene = make_simple_scene() -- Load all the stinger frames. local stinger_images = {} for idx=0,24 do local filename = cef_path .. "/stinger/blur" .. string.rep("0", 3 - string.len(tostring(idx))) .. idx .. ".png" stinger_images[idx] = ImageInput.new(filename) end -- A scene to show a single input on screen, with a stinger on top. function make_stinger_scene() local scene = make_simple_scene_no_finalize() scene.stinger_input = scene.scene:add_input() scene.scene:add_effect(OverlayEffect.new(), scene.overlay.overlay_effect, scene.stinger_input) scene.scene:finalize() return scene end local stinger_scene = make_stinger_scene() -- A scene to show a single static picture on screen. Never with HTML overlay. local static_scene = Scene.new(16, 9) static_scene:add_input(static_image) static_scene:finalize() -- A scene to show the overlay and nothing more. LQ only, -- since it is not a valid live signal. local overlay_scene_lq = Scene.new(16, 9) local overlay_scene_lq_input = overlay_scene_lq:add_input(cef_input) overlay_scene_lq:finalize(false) -- A debugging scene to show a simple multiviewer. HQ only, since there is no preview. function make_multiview_scene() local scene = Scene.new(16, 9) local bg = scene:add_input(ImageInput.new(cef_path .. "/nageru/white.png")) local mv_width = 1280 / 3 local mv_height = 720 / 3 local mv_offset = (720 - mv_height * 2) / 2 for input_idx=0,5 do local input = scene:add_input(input_idx) local resize = scene:add_effect(ResizeEffect.new()) resize:set_int("width", mv_width) resize:set_int("height", mv_height) local padding = scene:add_effect(IntegralPaddingEffect.new()) padding:set_int("width", 1280) padding:set_int("height", 720) padding:set_int("left", (input_idx % 3) * mv_width) padding:set_int("top", math.floor(input_idx / 3) * mv_height + mv_offset) local overlay = scene:add_effect(OverlayEffect.new(), bg, padding) bg = overlay end scene:finalize(true) return scene end local multiview_scene_hq = make_multiview_scene() function needs_scale(signals, signal_num, width, height) if signal_num == STATIC_SIGNAL_NUM then -- We assume this is already correctly scaled at load time. return false end assert(is_plain_signal(signal_num)) return (signals:get_width(signal_num) ~= width or signals:get_height(signal_num) ~= height) end function set_scale_parameters_if_needed(scene, signals, signal_num, width, height, hq) -- Futatabi rescaling will be linear, but original frames come back unscaled; -- make them linear, too, so that we don't get more sharpness on every other -- frame. This won't be right for fading, but it's probably hard to notice. if signal_num == VIDEO_SIGNAL_NUM then hq = false end if needs_scale(signals, signal_num, width, height) then if hq then scene.resample_effect:choose(ResampleEffect) -- High-quality resampling. else scene.resample_effect:choose(ResizeEffect) -- Low-quality resampling. end scene.resample_effect:set_int("width", width) scene.resample_effect:set_int("height", height) else scene.resample_effect:disable() end end -- API ENTRY POINT -- Returns the number of outputs in addition to the live (0) and preview (1). -- Called only once, at the start of the program. function num_channels() return NUM_CAMERAS + 4 -- sbs, static picture, video and overlay end function is_plain_signal(num) return (num >= INPUT0_SIGNAL_NUM and num <= INPUT6_SIGNAL_NUM) or (num == VIDEO_SIGNAL_NUM) end -- Helper function to write e.g. “720p60”. The difference between this -- and get_channel_resolution_raw() is that this one also can say that -- there's no signal. function get_channel_resolution(signal_num) local res = last_resolution[signal_num] if (not res) or not res.is_connected then return "disconnected" end if res.height <= 0 then return "no signal" end if not res.has_signal then if res.height == 525 then -- Special mode for the USB3 cards. return "no signal" end return get_channel_resolution_raw(res) .. ", no signal" else return get_channel_resolution_raw(res) end end -- Helper function to write e.g. “60” or “59.94”. function get_frame_rate(res) local nom = res.frame_rate_nom local den = res.frame_rate_den if nom % den == 0 then return nom / den else return string.format("%.2f", nom / den) end end -- Helper function to write e.g. “720p60”. function get_channel_resolution_raw(res) if res.interlaced then return res.height .. "i" .. get_frame_rate(res) else return res.height .. "p" .. get_frame_rate(res) end end function get_futatabi_status(str) local num_fields = 0 local fields = {} for word in string.gmatch(str, '([^;]+)') do table.insert(fields, word) num_fields = num_fields + 1 end if num_fields >= 4 then return fields[4] else return "???" end end -- API ENTRY POINT -- Returns the name for each additional channel (starting from 2). -- Called at the start of the program, and then each frame for live -- channels in case they change resolution. function channel_name(channel) local signal_num = channel - 2 if signal_num == INPUT0_SIGNAL_NUM then return "Main (" .. get_channel_resolution(signal_num) .. ")" elseif signal_num == INPUT1_SIGNAL_NUM then return "Secondary (" .. get_channel_resolution(signal_num) .. ")" elseif signal_num == INPUT2_SIGNAL_NUM then return "Goal L (" .. get_channel_resolution(signal_num) .. ")" elseif signal_num == INPUT3_SIGNAL_NUM then return "Goal R (" .. get_channel_resolution(signal_num) .. ")" elseif signal_num == INPUT4_SIGNAL_NUM then return "Commentators (" .. get_channel_resolution(signal_num) .. ")" elseif signal_num == INPUT5_SIGNAL_NUM then return "Hall A2 (" .. get_channel_resolution(signal_num) .. ")" elseif signal_num == INPUT6_SIGNAL_NUM then return "Hall A1 (" .. get_channel_resolution(signal_num) .. ")" elseif signal_num == VIDEO_SIGNAL_NUM then local res = last_resolution[iptv_video:get_signal_num()] if (not res) or res.last_subtitle == nil then return "Futatabi" else return "Futatabi (" .. get_futatabi_status(res.last_subtitle) .. ")" end elseif signal_num == SBS_SIGNAL_NUM then return "Side-by-side" elseif signal_num == STATIC_SIGNAL_NUM then return "Static picture" elseif signal_num == OVERLAY_SIGNAL_NUM then return "Overlay" end end -- API ENTRY POINT -- Returns, given a channel number, which signal it corresponds to (starting from 0). -- Should return -1 if the channel does not correspond to a simple signal. -- (The information is used for whether right-click on the channel should bring up -- an input selector or not.) -- Called once for each channel, at the start of the program. -- Will never be called for live (0) or preview (1). function channel_signal(channel) if channel - 2 == VIDEO_SIGNAL_NUM then return iptv_video:get_signal_num() elseif is_plain_signal(channel - 2) then return channel - 2 else return -1 end end -- API ENTRY POINT -- Called every frame. Returns the color (if any) to paint around the given -- channel. Returns a CSS color (typically to mark live and preview signals); -- "transparent" is allowed. -- Will never be called for live (0) or preview (1). function channel_color(channel) if state.transition_type ~= NO_TRANSITION then if channel_involved_in(channel, state.transition_src_signal) or channel_involved_in(channel, state.transition_dst_signal) then return "#f00" end else if channel_involved_in(channel, state.live_signal_num) then return "#f00" end end if channel_involved_in(channel, state.preview_signal_num) then return "#0f0" end return "transparent" end function channel_involved_in(channel, signal_num) if is_plain_signal(signal_num) then return channel == (signal_num + 2) end if signal_num == SBS_SIGNAL_NUM then return is_sbs_participating_signal(channel - 2) end if signal_num == STATIC_SIGNAL_NUM then return (channel == NUM_CAMERAS) end if signal_num == VIDEO_SIGNAL_NUM then return (channel == NUM_CAMERAS + 1) end return false end -- API ENTRY POINT -- Returns if a given channel supports setting white balance (starting from 2). -- Called only once for each channel, at the start of the program. function supports_set_wb(channel) return is_plain_signal(channel - 2) end function finish_transitions(t) if state.transition_type ~= NO_TRANSITION and t >= state.transition_end then state.live_signal_num = state.transition_dst_signal state.transition_type = NO_TRANSITION end -- Disable the overlay if it is no longer visible. if state.overlay_enabled and t > state.overlay_transition_end and state.overlay_alpha_dst == 0.0 then state.overlay_enabled = false print("Turning off overlay") end end function in_transition(t) return t >= state.transition_start and t <= state.transition_end end function is_sbs_participating_signal(signal_num) return signal_num == INPUT0_SIGNAL_NUM or signal_num == COMMENTATOR_SIGNAL_NUM end function simple_signal_has_overlay(signal_num) -- The commentator output has no overlay on it. return signal_num ~= COMMENTATOR_SIGNAL_NUM end -- API ENTRY POINT -- Called every frame. function get_transitions(t) if state.preview_signal_num == OVERLAY_SIGNAL_NUM then if t < state.overlay_transition_end then -- Fade in progress. return {} end if state.overlay_enabled then return {"Overlay off", "", "Fade ovl out"} else return {"Overlay on", "", "Fade ovl in"} end end if in_transition(t) then -- Transition already in progress, the only thing we can do is really -- cut to the preview. (TODO: Make an “abort” and/or “finish”, too?) return {"Cut"} end if state.live_signal_num == state.preview_signal_num then -- No transitions possible. return {} end if (is_plain_signal(state.live_signal_num) and state.preview_signal_num == VIDEO_SIGNAL_NUM) or (is_plain_signal(state.preview_signal_num) and state.live_signal_num == VIDEO_SIGNAL_NUM) then return {"Cut", "Sting", "Fade"} end if (is_plain_signal(state.live_signal_num) or state.live_signal_num == STATIC_SIGNAL_NUM) and (is_plain_signal(state.preview_signal_num) or state.preview_signal_num == STATIC_SIGNAL_NUM) then return {"Cut", "", "Fade"} end -- Various zooms. if state.live_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(state.preview_signal_num) then return {"Cut", "Zoom in"} elseif is_sbs_participating_signal(state.live_signal_num) and state.preview_signal_num == SBS_SIGNAL_NUM then return {"Cut", "Zoom out"} end return {"Cut"} end function swap_preview_live() local temp = state.live_signal_num state.live_signal_num = state.preview_signal_num state.preview_signal_num = temp end function start_transition(type_, t, duration) state.transition_start = t state.transition_end = t + duration state.transition_type = type_ state.transition_src_signal = state.live_signal_num state.transition_dst_signal = state.preview_signal_num state.stinger_in_progress = false swap_preview_live() end -- API ENTRY POINT -- Called when the user clicks a transition button. function transition_clicked(num, t) if state.preview_signal_num == OVERLAY_SIGNAL_NUM then if num == 0 then -- Cut. state.overlay_transition_start = -2.0 state.overlay_transition_end = -1.0 if state.overlay_enabled then state.overlay_enabled = false state.overlay_alpha_src = 0.0 state.overlay_alpha_dst = 0.0 else state.overlay_enabled = true state.overlay_alpha_src = 1.0 state.overlay_alpha_dst = 1.0 end elseif num == 2 then -- Fade. state.overlay_transition_start = t state.overlay_transition_end = t + 1.0 if state.overlay_enabled then state.overlay_alpha_src = 1.0 state.overlay_alpha_dst = 0.0 else state.overlay_alpha_src = 0.0 state.overlay_alpha_dst = 1.0 end state.overlay_enabled = true end return end if num == 0 then -- Cut. if in_transition(t) then -- Ongoing transition; finish it immediately before the cut. finish_transitions(state.transition_end) end swap_preview_live() state.stinger_in_progress = false elseif num == 1 then -- Zoom or sting. finish_transitions(t) if state.live_signal_num == state.preview_signal_num then -- Nothing to do. return end if (is_plain_signal(state.live_signal_num) and state.preview_signal_num == VIDEO_SIGNAL_NUM) or (is_plain_signal(state.preview_signal_num) and state.live_signal_num == VIDEO_SIGNAL_NUM) then -- Sting. if stinger_in_progress then return end state.stinger_in_progress = true state.stinger_frame = 0 state.stinger_src_signal = state.live_signal_num state.stinger_dst_signal = state.preview_signal_num return end if is_plain_signal(state.live_signal_num) and is_plain_signal(state.preview_signal_num) then -- We can't zoom between these. Just make a cut. io.write("Cutting from " .. state.live_signal_num .. " to " .. state.live_signal_num .. "\n") swap_preview_live() state.stinger_in_progress = false return end if (state.live_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(state.preview_signal_num)) or (state.preview_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(state.live_signal_num)) then start_transition(ZOOM_TRANSITION, t, 1.0) end elseif num == 2 then finish_transitions(t) -- Fade. if (state.live_signal_num ~= state.preview_signal_num) and (is_plain_signal(state.live_signal_num) or state.live_signal_num == STATIC_SIGNAL_NUM) and (is_plain_signal(state.preview_signal_num) or state.preview_signal_num == STATIC_SIGNAL_NUM) then start_transition(FADE_TRANSITION, t, 1.0) else -- Fades involving SBS are ignored (we have no scene for it). end end end -- API ENTRY POINT function channel_clicked(num) state.preview_signal_num = num end function setup_fade_input(state, input, signals, signal_num, width, height) if signal_num == STATIC_SIGNAL_NUM then input.input:display(static_image) -- We assume this is already correctly scaled at load time. input.resample_effect:disable() else input.input:display(signal_num) if (signals:get_width(signal_num) ~= width or signals:get_height(signal_num) ~= height) then input.resample_effect:enable() input.resample_effect:set_int("width", width) input.resample_effect:set_int("height", height) else input.resample_effect:disable() end end end function get_fade_scene(state, signals, t, width, height, input_resolution) local scene = fade_scene setup_fade_input(state, scene.input0, signals, state.transition_src_signal, width, height) setup_fade_input(state, scene.input1, signals, state.transition_dst_signal, width, height) local tt = calc_fade_progress(t, state.transition_start, state.transition_end) scene.mix_effect:set_float("strength_first", 1.0 - tt) scene.mix_effect:set_float("strength_second", tt) -- The commentator output has no overlay on it. local extra_alpha_factor = 1.0 if not simple_signal_has_overlay(state.transition_src_signal) and not simple_signal_has_overlay(state.transition_dst_signal) then extra_alpha_factor = 0.0 elseif not simple_signal_has_overlay(state.transition_src_signal) then extra_alpha_factor = tt elseif not simple_signal_has_overlay(state.transition_dst_signal) then extra_alpha_factor = 1.0 - tt end -- If fading between two live inputs, the overlay is put on top. -- If fading between the static picture and a live input, -- the overlay is put on the live input. scene.input0.overlay.overlay_effect:disable() scene.input1.overlay.overlay_effect:disable() scene.overlay.overlay_effect:disable() if state.overlay_enabled then local input0_live = (state.transition_src_signal ~= STATIC_SIGNAL_NUM) local input1_live = (state.transition_dst_signal ~= STATIC_SIGNAL_NUM) if input0_live and not input1_live then scene.input0.overlay.overlay_effect:enable() prepare_overlay_live(state, scene.input0, t, extra_alpha_factor) elseif input1_live and not input0_live then scene.input1.overlay.overlay_effect:enable() prepare_overlay_live(state, scene.input1, t, extra_alpha_factor) else scene.overlay.overlay_effect:enable() prepare_overlay_live(state, scene, t, extra_alpha_factor) end end return scene.scene end function fetch_input_resolution(signals, signal_num) local res = { width = signals:get_width(signal_num), height = signals:get_height(signal_num), interlaced = signals:get_interlaced(signal_num), has_signal = signals:get_has_signal(signal_num), is_connected = signals:get_is_connected(signal_num), frame_rate_nom = signals:get_frame_rate_nom(signal_num), frame_rate_den = signals:get_frame_rate_den(signal_num), last_subtitle = signals:get_last_subtitle(signal_num) } if res.interlaced then -- Convert height from frame height to field height. -- (Needed for e.g. place_rectangle.) res.height = res.height * 2 -- Show field rate instead of frame rate; really for cosmetics only -- (and actually contrary to EBU recommendations, although in line -- with typical user expectations). res.frame_rate_nom = res.frame_rate_nom * 2 end return res end local last_alsa_sound_shark_volume = -1 -- Always update on the first frame. function update_volume() if Nageru.get_num_audio_buses() < 3 or Nageru.get_audio_bus_name(2) ~= 'Sound Shark' then -- We haven't loaded the right audio mapping. return end if first_frame then Nageru.set_audio_bus_fader_level_db(2, -math.huge) first_frame = false end local volume_db = Nageru.get_audio_bus_fader_level_db(2) if Nageru.get_audio_bus_mute(2) or volume_db < -100.0 then volume_db = -100.0 end cef_input:execute_javascript_async("set_sound_shark_volume_db(" .. volume_db .. ")") local desired_alsa_sound_shark_volume = (volume_db < -40.0) and 0 or 154 -- 154 corresponds to 70% in alsamixer for this input. if desired_alsa_sound_shark_volume ~= last_alsa_sound_shark_volume then os.execute("amixer -D hw:CARD=USB sset 'Mix A Input 04' " .. desired_alsa_sound_shark_volume .. " &") os.execute("amixer -D hw:CARD=USB sset 'Mix B Input 04' " .. desired_alsa_sound_shark_volume .. " &") last_alsa_sound_shark_volume = desired_alsa_sound_shark_volume end end local last_rate = 0.0 -- API ENTRY POINT -- Called every frame. Get the scene for displaying at input , -- where 0 is live, 1 is preview, 2 is the first channel to display -- in the bottom bar, and so on up to num_channels()+1. t is the -- current time in seconds. width and height are the dimensions of -- the output, although you can ignore them if you don't need them -- (they're useful if you want to e.g. know what to resample by). -- -- is basically an exposed InputState, which you can use to -- query for information about the signals at the point of the current -- frame. In particular, you can call get_width() and get_height() -- for any signal number, and use that to e.g. assist in scene selection. -- -- You should return scene to use, after having set any parameters you -- want to set (through set_int() etc.). The parameters will be snapshot -- at return time and used during rendering. function get_scene(num, t, width, height, signals) if num == 0 then update_volume() end local input_resolution = {} for signal_num=0,(NUM_CAMERAS-1) do input_resolution[signal_num] = fetch_input_resolution(signals, signal_num) end input_resolution[iptv_video:get_signal_num()] = fetch_input_resolution(signals, iptv_video:get_signal_num()) last_resolution = input_resolution -- Save some CPU time if we're not having SBS on live. local new_rate if state.live_signal_num == SBS_SIGNAL_NUM or state.preview_signal_num == SBS_SIGNAL_NUM or state.transition_type == ZOOM_TRANSITION then new_rate = 1.0 else new_rate = 0.0001 end if new_rate ~= last_rate then -- Avoid waking up the video thread (which may be sleeping) if the rate is the same. bg_video:change_rate(new_rate) last_rate = new_rate end if num == 0 and multiviewer_enabled then -- Live, with debugging on. return multiview_scene_hq end if num == 0 then -- Live. -- See if we're in a transition. finish_transitions(t) if state.transition_type == ZOOM_TRANSITION then -- Transition in or out of SBS. sbs_scene.input0.overlay.overlay_effect:enable_if(state.overlay_enabled) 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) prepare_overlay_live(state, sbs_scene, t, 1.0) return sbs_scene.scene elseif state.transition_type == NO_TRANSITION and state.live_signal_num == SBS_SIGNAL_NUM then -- Static SBS view. sbs_scene.input0.overlay.overlay_effect:enable_if(state.overlay_enabled) prepare_sbs_scene(state, sbs_scene, 0.0, NO_TRANSITION, 0, SBS_SIGNAL_NUM, width, height, input_resolution, true) prepare_overlay_live(state, sbs_scene, t, 1.0) return sbs_scene.scene elseif state.transition_type == FADE_TRANSITION then return get_fade_scene(state, signals, t, width, height, input_resolution) elseif is_plain_signal(state.live_signal_num) then local overlay_really_enabled = state.overlay_enabled and simple_signal_has_overlay(state.live_signal_num) if state.stinger_in_progress then stinger_scene.overlay.overlay_effect:enable_if(overlay_really_enabled) if state.live_signal_num == VIDEO_SIGNAL_NUM then stinger_scene.input:display(iptv_video) else stinger_scene.input:display(state.live_signal_num) end set_scale_parameters_if_needed(stinger_scene, signals, state.live_signal_num, width, height, true) stinger_scene.stinger_input:display(stinger_images[state.stinger_frame]) state.stinger_frame = state.stinger_frame + 1 if state.stinger_frame >= 25 then state.stinger_in_progress = false state.preview_signal_num = state.stinger_src_signal state.live_signal_num = state.stinger_dst_signal if state.stinger_dst_signal == VIDEO_SIGNAL_NUM then -- Turn off the overlay when playing video. state.stinger_save_overlay = state.overlay_enabled state.overlay_enabled = false else -- Restore the state. state.overlay_enabled = state.stinger_save_overlay end end if overlay_really_enabled then prepare_overlay_live(state, stinger_scene, t, 1.0) end return stinger_scene.scene else simple_scene.overlay.overlay_effect:enable_if(overlay_really_enabled) if state.live_signal_num == VIDEO_SIGNAL_NUM then simple_scene.input:display(iptv_video) else simple_scene.input:display(state.live_signal_num) end set_scale_parameters_if_needed(simple_scene, signals, state.live_signal_num, width, height, true) prepare_overlay_live(state, simple_scene, t, 1.0) return simple_scene.scene end elseif state.live_signal_num == STATIC_SIGNAL_NUM then -- Static picture. return static_scene else assert(false) end end -- We do not show overlays on the individual preview inputs. -- The M/E preview matches what we'd put live by doing a transition, as always. local show_overlay = false if num == 1 then -- Preview. if state.preview_signal_num == OVERLAY_SIGNAL_NUM then num = state.live_signal_num + 2 show_overlay = not state.overlay_enabled if state.transition_type ~= NO_TRANSITION then num = state.transition_dst_signal + 2 end else num = state.preview_signal_num + 2 show_overlay = state.overlay_enabled and simple_signal_has_overlay(state.preview_signal_num) end end -- Individual preview inputs (usually without overlay). if is_plain_signal(num - 2) then local signal_num = num - 2 simple_scene.overlay.overlay_effect:enable_if(show_overlay) if signal_num == VIDEO_SIGNAL_NUM then simple_scene.input:display(iptv_video) else simple_scene.input:display(signal_num) end set_scale_parameters_if_needed(simple_scene, signals, signal_num, width, height, false) prepare_overlay_static(simple_scene, t) return simple_scene.scene end if num == SBS_SIGNAL_NUM + 2 then sbs_scene.input0.overlay.overlay_effect:enable_if(show_overlay) prepare_sbs_scene(state, sbs_scene, 0.0, NO_TRANSITION, 0, SBS_SIGNAL_NUM, width, height, input_resolution, false) return sbs_scene.scene end if num == STATIC_SIGNAL_NUM + 2 then return static_scene end if num == OVERLAY_SIGNAL_NUM + 2 then return overlay_scene_lq end end -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding. function round(x) return math.floor(x + 0.5) end function lerp(a, b, t) return a + (b - a) * t end function lerp_pos(a, b, t) return { x0 = lerp(a.x0, b.x0, t), y0 = lerp(a.y0, b.y0, t), x1 = lerp(a.x1, b.x1, t), y1 = lerp(a.y1, b.y1, t) } end function pos_from_top_left(x, y, width, height, screen_width, screen_height) local xs = screen_width / 1280.0 local ys = screen_height / 720.0 return { x0 = round(xs * x), y0 = round(ys * y), x1 = round(xs * (x + width)), y1 = round(ys * (y + height)) } end function prepare_sbs_scene(state, scene, t, transition_type, src_signal, dst_signal, screen_width, screen_height, input_resolution, hq) scene.input0.input:display(0) scene.input1.input:display(COMMENTATOR_SIGNAL_NUM) -- Both inputs are the same size (true side-by-side). local pos0 = pos_from_top_left(1280 - 616 - 16, 186, 616, 347, screen_width, screen_height) local pos1 = pos_from_top_left(16, 186, 616, 347, screen_width, screen_height) local pos_fs = { x0 = 0, y0 = 0, x1 = screen_width, y1 = screen_height } local affine_param if transition_type == NO_TRANSITION then -- Static SBS view. affine_param = { sx = 1.0, sy = 1.0, tx = 0.0, ty = 0.0 } -- Identity. else -- Zooming to/from SBS view into or out of a single view. assert(transition_type == ZOOM_TRANSITION) local signal, real_t if src_signal == SBS_SIGNAL_NUM then signal = dst_signal real_t = t else assert(dst_signal == SBS_SIGNAL_NUM) signal = src_signal real_t = 1.0 - t end if signal == INPUT0_SIGNAL_NUM then affine_param = find_affine_param(pos0, lerp_pos(pos0, pos_fs, real_t)) elseif signal == COMMENTATOR_SIGNAL_NUM then affine_param = find_affine_param(pos1, lerp_pos(pos1, pos_fs, real_t)) end end -- NOTE: input_resolution is not 1-indexed, unlike usual Lua arrays. place_rectangle_with_affine(scene.input0, pos0, affine_param, screen_width, screen_height, input_resolution[0].width, input_resolution[0].height, hq) place_rectangle_with_affine(scene.input1, pos1, affine_param, screen_width, screen_height, input_resolution[1].width, input_resolution[1].height, hq) end -- Find the transformation that changes the first rectangle to the second one. function find_affine_param(a, b) local sx = (b.x1 - b.x0) / (a.x1 - a.x0) local sy = (b.y1 - b.y0) / (a.y1 - a.y0) return { sx = sx, sy = sy, tx = b.x0 - a.x0 * sx, ty = b.y0 - a.y0 * sy } end function place_rectangle_with_affine(input, pos, aff, screen_width, screen_height, input_width, input_height, hq) local x0 = pos.x0 * aff.sx + aff.tx local x1 = pos.x1 * aff.sx + aff.tx local y0 = pos.y0 * aff.sy + aff.ty local y1 = pos.y1 * aff.sy + aff.ty place_rectangle(input, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height, hq) end function place_rectangle(input, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height, hq) input.padding_effect:set_int("width", screen_width) input.padding_effect:set_int("height", screen_height) -- Cull. if x0 > screen_width or x1 < 0.0 or y0 > screen_height or y1 < 0.0 then input.resample_effect:choose(ResizeEffect) -- Low-quality resizing. input.resample_effect:set_int("width", 1) input.resample_effect:set_int("height", 1) input.padding_effect:set_int("left", screen_width + 100) input.padding_effect:set_int("top", screen_height + 100) return end local srcx0 = 0.0 local srcx1 = 1.0 local srcy0 = 0.0 local srcy1 = 1.0 -- Clip. if x0 < 0 then srcx0 = -x0 / (x1 - x0) x0 = 0 end if y0 < 0 then srcy0 = -y0 / (y1 - y0) y0 = 0 end if x1 > screen_width then srcx1 = (screen_width - x0) / (x1 - x0) x1 = screen_width end if y1 > screen_height then srcy1 = (screen_height - y0) / (y1 - y0) y1 = screen_height end if hq then -- High-quality resampling. local resample_effect = input.resample_effect:choose(ResampleEffect) local x_subpixel_offset = x0 - math.floor(x0) local y_subpixel_offset = y0 - math.floor(y0) -- Resampling must be to an integral number of pixels. Round up, -- and then add an extra pixel so we have some leeway for the border. local width = math.ceil(x1 - x0) + 1 local height = math.ceil(y1 - y0) + 1 input.resample_effect:set_int("width", width) input.resample_effect:set_int("height", height) -- Correct the discrepancy with zoom. (This will leave a small -- excess edge of pixels and subpixels, which we'll correct for soon.) local zoom_x = (x1 - x0) / (width * (srcx1 - srcx0)) local zoom_y = (y1 - y0) / (height * (srcy1 - srcy0)) resample_effect:set_float("zoom_x", zoom_x) -- Use the actual effect specialization; specific to ResampleEffect. resample_effect:set_float("zoom_y", zoom_y) resample_effect:set_float("zoom_center_x", 0.0) resample_effect:set_float("zoom_center_y", 0.0) -- Padding must also be to a whole-pixel offset. input.padding_effect:set_int("left", math.floor(x0)) input.padding_effect:set_int("top", math.floor(y0)) -- Correct _that_ discrepancy by subpixel offset in the resampling. resample_effect:set_float("left", srcx0 * input_width - x_subpixel_offset / zoom_x) resample_effect:set_float("top", srcy0 * input_height - y_subpixel_offset / zoom_y) -- Finally, adjust the border so it is exactly where we want it. input.padding_effect:set_float("border_offset_left", x_subpixel_offset) input.padding_effect:set_float("border_offset_right", x1 - (math.floor(x0) + width)) input.padding_effect:set_float("border_offset_top", y_subpixel_offset) input.padding_effect:set_float("border_offset_bottom", y1 - (math.floor(y0) + height)) else -- Lower-quality simple resizing. input.resample_effect:choose(ResizeEffect) local width = round(x1 - x0) local height = round(y1 - y0) input.resample_effect:set_int("width", width) input.resample_effect:set_int("height", height) -- Padding must also be to a whole-pixel offset. input.padding_effect:set_int("left", math.floor(x0)) input.padding_effect:set_int("top", math.floor(y0)) -- No subpixel stuff. input.padding_effect:set_float("border_offset_left", 0.0) input.padding_effect:set_float("border_offset_right", 0.0) input.padding_effect:set_float("border_offset_top", 0.0) input.padding_effect:set_float("border_offset_bottom", 0.0) end end function prepare_overlay_live(state, scene, t, extra_alpha_factor) if scene.overlay then local tt = calc_fade_progress(t, state.overlay_transition_start, state.overlay_transition_end) local overlay_alpha = state.overlay_alpha_src + tt * (state.overlay_alpha_dst - state.overlay_alpha_src) overlay_alpha = overlay_alpha * extra_alpha_factor --print("overlay_alpha=" .. overlay_alpha .. " [" .. state.overlay_alpha_src .. "," .. state.overlay_alpha_dst .. "]@" .. tt) if t > state.overlay_transition_end and state.overlay_alpha_dst == 0.0 then state.overlay_enabled = false -- Takes effect next frame. -- print("Turning off overlay") end scene.overlay.multiply_effect:set_vec4("factor", overlay_alpha, overlay_alpha, overlay_alpha, overlay_alpha) end end function prepare_overlay_static(scene) if scene.overlay then scene.overlay.multiply_effect:set_vec4("factor", 1.0, 1.0, 1.0, 1.0) end end function calc_zoom_progress(t) if t < state.transition_start then return 0.0 elseif t > state.transition_end then return 1.0 else local tt = (t - state.transition_start) / (state.transition_end - state.transition_start) -- Smooth it a bit. return math.sin(tt * 3.14159265358 * 0.5) end end function calc_fade_progress(t, transition_start, transition_end) local tt = (t - transition_start) / (transition_end - transition_start) if tt < 0.0 then return 0.0 elseif tt > 1.0 then return 1.0 end -- Make the fade look maybe a tad more natural, by pumping it -- through a sigmoid function. tt = 10.0 * tt - 5.0 tt = 1.0 / (1.0 + math.exp(-tt)) return tt end ThemeMenu.set( { "Reload overlay", reload_cef }, { "Disconnect IPTV", disconnect_iptv }, { "Toggle multiviewer", toggle_multiviewer } )