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