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