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.
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.
10 local transition_start = -2.0
11 local transition_end = -1.0
14 local zoom_poi = 0 -- which input to zoom in on
18 local input0_neutral_color = {0.5, 0.5, 0.5}
19 local input1_neutral_color = {0.5, 0.5, 0.5}
21 local live_signal_num = 0
22 local preview_signal_num = 1
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())
34 local resample_effect = nil
35 local resize_effect = nil
37 resample_effect = chain:add_effect(ResampleEffect.new(), input0_wb_effect)
39 resize_effect = chain:add_effect(ResizeEffect.new(), input0_wb_effect)
42 local padding_effect = chain:add_effect(IntegralPaddingEffect.new())
43 padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 1.0)
45 local resample2_effect = nil
46 local resize2_effect = nil
48 resample2_effect = chain:add_effect(ResampleEffect.new(), input1_wb_effect)
50 resize2_effect = chain:add_effect(ResizeEffect.new(), input1_wb_effect)
52 local padding2_effect = chain:add_effect(IntegralPaddingEffect.new())
54 chain:add_effect(OverlayEffect.new(), padding_effect, padding2_effect)
61 white_balance_effect = input0_wb_effect,
62 resample_effect = resample_effect,
63 resize_effect = resize_effect,
64 padding_effect = padding_effect
68 white_balance_effect = input1_wb_effect,
69 resample_effect = resample2_effect,
70 resize_effect = resize2_effect,
71 padding_effect = padding2_effect
76 local main_chain_hq = make_sbs_chain(true)
77 local main_chain_lq = make_sbs_chain(false)
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)
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)
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)
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()
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)
115 elseif channel == 3 then
118 return "Side-by-side"
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
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)
132 input0_neutral_color = { red, green, blue }
133 elseif channel == 3 then
134 input1_neutral_color = { red, green, blue }
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
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
148 if live_signal_num == 3 and t >= transition_end and fade_dst == 0.0 then
153 -- Called every frame.
154 function get_transitions(t)
155 finish_transitions(t)
157 if live_signal_num == preview_signal_num then
161 if live_signal_num == 2 and t >= transition_start and t <= transition_end then
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"}
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"}
181 function transition_clicked(num, t)
184 if live_signal_num == 3 then
185 -- Ongoing fade; finish it immediately.
186 finish_transitions(transition_end)
189 local temp = live_signal_num
190 live_signal_num = preview_signal_num
191 preview_signal_num = temp
193 if live_signal_num == 2 then
194 -- Just cut to SBS, we need to reset any zooms.
197 transition_start = -2.0
198 transition_end = -1.0
203 finish_transitions(t)
205 if live_signal_num == preview_signal_num then
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
220 if live_signal_num == 2 and (preview_signal_num == 0 or preview_signal_num == 1) then
221 -- Zoom in from SBS to single.
223 transition_end = t + 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.
231 transition_end = t + 1.0
234 preview_signal_num = live_signal_num
235 zoom_poi = live_signal_num
239 finish_transitions(t)
242 if live_signal_num == 0 and preview_signal_num == 1 then
244 transition_end = t + 1.0
247 preview_signal_num = 0
249 elseif live_signal_num == 1 and preview_signal_num == 0 then
251 transition_end = t + 1.0
254 preview_signal_num = 1
257 -- Fades involving SBS are ignored (we have no chain for it).
262 function channel_clicked(num)
263 preview_signal_num = num
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).
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
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.
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)
292 set_neutral_color(simple_chain_hq_wb_effect, input1_neutral_color)
295 return simple_chain_hq, prepare
296 elseif live_signal_num == 3 then -- Fade.
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)
309 tt = fade_src + tt * (fade_dst - fade_src)
311 -- Make the fade look maybe a tad more natural, by pumping it
312 -- through a sigmoid function.
314 tt = 1.0 / (1.0 + math.exp(-tt))
316 fade_chain_mix_effect:set_float("strength_first", tt)
317 fade_chain_mix_effect:set_float("strength_second", 1.0 - tt)
319 return fade_chain_hq, prepare
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.
326 simple_chain_hq_input:connect_signal(0)
327 set_neutral_color(simple_chain_hq_wb_effect, input0_neutral_color)
329 return simple_chain_hq, prepare
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)
337 local tt = (t - transition_start) / (transition_end - transition_start)
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)
343 return main_chain_hq.chain, prepare
345 if num == 1 then -- Preview.
346 num = preview_signal_num + 2
350 simple_chain_lq_input:connect_signal(0)
351 set_neutral_color(simple_chain_lq_wb_effect, input0_neutral_color)
353 return simple_chain_lq, prepare
357 simple_chain_lq_input:connect_signal(1)
358 set_neutral_color(simple_chain_lq_wb_effect, input1_neutral_color)
360 return simple_chain_lq, prepare
364 prepare_sbs_chain(main_chain_lq, 0.0, width, height)
366 return main_chain_lq.chain, prepare
370 function place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height)
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)
389 srcx0 = -x0 / (x1 - x0)
393 srcy0 = -y0 / (y1 - y0)
396 if x1 > screen_width then
397 srcx1 = (screen_width - x0) / (x1 - x0)
400 if y1 > screen_height then
401 srcy1 = (screen_height - y0) / (y1 - y0)
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)
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)
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)
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))
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)
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))
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)
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))
452 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
454 return math.floor(x + 0.5)
457 function lerp(a, b, t)
458 return a + (b - a) * t
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)
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)
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
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)
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
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)
493 scale0 = (new_right0 - new_left0) / width0 -- Same vertically and horizonally.
494 tx0 = new_left0 - left0 * scale0
495 ty0 = new_top0 - top0 * scale0
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)
502 scale0 = (new_right1 - new_left1) / width1 -- Same vertically and horizonally.
503 tx0 = new_left1 - left1 * scale0
504 ty0 = new_top1 - top1 * scale0
507 top0 = top0 * scale0 + ty0
508 bottom0 = bottom0 * scale0 + ty0
509 left0 = left0 * scale0 + tx0
510 right0 = right0 * scale0 + tx0
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)
520 function set_neutral_color(effect, color)
521 effect:set_vec3("neutral_color", color[1], color[2], color[3])