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