]> git.sesse.net Git - nageru/blob - theme.lua
Make the fade look maybe a tad more natural.
[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 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()
25         input0:connect_signal(0)
26         local input1 = chain:add_live_input()
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()
79 local fade_chain_hq_input1 = fade_chain_hq:add_live_input()
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()
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()
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 function finish_transitions(t)
104         -- If live is 2 (SBS) but de-facto single, make it so.
105         if live_signal_num == 2 and t >= transition_end and zoom_dst == 1.0 then
106                 live_signal_num = 0
107         end
108
109         -- If live is 3 (fade) but de-facto single, make it so.
110         if live_signal_num == 3 and t >= transition_end and fade_dst == 1.0 then
111                 live_signal_num = 0
112         end
113         if live_signal_num == 3 and t >= transition_end and fade_dst == 0.0 then
114                 live_signal_num = 1
115         end
116 end
117
118 -- Called every frame.
119 function get_transitions(t)
120         finish_transitions(t)
121
122         if live_signal_num == preview_signal_num then
123                 return {}
124         end
125
126         if live_signal_num == 2 and t >= transition_start and t <= transition_end then
127                 -- Zoom in progress.
128                 return {"Cut"}
129         end
130
131         if (live_signal_num == 0 and preview_signal_num == 1) or
132            (live_signal_num == 1 and preview_signal_num == 0) then
133                 return {"Cut", "", "Fade"}
134         end
135
136         if live_signal_num == 2 and preview_signal_num == 1 then
137                 -- Zoom-out not supported here yet.
138                 return {"Cut"}
139         end
140
141         if live_signal_num == 2 and preview_signal_num == 0 then
142                 return {"Cut", "Zoom in"}
143         elseif live_signal_num == 0 and preview_signal_num == 2 then
144                 return {"Cut", "Zoom out"}
145         end
146
147         return {"Cut"}
148 end
149
150 function transition_clicked(num, t)
151         if num == 0 then
152                 -- Cut.
153                 if live_signal_num == 3 then
154                         -- Ongoing fade; finish it immediately.
155                         finish_transitions(transition_end)
156                 end
157
158                 local temp = live_signal_num
159                 live_signal_num = preview_signal_num
160                 preview_signal_num = temp
161
162                 if live_signal_num == 2 then
163                         -- Just cut to SBS, we need to reset any zooms.
164                         zoom_src = 1.0
165                         zoom_dst = 0.0
166                         transition_start = -2.0
167                         transition_end = -1.0
168                 end
169         elseif num == 1 then
170                 -- Zoom.
171
172                 finish_transitions(t)
173
174                 if live_signal_num == preview_signal_num then
175                         -- Nothing to do.
176                         return
177                 end
178
179                 if (live_signal_num == 0 and preview_signal_num == 1) or
180                    (live_signal_num == 1 and preview_signal_num == 0) then
181                         -- We can't zoom between these. Just make a cut.
182                         io.write("Cutting from " .. live_signal_num .. " to " .. live_signal_num .. "\n")
183                         local temp = live_signal_num
184                         live_signal_num = preview_signal_num
185                         preview_signal_num = temp
186                         return
187                 end
188
189                 if live_signal_num == 2 and preview_signal_num == 1 then
190                         io.write("NOT SUPPORTED YET\n")
191                         return
192                 end
193
194                 if live_signal_num == 2 and preview_signal_num == 0 then
195                         -- Zoom in from SBS to single.
196                         transition_start = t
197                         transition_end = t + 1.0
198                         zoom_src = 0.0
199                         zoom_dst = 1.0
200                         preview_signal_num = 2
201                 elseif live_signal_num == 0 and preview_signal_num == 2 then
202                         -- Zoom out from single to SBS.
203                         transition_start = t
204                         transition_end = t + 1.0
205                         zoom_src = 1.0
206                         zoom_dst = 0.0
207                         preview_signal_num = 0
208                         live_signal_num = 2
209                 end
210         elseif num == 2 then
211                 finish_transitions(t)
212
213                 -- Fade.
214                 if live_signal_num == 0 and preview_signal_num == 1 then
215                         transition_start = t
216                         transition_end = t + 1.0
217                         fade_src = 1.0
218                         fade_dst = 0.0
219                         preview_signal_num = 0
220                         live_signal_num = 3
221                 elseif live_signal_num == 1 and preview_signal_num == 0 then
222                         transition_start = t
223                         transition_end = t + 1.0
224                         fade_src = 0.0
225                         fade_dst = 1.0
226                         preview_signal_num = 1
227                         live_signal_num = 3
228                 else
229                         -- Fades involving SBS are ignored (we have no chain for it).
230                 end
231         end
232 end
233
234 function channel_clicked(num)
235         preview_signal_num = num
236 end
237
238 -- Called every frame. Get the chain for displaying at input <num>,
239 -- where 0 is live, 1 is preview, 2 is the first channel to display
240 -- in the bottom bar, and so on up to num_channels()+1. t is the
241 -- current time in seconds. width and height are the dimensions of
242 -- the output, although you can ignore them if you don't need them
243 -- (they're useful if you want to e.g. know what to resample by).
244 --
245 -- You should return two objects; the chain itself, and then a
246 -- function (taking no parameters) that is run just before rendering.
247 -- The function needs to call connect_signal on any inputs, so that
248 -- it gets updated video data for the given frame. (You are allowed
249 -- to switch which input your input is getting from between frames,
250 -- but not calling connect_signal results in undefined behavior.)
251 -- If you want to change any parameters in the chain, this is also
252 -- the right place.
253 --
254 -- NOTE: The chain returned must be finalized with the Y'CbCr flag
255 -- if and only if num==0.
256 function get_chain(num, t, width, height)
257         if num == 0 then  -- Live.
258                 if live_signal_num == 0 or live_signal_num == 1 then  -- Plain inputs.
259                         prepare = function()
260                                 simple_chain_hq_input:connect_signal(live_signal_num)
261                         end
262                         return simple_chain_hq, prepare
263                 elseif live_signal_num == 3 then  -- Fade.
264                         prepare = function()
265                                 fade_chain_hq_input0:connect_signal(0)
266                                 fade_chain_hq_input1:connect_signal(1)
267                                 local tt = (t - transition_start) / (transition_end - transition_start)
268                                 if tt < 0.0 then
269                                         tt = 0.0
270                                 elseif tt > 1.0 then
271                                         tt = 1.0
272                                 end
273
274                                 tt = fade_src + tt * (fade_dst - fade_src)
275
276                                 -- Make the fade look maybe a tad more natural, by pumping it
277                                 -- through a sigmoid function.
278                                 tt = 10.0 * tt - 5.0
279                                 tt = 1.0 / (1.0 + math.exp(-tt))
280
281                                 fade_chain_mix_effect:set_float("strength_first", tt)
282                                 fade_chain_mix_effect:set_float("strength_second", 1.0 - tt)
283                         end
284                         return fade_chain_hq, prepare
285                 end
286
287                 -- SBS code (live_signal_num == 2).
288                 if t > transition_end and zoom_dst == 1.0 then
289                         -- Special case: Show only the single image on screen.
290                         prepare = function()
291                                 simple_chain_hq_input:connect_signal(0)
292                         end
293                         return simple_chain_hq, prepare
294                 end
295                 prepare = function()
296                         if t < transition_start then
297                                 prepare_sbs_chain(main_chain_hq, zoom_src, width, height)
298                         elseif t > transition_end then
299                                 prepare_sbs_chain(main_chain_hq, zoom_dst, width, height)
300                         else
301                                 local tt = (t - transition_start) / (transition_end - transition_start)
302                                 -- Smooth it a bit.
303                                 tt = math.sin(tt * 3.14159265358 * 0.5)
304                                 prepare_sbs_chain(main_chain_hq, zoom_src + (zoom_dst - zoom_src) * tt, width, height)
305                         end
306                 end
307                 return main_chain_hq.chain, prepare
308         end
309         if num == 1 then  -- Preview.
310                 num = preview_signal_num + 2
311         end
312         if num == 2 then
313                 prepare = function()
314                         simple_chain_lq_input:connect_signal(0)
315                 end
316                 return simple_chain_lq, prepare
317         end
318         if num == 3 then
319                 prepare = function()
320                         simple_chain_lq_input:connect_signal(1)
321                 end
322                 return simple_chain_lq, prepare
323         end
324         if num == 4 then
325                 prepare = function()
326                         prepare_sbs_chain(main_chain_lq, 0.0, width, height)
327                 end
328                 return main_chain_lq.chain, prepare
329         end
330 end
331
332 function place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height)
333         local srcx0 = 0.0
334         local srcx1 = 1.0
335         local srcy0 = 0.0
336         local srcy1 = 1.0
337
338         -- Cull.
339         if x0 > screen_width or x1 < 0.0 or y0 > screen_height or y1 < 0.0 then
340                 resample_effect:set_int("width", 1)
341                 resample_effect:set_int("height", 1)
342                 resample_effect:set_float("zoom_x", screen_width)
343                 resample_effect:set_float("zoom_y", screen_height)
344                 padding_effect:set_int("left", screen_width + 100)
345                 padding_effect:set_int("top", screen_height + 100)
346                 return
347         end
348
349         -- Clip. (TODO: Clip on upper/left sides, too.)
350         if x1 > screen_width then
351                 srcx1 = (screen_width - x0) / (x1 - x0)
352                 x1 = screen_width
353         end
354         if y1 > screen_height then
355                 srcy1 = (screen_height - y0) / (y1 - y0)
356                 y1 = screen_height
357         end
358
359         if resample_effect ~= nil then
360                 -- High-quality resampling.
361                 local x_subpixel_offset = x0 - math.floor(x0)
362                 local y_subpixel_offset = y0 - math.floor(y0)
363
364                 -- Resampling must be to an integral number of pixels. Round up,
365                 -- and then add an extra pixel so we have some leeway for the border.
366                 local width = math.ceil(x1 - x0) + 1
367                 local height = math.ceil(y1 - y0) + 1
368                 resample_effect:set_int("width", width)
369                 resample_effect:set_int("height", height)
370
371                 -- Correct the discrepancy with zoom. (This will leave a small
372                 -- excess edge of pixels and subpixels, which we'll correct for soon.)
373                 local zoom_x = (x1 - x0) / (width * (srcx1 - srcx0))
374                 local zoom_y = (y1 - y0) / (height * (srcy1 - srcy0))
375                 resample_effect:set_float("zoom_x", zoom_x)
376                 resample_effect:set_float("zoom_y", zoom_y)
377                 resample_effect:set_float("zoom_center_x", 0.0)
378                 resample_effect:set_float("zoom_center_y", 0.0)
379
380                 -- Padding must also be to a whole-pixel offset.
381                 padding_effect:set_int("left", math.floor(x0))
382                 padding_effect:set_int("top", math.floor(y0))
383
384                 -- Correct _that_ discrepancy by subpixel offset in the resampling.
385                 resample_effect:set_float("left", -x_subpixel_offset / zoom_x)
386                 resample_effect:set_float("top", -y_subpixel_offset / zoom_y)
387
388                 -- Finally, adjust the border so it is exactly where we want it.
389                 padding_effect:set_float("border_offset_left", x_subpixel_offset)
390                 padding_effect:set_float("border_offset_right", x1 - (math.floor(x0) + width))
391                 padding_effect:set_float("border_offset_top", y_subpixel_offset)
392                 padding_effect:set_float("border_offset_bottom", y1 - (math.floor(y0) + height))
393         else
394                 -- Lower-quality simple resizing.
395                 local width = round(x1 - x0)
396                 local height = round(y1 - y0)
397                 resize_effect:set_int("width", width)
398                 resize_effect:set_int("height", height)
399
400                 -- Padding must also be to a whole-pixel offset.
401                 padding_effect:set_int("left", math.floor(x0))
402                 padding_effect:set_int("top", math.floor(y0))
403         end
404 end
405
406 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
407 function round(x)
408         return math.floor(x + 0.5)
409 end
410
411 function prepare_sbs_chain(chain, t, screen_width, screen_height)
412         chain.input0.input:connect_signal(0)
413         chain.input1.input:connect_signal(1)
414
415         -- First input is positioned (16,48) from top-left.
416         local width0 = round(848 * screen_width/1280.0)
417         local height0 = round(width0 * 9.0 / 16.0)
418
419         local top0 = 48 * screen_height/720.0
420         local left0 = 16 * screen_width/1280.0
421         local bottom0 = top0 + height0
422         local right0 = left0 + width0
423
424         -- Second input is positioned (16,48) from the bottom-right.
425         local width1 = 384 * screen_width/1280.0
426         local height1 = 216 * screen_height/720.0
427
428         local bottom1 = screen_height - 48 * screen_height/720.0
429         local right1 = screen_width - 16 * screen_width/1280.0
430         local top1 = bottom1 - height1
431         local left1 = right1 - width1
432
433         -- Interpolate between the fullscreen and side-by-side views.
434         local scale0 = 1.0 + t * (1280.0 / 848.0 - 1.0)
435         local tx0 = 0.0 + t * (-left0 * scale0)
436         local ty0 = 0.0 + t * (-top0 * scale0)
437
438         top0 = top0 * scale0 + ty0
439         bottom0 = bottom0 * scale0 + ty0
440         left0 = left0 * scale0 + tx0
441         right0 = right0 * scale0 + tx0
442
443         top1 = top1 * scale0 + ty0
444         bottom1 = bottom1 * scale0 + ty0
445         left1 = left1 * scale0 + tx0
446         right1 = right1 * scale0 + tx0
447         place_rectangle(chain.input0.resample_effect, chain.input0.resize_effect, chain.input0.padding_effect, left0, top0, right0, bottom0, screen_width, screen_height)
448         place_rectangle(chain.input1.resample_effect, chain.input1.resize_effect, chain.input1.padding_effect, left1, top1, right1, bottom1, screen_width, screen_height)
449 end