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 local mix_effect = chain:add_effect(MixEffect.new(), input0.last, input1.last)
126 local overlay = possibly_make_overlay(has_overlay, chain, mix_effect)
134 mix_effect = mix_effect,
139 -- Chains to fade between two inputs, in various configurations.
140 local fade_chains = make_cartesian_product({
141 {"static", "live", "livedeint"}, -- input0_type
142 {true, false}, -- input0_scale
143 {"static", "live", "livedeint"}, -- input1_type
144 {true, false}, -- input1_scale
145 {true, false}, -- has_overlay
147 }, function(input0_type, input0_scale, input1_type, input1_scale, has_overlay, hq)
148 local input0_live = (input0_type ~= "static")
149 local input1_live = (input1_type ~= "static")
150 local input0_deint = (input0_type == "livedeint")
151 local input1_deint = (input1_type == "livedeint")
152 return make_fade_chain(input0_live, input0_deint, input0_scale, input1_live, input1_deint, input1_scale, has_overlay, hq)
155 -- A chain to show a single input on screen.
156 function make_simple_chain(input_deint, input_scale, has_overlay, hq)
157 local chain = EffectChain.new(16, 9)
159 local input = chain:add_live_input(false, input_deint)
160 input:connect_signal(0) -- First input card. Can be changed whenever you want.
162 local resample_effect, resize_effect
165 resample_effect = chain:add_effect(ResampleEffect.new())
167 resize_effect = chain:add_effect(ResizeEffect.new())
171 local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
172 local overlay = possibly_make_overlay(has_overlay, chain, wb_effect)
179 wb_effect = wb_effect,
180 resample_effect = resample_effect,
181 resize_effect = resize_effect,
186 -- Make all possible combinations of single-input chains.
187 local simple_chains = make_cartesian_product({
188 {"live", "livedeint"}, -- input_type
189 {true, false}, -- input_scale
190 {true, false}, -- has_overlay
192 }, function(input_type, input_scale, has_overlay, hq)
193 local input_deint = (input_type == "livedeint")
194 return make_simple_chain(input_deint, input_scale, has_overlay, hq)
197 -- A chain to show a single static picture on screen.
198 local static_chains = make_cartesian_product({
199 {true, false}, -- has_overlay
201 }, function(has_overlay, hq)
202 local chain = EffectChain.new(16, 9)
203 local chain_input = chain:add_effect(ImageInput.new("tfk_pause.png"))
204 local overlay = possibly_make_overlay(has_overlay, chain, chain_input)
213 -- A chain to show the overlay and nothing more. LQ only,
214 -- since it is not a valid live signal.
215 local overlay_chain_lq = EffectChain.new(16, 9)
216 local overlay_chain_lq_input = overlay_chain_lq:add_video_input(caspar_input, false)
217 overlay_chain_lq:finalize(false)
219 -- Used for indexing into the tables of chains.
220 function get_input_type(signals, signal_num)
221 if signal_num == STATIC_SIGNAL_NUM then
223 elseif signals:get_interlaced(signal_num) then
230 function needs_scale(signals, signal_num, width, height)
231 if signal_num == STATIC_SIGNAL_NUM then
232 -- We assume this is already correctly scaled at load time.
235 assert(is_plain_signal(signal_num))
236 return (signals:get_width(signal_num) ~= width or signals:get_height(signal_num) ~= height)
239 function set_scale_parameters_if_needed(chain_or_input, width, height)
240 if chain_or_input.resample_effect then
241 chain_or_input.resample_effect:set_int("width", width)
242 chain_or_input.resample_effect:set_int("height", height)
243 elseif chain_or_input.resize_effect then
244 chain_or_input.resize_effect:set_int("width", width)
245 chain_or_input.resize_effect:set_int("height", height)
250 -- Returns the number of outputs in addition to the live (0) and preview (1).
251 -- Called only once, at the start of the program.
252 function num_channels()
256 function is_plain_signal(num)
257 return num >= INPUT0_SIGNAL_NUM and num <= INPUT2_SIGNAL_NUM
260 -- Helper function to write e.g. “720p60”. The difference between this
261 -- and get_channel_resolution_raw() is that this one also can say that
262 -- there's no signal.
263 function get_channel_resolution(signal_num)
264 res = last_resolution[signal_num]
265 if (not res) or not res.is_connected then
266 return "disconnected"
268 if res.height <= 0 then
271 if not res.has_signal then
272 if res.height == 525 then
273 -- Special mode for the USB3 cards.
276 return get_channel_resolution_raw(res) .. ", no signal"
278 return get_channel_resolution_raw(res)
282 -- Helper function to write e.g. “60” or “59.94”.
283 function get_frame_rate(res)
284 local nom = res.frame_rate_nom
285 local den = res.frame_rate_den
286 if nom % den == 0 then
289 return string.format("%.2f", nom / den)
293 -- Helper function to write e.g. “720p60”.
294 function get_channel_resolution_raw(res)
295 if res.interlaced then
296 return res.height .. "i" .. get_frame_rate(res)
298 return res.height .. "p" .. get_frame_rate(res)
303 -- Returns the name for each additional channel (starting from 2).
304 -- Called at the start of the program, and then each frame for live
305 -- channels in case they change resolution.
306 function channel_name(channel)
307 local signal_num = channel - 2
308 if signal_num == 0 then
309 return "Main (" .. get_channel_resolution(signal_num) .. ")"
310 elseif signal_num == 1 then
311 return "Goal 1 (" .. get_channel_resolution(signal_num) .. ")"
312 elseif signal_num == 2 then
313 return "Goal 2 (" .. get_channel_resolution(signal_num) .. ")"
314 elseif signal_num == STATIC_SIGNAL_NUM then
315 return "Static picture"
316 elseif signal_num == OVERLAY_SIGNAL_NUM then
322 -- Returns, given a channel number, which signal it corresponds to (starting from 0).
323 -- Should return -1 if the channel does not correspond to a simple signal.
324 -- (The information is used for whether right-click on the channel should bring up
325 -- an input selector or not.)
326 -- Called once for each channel, at the start of the program.
327 -- Will never be called for live (0) or preview (1).
328 function channel_signal(channel)
331 elseif channel == 3 then
339 -- Called every frame. Returns the color (if any) to paint around the given
340 -- channel. Returns a CSS color (typically to mark live and preview signals);
341 -- "transparent" is allowed.
342 -- Will never be called for live (0) or preview (1).
343 function channel_color(channel)
344 if transition_type ~= NO_TRANSITION then
345 if channel_involved_in(channel, transition_src_signal) or
346 channel_involved_in(channel, transition_dst_signal) then
350 if channel_involved_in(channel, live_signal_num) then
354 if channel_involved_in(channel, preview_signal_num) then
360 function channel_involved_in(channel, signal_num)
361 if is_plain_signal(signal_num) then
362 return channel == (signal_num + 2)
364 if signal_num == STATIC_SIGNAL_NUM then
365 return (channel == 5)
371 -- Returns if a given channel supports setting white balance (starting from 2).
372 -- Called only once for each channel, at the start of the program.
373 function supports_set_wb(channel)
374 return is_plain_signal(channel - 2)
378 -- Gets called with a new gray point when the white balance is changing.
379 -- The color is in linear light (not sRGB gamma).
380 function set_wb(channel, red, green, blue)
381 if is_plain_signal(channel - 2) then
382 neutral_colors[channel - 2 + 1] = { red, green, blue }
386 function finish_transitions(t)
387 if transition_type ~= NO_TRANSITION and t >= transition_end then
388 live_signal_num = transition_dst_signal
389 transition_type = NO_TRANSITION
392 -- Disable the overlay if it is no longer visible.
393 if overlay_enabled and t > overlay_transition_end and overlay_alpha_dst == 0.0 then
394 overlay_enabled = false
395 print("Turning off overlay")
399 function in_transition(t)
400 return t >= transition_start and t <= transition_end
404 -- Called every frame.
405 function get_transitions(t)
406 if preview_signal_num == OVERLAY_SIGNAL_NUM then
407 if t < overlay_transition_end then
411 if overlay_enabled then
412 return {"Overlay off", "", "Fade ovl out"}
414 return {"Overlay on", "", "Fade ovl in"}
418 if in_transition(t) then
419 -- Transition already in progress, the only thing we can do is really
420 -- cut to the preview. (TODO: Make an “abort” and/or “finish”, too?)
424 if live_signal_num == preview_signal_num then
425 -- No transitions possible.
429 if (is_plain_signal(live_signal_num) or live_signal_num == STATIC_SIGNAL_NUM) and
430 (is_plain_signal(preview_signal_num) or preview_signal_num == STATIC_SIGNAL_NUM) then
431 return {"Cut", "", "Fade"}
437 function swap_preview_live()
438 local temp = live_signal_num
439 live_signal_num = preview_signal_num
440 preview_signal_num = temp
443 function start_transition(type_, t, duration)
445 transition_end = t + duration
446 transition_type = type_
447 transition_src_signal = live_signal_num
448 transition_dst_signal = preview_signal_num
453 -- Called when the user clicks a transition button.
454 function transition_clicked(num, t)
455 if preview_signal_num == OVERLAY_SIGNAL_NUM then
458 overlay_transition_start = -2.0
459 overlay_transition_end = -1.0
460 if overlay_enabled then
461 overlay_enabled = false
462 overlay_alpha_src = 0.0
463 overlay_alpha_dst = 0.0
465 overlay_enabled = true
466 overlay_alpha_src = 1.0
467 overlay_alpha_dst = 1.0
471 overlay_transition_start = t
472 overlay_transition_end = t + 1.0
473 if overlay_enabled then
474 overlay_alpha_src = 1.0
475 overlay_alpha_dst = 0.0
477 overlay_alpha_src = 0.0
478 overlay_alpha_dst = 1.0
480 overlay_enabled = true
487 if in_transition(t) then
488 -- Ongoing transition; finish it immediately before the cut.
489 finish_transitions(transition_end)
495 finish_transitions(t)
497 if live_signal_num == preview_signal_num then
502 if is_plain_signal(live_signal_num) and is_plain_signal(preview_signal_num) then
503 -- We can't zoom between these. Just make a cut.
504 io.write("Cutting from " .. live_signal_num .. " to " .. live_signal_num .. "\n")
509 finish_transitions(t)
512 if (live_signal_num ~= preview_signal_num) and
513 (is_plain_signal(live_signal_num) or
514 live_signal_num == STATIC_SIGNAL_NUM) and
515 (is_plain_signal(preview_signal_num) or
516 preview_signal_num == STATIC_SIGNAL_NUM) then
517 start_transition(FADE_TRANSITION, t, 1.0)
519 -- Fades involving SBS are ignored (we have no chain for it).
525 function channel_clicked(num)
526 preview_signal_num = num
529 function get_fade_chain(signals, t, width, height, input_resolution)
530 local input0_type = get_input_type(signals, transition_src_signal)
531 local input0_scale = needs_scale(signals, transition_src_signal, width, height)
532 local input1_type = get_input_type(signals, transition_dst_signal)
533 local input1_scale = needs_scale(signals, transition_dst_signal, width, height)
534 local chain = fade_chains[input0_type][input0_scale][input1_type][input1_scale][overlay_enabled][true]
536 if input0_type == "live" or input0_type == "livedeint" then
537 chain.input0.input:connect_signal(transition_src_signal)
538 set_neutral_color_from_signal(chain.input0.wb_effect, transition_src_signal)
540 set_scale_parameters_if_needed(chain.input0, width, height)
541 if input1_type == "live" or input1_type == "livedeint" then
542 chain.input1.input:connect_signal(transition_dst_signal)
543 set_neutral_color_from_signal(chain.input1.wb_effect, transition_dst_signal)
545 set_scale_parameters_if_needed(chain.input1, width, height)
546 local tt = calc_fade_progress(t, transition_start, transition_end)
548 chain.mix_effect:set_float("strength_first", 1.0 - tt)
549 chain.mix_effect:set_float("strength_second", tt)
550 prepare_overlay_live(chain, t)
552 return chain.chain, prepare
556 -- Called every frame. Get the chain for displaying at input <num>,
557 -- where 0 is live, 1 is preview, 2 is the first channel to display
558 -- in the bottom bar, and so on up to num_channels()+1. t is the
559 -- current time in seconds. width and height are the dimensions of
560 -- the output, although you can ignore them if you don't need them
561 -- (they're useful if you want to e.g. know what to resample by).
563 -- <signals> is basically an exposed InputState, which you can use to
564 -- query for information about the signals at the point of the current
565 -- frame. In particular, you can call get_width() and get_height()
566 -- for any signal number, and use that to e.g. assist in chain selection.
568 -- You should return two objects; the chain itself, and then a
569 -- function (taking no parameters) that is run just before rendering.
570 -- The function needs to call connect_signal on any inputs, so that
571 -- it gets updated video data for the given frame. (You are allowed
572 -- to switch which input your input is getting from between frames,
573 -- but not calling connect_signal results in undefined behavior.)
574 -- If you want to change any parameters in the chain, this is also
577 -- NOTE: The chain returned must be finalized with the Y'CbCr flag
578 -- if and only if num==0.
579 function get_chain(num, t, width, height, signals)
580 local input_resolution = {}
581 for signal_num=0,2 do
583 width = signals:get_width(signal_num),
584 height = signals:get_height(signal_num),
585 interlaced = signals:get_interlaced(signal_num),
586 has_signal = signals:get_has_signal(signal_num),
587 is_connected = signals:get_is_connected(signal_num),
588 frame_rate_nom = signals:get_frame_rate_nom(signal_num),
589 frame_rate_den = signals:get_frame_rate_den(signal_num)
592 if res.interlaced then
593 -- Convert height from frame height to field height.
594 -- (Needed for e.g. place_rectangle.)
595 res.height = res.height * 2
597 -- Show field rate instead of frame rate; really for cosmetics only
598 -- (and actually contrary to EBU recommendations, although in line
599 -- with typical user expectations).
600 res.frame_rate_nom = res.frame_rate_nom * 2
603 input_resolution[signal_num] = res
605 last_resolution = input_resolution
607 if num == 0 then -- Live.
608 -- See if we're in a transition.
609 finish_transitions(t)
610 if transition_type == FADE_TRANSITION then
611 return get_fade_chain(signals, t, width, height, input_resolution)
612 elseif is_plain_signal(live_signal_num) then
613 local input_type = get_input_type(signals, live_signal_num)
614 local input_scale = needs_scale(signals, live_signal_num, width, height)
615 local chain = simple_chains[input_type][input_scale][overlay_enabled][true]
617 chain.input:connect_signal(live_signal_num)
618 set_scale_parameters_if_needed(chain, width, height)
619 set_neutral_color_from_signal(chain.wb_effect, live_signal_num)
620 prepare_overlay_live(chain, t)
622 return chain.chain, prepare
623 elseif live_signal_num == STATIC_SIGNAL_NUM then -- Static picture.
624 local chain = static_chains[overlay_enabled][true]
626 prepare_overlay_live(chain, t)
628 return chain.chain, prepare
634 -- We do not show overlays on the individual preview inputs.
635 -- The M/E preview matches what we'd put live by doing a transition, as always.
636 local show_overlay = false
637 if num == 1 then -- Preview.
638 if preview_signal_num == OVERLAY_SIGNAL_NUM then
639 num = live_signal_num + 2
640 show_overlay = not overlay_enabled
642 if transition_type ~= NO_TRANSITION then
643 num = transition_dst_signal + 2
646 num = preview_signal_num + 2
647 show_overlay = overlay_enabled
651 -- Individual preview inputs (usually without overlay).
652 if is_plain_signal(num - 2) then
653 local signal_num = num - 2
654 local input_type = get_input_type(signals, signal_num)
655 local input_scale = needs_scale(signals, signal_num, width, height)
656 local chain = simple_chains[input_type][input_scale][show_overlay][false]
658 chain.input:connect_signal(signal_num)
659 set_scale_parameters_if_needed(chain, width, height)
660 set_neutral_color(chain.wb_effect, neutral_colors[signal_num + 1])
661 prepare_overlay_static(chain, t)
663 return chain.chain, prepare
665 if num == STATIC_SIGNAL_NUM + 2 then
666 local chain = static_chains[show_overlay][false]
668 prepare_overlay_static(chain, t)
670 return chain.chain, prepare
672 if num == OVERLAY_SIGNAL_NUM + 2 then
674 -- prepare_overlay(overlay_chain_lq, t)
676 return overlay_chain_lq, prepare
680 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
682 return math.floor(x + 0.5)
685 function prepare_overlay_live(chain, t)
686 if chain.overlay then
687 local tt = calc_fade_progress(t, overlay_transition_start, overlay_transition_end)
688 overlay_alpha = overlay_alpha_src + tt * (overlay_alpha_dst - overlay_alpha_src)
689 --print("overlay_alpha=" .. overlay_alpha .. " [" .. overlay_alpha_src .. "," .. overlay_alpha_dst .. "]@" .. tt)
690 if t > overlay_transition_end and overlay_alpha_dst == 0.0 then
691 overlay_enabled = false -- Takes effect next frame.
692 -- print("Turning off overlay")
694 chain.overlay.multiply_effect:set_vec4("factor", overlay_alpha, overlay_alpha, overlay_alpha, overlay_alpha)
698 function prepare_overlay_static(chain)
699 if chain.overlay then
700 chain.overlay.multiply_effect:set_vec4("factor", 1.0, 1.0, 1.0, 1.0)
704 function set_neutral_color(effect, color)
705 effect:set_vec3("neutral_color", color[1], color[2], color[3])
708 function set_neutral_color_from_signal(effect, signal)
709 if is_plain_signal(signal) then
710 set_neutral_color(effect, neutral_colors[signal - INPUT0_SIGNAL_NUM + 1])
714 function calc_zoom_progress(t)
715 if t < transition_start then
717 elseif t > transition_end then
720 local tt = (t - transition_start) / (transition_end - transition_start)
722 return math.sin(tt * 3.14159265358 * 0.5)
726 function calc_fade_progress(t, transition_start, transition_end)
727 local tt = (t - transition_start) / (transition_end - transition_start)
734 -- Make the fade look maybe a tad more natural, by pumping it
735 -- through a sigmoid function.
737 tt = 1.0 / (1.0 + math.exp(-tt))