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 class LuaRefWithDeleter {
42 LuaRefWithDeleter(mutex *m, lua_State *L, int ref) : m(m), L(L), ref(ref) {}
43 ~LuaRefWithDeleter() {
44 unique_lock<mutex> lock(*m);
45 luaL_unref(L, LUA_REGISTRYINDEX, ref);
47 int get() const { return ref; }
50 LuaRefWithDeleter(const LuaRefWithDeleter &) = delete;
57 template<class T, class... Args>
58 int wrap_lua_object(lua_State* L, const char *class_name, Args&&... args)
60 // Construct the C++ object and put it on the stack.
61 void *mem = lua_newuserdata(L, sizeof(T));
62 new(mem) T(std::forward<Args>(args)...);
64 // Look up the metatable named <class_name>, and set it on the new object.
65 luaL_getmetatable(L, class_name);
66 lua_setmetatable(L, -2);
71 Theme *get_theme_updata(lua_State* L)
73 luaL_checktype(L, lua_upvalueindex(1), LUA_TLIGHTUSERDATA);
74 return (Theme *)lua_touserdata(L, lua_upvalueindex(1));
77 Effect *get_effect(lua_State *L, int idx)
79 if (luaL_testudata(L, idx, "WhiteBalanceEffect") ||
80 luaL_testudata(L, idx, "ResampleEffect") ||
81 luaL_testudata(L, idx, "PaddingEffect") ||
82 luaL_testudata(L, idx, "IntegralPaddingEffect") ||
83 luaL_testudata(L, idx, "OverlayEffect") ||
84 luaL_testudata(L, idx, "ResizeEffect") ||
85 luaL_testudata(L, idx, "MixEffect") ||
86 luaL_testudata(L, idx, "ImageInput")) {
87 return (Effect *)lua_touserdata(L, idx);
89 luaL_error(L, "Error: Index #%d was not an Effect type\n", idx);
93 bool checkbool(lua_State* L, int idx)
95 luaL_checktype(L, idx, LUA_TBOOLEAN);
96 return lua_toboolean(L, idx);
99 std::string checkstdstring(lua_State *L, int index)
102 const char* cstr = lua_tolstring(L, index, &len);
103 return std::string(cstr, len);
106 int EffectChain_new(lua_State* L)
108 assert(lua_gettop(L) == 2);
109 int aspect_w = luaL_checknumber(L, 1);
110 int aspect_h = luaL_checknumber(L, 2);
112 return wrap_lua_object<EffectChain>(L, "EffectChain", aspect_w, aspect_h);
115 int EffectChain_add_live_input(lua_State* L)
117 assert(lua_gettop(L) == 2);
118 Theme *theme = get_theme_updata(L);
119 EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
120 bool override_bounce = checkbool(L, 2);
121 return wrap_lua_object<LiveInputWrapper>(L, "LiveInputWrapper", theme, chain, override_bounce);
124 int EffectChain_add_effect(lua_State* L)
126 assert(lua_gettop(L) >= 2);
127 EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
129 // TODO: Better error reporting.
130 Effect *effect = get_effect(L, 2);
131 if (lua_gettop(L) == 2) {
132 if (effect->num_inputs() == 0) {
133 chain->add_input((Input *)effect);
135 chain->add_effect(effect);
138 vector<Effect *> inputs;
139 for (int idx = 3; idx <= lua_gettop(L); ++idx) {
140 if (luaL_testudata(L, idx, "LiveInputWrapper")) {
141 LiveInputWrapper *input = (LiveInputWrapper *)lua_touserdata(L, idx);
142 inputs.push_back(input->get_input());
144 inputs.push_back(get_effect(L, idx));
147 chain->add_effect(effect, inputs);
150 lua_settop(L, 2); // Return the effect itself.
152 // Make sure Lua doesn't garbage-collect it away.
153 lua_pushvalue(L, -1);
154 luaL_ref(L, LUA_REGISTRYINDEX); // TODO: leak?
159 int EffectChain_finalize(lua_State* L)
161 assert(lua_gettop(L) == 2);
162 EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
163 bool is_main_chain = checkbool(L, 2);
165 // Add outputs as needed.
166 // NOTE: If you change any details about the output format, you will need to
167 // also update what's given to the muxer (HTTPD::Mux constructor) and
168 // what's put in the H.264 stream (sps_rbsp()).
169 ImageFormat inout_format;
170 inout_format.color_space = COLORSPACE_REC_709;
172 // Output gamma is tricky. We should output Rec. 709 for TV, except that
173 // we expect to run with web players and others that don't really care and
174 // just output with no conversion. So that means we'll need to output sRGB,
175 // even though H.264 has no setting for that (we use “unspecified”).
176 inout_format.gamma_curve = GAMMA_sRGB;
179 YCbCrFormat output_ycbcr_format;
180 // We actually output 4:2:0 in the end, but chroma subsampling
181 // happens in a pass not run by Movit (see Mixer::subsample_chroma()).
182 output_ycbcr_format.chroma_subsampling_x = 1;
183 output_ycbcr_format.chroma_subsampling_y = 1;
185 // Rec. 709 would be the sane thing to do, but it seems many players
186 // (e.g. MPlayer and VLC) just default to BT.601 coefficients no matter
187 // what (see discussions in e.g. https://trac.ffmpeg.org/ticket/4978).
188 // We _do_ set the right flags, though, so that a player that works
189 // properly doesn't have to guess.
190 output_ycbcr_format.luma_coefficients = YCBCR_REC_601;
191 output_ycbcr_format.full_range = false;
192 output_ycbcr_format.num_levels = 256;
194 chain->add_ycbcr_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED, output_ycbcr_format, YCBCR_OUTPUT_SPLIT_Y_AND_CBCR);
195 chain->set_dither_bits(8);
196 chain->set_output_origin(OUTPUT_ORIGIN_TOP_LEFT);
198 chain->add_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED);
204 int LiveInputWrapper_connect_signal(lua_State* L)
206 assert(lua_gettop(L) == 2);
207 LiveInputWrapper *input = (LiveInputWrapper *)luaL_checkudata(L, 1, "LiveInputWrapper");
208 int signal_num = luaL_checknumber(L, 2);
209 input->connect_signal(signal_num);
213 int ImageInput_new(lua_State* L)
215 assert(lua_gettop(L) == 1);
216 std::string filename = checkstdstring(L, 1);
217 return wrap_lua_object<ImageInput>(L, "ImageInput", filename);
220 int WhiteBalanceEffect_new(lua_State* L)
222 assert(lua_gettop(L) == 0);
223 return wrap_lua_object<WhiteBalanceEffect>(L, "WhiteBalanceEffect");
226 int ResampleEffect_new(lua_State* L)
228 assert(lua_gettop(L) == 0);
229 return wrap_lua_object<ResampleEffect>(L, "ResampleEffect");
232 int PaddingEffect_new(lua_State* L)
234 assert(lua_gettop(L) == 0);
235 return wrap_lua_object<PaddingEffect>(L, "PaddingEffect");
238 int IntegralPaddingEffect_new(lua_State* L)
240 assert(lua_gettop(L) == 0);
241 return wrap_lua_object<IntegralPaddingEffect>(L, "IntegralPaddingEffect");
244 int OverlayEffect_new(lua_State* L)
246 assert(lua_gettop(L) == 0);
247 return wrap_lua_object<OverlayEffect>(L, "OverlayEffect");
250 int ResizeEffect_new(lua_State* L)
252 assert(lua_gettop(L) == 0);
253 return wrap_lua_object<ResizeEffect>(L, "ResizeEffect");
256 int MixEffect_new(lua_State* L)
258 assert(lua_gettop(L) == 0);
259 return wrap_lua_object<MixEffect>(L, "MixEffect");
262 int Effect_set_float(lua_State *L)
264 assert(lua_gettop(L) == 3);
265 Effect *effect = (Effect *)get_effect(L, 1);
266 std::string key = checkstdstring(L, 2);
267 float value = luaL_checknumber(L, 3);
268 if (!effect->set_float(key, value)) {
269 luaL_error(L, "Effect refused set_float(\"%s\", %d) (invalid key?)", key.c_str(), int(value));
274 int Effect_set_int(lua_State *L)
276 assert(lua_gettop(L) == 3);
277 Effect *effect = (Effect *)get_effect(L, 1);
278 std::string key = checkstdstring(L, 2);
279 float value = luaL_checknumber(L, 3);
280 if (!effect->set_int(key, value)) {
281 luaL_error(L, "Effect refused set_int(\"%s\", %d) (invalid key?)", key.c_str(), int(value));
286 int Effect_set_vec3(lua_State *L)
288 assert(lua_gettop(L) == 5);
289 Effect *effect = (Effect *)get_effect(L, 1);
290 std::string key = checkstdstring(L, 2);
292 v[0] = luaL_checknumber(L, 3);
293 v[1] = luaL_checknumber(L, 4);
294 v[2] = luaL_checknumber(L, 5);
295 if (!effect->set_vec3(key, v)) {
296 luaL_error(L, "Effect refused set_vec3(\"%s\", %f, %f, %f) (invalid key?)", key.c_str(),
302 int Effect_set_vec4(lua_State *L)
304 assert(lua_gettop(L) == 6);
305 Effect *effect = (Effect *)get_effect(L, 1);
306 std::string key = checkstdstring(L, 2);
308 v[0] = luaL_checknumber(L, 3);
309 v[1] = luaL_checknumber(L, 4);
310 v[2] = luaL_checknumber(L, 5);
311 v[3] = luaL_checknumber(L, 6);
312 if (!effect->set_vec4(key, v)) {
313 luaL_error(L, "Effect refused set_vec4(\"%s\", %f, %f, %f, %f) (invalid key?)", key.c_str(),
314 v[0], v[1], v[2], v[3]);
319 const luaL_Reg EffectChain_funcs[] = {
320 { "new", EffectChain_new },
321 { "add_live_input", EffectChain_add_live_input },
322 { "add_effect", EffectChain_add_effect },
323 { "finalize", EffectChain_finalize },
327 const luaL_Reg LiveInputWrapper_funcs[] = {
328 { "connect_signal", LiveInputWrapper_connect_signal },
332 const luaL_Reg ImageInput_funcs[] = {
333 { "new", ImageInput_new },
334 { "set_float", Effect_set_float },
335 { "set_int", Effect_set_int },
336 { "set_vec3", Effect_set_vec3 },
337 { "set_vec4", Effect_set_vec4 },
341 const luaL_Reg WhiteBalanceEffect_funcs[] = {
342 { "new", WhiteBalanceEffect_new },
343 { "set_float", Effect_set_float },
344 { "set_int", Effect_set_int },
345 { "set_vec3", Effect_set_vec3 },
346 { "set_vec4", Effect_set_vec4 },
350 const luaL_Reg ResampleEffect_funcs[] = {
351 { "new", ResampleEffect_new },
352 { "set_float", Effect_set_float },
353 { "set_int", Effect_set_int },
354 { "set_vec3", Effect_set_vec3 },
355 { "set_vec4", Effect_set_vec4 },
359 const luaL_Reg PaddingEffect_funcs[] = {
360 { "new", PaddingEffect_new },
361 { "set_float", Effect_set_float },
362 { "set_int", Effect_set_int },
363 { "set_vec3", Effect_set_vec3 },
364 { "set_vec4", Effect_set_vec4 },
368 const luaL_Reg IntegralPaddingEffect_funcs[] = {
369 { "new", IntegralPaddingEffect_new },
370 { "set_float", Effect_set_float },
371 { "set_int", Effect_set_int },
372 { "set_vec3", Effect_set_vec3 },
373 { "set_vec4", Effect_set_vec4 },
377 const luaL_Reg OverlayEffect_funcs[] = {
378 { "new", OverlayEffect_new },
379 { "set_float", Effect_set_float },
380 { "set_int", Effect_set_int },
381 { "set_vec3", Effect_set_vec3 },
382 { "set_vec4", Effect_set_vec4 },
386 const luaL_Reg ResizeEffect_funcs[] = {
387 { "new", ResizeEffect_new },
388 { "set_float", Effect_set_float },
389 { "set_int", Effect_set_int },
390 { "set_vec3", Effect_set_vec3 },
391 { "set_vec4", Effect_set_vec4 },
395 const luaL_Reg MixEffect_funcs[] = {
396 { "new", MixEffect_new },
397 { "set_float", Effect_set_float },
398 { "set_int", Effect_set_int },
399 { "set_vec3", Effect_set_vec3 },
400 { "set_vec4", Effect_set_vec4 },
406 LiveInputWrapper::LiveInputWrapper(Theme *theme, EffectChain *chain, bool override_bounce)
409 ImageFormat inout_format;
410 inout_format.color_space = COLORSPACE_sRGB;
412 // Gamma curve depends on the input signal, and we don't really get any
413 // indications. A camera would be expected to do Rec. 709, but
414 // I haven't checked if any do in practice. However, computers _do_ output
415 // in sRGB gamma (ie., they don't convert from sRGB to Rec. 709), and
416 // I wouldn't really be surprised if most non-professional cameras do, too.
417 // So we pick sRGB as the least evil here.
418 inout_format.gamma_curve = GAMMA_sRGB;
420 // The Blackmagic driver docs claim that the device outputs Y'CbCr
421 // according to Rec. 601, but practical testing indicates it definitely
422 // is Rec. 709 (at least up to errors attributable to rounding errors).
423 // Perhaps 601 was only to indicate the subsampling positions, not the
424 // colorspace itself? Tested with a Lenovo X1 gen 3 as input.
425 YCbCrFormat input_ycbcr_format;
426 input_ycbcr_format.chroma_subsampling_x = 2;
427 input_ycbcr_format.chroma_subsampling_y = 1;
428 input_ycbcr_format.cb_x_position = 0.0;
429 input_ycbcr_format.cr_x_position = 0.0;
430 input_ycbcr_format.cb_y_position = 0.5;
431 input_ycbcr_format.cr_y_position = 0.5;
432 input_ycbcr_format.luma_coefficients = YCBCR_REC_709;
433 input_ycbcr_format.full_range = false;
435 if (override_bounce) {
436 input = new NonBouncingYCbCrInput(inout_format, input_ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
438 input = new YCbCrInput(inout_format, input_ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
440 chain->add_input(input);
443 void LiveInputWrapper::connect_signal(int signal_num)
445 if (global_mixer == nullptr) {
449 signal_num = theme->map_signal(signal_num);
451 Mixer::BufferedFrame frame = global_mixer->get_buffered_frame(signal_num, 0);
452 const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
454 input->set_texture_num(0, userdata->tex_y[frame.field_number]);
455 input->set_texture_num(1, userdata->tex_cbcr[frame.field_number]);
456 input->set_width(userdata->last_width[frame.field_number]);
457 input->set_height(userdata->last_height[frame.field_number]);
459 // Hold on to the refcount so that we don't release the input frame
460 // until we're done rendering from it.
461 if (theme->used_input_frames_collector != nullptr) {
462 theme->used_input_frames_collector->push_back(frame.frame);
466 Theme::Theme(const char *filename, ResourcePool *resource_pool, unsigned num_cards)
467 : resource_pool(resource_pool), num_cards(num_cards)
472 register_class("EffectChain", EffectChain_funcs);
473 register_class("LiveInputWrapper", LiveInputWrapper_funcs);
474 register_class("ImageInput", ImageInput_funcs);
475 register_class("WhiteBalanceEffect", WhiteBalanceEffect_funcs);
476 register_class("ResampleEffect", ResampleEffect_funcs);
477 register_class("PaddingEffect", PaddingEffect_funcs);
478 register_class("IntegralPaddingEffect", IntegralPaddingEffect_funcs);
479 register_class("OverlayEffect", OverlayEffect_funcs);
480 register_class("ResizeEffect", ResizeEffect_funcs);
481 register_class("MixEffect", MixEffect_funcs);
485 if (luaL_dofile(L, filename)) {
486 fprintf(stderr, "error: %s\n", lua_tostring(L, -1));
490 assert(lua_gettop(L) == 0);
492 // Ask it for the number of channels.
493 lua_getglobal(L, "num_channels");
495 if (lua_pcall(L, 0, 1, 0) != 0) {
496 fprintf(stderr, "error running function `num_channels': %s\n", lua_tostring(L, -1));
500 num_channels = luaL_checknumber(L, 1);
502 assert(lua_gettop(L) == 0);
505 void Theme::register_class(const char *class_name, const luaL_Reg *funcs)
507 assert(lua_gettop(L) == 0);
508 luaL_newmetatable(L, class_name);
509 lua_pushlightuserdata(L, this);
510 luaL_setfuncs(L, funcs, 1);
511 lua_pushvalue(L, -1);
512 lua_setfield(L, -2, "__index");
513 lua_setglobal(L, class_name);
514 assert(lua_gettop(L) == 0);
517 Theme::Chain Theme::get_chain(unsigned num, float t, unsigned width, unsigned height)
521 unique_lock<mutex> lock(m);
522 assert(lua_gettop(L) == 0);
523 lua_getglobal(L, "get_chain"); /* function to be called */
524 lua_pushnumber(L, num);
525 lua_pushnumber(L, t);
526 lua_pushnumber(L, width);
527 lua_pushnumber(L, height);
529 this->used_input_frames_collector = &chain.input_frames;
530 if (lua_pcall(L, 4, 2, 0) != 0) {
531 fprintf(stderr, "error running function `get_chain': %s\n", lua_tostring(L, -1));
534 this->used_input_frames_collector = nullptr;
536 chain.chain = (EffectChain *)luaL_checkudata(L, -2, "EffectChain");
537 if (!lua_isfunction(L, -1)) {
538 fprintf(stderr, "Argument #-1 should be a function\n");
541 lua_pushvalue(L, -1);
542 shared_ptr<LuaRefWithDeleter> funcref(new LuaRefWithDeleter(&m, L, luaL_ref(L, LUA_REGISTRYINDEX)));
544 assert(lua_gettop(L) == 0);
546 chain.setup_chain = [this, funcref]{
547 unique_lock<mutex> lock(m);
549 // Set up state, including connecting signals.
550 lua_rawgeti(L, LUA_REGISTRYINDEX, funcref->get());
551 if (lua_pcall(L, 0, 0, 0) != 0) {
552 fprintf(stderr, "error running chain setup callback: %s\n", lua_tostring(L, -1));
555 assert(lua_gettop(L) == 0);
561 std::string Theme::get_channel_name(unsigned channel)
563 unique_lock<mutex> lock(m);
564 lua_getglobal(L, "channel_name");
565 lua_pushnumber(L, channel);
566 if (lua_pcall(L, 1, 1, 0) != 0) {
567 fprintf(stderr, "error running function `channel_nam': %s\n", lua_tostring(L, -1));
571 std::string ret = lua_tostring(L, -1);
573 assert(lua_gettop(L) == 0);
577 bool Theme::get_supports_set_wb(unsigned channel)
579 unique_lock<mutex> lock(m);
580 lua_getglobal(L, "supports_set_wb");
581 lua_pushnumber(L, channel);
582 if (lua_pcall(L, 1, 1, 0) != 0) {
583 fprintf(stderr, "error running function `supports_set_wb': %s\n", lua_tostring(L, -1));
587 bool ret = checkbool(L, -1);
589 assert(lua_gettop(L) == 0);
593 void Theme::set_wb(unsigned channel, double r, double g, double b)
595 unique_lock<mutex> lock(m);
596 lua_getglobal(L, "set_wb");
597 lua_pushnumber(L, channel);
598 lua_pushnumber(L, r);
599 lua_pushnumber(L, g);
600 lua_pushnumber(L, b);
601 if (lua_pcall(L, 4, 0, 0) != 0) {
602 fprintf(stderr, "error running function `set_wb': %s\n", lua_tostring(L, -1));
606 assert(lua_gettop(L) == 0);
609 std::vector<std::string> Theme::get_transition_names(float t)
611 unique_lock<mutex> lock(m);
612 lua_getglobal(L, "get_transitions");
613 lua_pushnumber(L, t);
614 if (lua_pcall(L, 1, 1, 0) != 0) {
615 fprintf(stderr, "error running function `get_transitions': %s\n", lua_tostring(L, -1));
619 std::vector<std::string> ret;
621 while (lua_next(L, -2) != 0) {
622 ret.push_back(lua_tostring(L, -1));
626 assert(lua_gettop(L) == 0);
630 int Theme::map_signal(int signal_num)
632 if (signal_num >= int(num_cards)) {
633 if (signals_warned_about.insert(signal_num).second) {
634 fprintf(stderr, "WARNING: Theme asked for input %d, but we only have %u card(s).\n", signal_num, num_cards);
635 fprintf(stderr, "Mapping to card %d instead.\n", signal_num % num_cards);
637 signal_num %= num_cards;
642 void Theme::transition_clicked(int transition_num, float t)
644 unique_lock<mutex> lock(m);
645 lua_getglobal(L, "transition_clicked");
646 lua_pushnumber(L, transition_num);
647 lua_pushnumber(L, t);
649 if (lua_pcall(L, 2, 0, 0) != 0) {
650 fprintf(stderr, "error running function `transition_clicked': %s\n", lua_tostring(L, -1));
653 assert(lua_gettop(L) == 0);
656 void Theme::channel_clicked(int preview_num)
658 unique_lock<mutex> lock(m);
659 lua_getglobal(L, "channel_clicked");
660 lua_pushnumber(L, preview_num);
662 if (lua_pcall(L, 1, 0, 0) != 0) {
663 fprintf(stderr, "error running function `channel_clicked': %s\n", lua_tostring(L, -1));
666 assert(lua_gettop(L) == 0);