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 local transition_start = -2.0
11 local transition_end = -1.0
14 local zoom_poi = 0 -- which input to zoom in on
18 local live_signal_num = 0
19 local preview_signal_num = 1
21 -- The main live chain.
22 function make_sbs_chain(hq)
23 local chain = EffectChain.new(16, 9)
24 local input0 = chain:add_live_input()
25 input0:connect_signal(0)
26 local input1 = chain:add_live_input()
27 input1:connect_signal(1)
29 local resample_effect = nil
30 local resize_effect = nil
32 resample_effect = chain:add_effect(ResampleEffect.new(), input0)
34 resize_effect = chain:add_effect(ResizeEffect.new(), input0)
37 local padding_effect = chain:add_effect(IntegralPaddingEffect.new())
38 padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 1.0)
40 local resample2_effect = nil
41 local resize2_effect = nil
43 resample2_effect = chain:add_effect(ResampleEffect.new(), input1)
45 resize2_effect = chain:add_effect(ResizeEffect.new(), input1)
47 -- Effect *saturation_effect = chain->add_effect(new SaturationEffect())
48 -- CHECK(saturation_effect->set_float("saturation", 0.3f))
49 local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
50 wb_effect:set_float("output_color_temperature", 3500.0)
51 local padding2_effect = chain:add_effect(IntegralPaddingEffect.new())
53 chain:add_effect(OverlayEffect.new(), padding_effect, padding2_effect)
60 resample_effect = resample_effect,
61 resize_effect = resize_effect,
62 padding_effect = padding_effect
66 resample_effect = resample2_effect,
67 resize_effect = resize2_effect,
68 padding_effect = padding2_effect
73 local main_chain_hq = make_sbs_chain(true)
74 local main_chain_lq = make_sbs_chain(false)
76 -- A chain to fade between two inputs (live chain only)
77 local fade_chain_hq = EffectChain.new(16, 9)
78 local fade_chain_hq_input0 = fade_chain_hq:add_live_input()
79 local fade_chain_hq_input1 = fade_chain_hq:add_live_input()
80 fade_chain_hq_input0:connect_signal(0)
81 fade_chain_hq_input1:connect_signal(1)
82 local fade_chain_mix_effect = fade_chain_hq:add_effect(MixEffect.new(), fade_chain_hq_input0, fade_chain_hq_input1)
83 fade_chain_hq:finalize(true)
85 -- A chain to show a single input on screen (HQ version).
86 local simple_chain_hq = EffectChain.new(16, 9)
87 local simple_chain_hq_input = simple_chain_hq:add_live_input()
88 simple_chain_hq_input:connect_signal(0) -- First input card. Can be changed whenever you want.
89 simple_chain_hq:finalize(true)
91 -- A chain to show a single input on screen (LQ version).
92 local simple_chain_lq = EffectChain.new(16, 9)
93 local simple_chain_lq_input = simple_chain_lq:add_live_input()
94 simple_chain_lq_input:connect_signal(0) -- First input card. Can be changed whenever you want.
95 simple_chain_lq:finalize(false)
97 -- Returns the number of outputs in addition to the live (0) and preview (1).
98 -- Called only once, at the start of the program.
99 function num_channels()
103 function finish_transitions(t)
104 -- If live is 2 (SBS) but de-facto single, make it so.
105 if live_signal_num == 2 and t >= transition_end and zoom_dst == 1.0 then
106 live_signal_num = zoom_poi
109 -- If live is 3 (fade) but de-facto single, make it so.
110 if live_signal_num == 3 and t >= transition_end and fade_dst == 1.0 then
113 if live_signal_num == 3 and t >= transition_end and fade_dst == 0.0 then
118 -- Called every frame.
119 function get_transitions(t)
120 finish_transitions(t)
122 if live_signal_num == preview_signal_num then
126 if live_signal_num == 2 and t >= transition_start and t <= transition_end then
131 if (live_signal_num == 0 and preview_signal_num == 1) or
132 (live_signal_num == 1 and preview_signal_num == 0) then
133 return {"Cut", "", "Fade"}
137 if live_signal_num == 2 and (preview_signal_num == 0 or preview_signal_num == 1) then
138 return {"Cut", "Zoom in"}
139 elseif (live_signal_num == 0 or live_signal_num == 1) and preview_signal_num == 2 then
140 return {"Cut", "Zoom out"}
146 function transition_clicked(num, t)
149 if live_signal_num == 3 then
150 -- Ongoing fade; finish it immediately.
151 finish_transitions(transition_end)
154 local temp = live_signal_num
155 live_signal_num = preview_signal_num
156 preview_signal_num = temp
158 if live_signal_num == 2 then
159 -- Just cut to SBS, we need to reset any zooms.
162 transition_start = -2.0
163 transition_end = -1.0
168 finish_transitions(t)
170 if live_signal_num == preview_signal_num then
175 if (live_signal_num == 0 and preview_signal_num == 1) or
176 (live_signal_num == 1 and preview_signal_num == 0) then
177 -- We can't zoom between these. Just make a cut.
178 io.write("Cutting from " .. live_signal_num .. " to " .. live_signal_num .. "\n")
179 local temp = live_signal_num
180 live_signal_num = preview_signal_num
181 preview_signal_num = temp
185 if live_signal_num == 2 and (preview_signal_num == 0 or preview_signal_num == 1) then
186 -- Zoom in from SBS to single.
188 transition_end = t + 1.0
191 zoom_poi = preview_signal_num
192 preview_signal_num = 2
193 elseif (live_signal_num == 0 or live_signal_num == 1) and preview_signal_num == 2 then
194 -- Zoom out from single to SBS.
196 transition_end = t + 1.0
199 preview_signal_num = live_signal_num
200 zoom_poi = live_signal_num
204 finish_transitions(t)
207 if live_signal_num == 0 and preview_signal_num == 1 then
209 transition_end = t + 1.0
212 preview_signal_num = 0
214 elseif live_signal_num == 1 and preview_signal_num == 0 then
216 transition_end = t + 1.0
219 preview_signal_num = 1
222 -- Fades involving SBS are ignored (we have no chain for it).
227 function channel_clicked(num)
228 preview_signal_num = num
231 -- Called every frame. Get the chain for displaying at input <num>,
232 -- where 0 is live, 1 is preview, 2 is the first channel to display
233 -- in the bottom bar, and so on up to num_channels()+1. t is the
234 -- current time in seconds. width and height are the dimensions of
235 -- the output, although you can ignore them if you don't need them
236 -- (they're useful if you want to e.g. know what to resample by).
238 -- You should return two objects; the chain itself, and then a
239 -- function (taking no parameters) that is run just before rendering.
240 -- The function needs to call connect_signal on any inputs, so that
241 -- it gets updated video data for the given frame. (You are allowed
242 -- to switch which input your input is getting from between frames,
243 -- but not calling connect_signal results in undefined behavior.)
244 -- If you want to change any parameters in the chain, this is also
247 -- NOTE: The chain returned must be finalized with the Y'CbCr flag
248 -- if and only if num==0.
249 function get_chain(num, t, width, height)
250 if num == 0 then -- Live.
251 if live_signal_num == 0 or live_signal_num == 1 then -- Plain inputs.
253 simple_chain_hq_input:connect_signal(live_signal_num)
255 return simple_chain_hq, prepare
256 elseif live_signal_num == 3 then -- Fade.
258 fade_chain_hq_input0:connect_signal(0)
259 fade_chain_hq_input1:connect_signal(1)
260 local tt = (t - transition_start) / (transition_end - transition_start)
267 tt = fade_src + tt * (fade_dst - fade_src)
269 -- Make the fade look maybe a tad more natural, by pumping it
270 -- through a sigmoid function.
272 tt = 1.0 / (1.0 + math.exp(-tt))
274 fade_chain_mix_effect:set_float("strength_first", tt)
275 fade_chain_mix_effect:set_float("strength_second", 1.0 - tt)
277 return fade_chain_hq, prepare
280 -- SBS code (live_signal_num == 2).
281 if t > transition_end and zoom_dst == 1.0 then
282 -- Special case: Show only the single image on screen.
284 simple_chain_hq_input:connect_signal(0)
286 return simple_chain_hq, prepare
289 if t < transition_start then
290 prepare_sbs_chain(main_chain_hq, zoom_src, width, height)
291 elseif t > transition_end then
292 prepare_sbs_chain(main_chain_hq, zoom_dst, width, height)
294 local tt = (t - transition_start) / (transition_end - transition_start)
296 tt = math.sin(tt * 3.14159265358 * 0.5)
297 prepare_sbs_chain(main_chain_hq, zoom_src + (zoom_dst - zoom_src) * tt, width, height)
300 return main_chain_hq.chain, prepare
302 if num == 1 then -- Preview.
303 num = preview_signal_num + 2
307 simple_chain_lq_input:connect_signal(0)
309 return simple_chain_lq, prepare
313 simple_chain_lq_input:connect_signal(1)
315 return simple_chain_lq, prepare
319 prepare_sbs_chain(main_chain_lq, 0.0, width, height)
321 return main_chain_lq.chain, prepare
325 function place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height)
332 if x0 > screen_width or x1 < 0.0 or y0 > screen_height or y1 < 0.0 then
333 resample_effect:set_int("width", 1)
334 resample_effect:set_int("height", 1)
335 resample_effect:set_float("zoom_x", screen_width)
336 resample_effect:set_float("zoom_y", screen_height)
337 padding_effect:set_int("left", screen_width + 100)
338 padding_effect:set_int("top", screen_height + 100)
344 srcx0 = -x0 / (x1 - x0)
348 srcy0 = -y0 / (y1 - y0)
351 if x1 > screen_width then
352 srcx1 = (screen_width - x0) / (x1 - x0)
355 if y1 > screen_height then
356 srcy1 = (screen_height - y0) / (y1 - y0)
360 if resample_effect ~= nil then
361 -- High-quality resampling.
362 local x_subpixel_offset = x0 - math.floor(x0)
363 local y_subpixel_offset = y0 - math.floor(y0)
365 -- Resampling must be to an integral number of pixels. Round up,
366 -- and then add an extra pixel so we have some leeway for the border.
367 local width = math.ceil(x1 - x0) + 1
368 local height = math.ceil(y1 - y0) + 1
369 resample_effect:set_int("width", width)
370 resample_effect:set_int("height", height)
372 -- Correct the discrepancy with zoom. (This will leave a small
373 -- excess edge of pixels and subpixels, which we'll correct for soon.)
374 local zoom_x = (x1 - x0) / (width * (srcx1 - srcx0))
375 local zoom_y = (y1 - y0) / (height * (srcy1 - srcy0))
376 resample_effect:set_float("zoom_x", zoom_x)
377 resample_effect:set_float("zoom_y", zoom_y)
378 resample_effect:set_float("zoom_center_x", 0.0)
379 resample_effect:set_float("zoom_center_y", 0.0)
381 -- Padding must also be to a whole-pixel offset.
382 padding_effect:set_int("left", math.floor(x0))
383 padding_effect:set_int("top", math.floor(y0))
385 -- Correct _that_ discrepancy by subpixel offset in the resampling.
386 resample_effect:set_float("left", srcx0 * input_width - x_subpixel_offset / zoom_x)
387 resample_effect:set_float("top", srcy0 * input_height - y_subpixel_offset / zoom_y)
389 -- Finally, adjust the border so it is exactly where we want it.
390 padding_effect:set_float("border_offset_left", x_subpixel_offset)
391 padding_effect:set_float("border_offset_right", x1 - (math.floor(x0) + width))
392 padding_effect:set_float("border_offset_top", y_subpixel_offset)
393 padding_effect:set_float("border_offset_bottom", y1 - (math.floor(y0) + height))
395 -- Lower-quality simple resizing.
396 local width = round(x1 - x0)
397 local height = round(y1 - y0)
398 resize_effect:set_int("width", width)
399 resize_effect:set_int("height", height)
401 -- Padding must also be to a whole-pixel offset.
402 padding_effect:set_int("left", math.floor(x0))
403 padding_effect:set_int("top", math.floor(y0))
407 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
409 return math.floor(x + 0.5)
412 function lerp(a, b, t)
413 return a + (b - a) * t
416 function prepare_sbs_chain(chain, t, screen_width, screen_height)
417 chain.input0.input:connect_signal(0)
418 chain.input1.input:connect_signal(1)
420 -- First input is positioned (16,48) from top-left.
421 local width0 = round(848 * screen_width/1280.0)
422 local height0 = round(width0 * 9.0 / 16.0)
424 local top0 = 48 * screen_height/720.0
425 local left0 = 16 * screen_width/1280.0
426 local bottom0 = top0 + height0
427 local right0 = left0 + width0
429 -- Second input is positioned (16,48) from the bottom-right.
430 local width1 = round(384 * screen_width/1280.0)
431 local height1 = round(216 * screen_height/720.0)
433 local bottom1 = screen_height - 48 * screen_height/720.0
434 local right1 = screen_width - 16 * screen_width/1280.0
435 local top1 = bottom1 - height1
436 local left1 = right1 - width1
438 -- Interpolate between the fullscreen and side-by-side views.
439 local scale0, tx0, tx0
440 if zoom_poi == 0 then
441 local new_left0 = lerp(left0, 0, t)
442 local new_right0 = lerp(right0, screen_width, t)
443 local new_top0 = lerp(top0, 0, t)
444 local new_bottom0 = lerp(bottom0, screen_height, t)
446 scale0 = (new_right0 - new_left0) / width0 -- Same vertically and horizonally.
447 tx0 = new_left0 - left0 * scale0
448 ty0 = new_top0 - top0 * scale0
450 local new_left1 = lerp(left1, 0, t)
451 local new_right1 = lerp(right1, screen_width, t)
452 local new_top1 = lerp(top1, 0, t)
453 local new_bottom1 = lerp(bottom1, screen_height, t)
455 scale0 = (new_right1 - new_left1) / width1 -- Same vertically and horizonally.
456 tx0 = new_left1 - left1 * scale0
457 ty0 = new_top1 - top1 * scale0
460 top0 = top0 * scale0 + ty0
461 bottom0 = bottom0 * scale0 + ty0
462 left0 = left0 * scale0 + tx0
463 right0 = right0 * scale0 + tx0
465 top1 = top1 * scale0 + ty0
466 bottom1 = bottom1 * scale0 + ty0
467 left1 = left1 * scale0 + tx0
468 right1 = right1 * scale0 + tx0
469 place_rectangle(chain.input0.resample_effect, chain.input0.resize_effect, chain.input0.padding_effect, left0, top0, right0, bottom0, screen_width, screen_height, 1280, 720)
470 place_rectangle(chain.input1.resample_effect, chain.input1.resize_effect, chain.input1.padding_effect, left1, top1, right1, bottom1, screen_width, screen_height, 1280, 720)