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 #include <movit/overlay_effect.h>
14 #include <movit/resize_effect.h>
15 #include <movit/mix_effect.h>
19 #define WIDTH 1280 // FIXME
20 #define HEIGHT 720 // FIXME
23 using namespace movit;
27 vector<LiveInputWrapper *> live_inputs;
29 template<class T, class... Args>
30 int wrap_lua_object(lua_State* L, const char *class_name, Args&&... args)
32 // Construct the C++ object and put it on the stack.
33 void *mem = lua_newuserdata(L, sizeof(T));
34 new(mem) T(std::forward<Args>(args)...);
36 // Look up the metatable named <class_name>, and set it on the new object.
37 luaL_getmetatable(L, class_name);
38 lua_setmetatable(L, -2);
43 Theme *get_theme_updata(lua_State* L)
45 luaL_checktype(L, lua_upvalueindex(1), LUA_TLIGHTUSERDATA);
46 return (Theme *)lua_touserdata(L, lua_upvalueindex(1));
49 Effect *get_effect(lua_State *L, int idx)
51 if (luaL_testudata(L, idx, "WhiteBalanceEffect") ||
52 luaL_testudata(L, idx, "ResampleEffect") ||
53 luaL_testudata(L, idx, "PaddingEffect") ||
54 luaL_testudata(L, idx, "IntegralPaddingEffect") ||
55 luaL_testudata(L, idx, "OverlayEffect") ||
56 luaL_testudata(L, idx, "ResizeEffect") ||
57 luaL_testudata(L, idx, "MixEffect")) {
58 return (Effect *)lua_touserdata(L, idx);
60 luaL_error(L, "Error: Index #%d was not an Effect type\n", idx);
64 bool checkbool(lua_State* L, int idx)
66 luaL_checktype(L, idx, LUA_TBOOLEAN);
67 return lua_toboolean(L, idx);
70 int EffectChain_new(lua_State* L)
72 assert(lua_gettop(L) == 2);
73 int aspect_w = luaL_checknumber(L, 1);
74 int aspect_h = luaL_checknumber(L, 2);
76 return wrap_lua_object<EffectChain>(L, "EffectChain", aspect_w, aspect_h);
79 int EffectChain_add_live_input(lua_State* L)
81 assert(lua_gettop(L) == 2);
82 Theme *theme = get_theme_updata(L);
83 EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
84 bool override_bounce = checkbool(L, 2);
85 return wrap_lua_object<LiveInputWrapper>(L, "LiveInputWrapper", theme, chain, override_bounce);
88 int EffectChain_add_effect(lua_State* L)
90 assert(lua_gettop(L) >= 2);
91 EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
93 // TODO: Better error reporting.
94 Effect *effect = get_effect(L, 2);
95 if (lua_gettop(L) == 2) {
96 chain->add_effect(effect);
98 vector<Effect *> inputs;
99 for (int idx = 3; idx <= lua_gettop(L); ++idx) {
100 if (luaL_testudata(L, idx, "LiveInputWrapper")) {
101 LiveInputWrapper *input = (LiveInputWrapper *)lua_touserdata(L, idx);
102 inputs.push_back(input->get_input());
104 inputs.push_back(get_effect(L, idx));
107 chain->add_effect(effect, inputs);
110 lua_settop(L, 2); // Return the effect itself.
112 // Make sure Lua doesn't garbage-collect it away.
113 lua_pushvalue(L, -1);
114 luaL_ref(L, LUA_REGISTRYINDEX); // TODO: leak?
119 int EffectChain_finalize(lua_State* L)
121 assert(lua_gettop(L) == 2);
122 EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
123 bool is_main_chain = checkbool(L, 2);
125 // Add outputs as needed.
126 // NOTE: If you change any details about the output format, you will need to
127 // also update what's given to the muxer (HTTPD::Mux constructor) and
128 // what's put in the H.264 stream (sps_rbsp()).
129 ImageFormat inout_format;
130 inout_format.color_space = COLORSPACE_REC_709;
132 // Output gamma is tricky. We should output Rec. 709 for TV, except that
133 // we expect to run with web players and others that don't really care and
134 // just output with no conversion. So that means we'll need to output sRGB,
135 // even though H.264 has no setting for that (we use “unspecified”).
136 inout_format.gamma_curve = GAMMA_sRGB;
139 YCbCrFormat output_ycbcr_format;
140 // We actually output 4:2:0 in the end, but chroma subsampling
141 // happens in a pass not run by Movit (see Mixer::subsample_chroma()).
142 output_ycbcr_format.chroma_subsampling_x = 1;
143 output_ycbcr_format.chroma_subsampling_y = 1;
145 // Rec. 709 would be the sane thing to do, but it seems many players
146 // (e.g. MPlayer and VLC) just default to BT.601 coefficients no matter
147 // what (see discussions in e.g. https://trac.ffmpeg.org/ticket/4978).
148 // We _do_ set the right flags, though, so that a player that works
149 // properly doesn't have to guess.
150 output_ycbcr_format.luma_coefficients = YCBCR_REC_601;
151 output_ycbcr_format.full_range = false;
152 output_ycbcr_format.num_levels = 256;
154 chain->add_ycbcr_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED, output_ycbcr_format, YCBCR_OUTPUT_SPLIT_Y_AND_CBCR);
155 chain->set_dither_bits(8);
156 chain->set_output_origin(OUTPUT_ORIGIN_TOP_LEFT);
158 chain->add_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED);
164 int LiveInputWrapper_connect_signal(lua_State* L)
166 assert(lua_gettop(L) == 2);
167 LiveInputWrapper *input = (LiveInputWrapper *)luaL_checkudata(L, 1, "LiveInputWrapper");
168 int signal_num = luaL_checknumber(L, 2);
169 input->connect_signal(signal_num);
173 int WhiteBalanceEffect_new(lua_State* L)
175 assert(lua_gettop(L) == 0);
176 return wrap_lua_object<WhiteBalanceEffect>(L, "WhiteBalanceEffect");
179 int ResampleEffect_new(lua_State* L)
181 assert(lua_gettop(L) == 0);
182 return wrap_lua_object<ResampleEffect>(L, "ResampleEffect");
185 int PaddingEffect_new(lua_State* L)
187 assert(lua_gettop(L) == 0);
188 return wrap_lua_object<PaddingEffect>(L, "PaddingEffect");
191 int IntegralPaddingEffect_new(lua_State* L)
193 assert(lua_gettop(L) == 0);
194 return wrap_lua_object<IntegralPaddingEffect>(L, "IntegralPaddingEffect");
197 int OverlayEffect_new(lua_State* L)
199 assert(lua_gettop(L) == 0);
200 return wrap_lua_object<OverlayEffect>(L, "OverlayEffect");
203 int ResizeEffect_new(lua_State* L)
205 assert(lua_gettop(L) == 0);
206 return wrap_lua_object<ResizeEffect>(L, "ResizeEffect");
209 int MixEffect_new(lua_State* L)
211 assert(lua_gettop(L) == 0);
212 return wrap_lua_object<MixEffect>(L, "MixEffect");
215 int Effect_set_float(lua_State *L)
217 assert(lua_gettop(L) == 3);
218 Effect *effect = (Effect *)get_effect(L, 1);
220 const char* cstr = lua_tolstring(L, 2, &len);
221 std::string key(cstr, len);
222 float value = luaL_checknumber(L, 3);
223 if (!effect->set_float(key, value)) {
224 luaL_error(L, "Effect refused set_float(\"%s\", %d) (invalid key?)", cstr, int(value));
229 int Effect_set_int(lua_State *L)
231 assert(lua_gettop(L) == 3);
232 Effect *effect = (Effect *)get_effect(L, 1);
234 const char* cstr = lua_tolstring(L, 2, &len);
235 std::string key(cstr, len);
236 float value = luaL_checknumber(L, 3);
237 if (!effect->set_int(key, value)) {
238 luaL_error(L, "Effect refused set_int(\"%s\", %d) (invalid key?)", cstr, int(value));
243 int Effect_set_vec4(lua_State *L)
245 assert(lua_gettop(L) == 6);
246 Effect *effect = (Effect *)get_effect(L, 1);
248 const char* cstr = lua_tolstring(L, 2, &len);
249 std::string key(cstr, len);
251 v[0] = luaL_checknumber(L, 3);
252 v[1] = luaL_checknumber(L, 4);
253 v[2] = luaL_checknumber(L, 5);
254 v[3] = luaL_checknumber(L, 6);
255 if (!effect->set_vec4(key, v)) {
256 luaL_error(L, "Effect refused set_vec4(\"%s\", %f, %f, %f, %f) (invalid key?)", cstr,
257 v[0], v[1], v[2], v[3]);
262 const luaL_Reg EffectChain_funcs[] = {
263 { "new", EffectChain_new },
264 { "add_live_input", EffectChain_add_live_input },
265 { "add_effect", EffectChain_add_effect },
266 { "finalize", EffectChain_finalize },
270 const luaL_Reg LiveInputWrapper_funcs[] = {
271 { "connect_signal", LiveInputWrapper_connect_signal },
275 const luaL_Reg WhiteBalanceEffect_funcs[] = {
276 { "new", WhiteBalanceEffect_new },
277 { "set_float", Effect_set_float },
278 { "set_int", Effect_set_int },
279 { "set_vec4", Effect_set_vec4 },
283 const luaL_Reg ResampleEffect_funcs[] = {
284 { "new", ResampleEffect_new },
285 { "set_float", Effect_set_float },
286 { "set_int", Effect_set_int },
287 { "set_vec4", Effect_set_vec4 },
291 const luaL_Reg PaddingEffect_funcs[] = {
292 { "new", PaddingEffect_new },
293 { "set_float", Effect_set_float },
294 { "set_int", Effect_set_int },
295 { "set_vec4", Effect_set_vec4 },
299 const luaL_Reg IntegralPaddingEffect_funcs[] = {
300 { "new", IntegralPaddingEffect_new },
301 { "set_float", Effect_set_float },
302 { "set_int", Effect_set_int },
303 { "set_vec4", Effect_set_vec4 },
307 const luaL_Reg OverlayEffect_funcs[] = {
308 { "new", OverlayEffect_new },
309 { "set_float", Effect_set_float },
310 { "set_int", Effect_set_int },
311 { "set_vec4", Effect_set_vec4 },
315 const luaL_Reg ResizeEffect_funcs[] = {
316 { "new", ResizeEffect_new },
317 { "set_float", Effect_set_float },
318 { "set_int", Effect_set_int },
319 { "set_vec4", Effect_set_vec4 },
323 const luaL_Reg MixEffect_funcs[] = {
324 { "new", MixEffect_new },
325 { "set_float", Effect_set_float },
326 { "set_int", Effect_set_int },
327 { "set_vec4", Effect_set_vec4 },
333 LiveInputWrapper::LiveInputWrapper(Theme *theme, EffectChain *chain, bool override_bounce)
336 ImageFormat inout_format;
337 inout_format.color_space = COLORSPACE_sRGB;
339 // Gamma curve depends on the input signal, and we don't really get any
340 // indications. A camera would be expected to do Rec. 709, but
341 // I haven't checked if any do in practice. However, computers _do_ output
342 // in sRGB gamma (ie., they don't convert from sRGB to Rec. 709), and
343 // I wouldn't really be surprised if most non-professional cameras do, too.
344 // So we pick sRGB as the least evil here.
345 inout_format.gamma_curve = GAMMA_sRGB;
347 // The Blackmagic driver docs claim that the device outputs Y'CbCr
348 // according to Rec. 601, but practical testing indicates it definitely
349 // is Rec. 709 (at least up to errors attributable to rounding errors).
350 // Perhaps 601 was only to indicate the subsampling positions, not the
351 // colorspace itself? Tested with a Lenovo X1 gen 3 as input.
352 YCbCrFormat input_ycbcr_format;
353 input_ycbcr_format.chroma_subsampling_x = 2;
354 input_ycbcr_format.chroma_subsampling_y = 1;
355 input_ycbcr_format.cb_x_position = 0.0;
356 input_ycbcr_format.cr_x_position = 0.0;
357 input_ycbcr_format.cb_y_position = 0.5;
358 input_ycbcr_format.cr_y_position = 0.5;
359 input_ycbcr_format.luma_coefficients = YCBCR_REC_709;
360 input_ycbcr_format.full_range = false;
362 if (override_bounce) {
363 input = new NonBouncingYCbCrInput(inout_format, input_ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
365 input = new YCbCrInput(inout_format, input_ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
367 chain->add_input(input);
370 void LiveInputWrapper::connect_signal(int signal_num)
372 theme->connect_signal(input, signal_num);
375 Theme::Theme(const char *filename, ResourcePool *resource_pool, unsigned num_cards)
376 : resource_pool(resource_pool), num_cards(num_cards)
381 register_class("EffectChain", EffectChain_funcs);
382 register_class("LiveInputWrapper", LiveInputWrapper_funcs);
383 register_class("WhiteBalanceEffect", WhiteBalanceEffect_funcs);
384 register_class("ResampleEffect", ResampleEffect_funcs);
385 register_class("PaddingEffect", PaddingEffect_funcs);
386 register_class("IntegralPaddingEffect", IntegralPaddingEffect_funcs);
387 register_class("OverlayEffect", OverlayEffect_funcs);
388 register_class("ResizeEffect", ResizeEffect_funcs);
389 register_class("MixEffect", MixEffect_funcs);
393 if (luaL_dofile(L, filename)) {
394 fprintf(stderr, "error: %s\n", lua_tostring(L, -1));
398 assert(lua_gettop(L) == 0);
400 // Ask it for the number of channels.
401 lua_getglobal(L, "num_channels");
403 if (lua_pcall(L, 0, 1, 0) != 0) {
404 fprintf(stderr, "error running function `num_channels': %s\n", lua_tostring(L, -1));
408 num_channels = luaL_checknumber(L, 1);
410 assert(lua_gettop(L) == 0);
413 void Theme::register_class(const char *class_name, const luaL_Reg *funcs)
415 assert(lua_gettop(L) == 0);
416 luaL_newmetatable(L, class_name);
417 lua_pushlightuserdata(L, this);
418 luaL_setfuncs(L, funcs, 1);
419 lua_pushvalue(L, -1);
420 lua_setfield(L, -2, "__index");
421 lua_setglobal(L, class_name);
422 assert(lua_gettop(L) == 0);
425 pair<EffectChain *, function<void()>>
426 Theme::get_chain(unsigned num, float t, unsigned width, unsigned height)
428 unique_lock<mutex> lock(m);
429 assert(lua_gettop(L) == 0);
430 lua_getglobal(L, "get_chain"); /* function to be called */
431 lua_pushnumber(L, num);
432 lua_pushnumber(L, t);
433 lua_pushnumber(L, width);
434 lua_pushnumber(L, height);
436 if (lua_pcall(L, 4, 2, 0) != 0) {
437 fprintf(stderr, "error running function `get_chain': %s\n", lua_tostring(L, -1));
441 EffectChain *chain = (EffectChain *)luaL_checkudata(L, -2, "EffectChain");
442 if (!lua_isfunction(L, -1)) {
443 fprintf(stderr, "Argument #-1 should be a function\n");
446 lua_pushvalue(L, -1);
447 int funcref = luaL_ref(L, LUA_REGISTRYINDEX); // TODO: leak!
449 assert(lua_gettop(L) == 0);
450 return make_pair(chain, [this, funcref]{
451 unique_lock<mutex> lock(m);
453 // Set up state, including connecting signals.
454 lua_rawgeti(L, LUA_REGISTRYINDEX, funcref);
455 if (lua_pcall(L, 0, 0, 0) != 0) {
456 fprintf(stderr, "error running chain setup callback: %s\n", lua_tostring(L, -1));
459 assert(lua_gettop(L) == 0);
463 std::vector<std::string> Theme::get_transition_names(float t)
465 unique_lock<mutex> lock(m);
466 lua_getglobal(L, "get_transitions");
467 lua_pushnumber(L, t);
468 if (lua_pcall(L, 1, 1, 0) != 0) {
469 fprintf(stderr, "error running function `get_transitions': %s\n", lua_tostring(L, -1));
473 std::vector<std::string> ret;
475 while (lua_next(L, -2) != 0) {
476 ret.push_back(lua_tostring(L, -1));
480 assert(lua_gettop(L) == 0);
484 void Theme::connect_signal(YCbCrInput *input, int signal_num)
486 if (signal_num >= int(num_cards)) {
487 fprintf(stderr, "WARNING: Theme asked for input %d, but we only have %u card(s).\n", signal_num, num_cards);
488 fprintf(stderr, "Mapping to card %d instead.\n", signal_num % num_cards);
489 signal_num %= num_cards;
491 input->set_texture_num(0, input_textures[signal_num].tex_y);
492 input->set_texture_num(1, input_textures[signal_num].tex_cbcr);
495 void Theme::transition_clicked(int transition_num, float t)
497 unique_lock<mutex> lock(m);
498 lua_getglobal(L, "transition_clicked");
499 lua_pushnumber(L, transition_num);
500 lua_pushnumber(L, t);
502 if (lua_pcall(L, 2, 0, 0) != 0) {
503 fprintf(stderr, "error running function `transition_clicked': %s\n", lua_tostring(L, -1));
506 assert(lua_gettop(L) == 0);
509 void Theme::channel_clicked(int preview_num)
511 unique_lock<mutex> lock(m);
512 lua_getglobal(L, "channel_clicked");
513 lua_pushnumber(L, preview_num);
515 if (lua_pcall(L, 1, 0, 0) != 0) {
516 fprintf(stderr, "error running function `channel_clicked': %s\n", lua_tostring(L, -1));
519 assert(lua_gettop(L) == 0);