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