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,
27 preview_signal_num = 1
29 local NUM_CAMERAS = 6 -- Remember to update neutral_colors, too.
31 -- Valid values for live_signal_num and preview_signal_num.
32 local INPUT0_SIGNAL_NUM = 0
33 local INPUT1_SIGNAL_NUM = 1
34 local INPUT2_SIGNAL_NUM = 2
35 local INPUT3_SIGNAL_NUM = 3
36 local INPUT4_SIGNAL_NUM = 4
37 local INPUT5_SIGNAL_NUM = 5
38 local SBS_SIGNAL_NUM = NUM_CAMERAS
39 local STATIC_SIGNAL_NUM = NUM_CAMERAS + 1
40 local VIDEO_SIGNAL_NUM = NUM_CAMERAS + 2
42 state.neutral_colors[VIDEO_SIGNAL_NUM - INPUT0_SIGNAL_NUM + 1] = {0.5, 0.5, 0.5}
44 -- Preview-only signal showing the current signal with the overlay.
45 -- Not valid for live_signal_num!
46 local OVERLAY_SIGNAL_NUM = NUM_CAMERAS + 3
48 -- Valid values for transition_type. (Cuts are done directly, so they need no entry.)
49 local NO_TRANSITION = 0
50 local ZOOM_TRANSITION = 1
51 local FADE_TRANSITION = 2
53 -- Last width/height/frame rate for each channel, if we have it.
54 -- Note that unlike the values we get from Nageru, the resolution is per
55 -- frame and not per field, since we deinterlace.
56 local last_resolution = {}
58 local cef_path = Nageru.THEME_PATH:match("(.*)/nageru/")
59 local cef_input = HTMLInput.new("file://" .. cef_path .. "/score.html")
60 cef_input:execute_javascript_async("play()")
62 local bg_video = VideoInput.new(cef_path .. "/flow-720.mp4", Nageru.VIDEO_FORMAT_YCBCR)
63 -- local iptv_video = VideoInput.new("http://10.34.129.69:9060/1/v.flv", Nageru.VIDEO_FORMAT_YCBCR)
64 local iptv_video = VideoInput.new("http://home.samfundet.no/~sesse/videostuff", Nageru.VIDEO_FORMAT_YCBCR)
68 cef_input:execute_javascript_async("play()")
71 function disconnect_iptv()
72 iptv_video:disconnect()
75 -- Utility function to help creating many similar chains that can differ
76 -- in a free set of chosen parameters.
77 function make_cartesian_product(parms, callback)
78 return make_cartesian_product_internal(parms, callback, 1, {})
81 function make_cartesian_product_internal(parms, callback, index, args)
82 if index > #parms then
83 return callback(unpack(args))
86 for _, value in ipairs(parms[index]) do
88 ret[value] = make_cartesian_product_internal(parms, callback, index + 1, args)
93 -- An overlay with variable alpha.
94 function make_overlay(chain, base)
95 local image = chain:add_html_input(cef_input)
96 local multiply_effect = chain:add_effect(MultiplyEffect.new())
97 local overlay_effect = chain:add_effect(OverlayEffect.new(), base, multiply_effect)
100 multiply_effect = multiply_effect,
101 overlay_effect = overlay_effect
105 function possibly_make_overlay(has_overlay, chain, base)
106 if has_overlay == true then
107 return make_overlay(chain, base)
113 function make_fade_input(chain, signal, live, deint, scale, video)
114 local input, wb_effect, resample_effect, last
116 input = chain:add_video_input(iptv_video, false)
119 input = chain:add_live_input(false, deint)
120 input:connect_signal(signal)
123 input = chain:add_effect(ImageInput.new(cef_path .. "/nageru/dsn-bg.png"))
127 -- If we cared about this for the non-main inputs, we would have
128 -- checked hq here and invoked ResizeEffect instead.
130 resample_effect = chain:add_effect(ResampleEffect.new())
131 last = resample_effect
134 -- Make sure to put the white balance after the scaling (usually more efficient).
136 wb_effect = chain:add_effect(WhiteBalanceEffect.new())
142 wb_effect = wb_effect,
143 resample_effect = resample_effect,
148 -- A chain to fade between two inputs, of which either can be a picture
149 -- or a live input. In practice only used live, but we still support the
151 function make_fade_chain(input0_live, input0_deint, input0_scale, input0_video, input1_live, input1_deint, input1_scale, input1_video, has_overlay, hq)
152 local chain = EffectChain.new(16, 9)
154 local input0 = make_fade_input(chain, INPUT0_SIGNAL_NUM, input0_live, input0_deint, input0_scale, input0_video)
155 local input1 = make_fade_input(chain, INPUT1_SIGNAL_NUM, input1_live, input1_deint, input1_scale, input1_video)
157 -- If fading between two live inputs, the overlay is put on top.
158 -- If fading between the static picture and a live input,
159 -- the overlay is put on the live input.
161 if input0_live and not input1_live then
162 overlay = possibly_make_overlay(has_overlay, chain, input0.last)
164 input0.last = overlay.overlay_effect
166 elseif input1_live and not input0_live then
167 overlay = possibly_make_overlay(has_overlay, chain, input1.last)
169 input1.last = overlay.overlay_effect
173 local mix_effect = chain:add_effect(MixEffect.new(), input0.last, input1.last)
174 if input0_live and input1_live then
175 overlay = possibly_make_overlay(has_overlay, chain, mix_effect)
184 mix_effect = mix_effect,
189 -- Chains to fade between two inputs, in various configurations.
190 local fade_chains = make_cartesian_product({
191 {"video", "static", "live", "livedeint"}, -- input0_type
192 {true, false}, -- input0_scale
193 {"video", "static", "live", "livedeint"}, -- input1_type
194 {true, false}, -- input1_scale
195 {true, false}, -- has_overlay
197 }, function(input0_type, input0_scale, input1_type, input1_scale, has_overlay, hq)
198 local input0_live = (input0_type ~= "static")
199 local input1_live = (input1_type ~= "static")
200 local input0_deint = (input0_type == "livedeint")
201 local input1_deint = (input1_type == "livedeint")
202 local input0_video = (input0_type == "video")
203 local input1_video = (input1_type == "video")
204 return make_fade_chain(input0_live, input0_deint, input0_scale, input0_video, input1_live, input1_deint, input1_scale, input1_video, has_overlay, hq)
207 function make_sbs_input(chain, signal, deint, has_overlay, hq)
208 local input = chain:add_live_input(not deint, deint) -- Override bounce only if not deinterlacing.
209 input:connect_signal(signal)
211 local wb_effect = chain:add_effect(WhiteBalanceEffect.new()) -- Needs to be before the overlay.
215 overlay = make_overlay(chain, wb_effect)
218 local resample_effect = nil
219 local resize_effect = nil
221 resample_effect = chain:add_effect(ResampleEffect.new())
223 resize_effect = chain:add_effect(ResizeEffect.new())
226 local padding_effect = chain:add_effect(IntegralPaddingEffect.new())
230 wb_effect = wb_effect,
231 resample_effect = resample_effect,
232 resize_effect = resize_effect,
233 padding_effect = padding_effect,
238 -- Side-by-side chains.
239 function make_sbs_chain(input0_type, input0_overlay, input1_type, hq)
240 local chain = EffectChain.new(16, 9)
242 local bg = chain:add_video_input(bg_video, false)
244 local input0 = make_sbs_input(chain, INPUT0_SIGNAL_NUM, input0_type == "livedeint", input0_overlay, hq)
245 local input1 = make_sbs_input(chain, INPUT4_SIGNAL_NUM, input1_type == "livedeint", false, hq)
247 input0.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0)
248 input1.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0)
250 local i0 = chain:add_effect(OverlayEffect.new(), bg, input0.padding_effect)
251 chain:add_effect(OverlayEffect.new(), i0, input1.padding_effect)
258 overlay = input0.overlay -- May be nil.
263 -- Make all possible combinations of side-by-side chains.
264 local sbs_chains = make_cartesian_product({
265 {"live", "livedeint"}, -- input0_type
266 {true, false}, -- input0_overlay
267 {"live", "livedeint"}, -- input1_type
269 }, function(input0_type, input0_overlay, input1_type, hq)
270 return make_sbs_chain(input0_type, input0_overlay, input1_type, hq)
273 -- A chain to show a single input on screen.
274 function make_simple_chain(input_deint, input_video, input_scale, has_overlay, hq)
275 local chain = EffectChain.new(16, 9)
279 input = chain:add_video_input(iptv_video, false)
281 input = chain:add_live_input(false, input_deint)
282 input:connect_signal(0) -- First input card. Can be changed whenever you want.
285 local resample_effect, resize_effect
288 resample_effect = chain:add_effect(ResampleEffect.new())
290 resize_effect = chain:add_effect(ResizeEffect.new())
294 local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
295 local overlay = possibly_make_overlay(has_overlay, chain, wb_effect)
302 wb_effect = wb_effect,
303 resample_effect = resample_effect,
304 resize_effect = resize_effect,
309 -- Make all possible combinations of single-input chains.
310 local simple_chains = make_cartesian_product({
311 {"video", "live", "livedeint"}, -- input_type
312 {true, false}, -- input_scale
313 {true, false}, -- has_overlay
315 }, function(input_type, input_scale, has_overlay, hq)
316 local input_deint = (input_type == "livedeint")
317 local input_video = (input_type == "video")
318 return make_simple_chain(input_deint, input_video, input_scale, has_overlay, hq)
321 -- A chain to show a single static picture on screen. Never with HTML overlay.
322 local static_chains = make_cartesian_product({
325 local chain = EffectChain.new(16, 9)
326 local chain_input = chain:add_effect(ImageInput.new(cef_path .. "/nageru/dsn-bg.png"))
334 -- A chain to show the overlay and nothing more. LQ only,
335 -- since it is not a valid live signal.
336 local overlay_chain_lq = EffectChain.new(16, 9)
337 local overlay_chain_lq_input = overlay_chain_lq:add_html_input(cef_input)
338 overlay_chain_lq:finalize(false)
340 -- Used for indexing into the tables of chains.
341 function get_input_type(signals, signal_num)
342 if signal_num == STATIC_SIGNAL_NUM then
344 elseif signal_num == VIDEO_SIGNAL_NUM then
346 elseif signals:get_interlaced(signal_num) then
353 function needs_scale(signals, signal_num, width, height)
354 if signal_num == STATIC_SIGNAL_NUM then
355 -- We assume this is already correctly scaled at load time.
358 assert(is_plain_signal(signal_num))
359 return (signals:get_width(signal_num) ~= width or signals:get_height(signal_num) ~= height)
362 function set_scale_parameters_if_needed(chain_or_input, width, height)
363 if chain_or_input.resample_effect then
364 chain_or_input.resample_effect:set_int("width", width)
365 chain_or_input.resample_effect:set_int("height", height)
366 elseif chain_or_input.resize_effect then
367 chain_or_input.resize_effect:set_int("width", width)
368 chain_or_input.resize_effect:set_int("height", height)
373 -- Returns the number of outputs in addition to the live (0) and preview (1).
374 -- Called only once, at the start of the program.
375 function num_channels()
376 return NUM_CAMERAS + 4 -- sbs, static picture, video and overlay
379 function is_plain_signal(num)
380 return (num >= INPUT0_SIGNAL_NUM and num <= INPUT5_SIGNAL_NUM) or (num == VIDEO_SIGNAL_NUM)
383 -- Helper function to write e.g. “720p60”. The difference between this
384 -- and get_channel_resolution_raw() is that this one also can say that
385 -- there's no signal.
386 function get_channel_resolution(signal_num)
387 local res = last_resolution[signal_num]
388 if (not res) or not res.is_connected then
389 return "disconnected"
391 if res.height <= 0 then
394 if not res.has_signal then
395 if res.height == 525 then
396 -- Special mode for the USB3 cards.
399 return get_channel_resolution_raw(res) .. ", no signal"
401 return get_channel_resolution_raw(res)
405 -- Helper function to write e.g. “60” or “59.94”.
406 function get_frame_rate(res)
407 local nom = res.frame_rate_nom
408 local den = res.frame_rate_den
409 if nom % den == 0 then
412 return string.format("%.2f", nom / den)
416 -- Helper function to write e.g. “720p60”.
417 function get_channel_resolution_raw(res)
418 if res.interlaced then
419 return res.height .. "i" .. get_frame_rate(res)
421 return res.height .. "p" .. get_frame_rate(res)
426 -- Returns the name for each additional channel (starting from 2).
427 -- Called at the start of the program, and then each frame for live
428 -- channels in case they change resolution.
429 function channel_name(channel)
430 local signal_num = channel - 2
431 if signal_num == INPUT0_SIGNAL_NUM then
432 return "Main (" .. get_channel_resolution(signal_num) .. ")"
433 elseif signal_num == INPUT1_SIGNAL_NUM then
434 return "Secondary (" .. get_channel_resolution(signal_num) .. ")"
435 elseif signal_num == INPUT2_SIGNAL_NUM then
436 return "Goal L (" .. get_channel_resolution(signal_num) .. ")"
437 elseif signal_num == INPUT3_SIGNAL_NUM then
438 return "Goal R (" .. get_channel_resolution(signal_num) .. ")"
439 elseif signal_num == INPUT4_SIGNAL_NUM then
440 return "Commentators (" .. get_channel_resolution(signal_num) .. ")"
441 elseif signal_num == INPUT5_SIGNAL_NUM then
442 return "Laptop (" .. get_channel_resolution(signal_num) .. ")"
443 elseif signal_num == SBS_SIGNAL_NUM then
444 return "Side-by-side"
445 elseif signal_num == STATIC_SIGNAL_NUM then
446 return "Static picture"
447 elseif signal_num == VIDEO_SIGNAL_NUM then
449 elseif signal_num == OVERLAY_SIGNAL_NUM then
455 -- Returns, given a channel number, which signal it corresponds to (starting from 0).
456 -- Should return -1 if the channel does not correspond to a simple signal.
457 -- (The information is used for whether right-click on the channel should bring up
458 -- an input selector or not.)
459 -- Called once for each channel, at the start of the program.
460 -- Will never be called for live (0) or preview (1).
461 function channel_signal(channel)
462 if channel - 2 == VIDEO_SIGNAL_NUM then
463 return iptv_video:get_signal_num()
464 elseif is_plain_signal(channel - 2) then
472 -- Called every frame. Returns the color (if any) to paint around the given
473 -- channel. Returns a CSS color (typically to mark live and preview signals);
474 -- "transparent" is allowed.
475 -- Will never be called for live (0) or preview (1).
476 function channel_color(channel)
477 if state.transition_type ~= NO_TRANSITION then
478 if channel_involved_in(channel, state.transition_src_signal) or
479 channel_involved_in(channel, state.transition_dst_signal) then
483 if channel_involved_in(channel, state.live_signal_num) then
487 if channel_involved_in(channel, state.preview_signal_num) then
493 function channel_involved_in(channel, signal_num)
494 if is_plain_signal(signal_num) then
495 return channel == (signal_num + 2)
497 if signal_num == SBS_SIGNAL_NUM then
498 return is_sbs_participating_signal(channel - 2)
500 if signal_num == STATIC_SIGNAL_NUM then
501 return (channel == NUM_CAMERAS)
503 if signal_num == VIDEO_SIGNAL_NUM then
504 return (channel == NUM_CAMERAS + 1)
510 -- Returns if a given channel supports setting white balance (starting from 2).
511 -- Called only once for each channel, at the start of the program.
512 function supports_set_wb(channel)
513 return is_plain_signal(channel - 2)
517 -- Gets called with a new gray point when the white balance is changing.
518 -- The color is in linear light (not sRGB gamma).
519 function set_wb(channel, red, green, blue)
520 if is_plain_signal(channel - 2) then
521 state.neutral_colors[channel - 2 + 1] = { red, green, blue }
525 function finish_transitions(t)
526 if state.transition_type ~= NO_TRANSITION and t >= state.transition_end then
527 state.live_signal_num = state.transition_dst_signal
528 state.transition_type = NO_TRANSITION
531 -- Disable the overlay if it is no longer visible.
532 if state.overlay_enabled and t > state.overlay_transition_end and state.overlay_alpha_dst == 0.0 then
533 state.overlay_enabled = false
534 print("Turning off overlay")
538 function in_transition(t)
539 return t >= state.transition_start and t <= state.transition_end
542 function is_sbs_participating_signal(signal_num)
543 return signal_num == INPUT0_SIGNAL_NUM or signal_num == INPUT4_SIGNAL_NUM
546 function simple_signal_has_overlay(signal_num)
547 -- The commentator output has no overlay on it.
548 return signal_num ~= INPUT4_SIGNAL_NUM
552 -- Called every frame.
553 function get_transitions(t)
554 if state.preview_signal_num == OVERLAY_SIGNAL_NUM then
555 if t < state.overlay_transition_end then
559 if state.overlay_enabled then
560 return {"Overlay off", "", "Fade ovl out"}
562 return {"Overlay on", "", "Fade ovl in"}
566 if in_transition(t) then
567 -- Transition already in progress, the only thing we can do is really
568 -- cut to the preview. (TODO: Make an “abort” and/or “finish”, too?)
572 if state.live_signal_num == state.preview_signal_num then
573 -- No transitions possible.
577 if (is_plain_signal(state.live_signal_num) or state.live_signal_num == STATIC_SIGNAL_NUM) and
578 (is_plain_signal(state.preview_signal_num) or state.preview_signal_num == STATIC_SIGNAL_NUM) then
579 return {"Cut", "", "Fade"}
583 if state.live_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(state.preview_signal_num) then
584 return {"Cut", "Zoom in"}
585 elseif is_sbs_participating_signal(state.live_signal_num) and state.preview_signal_num == SBS_SIGNAL_NUM then
586 return {"Cut", "Zoom out"}
592 function swap_preview_live()
593 local temp = state.live_signal_num
594 state.live_signal_num = state.preview_signal_num
595 state.preview_signal_num = temp
598 function start_transition(type_, t, duration)
599 state.transition_start = t
600 state.transition_end = t + duration
601 state.transition_type = type_
602 state.transition_src_signal = state.live_signal_num
603 state.transition_dst_signal = state.preview_signal_num
608 -- Called when the user clicks a transition button.
609 function transition_clicked(num, t)
610 if state.preview_signal_num == OVERLAY_SIGNAL_NUM then
613 state.overlay_transition_start = -2.0
614 state.overlay_transition_end = -1.0
615 if state.overlay_enabled then
616 state.overlay_enabled = false
617 state.overlay_alpha_src = 0.0
618 state.overlay_alpha_dst = 0.0
620 state.overlay_enabled = true
621 state.overlay_alpha_src = 1.0
622 state.overlay_alpha_dst = 1.0
626 state.overlay_transition_start = t
627 state.overlay_transition_end = t + 1.0
628 if state.overlay_enabled then
629 state.overlay_alpha_src = 1.0
630 state.overlay_alpha_dst = 0.0
632 state.overlay_alpha_src = 0.0
633 state.overlay_alpha_dst = 1.0
635 state.overlay_enabled = true
642 if in_transition(t) then
643 -- Ongoing transition; finish it immediately before the cut.
644 finish_transitions(state.transition_end)
650 finish_transitions(t)
652 if state.live_signal_num == state.preview_signal_num then
657 if is_plain_signal(state.live_signal_num) and is_plain_signal(state.preview_signal_num) then
658 -- We can't zoom between these. Just make a cut.
659 io.write("Cutting from " .. state.live_signal_num .. " to " .. state.live_signal_num .. "\n")
664 if (state.live_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(state.preview_signal_num)) or
665 (state.preview_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(state.live_signal_num)) then
666 start_transition(ZOOM_TRANSITION, t, 1.0)
669 finish_transitions(t)
672 if (state.live_signal_num ~= state.preview_signal_num) and
673 (is_plain_signal(state.live_signal_num) or
674 state.live_signal_num == STATIC_SIGNAL_NUM) and
675 (is_plain_signal(state.preview_signal_num) or
676 state.preview_signal_num == STATIC_SIGNAL_NUM) then
677 start_transition(FADE_TRANSITION, t, 1.0)
679 -- Fades involving SBS are ignored (we have no chain for it).
685 function channel_clicked(num)
686 state.preview_signal_num = num
689 function get_fade_chain(state, signals, t, width, height, input_resolution)
690 local input0_type = get_input_type(signals, state.transition_src_signal)
691 local input0_scale = needs_scale(signals, state.transition_src_signal, width, height)
692 local input1_type = get_input_type(signals, state.transition_dst_signal)
693 local input1_scale = needs_scale(signals, state.transition_dst_signal, width, height)
694 local chain = fade_chains[input0_type][input0_scale][input1_type][input1_scale][state.overlay_enabled][true]
695 local prepare = function()
696 if input0_type == "live" or input0_type == "livedeint" then
697 chain.input0.input:connect_signal(state.transition_src_signal)
699 if input0_type ~= "static" then
700 set_neutral_color_from_signal(state, chain.input0.wb_effect, state.transition_src_signal)
702 set_scale_parameters_if_needed(chain.input0, width, height)
703 if input1_type == "live" or input1_type == "livedeint" then
704 chain.input1.input:connect_signal(state.transition_dst_signal)
706 if input1_type ~= "static" then
707 set_neutral_color_from_signal(state, chain.input1.wb_effect, state.transition_dst_signal)
709 set_scale_parameters_if_needed(chain.input1, width, height)
710 local tt = calc_fade_progress(t, state.transition_start, state.transition_end)
712 chain.mix_effect:set_float("strength_first", 1.0 - tt)
713 chain.mix_effect:set_float("strength_second", tt)
715 -- The commentator output has no overlay on it.
716 local extra_alpha_factor = 1.0
717 if not simple_signal_has_overlay(state.transition_src_signal) and
718 not simple_signal_has_overlay(state.transition_dst_signal) then
719 extra_alpha_factor = 0.0
720 elseif not simple_signal_has_overlay(state.transition_src_signal) then
721 extra_alpha_factor = tt
722 elseif not simple_signal_has_overlay(state.transition_dst_signal) then
723 extra_alpha_factor = 1.0 - tt
725 prepare_overlay_live(state, chain, t, extra_alpha_factor)
727 return chain.chain, prepare
730 -- SBS code (live_signal_num == SBS_SIGNAL_NUM, or in a transition to/from it).
731 function get_sbs_chain(state, signals)
732 local input0_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
733 local input1_type = get_input_type(signals, INPUT4_SIGNAL_NUM)
734 return sbs_chains[input0_type][state.overlay_enabled][input1_type][true]
737 local last_rate = 0.0
740 -- Called every frame. Get the chain for displaying at input <num>,
741 -- where 0 is live, 1 is preview, 2 is the first channel to display
742 -- in the bottom bar, and so on up to num_channels()+1. t is the
743 -- current time in seconds. width and height are the dimensions of
744 -- the output, although you can ignore them if you don't need them
745 -- (they're useful if you want to e.g. know what to resample by).
747 -- <signals> is basically an exposed InputState, which you can use to
748 -- query for information about the signals at the point of the current
749 -- frame. In particular, you can call get_width() and get_height()
750 -- for any signal number, and use that to e.g. assist in chain selection.
752 -- You should return two objects; the chain itself, and then a
753 -- function (taking no parameters) that is run just before rendering.
754 -- The function needs to call connect_signal on any inputs, so that
755 -- it gets updated video data for the given frame. (You are allowed
756 -- to switch which input your input is getting from between frames,
757 -- but not calling connect_signal results in undefined behavior.)
758 -- If you want to change any parameters in the chain, this is also
761 -- NOTE: The chain returned must be finalized with the Y'CbCr flag
762 -- if and only if num==0.
763 function get_chain(num, t, width, height, signals)
764 local input_resolution = {}
765 for signal_num=0,(NUM_CAMERAS-1) do
767 width = signals:get_width(signal_num),
768 height = signals:get_height(signal_num),
769 interlaced = signals:get_interlaced(signal_num),
770 has_signal = signals:get_has_signal(signal_num),
771 is_connected = signals:get_is_connected(signal_num),
772 frame_rate_nom = signals:get_frame_rate_nom(signal_num),
773 frame_rate_den = signals:get_frame_rate_den(signal_num)
776 if res.interlaced then
777 -- Convert height from frame height to field height.
778 -- (Needed for e.g. place_rectangle.)
779 res.height = res.height * 2
781 -- Show field rate instead of frame rate; really for cosmetics only
782 -- (and actually contrary to EBU recommendations, although in line
783 -- with typical user expectations).
784 res.frame_rate_nom = res.frame_rate_nom * 2
787 input_resolution[signal_num] = res
789 last_resolution = input_resolution
791 -- Make a (semi-shallow) copy of the current state, so that the returned prepare function
792 -- is unaffected by state changes made by the UI before it is rendered.
793 local state_copy = {}
794 for key, value in pairs(state) do
795 state_copy[key] = value
797 for key, value in pairs(state.neutral_colors) do
798 state_copy.neutral_colors[key] = value
801 -- Save some CPU time if we're not having SBS on live.
803 if state.live_signal_num == SBS_SIGNAL_NUM or
804 state.preview_signal_num == SBS_SIGNAL_NUM or
805 state.transition_type == ZOOM_TRANSITION then
810 if new_rate ~= last_rate then
811 -- Avoid waking up the video thread (which may be sleeping) if the rate is the same.
812 bg_video:change_rate(new_rate)
816 if num == 0 then -- Live.
817 -- See if we're in a transition.
818 finish_transitions(t)
819 if state.transition_type == ZOOM_TRANSITION then
820 -- Transition in or out of SBS.
821 local chain = get_sbs_chain(state_copy, signals)
822 local prepare = function()
823 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)
824 prepare_overlay_live(state_copy, chain, t, 1.0)
826 return chain.chain, prepare
827 elseif state.transition_type == NO_TRANSITION and state.live_signal_num == SBS_SIGNAL_NUM then
829 local chain = get_sbs_chain(state_copy, signals)
830 local prepare = function()
831 prepare_sbs_chain(state_copy, chain, 0.0, NO_TRANSITION, 0, SBS_SIGNAL_NUM, width, height, input_resolution)
832 prepare_overlay_live(state_copy, chain, t, 1.0)
834 return chain.chain, prepare
835 elseif state.transition_type == FADE_TRANSITION then
836 return get_fade_chain(state_copy, signals, t, width, height, input_resolution)
837 elseif is_plain_signal(state.live_signal_num) then
838 local input_type = get_input_type(signals, state.live_signal_num)
839 local input_scale = needs_scale(signals, state.live_signal_num, width, height)
840 local overlay_really_enabled = state.overlay_enabled and simple_signal_has_overlay(state.live_signal_num)
841 local chain = simple_chains[input_type][input_scale][overlay_really_enabled][true]
842 local prepare = function()
843 if input_type ~= "video" then
844 chain.input:connect_signal(state.live_signal_num)
846 set_neutral_color_from_signal(state_copy, chain.wb_effect, state.live_signal_num)
847 set_scale_parameters_if_needed(chain, width, height)
848 prepare_overlay_live(state_copy, chain, t, 1.0)
850 return chain.chain, prepare
851 elseif state.live_signal_num == STATIC_SIGNAL_NUM then -- Static picture.
852 local chain = static_chains[true]
853 local prepare = function()
854 prepare_overlay_live(state_copy, chain, t, 1.0) -- FIXME: Should this ever be here?
856 return chain.chain, prepare
862 -- We do not show overlays on the individual preview inputs.
863 -- The M/E preview matches what we'd put live by doing a transition, as always.
864 local show_overlay = false
865 if num == 1 then -- Preview.
866 if state.preview_signal_num == OVERLAY_SIGNAL_NUM then
867 num = state.live_signal_num + 2
868 show_overlay = not state.overlay_enabled
870 if state.transition_type ~= NO_TRANSITION then
871 num = state.transition_dst_signal + 2
874 num = state.preview_signal_num + 2
875 show_overlay = state.overlay_enabled and simple_signal_has_overlay(state.preview_signal_num)
879 -- Individual preview inputs (usually without overlay).
880 if is_plain_signal(num - 2) then
881 local signal_num = num - 2
882 local input_type = get_input_type(signals, signal_num)
883 local input_scale = needs_scale(signals, signal_num, width, height)
884 local chain = simple_chains[input_type][input_scale][show_overlay][false]
885 local prepare = function()
886 if input_type ~= "video" then
887 chain.input:connect_signal(signal_num)
889 set_neutral_color(chain.wb_effect, state_copy.neutral_colors[signal_num + 1])
890 set_scale_parameters_if_needed(chain, width, height)
891 prepare_overlay_static(chain, t)
893 return chain.chain, prepare
895 if num == SBS_SIGNAL_NUM + 2 then
896 local input0_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
897 local input1_type = get_input_type(signals, INPUT4_SIGNAL_NUM)
898 local chain = sbs_chains[input0_type][show_overlay][input1_type][false]
899 local prepare = function()
900 prepare_sbs_chain(state_copy, chain, 0.0, NO_TRANSITION, 0, SBS_SIGNAL_NUM, width, height, input_resolution)
902 return chain.chain, prepare
904 if num == STATIC_SIGNAL_NUM + 2 then
905 local chain = static_chains[false]
906 local prepare = function()
907 prepare_overlay_static(chain, t)
909 return chain.chain, prepare
911 if num == OVERLAY_SIGNAL_NUM + 2 then
912 local prepare = function()
913 -- prepare_overlay(overlay_chain_lq, t)
915 return overlay_chain_lq, prepare
919 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
921 return math.floor(x + 0.5)
924 function lerp(a, b, t)
925 return a + (b - a) * t
928 function lerp_pos(a, b, t)
930 x0 = lerp(a.x0, b.x0, t),
931 y0 = lerp(a.y0, b.y0, t),
932 x1 = lerp(a.x1, b.x1, t),
933 y1 = lerp(a.y1, b.y1, t)
937 function pos_from_top_left(x, y, width, height, screen_width, screen_height)
938 local xs = screen_width / 1280.0
939 local ys = screen_height / 720.0
943 x1 = round(xs * (x + width)),
944 y1 = round(ys * (y + height))
948 function prepare_sbs_chain(state, chain, t, transition_type, src_signal, dst_signal, screen_width, screen_height, input_resolution)
949 chain.input0.input:connect_signal(0)
950 chain.input1.input:connect_signal(4)
951 set_neutral_color(chain.input0.wb_effect, state.neutral_colors[1])
952 set_neutral_color(chain.input1.wb_effect, state.neutral_colors[5])
954 -- Both inputs are the same size (true side-by-side).
955 local pos0 = pos_from_top_left(1280 - 616 - 16, 186, 616, 347, screen_width, screen_height)
956 local pos1 = pos_from_top_left(16, 186, 616, 347, screen_width, screen_height)
958 local pos_fs = { x0 = 0, y0 = 0, x1 = screen_width, y1 = screen_height }
960 if transition_type == NO_TRANSITION then
962 affine_param = { sx = 1.0, sy = 1.0, tx = 0.0, ty = 0.0 } -- Identity.
964 -- Zooming to/from SBS view into or out of a single view.
965 assert(transition_type == ZOOM_TRANSITION)
967 if src_signal == SBS_SIGNAL_NUM then
971 assert(dst_signal == SBS_SIGNAL_NUM)
976 if signal == INPUT0_SIGNAL_NUM then
977 affine_param = find_affine_param(pos0, lerp_pos(pos0, pos_fs, real_t))
978 elseif signal == INPUT4_SIGNAL_NUM then
979 affine_param = find_affine_param(pos1, lerp_pos(pos1, pos_fs, real_t))
983 -- NOTE: input_resolution is not 1-indexed, unlike usual Lua arrays.
984 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)
985 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)
988 -- Find the transformation that changes the first rectangle to the second one.
989 function find_affine_param(a, b)
990 local sx = (b.x1 - b.x0) / (a.x1 - a.x0)
991 local sy = (b.y1 - b.y0) / (a.y1 - a.y0)
995 tx = b.x0 - a.x0 * sx,
996 ty = b.y0 - a.y0 * sy
1000 function place_rectangle_with_affine(resample_effect, resize_effect, padding_effect, pos, aff, screen_width, screen_height, input_width, input_height)
1001 local x0 = pos.x0 * aff.sx + aff.tx
1002 local x1 = pos.x1 * aff.sx + aff.tx
1003 local y0 = pos.y0 * aff.sy + aff.ty
1004 local y1 = pos.y1 * aff.sy + aff.ty
1006 place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height)
1009 function place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height)
1015 padding_effect:set_int("width", screen_width)
1016 padding_effect:set_int("height", screen_height)
1019 if x0 > screen_width or x1 < 0.0 or y0 > screen_height or y1 < 0.0 then
1020 if resample_effect ~= nil then
1021 resample_effect:set_int("width", 1)
1022 resample_effect:set_int("height", 1)
1023 resample_effect:set_float("zoom_x", screen_width)
1024 resample_effect:set_float("zoom_y", screen_height)
1026 resize_effect:set_int("width", 1)
1027 resize_effect:set_int("height", 1)
1029 padding_effect:set_int("left", screen_width + 100)
1030 padding_effect:set_int("top", screen_height + 100)
1036 srcx0 = -x0 / (x1 - x0)
1040 srcy0 = -y0 / (y1 - y0)
1043 if x1 > screen_width then
1044 srcx1 = (screen_width - x0) / (x1 - x0)
1047 if y1 > screen_height then
1048 srcy1 = (screen_height - y0) / (y1 - y0)
1052 if resample_effect ~= nil then
1053 -- High-quality resampling.
1054 local x_subpixel_offset = x0 - math.floor(x0)
1055 local y_subpixel_offset = y0 - math.floor(y0)
1057 -- Resampling must be to an integral number of pixels. Round up,
1058 -- and then add an extra pixel so we have some leeway for the border.
1059 local width = math.ceil(x1 - x0) + 1
1060 local height = math.ceil(y1 - y0) + 1
1061 resample_effect:set_int("width", width)
1062 resample_effect:set_int("height", height)
1064 -- Correct the discrepancy with zoom. (This will leave a small
1065 -- excess edge of pixels and subpixels, which we'll correct for soon.)
1066 local zoom_x = (x1 - x0) / (width * (srcx1 - srcx0))
1067 local zoom_y = (y1 - y0) / (height * (srcy1 - srcy0))
1068 resample_effect:set_float("zoom_x", zoom_x)
1069 resample_effect:set_float("zoom_y", zoom_y)
1070 resample_effect:set_float("zoom_center_x", 0.0)
1071 resample_effect:set_float("zoom_center_y", 0.0)
1073 -- Padding must also be to a whole-pixel offset.
1074 padding_effect:set_int("left", math.floor(x0))
1075 padding_effect:set_int("top", math.floor(y0))
1077 -- Correct _that_ discrepancy by subpixel offset in the resampling.
1078 resample_effect:set_float("left", srcx0 * input_width - x_subpixel_offset / zoom_x)
1079 resample_effect:set_float("top", srcy0 * input_height - y_subpixel_offset / zoom_y)
1081 -- Finally, adjust the border so it is exactly where we want it.
1082 padding_effect:set_float("border_offset_left", x_subpixel_offset)
1083 padding_effect:set_float("border_offset_right", x1 - (math.floor(x0) + width))
1084 padding_effect:set_float("border_offset_top", y_subpixel_offset)
1085 padding_effect:set_float("border_offset_bottom", y1 - (math.floor(y0) + height))
1087 -- Lower-quality simple resizing.
1088 local width = round(x1 - x0)
1089 local height = round(y1 - y0)
1090 resize_effect:set_int("width", width)
1091 resize_effect:set_int("height", height)
1093 -- Padding must also be to a whole-pixel offset.
1094 padding_effect:set_int("left", math.floor(x0))
1095 padding_effect:set_int("top", math.floor(y0))
1099 function prepare_overlay_live(state, chain, t, extra_alpha_factor)
1100 if chain.overlay then
1101 local tt = calc_fade_progress(t, state.overlay_transition_start, state.overlay_transition_end)
1102 local overlay_alpha = state.overlay_alpha_src + tt * (state.overlay_alpha_dst - state.overlay_alpha_src)
1103 overlay_alpha = overlay_alpha * extra_alpha_factor
1104 --print("overlay_alpha=" .. overlay_alpha .. " [" .. state.overlay_alpha_src .. "," .. state.overlay_alpha_dst .. "]@" .. tt)
1105 if t > state.overlay_transition_end and state.overlay_alpha_dst == 0.0 then
1106 state.overlay_enabled = false -- Takes effect next frame.
1107 -- print("Turning off overlay")
1109 chain.overlay.multiply_effect:set_vec4("factor", overlay_alpha, overlay_alpha, overlay_alpha, overlay_alpha)
1113 function prepare_overlay_static(chain)
1114 if chain.overlay then
1115 chain.overlay.multiply_effect:set_vec4("factor", 1.0, 1.0, 1.0, 1.0)
1119 function set_neutral_color(effect, color)
1120 effect:set_vec3("neutral_color", color[1], color[2], color[3])
1123 function set_neutral_color_from_signal(state, effect, signal)
1124 if is_plain_signal(signal) then
1125 set_neutral_color(effect, state.neutral_colors[signal - INPUT0_SIGNAL_NUM + 1])
1129 function calc_zoom_progress(t)
1130 if t < state.transition_start then
1132 elseif t > state.transition_end then
1135 local tt = (t - state.transition_start) / (state.transition_end - state.transition_start)
1137 return math.sin(tt * 3.14159265358 * 0.5)
1141 function calc_fade_progress(t, transition_start, transition_end)
1142 local tt = (t - transition_start) / (transition_end - transition_start)
1145 elseif tt > 1.0 then
1149 -- Make the fade look maybe a tad more natural, by pumping it
1150 -- through a sigmoid function.
1151 tt = 10.0 * tt - 5.0
1152 tt = 1.0 / (1.0 + math.exp(-tt))
1158 { "Reload overlay", reload_cef },
1159 { "Disconnect IPTV", disconnect_iptv }