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 -- The main live chain.
37 function make_sbs_chain(input0_deint, input1_deint, hq)
38 local chain = EffectChain.new(16, 9)
39 local input0 = chain:add_live_input(not input0_deint, input0_deint) -- Override bounce only if not deinterlacing.
40 input0:connect_signal(0)
41 local input0_wb_effect = chain:add_effect(WhiteBalanceEffect.new())
42 local input1 = chain:add_live_input(not input1_deint, input1_deint)
43 input1:connect_signal(1)
44 local input1_wb_effect = chain:add_effect(WhiteBalanceEffect.new())
46 local resample_effect = nil
47 local resize_effect = nil
49 resample_effect = chain:add_effect(ResampleEffect.new(), input0_wb_effect)
51 resize_effect = chain:add_effect(ResizeEffect.new(), input0_wb_effect)
54 local padding_effect = chain:add_effect(IntegralPaddingEffect.new())
55 padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 1.0)
57 local resample2_effect = nil
58 local resize2_effect = nil
60 resample2_effect = chain:add_effect(ResampleEffect.new(), input1_wb_effect)
62 resize2_effect = chain:add_effect(ResizeEffect.new(), input1_wb_effect)
64 local padding2_effect = chain:add_effect(IntegralPaddingEffect.new())
66 chain:add_effect(OverlayEffect.new(), padding_effect, padding2_effect)
73 wb_effect = input0_wb_effect,
74 resample_effect = resample_effect,
75 resize_effect = resize_effect,
76 padding_effect = padding_effect
80 wb_effect = input1_wb_effect,
81 resample_effect = resample2_effect,
82 resize_effect = resize2_effect,
83 padding_effect = padding2_effect
88 -- Make all possible combinations of side-by-side chains.
90 for input0_type, input0_deint in pairs({live = false, livedeint = true}) do
91 sbs_chains[input0_type] = {}
92 for input1_type, input1_deint in pairs({live = false, livedeint = true}) do
93 sbs_chains[input0_type][input1_type] = {}
94 for _, hq in pairs({true, false}) do
95 sbs_chains[input0_type][input1_type][hq] =
96 make_sbs_chain(input0_deint, input1_deint, hq)
101 -- A chain to fade between two inputs, of which either can be a picture
102 -- or a live input. In practice only used live, but we still support the
104 function make_fade_chain(input0_live, input0_deint, input1_live, input1_deint, hq)
105 local chain = EffectChain.new(16, 9)
107 local input0, wb0_effect, input0_last, input1, wb1_effect, input1_last
110 input0 = chain:add_live_input(false, input0_deint)
111 wb0_effect = chain:add_effect(WhiteBalanceEffect.new())
112 input0:connect_signal(0)
113 input0_last = wb0_effect
115 input0 = chain:add_effect(ImageInput.new("bg.jpeg"))
120 input1 = chain:add_live_input(false, input1_deint)
121 wb1_effect = chain:add_effect(WhiteBalanceEffect.new())
122 input1:connect_signal(1)
123 input1_last = wb1_effect
125 input1 = chain:add_effect(ImageInput.new("bg.jpeg"))
129 local mix_effect = chain:add_effect(MixEffect.new(), input0_last, input1_last)
136 wb_effect = wb0_effect
140 wb_effect = wb1_effect
142 mix_effect = mix_effect
146 -- Chains to fade between two inputs, in various configurations.
147 local fade_chains = {}
148 for input0_type, input0_live in pairs({static = false, live = true, livedeint = true}) do
149 local input0_deint = (input0_live == "livedeint")
150 fade_chains[input0_type] = {}
151 for input1_type, input1_live in pairs({static = false, live = true, livedeint = true}) do
152 local input1_deint = (input1_live == "livedeint")
153 fade_chains[input0_type][input1_type] = {}
154 for _, hq in pairs({true, false}) do
155 fade_chains[input0_type][input1_type][hq] =
156 make_fade_chain(input0_live, input0_deint, input1_live, input1_deint, hq)
161 -- A chain to show a single input on screen.
162 function make_simple_chain(input_deint, hq)
163 local chain = EffectChain.new(16, 9)
165 local input = chain:add_live_input(false, input_deint)
166 input:connect_signal(0) -- First input card. Can be changed whenever you want.
167 local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
173 wb_effect = wb_effect
177 -- Make all possible combinations of single-input chains.
178 local simple_chains = {}
179 for input_type, input_deint in pairs({live = false, livedeint = true}) do
180 simple_chains[input_type] = {}
181 for _, hq in pairs({true, false}) do
182 simple_chains[input_type][hq] = make_simple_chain(input_deint, hq)
186 -- A chain to show a single static picture on screen (HQ version).
187 local static_chain_hq = EffectChain.new(16, 9)
188 local static_chain_hq_input = static_chain_hq:add_effect(ImageInput.new("bg.jpeg"))
189 static_chain_hq:finalize(true)
191 -- A chain to show a single static picture on screen (LQ version).
192 local static_chain_lq = EffectChain.new(16, 9)
193 local static_chain_lq_input = static_chain_lq:add_effect(ImageInput.new("bg.jpeg"))
194 static_chain_lq:finalize(false)
196 -- Used for indexing into the tables of chains.
197 function get_input_type(signals, signal_num)
198 if signal_num == STATIC_SIGNAL_NUM then
200 elseif signals:get_interlaced(signal_num) then
207 -- Returns the number of outputs in addition to the live (0) and preview (1).
208 -- Called only once, at the start of the program.
209 function num_channels()
213 -- Returns the name for each additional channel (starting from 2).
214 -- Called only once for each channel, at the start of the program.
215 function channel_name(channel)
218 elseif channel == 3 then
220 elseif channel == 4 then
221 return "Side-by-side"
222 elseif channel == 5 then
223 return "Static picture"
227 -- Returns if a given channel supports setting white balance (starting from 2).
228 -- Called only once for each channel, at the start of the program.
229 function supports_set_wb(channel)
230 return channel == 2 or channel == 3
233 -- Gets called with a new gray point when the white balance is changing.
234 -- The color is in linear light (not sRGB gamma).
235 function set_wb(channel, red, green, blue)
237 input0_neutral_color = { red, green, blue }
238 elseif channel == 3 then
239 input1_neutral_color = { red, green, blue }
243 function finish_transitions(t)
244 -- If live is SBS but de-facto single, make it so.
245 if live_signal_num == SBS_SIGNAL_NUM and t >= transition_end and zoom_dst == 1.0 then
246 live_signal_num = zoom_poi
249 -- If live is fade but de-facto single, make it so.
250 if live_signal_num == FADE_SIGNAL_NUM and t >= transition_end then
251 live_signal_num = fade_dst_signal
255 -- Called every frame.
256 function get_transitions(t)
257 finish_transitions(t)
259 if live_signal_num == preview_signal_num then
263 if live_signal_num == SBS_SIGNAL_NUM and t >= transition_start and t <= transition_end then
268 if (live_signal_num == INPUT0_SIGNAL_NUM or
269 live_signal_num == INPUT1_SIGNAL_NUM or
270 live_signal_num == STATIC_SIGNAL_NUM) and
271 (preview_signal_num == INPUT0_SIGNAL_NUM or
272 preview_signal_num == INPUT1_SIGNAL_NUM or
273 preview_signal_num == STATIC_SIGNAL_NUM) then
274 return {"Cut", "", "Fade"}
278 if live_signal_num == SBS_SIGNAL_NUM and
279 (preview_signal_num == INPUT0_SIGNAL_NUM or preview_signal_num == INPUT1_SIGNAL_NUM) then
280 return {"Cut", "Zoom in"}
281 elseif (live_signal_num == INPUT0_SIGNAL_NUM or live_signal_num == INPUT1_SIGNAL_NUM) and
282 preview_signal_num == SBS_SIGNAL_NUM then
283 return {"Cut", "Zoom out"}
289 function transition_clicked(num, t)
292 if live_signal_num == FADE_SIGNAL_NUM then
293 -- Ongoing fade; finish it immediately.
294 finish_transitions(transition_end)
297 local temp = live_signal_num
298 live_signal_num = preview_signal_num
299 preview_signal_num = temp
301 if live_signal_num == SBS_SIGNAL_NUM then
302 -- Just cut to SBS, we need to reset any zooms.
305 transition_start = -2.0
306 transition_end = -1.0
311 finish_transitions(t)
313 if live_signal_num == preview_signal_num then
318 if (live_signal_num == INPUT0_SIGNAL_NUM and preview_signal_num == INPUT1_SIGNAL_NUM) or
319 (live_signal_num == INPUT1_SIGNAL_NUM and preview_signal_num == INPUT0_SIGNAL_NUM) then
320 -- We can't zoom between these. Just make a cut.
321 io.write("Cutting from " .. live_signal_num .. " to " .. live_signal_num .. "\n")
322 local temp = live_signal_num
323 live_signal_num = preview_signal_num
324 preview_signal_num = temp
328 if live_signal_num == SBS_SIGNAL_NUM and
329 (preview_signal_num == INPUT0_SIGNAL_NUM or preview_signal_num == INPUT1_SIGNAL_NUM) then
330 -- Zoom in from SBS to single.
332 transition_end = t + 1.0
335 zoom_poi = preview_signal_num
336 preview_signal_num = SBS_SIGNAL_NUM
337 elseif (live_signal_num == INPUT0_SIGNAL_NUM or live_signal_num == INPUT1_SIGNAL_NUM) and
338 preview_signal_num == SBS_SIGNAL_NUM then
339 -- Zoom out from single to SBS.
341 transition_end = t + 1.0
344 preview_signal_num = live_signal_num
345 zoom_poi = live_signal_num
346 live_signal_num = SBS_SIGNAL_NUM
349 finish_transitions(t)
352 if (live_signal_num ~= preview_signal_num) and
353 (live_signal_num == INPUT0_SIGNAL_NUM or
354 live_signal_num == INPUT1_SIGNAL_NUM or
355 live_signal_num == STATIC_SIGNAL_NUM) and
356 (preview_signal_num == INPUT0_SIGNAL_NUM or
357 preview_signal_num == INPUT1_SIGNAL_NUM or
358 preview_signal_num == STATIC_SIGNAL_NUM) then
360 transition_end = t + 1.0
361 fade_src_signal = live_signal_num
362 fade_dst_signal = preview_signal_num
363 preview_signal_num = live_signal_num
364 live_signal_num = FADE_SIGNAL_NUM
366 -- Fades involving SBS are ignored (we have no chain for it).
371 function channel_clicked(num)
372 preview_signal_num = num
375 -- Called every frame. Get the chain for displaying at input <num>,
376 -- where 0 is live, 1 is preview, 2 is the first channel to display
377 -- in the bottom bar, and so on up to num_channels()+1. t is the
378 -- current time in seconds. width and height are the dimensions of
379 -- the output, although you can ignore them if you don't need them
380 -- (they're useful if you want to e.g. know what to resample by).
382 -- <signals> is basically an exposed InputState, which you can use to
383 -- query for information about the signals at the point of the current
384 -- frame. In particular, you can call get_width() and get_height()
385 -- for any signal number, and use that to e.g. assist in chain selection.
387 -- You should return two objects; the chain itself, and then a
388 -- function (taking no parameters) that is run just before rendering.
389 -- The function needs to call connect_signal on any inputs, so that
390 -- it gets updated video data for the given frame. (You are allowed
391 -- to switch which input your input is getting from between frames,
392 -- but not calling connect_signal results in undefined behavior.)
393 -- If you want to change any parameters in the chain, this is also
396 -- NOTE: The chain returned must be finalized with the Y'CbCr flag
397 -- if and only if num==0.
398 function get_chain(num, t, width, height, signals)
399 if num == 0 then -- Live.
400 if live_signal_num == INPUT0_SIGNAL_NUM or live_signal_num == INPUT1_SIGNAL_NUM then -- Plain input.
401 local input_type = get_input_type(signals, live_signal_num)
402 local chain = simple_chains[input_type][true]
404 chain.input:connect_signal(live_signal_num)
405 set_neutral_color_from_signal(chain.wb_effect, live_signal_num)
407 return chain.chain, prepare
408 elseif live_signal_num == STATIC_SIGNAL_NUM then -- Static picture.
411 return static_chain_hq, prepare
412 elseif live_signal_num == FADE_SIGNAL_NUM then -- Fade.
413 local input0_type = get_input_type(signals, fade_src_signal)
414 local input1_type = get_input_type(signals, fade_dst_signal)
415 local chain = fade_chains[input0_type][input1_type][true]
417 if input0_type == "live" then
418 chain.input0.input:connect_signal(fade_src_signal)
419 set_neutral_color_from_signal(chain.input0.wb_effect, fade_src_signal)
421 if input1_type == "live" then
422 chain.input1.input:connect_signal(fade_dst_signal)
423 set_neutral_color_from_signal(chain.input1.wb_effect, fade_dst_signal)
425 local tt = calc_fade_progress(t, transition_start, transition_end)
427 chain.mix_effect:set_float("strength_first", 1.0 - tt)
428 chain.mix_effect:set_float("strength_second", tt)
430 return chain.chain, prepare
433 -- SBS code (live_signal_num == SBS_SIGNAL_NUM).
434 local input0_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
435 local input1_type = get_input_type(signals, INPUT1_SIGNAL_NUM)
436 if t > transition_end and zoom_dst == 1.0 then
437 -- Special case: Show only the single image on screen.
438 local chain = simple_chains[input0_type][true]
440 chain.input:connect_signal(INPUT0_SIGNAL_NUM)
441 set_neutral_color(chain.wb_effect, input0_neutral_color)
443 return chain.chain, prepare
445 local chain = sbs_chains[input0_type][input1_type][true]
447 if t < transition_start then
448 prepare_sbs_chain(chain, zoom_src, width, height)
449 elseif t > transition_end then
450 prepare_sbs_chain(chain, zoom_dst, width, height)
452 local tt = (t - transition_start) / (transition_end - transition_start)
454 tt = math.sin(tt * 3.14159265358 * 0.5)
455 prepare_sbs_chain(chain, zoom_src + (zoom_dst - zoom_src) * tt, width, height)
458 return chain.chain, prepare
460 if num == 1 then -- Preview.
461 num = preview_signal_num + 2
464 -- Individual preview inputs.
465 if num == INPUT0_SIGNAL_NUM + 2 then
466 local input_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
467 local chain = simple_chains[input_type][false]
469 chain.input:connect_signal(INPUT0_SIGNAL_NUM)
470 set_neutral_color(chain.wb_effect, input0_neutral_color)
472 return chain.chain, prepare
474 if num == INPUT1_SIGNAL_NUM + 2 then
475 local input_type = get_input_type(signals, INPUT1_SIGNAL_NUM)
476 local chain = simple_chains[input_type][false]
478 chain.input:connect_signal(INPUT1_SIGNAL_NUM)
479 set_neutral_color(chain.wb_effect, input1_neutral_color)
481 return chain.chain, prepare
483 if num == SBS_SIGNAL_NUM + 2 then
484 local input0_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
485 local input1_type = get_input_type(signals, INPUT1_SIGNAL_NUM)
486 local chain = sbs_chains[input0_type][input1_type][false]
488 prepare_sbs_chain(chain, 0.0, width, height)
490 return chain.chain, prepare
492 if num == STATIC_SIGNAL_NUM + 2 then
495 return static_chain_lq, prepare
499 function place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height)
506 if x0 > screen_width or x1 < 0.0 or y0 > screen_height or y1 < 0.0 then
507 resample_effect:set_int("width", 1)
508 resample_effect:set_int("height", 1)
509 resample_effect:set_float("zoom_x", screen_width)
510 resample_effect:set_float("zoom_y", screen_height)
511 padding_effect:set_int("left", screen_width + 100)
512 padding_effect:set_int("top", screen_height + 100)
518 srcx0 = -x0 / (x1 - x0)
522 srcy0 = -y0 / (y1 - y0)
525 if x1 > screen_width then
526 srcx1 = (screen_width - x0) / (x1 - x0)
529 if y1 > screen_height then
530 srcy1 = (screen_height - y0) / (y1 - y0)
534 if resample_effect ~= nil then
535 -- High-quality resampling.
536 local x_subpixel_offset = x0 - math.floor(x0)
537 local y_subpixel_offset = y0 - math.floor(y0)
539 -- Resampling must be to an integral number of pixels. Round up,
540 -- and then add an extra pixel so we have some leeway for the border.
541 local width = math.ceil(x1 - x0) + 1
542 local height = math.ceil(y1 - y0) + 1
543 resample_effect:set_int("width", width)
544 resample_effect:set_int("height", height)
546 -- Correct the discrepancy with zoom. (This will leave a small
547 -- excess edge of pixels and subpixels, which we'll correct for soon.)
548 local zoom_x = (x1 - x0) / (width * (srcx1 - srcx0))
549 local zoom_y = (y1 - y0) / (height * (srcy1 - srcy0))
550 resample_effect:set_float("zoom_x", zoom_x)
551 resample_effect:set_float("zoom_y", zoom_y)
552 resample_effect:set_float("zoom_center_x", 0.0)
553 resample_effect:set_float("zoom_center_y", 0.0)
555 -- Padding must also be to a whole-pixel offset.
556 padding_effect:set_int("left", math.floor(x0))
557 padding_effect:set_int("top", math.floor(y0))
559 -- Correct _that_ discrepancy by subpixel offset in the resampling.
560 resample_effect:set_float("left", srcx0 * input_width - x_subpixel_offset / zoom_x)
561 resample_effect:set_float("top", srcy0 * input_height - y_subpixel_offset / zoom_y)
563 -- Finally, adjust the border so it is exactly where we want it.
564 padding_effect:set_float("border_offset_left", x_subpixel_offset)
565 padding_effect:set_float("border_offset_right", x1 - (math.floor(x0) + width))
566 padding_effect:set_float("border_offset_top", y_subpixel_offset)
567 padding_effect:set_float("border_offset_bottom", y1 - (math.floor(y0) + height))
569 -- Lower-quality simple resizing.
570 local width = round(x1 - x0)
571 local height = round(y1 - y0)
572 resize_effect:set_int("width", width)
573 resize_effect:set_int("height", height)
575 -- Padding must also be to a whole-pixel offset.
576 padding_effect:set_int("left", math.floor(x0))
577 padding_effect:set_int("top", math.floor(y0))
581 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
583 return math.floor(x + 0.5)
586 function lerp(a, b, t)
587 return a + (b - a) * t
590 function prepare_sbs_chain(chain, t, screen_width, screen_height)
591 chain.input0.input:connect_signal(0)
592 chain.input1.input:connect_signal(1)
593 set_neutral_color(chain.input0.wb_effect, input0_neutral_color)
594 set_neutral_color(chain.input1.wb_effect, input1_neutral_color)
596 -- First input is positioned (16,48) from top-left.
597 local width0 = round(848 * screen_width/1280.0)
598 local height0 = round(width0 * 9.0 / 16.0)
600 local top0 = 48 * screen_height/720.0
601 local left0 = 16 * screen_width/1280.0
602 local bottom0 = top0 + height0
603 local right0 = left0 + width0
605 -- Second input is positioned (16,48) from the bottom-right.
606 local width1 = round(384 * screen_width/1280.0)
607 local height1 = round(216 * screen_height/720.0)
609 local bottom1 = screen_height - 48 * screen_height/720.0
610 local right1 = screen_width - 16 * screen_width/1280.0
611 local top1 = bottom1 - height1
612 local left1 = right1 - width1
614 -- Interpolate between the fullscreen and side-by-side views.
615 local scale0, tx0, tx0
616 if zoom_poi == INPUT0_SIGNAL_NUM then
617 local new_left0 = lerp(left0, 0, t)
618 local new_right0 = lerp(right0, screen_width, t)
619 local new_top0 = lerp(top0, 0, t)
620 local new_bottom0 = lerp(bottom0, screen_height, t)
622 scale0 = (new_right0 - new_left0) / width0 -- Same vertically and horizonally.
623 tx0 = new_left0 - left0 * scale0
624 ty0 = new_top0 - top0 * scale0
626 local new_left1 = lerp(left1, 0, t)
627 local new_right1 = lerp(right1, screen_width, t)
628 local new_top1 = lerp(top1, 0, t)
629 local new_bottom1 = lerp(bottom1, screen_height, t)
631 scale0 = (new_right1 - new_left1) / width1 -- Same vertically and horizonally.
632 tx0 = new_left1 - left1 * scale0
633 ty0 = new_top1 - top1 * scale0
636 top0 = top0 * scale0 + ty0
637 bottom0 = bottom0 * scale0 + ty0
638 left0 = left0 * scale0 + tx0
639 right0 = right0 * scale0 + tx0
641 top1 = top1 * scale0 + ty0
642 bottom1 = bottom1 * scale0 + ty0
643 left1 = left1 * scale0 + tx0
644 right1 = right1 * scale0 + tx0
645 place_rectangle(chain.input0.resample_effect, chain.input0.resize_effect, chain.input0.padding_effect, left0, top0, right0, bottom0, screen_width, screen_height, 1280, 720)
646 place_rectangle(chain.input1.resample_effect, chain.input1.resize_effect, chain.input1.padding_effect, left1, top1, right1, bottom1, screen_width, screen_height, 1280, 720)
649 function set_neutral_color(effect, color)
650 effect:set_vec3("neutral_color", color[1], color[2], color[3])
653 function set_neutral_color_from_signal(effect, signal)
654 if signal == INPUT0_SIGNAL_NUM then
655 set_neutral_color(effect, input0_neutral_color)
657 set_neutral_color(effect, input1_neutral_color)
661 function calc_fade_progress(t, transition_start, transition_end)
662 local tt = (t - transition_start) / (transition_end - transition_start)
669 -- Make the fade look maybe a tad more natural, by pumping it
670 -- through a sigmoid function.
672 tt = 1.0 / (1.0 + math.exp(-tt))