6 #include <movit/effect.h>
7 #include <movit/effect_chain.h>
8 #include <movit/image_format.h>
9 #include <movit/mix_effect.h>
10 #include <movit/overlay_effect.h>
11 #include <movit/padding_effect.h>
12 #include <movit/resample_effect.h>
13 #include <movit/resize_effect.h>
14 #include <movit/util.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 // Contains basically the same data as InputState, but does not hold on to
41 // a reference to the frames. This is important so that we can release them
42 // without having to wait for Lua's GC.
43 struct InputStateInfo {
44 InputStateInfo(const InputState& input_state);
46 unsigned last_width[MAX_CARDS], last_height[MAX_CARDS];
47 bool last_interlaced[MAX_CARDS], last_has_signal[MAX_CARDS];
48 unsigned last_frame_rate_nom[MAX_CARDS], last_frame_rate_den[MAX_CARDS];
51 InputStateInfo::InputStateInfo(const InputState &input_state)
53 for (unsigned signal_num = 0; signal_num < MAX_CARDS; ++signal_num) {
54 BufferedFrame frame = input_state.buffered_frames[signal_num][0];
55 if (frame.frame == nullptr) {
56 last_width[signal_num] = last_height[signal_num] = 0;
57 last_interlaced[signal_num] = false;
58 last_has_signal[signal_num] = false;
61 const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
62 last_width[signal_num] = userdata->last_width[frame.field_number];
63 last_height[signal_num] = userdata->last_height[frame.field_number];
64 last_interlaced[signal_num] = userdata->last_interlaced;
65 last_has_signal[signal_num] = userdata->last_has_signal;
66 last_frame_rate_nom[signal_num] = userdata->last_frame_rate_nom;
67 last_frame_rate_den[signal_num] = userdata->last_frame_rate_den;
71 class LuaRefWithDeleter {
73 LuaRefWithDeleter(mutex *m, lua_State *L, int ref) : m(m), L(L), ref(ref) {}
74 ~LuaRefWithDeleter() {
75 unique_lock<mutex> lock(*m);
76 luaL_unref(L, LUA_REGISTRYINDEX, ref);
78 int get() const { return ref; }
81 LuaRefWithDeleter(const LuaRefWithDeleter &) = delete;
88 template<class T, class... Args>
89 int wrap_lua_object(lua_State* L, const char *class_name, Args&&... args)
91 // Construct the C++ object and put it on the stack.
92 void *mem = lua_newuserdata(L, sizeof(T));
93 new(mem) T(forward<Args>(args)...);
95 // Look up the metatable named <class_name>, and set it on the new object.
96 luaL_getmetatable(L, class_name);
97 lua_setmetatable(L, -2);
102 // Like wrap_lua_object, but the object is not owned by Lua; ie. it's not freed
103 // by Lua GC. This is typically the case for Effects, which are owned by EffectChain
104 // and expected to be destructed by it. The object will be of type T** instead of T*
105 // when exposed to Lua.
107 // Note that we currently leak if you allocate an Effect in this way and never call
108 // add_effect. We should see if there's a way to e.g. set __gc on it at construction time
109 // and then release that once add_effect() takes ownership.
110 template<class T, class... Args>
111 int wrap_lua_object_nonowned(lua_State* L, const char *class_name, Args&&... args)
113 // Construct the pointer ot the C++ object and put it on the stack.
114 T **obj = (T **)lua_newuserdata(L, sizeof(T *));
115 *obj = new T(forward<Args>(args)...);
117 // Look up the metatable named <class_name>, and set it on the new object.
118 luaL_getmetatable(L, class_name);
119 lua_setmetatable(L, -2);
124 Theme *get_theme_updata(lua_State* L)
126 luaL_checktype(L, lua_upvalueindex(1), LUA_TLIGHTUSERDATA);
127 return (Theme *)lua_touserdata(L, lua_upvalueindex(1));
130 Effect *get_effect(lua_State *L, int idx)
132 if (luaL_testudata(L, idx, "WhiteBalanceEffect") ||
133 luaL_testudata(L, idx, "ResampleEffect") ||
134 luaL_testudata(L, idx, "PaddingEffect") ||
135 luaL_testudata(L, idx, "IntegralPaddingEffect") ||
136 luaL_testudata(L, idx, "OverlayEffect") ||
137 luaL_testudata(L, idx, "ResizeEffect") ||
138 luaL_testudata(L, idx, "MixEffect") ||
139 luaL_testudata(L, idx, "ImageInput")) {
140 return *(Effect **)lua_touserdata(L, idx);
142 luaL_error(L, "Error: Index #%d was not an Effect type\n", idx);
146 InputStateInfo *get_input_state_info(lua_State *L, int idx)
148 if (luaL_testudata(L, idx, "InputStateInfo")) {
149 return (InputStateInfo *)lua_touserdata(L, idx);
151 luaL_error(L, "Error: Index #%d was not InputStateInfo\n", idx);
155 bool checkbool(lua_State* L, int idx)
157 luaL_checktype(L, idx, LUA_TBOOLEAN);
158 return lua_toboolean(L, idx);
161 string checkstdstring(lua_State *L, int index)
164 const char* cstr = lua_tolstring(L, index, &len);
165 return string(cstr, len);
168 int EffectChain_new(lua_State* L)
170 assert(lua_gettop(L) == 2);
171 Theme *theme = get_theme_updata(L);
172 int aspect_w = luaL_checknumber(L, 1);
173 int aspect_h = luaL_checknumber(L, 2);
175 return wrap_lua_object<EffectChain>(L, "EffectChain", aspect_w, aspect_h, theme->get_resource_pool());
178 int EffectChain_gc(lua_State* L)
180 assert(lua_gettop(L) == 1);
181 EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
182 chain->~EffectChain();
186 int EffectChain_add_live_input(lua_State* L)
188 assert(lua_gettop(L) == 3);
189 Theme *theme = get_theme_updata(L);
190 EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
191 bool override_bounce = checkbool(L, 2);
192 bool deinterlace = checkbool(L, 3);
193 return wrap_lua_object<LiveInputWrapper>(L, "LiveInputWrapper", theme, chain, override_bounce, deinterlace);
196 int EffectChain_add_effect(lua_State* L)
198 assert(lua_gettop(L) >= 2);
199 EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
201 // TODO: Better error reporting.
202 Effect *effect = get_effect(L, 2);
203 if (lua_gettop(L) == 2) {
204 if (effect->num_inputs() == 0) {
205 chain->add_input((Input *)effect);
207 chain->add_effect(effect);
210 vector<Effect *> inputs;
211 for (int idx = 3; idx <= lua_gettop(L); ++idx) {
212 if (luaL_testudata(L, idx, "LiveInputWrapper")) {
213 LiveInputWrapper *input = (LiveInputWrapper *)lua_touserdata(L, idx);
214 inputs.push_back(input->get_effect());
216 inputs.push_back(get_effect(L, idx));
219 chain->add_effect(effect, inputs);
222 lua_settop(L, 2); // Return the effect itself.
224 // Make sure Lua doesn't garbage-collect it away.
225 lua_pushvalue(L, -1);
226 luaL_ref(L, LUA_REGISTRYINDEX); // TODO: leak?
231 int EffectChain_finalize(lua_State* L)
233 assert(lua_gettop(L) == 2);
234 EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
235 bool is_main_chain = checkbool(L, 2);
237 // Add outputs as needed.
238 // NOTE: If you change any details about the output format, you will need to
239 // also update what's given to the muxer (HTTPD::Mux constructor) and
240 // what's put in the H.264 stream (sps_rbsp()).
241 ImageFormat inout_format;
242 inout_format.color_space = COLORSPACE_REC_709;
244 // Output gamma is tricky. We should output Rec. 709 for TV, except that
245 // we expect to run with web players and others that don't really care and
246 // just output with no conversion. So that means we'll need to output sRGB,
247 // even though H.264 has no setting for that (we use “unspecified”).
248 inout_format.gamma_curve = GAMMA_sRGB;
251 YCbCrFormat output_ycbcr_format;
252 // We actually output 4:2:0 in the end, but chroma subsampling
253 // happens in a pass not run by Movit (see Mixer::subsample_chroma()).
254 output_ycbcr_format.chroma_subsampling_x = 1;
255 output_ycbcr_format.chroma_subsampling_y = 1;
257 // Rec. 709 would be the sane thing to do, but it seems many players
258 // (e.g. MPlayer and VLC) just default to BT.601 coefficients no matter
259 // what (see discussions in e.g. https://trac.ffmpeg.org/ticket/4978).
260 // We _do_ set the right flags, though, so that a player that works
261 // properly doesn't have to guess.
262 output_ycbcr_format.luma_coefficients = YCBCR_REC_601;
263 output_ycbcr_format.full_range = false;
264 output_ycbcr_format.num_levels = 256;
266 chain->add_ycbcr_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED, output_ycbcr_format, YCBCR_OUTPUT_SPLIT_Y_AND_CBCR);
267 chain->set_dither_bits(8);
268 chain->set_output_origin(OUTPUT_ORIGIN_TOP_LEFT);
270 chain->add_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED);
276 int LiveInputWrapper_connect_signal(lua_State* L)
278 assert(lua_gettop(L) == 2);
279 LiveInputWrapper *input = (LiveInputWrapper *)luaL_checkudata(L, 1, "LiveInputWrapper");
280 int signal_num = luaL_checknumber(L, 2);
281 input->connect_signal(signal_num);
285 int ImageInput_new(lua_State* L)
287 assert(lua_gettop(L) == 1);
288 string filename = checkstdstring(L, 1);
289 return wrap_lua_object_nonowned<ImageInput>(L, "ImageInput", filename);
292 int WhiteBalanceEffect_new(lua_State* L)
294 assert(lua_gettop(L) == 0);
295 return wrap_lua_object_nonowned<WhiteBalanceEffect>(L, "WhiteBalanceEffect");
298 int ResampleEffect_new(lua_State* L)
300 assert(lua_gettop(L) == 0);
301 return wrap_lua_object_nonowned<ResampleEffect>(L, "ResampleEffect");
304 int PaddingEffect_new(lua_State* L)
306 assert(lua_gettop(L) == 0);
307 return wrap_lua_object_nonowned<PaddingEffect>(L, "PaddingEffect");
310 int IntegralPaddingEffect_new(lua_State* L)
312 assert(lua_gettop(L) == 0);
313 return wrap_lua_object_nonowned<IntegralPaddingEffect>(L, "IntegralPaddingEffect");
316 int OverlayEffect_new(lua_State* L)
318 assert(lua_gettop(L) == 0);
319 return wrap_lua_object_nonowned<OverlayEffect>(L, "OverlayEffect");
322 int ResizeEffect_new(lua_State* L)
324 assert(lua_gettop(L) == 0);
325 return wrap_lua_object_nonowned<ResizeEffect>(L, "ResizeEffect");
328 int MixEffect_new(lua_State* L)
330 assert(lua_gettop(L) == 0);
331 return wrap_lua_object_nonowned<MixEffect>(L, "MixEffect");
334 int InputStateInfo_get_width(lua_State* L)
336 assert(lua_gettop(L) == 2);
337 InputStateInfo *input_state_info = get_input_state_info(L, 1);
338 Theme *theme = get_theme_updata(L);
339 int signal_num = theme->map_signal(luaL_checknumber(L, 2));
340 lua_pushnumber(L, input_state_info->last_width[signal_num]);
344 int InputStateInfo_get_height(lua_State* L)
346 assert(lua_gettop(L) == 2);
347 InputStateInfo *input_state_info = get_input_state_info(L, 1);
348 Theme *theme = get_theme_updata(L);
349 int signal_num = theme->map_signal(luaL_checknumber(L, 2));
350 lua_pushnumber(L, input_state_info->last_height[signal_num]);
354 int InputStateInfo_get_interlaced(lua_State* L)
356 assert(lua_gettop(L) == 2);
357 InputStateInfo *input_state_info = get_input_state_info(L, 1);
358 Theme *theme = get_theme_updata(L);
359 int signal_num = theme->map_signal(luaL_checknumber(L, 2));
360 lua_pushboolean(L, input_state_info->last_interlaced[signal_num]);
364 int InputStateInfo_get_has_signal(lua_State* L)
366 assert(lua_gettop(L) == 2);
367 InputStateInfo *input_state_info = get_input_state_info(L, 1);
368 Theme *theme = get_theme_updata(L);
369 int signal_num = theme->map_signal(luaL_checknumber(L, 2));
370 lua_pushboolean(L, input_state_info->last_has_signal[signal_num]);
374 int InputStateInfo_get_frame_rate_nom(lua_State* L)
376 assert(lua_gettop(L) == 2);
377 InputStateInfo *input_state_info = get_input_state_info(L, 1);
378 Theme *theme = get_theme_updata(L);
379 int signal_num = theme->map_signal(luaL_checknumber(L, 2));
380 lua_pushnumber(L, input_state_info->last_frame_rate_nom[signal_num]);
384 int InputStateInfo_get_frame_rate_den(lua_State* L)
386 assert(lua_gettop(L) == 2);
387 InputStateInfo *input_state_info = get_input_state_info(L, 1);
388 Theme *theme = get_theme_updata(L);
389 int signal_num = theme->map_signal(luaL_checknumber(L, 2));
390 lua_pushnumber(L, input_state_info->last_frame_rate_den[signal_num]);
394 int Effect_set_float(lua_State *L)
396 assert(lua_gettop(L) == 3);
397 Effect *effect = (Effect *)get_effect(L, 1);
398 string key = checkstdstring(L, 2);
399 float value = luaL_checknumber(L, 3);
400 if (!effect->set_float(key, value)) {
401 luaL_error(L, "Effect refused set_float(\"%s\", %d) (invalid key?)", key.c_str(), int(value));
406 int Effect_set_int(lua_State *L)
408 assert(lua_gettop(L) == 3);
409 Effect *effect = (Effect *)get_effect(L, 1);
410 string key = checkstdstring(L, 2);
411 float value = luaL_checknumber(L, 3);
412 if (!effect->set_int(key, value)) {
413 luaL_error(L, "Effect refused set_int(\"%s\", %d) (invalid key?)", key.c_str(), int(value));
418 int Effect_set_vec3(lua_State *L)
420 assert(lua_gettop(L) == 5);
421 Effect *effect = (Effect *)get_effect(L, 1);
422 string key = checkstdstring(L, 2);
424 v[0] = luaL_checknumber(L, 3);
425 v[1] = luaL_checknumber(L, 4);
426 v[2] = luaL_checknumber(L, 5);
427 if (!effect->set_vec3(key, v)) {
428 luaL_error(L, "Effect refused set_vec3(\"%s\", %f, %f, %f) (invalid key?)", key.c_str(),
434 int Effect_set_vec4(lua_State *L)
436 assert(lua_gettop(L) == 6);
437 Effect *effect = (Effect *)get_effect(L, 1);
438 string key = checkstdstring(L, 2);
440 v[0] = luaL_checknumber(L, 3);
441 v[1] = luaL_checknumber(L, 4);
442 v[2] = luaL_checknumber(L, 5);
443 v[3] = luaL_checknumber(L, 6);
444 if (!effect->set_vec4(key, v)) {
445 luaL_error(L, "Effect refused set_vec4(\"%s\", %f, %f, %f, %f) (invalid key?)", key.c_str(),
446 v[0], v[1], v[2], v[3]);
451 const luaL_Reg EffectChain_funcs[] = {
452 { "new", EffectChain_new },
453 { "__gc", EffectChain_gc },
454 { "add_live_input", EffectChain_add_live_input },
455 { "add_effect", EffectChain_add_effect },
456 { "finalize", EffectChain_finalize },
460 const luaL_Reg LiveInputWrapper_funcs[] = {
461 { "connect_signal", LiveInputWrapper_connect_signal },
465 const luaL_Reg ImageInput_funcs[] = {
466 { "new", ImageInput_new },
467 { "set_float", Effect_set_float },
468 { "set_int", Effect_set_int },
469 { "set_vec3", Effect_set_vec3 },
470 { "set_vec4", Effect_set_vec4 },
474 const luaL_Reg WhiteBalanceEffect_funcs[] = {
475 { "new", WhiteBalanceEffect_new },
476 { "set_float", Effect_set_float },
477 { "set_int", Effect_set_int },
478 { "set_vec3", Effect_set_vec3 },
479 { "set_vec4", Effect_set_vec4 },
483 const luaL_Reg ResampleEffect_funcs[] = {
484 { "new", ResampleEffect_new },
485 { "set_float", Effect_set_float },
486 { "set_int", Effect_set_int },
487 { "set_vec3", Effect_set_vec3 },
488 { "set_vec4", Effect_set_vec4 },
492 const luaL_Reg PaddingEffect_funcs[] = {
493 { "new", PaddingEffect_new },
494 { "set_float", Effect_set_float },
495 { "set_int", Effect_set_int },
496 { "set_vec3", Effect_set_vec3 },
497 { "set_vec4", Effect_set_vec4 },
501 const luaL_Reg IntegralPaddingEffect_funcs[] = {
502 { "new", IntegralPaddingEffect_new },
503 { "set_float", Effect_set_float },
504 { "set_int", Effect_set_int },
505 { "set_vec3", Effect_set_vec3 },
506 { "set_vec4", Effect_set_vec4 },
510 const luaL_Reg OverlayEffect_funcs[] = {
511 { "new", OverlayEffect_new },
512 { "set_float", Effect_set_float },
513 { "set_int", Effect_set_int },
514 { "set_vec3", Effect_set_vec3 },
515 { "set_vec4", Effect_set_vec4 },
519 const luaL_Reg ResizeEffect_funcs[] = {
520 { "new", ResizeEffect_new },
521 { "set_float", Effect_set_float },
522 { "set_int", Effect_set_int },
523 { "set_vec3", Effect_set_vec3 },
524 { "set_vec4", Effect_set_vec4 },
528 const luaL_Reg MixEffect_funcs[] = {
529 { "new", MixEffect_new },
530 { "set_float", Effect_set_float },
531 { "set_int", Effect_set_int },
532 { "set_vec3", Effect_set_vec3 },
533 { "set_vec4", Effect_set_vec4 },
537 const luaL_Reg InputStateInfo_funcs[] = {
538 { "get_width", InputStateInfo_get_width },
539 { "get_height", InputStateInfo_get_height },
540 { "get_interlaced", InputStateInfo_get_interlaced },
541 { "get_has_signal", InputStateInfo_get_has_signal },
542 { "get_frame_rate_nom", InputStateInfo_get_frame_rate_nom },
543 { "get_frame_rate_den", InputStateInfo_get_frame_rate_den },
549 LiveInputWrapper::LiveInputWrapper(Theme *theme, EffectChain *chain, bool override_bounce, bool deinterlace)
551 deinterlace(deinterlace)
553 ImageFormat inout_format;
554 inout_format.color_space = COLORSPACE_sRGB;
556 // Gamma curve depends on the input signal, and we don't really get any
557 // indications. A camera would be expected to do Rec. 709, but
558 // I haven't checked if any do in practice. However, computers _do_ output
559 // in sRGB gamma (ie., they don't convert from sRGB to Rec. 709), and
560 // I wouldn't really be surprised if most non-professional cameras do, too.
561 // So we pick sRGB as the least evil here.
562 inout_format.gamma_curve = GAMMA_sRGB;
564 // The Blackmagic driver docs claim that the device outputs Y'CbCr
565 // according to Rec. 601, but practical testing indicates it definitely
566 // is Rec. 709 (at least up to errors attributable to rounding errors).
567 // Perhaps 601 was only to indicate the subsampling positions, not the
568 // colorspace itself? Tested with a Lenovo X1 gen 3 as input.
569 YCbCrFormat input_ycbcr_format;
570 input_ycbcr_format.chroma_subsampling_x = 2;
571 input_ycbcr_format.chroma_subsampling_y = 1;
572 input_ycbcr_format.cb_x_position = 0.0;
573 input_ycbcr_format.cr_x_position = 0.0;
574 input_ycbcr_format.cb_y_position = 0.5;
575 input_ycbcr_format.cr_y_position = 0.5;
576 input_ycbcr_format.luma_coefficients = YCBCR_REC_709;
577 input_ycbcr_format.full_range = false;
581 deinterlace_effect = new movit::DeinterlaceEffect();
583 // As per the comments in deinterlace_effect.h, we turn this off.
584 // The most likely interlaced input for us is either a camera
585 // (where it's fine to turn it off) or a laptop (where it _should_
587 CHECK(deinterlace_effect->set_int("enable_spatial_interlacing_check", 0));
589 num_inputs = deinterlace_effect->num_inputs();
590 assert(num_inputs == FRAME_HISTORY_LENGTH);
594 for (unsigned i = 0; i < num_inputs; ++i) {
595 if (override_bounce) {
596 inputs.push_back(new NonBouncingYCbCrInput(inout_format, input_ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR));
598 inputs.push_back(new YCbCrInput(inout_format, input_ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR));
600 chain->add_input(inputs.back());
604 vector<Effect *> reverse_inputs(inputs.rbegin(), inputs.rend());
605 chain->add_effect(deinterlace_effect, reverse_inputs);
609 void LiveInputWrapper::connect_signal(int signal_num)
611 if (global_mixer == nullptr) {
616 signal_num = theme->map_signal(signal_num);
618 BufferedFrame first_frame = theme->input_state->buffered_frames[signal_num][0];
619 if (first_frame.frame == nullptr) {
623 unsigned width, height;
625 const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)first_frame.frame->userdata;
626 width = userdata->last_width[first_frame.field_number];
627 height = userdata->last_height[first_frame.field_number];
630 BufferedFrame last_good_frame = first_frame;
631 for (unsigned i = 0; i < inputs.size(); ++i) {
632 BufferedFrame frame = theme->input_state->buffered_frames[signal_num][i];
633 if (frame.frame == nullptr) {
634 // Not enough data; reuse last frame (well, field).
635 // This is suboptimal, but we have nothing better.
636 frame = last_good_frame;
638 const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
640 if (userdata->last_width[frame.field_number] != width ||
641 userdata->last_height[frame.field_number] != height) {
642 // Resolution changed; reuse last frame/field.
643 frame = last_good_frame;
644 userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
647 inputs[i]->set_texture_num(0, userdata->tex_y[frame.field_number]);
648 inputs[i]->set_texture_num(1, userdata->tex_cbcr[frame.field_number]);
649 inputs[i]->set_width(userdata->last_width[frame.field_number]);
650 inputs[i]->set_height(userdata->last_height[frame.field_number]);
652 last_good_frame = frame;
656 BufferedFrame frame = theme->input_state->buffered_frames[signal_num][0];
657 CHECK(deinterlace_effect->set_int("current_field_position", frame.field_number));
663 int call_num_channels(lua_State *L)
665 lua_getglobal(L, "num_channels");
667 if (lua_pcall(L, 0, 1, 0) != 0) {
668 fprintf(stderr, "error running function `num_channels': %s\n", lua_tostring(L, -1));
672 int num_channels = luaL_checknumber(L, 1);
674 assert(lua_gettop(L) == 0);
680 Theme::Theme(const char *filename, ResourcePool *resource_pool, unsigned num_cards)
681 : resource_pool(resource_pool), num_cards(num_cards)
686 register_class("EffectChain", EffectChain_funcs);
687 register_class("LiveInputWrapper", LiveInputWrapper_funcs);
688 register_class("ImageInput", ImageInput_funcs);
689 register_class("WhiteBalanceEffect", WhiteBalanceEffect_funcs);
690 register_class("ResampleEffect", ResampleEffect_funcs);
691 register_class("PaddingEffect", PaddingEffect_funcs);
692 register_class("IntegralPaddingEffect", IntegralPaddingEffect_funcs);
693 register_class("OverlayEffect", OverlayEffect_funcs);
694 register_class("ResizeEffect", ResizeEffect_funcs);
695 register_class("MixEffect", MixEffect_funcs);
696 register_class("InputStateInfo", InputStateInfo_funcs);
700 if (luaL_dofile(L, filename)) {
701 fprintf(stderr, "error: %s\n", lua_tostring(L, -1));
705 assert(lua_gettop(L) == 0);
707 // Ask it for the number of channels.
708 num_channels = call_num_channels(L);
716 void Theme::register_class(const char *class_name, const luaL_Reg *funcs)
718 assert(lua_gettop(L) == 0);
719 luaL_newmetatable(L, class_name); // mt = {}
720 lua_pushlightuserdata(L, this);
721 luaL_setfuncs(L, funcs, 1); // for (name,f in funcs) { mt[name] = f, with upvalue {theme} }
722 lua_pushvalue(L, -1);
723 lua_setfield(L, -2, "__index"); // mt.__index = mt
724 lua_setglobal(L, class_name); // ClassName = mt
725 assert(lua_gettop(L) == 0);
728 Theme::Chain Theme::get_chain(unsigned num, float t, unsigned width, unsigned height, InputState input_state)
732 unique_lock<mutex> lock(m);
733 assert(lua_gettop(L) == 0);
734 lua_getglobal(L, "get_chain"); /* function to be called */
735 lua_pushnumber(L, num);
736 lua_pushnumber(L, t);
737 lua_pushnumber(L, width);
738 lua_pushnumber(L, height);
739 wrap_lua_object<InputStateInfo>(L, "InputStateInfo", input_state);
741 if (lua_pcall(L, 5, 2, 0) != 0) {
742 fprintf(stderr, "error running function `get_chain': %s\n", lua_tostring(L, -1));
746 chain.chain = (EffectChain *)luaL_checkudata(L, -2, "EffectChain");
747 if (!lua_isfunction(L, -1)) {
748 fprintf(stderr, "Argument #-1 should be a function\n");
751 lua_pushvalue(L, -1);
752 shared_ptr<LuaRefWithDeleter> funcref(new LuaRefWithDeleter(&m, L, luaL_ref(L, LUA_REGISTRYINDEX)));
754 assert(lua_gettop(L) == 0);
756 chain.setup_chain = [this, funcref, input_state]{
757 unique_lock<mutex> lock(m);
759 this->input_state = &input_state;
761 // Set up state, including connecting signals.
762 lua_rawgeti(L, LUA_REGISTRYINDEX, funcref->get());
763 if (lua_pcall(L, 0, 0, 0) != 0) {
764 fprintf(stderr, "error running chain setup callback: %s\n", lua_tostring(L, -1));
767 assert(lua_gettop(L) == 0);
770 // TODO: Can we do better, e.g. by running setup_chain() and seeing what it references?
771 // Actually, setup_chain does maybe hold all the references we need now anyway?
772 for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
773 for (unsigned frame_num = 0; frame_num < FRAME_HISTORY_LENGTH; ++frame_num) {
774 chain.input_frames.push_back(input_state.buffered_frames[card_index][frame_num].frame);
781 string Theme::get_channel_name(unsigned channel)
783 unique_lock<mutex> lock(m);
784 lua_getglobal(L, "channel_name");
785 lua_pushnumber(L, channel);
786 if (lua_pcall(L, 1, 1, 0) != 0) {
787 fprintf(stderr, "error running function `channel_name': %s\n", lua_tostring(L, -1));
791 string ret = lua_tostring(L, -1);
793 assert(lua_gettop(L) == 0);
797 int Theme::get_channel_signal(unsigned channel)
799 unique_lock<mutex> lock(m);
800 lua_getglobal(L, "channel_signal");
801 lua_pushnumber(L, channel);
802 if (lua_pcall(L, 1, 1, 0) != 0) {
803 fprintf(stderr, "error running function `channel_signal': %s\n", lua_tostring(L, -1));
807 int ret = luaL_checknumber(L, 1);
809 assert(lua_gettop(L) == 0);
813 std::string Theme::get_channel_color(unsigned channel)
815 unique_lock<mutex> lock(m);
816 lua_getglobal(L, "channel_color");
817 lua_pushnumber(L, channel);
818 if (lua_pcall(L, 1, 1, 0) != 0) {
819 fprintf(stderr, "error running function `channel_color': %s\n", lua_tostring(L, -1));
823 std::string ret = checkstdstring(L, -1);
825 assert(lua_gettop(L) == 0);
829 bool Theme::get_supports_set_wb(unsigned channel)
831 unique_lock<mutex> lock(m);
832 lua_getglobal(L, "supports_set_wb");
833 lua_pushnumber(L, channel);
834 if (lua_pcall(L, 1, 1, 0) != 0) {
835 fprintf(stderr, "error running function `supports_set_wb': %s\n", lua_tostring(L, -1));
839 bool ret = checkbool(L, -1);
841 assert(lua_gettop(L) == 0);
845 void Theme::set_wb(unsigned channel, double r, double g, double b)
847 unique_lock<mutex> lock(m);
848 lua_getglobal(L, "set_wb");
849 lua_pushnumber(L, channel);
850 lua_pushnumber(L, r);
851 lua_pushnumber(L, g);
852 lua_pushnumber(L, b);
853 if (lua_pcall(L, 4, 0, 0) != 0) {
854 fprintf(stderr, "error running function `set_wb': %s\n", lua_tostring(L, -1));
858 assert(lua_gettop(L) == 0);
861 vector<string> Theme::get_transition_names(float t)
863 unique_lock<mutex> lock(m);
864 lua_getglobal(L, "get_transitions");
865 lua_pushnumber(L, t);
866 if (lua_pcall(L, 1, 1, 0) != 0) {
867 fprintf(stderr, "error running function `get_transitions': %s\n", lua_tostring(L, -1));
873 while (lua_next(L, -2) != 0) {
874 ret.push_back(lua_tostring(L, -1));
878 assert(lua_gettop(L) == 0);
882 int Theme::map_signal(int signal_num)
884 unique_lock<mutex> lock(map_m);
885 if (signal_to_card_mapping.count(signal_num)) {
886 return signal_to_card_mapping[signal_num];
888 if (signal_num >= int(num_cards)) {
889 fprintf(stderr, "WARNING: Theme asked for input %d, but we only have %u card(s).\n", signal_num, num_cards);
890 fprintf(stderr, "Mapping to card %d instead.\n", signal_num % num_cards);
892 signal_to_card_mapping[signal_num] = signal_num % num_cards;
893 return signal_num % num_cards;
896 void Theme::set_signal_mapping(int signal_num, int card_num)
898 unique_lock<mutex> lock(map_m);
899 assert(card_num < int(num_cards));
900 signal_to_card_mapping[signal_num] = card_num;
903 void Theme::transition_clicked(int transition_num, float t)
905 unique_lock<mutex> lock(m);
906 lua_getglobal(L, "transition_clicked");
907 lua_pushnumber(L, transition_num);
908 lua_pushnumber(L, t);
910 if (lua_pcall(L, 2, 0, 0) != 0) {
911 fprintf(stderr, "error running function `transition_clicked': %s\n", lua_tostring(L, -1));
914 assert(lua_gettop(L) == 0);
917 void Theme::channel_clicked(int preview_num)
919 unique_lock<mutex> lock(m);
920 lua_getglobal(L, "channel_clicked");
921 lua_pushnumber(L, preview_num);
923 if (lua_pcall(L, 1, 0, 0) != 0) {
924 fprintf(stderr, "error running function `channel_clicked': %s\n", lua_tostring(L, -1));
927 assert(lua_gettop(L) == 0);