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 live_signal_num = 0;
12 local preview_signal_num = 1;
14 -- The main live chain.
15 local main_chain = EffectChain.new(16, 9);
16 local input0 = main_chain:add_live_input();
17 input0:connect_signal(0);
18 local input1 = main_chain:add_live_input();
19 input1:connect_signal(1);
20 local resample_effect = main_chain:add_effect(ResampleEffect.new(), input0);
21 local padding_effect = main_chain:add_effect(IntegralPaddingEffect.new());
22 padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 1.0);
24 local resample2_effect = main_chain:add_effect(ResampleEffect.new(), input1);
25 -- Effect *saturation_effect = main_chain->add_effect(new SaturationEffect());
26 -- CHECK(saturation_effect->set_float("saturation", 0.3f));
27 local wb_effect = main_chain:add_effect(WhiteBalanceEffect.new());
28 wb_effect:set_float("output_color_temperature", 3500.0);
29 local padding2_effect = main_chain:add_effect(IntegralPaddingEffect.new());
31 main_chain:add_effect(OverlayEffect.new(), padding_effect, padding2_effect);
32 main_chain:finalize(true);
34 -- A chain to show a single input on screen.
35 local simple_chain = EffectChain.new(16, 9);
36 local simple_chain_input = simple_chain:add_live_input();
37 simple_chain_input:connect_signal(0); -- First input card. Can be changed whenever you want.
38 simple_chain:finalize(false);
40 -- Returns the number of outputs in addition to the live (0) and preview (1).
41 -- Called only once, at the start of the program.
42 function num_channels()
46 -- Called every frame.
47 function get_transitions()
48 return {"Cut", "Fade", "Zoom!"};
51 function transition_clicked(num, t)
52 -- Only a cut for now.
53 local temp = live_signal_num;
54 live_signal_num = preview_signal_num;
55 preview_signal_num = temp;
58 function channel_clicked(num, t)
59 -- Presumably change the preview here.
60 io.write("STUB: channel_clicked\n");
63 -- Called every frame. Get the chain for displaying at input <num>,
64 -- where 0 is live, 1 is preview, 2 is the first channel to display
65 -- in the bottom bar, and so on up to num_channels()+1. t is the
66 -- current time in seconds. width and height are the dimensions of
67 -- the output, although you can ignore them if you don't need them
68 -- (they're useful if you want to e.g. know what to resample by).
70 -- You should return two objects; the chain itself, and then a
71 -- function (taking no parameters) that is run just before rendering.
72 -- The function needs to call connect_signal on any inputs, so that
73 -- it gets updated video data for the given frame. (You are allowed
74 -- to switch which input your input is getting from between frames,
75 -- but not calling connect_signal results in undefined behavior.)
76 -- If you want to change any parameters in the chain, this is also
79 -- NOTE: The chain returned must be finalized with the Y'CbCr flag
80 -- if and only if num==0.
81 function get_chain(num, t, width, height)
82 if num == 0 then -- Live.
84 prepare_live_chain(t, width, height);
86 return main_chain, prepare;
88 if num == 1 then -- Preview.
90 simple_chain_input:connect_signal(preview_signal_num);
92 return simple_chain, prepare;
96 simple_chain_input:connect_signal(0);
98 return simple_chain, prepare;
102 simple_chain_input:connect_signal(1);
104 return simple_chain, prepare;
108 function place_rectangle(resample_effect, padding_effect, x0, y0, x1, y1)
115 if x0 > 1280.0 or x1 < 0.0 or y0 > 720.0 or y1 < 0.0 then
116 resample_effect:set_int("width", 1);
117 resample_effect:set_int("height", 1);
118 resample_effect:set_float("zoom_x", 1280.0);
119 resample_effect:set_float("zoom_y", 720.0);
120 padding_effect:set_int("left", 2000);
121 padding_effect:set_int("top", 2000);
125 -- Clip. (TODO: Clip on upper/left sides, too.)
127 srcx1 = (1280.0 - x0) / (x1 - x0);
131 srcy1 = (720.0 - y0) / (y1 - y0);
135 local x_subpixel_offset = x0 - math.floor(x0);
136 local y_subpixel_offset = y0 - math.floor(y0);
138 -- Resampling must be to an integral number of pixels. Round up,
139 -- and then add an extra pixel so we have some leeway for the border.
140 local width = math.ceil(x1 - x0) + 1;
141 local height = math.ceil(y1 - y0) + 1;
142 resample_effect:set_int("width", width);
143 resample_effect:set_int("height", height);
145 -- Correct the discrepancy with zoom. (This will leave a small
146 -- excess edge of pixels and subpixels, which we'll correct for soon.)
147 local zoom_x = (x1 - x0) / (width * (srcx1 - srcx0));
148 local zoom_y = (y1 - y0) / (height * (srcy1 - srcy0));
149 resample_effect:set_float("zoom_x", zoom_x);
150 resample_effect:set_float("zoom_y", zoom_y);
151 resample_effect:set_float("zoom_center_x", 0.0);
152 resample_effect:set_float("zoom_center_y", 0.0);
154 -- Padding must also be to a whole-pixel offset.
155 padding_effect:set_int("left", math.floor(x0));
156 padding_effect:set_int("top", math.floor(y0));
158 -- Correct _that_ discrepancy by subpixel offset in the resampling.
159 resample_effect:set_float("left", -x_subpixel_offset / zoom_x);
160 resample_effect:set_float("top", -y_subpixel_offset / zoom_y);
162 -- Finally, adjust the border so it is exactly where we want it.
163 padding_effect:set_float("border_offset_left", x_subpixel_offset);
164 padding_effect:set_float("border_offset_right", x1 - (math.floor(x0) + width));
165 padding_effect:set_float("border_offset_top", y_subpixel_offset);
166 padding_effect:set_float("border_offset_bottom", y1 - (math.floor(y0) + height));
169 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
171 return math.floor(x + 0.5)
174 function prepare_live_chain(t, width, height)
175 input0:connect_signal(live_signal_num);
176 input1:connect_signal(1);
179 local height0 = round(width0 * 9.0 / 16.0);
183 local bottom0 = top0 + height0;
184 local right0 = left0 + width0;
189 local bottom1 = 720 - 48;
190 local right1 = 1280 - 16;
191 local top1 = bottom1 - height1;
192 local left1 = right1 - width1;
193 local sub_t = 0.5 + 0.5 * math.cos(t * 1.0);
194 local scale0 = 1.0 + sub_t * (1280.0 / 848.0 - 1.0);
195 local tx0 = 0.0 + sub_t * (-16.0 * scale0);
196 local ty0 = 0.0 + sub_t * (-48.0 * scale0);
198 top0 = top0 * scale0 + ty0;
199 bottom0 = bottom0 * scale0 + ty0;
200 left0 = left0 * scale0 + tx0;
201 right0 = right0 * scale0 + tx0;
203 top1 = top1 * scale0 + ty0;
204 bottom1 = bottom1 * scale0 + ty0;
205 left1 = left1 * scale0 + tx0;
206 right1 = right1 * scale0 + tx0;
207 place_rectangle(resample_effect, padding_effect, left0, top0, right0, bottom0);
208 place_rectangle(resample2_effect, padding2_effect, left1, top1, right1, bottom1);