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.
15 local overlay_transition_start = -2.0
16 local overlay_transition_end = -1.0
17 local overlay_alpha_src = 0.0
18 local overlay_alpha_dst = 1.0
19 local overlay_enabled = false
21 local live_signal_num = 0
22 local preview_signal_num = 1
24 -- Valid values for live_signal_num and preview_signal_num.
25 local INPUT0_SIGNAL_NUM = 0
26 local INPUT1_SIGNAL_NUM = 1
27 local INPUT2_SIGNAL_NUM = 2
28 local STATIC_SIGNAL_NUM = 3
30 -- Preview-only signal showing the current signal with the overlay.
31 -- Not valid for live_signal_num!
32 local OVERLAY_SIGNAL_NUM = 4
34 -- Valid values for transition_type. (Cuts are done directly, so they need no entry.)
35 local NO_TRANSITION = 0
36 local FADE_TRANSITION = 2
38 -- Last width/height/frame rate for each channel, if we have it.
39 -- Note that unlike the values we get from Nageru, the resolution is per
40 -- frame and not per field, since we deinterlace.
41 local last_resolution = {}
43 local caspar_input = VideoInput.new("unix:///tmp/caspar.sock")
44 caspar_input:change_rate(2.0)
46 -- Utility function to help creating many similar chains that can differ
47 -- in a free set of chosen parameters.
48 function make_cartesian_product(parms, callback)
49 return make_cartesian_product_internal(parms, callback, 1, {})
52 function make_cartesian_product_internal(parms, callback, index, args)
53 if index > #parms then
54 return callback(unpack(args))
57 for _, value in ipairs(parms[index]) do
59 ret[value] = make_cartesian_product_internal(parms, callback, index + 1, args)
64 -- An overlay with variable alpha.
65 function make_overlay(chain, base)
66 local image = chain:add_video_input(caspar_input, false)
67 local multiply_effect = chain:add_effect(MultiplyEffect.new())
68 local overlay_effect = chain:add_effect(OverlayEffect.new(), base, multiply_effect)
71 multiply_effect = multiply_effect,
72 overlay_effect = overlay_effect
76 function possibly_make_overlay(has_overlay, chain, base)
77 if has_overlay == true then
78 return make_overlay(chain, base)
84 function make_fade_input(chain, signal, live, deint, scale)
85 local input, wb_effect, resample_effect, last
87 input = chain:add_live_input(false, deint)
88 input:connect_signal(signal)
91 input = chain:add_effect(ImageInput.new("tfk_pause.png"))
95 -- If we cared about this for the non-main inputs, we would have
96 -- checked hq here and invoked ResizeEffect instead.
98 resample_effect = chain:add_effect(ResampleEffect.new())
99 last = resample_effect
102 -- Make sure to put the white balance after the scaling (usually more efficient).
104 wb_effect = chain:add_effect(WhiteBalanceEffect.new())
110 wb_effect = wb_effect,
111 resample_effect = resample_effect,
116 -- A chain to fade between two inputs, of which either can be a picture
117 -- or a live input. In practice only used live, but we still support the
119 function make_fade_chain(input0_live, input0_deint, input0_scale, input1_live, input1_deint, input1_scale, has_overlay, hq)
120 local chain = EffectChain.new(16, 9)
122 local input0 = make_fade_input(chain, INPUT0_SIGNAL_NUM, input0_live, input0_deint, input0_scale)
123 local input1 = make_fade_input(chain, INPUT1_SIGNAL_NUM, input1_live, input1_deint, input1_scale)
125 -- If fading between two live inputs, the overlay is put on top.
126 -- If fading between the static picture and a live input,
127 -- the overlay is put on the live input.
129 if input0_live and not input1_live then
130 overlay = possibly_make_overlay(has_overlay, chain, input0.last)
132 input0.last = overlay.overlay_effect
134 elseif input1_live and not input0_live then
135 overlay = possibly_make_overlay(has_overlay, chain, input1.last)
137 input1.last = overlay.overlay_effect
141 local mix_effect = chain:add_effect(MixEffect.new(), input0.last, input1.last)
142 if input0_live and input1_live then
143 overlay = possibly_make_overlay(has_overlay, chain, mix_effect)
152 mix_effect = mix_effect,
157 -- Chains to fade between two inputs, in various configurations.
158 local fade_chains = make_cartesian_product({
159 {"static", "live", "livedeint"}, -- input0_type
160 {true, false}, -- input0_scale
161 {"static", "live", "livedeint"}, -- input1_type
162 {true, false}, -- input1_scale
163 {true, false}, -- has_overlay
165 }, function(input0_type, input0_scale, input1_type, input1_scale, has_overlay, hq)
166 local input0_live = (input0_type ~= "static")
167 local input1_live = (input1_type ~= "static")
168 local input0_deint = (input0_type == "livedeint")
169 local input1_deint = (input1_type == "livedeint")
170 return make_fade_chain(input0_live, input0_deint, input0_scale, input1_live, input1_deint, input1_scale, has_overlay, hq)
173 -- A chain to show a single input on screen.
174 function make_simple_chain(input_deint, input_scale, has_overlay, hq)
175 local chain = EffectChain.new(16, 9)
177 local input = chain:add_live_input(false, input_deint)
178 input:connect_signal(0) -- First input card. Can be changed whenever you want.
180 local resample_effect, resize_effect
183 resample_effect = chain:add_effect(ResampleEffect.new())
185 resize_effect = chain:add_effect(ResizeEffect.new())
189 local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
190 local overlay = possibly_make_overlay(has_overlay, chain, wb_effect)
197 wb_effect = wb_effect,
198 resample_effect = resample_effect,
199 resize_effect = resize_effect,
204 -- Make all possible combinations of single-input chains.
205 local simple_chains = make_cartesian_product({
206 {"live", "livedeint"}, -- input_type
207 {true, false}, -- input_scale
208 {true, false}, -- has_overlay
210 }, function(input_type, input_scale, has_overlay, hq)
211 local input_deint = (input_type == "livedeint")
212 return make_simple_chain(input_deint, input_scale, has_overlay, hq)
215 -- A chain to show a single static picture on screen. Never with CasparCG overlay.
216 local static_chains = make_cartesian_product({
219 local chain = EffectChain.new(16, 9)
220 local chain_input = chain:add_effect(ImageInput.new("tfk_pause.png"))
228 -- A chain to show the overlay and nothing more. LQ only,
229 -- since it is not a valid live signal.
230 local overlay_chain_lq = EffectChain.new(16, 9)
231 local overlay_chain_lq_input = overlay_chain_lq:add_video_input(caspar_input, false)
232 overlay_chain_lq:finalize(false)
234 -- Used for indexing into the tables of chains.
235 function get_input_type(signals, signal_num)
236 if signal_num == STATIC_SIGNAL_NUM then
238 elseif signals:get_interlaced(signal_num) then
245 function needs_scale(signals, signal_num, width, height)
246 if signal_num == STATIC_SIGNAL_NUM then
247 -- We assume this is already correctly scaled at load time.
250 assert(is_plain_signal(signal_num))
251 return (signals:get_width(signal_num) ~= width or signals:get_height(signal_num) ~= height)
254 function set_scale_parameters_if_needed(chain_or_input, width, height)
255 if chain_or_input.resample_effect then
256 chain_or_input.resample_effect:set_int("width", width)
257 chain_or_input.resample_effect:set_int("height", height)
258 elseif chain_or_input.resize_effect then
259 chain_or_input.resize_effect:set_int("width", width)
260 chain_or_input.resize_effect:set_int("height", height)
265 -- Returns the number of outputs in addition to the live (0) and preview (1).
266 -- Called only once, at the start of the program.
267 function num_channels()
271 function is_plain_signal(num)
272 return num >= INPUT0_SIGNAL_NUM and num <= INPUT2_SIGNAL_NUM
275 -- Helper function to write e.g. “720p60”. The difference between this
276 -- and get_channel_resolution_raw() is that this one also can say that
277 -- there's no signal.
278 function get_channel_resolution(signal_num)
279 res = last_resolution[signal_num]
280 if (not res) or not res.is_connected then
281 return "disconnected"
283 if res.height <= 0 then
286 if not res.has_signal then
287 if res.height == 525 then
288 -- Special mode for the USB3 cards.
291 return get_channel_resolution_raw(res) .. ", no signal"
293 return get_channel_resolution_raw(res)
297 -- Helper function to write e.g. “60” or “59.94”.
298 function get_frame_rate(res)
299 local nom = res.frame_rate_nom
300 local den = res.frame_rate_den
301 if nom % den == 0 then
304 return string.format("%.2f", nom / den)
308 -- Helper function to write e.g. “720p60”.
309 function get_channel_resolution_raw(res)
310 if res.interlaced then
311 return res.height .. "i" .. get_frame_rate(res)
313 return res.height .. "p" .. get_frame_rate(res)
318 -- Returns the name for each additional channel (starting from 2).
319 -- Called at the start of the program, and then each frame for live
320 -- channels in case they change resolution.
321 function channel_name(channel)
322 local signal_num = channel - 2
323 if signal_num == 0 then
324 return "Main (" .. get_channel_resolution(signal_num) .. ")"
325 elseif signal_num == 1 then
326 return "Goal 1 (" .. get_channel_resolution(signal_num) .. ")"
327 elseif signal_num == 2 then
328 return "Goal 2 (" .. get_channel_resolution(signal_num) .. ")"
329 elseif signal_num == STATIC_SIGNAL_NUM then
330 return "Static picture"
331 elseif signal_num == OVERLAY_SIGNAL_NUM then
337 -- Returns, given a channel number, which signal it corresponds to (starting from 0).
338 -- Should return -1 if the channel does not correspond to a simple signal.
339 -- (The information is used for whether right-click on the channel should bring up
340 -- an input selector or not.)
341 -- Called once for each channel, at the start of the program.
342 -- Will never be called for live (0) or preview (1).
343 function channel_signal(channel)
346 elseif channel == 3 then
354 -- Called every frame. Returns the color (if any) to paint around the given
355 -- channel. Returns a CSS color (typically to mark live and preview signals);
356 -- "transparent" is allowed.
357 -- Will never be called for live (0) or preview (1).
358 function channel_color(channel)
359 if transition_type ~= NO_TRANSITION then
360 if channel_involved_in(channel, transition_src_signal) or
361 channel_involved_in(channel, transition_dst_signal) then
365 if channel_involved_in(channel, live_signal_num) then
369 if channel_involved_in(channel, preview_signal_num) then
375 function channel_involved_in(channel, signal_num)
376 if is_plain_signal(signal_num) then
377 return channel == (signal_num + 2)
379 if signal_num == STATIC_SIGNAL_NUM then
380 return (channel == 5)
386 -- Returns if a given channel supports setting white balance (starting from 2).
387 -- Called only once for each channel, at the start of the program.
388 function supports_set_wb(channel)
389 return is_plain_signal(channel - 2)
393 -- Gets called with a new gray point when the white balance is changing.
394 -- The color is in linear light (not sRGB gamma).
395 function set_wb(channel, red, green, blue)
396 if is_plain_signal(channel - 2) then
397 neutral_colors[channel - 2 + 1] = { red, green, blue }
401 function finish_transitions(t)
402 if transition_type ~= NO_TRANSITION and t >= transition_end then
403 live_signal_num = transition_dst_signal
404 transition_type = NO_TRANSITION
407 -- Disable the overlay if it is no longer visible.
408 if overlay_enabled and t > overlay_transition_end and overlay_alpha_dst == 0.0 then
409 overlay_enabled = false
410 print("Turning off overlay")
414 function in_transition(t)
415 return t >= transition_start and t <= transition_end
419 -- Called every frame.
420 function get_transitions(t)
421 if preview_signal_num == OVERLAY_SIGNAL_NUM then
422 if t < overlay_transition_end then
426 if overlay_enabled then
427 return {"Overlay off", "", "Fade ovl out"}
429 return {"Overlay on", "", "Fade ovl in"}
433 if in_transition(t) then
434 -- Transition already in progress, the only thing we can do is really
435 -- cut to the preview. (TODO: Make an “abort” and/or “finish”, too?)
439 if live_signal_num == preview_signal_num then
440 -- No transitions possible.
444 if (is_plain_signal(live_signal_num) or live_signal_num == STATIC_SIGNAL_NUM) and
445 (is_plain_signal(preview_signal_num) or preview_signal_num == STATIC_SIGNAL_NUM) then
446 return {"Cut", "", "Fade"}
452 function swap_preview_live()
453 local temp = live_signal_num
454 live_signal_num = preview_signal_num
455 preview_signal_num = temp
458 function start_transition(type_, t, duration)
460 transition_end = t + duration
461 transition_type = type_
462 transition_src_signal = live_signal_num
463 transition_dst_signal = preview_signal_num
468 -- Called when the user clicks a transition button.
469 function transition_clicked(num, t)
470 if preview_signal_num == OVERLAY_SIGNAL_NUM then
473 overlay_transition_start = -2.0
474 overlay_transition_end = -1.0
475 if overlay_enabled then
476 overlay_enabled = false
477 overlay_alpha_src = 0.0
478 overlay_alpha_dst = 0.0
480 overlay_enabled = true
481 overlay_alpha_src = 1.0
482 overlay_alpha_dst = 1.0
486 overlay_transition_start = t
487 overlay_transition_end = t + 1.0
488 if overlay_enabled then
489 overlay_alpha_src = 1.0
490 overlay_alpha_dst = 0.0
492 overlay_alpha_src = 0.0
493 overlay_alpha_dst = 1.0
495 overlay_enabled = true
502 if in_transition(t) then
503 -- Ongoing transition; finish it immediately before the cut.
504 finish_transitions(transition_end)
510 finish_transitions(t)
512 if live_signal_num == preview_signal_num then
517 if is_plain_signal(live_signal_num) and is_plain_signal(preview_signal_num) then
518 -- We can't zoom between these. Just make a cut.
519 io.write("Cutting from " .. live_signal_num .. " to " .. live_signal_num .. "\n")
524 finish_transitions(t)
527 if (live_signal_num ~= preview_signal_num) and
528 (is_plain_signal(live_signal_num) or
529 live_signal_num == STATIC_SIGNAL_NUM) and
530 (is_plain_signal(preview_signal_num) or
531 preview_signal_num == STATIC_SIGNAL_NUM) then
532 start_transition(FADE_TRANSITION, t, 1.0)
534 -- Fades involving SBS are ignored (we have no chain for it).
540 function channel_clicked(num)
541 preview_signal_num = num
544 function get_fade_chain(signals, t, width, height, input_resolution)
545 local input0_type = get_input_type(signals, transition_src_signal)
546 local input0_scale = needs_scale(signals, transition_src_signal, width, height)
547 local input1_type = get_input_type(signals, transition_dst_signal)
548 local input1_scale = needs_scale(signals, transition_dst_signal, width, height)
549 local chain = fade_chains[input0_type][input0_scale][input1_type][input1_scale][overlay_enabled][true]
551 if input0_type == "live" or input0_type == "livedeint" then
552 chain.input0.input:connect_signal(transition_src_signal)
553 set_neutral_color_from_signal(chain.input0.wb_effect, transition_src_signal)
555 set_scale_parameters_if_needed(chain.input0, width, height)
556 if input1_type == "live" or input1_type == "livedeint" then
557 chain.input1.input:connect_signal(transition_dst_signal)
558 set_neutral_color_from_signal(chain.input1.wb_effect, transition_dst_signal)
560 set_scale_parameters_if_needed(chain.input1, width, height)
561 local tt = calc_fade_progress(t, transition_start, transition_end)
563 chain.mix_effect:set_float("strength_first", 1.0 - tt)
564 chain.mix_effect:set_float("strength_second", tt)
565 prepare_overlay_live(chain, t)
567 return chain.chain, prepare
571 -- Called every frame. Get the chain for displaying at input <num>,
572 -- where 0 is live, 1 is preview, 2 is the first channel to display
573 -- in the bottom bar, and so on up to num_channels()+1. t is the
574 -- current time in seconds. width and height are the dimensions of
575 -- the output, although you can ignore them if you don't need them
576 -- (they're useful if you want to e.g. know what to resample by).
578 -- <signals> is basically an exposed InputState, which you can use to
579 -- query for information about the signals at the point of the current
580 -- frame. In particular, you can call get_width() and get_height()
581 -- for any signal number, and use that to e.g. assist in chain selection.
583 -- You should return two objects; the chain itself, and then a
584 -- function (taking no parameters) that is run just before rendering.
585 -- The function needs to call connect_signal on any inputs, so that
586 -- it gets updated video data for the given frame. (You are allowed
587 -- to switch which input your input is getting from between frames,
588 -- but not calling connect_signal results in undefined behavior.)
589 -- If you want to change any parameters in the chain, this is also
592 -- NOTE: The chain returned must be finalized with the Y'CbCr flag
593 -- if and only if num==0.
594 function get_chain(num, t, width, height, signals)
595 local input_resolution = {}
596 for signal_num=0,2 do
598 width = signals:get_width(signal_num),
599 height = signals:get_height(signal_num),
600 interlaced = signals:get_interlaced(signal_num),
601 has_signal = signals:get_has_signal(signal_num),
602 is_connected = signals:get_is_connected(signal_num),
603 frame_rate_nom = signals:get_frame_rate_nom(signal_num),
604 frame_rate_den = signals:get_frame_rate_den(signal_num)
607 if res.interlaced then
608 -- Convert height from frame height to field height.
609 -- (Needed for e.g. place_rectangle.)
610 res.height = res.height * 2
612 -- Show field rate instead of frame rate; really for cosmetics only
613 -- (and actually contrary to EBU recommendations, although in line
614 -- with typical user expectations).
615 res.frame_rate_nom = res.frame_rate_nom * 2
618 input_resolution[signal_num] = res
620 last_resolution = input_resolution
622 if num == 0 then -- Live.
623 -- See if we're in a transition.
624 finish_transitions(t)
625 if transition_type == FADE_TRANSITION then
626 return get_fade_chain(signals, t, width, height, input_resolution)
627 elseif is_plain_signal(live_signal_num) then
628 local input_type = get_input_type(signals, live_signal_num)
629 local input_scale = needs_scale(signals, live_signal_num, width, height)
630 local chain = simple_chains[input_type][input_scale][overlay_enabled][true]
632 chain.input:connect_signal(live_signal_num)
633 set_scale_parameters_if_needed(chain, width, height)
634 set_neutral_color_from_signal(chain.wb_effect, live_signal_num)
635 prepare_overlay_live(chain, t)
637 return chain.chain, prepare
638 elseif live_signal_num == STATIC_SIGNAL_NUM then -- Static picture.
639 local chain = static_chains[true]
641 prepare_overlay_live(chain, t)
643 return chain.chain, prepare
649 -- We do not show overlays on the individual preview inputs.
650 -- The M/E preview matches what we'd put live by doing a transition, as always.
651 local show_overlay = false
652 if num == 1 then -- Preview.
653 if preview_signal_num == OVERLAY_SIGNAL_NUM then
654 num = live_signal_num + 2
655 show_overlay = not overlay_enabled
657 if transition_type ~= NO_TRANSITION then
658 num = transition_dst_signal + 2
661 num = preview_signal_num + 2
662 show_overlay = overlay_enabled
666 -- Individual preview inputs (usually without overlay).
667 if is_plain_signal(num - 2) then
668 local signal_num = num - 2
669 local input_type = get_input_type(signals, signal_num)
670 local input_scale = needs_scale(signals, signal_num, width, height)
671 local chain = simple_chains[input_type][input_scale][show_overlay][false]
673 chain.input:connect_signal(signal_num)
674 set_scale_parameters_if_needed(chain, width, height)
675 set_neutral_color(chain.wb_effect, neutral_colors[signal_num + 1])
676 prepare_overlay_static(chain, t)
678 return chain.chain, prepare
680 if num == STATIC_SIGNAL_NUM + 2 then
681 local chain = static_chains[false]
683 prepare_overlay_static(chain, t)
685 return chain.chain, prepare
687 if num == OVERLAY_SIGNAL_NUM + 2 then
689 -- prepare_overlay(overlay_chain_lq, t)
691 return overlay_chain_lq, prepare
695 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
697 return math.floor(x + 0.5)
700 function prepare_overlay_live(chain, t)
701 if chain.overlay then
702 local tt = calc_fade_progress(t, overlay_transition_start, overlay_transition_end)
703 overlay_alpha = overlay_alpha_src + tt * (overlay_alpha_dst - overlay_alpha_src)
704 --print("overlay_alpha=" .. overlay_alpha .. " [" .. overlay_alpha_src .. "," .. overlay_alpha_dst .. "]@" .. tt)
705 if t > overlay_transition_end and overlay_alpha_dst == 0.0 then
706 overlay_enabled = false -- Takes effect next frame.
707 -- print("Turning off overlay")
709 chain.overlay.multiply_effect:set_vec4("factor", overlay_alpha, overlay_alpha, overlay_alpha, overlay_alpha)
713 function prepare_overlay_static(chain)
714 if chain.overlay then
715 chain.overlay.multiply_effect:set_vec4("factor", 1.0, 1.0, 1.0, 1.0)
719 function set_neutral_color(effect, color)
720 effect:set_vec3("neutral_color", color[1], color[2], color[3])
723 function set_neutral_color_from_signal(effect, signal)
724 if is_plain_signal(signal) then
725 set_neutral_color(effect, neutral_colors[signal - INPUT0_SIGNAL_NUM + 1])
729 function calc_zoom_progress(t)
730 if t < transition_start then
732 elseif t > transition_end then
735 local tt = (t - transition_start) / (transition_end - transition_start)
737 return math.sin(tt * 3.14159265358 * 0.5)
741 function calc_fade_progress(t, transition_start, transition_end)
742 local tt = (t - transition_start) / (transition_end - transition_start)
749 -- Make the fade look maybe a tad more natural, by pumping it
750 -- through a sigmoid function.
752 tt = 1.0 / (1.0 + math.exp(-tt))