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