1 -- Nageru theme for TFK mini-tournament 2017, based on the default theme.
3 local http_request = require("http.request")
4 local cqueues = require("cqueues")
5 local cq = cqueues.new()
6 local futatabi_server = "http://gruessi.trd.sesse.net:9095"
9 transition_start = -2.0,
10 transition_end = -1.0,
12 transition_src_signal = 0,
13 transition_dst_signal = 0,
16 {0.5, 0.5, 0.5}, -- Input 0.
17 {0.5, 0.5, 0.5}, -- Input 1.
18 {0.5, 0.5, 0.5}, -- Input 2.
19 {0.5, 0.5, 0.5}, -- Input 3.
20 {0.5, 0.5, 0.5}, -- Input 4.
21 {0.5, 0.5, 0.5} -- Input 5.
22 -- Will also be filled with VIDEO_SIGNAL_NUM below.
25 overlay_transition_start = -2.0,
26 overlay_transition_end = -1.0,
27 overlay_alpha_src = 0.0,
28 overlay_alpha_dst = 1.0,
29 overlay_enabled = false,
31 stinger_in_progress = false,
33 stinger_src_signal = 0,
34 stinger_dst_signal = 0,
35 stinger_save_overlay = false,
38 preview_signal_num = 1
40 local NUM_CAMERAS = 6 -- Remember to update neutral_colors, too.
42 -- Update the Futatabi status in the title.
46 local headers, stream = assert(http_request.new_from_uri(futatabi_server .. "/queue_status"):go())
48 if headers:get ":status" == "200" then
49 local body = assert(stream:get_body_as_string())
51 replay_title = "IPTV (" .. body .. ")"
57 -- Valid values for live_signal_num and preview_signal_num.
58 local INPUT0_SIGNAL_NUM = 0
59 local INPUT1_SIGNAL_NUM = 1
60 local INPUT2_SIGNAL_NUM = 2
61 local INPUT3_SIGNAL_NUM = 3
62 local INPUT4_SIGNAL_NUM = 4
63 local INPUT5_SIGNAL_NUM = 5
64 local SBS_SIGNAL_NUM = NUM_CAMERAS
65 local STATIC_SIGNAL_NUM = NUM_CAMERAS + 1
66 local VIDEO_SIGNAL_NUM = NUM_CAMERAS + 2
68 state.neutral_colors[VIDEO_SIGNAL_NUM - INPUT0_SIGNAL_NUM + 1] = {0.5, 0.5, 0.5}
70 -- Preview-only signal showing the current signal with the overlay.
71 -- Not valid for live_signal_num!
72 local OVERLAY_SIGNAL_NUM = NUM_CAMERAS + 3
74 -- Valid values for transition_type. (Cuts are done directly, so they need no entry.)
75 local NO_TRANSITION = 0
76 local ZOOM_TRANSITION = 1
77 local FADE_TRANSITION = 2
79 -- Last width/height/frame rate for each channel, if we have it.
80 -- Note that unlike the values we get from Nageru, the resolution is per
81 -- frame and not per field, since we deinterlace.
82 local last_resolution = {}
84 local cef_path = Nageru.THEME_PATH:match("(.*)/nageru/")
85 local cef_input = HTMLInput.new("file://" .. cef_path .. "/score.html")
86 cef_input:execute_javascript_async("play()")
88 local bg_video = VideoInput.new(cef_path .. "/flow-720.mp4", Nageru.VIDEO_FORMAT_YCBCR)
89 -- local iptv_video = VideoInput.new("http://10.34.129.69:9060/1/v.flv", Nageru.VIDEO_FORMAT_YCBCR)
90 local iptv_video = VideoInput.new(futatabi_server, Nageru.VIDEO_FORMAT_YCBCR)
94 cef_input:execute_javascript_async("play()")
97 function disconnect_iptv()
98 iptv_video:disconnect()
101 -- Utility function to help creating many similar chains that can differ
102 -- in a free set of chosen parameters.
103 function make_cartesian_product(parms, callback)
104 return make_cartesian_product_internal(parms, callback, 1, {})
107 function make_cartesian_product_internal(parms, callback, index, args)
108 if index > #parms then
109 return callback(unpack(args))
112 for _, value in ipairs(parms[index]) do
114 ret[value] = make_cartesian_product_internal(parms, callback, index + 1, args)
119 -- An overlay with variable alpha.
120 function make_overlay(chain, base)
121 local image = chain:add_html_input(cef_input)
122 local multiply_effect = chain:add_effect(MultiplyEffect.new())
123 local overlay_effect = chain:add_effect(OverlayEffect.new(), base, multiply_effect)
126 multiply_effect = multiply_effect,
127 overlay_effect = overlay_effect
131 function possibly_make_overlay(has_overlay, chain, base)
132 if has_overlay == true then
133 return make_overlay(chain, base)
139 function make_fade_input(chain, signal, live, deint, scale, video)
140 local input, wb_effect, resample_effect, last
142 input = chain:add_video_input(iptv_video, false)
145 input = chain:add_live_input(false, deint)
146 input:connect_signal(signal)
149 input = chain:add_effect(ImageInput.new(cef_path .. "/nageru/dsn-bg.png"))
153 -- If we cared about this for the non-main inputs, we would have
154 -- checked hq here and invoked ResizeEffect instead.
156 resample_effect = chain:add_effect(ResampleEffect.new())
157 last = resample_effect
160 -- Make sure to put the white balance after the scaling (usually more efficient).
162 wb_effect = chain:add_effect(WhiteBalanceEffect.new())
168 wb_effect = wb_effect,
169 resample_effect = resample_effect,
174 -- A chain to fade between two inputs, of which either can be a picture
175 -- or a live input. In practice only used live, but we still support the
177 function make_fade_chain(input0_live, input0_deint, input0_scale, input0_video, input1_live, input1_deint, input1_scale, input1_video, has_overlay, hq)
178 local chain = EffectChain.new(16, 9)
180 local input0 = make_fade_input(chain, INPUT0_SIGNAL_NUM, input0_live, input0_deint, input0_scale, input0_video)
181 local input1 = make_fade_input(chain, INPUT1_SIGNAL_NUM, input1_live, input1_deint, input1_scale, input1_video)
183 -- If fading between two live inputs, the overlay is put on top.
184 -- If fading between the static picture and a live input,
185 -- the overlay is put on the live input.
187 if input0_live and not input1_live then
188 overlay = possibly_make_overlay(has_overlay, chain, input0.last)
190 input0.last = overlay.overlay_effect
192 elseif input1_live and not input0_live then
193 overlay = possibly_make_overlay(has_overlay, chain, input1.last)
195 input1.last = overlay.overlay_effect
199 local mix_effect = chain:add_effect(MixEffect.new(), input0.last, input1.last)
200 if input0_live and input1_live then
201 overlay = possibly_make_overlay(has_overlay, chain, mix_effect)
210 mix_effect = mix_effect,
215 -- Chains to fade between two inputs, in various configurations.
216 local fade_chains = make_cartesian_product({
217 {"video", "static", "live", "livedeint"}, -- input0_type
218 {true, false}, -- input0_scale
219 {"video", "static", "live", "livedeint"}, -- input1_type
220 {true, false}, -- input1_scale
221 {true, false}, -- has_overlay
223 }, function(input0_type, input0_scale, input1_type, input1_scale, has_overlay, hq)
224 local input0_live = (input0_type ~= "static")
225 local input1_live = (input1_type ~= "static")
226 local input0_deint = (input0_type == "livedeint")
227 local input1_deint = (input1_type == "livedeint")
228 local input0_video = (input0_type == "video")
229 local input1_video = (input1_type == "video")
230 return make_fade_chain(input0_live, input0_deint, input0_scale, input0_video, input1_live, input1_deint, input1_scale, input1_video, has_overlay, hq)
233 function make_sbs_input(chain, signal, deint, has_overlay, hq)
234 local input = chain:add_live_input(not deint, deint) -- Override bounce only if not deinterlacing.
235 input:connect_signal(signal)
237 local wb_effect = chain:add_effect(WhiteBalanceEffect.new()) -- Needs to be before the overlay.
241 overlay = make_overlay(chain, wb_effect)
244 local resample_effect = nil
245 local resize_effect = nil
247 resample_effect = chain:add_effect(ResampleEffect.new())
249 resize_effect = chain:add_effect(ResizeEffect.new())
252 local padding_effect = chain:add_effect(IntegralPaddingEffect.new())
256 wb_effect = wb_effect,
257 resample_effect = resample_effect,
258 resize_effect = resize_effect,
259 padding_effect = padding_effect,
264 -- Side-by-side chains.
265 function make_sbs_chain(input0_type, input0_overlay, input1_type, hq)
266 local chain = EffectChain.new(16, 9)
268 local bg = chain:add_video_input(bg_video, false)
270 local input0 = make_sbs_input(chain, INPUT0_SIGNAL_NUM, input0_type == "livedeint", input0_overlay, hq)
271 local input1 = make_sbs_input(chain, INPUT4_SIGNAL_NUM, input1_type == "livedeint", false, hq)
273 input0.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0)
274 input1.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0)
276 local i0 = chain:add_effect(OverlayEffect.new(), bg, input0.padding_effect)
277 chain:add_effect(OverlayEffect.new(), i0, input1.padding_effect)
284 overlay = input0.overlay -- May be nil.
289 -- Make all possible combinations of side-by-side chains.
290 local sbs_chains = make_cartesian_product({
291 {"live", "livedeint"}, -- input0_type
292 {true, false}, -- input0_overlay
293 {"live", "livedeint"}, -- input1_type
295 }, function(input0_type, input0_overlay, input1_type, hq)
296 return make_sbs_chain(input0_type, input0_overlay, input1_type, hq)
299 function make_simple_chain_no_finalize(input_deint, input_video, input_scale, has_overlay, hq)
300 local chain = EffectChain.new(16, 9)
304 input = chain:add_video_input(iptv_video, false)
306 input = chain:add_live_input(false, input_deint)
307 input:connect_signal(0) -- First input card. Can be changed whenever you want.
310 local resample_effect, resize_effect
313 resample_effect = chain:add_effect(ResampleEffect.new())
315 resize_effect = chain:add_effect(ResizeEffect.new())
319 local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
320 local overlay = possibly_make_overlay(has_overlay, chain, wb_effect)
325 wb_effect = wb_effect,
326 resample_effect = resample_effect,
327 resize_effect = resize_effect,
332 -- A chain to show a single input on screen.
333 function make_simple_chain(input_deint, input_video, input_scale, has_overlay, hq)
334 local chain = make_simple_chain_no_finalize(input_deint, input_video, input_scale, has_overlay, hq)
335 chain.chain:finalize(hq)
339 -- Make all possible combinations of single-input chains.
340 local simple_chains = make_cartesian_product({
341 {"video", "live", "livedeint"}, -- input_type
342 {true, false}, -- input_scale
343 {true, false}, -- has_overlay
345 }, function(input_type, input_scale, has_overlay, hq)
346 local input_deint = (input_type == "livedeint")
347 local input_video = (input_type == "video")
348 return make_simple_chain(input_deint, input_video, input_scale, has_overlay, hq)
351 -- A chain to show a single input on screen, with a stinger on top.
352 function make_stinger_chain(input_deint, input_video, input_scale, has_overlay, stinger_frame, hq)
353 local chain = make_simple_chain_no_finalize(input_deint, input_video, input_scale, has_overlay, hq)
354 local filename = cef_path .. "/stinger/blur" .. string.rep("0", 3 - string.len(tostring(stinger_frame))) .. stinger_frame .. ".png"
358 last = chain.overlay.overlay_effect
360 last = chain.wb_effect
362 local input = chain.chain:add_effect(ImageInput.new(filename))
363 chain.chain:add_effect(OverlayEffect.new(), last, input)
364 chain.chain:finalize(hq)
368 -- Single-input chains with various stinger frames on top.
369 local stinger_chains = make_cartesian_product({
370 {"video", "live", "livedeint"}, -- input_type
371 {true, false}, -- input_scale
372 {true, false}, -- has_overlay
373 {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
375 }, function(input_type, input_scale, has_overlay, stinger_frame, hq)
376 local input_deint = (input_type == "livedeint")
377 local input_video = (input_type == "video")
378 return make_stinger_chain(input_deint, input_video, input_scale, has_overlay, stinger_frame, hq)
381 -- A chain to show a single static picture on screen. Never with HTML overlay.
382 local static_chains = make_cartesian_product({
385 local chain = EffectChain.new(16, 9)
386 local chain_input = chain:add_effect(ImageInput.new(cef_path .. "/nageru/dsn-bg.png"))
394 -- A chain to show the overlay and nothing more. LQ only,
395 -- since it is not a valid live signal.
396 local overlay_chain_lq = EffectChain.new(16, 9)
397 local overlay_chain_lq_input = overlay_chain_lq:add_html_input(cef_input)
398 overlay_chain_lq:finalize(false)
400 -- Used for indexing into the tables of chains.
401 function get_input_type(signals, signal_num)
402 if signal_num == STATIC_SIGNAL_NUM then
404 elseif signal_num == VIDEO_SIGNAL_NUM then
406 elseif signals:get_interlaced(signal_num) then
413 function needs_scale(signals, signal_num, width, height)
414 if signal_num == STATIC_SIGNAL_NUM then
415 -- We assume this is already correctly scaled at load time.
418 assert(is_plain_signal(signal_num))
419 return (signals:get_width(signal_num) ~= width or signals:get_height(signal_num) ~= height)
422 function set_scale_parameters_if_needed(chain_or_input, width, height)
423 if chain_or_input.resample_effect then
424 chain_or_input.resample_effect:set_int("width", width)
425 chain_or_input.resample_effect:set_int("height", height)
426 elseif chain_or_input.resize_effect then
427 chain_or_input.resize_effect:set_int("width", width)
428 chain_or_input.resize_effect:set_int("height", height)
433 -- Returns the number of outputs in addition to the live (0) and preview (1).
434 -- Called only once, at the start of the program.
435 function num_channels()
436 return NUM_CAMERAS + 4 -- sbs, static picture, video and overlay
439 function is_plain_signal(num)
440 return (num >= INPUT0_SIGNAL_NUM and num <= INPUT5_SIGNAL_NUM) or (num == VIDEO_SIGNAL_NUM)
443 -- Helper function to write e.g. “720p60”. The difference between this
444 -- and get_channel_resolution_raw() is that this one also can say that
445 -- there's no signal.
446 function get_channel_resolution(signal_num)
447 local res = last_resolution[signal_num]
448 if (not res) or not res.is_connected then
449 return "disconnected"
451 if res.height <= 0 then
454 if not res.has_signal then
455 if res.height == 525 then
456 -- Special mode for the USB3 cards.
459 return get_channel_resolution_raw(res) .. ", no signal"
461 return get_channel_resolution_raw(res)
465 -- Helper function to write e.g. “60” or “59.94”.
466 function get_frame_rate(res)
467 local nom = res.frame_rate_nom
468 local den = res.frame_rate_den
469 if nom % den == 0 then
472 return string.format("%.2f", nom / den)
476 -- Helper function to write e.g. “720p60”.
477 function get_channel_resolution_raw(res)
478 if res.interlaced then
479 return res.height .. "i" .. get_frame_rate(res)
481 return res.height .. "p" .. get_frame_rate(res)
486 -- Returns the name for each additional channel (starting from 2).
487 -- Called at the start of the program, and then each frame for live
488 -- channels in case they change resolution.
489 function channel_name(channel)
490 local signal_num = channel - 2
491 if signal_num == INPUT0_SIGNAL_NUM then
492 return "Main (" .. get_channel_resolution(signal_num) .. ")"
493 elseif signal_num == INPUT1_SIGNAL_NUM then
494 return "Secondary (" .. get_channel_resolution(signal_num) .. ")"
495 elseif signal_num == INPUT2_SIGNAL_NUM then
496 return "Goal L (" .. get_channel_resolution(signal_num) .. ")"
497 elseif signal_num == INPUT3_SIGNAL_NUM then
498 return "Goal R (" .. get_channel_resolution(signal_num) .. ")"
499 elseif signal_num == INPUT4_SIGNAL_NUM then
500 return "Commentators (" .. get_channel_resolution(signal_num) .. ")"
501 elseif signal_num == INPUT5_SIGNAL_NUM then
502 return "Laptop (" .. get_channel_resolution(signal_num) .. ")"
503 elseif signal_num == SBS_SIGNAL_NUM then
504 return "Side-by-side"
505 elseif signal_num == STATIC_SIGNAL_NUM then
506 return "Static picture"
507 elseif signal_num == VIDEO_SIGNAL_NUM then
508 -- return "IPTV input (" .. get_channel_resolution(iptv_video:get_signal_num()) .. ")"
510 elseif signal_num == OVERLAY_SIGNAL_NUM then
516 -- Returns, given a channel number, which signal it corresponds to (starting from 0).
517 -- Should return -1 if the channel does not correspond to a simple signal.
518 -- (The information is used for whether right-click on the channel should bring up
519 -- an input selector or not.)
520 -- Called once for each channel, at the start of the program.
521 -- Will never be called for live (0) or preview (1).
522 function channel_signal(channel)
523 if channel - 2 == VIDEO_SIGNAL_NUM then
524 return iptv_video:get_signal_num()
525 elseif is_plain_signal(channel - 2) then
533 -- Called every frame. Returns the color (if any) to paint around the given
534 -- channel. Returns a CSS color (typically to mark live and preview signals);
535 -- "transparent" is allowed.
536 -- Will never be called for live (0) or preview (1).
537 function channel_color(channel)
538 if state.transition_type ~= NO_TRANSITION then
539 if channel_involved_in(channel, state.transition_src_signal) or
540 channel_involved_in(channel, state.transition_dst_signal) then
544 if channel_involved_in(channel, state.live_signal_num) then
548 if channel_involved_in(channel, state.preview_signal_num) then
554 function channel_involved_in(channel, signal_num)
555 if is_plain_signal(signal_num) then
556 return channel == (signal_num + 2)
558 if signal_num == SBS_SIGNAL_NUM then
559 return is_sbs_participating_signal(channel - 2)
561 if signal_num == STATIC_SIGNAL_NUM then
562 return (channel == NUM_CAMERAS)
564 if signal_num == VIDEO_SIGNAL_NUM then
565 return (channel == NUM_CAMERAS + 1)
571 -- Returns if a given channel supports setting white balance (starting from 2).
572 -- Called only once for each channel, at the start of the program.
573 function supports_set_wb(channel)
574 return is_plain_signal(channel - 2)
578 -- Gets called with a new gray point when the white balance is changing.
579 -- The color is in linear light (not sRGB gamma).
580 function set_wb(channel, red, green, blue)
581 if is_plain_signal(channel - 2) then
582 state.neutral_colors[channel - 2 + 1] = { red, green, blue }
586 function finish_transitions(t)
587 if state.transition_type ~= NO_TRANSITION and t >= state.transition_end then
588 state.live_signal_num = state.transition_dst_signal
589 state.transition_type = NO_TRANSITION
592 -- Disable the overlay if it is no longer visible.
593 if state.overlay_enabled and t > state.overlay_transition_end and state.overlay_alpha_dst == 0.0 then
594 state.overlay_enabled = false
595 print("Turning off overlay")
599 function in_transition(t)
600 return t >= state.transition_start and t <= state.transition_end
603 function is_sbs_participating_signal(signal_num)
604 return signal_num == INPUT0_SIGNAL_NUM or signal_num == INPUT4_SIGNAL_NUM
607 function simple_signal_has_overlay(signal_num)
608 -- The commentator output has no overlay on it.
609 return signal_num ~= INPUT4_SIGNAL_NUM
613 -- Called every frame.
614 function get_transitions(t)
615 if state.preview_signal_num == OVERLAY_SIGNAL_NUM then
616 if t < state.overlay_transition_end then
620 if state.overlay_enabled then
621 return {"Overlay off", "", "Fade ovl out"}
623 return {"Overlay on", "", "Fade ovl in"}
627 if in_transition(t) then
628 -- Transition already in progress, the only thing we can do is really
629 -- cut to the preview. (TODO: Make an “abort” and/or “finish”, too?)
633 if state.live_signal_num == state.preview_signal_num then
634 -- No transitions possible.
638 if (is_plain_signal(state.live_signal_num) and state.preview_signal_num == VIDEO_SIGNAL_NUM) or
639 (is_plain_signal(state.preview_signal_num) and state.live_signal_num == VIDEO_SIGNAL_NUM) then
640 return {"Cut", "Sting", "Fade"}
643 if (is_plain_signal(state.live_signal_num) or state.live_signal_num == STATIC_SIGNAL_NUM) and
644 (is_plain_signal(state.preview_signal_num) or state.preview_signal_num == STATIC_SIGNAL_NUM) then
645 return {"Cut", "", "Fade"}
649 if state.live_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(state.preview_signal_num) then
650 return {"Cut", "Zoom in"}
651 elseif is_sbs_participating_signal(state.live_signal_num) and state.preview_signal_num == SBS_SIGNAL_NUM then
652 return {"Cut", "Zoom out"}
658 function swap_preview_live()
659 local temp = state.live_signal_num
660 state.live_signal_num = state.preview_signal_num
661 state.preview_signal_num = temp
664 function start_transition(type_, t, duration)
665 state.transition_start = t
666 state.transition_end = t + duration
667 state.transition_type = type_
668 state.transition_src_signal = state.live_signal_num
669 state.transition_dst_signal = state.preview_signal_num
670 state.stinger_in_progress = false
675 -- Called when the user clicks a transition button.
676 function transition_clicked(num, t)
677 if state.preview_signal_num == OVERLAY_SIGNAL_NUM then
680 state.overlay_transition_start = -2.0
681 state.overlay_transition_end = -1.0
682 if state.overlay_enabled then
683 state.overlay_enabled = false
684 state.overlay_alpha_src = 0.0
685 state.overlay_alpha_dst = 0.0
687 state.overlay_enabled = true
688 state.overlay_alpha_src = 1.0
689 state.overlay_alpha_dst = 1.0
693 state.overlay_transition_start = t
694 state.overlay_transition_end = t + 1.0
695 if state.overlay_enabled then
696 state.overlay_alpha_src = 1.0
697 state.overlay_alpha_dst = 0.0
699 state.overlay_alpha_src = 0.0
700 state.overlay_alpha_dst = 1.0
702 state.overlay_enabled = true
709 if in_transition(t) then
710 -- Ongoing transition; finish it immediately before the cut.
711 finish_transitions(state.transition_end)
715 state.stinger_in_progress = false
718 finish_transitions(t)
720 if state.live_signal_num == state.preview_signal_num then
725 if (is_plain_signal(state.live_signal_num) and state.preview_signal_num == VIDEO_SIGNAL_NUM) or
726 (is_plain_signal(state.preview_signal_num) and state.live_signal_num == VIDEO_SIGNAL_NUM) then
728 if stinger_in_progress then
732 state.stinger_in_progress = true
733 state.stinger_frame = 0
734 state.stinger_src_signal = state.live_signal_num
735 state.stinger_dst_signal = state.preview_signal_num
739 if is_plain_signal(state.live_signal_num) and is_plain_signal(state.preview_signal_num) then
740 -- We can't zoom between these. Just make a cut.
741 io.write("Cutting from " .. state.live_signal_num .. " to " .. state.live_signal_num .. "\n")
743 state.stinger_in_progress = false
747 if (state.live_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(state.preview_signal_num)) or
748 (state.preview_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(state.live_signal_num)) then
749 start_transition(ZOOM_TRANSITION, t, 1.0)
752 finish_transitions(t)
755 if (state.live_signal_num ~= state.preview_signal_num) and
756 (is_plain_signal(state.live_signal_num) or
757 state.live_signal_num == STATIC_SIGNAL_NUM) and
758 (is_plain_signal(state.preview_signal_num) or
759 state.preview_signal_num == STATIC_SIGNAL_NUM) then
760 start_transition(FADE_TRANSITION, t, 1.0)
762 -- Fades involving SBS are ignored (we have no chain for it).
768 function channel_clicked(num)
769 state.preview_signal_num = num
772 function get_fade_chain(state, signals, t, width, height, input_resolution)
773 local input0_type = get_input_type(signals, state.transition_src_signal)
774 local input0_scale = needs_scale(signals, state.transition_src_signal, width, height)
775 local input1_type = get_input_type(signals, state.transition_dst_signal)
776 local input1_scale = needs_scale(signals, state.transition_dst_signal, width, height)
777 local chain = fade_chains[input0_type][input0_scale][input1_type][input1_scale][state.overlay_enabled][true]
778 local prepare = function()
779 if input0_type == "live" or input0_type == "livedeint" then
780 chain.input0.input:connect_signal(state.transition_src_signal)
782 if input0_type ~= "static" then
783 set_neutral_color_from_signal(state, chain.input0.wb_effect, state.transition_src_signal)
785 set_scale_parameters_if_needed(chain.input0, width, height)
786 if input1_type == "live" or input1_type == "livedeint" then
787 chain.input1.input:connect_signal(state.transition_dst_signal)
789 if input1_type ~= "static" then
790 set_neutral_color_from_signal(state, chain.input1.wb_effect, state.transition_dst_signal)
792 set_scale_parameters_if_needed(chain.input1, width, height)
793 local tt = calc_fade_progress(t, state.transition_start, state.transition_end)
795 chain.mix_effect:set_float("strength_first", 1.0 - tt)
796 chain.mix_effect:set_float("strength_second", tt)
798 -- The commentator output has no overlay on it.
799 local extra_alpha_factor = 1.0
800 if not simple_signal_has_overlay(state.transition_src_signal) and
801 not simple_signal_has_overlay(state.transition_dst_signal) then
802 extra_alpha_factor = 0.0
803 elseif not simple_signal_has_overlay(state.transition_src_signal) then
804 extra_alpha_factor = tt
805 elseif not simple_signal_has_overlay(state.transition_dst_signal) then
806 extra_alpha_factor = 1.0 - tt
808 prepare_overlay_live(state, chain, t, extra_alpha_factor)
810 return chain.chain, prepare
813 -- SBS code (live_signal_num == SBS_SIGNAL_NUM, or in a transition to/from it).
814 function get_sbs_chain(state, signals)
815 local input0_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
816 local input1_type = get_input_type(signals, INPUT4_SIGNAL_NUM)
817 return sbs_chains[input0_type][state.overlay_enabled][input1_type][true]
820 function fetch_input_resolution(signals, signal_num)
822 width = signals:get_width(signal_num),
823 height = signals:get_height(signal_num),
824 interlaced = signals:get_interlaced(signal_num),
825 has_signal = signals:get_has_signal(signal_num),
826 is_connected = signals:get_is_connected(signal_num),
827 frame_rate_nom = signals:get_frame_rate_nom(signal_num),
828 frame_rate_den = signals:get_frame_rate_den(signal_num)
831 if res.interlaced then
832 -- Convert height from frame height to field height.
833 -- (Needed for e.g. place_rectangle.)
834 res.height = res.height * 2
836 -- Show field rate instead of frame rate; really for cosmetics only
837 -- (and actually contrary to EBU recommendations, although in line
838 -- with typical user expectations).
839 res.frame_rate_nom = res.frame_rate_nom * 2
844 local last_rate = 0.0
847 -- Called every frame. Get the chain for displaying at input <num>,
848 -- where 0 is live, 1 is preview, 2 is the first channel to display
849 -- in the bottom bar, and so on up to num_channels()+1. t is the
850 -- current time in seconds. width and height are the dimensions of
851 -- the output, although you can ignore them if you don't need them
852 -- (they're useful if you want to e.g. know what to resample by).
854 -- <signals> is basically an exposed InputState, which you can use to
855 -- query for information about the signals at the point of the current
856 -- frame. In particular, you can call get_width() and get_height()
857 -- for any signal number, and use that to e.g. assist in chain selection.
859 -- You should return two objects; the chain itself, and then a
860 -- function (taking no parameters) that is run just before rendering.
861 -- The function needs to call connect_signal on any inputs, so that
862 -- it gets updated video data for the given frame. (You are allowed
863 -- to switch which input your input is getting from between frames,
864 -- but not calling connect_signal results in undefined behavior.)
865 -- If you want to change any parameters in the chain, this is also
868 -- NOTE: The chain returned must be finalized with the Y'CbCr flag
869 -- if and only if num==0.
870 function get_chain(num, t, width, height, signals)
873 local input_resolution = {}
874 for signal_num=0,(NUM_CAMERAS-1) do
875 input_resolution[signal_num] = fetch_input_resolution(signals, signal_num)
877 input_resolution[iptv_video:get_signal_num()] = fetch_input_resolution(signals, iptv_video:get_signal_num())
878 last_resolution = input_resolution
880 -- Make a (semi-shallow) copy of the current state, so that the returned prepare function
881 -- is unaffected by state changes made by the UI before it is rendered.
882 local state_copy = {}
883 for key, value in pairs(state) do
884 state_copy[key] = value
886 for key, value in pairs(state.neutral_colors) do
887 state_copy.neutral_colors[key] = value
890 -- Save some CPU time if we're not having SBS on live.
892 if state.live_signal_num == SBS_SIGNAL_NUM or
893 state.preview_signal_num == SBS_SIGNAL_NUM or
894 state.transition_type == ZOOM_TRANSITION then
899 if new_rate ~= last_rate then
900 -- Avoid waking up the video thread (which may be sleeping) if the rate is the same.
901 bg_video:change_rate(new_rate)
905 if num == 0 then -- Live.
906 -- See if we're in a transition.
907 finish_transitions(t)
908 if state.transition_type == ZOOM_TRANSITION then
909 -- Transition in or out of SBS.
910 local chain = get_sbs_chain(state_copy, signals)
911 local prepare = function()
912 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)
913 prepare_overlay_live(state_copy, chain, t, 1.0)
915 return chain.chain, prepare
916 elseif state.transition_type == NO_TRANSITION and state.live_signal_num == SBS_SIGNAL_NUM then
918 local chain = get_sbs_chain(state_copy, signals)
919 local prepare = function()
920 prepare_sbs_chain(state_copy, chain, 0.0, NO_TRANSITION, 0, SBS_SIGNAL_NUM, width, height, input_resolution)
921 prepare_overlay_live(state_copy, chain, t, 1.0)
923 return chain.chain, prepare
924 elseif state.transition_type == FADE_TRANSITION then
925 return get_fade_chain(state_copy, signals, t, width, height, input_resolution)
926 elseif is_plain_signal(state.live_signal_num) then
927 local input_type = get_input_type(signals, state.live_signal_num)
928 local input_scale = needs_scale(signals, state.live_signal_num, width, height)
929 local overlay_really_enabled = state.overlay_enabled and simple_signal_has_overlay(state.live_signal_num)
931 if state.stinger_in_progress then
932 chain = stinger_chains[input_type][input_scale][overlay_really_enabled][state.stinger_frame][true]
933 state.stinger_frame = state.stinger_frame + 1
934 if state.stinger_frame >= 25 then
935 state.stinger_in_progress = false
936 state.preview_signal_num = state.stinger_src_signal
937 state.live_signal_num = state.stinger_dst_signal
939 if state.stinger_dst_signal == VIDEO_SIGNAL_NUM then
940 -- Turn off the overlay when playing video.
941 state.stinger_save_overlay = state.overlay_enabled
942 state.overlay_enabled = false
944 -- Restore the state.
945 state.overlay_enabled = state.stinger_save_overlay
949 chain = simple_chains[input_type][input_scale][overlay_really_enabled][true]
951 local prepare = function()
952 if input_type ~= "video" then
953 chain.input:connect_signal(state.live_signal_num)
955 set_neutral_color_from_signal(state_copy, chain.wb_effect, state.live_signal_num)
956 set_scale_parameters_if_needed(chain, width, height)
957 prepare_overlay_live(state_copy, chain, t, 1.0)
959 return chain.chain, prepare
960 elseif state.live_signal_num == STATIC_SIGNAL_NUM then -- Static picture.
961 local chain = static_chains[true]
962 local prepare = function()
963 prepare_overlay_live(state_copy, chain, t, 1.0) -- FIXME: Should this ever be here?
965 return chain.chain, prepare
971 -- We do not show overlays on the individual preview inputs.
972 -- The M/E preview matches what we'd put live by doing a transition, as always.
973 local show_overlay = false
974 if num == 1 then -- Preview.
975 if state.preview_signal_num == OVERLAY_SIGNAL_NUM then
976 num = state.live_signal_num + 2
977 show_overlay = not state.overlay_enabled
979 if state.transition_type ~= NO_TRANSITION then
980 num = state.transition_dst_signal + 2
983 num = state.preview_signal_num + 2
984 show_overlay = state.overlay_enabled and simple_signal_has_overlay(state.preview_signal_num)
988 -- Individual preview inputs (usually without overlay).
989 if is_plain_signal(num - 2) then
990 local signal_num = num - 2
991 local input_type = get_input_type(signals, signal_num)
992 local input_scale = needs_scale(signals, signal_num, width, height)
993 local chain = simple_chains[input_type][input_scale][show_overlay][false]
994 local prepare = function()
995 if input_type ~= "video" then
996 chain.input:connect_signal(signal_num)
998 set_neutral_color(chain.wb_effect, state_copy.neutral_colors[signal_num + 1])
999 set_scale_parameters_if_needed(chain, width, height)
1000 prepare_overlay_static(chain, t)
1002 return chain.chain, prepare
1004 if num == SBS_SIGNAL_NUM + 2 then
1005 local input0_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
1006 local input1_type = get_input_type(signals, INPUT4_SIGNAL_NUM)
1007 local chain = sbs_chains[input0_type][show_overlay][input1_type][false]
1008 local prepare = function()
1009 prepare_sbs_chain(state_copy, chain, 0.0, NO_TRANSITION, 0, SBS_SIGNAL_NUM, width, height, input_resolution)
1011 return chain.chain, prepare
1013 if num == STATIC_SIGNAL_NUM + 2 then
1014 local chain = static_chains[false]
1015 local prepare = function()
1016 prepare_overlay_static(chain, t)
1018 return chain.chain, prepare
1020 if num == OVERLAY_SIGNAL_NUM + 2 then
1021 local prepare = function()
1022 -- prepare_overlay(overlay_chain_lq, t)
1024 return overlay_chain_lq, prepare
1028 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
1030 return math.floor(x + 0.5)
1033 function lerp(a, b, t)
1034 return a + (b - a) * t
1037 function lerp_pos(a, b, t)
1039 x0 = lerp(a.x0, b.x0, t),
1040 y0 = lerp(a.y0, b.y0, t),
1041 x1 = lerp(a.x1, b.x1, t),
1042 y1 = lerp(a.y1, b.y1, t)
1046 function pos_from_top_left(x, y, width, height, screen_width, screen_height)
1047 local xs = screen_width / 1280.0
1048 local ys = screen_height / 720.0
1052 x1 = round(xs * (x + width)),
1053 y1 = round(ys * (y + height))
1057 function prepare_sbs_chain(state, chain, t, transition_type, src_signal, dst_signal, screen_width, screen_height, input_resolution)
1058 chain.input0.input:connect_signal(0)
1059 chain.input1.input:connect_signal(4)
1060 set_neutral_color(chain.input0.wb_effect, state.neutral_colors[1])
1061 set_neutral_color(chain.input1.wb_effect, state.neutral_colors[5])
1063 -- Both inputs are the same size (true side-by-side).
1064 local pos0 = pos_from_top_left(1280 - 616 - 16, 186, 616, 347, screen_width, screen_height)
1065 local pos1 = pos_from_top_left(16, 186, 616, 347, screen_width, screen_height)
1067 local pos_fs = { x0 = 0, y0 = 0, x1 = screen_width, y1 = screen_height }
1069 if transition_type == NO_TRANSITION then
1071 affine_param = { sx = 1.0, sy = 1.0, tx = 0.0, ty = 0.0 } -- Identity.
1073 -- Zooming to/from SBS view into or out of a single view.
1074 assert(transition_type == ZOOM_TRANSITION)
1075 local signal, real_t
1076 if src_signal == SBS_SIGNAL_NUM then
1080 assert(dst_signal == SBS_SIGNAL_NUM)
1085 if signal == INPUT0_SIGNAL_NUM then
1086 affine_param = find_affine_param(pos0, lerp_pos(pos0, pos_fs, real_t))
1087 elseif signal == INPUT4_SIGNAL_NUM then
1088 affine_param = find_affine_param(pos1, lerp_pos(pos1, pos_fs, real_t))
1092 -- NOTE: input_resolution is not 1-indexed, unlike usual Lua arrays.
1093 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)
1094 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)
1097 -- Find the transformation that changes the first rectangle to the second one.
1098 function find_affine_param(a, b)
1099 local sx = (b.x1 - b.x0) / (a.x1 - a.x0)
1100 local sy = (b.y1 - b.y0) / (a.y1 - a.y0)
1104 tx = b.x0 - a.x0 * sx,
1105 ty = b.y0 - a.y0 * sy
1109 function place_rectangle_with_affine(resample_effect, resize_effect, padding_effect, pos, aff, screen_width, screen_height, input_width, input_height)
1110 local x0 = pos.x0 * aff.sx + aff.tx
1111 local x1 = pos.x1 * aff.sx + aff.tx
1112 local y0 = pos.y0 * aff.sy + aff.ty
1113 local y1 = pos.y1 * aff.sy + aff.ty
1115 place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height)
1118 function place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height)
1124 padding_effect:set_int("width", screen_width)
1125 padding_effect:set_int("height", screen_height)
1128 if x0 > screen_width or x1 < 0.0 or y0 > screen_height or y1 < 0.0 then
1129 if resample_effect ~= nil then
1130 resample_effect:set_int("width", 1)
1131 resample_effect:set_int("height", 1)
1132 resample_effect:set_float("zoom_x", screen_width)
1133 resample_effect:set_float("zoom_y", screen_height)
1135 resize_effect:set_int("width", 1)
1136 resize_effect:set_int("height", 1)
1138 padding_effect:set_int("left", screen_width + 100)
1139 padding_effect:set_int("top", screen_height + 100)
1145 srcx0 = -x0 / (x1 - x0)
1149 srcy0 = -y0 / (y1 - y0)
1152 if x1 > screen_width then
1153 srcx1 = (screen_width - x0) / (x1 - x0)
1156 if y1 > screen_height then
1157 srcy1 = (screen_height - y0) / (y1 - y0)
1161 if resample_effect ~= nil then
1162 -- High-quality resampling.
1163 local x_subpixel_offset = x0 - math.floor(x0)
1164 local y_subpixel_offset = y0 - math.floor(y0)
1166 -- Resampling must be to an integral number of pixels. Round up,
1167 -- and then add an extra pixel so we have some leeway for the border.
1168 local width = math.ceil(x1 - x0) + 1
1169 local height = math.ceil(y1 - y0) + 1
1170 resample_effect:set_int("width", width)
1171 resample_effect:set_int("height", height)
1173 -- Correct the discrepancy with zoom. (This will leave a small
1174 -- excess edge of pixels and subpixels, which we'll correct for soon.)
1175 local zoom_x = (x1 - x0) / (width * (srcx1 - srcx0))
1176 local zoom_y = (y1 - y0) / (height * (srcy1 - srcy0))
1177 resample_effect:set_float("zoom_x", zoom_x)
1178 resample_effect:set_float("zoom_y", zoom_y)
1179 resample_effect:set_float("zoom_center_x", 0.0)
1180 resample_effect:set_float("zoom_center_y", 0.0)
1182 -- Padding must also be to a whole-pixel offset.
1183 padding_effect:set_int("left", math.floor(x0))
1184 padding_effect:set_int("top", math.floor(y0))
1186 -- Correct _that_ discrepancy by subpixel offset in the resampling.
1187 resample_effect:set_float("left", srcx0 * input_width - x_subpixel_offset / zoom_x)
1188 resample_effect:set_float("top", srcy0 * input_height - y_subpixel_offset / zoom_y)
1190 -- Finally, adjust the border so it is exactly where we want it.
1191 padding_effect:set_float("border_offset_left", x_subpixel_offset)
1192 padding_effect:set_float("border_offset_right", x1 - (math.floor(x0) + width))
1193 padding_effect:set_float("border_offset_top", y_subpixel_offset)
1194 padding_effect:set_float("border_offset_bottom", y1 - (math.floor(y0) + height))
1196 -- Lower-quality simple resizing.
1197 local width = round(x1 - x0)
1198 local height = round(y1 - y0)
1199 resize_effect:set_int("width", width)
1200 resize_effect:set_int("height", height)
1202 -- Padding must also be to a whole-pixel offset.
1203 padding_effect:set_int("left", math.floor(x0))
1204 padding_effect:set_int("top", math.floor(y0))
1208 function prepare_overlay_live(state, chain, t, extra_alpha_factor)
1209 if chain.overlay then
1210 local tt = calc_fade_progress(t, state.overlay_transition_start, state.overlay_transition_end)
1211 local overlay_alpha = state.overlay_alpha_src + tt * (state.overlay_alpha_dst - state.overlay_alpha_src)
1212 overlay_alpha = overlay_alpha * extra_alpha_factor
1213 --print("overlay_alpha=" .. overlay_alpha .. " [" .. state.overlay_alpha_src .. "," .. state.overlay_alpha_dst .. "]@" .. tt)
1214 if t > state.overlay_transition_end and state.overlay_alpha_dst == 0.0 then
1215 state.overlay_enabled = false -- Takes effect next frame.
1216 -- print("Turning off overlay")
1218 chain.overlay.multiply_effect:set_vec4("factor", overlay_alpha, overlay_alpha, overlay_alpha, overlay_alpha)
1222 function prepare_overlay_static(chain)
1223 if chain.overlay then
1224 chain.overlay.multiply_effect:set_vec4("factor", 1.0, 1.0, 1.0, 1.0)
1228 function set_neutral_color(effect, color)
1229 effect:set_vec3("neutral_color", color[1], color[2], color[3])
1232 function set_neutral_color_from_signal(state, effect, signal)
1233 if is_plain_signal(signal) then
1234 set_neutral_color(effect, state.neutral_colors[signal - INPUT0_SIGNAL_NUM + 1])
1238 function calc_zoom_progress(t)
1239 if t < state.transition_start then
1241 elseif t > state.transition_end then
1244 local tt = (t - state.transition_start) / (state.transition_end - state.transition_start)
1246 return math.sin(tt * 3.14159265358 * 0.5)
1250 function calc_fade_progress(t, transition_start, transition_end)
1251 local tt = (t - transition_start) / (transition_end - transition_start)
1254 elseif tt > 1.0 then
1258 -- Make the fade look maybe a tad more natural, by pumping it
1259 -- through a sigmoid function.
1260 tt = 10.0 * tt - 5.0
1261 tt = 1.0 / (1.0 + math.exp(-tt))
1267 { "Reload overlay", reload_cef },
1268 { "Disconnect IPTV", disconnect_iptv }