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