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 -- <signals> is basically an exposed InputState, which you can use to
317 -- query for information about the signals at the point of the current
318 -- frame. In particular, you can call get_width() and get_height()
319 -- for any signal number, and use that to e.g. assist in chain selection.
321 -- You should return two objects; the chain itself, and then a
322 -- function (taking no parameters) that is run just before rendering.
323 -- The function needs to call connect_signal on any inputs, so that
324 -- it gets updated video data for the given frame. (You are allowed
325 -- to switch which input your input is getting from between frames,
326 -- but not calling connect_signal results in undefined behavior.)
327 -- If you want to change any parameters in the chain, this is also
330 -- NOTE: The chain returned must be finalized with the Y'CbCr flag
331 -- if and only if num==0.
332 function get_chain(num, t, width, height, signals)
333 if num == 0 then -- Live.
334 if live_signal_num == INPUT0_SIGNAL_NUM or live_signal_num == INPUT1_SIGNAL_NUM then -- Plain input.
336 simple_chain_hq_input:connect_signal(live_signal_num)
337 set_neutral_color_from_signal(simple_chain_hq_wb_effect, live_signal_num)
339 return simple_chain_hq, prepare
340 elseif live_signal_num == STATIC_SIGNAL_NUM then -- Static picture.
343 return static_chain_hq, prepare
344 elseif live_signal_num == FADE_VTV_SIGNAL_NUM then -- Fade video-to-video.
346 fade_vtv_chain_hq_input0:connect_signal(fade_src_signal)
347 set_neutral_color_from_signal(fade_vtv_chain_hq_wb0_effect, fade_src_signal)
348 fade_vtv_chain_hq_input1:connect_signal(fade_dst_signal)
349 set_neutral_color_from_signal(fade_vtv_chain_hq_wb1_effect, fade_dst_signal)
350 local tt = calc_fade_progress(t, transition_start, transition_end)
352 fade_vtv_chain_mix_effect:set_float("strength_first", 1.0 - tt)
353 fade_vtv_chain_mix_effect:set_float("strength_second", tt)
355 return fade_vtv_chain_hq, prepare
356 elseif live_signal_num == FADE_VTP_SIGNAL_NUM then -- Fade video-to-picture (or picture-to-video).
359 if fade_src_signal == STATIC_SIGNAL_NUM then
360 fade_vtp_chain_hq_input0:connect_signal(fade_dst_signal)
361 set_neutral_color_from_signal(fade_vtp_chain_hq_wb0_effect, fade_dst_signal)
362 tt = 1.0 - calc_fade_progress(t, transition_start, transition_end)
364 fade_vtp_chain_hq_input0:connect_signal(fade_src_signal)
365 set_neutral_color_from_signal(fade_vtp_chain_hq_wb0_effect, fade_src_signal)
366 tt = calc_fade_progress(t, transition_start, transition_end)
368 fade_vtp_chain_mix_effect:set_float("strength_first", 1.0 - tt)
369 fade_vtp_chain_mix_effect:set_float("strength_second", tt)
371 return fade_vtp_chain_hq, prepare
374 -- SBS code (live_signal_num == SBS_SIGNAL_NUM).
375 if t > transition_end and zoom_dst == 1.0 then
376 -- Special case: Show only the single image on screen.
378 simple_chain_hq_input:connect_signal(INPUT0_SIGNAL_NUM)
379 set_neutral_color(simple_chain_hq_wb_effect, input0_neutral_color)
381 return simple_chain_hq, prepare
384 if t < transition_start then
385 prepare_sbs_chain(main_chain_hq, zoom_src, width, height)
386 elseif t > transition_end then
387 prepare_sbs_chain(main_chain_hq, zoom_dst, width, height)
389 local tt = (t - transition_start) / (transition_end - transition_start)
391 tt = math.sin(tt * 3.14159265358 * 0.5)
392 prepare_sbs_chain(main_chain_hq, zoom_src + (zoom_dst - zoom_src) * tt, width, height)
395 return main_chain_hq.chain, prepare
397 if num == 1 then -- Preview.
398 num = preview_signal_num + 2
400 if num == INPUT0_SIGNAL_NUM + 2 then
402 simple_chain_lq_input:connect_signal(0)
403 set_neutral_color(simple_chain_lq_wb_effect, input0_neutral_color)
405 return simple_chain_lq, prepare
407 if num == INPUT1_SIGNAL_NUM + 2 then
409 simple_chain_lq_input:connect_signal(1)
410 set_neutral_color(simple_chain_lq_wb_effect, input1_neutral_color)
412 return simple_chain_lq, prepare
414 if num == SBS_SIGNAL_NUM + 2 then
416 prepare_sbs_chain(main_chain_lq, 0.0, width, height)
418 return main_chain_lq.chain, prepare
420 if num == STATIC_SIGNAL_NUM + 2 then
423 return static_chain_lq, prepare
427 function place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height)
434 if x0 > screen_width or x1 < 0.0 or y0 > screen_height or y1 < 0.0 then
435 resample_effect:set_int("width", 1)
436 resample_effect:set_int("height", 1)
437 resample_effect:set_float("zoom_x", screen_width)
438 resample_effect:set_float("zoom_y", screen_height)
439 padding_effect:set_int("left", screen_width + 100)
440 padding_effect:set_int("top", screen_height + 100)
446 srcx0 = -x0 / (x1 - x0)
450 srcy0 = -y0 / (y1 - y0)
453 if x1 > screen_width then
454 srcx1 = (screen_width - x0) / (x1 - x0)
457 if y1 > screen_height then
458 srcy1 = (screen_height - y0) / (y1 - y0)
462 if resample_effect ~= nil then
463 -- High-quality resampling.
464 local x_subpixel_offset = x0 - math.floor(x0)
465 local y_subpixel_offset = y0 - math.floor(y0)
467 -- Resampling must be to an integral number of pixels. Round up,
468 -- and then add an extra pixel so we have some leeway for the border.
469 local width = math.ceil(x1 - x0) + 1
470 local height = math.ceil(y1 - y0) + 1
471 resample_effect:set_int("width", width)
472 resample_effect:set_int("height", height)
474 -- Correct the discrepancy with zoom. (This will leave a small
475 -- excess edge of pixels and subpixels, which we'll correct for soon.)
476 local zoom_x = (x1 - x0) / (width * (srcx1 - srcx0))
477 local zoom_y = (y1 - y0) / (height * (srcy1 - srcy0))
478 resample_effect:set_float("zoom_x", zoom_x)
479 resample_effect:set_float("zoom_y", zoom_y)
480 resample_effect:set_float("zoom_center_x", 0.0)
481 resample_effect:set_float("zoom_center_y", 0.0)
483 -- Padding must also be to a whole-pixel offset.
484 padding_effect:set_int("left", math.floor(x0))
485 padding_effect:set_int("top", math.floor(y0))
487 -- Correct _that_ discrepancy by subpixel offset in the resampling.
488 resample_effect:set_float("left", srcx0 * input_width - x_subpixel_offset / zoom_x)
489 resample_effect:set_float("top", srcy0 * input_height - y_subpixel_offset / zoom_y)
491 -- Finally, adjust the border so it is exactly where we want it.
492 padding_effect:set_float("border_offset_left", x_subpixel_offset)
493 padding_effect:set_float("border_offset_right", x1 - (math.floor(x0) + width))
494 padding_effect:set_float("border_offset_top", y_subpixel_offset)
495 padding_effect:set_float("border_offset_bottom", y1 - (math.floor(y0) + height))
497 -- Lower-quality simple resizing.
498 local width = round(x1 - x0)
499 local height = round(y1 - y0)
500 resize_effect:set_int("width", width)
501 resize_effect:set_int("height", height)
503 -- Padding must also be to a whole-pixel offset.
504 padding_effect:set_int("left", math.floor(x0))
505 padding_effect:set_int("top", math.floor(y0))
509 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
511 return math.floor(x + 0.5)
514 function lerp(a, b, t)
515 return a + (b - a) * t
518 function prepare_sbs_chain(chain, t, screen_width, screen_height)
519 chain.input0.input:connect_signal(0)
520 chain.input1.input:connect_signal(1)
521 set_neutral_color(chain.input0.white_balance_effect, input0_neutral_color)
522 set_neutral_color(chain.input1.white_balance_effect, input1_neutral_color)
524 -- First input is positioned (16,48) from top-left.
525 local width0 = round(848 * screen_width/1280.0)
526 local height0 = round(width0 * 9.0 / 16.0)
528 local top0 = 48 * screen_height/720.0
529 local left0 = 16 * screen_width/1280.0
530 local bottom0 = top0 + height0
531 local right0 = left0 + width0
533 -- Second input is positioned (16,48) from the bottom-right.
534 local width1 = round(384 * screen_width/1280.0)
535 local height1 = round(216 * screen_height/720.0)
537 local bottom1 = screen_height - 48 * screen_height/720.0
538 local right1 = screen_width - 16 * screen_width/1280.0
539 local top1 = bottom1 - height1
540 local left1 = right1 - width1
542 -- Interpolate between the fullscreen and side-by-side views.
543 local scale0, tx0, tx0
544 if zoom_poi == INPUT0_SIGNAL_NUM then
545 local new_left0 = lerp(left0, 0, t)
546 local new_right0 = lerp(right0, screen_width, t)
547 local new_top0 = lerp(top0, 0, t)
548 local new_bottom0 = lerp(bottom0, screen_height, t)
550 scale0 = (new_right0 - new_left0) / width0 -- Same vertically and horizonally.
551 tx0 = new_left0 - left0 * scale0
552 ty0 = new_top0 - top0 * scale0
554 local new_left1 = lerp(left1, 0, t)
555 local new_right1 = lerp(right1, screen_width, t)
556 local new_top1 = lerp(top1, 0, t)
557 local new_bottom1 = lerp(bottom1, screen_height, t)
559 scale0 = (new_right1 - new_left1) / width1 -- Same vertically and horizonally.
560 tx0 = new_left1 - left1 * scale0
561 ty0 = new_top1 - top1 * scale0
564 top0 = top0 * scale0 + ty0
565 bottom0 = bottom0 * scale0 + ty0
566 left0 = left0 * scale0 + tx0
567 right0 = right0 * scale0 + tx0
569 top1 = top1 * scale0 + ty0
570 bottom1 = bottom1 * scale0 + ty0
571 left1 = left1 * scale0 + tx0
572 right1 = right1 * scale0 + tx0
573 place_rectangle(chain.input0.resample_effect, chain.input0.resize_effect, chain.input0.padding_effect, left0, top0, right0, bottom0, screen_width, screen_height, 1280, 720)
574 place_rectangle(chain.input1.resample_effect, chain.input1.resize_effect, chain.input1.padding_effect, left1, top1, right1, bottom1, screen_width, screen_height, 1280, 720)
577 function set_neutral_color(effect, color)
578 effect:set_vec3("neutral_color", color[1], color[2], color[3])
581 function set_neutral_color_from_signal(effect, signal)
582 if signal == INPUT0_SIGNAL_NUM then
583 set_neutral_color(effect, input0_neutral_color)
585 set_neutral_color(effect, input1_neutral_color)
589 function calc_fade_progress(t, transition_start, transition_end)
590 local tt = (t - transition_start) / (transition_end - transition_start)
597 -- Make the fade look maybe a tad more natural, by pumping it
598 -- through a sigmoid function.
600 tt = 1.0 / (1.0 + math.exp(-tt))