]> git.sesse.net Git - ultimatescore/blob - nageru/ultimate.lua
c57915b321317f86fd0662df1d841e04f7775348
[ultimatescore] / nageru / ultimate.lua
1 -- Nageru theme for TFK mini-tournament 2017, based on the default theme.
2
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
8
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 }
16
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
22
23 local live_signal_num = 0
24 local preview_signal_num = 1
25 local NUM_CAMERAS = 5  -- Remember to update neutral_colors, too.
26
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 SBS_SIGNAL_NUM = NUM_CAMERAS
34 local STATIC_SIGNAL_NUM = NUM_CAMERAS + 1
35
36 -- Preview-only signal showing the current signal with the overlay.
37 -- Not valid for live_signal_num!
38 local OVERLAY_SIGNAL_NUM = NUM_CAMERAS + 2
39
40 -- Valid values for transition_type. (Cuts are done directly, so they need no entry.)
41 local NO_TRANSITION = 0
42 local ZOOM_TRANSITION = 1
43 local FADE_TRANSITION = 2
44
45 -- Last width/height/frame rate for each channel, if we have it.
46 -- Note that unlike the values we get from Nageru, the resolution is per
47 -- frame and not per field, since we deinterlace.
48 local last_resolution = {}
49
50 local cef_path = Nageru.THEME_PATH:match("(.*)/nageru/")
51 local cef_input = HTMLInput.new("file://" .. cef_path .. "/score.html")
52 cef_input:execute_javascript_async("play()")
53
54 function reload_cef()
55         cef_input:reload()
56         cef_input:execute_javascript_async("play()")
57 end
58
59 -- Utility function to help creating many similar chains that can differ
60 -- in a free set of chosen parameters.
61 function make_cartesian_product(parms, callback)
62         return make_cartesian_product_internal(parms, callback, 1, {})
63 end
64
65 function make_cartesian_product_internal(parms, callback, index, args)
66         if index > #parms then
67                 return callback(unpack(args))
68         end
69         local ret = {}
70         for _, value in ipairs(parms[index]) do
71                 args[index] = value
72                 ret[value] = make_cartesian_product_internal(parms, callback, index + 1, args)
73         end
74         return ret
75 end
76
77 -- An overlay with variable alpha.
78 function make_overlay(chain, base)
79         local image = chain:add_html_input(cef_input)
80         local multiply_effect = chain:add_effect(MultiplyEffect.new())
81         local overlay_effect = chain:add_effect(OverlayEffect.new(), base, multiply_effect)
82         return {
83                 image = image,
84                 multiply_effect = multiply_effect,
85                 overlay_effect = overlay_effect
86         }
87 end
88
89 function possibly_make_overlay(has_overlay, chain, base)
90         if has_overlay == true then
91                 return make_overlay(chain, base)
92         else
93                 return nil
94         end
95 end
96
97 function make_fade_input(chain, signal, live, deint, scale)
98         local input, wb_effect, resample_effect, last
99         if live then
100                 input = chain:add_live_input(false, deint)
101                 input:connect_signal(signal)
102                 last = input
103         else
104                 input = chain:add_effect(ImageInput.new("tfk_pause.png"))
105                 last = input
106         end
107
108         -- If we cared about this for the non-main inputs, we would have
109         -- checked hq here and invoked ResizeEffect instead.
110         if scale then
111                 resample_effect = chain:add_effect(ResampleEffect.new())
112                 last = resample_effect
113         end
114
115         -- Make sure to put the white balance after the scaling (usually more efficient).
116         if live then
117                 wb_effect = chain:add_effect(WhiteBalanceEffect.new())
118                 last = wb_effect
119         end
120
121         return {
122                 input = input,
123                 wb_effect = wb_effect,
124                 resample_effect = resample_effect,
125                 last = last
126         }
127 end
128
129 -- A chain to fade between two inputs, of which either can be a picture
130 -- or a live input. In practice only used live, but we still support the
131 -- hq parameter.
132 function make_fade_chain(input0_live, input0_deint, input0_scale, input1_live, input1_deint, input1_scale, has_overlay, hq)
133         local chain = EffectChain.new(16, 9)
134
135         local input0 = make_fade_input(chain, INPUT0_SIGNAL_NUM, input0_live, input0_deint, input0_scale)
136         local input1 = make_fade_input(chain, INPUT1_SIGNAL_NUM, input1_live, input1_deint, input1_scale)
137
138         -- If fading between two live inputs, the overlay is put on top.
139         -- If fading between the static picture and a live input,
140         -- the overlay is put on the live input.
141         local overlay = nil
142         if input0_live and not input1_live then
143                 overlay = possibly_make_overlay(has_overlay, chain, input0.last)
144                 if overlay then
145                         input0.last = overlay.overlay_effect
146                 end
147         elseif input1_live and not input0_live then
148                 overlay = possibly_make_overlay(has_overlay, chain, input1.last)
149                 if overlay then
150                         input1.last = overlay.overlay_effect
151                 end
152         end
153
154         local mix_effect = chain:add_effect(MixEffect.new(), input0.last, input1.last)
155         if input0_live and input1_live then
156                 overlay = possibly_make_overlay(has_overlay, chain, mix_effect)
157         end
158
159         chain:finalize(hq)
160
161         return {
162                 chain = chain,
163                 input0 = input0,
164                 input1 = input1,
165                 mix_effect = mix_effect,
166                 overlay = overlay
167         }
168 end
169
170 -- Chains to fade between two inputs, in various configurations.
171 local fade_chains = make_cartesian_product({
172         {"static", "live", "livedeint"},  -- input0_type
173         {true, false},                    -- input0_scale
174         {"static", "live", "livedeint"},  -- input1_type
175         {true, false},                    -- input1_scale
176         {true, false},                    -- has_overlay
177         {true}                            -- hq
178 }, function(input0_type, input0_scale, input1_type, input1_scale, has_overlay, hq)
179         local input0_live = (input0_type ~= "static")
180         local input1_live = (input1_type ~= "static")
181         local input0_deint = (input0_type == "livedeint")
182         local input1_deint = (input1_type == "livedeint")
183         return make_fade_chain(input0_live, input0_deint, input0_scale, input1_live, input1_deint, input1_scale, has_overlay, hq)
184 end)
185
186 function make_sbs_input(chain, signal, deint, hq)
187         local input = chain:add_live_input(not deint, deint)  -- Override bounce only if not deinterlacing.
188         input:connect_signal(signal)
189
190         local resample_effect = nil
191         local resize_effect = nil
192         if (hq) then
193                 resample_effect = chain:add_effect(ResampleEffect.new())
194         else
195                 resize_effect = chain:add_effect(ResizeEffect.new())
196         end
197         local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
198
199         local padding_effect = chain:add_effect(IntegralPaddingEffect.new())
200
201         return {
202                 input = input,
203                 wb_effect = wb_effect,
204                 resample_effect = resample_effect,
205                 resize_effect = resize_effect,
206                 padding_effect = padding_effect
207         }
208 end
209
210 -- Side-by-side chains.
211 function make_sbs_chain(input0_type, input0_overlay, input1_type, hq)
212         local chain = EffectChain.new(16, 9)
213
214         local input0 = make_sbs_input(chain, INPUT0_SIGNAL_NUM, input0_type == "livedeint", hq)
215         local input1 = make_sbs_input(chain, INPUT1_SIGNAL_NUM, input1_type == "livedeint", hq)
216
217         -- FIXME: input0_overlay
218
219         input0.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 1.0)
220         input1.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0)
221
222         chain:add_effect(OverlayEffect.new(), input0.padding_effect, input1.padding_effect)
223         chain:finalize(hq)
224
225         return {
226                 chain = chain,
227                 input0 = input0,
228                 input1 = input1
229         }
230 end
231
232
233 -- Make all possible combinations of side-by-side chains.
234 local sbs_chains = make_cartesian_product({
235         {"live", "livedeint"},  -- input0_type
236         {true, false},          -- input0_overlay
237         {"live", "livedeint"},  -- input1_type
238         {true, false}           -- hq
239 }, function(input0_type, input0_overlay, input1_type, hq)
240         return make_sbs_chain(input0_type, input0_overlay, input1_type, hq)
241 end)
242
243 -- A chain to show a single input on screen.
244 function make_simple_chain(input_deint, input_scale, has_overlay, hq)
245         local chain = EffectChain.new(16, 9)
246
247         local input = chain:add_live_input(false, input_deint)
248         input:connect_signal(0)  -- First input card. Can be changed whenever you want.
249
250         local resample_effect, resize_effect
251         if scale then
252                 if hq then
253                         resample_effect = chain:add_effect(ResampleEffect.new())
254                 else
255                         resize_effect = chain:add_effect(ResizeEffect.new())
256                 end
257         end
258
259         local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
260         local overlay = possibly_make_overlay(has_overlay, chain, wb_effect)
261
262         chain:finalize(hq)
263
264         return {
265                 chain = chain,
266                 input = input,
267                 wb_effect = wb_effect,
268                 resample_effect = resample_effect,
269                 resize_effect = resize_effect,
270                 overlay = overlay
271         }
272 end
273
274 -- Make all possible combinations of single-input chains.
275 local simple_chains = make_cartesian_product({
276         {"live", "livedeint"},  -- input_type
277         {true, false},          -- input_scale
278         {true, false},          -- has_overlay
279         {true, false}           -- hq
280 }, function(input_type, input_scale, has_overlay, hq)
281         local input_deint = (input_type == "livedeint")
282         return make_simple_chain(input_deint, input_scale, has_overlay, hq)
283 end)
284
285 -- A chain to show a single static picture on screen. Never with CasparCG overlay.
286 local static_chains = make_cartesian_product({
287         {true, false}            -- hq
288 }, function(hq)
289         local chain = EffectChain.new(16, 9)
290         local chain_input = chain:add_effect(ImageInput.new("tfk_pause.png"))
291
292         chain:finalize(hq)
293         return {
294                 chain = chain
295         }
296 end)
297
298 -- A chain to show the overlay and nothing more. LQ only,
299 -- since it is not a valid live signal.
300 local overlay_chain_lq = EffectChain.new(16, 9)
301 local overlay_chain_lq_input = overlay_chain_lq:add_html_input(cef_input)
302 overlay_chain_lq:finalize(false)
303
304 -- Used for indexing into the tables of chains.
305 function get_input_type(signals, signal_num)
306         if signal_num == STATIC_SIGNAL_NUM then
307                 return "static"
308         elseif signals:get_interlaced(signal_num) then
309                 return "livedeint"
310         else
311                 return "live"
312         end
313 end
314
315 function needs_scale(signals, signal_num, width, height)
316         if signal_num == STATIC_SIGNAL_NUM then
317                 -- We assume this is already correctly scaled at load time.
318                 return false
319         end
320         assert(is_plain_signal(signal_num))
321         return (signals:get_width(signal_num) ~= width or signals:get_height(signal_num) ~= height)
322 end
323
324 function set_scale_parameters_if_needed(chain_or_input, width, height)
325         if chain_or_input.resample_effect then
326                 chain_or_input.resample_effect:set_int("width", width)
327                 chain_or_input.resample_effect:set_int("height", height)
328         elseif chain_or_input.resize_effect then
329                 chain_or_input.resize_effect:set_int("width", width)
330                 chain_or_input.resize_effect:set_int("height", height)
331         end
332 end
333
334 -- API ENTRY POINT
335 -- Returns the number of outputs in addition to the live (0) and preview (1).
336 -- Called only once, at the start of the program.
337 function num_channels()
338         return NUM_CAMERAS + 3  -- sbs, static picture and overlay
339 end
340
341 function is_plain_signal(num)
342         return num >= INPUT0_SIGNAL_NUM and num <= INPUT4_SIGNAL_NUM
343 end
344
345 -- Helper function to write e.g. “720p60”. The difference between this
346 -- and get_channel_resolution_raw() is that this one also can say that
347 -- there's no signal.
348 function get_channel_resolution(signal_num)
349         res = last_resolution[signal_num]
350         if (not res) or not res.is_connected then
351                 return "disconnected"
352         end
353         if res.height <= 0 then
354                 return "no signal"
355         end
356         if not res.has_signal then
357                 if res.height == 525 then
358                         -- Special mode for the USB3 cards.
359                         return "no signal"
360                 end
361                 return get_channel_resolution_raw(res) .. ", no signal"
362         else
363                 return get_channel_resolution_raw(res)
364         end
365 end
366
367 -- Helper function to write e.g. “60” or “59.94”.
368 function get_frame_rate(res)
369         local nom = res.frame_rate_nom
370         local den = res.frame_rate_den
371         if nom % den == 0 then
372                 return nom / den
373         else
374                 return string.format("%.2f", nom / den)
375         end
376 end
377
378 -- Helper function to write e.g. “720p60”.
379 function get_channel_resolution_raw(res)
380         if res.interlaced then
381                 return res.height .. "i" .. get_frame_rate(res)
382         else
383                 return res.height .. "p" .. get_frame_rate(res)
384         end
385 end
386
387 -- API ENTRY POINT
388 -- Returns the name for each additional channel (starting from 2).
389 -- Called at the start of the program, and then each frame for live
390 -- channels in case they change resolution.
391 function channel_name(channel)
392         local signal_num = channel - 2
393         if signal_num == INPUT0_SIGNAL_NUM then
394                 return "Main (" .. get_channel_resolution(signal_num) .. ")"
395         elseif signal_num == INPUT1_SIGNAL_NUM then
396                 return "Secondary (" .. get_channel_resolution(signal_num) .. ")"
397         elseif signal_num == INPUT2_SIGNAL_NUM then
398                 return "Goal L (" .. get_channel_resolution(signal_num) .. ")"
399         elseif signal_num == INPUT3_SIGNAL_NUM then
400                 return "Goal R (" .. get_channel_resolution(signal_num) .. ")"
401         elseif signal_num == INPUT4_SIGNAL_NUM then
402                 return "Commentators (" .. get_channel_resolution(signal_num) .. ")"
403         elseif signal_num == SBS_SIGNAL_NUM then
404                 return "Side-by-side"
405         elseif signal_num == STATIC_SIGNAL_NUM then
406                 return "Static picture"
407         elseif signal_num == OVERLAY_SIGNAL_NUM then
408                 return "Overlay"
409         end
410 end
411
412 -- API ENTRY POINT
413 -- Returns, given a channel number, which signal it corresponds to (starting from 0).
414 -- Should return -1 if the channel does not correspond to a simple signal.
415 -- (The information is used for whether right-click on the channel should bring up
416 -- an input selector or not.)
417 -- Called once for each channel, at the start of the program.
418 -- Will never be called for live (0) or preview (1).
419 function channel_signal(channel)
420         if is_plain_signal(channel - 2) then
421                 return channel - 2
422         else
423                 return -1
424         end
425 end
426
427 -- API ENTRY POINT
428 -- Called every frame. Returns the color (if any) to paint around the given
429 -- channel. Returns a CSS color (typically to mark live and preview signals);
430 -- "transparent" is allowed.
431 -- Will never be called for live (0) or preview (1).
432 function channel_color(channel)
433         if transition_type ~= NO_TRANSITION then
434                 if channel_involved_in(channel, transition_src_signal) or
435                    channel_involved_in(channel, transition_dst_signal) then
436                         return "#f00"
437                 end
438         else
439                 if channel_involved_in(channel, live_signal_num) then
440                         return "#f00"
441                 end
442         end
443         if channel_involved_in(channel, preview_signal_num) then
444                 return "#0f0"
445         end
446         return "transparent"
447 end
448
449 function channel_involved_in(channel, signal_num)
450         if is_plain_signal(signal_num) then
451                 return channel == (signal_num + 2)
452         end
453         if signal_num == STATIC_SIGNAL_NUM then
454                 return (channel == NUM_CAMERAS)
455         end
456         return false
457 end
458
459 -- API ENTRY POINT
460 -- Returns if a given channel supports setting white balance (starting from 2).
461 -- Called only once for each channel, at the start of the program.
462 function supports_set_wb(channel)
463         return is_plain_signal(channel - 2)
464 end
465
466 -- API ENTRY POINT
467 -- Gets called with a new gray point when the white balance is changing.
468 -- The color is in linear light (not sRGB gamma).
469 function set_wb(channel, red, green, blue)
470         if is_plain_signal(channel - 2) then
471                 neutral_colors[channel - 2 + 1] = { red, green, blue }
472         end
473 end
474
475 function finish_transitions(t)
476         if transition_type ~= NO_TRANSITION and t >= transition_end then
477                 live_signal_num = transition_dst_signal
478                 transition_type = NO_TRANSITION
479         end
480
481         -- Disable the overlay if it is no longer visible.
482         if overlay_enabled and t > overlay_transition_end and overlay_alpha_dst == 0.0 then
483                 overlay_enabled = false
484                 print("Turning off overlay")
485         end
486 end
487
488 function in_transition(t)
489         return t >= transition_start and t <= transition_end
490 end
491
492 -- API ENTRY POINT
493 -- Called every frame.
494 function get_transitions(t)
495         if preview_signal_num == OVERLAY_SIGNAL_NUM then
496                 if t < overlay_transition_end then
497                         -- Fade in progress.
498                         return {}
499                 end
500                 if overlay_enabled then
501                         return {"Overlay off", "", "Fade ovl out"}
502                 else
503                         return {"Overlay on", "", "Fade ovl in"}
504                 end
505         end
506
507         if in_transition(t) then
508                 -- Transition already in progress, the only thing we can do is really
509                 -- cut to the preview. (TODO: Make an “abort” and/or “finish”, too?)
510                 return {"Cut"}
511         end
512
513         if live_signal_num == preview_signal_num then
514                 -- No transitions possible.
515                 return {}
516         end
517
518         if (is_plain_signal(live_signal_num) or live_signal_num == STATIC_SIGNAL_NUM) and
519            (is_plain_signal(preview_signal_num) or preview_signal_num == STATIC_SIGNAL_NUM) then
520                 return {"Cut", "", "Fade"}
521         end
522
523         -- Various zooms.
524         if live_signal_num == SBS_SIGNAL_NUM and is_plain_signal(preview_signal_num) then
525                 return {"Cut", "Zoom in"}
526         elseif is_plain_signal(live_signal_num) and preview_signal_num == SBS_SIGNAL_NUM then
527                 return {"Cut", "Zoom out"}
528         end
529
530         return {"Cut"}
531 end
532
533 function swap_preview_live()
534         local temp = live_signal_num
535         live_signal_num = preview_signal_num
536         preview_signal_num = temp
537 end
538
539 function start_transition(type_, t, duration)
540         transition_start = t
541         transition_end = t + duration
542         transition_type = type_
543         transition_src_signal = live_signal_num
544         transition_dst_signal = preview_signal_num
545         swap_preview_live()
546 end
547
548 -- API ENTRY POINT
549 -- Called when the user clicks a transition button.
550 function transition_clicked(num, t)
551         if preview_signal_num == OVERLAY_SIGNAL_NUM then
552                 if num == 0 then
553                         -- Cut.
554                         overlay_transition_start = -2.0
555                         overlay_transition_end = -1.0
556                         if overlay_enabled then
557                                 overlay_enabled = false
558                                 overlay_alpha_src = 0.0
559                                 overlay_alpha_dst = 0.0
560                         else
561                                 overlay_enabled = true
562                                 overlay_alpha_src = 1.0
563                                 overlay_alpha_dst = 1.0
564                         end
565                 elseif num == 2 then
566                         -- Fade.
567                         overlay_transition_start = t
568                         overlay_transition_end = t + 1.0
569                         if overlay_enabled then
570                                 overlay_alpha_src = 1.0
571                                 overlay_alpha_dst = 0.0
572                         else
573                                 overlay_alpha_src = 0.0
574                                 overlay_alpha_dst = 1.0
575                         end
576                         overlay_enabled = true
577                 end
578                 return
579         end
580
581         if num == 0 then
582                 -- Cut.
583                 if in_transition(t) then
584                         -- Ongoing transition; finish it immediately before the cut.
585                         finish_transitions(transition_end)
586                 end
587
588                 swap_preview_live()
589         elseif num == 1 then
590                 -- Zoom.
591                 finish_transitions(t)
592
593                 if live_signal_num == preview_signal_num then
594                         -- Nothing to do.
595                         return
596                 end
597
598                 if is_plain_signal(live_signal_num) and is_plain_signal(preview_signal_num) then
599                         -- We can't zoom between these. Just make a cut.
600                         io.write("Cutting from " .. live_signal_num .. " to " .. live_signal_num .. "\n")
601                         swap_preview_live()
602                         return
603                 end
604
605                 if (live_signal_num == SBS_SIGNAL_NUM and is_plain_signal(preview_signal_num)) or
606                    (preview_signal_num == SBS_SIGNAL_NUM and is_plain_signal(live_signal_num)) then
607                         start_transition(ZOOM_TRANSITION, t, 1.0)
608                 end
609         elseif num == 2 then
610                 finish_transitions(t)
611
612                 -- Fade.
613                 if (live_signal_num ~= preview_signal_num) and
614                    (is_plain_signal(live_signal_num) or
615                     live_signal_num == STATIC_SIGNAL_NUM) and
616                    (is_plain_signal(preview_signal_num) or
617                     preview_signal_num == STATIC_SIGNAL_NUM) then
618                         start_transition(FADE_TRANSITION, t, 1.0)
619                 else
620                         -- Fades involving SBS are ignored (we have no chain for it).
621                 end
622         end
623 end
624
625 -- API ENTRY POINT
626 function channel_clicked(num)
627         preview_signal_num = num
628 end
629
630 function get_fade_chain(signals, t, width, height, input_resolution)
631         local input0_type = get_input_type(signals, transition_src_signal)
632         local input0_scale = needs_scale(signals, transition_src_signal, width, height)
633         local input1_type = get_input_type(signals, transition_dst_signal)
634         local input1_scale = needs_scale(signals, transition_dst_signal, width, height)
635         local chain = fade_chains[input0_type][input0_scale][input1_type][input1_scale][overlay_enabled][true]
636         prepare = function()
637                 if input0_type == "live" or input0_type == "livedeint" then
638                         chain.input0.input:connect_signal(transition_src_signal)
639                         set_neutral_color_from_signal(chain.input0.wb_effect, transition_src_signal)
640                 end
641                 set_scale_parameters_if_needed(chain.input0, width, height)
642                 if input1_type == "live" or input1_type == "livedeint" then
643                         chain.input1.input:connect_signal(transition_dst_signal)
644                         set_neutral_color_from_signal(chain.input1.wb_effect, transition_dst_signal)
645                 end
646                 set_scale_parameters_if_needed(chain.input1, width, height)
647                 local tt = calc_fade_progress(t, transition_start, transition_end)
648
649                 chain.mix_effect:set_float("strength_first", 1.0 - tt)
650                 chain.mix_effect:set_float("strength_second", tt)
651                 prepare_overlay_live(chain, t)
652         end
653         return chain.chain, prepare
654 end
655
656 -- SBS code (live_signal_num == SBS_SIGNAL_NUM, or in a transition to/from it).
657 function get_sbs_chain(signals, t, width, height, input_resolution)
658         local input0_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
659         local input1_type = get_input_type(signals, INPUT1_SIGNAL_NUM)
660         return sbs_chains[input0_type][false][input1_type][true]  -- FIXME: overlay is always false?
661 end
662
663 -- API ENTRY POINT
664 -- Called every frame. Get the chain for displaying at input <num>,
665 -- where 0 is live, 1 is preview, 2 is the first channel to display
666 -- in the bottom bar, and so on up to num_channels()+1. t is the
667 -- current time in seconds. width and height are the dimensions of
668 -- the output, although you can ignore them if you don't need them
669 -- (they're useful if you want to e.g. know what to resample by).
670 --
671 -- <signals> is basically an exposed InputState, which you can use to
672 -- query for information about the signals at the point of the current
673 -- frame. In particular, you can call get_width() and get_height()
674 -- for any signal number, and use that to e.g. assist in chain selection.
675 --
676 -- You should return two objects; the chain itself, and then a
677 -- function (taking no parameters) that is run just before rendering.
678 -- The function needs to call connect_signal on any inputs, so that
679 -- it gets updated video data for the given frame. (You are allowed
680 -- to switch which input your input is getting from between frames,
681 -- but not calling connect_signal results in undefined behavior.)
682 -- If you want to change any parameters in the chain, this is also
683 -- the right place.
684 --
685 -- NOTE: The chain returned must be finalized with the Y'CbCr flag
686 -- if and only if num==0.
687 function get_chain(num, t, width, height, signals)
688         local input_resolution = {}
689         for signal_num=0,(NUM_CAMERAS-1) do
690                 local res = {
691                         width = signals:get_width(signal_num),
692                         height = signals:get_height(signal_num),
693                         interlaced = signals:get_interlaced(signal_num),
694                         has_signal = signals:get_has_signal(signal_num),
695                         is_connected = signals:get_is_connected(signal_num),
696                         frame_rate_nom = signals:get_frame_rate_nom(signal_num),
697                         frame_rate_den = signals:get_frame_rate_den(signal_num)
698                 }
699
700                 if res.interlaced then
701                         -- Convert height from frame height to field height.
702                         -- (Needed for e.g. place_rectangle.)
703                         res.height = res.height * 2
704
705                         -- Show field rate instead of frame rate; really for cosmetics only
706                         -- (and actually contrary to EBU recommendations, although in line
707                         -- with typical user expectations).
708                         res.frame_rate_nom = res.frame_rate_nom * 2
709                 end
710
711                 input_resolution[signal_num] = res
712         end
713         last_resolution = input_resolution
714
715         if num == 0 then  -- Live.
716                 -- See if we're in a transition.
717                 finish_transitions(t)
718                 if transition_type == ZOOM_TRANSITION then
719                         -- Transition in or out of SBS.
720                         local chain = get_sbs_chain(signals, t, width, height, input_resolution)
721                         prepare = function()
722                                 prepare_sbs_chain(chain, calc_zoom_progress(t), transition_type, transition_src_signal, transition_dst_signal, width, height, input_resolution)
723                         end
724                         return chain.chain, prepare
725                 elseif transition_type == NO_TRANSITION and live_signal_num == SBS_SIGNAL_NUM then
726                         -- Static SBS view.
727                         local chain = get_sbs_chain(signals, t, width, height, input_resolution)
728                         prepare = function()
729                                 prepare_sbs_chain(chain, 0.0, NO_TRANSITION, 0, SBS_SIGNAL_NUM, width, height, input_resolution)
730                         end
731                         return chain.chain, prepare
732                 elseif transition_type == FADE_TRANSITION then
733                         return get_fade_chain(signals, t, width, height, input_resolution)
734                 elseif is_plain_signal(live_signal_num) then
735                         local input_type = get_input_type(signals, live_signal_num)
736                         local input_scale = needs_scale(signals, live_signal_num, width, height)
737                         local chain = simple_chains[input_type][input_scale][overlay_enabled][true]
738                         prepare = function()
739                                 chain.input:connect_signal(live_signal_num)
740                                 set_scale_parameters_if_needed(chain, width, height)
741                                 set_neutral_color_from_signal(chain.wb_effect, live_signal_num)
742                                 prepare_overlay_live(chain, t)
743                         end
744                         return chain.chain, prepare
745                 elseif live_signal_num == STATIC_SIGNAL_NUM then  -- Static picture.
746                         local chain = static_chains[true]
747                         prepare = function()
748                                 prepare_overlay_live(chain, t)
749                         end
750                         return chain.chain, prepare
751                 else
752                         assert(false)
753                 end
754         end
755
756         -- We do not show overlays on the individual preview inputs.
757         -- The M/E preview matches what we'd put live by doing a transition, as always.
758         local show_overlay = false
759         if num == 1 then  -- Preview.
760                 if preview_signal_num == OVERLAY_SIGNAL_NUM then
761                         num = live_signal_num + 2
762                         show_overlay = not overlay_enabled
763
764                         if transition_type ~= NO_TRANSITION then
765                                 num = transition_dst_signal + 2
766                         end
767                 else
768                         num = preview_signal_num + 2
769                         show_overlay = overlay_enabled
770                 end
771         end
772
773         -- Individual preview inputs (usually without overlay).
774         if is_plain_signal(num - 2) then
775                 local signal_num = num - 2
776                 local input_type = get_input_type(signals, signal_num)
777                 local input_scale = needs_scale(signals, signal_num, width, height)
778                 local chain = simple_chains[input_type][input_scale][show_overlay][false]
779                 prepare = function()
780                         chain.input:connect_signal(signal_num)
781                         set_scale_parameters_if_needed(chain, width, height)
782                         set_neutral_color(chain.wb_effect, neutral_colors[signal_num + 1])
783                         prepare_overlay_static(chain, t)
784                 end
785                 return chain.chain, prepare
786         end
787         if num == SBS_SIGNAL_NUM + 2 then
788                 local input0_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
789                 local input1_type = get_input_type(signals, INPUT1_SIGNAL_NUM)
790                 local chain = sbs_chains[input0_type][show_overlay][input1_type][false]
791                 prepare = function()
792                         prepare_sbs_chain(chain, 0.0, NO_TRANSITION, 0, SBS_SIGNAL_NUM, width, height, input_resolution)
793                 end
794                 return chain.chain, prepare
795         end
796         if num == STATIC_SIGNAL_NUM + 2 then
797                 local chain = static_chains[false]
798                 prepare = function()
799                         prepare_overlay_static(chain, t)
800                 end
801                 return chain.chain, prepare
802         end
803         if num == OVERLAY_SIGNAL_NUM + 2 then
804                 prepare = function()
805 --                      prepare_overlay(overlay_chain_lq, t)
806                 end
807                 return overlay_chain_lq, prepare
808         end
809 end
810
811 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
812 function round(x)
813         return math.floor(x + 0.5)
814 end
815
816 function lerp(a, b, t)
817         return a + (b - a) * t
818 end
819
820 function lerp_pos(a, b, t)
821         return {
822                 x0 = lerp(a.x0, b.x0, t),
823                 y0 = lerp(a.y0, b.y0, t),
824                 x1 = lerp(a.x1, b.x1, t),
825                 y1 = lerp(a.y1, b.y1, t)
826         }
827 end
828
829 function pos_from_top_left(x, y, width, height, screen_width, screen_height)
830         local xs = screen_width / 1280.0
831         local ys = screen_height / 720.0
832         return {
833                 x0 = round(xs * x),
834                 y0 = round(ys * y),
835                 x1 = round(xs * (x + width)),
836                 y1 = round(ys * (y + height))
837         }
838 end
839
840 function prepare_sbs_chain(chain, t, transition_type, src_signal, dst_signal, screen_width, screen_height, input_resolution)
841         chain.input0.input:connect_signal(0)
842         chain.input1.input:connect_signal(1)
843         set_neutral_color(chain.input0.wb_effect, neutral_colors[1])
844         set_neutral_color(chain.input1.wb_effect, neutral_colors[2])
845
846         -- First input is positioned (16,48) from top-left.
847         -- Second input is positioned (16,48) from the bottom-right.
848         local pos0 = pos_from_top_left(16, 48, 848, 477, screen_width, screen_height)
849         local pos1 = pos_from_top_left(1280 - 384 - 16, 720 - 216 - 48, 384, 216, screen_width, screen_height)
850
851         local pos_fs = { x0 = 0, y0 = 0, x1 = screen_width, y1 = screen_height }
852         local affine_param
853         if transition_type == NO_TRANSITION then
854                 -- Static SBS view.
855                 affine_param = { sx = 1.0, sy = 1.0, tx = 0.0, ty = 0.0 }   -- Identity.
856         else
857                 -- Zooming to/from SBS view into or out of a single view.
858                 assert(transition_type == ZOOM_TRANSITION)
859                 local signal, real_t
860                 if src_signal == SBS_SIGNAL_NUM then
861                         signal = dst_signal
862                         real_t = t
863                 else
864                         assert(dst_signal == SBS_SIGNAL_NUM)
865                         signal = src_signal
866                         real_t = 1.0 - t
867                 end
868
869                 if signal == INPUT0_SIGNAL_NUM then
870                         affine_param = find_affine_param(pos0, lerp_pos(pos0, pos_fs, real_t))
871                 elseif signal == INPUT1_SIGNAL_NUM then
872                         affine_param = find_affine_param(pos1, lerp_pos(pos1, pos_fs, real_t))
873                 end
874         end
875
876         -- NOTE: input_resolution is not 1-indexed, unlike usual Lua arrays.
877         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)
878         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)
879 end
880
881 -- Find the transformation that changes the first rectangle to the second one.
882 function find_affine_param(a, b)
883         local sx = (b.x1 - b.x0) / (a.x1 - a.x0)
884         local sy = (b.y1 - b.y0) / (a.y1 - a.y0)
885         return {
886                 sx = sx,
887                 sy = sy,
888                 tx = b.x0 - a.x0 * sx,
889                 ty = b.y0 - a.y0 * sy
890         }
891 end
892
893 function place_rectangle_with_affine(resample_effect, resize_effect, padding_effect, pos, aff, screen_width, screen_height, input_width, input_height)
894         local x0 = pos.x0 * aff.sx + aff.tx
895         local x1 = pos.x1 * aff.sx + aff.tx
896         local y0 = pos.y0 * aff.sy + aff.ty
897         local y1 = pos.y1 * aff.sy + aff.ty
898
899         place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height)
900 end
901
902 function place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height)
903         local srcx0 = 0.0
904         local srcx1 = 1.0
905         local srcy0 = 0.0
906         local srcy1 = 1.0
907
908         padding_effect:set_int("width", screen_width)
909         padding_effect:set_int("height", screen_height)
910
911         -- Cull.
912         if x0 > screen_width or x1 < 0.0 or y0 > screen_height or y1 < 0.0 then
913                 if resample_effect ~= nil then
914                         resample_effect:set_int("width", 1)
915                         resample_effect:set_int("height", 1)
916                         resample_effect:set_float("zoom_x", screen_width)
917                         resample_effect:set_float("zoom_y", screen_height)
918                 else
919                         resize_effect:set_int("width", 1)
920                         resize_effect:set_int("height", 1)
921                 end
922                 padding_effect:set_int("left", screen_width + 100)
923                 padding_effect:set_int("top", screen_height + 100)
924                 return
925         end
926
927         -- Clip.
928         if x0 < 0 then
929                 srcx0 = -x0 / (x1 - x0)
930                 x0 = 0
931         end
932         if y0 < 0 then
933                 srcy0 = -y0 / (y1 - y0)
934                 y0 = 0
935         end
936         if x1 > screen_width then
937                 srcx1 = (screen_width - x0) / (x1 - x0)
938                 x1 = screen_width
939         end
940         if y1 > screen_height then
941                 srcy1 = (screen_height - y0) / (y1 - y0)
942                 y1 = screen_height
943         end
944
945         if resample_effect ~= nil then
946                 -- High-quality resampling.
947                 local x_subpixel_offset = x0 - math.floor(x0)
948                 local y_subpixel_offset = y0 - math.floor(y0)
949
950                 -- Resampling must be to an integral number of pixels. Round up,
951                 -- and then add an extra pixel so we have some leeway for the border.
952                 local width = math.ceil(x1 - x0) + 1
953                 local height = math.ceil(y1 - y0) + 1
954                 resample_effect:set_int("width", width)
955                 resample_effect:set_int("height", height)
956
957                 -- Correct the discrepancy with zoom. (This will leave a small
958                 -- excess edge of pixels and subpixels, which we'll correct for soon.)
959                 local zoom_x = (x1 - x0) / (width * (srcx1 - srcx0))
960                 local zoom_y = (y1 - y0) / (height * (srcy1 - srcy0))
961                 resample_effect:set_float("zoom_x", zoom_x)
962                 resample_effect:set_float("zoom_y", zoom_y)
963                 resample_effect:set_float("zoom_center_x", 0.0)
964                 resample_effect:set_float("zoom_center_y", 0.0)
965
966                 -- Padding must also be to a whole-pixel offset.
967                 padding_effect:set_int("left", math.floor(x0))
968                 padding_effect:set_int("top", math.floor(y0))
969
970                 -- Correct _that_ discrepancy by subpixel offset in the resampling.
971                 resample_effect:set_float("left", srcx0 * input_width - x_subpixel_offset / zoom_x)
972                 resample_effect:set_float("top", srcy0 * input_height - y_subpixel_offset / zoom_y)
973
974                 -- Finally, adjust the border so it is exactly where we want it.
975                 padding_effect:set_float("border_offset_left", x_subpixel_offset)
976                 padding_effect:set_float("border_offset_right", x1 - (math.floor(x0) + width))
977                 padding_effect:set_float("border_offset_top", y_subpixel_offset)
978                 padding_effect:set_float("border_offset_bottom", y1 - (math.floor(y0) + height))
979         else
980                 -- Lower-quality simple resizing.
981                 local width = round(x1 - x0)
982                 local height = round(y1 - y0)
983                 resize_effect:set_int("width", width)
984                 resize_effect:set_int("height", height)
985
986                 -- Padding must also be to a whole-pixel offset.
987                 padding_effect:set_int("left", math.floor(x0))
988                 padding_effect:set_int("top", math.floor(y0))
989         end
990 end
991
992 function prepare_overlay_live(chain, t)
993         if chain.overlay then
994                 local tt = calc_fade_progress(t, overlay_transition_start, overlay_transition_end)
995                 overlay_alpha = overlay_alpha_src + tt * (overlay_alpha_dst - overlay_alpha_src)
996                 --print("overlay_alpha=" .. overlay_alpha .. " [" .. overlay_alpha_src .. "," .. overlay_alpha_dst .. "]@" .. tt)
997                 if t > overlay_transition_end and overlay_alpha_dst == 0.0 then
998                         overlay_enabled = false  -- Takes effect next frame.
999         --              print("Turning off overlay")
1000                 end
1001                 chain.overlay.multiply_effect:set_vec4("factor", overlay_alpha, overlay_alpha, overlay_alpha, overlay_alpha)
1002         end
1003 end
1004
1005 function prepare_overlay_static(chain)
1006         if chain.overlay then
1007                 chain.overlay.multiply_effect:set_vec4("factor", 1.0, 1.0, 1.0, 1.0)
1008         end
1009 end
1010
1011 function set_neutral_color(effect, color)
1012         effect:set_vec3("neutral_color", color[1], color[2], color[3])
1013 end
1014
1015 function set_neutral_color_from_signal(effect, signal)
1016         if is_plain_signal(signal) then
1017                 set_neutral_color(effect, neutral_colors[signal - INPUT0_SIGNAL_NUM + 1])
1018         end
1019 end
1020
1021 function calc_zoom_progress(t)
1022         if t < transition_start then
1023                 return 0.0
1024         elseif t > transition_end then
1025                 return 1.0
1026         else
1027                 local tt = (t - transition_start) / (transition_end - transition_start)
1028                 -- Smooth it a bit.
1029                 return math.sin(tt * 3.14159265358 * 0.5)
1030         end
1031 end
1032
1033 function calc_fade_progress(t, transition_start, transition_end)
1034         local tt = (t - transition_start) / (transition_end - transition_start)
1035         if tt < 0.0 then
1036                 return 0.0
1037         elseif tt > 1.0 then
1038                 return 1.0
1039         end
1040
1041         -- Make the fade look maybe a tad more natural, by pumping it
1042         -- through a sigmoid function.
1043         tt = 10.0 * tt - 5.0
1044         tt = 1.0 / (1.0 + math.exp(-tt))
1045
1046         return tt
1047 end
1048
1049 ThemeMenu.set(
1050         { "Reload overlay", reload_cef }
1051 )