]> git.sesse.net Git - nageru/blob - theme.cpp
Ask the Lua script for number of channels.
[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
12 #include "theme.h"
13
14 #define WIDTH 1280  // FIXME
15 #define HEIGHT 720  // FIXME
16
17 using namespace std;
18 using namespace movit;
19
20 namespace {
21
22 vector<LiveInputWrapper *> live_inputs;
23
24 template<class T, class... Args>
25 int wrap_lua_object(lua_State* L, const char *class_name, Args&&... args)
26 {
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)...);
30
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);
34
35         return 1;
36 }
37
38 Theme *get_theme_updata(lua_State* L)
39 {       
40         luaL_checktype(L, lua_upvalueindex(1), LUA_TLIGHTUSERDATA);
41         return (Theme *)lua_touserdata(L, lua_upvalueindex(1));
42 }
43
44 bool checkbool(lua_State* L, int idx)
45 {
46         luaL_checktype(L, idx, LUA_TBOOLEAN);
47         return lua_toboolean(L, idx);
48 }
49
50 int EffectChain_new(lua_State* L)
51 {
52         assert(lua_gettop(L) == 2);
53         int aspect_w = luaL_checknumber(L, 1);
54         int aspect_h = luaL_checknumber(L, 2);
55
56         return wrap_lua_object<EffectChain>(L, "EffectChain", aspect_w, aspect_h);
57 }
58
59 int EffectChain_add_live_input(lua_State* L)
60 {
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);
65 }
66
67 int EffectChain_add_effect(lua_State* L)
68 {
69         assert(lua_gettop(L) == 3);
70         EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
71
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());
76
77         lua_settop(L, 2);       
78         return 1;
79 }
80
81 int EffectChain_finalize(lua_State* L)
82 {
83         assert(lua_gettop(L) == 2);
84         EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
85         bool is_main_chain = checkbool(L, 2);
86
87         // Add outputs as needed.
88         ImageFormat inout_format;
89         inout_format.color_space = COLORSPACE_sRGB;
90         inout_format.gamma_curve = GAMMA_sRGB;
91         if (is_main_chain) {
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;
97
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);
101         }
102         chain->add_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED);
103
104         chain->finalize();
105         return 0;
106 }
107
108 int LiveInputWrapper_connect_signal(lua_State* L)
109 {
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);
114         return 0;
115 }
116
117 int WhiteBalanceEffect_new(lua_State* L)
118 {
119         assert(lua_gettop(L) == 0);
120         return wrap_lua_object<WhiteBalanceEffect>(L, "WhiteBalanceEffect");
121 }
122
123 int WhiteBalanceEffect_set_float(lua_State *L)
124 {
125         assert(lua_gettop(L) == 3);
126         WhiteBalanceEffect *effect = (WhiteBalanceEffect *)luaL_checkudata(L, 1, "WhiteBalanceEffect");
127         size_t len;
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);
132         return 0;
133 }
134
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 },
140         { NULL, NULL }
141 };
142
143 const luaL_Reg LiveInputWrapper_funcs[] = {
144         { "connect_signal", LiveInputWrapper_connect_signal },
145         { NULL, NULL }
146 };
147
148 const luaL_Reg WhiteBalanceEffect_funcs[] = {
149         { "new", WhiteBalanceEffect_new },
150         { "set_float", WhiteBalanceEffect_set_float },
151         { NULL, NULL }
152 };
153
154 }  // namespace
155
156 LiveInputWrapper::LiveInputWrapper(Theme *theme, EffectChain *chain)
157         : theme(theme)
158 {
159         ImageFormat inout_format;
160         inout_format.color_space = COLORSPACE_sRGB;
161         inout_format.gamma_curve = GAMMA_sRGB;
162
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;
172
173         input = new YCbCrInput(inout_format, input_ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
174         chain->add_input(input);
175 }
176
177 void LiveInputWrapper::connect_signal(int signal_num)
178 {
179         theme->connect_signal(input, signal_num);
180 }
181
182 Theme::Theme(const char *filename, ResourcePool *resource_pool)
183         : resource_pool(resource_pool)
184 {
185         L = luaL_newstate();
186         luaL_openlibs(L);
187
188         printf("constructing, this=%p\n", this);
189        
190         register_class("EffectChain", EffectChain_funcs); 
191         register_class("LiveInputWrapper", LiveInputWrapper_funcs); 
192         register_class("WhiteBalanceEffect", WhiteBalanceEffect_funcs); 
193
194         // Run script.
195         lua_settop(L, 0);
196         if (luaL_dofile(L, filename)) {
197                 fprintf(stderr, "error: %s\n", lua_tostring(L, -1));
198                 lua_pop(L, 1);
199                 exit(1);
200         }
201         assert(lua_gettop(L) == 0);
202
203         // Ask it for the number of channels.
204         lua_getglobal(L, "num_channels");
205
206         if (lua_pcall(L, 0, 1, 0) != 0) {
207                 fprintf(stderr, "error running function `num_channels': %s", lua_tostring(L, -1));
208                 exit(1);
209         }
210
211         num_channels = luaL_checknumber(L, 1);
212 }
213
214 void Theme::register_class(const char *class_name, const luaL_Reg *funcs)
215 {
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);
222 }
223
224 pair<EffectChain *, function<void()>>
225 Theme::get_chain(unsigned num, float t, unsigned width, unsigned height)
226 {
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);
233
234         if (lua_pcall(L, 4, 2, 0) != 0) {
235                 fprintf(stderr, "error running function `get_chain': %s", lua_tostring(L, -1));
236                 exit(1);
237         }
238
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");
242                 exit(1);
243         }
244         lua_pushvalue(L, -1);
245         int funcref = luaL_ref(L, LUA_REGISTRYINDEX);  // TODO: leak!
246         lua_pop(L, 2);
247         return make_pair(chain, [this, funcref]{
248                 unique_lock<mutex> lock(m);
249
250                 // Set up state, including connecting signals.
251                 lua_rawgeti(L, LUA_REGISTRYINDEX, funcref);
252                 lua_pcall(L, 0, 0, 0);
253         });
254 }
255
256 void Theme::connect_signal(YCbCrInput *input, int signal_num)
257 {
258         input->set_texture_num(0, input_textures[signal_num].tex_y);
259         input->set_texture_num(1, input_textures[signal_num].tex_cbcr);
260 }
261
262 void Theme::transition_clicked(int transition_num, float t)
263 {
264         unique_lock<mutex> lock(m);
265         lua_getglobal(L, "transition_clicked");
266         lua_pushnumber(L, transition_num);
267         lua_pushnumber(L, t);
268
269         if (lua_pcall(L, 2, 0, 0) != 0) {
270                 fprintf(stderr, "error running function `transition_clicked': %s", lua_tostring(L, -1));
271                 exit(1);
272         }
273 }