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