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