1 -- Nageru theme for ultimate productions, based on the default theme.
3 local transition_start = -2.0
4 local transition_end = -1.0
5 local transition_type = 0
6 local transition_src_signal = 0
7 local transition_dst_signal = 0
9 local neutral_colors = {
10 {0.5, 0.5, 0.5}, -- Input 0.
11 {0.5, 0.5, 0.5}, -- Input 1.
12 {0.5, 0.5, 0.5}, -- Input 2.
13 {0.5, 0.5, 0.5}, -- Input 3.
14 {0.5, 0.5, 0.5}, -- Input 4.
15 {0.5, 0.5, 0.5} -- Input 5.
16 -- Will also be filled with VIDEO_SIGNAL_NUM below.
19 local overlay_transition_start = -2.0
20 local overlay_transition_end = -1.0
21 local overlay_alpha_src = 0.0
22 local overlay_alpha_dst = 1.0
23 local overlay_enabled = false
25 local live_signal_num = 0
26 local preview_signal_num = 1
27 local NUM_CAMERAS = 6 -- Remember to update neutral_colors, too.
29 -- Valid values for live_signal_num and preview_signal_num.
30 local INPUT0_SIGNAL_NUM = 0
31 local INPUT1_SIGNAL_NUM = 1
32 local INPUT2_SIGNAL_NUM = 2
33 local INPUT3_SIGNAL_NUM = 3
34 local INPUT4_SIGNAL_NUM = 4
35 local INPUT5_SIGNAL_NUM = 5
36 local SBS_SIGNAL_NUM = NUM_CAMERAS
37 local STATIC_SIGNAL_NUM = NUM_CAMERAS + 1
38 local VIDEO_SIGNAL_NUM = NUM_CAMERAS + 2
40 neutral_colors[VIDEO_SIGNAL_NUM - INPUT0_SIGNAL_NUM + 1] = {0.5, 0.5, 0.5};
42 -- Preview-only signal showing the current signal with the overlay.
43 -- Not valid for live_signal_num!
44 local OVERLAY_SIGNAL_NUM = NUM_CAMERAS + 3
46 -- Valid values for transition_type. (Cuts are done directly, so they need no entry.)
47 local NO_TRANSITION = 0
48 local ZOOM_TRANSITION = 1
49 local FADE_TRANSITION = 2
51 -- Last width/height/frame rate for each channel, if we have it.
52 -- Note that unlike the values we get from Nageru, the resolution is per
53 -- frame and not per field, since we deinterlace.
54 local last_resolution = {}
56 local cef_path = Nageru.THEME_PATH:match("(.*)/nageru/")
57 local cef_input = HTMLInput.new("file://" .. cef_path .. "/score.html")
58 cef_input:execute_javascript_async("play()")
60 local bg_video = VideoInput.new(cef_path .. "/flow-720.mp4", Nageru.VIDEO_FORMAT_YCBCR)
61 -- local iptv_video = VideoInput.new("http://10.34.129.69:9060/1/v.flv", Nageru.VIDEO_FORMAT_YCBCR)
62 local iptv_video = VideoInput.new("http://home.samfundet.no/~sesse/videostuff", Nageru.VIDEO_FORMAT_YCBCR)
66 cef_input:execute_javascript_async("play()")
69 function disconnect_iptv()
70 iptv_video:disconnect()
73 -- Utility function to help creating many similar chains that can differ
74 -- in a free set of chosen parameters.
75 function make_cartesian_product(parms, callback)
76 return make_cartesian_product_internal(parms, callback, 1, {})
79 function make_cartesian_product_internal(parms, callback, index, args)
80 if index > #parms then
81 return callback(unpack(args))
84 for _, value in ipairs(parms[index]) do
86 ret[value] = make_cartesian_product_internal(parms, callback, index + 1, args)
91 -- An overlay with variable alpha.
92 function make_overlay(chain, base)
93 local image = chain:add_html_input(cef_input)
94 local multiply_effect = chain:add_effect(MultiplyEffect.new())
95 local overlay_effect = chain:add_effect(OverlayEffect.new(), base, multiply_effect)
98 multiply_effect = multiply_effect,
99 overlay_effect = overlay_effect
103 function possibly_make_overlay(has_overlay, chain, base)
104 if has_overlay == true then
105 return make_overlay(chain, base)
111 function make_fade_input(chain, signal, live, deint, scale, video)
112 local input, wb_effect, resample_effect, last
114 input = chain:add_video_input(iptv_video, false)
117 input = chain:add_live_input(false, deint)
118 input:connect_signal(signal)
121 input = chain:add_effect(ImageInput.new(cef_path .. "/nageru/dsn-bg.png"))
125 -- If we cared about this for the non-main inputs, we would have
126 -- checked hq here and invoked ResizeEffect instead.
128 resample_effect = chain:add_effect(ResampleEffect.new())
129 last = resample_effect
132 -- Make sure to put the white balance after the scaling (usually more efficient).
134 wb_effect = chain:add_effect(WhiteBalanceEffect.new())
140 wb_effect = wb_effect,
141 resample_effect = resample_effect,
146 -- A chain to fade between two inputs, of which either can be a picture
147 -- or a live input. In practice only used live, but we still support the
149 function make_fade_chain(input0_live, input0_deint, input0_scale, input0_video, input1_live, input1_deint, input1_scale, input1_video, has_overlay, hq)
150 local chain = EffectChain.new(16, 9)
152 local input0 = make_fade_input(chain, INPUT0_SIGNAL_NUM, input0_live, input0_deint, input0_scale, input0_video)
153 local input1 = make_fade_input(chain, INPUT1_SIGNAL_NUM, input1_live, input1_deint, input1_scale, input1_video)
155 -- If fading between two live inputs, the overlay is put on top.
156 -- If fading between the static picture and a live input,
157 -- the overlay is put on the live input.
159 if input0_live and not input1_live then
160 overlay = possibly_make_overlay(has_overlay, chain, input0.last)
162 input0.last = overlay.overlay_effect
164 elseif input1_live and not input0_live then
165 overlay = possibly_make_overlay(has_overlay, chain, input1.last)
167 input1.last = overlay.overlay_effect
171 local mix_effect = chain:add_effect(MixEffect.new(), input0.last, input1.last)
172 if input0_live and input1_live then
173 overlay = possibly_make_overlay(has_overlay, chain, mix_effect)
182 mix_effect = mix_effect,
187 -- Chains to fade between two inputs, in various configurations.
188 local fade_chains = make_cartesian_product({
189 {"video", "static", "live", "livedeint"}, -- input0_type
190 {true, false}, -- input0_scale
191 {"video", "static", "live", "livedeint"}, -- input1_type
192 {true, false}, -- input1_scale
193 {true, false}, -- has_overlay
195 }, function(input0_type, input0_scale, input1_type, input1_scale, has_overlay, hq)
196 local input0_live = (input0_type ~= "static")
197 local input1_live = (input1_type ~= "static")
198 local input0_deint = (input0_type == "livedeint")
199 local input1_deint = (input1_type == "livedeint")
200 local input0_video = (input0_type == "video")
201 local input1_video = (input1_type == "video")
202 return make_fade_chain(input0_live, input0_deint, input0_scale, input0_video, input1_live, input1_deint, input1_scale, input1_video, has_overlay, hq)
205 function make_sbs_input(chain, signal, deint, has_overlay, hq)
206 local input = chain:add_live_input(not deint, deint) -- Override bounce only if not deinterlacing.
207 input:connect_signal(signal)
211 overlay = make_overlay(chain, input)
214 local resample_effect = nil
215 local resize_effect = nil
217 resample_effect = chain:add_effect(ResampleEffect.new())
219 resize_effect = chain:add_effect(ResizeEffect.new())
221 local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
223 local padding_effect = chain:add_effect(IntegralPaddingEffect.new())
227 wb_effect = wb_effect,
228 resample_effect = resample_effect,
229 resize_effect = resize_effect,
230 padding_effect = padding_effect,
235 -- Side-by-side chains.
236 function make_sbs_chain(input0_type, input0_overlay, input1_type, hq)
237 local chain = EffectChain.new(16, 9)
239 local bg = chain:add_video_input(bg_video, false)
241 local input0 = make_sbs_input(chain, INPUT0_SIGNAL_NUM, input0_type == "livedeint", input0_overlay, hq)
242 local input1 = make_sbs_input(chain, INPUT4_SIGNAL_NUM, input1_type == "livedeint", false, hq)
244 input0.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0)
245 input1.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0)
247 local i0 = chain:add_effect(OverlayEffect.new(), bg, input0.padding_effect)
248 chain:add_effect(OverlayEffect.new(), i0, input1.padding_effect)
255 overlay = input0.overlay -- May be nil.
260 -- Make all possible combinations of side-by-side chains.
261 local sbs_chains = make_cartesian_product({
262 {"live", "livedeint"}, -- input0_type
263 {true, false}, -- input0_overlay
264 {"live", "livedeint"}, -- input1_type
266 }, function(input0_type, input0_overlay, input1_type, hq)
267 return make_sbs_chain(input0_type, input0_overlay, input1_type, hq)
270 -- A chain to show a single input on screen.
271 function make_simple_chain(input_deint, input_video, input_scale, has_overlay, hq)
272 local chain = EffectChain.new(16, 9)
276 input = chain:add_video_input(iptv_video, false)
278 input = chain:add_live_input(false, input_deint)
279 input:connect_signal(0) -- First input card. Can be changed whenever you want.
282 local resample_effect, resize_effect
285 resample_effect = chain:add_effect(ResampleEffect.new())
287 resize_effect = chain:add_effect(ResizeEffect.new())
291 local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
292 local overlay = possibly_make_overlay(has_overlay, chain, wb_effect)
299 wb_effect = wb_effect,
300 resample_effect = resample_effect,
301 resize_effect = resize_effect,
306 -- Make all possible combinations of single-input chains.
307 local simple_chains = make_cartesian_product({
308 {"video", "live", "livedeint"}, -- input_type
309 {true, false}, -- input_scale
310 {true, false}, -- has_overlay
312 }, function(input_type, input_scale, has_overlay, hq)
313 local input_deint = (input_type == "livedeint")
314 local input_video = (input_type == "video")
315 return make_simple_chain(input_deint, input_video, input_scale, has_overlay, hq)
318 -- A chain to show a single static picture on screen. Never with HTML overlay.
319 local static_chains = make_cartesian_product({
322 local chain = EffectChain.new(16, 9)
323 local chain_input = chain:add_effect(ImageInput.new(cef_path .. "/nageru/dsn-bg.png"))
331 -- A chain to show the overlay and nothing more. LQ only,
332 -- since it is not a valid live signal.
333 local overlay_chain_lq = EffectChain.new(16, 9)
334 local overlay_chain_lq_input = overlay_chain_lq:add_html_input(cef_input)
335 overlay_chain_lq:finalize(false)
337 -- Used for indexing into the tables of chains.
338 function get_input_type(signals, signal_num)
339 if signal_num == STATIC_SIGNAL_NUM then
341 elseif signal_num == VIDEO_SIGNAL_NUM then
343 elseif signals:get_interlaced(signal_num) then
350 function needs_scale(signals, signal_num, width, height)
351 if signal_num == STATIC_SIGNAL_NUM then
352 -- We assume this is already correctly scaled at load time.
355 assert(is_plain_signal(signal_num))
356 return (signals:get_width(signal_num) ~= width or signals:get_height(signal_num) ~= height)
359 function set_scale_parameters_if_needed(chain_or_input, width, height)
360 if chain_or_input.resample_effect then
361 chain_or_input.resample_effect:set_int("width", width)
362 chain_or_input.resample_effect:set_int("height", height)
363 elseif chain_or_input.resize_effect then
364 chain_or_input.resize_effect:set_int("width", width)
365 chain_or_input.resize_effect:set_int("height", height)
370 -- Returns the number of outputs in addition to the live (0) and preview (1).
371 -- Called only once, at the start of the program.
372 function num_channels()
373 return NUM_CAMERAS + 4 -- sbs, static picture, video and overlay
376 function is_plain_signal(num)
377 return (num >= INPUT0_SIGNAL_NUM and num <= INPUT5_SIGNAL_NUM) or (num == VIDEO_SIGNAL_NUM)
380 -- Helper function to write e.g. “720p60”. The difference between this
381 -- and get_channel_resolution_raw() is that this one also can say that
382 -- there's no signal.
383 function get_channel_resolution(signal_num)
384 res = last_resolution[signal_num]
385 if (not res) or not res.is_connected then
386 return "disconnected"
388 if res.height <= 0 then
391 if not res.has_signal then
392 if res.height == 525 then
393 -- Special mode for the USB3 cards.
396 return get_channel_resolution_raw(res) .. ", no signal"
398 return get_channel_resolution_raw(res)
402 -- Helper function to write e.g. “60” or “59.94”.
403 function get_frame_rate(res)
404 local nom = res.frame_rate_nom
405 local den = res.frame_rate_den
406 if nom % den == 0 then
409 return string.format("%.2f", nom / den)
413 -- Helper function to write e.g. “720p60”.
414 function get_channel_resolution_raw(res)
415 if res.interlaced then
416 return res.height .. "i" .. get_frame_rate(res)
418 return res.height .. "p" .. get_frame_rate(res)
423 -- Returns the name for each additional channel (starting from 2).
424 -- Called at the start of the program, and then each frame for live
425 -- channels in case they change resolution.
426 function channel_name(channel)
427 local signal_num = channel - 2
428 if signal_num == INPUT0_SIGNAL_NUM then
429 return "Main (" .. get_channel_resolution(signal_num) .. ")"
430 elseif signal_num == INPUT1_SIGNAL_NUM then
431 return "Secondary (" .. get_channel_resolution(signal_num) .. ")"
432 elseif signal_num == INPUT2_SIGNAL_NUM then
433 return "Goal L (" .. get_channel_resolution(signal_num) .. ")"
434 elseif signal_num == INPUT3_SIGNAL_NUM then
435 return "Goal R (" .. get_channel_resolution(signal_num) .. ")"
436 elseif signal_num == INPUT4_SIGNAL_NUM then
437 return "Commentators (" .. get_channel_resolution(signal_num) .. ")"
438 elseif signal_num == INPUT5_SIGNAL_NUM then
439 return "Laptop (" .. get_channel_resolution(signal_num) .. ")"
440 elseif signal_num == SBS_SIGNAL_NUM then
441 return "Side-by-side"
442 elseif signal_num == STATIC_SIGNAL_NUM then
443 return "Static picture"
444 elseif signal_num == VIDEO_SIGNAL_NUM then
446 elseif signal_num == OVERLAY_SIGNAL_NUM then
452 -- Returns, given a channel number, which signal it corresponds to (starting from 0).
453 -- Should return -1 if the channel does not correspond to a simple signal.
454 -- (The information is used for whether right-click on the channel should bring up
455 -- an input selector or not.)
456 -- Called once for each channel, at the start of the program.
457 -- Will never be called for live (0) or preview (1).
458 function channel_signal(channel)
459 if channel - 2 == VIDEO_SIGNAL_NUM then
460 return iptv_video:get_signal_num()
461 elseif is_plain_signal(channel - 2) then
469 -- Called every frame. Returns the color (if any) to paint around the given
470 -- channel. Returns a CSS color (typically to mark live and preview signals);
471 -- "transparent" is allowed.
472 -- Will never be called for live (0) or preview (1).
473 function channel_color(channel)
474 if transition_type ~= NO_TRANSITION then
475 if channel_involved_in(channel, transition_src_signal) or
476 channel_involved_in(channel, transition_dst_signal) then
480 if channel_involved_in(channel, live_signal_num) then
484 if channel_involved_in(channel, preview_signal_num) then
490 function channel_involved_in(channel, signal_num)
491 if is_plain_signal(signal_num) then
492 return channel == (signal_num + 2)
494 if signal_num == SBS_SIGNAL_NUM then
495 return is_sbs_participating_signal(channel - 2)
497 if signal_num == STATIC_SIGNAL_NUM then
498 return (channel == NUM_CAMERAS)
500 if signal_num == VIDEO_SIGNAL_NUM then
501 return (channel == NUM_CAMERAS + 1)
507 -- Returns if a given channel supports setting white balance (starting from 2).
508 -- Called only once for each channel, at the start of the program.
509 function supports_set_wb(channel)
510 return is_plain_signal(channel - 2)
514 -- Gets called with a new gray point when the white balance is changing.
515 -- The color is in linear light (not sRGB gamma).
516 function set_wb(channel, red, green, blue)
517 if is_plain_signal(channel - 2) then
518 neutral_colors[channel - 2 + 1] = { red, green, blue }
522 function finish_transitions(t)
523 if transition_type ~= NO_TRANSITION and t >= transition_end then
524 live_signal_num = transition_dst_signal
525 transition_type = NO_TRANSITION
528 -- Disable the overlay if it is no longer visible.
529 if overlay_enabled and t > overlay_transition_end and overlay_alpha_dst == 0.0 then
530 overlay_enabled = false
531 print("Turning off overlay")
535 function in_transition(t)
536 return t >= transition_start and t <= transition_end
539 function is_sbs_participating_signal(signal_num)
540 return signal_num == INPUT0_SIGNAL_NUM or signal_num == INPUT4_SIGNAL_NUM
543 function simple_signal_has_overlay(signal_num)
544 -- The commentator output has no overlay on it.
545 return signal_num ~= INPUT4_SIGNAL_NUM
549 -- Called every frame.
550 function get_transitions(t)
551 if preview_signal_num == OVERLAY_SIGNAL_NUM then
552 if t < overlay_transition_end then
556 if overlay_enabled then
557 return {"Overlay off", "", "Fade ovl out"}
559 return {"Overlay on", "", "Fade ovl in"}
563 if in_transition(t) then
564 -- Transition already in progress, the only thing we can do is really
565 -- cut to the preview. (TODO: Make an “abort” and/or “finish”, too?)
569 if live_signal_num == preview_signal_num then
570 -- No transitions possible.
574 if (is_plain_signal(live_signal_num) or live_signal_num == STATIC_SIGNAL_NUM) and
575 (is_plain_signal(preview_signal_num) or preview_signal_num == STATIC_SIGNAL_NUM) then
576 return {"Cut", "", "Fade"}
580 if live_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(preview_signal_num) then
581 return {"Cut", "Zoom in"}
582 elseif is_sbs_participating_signal(live_signal_num) and preview_signal_num == SBS_SIGNAL_NUM then
583 return {"Cut", "Zoom out"}
589 function swap_preview_live()
590 local temp = live_signal_num
591 live_signal_num = preview_signal_num
592 preview_signal_num = temp
595 function start_transition(type_, t, duration)
597 transition_end = t + duration
598 transition_type = type_
599 transition_src_signal = live_signal_num
600 transition_dst_signal = preview_signal_num
605 -- Called when the user clicks a transition button.
606 function transition_clicked(num, t)
607 if preview_signal_num == OVERLAY_SIGNAL_NUM then
610 overlay_transition_start = -2.0
611 overlay_transition_end = -1.0
612 if overlay_enabled then
613 overlay_enabled = false
614 overlay_alpha_src = 0.0
615 overlay_alpha_dst = 0.0
617 overlay_enabled = true
618 overlay_alpha_src = 1.0
619 overlay_alpha_dst = 1.0
623 overlay_transition_start = t
624 overlay_transition_end = t + 1.0
625 if overlay_enabled then
626 overlay_alpha_src = 1.0
627 overlay_alpha_dst = 0.0
629 overlay_alpha_src = 0.0
630 overlay_alpha_dst = 1.0
632 overlay_enabled = true
639 if in_transition(t) then
640 -- Ongoing transition; finish it immediately before the cut.
641 finish_transitions(transition_end)
647 finish_transitions(t)
649 if live_signal_num == preview_signal_num then
654 if is_plain_signal(live_signal_num) and is_plain_signal(preview_signal_num) then
655 -- We can't zoom between these. Just make a cut.
656 io.write("Cutting from " .. live_signal_num .. " to " .. live_signal_num .. "\n")
661 if (live_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(preview_signal_num)) or
662 (preview_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(live_signal_num)) then
663 start_transition(ZOOM_TRANSITION, t, 1.0)
666 finish_transitions(t)
669 if (live_signal_num ~= preview_signal_num) and
670 (is_plain_signal(live_signal_num) or
671 live_signal_num == STATIC_SIGNAL_NUM) and
672 (is_plain_signal(preview_signal_num) or
673 preview_signal_num == STATIC_SIGNAL_NUM) then
674 start_transition(FADE_TRANSITION, t, 1.0)
676 -- Fades involving SBS are ignored (we have no chain for it).
682 function channel_clicked(num)
683 preview_signal_num = num
686 function get_fade_chain(signals, t, width, height, input_resolution)
687 local input0_type = get_input_type(signals, transition_src_signal)
688 local input0_scale = needs_scale(signals, transition_src_signal, width, height)
689 local input1_type = get_input_type(signals, transition_dst_signal)
690 local input1_scale = needs_scale(signals, transition_dst_signal, width, height)
691 local chain = fade_chains[input0_type][input0_scale][input1_type][input1_scale][overlay_enabled][true]
693 if input0_type == "live" or input0_type == "livedeint" then
694 chain.input0.input:connect_signal(transition_src_signal)
696 if input0_type ~= "static" then
697 set_neutral_color_from_signal(chain.input0.wb_effect, transition_src_signal)
699 set_scale_parameters_if_needed(chain.input0, width, height)
700 if input1_type == "live" or input1_type == "livedeint" then
701 chain.input1.input:connect_signal(transition_dst_signal)
703 if input1_type ~= "static" then
704 set_neutral_color_from_signal(chain.input1.wb_effect, transition_dst_signal)
706 set_scale_parameters_if_needed(chain.input1, width, height)
707 local tt = calc_fade_progress(t, transition_start, transition_end)
709 chain.mix_effect:set_float("strength_first", 1.0 - tt)
710 chain.mix_effect:set_float("strength_second", tt)
712 -- The commentator output has no overlay on it.
713 local extra_alpha_factor = 1.0
714 if not simple_signal_has_overlay(transition_src_signal) and
715 not simple_signal_has_overlay(transition_dst_signal) then
716 extra_alpha_factor = 0.0
717 elseif not simple_signal_has_overlay(transition_src_signal) then
718 extra_alpha_factor = tt
719 elseif not simple_signal_has_overlay(transition_dst_signal) then
720 extra_alpha_factor = 1.0 - tt
722 prepare_overlay_live(chain, t, extra_alpha_factor)
724 return chain.chain, prepare
727 -- SBS code (live_signal_num == SBS_SIGNAL_NUM, or in a transition to/from it).
728 function get_sbs_chain(signals, t, width, height, input_resolution)
729 local input0_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
730 local input1_type = get_input_type(signals, INPUT4_SIGNAL_NUM)
731 return sbs_chains[input0_type][overlay_enabled][input1_type][true]
734 local last_rate = 0.0
737 -- Called every frame. Get the chain for displaying at input <num>,
738 -- where 0 is live, 1 is preview, 2 is the first channel to display
739 -- in the bottom bar, and so on up to num_channels()+1. t is the
740 -- current time in seconds. width and height are the dimensions of
741 -- the output, although you can ignore them if you don't need them
742 -- (they're useful if you want to e.g. know what to resample by).
744 -- <signals> is basically an exposed InputState, which you can use to
745 -- query for information about the signals at the point of the current
746 -- frame. In particular, you can call get_width() and get_height()
747 -- for any signal number, and use that to e.g. assist in chain selection.
749 -- You should return two objects; the chain itself, and then a
750 -- function (taking no parameters) that is run just before rendering.
751 -- The function needs to call connect_signal on any inputs, so that
752 -- it gets updated video data for the given frame. (You are allowed
753 -- to switch which input your input is getting from between frames,
754 -- but not calling connect_signal results in undefined behavior.)
755 -- If you want to change any parameters in the chain, this is also
758 -- NOTE: The chain returned must be finalized with the Y'CbCr flag
759 -- if and only if num==0.
760 function get_chain(num, t, width, height, signals)
761 local input_resolution = {}
762 for signal_num=0,(NUM_CAMERAS-1) do
764 width = signals:get_width(signal_num),
765 height = signals:get_height(signal_num),
766 interlaced = signals:get_interlaced(signal_num),
767 has_signal = signals:get_has_signal(signal_num),
768 is_connected = signals:get_is_connected(signal_num),
769 frame_rate_nom = signals:get_frame_rate_nom(signal_num),
770 frame_rate_den = signals:get_frame_rate_den(signal_num)
773 if res.interlaced then
774 -- Convert height from frame height to field height.
775 -- (Needed for e.g. place_rectangle.)
776 res.height = res.height * 2
778 -- Show field rate instead of frame rate; really for cosmetics only
779 -- (and actually contrary to EBU recommendations, although in line
780 -- with typical user expectations).
781 res.frame_rate_nom = res.frame_rate_nom * 2
784 input_resolution[signal_num] = res
786 last_resolution = input_resolution
788 -- Save some CPU time if we're not having SBS on live.
790 if live_signal_num == SBS_SIGNAL_NUM or
791 preview_signal_num == SBS_SIGNAL_NUM or
792 transition_type == ZOOM_TRANSITION then
797 if new_rate ~= last_rate then
798 -- Avoid waking up the video thread (which may be sleeping) if the rate is the same.
799 bg_video:change_rate(new_rate)
803 if num == 0 then -- Live.
804 -- See if we're in a transition.
805 finish_transitions(t)
806 if transition_type == ZOOM_TRANSITION then
807 -- Transition in or out of SBS.
808 local chain = get_sbs_chain(signals, t, width, height, input_resolution)
810 prepare_sbs_chain(chain, calc_zoom_progress(t), transition_type, transition_src_signal, transition_dst_signal, width, height, input_resolution)
811 prepare_overlay_live(chain, t, 1.0)
813 return chain.chain, prepare
814 elseif transition_type == NO_TRANSITION and live_signal_num == SBS_SIGNAL_NUM then
816 local chain = get_sbs_chain(signals, t, width, height, input_resolution)
818 prepare_sbs_chain(chain, 0.0, NO_TRANSITION, 0, SBS_SIGNAL_NUM, width, height, input_resolution)
819 prepare_overlay_live(chain, t, 1.0)
821 return chain.chain, prepare
822 elseif transition_type == FADE_TRANSITION then
823 return get_fade_chain(signals, t, width, height, input_resolution)
824 elseif is_plain_signal(live_signal_num) then
825 local input_type = get_input_type(signals, live_signal_num)
826 local input_scale = needs_scale(signals, live_signal_num, width, height)
827 local overlay_really_enabled = overlay_enabled and simple_signal_has_overlay(live_signal_num)
828 local chain = simple_chains[input_type][input_scale][overlay_really_enabled][true]
830 if input_type ~= "video" then
831 chain.input:connect_signal(live_signal_num)
833 set_neutral_color_from_signal(chain.wb_effect, live_signal_num)
834 set_scale_parameters_if_needed(chain, width, height)
835 prepare_overlay_live(chain, t, 1.0)
837 return chain.chain, prepare
838 elseif live_signal_num == STATIC_SIGNAL_NUM then -- Static picture.
839 local chain = static_chains[true]
841 prepare_overlay_live(chain, t, 1.0) -- FIXME: Should this ever be here?
843 return chain.chain, prepare
849 -- We do not show overlays on the individual preview inputs.
850 -- The M/E preview matches what we'd put live by doing a transition, as always.
851 local show_overlay = false
852 if num == 1 then -- Preview.
853 if preview_signal_num == OVERLAY_SIGNAL_NUM then
854 num = live_signal_num + 2
855 show_overlay = not overlay_enabled
857 if transition_type ~= NO_TRANSITION then
858 num = transition_dst_signal + 2
861 num = preview_signal_num + 2
862 show_overlay = overlay_enabled and simple_signal_has_overlay(preview_signal_num)
866 -- Individual preview inputs (usually without overlay).
867 if is_plain_signal(num - 2) then
868 local signal_num = num - 2
869 local input_type = get_input_type(signals, signal_num)
870 local input_scale = needs_scale(signals, signal_num, width, height)
871 local chain = simple_chains[input_type][input_scale][show_overlay][false]
873 if input_type ~= "video" then
874 chain.input:connect_signal(signal_num)
876 set_neutral_color(chain.wb_effect, neutral_colors[signal_num + 1])
877 set_scale_parameters_if_needed(chain, width, height)
878 prepare_overlay_static(chain, t)
880 return chain.chain, prepare
882 if num == SBS_SIGNAL_NUM + 2 then
883 local input0_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
884 local input1_type = get_input_type(signals, INPUT4_SIGNAL_NUM)
885 local chain = sbs_chains[input0_type][show_overlay][input1_type][false]
887 prepare_sbs_chain(chain, 0.0, NO_TRANSITION, 0, SBS_SIGNAL_NUM, width, height, input_resolution)
889 return chain.chain, prepare
891 if num == STATIC_SIGNAL_NUM + 2 then
892 local chain = static_chains[false]
894 prepare_overlay_static(chain, t)
896 return chain.chain, prepare
898 if num == OVERLAY_SIGNAL_NUM + 2 then
900 -- prepare_overlay(overlay_chain_lq, t)
902 return overlay_chain_lq, prepare
906 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
908 return math.floor(x + 0.5)
911 function lerp(a, b, t)
912 return a + (b - a) * t
915 function lerp_pos(a, b, t)
917 x0 = lerp(a.x0, b.x0, t),
918 y0 = lerp(a.y0, b.y0, t),
919 x1 = lerp(a.x1, b.x1, t),
920 y1 = lerp(a.y1, b.y1, t)
924 function pos_from_top_left(x, y, width, height, screen_width, screen_height)
925 local xs = screen_width / 1280.0
926 local ys = screen_height / 720.0
930 x1 = round(xs * (x + width)),
931 y1 = round(ys * (y + height))
935 function prepare_sbs_chain(chain, t, transition_type, src_signal, dst_signal, screen_width, screen_height, input_resolution)
936 chain.input0.input:connect_signal(0)
937 chain.input1.input:connect_signal(4)
938 set_neutral_color(chain.input0.wb_effect, neutral_colors[1])
939 set_neutral_color(chain.input1.wb_effect, neutral_colors[5])
941 -- Both inputs are the same size (true side-by-side).
942 local pos0 = pos_from_top_left(1280 - 616 - 16, 186, 616, 347, screen_width, screen_height)
943 local pos1 = pos_from_top_left(16, 186, 616, 347, screen_width, screen_height)
945 local pos_fs = { x0 = 0, y0 = 0, x1 = screen_width, y1 = screen_height }
947 if transition_type == NO_TRANSITION then
949 affine_param = { sx = 1.0, sy = 1.0, tx = 0.0, ty = 0.0 } -- Identity.
951 -- Zooming to/from SBS view into or out of a single view.
952 assert(transition_type == ZOOM_TRANSITION)
954 if src_signal == SBS_SIGNAL_NUM then
958 assert(dst_signal == SBS_SIGNAL_NUM)
963 if signal == INPUT0_SIGNAL_NUM then
964 affine_param = find_affine_param(pos0, lerp_pos(pos0, pos_fs, real_t))
965 elseif signal == INPUT4_SIGNAL_NUM then
966 affine_param = find_affine_param(pos1, lerp_pos(pos1, pos_fs, real_t))
970 -- NOTE: input_resolution is not 1-indexed, unlike usual Lua arrays.
971 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)
972 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)
975 -- Find the transformation that changes the first rectangle to the second one.
976 function find_affine_param(a, b)
977 local sx = (b.x1 - b.x0) / (a.x1 - a.x0)
978 local sy = (b.y1 - b.y0) / (a.y1 - a.y0)
982 tx = b.x0 - a.x0 * sx,
983 ty = b.y0 - a.y0 * sy
987 function place_rectangle_with_affine(resample_effect, resize_effect, padding_effect, pos, aff, screen_width, screen_height, input_width, input_height)
988 local x0 = pos.x0 * aff.sx + aff.tx
989 local x1 = pos.x1 * aff.sx + aff.tx
990 local y0 = pos.y0 * aff.sy + aff.ty
991 local y1 = pos.y1 * aff.sy + aff.ty
993 place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height)
996 function place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height)
1002 padding_effect:set_int("width", screen_width)
1003 padding_effect:set_int("height", screen_height)
1006 if x0 > screen_width or x1 < 0.0 or y0 > screen_height or y1 < 0.0 then
1007 if resample_effect ~= nil then
1008 resample_effect:set_int("width", 1)
1009 resample_effect:set_int("height", 1)
1010 resample_effect:set_float("zoom_x", screen_width)
1011 resample_effect:set_float("zoom_y", screen_height)
1013 resize_effect:set_int("width", 1)
1014 resize_effect:set_int("height", 1)
1016 padding_effect:set_int("left", screen_width + 100)
1017 padding_effect:set_int("top", screen_height + 100)
1023 srcx0 = -x0 / (x1 - x0)
1027 srcy0 = -y0 / (y1 - y0)
1030 if x1 > screen_width then
1031 srcx1 = (screen_width - x0) / (x1 - x0)
1034 if y1 > screen_height then
1035 srcy1 = (screen_height - y0) / (y1 - y0)
1039 if resample_effect ~= nil then
1040 -- High-quality resampling.
1041 local x_subpixel_offset = x0 - math.floor(x0)
1042 local y_subpixel_offset = y0 - math.floor(y0)
1044 -- Resampling must be to an integral number of pixels. Round up,
1045 -- and then add an extra pixel so we have some leeway for the border.
1046 local width = math.ceil(x1 - x0) + 1
1047 local height = math.ceil(y1 - y0) + 1
1048 resample_effect:set_int("width", width)
1049 resample_effect:set_int("height", height)
1051 -- Correct the discrepancy with zoom. (This will leave a small
1052 -- excess edge of pixels and subpixels, which we'll correct for soon.)
1053 local zoom_x = (x1 - x0) / (width * (srcx1 - srcx0))
1054 local zoom_y = (y1 - y0) / (height * (srcy1 - srcy0))
1055 resample_effect:set_float("zoom_x", zoom_x)
1056 resample_effect:set_float("zoom_y", zoom_y)
1057 resample_effect:set_float("zoom_center_x", 0.0)
1058 resample_effect:set_float("zoom_center_y", 0.0)
1060 -- Padding must also be to a whole-pixel offset.
1061 padding_effect:set_int("left", math.floor(x0))
1062 padding_effect:set_int("top", math.floor(y0))
1064 -- Correct _that_ discrepancy by subpixel offset in the resampling.
1065 resample_effect:set_float("left", srcx0 * input_width - x_subpixel_offset / zoom_x)
1066 resample_effect:set_float("top", srcy0 * input_height - y_subpixel_offset / zoom_y)
1068 -- Finally, adjust the border so it is exactly where we want it.
1069 padding_effect:set_float("border_offset_left", x_subpixel_offset)
1070 padding_effect:set_float("border_offset_right", x1 - (math.floor(x0) + width))
1071 padding_effect:set_float("border_offset_top", y_subpixel_offset)
1072 padding_effect:set_float("border_offset_bottom", y1 - (math.floor(y0) + height))
1074 -- Lower-quality simple resizing.
1075 local width = round(x1 - x0)
1076 local height = round(y1 - y0)
1077 resize_effect:set_int("width", width)
1078 resize_effect:set_int("height", height)
1080 -- Padding must also be to a whole-pixel offset.
1081 padding_effect:set_int("left", math.floor(x0))
1082 padding_effect:set_int("top", math.floor(y0))
1086 function prepare_overlay_live(chain, t, extra_alpha_factor)
1087 if chain.overlay then
1088 local tt = calc_fade_progress(t, overlay_transition_start, overlay_transition_end)
1089 overlay_alpha = overlay_alpha_src + tt * (overlay_alpha_dst - overlay_alpha_src)
1090 overlay_alpha = overlay_alpha * extra_alpha_factor
1091 --print("overlay_alpha=" .. overlay_alpha .. " [" .. overlay_alpha_src .. "," .. overlay_alpha_dst .. "]@" .. tt)
1092 if t > overlay_transition_end and overlay_alpha_dst == 0.0 then
1093 overlay_enabled = false -- Takes effect next frame.
1094 -- print("Turning off overlay")
1096 chain.overlay.multiply_effect:set_vec4("factor", overlay_alpha, overlay_alpha, overlay_alpha, overlay_alpha)
1100 function prepare_overlay_static(chain)
1101 if chain.overlay then
1102 chain.overlay.multiply_effect:set_vec4("factor", 1.0, 1.0, 1.0, 1.0)
1106 function set_neutral_color(effect, color)
1107 effect:set_vec3("neutral_color", color[1], color[2], color[3])
1110 function set_neutral_color_from_signal(effect, signal)
1111 if is_plain_signal(signal) then
1112 set_neutral_color(effect, neutral_colors[signal - INPUT0_SIGNAL_NUM + 1])
1116 function calc_zoom_progress(t)
1117 if t < transition_start then
1119 elseif t > transition_end then
1122 local tt = (t - transition_start) / (transition_end - transition_start)
1124 return math.sin(tt * 3.14159265358 * 0.5)
1128 function calc_fade_progress(t, transition_start, transition_end)
1129 local tt = (t - transition_start) / (transition_end - transition_start)
1132 elseif tt > 1.0 then
1136 -- Make the fade look maybe a tad more natural, by pumping it
1137 -- through a sigmoid function.
1138 tt = 10.0 * tt - 5.0
1139 tt = 1.0 / (1.0 + math.exp(-tt))
1145 { "Reload overlay", reload_cef },
1146 { "Disconnect IPTV", disconnect_iptv }