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 -- TODO: Deal with inputs that are different from our native 1280x720 resolution.
12 local transition_start = -2.0
13 local transition_end = -1.0
16 local zoom_poi = 0 -- which input to zoom in on
17 local fade_src_signal = 0
18 local fade_dst_signal = 0
20 local input0_neutral_color = {0.5, 0.5, 0.5}
21 local input1_neutral_color = {0.5, 0.5, 0.5}
23 local live_signal_num = 0
24 local preview_signal_num = 1
26 -- Valid values for live_signal_num and preview_signal_num.
27 local INPUT0_SIGNAL_NUM = 0
28 local INPUT1_SIGNAL_NUM = 1
29 local SBS_SIGNAL_NUM = 2
30 local STATIC_SIGNAL_NUM = 3
32 -- “fake” signal numbers that signifies that we are fading from one input
34 local FADE_VTV_SIGNAL_NUM = 4 -- Video to/from video.
35 local FADE_VTP_SIGNAL_NUM = 5 -- Video to/from static picture.
37 -- The main live chain.
38 function make_sbs_chain(hq)
39 local chain = EffectChain.new(16, 9)
40 local input0 = chain:add_live_input(true)
41 input0:connect_signal(0)
42 local input0_wb_effect = chain:add_effect(WhiteBalanceEffect.new())
43 local input1 = chain:add_live_input(true)
44 input1:connect_signal(1)
45 local input1_wb_effect = chain:add_effect(WhiteBalanceEffect.new())
47 local resample_effect = nil
48 local resize_effect = nil
50 resample_effect = chain:add_effect(ResampleEffect.new(), input0_wb_effect)
52 resize_effect = chain:add_effect(ResizeEffect.new(), input0_wb_effect)
55 local padding_effect = chain:add_effect(IntegralPaddingEffect.new())
56 padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 1.0)
58 local resample2_effect = nil
59 local resize2_effect = nil
61 resample2_effect = chain:add_effect(ResampleEffect.new(), input1_wb_effect)
63 resize2_effect = chain:add_effect(ResizeEffect.new(), input1_wb_effect)
65 local padding2_effect = chain:add_effect(IntegralPaddingEffect.new())
67 chain:add_effect(OverlayEffect.new(), padding_effect, padding2_effect)
74 white_balance_effect = input0_wb_effect,
75 resample_effect = resample_effect,
76 resize_effect = resize_effect,
77 padding_effect = padding_effect
81 white_balance_effect = input1_wb_effect,
82 resample_effect = resample2_effect,
83 resize_effect = resize2_effect,
84 padding_effect = padding2_effect
89 local main_chain_hq = make_sbs_chain(true)
90 local main_chain_lq = make_sbs_chain(false)
92 -- A chain to fade between two inputs (live chain only)
93 local fade_vtv_chain_hq = EffectChain.new(16, 9)
94 local fade_vtv_chain_hq_input0 = fade_vtv_chain_hq:add_live_input(true)
95 local fade_vtv_chain_hq_wb0_effect = fade_vtv_chain_hq:add_effect(WhiteBalanceEffect.new())
96 local fade_vtv_chain_hq_input1 = fade_vtv_chain_hq:add_live_input(true)
97 local fade_vtv_chain_hq_wb1_effect = fade_vtv_chain_hq:add_effect(WhiteBalanceEffect.new())
98 fade_vtv_chain_hq_input0:connect_signal(0)
99 fade_vtv_chain_hq_input1:connect_signal(1)
100 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)
101 fade_vtv_chain_hq:finalize(true)
103 -- A chain to fade between an inputs and a picture (live chain only)
104 local fade_vtp_chain_hq = EffectChain.new(16, 9)
105 local fade_vtp_chain_hq_input0 = fade_vtp_chain_hq:add_live_input(true)
106 local fade_vtp_chain_hq_wb0_effect = fade_vtp_chain_hq:add_effect(WhiteBalanceEffect.new())
107 local fade_vtp_chain_hq_input1 = fade_vtp_chain_hq:add_effect(ImageInput.new("bg.jpeg"))
108 fade_vtp_chain_hq_input0:connect_signal(0)
109 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)
110 fade_vtp_chain_hq:finalize(true)
112 -- A chain to show a single input on screen (HQ version).
113 local simple_chain_hq = EffectChain.new(16, 9)
114 local simple_chain_hq_input = simple_chain_hq:add_live_input(true)
115 simple_chain_hq_input:connect_signal(0) -- First input card. Can be changed whenever you want.
116 local simple_chain_hq_wb_effect = simple_chain_hq:add_effect(WhiteBalanceEffect.new())
117 simple_chain_hq:finalize(true)
119 -- A chain to show a single input on screen (LQ version).
120 local simple_chain_lq = EffectChain.new(16, 9)
121 local simple_chain_lq_input = simple_chain_lq:add_live_input(true)
122 simple_chain_lq_input:connect_signal(0) -- First input card. Can be changed whenever you want.
123 local simple_chain_lq_wb_effect = simple_chain_lq:add_effect(WhiteBalanceEffect.new())
124 simple_chain_lq:finalize(false)
126 -- A chain to show a single static picture on screen (HQ version).
127 local static_chain_hq = EffectChain.new(16, 9)
128 local static_chain_hq_input = static_chain_hq:add_effect(ImageInput.new("bg.jpeg"))
129 static_chain_hq:finalize(true)
131 -- A chain to show a single static picture on screen (LQ version).
132 local static_chain_lq = EffectChain.new(16, 9)
133 local static_chain_lq_input = static_chain_lq:add_effect(ImageInput.new("bg.jpeg"))
134 static_chain_lq:finalize(false)
136 -- Returns the number of outputs in addition to the live (0) and preview (1).
137 -- Called only once, at the start of the program.
138 function num_channels()
142 -- Returns the name for each additional channel (starting from 2).
143 -- Called only once for each channel, at the start of the program.
144 function channel_name(channel)
147 elseif channel == 3 then
149 elseif channel == 4 then
150 return "Side-by-side"
151 elseif channel == 5 then
152 return "Static picture"
156 -- Returns if a given channel supports setting white balance (starting from 2).
157 -- Called only once for each channel, at the start of the program.
158 function supports_set_wb(channel)
159 return channel == 2 or channel == 3
162 -- Gets called with a new gray point when the white balance is changing.
163 -- The color is in linear light (not sRGB gamma).
164 function set_wb(channel, red, green, blue)
166 input0_neutral_color = { red, green, blue }
167 elseif channel == 3 then
168 input1_neutral_color = { red, green, blue }
172 function finish_transitions(t)
173 -- If live is SBS but de-facto single, make it so.
174 if live_signal_num == SBS_SIGNAL_NUM and t >= transition_end and zoom_dst == 1.0 then
175 live_signal_num = zoom_poi
178 -- If live is fade but de-facto single, make it so.
179 if (live_signal_num == FADE_VTV_SIGNAL_NUM or live_signal_num == FADE_VTP_SIGNAL_NUM) and t >= transition_end then
180 live_signal_num = fade_dst_signal
184 -- Called every frame.
185 function get_transitions(t)
186 finish_transitions(t)
188 if live_signal_num == preview_signal_num then
192 if live_signal_num == SBS_SIGNAL_NUM and t >= transition_start and t <= transition_end then
197 if (live_signal_num == INPUT0_SIGNAL_NUM or
198 live_signal_num == INPUT1_SIGNAL_NUM or
199 live_signal_num == STATIC_SIGNAL_NUM) and
200 (preview_signal_num == INPUT0_SIGNAL_NUM or
201 preview_signal_num == INPUT1_SIGNAL_NUM or
202 preview_signal_num == STATIC_SIGNAL_NUM) then
203 return {"Cut", "", "Fade"}
207 if live_signal_num == SBS_SIGNAL_NUM and
208 (preview_signal_num == INPUT0_SIGNAL_NUM or preview_signal_num == INPUT1_SIGNAL_NUM) then
209 return {"Cut", "Zoom in"}
210 elseif (live_signal_num == INPUT0_SIGNAL_NUM or live_signal_num == INPUT1_SIGNAL_NUM) and
211 preview_signal_num == SBS_SIGNAL_NUM then
212 return {"Cut", "Zoom out"}
218 function transition_clicked(num, t)
221 if live_signal_num == FADE_VTV_SIGNAL_NUM or live_signal_num == FADE_VTP_SIGNAL_NUM then
222 -- Ongoing fade; finish it immediately.
223 finish_transitions(transition_end)
226 local temp = live_signal_num
227 live_signal_num = preview_signal_num
228 preview_signal_num = temp
230 if live_signal_num == SBS_SIGNAL_NUM then
231 -- Just cut to SBS, we need to reset any zooms.
234 transition_start = -2.0
235 transition_end = -1.0
240 finish_transitions(t)
242 if live_signal_num == preview_signal_num then
247 if (live_signal_num == INPUT0_SIGNAL_NUM and preview_signal_num == INPUT1_SIGNAL_NUM) or
248 (live_signal_num == INPUT1_SIGNAL_NUM and preview_signal_num == INPUT0_SIGNAL_NUM) then
249 -- We can't zoom between these. Just make a cut.
250 io.write("Cutting from " .. live_signal_num .. " to " .. live_signal_num .. "\n")
251 local temp = live_signal_num
252 live_signal_num = preview_signal_num
253 preview_signal_num = temp
257 if live_signal_num == SBS_SIGNAL_NUM and
258 (preview_signal_num == INPUT0_SIGNAL_NUM or preview_signal_num == INPUT1_SIGNAL_NUM) then
259 -- Zoom in from SBS to single.
261 transition_end = t + 1.0
264 zoom_poi = preview_signal_num
265 preview_signal_num = SBS_SIGNAL_NUM
266 elseif (live_signal_num == INPUT0_SIGNAL_NUM or live_signal_num == INPUT1_SIGNAL_NUM) and
267 preview_signal_num == SBS_SIGNAL_NUM then
268 -- Zoom out from single to SBS.
270 transition_end = t + 1.0
273 preview_signal_num = live_signal_num
274 zoom_poi = live_signal_num
275 live_signal_num = SBS_SIGNAL_NUM
278 finish_transitions(t)
281 if (live_signal_num == INPUT0_SIGNAL_NUM and preview_signal_num == INPUT1_SIGNAL_NUM) or
282 (live_signal_num == INPUT1_SIGNAL_NUM and preview_signal_num == INPUT0_SIGNAL_NUM) then
284 transition_end = t + 1.0
285 fade_src_signal = live_signal_num
286 fade_dst_signal = preview_signal_num
287 preview_signal_num = live_signal_num
288 live_signal_num = FADE_VTV_SIGNAL_NUM
289 elseif ((live_signal_num == INPUT0_SIGNAL_NUM or live_signal_num == INPUT1_SIGNAL_NUM) and
290 preview_signal_num == STATIC_SIGNAL_NUM) or
291 ((preview_signal_num == INPUT0_SIGNAL_NUM or preview_signal_num == INPUT1_SIGNAL_NUM) and
292 live_signal_num == STATIC_SIGNAL_NUM) then
294 transition_end = t + 1.0
295 fade_src_signal = live_signal_num
296 fade_dst_signal = preview_signal_num
297 preview_signal_num = live_signal_num
298 live_signal_num = FADE_VTP_SIGNAL_NUM
300 -- Fades involving SBS are ignored (we have no chain for it).
305 function channel_clicked(num)
306 preview_signal_num = num
309 -- Called every frame. Get the chain for displaying at input <num>,
310 -- where 0 is live, 1 is preview, 2 is the first channel to display
311 -- in the bottom bar, and so on up to num_channels()+1. t is the
312 -- current time in seconds. width and height are the dimensions of
313 -- the output, although you can ignore them if you don't need them
314 -- (they're useful if you want to e.g. know what to resample by).
316 -- You should return two objects; the chain itself, and then a
317 -- function (taking no parameters) that is run just before rendering.
318 -- The function needs to call connect_signal on any inputs, so that
319 -- it gets updated video data for the given frame. (You are allowed
320 -- to switch which input your input is getting from between frames,
321 -- but not calling connect_signal results in undefined behavior.)
322 -- If you want to change any parameters in the chain, this is also
325 -- NOTE: The chain returned must be finalized with the Y'CbCr flag
326 -- if and only if num==0.
327 function get_chain(num, t, width, height)
328 if num == 0 then -- Live.
329 if live_signal_num == INPUT0_SIGNAL_NUM or live_signal_num == INPUT1_SIGNAL_NUM then -- Plain input.
331 simple_chain_hq_input:connect_signal(live_signal_num)
332 set_neutral_color_from_signal(simple_chain_hq_wb_effect, live_signal_num)
334 return simple_chain_hq, prepare
335 elseif live_signal_num == STATIC_SIGNAL_NUM then -- Static picture.
338 return static_chain_hq, prepare
339 elseif live_signal_num == FADE_VTV_SIGNAL_NUM then -- Fade video-to-video.
341 fade_vtv_chain_hq_input0:connect_signal(fade_src_signal)
342 set_neutral_color_from_signal(fade_vtv_chain_hq_wb0_effect, fade_src_signal)
343 fade_vtv_chain_hq_input1:connect_signal(fade_dst_signal)
344 set_neutral_color_from_signal(fade_vtv_chain_hq_wb1_effect, fade_dst_signal)
345 local tt = calc_fade_progress(t, transition_start, transition_end)
347 fade_vtv_chain_mix_effect:set_float("strength_first", 1.0 - tt)
348 fade_vtv_chain_mix_effect:set_float("strength_second", tt)
350 return fade_vtv_chain_hq, prepare
351 elseif live_signal_num == FADE_VTP_SIGNAL_NUM then -- Fade video-to-picture (or picture-to-video).
354 if fade_src_signal == STATIC_SIGNAL_NUM then
355 fade_vtp_chain_hq_input0:connect_signal(fade_dst_signal)
356 set_neutral_color_from_signal(fade_vtp_chain_hq_wb0_effect, fade_dst_signal)
357 tt = 1.0 - calc_fade_progress(t, transition_start, transition_end)
359 fade_vtp_chain_hq_input0:connect_signal(fade_src_signal)
360 set_neutral_color_from_signal(fade_vtp_chain_hq_wb0_effect, fade_src_signal)
361 tt = calc_fade_progress(t, transition_start, transition_end)
363 fade_vtp_chain_mix_effect:set_float("strength_first", 1.0 - tt)
364 fade_vtp_chain_mix_effect:set_float("strength_second", tt)
366 return fade_vtp_chain_hq, prepare
369 -- SBS code (live_signal_num == SBS_SIGNAL_NUM).
370 if t > transition_end and zoom_dst == 1.0 then
371 -- Special case: Show only the single image on screen.
373 simple_chain_hq_input:connect_signal(INPUT0_SIGNAL_NUM)
374 set_neutral_color(simple_chain_hq_wb_effect, input0_neutral_color)
376 return simple_chain_hq, prepare
379 if t < transition_start then
380 prepare_sbs_chain(main_chain_hq, zoom_src, width, height)
381 elseif t > transition_end then
382 prepare_sbs_chain(main_chain_hq, zoom_dst, width, height)
384 local tt = (t - transition_start) / (transition_end - transition_start)
386 tt = math.sin(tt * 3.14159265358 * 0.5)
387 prepare_sbs_chain(main_chain_hq, zoom_src + (zoom_dst - zoom_src) * tt, width, height)
390 return main_chain_hq.chain, prepare
392 if num == 1 then -- Preview.
393 num = preview_signal_num + 2
395 if num == INPUT0_SIGNAL_NUM + 2 then
397 simple_chain_lq_input:connect_signal(0)
398 set_neutral_color(simple_chain_lq_wb_effect, input0_neutral_color)
400 return simple_chain_lq, prepare
402 if num == INPUT1_SIGNAL_NUM + 2 then
404 simple_chain_lq_input:connect_signal(1)
405 set_neutral_color(simple_chain_lq_wb_effect, input1_neutral_color)
407 return simple_chain_lq, prepare
409 if num == SBS_SIGNAL_NUM + 2 then
411 prepare_sbs_chain(main_chain_lq, 0.0, width, height)
413 return main_chain_lq.chain, prepare
415 if num == STATIC_SIGNAL_NUM + 2 then
418 return static_chain_lq, prepare
422 function place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height)
429 if x0 > screen_width or x1 < 0.0 or y0 > screen_height or y1 < 0.0 then
430 resample_effect:set_int("width", 1)
431 resample_effect:set_int("height", 1)
432 resample_effect:set_float("zoom_x", screen_width)
433 resample_effect:set_float("zoom_y", screen_height)
434 padding_effect:set_int("left", screen_width + 100)
435 padding_effect:set_int("top", screen_height + 100)
441 srcx0 = -x0 / (x1 - x0)
445 srcy0 = -y0 / (y1 - y0)
448 if x1 > screen_width then
449 srcx1 = (screen_width - x0) / (x1 - x0)
452 if y1 > screen_height then
453 srcy1 = (screen_height - y0) / (y1 - y0)
457 if resample_effect ~= nil then
458 -- High-quality resampling.
459 local x_subpixel_offset = x0 - math.floor(x0)
460 local y_subpixel_offset = y0 - math.floor(y0)
462 -- Resampling must be to an integral number of pixels. Round up,
463 -- and then add an extra pixel so we have some leeway for the border.
464 local width = math.ceil(x1 - x0) + 1
465 local height = math.ceil(y1 - y0) + 1
466 resample_effect:set_int("width", width)
467 resample_effect:set_int("height", height)
469 -- Correct the discrepancy with zoom. (This will leave a small
470 -- excess edge of pixels and subpixels, which we'll correct for soon.)
471 local zoom_x = (x1 - x0) / (width * (srcx1 - srcx0))
472 local zoom_y = (y1 - y0) / (height * (srcy1 - srcy0))
473 resample_effect:set_float("zoom_x", zoom_x)
474 resample_effect:set_float("zoom_y", zoom_y)
475 resample_effect:set_float("zoom_center_x", 0.0)
476 resample_effect:set_float("zoom_center_y", 0.0)
478 -- Padding must also be to a whole-pixel offset.
479 padding_effect:set_int("left", math.floor(x0))
480 padding_effect:set_int("top", math.floor(y0))
482 -- Correct _that_ discrepancy by subpixel offset in the resampling.
483 resample_effect:set_float("left", srcx0 * input_width - x_subpixel_offset / zoom_x)
484 resample_effect:set_float("top", srcy0 * input_height - y_subpixel_offset / zoom_y)
486 -- Finally, adjust the border so it is exactly where we want it.
487 padding_effect:set_float("border_offset_left", x_subpixel_offset)
488 padding_effect:set_float("border_offset_right", x1 - (math.floor(x0) + width))
489 padding_effect:set_float("border_offset_top", y_subpixel_offset)
490 padding_effect:set_float("border_offset_bottom", y1 - (math.floor(y0) + height))
492 -- Lower-quality simple resizing.
493 local width = round(x1 - x0)
494 local height = round(y1 - y0)
495 resize_effect:set_int("width", width)
496 resize_effect:set_int("height", height)
498 -- Padding must also be to a whole-pixel offset.
499 padding_effect:set_int("left", math.floor(x0))
500 padding_effect:set_int("top", math.floor(y0))
504 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
506 return math.floor(x + 0.5)
509 function lerp(a, b, t)
510 return a + (b - a) * t
513 function prepare_sbs_chain(chain, t, screen_width, screen_height)
514 chain.input0.input:connect_signal(0)
515 chain.input1.input:connect_signal(1)
516 set_neutral_color(chain.input0.white_balance_effect, input0_neutral_color)
517 set_neutral_color(chain.input1.white_balance_effect, input1_neutral_color)
519 -- First input is positioned (16,48) from top-left.
520 local width0 = round(848 * screen_width/1280.0)
521 local height0 = round(width0 * 9.0 / 16.0)
523 local top0 = 48 * screen_height/720.0
524 local left0 = 16 * screen_width/1280.0
525 local bottom0 = top0 + height0
526 local right0 = left0 + width0
528 -- Second input is positioned (16,48) from the bottom-right.
529 local width1 = round(384 * screen_width/1280.0)
530 local height1 = round(216 * screen_height/720.0)
532 local bottom1 = screen_height - 48 * screen_height/720.0
533 local right1 = screen_width - 16 * screen_width/1280.0
534 local top1 = bottom1 - height1
535 local left1 = right1 - width1
537 -- Interpolate between the fullscreen and side-by-side views.
538 local scale0, tx0, tx0
539 if zoom_poi == INPUT0_SIGNAL_NUM then
540 local new_left0 = lerp(left0, 0, t)
541 local new_right0 = lerp(right0, screen_width, t)
542 local new_top0 = lerp(top0, 0, t)
543 local new_bottom0 = lerp(bottom0, screen_height, t)
545 scale0 = (new_right0 - new_left0) / width0 -- Same vertically and horizonally.
546 tx0 = new_left0 - left0 * scale0
547 ty0 = new_top0 - top0 * scale0
549 local new_left1 = lerp(left1, 0, t)
550 local new_right1 = lerp(right1, screen_width, t)
551 local new_top1 = lerp(top1, 0, t)
552 local new_bottom1 = lerp(bottom1, screen_height, t)
554 scale0 = (new_right1 - new_left1) / width1 -- Same vertically and horizonally.
555 tx0 = new_left1 - left1 * scale0
556 ty0 = new_top1 - top1 * scale0
559 top0 = top0 * scale0 + ty0
560 bottom0 = bottom0 * scale0 + ty0
561 left0 = left0 * scale0 + tx0
562 right0 = right0 * scale0 + tx0
564 top1 = top1 * scale0 + ty0
565 bottom1 = bottom1 * scale0 + ty0
566 left1 = left1 * scale0 + tx0
567 right1 = right1 * scale0 + tx0
568 place_rectangle(chain.input0.resample_effect, chain.input0.resize_effect, chain.input0.padding_effect, left0, top0, right0, bottom0, screen_width, screen_height, 1280, 720)
569 place_rectangle(chain.input1.resample_effect, chain.input1.resize_effect, chain.input1.padding_effect, left1, top1, right1, bottom1, screen_width, screen_height, 1280, 720)
572 function set_neutral_color(effect, color)
573 effect:set_vec3("neutral_color", color[1], color[2], color[3])
576 function set_neutral_color_from_signal(effect, signal)
577 if signal == INPUT0_SIGNAL_NUM then
578 set_neutral_color(effect, input0_neutral_color)
580 set_neutral_color(effect, input1_neutral_color)
584 function calc_fade_progress(t, transition_start, transition_end)
585 local tt = (t - transition_start) / (transition_end - transition_start)
592 -- Make the fade look maybe a tad more natural, by pumping it
593 -- through a sigmoid function.
595 tt = 1.0 / (1.0 + math.exp(-tt))