1 -- Nageru theme for TFK mini-tournament 2017, based on the default theme.
4 transition_start = -2.0,
7 transition_src_signal = 0,
8 transition_dst_signal = 0,
11 {0.5, 0.5, 0.5}, -- Input 0.
12 {0.5, 0.5, 0.5}, -- Input 1.
13 {0.5, 0.5, 0.5}, -- Input 2.
14 {0.5, 0.5, 0.5}, -- Input 3.
15 {0.5, 0.5, 0.5}, -- Input 4.
16 {0.5, 0.5, 0.5} -- Input 5.
17 -- Will also be filled with VIDEO_SIGNAL_NUM below.
20 overlay_transition_start = -2.0,
21 overlay_transition_end = -1.0,
22 overlay_alpha_src = 0.0,
23 overlay_alpha_dst = 1.0,
24 overlay_enabled = false,
26 stinger_in_progress = false,
28 stinger_src_signal = 0,
29 stinger_dst_signal = 0,
32 preview_signal_num = 1
34 local NUM_CAMERAS = 6 -- Remember to update neutral_colors, too.
36 -- Valid values for live_signal_num and preview_signal_num.
37 local INPUT0_SIGNAL_NUM = 0
38 local INPUT1_SIGNAL_NUM = 1
39 local INPUT2_SIGNAL_NUM = 2
40 local INPUT3_SIGNAL_NUM = 3
41 local INPUT4_SIGNAL_NUM = 4
42 local INPUT5_SIGNAL_NUM = 5
43 local SBS_SIGNAL_NUM = NUM_CAMERAS
44 local STATIC_SIGNAL_NUM = NUM_CAMERAS + 1
45 local VIDEO_SIGNAL_NUM = NUM_CAMERAS + 2
47 state.neutral_colors[VIDEO_SIGNAL_NUM - INPUT0_SIGNAL_NUM + 1] = {0.5, 0.5, 0.5}
49 -- Preview-only signal showing the current signal with the overlay.
50 -- Not valid for live_signal_num!
51 local OVERLAY_SIGNAL_NUM = NUM_CAMERAS + 3
53 -- Valid values for transition_type. (Cuts are done directly, so they need no entry.)
54 local NO_TRANSITION = 0
55 local ZOOM_TRANSITION = 1
56 local FADE_TRANSITION = 2
58 -- Last width/height/frame rate for each channel, if we have it.
59 -- Note that unlike the values we get from Nageru, the resolution is per
60 -- frame and not per field, since we deinterlace.
61 local last_resolution = {}
63 local cef_path = Nageru.THEME_PATH:match("(.*)/nageru/")
64 local cef_input = HTMLInput.new("file://" .. cef_path .. "/score.html")
65 cef_input:execute_javascript_async("play()")
67 local bg_video = VideoInput.new(cef_path .. "/flow-720.mp4", Nageru.VIDEO_FORMAT_YCBCR)
68 -- local iptv_video = VideoInput.new("http://10.34.129.69:9060/1/v.flv", Nageru.VIDEO_FORMAT_YCBCR)
69 local iptv_video = VideoInput.new("http://home.samfundet.no/~sesse/videostuff", Nageru.VIDEO_FORMAT_YCBCR)
73 cef_input:execute_javascript_async("play()")
76 function disconnect_iptv()
77 iptv_video:disconnect()
80 -- Utility function to help creating many similar chains that can differ
81 -- in a free set of chosen parameters.
82 function make_cartesian_product(parms, callback)
83 return make_cartesian_product_internal(parms, callback, 1, {})
86 function make_cartesian_product_internal(parms, callback, index, args)
87 if index > #parms then
88 return callback(unpack(args))
91 for _, value in ipairs(parms[index]) do
93 ret[value] = make_cartesian_product_internal(parms, callback, index + 1, args)
98 -- An overlay with variable alpha.
99 function make_overlay(chain, base)
100 local image = chain:add_html_input(cef_input)
101 local multiply_effect = chain:add_effect(MultiplyEffect.new())
102 local overlay_effect = chain:add_effect(OverlayEffect.new(), base, multiply_effect)
105 multiply_effect = multiply_effect,
106 overlay_effect = overlay_effect
110 function possibly_make_overlay(has_overlay, chain, base)
111 if has_overlay == true then
112 return make_overlay(chain, base)
118 function make_fade_input(chain, signal, live, deint, scale, video)
119 local input, wb_effect, resample_effect, last
121 input = chain:add_video_input(iptv_video, false)
124 input = chain:add_live_input(false, deint)
125 input:connect_signal(signal)
128 input = chain:add_effect(ImageInput.new(cef_path .. "/nageru/dsn-bg.png"))
132 -- If we cared about this for the non-main inputs, we would have
133 -- checked hq here and invoked ResizeEffect instead.
135 resample_effect = chain:add_effect(ResampleEffect.new())
136 last = resample_effect
139 -- Make sure to put the white balance after the scaling (usually more efficient).
141 wb_effect = chain:add_effect(WhiteBalanceEffect.new())
147 wb_effect = wb_effect,
148 resample_effect = resample_effect,
153 -- A chain to fade between two inputs, of which either can be a picture
154 -- or a live input. In practice only used live, but we still support the
156 function make_fade_chain(input0_live, input0_deint, input0_scale, input0_video, input1_live, input1_deint, input1_scale, input1_video, has_overlay, hq)
157 local chain = EffectChain.new(16, 9)
159 local input0 = make_fade_input(chain, INPUT0_SIGNAL_NUM, input0_live, input0_deint, input0_scale, input0_video)
160 local input1 = make_fade_input(chain, INPUT1_SIGNAL_NUM, input1_live, input1_deint, input1_scale, input1_video)
162 -- If fading between two live inputs, the overlay is put on top.
163 -- If fading between the static picture and a live input,
164 -- the overlay is put on the live input.
166 if input0_live and not input1_live then
167 overlay = possibly_make_overlay(has_overlay, chain, input0.last)
169 input0.last = overlay.overlay_effect
171 elseif input1_live and not input0_live then
172 overlay = possibly_make_overlay(has_overlay, chain, input1.last)
174 input1.last = overlay.overlay_effect
178 local mix_effect = chain:add_effect(MixEffect.new(), input0.last, input1.last)
179 if input0_live and input1_live then
180 overlay = possibly_make_overlay(has_overlay, chain, mix_effect)
189 mix_effect = mix_effect,
194 -- Chains to fade between two inputs, in various configurations.
195 local fade_chains = make_cartesian_product({
196 {"video", "static", "live", "livedeint"}, -- input0_type
197 {true, false}, -- input0_scale
198 {"video", "static", "live", "livedeint"}, -- input1_type
199 {true, false}, -- input1_scale
200 {true, false}, -- has_overlay
202 }, function(input0_type, input0_scale, input1_type, input1_scale, has_overlay, hq)
203 local input0_live = (input0_type ~= "static")
204 local input1_live = (input1_type ~= "static")
205 local input0_deint = (input0_type == "livedeint")
206 local input1_deint = (input1_type == "livedeint")
207 local input0_video = (input0_type == "video")
208 local input1_video = (input1_type == "video")
209 return make_fade_chain(input0_live, input0_deint, input0_scale, input0_video, input1_live, input1_deint, input1_scale, input1_video, has_overlay, hq)
212 function make_sbs_input(chain, signal, deint, has_overlay, hq)
213 local input = chain:add_live_input(not deint, deint) -- Override bounce only if not deinterlacing.
214 input:connect_signal(signal)
216 local wb_effect = chain:add_effect(WhiteBalanceEffect.new()) -- Needs to be before the overlay.
220 overlay = make_overlay(chain, wb_effect)
223 local resample_effect = nil
224 local resize_effect = nil
226 resample_effect = chain:add_effect(ResampleEffect.new())
228 resize_effect = chain:add_effect(ResizeEffect.new())
231 local padding_effect = chain:add_effect(IntegralPaddingEffect.new())
235 wb_effect = wb_effect,
236 resample_effect = resample_effect,
237 resize_effect = resize_effect,
238 padding_effect = padding_effect,
243 -- Side-by-side chains.
244 function make_sbs_chain(input0_type, input0_overlay, input1_type, hq)
245 local chain = EffectChain.new(16, 9)
247 local bg = chain:add_video_input(bg_video, false)
249 local input0 = make_sbs_input(chain, INPUT0_SIGNAL_NUM, input0_type == "livedeint", input0_overlay, hq)
250 local input1 = make_sbs_input(chain, INPUT4_SIGNAL_NUM, input1_type == "livedeint", false, hq)
252 input0.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0)
253 input1.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0)
255 local i0 = chain:add_effect(OverlayEffect.new(), bg, input0.padding_effect)
256 chain:add_effect(OverlayEffect.new(), i0, input1.padding_effect)
263 overlay = input0.overlay -- May be nil.
268 -- Make all possible combinations of side-by-side chains.
269 local sbs_chains = make_cartesian_product({
270 {"live", "livedeint"}, -- input0_type
271 {true, false}, -- input0_overlay
272 {"live", "livedeint"}, -- input1_type
274 }, function(input0_type, input0_overlay, input1_type, hq)
275 return make_sbs_chain(input0_type, input0_overlay, input1_type, hq)
278 function make_simple_chain_no_finalize(input_deint, input_video, input_scale, has_overlay, hq)
279 local chain = EffectChain.new(16, 9)
283 input = chain:add_video_input(iptv_video, false)
285 input = chain:add_live_input(false, input_deint)
286 input:connect_signal(0) -- First input card. Can be changed whenever you want.
289 local resample_effect, resize_effect
292 resample_effect = chain:add_effect(ResampleEffect.new())
294 resize_effect = chain:add_effect(ResizeEffect.new())
298 local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
299 local overlay = possibly_make_overlay(has_overlay, chain, wb_effect)
304 wb_effect = wb_effect,
305 resample_effect = resample_effect,
306 resize_effect = resize_effect,
311 -- A chain to show a single input on screen.
312 function make_simple_chain(input_deint, input_video, input_scale, has_overlay, hq)
313 local chain = make_simple_chain_no_finalize(input_deint, input_video, input_scale, has_overlay, hq)
314 chain.chain:finalize(hq)
318 -- Make all possible combinations of single-input chains.
319 local simple_chains = make_cartesian_product({
320 {"video", "live", "livedeint"}, -- input_type
321 {true, false}, -- input_scale
322 {true, false}, -- has_overlay
324 }, function(input_type, input_scale, has_overlay, hq)
325 local input_deint = (input_type == "livedeint")
326 local input_video = (input_type == "video")
327 return make_simple_chain(input_deint, input_video, input_scale, has_overlay, hq)
330 -- A chain to show a single input on screen, with a stinger on top.
331 function make_stinger_chain(input_deint, input_video, input_scale, has_overlay, stinger_frame, hq)
332 local chain = make_simple_chain_no_finalize(input_deint, input_video, input_scale, has_overlay, hq)
333 local filename = cef_path .. "/stinger/blur" .. string.rep("0", 3 - string.len(tostring(stinger_frame))) .. stinger_frame .. ".png"
337 last = chain.overlay.overlay_effect
339 last = chain.wb_effect
341 local input = chain.chain:add_effect(ImageInput.new(filename))
342 chain.chain:add_effect(OverlayEffect.new(), last, input)
343 chain.chain:finalize(hq)
347 -- Single-input chains with various stinger frames on top.
348 local stinger_chains = make_cartesian_product({
349 {"video", "live", "livedeint"}, -- input_type
350 {true, false}, -- input_scale
351 {true, false}, -- has_overlay
352 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}, -- stinger_frame
354 }, function(input_type, input_scale, has_overlay, stinger_frame, hq)
355 local input_deint = (input_type == "livedeint")
356 local input_video = (input_type == "video")
357 return make_stinger_chain(input_deint, input_video, input_scale, has_overlay, stinger_frame, hq)
360 -- A chain to show a single static picture on screen. Never with HTML overlay.
361 local static_chains = make_cartesian_product({
364 local chain = EffectChain.new(16, 9)
365 local chain_input = chain:add_effect(ImageInput.new(cef_path .. "/nageru/dsn-bg.png"))
373 -- A chain to show the overlay and nothing more. LQ only,
374 -- since it is not a valid live signal.
375 local overlay_chain_lq = EffectChain.new(16, 9)
376 local overlay_chain_lq_input = overlay_chain_lq:add_html_input(cef_input)
377 overlay_chain_lq:finalize(false)
379 -- Used for indexing into the tables of chains.
380 function get_input_type(signals, signal_num)
381 if signal_num == STATIC_SIGNAL_NUM then
383 elseif signal_num == VIDEO_SIGNAL_NUM then
385 elseif signals:get_interlaced(signal_num) then
392 function needs_scale(signals, signal_num, width, height)
393 if signal_num == STATIC_SIGNAL_NUM then
394 -- We assume this is already correctly scaled at load time.
397 assert(is_plain_signal(signal_num))
398 return (signals:get_width(signal_num) ~= width or signals:get_height(signal_num) ~= height)
401 function set_scale_parameters_if_needed(chain_or_input, width, height)
402 if chain_or_input.resample_effect then
403 chain_or_input.resample_effect:set_int("width", width)
404 chain_or_input.resample_effect:set_int("height", height)
405 elseif chain_or_input.resize_effect then
406 chain_or_input.resize_effect:set_int("width", width)
407 chain_or_input.resize_effect:set_int("height", height)
412 -- Returns the number of outputs in addition to the live (0) and preview (1).
413 -- Called only once, at the start of the program.
414 function num_channels()
415 return NUM_CAMERAS + 4 -- sbs, static picture, video and overlay
418 function is_plain_signal(num)
419 return (num >= INPUT0_SIGNAL_NUM and num <= INPUT5_SIGNAL_NUM) or (num == VIDEO_SIGNAL_NUM)
422 -- Helper function to write e.g. “720p60”. The difference between this
423 -- and get_channel_resolution_raw() is that this one also can say that
424 -- there's no signal.
425 function get_channel_resolution(signal_num)
426 local res = last_resolution[signal_num]
427 if (not res) or not res.is_connected then
428 return "disconnected"
430 if res.height <= 0 then
433 if not res.has_signal then
434 if res.height == 525 then
435 -- Special mode for the USB3 cards.
438 return get_channel_resolution_raw(res) .. ", no signal"
440 return get_channel_resolution_raw(res)
444 -- Helper function to write e.g. “60” or “59.94”.
445 function get_frame_rate(res)
446 local nom = res.frame_rate_nom
447 local den = res.frame_rate_den
448 if nom % den == 0 then
451 return string.format("%.2f", nom / den)
455 -- Helper function to write e.g. “720p60”.
456 function get_channel_resolution_raw(res)
457 if res.interlaced then
458 return res.height .. "i" .. get_frame_rate(res)
460 return res.height .. "p" .. get_frame_rate(res)
465 -- Returns the name for each additional channel (starting from 2).
466 -- Called at the start of the program, and then each frame for live
467 -- channels in case they change resolution.
468 function channel_name(channel)
469 local signal_num = channel - 2
470 if signal_num == INPUT0_SIGNAL_NUM then
471 return "Main (" .. get_channel_resolution(signal_num) .. ")"
472 elseif signal_num == INPUT1_SIGNAL_NUM then
473 return "Secondary (" .. get_channel_resolution(signal_num) .. ")"
474 elseif signal_num == INPUT2_SIGNAL_NUM then
475 return "Goal L (" .. get_channel_resolution(signal_num) .. ")"
476 elseif signal_num == INPUT3_SIGNAL_NUM then
477 return "Goal R (" .. get_channel_resolution(signal_num) .. ")"
478 elseif signal_num == INPUT4_SIGNAL_NUM then
479 return "Commentators (" .. get_channel_resolution(signal_num) .. ")"
480 elseif signal_num == INPUT5_SIGNAL_NUM then
481 return "Laptop (" .. get_channel_resolution(signal_num) .. ")"
482 elseif signal_num == SBS_SIGNAL_NUM then
483 return "Side-by-side"
484 elseif signal_num == STATIC_SIGNAL_NUM then
485 return "Static picture"
486 elseif signal_num == VIDEO_SIGNAL_NUM then
488 elseif signal_num == OVERLAY_SIGNAL_NUM then
494 -- Returns, given a channel number, which signal it corresponds to (starting from 0).
495 -- Should return -1 if the channel does not correspond to a simple signal.
496 -- (The information is used for whether right-click on the channel should bring up
497 -- an input selector or not.)
498 -- Called once for each channel, at the start of the program.
499 -- Will never be called for live (0) or preview (1).
500 function channel_signal(channel)
501 if channel - 2 == VIDEO_SIGNAL_NUM then
502 return iptv_video:get_signal_num()
503 elseif is_plain_signal(channel - 2) then
511 -- Called every frame. Returns the color (if any) to paint around the given
512 -- channel. Returns a CSS color (typically to mark live and preview signals);
513 -- "transparent" is allowed.
514 -- Will never be called for live (0) or preview (1).
515 function channel_color(channel)
516 if state.transition_type ~= NO_TRANSITION then
517 if channel_involved_in(channel, state.transition_src_signal) or
518 channel_involved_in(channel, state.transition_dst_signal) then
522 if channel_involved_in(channel, state.live_signal_num) then
526 if channel_involved_in(channel, state.preview_signal_num) then
532 function channel_involved_in(channel, signal_num)
533 if is_plain_signal(signal_num) then
534 return channel == (signal_num + 2)
536 if signal_num == SBS_SIGNAL_NUM then
537 return is_sbs_participating_signal(channel - 2)
539 if signal_num == STATIC_SIGNAL_NUM then
540 return (channel == NUM_CAMERAS)
542 if signal_num == VIDEO_SIGNAL_NUM then
543 return (channel == NUM_CAMERAS + 1)
549 -- Returns if a given channel supports setting white balance (starting from 2).
550 -- Called only once for each channel, at the start of the program.
551 function supports_set_wb(channel)
552 return is_plain_signal(channel - 2)
556 -- Gets called with a new gray point when the white balance is changing.
557 -- The color is in linear light (not sRGB gamma).
558 function set_wb(channel, red, green, blue)
559 if is_plain_signal(channel - 2) then
560 state.neutral_colors[channel - 2 + 1] = { red, green, blue }
564 function finish_transitions(t)
565 if state.transition_type ~= NO_TRANSITION and t >= state.transition_end then
566 state.live_signal_num = state.transition_dst_signal
567 state.transition_type = NO_TRANSITION
570 -- Disable the overlay if it is no longer visible.
571 if state.overlay_enabled and t > state.overlay_transition_end and state.overlay_alpha_dst == 0.0 then
572 state.overlay_enabled = false
573 print("Turning off overlay")
577 function in_transition(t)
578 return t >= state.transition_start and t <= state.transition_end
581 function is_sbs_participating_signal(signal_num)
582 return signal_num == INPUT0_SIGNAL_NUM or signal_num == INPUT4_SIGNAL_NUM
585 function simple_signal_has_overlay(signal_num)
586 -- The commentator output has no overlay on it.
587 return signal_num ~= INPUT4_SIGNAL_NUM
591 -- Called every frame.
592 function get_transitions(t)
593 if state.preview_signal_num == OVERLAY_SIGNAL_NUM then
594 if t < state.overlay_transition_end then
598 if state.overlay_enabled then
599 return {"Overlay off", "", "Fade ovl out"}
601 return {"Overlay on", "", "Fade ovl in"}
605 if in_transition(t) then
606 -- Transition already in progress, the only thing we can do is really
607 -- cut to the preview. (TODO: Make an “abort” and/or “finish”, too?)
611 if state.live_signal_num == state.preview_signal_num then
612 -- No transitions possible.
616 if (is_plain_signal(state.live_signal_num) and state.preview_signal_num == VIDEO_SIGNAL_NUM) or
617 (is_plain_signal(state.preview_signal_num) and state.live_signal_num == VIDEO_SIGNAL_NUM) then
618 return {"Cut", "Sting", "Fade"}
621 if (is_plain_signal(state.live_signal_num) or state.live_signal_num == STATIC_SIGNAL_NUM) and
622 (is_plain_signal(state.preview_signal_num) or state.preview_signal_num == STATIC_SIGNAL_NUM) then
623 return {"Cut", "", "Fade"}
627 if state.live_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(state.preview_signal_num) then
628 return {"Cut", "Zoom in"}
629 elseif is_sbs_participating_signal(state.live_signal_num) and state.preview_signal_num == SBS_SIGNAL_NUM then
630 return {"Cut", "Zoom out"}
636 function swap_preview_live()
637 local temp = state.live_signal_num
638 state.live_signal_num = state.preview_signal_num
639 state.preview_signal_num = temp
642 function start_transition(type_, t, duration)
643 state.transition_start = t
644 state.transition_end = t + duration
645 state.transition_type = type_
646 state.transition_src_signal = state.live_signal_num
647 state.transition_dst_signal = state.preview_signal_num
648 state.stinger_in_progress = false
653 -- Called when the user clicks a transition button.
654 function transition_clicked(num, t)
655 if state.preview_signal_num == OVERLAY_SIGNAL_NUM then
658 state.overlay_transition_start = -2.0
659 state.overlay_transition_end = -1.0
660 if state.overlay_enabled then
661 state.overlay_enabled = false
662 state.overlay_alpha_src = 0.0
663 state.overlay_alpha_dst = 0.0
665 state.overlay_enabled = true
666 state.overlay_alpha_src = 1.0
667 state.overlay_alpha_dst = 1.0
671 state.overlay_transition_start = t
672 state.overlay_transition_end = t + 1.0
673 if state.overlay_enabled then
674 state.overlay_alpha_src = 1.0
675 state.overlay_alpha_dst = 0.0
677 state.overlay_alpha_src = 0.0
678 state.overlay_alpha_dst = 1.0
680 state.overlay_enabled = true
687 if in_transition(t) then
688 -- Ongoing transition; finish it immediately before the cut.
689 finish_transitions(state.transition_end)
693 state.stinger_in_progress = false
696 finish_transitions(t)
698 if state.live_signal_num == state.preview_signal_num then
703 if (is_plain_signal(state.live_signal_num) and state.preview_signal_num == VIDEO_SIGNAL_NUM) or
704 (is_plain_signal(state.preview_signal_num) and state.live_signal_num == VIDEO_SIGNAL_NUM) then
706 if stinger_in_progress then
710 io.write("Inserting stinger from " .. state.live_signal_num .. " to " .. state.live_signal_num .. "\n")
711 state.stinger_in_progress = true
712 state.stinger_frame = 0
713 state.stinger_src_signal = state.live_signal_num
714 state.stinger_dst_signal = state.preview_signal_num
718 if is_plain_signal(state.live_signal_num) and is_plain_signal(state.preview_signal_num) then
719 -- We can't zoom between these. Just make a cut.
720 io.write("Cutting from " .. state.live_signal_num .. " to " .. state.live_signal_num .. "\n")
722 state.stinger_in_progress = false
726 if (state.live_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(state.preview_signal_num)) or
727 (state.preview_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(state.live_signal_num)) then
728 start_transition(ZOOM_TRANSITION, t, 1.0)
731 finish_transitions(t)
734 if (state.live_signal_num ~= state.preview_signal_num) and
735 (is_plain_signal(state.live_signal_num) or
736 state.live_signal_num == STATIC_SIGNAL_NUM) and
737 (is_plain_signal(state.preview_signal_num) or
738 state.preview_signal_num == STATIC_SIGNAL_NUM) then
739 start_transition(FADE_TRANSITION, t, 1.0)
741 -- Fades involving SBS are ignored (we have no chain for it).
747 function channel_clicked(num)
748 state.preview_signal_num = num
751 function get_fade_chain(state, signals, t, width, height, input_resolution)
752 local input0_type = get_input_type(signals, state.transition_src_signal)
753 local input0_scale = needs_scale(signals, state.transition_src_signal, width, height)
754 local input1_type = get_input_type(signals, state.transition_dst_signal)
755 local input1_scale = needs_scale(signals, state.transition_dst_signal, width, height)
756 local chain = fade_chains[input0_type][input0_scale][input1_type][input1_scale][state.overlay_enabled][true]
757 local prepare = function()
758 if input0_type == "live" or input0_type == "livedeint" then
759 chain.input0.input:connect_signal(state.transition_src_signal)
761 if input0_type ~= "static" then
762 set_neutral_color_from_signal(state, chain.input0.wb_effect, state.transition_src_signal)
764 set_scale_parameters_if_needed(chain.input0, width, height)
765 if input1_type == "live" or input1_type == "livedeint" then
766 chain.input1.input:connect_signal(state.transition_dst_signal)
768 if input1_type ~= "static" then
769 set_neutral_color_from_signal(state, chain.input1.wb_effect, state.transition_dst_signal)
771 set_scale_parameters_if_needed(chain.input1, width, height)
772 local tt = calc_fade_progress(t, state.transition_start, state.transition_end)
774 chain.mix_effect:set_float("strength_first", 1.0 - tt)
775 chain.mix_effect:set_float("strength_second", tt)
777 -- The commentator output has no overlay on it.
778 local extra_alpha_factor = 1.0
779 if not simple_signal_has_overlay(state.transition_src_signal) and
780 not simple_signal_has_overlay(state.transition_dst_signal) then
781 extra_alpha_factor = 0.0
782 elseif not simple_signal_has_overlay(state.transition_src_signal) then
783 extra_alpha_factor = tt
784 elseif not simple_signal_has_overlay(state.transition_dst_signal) then
785 extra_alpha_factor = 1.0 - tt
787 prepare_overlay_live(state, chain, t, extra_alpha_factor)
789 return chain.chain, prepare
792 -- SBS code (live_signal_num == SBS_SIGNAL_NUM, or in a transition to/from it).
793 function get_sbs_chain(state, signals)
794 local input0_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
795 local input1_type = get_input_type(signals, INPUT4_SIGNAL_NUM)
796 return sbs_chains[input0_type][state.overlay_enabled][input1_type][true]
799 local last_rate = 0.0
802 -- Called every frame. Get the chain for displaying at input <num>,
803 -- where 0 is live, 1 is preview, 2 is the first channel to display
804 -- in the bottom bar, and so on up to num_channels()+1. t is the
805 -- current time in seconds. width and height are the dimensions of
806 -- the output, although you can ignore them if you don't need them
807 -- (they're useful if you want to e.g. know what to resample by).
809 -- <signals> is basically an exposed InputState, which you can use to
810 -- query for information about the signals at the point of the current
811 -- frame. In particular, you can call get_width() and get_height()
812 -- for any signal number, and use that to e.g. assist in chain selection.
814 -- You should return two objects; the chain itself, and then a
815 -- function (taking no parameters) that is run just before rendering.
816 -- The function needs to call connect_signal on any inputs, so that
817 -- it gets updated video data for the given frame. (You are allowed
818 -- to switch which input your input is getting from between frames,
819 -- but not calling connect_signal results in undefined behavior.)
820 -- If you want to change any parameters in the chain, this is also
823 -- NOTE: The chain returned must be finalized with the Y'CbCr flag
824 -- if and only if num==0.
825 function get_chain(num, t, width, height, signals)
826 local input_resolution = {}
827 for signal_num=0,(NUM_CAMERAS-1) do
829 width = signals:get_width(signal_num),
830 height = signals:get_height(signal_num),
831 interlaced = signals:get_interlaced(signal_num),
832 has_signal = signals:get_has_signal(signal_num),
833 is_connected = signals:get_is_connected(signal_num),
834 frame_rate_nom = signals:get_frame_rate_nom(signal_num),
835 frame_rate_den = signals:get_frame_rate_den(signal_num)
838 if res.interlaced then
839 -- Convert height from frame height to field height.
840 -- (Needed for e.g. place_rectangle.)
841 res.height = res.height * 2
843 -- Show field rate instead of frame rate; really for cosmetics only
844 -- (and actually contrary to EBU recommendations, although in line
845 -- with typical user expectations).
846 res.frame_rate_nom = res.frame_rate_nom * 2
849 input_resolution[signal_num] = res
851 last_resolution = input_resolution
853 -- Make a (semi-shallow) copy of the current state, so that the returned prepare function
854 -- is unaffected by state changes made by the UI before it is rendered.
855 local state_copy = {}
856 for key, value in pairs(state) do
857 state_copy[key] = value
859 for key, value in pairs(state.neutral_colors) do
860 state_copy.neutral_colors[key] = value
863 -- Save some CPU time if we're not having SBS on live.
865 if state.live_signal_num == SBS_SIGNAL_NUM or
866 state.preview_signal_num == SBS_SIGNAL_NUM or
867 state.transition_type == ZOOM_TRANSITION then
872 if new_rate ~= last_rate then
873 -- Avoid waking up the video thread (which may be sleeping) if the rate is the same.
874 bg_video:change_rate(new_rate)
878 if num == 0 then -- Live.
879 -- See if we're in a transition.
880 finish_transitions(t)
881 if state.transition_type == ZOOM_TRANSITION then
882 -- Transition in or out of SBS.
883 local chain = get_sbs_chain(state_copy, signals)
884 local prepare = function()
885 prepare_sbs_chain(state_copy, chain, calc_zoom_progress(t), state_copy.transition_type, state_copy.transition_src_signal, state_copy.transition_dst_signal, width, height, input_resolution)
886 prepare_overlay_live(state_copy, chain, t, 1.0)
888 return chain.chain, prepare
889 elseif state.transition_type == NO_TRANSITION and state.live_signal_num == SBS_SIGNAL_NUM then
891 local chain = get_sbs_chain(state_copy, signals)
892 local prepare = function()
893 prepare_sbs_chain(state_copy, chain, 0.0, NO_TRANSITION, 0, SBS_SIGNAL_NUM, width, height, input_resolution)
894 prepare_overlay_live(state_copy, chain, t, 1.0)
896 return chain.chain, prepare
897 elseif state.transition_type == FADE_TRANSITION then
898 return get_fade_chain(state_copy, signals, t, width, height, input_resolution)
899 elseif is_plain_signal(state.live_signal_num) then
900 local input_type = get_input_type(signals, state.live_signal_num)
901 local input_scale = needs_scale(signals, state.live_signal_num, width, height)
902 local overlay_really_enabled = state.overlay_enabled and simple_signal_has_overlay(state.live_signal_num)
904 if state.stinger_in_progress then
905 chain = stinger_chains[input_type][input_scale][overlay_really_enabled][state.stinger_frame][true]
906 state.stinger_frame = state.stinger_frame + 1
907 if state.stinger_frame >= 25 then
908 state.stinger_in_progress = false
909 state.preview_signal_num = state.stinger_src_signal
910 state.live_signal_num = state.stinger_dst_signal
913 chain = simple_chains[input_type][input_scale][overlay_really_enabled][true]
915 local prepare = function()
916 if input_type ~= "video" then
917 chain.input:connect_signal(state.live_signal_num)
919 set_neutral_color_from_signal(state_copy, chain.wb_effect, state.live_signal_num)
920 set_scale_parameters_if_needed(chain, width, height)
921 prepare_overlay_live(state_copy, chain, t, 1.0)
923 return chain.chain, prepare
924 elseif state.live_signal_num == STATIC_SIGNAL_NUM then -- Static picture.
925 local chain = static_chains[true]
926 local prepare = function()
927 prepare_overlay_live(state_copy, chain, t, 1.0) -- FIXME: Should this ever be here?
929 return chain.chain, prepare
935 -- We do not show overlays on the individual preview inputs.
936 -- The M/E preview matches what we'd put live by doing a transition, as always.
937 local show_overlay = false
938 if num == 1 then -- Preview.
939 if state.preview_signal_num == OVERLAY_SIGNAL_NUM then
940 num = state.live_signal_num + 2
941 show_overlay = not state.overlay_enabled
943 if state.transition_type ~= NO_TRANSITION then
944 num = state.transition_dst_signal + 2
947 num = state.preview_signal_num + 2
948 show_overlay = state.overlay_enabled and simple_signal_has_overlay(state.preview_signal_num)
952 -- Individual preview inputs (usually without overlay).
953 if is_plain_signal(num - 2) then
954 local signal_num = num - 2
955 local input_type = get_input_type(signals, signal_num)
956 local input_scale = needs_scale(signals, signal_num, width, height)
957 local chain = simple_chains[input_type][input_scale][show_overlay][false]
958 local prepare = function()
959 if input_type ~= "video" then
960 chain.input:connect_signal(signal_num)
962 set_neutral_color(chain.wb_effect, state_copy.neutral_colors[signal_num + 1])
963 set_scale_parameters_if_needed(chain, width, height)
964 prepare_overlay_static(chain, t)
966 return chain.chain, prepare
968 if num == SBS_SIGNAL_NUM + 2 then
969 local input0_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
970 local input1_type = get_input_type(signals, INPUT4_SIGNAL_NUM)
971 local chain = sbs_chains[input0_type][show_overlay][input1_type][false]
972 local prepare = function()
973 prepare_sbs_chain(state_copy, chain, 0.0, NO_TRANSITION, 0, SBS_SIGNAL_NUM, width, height, input_resolution)
975 return chain.chain, prepare
977 if num == STATIC_SIGNAL_NUM + 2 then
978 local chain = static_chains[false]
979 local prepare = function()
980 prepare_overlay_static(chain, t)
982 return chain.chain, prepare
984 if num == OVERLAY_SIGNAL_NUM + 2 then
985 local prepare = function()
986 -- prepare_overlay(overlay_chain_lq, t)
988 return overlay_chain_lq, prepare
992 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
994 return math.floor(x + 0.5)
997 function lerp(a, b, t)
998 return a + (b - a) * t
1001 function lerp_pos(a, b, t)
1003 x0 = lerp(a.x0, b.x0, t),
1004 y0 = lerp(a.y0, b.y0, t),
1005 x1 = lerp(a.x1, b.x1, t),
1006 y1 = lerp(a.y1, b.y1, t)
1010 function pos_from_top_left(x, y, width, height, screen_width, screen_height)
1011 local xs = screen_width / 1280.0
1012 local ys = screen_height / 720.0
1016 x1 = round(xs * (x + width)),
1017 y1 = round(ys * (y + height))
1021 function prepare_sbs_chain(state, chain, t, transition_type, src_signal, dst_signal, screen_width, screen_height, input_resolution)
1022 chain.input0.input:connect_signal(0)
1023 chain.input1.input:connect_signal(4)
1024 set_neutral_color(chain.input0.wb_effect, state.neutral_colors[1])
1025 set_neutral_color(chain.input1.wb_effect, state.neutral_colors[5])
1027 -- Both inputs are the same size (true side-by-side).
1028 local pos0 = pos_from_top_left(1280 - 616 - 16, 186, 616, 347, screen_width, screen_height)
1029 local pos1 = pos_from_top_left(16, 186, 616, 347, screen_width, screen_height)
1031 local pos_fs = { x0 = 0, y0 = 0, x1 = screen_width, y1 = screen_height }
1033 if transition_type == NO_TRANSITION then
1035 affine_param = { sx = 1.0, sy = 1.0, tx = 0.0, ty = 0.0 } -- Identity.
1037 -- Zooming to/from SBS view into or out of a single view.
1038 assert(transition_type == ZOOM_TRANSITION)
1039 local signal, real_t
1040 if src_signal == SBS_SIGNAL_NUM then
1044 assert(dst_signal == SBS_SIGNAL_NUM)
1049 if signal == INPUT0_SIGNAL_NUM then
1050 affine_param = find_affine_param(pos0, lerp_pos(pos0, pos_fs, real_t))
1051 elseif signal == INPUT4_SIGNAL_NUM then
1052 affine_param = find_affine_param(pos1, lerp_pos(pos1, pos_fs, real_t))
1056 -- NOTE: input_resolution is not 1-indexed, unlike usual Lua arrays.
1057 place_rectangle_with_affine(chain.input0.resample_effect, chain.input0.resize_effect, chain.input0.padding_effect, pos0, affine_param, screen_width, screen_height, input_resolution[0].width, input_resolution[0].height)
1058 place_rectangle_with_affine(chain.input1.resample_effect, chain.input1.resize_effect, chain.input1.padding_effect, pos1, affine_param, screen_width, screen_height, input_resolution[1].width, input_resolution[1].height)
1061 -- Find the transformation that changes the first rectangle to the second one.
1062 function find_affine_param(a, b)
1063 local sx = (b.x1 - b.x0) / (a.x1 - a.x0)
1064 local sy = (b.y1 - b.y0) / (a.y1 - a.y0)
1068 tx = b.x0 - a.x0 * sx,
1069 ty = b.y0 - a.y0 * sy
1073 function place_rectangle_with_affine(resample_effect, resize_effect, padding_effect, pos, aff, screen_width, screen_height, input_width, input_height)
1074 local x0 = pos.x0 * aff.sx + aff.tx
1075 local x1 = pos.x1 * aff.sx + aff.tx
1076 local y0 = pos.y0 * aff.sy + aff.ty
1077 local y1 = pos.y1 * aff.sy + aff.ty
1079 place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height)
1082 function place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height)
1088 padding_effect:set_int("width", screen_width)
1089 padding_effect:set_int("height", screen_height)
1092 if x0 > screen_width or x1 < 0.0 or y0 > screen_height or y1 < 0.0 then
1093 if resample_effect ~= nil then
1094 resample_effect:set_int("width", 1)
1095 resample_effect:set_int("height", 1)
1096 resample_effect:set_float("zoom_x", screen_width)
1097 resample_effect:set_float("zoom_y", screen_height)
1099 resize_effect:set_int("width", 1)
1100 resize_effect:set_int("height", 1)
1102 padding_effect:set_int("left", screen_width + 100)
1103 padding_effect:set_int("top", screen_height + 100)
1109 srcx0 = -x0 / (x1 - x0)
1113 srcy0 = -y0 / (y1 - y0)
1116 if x1 > screen_width then
1117 srcx1 = (screen_width - x0) / (x1 - x0)
1120 if y1 > screen_height then
1121 srcy1 = (screen_height - y0) / (y1 - y0)
1125 if resample_effect ~= nil then
1126 -- High-quality resampling.
1127 local x_subpixel_offset = x0 - math.floor(x0)
1128 local y_subpixel_offset = y0 - math.floor(y0)
1130 -- Resampling must be to an integral number of pixels. Round up,
1131 -- and then add an extra pixel so we have some leeway for the border.
1132 local width = math.ceil(x1 - x0) + 1
1133 local height = math.ceil(y1 - y0) + 1
1134 resample_effect:set_int("width", width)
1135 resample_effect:set_int("height", height)
1137 -- Correct the discrepancy with zoom. (This will leave a small
1138 -- excess edge of pixels and subpixels, which we'll correct for soon.)
1139 local zoom_x = (x1 - x0) / (width * (srcx1 - srcx0))
1140 local zoom_y = (y1 - y0) / (height * (srcy1 - srcy0))
1141 resample_effect:set_float("zoom_x", zoom_x)
1142 resample_effect:set_float("zoom_y", zoom_y)
1143 resample_effect:set_float("zoom_center_x", 0.0)
1144 resample_effect:set_float("zoom_center_y", 0.0)
1146 -- Padding must also be to a whole-pixel offset.
1147 padding_effect:set_int("left", math.floor(x0))
1148 padding_effect:set_int("top", math.floor(y0))
1150 -- Correct _that_ discrepancy by subpixel offset in the resampling.
1151 resample_effect:set_float("left", srcx0 * input_width - x_subpixel_offset / zoom_x)
1152 resample_effect:set_float("top", srcy0 * input_height - y_subpixel_offset / zoom_y)
1154 -- Finally, adjust the border so it is exactly where we want it.
1155 padding_effect:set_float("border_offset_left", x_subpixel_offset)
1156 padding_effect:set_float("border_offset_right", x1 - (math.floor(x0) + width))
1157 padding_effect:set_float("border_offset_top", y_subpixel_offset)
1158 padding_effect:set_float("border_offset_bottom", y1 - (math.floor(y0) + height))
1160 -- Lower-quality simple resizing.
1161 local width = round(x1 - x0)
1162 local height = round(y1 - y0)
1163 resize_effect:set_int("width", width)
1164 resize_effect:set_int("height", height)
1166 -- Padding must also be to a whole-pixel offset.
1167 padding_effect:set_int("left", math.floor(x0))
1168 padding_effect:set_int("top", math.floor(y0))
1172 function prepare_overlay_live(state, chain, t, extra_alpha_factor)
1173 if chain.overlay then
1174 local tt = calc_fade_progress(t, state.overlay_transition_start, state.overlay_transition_end)
1175 local overlay_alpha = state.overlay_alpha_src + tt * (state.overlay_alpha_dst - state.overlay_alpha_src)
1176 overlay_alpha = overlay_alpha * extra_alpha_factor
1177 --print("overlay_alpha=" .. overlay_alpha .. " [" .. state.overlay_alpha_src .. "," .. state.overlay_alpha_dst .. "]@" .. tt)
1178 if t > state.overlay_transition_end and state.overlay_alpha_dst == 0.0 then
1179 state.overlay_enabled = false -- Takes effect next frame.
1180 -- print("Turning off overlay")
1182 chain.overlay.multiply_effect:set_vec4("factor", overlay_alpha, overlay_alpha, overlay_alpha, overlay_alpha)
1186 function prepare_overlay_static(chain)
1187 if chain.overlay then
1188 chain.overlay.multiply_effect:set_vec4("factor", 1.0, 1.0, 1.0, 1.0)
1192 function set_neutral_color(effect, color)
1193 effect:set_vec3("neutral_color", color[1], color[2], color[3])
1196 function set_neutral_color_from_signal(state, effect, signal)
1197 if is_plain_signal(signal) then
1198 set_neutral_color(effect, state.neutral_colors[signal - INPUT0_SIGNAL_NUM + 1])
1202 function calc_zoom_progress(t)
1203 if t < state.transition_start then
1205 elseif t > state.transition_end then
1208 local tt = (t - state.transition_start) / (state.transition_end - state.transition_start)
1210 return math.sin(tt * 3.14159265358 * 0.5)
1214 function calc_fade_progress(t, transition_start, transition_end)
1215 local tt = (t - transition_start) / (transition_end - transition_start)
1218 elseif tt > 1.0 then
1222 -- Make the fade look maybe a tad more natural, by pumping it
1223 -- through a sigmoid function.
1224 tt = 10.0 * tt - 5.0
1225 tt = 1.0 / (1.0 + math.exp(-tt))
1231 { "Reload overlay", reload_cef },
1232 { "Disconnect IPTV", disconnect_iptv }