]> git.sesse.net Git - nageru/blob - theme.lua
Use the new has_signal flag.
[nageru] / theme.lua
1 -- The theme is what decides what's actually shown on screen, what kind of
2 -- transitions are available (if any), and what kind of inputs there are,
3 -- if any. In general, it drives the entire display logic by creating Movit
4 -- chains, setting their parameters and then deciding which to show when.
5 --
6 -- Themes are written in Lua, which reflects a simplified form of the Movit API
7 -- where all the low-level details (such as texture formats) are handled by the
8 -- C++ side and you generally just build chains.
9
10 local transition_start = -2.0
11 local transition_end = -1.0
12 local zoom_src = 0.0
13 local zoom_dst = 1.0
14 local zoom_poi = 0   -- which input to zoom in on
15 local fade_src_signal = 0
16 local fade_dst_signal = 0
17
18 local input0_neutral_color = {0.5, 0.5, 0.5}
19 local input1_neutral_color = {0.5, 0.5, 0.5}
20
21 local live_signal_num = 0
22 local preview_signal_num = 1
23
24 -- Valid values for live_signal_num and preview_signal_num.
25 local INPUT0_SIGNAL_NUM = 0
26 local INPUT1_SIGNAL_NUM = 1
27 local SBS_SIGNAL_NUM = 2
28 local STATIC_SIGNAL_NUM = 3
29
30 -- “fake” signal number that signifies that we are fading from one input
31 -- to the next.
32 local FADE_SIGNAL_NUM = 4
33
34 -- Last width/height/frame rate for each channel, if we have it.
35 -- Note that unlike the values we get from Nageru, the resolution is per
36 -- frame and not per field, since we deinterlace.
37 local last_resolution = {}
38
39 -- Utility function to help creating many similar chains that can differ
40 -- in a free set of chosen parameters.
41 function make_cartesian_product(parms, callback)
42         return make_cartesian_product_internal(parms, callback, 1, {})
43 end
44
45 function make_cartesian_product_internal(parms, callback, index, args)
46         if index > #parms then
47                 return callback(unpack(args))
48         end
49         local ret = {}
50         for _, value in ipairs(parms[index]) do
51                 args[index] = value
52                 ret[value] = make_cartesian_product_internal(parms, callback, index + 1, args)
53         end
54         return ret
55 end
56
57 function make_sbs_input(chain, signal, deint, hq)
58         local input = chain:add_live_input(not deint, deint)  -- Override bounce only if not deinterlacing.
59         input:connect_signal(signal)
60         local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
61
62         local resample_effect = nil
63         local resize_effect = nil
64         if (hq) then
65                 resample_effect = chain:add_effect(ResampleEffect.new())
66         else
67                 resize_effect = chain:add_effect(ResizeEffect.new())
68         end
69
70         local padding_effect = chain:add_effect(IntegralPaddingEffect.new())
71
72         return {
73                 input = input,
74                 wb_effect = wb_effect,
75                 resample_effect = resample_effect,
76                 resize_effect = resize_effect,
77                 padding_effect = padding_effect
78         }
79 end
80
81 -- The main live chain.
82 function make_sbs_chain(input0_deint, input1_deint, hq)
83         local chain = EffectChain.new(16, 9)
84
85         local input0 = make_sbs_input(chain, INPUT0_SIGNAL_NUM, input0_deint, hq)
86         local input1 = make_sbs_input(chain, INPUT1_SIGNAL_NUM, input1_deint, hq)
87
88         input0.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 1.0)
89         input1.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0)
90
91         chain:add_effect(OverlayEffect.new(), input0.padding_effect, input1.padding_effect)
92         chain:finalize(hq)
93
94         return {
95                 chain = chain,
96                 input0 = input0,
97                 input1 = input1
98         }
99 end
100
101 -- Make all possible combinations of side-by-side chains.
102 local sbs_chains = make_cartesian_product({
103         {"live", "livedeint"},  -- input0_type
104         {"live", "livedeint"},  -- input1_type
105         {true, false}           -- hq
106 }, function(input0_type, input1_type, hq)
107         local input0_deint = (input0_type == "livedeint")
108         local input1_deint = (input1_type == "livedeint")
109         return make_sbs_chain(input0_deint, input1_deint, hq)
110 end)
111
112 function make_fade_input(chain, signal, live, deint, scale)
113         local input, wb_effect, resample_effect, last
114         if live then
115                 input = chain:add_live_input(false, deint)
116                 wb_effect = chain:add_effect(WhiteBalanceEffect.new())
117                 input:connect_signal(signal)
118                 last = wb_effect
119         else
120                 input = chain:add_effect(ImageInput.new("bg.jpeg"))
121                 last = input
122         end
123
124         -- If we cared about this for the non-main inputs, we would have
125         -- checked hq here and invoked ResizeEffect instead.
126         if scale then
127                 resample_effect = chain:add_effect(ResampleEffect.new())
128                 last = resample_effect
129         end
130
131         return {
132                 input = input,
133                 wb_effect = wb_effect,
134                 resample_effect = resample_effect,
135                 last = last
136         }
137 end
138
139 -- A chain to fade between two inputs, of which either can be a picture
140 -- or a live input. In practice only used live, but we still support the
141 -- hq parameter.
142 function make_fade_chain(input0_live, input0_deint, input0_scale, input1_live, input1_deint, input1_scale, hq)
143         local chain = EffectChain.new(16, 9)
144
145         local input0 = make_fade_input(chain, INPUT0_SIGNAL_NUM, input0_live, input0_deint, input0_scale)
146         local input1 = make_fade_input(chain, INPUT1_SIGNAL_NUM, input1_live, input1_deint, input1_scale)
147
148         local mix_effect = chain:add_effect(MixEffect.new(), input0.last, input1.last)
149         chain:finalize(hq)
150
151         return {
152                 chain = chain,
153                 input0 = input0,
154                 input1 = input1,
155                 mix_effect = mix_effect
156         }
157 end
158
159 -- Chains to fade between two inputs, in various configurations.
160 local fade_chains = make_cartesian_product({
161         {"static", "live", "livedeint"},  -- input0_type
162         {true, false},                    -- input0_scale
163         {"static", "live", "livedeint"},  -- input1_type
164         {true, false},                    -- input1_scale
165         {true}                            -- hq
166 }, function(input0_type, input0_scale, input1_type, input1_scale, hq)
167         local input0_live = (input0_type ~= "static")
168         local input1_live = (input1_type ~= "static")
169         local input0_deint = (input0_type == "livedeint")
170         local input1_deint = (input1_type == "livedeint")
171         return make_fade_chain(input0_live, input0_deint, input0_scale, input1_live, input1_deint, input1_scale, hq)
172 end)
173
174 -- A chain to show a single input on screen.
175 function make_simple_chain(input_deint, input_scale, hq)
176         local chain = EffectChain.new(16, 9)
177
178         local input = chain:add_live_input(false, input_deint)
179         input:connect_signal(0)  -- First input card. Can be changed whenever you want.
180         local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
181         chain:finalize(hq)
182
183         local resample_effect, resize_effect
184         if scale then
185                 if hq then
186                         resample_effect = chain:add_effect(ResampleEffect.new())
187                 else
188                         resize_effect = chain:add_effect(ResizeEffect.new())
189                 end
190         end
191
192         return {
193                 chain = chain,
194                 input = input,
195                 wb_effect = wb_effect,
196                 resample_effect = resample_effect,
197                 resize_effect = resize_effect
198         }
199 end
200
201 -- Make all possible combinations of single-input chains.
202 local simple_chains = make_cartesian_product({
203         {"live", "livedeint"},  -- input_type
204         {true, false},          -- input_scale
205         {true, false}           -- hq
206 }, function(input_type, input_scale, hq)
207         local input_deint = (input_type == "livedeint")
208         return make_simple_chain(input_deint, input_scale, hq)
209 end)
210
211 -- A chain to show a single static picture on screen (HQ version).
212 local static_chain_hq = EffectChain.new(16, 9)
213 local static_chain_hq_input = static_chain_hq:add_effect(ImageInput.new("bg.jpeg"))
214 static_chain_hq:finalize(true)
215
216 -- A chain to show a single static picture on screen (LQ version).
217 local static_chain_lq = EffectChain.new(16, 9)
218 local static_chain_lq_input = static_chain_lq:add_effect(ImageInput.new("bg.jpeg"))
219 static_chain_lq:finalize(false)
220
221 -- Used for indexing into the tables of chains.
222 function get_input_type(signals, signal_num)
223         if signal_num == STATIC_SIGNAL_NUM then
224                 return "static"
225         elseif signals:get_interlaced(signal_num) then
226                 return "livedeint"
227         else
228                 return "live"
229         end
230 end
231
232 function needs_scale(signals, signal_num, width, height)
233         if signal_num == STATIC_SIGNAL_NUM then
234                 -- We assume this is already correctly scaled at load time.
235                 return false
236         end
237         assert(signal_num == INPUT0_SIGNAL_NUM or signal_num == INPUT1_SIGNAL_NUM)
238         return (signals:get_width(signal_num) ~= width or signals:get_height(signal_num) ~= height)
239 end
240
241 function set_scale_parameters_if_needed(chain_or_input, width, height)
242         if chain_or_input.resample_effect then
243                 chain_or_input.resample_effect:set_int("width", width)
244                 chain_or_input.resample_effect:set_int("height", height)
245         elseif chain_or_input.resize_effect then
246                 chain_or_input.resize_effect:set_int("width", width)
247                 chain_or_input.resize_effect:set_int("height", height)
248         end
249 end
250
251 -- API ENTRY POINT
252 -- Returns the number of outputs in addition to the live (0) and preview (1).
253 -- Called only once, at the start of the program.
254 function num_channels()
255         return 4
256 end
257
258 -- Helper function to write e.g. “60” or “59.94”.
259 function get_frame_rate(signal_num)
260         local nom = last_resolution[signal_num].frame_rate_nom
261         local den = last_resolution[signal_num].frame_rate_den
262         if nom % den == 0 then
263                 return nom / den
264         else
265                 return string.format("%.2f", nom / den)
266         end
267 end
268
269 -- Helper function to write e.g. “720p60”.
270 function get_channel_resolution(signal_num)
271         if last_resolution[signal_num] then
272                 if not last_resolution[signal_num].has_signal then
273                         return "no signal"
274                 elseif last_resolution[signal_num].interlaced then
275                         return last_resolution[signal_num].height .. "i" .. get_frame_rate(signal_num)
276                 else
277                         return last_resolution[signal_num].height .. "p" .. get_frame_rate(signal_num)
278                 end
279         else
280                 return "no signal"
281         end
282 end
283
284 -- API ENTRY POINT
285 -- Returns the name for each additional channel (starting from 2).
286 -- Called at the start of the program, and then each frame for live
287 -- channels in case they change resolution.
288 function channel_name(channel)
289         if channel == 2 then
290                 return "Input 1 (" .. get_channel_resolution(0) .. ")"
291         elseif channel == 3 then
292                 return "Input 2 (" .. get_channel_resolution(1) .. ")"
293         elseif channel == 4 then
294                 return "Side-by-side"
295         elseif channel == 5 then
296                 return "Static picture"
297         end
298 end
299
300 -- API ENTRY POINT
301 -- Returns, given a channel number, which signal it corresponds to (starting from 0).
302 -- Should return -1 if the channel does not correspond to a simple signal.
303 -- Called once for each channel, at the start of the program.
304 -- Will never be called for live (0) or preview (1).
305 function channel_signal(channel)
306         if channel == 2 then
307                 return 0
308         elseif channel == 3 then
309                 return 1
310         else
311                 return -1
312         end
313 end
314
315 -- API ENTRY POINT
316 -- Returns if a given channel supports setting white balance (starting from 2).
317 -- Called only once for each channel, at the start of the program.
318 function supports_set_wb(channel)
319         return channel == 2 or channel == 3
320 end
321
322 -- API ENTRY POINT
323 -- Gets called with a new gray point when the white balance is changing.
324 -- The color is in linear light (not sRGB gamma).
325 function set_wb(channel, red, green, blue)
326         if channel == 2 then
327                 input0_neutral_color = { red, green, blue }
328         elseif channel == 3 then
329                 input1_neutral_color = { red, green, blue }
330         end
331 end
332
333 function finish_transitions(t)
334         -- If live is SBS but de-facto single, make it so.
335         if live_signal_num == SBS_SIGNAL_NUM and t >= transition_end and zoom_dst == 1.0 then
336                 live_signal_num = zoom_poi
337         end
338
339         -- If live is fade but de-facto single, make it so.
340         if live_signal_num == FADE_SIGNAL_NUM and t >= transition_end then
341                 live_signal_num = fade_dst_signal
342         end
343 end
344
345 -- API ENTRY POINT
346 -- Called every frame.
347 function get_transitions(t)
348         finish_transitions(t)
349
350         if live_signal_num == preview_signal_num then
351                 return {}
352         end
353
354         if live_signal_num == SBS_SIGNAL_NUM and t >= transition_start and t <= transition_end then
355                 -- Zoom in progress.
356                 return {"Cut"}
357         end
358
359         if (live_signal_num == INPUT0_SIGNAL_NUM or
360             live_signal_num == INPUT1_SIGNAL_NUM or
361             live_signal_num == STATIC_SIGNAL_NUM) and
362            (preview_signal_num == INPUT0_SIGNAL_NUM or
363             preview_signal_num == INPUT1_SIGNAL_NUM or
364             preview_signal_num == STATIC_SIGNAL_NUM) then
365                 return {"Cut", "", "Fade"}
366         end
367
368         -- Various zooms.
369         if live_signal_num == SBS_SIGNAL_NUM and
370            (preview_signal_num == INPUT0_SIGNAL_NUM or preview_signal_num == INPUT1_SIGNAL_NUM) then
371                 return {"Cut", "Zoom in"}
372         elseif (live_signal_num == INPUT0_SIGNAL_NUM or live_signal_num == INPUT1_SIGNAL_NUM) and
373                preview_signal_num == SBS_SIGNAL_NUM then
374                 return {"Cut", "Zoom out"}
375         end
376
377         return {"Cut"}
378 end
379
380 function transition_clicked(num, t)
381         if num == 0 then
382                 -- Cut.
383                 if live_signal_num == FADE_SIGNAL_NUM then
384                         -- Ongoing fade; finish it immediately.
385                         finish_transitions(transition_end)
386                 end
387
388                 local temp = live_signal_num
389                 live_signal_num = preview_signal_num
390                 preview_signal_num = temp
391
392                 if live_signal_num == SBS_SIGNAL_NUM then
393                         -- Just cut to SBS, we need to reset any zooms.
394                         zoom_src = 1.0
395                         zoom_dst = 0.0
396                         transition_start = -2.0
397                         transition_end = -1.0
398                 end
399         elseif num == 1 then
400                 -- Zoom.
401
402                 finish_transitions(t)
403
404                 if live_signal_num == preview_signal_num then
405                         -- Nothing to do.
406                         return
407                 end
408
409                 if (live_signal_num == INPUT0_SIGNAL_NUM and preview_signal_num == INPUT1_SIGNAL_NUM) or
410                    (live_signal_num == INPUT1_SIGNAL_NUM and preview_signal_num == INPUT0_SIGNAL_NUM) then
411                         -- We can't zoom between these. Just make a cut.
412                         io.write("Cutting from " .. live_signal_num .. " to " .. live_signal_num .. "\n")
413                         local temp = live_signal_num
414                         live_signal_num = preview_signal_num
415                         preview_signal_num = temp
416                         return
417                 end
418
419                 if live_signal_num == SBS_SIGNAL_NUM and
420                    (preview_signal_num == INPUT0_SIGNAL_NUM or preview_signal_num == INPUT1_SIGNAL_NUM) then
421                         -- Zoom in from SBS to single.
422                         transition_start = t
423                         transition_end = t + 1.0
424                         zoom_src = 0.0
425                         zoom_dst = 1.0
426                         zoom_poi = preview_signal_num
427                         preview_signal_num = SBS_SIGNAL_NUM
428                 elseif (live_signal_num == INPUT0_SIGNAL_NUM or live_signal_num == INPUT1_SIGNAL_NUM) and
429                        preview_signal_num == SBS_SIGNAL_NUM then
430                         -- Zoom out from single to SBS.
431                         transition_start = t
432                         transition_end = t + 1.0
433                         zoom_src = 1.0
434                         zoom_dst = 0.0
435                         preview_signal_num = live_signal_num
436                         zoom_poi = live_signal_num
437                         live_signal_num = SBS_SIGNAL_NUM
438                 end
439         elseif num == 2 then
440                 finish_transitions(t)
441
442                 -- Fade.
443                 if (live_signal_num ~= preview_signal_num) and
444                    (live_signal_num == INPUT0_SIGNAL_NUM or
445                     live_signal_num == INPUT1_SIGNAL_NUM or
446                     live_signal_num == STATIC_SIGNAL_NUM) and
447                    (preview_signal_num == INPUT0_SIGNAL_NUM or
448                     preview_signal_num == INPUT1_SIGNAL_NUM or
449                     preview_signal_num == STATIC_SIGNAL_NUM) then
450                         transition_start = t
451                         transition_end = t + 1.0
452                         fade_src_signal = live_signal_num
453                         fade_dst_signal = preview_signal_num
454                         preview_signal_num = live_signal_num
455                         live_signal_num = FADE_SIGNAL_NUM
456                 else
457                         -- Fades involving SBS are ignored (we have no chain for it).
458                 end
459         end
460 end
461
462 -- API ENTRY POINT
463 function channel_clicked(num)
464         preview_signal_num = num
465 end
466
467 -- API ENTRY POINT
468 -- Called every frame. Get the chain for displaying at input <num>,
469 -- where 0 is live, 1 is preview, 2 is the first channel to display
470 -- in the bottom bar, and so on up to num_channels()+1. t is the
471 -- current time in seconds. width and height are the dimensions of
472 -- the output, although you can ignore them if you don't need them
473 -- (they're useful if you want to e.g. know what to resample by).
474 --
475 -- <signals> is basically an exposed InputState, which you can use to
476 -- query for information about the signals at the point of the current
477 -- frame. In particular, you can call get_width() and get_height()
478 -- for any signal number, and use that to e.g. assist in chain selection.
479 --
480 -- You should return two objects; the chain itself, and then a
481 -- function (taking no parameters) that is run just before rendering.
482 -- The function needs to call connect_signal on any inputs, so that
483 -- it gets updated video data for the given frame. (You are allowed
484 -- to switch which input your input is getting from between frames,
485 -- but not calling connect_signal results in undefined behavior.)
486 -- If you want to change any parameters in the chain, this is also
487 -- the right place.
488 --
489 -- NOTE: The chain returned must be finalized with the Y'CbCr flag
490 -- if and only if num==0.
491 function get_chain(num, t, width, height, signals)
492         local input_resolution = {}
493         for signal_num=0,1 do
494                 local res = {
495                         width = signals:get_width(signal_num),
496                         height = signals:get_height(signal_num),
497                         interlaced = signals:get_interlaced(signal_num),
498                         has_signal = signals:get_has_signal(signal_num),
499                         frame_rate_nom = signals:get_frame_rate_nom(signal_num),
500                         frame_rate_den = signals:get_frame_rate_den(signal_num)
501                 }
502
503                 if res.interlaced then
504                         -- Convert height from frame height to field height.
505                         -- (Needed for e.g. place_rectangle.)
506                         res.height = res.height * 2
507
508                         -- Show field rate instead of frame rate; really for cosmetics only
509                         -- (and actually contrary to EBU recommendations, although in line
510                         -- with typical user expectations).
511                         res.frame_rate_nom = res.frame_rate_nom * 2
512                 end
513
514                 input_resolution[signal_num] = res
515         end
516         last_resolution = input_resolution
517
518         if num == 0 then  -- Live.
519                 if live_signal_num == INPUT0_SIGNAL_NUM or live_signal_num == INPUT1_SIGNAL_NUM then  -- Plain input.
520                         local input_type = get_input_type(signals, live_signal_num)
521                         local input_scale = needs_scale(signals, live_signal_num, width, height)
522                         local chain = simple_chains[input_type][input_scale][true]
523                         prepare = function()
524                                 chain.input:connect_signal(live_signal_num)
525                                 set_scale_parameters_if_needed(chain, width, height)
526                                 set_neutral_color_from_signal(chain.wb_effect, live_signal_num)
527                         end
528                         return chain.chain, prepare
529                 elseif live_signal_num == STATIC_SIGNAL_NUM then  -- Static picture.
530                         prepare = function()
531                         end
532                         return static_chain_hq, prepare
533                 elseif live_signal_num == FADE_SIGNAL_NUM then  -- Fade.
534                         local input0_type = get_input_type(signals, fade_src_signal)
535                         local input0_scale = needs_scale(signals, fade_src_signal, width, height)
536                         local input1_type = get_input_type(signals, fade_dst_signal)
537                         local input1_scale = needs_scale(signals, fade_dst_signal, width, height)
538                         local chain = fade_chains[input0_type][input0_scale][input1_type][input1_scale][true]
539                         prepare = function()
540                                 if input0_type == "live" or input0_type == "livedeint" then
541                                         chain.input0.input:connect_signal(fade_src_signal)
542                                         set_neutral_color_from_signal(chain.input0.wb_effect, fade_src_signal)
543                                 end
544                                 set_scale_parameters_if_needed(chain.input0, width, height)
545                                 if input1_type == "live" or input1_type == "livedeint" then
546                                         chain.input1.input:connect_signal(fade_dst_signal)
547                                         set_neutral_color_from_signal(chain.input1.wb_effect, fade_dst_signal)
548                                 end
549                                 set_scale_parameters_if_needed(chain.input1, width, height)
550                                 local tt = calc_fade_progress(t, transition_start, transition_end)
551
552                                 chain.mix_effect:set_float("strength_first", 1.0 - tt)
553                                 chain.mix_effect:set_float("strength_second", tt)
554                         end
555                         return chain.chain, prepare
556                 end
557
558                 -- SBS code (live_signal_num == SBS_SIGNAL_NUM).
559                 local input0_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
560                 local input1_type = get_input_type(signals, INPUT1_SIGNAL_NUM)
561                 if t > transition_end and zoom_dst == 1.0 then
562                         -- Special case: Show only the single image on screen.
563                         local input0_scale = needs_scale(signals, fade_src_signal, width, height)
564                         local chain = simple_chains[input0_type][input0_scale][true]
565                         prepare = function()
566                                 chain.input:connect_signal(INPUT0_SIGNAL_NUM)
567                                 set_scale_parameters_if_needed(chain, width, height)
568                                 set_neutral_color(chain.wb_effect, input0_neutral_color)
569                         end
570                         return chain.chain, prepare
571                 end
572                 local chain = sbs_chains[input0_type][input1_type][true]
573                 prepare = function()
574                         if t < transition_start then
575                                 prepare_sbs_chain(chain, zoom_src, width, height, input_resolution)
576                         elseif t > transition_end then
577                                 prepare_sbs_chain(chain, zoom_dst, width, height, input_resolution)
578                         else
579                                 local tt = (t - transition_start) / (transition_end - transition_start)
580                                 -- Smooth it a bit.
581                                 tt = math.sin(tt * 3.14159265358 * 0.5)
582                                 prepare_sbs_chain(chain, zoom_src + (zoom_dst - zoom_src) * tt, width, height, input_resolution)
583                         end
584                 end
585                 return chain.chain, prepare
586         end
587         if num == 1 then  -- Preview.
588                 num = preview_signal_num + 2
589         end
590
591         -- Individual preview inputs.
592         if num == INPUT0_SIGNAL_NUM + 2 then
593                 local input_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
594                 local input_scale = needs_scale(signals, INPUT0_SIGNAL_NUM, width, height)
595                 local chain = simple_chains[input_type][input_scale][false]
596                 prepare = function()
597                         chain.input:connect_signal(INPUT0_SIGNAL_NUM)
598                         set_scale_parameters_if_needed(chain, width, height)
599                         set_neutral_color(chain.wb_effect, input0_neutral_color)
600                 end
601                 return chain.chain, prepare
602         end
603         if num == INPUT1_SIGNAL_NUM + 2 then
604                 local input_type = get_input_type(signals, INPUT1_SIGNAL_NUM)
605                 local input_scale = needs_scale(signals, INPUT1_SIGNAL_NUM, width, height)
606                 local chain = simple_chains[input_type][input_scale][false]
607                 prepare = function()
608                         chain.input:connect_signal(INPUT1_SIGNAL_NUM)
609                         set_scale_parameters_if_needed(chain, width, height)
610                         set_neutral_color(chain.wb_effect, input1_neutral_color)
611                 end
612                 return chain.chain, prepare
613         end
614         if num == SBS_SIGNAL_NUM + 2 then
615                 local input0_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
616                 local input1_type = get_input_type(signals, INPUT1_SIGNAL_NUM)
617                 local chain = sbs_chains[input0_type][input1_type][false]
618                 prepare = function()
619                         prepare_sbs_chain(chain, 0.0, width, height, input_resolution)
620                 end
621                 return chain.chain, prepare
622         end
623         if num == STATIC_SIGNAL_NUM + 2 then
624                 prepare = function()
625                 end
626                 return static_chain_lq, prepare
627         end
628 end
629
630 function place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height)
631         local srcx0 = 0.0
632         local srcx1 = 1.0
633         local srcy0 = 0.0
634         local srcy1 = 1.0
635
636         -- Cull.
637         if x0 > screen_width or x1 < 0.0 or y0 > screen_height or y1 < 0.0 then
638                 resample_effect:set_int("width", 1)
639                 resample_effect:set_int("height", 1)
640                 resample_effect:set_float("zoom_x", screen_width)
641                 resample_effect:set_float("zoom_y", screen_height)
642                 padding_effect:set_int("left", screen_width + 100)
643                 padding_effect:set_int("top", screen_height + 100)
644                 return
645         end
646
647         -- Clip.
648         if x0 < 0 then
649                 srcx0 = -x0 / (x1 - x0)
650                 x0 = 0
651         end
652         if y0 < 0 then
653                 srcy0 = -y0 / (y1 - y0)
654                 y0 = 0
655         end
656         if x1 > screen_width then
657                 srcx1 = (screen_width - x0) / (x1 - x0)
658                 x1 = screen_width
659         end
660         if y1 > screen_height then
661                 srcy1 = (screen_height - y0) / (y1 - y0)
662                 y1 = screen_height
663         end
664
665         if resample_effect ~= nil then
666                 -- High-quality resampling.
667                 local x_subpixel_offset = x0 - math.floor(x0)
668                 local y_subpixel_offset = y0 - math.floor(y0)
669
670                 -- Resampling must be to an integral number of pixels. Round up,
671                 -- and then add an extra pixel so we have some leeway for the border.
672                 local width = math.ceil(x1 - x0) + 1
673                 local height = math.ceil(y1 - y0) + 1
674                 resample_effect:set_int("width", width)
675                 resample_effect:set_int("height", height)
676
677                 -- Correct the discrepancy with zoom. (This will leave a small
678                 -- excess edge of pixels and subpixels, which we'll correct for soon.)
679                 local zoom_x = (x1 - x0) / (width * (srcx1 - srcx0))
680                 local zoom_y = (y1 - y0) / (height * (srcy1 - srcy0))
681                 resample_effect:set_float("zoom_x", zoom_x)
682                 resample_effect:set_float("zoom_y", zoom_y)
683                 resample_effect:set_float("zoom_center_x", 0.0)
684                 resample_effect:set_float("zoom_center_y", 0.0)
685
686                 -- Padding must also be to a whole-pixel offset.
687                 padding_effect:set_int("left", math.floor(x0))
688                 padding_effect:set_int("top", math.floor(y0))
689
690                 -- Correct _that_ discrepancy by subpixel offset in the resampling.
691                 resample_effect:set_float("left", srcx0 * input_width - x_subpixel_offset / zoom_x)
692                 resample_effect:set_float("top", srcy0 * input_height - y_subpixel_offset / zoom_y)
693
694                 -- Finally, adjust the border so it is exactly where we want it.
695                 padding_effect:set_float("border_offset_left", x_subpixel_offset)
696                 padding_effect:set_float("border_offset_right", x1 - (math.floor(x0) + width))
697                 padding_effect:set_float("border_offset_top", y_subpixel_offset)
698                 padding_effect:set_float("border_offset_bottom", y1 - (math.floor(y0) + height))
699         else
700                 -- Lower-quality simple resizing.
701                 local width = round(x1 - x0)
702                 local height = round(y1 - y0)
703                 resize_effect:set_int("width", width)
704                 resize_effect:set_int("height", height)
705
706                 -- Padding must also be to a whole-pixel offset.
707                 padding_effect:set_int("left", math.floor(x0))
708                 padding_effect:set_int("top", math.floor(y0))
709         end
710 end
711
712 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
713 function round(x)
714         return math.floor(x + 0.5)
715 end
716
717 function lerp(a, b, t)
718         return a + (b - a) * t
719 end
720
721 function prepare_sbs_chain(chain, t, screen_width, screen_height, input_resolution)
722         chain.input0.input:connect_signal(0)
723         chain.input1.input:connect_signal(1)
724         set_neutral_color(chain.input0.wb_effect, input0_neutral_color)
725         set_neutral_color(chain.input1.wb_effect, input1_neutral_color)
726
727         -- First input is positioned (16,48) from top-left.
728         local width0 = round(848 * screen_width/1280.0)
729         local height0 = round(width0 * 9.0 / 16.0)
730
731         local top0 = 48 * screen_height/720.0
732         local left0 = 16 * screen_width/1280.0
733         local bottom0 = top0 + height0
734         local right0 = left0 + width0
735
736         -- Second input is positioned (16,48) from the bottom-right.
737         local width1 = round(384 * screen_width/1280.0)
738         local height1 = round(216 * screen_height/720.0)
739
740         local bottom1 = screen_height - 48 * screen_height/720.0
741         local right1 = screen_width - 16 * screen_width/1280.0
742         local top1 = bottom1 - height1
743         local left1 = right1 - width1
744
745         -- Interpolate between the fullscreen and side-by-side views.
746         local scale0, tx0, tx0
747         if zoom_poi == INPUT0_SIGNAL_NUM then
748                 local new_left0 = lerp(left0, 0, t)
749                 local new_right0 = lerp(right0, screen_width, t)
750                 local new_top0 = lerp(top0, 0, t)
751                 local new_bottom0 = lerp(bottom0, screen_height, t)
752
753                 scale0 = (new_right0 - new_left0) / width0  -- Same vertically and horizonally.
754                 tx0 = new_left0 - left0 * scale0
755                 ty0 = new_top0 - top0 * scale0
756         else
757                 local new_left1 = lerp(left1, 0, t)
758                 local new_right1 = lerp(right1, screen_width, t)
759                 local new_top1 = lerp(top1, 0, t)
760                 local new_bottom1 = lerp(bottom1, screen_height, t)
761
762                 scale0 = (new_right1 - new_left1) / width1  -- Same vertically and horizonally.
763                 tx0 = new_left1 - left1 * scale0
764                 ty0 = new_top1 - top1 * scale0
765         end
766
767         top0 = top0 * scale0 + ty0
768         bottom0 = bottom0 * scale0 + ty0
769         left0 = left0 * scale0 + tx0
770         right0 = right0 * scale0 + tx0
771
772         top1 = top1 * scale0 + ty0
773         bottom1 = bottom1 * scale0 + ty0
774         left1 = left1 * scale0 + tx0
775         right1 = right1 * scale0 + tx0
776         place_rectangle(chain.input0.resample_effect, chain.input0.resize_effect, chain.input0.padding_effect, left0, top0, right0, bottom0, screen_width, screen_height, input_resolution[0].width, input_resolution[0].height)
777         place_rectangle(chain.input1.resample_effect, chain.input1.resize_effect, chain.input1.padding_effect, left1, top1, right1, bottom1, screen_width, screen_height, input_resolution[1].width, input_resolution[1].height)
778 end
779
780 function set_neutral_color(effect, color)
781         effect:set_vec3("neutral_color", color[1], color[2], color[3])
782 end
783
784 function set_neutral_color_from_signal(effect, signal)
785         if signal == INPUT0_SIGNAL_NUM then
786                 set_neutral_color(effect, input0_neutral_color)
787         else
788                 set_neutral_color(effect, input1_neutral_color)
789         end
790 end
791
792 function calc_fade_progress(t, transition_start, transition_end)
793         local tt = (t - transition_start) / (transition_end - transition_start)
794         if tt < 0.0 then
795                 tt = 0.0
796         elseif tt > 1.0 then
797                 tt = 1.0
798         end
799
800         -- Make the fade look maybe a tad more natural, by pumping it
801         -- through a sigmoid function.
802         tt = 10.0 * tt - 5.0
803         tt = 1.0 / (1.0 + math.exp(-tt))
804
805         return tt
806 end