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.
18 local overlay_transition_start = -2.0
19 local overlay_transition_end = -1.0
20 local overlay_alpha_src = 0.0
21 local overlay_alpha_dst = 1.0
22 local overlay_enabled = false
24 local live_signal_num = 0
25 local preview_signal_num = 1
26 local NUM_CAMERAS = 6 -- Remember to update neutral_colors, too.
28 -- Valid values for live_signal_num and preview_signal_num.
29 local INPUT0_SIGNAL_NUM = 0
30 local INPUT1_SIGNAL_NUM = 1
31 local INPUT2_SIGNAL_NUM = 2
32 local INPUT3_SIGNAL_NUM = 3
33 local INPUT4_SIGNAL_NUM = 4
34 local INPUT5_SIGNAL_NUM = 5
35 local SBS_SIGNAL_NUM = NUM_CAMERAS
36 local STATIC_SIGNAL_NUM = NUM_CAMERAS + 1
37 local VIDEO_SIGNAL_NUM = NUM_CAMERAS + 2
39 -- Preview-only signal showing the current signal with the overlay.
40 -- Not valid for live_signal_num!
41 local OVERLAY_SIGNAL_NUM = NUM_CAMERAS + 3
43 -- Valid values for transition_type. (Cuts are done directly, so they need no entry.)
44 local NO_TRANSITION = 0
45 local ZOOM_TRANSITION = 1
46 local FADE_TRANSITION = 2
48 -- Last width/height/frame rate for each channel, if we have it.
49 -- Note that unlike the values we get from Nageru, the resolution is per
50 -- frame and not per field, since we deinterlace.
51 local last_resolution = {}
53 local cef_path = Nageru.THEME_PATH:match("(.*)/nageru/")
54 local cef_input = HTMLInput.new("file://" .. cef_path .. "/score.html")
55 cef_input:execute_javascript_async("play()")
57 local bg_video = VideoInput.new(cef_path .. "/flow-720.mp4", Nageru.VIDEO_FORMAT_YCBCR)
58 -- local iptv_video = VideoInput.new("http://10.34.129.69:9060/1/v.flv", Nageru.VIDEO_FORMAT_YCBCR)
59 local iptv_video = VideoInput.new("http://home.samfundet.no/~sesse/videostuff", Nageru.VIDEO_FORMAT_YCBCR)
63 cef_input:execute_javascript_async("play()")
66 -- Utility function to help creating many similar chains that can differ
67 -- in a free set of chosen parameters.
68 function make_cartesian_product(parms, callback)
69 return make_cartesian_product_internal(parms, callback, 1, {})
72 function make_cartesian_product_internal(parms, callback, index, args)
73 if index > #parms then
74 return callback(unpack(args))
77 for _, value in ipairs(parms[index]) do
79 ret[value] = make_cartesian_product_internal(parms, callback, index + 1, args)
84 -- An overlay with variable alpha.
85 function make_overlay(chain, base)
86 local image = chain:add_html_input(cef_input)
87 local multiply_effect = chain:add_effect(MultiplyEffect.new())
88 local overlay_effect = chain:add_effect(OverlayEffect.new(), base, multiply_effect)
91 multiply_effect = multiply_effect,
92 overlay_effect = overlay_effect
96 function possibly_make_overlay(has_overlay, chain, base)
97 if has_overlay == true then
98 return make_overlay(chain, base)
104 function make_fade_input(chain, signal, live, deint, scale, video)
105 local input, wb_effect, resample_effect, last
107 input = chain:add_video_input(iptv_video, false)
110 input = chain:add_live_input(false, deint)
111 input:connect_signal(signal)
114 input = chain:add_effect(ImageInput.new(cef_path .. "/nageru/dsn-bg.png"))
118 -- If we cared about this for the non-main inputs, we would have
119 -- checked hq here and invoked ResizeEffect instead.
121 resample_effect = chain:add_effect(ResampleEffect.new())
122 last = resample_effect
125 -- Make sure to put the white balance after the scaling (usually more efficient).
127 wb_effect = chain:add_effect(WhiteBalanceEffect.new())
133 wb_effect = wb_effect,
134 resample_effect = resample_effect,
139 -- A chain to fade between two inputs, of which either can be a picture
140 -- or a live input. In practice only used live, but we still support the
142 function make_fade_chain(input0_live, input0_deint, input0_scale, input0_video, input1_live, input1_deint, input1_scale, input1_video, has_overlay, hq)
143 local chain = EffectChain.new(16, 9)
145 local input0 = make_fade_input(chain, INPUT0_SIGNAL_NUM, input0_live, input0_deint, input0_scale, input0_video)
146 local input1 = make_fade_input(chain, INPUT1_SIGNAL_NUM, input1_live, input1_deint, input1_scale, input1_video)
148 -- If fading between two live inputs, the overlay is put on top.
149 -- If fading between the static picture and a live input,
150 -- the overlay is put on the live input.
152 if input0_live and not input1_live then
153 overlay = possibly_make_overlay(has_overlay, chain, input0.last)
155 input0.last = overlay.overlay_effect
157 elseif input1_live and not input0_live then
158 overlay = possibly_make_overlay(has_overlay, chain, input1.last)
160 input1.last = overlay.overlay_effect
164 local mix_effect = chain:add_effect(MixEffect.new(), input0.last, input1.last)
165 if input0_live and input1_live then
166 overlay = possibly_make_overlay(has_overlay, chain, mix_effect)
175 mix_effect = mix_effect,
180 -- Chains to fade between two inputs, in various configurations.
181 local fade_chains = make_cartesian_product({
182 {"video", "static", "live", "livedeint"}, -- input0_type
183 {true, false}, -- input0_scale
184 {"video", "static", "live", "livedeint"}, -- input1_type
185 {true, false}, -- input1_scale
186 {true, false}, -- has_overlay
188 }, function(input0_type, input0_scale, input1_type, input1_scale, has_overlay, hq)
189 local input0_live = (input0_type ~= "static")
190 local input1_live = (input1_type ~= "static")
191 local input0_deint = (input0_type == "livedeint")
192 local input1_deint = (input1_type == "livedeint")
193 local input0_video = (input0_type == "video")
194 local input1_video = (input1_type == "video")
195 return make_fade_chain(input0_live, input0_deint, input0_scale, input0_video, input1_live, input1_deint, input1_scale, input1_video, has_overlay, hq)
198 function make_sbs_input(chain, signal, deint, has_overlay, hq)
199 local input = chain:add_live_input(not deint, deint) -- Override bounce only if not deinterlacing.
200 input:connect_signal(signal)
204 overlay = make_overlay(chain, input)
207 local resample_effect = nil
208 local resize_effect = nil
210 resample_effect = chain:add_effect(ResampleEffect.new())
212 resize_effect = chain:add_effect(ResizeEffect.new())
214 local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
216 local padding_effect = chain:add_effect(IntegralPaddingEffect.new())
220 wb_effect = wb_effect,
221 resample_effect = resample_effect,
222 resize_effect = resize_effect,
223 padding_effect = padding_effect,
228 -- Side-by-side chains.
229 function make_sbs_chain(input0_type, input0_overlay, input1_type, hq)
230 local chain = EffectChain.new(16, 9)
232 local bg = chain:add_video_input(bg_video, false)
234 local input0 = make_sbs_input(chain, INPUT0_SIGNAL_NUM, input0_type == "livedeint", input0_overlay, hq)
235 local input1 = make_sbs_input(chain, INPUT4_SIGNAL_NUM, input1_type == "livedeint", false, hq)
237 input0.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0)
238 input1.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0)
240 local i0 = chain:add_effect(OverlayEffect.new(), bg, input0.padding_effect)
241 chain:add_effect(OverlayEffect.new(), i0, input1.padding_effect)
248 overlay = input0.overlay -- May be nil.
253 -- Make all possible combinations of side-by-side chains.
254 local sbs_chains = make_cartesian_product({
255 {"live", "livedeint"}, -- input0_type
256 {true, false}, -- input0_overlay
257 {"live", "livedeint"}, -- input1_type
259 }, function(input0_type, input0_overlay, input1_type, hq)
260 return make_sbs_chain(input0_type, input0_overlay, input1_type, hq)
263 -- A chain to show a single input on screen.
264 function make_simple_chain(input_deint, input_video, input_scale, has_overlay, hq)
265 local chain = EffectChain.new(16, 9)
269 input = chain:add_video_input(iptv_video, false)
271 input = chain:add_live_input(false, input_deint)
273 input:connect_signal(0) -- First input card. Can be changed whenever you want.
275 local resample_effect, resize_effect
278 resample_effect = chain:add_effect(ResampleEffect.new())
280 resize_effect = chain:add_effect(ResizeEffect.new())
284 local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
285 local overlay = possibly_make_overlay(has_overlay, chain, wb_effect)
292 wb_effect = wb_effect,
293 resample_effect = resample_effect,
294 resize_effect = resize_effect,
299 -- Make all possible combinations of single-input chains.
300 local simple_chains = make_cartesian_product({
301 {"video", "live", "livedeint"}, -- input_type
302 {true, false}, -- input_scale
303 {true, false}, -- has_overlay
305 }, function(input_type, input_scale, has_overlay, hq)
306 local input_deint = (input_type == "livedeint")
307 local input_video = (input_type == "video")
308 return make_simple_chain(input_deint, input_video, input_scale, has_overlay, hq)
311 -- A chain to show a single static picture on screen. Never with HTML overlay.
312 local static_chains = make_cartesian_product({
315 local chain = EffectChain.new(16, 9)
316 local chain_input = chain:add_effect(ImageInput.new(cef_path .. "/nageru/dsn-bg.png"))
324 -- A chain to show the overlay and nothing more. LQ only,
325 -- since it is not a valid live signal.
326 local overlay_chain_lq = EffectChain.new(16, 9)
327 local overlay_chain_lq_input = overlay_chain_lq:add_html_input(cef_input)
328 overlay_chain_lq:finalize(false)
330 -- Used for indexing into the tables of chains.
331 function get_input_type(signals, signal_num)
332 if signal_num == STATIC_SIGNAL_NUM then
334 elseif signal_num == VIDEO_SIGNAL_NUM then
336 elseif signals:get_interlaced(signal_num) then
343 function needs_scale(signals, signal_num, width, height)
344 if signal_num == STATIC_SIGNAL_NUM then
345 -- We assume this is already correctly scaled at load time.
348 assert(is_plain_signal(signal_num))
349 return (signals:get_width(signal_num) ~= width or signals:get_height(signal_num) ~= height)
352 function set_scale_parameters_if_needed(chain_or_input, width, height)
353 if chain_or_input.resample_effect then
354 chain_or_input.resample_effect:set_int("width", width)
355 chain_or_input.resample_effect:set_int("height", height)
356 elseif chain_or_input.resize_effect then
357 chain_or_input.resize_effect:set_int("width", width)
358 chain_or_input.resize_effect:set_int("height", height)
363 -- Returns the number of outputs in addition to the live (0) and preview (1).
364 -- Called only once, at the start of the program.
365 function num_channels()
366 return NUM_CAMERAS + 4 -- sbs, static picture, video and overlay
369 function is_plain_signal(num)
370 return (num >= INPUT0_SIGNAL_NUM and num <= INPUT5_SIGNAL_NUM) or (num == VIDEO_SIGNAL_NUM)
373 -- Helper function to write e.g. “720p60”. The difference between this
374 -- and get_channel_resolution_raw() is that this one also can say that
375 -- there's no signal.
376 function get_channel_resolution(signal_num)
377 res = last_resolution[signal_num]
378 if (not res) or not res.is_connected then
379 return "disconnected"
381 if res.height <= 0 then
384 if not res.has_signal then
385 if res.height == 525 then
386 -- Special mode for the USB3 cards.
389 return get_channel_resolution_raw(res) .. ", no signal"
391 return get_channel_resolution_raw(res)
395 -- Helper function to write e.g. “60” or “59.94”.
396 function get_frame_rate(res)
397 local nom = res.frame_rate_nom
398 local den = res.frame_rate_den
399 if nom % den == 0 then
402 return string.format("%.2f", nom / den)
406 -- Helper function to write e.g. “720p60”.
407 function get_channel_resolution_raw(res)
408 if res.interlaced then
409 return res.height .. "i" .. get_frame_rate(res)
411 return res.height .. "p" .. get_frame_rate(res)
416 -- Returns the name for each additional channel (starting from 2).
417 -- Called at the start of the program, and then each frame for live
418 -- channels in case they change resolution.
419 function channel_name(channel)
420 local signal_num = channel - 2
421 if signal_num == INPUT0_SIGNAL_NUM then
422 return "Main (" .. get_channel_resolution(signal_num) .. ")"
423 elseif signal_num == INPUT1_SIGNAL_NUM then
424 return "Secondary (" .. get_channel_resolution(signal_num) .. ")"
425 elseif signal_num == INPUT2_SIGNAL_NUM then
426 return "Goal L (" .. get_channel_resolution(signal_num) .. ")"
427 elseif signal_num == INPUT3_SIGNAL_NUM then
428 return "Goal R (" .. get_channel_resolution(signal_num) .. ")"
429 elseif signal_num == INPUT4_SIGNAL_NUM then
430 return "Commentators (" .. get_channel_resolution(signal_num) .. ")"
431 elseif signal_num == INPUT5_SIGNAL_NUM then
432 return "Laptop (" .. get_channel_resolution(signal_num) .. ")"
433 elseif signal_num == SBS_SIGNAL_NUM then
434 return "Side-by-side"
435 elseif signal_num == STATIC_SIGNAL_NUM then
436 return "Static picture"
437 elseif signal_num == VIDEO_SIGNAL_NUM then
439 elseif signal_num == OVERLAY_SIGNAL_NUM then
445 -- Returns, given a channel number, which signal it corresponds to (starting from 0).
446 -- Should return -1 if the channel does not correspond to a simple signal.
447 -- (The information is used for whether right-click on the channel should bring up
448 -- an input selector or not.)
449 -- Called once for each channel, at the start of the program.
450 -- Will never be called for live (0) or preview (1).
451 function channel_signal(channel)
452 if is_plain_signal(channel - 2) then
460 -- Called every frame. Returns the color (if any) to paint around the given
461 -- channel. Returns a CSS color (typically to mark live and preview signals);
462 -- "transparent" is allowed.
463 -- Will never be called for live (0) or preview (1).
464 function channel_color(channel)
465 if transition_type ~= NO_TRANSITION then
466 if channel_involved_in(channel, transition_src_signal) or
467 channel_involved_in(channel, transition_dst_signal) then
471 if channel_involved_in(channel, live_signal_num) then
475 if channel_involved_in(channel, preview_signal_num) then
481 function channel_involved_in(channel, signal_num)
482 if is_plain_signal(signal_num) then
483 return channel == (signal_num + 2)
485 if signal_num == SBS_SIGNAL_NUM then
486 return is_sbs_participating_signal(channel - 2)
488 if signal_num == STATIC_SIGNAL_NUM then
489 return (channel == NUM_CAMERAS)
491 if signal_num == VIDEO_SIGNAL_NUM then
492 return (channel == NUM_CAMERAS + 1)
498 -- Returns if a given channel supports setting white balance (starting from 2).
499 -- Called only once for each channel, at the start of the program.
500 function supports_set_wb(channel)
501 return is_plain_signal(channel - 2)
505 -- Gets called with a new gray point when the white balance is changing.
506 -- The color is in linear light (not sRGB gamma).
507 function set_wb(channel, red, green, blue)
508 if is_plain_signal(channel - 2) then
509 neutral_colors[channel - 2 + 1] = { red, green, blue }
513 function finish_transitions(t)
514 if transition_type ~= NO_TRANSITION and t >= transition_end then
515 live_signal_num = transition_dst_signal
516 transition_type = NO_TRANSITION
519 -- Disable the overlay if it is no longer visible.
520 if overlay_enabled and t > overlay_transition_end and overlay_alpha_dst == 0.0 then
521 overlay_enabled = false
522 print("Turning off overlay")
526 function in_transition(t)
527 return t >= transition_start and t <= transition_end
530 function is_sbs_participating_signal(signal_num)
531 return signal_num == INPUT0_SIGNAL_NUM or signal_num == INPUT4_SIGNAL_NUM
534 function simple_signal_has_overlay(signal_num)
535 -- The commentator output has no overlay on it.
536 return signal_num ~= INPUT4_SIGNAL_NUM
540 -- Called every frame.
541 function get_transitions(t)
542 if preview_signal_num == OVERLAY_SIGNAL_NUM then
543 if t < overlay_transition_end then
547 if overlay_enabled then
548 return {"Overlay off", "", "Fade ovl out"}
550 return {"Overlay on", "", "Fade ovl in"}
554 if in_transition(t) then
555 -- Transition already in progress, the only thing we can do is really
556 -- cut to the preview. (TODO: Make an “abort” and/or “finish”, too?)
560 if live_signal_num == preview_signal_num then
561 -- No transitions possible.
565 if (is_plain_signal(live_signal_num) or live_signal_num == STATIC_SIGNAL_NUM) and
566 (is_plain_signal(preview_signal_num) or preview_signal_num == STATIC_SIGNAL_NUM) then
567 return {"Cut", "", "Fade"}
571 if live_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(preview_signal_num) then
572 return {"Cut", "Zoom in"}
573 elseif is_sbs_participating_signal(live_signal_num) and preview_signal_num == SBS_SIGNAL_NUM then
574 return {"Cut", "Zoom out"}
580 function swap_preview_live()
581 local temp = live_signal_num
582 live_signal_num = preview_signal_num
583 preview_signal_num = temp
586 function start_transition(type_, t, duration)
588 transition_end = t + duration
589 transition_type = type_
590 transition_src_signal = live_signal_num
591 transition_dst_signal = preview_signal_num
596 -- Called when the user clicks a transition button.
597 function transition_clicked(num, t)
598 if preview_signal_num == OVERLAY_SIGNAL_NUM then
601 overlay_transition_start = -2.0
602 overlay_transition_end = -1.0
603 if overlay_enabled then
604 overlay_enabled = false
605 overlay_alpha_src = 0.0
606 overlay_alpha_dst = 0.0
608 overlay_enabled = true
609 overlay_alpha_src = 1.0
610 overlay_alpha_dst = 1.0
614 overlay_transition_start = t
615 overlay_transition_end = t + 1.0
616 if overlay_enabled then
617 overlay_alpha_src = 1.0
618 overlay_alpha_dst = 0.0
620 overlay_alpha_src = 0.0
621 overlay_alpha_dst = 1.0
623 overlay_enabled = true
630 if in_transition(t) then
631 -- Ongoing transition; finish it immediately before the cut.
632 finish_transitions(transition_end)
638 finish_transitions(t)
640 if live_signal_num == preview_signal_num then
645 if is_plain_signal(live_signal_num) and is_plain_signal(preview_signal_num) then
646 -- We can't zoom between these. Just make a cut.
647 io.write("Cutting from " .. live_signal_num .. " to " .. live_signal_num .. "\n")
652 if (live_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(preview_signal_num)) or
653 (preview_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(live_signal_num)) then
654 start_transition(ZOOM_TRANSITION, t, 1.0)
657 finish_transitions(t)
660 if (live_signal_num ~= preview_signal_num) and
661 (is_plain_signal(live_signal_num) or
662 live_signal_num == STATIC_SIGNAL_NUM) and
663 (is_plain_signal(preview_signal_num) or
664 preview_signal_num == STATIC_SIGNAL_NUM) then
665 start_transition(FADE_TRANSITION, t, 1.0)
667 -- Fades involving SBS are ignored (we have no chain for it).
673 function channel_clicked(num)
674 preview_signal_num = num
677 function get_fade_chain(signals, t, width, height, input_resolution)
678 local input0_type = get_input_type(signals, transition_src_signal)
679 local input0_scale = needs_scale(signals, transition_src_signal, width, height)
680 local input1_type = get_input_type(signals, transition_dst_signal)
681 local input1_scale = needs_scale(signals, transition_dst_signal, width, height)
682 local chain = fade_chains[input0_type][input0_scale][input1_type][input1_scale][overlay_enabled][true]
684 if input0_type == "live" or input0_type == "livedeint" then
685 chain.input0.input:connect_signal(transition_src_signal)
686 set_neutral_color_from_signal(chain.input0.wb_effect, transition_src_signal)
688 set_scale_parameters_if_needed(chain.input0, width, height)
689 if input1_type == "live" or input1_type == "livedeint" then
690 chain.input1.input:connect_signal(transition_dst_signal)
691 set_neutral_color_from_signal(chain.input1.wb_effect, transition_dst_signal)
693 set_scale_parameters_if_needed(chain.input1, width, height)
694 local tt = calc_fade_progress(t, transition_start, transition_end)
696 chain.mix_effect:set_float("strength_first", 1.0 - tt)
697 chain.mix_effect:set_float("strength_second", tt)
699 -- The commentator output has no overlay on it.
700 local extra_alpha_factor = 1.0
701 if not simple_signal_has_overlay(transition_src_signal) and
702 not simple_signal_has_overlay(transition_dst_signal) then
703 extra_alpha_factor = 0.0
704 elseif not simple_signal_has_overlay(transition_src_signal) then
705 extra_alpha_factor = tt
706 elseif not simple_signal_has_overlay(transition_dst_signal) then
707 extra_alpha_factor = 1.0 - tt
709 prepare_overlay_live(chain, t, extra_alpha_factor)
711 return chain.chain, prepare
714 -- SBS code (live_signal_num == SBS_SIGNAL_NUM, or in a transition to/from it).
715 function get_sbs_chain(signals, t, width, height, input_resolution)
716 local input0_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
717 local input1_type = get_input_type(signals, INPUT4_SIGNAL_NUM)
718 return sbs_chains[input0_type][overlay_enabled][input1_type][true]
721 local last_rate = 0.0
724 -- Called every frame. Get the chain for displaying at input <num>,
725 -- where 0 is live, 1 is preview, 2 is the first channel to display
726 -- in the bottom bar, and so on up to num_channels()+1. t is the
727 -- current time in seconds. width and height are the dimensions of
728 -- the output, although you can ignore them if you don't need them
729 -- (they're useful if you want to e.g. know what to resample by).
731 -- <signals> is basically an exposed InputState, which you can use to
732 -- query for information about the signals at the point of the current
733 -- frame. In particular, you can call get_width() and get_height()
734 -- for any signal number, and use that to e.g. assist in chain selection.
736 -- You should return two objects; the chain itself, and then a
737 -- function (taking no parameters) that is run just before rendering.
738 -- The function needs to call connect_signal on any inputs, so that
739 -- it gets updated video data for the given frame. (You are allowed
740 -- to switch which input your input is getting from between frames,
741 -- but not calling connect_signal results in undefined behavior.)
742 -- If you want to change any parameters in the chain, this is also
745 -- NOTE: The chain returned must be finalized with the Y'CbCr flag
746 -- if and only if num==0.
747 function get_chain(num, t, width, height, signals)
748 local input_resolution = {}
749 for signal_num=0,(NUM_CAMERAS-1) do
751 width = signals:get_width(signal_num),
752 height = signals:get_height(signal_num),
753 interlaced = signals:get_interlaced(signal_num),
754 has_signal = signals:get_has_signal(signal_num),
755 is_connected = signals:get_is_connected(signal_num),
756 frame_rate_nom = signals:get_frame_rate_nom(signal_num),
757 frame_rate_den = signals:get_frame_rate_den(signal_num)
760 if res.interlaced then
761 -- Convert height from frame height to field height.
762 -- (Needed for e.g. place_rectangle.)
763 res.height = res.height * 2
765 -- Show field rate instead of frame rate; really for cosmetics only
766 -- (and actually contrary to EBU recommendations, although in line
767 -- with typical user expectations).
768 res.frame_rate_nom = res.frame_rate_nom * 2
771 input_resolution[signal_num] = res
773 last_resolution = input_resolution
775 -- Save some CPU time if we're not having SBS on live.
777 if live_signal_num == SBS_SIGNAL_NUM or
778 preview_signal_num == SBS_SIGNAL_NUM or
779 transition_type == ZOOM_TRANSITION then
784 if new_rate ~= last_rate then
785 -- Avoid waking up the video thread (which may be sleeping) if the rate is the same.
786 bg_video:change_rate(new_rate)
790 if num == 0 then -- Live.
791 -- See if we're in a transition.
792 finish_transitions(t)
793 if transition_type == ZOOM_TRANSITION then
794 -- Transition in or out of SBS.
795 local chain = get_sbs_chain(signals, t, width, height, input_resolution)
797 prepare_sbs_chain(chain, calc_zoom_progress(t), transition_type, transition_src_signal, transition_dst_signal, width, height, input_resolution)
798 prepare_overlay_live(chain, t, 1.0)
800 return chain.chain, prepare
801 elseif transition_type == NO_TRANSITION and live_signal_num == SBS_SIGNAL_NUM then
803 local chain = get_sbs_chain(signals, t, width, height, input_resolution)
805 prepare_sbs_chain(chain, 0.0, NO_TRANSITION, 0, SBS_SIGNAL_NUM, width, height, input_resolution)
806 prepare_overlay_live(chain, t, 1.0)
808 return chain.chain, prepare
809 elseif transition_type == FADE_TRANSITION then
810 return get_fade_chain(signals, t, width, height, input_resolution)
811 elseif is_plain_signal(live_signal_num) then
812 local input_type = get_input_type(signals, live_signal_num)
813 local input_scale = needs_scale(signals, live_signal_num, width, height)
814 local overlay_really_enabled = overlay_enabled and simple_signal_has_overlay(live_signal_num)
815 local chain = simple_chains[input_type][input_scale][overlay_really_enabled][true]
817 if input_type ~= "video" then
818 chain.input:connect_signal(live_signal_num)
819 set_neutral_color_from_signal(chain.wb_effect, live_signal_num)
821 set_scale_parameters_if_needed(chain, width, height)
822 prepare_overlay_live(chain, t, 1.0)
824 return chain.chain, prepare
825 elseif live_signal_num == STATIC_SIGNAL_NUM then -- Static picture.
826 local chain = static_chains[true]
828 prepare_overlay_live(chain, t, 1.0) -- FIXME: Should this ever be here?
830 return chain.chain, prepare
836 -- We do not show overlays on the individual preview inputs.
837 -- The M/E preview matches what we'd put live by doing a transition, as always.
838 local show_overlay = false
839 if num == 1 then -- Preview.
840 if preview_signal_num == OVERLAY_SIGNAL_NUM then
841 num = live_signal_num + 2
842 show_overlay = not overlay_enabled
844 if transition_type ~= NO_TRANSITION then
845 num = transition_dst_signal + 2
848 num = preview_signal_num + 2
849 show_overlay = overlay_enabled and simple_signal_has_overlay(preview_signal_num)
853 -- Individual preview inputs (usually without overlay).
854 if is_plain_signal(num - 2) then
855 local signal_num = num - 2
856 local input_type = get_input_type(signals, signal_num)
857 local input_scale = needs_scale(signals, signal_num, width, height)
858 local chain = simple_chains[input_type][input_scale][show_overlay][false]
860 if input_type ~= "video" then
861 chain.input:connect_signal(signal_num)
862 set_neutral_color(chain.wb_effect, neutral_colors[signal_num + 1])
864 set_scale_parameters_if_needed(chain, width, height)
865 prepare_overlay_static(chain, t)
867 return chain.chain, prepare
869 if num == SBS_SIGNAL_NUM + 2 then
870 local input0_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
871 local input1_type = get_input_type(signals, INPUT4_SIGNAL_NUM)
872 local chain = sbs_chains[input0_type][show_overlay][input1_type][false]
874 prepare_sbs_chain(chain, 0.0, NO_TRANSITION, 0, SBS_SIGNAL_NUM, width, height, input_resolution)
876 return chain.chain, prepare
878 if num == STATIC_SIGNAL_NUM + 2 then
879 local chain = static_chains[false]
881 prepare_overlay_static(chain, t)
883 return chain.chain, prepare
885 if num == OVERLAY_SIGNAL_NUM + 2 then
887 -- prepare_overlay(overlay_chain_lq, t)
889 return overlay_chain_lq, prepare
893 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
895 return math.floor(x + 0.5)
898 function lerp(a, b, t)
899 return a + (b - a) * t
902 function lerp_pos(a, b, t)
904 x0 = lerp(a.x0, b.x0, t),
905 y0 = lerp(a.y0, b.y0, t),
906 x1 = lerp(a.x1, b.x1, t),
907 y1 = lerp(a.y1, b.y1, t)
911 function pos_from_top_left(x, y, width, height, screen_width, screen_height)
912 local xs = screen_width / 1280.0
913 local ys = screen_height / 720.0
917 x1 = round(xs * (x + width)),
918 y1 = round(ys * (y + height))
922 function prepare_sbs_chain(chain, t, transition_type, src_signal, dst_signal, screen_width, screen_height, input_resolution)
923 chain.input0.input:connect_signal(0)
924 chain.input1.input:connect_signal(4)
925 set_neutral_color(chain.input0.wb_effect, neutral_colors[1])
926 set_neutral_color(chain.input1.wb_effect, neutral_colors[5])
928 -- Both inputs are the same size (true side-by-side).
929 local pos0 = pos_from_top_left(1280 - 616 - 16, 186, 616, 347, screen_width, screen_height)
930 local pos1 = pos_from_top_left(16, 186, 616, 347, screen_width, screen_height)
932 local pos_fs = { x0 = 0, y0 = 0, x1 = screen_width, y1 = screen_height }
934 if transition_type == NO_TRANSITION then
936 affine_param = { sx = 1.0, sy = 1.0, tx = 0.0, ty = 0.0 } -- Identity.
938 -- Zooming to/from SBS view into or out of a single view.
939 assert(transition_type == ZOOM_TRANSITION)
941 if src_signal == SBS_SIGNAL_NUM then
945 assert(dst_signal == SBS_SIGNAL_NUM)
950 if signal == INPUT0_SIGNAL_NUM then
951 affine_param = find_affine_param(pos0, lerp_pos(pos0, pos_fs, real_t))
952 elseif signal == INPUT4_SIGNAL_NUM then
953 affine_param = find_affine_param(pos1, lerp_pos(pos1, pos_fs, real_t))
957 -- NOTE: input_resolution is not 1-indexed, unlike usual Lua arrays.
958 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)
959 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)
962 -- Find the transformation that changes the first rectangle to the second one.
963 function find_affine_param(a, b)
964 local sx = (b.x1 - b.x0) / (a.x1 - a.x0)
965 local sy = (b.y1 - b.y0) / (a.y1 - a.y0)
969 tx = b.x0 - a.x0 * sx,
970 ty = b.y0 - a.y0 * sy
974 function place_rectangle_with_affine(resample_effect, resize_effect, padding_effect, pos, aff, screen_width, screen_height, input_width, input_height)
975 local x0 = pos.x0 * aff.sx + aff.tx
976 local x1 = pos.x1 * aff.sx + aff.tx
977 local y0 = pos.y0 * aff.sy + aff.ty
978 local y1 = pos.y1 * aff.sy + aff.ty
980 place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height)
983 function place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height)
989 padding_effect:set_int("width", screen_width)
990 padding_effect:set_int("height", screen_height)
993 if x0 > screen_width or x1 < 0.0 or y0 > screen_height or y1 < 0.0 then
994 if resample_effect ~= nil then
995 resample_effect:set_int("width", 1)
996 resample_effect:set_int("height", 1)
997 resample_effect:set_float("zoom_x", screen_width)
998 resample_effect:set_float("zoom_y", screen_height)
1000 resize_effect:set_int("width", 1)
1001 resize_effect:set_int("height", 1)
1003 padding_effect:set_int("left", screen_width + 100)
1004 padding_effect:set_int("top", screen_height + 100)
1010 srcx0 = -x0 / (x1 - x0)
1014 srcy0 = -y0 / (y1 - y0)
1017 if x1 > screen_width then
1018 srcx1 = (screen_width - x0) / (x1 - x0)
1021 if y1 > screen_height then
1022 srcy1 = (screen_height - y0) / (y1 - y0)
1026 if resample_effect ~= nil then
1027 -- High-quality resampling.
1028 local x_subpixel_offset = x0 - math.floor(x0)
1029 local y_subpixel_offset = y0 - math.floor(y0)
1031 -- Resampling must be to an integral number of pixels. Round up,
1032 -- and then add an extra pixel so we have some leeway for the border.
1033 local width = math.ceil(x1 - x0) + 1
1034 local height = math.ceil(y1 - y0) + 1
1035 resample_effect:set_int("width", width)
1036 resample_effect:set_int("height", height)
1038 -- Correct the discrepancy with zoom. (This will leave a small
1039 -- excess edge of pixels and subpixels, which we'll correct for soon.)
1040 local zoom_x = (x1 - x0) / (width * (srcx1 - srcx0))
1041 local zoom_y = (y1 - y0) / (height * (srcy1 - srcy0))
1042 resample_effect:set_float("zoom_x", zoom_x)
1043 resample_effect:set_float("zoom_y", zoom_y)
1044 resample_effect:set_float("zoom_center_x", 0.0)
1045 resample_effect:set_float("zoom_center_y", 0.0)
1047 -- Padding must also be to a whole-pixel offset.
1048 padding_effect:set_int("left", math.floor(x0))
1049 padding_effect:set_int("top", math.floor(y0))
1051 -- Correct _that_ discrepancy by subpixel offset in the resampling.
1052 resample_effect:set_float("left", srcx0 * input_width - x_subpixel_offset / zoom_x)
1053 resample_effect:set_float("top", srcy0 * input_height - y_subpixel_offset / zoom_y)
1055 -- Finally, adjust the border so it is exactly where we want it.
1056 padding_effect:set_float("border_offset_left", x_subpixel_offset)
1057 padding_effect:set_float("border_offset_right", x1 - (math.floor(x0) + width))
1058 padding_effect:set_float("border_offset_top", y_subpixel_offset)
1059 padding_effect:set_float("border_offset_bottom", y1 - (math.floor(y0) + height))
1061 -- Lower-quality simple resizing.
1062 local width = round(x1 - x0)
1063 local height = round(y1 - y0)
1064 resize_effect:set_int("width", width)
1065 resize_effect:set_int("height", height)
1067 -- Padding must also be to a whole-pixel offset.
1068 padding_effect:set_int("left", math.floor(x0))
1069 padding_effect:set_int("top", math.floor(y0))
1073 function prepare_overlay_live(chain, t, extra_alpha_factor)
1074 if chain.overlay then
1075 local tt = calc_fade_progress(t, overlay_transition_start, overlay_transition_end)
1076 overlay_alpha = overlay_alpha_src + tt * (overlay_alpha_dst - overlay_alpha_src)
1077 overlay_alpha = overlay_alpha * extra_alpha_factor
1078 --print("overlay_alpha=" .. overlay_alpha .. " [" .. overlay_alpha_src .. "," .. overlay_alpha_dst .. "]@" .. tt)
1079 if t > overlay_transition_end and overlay_alpha_dst == 0.0 then
1080 overlay_enabled = false -- Takes effect next frame.
1081 -- print("Turning off overlay")
1083 chain.overlay.multiply_effect:set_vec4("factor", overlay_alpha, overlay_alpha, overlay_alpha, overlay_alpha)
1087 function prepare_overlay_static(chain)
1088 if chain.overlay then
1089 chain.overlay.multiply_effect:set_vec4("factor", 1.0, 1.0, 1.0, 1.0)
1093 function set_neutral_color(effect, color)
1094 effect:set_vec3("neutral_color", color[1], color[2], color[3])
1097 function set_neutral_color_from_signal(effect, signal)
1098 if is_plain_signal(signal) then
1099 set_neutral_color(effect, neutral_colors[signal - INPUT0_SIGNAL_NUM + 1])
1103 function calc_zoom_progress(t)
1104 if t < transition_start then
1106 elseif t > transition_end then
1109 local tt = (t - transition_start) / (transition_end - transition_start)
1111 return math.sin(tt * 3.14159265358 * 0.5)
1115 function calc_fade_progress(t, transition_start, transition_end)
1116 local tt = (t - transition_start) / (transition_end - transition_start)
1119 elseif tt > 1.0 then
1123 -- Make the fade look maybe a tad more natural, by pumping it
1124 -- through a sigmoid function.
1125 tt = 10.0 * tt - 5.0
1126 tt = 1.0 / (1.0 + math.exp(-tt))
1132 { "Reload overlay", reload_cef }