1 -- Nageru theme for TFK mini-tournament 2017, based on the default theme.
3 local futatabi_server = "http://gruessi.trd.sesse.net:9096"
6 transition_start = -2.0,
9 transition_src_signal = 0,
10 transition_dst_signal = 0,
13 {0.5, 0.5, 0.5}, -- Input 0.
14 {0.5, 0.5, 0.5}, -- Input 1.
15 {0.5, 0.5, 0.5}, -- Input 2.
16 {0.5, 0.5, 0.5}, -- Input 3.
17 {0.5, 0.5, 0.5}, -- Input 4.
18 {0.5, 0.5, 0.5} -- Input 5.
19 -- Will also be filled with VIDEO_SIGNAL_NUM below.
22 overlay_transition_start = -2.0,
23 overlay_transition_end = -1.0,
24 overlay_alpha_src = 0.0,
25 overlay_alpha_dst = 1.0,
26 overlay_enabled = false,
28 stinger_in_progress = false,
30 stinger_src_signal = 0,
31 stinger_dst_signal = 0,
32 stinger_save_overlay = false,
35 preview_signal_num = 1
37 local NUM_CAMERAS = 6 -- Remember to update neutral_colors, too.
39 -- Valid values for live_signal_num and preview_signal_num.
40 local INPUT0_SIGNAL_NUM = 0
41 local INPUT1_SIGNAL_NUM = 1
42 local INPUT2_SIGNAL_NUM = 2
43 local INPUT3_SIGNAL_NUM = 3
44 local INPUT4_SIGNAL_NUM = 4
45 local INPUT5_SIGNAL_NUM = 5
46 local SBS_SIGNAL_NUM = NUM_CAMERAS
47 local STATIC_SIGNAL_NUM = NUM_CAMERAS + 1
48 local VIDEO_SIGNAL_NUM = NUM_CAMERAS + 2
50 state.neutral_colors[VIDEO_SIGNAL_NUM - INPUT0_SIGNAL_NUM + 1] = {0.5, 0.5, 0.5}
52 -- Preview-only signal showing the current signal with the overlay.
53 -- Not valid for live_signal_num!
54 local OVERLAY_SIGNAL_NUM = NUM_CAMERAS + 3
56 -- Valid values for transition_type. (Cuts are done directly, so they need no entry.)
57 local NO_TRANSITION = 0
58 local ZOOM_TRANSITION = 1
59 local FADE_TRANSITION = 2
61 -- Last width/height/frame rate for each channel, if we have it.
62 -- Note that unlike the values we get from Nageru, the resolution is per
63 -- frame and not per field, since we deinterlace.
64 local last_resolution = {}
66 local cef_path = Nageru.THEME_PATH:match("(.*)/nageru/")
67 local cef_input = HTMLInput.new("file://" .. cef_path .. "/score.html")
68 cef_input:execute_javascript_async("play()")
70 local bg_video = VideoInput.new(cef_path .. "/flow-720.mp4", Nageru.VIDEO_FORMAT_YCBCR)
71 -- local iptv_video = VideoInput.new("http://10.34.129.69:9060/1/v.flv", Nageru.VIDEO_FORMAT_YCBCR)
72 local iptv_video = VideoInput.new(futatabi_server, Nageru.VIDEO_FORMAT_YCBCR)
73 iptv_video:change_rate(10.0)
75 local static_image = ImageInput.new(cef_path .. "/nageru/dsn-bg.png")
79 cef_input:execute_javascript_async("play()")
82 function disconnect_iptv()
83 iptv_video:disconnect()
86 -- An overlay with variable alpha.
87 function make_overlay(scene, base)
88 local image = scene:add_input(cef_input)
89 local multiply_effect = scene:add_effect(MultiplyEffect.new())
90 local overlay_effect = scene:add_optional_effect(OverlayEffect.new(), base, multiply_effect)
93 multiply_effect = multiply_effect,
94 overlay_effect = overlay_effect
98 function make_fade_input(scene)
100 input = scene:add_input(),
101 resample_effect = scene:add_optional_effect(ResampleEffect.new()), -- Activated if scaling.
102 wb_effect = scene:add_optional_effect(WhiteBalanceEffect.new()) -- Activated for video inputs.
104 ret.overlay = make_overlay(scene, ret.wb_effect)
108 -- A scene to fade between two inputs, of which either can be a picture
110 function make_fade_scene()
111 local scene = Scene.new(16, 9)
113 -- If fading between two live inputs, the overlay is put on top.
114 -- If fading between the static picture and a live input,
115 -- the overlay is put on the live input.
116 local input0 = make_fade_input(scene)
117 local input1 = make_fade_input(scene)
118 local mix_effect = scene:add_effect(MixEffect.new(), input0.overlay.overlay_effect, input1.overlay.overlay_effect)
119 local overlay = make_overlay(scene, mix_effect)
121 -- At most one overlay can be active at any given time.
122 input0.overlay.overlay_effect:promise_to_disable_if_enabled(input1.overlay.overlay_effect)
123 input0.overlay.overlay_effect:promise_to_disable_if_enabled(overlay.overlay_effect)
124 input1.overlay.overlay_effect:promise_to_disable_if_enabled(input0.overlay.overlay_effect)
125 input1.overlay.overlay_effect:promise_to_disable_if_enabled(overlay.overlay_effect)
126 overlay.overlay_effect:promise_to_disable_if_enabled(input0.overlay.overlay_effect)
127 overlay.overlay_effect:promise_to_disable_if_enabled(input1.overlay.overlay_effect)
129 scene:finalize(true) -- Only used live, don't bother creating LQ versions.
135 mix_effect = mix_effect,
139 fade_scene = make_fade_scene()
141 function make_sbs_input(scene, has_overlay)
142 local input = scene:add_input()
145 overlay = has_overlay and make_overlay(scene, input) or nil,
146 resample_effect = scene:add_effect({ResampleEffect.new(), ResizeEffect.new()}),
147 wb_effect = scene:add_effect(WhiteBalanceEffect.new()),
148 padding_effect = scene:add_effect(IntegralPaddingEffect.new())
152 -- Side-by-side scene.
153 function make_sbs_scene()
154 local scene = Scene.new(16, 9)
156 local bg = scene:add_input(bg_video)
157 local input0 = make_sbs_input(scene, true)
158 local input1 = make_sbs_input(scene, false)
160 input0.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0)
161 input1.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0)
163 input0.input:display(0)
164 input1.input:display(1)
166 local i0 = scene:add_effect(OverlayEffect.new(), bg, input0.padding_effect)
167 scene:add_effect(OverlayEffect.new(), i0, input1.padding_effect)
174 overlay = input0.overlay -- May be nil.
177 local sbs_scene = make_sbs_scene()
179 function make_simple_scene_no_finalize()
180 local scene = Scene.new(16, 9)
182 local input = scene:add_input()
183 local resample_effect = scene:add_effect({ResampleEffect.new(), ResizeEffect.new(), IdentityEffect.new()})
184 local wb_effect = scene:add_effect(WhiteBalanceEffect.new())
185 local overlay = make_overlay(scene, wb_effect)
190 wb_effect = wb_effect,
191 resample_effect = resample_effect,
196 -- A scene to show a single input on screen.
197 function make_simple_scene()
198 local scene = make_simple_scene_no_finalize()
199 scene.scene:finalize()
203 local simple_scene = make_simple_scene()
205 -- Load all the stinger frames.
206 local stinger_images = {}
208 local filename = cef_path .. "/stinger/blur" .. string.rep("0", 3 - string.len(tostring(idx))) .. idx .. ".png"
209 stinger_images[idx] = ImageInput.new(filename)
212 -- A scene to show a single input on screen, with a stinger on top.
213 function make_stinger_scene()
214 local scene = make_simple_scene_no_finalize()
215 scene.stinger_input = scene.scene:add_input()
216 scene.scene:add_effect(OverlayEffect.new(), scene.overlay.overlay_effect, scene.stinger_input)
217 scene.scene:finalize()
220 local stinger_scene = make_stinger_scene()
222 -- A scene to show a single static picture on screen. Never with HTML overlay.
223 local static_scene = Scene.new(16, 9)
224 static_scene:add_input(static_image)
225 static_scene:finalize()
227 -- A scene to show the overlay and nothing more. LQ only,
228 -- since it is not a valid live signal.
229 local overlay_scene_lq = Scene.new(16, 9)
230 local overlay_scene_lq_input = overlay_scene_lq:add_input(cef_input)
231 overlay_scene_lq:finalize(false)
233 function needs_scale(signals, signal_num, width, height)
234 if signal_num == STATIC_SIGNAL_NUM then
235 -- We assume this is already correctly scaled at load time.
238 assert(is_plain_signal(signal_num))
239 return (signals:get_width(signal_num) ~= width or signals:get_height(signal_num) ~= height)
242 function set_scale_parameters_if_needed(scene, signals, signal_num, width, height, hq)
243 if needs_scale(signals, signal_num, width, height) then
245 scene.resample_effect:choose(ResampleEffect) -- High-quality resampling.
247 scene.resample_effect:choose(ResizeEffect) -- Low-quality resampling.
249 scene.resample_effect:set_int("width", width)
250 scene.resample_effect:set_int("height", height)
252 scene.resample_effect:disable()
258 -- Returns the number of outputs in addition to the live (0) and preview (1).
259 -- Called only once, at the start of the program.
260 function num_channels()
261 return NUM_CAMERAS + 4 -- sbs, static picture, video and overlay
264 function is_plain_signal(num)
265 return (num >= INPUT0_SIGNAL_NUM and num <= INPUT5_SIGNAL_NUM) or (num == VIDEO_SIGNAL_NUM)
268 -- Helper function to write e.g. “720p60”. The difference between this
269 -- and get_channel_resolution_raw() is that this one also can say that
270 -- there's no signal.
271 function get_channel_resolution(signal_num)
272 local res = last_resolution[signal_num]
273 if (not res) or not res.is_connected then
274 return "disconnected"
276 if res.height <= 0 then
279 if not res.has_signal then
280 if res.height == 525 then
281 -- Special mode for the USB3 cards.
284 return get_channel_resolution_raw(res) .. ", no signal"
286 return get_channel_resolution_raw(res)
290 -- Helper function to write e.g. “60” or “59.94”.
291 function get_frame_rate(res)
292 local nom = res.frame_rate_nom
293 local den = res.frame_rate_den
294 if nom % den == 0 then
297 return string.format("%.2f", nom / den)
301 -- Helper function to write e.g. “720p60”.
302 function get_channel_resolution_raw(res)
303 if res.interlaced then
304 return res.height .. "i" .. get_frame_rate(res)
306 return res.height .. "p" .. get_frame_rate(res)
310 function get_futatabi_status(str)
313 for word in string.gmatch(str, '([^;]+)') do
314 table.insert(fields, word)
315 num_fields = num_fields + 1
317 if num_fields >= 4 then
325 -- Returns the name for each additional channel (starting from 2).
326 -- Called at the start of the program, and then each frame for live
327 -- channels in case they change resolution.
328 function channel_name(channel)
329 local signal_num = channel - 2
330 if signal_num == INPUT0_SIGNAL_NUM then
331 return "Main (" .. get_channel_resolution(signal_num) .. ")"
332 elseif signal_num == INPUT1_SIGNAL_NUM then
333 return "Secondary (" .. get_channel_resolution(signal_num) .. ")"
334 elseif signal_num == INPUT2_SIGNAL_NUM then
335 return "Goal L (" .. get_channel_resolution(signal_num) .. ")"
336 elseif signal_num == INPUT3_SIGNAL_NUM then
337 return "Goal R (" .. get_channel_resolution(signal_num) .. ")"
338 elseif signal_num == INPUT4_SIGNAL_NUM then
339 return "Commentators (" .. get_channel_resolution(signal_num) .. ")"
340 elseif signal_num == INPUT5_SIGNAL_NUM then
341 return "Laptop (" .. get_channel_resolution(signal_num) .. ")"
342 elseif signal_num == SBS_SIGNAL_NUM then
343 return "Side-by-side"
344 elseif signal_num == STATIC_SIGNAL_NUM then
345 return "Static picture"
346 elseif signal_num == VIDEO_SIGNAL_NUM then
347 local res = last_resolution[iptv_video:get_signal_num()]
348 if (not res) or res.last_subtitle == nil then
351 return "IPTV (" .. get_futatabi_status(res.last_subtitle) .. ")"
353 elseif signal_num == OVERLAY_SIGNAL_NUM then
359 -- Returns, given a channel number, which signal it corresponds to (starting from 0).
360 -- Should return -1 if the channel does not correspond to a simple signal.
361 -- (The information is used for whether right-click on the channel should bring up
362 -- an input selector or not.)
363 -- Called once for each channel, at the start of the program.
364 -- Will never be called for live (0) or preview (1).
365 function channel_signal(channel)
366 if channel - 2 == VIDEO_SIGNAL_NUM then
367 return iptv_video:get_signal_num()
368 elseif is_plain_signal(channel - 2) then
376 -- Called every frame. Returns the color (if any) to paint around the given
377 -- channel. Returns a CSS color (typically to mark live and preview signals);
378 -- "transparent" is allowed.
379 -- Will never be called for live (0) or preview (1).
380 function channel_color(channel)
381 if state.transition_type ~= NO_TRANSITION then
382 if channel_involved_in(channel, state.transition_src_signal) or
383 channel_involved_in(channel, state.transition_dst_signal) then
387 if channel_involved_in(channel, state.live_signal_num) then
391 if channel_involved_in(channel, state.preview_signal_num) then
397 function channel_involved_in(channel, signal_num)
398 if is_plain_signal(signal_num) then
399 return channel == (signal_num + 2)
401 if signal_num == SBS_SIGNAL_NUM then
402 return is_sbs_participating_signal(channel - 2)
404 if signal_num == STATIC_SIGNAL_NUM then
405 return (channel == NUM_CAMERAS)
407 if signal_num == VIDEO_SIGNAL_NUM then
408 return (channel == NUM_CAMERAS + 1)
414 -- Returns if a given channel supports setting white balance (starting from 2).
415 -- Called only once for each channel, at the start of the program.
416 function supports_set_wb(channel)
417 return is_plain_signal(channel - 2)
421 -- Gets called with a new gray point when the white balance is changing.
422 -- The color is in linear light (not sRGB gamma).
423 function set_wb(channel, red, green, blue)
424 if is_plain_signal(channel - 2) then
425 state.neutral_colors[channel - 2 + 1] = { red, green, blue }
429 function finish_transitions(t)
430 if state.transition_type ~= NO_TRANSITION and t >= state.transition_end then
431 state.live_signal_num = state.transition_dst_signal
432 state.transition_type = NO_TRANSITION
435 -- Disable the overlay if it is no longer visible.
436 if state.overlay_enabled and t > state.overlay_transition_end and state.overlay_alpha_dst == 0.0 then
437 state.overlay_enabled = false
438 print("Turning off overlay")
442 function in_transition(t)
443 return t >= state.transition_start and t <= state.transition_end
446 function is_sbs_participating_signal(signal_num)
447 return signal_num == INPUT0_SIGNAL_NUM or signal_num == INPUT4_SIGNAL_NUM
450 function simple_signal_has_overlay(signal_num)
451 -- The commentator output has no overlay on it.
452 return signal_num ~= INPUT4_SIGNAL_NUM
456 -- Called every frame.
457 function get_transitions(t)
458 if state.preview_signal_num == OVERLAY_SIGNAL_NUM then
459 if t < state.overlay_transition_end then
463 if state.overlay_enabled then
464 return {"Overlay off", "", "Fade ovl out"}
466 return {"Overlay on", "", "Fade ovl in"}
470 if in_transition(t) then
471 -- Transition already in progress, the only thing we can do is really
472 -- cut to the preview. (TODO: Make an “abort” and/or “finish”, too?)
476 if state.live_signal_num == state.preview_signal_num then
477 -- No transitions possible.
481 if (is_plain_signal(state.live_signal_num) and state.preview_signal_num == VIDEO_SIGNAL_NUM) or
482 (is_plain_signal(state.preview_signal_num) and state.live_signal_num == VIDEO_SIGNAL_NUM) then
483 return {"Cut", "Sting", "Fade"}
486 if (is_plain_signal(state.live_signal_num) or state.live_signal_num == STATIC_SIGNAL_NUM) and
487 (is_plain_signal(state.preview_signal_num) or state.preview_signal_num == STATIC_SIGNAL_NUM) then
488 return {"Cut", "", "Fade"}
492 if state.live_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(state.preview_signal_num) then
493 return {"Cut", "Zoom in"}
494 elseif is_sbs_participating_signal(state.live_signal_num) and state.preview_signal_num == SBS_SIGNAL_NUM then
495 return {"Cut", "Zoom out"}
501 function swap_preview_live()
502 local temp = state.live_signal_num
503 state.live_signal_num = state.preview_signal_num
504 state.preview_signal_num = temp
507 function start_transition(type_, t, duration)
508 state.transition_start = t
509 state.transition_end = t + duration
510 state.transition_type = type_
511 state.transition_src_signal = state.live_signal_num
512 state.transition_dst_signal = state.preview_signal_num
513 state.stinger_in_progress = false
518 -- Called when the user clicks a transition button.
519 function transition_clicked(num, t)
520 if state.preview_signal_num == OVERLAY_SIGNAL_NUM then
523 state.overlay_transition_start = -2.0
524 state.overlay_transition_end = -1.0
525 if state.overlay_enabled then
526 state.overlay_enabled = false
527 state.overlay_alpha_src = 0.0
528 state.overlay_alpha_dst = 0.0
530 state.overlay_enabled = true
531 state.overlay_alpha_src = 1.0
532 state.overlay_alpha_dst = 1.0
536 state.overlay_transition_start = t
537 state.overlay_transition_end = t + 1.0
538 if state.overlay_enabled then
539 state.overlay_alpha_src = 1.0
540 state.overlay_alpha_dst = 0.0
542 state.overlay_alpha_src = 0.0
543 state.overlay_alpha_dst = 1.0
545 state.overlay_enabled = true
552 if in_transition(t) then
553 -- Ongoing transition; finish it immediately before the cut.
554 finish_transitions(state.transition_end)
558 state.stinger_in_progress = false
561 finish_transitions(t)
563 if state.live_signal_num == state.preview_signal_num then
568 if (is_plain_signal(state.live_signal_num) and state.preview_signal_num == VIDEO_SIGNAL_NUM) or
569 (is_plain_signal(state.preview_signal_num) and state.live_signal_num == VIDEO_SIGNAL_NUM) then
571 if stinger_in_progress then
575 state.stinger_in_progress = true
576 state.stinger_frame = 0
577 state.stinger_src_signal = state.live_signal_num
578 state.stinger_dst_signal = state.preview_signal_num
582 if is_plain_signal(state.live_signal_num) and is_plain_signal(state.preview_signal_num) then
583 -- We can't zoom between these. Just make a cut.
584 io.write("Cutting from " .. state.live_signal_num .. " to " .. state.live_signal_num .. "\n")
586 state.stinger_in_progress = false
590 if (state.live_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(state.preview_signal_num)) or
591 (state.preview_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(state.live_signal_num)) then
592 start_transition(ZOOM_TRANSITION, t, 1.0)
595 finish_transitions(t)
598 if (state.live_signal_num ~= state.preview_signal_num) and
599 (is_plain_signal(state.live_signal_num) or
600 state.live_signal_num == STATIC_SIGNAL_NUM) and
601 (is_plain_signal(state.preview_signal_num) or
602 state.preview_signal_num == STATIC_SIGNAL_NUM) then
603 start_transition(FADE_TRANSITION, t, 1.0)
605 -- Fades involving SBS are ignored (we have no scene for it).
611 function channel_clicked(num)
612 state.preview_signal_num = num
615 function setup_fade_input(state, input, signals, signal_num, width, height)
616 if signal_num == STATIC_SIGNAL_NUM then
617 input.input:display(static_image)
618 input.wb_effect:disable()
620 -- We assume this is already correctly scaled at load time.
621 input.resample_effect:disable()
623 input.input:display(signal_num)
624 input.wb_effect:enable()
625 set_neutral_color(input.wb_effect, state.neutral_colors[signal_num - INPUT0_SIGNAL_NUM + 1])
627 if (signals:get_width(signal_num) ~= width or signals:get_height(signal_num) ~= height) then
628 input.resample_effect:enable()
629 input.resample_effect:set_int("width", width)
630 input.resample_effect:set_int("height", height)
632 input.resample_effect:disable()
637 function get_fade_scene(state, signals, t, width, height, input_resolution)
638 local scene = fade_scene
639 setup_fade_input(state, scene.input0, signals, state.transition_src_signal, width, height)
640 setup_fade_input(state, scene.input1, signals, state.transition_dst_signal, width, height)
642 local tt = calc_fade_progress(t, state.transition_start, state.transition_end)
643 scene.mix_effect:set_float("strength_first", 1.0 - tt)
644 scene.mix_effect:set_float("strength_second", tt)
646 -- The commentator output has no overlay on it.
647 local extra_alpha_factor = 1.0
648 if not simple_signal_has_overlay(state.transition_src_signal) and
649 not simple_signal_has_overlay(state.transition_dst_signal) then
650 extra_alpha_factor = 0.0
651 elseif not simple_signal_has_overlay(state.transition_src_signal) then
652 extra_alpha_factor = tt
653 elseif not simple_signal_has_overlay(state.transition_dst_signal) then
654 extra_alpha_factor = 1.0 - tt
657 -- If fading between two live inputs, the overlay is put on top.
658 -- If fading between the static picture and a live input,
659 -- the overlay is put on the live input.
660 scene.input0.overlay.overlay_effect:disable()
661 scene.input1.overlay.overlay_effect:disable()
662 scene.overlay.overlay_effect:disable()
663 if state.overlay_enabled then
664 local input0_live = (state.transition_src_signal ~= STATIC_SIGNAL_NUM)
665 local input1_live = (state.transition_dst_signal ~= STATIC_SIGNAL_NUM)
666 if input0_live and not input1_live then
667 scene.input0.overlay.overlay_effect:enable()
668 prepare_overlay_live(state, scene.input0, t, extra_alpha_factor)
669 elseif input1_live and not input0_live then
670 scene.input1.overlay.overlay_effect:enable()
671 prepare_overlay_live(state, scene.input1, t, extra_alpha_factor)
673 scene.overlay.overlay_effect:enable()
674 prepare_overlay_live(state, scene, t, extra_alpha_factor)
680 function fetch_input_resolution(signals, signal_num)
682 width = signals:get_width(signal_num),
683 height = signals:get_height(signal_num),
684 interlaced = signals:get_interlaced(signal_num),
685 has_signal = signals:get_has_signal(signal_num),
686 is_connected = signals:get_is_connected(signal_num),
687 frame_rate_nom = signals:get_frame_rate_nom(signal_num),
688 frame_rate_den = signals:get_frame_rate_den(signal_num),
689 last_subtitle = signals:get_last_subtitle(signal_num)
692 if res.interlaced then
693 -- Convert height from frame height to field height.
694 -- (Needed for e.g. place_rectangle.)
695 res.height = res.height * 2
697 -- Show field rate instead of frame rate; really for cosmetics only
698 -- (and actually contrary to EBU recommendations, although in line
699 -- with typical user expectations).
700 res.frame_rate_nom = res.frame_rate_nom * 2
705 local last_rate = 0.0
708 -- Called every frame. Get the scene for displaying at input <num>,
709 -- where 0 is live, 1 is preview, 2 is the first channel to display
710 -- in the bottom bar, and so on up to num_channels()+1. t is the
711 -- current time in seconds. width and height are the dimensions of
712 -- the output, although you can ignore them if you don't need them
713 -- (they're useful if you want to e.g. know what to resample by).
715 -- <signals> is basically an exposed InputState, which you can use to
716 -- query for information about the signals at the point of the current
717 -- frame. In particular, you can call get_width() and get_height()
718 -- for any signal number, and use that to e.g. assist in scene selection.
720 -- You should return scene to use, after having set any parameters you
721 -- want to set (through set_int() etc.). The parameters will be snapshot
722 -- at return time and used during rendering.
723 function get_scene(num, t, width, height, signals)
724 local input_resolution = {}
725 for signal_num=0,(NUM_CAMERAS-1) do
726 input_resolution[signal_num] = fetch_input_resolution(signals, signal_num)
728 input_resolution[iptv_video:get_signal_num()] = fetch_input_resolution(signals, iptv_video:get_signal_num())
729 last_resolution = input_resolution
731 -- Save some CPU time if we're not having SBS on live.
733 if state.live_signal_num == SBS_SIGNAL_NUM or
734 state.preview_signal_num == SBS_SIGNAL_NUM or
735 state.transition_type == ZOOM_TRANSITION then
740 if new_rate ~= last_rate then
741 -- Avoid waking up the video thread (which may be sleeping) if the rate is the same.
742 bg_video:change_rate(new_rate)
746 if num == 0 then -- Live.
747 -- See if we're in a transition.
748 finish_transitions(t)
749 if state.transition_type == ZOOM_TRANSITION then
750 -- Transition in or out of SBS.
751 sbs_scene.input0.overlay.overlay_effect:enable_if(state.overlay_enabled)
752 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)
753 prepare_overlay_live(state, sbs_scene, t, 1.0)
754 return sbs_scene.scene
755 elseif state.transition_type == NO_TRANSITION and state.live_signal_num == SBS_SIGNAL_NUM then
757 sbs_scene.input0.overlay.overlay_effect:enable_if(state.overlay_enabled)
758 prepare_sbs_scene(state, sbs_scene, 0.0, NO_TRANSITION, 0, SBS_SIGNAL_NUM, width, height, input_resolution, true)
759 prepare_overlay_live(state, sbs_scene, t, 1.0)
760 return sbs_scene.scene
761 elseif state.transition_type == FADE_TRANSITION then
762 return get_fade_scene(state, signals, t, width, height, input_resolution)
763 elseif is_plain_signal(state.live_signal_num) then
764 local overlay_really_enabled = state.overlay_enabled and simple_signal_has_overlay(state.live_signal_num)
765 if state.stinger_in_progress then
766 stinger_scene.overlay.overlay_effect:enable_if(overlay_really_enabled)
767 stinger_scene.input:display(state.live_signal_num)
768 set_scale_parameters_if_needed(stinger_scene, signals, state.live_signal_num, width, height, true)
769 set_neutral_color_from_signal(state, stinger_scene.wb_effect, state.live_signal_num)
771 stinger_scene.stinger_input:display(stinger_images[state.stinger_frame])
772 state.stinger_frame = state.stinger_frame + 1
773 if state.stinger_frame >= 25 then
774 state.stinger_in_progress = false
775 state.preview_signal_num = state.stinger_src_signal
776 state.live_signal_num = state.stinger_dst_signal
778 if state.stinger_dst_signal == VIDEO_SIGNAL_NUM then
779 -- Turn off the overlay when playing video.
780 state.stinger_save_overlay = state.overlay_enabled
781 state.overlay_enabled = false
783 -- Restore the state.
784 state.overlay_enabled = state.stinger_save_overlay
788 if overlay_really_enabled then
789 prepare_overlay_live(state, stinger_scene, t, 1.0)
791 return stinger_scene.scene
793 simple_scene.overlay.overlay_effect:enable_if(overlay_really_enabled)
794 simple_scene.input:display(state.live_signal_num)
795 set_scale_parameters_if_needed(simple_scene, signals, state.live_signal_num, width, height, true)
796 set_neutral_color_from_signal(state, simple_scene.wb_effect, state.live_signal_num)
797 prepare_overlay_live(state, simple_scene, t, 1.0)
798 return simple_scene.scene
800 elseif state.live_signal_num == STATIC_SIGNAL_NUM then -- Static picture.
807 -- We do not show overlays on the individual preview inputs.
808 -- The M/E preview matches what we'd put live by doing a transition, as always.
809 local show_overlay = false
810 if num == 1 then -- Preview.
811 if state.preview_signal_num == OVERLAY_SIGNAL_NUM then
812 num = state.live_signal_num + 2
813 show_overlay = not state.overlay_enabled
815 if state.transition_type ~= NO_TRANSITION then
816 num = state.transition_dst_signal + 2
819 num = state.preview_signal_num + 2
820 show_overlay = state.overlay_enabled and simple_signal_has_overlay(state.preview_signal_num)
824 -- Individual preview inputs (usually without overlay).
825 if is_plain_signal(num - 2) then
826 local signal_num = num - 2
827 simple_scene.overlay.overlay_effect:enable_if(show_overlay)
828 simple_scene.input:display(signal_num)
829 set_scale_parameters_if_needed(simple_scene, signals, signal_num, width, height, false)
830 set_neutral_color_from_signal(state, simple_scene.wb_effect, signal_num)
831 prepare_overlay_static(simple_scene, t)
832 return simple_scene.scene
834 if num == SBS_SIGNAL_NUM + 2 then
835 sbs_scene.input0.overlay.overlay_effect:enable_if(show_overlay)
836 prepare_sbs_scene(state, sbs_scene, 0.0, NO_TRANSITION, 0, SBS_SIGNAL_NUM, width, height, input_resolution, false)
837 return sbs_scene.scene
839 if num == STATIC_SIGNAL_NUM + 2 then
842 if num == OVERLAY_SIGNAL_NUM + 2 then
843 return overlay_scene_lq
847 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
849 return math.floor(x + 0.5)
852 function lerp(a, b, t)
853 return a + (b - a) * t
856 function lerp_pos(a, b, t)
858 x0 = lerp(a.x0, b.x0, t),
859 y0 = lerp(a.y0, b.y0, t),
860 x1 = lerp(a.x1, b.x1, t),
861 y1 = lerp(a.y1, b.y1, t)
865 function pos_from_top_left(x, y, width, height, screen_width, screen_height)
866 local xs = screen_width / 1280.0
867 local ys = screen_height / 720.0
871 x1 = round(xs * (x + width)),
872 y1 = round(ys * (y + height))
876 function prepare_sbs_scene(state, scene, t, transition_type, src_signal, dst_signal, screen_width, screen_height, input_resolution, hq)
877 scene.input0.input:display(0)
878 scene.input1.input:display(4)
879 set_neutral_color(scene.input0.wb_effect, state.neutral_colors[1])
880 set_neutral_color(scene.input1.wb_effect, state.neutral_colors[5])
882 -- Both inputs are the same size (true side-by-side).
883 local pos0 = pos_from_top_left(1280 - 616 - 16, 186, 616, 347, screen_width, screen_height)
884 local pos1 = pos_from_top_left(16, 186, 616, 347, screen_width, screen_height)
886 local pos_fs = { x0 = 0, y0 = 0, x1 = screen_width, y1 = screen_height }
888 if transition_type == NO_TRANSITION then
890 affine_param = { sx = 1.0, sy = 1.0, tx = 0.0, ty = 0.0 } -- Identity.
892 -- Zooming to/from SBS view into or out of a single view.
893 assert(transition_type == ZOOM_TRANSITION)
895 if src_signal == SBS_SIGNAL_NUM then
899 assert(dst_signal == SBS_SIGNAL_NUM)
904 if signal == INPUT0_SIGNAL_NUM then
905 affine_param = find_affine_param(pos0, lerp_pos(pos0, pos_fs, real_t))
906 elseif signal == INPUT4_SIGNAL_NUM then
907 affine_param = find_affine_param(pos1, lerp_pos(pos1, pos_fs, real_t))
911 -- NOTE: input_resolution is not 1-indexed, unlike usual Lua arrays.
912 place_rectangle_with_affine(scene.input0, pos0, affine_param, screen_width, screen_height, input_resolution[0].width, input_resolution[0].height, hq)
913 place_rectangle_with_affine(scene.input1, pos1, affine_param, screen_width, screen_height, input_resolution[1].width, input_resolution[1].height, hq)
916 -- Find the transformation that changes the first rectangle to the second one.
917 function find_affine_param(a, b)
918 local sx = (b.x1 - b.x0) / (a.x1 - a.x0)
919 local sy = (b.y1 - b.y0) / (a.y1 - a.y0)
923 tx = b.x0 - a.x0 * sx,
924 ty = b.y0 - a.y0 * sy
928 function place_rectangle_with_affine(input, pos, aff, screen_width, screen_height, input_width, input_height, hq)
929 local x0 = pos.x0 * aff.sx + aff.tx
930 local x1 = pos.x1 * aff.sx + aff.tx
931 local y0 = pos.y0 * aff.sy + aff.ty
932 local y1 = pos.y1 * aff.sy + aff.ty
934 place_rectangle(input, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height, hq)
937 function place_rectangle(input, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height, hq)
938 input.padding_effect:set_int("width", screen_width)
939 input.padding_effect:set_int("height", screen_height)
942 if x0 > screen_width or x1 < 0.0 or y0 > screen_height or y1 < 0.0 then
943 input.resample_effect:choose(ResizeEffect) -- Low-quality resizing.
944 input.resample_effect:set_int("width", 1)
945 input.resample_effect:set_int("height", 1)
946 input.padding_effect:set_int("left", screen_width + 100)
947 input.padding_effect:set_int("top", screen_height + 100)
958 srcx0 = -x0 / (x1 - x0)
962 srcy0 = -y0 / (y1 - y0)
965 if x1 > screen_width then
966 srcx1 = (screen_width - x0) / (x1 - x0)
969 if y1 > screen_height then
970 srcy1 = (screen_height - y0) / (y1 - y0)
975 -- High-quality resampling.
976 local resample_effect = input.resample_effect:choose(ResampleEffect)
978 local x_subpixel_offset = x0 - math.floor(x0)
979 local y_subpixel_offset = y0 - math.floor(y0)
981 -- Resampling must be to an integral number of pixels. Round up,
982 -- and then add an extra pixel so we have some leeway for the border.
983 local width = math.ceil(x1 - x0) + 1
984 local height = math.ceil(y1 - y0) + 1
985 input.resample_effect:set_int("width", width)
986 input.resample_effect:set_int("height", height)
988 -- Correct the discrepancy with zoom. (This will leave a small
989 -- excess edge of pixels and subpixels, which we'll correct for soon.)
990 local zoom_x = (x1 - x0) / (width * (srcx1 - srcx0))
991 local zoom_y = (y1 - y0) / (height * (srcy1 - srcy0))
992 resample_effect:set_float("zoom_x", zoom_x) -- Use the actual effect specialization; specific to ResampleEffect.
993 resample_effect:set_float("zoom_y", zoom_y)
994 resample_effect:set_float("zoom_center_x", 0.0)
995 resample_effect:set_float("zoom_center_y", 0.0)
997 -- Padding must also be to a whole-pixel offset.
998 input.padding_effect:set_int("left", math.floor(x0))
999 input.padding_effect:set_int("top", math.floor(y0))
1001 -- Correct _that_ discrepancy by subpixel offset in the resampling.
1002 resample_effect:set_float("left", srcx0 * input_width - x_subpixel_offset / zoom_x)
1003 resample_effect:set_float("top", srcy0 * input_height - y_subpixel_offset / zoom_y)
1005 -- Finally, adjust the border so it is exactly where we want it.
1006 input.padding_effect:set_float("border_offset_left", x_subpixel_offset)
1007 input.padding_effect:set_float("border_offset_right", x1 - (math.floor(x0) + width))
1008 input.padding_effect:set_float("border_offset_top", y_subpixel_offset)
1009 input.padding_effect:set_float("border_offset_bottom", y1 - (math.floor(y0) + height))
1011 -- Lower-quality simple resizing.
1012 input.resample_effect:choose(ResizeEffect)
1014 local width = round(x1 - x0)
1015 local height = round(y1 - y0)
1016 input.resample_effect:set_int("width", width)
1017 input.resample_effect:set_int("height", height)
1019 -- Padding must also be to a whole-pixel offset.
1020 input.padding_effect:set_int("left", math.floor(x0))
1021 input.padding_effect:set_int("top", math.floor(y0))
1023 -- No subpixel stuff.
1024 input.padding_effect:set_float("border_offset_left", 0.0)
1025 input.padding_effect:set_float("border_offset_right", 0.0)
1026 input.padding_effect:set_float("border_offset_top", 0.0)
1027 input.padding_effect:set_float("border_offset_bottom", 0.0)
1031 function prepare_overlay_live(state, scene, t, extra_alpha_factor)
1032 if scene.overlay then
1033 local tt = calc_fade_progress(t, state.overlay_transition_start, state.overlay_transition_end)
1034 local overlay_alpha = state.overlay_alpha_src + tt * (state.overlay_alpha_dst - state.overlay_alpha_src)
1035 overlay_alpha = overlay_alpha * extra_alpha_factor
1036 --print("overlay_alpha=" .. overlay_alpha .. " [" .. state.overlay_alpha_src .. "," .. state.overlay_alpha_dst .. "]@" .. tt)
1037 if t > state.overlay_transition_end and state.overlay_alpha_dst == 0.0 then
1038 state.overlay_enabled = false -- Takes effect next frame.
1039 -- print("Turning off overlay")
1041 scene.overlay.multiply_effect:set_vec4("factor", overlay_alpha, overlay_alpha, overlay_alpha, overlay_alpha)
1045 function prepare_overlay_static(scene)
1046 if scene.overlay then
1047 scene.overlay.multiply_effect:set_vec4("factor", 1.0, 1.0, 1.0, 1.0)
1051 function set_neutral_color(effect, color)
1052 effect:set_vec3("neutral_color", color[1], color[2], color[3])
1055 function set_neutral_color_from_signal(state, effect, signal)
1056 if is_plain_signal(signal) then
1057 set_neutral_color(effect, state.neutral_colors[signal - INPUT0_SIGNAL_NUM + 1])
1061 function calc_zoom_progress(t)
1062 if t < state.transition_start then
1064 elseif t > state.transition_end then
1067 local tt = (t - state.transition_start) / (state.transition_end - state.transition_start)
1069 return math.sin(tt * 3.14159265358 * 0.5)
1073 function calc_fade_progress(t, transition_start, transition_end)
1074 local tt = (t - transition_start) / (transition_end - transition_start)
1077 elseif tt > 1.0 then
1081 -- Make the fade look maybe a tad more natural, by pumping it
1082 -- through a sigmoid function.
1083 tt = 10.0 * tt - 5.0
1084 tt = 1.0 / (1.0 + math.exp(-tt))
1090 { "Reload overlay", reload_cef },
1091 { "Disconnect IPTV", disconnect_iptv }