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