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 number that signifies that we are fading from one input
34 local FADE_SIGNAL_NUM = 4
36 function make_sbs_input(chain, signal, deint, hq)
37 local input = chain:add_live_input(not deint, deint) -- Override bounce only if not deinterlacing.
38 input:connect_signal(signal)
39 local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
41 local resample_effect = nil
42 local resize_effect = nil
44 resample_effect = chain:add_effect(ResampleEffect.new())
46 resize_effect = chain:add_effect(ResizeEffect.new())
49 local padding_effect = chain:add_effect(IntegralPaddingEffect.new())
53 wb_effect = wb_effect,
54 resample_effect = resample_effect,
55 resize_effect = resize_effect,
56 padding_effect = padding_effect
60 -- The main live chain.
61 function make_sbs_chain(input0_deint, input1_deint, hq)
62 local chain = EffectChain.new(16, 9)
64 local input0 = make_sbs_input(chain, INPUT0_SIGNAL_NUM, input0_deint, hq)
65 local input1 = make_sbs_input(chain, INPUT1_SIGNAL_NUM, input1_deint, hq)
67 input0.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 1.0)
68 input1.padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 0.0)
70 chain:add_effect(OverlayEffect.new(), input0.padding_effect, input1.padding_effect)
80 -- Make all possible combinations of side-by-side chains.
82 for input0_type, input0_deint in pairs({live = false, livedeint = true}) do
83 sbs_chains[input0_type] = {}
84 for input1_type, input1_deint in pairs({live = false, livedeint = true}) do
85 sbs_chains[input0_type][input1_type] = {}
86 for _, hq in pairs({true, false}) do
87 sbs_chains[input0_type][input1_type][hq] =
88 make_sbs_chain(input0_deint, input1_deint, hq)
93 function make_fade_input(chain, signal, live, deint)
94 local input, wb_effect, last
96 input = chain:add_live_input(false, deint)
97 wb_effect = chain:add_effect(WhiteBalanceEffect.new())
98 input:connect_signal(signal)
101 input = chain:add_effect(ImageInput.new("bg.jpeg"))
107 wb_effect = wb_effect,
112 -- A chain to fade between two inputs, of which either can be a picture
113 -- or a live input. In practice only used live, but we still support the
115 function make_fade_chain(input0_live, input0_deint, input1_live, input1_deint, hq)
116 local chain = EffectChain.new(16, 9)
118 local input0 = make_fade_input(chain, INPUT0_SIGNAL_NUM, input0_live, input0_deint)
119 local input1 = make_fade_input(chain, INPUT1_SIGNAL_NUM, input1_live, input1_deint)
121 local mix_effect = chain:add_effect(MixEffect.new(), input0.last, input1.last)
128 mix_effect = mix_effect
132 -- Chains to fade between two inputs, in various configurations.
133 local fade_chains = {}
134 for input0_type, input0_live in pairs({static = false, live = true, livedeint = true}) do
135 local input0_deint = (input0_live == "livedeint")
136 fade_chains[input0_type] = {}
137 for input1_type, input1_live in pairs({static = false, live = true, livedeint = true}) do
138 local input1_deint = (input1_live == "livedeint")
139 fade_chains[input0_type][input1_type] = {}
140 for _, hq in pairs({true, false}) do
141 fade_chains[input0_type][input1_type][hq] =
142 make_fade_chain(input0_live, input0_deint, input1_live, input1_deint, hq)
147 -- A chain to show a single input on screen.
148 function make_simple_chain(input_deint, hq)
149 local chain = EffectChain.new(16, 9)
151 local input = chain:add_live_input(false, input_deint)
152 input:connect_signal(0) -- First input card. Can be changed whenever you want.
153 local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
159 wb_effect = wb_effect
163 -- Make all possible combinations of single-input chains.
164 local simple_chains = {}
165 for input_type, input_deint in pairs({live = false, livedeint = true}) do
166 simple_chains[input_type] = {}
167 for _, hq in pairs({true, false}) do
168 simple_chains[input_type][hq] = make_simple_chain(input_deint, hq)
172 -- A chain to show a single static picture on screen (HQ version).
173 local static_chain_hq = EffectChain.new(16, 9)
174 local static_chain_hq_input = static_chain_hq:add_effect(ImageInput.new("bg.jpeg"))
175 static_chain_hq:finalize(true)
177 -- A chain to show a single static picture on screen (LQ version).
178 local static_chain_lq = EffectChain.new(16, 9)
179 local static_chain_lq_input = static_chain_lq:add_effect(ImageInput.new("bg.jpeg"))
180 static_chain_lq:finalize(false)
182 -- Used for indexing into the tables of chains.
183 function get_input_type(signals, signal_num)
184 if signal_num == STATIC_SIGNAL_NUM then
186 elseif signals:get_interlaced(signal_num) then
193 -- Returns the number of outputs in addition to the live (0) and preview (1).
194 -- Called only once, at the start of the program.
195 function num_channels()
199 -- Returns the name for each additional channel (starting from 2).
200 -- Called only once for each channel, at the start of the program.
201 function channel_name(channel)
204 elseif channel == 3 then
206 elseif channel == 4 then
207 return "Side-by-side"
208 elseif channel == 5 then
209 return "Static picture"
213 -- Returns if a given channel supports setting white balance (starting from 2).
214 -- Called only once for each channel, at the start of the program.
215 function supports_set_wb(channel)
216 return channel == 2 or channel == 3
219 -- Gets called with a new gray point when the white balance is changing.
220 -- The color is in linear light (not sRGB gamma).
221 function set_wb(channel, red, green, blue)
223 input0_neutral_color = { red, green, blue }
224 elseif channel == 3 then
225 input1_neutral_color = { red, green, blue }
229 function finish_transitions(t)
230 -- If live is SBS but de-facto single, make it so.
231 if live_signal_num == SBS_SIGNAL_NUM and t >= transition_end and zoom_dst == 1.0 then
232 live_signal_num = zoom_poi
235 -- If live is fade but de-facto single, make it so.
236 if live_signal_num == FADE_SIGNAL_NUM and t >= transition_end then
237 live_signal_num = fade_dst_signal
241 -- Called every frame.
242 function get_transitions(t)
243 finish_transitions(t)
245 if live_signal_num == preview_signal_num then
249 if live_signal_num == SBS_SIGNAL_NUM and t >= transition_start and t <= transition_end then
254 if (live_signal_num == INPUT0_SIGNAL_NUM or
255 live_signal_num == INPUT1_SIGNAL_NUM or
256 live_signal_num == STATIC_SIGNAL_NUM) and
257 (preview_signal_num == INPUT0_SIGNAL_NUM or
258 preview_signal_num == INPUT1_SIGNAL_NUM or
259 preview_signal_num == STATIC_SIGNAL_NUM) then
260 return {"Cut", "", "Fade"}
264 if live_signal_num == SBS_SIGNAL_NUM and
265 (preview_signal_num == INPUT0_SIGNAL_NUM or preview_signal_num == INPUT1_SIGNAL_NUM) then
266 return {"Cut", "Zoom in"}
267 elseif (live_signal_num == INPUT0_SIGNAL_NUM or live_signal_num == INPUT1_SIGNAL_NUM) and
268 preview_signal_num == SBS_SIGNAL_NUM then
269 return {"Cut", "Zoom out"}
275 function transition_clicked(num, t)
278 if live_signal_num == FADE_SIGNAL_NUM then
279 -- Ongoing fade; finish it immediately.
280 finish_transitions(transition_end)
283 local temp = live_signal_num
284 live_signal_num = preview_signal_num
285 preview_signal_num = temp
287 if live_signal_num == SBS_SIGNAL_NUM then
288 -- Just cut to SBS, we need to reset any zooms.
291 transition_start = -2.0
292 transition_end = -1.0
297 finish_transitions(t)
299 if live_signal_num == preview_signal_num then
304 if (live_signal_num == INPUT0_SIGNAL_NUM and preview_signal_num == INPUT1_SIGNAL_NUM) or
305 (live_signal_num == INPUT1_SIGNAL_NUM and preview_signal_num == INPUT0_SIGNAL_NUM) then
306 -- We can't zoom between these. Just make a cut.
307 io.write("Cutting from " .. live_signal_num .. " to " .. live_signal_num .. "\n")
308 local temp = live_signal_num
309 live_signal_num = preview_signal_num
310 preview_signal_num = temp
314 if live_signal_num == SBS_SIGNAL_NUM and
315 (preview_signal_num == INPUT0_SIGNAL_NUM or preview_signal_num == INPUT1_SIGNAL_NUM) then
316 -- Zoom in from SBS to single.
318 transition_end = t + 1.0
321 zoom_poi = preview_signal_num
322 preview_signal_num = SBS_SIGNAL_NUM
323 elseif (live_signal_num == INPUT0_SIGNAL_NUM or live_signal_num == INPUT1_SIGNAL_NUM) and
324 preview_signal_num == SBS_SIGNAL_NUM then
325 -- Zoom out from single to SBS.
327 transition_end = t + 1.0
330 preview_signal_num = live_signal_num
331 zoom_poi = live_signal_num
332 live_signal_num = SBS_SIGNAL_NUM
335 finish_transitions(t)
338 if (live_signal_num ~= preview_signal_num) and
339 (live_signal_num == INPUT0_SIGNAL_NUM or
340 live_signal_num == INPUT1_SIGNAL_NUM or
341 live_signal_num == STATIC_SIGNAL_NUM) and
342 (preview_signal_num == INPUT0_SIGNAL_NUM or
343 preview_signal_num == INPUT1_SIGNAL_NUM or
344 preview_signal_num == STATIC_SIGNAL_NUM) then
346 transition_end = t + 1.0
347 fade_src_signal = live_signal_num
348 fade_dst_signal = preview_signal_num
349 preview_signal_num = live_signal_num
350 live_signal_num = FADE_SIGNAL_NUM
352 -- Fades involving SBS are ignored (we have no chain for it).
357 function channel_clicked(num)
358 preview_signal_num = num
361 -- Called every frame. Get the chain for displaying at input <num>,
362 -- where 0 is live, 1 is preview, 2 is the first channel to display
363 -- in the bottom bar, and so on up to num_channels()+1. t is the
364 -- current time in seconds. width and height are the dimensions of
365 -- the output, although you can ignore them if you don't need them
366 -- (they're useful if you want to e.g. know what to resample by).
368 -- <signals> is basically an exposed InputState, which you can use to
369 -- query for information about the signals at the point of the current
370 -- frame. In particular, you can call get_width() and get_height()
371 -- for any signal number, and use that to e.g. assist in chain selection.
373 -- You should return two objects; the chain itself, and then a
374 -- function (taking no parameters) that is run just before rendering.
375 -- The function needs to call connect_signal on any inputs, so that
376 -- it gets updated video data for the given frame. (You are allowed
377 -- to switch which input your input is getting from between frames,
378 -- but not calling connect_signal results in undefined behavior.)
379 -- If you want to change any parameters in the chain, this is also
382 -- NOTE: The chain returned must be finalized with the Y'CbCr flag
383 -- if and only if num==0.
384 function get_chain(num, t, width, height, signals)
385 if num == 0 then -- Live.
386 if live_signal_num == INPUT0_SIGNAL_NUM or live_signal_num == INPUT1_SIGNAL_NUM then -- Plain input.
387 local input_type = get_input_type(signals, live_signal_num)
388 local chain = simple_chains[input_type][true]
390 chain.input:connect_signal(live_signal_num)
391 set_neutral_color_from_signal(chain.wb_effect, live_signal_num)
393 return chain.chain, prepare
394 elseif live_signal_num == STATIC_SIGNAL_NUM then -- Static picture.
397 return static_chain_hq, prepare
398 elseif live_signal_num == FADE_SIGNAL_NUM then -- Fade.
399 local input0_type = get_input_type(signals, fade_src_signal)
400 local input1_type = get_input_type(signals, fade_dst_signal)
401 local chain = fade_chains[input0_type][input1_type][true]
403 if input0_type == "live" then
404 chain.input0.input:connect_signal(fade_src_signal)
405 set_neutral_color_from_signal(chain.input0.wb_effect, fade_src_signal)
407 if input1_type == "live" then
408 chain.input1.input:connect_signal(fade_dst_signal)
409 set_neutral_color_from_signal(chain.input1.wb_effect, fade_dst_signal)
411 local tt = calc_fade_progress(t, transition_start, transition_end)
413 chain.mix_effect:set_float("strength_first", 1.0 - tt)
414 chain.mix_effect:set_float("strength_second", tt)
416 return chain.chain, prepare
419 -- SBS code (live_signal_num == SBS_SIGNAL_NUM).
420 local input0_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
421 local input1_type = get_input_type(signals, INPUT1_SIGNAL_NUM)
422 if t > transition_end and zoom_dst == 1.0 then
423 -- Special case: Show only the single image on screen.
424 local chain = simple_chains[input0_type][true]
426 chain.input:connect_signal(INPUT0_SIGNAL_NUM)
427 set_neutral_color(chain.wb_effect, input0_neutral_color)
429 return chain.chain, prepare
431 local chain = sbs_chains[input0_type][input1_type][true]
433 if t < transition_start then
434 prepare_sbs_chain(chain, zoom_src, width, height)
435 elseif t > transition_end then
436 prepare_sbs_chain(chain, zoom_dst, width, height)
438 local tt = (t - transition_start) / (transition_end - transition_start)
440 tt = math.sin(tt * 3.14159265358 * 0.5)
441 prepare_sbs_chain(chain, zoom_src + (zoom_dst - zoom_src) * tt, width, height)
444 return chain.chain, prepare
446 if num == 1 then -- Preview.
447 num = preview_signal_num + 2
450 -- Individual preview inputs.
451 if num == INPUT0_SIGNAL_NUM + 2 then
452 local input_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
453 local chain = simple_chains[input_type][false]
455 chain.input:connect_signal(INPUT0_SIGNAL_NUM)
456 set_neutral_color(chain.wb_effect, input0_neutral_color)
458 return chain.chain, prepare
460 if num == INPUT1_SIGNAL_NUM + 2 then
461 local input_type = get_input_type(signals, INPUT1_SIGNAL_NUM)
462 local chain = simple_chains[input_type][false]
464 chain.input:connect_signal(INPUT1_SIGNAL_NUM)
465 set_neutral_color(chain.wb_effect, input1_neutral_color)
467 return chain.chain, prepare
469 if num == SBS_SIGNAL_NUM + 2 then
470 local input0_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
471 local input1_type = get_input_type(signals, INPUT1_SIGNAL_NUM)
472 local chain = sbs_chains[input0_type][input1_type][false]
474 prepare_sbs_chain(chain, 0.0, width, height)
476 return chain.chain, prepare
478 if num == STATIC_SIGNAL_NUM + 2 then
481 return static_chain_lq, prepare
485 function place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height)
492 if x0 > screen_width or x1 < 0.0 or y0 > screen_height or y1 < 0.0 then
493 resample_effect:set_int("width", 1)
494 resample_effect:set_int("height", 1)
495 resample_effect:set_float("zoom_x", screen_width)
496 resample_effect:set_float("zoom_y", screen_height)
497 padding_effect:set_int("left", screen_width + 100)
498 padding_effect:set_int("top", screen_height + 100)
504 srcx0 = -x0 / (x1 - x0)
508 srcy0 = -y0 / (y1 - y0)
511 if x1 > screen_width then
512 srcx1 = (screen_width - x0) / (x1 - x0)
515 if y1 > screen_height then
516 srcy1 = (screen_height - y0) / (y1 - y0)
520 if resample_effect ~= nil then
521 -- High-quality resampling.
522 local x_subpixel_offset = x0 - math.floor(x0)
523 local y_subpixel_offset = y0 - math.floor(y0)
525 -- Resampling must be to an integral number of pixels. Round up,
526 -- and then add an extra pixel so we have some leeway for the border.
527 local width = math.ceil(x1 - x0) + 1
528 local height = math.ceil(y1 - y0) + 1
529 resample_effect:set_int("width", width)
530 resample_effect:set_int("height", height)
532 -- Correct the discrepancy with zoom. (This will leave a small
533 -- excess edge of pixels and subpixels, which we'll correct for soon.)
534 local zoom_x = (x1 - x0) / (width * (srcx1 - srcx0))
535 local zoom_y = (y1 - y0) / (height * (srcy1 - srcy0))
536 resample_effect:set_float("zoom_x", zoom_x)
537 resample_effect:set_float("zoom_y", zoom_y)
538 resample_effect:set_float("zoom_center_x", 0.0)
539 resample_effect:set_float("zoom_center_y", 0.0)
541 -- Padding must also be to a whole-pixel offset.
542 padding_effect:set_int("left", math.floor(x0))
543 padding_effect:set_int("top", math.floor(y0))
545 -- Correct _that_ discrepancy by subpixel offset in the resampling.
546 resample_effect:set_float("left", srcx0 * input_width - x_subpixel_offset / zoom_x)
547 resample_effect:set_float("top", srcy0 * input_height - y_subpixel_offset / zoom_y)
549 -- Finally, adjust the border so it is exactly where we want it.
550 padding_effect:set_float("border_offset_left", x_subpixel_offset)
551 padding_effect:set_float("border_offset_right", x1 - (math.floor(x0) + width))
552 padding_effect:set_float("border_offset_top", y_subpixel_offset)
553 padding_effect:set_float("border_offset_bottom", y1 - (math.floor(y0) + height))
555 -- Lower-quality simple resizing.
556 local width = round(x1 - x0)
557 local height = round(y1 - y0)
558 resize_effect:set_int("width", width)
559 resize_effect:set_int("height", height)
561 -- Padding must also be to a whole-pixel offset.
562 padding_effect:set_int("left", math.floor(x0))
563 padding_effect:set_int("top", math.floor(y0))
567 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
569 return math.floor(x + 0.5)
572 function lerp(a, b, t)
573 return a + (b - a) * t
576 function prepare_sbs_chain(chain, t, screen_width, screen_height)
577 chain.input0.input:connect_signal(0)
578 chain.input1.input:connect_signal(1)
579 set_neutral_color(chain.input0.wb_effect, input0_neutral_color)
580 set_neutral_color(chain.input1.wb_effect, input1_neutral_color)
582 -- First input is positioned (16,48) from top-left.
583 local width0 = round(848 * screen_width/1280.0)
584 local height0 = round(width0 * 9.0 / 16.0)
586 local top0 = 48 * screen_height/720.0
587 local left0 = 16 * screen_width/1280.0
588 local bottom0 = top0 + height0
589 local right0 = left0 + width0
591 -- Second input is positioned (16,48) from the bottom-right.
592 local width1 = round(384 * screen_width/1280.0)
593 local height1 = round(216 * screen_height/720.0)
595 local bottom1 = screen_height - 48 * screen_height/720.0
596 local right1 = screen_width - 16 * screen_width/1280.0
597 local top1 = bottom1 - height1
598 local left1 = right1 - width1
600 -- Interpolate between the fullscreen and side-by-side views.
601 local scale0, tx0, tx0
602 if zoom_poi == INPUT0_SIGNAL_NUM then
603 local new_left0 = lerp(left0, 0, t)
604 local new_right0 = lerp(right0, screen_width, t)
605 local new_top0 = lerp(top0, 0, t)
606 local new_bottom0 = lerp(bottom0, screen_height, t)
608 scale0 = (new_right0 - new_left0) / width0 -- Same vertically and horizonally.
609 tx0 = new_left0 - left0 * scale0
610 ty0 = new_top0 - top0 * scale0
612 local new_left1 = lerp(left1, 0, t)
613 local new_right1 = lerp(right1, screen_width, t)
614 local new_top1 = lerp(top1, 0, t)
615 local new_bottom1 = lerp(bottom1, screen_height, t)
617 scale0 = (new_right1 - new_left1) / width1 -- Same vertically and horizonally.
618 tx0 = new_left1 - left1 * scale0
619 ty0 = new_top1 - top1 * scale0
622 top0 = top0 * scale0 + ty0
623 bottom0 = bottom0 * scale0 + ty0
624 left0 = left0 * scale0 + tx0
625 right0 = right0 * scale0 + tx0
627 top1 = top1 * scale0 + ty0
628 bottom1 = bottom1 * scale0 + ty0
629 left1 = left1 * scale0 + tx0
630 right1 = right1 * scale0 + tx0
631 place_rectangle(chain.input0.resample_effect, chain.input0.resize_effect, chain.input0.padding_effect, left0, top0, right0, bottom0, screen_width, screen_height, 1280, 720)
632 place_rectangle(chain.input1.resample_effect, chain.input1.resize_effect, chain.input1.padding_effect, left1, top1, right1, bottom1, screen_width, screen_height, 1280, 720)
635 function set_neutral_color(effect, color)
636 effect:set_vec3("neutral_color", color[1], color[2], color[3])
639 function set_neutral_color_from_signal(effect, signal)
640 if signal == INPUT0_SIGNAL_NUM then
641 set_neutral_color(effect, input0_neutral_color)
643 set_neutral_color(effect, input1_neutral_color)
647 function calc_fade_progress(t, transition_start, transition_end)
648 local tt = (t - transition_start) / (transition_end - transition_start)
655 -- Make the fade look maybe a tad more natural, by pumping it
656 -- through a sigmoid function.
658 tt = 1.0 / (1.0 + math.exp(-tt))