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