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>
26 #include "image_input.h"
33 using namespace movit;
37 vector<LiveInputWrapper *> live_inputs;
39 class LuaRefWithDeleter {
41 LuaRefWithDeleter(mutex *m, lua_State *L, int ref) : m(m), L(L), ref(ref) {}
42 ~LuaRefWithDeleter() {
43 unique_lock<mutex> lock(*m);
44 luaL_unref(L, LUA_REGISTRYINDEX, ref);
46 int get() const { return ref; }
49 LuaRefWithDeleter(const LuaRefWithDeleter &) = delete;
56 template<class T, class... Args>
57 int wrap_lua_object(lua_State* L, const char *class_name, Args&&... args)
59 // Construct the C++ object and put it on the stack.
60 void *mem = lua_newuserdata(L, sizeof(T));
61 new(mem) T(std::forward<Args>(args)...);
63 // Look up the metatable named <class_name>, and set it on the new object.
64 luaL_getmetatable(L, class_name);
65 lua_setmetatable(L, -2);
70 Theme *get_theme_updata(lua_State* L)
72 luaL_checktype(L, lua_upvalueindex(1), LUA_TLIGHTUSERDATA);
73 return (Theme *)lua_touserdata(L, lua_upvalueindex(1));
76 Effect *get_effect(lua_State *L, int idx)
78 if (luaL_testudata(L, idx, "WhiteBalanceEffect") ||
79 luaL_testudata(L, idx, "ResampleEffect") ||
80 luaL_testudata(L, idx, "PaddingEffect") ||
81 luaL_testudata(L, idx, "IntegralPaddingEffect") ||
82 luaL_testudata(L, idx, "OverlayEffect") ||
83 luaL_testudata(L, idx, "ResizeEffect") ||
84 luaL_testudata(L, idx, "MixEffect") ||
85 luaL_testudata(L, idx, "ImageInput")) {
86 return (Effect *)lua_touserdata(L, idx);
88 luaL_error(L, "Error: Index #%d was not an Effect type\n", idx);
92 bool checkbool(lua_State* L, int idx)
94 luaL_checktype(L, idx, LUA_TBOOLEAN);
95 return lua_toboolean(L, idx);
98 std::string checkstdstring(lua_State *L, int index)
101 const char* cstr = lua_tolstring(L, index, &len);
102 return std::string(cstr, len);
105 int EffectChain_new(lua_State* L)
107 assert(lua_gettop(L) == 2);
108 int aspect_w = luaL_checknumber(L, 1);
109 int aspect_h = luaL_checknumber(L, 2);
111 return wrap_lua_object<EffectChain>(L, "EffectChain", aspect_w, aspect_h);
114 int EffectChain_add_live_input(lua_State* L)
116 assert(lua_gettop(L) == 2);
117 Theme *theme = get_theme_updata(L);
118 EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
119 bool override_bounce = checkbool(L, 2);
120 return wrap_lua_object<LiveInputWrapper>(L, "LiveInputWrapper", theme, chain, override_bounce);
123 int EffectChain_add_effect(lua_State* L)
125 assert(lua_gettop(L) >= 2);
126 EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
128 // TODO: Better error reporting.
129 Effect *effect = get_effect(L, 2);
130 if (lua_gettop(L) == 2) {
131 if (effect->num_inputs() == 0) {
132 chain->add_input((Input *)effect);
134 chain->add_effect(effect);
137 vector<Effect *> inputs;
138 for (int idx = 3; idx <= lua_gettop(L); ++idx) {
139 if (luaL_testudata(L, idx, "LiveInputWrapper")) {
140 LiveInputWrapper *input = (LiveInputWrapper *)lua_touserdata(L, idx);
141 inputs.push_back(input->get_input());
143 inputs.push_back(get_effect(L, idx));
146 chain->add_effect(effect, inputs);
149 lua_settop(L, 2); // Return the effect itself.
151 // Make sure Lua doesn't garbage-collect it away.
152 lua_pushvalue(L, -1);
153 luaL_ref(L, LUA_REGISTRYINDEX); // TODO: leak?
158 int EffectChain_finalize(lua_State* L)
160 assert(lua_gettop(L) == 2);
161 EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
162 bool is_main_chain = checkbool(L, 2);
164 // Add outputs as needed.
165 // NOTE: If you change any details about the output format, you will need to
166 // also update what's given to the muxer (HTTPD::Mux constructor) and
167 // what's put in the H.264 stream (sps_rbsp()).
168 ImageFormat inout_format;
169 inout_format.color_space = COLORSPACE_REC_709;
171 // Output gamma is tricky. We should output Rec. 709 for TV, except that
172 // we expect to run with web players and others that don't really care and
173 // just output with no conversion. So that means we'll need to output sRGB,
174 // even though H.264 has no setting for that (we use “unspecified”).
175 inout_format.gamma_curve = GAMMA_sRGB;
178 YCbCrFormat output_ycbcr_format;
179 // We actually output 4:2:0 in the end, but chroma subsampling
180 // happens in a pass not run by Movit (see Mixer::subsample_chroma()).
181 output_ycbcr_format.chroma_subsampling_x = 1;
182 output_ycbcr_format.chroma_subsampling_y = 1;
184 // Rec. 709 would be the sane thing to do, but it seems many players
185 // (e.g. MPlayer and VLC) just default to BT.601 coefficients no matter
186 // what (see discussions in e.g. https://trac.ffmpeg.org/ticket/4978).
187 // We _do_ set the right flags, though, so that a player that works
188 // properly doesn't have to guess.
189 output_ycbcr_format.luma_coefficients = YCBCR_REC_601;
190 output_ycbcr_format.full_range = false;
191 output_ycbcr_format.num_levels = 256;
193 chain->add_ycbcr_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED, output_ycbcr_format, YCBCR_OUTPUT_SPLIT_Y_AND_CBCR);
194 chain->set_dither_bits(8);
195 chain->set_output_origin(OUTPUT_ORIGIN_TOP_LEFT);
197 chain->add_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED);
203 int LiveInputWrapper_connect_signal(lua_State* L)
205 assert(lua_gettop(L) == 2);
206 LiveInputWrapper *input = (LiveInputWrapper *)luaL_checkudata(L, 1, "LiveInputWrapper");
207 int signal_num = luaL_checknumber(L, 2);
208 input->connect_signal(signal_num);
212 int ImageInput_new(lua_State* L)
214 assert(lua_gettop(L) == 1);
215 std::string filename = checkstdstring(L, 1);
216 return wrap_lua_object<ImageInput>(L, "ImageInput", filename);
219 int WhiteBalanceEffect_new(lua_State* L)
221 assert(lua_gettop(L) == 0);
222 return wrap_lua_object<WhiteBalanceEffect>(L, "WhiteBalanceEffect");
225 int ResampleEffect_new(lua_State* L)
227 assert(lua_gettop(L) == 0);
228 return wrap_lua_object<ResampleEffect>(L, "ResampleEffect");
231 int PaddingEffect_new(lua_State* L)
233 assert(lua_gettop(L) == 0);
234 return wrap_lua_object<PaddingEffect>(L, "PaddingEffect");
237 int IntegralPaddingEffect_new(lua_State* L)
239 assert(lua_gettop(L) == 0);
240 return wrap_lua_object<IntegralPaddingEffect>(L, "IntegralPaddingEffect");
243 int OverlayEffect_new(lua_State* L)
245 assert(lua_gettop(L) == 0);
246 return wrap_lua_object<OverlayEffect>(L, "OverlayEffect");
249 int ResizeEffect_new(lua_State* L)
251 assert(lua_gettop(L) == 0);
252 return wrap_lua_object<ResizeEffect>(L, "ResizeEffect");
255 int MixEffect_new(lua_State* L)
257 assert(lua_gettop(L) == 0);
258 return wrap_lua_object<MixEffect>(L, "MixEffect");
261 int Effect_set_float(lua_State *L)
263 assert(lua_gettop(L) == 3);
264 Effect *effect = (Effect *)get_effect(L, 1);
265 std::string key = checkstdstring(L, 2);
266 float value = luaL_checknumber(L, 3);
267 if (!effect->set_float(key, value)) {
268 luaL_error(L, "Effect refused set_float(\"%s\", %d) (invalid key?)", key.c_str(), int(value));
273 int Effect_set_int(lua_State *L)
275 assert(lua_gettop(L) == 3);
276 Effect *effect = (Effect *)get_effect(L, 1);
277 std::string key = checkstdstring(L, 2);
278 float value = luaL_checknumber(L, 3);
279 if (!effect->set_int(key, value)) {
280 luaL_error(L, "Effect refused set_int(\"%s\", %d) (invalid key?)", key.c_str(), int(value));
285 int Effect_set_vec3(lua_State *L)
287 assert(lua_gettop(L) == 5);
288 Effect *effect = (Effect *)get_effect(L, 1);
289 std::string key = checkstdstring(L, 2);
291 v[0] = luaL_checknumber(L, 3);
292 v[1] = luaL_checknumber(L, 4);
293 v[2] = luaL_checknumber(L, 5);
294 if (!effect->set_vec3(key, v)) {
295 luaL_error(L, "Effect refused set_vec3(\"%s\", %f, %f, %f) (invalid key?)", key.c_str(),
301 int Effect_set_vec4(lua_State *L)
303 assert(lua_gettop(L) == 6);
304 Effect *effect = (Effect *)get_effect(L, 1);
305 std::string key = checkstdstring(L, 2);
307 v[0] = luaL_checknumber(L, 3);
308 v[1] = luaL_checknumber(L, 4);
309 v[2] = luaL_checknumber(L, 5);
310 v[3] = luaL_checknumber(L, 6);
311 if (!effect->set_vec4(key, v)) {
312 luaL_error(L, "Effect refused set_vec4(\"%s\", %f, %f, %f, %f) (invalid key?)", key.c_str(),
313 v[0], v[1], v[2], v[3]);
318 const luaL_Reg EffectChain_funcs[] = {
319 { "new", EffectChain_new },
320 { "add_live_input", EffectChain_add_live_input },
321 { "add_effect", EffectChain_add_effect },
322 { "finalize", EffectChain_finalize },
326 const luaL_Reg LiveInputWrapper_funcs[] = {
327 { "connect_signal", LiveInputWrapper_connect_signal },
331 const luaL_Reg ImageInput_funcs[] = {
332 { "new", ImageInput_new },
333 { "set_float", Effect_set_float },
334 { "set_int", Effect_set_int },
335 { "set_vec3", Effect_set_vec3 },
336 { "set_vec4", Effect_set_vec4 },
340 const luaL_Reg WhiteBalanceEffect_funcs[] = {
341 { "new", WhiteBalanceEffect_new },
342 { "set_float", Effect_set_float },
343 { "set_int", Effect_set_int },
344 { "set_vec3", Effect_set_vec3 },
345 { "set_vec4", Effect_set_vec4 },
349 const luaL_Reg ResampleEffect_funcs[] = {
350 { "new", ResampleEffect_new },
351 { "set_float", Effect_set_float },
352 { "set_int", Effect_set_int },
353 { "set_vec3", Effect_set_vec3 },
354 { "set_vec4", Effect_set_vec4 },
358 const luaL_Reg PaddingEffect_funcs[] = {
359 { "new", PaddingEffect_new },
360 { "set_float", Effect_set_float },
361 { "set_int", Effect_set_int },
362 { "set_vec3", Effect_set_vec3 },
363 { "set_vec4", Effect_set_vec4 },
367 const luaL_Reg IntegralPaddingEffect_funcs[] = {
368 { "new", IntegralPaddingEffect_new },
369 { "set_float", Effect_set_float },
370 { "set_int", Effect_set_int },
371 { "set_vec3", Effect_set_vec3 },
372 { "set_vec4", Effect_set_vec4 },
376 const luaL_Reg OverlayEffect_funcs[] = {
377 { "new", OverlayEffect_new },
378 { "set_float", Effect_set_float },
379 { "set_int", Effect_set_int },
380 { "set_vec3", Effect_set_vec3 },
381 { "set_vec4", Effect_set_vec4 },
385 const luaL_Reg ResizeEffect_funcs[] = {
386 { "new", ResizeEffect_new },
387 { "set_float", Effect_set_float },
388 { "set_int", Effect_set_int },
389 { "set_vec3", Effect_set_vec3 },
390 { "set_vec4", Effect_set_vec4 },
394 const luaL_Reg MixEffect_funcs[] = {
395 { "new", MixEffect_new },
396 { "set_float", Effect_set_float },
397 { "set_int", Effect_set_int },
398 { "set_vec3", Effect_set_vec3 },
399 { "set_vec4", Effect_set_vec4 },
405 LiveInputWrapper::LiveInputWrapper(Theme *theme, EffectChain *chain, bool override_bounce)
408 ImageFormat inout_format;
409 inout_format.color_space = COLORSPACE_sRGB;
411 // Gamma curve depends on the input signal, and we don't really get any
412 // indications. A camera would be expected to do Rec. 709, but
413 // I haven't checked if any do in practice. However, computers _do_ output
414 // in sRGB gamma (ie., they don't convert from sRGB to Rec. 709), and
415 // I wouldn't really be surprised if most non-professional cameras do, too.
416 // So we pick sRGB as the least evil here.
417 inout_format.gamma_curve = GAMMA_sRGB;
419 // The Blackmagic driver docs claim that the device outputs Y'CbCr
420 // according to Rec. 601, but practical testing indicates it definitely
421 // is Rec. 709 (at least up to errors attributable to rounding errors).
422 // Perhaps 601 was only to indicate the subsampling positions, not the
423 // colorspace itself? Tested with a Lenovo X1 gen 3 as input.
424 YCbCrFormat input_ycbcr_format;
425 input_ycbcr_format.chroma_subsampling_x = 2;
426 input_ycbcr_format.chroma_subsampling_y = 1;
427 input_ycbcr_format.cb_x_position = 0.0;
428 input_ycbcr_format.cr_x_position = 0.0;
429 input_ycbcr_format.cb_y_position = 0.5;
430 input_ycbcr_format.cr_y_position = 0.5;
431 input_ycbcr_format.luma_coefficients = YCBCR_REC_709;
432 input_ycbcr_format.full_range = false;
434 if (override_bounce) {
435 input = new NonBouncingYCbCrInput(inout_format, input_ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
437 input = new YCbCrInput(inout_format, input_ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
439 chain->add_input(input);
442 void LiveInputWrapper::connect_signal(int signal_num)
444 theme->connect_signal(input, signal_num);
447 Theme::Theme(const char *filename, ResourcePool *resource_pool, unsigned num_cards)
448 : resource_pool(resource_pool), num_cards(num_cards)
453 register_class("EffectChain", EffectChain_funcs);
454 register_class("LiveInputWrapper", LiveInputWrapper_funcs);
455 register_class("ImageInput", ImageInput_funcs);
456 register_class("WhiteBalanceEffect", WhiteBalanceEffect_funcs);
457 register_class("ResampleEffect", ResampleEffect_funcs);
458 register_class("PaddingEffect", PaddingEffect_funcs);
459 register_class("IntegralPaddingEffect", IntegralPaddingEffect_funcs);
460 register_class("OverlayEffect", OverlayEffect_funcs);
461 register_class("ResizeEffect", ResizeEffect_funcs);
462 register_class("MixEffect", MixEffect_funcs);
466 if (luaL_dofile(L, filename)) {
467 fprintf(stderr, "error: %s\n", lua_tostring(L, -1));
471 assert(lua_gettop(L) == 0);
473 // Ask it for the number of channels.
474 lua_getglobal(L, "num_channels");
476 if (lua_pcall(L, 0, 1, 0) != 0) {
477 fprintf(stderr, "error running function `num_channels': %s\n", lua_tostring(L, -1));
481 num_channels = luaL_checknumber(L, 1);
483 assert(lua_gettop(L) == 0);
486 void Theme::register_class(const char *class_name, const luaL_Reg *funcs)
488 assert(lua_gettop(L) == 0);
489 luaL_newmetatable(L, class_name);
490 lua_pushlightuserdata(L, this);
491 luaL_setfuncs(L, funcs, 1);
492 lua_pushvalue(L, -1);
493 lua_setfield(L, -2, "__index");
494 lua_setglobal(L, class_name);
495 assert(lua_gettop(L) == 0);
498 pair<EffectChain *, function<void()>>
499 Theme::get_chain(unsigned num, float t, unsigned width, unsigned height)
501 unique_lock<mutex> lock(m);
502 assert(lua_gettop(L) == 0);
503 lua_getglobal(L, "get_chain"); /* function to be called */
504 lua_pushnumber(L, num);
505 lua_pushnumber(L, t);
506 lua_pushnumber(L, width);
507 lua_pushnumber(L, height);
509 if (lua_pcall(L, 4, 2, 0) != 0) {
510 fprintf(stderr, "error running function `get_chain': %s\n", lua_tostring(L, -1));
514 EffectChain *chain = (EffectChain *)luaL_checkudata(L, -2, "EffectChain");
515 if (!lua_isfunction(L, -1)) {
516 fprintf(stderr, "Argument #-1 should be a function\n");
519 lua_pushvalue(L, -1);
520 shared_ptr<LuaRefWithDeleter> funcref(new LuaRefWithDeleter(&m, L, luaL_ref(L, LUA_REGISTRYINDEX)));
522 assert(lua_gettop(L) == 0);
523 return make_pair(chain, [this, funcref]{
524 unique_lock<mutex> lock(m);
526 // Set up state, including connecting signals.
527 lua_rawgeti(L, LUA_REGISTRYINDEX, funcref->get());
528 if (lua_pcall(L, 0, 0, 0) != 0) {
529 fprintf(stderr, "error running chain setup callback: %s\n", lua_tostring(L, -1));
532 assert(lua_gettop(L) == 0);
536 std::string Theme::get_channel_name(unsigned channel)
538 unique_lock<mutex> lock(m);
539 lua_getglobal(L, "channel_name");
540 lua_pushnumber(L, channel);
541 if (lua_pcall(L, 1, 1, 0) != 0) {
542 fprintf(stderr, "error running function `channel_nam': %s\n", lua_tostring(L, -1));
546 std::string ret = lua_tostring(L, -1);
548 assert(lua_gettop(L) == 0);
552 bool Theme::get_supports_set_wb(unsigned channel)
554 unique_lock<mutex> lock(m);
555 lua_getglobal(L, "supports_set_wb");
556 lua_pushnumber(L, channel);
557 if (lua_pcall(L, 1, 1, 0) != 0) {
558 fprintf(stderr, "error running function `supports_set_wb': %s\n", lua_tostring(L, -1));
562 bool ret = checkbool(L, -1);
564 assert(lua_gettop(L) == 0);
568 void Theme::set_wb(unsigned channel, double r, double g, double b)
570 unique_lock<mutex> lock(m);
571 lua_getglobal(L, "set_wb");
572 lua_pushnumber(L, channel);
573 lua_pushnumber(L, r);
574 lua_pushnumber(L, g);
575 lua_pushnumber(L, b);
576 if (lua_pcall(L, 4, 0, 0) != 0) {
577 fprintf(stderr, "error running function `set_wb': %s\n", lua_tostring(L, -1));
581 assert(lua_gettop(L) == 0);
584 std::vector<std::string> Theme::get_transition_names(float t)
586 unique_lock<mutex> lock(m);
587 lua_getglobal(L, "get_transitions");
588 lua_pushnumber(L, t);
589 if (lua_pcall(L, 1, 1, 0) != 0) {
590 fprintf(stderr, "error running function `get_transitions': %s\n", lua_tostring(L, -1));
594 std::vector<std::string> ret;
596 while (lua_next(L, -2) != 0) {
597 ret.push_back(lua_tostring(L, -1));
601 assert(lua_gettop(L) == 0);
605 void Theme::connect_signal(YCbCrInput *input, int signal_num)
607 if (signal_num >= int(num_cards)) {
608 if (signals_warned_about.insert(signal_num).second) {
609 fprintf(stderr, "WARNING: Theme asked for input %d, but we only have %u card(s).\n", signal_num, num_cards);
610 fprintf(stderr, "Mapping to card %d instead.\n", signal_num % num_cards);
612 signal_num %= num_cards;
614 input->set_texture_num(0, input_textures[signal_num].tex_y);
615 input->set_texture_num(1, input_textures[signal_num].tex_cbcr);
618 void Theme::transition_clicked(int transition_num, float t)
620 unique_lock<mutex> lock(m);
621 lua_getglobal(L, "transition_clicked");
622 lua_pushnumber(L, transition_num);
623 lua_pushnumber(L, t);
625 if (lua_pcall(L, 2, 0, 0) != 0) {
626 fprintf(stderr, "error running function `transition_clicked': %s\n", lua_tostring(L, -1));
629 assert(lua_gettop(L) == 0);
632 void Theme::channel_clicked(int preview_num)
634 unique_lock<mutex> lock(m);
635 lua_getglobal(L, "channel_clicked");
636 lua_pushnumber(L, preview_num);
638 if (lua_pcall(L, 1, 0, 0) != 0) {
639 fprintf(stderr, "error running function `channel_clicked': %s\n", lua_tostring(L, -1));
642 assert(lua_gettop(L) == 0);