]> git.sesse.net Git - nageru/blob - theme.lua
Even more Lua SBS cleanups/generalizations.
[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 live_signal_num = 0;
12 local preview_signal_num = 1;
13
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);
23
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());
30
31 main_chain:add_effect(OverlayEffect.new(), padding_effect, padding2_effect);
32 main_chain:finalize(true);
33
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);
39
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()
43         return 2;
44 end
45
46 -- Called every frame.
47 function get_transitions()
48         return {"Cut", "Fade", "Zoom!"};
49 end
50
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;
56 end
57
58 function channel_clicked(num, t)
59         -- Presumably change the preview here.
60         io.write("STUB: channel_clicked\n");
61 end
62
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).
69 --
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
77 -- the right place.
78 --
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.
83                 prepare = function()
84                         prepare_sbs_chain(t, width, height);
85                 end
86                 return main_chain, prepare;
87         end
88         if num == 1 then  -- Preview.
89                 prepare = function()
90                         simple_chain_input:connect_signal(preview_signal_num);
91                 end
92                 return simple_chain, prepare;
93         end
94         if num == 2 then
95                 prepare = function()
96                         simple_chain_input:connect_signal(0);
97                 end
98                 return simple_chain, prepare;
99         end
100         if num == 3 then
101                 prepare = function()
102                         simple_chain_input:connect_signal(1);
103                 end
104                 return simple_chain, prepare;
105         end
106 end
107
108 function place_rectangle(resample_effect, padding_effect, x0, y0, x1, y1, screen_width, screen_height)
109         local srcx0 = 0.0;
110         local srcx1 = 1.0;
111         local srcy0 = 0.0;
112         local srcy1 = 1.0;
113
114         -- Cull.
115         if x0 > screen_width or x1 < 0.0 or y0 > screen_height 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", screen_width);
119                 resample_effect:set_float("zoom_y", screen_height);
120                 padding_effect:set_int("left", screen_width + 100);
121                 padding_effect:set_int("top", screen_height + 100);
122                 return;
123         end
124
125         -- Clip. (TODO: Clip on upper/left sides, too.)
126         if x1 > screen_width then
127                 srcx1 = (screen_width - x0) / (x1 - x0);
128                 x1 = screen_width;
129         end
130         if y1 > screen_height then
131                 srcy1 = (screen_height - y0) / (y1 - y0);
132                 y1 = screen_height;
133         end
134
135         local x_subpixel_offset = x0 - math.floor(x0);
136         local y_subpixel_offset = y0 - math.floor(y0);
137
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);
144
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);
153
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));
157
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);
161
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));
167 end
168
169 -- This is broken, of course (even for positive numbers), but Lua doesn't give us access to real rounding.
170 function round(x)
171         return math.floor(x + 0.5)
172 end
173
174 function prepare_sbs_chain(t, screen_width, screen_height)
175         input0:connect_signal(live_signal_num);
176         input1:connect_signal(1);
177
178         -- First input is positioned (16,48) from top-left.
179         local width0 = round(848 * screen_width/1280.0);
180         local height0 = round(width0 * 9.0 / 16.0);
181
182         local top0 = 48 * screen_height/720.0;
183         local left0 = 16 * screen_width/1280.0;
184         local bottom0 = top0 + height0;
185         local right0 = left0 + width0;
186
187         -- Second input is positioned (16,48) from the bottom-right.
188         local width1 = 384 * screen_width/1280.0;
189         local height1 = 216 * screen_height/720.0;
190
191         local bottom1 = screen_height - 48 * screen_height/720.0;
192         local right1 = screen_width - 16 * screen_width/1280.0;
193         local top1 = bottom1 - height1;
194         local left1 = right1 - width1;
195
196         -- Interpolate between the fullscreen and side-by-side views.
197         local sub_t = 0.5 + 0.5 * math.cos(t * 1.0);
198         local scale0 = 1.0 + sub_t * (1280.0 / 848.0 - 1.0);
199         local tx0 = 0.0 + sub_t * (-left0 * scale0);
200         local ty0 = 0.0 + sub_t * (-top0 * scale0);
201
202         top0 = top0 * scale0 + ty0;
203         bottom0 = bottom0 * scale0 + ty0;
204         left0 = left0 * scale0 + tx0;
205         right0 = right0 * scale0 + tx0;
206
207         top1 = top1 * scale0 + ty0;
208         bottom1 = bottom1 * scale0 + ty0;
209         left1 = left1 * scale0 + tx0;
210         right1 = right1 * scale0 + tx0;
211         place_rectangle(resample_effect, padding_effect, left0, top0, right0, bottom0, screen_width, screen_height);
212         place_rectangle(resample2_effect, padding2_effect, left1, top1, right1, bottom1, screen_width, screen_height);
213 end