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,
30 stinger_save_overlay = false,
33 preview_signal_num = 1
35 local NUM_CAMERAS = 6 -- Remember to update neutral_colors, too.
37 -- Valid values for live_signal_num and preview_signal_num.
38 local INPUT0_SIGNAL_NUM = 0
39 local INPUT1_SIGNAL_NUM = 1
40 local INPUT2_SIGNAL_NUM = 2
41 local INPUT3_SIGNAL_NUM = 3
42 local INPUT4_SIGNAL_NUM = 4
43 local INPUT5_SIGNAL_NUM = 5
44 local SBS_SIGNAL_NUM = NUM_CAMERAS
45 local STATIC_SIGNAL_NUM = NUM_CAMERAS + 1
46 local VIDEO_SIGNAL_NUM = NUM_CAMERAS + 2
48 state.neutral_colors[VIDEO_SIGNAL_NUM - INPUT0_SIGNAL_NUM + 1] = {0.5, 0.5, 0.5}
50 -- Preview-only signal showing the current signal with the overlay.
51 -- Not valid for live_signal_num!
52 local OVERLAY_SIGNAL_NUM = NUM_CAMERAS + 3
54 -- Valid values for transition_type. (Cuts are done directly, so they need no entry.)
55 local NO_TRANSITION = 0
56 local ZOOM_TRANSITION = 1
57 local FADE_TRANSITION = 2
59 -- Last width/height/frame rate for each channel, if we have it.
60 -- Note that unlike the values we get from Nageru, the resolution is per
61 -- frame and not per field, since we deinterlace.
62 local last_resolution = {}
64 local cef_path = Nageru.THEME_PATH:match("(.*)/nageru/")
65 local cef_input = HTMLInput.new("file://" .. cef_path .. "/score.html")
66 cef_input:execute_javascript_async("play()")
68 local bg_video = VideoInput.new(cef_path .. "/flow-720.mp4", Nageru.VIDEO_FORMAT_YCBCR)
69 -- local iptv_video = VideoInput.new("http://10.34.129.69:9060/1/v.flv", Nageru.VIDEO_FORMAT_YCBCR)
70 local iptv_video = VideoInput.new("http://home.samfundet.no/~sesse/videostuff", Nageru.VIDEO_FORMAT_YCBCR)
74 cef_input:execute_javascript_async("play()")
77 function disconnect_iptv()
78 iptv_video:disconnect()
81 -- Utility function to help creating many similar chains that can differ
82 -- in a free set of chosen parameters.
83 function make_cartesian_product(parms, callback)
84 return make_cartesian_product_internal(parms, callback, 1, {})
87 function make_cartesian_product_internal(parms, callback, index, args)
88 if index > #parms then
89 return callback(unpack(args))
92 for _, value in ipairs(parms[index]) do
94 ret[value] = make_cartesian_product_internal(parms, callback, index + 1, args)
99 -- An overlay with variable alpha.
100 function make_overlay(chain, base)
101 local image = chain:add_html_input(cef_input)
102 local multiply_effect = chain:add_effect(MultiplyEffect.new())
103 local overlay_effect = chain:add_effect(OverlayEffect.new(), base, multiply_effect)
106 multiply_effect = multiply_effect,
107 overlay_effect = overlay_effect
111 function possibly_make_overlay(has_overlay, chain, base)
112 if has_overlay == true then
113 return make_overlay(chain, base)
119 function make_fade_input(chain, signal, live, deint, scale, video)
120 local input, wb_effect, resample_effect, last
122 input = chain:add_video_input(iptv_video, false)
125 input = chain:add_live_input(false, deint)
126 input:connect_signal(signal)
129 input = chain:add_effect(ImageInput.new(cef_path .. "/nageru/dsn-bg.png"))
133 -- If we cared about this for the non-main inputs, we would have
134 -- checked hq here and invoked ResizeEffect instead.
136 resample_effect = chain:add_effect(ResampleEffect.new())
137 last = resample_effect
140 -- Make sure to put the white balance after the scaling (usually more efficient).
142 wb_effect = chain:add_effect(WhiteBalanceEffect.new())
148 wb_effect = wb_effect,
149 resample_effect = resample_effect,
154 -- A chain to fade between two inputs, of which either can be a picture
155 -- or a live input. In practice only used live, but we still support the
157 function make_fade_chain(input0_live, input0_deint, input0_scale, input0_video, input1_live, input1_deint, input1_scale, input1_video, has_overlay, hq)
158 local chain = EffectChain.new(16, 9)
160 local input0 = make_fade_input(chain, INPUT0_SIGNAL_NUM, input0_live, input0_deint, input0_scale, input0_video)
161 local input1 = make_fade_input(chain, INPUT1_SIGNAL_NUM, input1_live, input1_deint, input1_scale, input1_video)
163 -- If fading between two live inputs, the overlay is put on top.
164 -- If fading between the static picture and a live input,
165 -- the overlay is put on the live input.
167 if input0_live and not input1_live then
168 overlay = possibly_make_overlay(has_overlay, chain, input0.last)
170 input0.last = overlay.overlay_effect
172 elseif input1_live and not input0_live then
173 overlay = possibly_make_overlay(has_overlay, chain, input1.last)
175 input1.last = overlay.overlay_effect
179 local mix_effect = chain:add_effect(MixEffect.new(), input0.last, input1.last)
180 if input0_live and input1_live then
181 overlay = possibly_make_overlay(has_overlay, chain, mix_effect)
190 mix_effect = mix_effect,
195 -- Chains to fade between two inputs, in various configurations.
196 local fade_chains = make_cartesian_product({
197 {"video", "static", "live", "livedeint"}, -- input0_type
198 {true, false}, -- input0_scale
199 {"video", "static", "live", "livedeint"}, -- input1_type
200 {true, false}, -- input1_scale
201 {true, false}, -- has_overlay
203 }, function(input0_type, input0_scale, input1_type, input1_scale, has_overlay, hq)
204 local input0_live = (input0_type ~= "static")
205 local input1_live = (input1_type ~= "static")
206 local input0_deint = (input0_type == "livedeint")
207 local input1_deint = (input1_type == "livedeint")
208 local input0_video = (input0_type == "video")
209 local input1_video = (input1_type == "video")
210 return make_fade_chain(input0_live, input0_deint, input0_scale, input0_video, input1_live, input1_deint, input1_scale, input1_video, has_overlay, hq)
213 function make_sbs_input(chain, signal, deint, has_overlay, hq)
214 local input = chain:add_live_input(not deint, deint) -- Override bounce only if not deinterlacing.
215 input:connect_signal(signal)
217 local wb_effect = chain:add_effect(WhiteBalanceEffect.new()) -- Needs to be before the overlay.
221 overlay = make_overlay(chain, wb_effect)
224 local resample_effect = nil
225 local resize_effect = nil
227 resample_effect = chain:add_effect(ResampleEffect.new())
229 resize_effect = chain:add_effect(ResizeEffect.new())
232 local padding_effect = chain:add_effect(IntegralPaddingEffect.new())
236 wb_effect = wb_effect,
237 resample_effect = resample_effect,
238 resize_effect = resize_effect,
239 padding_effect = padding_effect,
244 -- Side-by-side chains.
245 function make_sbs_chain(input0_type, input0_overlay, input1_type, hq)
246 local chain = EffectChain.new(16, 9)
248 local bg = chain:add_video_input(bg_video, false)
250 local input0 = make_sbs_input(chain, INPUT0_SIGNAL_NUM, input0_type == "livedeint", input0_overlay, hq)
251 local input1 = make_sbs_input(chain, INPUT4_SIGNAL_NUM, input1_type == "livedeint", false, hq)
253 input0.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0)
254 input1.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0)
256 local i0 = chain:add_effect(OverlayEffect.new(), bg, input0.padding_effect)
257 chain:add_effect(OverlayEffect.new(), i0, input1.padding_effect)
264 overlay = input0.overlay -- May be nil.
269 -- Make all possible combinations of side-by-side chains.
270 local sbs_chains = make_cartesian_product({
271 {"live", "livedeint"}, -- input0_type
272 {true, false}, -- input0_overlay
273 {"live", "livedeint"}, -- input1_type
275 }, function(input0_type, input0_overlay, input1_type, hq)
276 return make_sbs_chain(input0_type, input0_overlay, input1_type, hq)
279 function make_simple_chain_no_finalize(input_deint, input_video, input_scale, has_overlay, hq)
280 local chain = EffectChain.new(16, 9)
284 input = chain:add_video_input(iptv_video, false)
286 input = chain:add_live_input(false, input_deint)
287 input:connect_signal(0) -- First input card. Can be changed whenever you want.
290 local resample_effect, resize_effect
293 resample_effect = chain:add_effect(ResampleEffect.new())
295 resize_effect = chain:add_effect(ResizeEffect.new())
299 local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
300 local overlay = possibly_make_overlay(has_overlay, chain, wb_effect)
305 wb_effect = wb_effect,
306 resample_effect = resample_effect,
307 resize_effect = resize_effect,
312 -- A chain to show a single input on screen.
313 function make_simple_chain(input_deint, input_video, input_scale, has_overlay, hq)
314 local chain = make_simple_chain_no_finalize(input_deint, input_video, input_scale, has_overlay, hq)
315 chain.chain:finalize(hq)
319 -- Make all possible combinations of single-input chains.
320 local simple_chains = make_cartesian_product({
321 {"video", "live", "livedeint"}, -- input_type
322 {true, false}, -- input_scale
323 {true, false}, -- has_overlay
325 }, function(input_type, input_scale, has_overlay, hq)
326 local input_deint = (input_type == "livedeint")
327 local input_video = (input_type == "video")
328 return make_simple_chain(input_deint, input_video, input_scale, has_overlay, hq)
331 -- A chain to show a single input on screen, with a stinger on top.
332 function make_stinger_chain(input_deint, input_video, input_scale, has_overlay, stinger_frame, hq)
333 local chain = make_simple_chain_no_finalize(input_deint, input_video, input_scale, has_overlay, hq)
334 local filename = cef_path .. "/stinger/blur" .. string.rep("0", 3 - string.len(tostring(stinger_frame))) .. stinger_frame .. ".png"
338 last = chain.overlay.overlay_effect
340 last = chain.wb_effect
342 local input = chain.chain:add_effect(ImageInput.new(filename))
343 chain.chain:add_effect(OverlayEffect.new(), last, input)
344 chain.chain:finalize(hq)
348 -- Single-input chains with various stinger frames on top.
349 local stinger_chains = make_cartesian_product({
350 {"video", "live", "livedeint"}, -- input_type
351 {true, false}, -- input_scale
352 {true, false}, -- has_overlay
353 {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
355 }, function(input_type, input_scale, has_overlay, stinger_frame, hq)
356 local input_deint = (input_type == "livedeint")
357 local input_video = (input_type == "video")
358 return make_stinger_chain(input_deint, input_video, input_scale, has_overlay, stinger_frame, hq)
361 -- A chain to show a single static picture on screen. Never with HTML overlay.
362 local static_chains = make_cartesian_product({
365 local chain = EffectChain.new(16, 9)
366 local chain_input = chain:add_effect(ImageInput.new(cef_path .. "/nageru/dsn-bg.png"))
374 -- A chain to show the overlay and nothing more. LQ only,
375 -- since it is not a valid live signal.
376 local overlay_chain_lq = EffectChain.new(16, 9)
377 local overlay_chain_lq_input = overlay_chain_lq:add_html_input(cef_input)
378 overlay_chain_lq:finalize(false)
380 -- Used for indexing into the tables of chains.
381 function get_input_type(signals, signal_num)
382 if signal_num == STATIC_SIGNAL_NUM then
384 elseif signal_num == VIDEO_SIGNAL_NUM then
386 elseif signals:get_interlaced(signal_num) then
393 function needs_scale(signals, signal_num, width, height)
394 if signal_num == STATIC_SIGNAL_NUM then
395 -- We assume this is already correctly scaled at load time.
398 assert(is_plain_signal(signal_num))
399 return (signals:get_width(signal_num) ~= width or signals:get_height(signal_num) ~= height)
402 function set_scale_parameters_if_needed(chain_or_input, width, height)
403 if chain_or_input.resample_effect then
404 chain_or_input.resample_effect:set_int("width", width)
405 chain_or_input.resample_effect:set_int("height", height)
406 elseif chain_or_input.resize_effect then
407 chain_or_input.resize_effect:set_int("width", width)
408 chain_or_input.resize_effect:set_int("height", height)
413 -- Returns the number of outputs in addition to the live (0) and preview (1).
414 -- Called only once, at the start of the program.
415 function num_channels()
416 return NUM_CAMERAS + 4 -- sbs, static picture, video and overlay
419 function is_plain_signal(num)
420 return (num >= INPUT0_SIGNAL_NUM and num <= INPUT5_SIGNAL_NUM) or (num == VIDEO_SIGNAL_NUM)
423 -- Helper function to write e.g. “720p60”. The difference between this
424 -- and get_channel_resolution_raw() is that this one also can say that
425 -- there's no signal.
426 function get_channel_resolution(signal_num)
427 local res = last_resolution[signal_num]
428 if (not res) or not res.is_connected then
429 return "disconnected"
431 if res.height <= 0 then
434 if not res.has_signal then
435 if res.height == 525 then
436 -- Special mode for the USB3 cards.
439 return get_channel_resolution_raw(res) .. ", no signal"
441 return get_channel_resolution_raw(res)
445 -- Helper function to write e.g. “60” or “59.94”.
446 function get_frame_rate(res)
447 local nom = res.frame_rate_nom
448 local den = res.frame_rate_den
449 if nom % den == 0 then
452 return string.format("%.2f", nom / den)
456 -- Helper function to write e.g. “720p60”.
457 function get_channel_resolution_raw(res)
458 if res.interlaced then
459 return res.height .. "i" .. get_frame_rate(res)
461 return res.height .. "p" .. get_frame_rate(res)
466 -- Returns the name for each additional channel (starting from 2).
467 -- Called at the start of the program, and then each frame for live
468 -- channels in case they change resolution.
469 function channel_name(channel)
470 local signal_num = channel - 2
471 if signal_num == INPUT0_SIGNAL_NUM then
472 return "Main (" .. get_channel_resolution(signal_num) .. ")"
473 elseif signal_num == INPUT1_SIGNAL_NUM then
474 return "Secondary (" .. get_channel_resolution(signal_num) .. ")"
475 elseif signal_num == INPUT2_SIGNAL_NUM then
476 return "Goal L (" .. get_channel_resolution(signal_num) .. ")"
477 elseif signal_num == INPUT3_SIGNAL_NUM then
478 return "Goal R (" .. get_channel_resolution(signal_num) .. ")"
479 elseif signal_num == INPUT4_SIGNAL_NUM then
480 return "Commentators (" .. get_channel_resolution(signal_num) .. ")"
481 elseif signal_num == INPUT5_SIGNAL_NUM then
482 return "Laptop (" .. get_channel_resolution(signal_num) .. ")"
483 elseif signal_num == SBS_SIGNAL_NUM then
484 return "Side-by-side"
485 elseif signal_num == STATIC_SIGNAL_NUM then
486 return "Static picture"
487 elseif signal_num == VIDEO_SIGNAL_NUM then
489 elseif signal_num == OVERLAY_SIGNAL_NUM then
495 -- Returns, given a channel number, which signal it corresponds to (starting from 0).
496 -- Should return -1 if the channel does not correspond to a simple signal.
497 -- (The information is used for whether right-click on the channel should bring up
498 -- an input selector or not.)
499 -- Called once for each channel, at the start of the program.
500 -- Will never be called for live (0) or preview (1).
501 function channel_signal(channel)
502 if channel - 2 == VIDEO_SIGNAL_NUM then
503 return iptv_video:get_signal_num()
504 elseif is_plain_signal(channel - 2) then
512 -- Called every frame. Returns the color (if any) to paint around the given
513 -- channel. Returns a CSS color (typically to mark live and preview signals);
514 -- "transparent" is allowed.
515 -- Will never be called for live (0) or preview (1).
516 function channel_color(channel)
517 if state.transition_type ~= NO_TRANSITION then
518 if channel_involved_in(channel, state.transition_src_signal) or
519 channel_involved_in(channel, state.transition_dst_signal) then
523 if channel_involved_in(channel, state.live_signal_num) then
527 if channel_involved_in(channel, state.preview_signal_num) then
533 function channel_involved_in(channel, signal_num)
534 if is_plain_signal(signal_num) then
535 return channel == (signal_num + 2)
537 if signal_num == SBS_SIGNAL_NUM then
538 return is_sbs_participating_signal(channel - 2)
540 if signal_num == STATIC_SIGNAL_NUM then
541 return (channel == NUM_CAMERAS)
543 if signal_num == VIDEO_SIGNAL_NUM then
544 return (channel == NUM_CAMERAS + 1)
550 -- Returns if a given channel supports setting white balance (starting from 2).
551 -- Called only once for each channel, at the start of the program.
552 function supports_set_wb(channel)
553 return is_plain_signal(channel - 2)
557 -- Gets called with a new gray point when the white balance is changing.
558 -- The color is in linear light (not sRGB gamma).
559 function set_wb(channel, red, green, blue)
560 if is_plain_signal(channel - 2) then
561 state.neutral_colors[channel - 2 + 1] = { red, green, blue }
565 function finish_transitions(t)
566 if state.transition_type ~= NO_TRANSITION and t >= state.transition_end then
567 state.live_signal_num = state.transition_dst_signal
568 state.transition_type = NO_TRANSITION
571 -- Disable the overlay if it is no longer visible.
572 if state.overlay_enabled and t > state.overlay_transition_end and state.overlay_alpha_dst == 0.0 then
573 state.overlay_enabled = false
574 print("Turning off overlay")
578 function in_transition(t)
579 return t >= state.transition_start and t <= state.transition_end
582 function is_sbs_participating_signal(signal_num)
583 return signal_num == INPUT0_SIGNAL_NUM or signal_num == INPUT4_SIGNAL_NUM
586 function simple_signal_has_overlay(signal_num)
587 -- The commentator output has no overlay on it.
588 return signal_num ~= INPUT4_SIGNAL_NUM
592 -- Called every frame.
593 function get_transitions(t)
594 if state.preview_signal_num == OVERLAY_SIGNAL_NUM then
595 if t < state.overlay_transition_end then
599 if state.overlay_enabled then
600 return {"Overlay off", "", "Fade ovl out"}
602 return {"Overlay on", "", "Fade ovl in"}
606 if in_transition(t) then
607 -- Transition already in progress, the only thing we can do is really
608 -- cut to the preview. (TODO: Make an “abort” and/or “finish”, too?)
612 if state.live_signal_num == state.preview_signal_num then
613 -- No transitions possible.
617 if (is_plain_signal(state.live_signal_num) and state.preview_signal_num == VIDEO_SIGNAL_NUM) or
618 (is_plain_signal(state.preview_signal_num) and state.live_signal_num == VIDEO_SIGNAL_NUM) then
619 return {"Cut", "Sting", "Fade"}
622 if (is_plain_signal(state.live_signal_num) or state.live_signal_num == STATIC_SIGNAL_NUM) and
623 (is_plain_signal(state.preview_signal_num) or state.preview_signal_num == STATIC_SIGNAL_NUM) then
624 return {"Cut", "", "Fade"}
628 if state.live_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(state.preview_signal_num) then
629 return {"Cut", "Zoom in"}
630 elseif is_sbs_participating_signal(state.live_signal_num) and state.preview_signal_num == SBS_SIGNAL_NUM then
631 return {"Cut", "Zoom out"}
637 function swap_preview_live()
638 local temp = state.live_signal_num
639 state.live_signal_num = state.preview_signal_num
640 state.preview_signal_num = temp
643 function start_transition(type_, t, duration)
644 state.transition_start = t
645 state.transition_end = t + duration
646 state.transition_type = type_
647 state.transition_src_signal = state.live_signal_num
648 state.transition_dst_signal = state.preview_signal_num
649 state.stinger_in_progress = false
654 -- Called when the user clicks a transition button.
655 function transition_clicked(num, t)
656 if state.preview_signal_num == OVERLAY_SIGNAL_NUM then
659 state.overlay_transition_start = -2.0
660 state.overlay_transition_end = -1.0
661 if state.overlay_enabled then
662 state.overlay_enabled = false
663 state.overlay_alpha_src = 0.0
664 state.overlay_alpha_dst = 0.0
666 state.overlay_enabled = true
667 state.overlay_alpha_src = 1.0
668 state.overlay_alpha_dst = 1.0
672 state.overlay_transition_start = t
673 state.overlay_transition_end = t + 1.0
674 if state.overlay_enabled then
675 state.overlay_alpha_src = 1.0
676 state.overlay_alpha_dst = 0.0
678 state.overlay_alpha_src = 0.0
679 state.overlay_alpha_dst = 1.0
681 state.overlay_enabled = true
688 if in_transition(t) then
689 -- Ongoing transition; finish it immediately before the cut.
690 finish_transitions(state.transition_end)
694 state.stinger_in_progress = false
697 finish_transitions(t)
699 if state.live_signal_num == state.preview_signal_num then
704 if (is_plain_signal(state.live_signal_num) and state.preview_signal_num == VIDEO_SIGNAL_NUM) or
705 (is_plain_signal(state.preview_signal_num) and state.live_signal_num == VIDEO_SIGNAL_NUM) then
707 if stinger_in_progress then
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
912 if state.stinger_dst_signal == VIDEO_SIGNAL_NUM then
913 -- Turn off the overlay when playing video.
914 state.stinger_save_overlay = state.overlay_enabled
915 state.overlay_enabled = false
917 -- Restore the state.
918 state.overlay_enabled = state.stinger_save_overlay
922 chain = simple_chains[input_type][input_scale][overlay_really_enabled][true]
924 local prepare = function()
925 if input_type ~= "video" then
926 chain.input:connect_signal(state.live_signal_num)
928 set_neutral_color_from_signal(state_copy, chain.wb_effect, state.live_signal_num)
929 set_scale_parameters_if_needed(chain, width, height)
930 prepare_overlay_live(state_copy, chain, t, 1.0)
932 return chain.chain, prepare
933 elseif state.live_signal_num == STATIC_SIGNAL_NUM then -- Static picture.
934 local chain = static_chains[true]
935 local prepare = function()
936 prepare_overlay_live(state_copy, chain, t, 1.0) -- FIXME: Should this ever be here?
938 return chain.chain, prepare
944 -- We do not show overlays on the individual preview inputs.
945 -- The M/E preview matches what we'd put live by doing a transition, as always.
946 local show_overlay = false
947 if num == 1 then -- Preview.
948 if state.preview_signal_num == OVERLAY_SIGNAL_NUM then
949 num = state.live_signal_num + 2
950 show_overlay = not state.overlay_enabled
952 if state.transition_type ~= NO_TRANSITION then
953 num = state.transition_dst_signal + 2
956 num = state.preview_signal_num + 2
957 show_overlay = state.overlay_enabled and simple_signal_has_overlay(state.preview_signal_num)
961 -- Individual preview inputs (usually without overlay).
962 if is_plain_signal(num - 2) then
963 local signal_num = num - 2
964 local input_type = get_input_type(signals, signal_num)
965 local input_scale = needs_scale(signals, signal_num, width, height)
966 local chain = simple_chains[input_type][input_scale][show_overlay][false]
967 local prepare = function()
968 if input_type ~= "video" then
969 chain.input:connect_signal(signal_num)
971 set_neutral_color(chain.wb_effect, state_copy.neutral_colors[signal_num + 1])
972 set_scale_parameters_if_needed(chain, width, height)
973 prepare_overlay_static(chain, t)
975 return chain.chain, prepare
977 if num == SBS_SIGNAL_NUM + 2 then
978 local input0_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
979 local input1_type = get_input_type(signals, INPUT4_SIGNAL_NUM)
980 local chain = sbs_chains[input0_type][show_overlay][input1_type][false]
981 local prepare = function()
982 prepare_sbs_chain(state_copy, chain, 0.0, NO_TRANSITION, 0, SBS_SIGNAL_NUM, width, height, input_resolution)
984 return chain.chain, prepare
986 if num == STATIC_SIGNAL_NUM + 2 then
987 local chain = static_chains[false]
988 local prepare = function()
989 prepare_overlay_static(chain, t)
991 return chain.chain, prepare
993 if num == OVERLAY_SIGNAL_NUM + 2 then
994 local prepare = function()
995 -- prepare_overlay(overlay_chain_lq, t)
997 return overlay_chain_lq, prepare
1001 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
1003 return math.floor(x + 0.5)
1006 function lerp(a, b, t)
1007 return a + (b - a) * t
1010 function lerp_pos(a, b, t)
1012 x0 = lerp(a.x0, b.x0, t),
1013 y0 = lerp(a.y0, b.y0, t),
1014 x1 = lerp(a.x1, b.x1, t),
1015 y1 = lerp(a.y1, b.y1, t)
1019 function pos_from_top_left(x, y, width, height, screen_width, screen_height)
1020 local xs = screen_width / 1280.0
1021 local ys = screen_height / 720.0
1025 x1 = round(xs * (x + width)),
1026 y1 = round(ys * (y + height))
1030 function prepare_sbs_chain(state, chain, t, transition_type, src_signal, dst_signal, screen_width, screen_height, input_resolution)
1031 chain.input0.input:connect_signal(0)
1032 chain.input1.input:connect_signal(4)
1033 set_neutral_color(chain.input0.wb_effect, state.neutral_colors[1])
1034 set_neutral_color(chain.input1.wb_effect, state.neutral_colors[5])
1036 -- Both inputs are the same size (true side-by-side).
1037 local pos0 = pos_from_top_left(1280 - 616 - 16, 186, 616, 347, screen_width, screen_height)
1038 local pos1 = pos_from_top_left(16, 186, 616, 347, screen_width, screen_height)
1040 local pos_fs = { x0 = 0, y0 = 0, x1 = screen_width, y1 = screen_height }
1042 if transition_type == NO_TRANSITION then
1044 affine_param = { sx = 1.0, sy = 1.0, tx = 0.0, ty = 0.0 } -- Identity.
1046 -- Zooming to/from SBS view into or out of a single view.
1047 assert(transition_type == ZOOM_TRANSITION)
1048 local signal, real_t
1049 if src_signal == SBS_SIGNAL_NUM then
1053 assert(dst_signal == SBS_SIGNAL_NUM)
1058 if signal == INPUT0_SIGNAL_NUM then
1059 affine_param = find_affine_param(pos0, lerp_pos(pos0, pos_fs, real_t))
1060 elseif signal == INPUT4_SIGNAL_NUM then
1061 affine_param = find_affine_param(pos1, lerp_pos(pos1, pos_fs, real_t))
1065 -- NOTE: input_resolution is not 1-indexed, unlike usual Lua arrays.
1066 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)
1067 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)
1070 -- Find the transformation that changes the first rectangle to the second one.
1071 function find_affine_param(a, b)
1072 local sx = (b.x1 - b.x0) / (a.x1 - a.x0)
1073 local sy = (b.y1 - b.y0) / (a.y1 - a.y0)
1077 tx = b.x0 - a.x0 * sx,
1078 ty = b.y0 - a.y0 * sy
1082 function place_rectangle_with_affine(resample_effect, resize_effect, padding_effect, pos, aff, screen_width, screen_height, input_width, input_height)
1083 local x0 = pos.x0 * aff.sx + aff.tx
1084 local x1 = pos.x1 * aff.sx + aff.tx
1085 local y0 = pos.y0 * aff.sy + aff.ty
1086 local y1 = pos.y1 * aff.sy + aff.ty
1088 place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height)
1091 function place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height)
1097 padding_effect:set_int("width", screen_width)
1098 padding_effect:set_int("height", screen_height)
1101 if x0 > screen_width or x1 < 0.0 or y0 > screen_height or y1 < 0.0 then
1102 if resample_effect ~= nil then
1103 resample_effect:set_int("width", 1)
1104 resample_effect:set_int("height", 1)
1105 resample_effect:set_float("zoom_x", screen_width)
1106 resample_effect:set_float("zoom_y", screen_height)
1108 resize_effect:set_int("width", 1)
1109 resize_effect:set_int("height", 1)
1111 padding_effect:set_int("left", screen_width + 100)
1112 padding_effect:set_int("top", screen_height + 100)
1118 srcx0 = -x0 / (x1 - x0)
1122 srcy0 = -y0 / (y1 - y0)
1125 if x1 > screen_width then
1126 srcx1 = (screen_width - x0) / (x1 - x0)
1129 if y1 > screen_height then
1130 srcy1 = (screen_height - y0) / (y1 - y0)
1134 if resample_effect ~= nil then
1135 -- High-quality resampling.
1136 local x_subpixel_offset = x0 - math.floor(x0)
1137 local y_subpixel_offset = y0 - math.floor(y0)
1139 -- Resampling must be to an integral number of pixels. Round up,
1140 -- and then add an extra pixel so we have some leeway for the border.
1141 local width = math.ceil(x1 - x0) + 1
1142 local height = math.ceil(y1 - y0) + 1
1143 resample_effect:set_int("width", width)
1144 resample_effect:set_int("height", height)
1146 -- Correct the discrepancy with zoom. (This will leave a small
1147 -- excess edge of pixels and subpixels, which we'll correct for soon.)
1148 local zoom_x = (x1 - x0) / (width * (srcx1 - srcx0))
1149 local zoom_y = (y1 - y0) / (height * (srcy1 - srcy0))
1150 resample_effect:set_float("zoom_x", zoom_x)
1151 resample_effect:set_float("zoom_y", zoom_y)
1152 resample_effect:set_float("zoom_center_x", 0.0)
1153 resample_effect:set_float("zoom_center_y", 0.0)
1155 -- Padding must also be to a whole-pixel offset.
1156 padding_effect:set_int("left", math.floor(x0))
1157 padding_effect:set_int("top", math.floor(y0))
1159 -- Correct _that_ discrepancy by subpixel offset in the resampling.
1160 resample_effect:set_float("left", srcx0 * input_width - x_subpixel_offset / zoom_x)
1161 resample_effect:set_float("top", srcy0 * input_height - y_subpixel_offset / zoom_y)
1163 -- Finally, adjust the border so it is exactly where we want it.
1164 padding_effect:set_float("border_offset_left", x_subpixel_offset)
1165 padding_effect:set_float("border_offset_right", x1 - (math.floor(x0) + width))
1166 padding_effect:set_float("border_offset_top", y_subpixel_offset)
1167 padding_effect:set_float("border_offset_bottom", y1 - (math.floor(y0) + height))
1169 -- Lower-quality simple resizing.
1170 local width = round(x1 - x0)
1171 local height = round(y1 - y0)
1172 resize_effect:set_int("width", width)
1173 resize_effect:set_int("height", height)
1175 -- Padding must also be to a whole-pixel offset.
1176 padding_effect:set_int("left", math.floor(x0))
1177 padding_effect:set_int("top", math.floor(y0))
1181 function prepare_overlay_live(state, chain, t, extra_alpha_factor)
1182 if chain.overlay then
1183 local tt = calc_fade_progress(t, state.overlay_transition_start, state.overlay_transition_end)
1184 local overlay_alpha = state.overlay_alpha_src + tt * (state.overlay_alpha_dst - state.overlay_alpha_src)
1185 overlay_alpha = overlay_alpha * extra_alpha_factor
1186 --print("overlay_alpha=" .. overlay_alpha .. " [" .. state.overlay_alpha_src .. "," .. state.overlay_alpha_dst .. "]@" .. tt)
1187 if t > state.overlay_transition_end and state.overlay_alpha_dst == 0.0 then
1188 state.overlay_enabled = false -- Takes effect next frame.
1189 -- print("Turning off overlay")
1191 chain.overlay.multiply_effect:set_vec4("factor", overlay_alpha, overlay_alpha, overlay_alpha, overlay_alpha)
1195 function prepare_overlay_static(chain)
1196 if chain.overlay then
1197 chain.overlay.multiply_effect:set_vec4("factor", 1.0, 1.0, 1.0, 1.0)
1201 function set_neutral_color(effect, color)
1202 effect:set_vec3("neutral_color", color[1], color[2], color[3])
1205 function set_neutral_color_from_signal(state, effect, signal)
1206 if is_plain_signal(signal) then
1207 set_neutral_color(effect, state.neutral_colors[signal - INPUT0_SIGNAL_NUM + 1])
1211 function calc_zoom_progress(t)
1212 if t < state.transition_start then
1214 elseif t > state.transition_end then
1217 local tt = (t - state.transition_start) / (state.transition_end - state.transition_start)
1219 return math.sin(tt * 3.14159265358 * 0.5)
1223 function calc_fade_progress(t, transition_start, transition_end)
1224 local tt = (t - transition_start) / (transition_end - transition_start)
1227 elseif tt > 1.0 then
1231 -- Make the fade look maybe a tad more natural, by pumping it
1232 -- through a sigmoid function.
1233 tt = 10.0 * tt - 5.0
1234 tt = 1.0 / (1.0 + math.exp(-tt))
1240 { "Reload overlay", reload_cef },
1241 { "Disconnect IPTV", disconnect_iptv }