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 -- A chain to fade between two inputs, of which either can be a picture
94 -- or a live input. In practice only used live, but we still support the
96 function make_fade_chain(input0_live, input0_deint, input1_live, input1_deint, hq)
97 local chain = EffectChain.new(16, 9)
99 local input0, wb0_effect, input0_last, input1, wb1_effect, input1_last
102 input0 = chain:add_live_input(false, input0_deint)
103 wb0_effect = chain:add_effect(WhiteBalanceEffect.new())
104 input0:connect_signal(0)
105 input0_last = wb0_effect
107 input0 = chain:add_effect(ImageInput.new("bg.jpeg"))
112 input1 = chain:add_live_input(false, input1_deint)
113 wb1_effect = chain:add_effect(WhiteBalanceEffect.new())
114 input1:connect_signal(1)
115 input1_last = wb1_effect
117 input1 = chain:add_effect(ImageInput.new("bg.jpeg"))
121 local mix_effect = chain:add_effect(MixEffect.new(), input0_last, input1_last)
128 wb_effect = wb0_effect
132 wb_effect = wb1_effect
134 mix_effect = mix_effect
138 -- Chains to fade between two inputs, in various configurations.
139 local fade_chains = {}
140 for input0_type, input0_live in pairs({static = false, live = true, livedeint = true}) do
141 local input0_deint = (input0_live == "livedeint")
142 fade_chains[input0_type] = {}
143 for input1_type, input1_live in pairs({static = false, live = true, livedeint = true}) do
144 local input1_deint = (input1_live == "livedeint")
145 fade_chains[input0_type][input1_type] = {}
146 for _, hq in pairs({true, false}) do
147 fade_chains[input0_type][input1_type][hq] =
148 make_fade_chain(input0_live, input0_deint, input1_live, input1_deint, hq)
153 -- A chain to show a single input on screen.
154 function make_simple_chain(input_deint, hq)
155 local chain = EffectChain.new(16, 9)
157 local input = chain:add_live_input(false, input_deint)
158 input:connect_signal(0) -- First input card. Can be changed whenever you want.
159 local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
165 wb_effect = wb_effect
169 -- Make all possible combinations of single-input chains.
170 local simple_chains = {}
171 for input_type, input_deint in pairs({live = false, livedeint = true}) do
172 simple_chains[input_type] = {}
173 for _, hq in pairs({true, false}) do
174 simple_chains[input_type][hq] = make_simple_chain(input_deint, hq)
178 -- A chain to show a single static picture on screen (HQ version).
179 local static_chain_hq = EffectChain.new(16, 9)
180 local static_chain_hq_input = static_chain_hq:add_effect(ImageInput.new("bg.jpeg"))
181 static_chain_hq:finalize(true)
183 -- A chain to show a single static picture on screen (LQ version).
184 local static_chain_lq = EffectChain.new(16, 9)
185 local static_chain_lq_input = static_chain_lq:add_effect(ImageInput.new("bg.jpeg"))
186 static_chain_lq:finalize(false)
188 -- Used for indexing into the tables of chains.
189 function get_input_type(signals, signal_num)
190 if signal_num == STATIC_SIGNAL_NUM then
192 elseif signals:get_interlaced(signal_num) then
199 -- Returns the number of outputs in addition to the live (0) and preview (1).
200 -- Called only once, at the start of the program.
201 function num_channels()
205 -- Returns the name for each additional channel (starting from 2).
206 -- Called only once for each channel, at the start of the program.
207 function channel_name(channel)
210 elseif channel == 3 then
212 elseif channel == 4 then
213 return "Side-by-side"
214 elseif channel == 5 then
215 return "Static picture"
219 -- Returns if a given channel supports setting white balance (starting from 2).
220 -- Called only once for each channel, at the start of the program.
221 function supports_set_wb(channel)
222 return channel == 2 or channel == 3
225 -- Gets called with a new gray point when the white balance is changing.
226 -- The color is in linear light (not sRGB gamma).
227 function set_wb(channel, red, green, blue)
229 input0_neutral_color = { red, green, blue }
230 elseif channel == 3 then
231 input1_neutral_color = { red, green, blue }
235 function finish_transitions(t)
236 -- If live is SBS but de-facto single, make it so.
237 if live_signal_num == SBS_SIGNAL_NUM and t >= transition_end and zoom_dst == 1.0 then
238 live_signal_num = zoom_poi
241 -- If live is fade but de-facto single, make it so.
242 if live_signal_num == FADE_SIGNAL_NUM and t >= transition_end then
243 live_signal_num = fade_dst_signal
247 -- Called every frame.
248 function get_transitions(t)
249 finish_transitions(t)
251 if live_signal_num == preview_signal_num then
255 if live_signal_num == SBS_SIGNAL_NUM and t >= transition_start and t <= transition_end then
260 if (live_signal_num == INPUT0_SIGNAL_NUM or
261 live_signal_num == INPUT1_SIGNAL_NUM or
262 live_signal_num == STATIC_SIGNAL_NUM) and
263 (preview_signal_num == INPUT0_SIGNAL_NUM or
264 preview_signal_num == INPUT1_SIGNAL_NUM or
265 preview_signal_num == STATIC_SIGNAL_NUM) then
266 return {"Cut", "", "Fade"}
270 if live_signal_num == SBS_SIGNAL_NUM and
271 (preview_signal_num == INPUT0_SIGNAL_NUM or preview_signal_num == INPUT1_SIGNAL_NUM) then
272 return {"Cut", "Zoom in"}
273 elseif (live_signal_num == INPUT0_SIGNAL_NUM or live_signal_num == INPUT1_SIGNAL_NUM) and
274 preview_signal_num == SBS_SIGNAL_NUM then
275 return {"Cut", "Zoom out"}
281 function transition_clicked(num, t)
284 if live_signal_num == FADE_SIGNAL_NUM then
285 -- Ongoing fade; finish it immediately.
286 finish_transitions(transition_end)
289 local temp = live_signal_num
290 live_signal_num = preview_signal_num
291 preview_signal_num = temp
293 if live_signal_num == SBS_SIGNAL_NUM then
294 -- Just cut to SBS, we need to reset any zooms.
297 transition_start = -2.0
298 transition_end = -1.0
303 finish_transitions(t)
305 if live_signal_num == preview_signal_num then
310 if (live_signal_num == INPUT0_SIGNAL_NUM and preview_signal_num == INPUT1_SIGNAL_NUM) or
311 (live_signal_num == INPUT1_SIGNAL_NUM and preview_signal_num == INPUT0_SIGNAL_NUM) then
312 -- We can't zoom between these. Just make a cut.
313 io.write("Cutting from " .. live_signal_num .. " to " .. live_signal_num .. "\n")
314 local temp = live_signal_num
315 live_signal_num = preview_signal_num
316 preview_signal_num = temp
320 if live_signal_num == SBS_SIGNAL_NUM and
321 (preview_signal_num == INPUT0_SIGNAL_NUM or preview_signal_num == INPUT1_SIGNAL_NUM) then
322 -- Zoom in from SBS to single.
324 transition_end = t + 1.0
327 zoom_poi = preview_signal_num
328 preview_signal_num = SBS_SIGNAL_NUM
329 elseif (live_signal_num == INPUT0_SIGNAL_NUM or live_signal_num == INPUT1_SIGNAL_NUM) and
330 preview_signal_num == SBS_SIGNAL_NUM then
331 -- Zoom out from single to SBS.
333 transition_end = t + 1.0
336 preview_signal_num = live_signal_num
337 zoom_poi = live_signal_num
338 live_signal_num = SBS_SIGNAL_NUM
341 finish_transitions(t)
344 if (live_signal_num ~= preview_signal_num) and
345 (live_signal_num == INPUT0_SIGNAL_NUM or
346 live_signal_num == INPUT1_SIGNAL_NUM or
347 live_signal_num == STATIC_SIGNAL_NUM) and
348 (preview_signal_num == INPUT0_SIGNAL_NUM or
349 preview_signal_num == INPUT1_SIGNAL_NUM or
350 preview_signal_num == STATIC_SIGNAL_NUM) then
352 transition_end = t + 1.0
353 fade_src_signal = live_signal_num
354 fade_dst_signal = preview_signal_num
355 preview_signal_num = live_signal_num
356 live_signal_num = FADE_SIGNAL_NUM
358 -- Fades involving SBS are ignored (we have no chain for it).
363 function channel_clicked(num)
364 preview_signal_num = num
367 -- Called every frame. Get the chain for displaying at input <num>,
368 -- where 0 is live, 1 is preview, 2 is the first channel to display
369 -- in the bottom bar, and so on up to num_channels()+1. t is the
370 -- current time in seconds. width and height are the dimensions of
371 -- the output, although you can ignore them if you don't need them
372 -- (they're useful if you want to e.g. know what to resample by).
374 -- <signals> is basically an exposed InputState, which you can use to
375 -- query for information about the signals at the point of the current
376 -- frame. In particular, you can call get_width() and get_height()
377 -- for any signal number, and use that to e.g. assist in chain selection.
379 -- You should return two objects; the chain itself, and then a
380 -- function (taking no parameters) that is run just before rendering.
381 -- The function needs to call connect_signal on any inputs, so that
382 -- it gets updated video data for the given frame. (You are allowed
383 -- to switch which input your input is getting from between frames,
384 -- but not calling connect_signal results in undefined behavior.)
385 -- If you want to change any parameters in the chain, this is also
388 -- NOTE: The chain returned must be finalized with the Y'CbCr flag
389 -- if and only if num==0.
390 function get_chain(num, t, width, height, signals)
391 if num == 0 then -- Live.
392 if live_signal_num == INPUT0_SIGNAL_NUM or live_signal_num == INPUT1_SIGNAL_NUM then -- Plain input.
393 local input_type = get_input_type(signals, live_signal_num)
394 local chain = simple_chains[input_type][true]
396 chain.input:connect_signal(live_signal_num)
397 set_neutral_color_from_signal(chain.wb_effect, live_signal_num)
399 return chain.chain, prepare
400 elseif live_signal_num == STATIC_SIGNAL_NUM then -- Static picture.
403 return static_chain_hq, prepare
404 elseif live_signal_num == FADE_SIGNAL_NUM then -- Fade.
405 local input0_type = get_input_type(signals, fade_src_signal)
406 local input1_type = get_input_type(signals, fade_dst_signal)
407 local chain = fade_chains[input0_type][input1_type][true]
409 if input0_type == "live" then
410 chain.input0.input:connect_signal(fade_src_signal)
411 set_neutral_color_from_signal(chain.input0.wb_effect, fade_src_signal)
413 if input1_type == "live" then
414 chain.input1.input:connect_signal(fade_dst_signal)
415 set_neutral_color_from_signal(chain.input1.wb_effect, fade_dst_signal)
417 local tt = calc_fade_progress(t, transition_start, transition_end)
419 chain.mix_effect:set_float("strength_first", 1.0 - tt)
420 chain.mix_effect:set_float("strength_second", tt)
422 return chain.chain, prepare
425 -- SBS code (live_signal_num == SBS_SIGNAL_NUM).
426 local input0_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
427 local input1_type = get_input_type(signals, INPUT1_SIGNAL_NUM)
428 if t > transition_end and zoom_dst == 1.0 then
429 -- Special case: Show only the single image on screen.
430 local chain = simple_chains[input0_type][true]
432 chain.input:connect_signal(INPUT0_SIGNAL_NUM)
433 set_neutral_color(chain.wb_effect, input0_neutral_color)
435 return chain.chain, prepare
437 local chain = sbs_chains[input0_type][input1_type][true]
439 if t < transition_start then
440 prepare_sbs_chain(chain, zoom_src, width, height)
441 elseif t > transition_end then
442 prepare_sbs_chain(chain, zoom_dst, width, height)
444 local tt = (t - transition_start) / (transition_end - transition_start)
446 tt = math.sin(tt * 3.14159265358 * 0.5)
447 prepare_sbs_chain(chain, zoom_src + (zoom_dst - zoom_src) * tt, width, height)
450 return chain.chain, prepare
452 if num == 1 then -- Preview.
453 num = preview_signal_num + 2
456 -- Individual preview inputs.
457 if num == INPUT0_SIGNAL_NUM + 2 then
458 local input_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
459 local chain = simple_chains[input_type][false]
461 chain.input:connect_signal(INPUT0_SIGNAL_NUM)
462 set_neutral_color(chain.wb_effect, input0_neutral_color)
464 return chain.chain, prepare
466 if num == INPUT1_SIGNAL_NUM + 2 then
467 local input_type = get_input_type(signals, INPUT1_SIGNAL_NUM)
468 local chain = simple_chains[input_type][false]
470 chain.input:connect_signal(INPUT1_SIGNAL_NUM)
471 set_neutral_color(chain.wb_effect, input1_neutral_color)
473 return chain.chain, prepare
475 if num == SBS_SIGNAL_NUM + 2 then
476 local input0_type = get_input_type(signals, INPUT0_SIGNAL_NUM)
477 local input1_type = get_input_type(signals, INPUT1_SIGNAL_NUM)
478 local chain = sbs_chains[input0_type][input1_type][false]
480 prepare_sbs_chain(chain, 0.0, width, height)
482 return chain.chain, prepare
484 if num == STATIC_SIGNAL_NUM + 2 then
487 return static_chain_lq, prepare
491 function place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height)
498 if x0 > screen_width or x1 < 0.0 or y0 > screen_height or y1 < 0.0 then
499 resample_effect:set_int("width", 1)
500 resample_effect:set_int("height", 1)
501 resample_effect:set_float("zoom_x", screen_width)
502 resample_effect:set_float("zoom_y", screen_height)
503 padding_effect:set_int("left", screen_width + 100)
504 padding_effect:set_int("top", screen_height + 100)
510 srcx0 = -x0 / (x1 - x0)
514 srcy0 = -y0 / (y1 - y0)
517 if x1 > screen_width then
518 srcx1 = (screen_width - x0) / (x1 - x0)
521 if y1 > screen_height then
522 srcy1 = (screen_height - y0) / (y1 - y0)
526 if resample_effect ~= nil then
527 -- High-quality resampling.
528 local x_subpixel_offset = x0 - math.floor(x0)
529 local y_subpixel_offset = y0 - math.floor(y0)
531 -- Resampling must be to an integral number of pixels. Round up,
532 -- and then add an extra pixel so we have some leeway for the border.
533 local width = math.ceil(x1 - x0) + 1
534 local height = math.ceil(y1 - y0) + 1
535 resample_effect:set_int("width", width)
536 resample_effect:set_int("height", height)
538 -- Correct the discrepancy with zoom. (This will leave a small
539 -- excess edge of pixels and subpixels, which we'll correct for soon.)
540 local zoom_x = (x1 - x0) / (width * (srcx1 - srcx0))
541 local zoom_y = (y1 - y0) / (height * (srcy1 - srcy0))
542 resample_effect:set_float("zoom_x", zoom_x)
543 resample_effect:set_float("zoom_y", zoom_y)
544 resample_effect:set_float("zoom_center_x", 0.0)
545 resample_effect:set_float("zoom_center_y", 0.0)
547 -- Padding must also be to a whole-pixel offset.
548 padding_effect:set_int("left", math.floor(x0))
549 padding_effect:set_int("top", math.floor(y0))
551 -- Correct _that_ discrepancy by subpixel offset in the resampling.
552 resample_effect:set_float("left", srcx0 * input_width - x_subpixel_offset / zoom_x)
553 resample_effect:set_float("top", srcy0 * input_height - y_subpixel_offset / zoom_y)
555 -- Finally, adjust the border so it is exactly where we want it.
556 padding_effect:set_float("border_offset_left", x_subpixel_offset)
557 padding_effect:set_float("border_offset_right", x1 - (math.floor(x0) + width))
558 padding_effect:set_float("border_offset_top", y_subpixel_offset)
559 padding_effect:set_float("border_offset_bottom", y1 - (math.floor(y0) + height))
561 -- Lower-quality simple resizing.
562 local width = round(x1 - x0)
563 local height = round(y1 - y0)
564 resize_effect:set_int("width", width)
565 resize_effect:set_int("height", height)
567 -- Padding must also be to a whole-pixel offset.
568 padding_effect:set_int("left", math.floor(x0))
569 padding_effect:set_int("top", math.floor(y0))
573 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
575 return math.floor(x + 0.5)
578 function lerp(a, b, t)
579 return a + (b - a) * t
582 function prepare_sbs_chain(chain, t, screen_width, screen_height)
583 chain.input0.input:connect_signal(0)
584 chain.input1.input:connect_signal(1)
585 set_neutral_color(chain.input0.wb_effect, input0_neutral_color)
586 set_neutral_color(chain.input1.wb_effect, input1_neutral_color)
588 -- First input is positioned (16,48) from top-left.
589 local width0 = round(848 * screen_width/1280.0)
590 local height0 = round(width0 * 9.0 / 16.0)
592 local top0 = 48 * screen_height/720.0
593 local left0 = 16 * screen_width/1280.0
594 local bottom0 = top0 + height0
595 local right0 = left0 + width0
597 -- Second input is positioned (16,48) from the bottom-right.
598 local width1 = round(384 * screen_width/1280.0)
599 local height1 = round(216 * screen_height/720.0)
601 local bottom1 = screen_height - 48 * screen_height/720.0
602 local right1 = screen_width - 16 * screen_width/1280.0
603 local top1 = bottom1 - height1
604 local left1 = right1 - width1
606 -- Interpolate between the fullscreen and side-by-side views.
607 local scale0, tx0, tx0
608 if zoom_poi == INPUT0_SIGNAL_NUM then
609 local new_left0 = lerp(left0, 0, t)
610 local new_right0 = lerp(right0, screen_width, t)
611 local new_top0 = lerp(top0, 0, t)
612 local new_bottom0 = lerp(bottom0, screen_height, t)
614 scale0 = (new_right0 - new_left0) / width0 -- Same vertically and horizonally.
615 tx0 = new_left0 - left0 * scale0
616 ty0 = new_top0 - top0 * scale0
618 local new_left1 = lerp(left1, 0, t)
619 local new_right1 = lerp(right1, screen_width, t)
620 local new_top1 = lerp(top1, 0, t)
621 local new_bottom1 = lerp(bottom1, screen_height, t)
623 scale0 = (new_right1 - new_left1) / width1 -- Same vertically and horizonally.
624 tx0 = new_left1 - left1 * scale0
625 ty0 = new_top1 - top1 * scale0
628 top0 = top0 * scale0 + ty0
629 bottom0 = bottom0 * scale0 + ty0
630 left0 = left0 * scale0 + tx0
631 right0 = right0 * scale0 + tx0
633 top1 = top1 * scale0 + ty0
634 bottom1 = bottom1 * scale0 + ty0
635 left1 = left1 * scale0 + tx0
636 right1 = right1 * scale0 + tx0
637 place_rectangle(chain.input0.resample_effect, chain.input0.resize_effect, chain.input0.padding_effect, left0, top0, right0, bottom0, screen_width, screen_height, 1280, 720)
638 place_rectangle(chain.input1.resample_effect, chain.input1.resize_effect, chain.input1.padding_effect, left1, top1, right1, bottom1, screen_width, screen_height, 1280, 720)
641 function set_neutral_color(effect, color)
642 effect:set_vec3("neutral_color", color[1], color[2], color[3])
645 function set_neutral_color_from_signal(effect, signal)
646 if signal == INPUT0_SIGNAL_NUM then
647 set_neutral_color(effect, input0_neutral_color)
649 set_neutral_color(effect, input1_neutral_color)
653 function calc_fade_progress(t, transition_start, transition_end)
654 local tt = (t - transition_start) / (transition_end - transition_start)
661 -- Make the fade look maybe a tad more natural, by pumping it
662 -- through a sigmoid function.
664 tt = 1.0 / (1.0 + math.exp(-tt))