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