1 -- Nageru theme for TFK mini-tournament 2017, 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.
17 local overlay_transition_start = -2.0
18 local overlay_transition_end = -1.0
19 local overlay_alpha_src = 0.0
20 local overlay_alpha_dst = 1.0
21 local overlay_enabled = false
23 local live_signal_num = 0
24 local preview_signal_num = 1
25 local NUM_CAMERAS = 5 -- Remember to update neutral_colors, too.
27 -- Valid values for live_signal_num and preview_signal_num.
28 local INPUT0_SIGNAL_NUM = 0
29 local INPUT1_SIGNAL_NUM = 1
30 local INPUT2_SIGNAL_NUM = 2
31 local INPUT3_SIGNAL_NUM = 3
32 local INPUT4_SIGNAL_NUM = 4
33 local STATIC_SIGNAL_NUM = NUM_CAMERAS
35 -- Preview-only signal showing the current signal with the overlay.
36 -- Not valid for live_signal_num!
37 local OVERLAY_SIGNAL_NUM = NUM_CAMERAS + 1
39 -- Valid values for transition_type. (Cuts are done directly, so they need no entry.)
40 local NO_TRANSITION = 0
41 local FADE_TRANSITION = 2
43 -- Last width/height/frame rate for each channel, if we have it.
44 -- Note that unlike the values we get from Nageru, the resolution is per
45 -- frame and not per field, since we deinterlace.
46 local last_resolution = {}
48 local caspar_input = VideoInput.new("unix:///tmp/caspar.sock", Nageru.VIDEO_FORMAT_BGRA)
49 caspar_input:change_rate(2.0)
51 -- Utility function to help creating many similar chains that can differ
52 -- in a free set of chosen parameters.
53 function make_cartesian_product(parms, callback)
54 return make_cartesian_product_internal(parms, callback, 1, {})
57 function make_cartesian_product_internal(parms, callback, index, args)
58 if index > #parms then
59 return callback(unpack(args))
62 for _, value in ipairs(parms[index]) do
64 ret[value] = make_cartesian_product_internal(parms, callback, index + 1, args)
69 -- An overlay with variable alpha.
70 function make_overlay(chain, base)
71 local image = chain:add_video_input(caspar_input, false)
72 local multiply_effect = chain:add_effect(MultiplyEffect.new())
73 local overlay_effect = chain:add_effect(OverlayEffect.new(), base, multiply_effect)
76 multiply_effect = multiply_effect,
77 overlay_effect = overlay_effect
81 function possibly_make_overlay(has_overlay, chain, base)
82 if has_overlay == true then
83 return make_overlay(chain, base)
89 function make_fade_input(chain, signal, live, deint, scale)
90 local input, wb_effect, resample_effect, last
92 input = chain:add_live_input(false, deint)
93 input:connect_signal(signal)
96 input = chain:add_effect(ImageInput.new("tfk_pause.png"))
100 -- If we cared about this for the non-main inputs, we would have
101 -- checked hq here and invoked ResizeEffect instead.
103 resample_effect = chain:add_effect(ResampleEffect.new())
104 last = resample_effect
107 -- Make sure to put the white balance after the scaling (usually more efficient).
109 wb_effect = chain:add_effect(WhiteBalanceEffect.new())
115 wb_effect = wb_effect,
116 resample_effect = resample_effect,
121 -- A chain to fade between two inputs, of which either can be a picture
122 -- or a live input. In practice only used live, but we still support the
124 function make_fade_chain(input0_live, input0_deint, input0_scale, input1_live, input1_deint, input1_scale, has_overlay, hq)
125 local chain = EffectChain.new(16, 9)
127 local input0 = make_fade_input(chain, INPUT0_SIGNAL_NUM, input0_live, input0_deint, input0_scale)
128 local input1 = make_fade_input(chain, INPUT1_SIGNAL_NUM, input1_live, input1_deint, input1_scale)
130 -- If fading between two live inputs, the overlay is put on top.
131 -- If fading between the static picture and a live input,
132 -- the overlay is put on the live input.
134 if input0_live and not input1_live then
135 overlay = possibly_make_overlay(has_overlay, chain, input0.last)
137 input0.last = overlay.overlay_effect
139 elseif input1_live and not input0_live then
140 overlay = possibly_make_overlay(has_overlay, chain, input1.last)
142 input1.last = overlay.overlay_effect
146 local mix_effect = chain:add_effect(MixEffect.new(), input0.last, input1.last)
147 if input0_live and input1_live then
148 overlay = possibly_make_overlay(has_overlay, chain, mix_effect)
157 mix_effect = mix_effect,
162 -- Chains to fade between two inputs, in various configurations.
163 local fade_chains = make_cartesian_product({
164 {"static", "live", "livedeint"}, -- input0_type
165 {true, false}, -- input0_scale
166 {"static", "live", "livedeint"}, -- input1_type
167 {true, false}, -- input1_scale
168 {true, false}, -- has_overlay
170 }, function(input0_type, input0_scale, input1_type, input1_scale, has_overlay, hq)
171 local input0_live = (input0_type ~= "static")
172 local input1_live = (input1_type ~= "static")
173 local input0_deint = (input0_type == "livedeint")
174 local input1_deint = (input1_type == "livedeint")
175 return make_fade_chain(input0_live, input0_deint, input0_scale, input1_live, input1_deint, input1_scale, has_overlay, hq)
178 -- A chain to show a single input on screen.
179 function make_simple_chain(input_deint, input_scale, has_overlay, hq)
180 local chain = EffectChain.new(16, 9)
182 local input = chain:add_live_input(false, input_deint)
183 input:connect_signal(0) -- First input card. Can be changed whenever you want.
185 local resample_effect, resize_effect
188 resample_effect = chain:add_effect(ResampleEffect.new())
190 resize_effect = chain:add_effect(ResizeEffect.new())
194 local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
195 local overlay = possibly_make_overlay(has_overlay, chain, wb_effect)
202 wb_effect = wb_effect,
203 resample_effect = resample_effect,
204 resize_effect = resize_effect,
209 -- Make all possible combinations of single-input chains.
210 local simple_chains = make_cartesian_product({
211 {"live", "livedeint"}, -- input_type
212 {true, false}, -- input_scale
213 {true, false}, -- has_overlay
215 }, function(input_type, input_scale, has_overlay, hq)
216 local input_deint = (input_type == "livedeint")
217 return make_simple_chain(input_deint, input_scale, has_overlay, hq)
220 -- A chain to show a single static picture on screen. Never with CasparCG overlay.
221 local static_chains = make_cartesian_product({
224 local chain = EffectChain.new(16, 9)
225 local chain_input = chain:add_effect(ImageInput.new("tfk_pause.png"))
233 -- A chain to show the overlay and nothing more. LQ only,
234 -- since it is not a valid live signal.
235 local overlay_chain_lq = EffectChain.new(16, 9)
236 local overlay_chain_lq_input = overlay_chain_lq:add_video_input(caspar_input, false)
237 overlay_chain_lq:finalize(false)
239 -- Used for indexing into the tables of chains.
240 function get_input_type(signals, signal_num)
241 if signal_num == STATIC_SIGNAL_NUM then
243 elseif signals:get_interlaced(signal_num) then
250 function needs_scale(signals, signal_num, width, height)
251 if signal_num == STATIC_SIGNAL_NUM then
252 -- We assume this is already correctly scaled at load time.
255 assert(is_plain_signal(signal_num))
256 return (signals:get_width(signal_num) ~= width or signals:get_height(signal_num) ~= height)
259 function set_scale_parameters_if_needed(chain_or_input, width, height)
260 if chain_or_input.resample_effect then
261 chain_or_input.resample_effect:set_int("width", width)
262 chain_or_input.resample_effect:set_int("height", height)
263 elseif chain_or_input.resize_effect then
264 chain_or_input.resize_effect:set_int("width", width)
265 chain_or_input.resize_effect:set_int("height", height)
270 -- Returns the number of outputs in addition to the live (0) and preview (1).
271 -- Called only once, at the start of the program.
272 function num_channels()
273 return NUM_CAMERAS + 2 -- static picture and overlay
276 function is_plain_signal(num)
277 return num >= INPUT0_SIGNAL_NUM and num <= INPUT4_SIGNAL_NUM
280 -- Helper function to write e.g. “720p60”. The difference between this
281 -- and get_channel_resolution_raw() is that this one also can say that
282 -- there's no signal.
283 function get_channel_resolution(signal_num)
284 res = last_resolution[signal_num]
285 if (not res) or not res.is_connected then
286 return "disconnected"
288 if res.height <= 0 then
291 if not res.has_signal then
292 if res.height == 525 then
293 -- Special mode for the USB3 cards.
296 return get_channel_resolution_raw(res) .. ", no signal"
298 return get_channel_resolution_raw(res)
302 -- Helper function to write e.g. “60” or “59.94”.
303 function get_frame_rate(res)
304 local nom = res.frame_rate_nom
305 local den = res.frame_rate_den
306 if nom % den == 0 then
309 return string.format("%.2f", nom / den)
313 -- Helper function to write e.g. “720p60”.
314 function get_channel_resolution_raw(res)
315 if res.interlaced then
316 return res.height .. "i" .. get_frame_rate(res)
318 return res.height .. "p" .. get_frame_rate(res)
323 -- Returns the name for each additional channel (starting from 2).
324 -- Called at the start of the program, and then each frame for live
325 -- channels in case they change resolution.
326 function channel_name(channel)
327 local signal_num = channel - 2
328 if signal_num == INPUT0_SIGNAL_NUM then
329 return "Main (" .. get_channel_resolution(signal_num) .. ")"
330 elseif signal_num == INPUT1_SIGNAL_NUM then
331 return "Secondary (" .. get_channel_resolution(signal_num) .. ")"
332 elseif signal_num == INPUT2_SIGNAL_NUM then
333 return "Goal L (" .. get_channel_resolution(signal_num) .. ")"
334 elseif signal_num == INPUT3_SIGNAL_NUM then
335 return "Goal R (" .. get_channel_resolution(signal_num) .. ")"
336 elseif signal_num == INPUT4_SIGNAL_NUM then
337 return "Ambience (" .. get_channel_resolution(signal_num) .. ")"
338 elseif signal_num == STATIC_SIGNAL_NUM then
339 return "Static picture"
340 elseif signal_num == OVERLAY_SIGNAL_NUM then
346 -- Returns, given a channel number, which signal it corresponds to (starting from 0).
347 -- Should return -1 if the channel does not correspond to a simple signal.
348 -- (The information is used for whether right-click on the channel should bring up
349 -- an input selector or not.)
350 -- Called once for each channel, at the start of the program.
351 -- Will never be called for live (0) or preview (1).
352 function channel_signal(channel)
353 if is_plain_signal(channel - 2) then
361 -- Called every frame. Returns the color (if any) to paint around the given
362 -- channel. Returns a CSS color (typically to mark live and preview signals);
363 -- "transparent" is allowed.
364 -- Will never be called for live (0) or preview (1).
365 function channel_color(channel)
366 if transition_type ~= NO_TRANSITION then
367 if channel_involved_in(channel, transition_src_signal) or
368 channel_involved_in(channel, transition_dst_signal) then
372 if channel_involved_in(channel, live_signal_num) then
376 if channel_involved_in(channel, preview_signal_num) then
382 function channel_involved_in(channel, signal_num)
383 if is_plain_signal(signal_num) then
384 return channel == (signal_num + 2)
386 if signal_num == STATIC_SIGNAL_NUM then
387 return (channel == NUM_CAMERAS)
393 -- Returns if a given channel supports setting white balance (starting from 2).
394 -- Called only once for each channel, at the start of the program.
395 function supports_set_wb(channel)
396 return is_plain_signal(channel - 2)
400 -- Gets called with a new gray point when the white balance is changing.
401 -- The color is in linear light (not sRGB gamma).
402 function set_wb(channel, red, green, blue)
403 if is_plain_signal(channel - 2) then
404 neutral_colors[channel - 2 + 1] = { red, green, blue }
408 function finish_transitions(t)
409 if transition_type ~= NO_TRANSITION and t >= transition_end then
410 live_signal_num = transition_dst_signal
411 transition_type = NO_TRANSITION
414 -- Disable the overlay if it is no longer visible.
415 if overlay_enabled and t > overlay_transition_end and overlay_alpha_dst == 0.0 then
416 overlay_enabled = false
417 print("Turning off overlay")
421 function in_transition(t)
422 return t >= transition_start and t <= transition_end
426 -- Called every frame.
427 function get_transitions(t)
428 if preview_signal_num == OVERLAY_SIGNAL_NUM then
429 if t < overlay_transition_end then
433 if overlay_enabled then
434 return {"Overlay off", "", "Fade ovl out"}
436 return {"Overlay on", "", "Fade ovl in"}
440 if in_transition(t) then
441 -- Transition already in progress, the only thing we can do is really
442 -- cut to the preview. (TODO: Make an “abort” and/or “finish”, too?)
446 if live_signal_num == preview_signal_num then
447 -- No transitions possible.
451 if (is_plain_signal(live_signal_num) or live_signal_num == STATIC_SIGNAL_NUM) and
452 (is_plain_signal(preview_signal_num) or preview_signal_num == STATIC_SIGNAL_NUM) then
453 return {"Cut", "", "Fade"}
459 function swap_preview_live()
460 local temp = live_signal_num
461 live_signal_num = preview_signal_num
462 preview_signal_num = temp
465 function start_transition(type_, t, duration)
467 transition_end = t + duration
468 transition_type = type_
469 transition_src_signal = live_signal_num
470 transition_dst_signal = preview_signal_num
475 -- Called when the user clicks a transition button.
476 function transition_clicked(num, t)
477 if preview_signal_num == OVERLAY_SIGNAL_NUM then
480 overlay_transition_start = -2.0
481 overlay_transition_end = -1.0
482 if overlay_enabled then
483 overlay_enabled = false
484 overlay_alpha_src = 0.0
485 overlay_alpha_dst = 0.0
487 overlay_enabled = true
488 overlay_alpha_src = 1.0
489 overlay_alpha_dst = 1.0
493 overlay_transition_start = t
494 overlay_transition_end = t + 1.0
495 if overlay_enabled then
496 overlay_alpha_src = 1.0
497 overlay_alpha_dst = 0.0
499 overlay_alpha_src = 0.0
500 overlay_alpha_dst = 1.0
502 overlay_enabled = true
509 if in_transition(t) then
510 -- Ongoing transition; finish it immediately before the cut.
511 finish_transitions(transition_end)
517 finish_transitions(t)
519 if live_signal_num == preview_signal_num then
524 if is_plain_signal(live_signal_num) and is_plain_signal(preview_signal_num) then
525 -- We can't zoom between these. Just make a cut.
526 io.write("Cutting from " .. live_signal_num .. " to " .. live_signal_num .. "\n")
531 finish_transitions(t)
534 if (live_signal_num ~= preview_signal_num) and
535 (is_plain_signal(live_signal_num) or
536 live_signal_num == STATIC_SIGNAL_NUM) and
537 (is_plain_signal(preview_signal_num) or
538 preview_signal_num == STATIC_SIGNAL_NUM) then
539 start_transition(FADE_TRANSITION, t, 1.0)
541 -- Fades involving SBS are ignored (we have no chain for it).
547 function channel_clicked(num)
548 preview_signal_num = num
551 function get_fade_chain(signals, t, width, height, input_resolution)
552 local input0_type = get_input_type(signals, transition_src_signal)
553 local input0_scale = needs_scale(signals, transition_src_signal, width, height)
554 local input1_type = get_input_type(signals, transition_dst_signal)
555 local input1_scale = needs_scale(signals, transition_dst_signal, width, height)
556 local chain = fade_chains[input0_type][input0_scale][input1_type][input1_scale][overlay_enabled][true]
558 if input0_type == "live" or input0_type == "livedeint" then
559 chain.input0.input:connect_signal(transition_src_signal)
560 set_neutral_color_from_signal(chain.input0.wb_effect, transition_src_signal)
562 set_scale_parameters_if_needed(chain.input0, width, height)
563 if input1_type == "live" or input1_type == "livedeint" then
564 chain.input1.input:connect_signal(transition_dst_signal)
565 set_neutral_color_from_signal(chain.input1.wb_effect, transition_dst_signal)
567 set_scale_parameters_if_needed(chain.input1, width, height)
568 local tt = calc_fade_progress(t, transition_start, transition_end)
570 chain.mix_effect:set_float("strength_first", 1.0 - tt)
571 chain.mix_effect:set_float("strength_second", tt)
572 prepare_overlay_live(chain, t)
574 return chain.chain, prepare
578 -- Called every frame. Get the chain for displaying at input <num>,
579 -- where 0 is live, 1 is preview, 2 is the first channel to display
580 -- in the bottom bar, and so on up to num_channels()+1. t is the
581 -- current time in seconds. width and height are the dimensions of
582 -- the output, although you can ignore them if you don't need them
583 -- (they're useful if you want to e.g. know what to resample by).
585 -- <signals> is basically an exposed InputState, which you can use to
586 -- query for information about the signals at the point of the current
587 -- frame. In particular, you can call get_width() and get_height()
588 -- for any signal number, and use that to e.g. assist in chain selection.
590 -- You should return two objects; the chain itself, and then a
591 -- function (taking no parameters) that is run just before rendering.
592 -- The function needs to call connect_signal on any inputs, so that
593 -- it gets updated video data for the given frame. (You are allowed
594 -- to switch which input your input is getting from between frames,
595 -- but not calling connect_signal results in undefined behavior.)
596 -- If you want to change any parameters in the chain, this is also
599 -- NOTE: The chain returned must be finalized with the Y'CbCr flag
600 -- if and only if num==0.
601 function get_chain(num, t, width, height, signals)
602 local input_resolution = {}
603 for signal_num=0,(NUM_CAMERAS-1) do
605 width = signals:get_width(signal_num),
606 height = signals:get_height(signal_num),
607 interlaced = signals:get_interlaced(signal_num),
608 has_signal = signals:get_has_signal(signal_num),
609 is_connected = signals:get_is_connected(signal_num),
610 frame_rate_nom = signals:get_frame_rate_nom(signal_num),
611 frame_rate_den = signals:get_frame_rate_den(signal_num)
614 if res.interlaced then
615 -- Convert height from frame height to field height.
616 -- (Needed for e.g. place_rectangle.)
617 res.height = res.height * 2
619 -- Show field rate instead of frame rate; really for cosmetics only
620 -- (and actually contrary to EBU recommendations, although in line
621 -- with typical user expectations).
622 res.frame_rate_nom = res.frame_rate_nom * 2
625 input_resolution[signal_num] = res
627 last_resolution = input_resolution
629 if num == 0 then -- Live.
630 -- See if we're in a transition.
631 finish_transitions(t)
632 if transition_type == FADE_TRANSITION then
633 return get_fade_chain(signals, t, width, height, input_resolution)
634 elseif is_plain_signal(live_signal_num) then
635 local input_type = get_input_type(signals, live_signal_num)
636 local input_scale = needs_scale(signals, live_signal_num, width, height)
637 local chain = simple_chains[input_type][input_scale][overlay_enabled][true]
639 chain.input:connect_signal(live_signal_num)
640 set_scale_parameters_if_needed(chain, width, height)
641 set_neutral_color_from_signal(chain.wb_effect, live_signal_num)
642 prepare_overlay_live(chain, t)
644 return chain.chain, prepare
645 elseif live_signal_num == STATIC_SIGNAL_NUM then -- Static picture.
646 local chain = static_chains[true]
648 prepare_overlay_live(chain, t)
650 return chain.chain, prepare
656 -- We do not show overlays on the individual preview inputs.
657 -- The M/E preview matches what we'd put live by doing a transition, as always.
658 local show_overlay = false
659 if num == 1 then -- Preview.
660 if preview_signal_num == OVERLAY_SIGNAL_NUM then
661 num = live_signal_num + 2
662 show_overlay = not overlay_enabled
664 if transition_type ~= NO_TRANSITION then
665 num = transition_dst_signal + 2
668 num = preview_signal_num + 2
669 show_overlay = overlay_enabled
673 -- Individual preview inputs (usually without overlay).
674 if is_plain_signal(num - 2) then
675 local signal_num = num - 2
676 local input_type = get_input_type(signals, signal_num)
677 local input_scale = needs_scale(signals, signal_num, width, height)
678 local chain = simple_chains[input_type][input_scale][show_overlay][false]
680 chain.input:connect_signal(signal_num)
681 set_scale_parameters_if_needed(chain, width, height)
682 set_neutral_color(chain.wb_effect, neutral_colors[signal_num + 1])
683 prepare_overlay_static(chain, t)
685 return chain.chain, prepare
687 if num == STATIC_SIGNAL_NUM + 2 then
688 local chain = static_chains[false]
690 prepare_overlay_static(chain, t)
692 return chain.chain, prepare
694 if num == OVERLAY_SIGNAL_NUM + 2 then
696 -- prepare_overlay(overlay_chain_lq, t)
698 return overlay_chain_lq, prepare
702 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
704 return math.floor(x + 0.5)
707 function prepare_overlay_live(chain, t)
708 if chain.overlay then
709 local tt = calc_fade_progress(t, overlay_transition_start, overlay_transition_end)
710 overlay_alpha = overlay_alpha_src + tt * (overlay_alpha_dst - overlay_alpha_src)
711 --print("overlay_alpha=" .. overlay_alpha .. " [" .. overlay_alpha_src .. "," .. overlay_alpha_dst .. "]@" .. tt)
712 if t > overlay_transition_end and overlay_alpha_dst == 0.0 then
713 overlay_enabled = false -- Takes effect next frame.
714 -- print("Turning off overlay")
716 chain.overlay.multiply_effect:set_vec4("factor", overlay_alpha, overlay_alpha, overlay_alpha, overlay_alpha)
720 function prepare_overlay_static(chain)
721 if chain.overlay then
722 chain.overlay.multiply_effect:set_vec4("factor", 1.0, 1.0, 1.0, 1.0)
726 function set_neutral_color(effect, color)
727 effect:set_vec3("neutral_color", color[1], color[2], color[3])
730 function set_neutral_color_from_signal(effect, signal)
731 if is_plain_signal(signal) then
732 set_neutral_color(effect, neutral_colors[signal - INPUT0_SIGNAL_NUM + 1])
736 function calc_zoom_progress(t)
737 if t < transition_start then
739 elseif t > transition_end then
742 local tt = (t - transition_start) / (transition_end - transition_start)
744 return math.sin(tt * 3.14159265358 * 0.5)
748 function calc_fade_progress(t, transition_start, transition_end)
749 local tt = (t - transition_start) / (transition_end - transition_start)
756 -- Make the fade look maybe a tad more natural, by pumping it
757 -- through a sigmoid function.
759 tt = 1.0 / (1.0 + math.exp(-tt))