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
15 local fade_src_signal = 0
16 local fade_dst_signal = 0
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 -- Valid values for live_signal_num and preview_signal_num.
25 local INPUT0_SIGNAL_NUM = 0
26 local INPUT1_SIGNAL_NUM = 1
27 local SBS_SIGNAL_NUM = 2
28 local STATIC_SIGNAL_NUM = 3
30 -- “fake” signal numbers that signifies that we are fading from one input
32 local FADE_VTV_SIGNAL_NUM = 4 -- Video to/from video.
33 local FADE_VTP_SIGNAL_NUM = 5 -- Video to/from static picture.
35 -- The main live chain.
36 function make_sbs_chain(hq)
37 local chain = EffectChain.new(16, 9)
38 local input0 = chain:add_live_input(true)
39 input0:connect_signal(0)
40 local input0_wb_effect = chain:add_effect(WhiteBalanceEffect.new())
41 local input1 = chain:add_live_input(true)
42 input1:connect_signal(1)
43 local input1_wb_effect = chain:add_effect(WhiteBalanceEffect.new())
45 local resample_effect = nil
46 local resize_effect = nil
48 resample_effect = chain:add_effect(ResampleEffect.new(), input0_wb_effect)
50 resize_effect = chain:add_effect(ResizeEffect.new(), input0_wb_effect)
53 local padding_effect = chain:add_effect(IntegralPaddingEffect.new())
54 padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 1.0)
56 local resample2_effect = nil
57 local resize2_effect = nil
59 resample2_effect = chain:add_effect(ResampleEffect.new(), input1_wb_effect)
61 resize2_effect = chain:add_effect(ResizeEffect.new(), input1_wb_effect)
63 local padding2_effect = chain:add_effect(IntegralPaddingEffect.new())
65 chain:add_effect(OverlayEffect.new(), padding_effect, padding2_effect)
72 white_balance_effect = input0_wb_effect,
73 resample_effect = resample_effect,
74 resize_effect = resize_effect,
75 padding_effect = padding_effect
79 white_balance_effect = input1_wb_effect,
80 resample_effect = resample2_effect,
81 resize_effect = resize2_effect,
82 padding_effect = padding2_effect
87 local main_chain_hq = make_sbs_chain(true)
88 local main_chain_lq = make_sbs_chain(false)
90 -- A chain to fade between two inputs (live chain only)
91 local fade_vtv_chain_hq = EffectChain.new(16, 9)
92 local fade_vtv_chain_hq_input0 = fade_vtv_chain_hq:add_live_input(true)
93 local fade_vtv_chain_hq_wb0_effect = fade_vtv_chain_hq:add_effect(WhiteBalanceEffect.new())
94 local fade_vtv_chain_hq_input1 = fade_vtv_chain_hq:add_live_input(true)
95 local fade_vtv_chain_hq_wb1_effect = fade_vtv_chain_hq:add_effect(WhiteBalanceEffect.new())
96 fade_vtv_chain_hq_input0:connect_signal(0)
97 fade_vtv_chain_hq_input1:connect_signal(1)
98 local fade_vtv_chain_mix_effect = fade_vtv_chain_hq:add_effect(MixEffect.new(), fade_vtv_chain_hq_wb0_effect, fade_vtv_chain_hq_wb1_effect)
99 fade_vtv_chain_hq:finalize(true)
101 -- A chain to fade between an inputs and a picture (live chain only)
102 local fade_vtp_chain_hq = EffectChain.new(16, 9)
103 local fade_vtp_chain_hq_input0 = fade_vtp_chain_hq:add_live_input(true)
104 local fade_vtp_chain_hq_wb0_effect = fade_vtp_chain_hq:add_effect(WhiteBalanceEffect.new())
105 local fade_vtp_chain_hq_input1 = fade_vtp_chain_hq:add_effect(ImageInput.new("bg.jpeg"))
106 fade_vtp_chain_hq_input0:connect_signal(0)
107 local fade_vtp_chain_mix_effect = fade_vtp_chain_hq:add_effect(MixEffect.new(), fade_vtp_chain_hq_wb0_effect, fade_vtp_chain_hq_input1)
108 fade_vtp_chain_hq:finalize(true)
110 -- A chain to show a single input on screen (HQ version).
111 local simple_chain_hq = EffectChain.new(16, 9)
112 local simple_chain_hq_input = simple_chain_hq:add_live_input(true)
113 simple_chain_hq_input:connect_signal(0) -- First input card. Can be changed whenever you want.
114 local simple_chain_hq_wb_effect = simple_chain_hq:add_effect(WhiteBalanceEffect.new())
115 simple_chain_hq:finalize(true)
117 -- A chain to show a single input on screen (LQ version).
118 local simple_chain_lq = EffectChain.new(16, 9)
119 local simple_chain_lq_input = simple_chain_lq:add_live_input(true)
120 simple_chain_lq_input:connect_signal(0) -- First input card. Can be changed whenever you want.
121 local simple_chain_lq_wb_effect = simple_chain_lq:add_effect(WhiteBalanceEffect.new())
122 simple_chain_lq:finalize(false)
124 -- A chain to show a single static picture on screen (HQ version).
125 local static_chain_hq = EffectChain.new(16, 9)
126 local static_chain_hq_input = static_chain_hq:add_effect(ImageInput.new("bg.jpeg"))
127 static_chain_hq:finalize(true)
129 -- A chain to show a single static picture on screen (LQ version).
130 local static_chain_lq = EffectChain.new(16, 9)
131 local static_chain_lq_input = static_chain_lq:add_effect(ImageInput.new("bg.jpeg"))
132 static_chain_lq:finalize(false)
134 -- Returns the number of outputs in addition to the live (0) and preview (1).
135 -- Called only once, at the start of the program.
136 function num_channels()
140 -- Returns the name for each additional channel (starting from 2).
141 -- Called only once for each channel, at the start of the program.
142 function channel_name(channel)
145 elseif channel == 3 then
147 elseif channel == 4 then
148 return "Side-by-side"
149 elseif channel == 5 then
150 return "Static picture"
154 -- Returns if a given channel supports setting white balance (starting from 2).
155 -- Called only once for each channel, at the start of the program.
156 function supports_set_wb(channel)
157 return channel == 2 or channel == 3
160 -- Gets called with a new gray point when the white balance is changing.
161 -- The color is in linear light (not sRGB gamma).
162 function set_wb(channel, red, green, blue)
164 input0_neutral_color = { red, green, blue }
165 elseif channel == 3 then
166 input1_neutral_color = { red, green, blue }
170 function finish_transitions(t)
171 -- If live is SBS but de-facto single, make it so.
172 if live_signal_num == SBS_SIGNAL_NUM and t >= transition_end and zoom_dst == 1.0 then
173 live_signal_num = zoom_poi
176 -- If live is fade but de-facto single, make it so.
177 if (live_signal_num == FADE_VTV_SIGNAL_NUM or live_signal_num == FADE_VTP_SIGNAL_NUM) and t >= transition_end then
178 live_signal_num = fade_dst_signal
182 -- Called every frame.
183 function get_transitions(t)
184 finish_transitions(t)
186 if live_signal_num == preview_signal_num then
190 if live_signal_num == SBS_SIGNAL_NUM and t >= transition_start and t <= transition_end then
195 if (live_signal_num == INPUT0_SIGNAL_NUM or
196 live_signal_num == INPUT1_SIGNAL_NUM or
197 live_signal_num == STATIC_SIGNAL_NUM) and
198 (preview_signal_num == INPUT0_SIGNAL_NUM or
199 preview_signal_num == INPUT1_SIGNAL_NUM or
200 preview_signal_num == STATIC_SIGNAL_NUM) then
201 return {"Cut", "", "Fade"}
205 if live_signal_num == SBS_SIGNAL_NUM and
206 (preview_signal_num == INPUT0_SIGNAL_NUM or preview_signal_num == INPUT1_SIGNAL_NUM) then
207 return {"Cut", "Zoom in"}
208 elseif (live_signal_num == INPUT0_SIGNAL_NUM or live_signal_num == INPUT1_SIGNAL_NUM) and
209 preview_signal_num == SBS_SIGNAL_NUM then
210 return {"Cut", "Zoom out"}
216 function transition_clicked(num, t)
219 if live_signal_num == FADE_VTV_SIGNAL_NUM or live_signal_num == FADE_VTP_SIGNAL_NUM then
220 -- Ongoing fade; finish it immediately.
221 finish_transitions(transition_end)
224 local temp = live_signal_num
225 live_signal_num = preview_signal_num
226 preview_signal_num = temp
228 if live_signal_num == SBS_SIGNAL_NUM then
229 -- Just cut to SBS, we need to reset any zooms.
232 transition_start = -2.0
233 transition_end = -1.0
238 finish_transitions(t)
240 if live_signal_num == preview_signal_num then
245 if (live_signal_num == INPUT0_SIGNAL_NUM and preview_signal_num == INPUT1_SIGNAL_NUM) or
246 (live_signal_num == INPUT1_SIGNAL_NUM and preview_signal_num == INPUT0_SIGNAL_NUM) then
247 -- We can't zoom between these. Just make a cut.
248 io.write("Cutting from " .. live_signal_num .. " to " .. live_signal_num .. "\n")
249 local temp = live_signal_num
250 live_signal_num = preview_signal_num
251 preview_signal_num = temp
255 if live_signal_num == SBS_SIGNAL_NUM and
256 (preview_signal_num == INPUT0_SIGNAL_NUM or preview_signal_num == INPUT1_SIGNAL_NUM) then
257 -- Zoom in from SBS to single.
259 transition_end = t + 1.0
262 zoom_poi = preview_signal_num
263 preview_signal_num = SBS_SIGNAL_NUM
264 elseif (live_signal_num == INPUT0_SIGNAL_NUM or live_signal_num == INPUT1_SIGNAL_NUM) and
265 preview_signal_num == SBS_SIGNAL_NUM then
266 -- Zoom out from single to SBS.
268 transition_end = t + 1.0
271 preview_signal_num = live_signal_num
272 zoom_poi = live_signal_num
273 live_signal_num = SBS_SIGNAL_NUM
276 finish_transitions(t)
279 if (live_signal_num == INPUT0_SIGNAL_NUM and preview_signal_num == INPUT1_SIGNAL_NUM) or
280 (live_signal_num == INPUT1_SIGNAL_NUM and preview_signal_num == INPUT0_SIGNAL_NUM) then
282 transition_end = t + 1.0
283 fade_src_signal = live_signal_num
284 fade_dst_signal = preview_signal_num
285 preview_signal_num = live_signal_num
286 live_signal_num = FADE_VTV_SIGNAL_NUM
287 elseif ((live_signal_num == INPUT0_SIGNAL_NUM or live_signal_num == INPUT1_SIGNAL_NUM) and
288 preview_signal_num == STATIC_SIGNAL_NUM) or
289 ((preview_signal_num == INPUT0_SIGNAL_NUM or preview_signal_num == INPUT1_SIGNAL_NUM) and
290 live_signal_num == STATIC_SIGNAL_NUM) then
292 transition_end = t + 1.0
293 fade_src_signal = live_signal_num
294 fade_dst_signal = preview_signal_num
295 preview_signal_num = live_signal_num
296 live_signal_num = FADE_VTP_SIGNAL_NUM
298 -- Fades involving SBS are ignored (we have no chain for it).
303 function channel_clicked(num)
304 preview_signal_num = num
307 -- Called every frame. Get the chain for displaying at input <num>,
308 -- where 0 is live, 1 is preview, 2 is the first channel to display
309 -- in the bottom bar, and so on up to num_channels()+1. t is the
310 -- current time in seconds. width and height are the dimensions of
311 -- the output, although you can ignore them if you don't need them
312 -- (they're useful if you want to e.g. know what to resample by).
314 -- You should return two objects; the chain itself, and then a
315 -- function (taking no parameters) that is run just before rendering.
316 -- The function needs to call connect_signal on any inputs, so that
317 -- it gets updated video data for the given frame. (You are allowed
318 -- to switch which input your input is getting from between frames,
319 -- but not calling connect_signal results in undefined behavior.)
320 -- If you want to change any parameters in the chain, this is also
323 -- NOTE: The chain returned must be finalized with the Y'CbCr flag
324 -- if and only if num==0.
325 function get_chain(num, t, width, height)
326 if num == 0 then -- Live.
327 if live_signal_num == INPUT0_SIGNAL_NUM or live_signal_num == INPUT1_SIGNAL_NUM then -- Plain input.
329 simple_chain_hq_input:connect_signal(live_signal_num)
330 set_neutral_color_from_signal(simple_chain_hq_wb_effect, live_signal_num)
332 return simple_chain_hq, prepare
333 elseif live_signal_num == STATIC_SIGNAL_NUM then -- Static picture.
336 return static_chain_hq, prepare
337 elseif live_signal_num == FADE_VTV_SIGNAL_NUM then -- Fade video-to-video.
339 fade_vtv_chain_hq_input0:connect_signal(fade_src_signal)
340 set_neutral_color_from_signal(fade_vtv_chain_hq_wb0_effect, fade_src_signal)
341 fade_vtv_chain_hq_input1:connect_signal(fade_dst_signal)
342 set_neutral_color_from_signal(fade_vtv_chain_hq_wb1_effect, fade_dst_signal)
343 local tt = calc_fade_progress(t, transition_start, transition_end)
345 fade_vtv_chain_mix_effect:set_float("strength_first", 1.0 - tt)
346 fade_vtv_chain_mix_effect:set_float("strength_second", tt)
348 return fade_vtv_chain_hq, prepare
349 elseif live_signal_num == FADE_VTP_SIGNAL_NUM then -- Fade video-to-picture (or picture-to-video).
352 if fade_src_signal == STATIC_SIGNAL_NUM then
353 fade_vtp_chain_hq_input0:connect_signal(fade_dst_signal)
354 set_neutral_color_from_signal(fade_vtp_chain_hq_wb0_effect, fade_dst_signal)
355 tt = 1.0 - calc_fade_progress(t, transition_start, transition_end)
357 fade_vtp_chain_hq_input0:connect_signal(fade_src_signal)
358 set_neutral_color_from_signal(fade_vtp_chain_hq_wb0_effect, fade_src_signal)
359 tt = calc_fade_progress(t, transition_start, transition_end)
361 fade_vtp_chain_mix_effect:set_float("strength_first", 1.0 - tt)
362 fade_vtp_chain_mix_effect:set_float("strength_second", tt)
364 return fade_vtp_chain_hq, prepare
367 -- SBS code (live_signal_num == SBS_SIGNAL_NUM).
368 if t > transition_end and zoom_dst == 1.0 then
369 -- Special case: Show only the single image on screen.
371 simple_chain_hq_input:connect_signal(INPUT0_SIGNAL_NUM)
372 set_neutral_color(simple_chain_hq_wb_effect, input0_neutral_color)
374 return simple_chain_hq, prepare
377 if t < transition_start then
378 prepare_sbs_chain(main_chain_hq, zoom_src, width, height)
379 elseif t > transition_end then
380 prepare_sbs_chain(main_chain_hq, zoom_dst, width, height)
382 local tt = (t - transition_start) / (transition_end - transition_start)
384 tt = math.sin(tt * 3.14159265358 * 0.5)
385 prepare_sbs_chain(main_chain_hq, zoom_src + (zoom_dst - zoom_src) * tt, width, height)
388 return main_chain_hq.chain, prepare
390 if num == 1 then -- Preview.
391 num = preview_signal_num + 2
393 if num == INPUT0_SIGNAL_NUM + 2 then
395 simple_chain_lq_input:connect_signal(0)
396 set_neutral_color(simple_chain_lq_wb_effect, input0_neutral_color)
398 return simple_chain_lq, prepare
400 if num == INPUT1_SIGNAL_NUM + 2 then
402 simple_chain_lq_input:connect_signal(1)
403 set_neutral_color(simple_chain_lq_wb_effect, input1_neutral_color)
405 return simple_chain_lq, prepare
407 if num == SBS_SIGNAL_NUM + 2 then
409 prepare_sbs_chain(main_chain_lq, 0.0, width, height)
411 return main_chain_lq.chain, prepare
413 if num == STATIC_SIGNAL_NUM + 2 then
416 return static_chain_lq, prepare
420 function place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height)
427 if x0 > screen_width or x1 < 0.0 or y0 > screen_height or y1 < 0.0 then
428 resample_effect:set_int("width", 1)
429 resample_effect:set_int("height", 1)
430 resample_effect:set_float("zoom_x", screen_width)
431 resample_effect:set_float("zoom_y", screen_height)
432 padding_effect:set_int("left", screen_width + 100)
433 padding_effect:set_int("top", screen_height + 100)
439 srcx0 = -x0 / (x1 - x0)
443 srcy0 = -y0 / (y1 - y0)
446 if x1 > screen_width then
447 srcx1 = (screen_width - x0) / (x1 - x0)
450 if y1 > screen_height then
451 srcy1 = (screen_height - y0) / (y1 - y0)
455 if resample_effect ~= nil then
456 -- High-quality resampling.
457 local x_subpixel_offset = x0 - math.floor(x0)
458 local y_subpixel_offset = y0 - math.floor(y0)
460 -- Resampling must be to an integral number of pixels. Round up,
461 -- and then add an extra pixel so we have some leeway for the border.
462 local width = math.ceil(x1 - x0) + 1
463 local height = math.ceil(y1 - y0) + 1
464 resample_effect:set_int("width", width)
465 resample_effect:set_int("height", height)
467 -- Correct the discrepancy with zoom. (This will leave a small
468 -- excess edge of pixels and subpixels, which we'll correct for soon.)
469 local zoom_x = (x1 - x0) / (width * (srcx1 - srcx0))
470 local zoom_y = (y1 - y0) / (height * (srcy1 - srcy0))
471 resample_effect:set_float("zoom_x", zoom_x)
472 resample_effect:set_float("zoom_y", zoom_y)
473 resample_effect:set_float("zoom_center_x", 0.0)
474 resample_effect:set_float("zoom_center_y", 0.0)
476 -- Padding must also be to a whole-pixel offset.
477 padding_effect:set_int("left", math.floor(x0))
478 padding_effect:set_int("top", math.floor(y0))
480 -- Correct _that_ discrepancy by subpixel offset in the resampling.
481 resample_effect:set_float("left", srcx0 * input_width - x_subpixel_offset / zoom_x)
482 resample_effect:set_float("top", srcy0 * input_height - y_subpixel_offset / zoom_y)
484 -- Finally, adjust the border so it is exactly where we want it.
485 padding_effect:set_float("border_offset_left", x_subpixel_offset)
486 padding_effect:set_float("border_offset_right", x1 - (math.floor(x0) + width))
487 padding_effect:set_float("border_offset_top", y_subpixel_offset)
488 padding_effect:set_float("border_offset_bottom", y1 - (math.floor(y0) + height))
490 -- Lower-quality simple resizing.
491 local width = round(x1 - x0)
492 local height = round(y1 - y0)
493 resize_effect:set_int("width", width)
494 resize_effect:set_int("height", height)
496 -- Padding must also be to a whole-pixel offset.
497 padding_effect:set_int("left", math.floor(x0))
498 padding_effect:set_int("top", math.floor(y0))
502 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
504 return math.floor(x + 0.5)
507 function lerp(a, b, t)
508 return a + (b - a) * t
511 function prepare_sbs_chain(chain, t, screen_width, screen_height)
512 chain.input0.input:connect_signal(0)
513 chain.input1.input:connect_signal(1)
514 set_neutral_color(chain.input0.white_balance_effect, input0_neutral_color)
515 set_neutral_color(chain.input1.white_balance_effect, input1_neutral_color)
517 -- First input is positioned (16,48) from top-left.
518 local width0 = round(848 * screen_width/1280.0)
519 local height0 = round(width0 * 9.0 / 16.0)
521 local top0 = 48 * screen_height/720.0
522 local left0 = 16 * screen_width/1280.0
523 local bottom0 = top0 + height0
524 local right0 = left0 + width0
526 -- Second input is positioned (16,48) from the bottom-right.
527 local width1 = round(384 * screen_width/1280.0)
528 local height1 = round(216 * screen_height/720.0)
530 local bottom1 = screen_height - 48 * screen_height/720.0
531 local right1 = screen_width - 16 * screen_width/1280.0
532 local top1 = bottom1 - height1
533 local left1 = right1 - width1
535 -- Interpolate between the fullscreen and side-by-side views.
536 local scale0, tx0, tx0
537 if zoom_poi == INPUT0_SIGNAL_NUM then
538 local new_left0 = lerp(left0, 0, t)
539 local new_right0 = lerp(right0, screen_width, t)
540 local new_top0 = lerp(top0, 0, t)
541 local new_bottom0 = lerp(bottom0, screen_height, t)
543 scale0 = (new_right0 - new_left0) / width0 -- Same vertically and horizonally.
544 tx0 = new_left0 - left0 * scale0
545 ty0 = new_top0 - top0 * scale0
547 local new_left1 = lerp(left1, 0, t)
548 local new_right1 = lerp(right1, screen_width, t)
549 local new_top1 = lerp(top1, 0, t)
550 local new_bottom1 = lerp(bottom1, screen_height, t)
552 scale0 = (new_right1 - new_left1) / width1 -- Same vertically and horizonally.
553 tx0 = new_left1 - left1 * scale0
554 ty0 = new_top1 - top1 * scale0
557 top0 = top0 * scale0 + ty0
558 bottom0 = bottom0 * scale0 + ty0
559 left0 = left0 * scale0 + tx0
560 right0 = right0 * scale0 + tx0
562 top1 = top1 * scale0 + ty0
563 bottom1 = bottom1 * scale0 + ty0
564 left1 = left1 * scale0 + tx0
565 right1 = right1 * scale0 + tx0
566 place_rectangle(chain.input0.resample_effect, chain.input0.resize_effect, chain.input0.padding_effect, left0, top0, right0, bottom0, screen_width, screen_height, 1280, 720)
567 place_rectangle(chain.input1.resample_effect, chain.input1.resize_effect, chain.input1.padding_effect, left1, top1, right1, bottom1, screen_width, screen_height, 1280, 720)
570 function set_neutral_color(effect, color)
571 effect:set_vec3("neutral_color", color[1], color[2], color[3])
574 function set_neutral_color_from_signal(effect, signal)
575 if signal == INPUT0_SIGNAL_NUM then
576 set_neutral_color(effect, input0_neutral_color)
578 set_neutral_color(effect, input1_neutral_color)
582 function calc_fade_progress(t, transition_start, transition_end)
583 local tt = (t - transition_start) / (transition_end - transition_start)
590 -- Make the fade look maybe a tad more natural, by pumping it
591 -- through a sigmoid function.
593 tt = 1.0 / (1.0 + math.exp(-tt))