4 #include <bmusb/bmusb.h>
8 #include <movit/effect.h>
9 #include <movit/effect_chain.h>
10 #include <movit/image_format.h>
11 #include <movit/mix_effect.h>
12 #include <movit/multiply_effect.h>
13 #include <movit/overlay_effect.h>
14 #include <movit/padding_effect.h>
15 #include <movit/resample_effect.h>
16 #include <movit/resize_effect.h>
17 #include <movit/util.h>
18 #include <movit/white_balance_effect.h>
19 #include <movit/ycbcr.h>
20 #include <movit/ycbcr_input.h>
29 #include "deinterlace_effect.h"
31 #include "image_input.h"
33 #include "input_state.h"
34 #include "pbo_frame_allocator.h"
43 using namespace movit;
45 extern Mixer *global_mixer;
49 // Contains basically the same data as InputState, but does not hold on to
50 // a reference to the frames. This is important so that we can release them
51 // without having to wait for Lua's GC.
52 struct InputStateInfo {
53 InputStateInfo(const InputState& input_state);
55 unsigned last_width[MAX_VIDEO_CARDS], last_height[MAX_VIDEO_CARDS];
56 bool last_interlaced[MAX_VIDEO_CARDS], last_has_signal[MAX_VIDEO_CARDS], last_is_connected[MAX_VIDEO_CARDS];
57 unsigned last_frame_rate_nom[MAX_VIDEO_CARDS], last_frame_rate_den[MAX_VIDEO_CARDS];
60 InputStateInfo::InputStateInfo(const InputState &input_state)
62 for (unsigned signal_num = 0; signal_num < MAX_VIDEO_CARDS; ++signal_num) {
63 BufferedFrame frame = input_state.buffered_frames[signal_num][0];
64 if (frame.frame == nullptr) {
65 last_width[signal_num] = last_height[signal_num] = 0;
66 last_interlaced[signal_num] = false;
67 last_has_signal[signal_num] = false;
68 last_is_connected[signal_num] = false;
71 const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
72 last_width[signal_num] = userdata->last_width[frame.field_number];
73 last_height[signal_num] = userdata->last_height[frame.field_number];
74 last_interlaced[signal_num] = userdata->last_interlaced;
75 last_has_signal[signal_num] = userdata->last_has_signal;
76 last_is_connected[signal_num] = userdata->last_is_connected;
77 last_frame_rate_nom[signal_num] = userdata->last_frame_rate_nom;
78 last_frame_rate_den[signal_num] = userdata->last_frame_rate_den;
82 class LuaRefWithDeleter {
84 LuaRefWithDeleter(mutex *m, lua_State *L, int ref) : m(m), L(L), ref(ref) {}
85 ~LuaRefWithDeleter() {
86 unique_lock<mutex> lock(*m);
87 luaL_unref(L, LUA_REGISTRYINDEX, ref);
89 int get() const { return ref; }
92 LuaRefWithDeleter(const LuaRefWithDeleter &) = delete;
99 template<class T, class... Args>
100 int wrap_lua_object(lua_State* L, const char *class_name, Args&&... args)
102 // Construct the C++ object and put it on the stack.
103 void *mem = lua_newuserdata(L, sizeof(T));
104 new(mem) T(forward<Args>(args)...);
106 // Look up the metatable named <class_name>, and set it on the new object.
107 luaL_getmetatable(L, class_name);
108 lua_setmetatable(L, -2);
113 // Like wrap_lua_object, but the object is not owned by Lua; ie. it's not freed
114 // by Lua GC. This is typically the case for Effects, which are owned by EffectChain
115 // and expected to be destructed by it. The object will be of type T** instead of T*
116 // when exposed to Lua.
118 // Note that we currently leak if you allocate an Effect in this way and never call
119 // add_effect. We should see if there's a way to e.g. set __gc on it at construction time
120 // and then release that once add_effect() takes ownership.
121 template<class T, class... Args>
122 int wrap_lua_object_nonowned(lua_State* L, const char *class_name, Args&&... args)
124 // Construct the pointer ot the C++ object and put it on the stack.
125 T **obj = (T **)lua_newuserdata(L, sizeof(T *));
126 *obj = new T(forward<Args>(args)...);
128 // Look up the metatable named <class_name>, and set it on the new object.
129 luaL_getmetatable(L, class_name);
130 lua_setmetatable(L, -2);
135 Theme *get_theme_updata(lua_State* L)
137 luaL_checktype(L, lua_upvalueindex(1), LUA_TLIGHTUSERDATA);
138 return (Theme *)lua_touserdata(L, lua_upvalueindex(1));
141 Effect *get_effect(lua_State *L, int idx)
143 if (luaL_testudata(L, idx, "WhiteBalanceEffect") ||
144 luaL_testudata(L, idx, "ResampleEffect") ||
145 luaL_testudata(L, idx, "PaddingEffect") ||
146 luaL_testudata(L, idx, "IntegralPaddingEffect") ||
147 luaL_testudata(L, idx, "OverlayEffect") ||
148 luaL_testudata(L, idx, "ResizeEffect") ||
149 luaL_testudata(L, idx, "MultiplyEffect") ||
150 luaL_testudata(L, idx, "MixEffect") ||
151 luaL_testudata(L, idx, "ImageInput")) {
152 return *(Effect **)lua_touserdata(L, idx);
154 luaL_error(L, "Error: Index #%d was not an Effect type\n", idx);
158 InputStateInfo *get_input_state_info(lua_State *L, int idx)
160 if (luaL_testudata(L, idx, "InputStateInfo")) {
161 return (InputStateInfo *)lua_touserdata(L, idx);
163 luaL_error(L, "Error: Index #%d was not InputStateInfo\n", idx);
167 bool checkbool(lua_State* L, int idx)
169 luaL_checktype(L, idx, LUA_TBOOLEAN);
170 return lua_toboolean(L, idx);
173 string checkstdstring(lua_State *L, int index)
176 const char* cstr = lua_tolstring(L, index, &len);
177 return string(cstr, len);
180 int EffectChain_new(lua_State* L)
182 assert(lua_gettop(L) == 2);
183 Theme *theme = get_theme_updata(L);
184 int aspect_w = luaL_checknumber(L, 1);
185 int aspect_h = luaL_checknumber(L, 2);
187 return wrap_lua_object<EffectChain>(L, "EffectChain", aspect_w, aspect_h, theme->get_resource_pool());
190 int EffectChain_gc(lua_State* L)
192 assert(lua_gettop(L) == 1);
193 EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
194 chain->~EffectChain();
198 int EffectChain_add_live_input(lua_State* L)
200 assert(lua_gettop(L) == 3);
201 Theme *theme = get_theme_updata(L);
202 EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
203 bool override_bounce = checkbool(L, 2);
204 bool deinterlace = checkbool(L, 3);
205 return wrap_lua_object<LiveInputWrapper>(L, "LiveInputWrapper", theme, chain, override_bounce, deinterlace);
208 int EffectChain_add_effect(lua_State* L)
210 assert(lua_gettop(L) >= 2);
211 EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
213 // TODO: Better error reporting.
214 Effect *effect = get_effect(L, 2);
215 if (lua_gettop(L) == 2) {
216 if (effect->num_inputs() == 0) {
217 chain->add_input((Input *)effect);
219 chain->add_effect(effect);
222 vector<Effect *> inputs;
223 for (int idx = 3; idx <= lua_gettop(L); ++idx) {
224 if (luaL_testudata(L, idx, "LiveInputWrapper")) {
225 LiveInputWrapper *input = (LiveInputWrapper *)lua_touserdata(L, idx);
226 inputs.push_back(input->get_effect());
228 inputs.push_back(get_effect(L, idx));
231 chain->add_effect(effect, inputs);
234 lua_settop(L, 2); // Return the effect itself.
236 // Make sure Lua doesn't garbage-collect it away.
237 lua_pushvalue(L, -1);
238 luaL_ref(L, LUA_REGISTRYINDEX); // TODO: leak?
243 int EffectChain_finalize(lua_State* L)
245 assert(lua_gettop(L) == 2);
246 EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
247 bool is_main_chain = checkbool(L, 2);
249 // Add outputs as needed.
250 // NOTE: If you change any details about the output format, you will need to
251 // also update what's given to the muxer (HTTPD::Mux constructor) and
252 // what's put in the H.264 stream (sps_rbsp()).
253 ImageFormat inout_format;
254 inout_format.color_space = COLORSPACE_REC_709;
256 // Output gamma is tricky. We should output Rec. 709 for TV, except that
257 // we expect to run with web players and others that don't really care and
258 // just output with no conversion. So that means we'll need to output sRGB,
259 // even though H.264 has no setting for that (we use “unspecified”).
260 inout_format.gamma_curve = GAMMA_sRGB;
263 YCbCrFormat output_ycbcr_format;
264 // We actually output 4:2:0 and/or 4:2:2 in the end, but chroma subsampling
265 // happens in a pass not run by Movit (see ChromaSubsampler::subsample_chroma()).
266 output_ycbcr_format.chroma_subsampling_x = 1;
267 output_ycbcr_format.chroma_subsampling_y = 1;
269 // This will be overridden if HDMI/SDI output is in force.
270 if (global_flags.ycbcr_rec709_coefficients) {
271 output_ycbcr_format.luma_coefficients = YCBCR_REC_709;
273 output_ycbcr_format.luma_coefficients = YCBCR_REC_601;
276 output_ycbcr_format.full_range = false;
277 output_ycbcr_format.num_levels = 1 << global_flags.x264_bit_depth;
279 GLenum type = global_flags.x264_bit_depth > 8 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_BYTE;
281 chain->add_ycbcr_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED, output_ycbcr_format, YCBCR_OUTPUT_SPLIT_Y_AND_CBCR, type);
282 chain->add_ycbcr_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED, output_ycbcr_format, YCBCR_OUTPUT_INTERLEAVED, type); // Add a copy where we'll only be using the Y component.
283 chain->set_dither_bits(global_flags.x264_bit_depth > 8 ? 16 : 8);
284 chain->set_output_origin(OUTPUT_ORIGIN_TOP_LEFT);
286 chain->add_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED);
293 int LiveInputWrapper_connect_signal(lua_State* L)
295 assert(lua_gettop(L) == 2);
296 LiveInputWrapper *input = (LiveInputWrapper *)luaL_checkudata(L, 1, "LiveInputWrapper");
297 int signal_num = luaL_checknumber(L, 2);
298 input->connect_signal(signal_num);
302 int ImageInput_new(lua_State* L)
304 assert(lua_gettop(L) == 1);
305 string filename = checkstdstring(L, 1);
306 return wrap_lua_object_nonowned<ImageInput>(L, "ImageInput", filename);
309 int WhiteBalanceEffect_new(lua_State* L)
311 assert(lua_gettop(L) == 0);
312 return wrap_lua_object_nonowned<WhiteBalanceEffect>(L, "WhiteBalanceEffect");
315 int ResampleEffect_new(lua_State* L)
317 assert(lua_gettop(L) == 0);
318 return wrap_lua_object_nonowned<ResampleEffect>(L, "ResampleEffect");
321 int PaddingEffect_new(lua_State* L)
323 assert(lua_gettop(L) == 0);
324 return wrap_lua_object_nonowned<PaddingEffect>(L, "PaddingEffect");
327 int IntegralPaddingEffect_new(lua_State* L)
329 assert(lua_gettop(L) == 0);
330 return wrap_lua_object_nonowned<IntegralPaddingEffect>(L, "IntegralPaddingEffect");
333 int OverlayEffect_new(lua_State* L)
335 assert(lua_gettop(L) == 0);
336 return wrap_lua_object_nonowned<OverlayEffect>(L, "OverlayEffect");
339 int ResizeEffect_new(lua_State* L)
341 assert(lua_gettop(L) == 0);
342 return wrap_lua_object_nonowned<ResizeEffect>(L, "ResizeEffect");
345 int MultiplyEffect_new(lua_State* L)
347 assert(lua_gettop(L) == 0);
348 return wrap_lua_object_nonowned<MultiplyEffect>(L, "MultiplyEffect");
351 int MixEffect_new(lua_State* L)
353 assert(lua_gettop(L) == 0);
354 return wrap_lua_object_nonowned<MixEffect>(L, "MixEffect");
357 int InputStateInfo_get_width(lua_State* L)
359 assert(lua_gettop(L) == 2);
360 InputStateInfo *input_state_info = get_input_state_info(L, 1);
361 Theme *theme = get_theme_updata(L);
362 int signal_num = theme->map_signal(luaL_checknumber(L, 2));
363 lua_pushnumber(L, input_state_info->last_width[signal_num]);
367 int InputStateInfo_get_height(lua_State* L)
369 assert(lua_gettop(L) == 2);
370 InputStateInfo *input_state_info = get_input_state_info(L, 1);
371 Theme *theme = get_theme_updata(L);
372 int signal_num = theme->map_signal(luaL_checknumber(L, 2));
373 lua_pushnumber(L, input_state_info->last_height[signal_num]);
377 int InputStateInfo_get_interlaced(lua_State* L)
379 assert(lua_gettop(L) == 2);
380 InputStateInfo *input_state_info = get_input_state_info(L, 1);
381 Theme *theme = get_theme_updata(L);
382 int signal_num = theme->map_signal(luaL_checknumber(L, 2));
383 lua_pushboolean(L, input_state_info->last_interlaced[signal_num]);
387 int InputStateInfo_get_has_signal(lua_State* L)
389 assert(lua_gettop(L) == 2);
390 InputStateInfo *input_state_info = get_input_state_info(L, 1);
391 Theme *theme = get_theme_updata(L);
392 int signal_num = theme->map_signal(luaL_checknumber(L, 2));
393 lua_pushboolean(L, input_state_info->last_has_signal[signal_num]);
397 int InputStateInfo_get_is_connected(lua_State* L)
399 assert(lua_gettop(L) == 2);
400 InputStateInfo *input_state_info = get_input_state_info(L, 1);
401 Theme *theme = get_theme_updata(L);
402 int signal_num = theme->map_signal(luaL_checknumber(L, 2));
403 lua_pushboolean(L, input_state_info->last_is_connected[signal_num]);
407 int InputStateInfo_get_frame_rate_nom(lua_State* L)
409 assert(lua_gettop(L) == 2);
410 InputStateInfo *input_state_info = get_input_state_info(L, 1);
411 Theme *theme = get_theme_updata(L);
412 int signal_num = theme->map_signal(luaL_checknumber(L, 2));
413 lua_pushnumber(L, input_state_info->last_frame_rate_nom[signal_num]);
417 int InputStateInfo_get_frame_rate_den(lua_State* L)
419 assert(lua_gettop(L) == 2);
420 InputStateInfo *input_state_info = get_input_state_info(L, 1);
421 Theme *theme = get_theme_updata(L);
422 int signal_num = theme->map_signal(luaL_checknumber(L, 2));
423 lua_pushnumber(L, input_state_info->last_frame_rate_den[signal_num]);
427 int Effect_set_float(lua_State *L)
429 assert(lua_gettop(L) == 3);
430 Effect *effect = (Effect *)get_effect(L, 1);
431 string key = checkstdstring(L, 2);
432 float value = luaL_checknumber(L, 3);
433 if (!effect->set_float(key, value)) {
434 luaL_error(L, "Effect refused set_float(\"%s\", %d) (invalid key?)", key.c_str(), int(value));
439 int Effect_set_int(lua_State *L)
441 assert(lua_gettop(L) == 3);
442 Effect *effect = (Effect *)get_effect(L, 1);
443 string key = checkstdstring(L, 2);
444 float value = luaL_checknumber(L, 3);
445 if (!effect->set_int(key, value)) {
446 luaL_error(L, "Effect refused set_int(\"%s\", %d) (invalid key?)", key.c_str(), int(value));
451 int Effect_set_vec3(lua_State *L)
453 assert(lua_gettop(L) == 5);
454 Effect *effect = (Effect *)get_effect(L, 1);
455 string key = checkstdstring(L, 2);
457 v[0] = luaL_checknumber(L, 3);
458 v[1] = luaL_checknumber(L, 4);
459 v[2] = luaL_checknumber(L, 5);
460 if (!effect->set_vec3(key, v)) {
461 luaL_error(L, "Effect refused set_vec3(\"%s\", %f, %f, %f) (invalid key?)", key.c_str(),
467 int Effect_set_vec4(lua_State *L)
469 assert(lua_gettop(L) == 6);
470 Effect *effect = (Effect *)get_effect(L, 1);
471 string key = checkstdstring(L, 2);
473 v[0] = luaL_checknumber(L, 3);
474 v[1] = luaL_checknumber(L, 4);
475 v[2] = luaL_checknumber(L, 5);
476 v[3] = luaL_checknumber(L, 6);
477 if (!effect->set_vec4(key, v)) {
478 luaL_error(L, "Effect refused set_vec4(\"%s\", %f, %f, %f, %f) (invalid key?)", key.c_str(),
479 v[0], v[1], v[2], v[3]);
484 const luaL_Reg EffectChain_funcs[] = {
485 { "new", EffectChain_new },
486 { "__gc", EffectChain_gc },
487 { "add_live_input", EffectChain_add_live_input },
488 { "add_effect", EffectChain_add_effect },
489 { "finalize", EffectChain_finalize },
493 const luaL_Reg LiveInputWrapper_funcs[] = {
494 { "connect_signal", LiveInputWrapper_connect_signal },
498 const luaL_Reg ImageInput_funcs[] = {
499 { "new", ImageInput_new },
500 { "set_float", Effect_set_float },
501 { "set_int", Effect_set_int },
502 { "set_vec3", Effect_set_vec3 },
503 { "set_vec4", Effect_set_vec4 },
507 const luaL_Reg WhiteBalanceEffect_funcs[] = {
508 { "new", WhiteBalanceEffect_new },
509 { "set_float", Effect_set_float },
510 { "set_int", Effect_set_int },
511 { "set_vec3", Effect_set_vec3 },
512 { "set_vec4", Effect_set_vec4 },
516 const luaL_Reg ResampleEffect_funcs[] = {
517 { "new", ResampleEffect_new },
518 { "set_float", Effect_set_float },
519 { "set_int", Effect_set_int },
520 { "set_vec3", Effect_set_vec3 },
521 { "set_vec4", Effect_set_vec4 },
525 const luaL_Reg PaddingEffect_funcs[] = {
526 { "new", PaddingEffect_new },
527 { "set_float", Effect_set_float },
528 { "set_int", Effect_set_int },
529 { "set_vec3", Effect_set_vec3 },
530 { "set_vec4", Effect_set_vec4 },
534 const luaL_Reg IntegralPaddingEffect_funcs[] = {
535 { "new", IntegralPaddingEffect_new },
536 { "set_float", Effect_set_float },
537 { "set_int", Effect_set_int },
538 { "set_vec3", Effect_set_vec3 },
539 { "set_vec4", Effect_set_vec4 },
543 const luaL_Reg OverlayEffect_funcs[] = {
544 { "new", OverlayEffect_new },
545 { "set_float", Effect_set_float },
546 { "set_int", Effect_set_int },
547 { "set_vec3", Effect_set_vec3 },
548 { "set_vec4", Effect_set_vec4 },
552 const luaL_Reg ResizeEffect_funcs[] = {
553 { "new", ResizeEffect_new },
554 { "set_float", Effect_set_float },
555 { "set_int", Effect_set_int },
556 { "set_vec3", Effect_set_vec3 },
557 { "set_vec4", Effect_set_vec4 },
561 const luaL_Reg MultiplyEffect_funcs[] = {
562 { "new", MultiplyEffect_new },
563 { "set_float", Effect_set_float },
564 { "set_int", Effect_set_int },
565 { "set_vec3", Effect_set_vec3 },
566 { "set_vec4", Effect_set_vec4 },
570 const luaL_Reg MixEffect_funcs[] = {
571 { "new", MixEffect_new },
572 { "set_float", Effect_set_float },
573 { "set_int", Effect_set_int },
574 { "set_vec3", Effect_set_vec3 },
575 { "set_vec4", Effect_set_vec4 },
579 const luaL_Reg InputStateInfo_funcs[] = {
580 { "get_width", InputStateInfo_get_width },
581 { "get_height", InputStateInfo_get_height },
582 { "get_interlaced", InputStateInfo_get_interlaced },
583 { "get_has_signal", InputStateInfo_get_has_signal },
584 { "get_is_connected", InputStateInfo_get_is_connected },
585 { "get_frame_rate_nom", InputStateInfo_get_frame_rate_nom },
586 { "get_frame_rate_den", InputStateInfo_get_frame_rate_den },
592 LiveInputWrapper::LiveInputWrapper(Theme *theme, EffectChain *chain, bool override_bounce, bool deinterlace)
594 deinterlace(deinterlace)
596 ImageFormat inout_format;
597 inout_format.color_space = COLORSPACE_sRGB;
599 // Gamma curve depends on the input signal, and we don't really get any
600 // indications. A camera would be expected to do Rec. 709, but
601 // I haven't checked if any do in practice. However, computers _do_ output
602 // in sRGB gamma (ie., they don't convert from sRGB to Rec. 709), and
603 // I wouldn't really be surprised if most non-professional cameras do, too.
604 // So we pick sRGB as the least evil here.
605 inout_format.gamma_curve = GAMMA_sRGB;
607 // The Blackmagic driver docs claim that the device outputs Y'CbCr
608 // according to Rec. 601, but practical testing indicates it definitely
609 // is Rec. 709 (at least up to errors attributable to rounding errors).
610 // Perhaps 601 was only to indicate the subsampling positions, not the
611 // colorspace itself? Tested with a Lenovo X1 gen 3 as input.
612 YCbCrFormat input_ycbcr_format;
613 input_ycbcr_format.chroma_subsampling_x = global_flags.ten_bit_input ? 1 : 2;
614 input_ycbcr_format.chroma_subsampling_y = 1;
615 input_ycbcr_format.num_levels = global_flags.ten_bit_input ? 1024 : 256;
616 input_ycbcr_format.cb_x_position = 0.0;
617 input_ycbcr_format.cr_x_position = 0.0;
618 input_ycbcr_format.cb_y_position = 0.5;
619 input_ycbcr_format.cr_y_position = 0.5;
620 input_ycbcr_format.luma_coefficients = YCBCR_REC_709;
621 input_ycbcr_format.full_range = false;
625 deinterlace_effect = new movit::DeinterlaceEffect();
627 // As per the comments in deinterlace_effect.h, we turn this off.
628 // The most likely interlaced input for us is either a camera
629 // (where it's fine to turn it off) or a laptop (where it _should_
631 CHECK(deinterlace_effect->set_int("enable_spatial_interlacing_check", 0));
633 num_inputs = deinterlace_effect->num_inputs();
634 assert(num_inputs == FRAME_HISTORY_LENGTH);
638 for (unsigned i = 0; i < num_inputs; ++i) {
639 // When using 10-bit input, we're converting to interleaved through v210Converter.
640 YCbCrInputSplitting splitting = global_flags.ten_bit_input ? YCBCR_INPUT_INTERLEAVED : YCBCR_INPUT_SPLIT_Y_AND_CBCR;
641 if (override_bounce) {
642 inputs.push_back(new NonBouncingYCbCrInput(inout_format, input_ycbcr_format, global_flags.width, global_flags.height, splitting));
644 inputs.push_back(new YCbCrInput(inout_format, input_ycbcr_format, global_flags.width, global_flags.height, splitting));
646 chain->add_input(inputs.back());
650 vector<Effect *> reverse_inputs(inputs.rbegin(), inputs.rend());
651 chain->add_effect(deinterlace_effect, reverse_inputs);
655 void LiveInputWrapper::connect_signal(int signal_num)
657 if (global_mixer == nullptr) {
662 signal_num = theme->map_signal(signal_num);
664 BufferedFrame first_frame = theme->input_state->buffered_frames[signal_num][0];
665 if (first_frame.frame == nullptr) {
669 unsigned width, height;
671 const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)first_frame.frame->userdata;
672 width = userdata->last_width[first_frame.field_number];
673 height = userdata->last_height[first_frame.field_number];
676 BufferedFrame last_good_frame = first_frame;
677 for (unsigned i = 0; i < inputs.size(); ++i) {
678 BufferedFrame frame = theme->input_state->buffered_frames[signal_num][i];
679 if (frame.frame == nullptr) {
680 // Not enough data; reuse last frame (well, field).
681 // This is suboptimal, but we have nothing better.
682 frame = last_good_frame;
684 const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
686 if (userdata->last_width[frame.field_number] != width ||
687 userdata->last_height[frame.field_number] != height) {
688 // Resolution changed; reuse last frame/field.
689 frame = last_good_frame;
690 userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
693 if (global_flags.ten_bit_input) {
694 inputs[i]->set_texture_num(0, userdata->tex_444[frame.field_number]);
696 inputs[i]->set_texture_num(0, userdata->tex_y[frame.field_number]);
697 inputs[i]->set_texture_num(1, userdata->tex_cbcr[frame.field_number]);
699 inputs[i]->set_width(userdata->last_width[frame.field_number]);
700 inputs[i]->set_height(userdata->last_height[frame.field_number]);
702 last_good_frame = frame;
706 BufferedFrame frame = theme->input_state->buffered_frames[signal_num][0];
707 CHECK(deinterlace_effect->set_int("current_field_position", frame.field_number));
713 int call_num_channels(lua_State *L)
715 lua_getglobal(L, "num_channels");
717 if (lua_pcall(L, 0, 1, 0) != 0) {
718 fprintf(stderr, "error running function `num_channels': %s\n", lua_tostring(L, -1));
722 int num_channels = luaL_checknumber(L, 1);
724 assert(lua_gettop(L) == 0);
730 Theme::Theme(const string &filename, const vector<string> &search_dirs, ResourcePool *resource_pool, unsigned num_cards)
731 : resource_pool(resource_pool), num_cards(num_cards), signal_to_card_mapping(global_flags.default_stream_mapping)
736 register_class("EffectChain", EffectChain_funcs);
737 register_class("LiveInputWrapper", LiveInputWrapper_funcs);
738 register_class("ImageInput", ImageInput_funcs);
739 register_class("WhiteBalanceEffect", WhiteBalanceEffect_funcs);
740 register_class("ResampleEffect", ResampleEffect_funcs);
741 register_class("PaddingEffect", PaddingEffect_funcs);
742 register_class("IntegralPaddingEffect", IntegralPaddingEffect_funcs);
743 register_class("OverlayEffect", OverlayEffect_funcs);
744 register_class("ResizeEffect", ResizeEffect_funcs);
745 register_class("MultiplyEffect", MultiplyEffect_funcs);
746 register_class("MixEffect", MixEffect_funcs);
747 register_class("InputStateInfo", InputStateInfo_funcs);
749 // Run script. Search through all directories until we find a file that will load
750 // (as in, does not return LUA_ERRFILE); then run it. We store load errors
751 // from all the attempts, and show them once we know we can't find any of them.
753 vector<string> errors;
754 bool success = false;
755 for (size_t i = 0; i < search_dirs.size(); ++i) {
756 string path = search_dirs[i] + "/" + filename;
757 int err = luaL_loadfile(L, path.c_str());
759 // Success; actually call the code.
760 if (lua_pcall(L, 0, LUA_MULTRET, 0)) {
761 fprintf(stderr, "Error when running %s: %s\n", path.c_str(), lua_tostring(L, -1));
767 errors.push_back(lua_tostring(L, -1));
769 if (err != LUA_ERRFILE) {
770 // The file actually loaded, but failed to parse somehow. Abort; don't try the next one.
776 for (const string &error : errors) {
777 fprintf(stderr, "%s\n", error.c_str());
781 assert(lua_gettop(L) == 0);
783 // Ask it for the number of channels.
784 num_channels = call_num_channels(L);
792 void Theme::register_class(const char *class_name, const luaL_Reg *funcs)
794 assert(lua_gettop(L) == 0);
795 luaL_newmetatable(L, class_name); // mt = {}
796 lua_pushlightuserdata(L, this);
797 luaL_setfuncs(L, funcs, 1); // for (name,f in funcs) { mt[name] = f, with upvalue {theme} }
798 lua_pushvalue(L, -1);
799 lua_setfield(L, -2, "__index"); // mt.__index = mt
800 lua_setglobal(L, class_name); // ClassName = mt
801 assert(lua_gettop(L) == 0);
804 Theme::Chain Theme::get_chain(unsigned num, float t, unsigned width, unsigned height, InputState input_state)
808 unique_lock<mutex> lock(m);
809 assert(lua_gettop(L) == 0);
810 lua_getglobal(L, "get_chain"); /* function to be called */
811 lua_pushnumber(L, num);
812 lua_pushnumber(L, t);
813 lua_pushnumber(L, width);
814 lua_pushnumber(L, height);
815 wrap_lua_object<InputStateInfo>(L, "InputStateInfo", input_state);
817 if (lua_pcall(L, 5, 2, 0) != 0) {
818 fprintf(stderr, "error running function `get_chain': %s\n", lua_tostring(L, -1));
822 chain.chain = (EffectChain *)luaL_testudata(L, -2, "EffectChain");
823 if (chain.chain == nullptr) {
824 fprintf(stderr, "get_chain() for chain number %d did not return an EffectChain\n",
828 if (!lua_isfunction(L, -1)) {
829 fprintf(stderr, "Argument #-1 should be a function\n");
832 lua_pushvalue(L, -1);
833 shared_ptr<LuaRefWithDeleter> funcref(new LuaRefWithDeleter(&m, L, luaL_ref(L, LUA_REGISTRYINDEX)));
835 assert(lua_gettop(L) == 0);
837 chain.setup_chain = [this, funcref, input_state]{
838 unique_lock<mutex> lock(m);
840 this->input_state = &input_state;
842 // Set up state, including connecting signals.
843 lua_rawgeti(L, LUA_REGISTRYINDEX, funcref->get());
844 if (lua_pcall(L, 0, 0, 0) != 0) {
845 fprintf(stderr, "error running chain setup callback: %s\n", lua_tostring(L, -1));
848 assert(lua_gettop(L) == 0);
851 // TODO: Can we do better, e.g. by running setup_chain() and seeing what it references?
852 // Actually, setup_chain does maybe hold all the references we need now anyway?
853 for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
854 for (unsigned frame_num = 0; frame_num < FRAME_HISTORY_LENGTH; ++frame_num) {
855 chain.input_frames.push_back(input_state.buffered_frames[card_index][frame_num].frame);
862 string Theme::get_channel_name(unsigned channel)
864 unique_lock<mutex> lock(m);
865 lua_getglobal(L, "channel_name");
866 lua_pushnumber(L, channel);
867 if (lua_pcall(L, 1, 1, 0) != 0) {
868 fprintf(stderr, "error running function `channel_name': %s\n", lua_tostring(L, -1));
871 const char *ret = lua_tostring(L, -1);
872 if (ret == nullptr) {
873 fprintf(stderr, "function `channel_name' returned nil for channel %d\n", channel);
879 assert(lua_gettop(L) == 0);
883 int Theme::get_channel_signal(unsigned channel)
885 unique_lock<mutex> lock(m);
886 lua_getglobal(L, "channel_signal");
887 lua_pushnumber(L, channel);
888 if (lua_pcall(L, 1, 1, 0) != 0) {
889 fprintf(stderr, "error running function `channel_signal': %s\n", lua_tostring(L, -1));
893 int ret = luaL_checknumber(L, 1);
895 assert(lua_gettop(L) == 0);
899 std::string Theme::get_channel_color(unsigned channel)
901 unique_lock<mutex> lock(m);
902 lua_getglobal(L, "channel_color");
903 lua_pushnumber(L, channel);
904 if (lua_pcall(L, 1, 1, 0) != 0) {
905 fprintf(stderr, "error running function `channel_color': %s\n", lua_tostring(L, -1));
909 const char *ret = lua_tostring(L, -1);
910 if (ret == nullptr) {
911 fprintf(stderr, "function `channel_color' returned nil for channel %d\n", channel);
917 assert(lua_gettop(L) == 0);
921 bool Theme::get_supports_set_wb(unsigned channel)
923 unique_lock<mutex> lock(m);
924 lua_getglobal(L, "supports_set_wb");
925 lua_pushnumber(L, channel);
926 if (lua_pcall(L, 1, 1, 0) != 0) {
927 fprintf(stderr, "error running function `supports_set_wb': %s\n", lua_tostring(L, -1));
931 bool ret = checkbool(L, -1);
933 assert(lua_gettop(L) == 0);
937 void Theme::set_wb(unsigned channel, double r, double g, double b)
939 unique_lock<mutex> lock(m);
940 lua_getglobal(L, "set_wb");
941 lua_pushnumber(L, channel);
942 lua_pushnumber(L, r);
943 lua_pushnumber(L, g);
944 lua_pushnumber(L, b);
945 if (lua_pcall(L, 4, 0, 0) != 0) {
946 fprintf(stderr, "error running function `set_wb': %s\n", lua_tostring(L, -1));
950 assert(lua_gettop(L) == 0);
953 vector<string> Theme::get_transition_names(float t)
955 unique_lock<mutex> lock(m);
956 lua_getglobal(L, "get_transitions");
957 lua_pushnumber(L, t);
958 if (lua_pcall(L, 1, 1, 0) != 0) {
959 fprintf(stderr, "error running function `get_transitions': %s\n", lua_tostring(L, -1));
965 while (lua_next(L, -2) != 0) {
966 ret.push_back(lua_tostring(L, -1));
970 assert(lua_gettop(L) == 0);
974 int Theme::map_signal(int signal_num)
976 unique_lock<mutex> lock(map_m);
977 if (signal_to_card_mapping.count(signal_num)) {
978 return signal_to_card_mapping[signal_num];
982 if (global_flags.output_card != -1 && num_cards > 1) {
983 // Try to exclude the output card from the default card_index.
984 card_index = signal_num % (num_cards - 1);
985 if (card_index >= global_flags.output_card) {
988 if (signal_num >= int(num_cards - 1)) {
989 fprintf(stderr, "WARNING: Theme asked for input %d, but we only have %u input card(s) (card %d is busy with output).\n",
990 signal_num, num_cards - 1, global_flags.output_card);
991 fprintf(stderr, "Mapping to card %d instead.\n", card_index);
994 card_index = signal_num % num_cards;
995 if (signal_num >= int(num_cards)) {
996 fprintf(stderr, "WARNING: Theme asked for input %d, but we only have %u card(s).\n", signal_num, num_cards);
997 fprintf(stderr, "Mapping to card %d instead.\n", card_index);
1000 signal_to_card_mapping[signal_num] = card_index;
1004 void Theme::set_signal_mapping(int signal_num, int card_num)
1006 unique_lock<mutex> lock(map_m);
1007 assert(card_num < int(num_cards));
1008 signal_to_card_mapping[signal_num] = card_num;
1011 void Theme::transition_clicked(int transition_num, float t)
1013 unique_lock<mutex> lock(m);
1014 lua_getglobal(L, "transition_clicked");
1015 lua_pushnumber(L, transition_num);
1016 lua_pushnumber(L, t);
1018 if (lua_pcall(L, 2, 0, 0) != 0) {
1019 fprintf(stderr, "error running function `transition_clicked': %s\n", lua_tostring(L, -1));
1022 assert(lua_gettop(L) == 0);
1025 void Theme::channel_clicked(int preview_num)
1027 unique_lock<mutex> lock(m);
1028 lua_getglobal(L, "channel_clicked");
1029 lua_pushnumber(L, preview_num);
1031 if (lua_pcall(L, 1, 0, 0) != 0) {
1032 fprintf(stderr, "error running function `channel_clicked': %s\n", lua_tostring(L, -1));
1035 assert(lua_gettop(L) == 0);