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