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