]> git.sesse.net Git - nageru/blob - theme.cpp
Hook over OverlayEffect, and make add_effect() take a variable number of parameters.
[nageru] / theme.cpp
1 #include <stdio.h>
2 #include <lua.h>
3 #include <lualib.h>
4 #include <lauxlib.h>
5 #include <new>
6 #include <utility>
7
8 #include <movit/effect_chain.h>
9 #include <movit/ycbcr_input.h>
10 #include <movit/white_balance_effect.h>
11 #include <movit/resample_effect.h>
12 #include <movit/padding_effect.h>
13 #include <movit/overlay_effect.h>
14
15 #include "theme.h"
16
17 #define WIDTH 1280  // FIXME
18 #define HEIGHT 720  // FIXME
19
20 using namespace std;
21 using namespace movit;
22
23 namespace {
24
25 vector<LiveInputWrapper *> live_inputs;
26
27 template<class T, class... Args>
28 int wrap_lua_object(lua_State* L, const char *class_name, Args&&... args)
29 {
30         // Construct the C++ object and put it on the stack.
31         void *mem = lua_newuserdata(L, sizeof(T));
32         new(mem) T(std::forward<Args>(args)...);
33
34         // Look up the metatable named <class_name>, and set it on the new object.
35         luaL_getmetatable(L, class_name);
36         lua_setmetatable(L, -2);
37
38         return 1;
39 }
40
41 Theme *get_theme_updata(lua_State* L)
42 {       
43         luaL_checktype(L, lua_upvalueindex(1), LUA_TLIGHTUSERDATA);
44         return (Theme *)lua_touserdata(L, lua_upvalueindex(1));
45 }
46
47 Effect *get_effect(lua_State *L, int idx)
48 {
49         if (luaL_testudata(L, idx, "WhiteBalanceEffect") ||
50             luaL_testudata(L, idx, "ResampleEffect") ||
51             luaL_testudata(L, idx, "PaddingEffect") ||
52             luaL_testudata(L, idx, "IntegralPaddingEffect") ||
53             luaL_testudata(L, idx, "OverlayEffect")) {
54                 return (Effect *)lua_touserdata(L, idx);
55         }
56         fprintf(stderr, "Error: Index #%d was not an Effect type\n", idx);
57         exit(1);
58 }
59
60 bool checkbool(lua_State* L, int idx)
61 {
62         luaL_checktype(L, idx, LUA_TBOOLEAN);
63         return lua_toboolean(L, idx);
64 }
65
66 int EffectChain_new(lua_State* L)
67 {
68         assert(lua_gettop(L) == 2);
69         int aspect_w = luaL_checknumber(L, 1);
70         int aspect_h = luaL_checknumber(L, 2);
71
72         return wrap_lua_object<EffectChain>(L, "EffectChain", aspect_w, aspect_h);
73 }
74
75 int EffectChain_add_live_input(lua_State* L)
76 {
77         assert(lua_gettop(L) == 1);
78         Theme *theme = get_theme_updata(L);
79         EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
80         return wrap_lua_object<LiveInputWrapper>(L, "LiveInputWrapper", theme, chain);
81 }
82
83 int EffectChain_add_effect(lua_State* L)
84 {
85         assert(lua_gettop(L) >= 2);
86         EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
87
88         // TODO: Better error reporting.
89         Effect *effect = get_effect(L, 2);
90         vector<Effect *> inputs;
91         for (int idx = 3; idx <= lua_gettop(L); ++idx) {
92                 if (luaL_testudata(L, idx, "LiveInputWrapper")) {
93                         LiveInputWrapper *input = (LiveInputWrapper *)lua_touserdata(L, idx);
94                         inputs.push_back(input->get_input());
95                 } else {
96                         inputs.push_back(get_effect(L, idx));
97                 }
98         }
99         chain->add_effect(effect, inputs);
100
101         lua_settop(L, 2);  // Return the effect itself.
102         return 1;
103 }
104
105 int EffectChain_finalize(lua_State* L)
106 {
107         assert(lua_gettop(L) == 2);
108         EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
109         bool is_main_chain = checkbool(L, 2);
110
111         // Add outputs as needed.
112         ImageFormat inout_format;
113         inout_format.color_space = COLORSPACE_sRGB;
114         inout_format.gamma_curve = GAMMA_sRGB;
115         if (is_main_chain) {
116                 YCbCrFormat output_ycbcr_format;
117                 output_ycbcr_format.chroma_subsampling_x = 1;
118                 output_ycbcr_format.chroma_subsampling_y = 1;
119                 output_ycbcr_format.luma_coefficients = YCBCR_REC_601;
120                 output_ycbcr_format.full_range = false;
121
122                 chain->add_ycbcr_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED, output_ycbcr_format, YCBCR_OUTPUT_SPLIT_Y_AND_CBCR);
123                 chain->set_dither_bits(8);
124                 chain->set_output_origin(OUTPUT_ORIGIN_TOP_LEFT);
125         }
126         chain->add_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED);
127
128         chain->finalize();
129         return 0;
130 }
131
132 int LiveInputWrapper_connect_signal(lua_State* L)
133 {
134         assert(lua_gettop(L) == 2);
135         LiveInputWrapper *input = (LiveInputWrapper *)luaL_checkudata(L, 1, "LiveInputWrapper");
136         int signal_num = luaL_checknumber(L, 2);
137         input->connect_signal(signal_num);
138         return 0;
139 }
140
141 int WhiteBalanceEffect_new(lua_State* L)
142 {
143         assert(lua_gettop(L) == 0);
144         return wrap_lua_object<WhiteBalanceEffect>(L, "WhiteBalanceEffect");
145 }
146
147 int ResampleEffect_new(lua_State* L)
148 {
149         assert(lua_gettop(L) == 0);
150         return wrap_lua_object<ResampleEffect>(L, "ResampleEffect");
151 }
152
153 int PaddingEffect_new(lua_State* L)
154 {
155         assert(lua_gettop(L) == 0);
156         return wrap_lua_object<PaddingEffect>(L, "PaddingEffect");
157 }
158
159 int IntegralPaddingEffect_new(lua_State* L)
160 {
161         assert(lua_gettop(L) == 0);
162         return wrap_lua_object<IntegralPaddingEffect>(L, "IntegralPaddingEffect");
163 }
164
165 int OverlayEffect_new(lua_State* L)
166 {
167         assert(lua_gettop(L) == 0);
168         return wrap_lua_object<OverlayEffect>(L, "OverlayEffect");
169 }
170
171 int Effect_set_float(lua_State *L)
172 {
173         assert(lua_gettop(L) == 3);
174         Effect *effect = (Effect *)get_effect(L, 1);
175         size_t len;
176         const char* cstr = lua_tolstring(L, 2, &len);
177         std::string key(cstr, len);
178         float value = luaL_checknumber(L, 3);
179         (void)effect->set_float(key, value);
180         return 0;
181 }
182
183 int Effect_set_int(lua_State *L)
184 {
185         assert(lua_gettop(L) == 3);
186         Effect *effect = (Effect *)get_effect(L, 1);
187         size_t len;
188         const char* cstr = lua_tolstring(L, 2, &len);
189         std::string key(cstr, len);
190         float value = luaL_checknumber(L, 3);
191         (void)effect->set_int(key, value);
192         return 0;
193 }
194
195 const luaL_Reg EffectChain_funcs[] = {
196         { "new", EffectChain_new },
197         { "add_live_input", EffectChain_add_live_input },
198         { "add_effect", EffectChain_add_effect },
199         { "finalize", EffectChain_finalize },
200         { NULL, NULL }
201 };
202
203 const luaL_Reg LiveInputWrapper_funcs[] = {
204         { "connect_signal", LiveInputWrapper_connect_signal },
205         { NULL, NULL }
206 };
207
208 const luaL_Reg WhiteBalanceEffect_funcs[] = {
209         { "new", WhiteBalanceEffect_new },
210         { "set_float", Effect_set_float },
211         { "set_int", Effect_set_int },
212         { NULL, NULL }
213 };
214
215 const luaL_Reg ResampleEffect_funcs[] = {
216         { "new", ResampleEffect_new },
217         { "set_float", Effect_set_float },
218         { "set_int", Effect_set_int },
219         { NULL, NULL }
220 };
221
222 const luaL_Reg PaddingEffect_funcs[] = {
223         { "new", PaddingEffect_new },
224         { "set_float", Effect_set_float },
225         { "set_int", Effect_set_int },
226         { NULL, NULL }
227 };
228
229 const luaL_Reg IntegralPaddingEffect_funcs[] = {
230         { "new", IntegralPaddingEffect_new },
231         { "set_float", Effect_set_float },
232         { "set_int", Effect_set_int },
233         { NULL, NULL }
234 };
235
236 const luaL_Reg OverlayEffect_funcs[] = {
237         { "new", OverlayEffect_new },
238         { "set_float", Effect_set_float },
239         { "set_int", Effect_set_int },
240         { NULL, NULL }
241 };
242
243 }  // namespace
244
245 LiveInputWrapper::LiveInputWrapper(Theme *theme, EffectChain *chain)
246         : theme(theme)
247 {
248         ImageFormat inout_format;
249         inout_format.color_space = COLORSPACE_sRGB;
250         inout_format.gamma_curve = GAMMA_sRGB;
251
252         YCbCrFormat input_ycbcr_format;
253         input_ycbcr_format.chroma_subsampling_x = 2;
254         input_ycbcr_format.chroma_subsampling_y = 1;
255         input_ycbcr_format.cb_x_position = 0.0;
256         input_ycbcr_format.cr_x_position = 0.0;
257         input_ycbcr_format.cb_y_position = 0.5;
258         input_ycbcr_format.cr_y_position = 0.5;
259         input_ycbcr_format.luma_coefficients = YCBCR_REC_601;
260         input_ycbcr_format.full_range = false;
261
262         input = new YCbCrInput(inout_format, input_ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
263         chain->add_input(input);
264 }
265
266 void LiveInputWrapper::connect_signal(int signal_num)
267 {
268         theme->connect_signal(input, signal_num);
269 }
270
271 Theme::Theme(const char *filename, ResourcePool *resource_pool)
272         : resource_pool(resource_pool)
273 {
274         L = luaL_newstate();
275         luaL_openlibs(L);
276
277         printf("constructing, this=%p\n", this);
278        
279         register_class("EffectChain", EffectChain_funcs); 
280         register_class("LiveInputWrapper", LiveInputWrapper_funcs); 
281         register_class("WhiteBalanceEffect", WhiteBalanceEffect_funcs);
282         register_class("ResampleEffect", ResampleEffect_funcs);
283         register_class("PaddingEffect", PaddingEffect_funcs);
284         register_class("IntegralPaddingEffect", IntegralPaddingEffect_funcs);
285         register_class("OverlayEffect", OverlayEffect_funcs);
286
287         // Run script.
288         lua_settop(L, 0);
289         if (luaL_dofile(L, filename)) {
290                 fprintf(stderr, "error: %s\n", lua_tostring(L, -1));
291                 lua_pop(L, 1);
292                 exit(1);
293         }
294         assert(lua_gettop(L) == 0);
295
296         // Ask it for the number of channels.
297         lua_getglobal(L, "num_channels");
298
299         if (lua_pcall(L, 0, 1, 0) != 0) {
300                 fprintf(stderr, "error running function `num_channels': %s", lua_tostring(L, -1));
301                 exit(1);
302         }
303
304         num_channels = luaL_checknumber(L, 1);
305 }
306
307 void Theme::register_class(const char *class_name, const luaL_Reg *funcs)
308 {
309         luaL_newmetatable(L, class_name);
310         lua_pushlightuserdata(L, this);
311         luaL_setfuncs(L, funcs, 1);
312         lua_pushvalue(L, -1);
313         lua_setfield(L, -2, "__index");
314         lua_setglobal(L, class_name);
315 }
316
317 pair<EffectChain *, function<void()>>
318 Theme::get_chain(unsigned num, float t, unsigned width, unsigned height)
319 {
320         unique_lock<mutex> lock(m);
321         lua_getglobal(L, "get_chain");  /* function to be called */
322         lua_pushnumber(L, num);
323         lua_pushnumber(L, t);
324         lua_pushnumber(L, width);
325         lua_pushnumber(L, height);
326
327         if (lua_pcall(L, 4, 2, 0) != 0) {
328                 fprintf(stderr, "error running function `get_chain': %s", lua_tostring(L, -1));
329                 exit(1);
330         }
331
332         EffectChain *chain = (EffectChain *)luaL_checkudata(L, -2, "EffectChain");
333         if (!lua_isfunction(L, -1)) {
334                 fprintf(stderr, "Argument #-1 should be a function\n");
335                 exit(1);
336         }
337         lua_pushvalue(L, -1);
338         int funcref = luaL_ref(L, LUA_REGISTRYINDEX);  // TODO: leak!
339         lua_pop(L, 2);
340         return make_pair(chain, [this, funcref]{
341                 unique_lock<mutex> lock(m);
342
343                 // Set up state, including connecting signals.
344                 lua_rawgeti(L, LUA_REGISTRYINDEX, funcref);
345                 lua_pcall(L, 0, 0, 0);
346         });
347 }
348
349 void Theme::connect_signal(YCbCrInput *input, int signal_num)
350 {
351         input->set_texture_num(0, input_textures[signal_num].tex_y);
352         input->set_texture_num(1, input_textures[signal_num].tex_cbcr);
353 }
354
355 void Theme::transition_clicked(int transition_num, float t)
356 {
357         unique_lock<mutex> lock(m);
358         lua_getglobal(L, "transition_clicked");
359         lua_pushnumber(L, transition_num);
360         lua_pushnumber(L, t);
361
362         if (lua_pcall(L, 2, 0, 0) != 0) {
363                 fprintf(stderr, "error running function `transition_clicked': %s", lua_tostring(L, -1));
364                 exit(1);
365         }
366 }