]> git.sesse.net Git - nageru/blob - theme.lua
Make an optimization to the zoombox in the theme.
[nageru] / theme.lua
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.
5 --
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");
10
11 local zoom_start = -2.0;
12 local zoom_end = -1.0;
13 local zoom_src = 0.0;
14 local zoom_dst = 1.0;
15
16 local live_signal_num = 0;
17 local preview_signal_num = 1;
18
19 -- The main live chain.
20 local main_chain = EffectChain.new(16, 9);
21 local input0 = main_chain:add_live_input();
22 input0:connect_signal(0);
23 local input1 = main_chain:add_live_input();
24 input1:connect_signal(1);
25 local resample_effect = main_chain:add_effect(ResampleEffect.new(), input0);
26 local padding_effect = main_chain:add_effect(IntegralPaddingEffect.new());
27 padding_effect:set_vec4("border_color", 0.0, 0.0, 0.0, 1.0);
28
29 local resample2_effect = main_chain:add_effect(ResampleEffect.new(), input1);
30 -- Effect *saturation_effect = main_chain->add_effect(new SaturationEffect());
31 -- CHECK(saturation_effect->set_float("saturation", 0.3f));
32 local wb_effect = main_chain:add_effect(WhiteBalanceEffect.new());
33 wb_effect:set_float("output_color_temperature", 3500.0);
34 local padding2_effect = main_chain:add_effect(IntegralPaddingEffect.new());
35
36 main_chain:add_effect(OverlayEffect.new(), padding_effect, padding2_effect);
37 main_chain:finalize(true);
38
39 -- A chain to show a single input on screen (HQ version).
40 local simple_chain_hq = EffectChain.new(16, 9);
41 local simple_chain_hq_input = simple_chain_hq:add_live_input();
42 simple_chain_hq_input:connect_signal(0);  -- First input card. Can be changed whenever you want.
43 simple_chain_hq:finalize(true);
44
45 -- A chain to show a single input on screen (LQ version).
46 local simple_chain_lq = EffectChain.new(16, 9);
47 local simple_chain_lq_input = simple_chain_lq:add_live_input();
48 simple_chain_lq_input:connect_signal(0);  -- First input card. Can be changed whenever you want.
49 simple_chain_lq:finalize(false);
50
51 -- Returns the number of outputs in addition to the live (0) and preview (1).
52 -- Called only once, at the start of the program.
53 function num_channels()
54         return 2;
55 end
56
57 -- Called every frame.
58 function get_transitions()
59         return {"Cut", "Fade", "Zoom!"};
60 end
61
62 function transition_clicked(num, t)
63         -- local temp = live_signal_num;
64         -- live_signal_num = preview_signal_num;
65         -- preview_signal_num = temp;
66
67         zoom_start = t;
68         zoom_end = t + 1.0;
69
70         local temp = zoom_src;
71         zoom_src = zoom_dst;
72         zoom_dst = temp;
73 end
74
75 function channel_clicked(num, t)
76         -- Presumably change the preview here.
77         io.write("STUB: channel_clicked\n");
78 end
79
80 -- Called every frame. Get the chain for displaying at input <num>,
81 -- where 0 is live, 1 is preview, 2 is the first channel to display
82 -- in the bottom bar, and so on up to num_channels()+1. t is the 
83 -- current time in seconds. width and height are the dimensions of
84 -- the output, although you can ignore them if you don't need them
85 -- (they're useful if you want to e.g. know what to resample by).
86 --
87 -- You should return two objects; the chain itself, and then a
88 -- function (taking no parameters) that is run just before rendering.
89 -- The function needs to call connect_signal on any inputs, so that
90 -- it gets updated video data for the given frame. (You are allowed
91 -- to switch which input your input is getting from between frames,
92 -- but not calling connect_signal results in undefined behavior.)
93 -- If you want to change any parameters in the chain, this is also
94 -- the right place.
95 --
96 -- NOTE: The chain returned must be finalized with the Y'CbCr flag
97 -- if and only if num==0.
98 function get_chain(num, t, width, height)
99         if num == 0 then  -- Live.
100                 if t > zoom_end and zoom_dst == 1.0 then
101                         -- Special case: Show only the single image on screen.
102                         prepare = function()
103                                 simple_chain_hq_input:connect_signal(live_signal_num);
104                         end
105                         return simple_chain_hq, prepare;
106                 end
107                 prepare = function()
108                         if t < zoom_start then
109                                 prepare_sbs_chain(zoom_src, width, height);
110                         elseif t > zoom_end then
111                                 prepare_sbs_chain(zoom_dst, width, height);
112                         else
113                                 local tt = (t - zoom_start) / (zoom_end - zoom_start);
114                                 -- Smooth it a bit.
115                                 tt = math.sin(tt * 3.14159265358 * 0.5)
116                                 prepare_sbs_chain(zoom_src + (zoom_dst - zoom_src) * tt, width, height);
117                         end
118                 end
119                 return main_chain, prepare;
120         end
121         if num == 1 then  -- Preview.
122                 prepare = function()
123                         simple_chain_lq_input:connect_signal(preview_signal_num);
124                 end
125                 return simple_chain_lq, prepare;
126         end
127         if num == 2 then
128                 prepare = function()
129                         simple_chain_lq_input:connect_signal(0);
130                 end
131                 return simple_chain_lq, prepare;
132         end
133         if num == 3 then
134                 prepare = function()
135                         simple_chain_lq_input:connect_signal(1);
136                 end
137                 return simple_chain_lq, prepare;
138         end
139 end
140
141 function place_rectangle(resample_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height)
142         local srcx0 = 0.0;
143         local srcx1 = 1.0;
144         local srcy0 = 0.0;
145         local srcy1 = 1.0;
146
147         -- Cull.
148         if x0 > screen_width or x1 < 0.0 or y0 > screen_height or y1 < 0.0 then
149                 resample_effect:set_int("width", 1);
150                 resample_effect:set_int("height", 1);
151                 resample_effect:set_float("zoom_x", screen_width);
152                 resample_effect:set_float("zoom_y", screen_height);
153                 padding_effect:set_int("left", screen_width + 100);
154                 padding_effect:set_int("top", screen_height + 100);
155                 return;
156         end
157
158         -- Clip. (TODO: Clip on upper/left sides, too.)
159         if x1 > screen_width then
160                 srcx1 = (screen_width - x0) / (x1 - x0);
161                 x1 = screen_width;
162         end
163         if y1 > screen_height then
164                 srcy1 = (screen_height - y0) / (y1 - y0);
165                 y1 = screen_height;
166         end
167
168         local x_subpixel_offset = x0 - math.floor(x0);
169         local y_subpixel_offset = y0 - math.floor(y0);
170
171         -- Resampling must be to an integral number of pixels. Round up,
172         -- and then add an extra pixel so we have some leeway for the border.
173         local width = math.ceil(x1 - x0) + 1;
174         local height = math.ceil(y1 - y0) + 1;
175         resample_effect:set_int("width", width);
176         resample_effect:set_int("height", height);
177
178         -- Correct the discrepancy with zoom. (This will leave a small
179         -- excess edge of pixels and subpixels, which we'll correct for soon.)
180         local zoom_x = (x1 - x0) / (width * (srcx1 - srcx0));
181         local zoom_y = (y1 - y0) / (height * (srcy1 - srcy0));
182         resample_effect:set_float("zoom_x", zoom_x);
183         resample_effect:set_float("zoom_y", zoom_y);
184         resample_effect:set_float("zoom_center_x", 0.0);
185         resample_effect:set_float("zoom_center_y", 0.0);
186
187         -- Padding must also be to a whole-pixel offset.
188         padding_effect:set_int("left", math.floor(x0));
189         padding_effect:set_int("top", math.floor(y0));
190
191         -- Correct _that_ discrepancy by subpixel offset in the resampling.
192         resample_effect:set_float("left", -x_subpixel_offset / zoom_x);
193         resample_effect:set_float("top", -y_subpixel_offset / zoom_y);
194
195         -- Finally, adjust the border so it is exactly where we want it.
196         padding_effect:set_float("border_offset_left", x_subpixel_offset);
197         padding_effect:set_float("border_offset_right", x1 - (math.floor(x0) + width));
198         padding_effect:set_float("border_offset_top", y_subpixel_offset);
199         padding_effect:set_float("border_offset_bottom", y1 - (math.floor(y0) + height));
200 end
201
202 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
203 function round(x)
204         return math.floor(x + 0.5)
205 end
206
207 function prepare_sbs_chain(t, screen_width, screen_height)
208         input0:connect_signal(live_signal_num);
209         input1:connect_signal(1);
210
211         -- First input is positioned (16,48) from top-left.
212         local width0 = round(848 * screen_width/1280.0);
213         local height0 = round(width0 * 9.0 / 16.0);
214
215         local top0 = 48 * screen_height/720.0;
216         local left0 = 16 * screen_width/1280.0;
217         local bottom0 = top0 + height0;
218         local right0 = left0 + width0;
219
220         -- Second input is positioned (16,48) from the bottom-right.
221         local width1 = 384 * screen_width/1280.0;
222         local height1 = 216 * screen_height/720.0;
223
224         local bottom1 = screen_height - 48 * screen_height/720.0;
225         local right1 = screen_width - 16 * screen_width/1280.0;
226         local top1 = bottom1 - height1;
227         local left1 = right1 - width1;
228
229         -- Interpolate between the fullscreen and side-by-side views.
230         local scale0 = 1.0 + t * (1280.0 / 848.0 - 1.0);
231         local tx0 = 0.0 + t * (-left0 * scale0);
232         local ty0 = 0.0 + t * (-top0 * scale0);
233
234         top0 = top0 * scale0 + ty0;
235         bottom0 = bottom0 * scale0 + ty0;
236         left0 = left0 * scale0 + tx0;
237         right0 = right0 * scale0 + tx0;
238
239         top1 = top1 * scale0 + ty0;
240         bottom1 = bottom1 * scale0 + ty0;
241         left1 = left1 * scale0 + tx0;
242         right1 = right1 * scale0 + tx0;
243         place_rectangle(resample_effect, padding_effect, left0, top0, right0, bottom0, screen_width, screen_height);
244         place_rectangle(resample2_effect, padding2_effect, left1, top1, right1, bottom1, screen_width, screen_height);
245 end