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 function disconnect_iptv()
67 iptv_video:disconnect()
70 -- Utility function to help creating many similar chains that can differ
71 -- in a free set of chosen parameters.
72 function make_cartesian_product(parms, callback)
73 return make_cartesian_product_internal(parms, callback, 1, {})
76 function make_cartesian_product_internal(parms, callback, index, args)
77 if index > #parms then
78 return callback(unpack(args))
81 for _, value in ipairs(parms[index]) do
83 ret[value] = make_cartesian_product_internal(parms, callback, index + 1, args)
88 -- An overlay with variable alpha.
89 function make_overlay(chain, base)
90 local image = chain:add_html_input(cef_input)
91 local multiply_effect = chain:add_effect(MultiplyEffect.new())
92 local overlay_effect = chain:add_effect(OverlayEffect.new(), base, multiply_effect)
95 multiply_effect = multiply_effect,
96 overlay_effect = overlay_effect
100 function possibly_make_overlay(has_overlay, chain, base)
101 if has_overlay == true then
102 return make_overlay(chain, base)
108 function make_fade_input(chain, signal, live, deint, scale, video)
109 local input, wb_effect, resample_effect, last
111 input = chain:add_video_input(iptv_video, false)
114 input = chain:add_live_input(false, deint)
115 input:connect_signal(signal)
118 input = chain:add_effect(ImageInput.new(cef_path .. "/nageru/dsn-bg.png"))
122 -- If we cared about this for the non-main inputs, we would have
123 -- checked hq here and invoked ResizeEffect instead.
125 resample_effect = chain:add_effect(ResampleEffect.new())
126 last = resample_effect
129 -- Make sure to put the white balance after the scaling (usually more efficient).
131 wb_effect = chain:add_effect(WhiteBalanceEffect.new())
137 wb_effect = wb_effect,
138 resample_effect = resample_effect,
143 -- A chain to fade between two inputs, of which either can be a picture
144 -- or a live input. In practice only used live, but we still support the
146 function make_fade_chain(input0_live, input0_deint, input0_scale, input0_video, input1_live, input1_deint, input1_scale, input1_video, has_overlay, hq)
147 local chain = EffectChain.new(16, 9)
149 local input0 = make_fade_input(chain, INPUT0_SIGNAL_NUM, input0_live, input0_deint, input0_scale, input0_video)
150 local input1 = make_fade_input(chain, INPUT1_SIGNAL_NUM, input1_live, input1_deint, input1_scale, input1_video)
152 -- If fading between two live inputs, the overlay is put on top.
153 -- If fading between the static picture and a live input,
154 -- the overlay is put on the live input.
156 if input0_live and not input1_live then
157 overlay = possibly_make_overlay(has_overlay, chain, input0.last)
159 input0.last = overlay.overlay_effect
161 elseif input1_live and not input0_live then
162 overlay = possibly_make_overlay(has_overlay, chain, input1.last)
164 input1.last = overlay.overlay_effect
168 local mix_effect = chain:add_effect(MixEffect.new(), input0.last, input1.last)
169 if input0_live and input1_live then
170 overlay = possibly_make_overlay(has_overlay, chain, mix_effect)
179 mix_effect = mix_effect,
184 -- Chains to fade between two inputs, in various configurations.
185 local fade_chains = make_cartesian_product({
186 {"video", "static", "live", "livedeint"}, -- input0_type
187 {true, false}, -- input0_scale
188 {"video", "static", "live", "livedeint"}, -- input1_type
189 {true, false}, -- input1_scale
190 {true, false}, -- has_overlay
192 }, function(input0_type, input0_scale, input1_type, input1_scale, has_overlay, hq)
193 local input0_live = (input0_type ~= "static")
194 local input1_live = (input1_type ~= "static")
195 local input0_deint = (input0_type == "livedeint")
196 local input1_deint = (input1_type == "livedeint")
197 local input0_video = (input0_type == "video")
198 local input1_video = (input1_type == "video")
199 return make_fade_chain(input0_live, input0_deint, input0_scale, input0_video, input1_live, input1_deint, input1_scale, input1_video, has_overlay, hq)
202 function make_sbs_input(chain, signal, deint, has_overlay, hq)
203 local input = chain:add_live_input(not deint, deint) -- Override bounce only if not deinterlacing.
204 input:connect_signal(signal)
208 overlay = make_overlay(chain, input)
211 local resample_effect = nil
212 local resize_effect = nil
214 resample_effect = chain:add_effect(ResampleEffect.new())
216 resize_effect = chain:add_effect(ResizeEffect.new())
218 local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
220 local padding_effect = chain:add_effect(IntegralPaddingEffect.new())
224 wb_effect = wb_effect,
225 resample_effect = resample_effect,
226 resize_effect = resize_effect,
227 padding_effect = padding_effect,
232 -- Side-by-side chains.
233 function make_sbs_chain(input0_type, input0_overlay, input1_type, hq)
234 local chain = EffectChain.new(16, 9)
236 local bg = chain:add_video_input(bg_video, false)
238 local input0 = make_sbs_input(chain, INPUT0_SIGNAL_NUM, input0_type == "livedeint", input0_overlay, hq)
239 local input1 = make_sbs_input(chain, INPUT4_SIGNAL_NUM, input1_type == "livedeint", false, hq)
241 input0.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0)
242 input1.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0)
244 local i0 = chain:add_effect(OverlayEffect.new(), bg, input0.padding_effect)
245 chain:add_effect(OverlayEffect.new(), i0, input1.padding_effect)
252 overlay = input0.overlay -- May be nil.
257 -- Make all possible combinations of side-by-side chains.
258 local sbs_chains = make_cartesian_product({
259 {"live", "livedeint"}, -- input0_type
260 {true, false}, -- input0_overlay
261 {"live", "livedeint"}, -- input1_type
263 }, function(input0_type, input0_overlay, input1_type, hq)
264 return make_sbs_chain(input0_type, input0_overlay, input1_type, hq)
267 -- A chain to show a single input on screen.
268 function make_simple_chain(input_deint, input_video, input_scale, has_overlay, hq)
269 local chain = EffectChain.new(16, 9)
273 input = chain:add_video_input(iptv_video, false)
275 input = chain:add_live_input(false, input_deint)
277 input:connect_signal(0) -- First input card. Can be changed whenever you want.
279 local resample_effect, resize_effect
282 resample_effect = chain:add_effect(ResampleEffect.new())
284 resize_effect = chain:add_effect(ResizeEffect.new())
288 local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
289 local overlay = possibly_make_overlay(has_overlay, chain, wb_effect)
296 wb_effect = wb_effect,
297 resample_effect = resample_effect,
298 resize_effect = resize_effect,
303 -- Make all possible combinations of single-input chains.
304 local simple_chains = make_cartesian_product({
305 {"video", "live", "livedeint"}, -- input_type
306 {true, false}, -- input_scale
307 {true, false}, -- has_overlay
309 }, function(input_type, input_scale, has_overlay, hq)
310 local input_deint = (input_type == "livedeint")
311 local input_video = (input_type == "video")
312 return make_simple_chain(input_deint, input_video, input_scale, has_overlay, hq)
315 -- A chain to show a single static picture on screen. Never with HTML overlay.
316 local static_chains = make_cartesian_product({
319 local chain = EffectChain.new(16, 9)
320 local chain_input = chain:add_effect(ImageInput.new(cef_path .. "/nageru/dsn-bg.png"))
328 -- A chain to show the overlay and nothing more. LQ only,
329 -- since it is not a valid live signal.
330 local overlay_chain_lq = EffectChain.new(16, 9)
331 local overlay_chain_lq_input = overlay_chain_lq:add_html_input(cef_input)
332 overlay_chain_lq:finalize(false)
334 -- Used for indexing into the tables of chains.
335 function get_input_type(signals, signal_num)
336 if signal_num == STATIC_SIGNAL_NUM then
338 elseif signal_num == VIDEO_SIGNAL_NUM then
340 elseif signals:get_interlaced(signal_num) then
347 function needs_scale(signals, signal_num, width, height)
348 if signal_num == STATIC_SIGNAL_NUM then
349 -- We assume this is already correctly scaled at load time.
352 assert(is_plain_signal(signal_num))
353 return (signals:get_width(signal_num) ~= width or signals:get_height(signal_num) ~= height)
356 function set_scale_parameters_if_needed(chain_or_input, width, height)
357 if chain_or_input.resample_effect then
358 chain_or_input.resample_effect:set_int("width", width)
359 chain_or_input.resample_effect:set_int("height", height)
360 elseif chain_or_input.resize_effect then
361 chain_or_input.resize_effect:set_int("width", width)
362 chain_or_input.resize_effect:set_int("height", height)
367 -- Returns the number of outputs in addition to the live (0) and preview (1).
368 -- Called only once, at the start of the program.
369 function num_channels()
370 return NUM_CAMERAS + 4 -- sbs, static picture, video and overlay
373 function is_plain_signal(num)
374 return (num >= INPUT0_SIGNAL_NUM and num <= INPUT5_SIGNAL_NUM) or (num == VIDEO_SIGNAL_NUM)
377 -- Helper function to write e.g. “720p60”. The difference between this
378 -- and get_channel_resolution_raw() is that this one also can say that
379 -- there's no signal.
380 function get_channel_resolution(signal_num)
381 res = last_resolution[signal_num]
382 if (not res) or not res.is_connected then
383 return "disconnected"
385 if res.height <= 0 then
388 if not res.has_signal then
389 if res.height == 525 then
390 -- Special mode for the USB3 cards.
393 return get_channel_resolution_raw(res) .. ", no signal"
395 return get_channel_resolution_raw(res)
399 -- Helper function to write e.g. “60” or “59.94”.
400 function get_frame_rate(res)
401 local nom = res.frame_rate_nom
402 local den = res.frame_rate_den
403 if nom % den == 0 then
406 return string.format("%.2f", nom / den)
410 -- Helper function to write e.g. “720p60”.
411 function get_channel_resolution_raw(res)
412 if res.interlaced then
413 return res.height .. "i" .. get_frame_rate(res)
415 return res.height .. "p" .. get_frame_rate(res)
420 -- Returns the name for each additional channel (starting from 2).
421 -- Called at the start of the program, and then each frame for live
422 -- channels in case they change resolution.
423 function channel_name(channel)
424 local signal_num = channel - 2
425 if signal_num == INPUT0_SIGNAL_NUM then
426 return "Main (" .. get_channel_resolution(signal_num) .. ")"
427 elseif signal_num == INPUT1_SIGNAL_NUM then
428 return "Secondary (" .. get_channel_resolution(signal_num) .. ")"
429 elseif signal_num == INPUT2_SIGNAL_NUM then
430 return "Goal L (" .. get_channel_resolution(signal_num) .. ")"
431 elseif signal_num == INPUT3_SIGNAL_NUM then
432 return "Goal R (" .. get_channel_resolution(signal_num) .. ")"
433 elseif signal_num == INPUT4_SIGNAL_NUM then
434 return "Commentators (" .. get_channel_resolution(signal_num) .. ")"
435 elseif signal_num == INPUT5_SIGNAL_NUM then
436 return "Laptop (" .. get_channel_resolution(signal_num) .. ")"
437 elseif signal_num == SBS_SIGNAL_NUM then
438 return "Side-by-side"
439 elseif signal_num == STATIC_SIGNAL_NUM then
440 return "Static picture"
441 elseif signal_num == VIDEO_SIGNAL_NUM then
443 elseif signal_num == OVERLAY_SIGNAL_NUM then
449 -- Returns, given a channel number, which signal it corresponds to (starting from 0).
450 -- Should return -1 if the channel does not correspond to a simple signal.
451 -- (The information is used for whether right-click on the channel should bring up
452 -- an input selector or not.)
453 -- Called once for each channel, at the start of the program.
454 -- Will never be called for live (0) or preview (1).
455 function channel_signal(channel)
456 if is_plain_signal(channel - 2) then
464 -- Called every frame. Returns the color (if any) to paint around the given
465 -- channel. Returns a CSS color (typically to mark live and preview signals);
466 -- "transparent" is allowed.
467 -- Will never be called for live (0) or preview (1).
468 function channel_color(channel)
469 if transition_type ~= NO_TRANSITION then
470 if channel_involved_in(channel, transition_src_signal) or
471 channel_involved_in(channel, transition_dst_signal) then
475 if channel_involved_in(channel, live_signal_num) then
479 if channel_involved_in(channel, preview_signal_num) then
485 function channel_involved_in(channel, signal_num)
486 if is_plain_signal(signal_num) then
487 return channel == (signal_num + 2)
489 if signal_num == SBS_SIGNAL_NUM then
490 return is_sbs_participating_signal(channel - 2)
492 if signal_num == STATIC_SIGNAL_NUM then
493 return (channel == NUM_CAMERAS)
495 if signal_num == VIDEO_SIGNAL_NUM then
496 return (channel == NUM_CAMERAS + 1)
502 -- Returns if a given channel supports setting white balance (starting from 2).
503 -- Called only once for each channel, at the start of the program.
504 function supports_set_wb(channel)
505 return is_plain_signal(channel - 2)
509 -- Gets called with a new gray point when the white balance is changing.
510 -- The color is in linear light (not sRGB gamma).
511 function set_wb(channel, red, green, blue)
512 if is_plain_signal(channel - 2) then
513 neutral_colors[channel - 2 + 1] = { red, green, blue }
517 function finish_transitions(t)
518 if transition_type ~= NO_TRANSITION and t >= transition_end then
519 live_signal_num = transition_dst_signal
520 transition_type = NO_TRANSITION
523 -- Disable the overlay if it is no longer visible.
524 if overlay_enabled and t > overlay_transition_end and overlay_alpha_dst == 0.0 then
525 overlay_enabled = false
526 print("Turning off overlay")
530 function in_transition(t)
531 return t >= transition_start and t <= transition_end
534 function is_sbs_participating_signal(signal_num)
535 return signal_num == INPUT0_SIGNAL_NUM or signal_num == INPUT4_SIGNAL_NUM
538 function simple_signal_has_overlay(signal_num)
539 -- The commentator output has no overlay on it.
540 return signal_num ~= INPUT4_SIGNAL_NUM
544 -- Called every frame.
545 function get_transitions(t)
546 if preview_signal_num == OVERLAY_SIGNAL_NUM then
547 if t < overlay_transition_end then
551 if overlay_enabled then
552 return {"Overlay off", "", "Fade ovl out"}
554 return {"Overlay on", "", "Fade ovl in"}
558 if in_transition(t) then
559 -- Transition already in progress, the only thing we can do is really
560 -- cut to the preview. (TODO: Make an “abort” and/or “finish”, too?)
564 if live_signal_num == preview_signal_num then
565 -- No transitions possible.
569 if (is_plain_signal(live_signal_num) or live_signal_num == STATIC_SIGNAL_NUM) and
570 (is_plain_signal(preview_signal_num) or preview_signal_num == STATIC_SIGNAL_NUM) then
571 return {"Cut", "", "Fade"}
575 if live_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(preview_signal_num) then
576 return {"Cut", "Zoom in"}
577 elseif is_sbs_participating_signal(live_signal_num) and preview_signal_num == SBS_SIGNAL_NUM then
578 return {"Cut", "Zoom out"}
584 function swap_preview_live()
585 local temp = live_signal_num
586 live_signal_num = preview_signal_num
587 preview_signal_num = temp
590 function start_transition(type_, t, duration)
592 transition_end = t + duration
593 transition_type = type_
594 transition_src_signal = live_signal_num
595 transition_dst_signal = preview_signal_num
600 -- Called when the user clicks a transition button.
601 function transition_clicked(num, t)
602 if preview_signal_num == OVERLAY_SIGNAL_NUM then
605 overlay_transition_start = -2.0
606 overlay_transition_end = -1.0
607 if overlay_enabled then
608 overlay_enabled = false
609 overlay_alpha_src = 0.0
610 overlay_alpha_dst = 0.0
612 overlay_enabled = true
613 overlay_alpha_src = 1.0
614 overlay_alpha_dst = 1.0
618 overlay_transition_start = t
619 overlay_transition_end = t + 1.0
620 if overlay_enabled then
621 overlay_alpha_src = 1.0
622 overlay_alpha_dst = 0.0
624 overlay_alpha_src = 0.0
625 overlay_alpha_dst = 1.0
627 overlay_enabled = true
634 if in_transition(t) then
635 -- Ongoing transition; finish it immediately before the cut.
636 finish_transitions(transition_end)
642 finish_transitions(t)
644 if live_signal_num == preview_signal_num then
649 if is_plain_signal(live_signal_num) and is_plain_signal(preview_signal_num) then
650 -- We can't zoom between these. Just make a cut.
651 io.write("Cutting from " .. live_signal_num .. " to " .. live_signal_num .. "\n")
656 if (live_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(preview_signal_num)) or
657 (preview_signal_num == SBS_SIGNAL_NUM and is_sbs_participating_signal(live_signal_num)) then
658 start_transition(ZOOM_TRANSITION, t, 1.0)
661 finish_transitions(t)
664 if (live_signal_num ~= preview_signal_num) and
665 (is_plain_signal(live_signal_num) or
666 live_signal_num == STATIC_SIGNAL_NUM) and
667 (is_plain_signal(preview_signal_num) or
668 preview_signal_num == STATIC_SIGNAL_NUM) then
669 start_transition(FADE_TRANSITION, t, 1.0)
671 -- Fades involving SBS are ignored (we have no chain for it).
677 function channel_clicked(num)
678 preview_signal_num = num
681 function get_fade_chain(signals, t, width, height, input_resolution)
682 local input0_type = get_input_type(signals, transition_src_signal)
683 local input0_scale = needs_scale(signals, transition_src_signal, width, height)
684 local input1_type = get_input_type(signals, transition_dst_signal)
685 local input1_scale = needs_scale(signals, transition_dst_signal, width, height)
686 local chain = fade_chains[input0_type][input0_scale][input1_type][input1_scale][overlay_enabled][true]
688 if input0_type == "live" or input0_type == "livedeint" then
689 chain.input0.input:connect_signal(transition_src_signal)
690 set_neutral_color_from_signal(chain.input0.wb_effect, transition_src_signal)
692 set_scale_parameters_if_needed(chain.input0, width, height)
693 if input1_type == "live" or input1_type == "livedeint" then
694 chain.input1.input:connect_signal(transition_dst_signal)
695 set_neutral_color_from_signal(chain.input1.wb_effect, transition_dst_signal)
697 set_scale_parameters_if_needed(chain.input1, width, height)
698 local tt = calc_fade_progress(t, transition_start, transition_end)
700 chain.mix_effect:set_float("strength_first", 1.0 - tt)
701 chain.mix_effect:set_float("strength_second", tt)
703 -- The commentator output has no overlay on it.
704 local extra_alpha_factor = 1.0
705 if not simple_signal_has_overlay(transition_src_signal) and
706 not simple_signal_has_overlay(transition_dst_signal) then
707 extra_alpha_factor = 0.0
708 elseif not simple_signal_has_overlay(transition_src_signal) then
709 extra_alpha_factor = tt
710 elseif not simple_signal_has_overlay(transition_dst_signal) then
711 extra_alpha_factor = 1.0 - tt
713 prepare_overlay_live(chain, t, extra_alpha_factor)
715 return chain.chain, prepare
718 -- SBS code (live_signal_num == SBS_SIGNAL_NUM, or in a transition to/from it).
719 function get_sbs_chain(signals, t, width, height, input_resolution)
720 local input0_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
721 local input1_type = get_input_type(signals, INPUT4_SIGNAL_NUM)
722 return sbs_chains[input0_type][overlay_enabled][input1_type][true]
725 local last_rate = 0.0
728 -- Called every frame. Get the chain for displaying at input <num>,
729 -- where 0 is live, 1 is preview, 2 is the first channel to display
730 -- in the bottom bar, and so on up to num_channels()+1. t is the
731 -- current time in seconds. width and height are the dimensions of
732 -- the output, although you can ignore them if you don't need them
733 -- (they're useful if you want to e.g. know what to resample by).
735 -- <signals> is basically an exposed InputState, which you can use to
736 -- query for information about the signals at the point of the current
737 -- frame. In particular, you can call get_width() and get_height()
738 -- for any signal number, and use that to e.g. assist in chain selection.
740 -- You should return two objects; the chain itself, and then a
741 -- function (taking no parameters) that is run just before rendering.
742 -- The function needs to call connect_signal on any inputs, so that
743 -- it gets updated video data for the given frame. (You are allowed
744 -- to switch which input your input is getting from between frames,
745 -- but not calling connect_signal results in undefined behavior.)
746 -- If you want to change any parameters in the chain, this is also
749 -- NOTE: The chain returned must be finalized with the Y'CbCr flag
750 -- if and only if num==0.
751 function get_chain(num, t, width, height, signals)
752 local input_resolution = {}
753 for signal_num=0,(NUM_CAMERAS-1) do
755 width = signals:get_width(signal_num),
756 height = signals:get_height(signal_num),
757 interlaced = signals:get_interlaced(signal_num),
758 has_signal = signals:get_has_signal(signal_num),
759 is_connected = signals:get_is_connected(signal_num),
760 frame_rate_nom = signals:get_frame_rate_nom(signal_num),
761 frame_rate_den = signals:get_frame_rate_den(signal_num)
764 if res.interlaced then
765 -- Convert height from frame height to field height.
766 -- (Needed for e.g. place_rectangle.)
767 res.height = res.height * 2
769 -- Show field rate instead of frame rate; really for cosmetics only
770 -- (and actually contrary to EBU recommendations, although in line
771 -- with typical user expectations).
772 res.frame_rate_nom = res.frame_rate_nom * 2
775 input_resolution[signal_num] = res
777 last_resolution = input_resolution
779 -- Save some CPU time if we're not having SBS on live.
781 if live_signal_num == SBS_SIGNAL_NUM or
782 preview_signal_num == SBS_SIGNAL_NUM or
783 transition_type == ZOOM_TRANSITION then
788 if new_rate ~= last_rate then
789 -- Avoid waking up the video thread (which may be sleeping) if the rate is the same.
790 bg_video:change_rate(new_rate)
794 if num == 0 then -- Live.
795 -- See if we're in a transition.
796 finish_transitions(t)
797 if transition_type == ZOOM_TRANSITION then
798 -- Transition in or out of SBS.
799 local chain = get_sbs_chain(signals, t, width, height, input_resolution)
801 prepare_sbs_chain(chain, calc_zoom_progress(t), transition_type, transition_src_signal, transition_dst_signal, width, height, input_resolution)
802 prepare_overlay_live(chain, t, 1.0)
804 return chain.chain, prepare
805 elseif transition_type == NO_TRANSITION and live_signal_num == SBS_SIGNAL_NUM then
807 local chain = get_sbs_chain(signals, t, width, height, input_resolution)
809 prepare_sbs_chain(chain, 0.0, NO_TRANSITION, 0, SBS_SIGNAL_NUM, width, height, input_resolution)
810 prepare_overlay_live(chain, t, 1.0)
812 return chain.chain, prepare
813 elseif transition_type == FADE_TRANSITION then
814 return get_fade_chain(signals, t, width, height, input_resolution)
815 elseif is_plain_signal(live_signal_num) then
816 local input_type = get_input_type(signals, live_signal_num)
817 local input_scale = needs_scale(signals, live_signal_num, width, height)
818 local overlay_really_enabled = overlay_enabled and simple_signal_has_overlay(live_signal_num)
819 local chain = simple_chains[input_type][input_scale][overlay_really_enabled][true]
821 if input_type ~= "video" then
822 chain.input:connect_signal(live_signal_num)
823 set_neutral_color_from_signal(chain.wb_effect, live_signal_num)
825 set_scale_parameters_if_needed(chain, width, height)
826 prepare_overlay_live(chain, t, 1.0)
828 return chain.chain, prepare
829 elseif live_signal_num == STATIC_SIGNAL_NUM then -- Static picture.
830 local chain = static_chains[true]
832 prepare_overlay_live(chain, t, 1.0) -- FIXME: Should this ever be here?
834 return chain.chain, prepare
840 -- We do not show overlays on the individual preview inputs.
841 -- The M/E preview matches what we'd put live by doing a transition, as always.
842 local show_overlay = false
843 if num == 1 then -- Preview.
844 if preview_signal_num == OVERLAY_SIGNAL_NUM then
845 num = live_signal_num + 2
846 show_overlay = not overlay_enabled
848 if transition_type ~= NO_TRANSITION then
849 num = transition_dst_signal + 2
852 num = preview_signal_num + 2
853 show_overlay = overlay_enabled and simple_signal_has_overlay(preview_signal_num)
857 -- Individual preview inputs (usually without overlay).
858 if is_plain_signal(num - 2) then
859 local signal_num = num - 2
860 local input_type = get_input_type(signals, signal_num)
861 local input_scale = needs_scale(signals, signal_num, width, height)
862 local chain = simple_chains[input_type][input_scale][show_overlay][false]
864 if input_type ~= "video" then
865 chain.input:connect_signal(signal_num)
866 set_neutral_color(chain.wb_effect, neutral_colors[signal_num + 1])
868 set_scale_parameters_if_needed(chain, width, height)
869 prepare_overlay_static(chain, t)
871 return chain.chain, prepare
873 if num == SBS_SIGNAL_NUM + 2 then
874 local input0_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
875 local input1_type = get_input_type(signals, INPUT4_SIGNAL_NUM)
876 local chain = sbs_chains[input0_type][show_overlay][input1_type][false]
878 prepare_sbs_chain(chain, 0.0, NO_TRANSITION, 0, SBS_SIGNAL_NUM, width, height, input_resolution)
880 return chain.chain, prepare
882 if num == STATIC_SIGNAL_NUM + 2 then
883 local chain = static_chains[false]
885 prepare_overlay_static(chain, t)
887 return chain.chain, prepare
889 if num == OVERLAY_SIGNAL_NUM + 2 then
891 -- prepare_overlay(overlay_chain_lq, t)
893 return overlay_chain_lq, prepare
897 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
899 return math.floor(x + 0.5)
902 function lerp(a, b, t)
903 return a + (b - a) * t
906 function lerp_pos(a, b, t)
908 x0 = lerp(a.x0, b.x0, t),
909 y0 = lerp(a.y0, b.y0, t),
910 x1 = lerp(a.x1, b.x1, t),
911 y1 = lerp(a.y1, b.y1, t)
915 function pos_from_top_left(x, y, width, height, screen_width, screen_height)
916 local xs = screen_width / 1280.0
917 local ys = screen_height / 720.0
921 x1 = round(xs * (x + width)),
922 y1 = round(ys * (y + height))
926 function prepare_sbs_chain(chain, t, transition_type, src_signal, dst_signal, screen_width, screen_height, input_resolution)
927 chain.input0.input:connect_signal(0)
928 chain.input1.input:connect_signal(4)
929 set_neutral_color(chain.input0.wb_effect, neutral_colors[1])
930 set_neutral_color(chain.input1.wb_effect, neutral_colors[5])
932 -- Both inputs are the same size (true side-by-side).
933 local pos0 = pos_from_top_left(1280 - 616 - 16, 186, 616, 347, screen_width, screen_height)
934 local pos1 = pos_from_top_left(16, 186, 616, 347, screen_width, screen_height)
936 local pos_fs = { x0 = 0, y0 = 0, x1 = screen_width, y1 = screen_height }
938 if transition_type == NO_TRANSITION then
940 affine_param = { sx = 1.0, sy = 1.0, tx = 0.0, ty = 0.0 } -- Identity.
942 -- Zooming to/from SBS view into or out of a single view.
943 assert(transition_type == ZOOM_TRANSITION)
945 if src_signal == SBS_SIGNAL_NUM then
949 assert(dst_signal == SBS_SIGNAL_NUM)
954 if signal == INPUT0_SIGNAL_NUM then
955 affine_param = find_affine_param(pos0, lerp_pos(pos0, pos_fs, real_t))
956 elseif signal == INPUT4_SIGNAL_NUM then
957 affine_param = find_affine_param(pos1, lerp_pos(pos1, pos_fs, real_t))
961 -- NOTE: input_resolution is not 1-indexed, unlike usual Lua arrays.
962 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)
963 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)
966 -- Find the transformation that changes the first rectangle to the second one.
967 function find_affine_param(a, b)
968 local sx = (b.x1 - b.x0) / (a.x1 - a.x0)
969 local sy = (b.y1 - b.y0) / (a.y1 - a.y0)
973 tx = b.x0 - a.x0 * sx,
974 ty = b.y0 - a.y0 * sy
978 function place_rectangle_with_affine(resample_effect, resize_effect, padding_effect, pos, aff, screen_width, screen_height, input_width, input_height)
979 local x0 = pos.x0 * aff.sx + aff.tx
980 local x1 = pos.x1 * aff.sx + aff.tx
981 local y0 = pos.y0 * aff.sy + aff.ty
982 local y1 = pos.y1 * aff.sy + aff.ty
984 place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height)
987 function place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height)
993 padding_effect:set_int("width", screen_width)
994 padding_effect:set_int("height", screen_height)
997 if x0 > screen_width or x1 < 0.0 or y0 > screen_height or y1 < 0.0 then
998 if resample_effect ~= nil then
999 resample_effect:set_int("width", 1)
1000 resample_effect:set_int("height", 1)
1001 resample_effect:set_float("zoom_x", screen_width)
1002 resample_effect:set_float("zoom_y", screen_height)
1004 resize_effect:set_int("width", 1)
1005 resize_effect:set_int("height", 1)
1007 padding_effect:set_int("left", screen_width + 100)
1008 padding_effect:set_int("top", screen_height + 100)
1014 srcx0 = -x0 / (x1 - x0)
1018 srcy0 = -y0 / (y1 - y0)
1021 if x1 > screen_width then
1022 srcx1 = (screen_width - x0) / (x1 - x0)
1025 if y1 > screen_height then
1026 srcy1 = (screen_height - y0) / (y1 - y0)
1030 if resample_effect ~= nil then
1031 -- High-quality resampling.
1032 local x_subpixel_offset = x0 - math.floor(x0)
1033 local y_subpixel_offset = y0 - math.floor(y0)
1035 -- Resampling must be to an integral number of pixels. Round up,
1036 -- and then add an extra pixel so we have some leeway for the border.
1037 local width = math.ceil(x1 - x0) + 1
1038 local height = math.ceil(y1 - y0) + 1
1039 resample_effect:set_int("width", width)
1040 resample_effect:set_int("height", height)
1042 -- Correct the discrepancy with zoom. (This will leave a small
1043 -- excess edge of pixels and subpixels, which we'll correct for soon.)
1044 local zoom_x = (x1 - x0) / (width * (srcx1 - srcx0))
1045 local zoom_y = (y1 - y0) / (height * (srcy1 - srcy0))
1046 resample_effect:set_float("zoom_x", zoom_x)
1047 resample_effect:set_float("zoom_y", zoom_y)
1048 resample_effect:set_float("zoom_center_x", 0.0)
1049 resample_effect:set_float("zoom_center_y", 0.0)
1051 -- Padding must also be to a whole-pixel offset.
1052 padding_effect:set_int("left", math.floor(x0))
1053 padding_effect:set_int("top", math.floor(y0))
1055 -- Correct _that_ discrepancy by subpixel offset in the resampling.
1056 resample_effect:set_float("left", srcx0 * input_width - x_subpixel_offset / zoom_x)
1057 resample_effect:set_float("top", srcy0 * input_height - y_subpixel_offset / zoom_y)
1059 -- Finally, adjust the border so it is exactly where we want it.
1060 padding_effect:set_float("border_offset_left", x_subpixel_offset)
1061 padding_effect:set_float("border_offset_right", x1 - (math.floor(x0) + width))
1062 padding_effect:set_float("border_offset_top", y_subpixel_offset)
1063 padding_effect:set_float("border_offset_bottom", y1 - (math.floor(y0) + height))
1065 -- Lower-quality simple resizing.
1066 local width = round(x1 - x0)
1067 local height = round(y1 - y0)
1068 resize_effect:set_int("width", width)
1069 resize_effect:set_int("height", height)
1071 -- Padding must also be to a whole-pixel offset.
1072 padding_effect:set_int("left", math.floor(x0))
1073 padding_effect:set_int("top", math.floor(y0))
1077 function prepare_overlay_live(chain, t, extra_alpha_factor)
1078 if chain.overlay then
1079 local tt = calc_fade_progress(t, overlay_transition_start, overlay_transition_end)
1080 overlay_alpha = overlay_alpha_src + tt * (overlay_alpha_dst - overlay_alpha_src)
1081 overlay_alpha = overlay_alpha * extra_alpha_factor
1082 --print("overlay_alpha=" .. overlay_alpha .. " [" .. overlay_alpha_src .. "," .. overlay_alpha_dst .. "]@" .. tt)
1083 if t > overlay_transition_end and overlay_alpha_dst == 0.0 then
1084 overlay_enabled = false -- Takes effect next frame.
1085 -- print("Turning off overlay")
1087 chain.overlay.multiply_effect:set_vec4("factor", overlay_alpha, overlay_alpha, overlay_alpha, overlay_alpha)
1091 function prepare_overlay_static(chain)
1092 if chain.overlay then
1093 chain.overlay.multiply_effect:set_vec4("factor", 1.0, 1.0, 1.0, 1.0)
1097 function set_neutral_color(effect, color)
1098 effect:set_vec3("neutral_color", color[1], color[2], color[3])
1101 function set_neutral_color_from_signal(effect, signal)
1102 if is_plain_signal(signal) then
1103 set_neutral_color(effect, neutral_colors[signal - INPUT0_SIGNAL_NUM + 1])
1107 function calc_zoom_progress(t)
1108 if t < transition_start then
1110 elseif t > transition_end then
1113 local tt = (t - transition_start) / (transition_end - transition_start)
1115 return math.sin(tt * 3.14159265358 * 0.5)
1119 function calc_fade_progress(t, transition_start, transition_end)
1120 local tt = (t - transition_start) / (transition_end - transition_start)
1123 elseif tt > 1.0 then
1127 -- Make the fade look maybe a tad more natural, by pumping it
1128 -- through a sigmoid function.
1129 tt = 10.0 * tt - 5.0
1130 tt = 1.0 / (1.0 + math.exp(-tt))
1136 { "Reload overlay", reload_cef },
1137 { "Disconnect IPTV", disconnect_iptv }