8 #include <movit/effect_chain.h>
9 #include <movit/ycbcr_input.h>
10 #include <movit/white_balance_effect.h>
14 #define WIDTH 1280 // FIXME
15 #define HEIGHT 720 // FIXME
18 using namespace movit;
22 vector<LiveInputWrapper *> live_inputs;
24 template<class T, class... Args>
25 int wrap_lua_object(lua_State* L, const char *class_name, Args&&... args)
27 // Construct the C++ object and put it on the stack.
28 void *mem = lua_newuserdata(L, sizeof(T));
29 new(mem) T(std::forward<Args>(args)...);
31 // Look up the metatable named <class_name>, and set it on the new object.
32 luaL_getmetatable(L, class_name);
33 lua_setmetatable(L, -2);
38 Theme *get_theme_updata(lua_State* L)
40 luaL_checktype(L, lua_upvalueindex(1), LUA_TLIGHTUSERDATA);
41 return (Theme *)lua_touserdata(L, lua_upvalueindex(1));
44 bool checkbool(lua_State* L, int idx)
46 luaL_checktype(L, idx, LUA_TBOOLEAN);
47 return lua_toboolean(L, idx);
50 int EffectChain_new(lua_State* L)
52 assert(lua_gettop(L) == 2);
53 int aspect_w = luaL_checknumber(L, 1);
54 int aspect_h = luaL_checknumber(L, 2);
56 return wrap_lua_object<EffectChain>(L, "EffectChain", aspect_w, aspect_h);
59 int EffectChain_add_live_input(lua_State* L)
61 assert(lua_gettop(L) == 1);
62 Theme *theme = get_theme_updata(L);
63 EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
64 return wrap_lua_object<LiveInputWrapper>(L, "LiveInputWrapper", theme, chain);
67 int EffectChain_add_effect(lua_State* L)
69 assert(lua_gettop(L) == 3);
70 EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
72 // FIXME: This needs a lot of work.
73 Effect *effect1 = (Effect *)luaL_checkudata(L, 2, "WhiteBalanceEffect");
74 LiveInputWrapper *effect2 = (LiveInputWrapper *)luaL_checkudata(L, 3, "LiveInputWrapper");
75 chain->add_effect(effect1, effect2->get_input());
81 int EffectChain_finalize(lua_State* L)
83 assert(lua_gettop(L) == 2);
84 EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
85 bool is_main_chain = checkbool(L, 2);
87 // Add outputs as needed.
88 ImageFormat inout_format;
89 inout_format.color_space = COLORSPACE_sRGB;
90 inout_format.gamma_curve = GAMMA_sRGB;
92 YCbCrFormat output_ycbcr_format;
93 output_ycbcr_format.chroma_subsampling_x = 1;
94 output_ycbcr_format.chroma_subsampling_y = 1;
95 output_ycbcr_format.luma_coefficients = YCBCR_REC_601;
96 output_ycbcr_format.full_range = false;
98 chain->add_ycbcr_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED, output_ycbcr_format, YCBCR_OUTPUT_SPLIT_Y_AND_CBCR);
99 chain->set_dither_bits(8);
100 chain->set_output_origin(OUTPUT_ORIGIN_TOP_LEFT);
102 chain->add_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED);
108 int LiveInputWrapper_connect_signal(lua_State* L)
110 assert(lua_gettop(L) == 2);
111 LiveInputWrapper *input = (LiveInputWrapper *)luaL_checkudata(L, 1, "LiveInputWrapper");
112 int signal_num = luaL_checknumber(L, 2);
113 input->connect_signal(signal_num);
117 int WhiteBalanceEffect_new(lua_State* L)
119 assert(lua_gettop(L) == 0);
120 return wrap_lua_object<WhiteBalanceEffect>(L, "WhiteBalanceEffect");
123 int WhiteBalanceEffect_set_float(lua_State *L)
125 assert(lua_gettop(L) == 3);
126 WhiteBalanceEffect *effect = (WhiteBalanceEffect *)luaL_checkudata(L, 1, "WhiteBalanceEffect");
128 const char* cstr = lua_tolstring(L, 2, &len);
129 std::string key(cstr, len);
130 float value = luaL_checknumber(L, 3);
131 (void)effect->set_float(key, value);
135 const luaL_Reg EffectChain_funcs[] = {
136 { "new", EffectChain_new },
137 { "add_live_input", EffectChain_add_live_input },
138 { "add_effect", EffectChain_add_effect },
139 { "finalize", EffectChain_finalize },
143 const luaL_Reg LiveInputWrapper_funcs[] = {
144 { "connect_signal", LiveInputWrapper_connect_signal },
148 const luaL_Reg WhiteBalanceEffect_funcs[] = {
149 { "new", WhiteBalanceEffect_new },
150 { "set_float", WhiteBalanceEffect_set_float },
156 LiveInputWrapper::LiveInputWrapper(Theme *theme, EffectChain *chain)
159 ImageFormat inout_format;
160 inout_format.color_space = COLORSPACE_sRGB;
161 inout_format.gamma_curve = GAMMA_sRGB;
163 YCbCrFormat input_ycbcr_format;
164 input_ycbcr_format.chroma_subsampling_x = 2;
165 input_ycbcr_format.chroma_subsampling_y = 1;
166 input_ycbcr_format.cb_x_position = 0.0;
167 input_ycbcr_format.cr_x_position = 0.0;
168 input_ycbcr_format.cb_y_position = 0.5;
169 input_ycbcr_format.cr_y_position = 0.5;
170 input_ycbcr_format.luma_coefficients = YCBCR_REC_601;
171 input_ycbcr_format.full_range = false;
173 input = new YCbCrInput(inout_format, input_ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
174 chain->add_input(input);
177 void LiveInputWrapper::connect_signal(int signal_num)
179 theme->connect_signal(input, signal_num);
182 Theme::Theme(const char *filename, ResourcePool *resource_pool)
183 : resource_pool(resource_pool)
188 printf("constructing, this=%p\n", this);
190 register_class("EffectChain", EffectChain_funcs);
191 register_class("LiveInputWrapper", LiveInputWrapper_funcs);
192 register_class("WhiteBalanceEffect", WhiteBalanceEffect_funcs);
196 if (luaL_dofile(L, filename)) {
197 fprintf(stderr, "error: %s\n", lua_tostring(L, -1));
201 assert(lua_gettop(L) == 0);
203 // Ask it for the number of channels.
204 lua_getglobal(L, "num_channels");
206 if (lua_pcall(L, 0, 1, 0) != 0) {
207 fprintf(stderr, "error running function `num_channels': %s", lua_tostring(L, -1));
211 num_channels = luaL_checknumber(L, 1);
214 void Theme::register_class(const char *class_name, const luaL_Reg *funcs)
216 luaL_newmetatable(L, class_name);
217 lua_pushlightuserdata(L, this);
218 luaL_setfuncs(L, funcs, 1);
219 lua_pushvalue(L, -1);
220 lua_setfield(L, -2, "__index");
221 lua_setglobal(L, class_name);
224 pair<EffectChain *, function<void()>>
225 Theme::get_chain(unsigned num, float t, unsigned width, unsigned height)
227 unique_lock<mutex> lock(m);
228 lua_getglobal(L, "get_chain"); /* function to be called */
229 lua_pushnumber(L, num);
230 lua_pushnumber(L, t);
231 lua_pushnumber(L, width);
232 lua_pushnumber(L, height);
234 if (lua_pcall(L, 4, 2, 0) != 0) {
235 fprintf(stderr, "error running function `get_chain': %s", lua_tostring(L, -1));
239 EffectChain *chain = (EffectChain *)luaL_checkudata(L, -2, "EffectChain");
240 if (!lua_isfunction(L, -1)) {
241 fprintf(stderr, "Argument #-1 should be a function\n");
244 lua_pushvalue(L, -1);
245 int funcref = luaL_ref(L, LUA_REGISTRYINDEX); // TODO: leak!
247 return make_pair(chain, [this, funcref]{
248 unique_lock<mutex> lock(m);
250 // Set up state, including connecting signals.
251 lua_rawgeti(L, LUA_REGISTRYINDEX, funcref);
252 lua_pcall(L, 0, 0, 0);
256 void Theme::connect_signal(YCbCrInput *input, int signal_num)
258 input->set_texture_num(0, input_textures[signal_num].tex_y);
259 input->set_texture_num(1, input_textures[signal_num].tex_cbcr);
262 void Theme::transition_clicked(int transition_num, float t)
264 unique_lock<mutex> lock(m);
265 lua_getglobal(L, "transition_clicked");
266 lua_pushnumber(L, transition_num);
267 lua_pushnumber(L, t);
269 if (lua_pcall(L, 2, 0, 0) != 0) {
270 fprintf(stderr, "error running function `transition_clicked': %s", lua_tostring(L, -1));