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>
16 #define WIDTH 1280 // FIXME
17 #define HEIGHT 720 // FIXME
20 using namespace movit;
24 vector<LiveInputWrapper *> live_inputs;
26 template<class T, class... Args>
27 int wrap_lua_object(lua_State* L, const char *class_name, Args&&... args)
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)...);
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);
40 Theme *get_theme_updata(lua_State* L)
42 luaL_checktype(L, lua_upvalueindex(1), LUA_TLIGHTUSERDATA);
43 return (Theme *)lua_touserdata(L, lua_upvalueindex(1));
46 Effect *get_effect(lua_State *L, int idx)
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);
53 fprintf(stderr, "Error: Index #%d was not an Effect type\n", idx);
57 bool checkbool(lua_State* L, int idx)
59 luaL_checktype(L, idx, LUA_TBOOLEAN);
60 return lua_toboolean(L, idx);
63 int EffectChain_new(lua_State* L)
65 assert(lua_gettop(L) == 2);
66 int aspect_w = luaL_checknumber(L, 1);
67 int aspect_h = luaL_checknumber(L, 2);
69 return wrap_lua_object<EffectChain>(L, "EffectChain", aspect_w, aspect_h);
72 int EffectChain_add_live_input(lua_State* L)
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);
80 int EffectChain_add_effect(lua_State* L)
82 assert(lua_gettop(L) == 3);
83 EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
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());
91 Effect *effect2 = get_effect(L, 3);
92 chain->add_effect(effect1, effect2);
99 int EffectChain_finalize(lua_State* L)
101 assert(lua_gettop(L) == 2);
102 EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
103 bool is_main_chain = checkbool(L, 2);
105 // Add outputs as needed.
106 ImageFormat inout_format;
107 inout_format.color_space = COLORSPACE_sRGB;
108 inout_format.gamma_curve = GAMMA_sRGB;
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;
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);
120 chain->add_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED);
126 int LiveInputWrapper_connect_signal(lua_State* L)
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);
135 int WhiteBalanceEffect_new(lua_State* L)
137 assert(lua_gettop(L) == 0);
138 return wrap_lua_object<WhiteBalanceEffect>(L, "WhiteBalanceEffect");
141 int ResampleEffect_new(lua_State* L)
143 assert(lua_gettop(L) == 0);
144 return wrap_lua_object<ResampleEffect>(L, "ResampleEffect");
147 int PaddingEffect_new(lua_State* L)
149 assert(lua_gettop(L) == 0);
150 return wrap_lua_object<PaddingEffect>(L, "PaddingEffect");
153 int Effect_set_float(lua_State *L)
155 assert(lua_gettop(L) == 3);
156 Effect *effect = (Effect *)get_effect(L, 1);
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);
165 int Effect_set_int(lua_State *L)
167 assert(lua_gettop(L) == 3);
168 Effect *effect = (Effect *)get_effect(L, 1);
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);
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 },
185 const luaL_Reg LiveInputWrapper_funcs[] = {
186 { "connect_signal", LiveInputWrapper_connect_signal },
190 const luaL_Reg WhiteBalanceEffect_funcs[] = {
191 { "new", WhiteBalanceEffect_new },
192 { "set_float", Effect_set_float },
193 { "set_int", Effect_set_int },
197 const luaL_Reg ResampleEffect_funcs[] = {
198 { "new", ResampleEffect_new },
199 { "set_float", Effect_set_float },
200 { "set_int", Effect_set_int },
204 const luaL_Reg PaddingEffect_funcs[] = {
205 { "new", PaddingEffect_new },
206 { "set_float", Effect_set_float },
207 { "set_int", Effect_set_int },
213 LiveInputWrapper::LiveInputWrapper(Theme *theme, EffectChain *chain)
216 ImageFormat inout_format;
217 inout_format.color_space = COLORSPACE_sRGB;
218 inout_format.gamma_curve = GAMMA_sRGB;
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;
230 input = new YCbCrInput(inout_format, input_ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
231 chain->add_input(input);
234 void LiveInputWrapper::connect_signal(int signal_num)
236 theme->connect_signal(input, signal_num);
239 Theme::Theme(const char *filename, ResourcePool *resource_pool)
240 : resource_pool(resource_pool)
245 printf("constructing, this=%p\n", this);
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);
255 if (luaL_dofile(L, filename)) {
256 fprintf(stderr, "error: %s\n", lua_tostring(L, -1));
260 assert(lua_gettop(L) == 0);
262 // Ask it for the number of channels.
263 lua_getglobal(L, "num_channels");
265 if (lua_pcall(L, 0, 1, 0) != 0) {
266 fprintf(stderr, "error running function `num_channels': %s", lua_tostring(L, -1));
270 num_channels = luaL_checknumber(L, 1);
273 void Theme::register_class(const char *class_name, const luaL_Reg *funcs)
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);
283 pair<EffectChain *, function<void()>>
284 Theme::get_chain(unsigned num, float t, unsigned width, unsigned height)
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);
293 if (lua_pcall(L, 4, 2, 0) != 0) {
294 fprintf(stderr, "error running function `get_chain': %s", lua_tostring(L, -1));
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");
303 lua_pushvalue(L, -1);
304 int funcref = luaL_ref(L, LUA_REGISTRYINDEX); // TODO: leak!
306 return make_pair(chain, [this, funcref]{
307 unique_lock<mutex> lock(m);
309 // Set up state, including connecting signals.
310 lua_rawgeti(L, LUA_REGISTRYINDEX, funcref);
311 lua_pcall(L, 0, 0, 0);
315 void Theme::connect_signal(YCbCrInput *input, int signal_num)
317 input->set_texture_num(0, input_textures[signal_num].tex_y);
318 input->set_texture_num(1, input_textures[signal_num].tex_cbcr);
321 void Theme::transition_clicked(int transition_num, float t)
323 unique_lock<mutex> lock(m);
324 lua_getglobal(L, "transition_clicked");
325 lua_pushnumber(L, transition_num);
326 lua_pushnumber(L, t);
328 if (lua_pcall(L, 2, 0, 0) != 0) {
329 fprintf(stderr, "error running function `transition_clicked': %s", lua_tostring(L, -1));