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