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