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 is_plain_signal(signal_num) then
309 return "Input " .. (signal_num + 1) .. " (" .. get_channel_resolution(signal_num) .. ")"
310 elseif signal_num == STATIC_SIGNAL_NUM then
311 return "Static picture"
312 elseif signal_num == OVERLAY_SIGNAL_NUM then
318 -- Returns, given a channel number, which signal it corresponds to (starting from 0).
319 -- Should return -1 if the channel does not correspond to a simple signal.
320 -- (The information is used for whether right-click on the channel should bring up
321 -- an input selector or not.)
322 -- Called once for each channel, at the start of the program.
323 -- Will never be called for live (0) or preview (1).
324 function channel_signal(channel)
327 elseif channel == 3 then
335 -- Called every frame. Returns the color (if any) to paint around the given
336 -- channel. Returns a CSS color (typically to mark live and preview signals);
337 -- "transparent" is allowed.
338 -- Will never be called for live (0) or preview (1).
339 function channel_color(channel)
340 if transition_type ~= NO_TRANSITION then
341 if channel_involved_in(channel, transition_src_signal) or
342 channel_involved_in(channel, transition_dst_signal) then
346 if channel_involved_in(channel, live_signal_num) then
350 if channel_involved_in(channel, preview_signal_num) then
356 function channel_involved_in(channel, signal_num)
357 if is_plain_signal(signal_num) then
358 return channel == (signal_num + 2)
360 if signal_num == STATIC_SIGNAL_NUM then
361 return (channel == 5)
367 -- Returns if a given channel supports setting white balance (starting from 2).
368 -- Called only once for each channel, at the start of the program.
369 function supports_set_wb(channel)
370 return is_plain_signal(channel - 2)
374 -- Gets called with a new gray point when the white balance is changing.
375 -- The color is in linear light (not sRGB gamma).
376 function set_wb(channel, red, green, blue)
377 if is_plain_signal(channel - 2) then
378 neutral_colors[channel - 2 + 1] = { red, green, blue }
382 function finish_transitions(t)
383 if transition_type ~= NO_TRANSITION and t >= transition_end then
384 live_signal_num = transition_dst_signal
385 transition_type = NO_TRANSITION
388 -- Disable the overlay if it is no longer visible.
389 if overlay_enabled and t > overlay_transition_end and overlay_alpha_dst == 0.0 then
390 overlay_enabled = false
391 print("Turning off overlay")
395 function in_transition(t)
396 return t >= transition_start and t <= transition_end
400 -- Called every frame.
401 function get_transitions(t)
402 if preview_signal_num == OVERLAY_SIGNAL_NUM then
403 if t < overlay_transition_end then
407 if overlay_enabled then
408 return {"Overlay off", "", "Fade ovl out"}
410 return {"Overlay on", "", "Fade ovl in"}
414 if in_transition(t) then
415 -- Transition already in progress, the only thing we can do is really
416 -- cut to the preview. (TODO: Make an “abort” and/or “finish”, too?)
420 if live_signal_num == preview_signal_num then
421 -- No transitions possible.
425 if (is_plain_signal(live_signal_num) or live_signal_num == STATIC_SIGNAL_NUM) and
426 (is_plain_signal(preview_signal_num) or preview_signal_num == STATIC_SIGNAL_NUM) then
427 return {"Cut", "", "Fade"}
433 function swap_preview_live()
434 local temp = live_signal_num
435 live_signal_num = preview_signal_num
436 preview_signal_num = temp
439 function start_transition(type_, t, duration)
441 transition_end = t + duration
442 transition_type = type_
443 transition_src_signal = live_signal_num
444 transition_dst_signal = preview_signal_num
449 -- Called when the user clicks a transition button.
450 function transition_clicked(num, t)
451 if preview_signal_num == OVERLAY_SIGNAL_NUM then
454 overlay_transition_start = -2.0
455 overlay_transition_end = -1.0
456 if overlay_enabled then
457 overlay_enabled = false
458 overlay_alpha_src = 0.0
459 overlay_alpha_dst = 0.0
461 overlay_enabled = true
462 overlay_alpha_src = 1.0
463 overlay_alpha_dst = 1.0
467 overlay_transition_start = t
468 overlay_transition_end = t + 1.0
469 if overlay_enabled then
470 overlay_alpha_src = 1.0
471 overlay_alpha_dst = 0.0
473 overlay_alpha_src = 0.0
474 overlay_alpha_dst = 1.0
476 overlay_enabled = true
483 if in_transition(t) then
484 -- Ongoing transition; finish it immediately before the cut.
485 finish_transitions(transition_end)
491 finish_transitions(t)
493 if live_signal_num == preview_signal_num then
498 if is_plain_signal(live_signal_num) and is_plain_signal(preview_signal_num) then
499 -- We can't zoom between these. Just make a cut.
500 io.write("Cutting from " .. live_signal_num .. " to " .. live_signal_num .. "\n")
505 finish_transitions(t)
508 if (live_signal_num ~= preview_signal_num) and
509 (is_plain_signal(live_signal_num) or
510 live_signal_num == STATIC_SIGNAL_NUM) and
511 (is_plain_signal(preview_signal_num) or
512 preview_signal_num == STATIC_SIGNAL_NUM) then
513 start_transition(FADE_TRANSITION, t, 1.0)
515 -- Fades involving SBS are ignored (we have no chain for it).
521 function channel_clicked(num)
522 preview_signal_num = num
525 function get_fade_chain(signals, t, width, height, input_resolution)
526 local input0_type = get_input_type(signals, transition_src_signal)
527 local input0_scale = needs_scale(signals, transition_src_signal, width, height)
528 local input1_type = get_input_type(signals, transition_dst_signal)
529 local input1_scale = needs_scale(signals, transition_dst_signal, width, height)
530 local chain = fade_chains[input0_type][input0_scale][input1_type][input1_scale][overlay_enabled][true]
532 if input0_type == "live" or input0_type == "livedeint" then
533 chain.input0.input:connect_signal(transition_src_signal)
534 set_neutral_color_from_signal(chain.input0.wb_effect, transition_src_signal)
536 set_scale_parameters_if_needed(chain.input0, width, height)
537 if input1_type == "live" or input1_type == "livedeint" then
538 chain.input1.input:connect_signal(transition_dst_signal)
539 set_neutral_color_from_signal(chain.input1.wb_effect, transition_dst_signal)
541 set_scale_parameters_if_needed(chain.input1, width, height)
542 local tt = calc_fade_progress(t, transition_start, transition_end)
544 chain.mix_effect:set_float("strength_first", 1.0 - tt)
545 chain.mix_effect:set_float("strength_second", tt)
546 prepare_overlay_live(chain, t)
548 return chain.chain, prepare
552 -- Called every frame. Get the chain for displaying at input <num>,
553 -- where 0 is live, 1 is preview, 2 is the first channel to display
554 -- in the bottom bar, and so on up to num_channels()+1. t is the
555 -- current time in seconds. width and height are the dimensions of
556 -- the output, although you can ignore them if you don't need them
557 -- (they're useful if you want to e.g. know what to resample by).
559 -- <signals> is basically an exposed InputState, which you can use to
560 -- query for information about the signals at the point of the current
561 -- frame. In particular, you can call get_width() and get_height()
562 -- for any signal number, and use that to e.g. assist in chain selection.
564 -- You should return two objects; the chain itself, and then a
565 -- function (taking no parameters) that is run just before rendering.
566 -- The function needs to call connect_signal on any inputs, so that
567 -- it gets updated video data for the given frame. (You are allowed
568 -- to switch which input your input is getting from between frames,
569 -- but not calling connect_signal results in undefined behavior.)
570 -- If you want to change any parameters in the chain, this is also
573 -- NOTE: The chain returned must be finalized with the Y'CbCr flag
574 -- if and only if num==0.
575 function get_chain(num, t, width, height, signals)
576 local input_resolution = {}
577 for signal_num=0,2 do
579 width = signals:get_width(signal_num),
580 height = signals:get_height(signal_num),
581 interlaced = signals:get_interlaced(signal_num),
582 has_signal = signals:get_has_signal(signal_num),
583 is_connected = signals:get_is_connected(signal_num),
584 frame_rate_nom = signals:get_frame_rate_nom(signal_num),
585 frame_rate_den = signals:get_frame_rate_den(signal_num)
588 if res.interlaced then
589 -- Convert height from frame height to field height.
590 -- (Needed for e.g. place_rectangle.)
591 res.height = res.height * 2
593 -- Show field rate instead of frame rate; really for cosmetics only
594 -- (and actually contrary to EBU recommendations, although in line
595 -- with typical user expectations).
596 res.frame_rate_nom = res.frame_rate_nom * 2
599 input_resolution[signal_num] = res
601 last_resolution = input_resolution
603 if num == 0 then -- Live.
604 -- See if we're in a transition.
605 finish_transitions(t)
606 if transition_type == FADE_TRANSITION then
607 return get_fade_chain(signals, t, width, height, input_resolution)
608 elseif is_plain_signal(live_signal_num) then
609 local input_type = get_input_type(signals, live_signal_num)
610 local input_scale = needs_scale(signals, live_signal_num, width, height)
611 local chain = simple_chains[input_type][input_scale][overlay_enabled][true]
613 chain.input:connect_signal(live_signal_num)
614 set_scale_parameters_if_needed(chain, width, height)
615 set_neutral_color_from_signal(chain.wb_effect, live_signal_num)
616 prepare_overlay_live(chain, t)
618 return chain.chain, prepare
619 elseif live_signal_num == STATIC_SIGNAL_NUM then -- Static picture.
620 local chain = static_chains[overlay_enabled][true]
622 prepare_overlay_live(chain, t)
624 return chain.chain, prepare
630 -- We do not show overlays on the individual preview inputs.
631 -- The M/E preview matches what we'd put live by doing a transition, as always.
632 local show_overlay = false
633 if num == 1 then -- Preview.
634 if preview_signal_num == OVERLAY_SIGNAL_NUM then
635 num = live_signal_num + 2
636 show_overlay = not overlay_enabled
638 if transition_type ~= NO_TRANSITION then
639 num = transition_dst_signal + 2
642 num = preview_signal_num + 2
643 show_overlay = overlay_enabled
647 -- Individual preview inputs (usually without overlay).
648 if is_plain_signal(num - 2) then
649 local signal_num = num - 2
650 local input_type = get_input_type(signals, signal_num)
651 local input_scale = needs_scale(signals, signal_num, width, height)
652 local chain = simple_chains[input_type][input_scale][show_overlay][false]
654 chain.input:connect_signal(signal_num)
655 set_scale_parameters_if_needed(chain, width, height)
656 set_neutral_color(chain.wb_effect, neutral_colors[signal_num + 1])
657 prepare_overlay_static(chain, t)
659 return chain.chain, prepare
661 if num == STATIC_SIGNAL_NUM + 2 then
662 local chain = static_chains[show_overlay][false]
664 prepare_overlay_static(chain, t)
666 return chain.chain, prepare
668 if num == OVERLAY_SIGNAL_NUM + 2 then
670 -- prepare_overlay(overlay_chain_lq, t)
672 return overlay_chain_lq, prepare
676 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
678 return math.floor(x + 0.5)
681 function prepare_overlay_live(chain, t)
682 if chain.overlay then
683 local tt = calc_fade_progress(t, overlay_transition_start, overlay_transition_end)
684 overlay_alpha = overlay_alpha_src + tt * (overlay_alpha_dst - overlay_alpha_src)
685 --print("overlay_alpha=" .. overlay_alpha .. " [" .. overlay_alpha_src .. "," .. overlay_alpha_dst .. "]@" .. tt)
686 if t > overlay_transition_end and overlay_alpha_dst == 0.0 then
687 overlay_enabled = false -- Takes effect next frame.
688 -- print("Turning off overlay")
690 chain.overlay.multiply_effect:set_vec4("factor", overlay_alpha, overlay_alpha, overlay_alpha, overlay_alpha)
694 function prepare_overlay_static(chain)
695 if chain.overlay then
696 chain.overlay.multiply_effect:set_vec4("factor", 1.0, 1.0, 1.0, 1.0)
700 function set_neutral_color(effect, color)
701 effect:set_vec3("neutral_color", color[1], color[2], color[3])
704 function set_neutral_color_from_signal(effect, signal)
705 if is_plain_signal(signal) then
706 set_neutral_color(effect, neutral_colors[signal - INPUT0_SIGNAL_NUM + 1])
710 function calc_zoom_progress(t)
711 if t < transition_start then
713 elseif t > transition_end then
716 local tt = (t - transition_start) / (transition_end - transition_start)
718 return math.sin(tt * 3.14159265358 * 0.5)
722 function calc_fade_progress(t, transition_start, transition_end)
723 local tt = (t - transition_start) / (transition_end - transition_start)
730 -- Make the fade look maybe a tad more natural, by pumping it
731 -- through a sigmoid function.
733 tt = 1.0 / (1.0 + math.exp(-tt))