7 #include <movit/effect.h>
8 #include <movit/effect_chain.h>
9 #include <movit/image_format.h>
10 #include <movit/mix_effect.h>
11 #include <movit/overlay_effect.h>
12 #include <movit/padding_effect.h>
13 #include <movit/resample_effect.h>
14 #include <movit/resize_effect.h>
15 #include <movit/white_balance_effect.h>
16 #include <movit/ycbcr.h>
17 #include <movit/ycbcr_input.h>
25 #include "image_input.h"
31 #define WIDTH 1280 // FIXME
32 #define HEIGHT 720 // FIXME
35 using namespace movit;
39 vector<LiveInputWrapper *> live_inputs;
41 class LuaRefWithDeleter {
43 LuaRefWithDeleter(mutex *m, lua_State *L, int ref) : m(m), L(L), ref(ref) {}
44 ~LuaRefWithDeleter() {
45 unique_lock<mutex> lock(*m);
46 luaL_unref(L, LUA_REGISTRYINDEX, ref);
48 int get() const { return ref; }
51 LuaRefWithDeleter(const LuaRefWithDeleter &) = delete;
58 template<class T, class... Args>
59 int wrap_lua_object(lua_State* L, const char *class_name, Args&&... args)
61 // Construct the C++ object and put it on the stack.
62 void *mem = lua_newuserdata(L, sizeof(T));
63 new(mem) T(std::forward<Args>(args)...);
65 // Look up the metatable named <class_name>, and set it on the new object.
66 luaL_getmetatable(L, class_name);
67 lua_setmetatable(L, -2);
72 Theme *get_theme_updata(lua_State* L)
74 luaL_checktype(L, lua_upvalueindex(1), LUA_TLIGHTUSERDATA);
75 return (Theme *)lua_touserdata(L, lua_upvalueindex(1));
78 Effect *get_effect(lua_State *L, int idx)
80 if (luaL_testudata(L, idx, "WhiteBalanceEffect") ||
81 luaL_testudata(L, idx, "ResampleEffect") ||
82 luaL_testudata(L, idx, "PaddingEffect") ||
83 luaL_testudata(L, idx, "IntegralPaddingEffect") ||
84 luaL_testudata(L, idx, "OverlayEffect") ||
85 luaL_testudata(L, idx, "ResizeEffect") ||
86 luaL_testudata(L, idx, "MixEffect") ||
87 luaL_testudata(L, idx, "ImageInput")) {
88 return (Effect *)lua_touserdata(L, idx);
90 luaL_error(L, "Error: Index #%d was not an Effect type\n", idx);
94 bool checkbool(lua_State* L, int idx)
96 luaL_checktype(L, idx, LUA_TBOOLEAN);
97 return lua_toboolean(L, idx);
100 std::string checkstdstring(lua_State *L, int index)
103 const char* cstr = lua_tolstring(L, index, &len);
104 return std::string(cstr, len);
107 int EffectChain_new(lua_State* L)
109 assert(lua_gettop(L) == 2);
110 int aspect_w = luaL_checknumber(L, 1);
111 int aspect_h = luaL_checknumber(L, 2);
113 return wrap_lua_object<EffectChain>(L, "EffectChain", aspect_w, aspect_h);
116 int EffectChain_add_live_input(lua_State* L)
118 assert(lua_gettop(L) == 2);
119 Theme *theme = get_theme_updata(L);
120 EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
121 bool override_bounce = checkbool(L, 2);
122 return wrap_lua_object<LiveInputWrapper>(L, "LiveInputWrapper", theme, chain, override_bounce);
125 int EffectChain_add_effect(lua_State* L)
127 assert(lua_gettop(L) >= 2);
128 EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
130 // TODO: Better error reporting.
131 Effect *effect = get_effect(L, 2);
132 if (lua_gettop(L) == 2) {
133 if (effect->num_inputs() == 0) {
134 chain->add_input((Input *)effect);
136 chain->add_effect(effect);
139 vector<Effect *> inputs;
140 for (int idx = 3; idx <= lua_gettop(L); ++idx) {
141 if (luaL_testudata(L, idx, "LiveInputWrapper")) {
142 LiveInputWrapper *input = (LiveInputWrapper *)lua_touserdata(L, idx);
143 inputs.push_back(input->get_input());
145 inputs.push_back(get_effect(L, idx));
148 chain->add_effect(effect, inputs);
151 lua_settop(L, 2); // Return the effect itself.
153 // Make sure Lua doesn't garbage-collect it away.
154 lua_pushvalue(L, -1);
155 luaL_ref(L, LUA_REGISTRYINDEX); // TODO: leak?
160 int EffectChain_finalize(lua_State* L)
162 assert(lua_gettop(L) == 2);
163 EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
164 bool is_main_chain = checkbool(L, 2);
166 // Add outputs as needed.
167 // NOTE: If you change any details about the output format, you will need to
168 // also update what's given to the muxer (HTTPD::Mux constructor) and
169 // what's put in the H.264 stream (sps_rbsp()).
170 ImageFormat inout_format;
171 inout_format.color_space = COLORSPACE_REC_709;
173 // Output gamma is tricky. We should output Rec. 709 for TV, except that
174 // we expect to run with web players and others that don't really care and
175 // just output with no conversion. So that means we'll need to output sRGB,
176 // even though H.264 has no setting for that (we use “unspecified”).
177 inout_format.gamma_curve = GAMMA_sRGB;
180 YCbCrFormat output_ycbcr_format;
181 // We actually output 4:2:0 in the end, but chroma subsampling
182 // happens in a pass not run by Movit (see Mixer::subsample_chroma()).
183 output_ycbcr_format.chroma_subsampling_x = 1;
184 output_ycbcr_format.chroma_subsampling_y = 1;
186 // Rec. 709 would be the sane thing to do, but it seems many players
187 // (e.g. MPlayer and VLC) just default to BT.601 coefficients no matter
188 // what (see discussions in e.g. https://trac.ffmpeg.org/ticket/4978).
189 // We _do_ set the right flags, though, so that a player that works
190 // properly doesn't have to guess.
191 output_ycbcr_format.luma_coefficients = YCBCR_REC_601;
192 output_ycbcr_format.full_range = false;
193 output_ycbcr_format.num_levels = 256;
195 chain->add_ycbcr_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED, output_ycbcr_format, YCBCR_OUTPUT_SPLIT_Y_AND_CBCR);
196 chain->set_dither_bits(8);
197 chain->set_output_origin(OUTPUT_ORIGIN_TOP_LEFT);
199 chain->add_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED);
205 int LiveInputWrapper_connect_signal(lua_State* L)
207 assert(lua_gettop(L) == 2);
208 LiveInputWrapper *input = (LiveInputWrapper *)luaL_checkudata(L, 1, "LiveInputWrapper");
209 int signal_num = luaL_checknumber(L, 2);
210 input->connect_signal(signal_num);
214 int ImageInput_new(lua_State* L)
216 assert(lua_gettop(L) == 1);
217 std::string filename = checkstdstring(L, 1);
218 return wrap_lua_object<ImageInput>(L, "ImageInput", filename);
221 int WhiteBalanceEffect_new(lua_State* L)
223 assert(lua_gettop(L) == 0);
224 return wrap_lua_object<WhiteBalanceEffect>(L, "WhiteBalanceEffect");
227 int ResampleEffect_new(lua_State* L)
229 assert(lua_gettop(L) == 0);
230 return wrap_lua_object<ResampleEffect>(L, "ResampleEffect");
233 int PaddingEffect_new(lua_State* L)
235 assert(lua_gettop(L) == 0);
236 return wrap_lua_object<PaddingEffect>(L, "PaddingEffect");
239 int IntegralPaddingEffect_new(lua_State* L)
241 assert(lua_gettop(L) == 0);
242 return wrap_lua_object<IntegralPaddingEffect>(L, "IntegralPaddingEffect");
245 int OverlayEffect_new(lua_State* L)
247 assert(lua_gettop(L) == 0);
248 return wrap_lua_object<OverlayEffect>(L, "OverlayEffect");
251 int ResizeEffect_new(lua_State* L)
253 assert(lua_gettop(L) == 0);
254 return wrap_lua_object<ResizeEffect>(L, "ResizeEffect");
257 int MixEffect_new(lua_State* L)
259 assert(lua_gettop(L) == 0);
260 return wrap_lua_object<MixEffect>(L, "MixEffect");
263 int Effect_set_float(lua_State *L)
265 assert(lua_gettop(L) == 3);
266 Effect *effect = (Effect *)get_effect(L, 1);
267 std::string key = checkstdstring(L, 2);
268 float value = luaL_checknumber(L, 3);
269 if (!effect->set_float(key, value)) {
270 luaL_error(L, "Effect refused set_float(\"%s\", %d) (invalid key?)", key.c_str(), int(value));
275 int Effect_set_int(lua_State *L)
277 assert(lua_gettop(L) == 3);
278 Effect *effect = (Effect *)get_effect(L, 1);
279 std::string key = checkstdstring(L, 2);
280 float value = luaL_checknumber(L, 3);
281 if (!effect->set_int(key, value)) {
282 luaL_error(L, "Effect refused set_int(\"%s\", %d) (invalid key?)", key.c_str(), int(value));
287 int Effect_set_vec3(lua_State *L)
289 assert(lua_gettop(L) == 5);
290 Effect *effect = (Effect *)get_effect(L, 1);
291 std::string key = checkstdstring(L, 2);
293 v[0] = luaL_checknumber(L, 3);
294 v[1] = luaL_checknumber(L, 4);
295 v[2] = luaL_checknumber(L, 5);
296 if (!effect->set_vec3(key, v)) {
297 luaL_error(L, "Effect refused set_vec3(\"%s\", %f, %f, %f) (invalid key?)", key.c_str(),
303 int Effect_set_vec4(lua_State *L)
305 assert(lua_gettop(L) == 6);
306 Effect *effect = (Effect *)get_effect(L, 1);
307 std::string key = checkstdstring(L, 2);
309 v[0] = luaL_checknumber(L, 3);
310 v[1] = luaL_checknumber(L, 4);
311 v[2] = luaL_checknumber(L, 5);
312 v[3] = luaL_checknumber(L, 6);
313 if (!effect->set_vec4(key, v)) {
314 luaL_error(L, "Effect refused set_vec4(\"%s\", %f, %f, %f, %f) (invalid key?)", key.c_str(),
315 v[0], v[1], v[2], v[3]);
320 const luaL_Reg EffectChain_funcs[] = {
321 { "new", EffectChain_new },
322 { "add_live_input", EffectChain_add_live_input },
323 { "add_effect", EffectChain_add_effect },
324 { "finalize", EffectChain_finalize },
328 const luaL_Reg LiveInputWrapper_funcs[] = {
329 { "connect_signal", LiveInputWrapper_connect_signal },
333 const luaL_Reg ImageInput_funcs[] = {
334 { "new", ImageInput_new },
335 { "set_float", Effect_set_float },
336 { "set_int", Effect_set_int },
337 { "set_vec3", Effect_set_vec3 },
338 { "set_vec4", Effect_set_vec4 },
342 const luaL_Reg WhiteBalanceEffect_funcs[] = {
343 { "new", WhiteBalanceEffect_new },
344 { "set_float", Effect_set_float },
345 { "set_int", Effect_set_int },
346 { "set_vec3", Effect_set_vec3 },
347 { "set_vec4", Effect_set_vec4 },
351 const luaL_Reg ResampleEffect_funcs[] = {
352 { "new", ResampleEffect_new },
353 { "set_float", Effect_set_float },
354 { "set_int", Effect_set_int },
355 { "set_vec3", Effect_set_vec3 },
356 { "set_vec4", Effect_set_vec4 },
360 const luaL_Reg PaddingEffect_funcs[] = {
361 { "new", PaddingEffect_new },
362 { "set_float", Effect_set_float },
363 { "set_int", Effect_set_int },
364 { "set_vec3", Effect_set_vec3 },
365 { "set_vec4", Effect_set_vec4 },
369 const luaL_Reg IntegralPaddingEffect_funcs[] = {
370 { "new", IntegralPaddingEffect_new },
371 { "set_float", Effect_set_float },
372 { "set_int", Effect_set_int },
373 { "set_vec3", Effect_set_vec3 },
374 { "set_vec4", Effect_set_vec4 },
378 const luaL_Reg OverlayEffect_funcs[] = {
379 { "new", OverlayEffect_new },
380 { "set_float", Effect_set_float },
381 { "set_int", Effect_set_int },
382 { "set_vec3", Effect_set_vec3 },
383 { "set_vec4", Effect_set_vec4 },
387 const luaL_Reg ResizeEffect_funcs[] = {
388 { "new", ResizeEffect_new },
389 { "set_float", Effect_set_float },
390 { "set_int", Effect_set_int },
391 { "set_vec3", Effect_set_vec3 },
392 { "set_vec4", Effect_set_vec4 },
396 const luaL_Reg MixEffect_funcs[] = {
397 { "new", MixEffect_new },
398 { "set_float", Effect_set_float },
399 { "set_int", Effect_set_int },
400 { "set_vec3", Effect_set_vec3 },
401 { "set_vec4", Effect_set_vec4 },
407 LiveInputWrapper::LiveInputWrapper(Theme *theme, EffectChain *chain, bool override_bounce)
410 ImageFormat inout_format;
411 inout_format.color_space = COLORSPACE_sRGB;
413 // Gamma curve depends on the input signal, and we don't really get any
414 // indications. A camera would be expected to do Rec. 709, but
415 // I haven't checked if any do in practice. However, computers _do_ output
416 // in sRGB gamma (ie., they don't convert from sRGB to Rec. 709), and
417 // I wouldn't really be surprised if most non-professional cameras do, too.
418 // So we pick sRGB as the least evil here.
419 inout_format.gamma_curve = GAMMA_sRGB;
421 // The Blackmagic driver docs claim that the device outputs Y'CbCr
422 // according to Rec. 601, but practical testing indicates it definitely
423 // is Rec. 709 (at least up to errors attributable to rounding errors).
424 // Perhaps 601 was only to indicate the subsampling positions, not the
425 // colorspace itself? Tested with a Lenovo X1 gen 3 as input.
426 YCbCrFormat input_ycbcr_format;
427 input_ycbcr_format.chroma_subsampling_x = 2;
428 input_ycbcr_format.chroma_subsampling_y = 1;
429 input_ycbcr_format.cb_x_position = 0.0;
430 input_ycbcr_format.cr_x_position = 0.0;
431 input_ycbcr_format.cb_y_position = 0.5;
432 input_ycbcr_format.cr_y_position = 0.5;
433 input_ycbcr_format.luma_coefficients = YCBCR_REC_709;
434 input_ycbcr_format.full_range = false;
436 if (override_bounce) {
437 input = new NonBouncingYCbCrInput(inout_format, input_ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
439 input = new YCbCrInput(inout_format, input_ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
441 chain->add_input(input);
444 void LiveInputWrapper::connect_signal(int signal_num)
446 theme->connect_signal(input, signal_num);
449 Theme::Theme(const char *filename, ResourcePool *resource_pool, unsigned num_cards)
450 : resource_pool(resource_pool), num_cards(num_cards)
455 register_class("EffectChain", EffectChain_funcs);
456 register_class("LiveInputWrapper", LiveInputWrapper_funcs);
457 register_class("ImageInput", ImageInput_funcs);
458 register_class("WhiteBalanceEffect", WhiteBalanceEffect_funcs);
459 register_class("ResampleEffect", ResampleEffect_funcs);
460 register_class("PaddingEffect", PaddingEffect_funcs);
461 register_class("IntegralPaddingEffect", IntegralPaddingEffect_funcs);
462 register_class("OverlayEffect", OverlayEffect_funcs);
463 register_class("ResizeEffect", ResizeEffect_funcs);
464 register_class("MixEffect", MixEffect_funcs);
468 if (luaL_dofile(L, filename)) {
469 fprintf(stderr, "error: %s\n", lua_tostring(L, -1));
473 assert(lua_gettop(L) == 0);
475 // Ask it for the number of channels.
476 lua_getglobal(L, "num_channels");
478 if (lua_pcall(L, 0, 1, 0) != 0) {
479 fprintf(stderr, "error running function `num_channels': %s\n", lua_tostring(L, -1));
483 num_channels = luaL_checknumber(L, 1);
485 assert(lua_gettop(L) == 0);
488 void Theme::register_class(const char *class_name, const luaL_Reg *funcs)
490 assert(lua_gettop(L) == 0);
491 luaL_newmetatable(L, class_name);
492 lua_pushlightuserdata(L, this);
493 luaL_setfuncs(L, funcs, 1);
494 lua_pushvalue(L, -1);
495 lua_setfield(L, -2, "__index");
496 lua_setglobal(L, class_name);
497 assert(lua_gettop(L) == 0);
500 pair<EffectChain *, function<void()>>
501 Theme::get_chain(unsigned num, float t, unsigned width, unsigned height)
503 unique_lock<mutex> lock(m);
504 assert(lua_gettop(L) == 0);
505 lua_getglobal(L, "get_chain"); /* function to be called */
506 lua_pushnumber(L, num);
507 lua_pushnumber(L, t);
508 lua_pushnumber(L, width);
509 lua_pushnumber(L, height);
511 if (lua_pcall(L, 4, 2, 0) != 0) {
512 fprintf(stderr, "error running function `get_chain': %s\n", lua_tostring(L, -1));
516 EffectChain *chain = (EffectChain *)luaL_checkudata(L, -2, "EffectChain");
517 if (!lua_isfunction(L, -1)) {
518 fprintf(stderr, "Argument #-1 should be a function\n");
521 lua_pushvalue(L, -1);
522 shared_ptr<LuaRefWithDeleter> funcref(new LuaRefWithDeleter(&m, L, luaL_ref(L, LUA_REGISTRYINDEX)));
524 assert(lua_gettop(L) == 0);
525 return make_pair(chain, [this, funcref]{
526 unique_lock<mutex> lock(m);
528 // Set up state, including connecting signals.
529 lua_rawgeti(L, LUA_REGISTRYINDEX, funcref->get());
530 if (lua_pcall(L, 0, 0, 0) != 0) {
531 fprintf(stderr, "error running chain setup callback: %s\n", lua_tostring(L, -1));
534 assert(lua_gettop(L) == 0);
538 std::string Theme::get_channel_name(unsigned channel)
540 unique_lock<mutex> lock(m);
541 lua_getglobal(L, "channel_name");
542 lua_pushnumber(L, channel);
543 if (lua_pcall(L, 1, 1, 0) != 0) {
544 fprintf(stderr, "error running function `channel_nam': %s\n", lua_tostring(L, -1));
548 std::string ret = lua_tostring(L, -1);
550 assert(lua_gettop(L) == 0);
554 bool Theme::get_supports_set_wb(unsigned channel)
556 unique_lock<mutex> lock(m);
557 lua_getglobal(L, "supports_set_wb");
558 lua_pushnumber(L, channel);
559 if (lua_pcall(L, 1, 1, 0) != 0) {
560 fprintf(stderr, "error running function `supports_set_wb': %s\n", lua_tostring(L, -1));
564 bool ret = checkbool(L, -1);
566 assert(lua_gettop(L) == 0);
570 void Theme::set_wb(unsigned channel, double r, double g, double b)
572 unique_lock<mutex> lock(m);
573 lua_getglobal(L, "set_wb");
574 lua_pushnumber(L, channel);
575 lua_pushnumber(L, r);
576 lua_pushnumber(L, g);
577 lua_pushnumber(L, b);
578 if (lua_pcall(L, 4, 0, 0) != 0) {
579 fprintf(stderr, "error running function `set_wb': %s\n", lua_tostring(L, -1));
583 assert(lua_gettop(L) == 0);
586 std::vector<std::string> Theme::get_transition_names(float t)
588 unique_lock<mutex> lock(m);
589 lua_getglobal(L, "get_transitions");
590 lua_pushnumber(L, t);
591 if (lua_pcall(L, 1, 1, 0) != 0) {
592 fprintf(stderr, "error running function `get_transitions': %s\n", lua_tostring(L, -1));
596 std::vector<std::string> ret;
598 while (lua_next(L, -2) != 0) {
599 ret.push_back(lua_tostring(L, -1));
603 assert(lua_gettop(L) == 0);
607 void Theme::connect_signal(YCbCrInput *input, int signal_num)
609 if (signal_num >= int(num_cards)) {
610 if (signals_warned_about.insert(signal_num).second) {
611 fprintf(stderr, "WARNING: Theme asked for input %d, but we only have %u card(s).\n", signal_num, num_cards);
612 fprintf(stderr, "Mapping to card %d instead.\n", signal_num % num_cards);
614 signal_num %= num_cards;
616 input->set_texture_num(0, input_textures[signal_num].tex_y);
617 input->set_texture_num(1, input_textures[signal_num].tex_cbcr);
620 void Theme::transition_clicked(int transition_num, float t)
622 unique_lock<mutex> lock(m);
623 lua_getglobal(L, "transition_clicked");
624 lua_pushnumber(L, transition_num);
625 lua_pushnumber(L, t);
627 if (lua_pcall(L, 2, 0, 0) != 0) {
628 fprintf(stderr, "error running function `transition_clicked': %s\n", lua_tostring(L, -1));
631 assert(lua_gettop(L) == 0);
634 void Theme::channel_clicked(int preview_num)
636 unique_lock<mutex> lock(m);
637 lua_getglobal(L, "channel_clicked");
638 lua_pushnumber(L, preview_num);
640 if (lua_pcall(L, 1, 0, 0) != 0) {
641 fprintf(stderr, "error running function `channel_clicked': %s\n", lua_tostring(L, -1));
644 assert(lua_gettop(L) == 0);