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"
34 using namespace movit;
36 extern Mixer *global_mixer;
40 vector<LiveInputWrapper *> live_inputs;
42 class LuaRefWithDeleter {
44 LuaRefWithDeleter(mutex *m, lua_State *L, int ref) : m(m), L(L), ref(ref) {}
45 ~LuaRefWithDeleter() {
46 unique_lock<mutex> lock(*m);
47 luaL_unref(L, LUA_REGISTRYINDEX, ref);
49 int get() const { return ref; }
52 LuaRefWithDeleter(const LuaRefWithDeleter &) = delete;
59 template<class T, class... Args>
60 int wrap_lua_object(lua_State* L, const char *class_name, Args&&... args)
62 // Construct the C++ object and put it on the stack.
63 void *mem = lua_newuserdata(L, sizeof(T));
64 new(mem) T(std::forward<Args>(args)...);
66 // Look up the metatable named <class_name>, and set it on the new object.
67 luaL_getmetatable(L, class_name);
68 lua_setmetatable(L, -2);
73 Theme *get_theme_updata(lua_State* L)
75 luaL_checktype(L, lua_upvalueindex(1), LUA_TLIGHTUSERDATA);
76 return (Theme *)lua_touserdata(L, lua_upvalueindex(1));
79 Effect *get_effect(lua_State *L, int idx)
81 if (luaL_testudata(L, idx, "WhiteBalanceEffect") ||
82 luaL_testudata(L, idx, "ResampleEffect") ||
83 luaL_testudata(L, idx, "PaddingEffect") ||
84 luaL_testudata(L, idx, "IntegralPaddingEffect") ||
85 luaL_testudata(L, idx, "OverlayEffect") ||
86 luaL_testudata(L, idx, "ResizeEffect") ||
87 luaL_testudata(L, idx, "MixEffect") ||
88 luaL_testudata(L, idx, "ImageInput")) {
89 return (Effect *)lua_touserdata(L, idx);
91 luaL_error(L, "Error: Index #%d was not an Effect type\n", idx);
95 bool checkbool(lua_State* L, int idx)
97 luaL_checktype(L, idx, LUA_TBOOLEAN);
98 return lua_toboolean(L, idx);
101 std::string checkstdstring(lua_State *L, int index)
104 const char* cstr = lua_tolstring(L, index, &len);
105 return std::string(cstr, len);
108 int EffectChain_new(lua_State* L)
110 assert(lua_gettop(L) == 2);
111 int aspect_w = luaL_checknumber(L, 1);
112 int aspect_h = luaL_checknumber(L, 2);
114 return wrap_lua_object<EffectChain>(L, "EffectChain", aspect_w, aspect_h);
117 int EffectChain_add_live_input(lua_State* L)
119 assert(lua_gettop(L) == 2);
120 Theme *theme = get_theme_updata(L);
121 EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
122 bool override_bounce = checkbool(L, 2);
123 return wrap_lua_object<LiveInputWrapper>(L, "LiveInputWrapper", theme, chain, override_bounce);
126 int EffectChain_add_effect(lua_State* L)
128 assert(lua_gettop(L) >= 2);
129 EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
131 // TODO: Better error reporting.
132 Effect *effect = get_effect(L, 2);
133 if (lua_gettop(L) == 2) {
134 if (effect->num_inputs() == 0) {
135 chain->add_input((Input *)effect);
137 chain->add_effect(effect);
140 vector<Effect *> inputs;
141 for (int idx = 3; idx <= lua_gettop(L); ++idx) {
142 if (luaL_testudata(L, idx, "LiveInputWrapper")) {
143 LiveInputWrapper *input = (LiveInputWrapper *)lua_touserdata(L, idx);
144 inputs.push_back(input->get_input());
146 inputs.push_back(get_effect(L, idx));
149 chain->add_effect(effect, inputs);
152 lua_settop(L, 2); // Return the effect itself.
154 // Make sure Lua doesn't garbage-collect it away.
155 lua_pushvalue(L, -1);
156 luaL_ref(L, LUA_REGISTRYINDEX); // TODO: leak?
161 int EffectChain_finalize(lua_State* L)
163 assert(lua_gettop(L) == 2);
164 EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
165 bool is_main_chain = checkbool(L, 2);
167 // Add outputs as needed.
168 // NOTE: If you change any details about the output format, you will need to
169 // also update what's given to the muxer (HTTPD::Mux constructor) and
170 // what's put in the H.264 stream (sps_rbsp()).
171 ImageFormat inout_format;
172 inout_format.color_space = COLORSPACE_REC_709;
174 // Output gamma is tricky. We should output Rec. 709 for TV, except that
175 // we expect to run with web players and others that don't really care and
176 // just output with no conversion. So that means we'll need to output sRGB,
177 // even though H.264 has no setting for that (we use “unspecified”).
178 inout_format.gamma_curve = GAMMA_sRGB;
181 YCbCrFormat output_ycbcr_format;
182 // We actually output 4:2:0 in the end, but chroma subsampling
183 // happens in a pass not run by Movit (see Mixer::subsample_chroma()).
184 output_ycbcr_format.chroma_subsampling_x = 1;
185 output_ycbcr_format.chroma_subsampling_y = 1;
187 // Rec. 709 would be the sane thing to do, but it seems many players
188 // (e.g. MPlayer and VLC) just default to BT.601 coefficients no matter
189 // what (see discussions in e.g. https://trac.ffmpeg.org/ticket/4978).
190 // We _do_ set the right flags, though, so that a player that works
191 // properly doesn't have to guess.
192 output_ycbcr_format.luma_coefficients = YCBCR_REC_601;
193 output_ycbcr_format.full_range = false;
194 output_ycbcr_format.num_levels = 256;
196 chain->add_ycbcr_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED, output_ycbcr_format, YCBCR_OUTPUT_SPLIT_Y_AND_CBCR);
197 chain->set_dither_bits(8);
198 chain->set_output_origin(OUTPUT_ORIGIN_TOP_LEFT);
200 chain->add_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED);
206 int LiveInputWrapper_connect_signal(lua_State* L)
208 assert(lua_gettop(L) == 2);
209 LiveInputWrapper *input = (LiveInputWrapper *)luaL_checkudata(L, 1, "LiveInputWrapper");
210 int signal_num = luaL_checknumber(L, 2);
211 input->connect_signal(signal_num);
215 int ImageInput_new(lua_State* L)
217 assert(lua_gettop(L) == 1);
218 std::string filename = checkstdstring(L, 1);
219 return wrap_lua_object<ImageInput>(L, "ImageInput", filename);
222 int WhiteBalanceEffect_new(lua_State* L)
224 assert(lua_gettop(L) == 0);
225 return wrap_lua_object<WhiteBalanceEffect>(L, "WhiteBalanceEffect");
228 int ResampleEffect_new(lua_State* L)
230 assert(lua_gettop(L) == 0);
231 return wrap_lua_object<ResampleEffect>(L, "ResampleEffect");
234 int PaddingEffect_new(lua_State* L)
236 assert(lua_gettop(L) == 0);
237 return wrap_lua_object<PaddingEffect>(L, "PaddingEffect");
240 int IntegralPaddingEffect_new(lua_State* L)
242 assert(lua_gettop(L) == 0);
243 return wrap_lua_object<IntegralPaddingEffect>(L, "IntegralPaddingEffect");
246 int OverlayEffect_new(lua_State* L)
248 assert(lua_gettop(L) == 0);
249 return wrap_lua_object<OverlayEffect>(L, "OverlayEffect");
252 int ResizeEffect_new(lua_State* L)
254 assert(lua_gettop(L) == 0);
255 return wrap_lua_object<ResizeEffect>(L, "ResizeEffect");
258 int MixEffect_new(lua_State* L)
260 assert(lua_gettop(L) == 0);
261 return wrap_lua_object<MixEffect>(L, "MixEffect");
264 int Effect_set_float(lua_State *L)
266 assert(lua_gettop(L) == 3);
267 Effect *effect = (Effect *)get_effect(L, 1);
268 std::string key = checkstdstring(L, 2);
269 float value = luaL_checknumber(L, 3);
270 if (!effect->set_float(key, value)) {
271 luaL_error(L, "Effect refused set_float(\"%s\", %d) (invalid key?)", key.c_str(), int(value));
276 int Effect_set_int(lua_State *L)
278 assert(lua_gettop(L) == 3);
279 Effect *effect = (Effect *)get_effect(L, 1);
280 std::string key = checkstdstring(L, 2);
281 float value = luaL_checknumber(L, 3);
282 if (!effect->set_int(key, value)) {
283 luaL_error(L, "Effect refused set_int(\"%s\", %d) (invalid key?)", key.c_str(), int(value));
288 int Effect_set_vec3(lua_State *L)
290 assert(lua_gettop(L) == 5);
291 Effect *effect = (Effect *)get_effect(L, 1);
292 std::string key = checkstdstring(L, 2);
294 v[0] = luaL_checknumber(L, 3);
295 v[1] = luaL_checknumber(L, 4);
296 v[2] = luaL_checknumber(L, 5);
297 if (!effect->set_vec3(key, v)) {
298 luaL_error(L, "Effect refused set_vec3(\"%s\", %f, %f, %f) (invalid key?)", key.c_str(),
304 int Effect_set_vec4(lua_State *L)
306 assert(lua_gettop(L) == 6);
307 Effect *effect = (Effect *)get_effect(L, 1);
308 std::string key = checkstdstring(L, 2);
310 v[0] = luaL_checknumber(L, 3);
311 v[1] = luaL_checknumber(L, 4);
312 v[2] = luaL_checknumber(L, 5);
313 v[3] = luaL_checknumber(L, 6);
314 if (!effect->set_vec4(key, v)) {
315 luaL_error(L, "Effect refused set_vec4(\"%s\", %f, %f, %f, %f) (invalid key?)", key.c_str(),
316 v[0], v[1], v[2], v[3]);
321 const luaL_Reg EffectChain_funcs[] = {
322 { "new", EffectChain_new },
323 { "add_live_input", EffectChain_add_live_input },
324 { "add_effect", EffectChain_add_effect },
325 { "finalize", EffectChain_finalize },
329 const luaL_Reg LiveInputWrapper_funcs[] = {
330 { "connect_signal", LiveInputWrapper_connect_signal },
334 const luaL_Reg ImageInput_funcs[] = {
335 { "new", ImageInput_new },
336 { "set_float", Effect_set_float },
337 { "set_int", Effect_set_int },
338 { "set_vec3", Effect_set_vec3 },
339 { "set_vec4", Effect_set_vec4 },
343 const luaL_Reg WhiteBalanceEffect_funcs[] = {
344 { "new", WhiteBalanceEffect_new },
345 { "set_float", Effect_set_float },
346 { "set_int", Effect_set_int },
347 { "set_vec3", Effect_set_vec3 },
348 { "set_vec4", Effect_set_vec4 },
352 const luaL_Reg ResampleEffect_funcs[] = {
353 { "new", ResampleEffect_new },
354 { "set_float", Effect_set_float },
355 { "set_int", Effect_set_int },
356 { "set_vec3", Effect_set_vec3 },
357 { "set_vec4", Effect_set_vec4 },
361 const luaL_Reg PaddingEffect_funcs[] = {
362 { "new", PaddingEffect_new },
363 { "set_float", Effect_set_float },
364 { "set_int", Effect_set_int },
365 { "set_vec3", Effect_set_vec3 },
366 { "set_vec4", Effect_set_vec4 },
370 const luaL_Reg IntegralPaddingEffect_funcs[] = {
371 { "new", IntegralPaddingEffect_new },
372 { "set_float", Effect_set_float },
373 { "set_int", Effect_set_int },
374 { "set_vec3", Effect_set_vec3 },
375 { "set_vec4", Effect_set_vec4 },
379 const luaL_Reg OverlayEffect_funcs[] = {
380 { "new", OverlayEffect_new },
381 { "set_float", Effect_set_float },
382 { "set_int", Effect_set_int },
383 { "set_vec3", Effect_set_vec3 },
384 { "set_vec4", Effect_set_vec4 },
388 const luaL_Reg ResizeEffect_funcs[] = {
389 { "new", ResizeEffect_new },
390 { "set_float", Effect_set_float },
391 { "set_int", Effect_set_int },
392 { "set_vec3", Effect_set_vec3 },
393 { "set_vec4", Effect_set_vec4 },
397 const luaL_Reg MixEffect_funcs[] = {
398 { "new", MixEffect_new },
399 { "set_float", Effect_set_float },
400 { "set_int", Effect_set_int },
401 { "set_vec3", Effect_set_vec3 },
402 { "set_vec4", Effect_set_vec4 },
408 LiveInputWrapper::LiveInputWrapper(Theme *theme, EffectChain *chain, bool override_bounce)
411 ImageFormat inout_format;
412 inout_format.color_space = COLORSPACE_sRGB;
414 // Gamma curve depends on the input signal, and we don't really get any
415 // indications. A camera would be expected to do Rec. 709, but
416 // I haven't checked if any do in practice. However, computers _do_ output
417 // in sRGB gamma (ie., they don't convert from sRGB to Rec. 709), and
418 // I wouldn't really be surprised if most non-professional cameras do, too.
419 // So we pick sRGB as the least evil here.
420 inout_format.gamma_curve = GAMMA_sRGB;
422 // The Blackmagic driver docs claim that the device outputs Y'CbCr
423 // according to Rec. 601, but practical testing indicates it definitely
424 // is Rec. 709 (at least up to errors attributable to rounding errors).
425 // Perhaps 601 was only to indicate the subsampling positions, not the
426 // colorspace itself? Tested with a Lenovo X1 gen 3 as input.
427 YCbCrFormat input_ycbcr_format;
428 input_ycbcr_format.chroma_subsampling_x = 2;
429 input_ycbcr_format.chroma_subsampling_y = 1;
430 input_ycbcr_format.cb_x_position = 0.0;
431 input_ycbcr_format.cr_x_position = 0.0;
432 input_ycbcr_format.cb_y_position = 0.5;
433 input_ycbcr_format.cr_y_position = 0.5;
434 input_ycbcr_format.luma_coefficients = YCBCR_REC_709;
435 input_ycbcr_format.full_range = false;
437 if (override_bounce) {
438 input = new NonBouncingYCbCrInput(inout_format, input_ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
440 input = new YCbCrInput(inout_format, input_ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
442 chain->add_input(input);
445 void LiveInputWrapper::connect_signal(int signal_num)
447 if (global_mixer == nullptr) {
451 signal_num = theme->map_signal(signal_num);
453 Mixer::BufferedFrame frame = global_mixer->get_buffered_frame(signal_num, 0);
454 const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
456 input->set_texture_num(0, userdata->tex_y[frame.field_number]);
457 input->set_texture_num(1, userdata->tex_cbcr[frame.field_number]);
458 input->set_width(userdata->last_width[frame.field_number]);
459 input->set_height(userdata->last_height[frame.field_number]);
461 // Hold on to the refcount so that we don't release the input frame
462 // until we're done rendering from it.
463 if (theme->used_input_frames_collector != nullptr) {
464 theme->used_input_frames_collector->push_back(frame.frame);
468 Theme::Theme(const char *filename, ResourcePool *resource_pool, unsigned num_cards)
469 : resource_pool(resource_pool), num_cards(num_cards)
474 register_class("EffectChain", EffectChain_funcs);
475 register_class("LiveInputWrapper", LiveInputWrapper_funcs);
476 register_class("ImageInput", ImageInput_funcs);
477 register_class("WhiteBalanceEffect", WhiteBalanceEffect_funcs);
478 register_class("ResampleEffect", ResampleEffect_funcs);
479 register_class("PaddingEffect", PaddingEffect_funcs);
480 register_class("IntegralPaddingEffect", IntegralPaddingEffect_funcs);
481 register_class("OverlayEffect", OverlayEffect_funcs);
482 register_class("ResizeEffect", ResizeEffect_funcs);
483 register_class("MixEffect", MixEffect_funcs);
487 if (luaL_dofile(L, filename)) {
488 fprintf(stderr, "error: %s\n", lua_tostring(L, -1));
492 assert(lua_gettop(L) == 0);
494 // Ask it for the number of channels.
495 lua_getglobal(L, "num_channels");
497 if (lua_pcall(L, 0, 1, 0) != 0) {
498 fprintf(stderr, "error running function `num_channels': %s\n", lua_tostring(L, -1));
502 num_channels = luaL_checknumber(L, 1);
504 assert(lua_gettop(L) == 0);
507 void Theme::register_class(const char *class_name, const luaL_Reg *funcs)
509 assert(lua_gettop(L) == 0);
510 luaL_newmetatable(L, class_name);
511 lua_pushlightuserdata(L, this);
512 luaL_setfuncs(L, funcs, 1);
513 lua_pushvalue(L, -1);
514 lua_setfield(L, -2, "__index");
515 lua_setglobal(L, class_name);
516 assert(lua_gettop(L) == 0);
519 Theme::Chain Theme::get_chain(unsigned num, float t, unsigned width, unsigned height)
523 unique_lock<mutex> lock(m);
524 assert(lua_gettop(L) == 0);
525 lua_getglobal(L, "get_chain"); /* function to be called */
526 lua_pushnumber(L, num);
527 lua_pushnumber(L, t);
528 lua_pushnumber(L, width);
529 lua_pushnumber(L, height);
531 this->used_input_frames_collector = &chain.input_frames;
532 if (lua_pcall(L, 4, 2, 0) != 0) {
533 fprintf(stderr, "error running function `get_chain': %s\n", lua_tostring(L, -1));
536 this->used_input_frames_collector = nullptr;
538 chain.chain = (EffectChain *)luaL_checkudata(L, -2, "EffectChain");
539 if (!lua_isfunction(L, -1)) {
540 fprintf(stderr, "Argument #-1 should be a function\n");
543 lua_pushvalue(L, -1);
544 shared_ptr<LuaRefWithDeleter> funcref(new LuaRefWithDeleter(&m, L, luaL_ref(L, LUA_REGISTRYINDEX)));
546 assert(lua_gettop(L) == 0);
548 chain.setup_chain = [this, funcref]{
549 unique_lock<mutex> lock(m);
551 // Set up state, including connecting signals.
552 lua_rawgeti(L, LUA_REGISTRYINDEX, funcref->get());
553 if (lua_pcall(L, 0, 0, 0) != 0) {
554 fprintf(stderr, "error running chain setup callback: %s\n", lua_tostring(L, -1));
557 assert(lua_gettop(L) == 0);
563 std::string Theme::get_channel_name(unsigned channel)
565 unique_lock<mutex> lock(m);
566 lua_getglobal(L, "channel_name");
567 lua_pushnumber(L, channel);
568 if (lua_pcall(L, 1, 1, 0) != 0) {
569 fprintf(stderr, "error running function `channel_nam': %s\n", lua_tostring(L, -1));
573 std::string ret = lua_tostring(L, -1);
575 assert(lua_gettop(L) == 0);
579 bool Theme::get_supports_set_wb(unsigned channel)
581 unique_lock<mutex> lock(m);
582 lua_getglobal(L, "supports_set_wb");
583 lua_pushnumber(L, channel);
584 if (lua_pcall(L, 1, 1, 0) != 0) {
585 fprintf(stderr, "error running function `supports_set_wb': %s\n", lua_tostring(L, -1));
589 bool ret = checkbool(L, -1);
591 assert(lua_gettop(L) == 0);
595 void Theme::set_wb(unsigned channel, double r, double g, double b)
597 unique_lock<mutex> lock(m);
598 lua_getglobal(L, "set_wb");
599 lua_pushnumber(L, channel);
600 lua_pushnumber(L, r);
601 lua_pushnumber(L, g);
602 lua_pushnumber(L, b);
603 if (lua_pcall(L, 4, 0, 0) != 0) {
604 fprintf(stderr, "error running function `set_wb': %s\n", lua_tostring(L, -1));
608 assert(lua_gettop(L) == 0);
611 std::vector<std::string> Theme::get_transition_names(float t)
613 unique_lock<mutex> lock(m);
614 lua_getglobal(L, "get_transitions");
615 lua_pushnumber(L, t);
616 if (lua_pcall(L, 1, 1, 0) != 0) {
617 fprintf(stderr, "error running function `get_transitions': %s\n", lua_tostring(L, -1));
621 std::vector<std::string> ret;
623 while (lua_next(L, -2) != 0) {
624 ret.push_back(lua_tostring(L, -1));
628 assert(lua_gettop(L) == 0);
632 int Theme::map_signal(int signal_num)
634 if (signal_num >= int(num_cards)) {
635 if (signals_warned_about.insert(signal_num).second) {
636 fprintf(stderr, "WARNING: Theme asked for input %d, but we only have %u card(s).\n", signal_num, num_cards);
637 fprintf(stderr, "Mapping to card %d instead.\n", signal_num % num_cards);
639 signal_num %= num_cards;
644 void Theme::transition_clicked(int transition_num, float t)
646 unique_lock<mutex> lock(m);
647 lua_getglobal(L, "transition_clicked");
648 lua_pushnumber(L, transition_num);
649 lua_pushnumber(L, t);
651 if (lua_pcall(L, 2, 0, 0) != 0) {
652 fprintf(stderr, "error running function `transition_clicked': %s\n", lua_tostring(L, -1));
655 assert(lua_gettop(L) == 0);
658 void Theme::channel_clicked(int preview_num)
660 unique_lock<mutex> lock(m);
661 lua_getglobal(L, "channel_clicked");
662 lua_pushnumber(L, preview_num);
664 if (lua_pcall(L, 1, 0, 0) != 0) {
665 fprintf(stderr, "error running function `channel_clicked': %s\n", lua_tostring(L, -1));
668 assert(lua_gettop(L) == 0);