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