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