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.
9 io.write("hello from lua\n")
11 local transition_start = -2.0
12 local transition_end = -1.0
15 local zoom_poi = 0 -- which input to zoom in on
19 local live_signal_num = 0
20 local preview_signal_num = 1
22 -- The main live chain.
23 function make_sbs_chain(hq)
24 local chain = EffectChain.new(16, 9)
25 local input0 = chain:add_live_input()
26 input0:connect_signal(0)
27 local input1 = chain:add_live_input()
28 input1:connect_signal(1)
30 local resample_effect = nil
31 local resize_effect = nil
33 resample_effect = chain:add_effect(ResampleEffect.new(), input0)
35 resize_effect = chain:add_effect(ResizeEffect.new(), input0)
38 local padding_effect = chain:add_effect(IntegralPaddingEffect.new())
39 padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 1.0)
41 local resample2_effect = nil
42 local resize2_effect = nil
44 resample2_effect = chain:add_effect(ResampleEffect.new(), input1)
46 resize2_effect = chain:add_effect(ResizeEffect.new(), input1)
48 -- Effect *saturation_effect = chain->add_effect(new SaturationEffect())
49 -- CHECK(saturation_effect->set_float("saturation", 0.3f))
50 local wb_effect = chain:add_effect(WhiteBalanceEffect.new())
51 wb_effect:set_float("output_color_temperature", 3500.0)
52 local padding2_effect = chain:add_effect(IntegralPaddingEffect.new())
54 chain:add_effect(OverlayEffect.new(), padding_effect, padding2_effect)
61 resample_effect = resample_effect,
62 resize_effect = resize_effect,
63 padding_effect = padding_effect
67 resample_effect = resample2_effect,
68 resize_effect = resize2_effect,
69 padding_effect = padding2_effect
74 local main_chain_hq = make_sbs_chain(true)
75 local main_chain_lq = make_sbs_chain(false)
77 -- A chain to fade between two inputs (live chain only)
78 local fade_chain_hq = EffectChain.new(16, 9)
79 local fade_chain_hq_input0 = fade_chain_hq:add_live_input()
80 local fade_chain_hq_input1 = fade_chain_hq:add_live_input()
81 fade_chain_hq_input0:connect_signal(0)
82 fade_chain_hq_input1:connect_signal(1)
83 local fade_chain_mix_effect = fade_chain_hq:add_effect(MixEffect.new(), fade_chain_hq_input0, fade_chain_hq_input1)
84 fade_chain_hq:finalize(true)
86 -- A chain to show a single input on screen (HQ version).
87 local simple_chain_hq = EffectChain.new(16, 9)
88 local simple_chain_hq_input = simple_chain_hq:add_live_input()
89 simple_chain_hq_input:connect_signal(0) -- First input card. Can be changed whenever you want.
90 simple_chain_hq:finalize(true)
92 -- A chain to show a single input on screen (LQ version).
93 local simple_chain_lq = EffectChain.new(16, 9)
94 local simple_chain_lq_input = simple_chain_lq:add_live_input()
95 simple_chain_lq_input:connect_signal(0) -- First input card. Can be changed whenever you want.
96 simple_chain_lq:finalize(false)
98 -- Returns the number of outputs in addition to the live (0) and preview (1).
99 -- Called only once, at the start of the program.
100 function num_channels()
104 function finish_transitions(t)
105 -- If live is 2 (SBS) but de-facto single, make it so.
106 if live_signal_num == 2 and t >= transition_end and zoom_dst == 1.0 then
107 live_signal_num = zoom_poi
110 -- If live is 3 (fade) but de-facto single, make it so.
111 if live_signal_num == 3 and t >= transition_end and fade_dst == 1.0 then
114 if live_signal_num == 3 and t >= transition_end and fade_dst == 0.0 then
119 -- Called every frame.
120 function get_transitions(t)
121 finish_transitions(t)
123 if live_signal_num == preview_signal_num then
127 if live_signal_num == 2 and t >= transition_start and t <= transition_end then
132 if (live_signal_num == 0 and preview_signal_num == 1) or
133 (live_signal_num == 1 and preview_signal_num == 0) then
134 return {"Cut", "", "Fade"}
138 if live_signal_num == 2 and (preview_signal_num == 0 or preview_signal_num == 1) then
139 return {"Cut", "Zoom in"}
140 elseif (live_signal_num == 0 or live_signal_num == 1) and preview_signal_num == 2 then
141 return {"Cut", "Zoom out"}
147 function transition_clicked(num, t)
150 if live_signal_num == 3 then
151 -- Ongoing fade; finish it immediately.
152 finish_transitions(transition_end)
155 local temp = live_signal_num
156 live_signal_num = preview_signal_num
157 preview_signal_num = temp
159 if live_signal_num == 2 then
160 -- Just cut to SBS, we need to reset any zooms.
163 transition_start = -2.0
164 transition_end = -1.0
169 finish_transitions(t)
171 if live_signal_num == preview_signal_num then
176 if (live_signal_num == 0 and preview_signal_num == 1) or
177 (live_signal_num == 1 and preview_signal_num == 0) then
178 -- We can't zoom between these. Just make a cut.
179 io.write("Cutting from " .. live_signal_num .. " to " .. live_signal_num .. "\n")
180 local temp = live_signal_num
181 live_signal_num = preview_signal_num
182 preview_signal_num = temp
186 if live_signal_num == 2 and (preview_signal_num == 0 or preview_signal_num == 1) then
187 -- Zoom in from SBS to single.
189 transition_end = t + 1.0
192 zoom_poi = preview_signal_num
193 preview_signal_num = 2
194 elseif (live_signal_num == 0 or live_signal_num == 1) and preview_signal_num == 2 then
195 -- Zoom out from single to SBS.
197 transition_end = t + 1.0
200 preview_signal_num = live_signal_num
201 zoom_poi = live_signal_num
205 finish_transitions(t)
208 if live_signal_num == 0 and preview_signal_num == 1 then
210 transition_end = t + 1.0
213 preview_signal_num = 0
215 elseif live_signal_num == 1 and preview_signal_num == 0 then
217 transition_end = t + 1.0
220 preview_signal_num = 1
223 -- Fades involving SBS are ignored (we have no chain for it).
228 function channel_clicked(num)
229 preview_signal_num = num
232 -- Called every frame. Get the chain for displaying at input <num>,
233 -- where 0 is live, 1 is preview, 2 is the first channel to display
234 -- in the bottom bar, and so on up to num_channels()+1. t is the
235 -- current time in seconds. width and height are the dimensions of
236 -- the output, although you can ignore them if you don't need them
237 -- (they're useful if you want to e.g. know what to resample by).
239 -- You should return two objects; the chain itself, and then a
240 -- function (taking no parameters) that is run just before rendering.
241 -- The function needs to call connect_signal on any inputs, so that
242 -- it gets updated video data for the given frame. (You are allowed
243 -- to switch which input your input is getting from between frames,
244 -- but not calling connect_signal results in undefined behavior.)
245 -- If you want to change any parameters in the chain, this is also
248 -- NOTE: The chain returned must be finalized with the Y'CbCr flag
249 -- if and only if num==0.
250 function get_chain(num, t, width, height)
251 if num == 0 then -- Live.
252 if live_signal_num == 0 or live_signal_num == 1 then -- Plain inputs.
254 simple_chain_hq_input:connect_signal(live_signal_num)
256 return simple_chain_hq, prepare
257 elseif live_signal_num == 3 then -- Fade.
259 fade_chain_hq_input0:connect_signal(0)
260 fade_chain_hq_input1:connect_signal(1)
261 local tt = (t - transition_start) / (transition_end - transition_start)
268 tt = fade_src + tt * (fade_dst - fade_src)
270 -- Make the fade look maybe a tad more natural, by pumping it
271 -- through a sigmoid function.
273 tt = 1.0 / (1.0 + math.exp(-tt))
275 fade_chain_mix_effect:set_float("strength_first", tt)
276 fade_chain_mix_effect:set_float("strength_second", 1.0 - tt)
278 return fade_chain_hq, prepare
281 -- SBS code (live_signal_num == 2).
282 if t > transition_end and zoom_dst == 1.0 then
283 -- Special case: Show only the single image on screen.
285 simple_chain_hq_input:connect_signal(0)
287 return simple_chain_hq, prepare
290 if t < transition_start then
291 prepare_sbs_chain(main_chain_hq, zoom_src, width, height)
292 elseif t > transition_end then
293 prepare_sbs_chain(main_chain_hq, zoom_dst, width, height)
295 local tt = (t - transition_start) / (transition_end - transition_start)
297 tt = math.sin(tt * 3.14159265358 * 0.5)
298 prepare_sbs_chain(main_chain_hq, zoom_src + (zoom_dst - zoom_src) * tt, width, height)
301 return main_chain_hq.chain, prepare
303 if num == 1 then -- Preview.
304 num = preview_signal_num + 2
308 simple_chain_lq_input:connect_signal(0)
310 return simple_chain_lq, prepare
314 simple_chain_lq_input:connect_signal(1)
316 return simple_chain_lq, prepare
320 prepare_sbs_chain(main_chain_lq, 0.0, width, height)
322 return main_chain_lq.chain, prepare
326 function place_rectangle(resample_effect, resize_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height, input_width, input_height)
333 if x0 > screen_width or x1 < 0.0 or y0 > screen_height or y1 < 0.0 then
334 resample_effect:set_int("width", 1)
335 resample_effect:set_int("height", 1)
336 resample_effect:set_float("zoom_x", screen_width)
337 resample_effect:set_float("zoom_y", screen_height)
338 padding_effect:set_int("left", screen_width + 100)
339 padding_effect:set_int("top", screen_height + 100)
345 srcx0 = -x0 / (x1 - x0)
349 srcy0 = -y0 / (y1 - y0)
352 if x1 > screen_width then
353 srcx1 = (screen_width - x0) / (x1 - x0)
356 if y1 > screen_height then
357 srcy1 = (screen_height - y0) / (y1 - y0)
361 if resample_effect ~= nil then
362 -- High-quality resampling.
363 local x_subpixel_offset = x0 - math.floor(x0)
364 local y_subpixel_offset = y0 - math.floor(y0)
366 -- Resampling must be to an integral number of pixels. Round up,
367 -- and then add an extra pixel so we have some leeway for the border.
368 local width = math.ceil(x1 - x0) + 1
369 local height = math.ceil(y1 - y0) + 1
370 resample_effect:set_int("width", width)
371 resample_effect:set_int("height", height)
373 -- Correct the discrepancy with zoom. (This will leave a small
374 -- excess edge of pixels and subpixels, which we'll correct for soon.)
375 local zoom_x = (x1 - x0) / (width * (srcx1 - srcx0))
376 local zoom_y = (y1 - y0) / (height * (srcy1 - srcy0))
377 resample_effect:set_float("zoom_x", zoom_x)
378 resample_effect:set_float("zoom_y", zoom_y)
379 resample_effect:set_float("zoom_center_x", 0.0)
380 resample_effect:set_float("zoom_center_y", 0.0)
382 -- Padding must also be to a whole-pixel offset.
383 padding_effect:set_int("left", math.floor(x0))
384 padding_effect:set_int("top", math.floor(y0))
386 -- Correct _that_ discrepancy by subpixel offset in the resampling.
387 resample_effect:set_float("left", srcx0 * input_width - x_subpixel_offset / zoom_x)
388 resample_effect:set_float("top", srcy0 * input_height - y_subpixel_offset / zoom_y)
390 -- Finally, adjust the border so it is exactly where we want it.
391 padding_effect:set_float("border_offset_left", x_subpixel_offset)
392 padding_effect:set_float("border_offset_right", x1 - (math.floor(x0) + width))
393 padding_effect:set_float("border_offset_top", y_subpixel_offset)
394 padding_effect:set_float("border_offset_bottom", y1 - (math.floor(y0) + height))
396 -- Lower-quality simple resizing.
397 local width = round(x1 - x0)
398 local height = round(y1 - y0)
399 resize_effect:set_int("width", width)
400 resize_effect:set_int("height", height)
402 -- Padding must also be to a whole-pixel offset.
403 padding_effect:set_int("left", math.floor(x0))
404 padding_effect:set_int("top", math.floor(y0))
408 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
410 return math.floor(x + 0.5)
413 function lerp(a, b, t)
414 return a + (b - a) * t
417 function prepare_sbs_chain(chain, t, screen_width, screen_height)
418 chain.input0.input:connect_signal(0)
419 chain.input1.input:connect_signal(1)
421 -- First input is positioned (16,48) from top-left.
422 local width0 = round(848 * screen_width/1280.0)
423 local height0 = round(width0 * 9.0 / 16.0)
425 local top0 = 48 * screen_height/720.0
426 local left0 = 16 * screen_width/1280.0
427 local bottom0 = top0 + height0
428 local right0 = left0 + width0
430 -- Second input is positioned (16,48) from the bottom-right.
431 local width1 = round(384 * screen_width/1280.0)
432 local height1 = round(216 * screen_height/720.0)
434 local bottom1 = screen_height - 48 * screen_height/720.0
435 local right1 = screen_width - 16 * screen_width/1280.0
436 local top1 = bottom1 - height1
437 local left1 = right1 - width1
439 -- Interpolate between the fullscreen and side-by-side views.
440 local scale0, tx0, tx0
441 if zoom_poi == 0 then
442 local new_left0 = lerp(left0, 0, t)
443 local new_right0 = lerp(right0, screen_width, t)
444 local new_top0 = lerp(top0, 0, t)
445 local new_bottom0 = lerp(bottom0, screen_height, t)
447 scale0 = (new_right0 - new_left0) / width0 -- Same vertically and horizonally.
448 tx0 = new_left0 - left0 * scale0
449 ty0 = new_top0 - top0 * scale0
451 local new_left1 = lerp(left1, 0, t)
452 local new_right1 = lerp(right1, screen_width, t)
453 local new_top1 = lerp(top1, 0, t)
454 local new_bottom1 = lerp(bottom1, screen_height, t)
456 scale0 = (new_right1 - new_left1) / width1 -- Same vertically and horizonally.
457 tx0 = new_left1 - left1 * scale0
458 ty0 = new_top1 - top1 * scale0
461 top0 = top0 * scale0 + ty0
462 bottom0 = bottom0 * scale0 + ty0
463 left0 = left0 * scale0 + tx0
464 right0 = right0 * scale0 + tx0
466 top1 = top1 * scale0 + ty0
467 bottom1 = bottom1 * scale0 + ty0
468 left1 = left1 * scale0 + tx0
469 right1 = right1 * scale0 + tx0
470 place_rectangle(chain.input0.resample_effect, chain.input0.resize_effect, chain.input0.padding_effect, left0, top0, right0, bottom0, screen_width, screen_height, 1280, 720)
471 place_rectangle(chain.input1.resample_effect, chain.input1.resize_effect, chain.input1.padding_effect, left1, top1, right1, bottom1, screen_width, screen_height, 1280, 720)