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