]> git.sesse.net Git - nageru/blob - theme.cpp
Do signal mapping in get_width/get_height.
[nageru] / theme.cpp
1 #include "theme.h"
2
3 #include <assert.h>
4 #include <lauxlib.h>
5 #include <lua.h>
6 #include <lualib.h>
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>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <cstddef>
21 #include <new>
22 #include <utility>
23 #include <memory>
24
25 #include "defs.h"
26 #include "image_input.h"
27 #include "mixer.h"
28
29 namespace movit {
30 class ResourcePool;
31 }  // namespace movit
32
33 using namespace std;
34 using namespace movit;
35
36 extern Mixer *global_mixer;
37
38 namespace {
39
40 class LuaRefWithDeleter {
41 public:
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);
46         }
47         int get() const { return ref; }
48
49 private:
50         LuaRefWithDeleter(const LuaRefWithDeleter &) = delete;
51
52         mutex *m;
53         lua_State *L;
54         int ref;
55 };
56
57 template<class T, class... Args>
58 int wrap_lua_object(lua_State* L, const char *class_name, Args&&... args)
59 {
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)...);
63
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);
67
68         return 1;
69 }
70
71 Theme *get_theme_updata(lua_State* L)
72 {       
73         luaL_checktype(L, lua_upvalueindex(1), LUA_TLIGHTUSERDATA);
74         return (Theme *)lua_touserdata(L, lua_upvalueindex(1));
75 }
76
77 Effect *get_effect(lua_State *L, int idx)
78 {
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);
88         }
89         luaL_error(L, "Error: Index #%d was not an Effect type\n", idx);
90         return nullptr;
91 }
92
93 InputState *get_input_state(lua_State *L, int idx)
94 {
95         if (luaL_testudata(L, idx, "InputState")) {
96                 return (InputState *)lua_touserdata(L, idx);
97         }
98         luaL_error(L, "Error: Index #%d was not InputState\n", idx);
99         return nullptr;
100 }
101
102 bool checkbool(lua_State* L, int idx)
103 {
104         luaL_checktype(L, idx, LUA_TBOOLEAN);
105         return lua_toboolean(L, idx);
106 }
107
108 std::string checkstdstring(lua_State *L, int index)
109 {
110         size_t len;
111         const char* cstr = lua_tolstring(L, index, &len);
112         return std::string(cstr, len);
113 }
114
115 int EffectChain_new(lua_State* L)
116 {
117         assert(lua_gettop(L) == 2);
118         int aspect_w = luaL_checknumber(L, 1);
119         int aspect_h = luaL_checknumber(L, 2);
120
121         return wrap_lua_object<EffectChain>(L, "EffectChain", aspect_w, aspect_h);
122 }
123
124 int EffectChain_add_live_input(lua_State* L)
125 {
126         assert(lua_gettop(L) == 2);
127         Theme *theme = get_theme_updata(L);
128         EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
129         bool override_bounce = checkbool(L, 2);
130         return wrap_lua_object<LiveInputWrapper>(L, "LiveInputWrapper", theme, chain, override_bounce);
131 }
132
133 int EffectChain_add_effect(lua_State* L)
134 {
135         assert(lua_gettop(L) >= 2);
136         EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
137
138         // TODO: Better error reporting.
139         Effect *effect = get_effect(L, 2);
140         if (lua_gettop(L) == 2) {
141                 if (effect->num_inputs() == 0) {
142                         chain->add_input((Input *)effect);
143                 } else {
144                         chain->add_effect(effect);
145                 }
146         } else {
147                 vector<Effect *> inputs;
148                 for (int idx = 3; idx <= lua_gettop(L); ++idx) {
149                         if (luaL_testudata(L, idx, "LiveInputWrapper")) {
150                                 LiveInputWrapper *input = (LiveInputWrapper *)lua_touserdata(L, idx);
151                                 inputs.push_back(input->get_input());
152                         } else {
153                                 inputs.push_back(get_effect(L, idx));
154                         }
155                 }
156                 chain->add_effect(effect, inputs);
157         }
158
159         lua_settop(L, 2);  // Return the effect itself.
160
161         // Make sure Lua doesn't garbage-collect it away.
162         lua_pushvalue(L, -1);
163         luaL_ref(L, LUA_REGISTRYINDEX);  // TODO: leak?
164
165         return 1;
166 }
167
168 int EffectChain_finalize(lua_State* L)
169 {
170         assert(lua_gettop(L) == 2);
171         EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
172         bool is_main_chain = checkbool(L, 2);
173
174         // Add outputs as needed.
175         // NOTE: If you change any details about the output format, you will need to
176         // also update what's given to the muxer (HTTPD::Mux constructor) and
177         // what's put in the H.264 stream (sps_rbsp()).
178         ImageFormat inout_format;
179         inout_format.color_space = COLORSPACE_REC_709;
180
181         // Output gamma is tricky. We should output Rec. 709 for TV, except that
182         // we expect to run with web players and others that don't really care and
183         // just output with no conversion. So that means we'll need to output sRGB,
184         // even though H.264 has no setting for that (we use “unspecified”).
185         inout_format.gamma_curve = GAMMA_sRGB;
186
187         if (is_main_chain) {
188                 YCbCrFormat output_ycbcr_format;
189                 // We actually output 4:2:0 in the end, but chroma subsampling
190                 // happens in a pass not run by Movit (see Mixer::subsample_chroma()).
191                 output_ycbcr_format.chroma_subsampling_x = 1;
192                 output_ycbcr_format.chroma_subsampling_y = 1;
193
194                 // Rec. 709 would be the sane thing to do, but it seems many players
195                 // (e.g. MPlayer and VLC) just default to BT.601 coefficients no matter
196                 // what (see discussions in e.g. https://trac.ffmpeg.org/ticket/4978).
197                 // We _do_ set the right flags, though, so that a player that works
198                 // properly doesn't have to guess.
199                 output_ycbcr_format.luma_coefficients = YCBCR_REC_601;
200                 output_ycbcr_format.full_range = false;
201                 output_ycbcr_format.num_levels = 256;
202
203                 chain->add_ycbcr_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED, output_ycbcr_format, YCBCR_OUTPUT_SPLIT_Y_AND_CBCR);
204                 chain->set_dither_bits(8);
205                 chain->set_output_origin(OUTPUT_ORIGIN_TOP_LEFT);
206         }
207         chain->add_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED);
208
209         chain->finalize();
210         return 0;
211 }
212
213 int LiveInputWrapper_connect_signal(lua_State* L)
214 {
215         assert(lua_gettop(L) == 2);
216         LiveInputWrapper *input = (LiveInputWrapper *)luaL_checkudata(L, 1, "LiveInputWrapper");
217         int signal_num = luaL_checknumber(L, 2);
218         input->connect_signal(signal_num);
219         return 0;
220 }
221
222 int ImageInput_new(lua_State* L)
223 {
224         assert(lua_gettop(L) == 1);
225         std::string filename = checkstdstring(L, 1);
226         return wrap_lua_object<ImageInput>(L, "ImageInput", filename);
227 }
228
229 int WhiteBalanceEffect_new(lua_State* L)
230 {
231         assert(lua_gettop(L) == 0);
232         return wrap_lua_object<WhiteBalanceEffect>(L, "WhiteBalanceEffect");
233 }
234
235 int ResampleEffect_new(lua_State* L)
236 {
237         assert(lua_gettop(L) == 0);
238         return wrap_lua_object<ResampleEffect>(L, "ResampleEffect");
239 }
240
241 int PaddingEffect_new(lua_State* L)
242 {
243         assert(lua_gettop(L) == 0);
244         return wrap_lua_object<PaddingEffect>(L, "PaddingEffect");
245 }
246
247 int IntegralPaddingEffect_new(lua_State* L)
248 {
249         assert(lua_gettop(L) == 0);
250         return wrap_lua_object<IntegralPaddingEffect>(L, "IntegralPaddingEffect");
251 }
252
253 int OverlayEffect_new(lua_State* L)
254 {
255         assert(lua_gettop(L) == 0);
256         return wrap_lua_object<OverlayEffect>(L, "OverlayEffect");
257 }
258
259 int ResizeEffect_new(lua_State* L)
260 {
261         assert(lua_gettop(L) == 0);
262         return wrap_lua_object<ResizeEffect>(L, "ResizeEffect");
263 }
264
265 int MixEffect_new(lua_State* L)
266 {
267         assert(lua_gettop(L) == 0);
268         return wrap_lua_object<MixEffect>(L, "MixEffect");
269 }
270
271 int InputState_gc(lua_State* L)
272 {
273         assert(lua_gettop(L) == 1);
274         InputState *input_state = get_input_state(L, 1);
275         input_state->~InputState();
276         return 0;
277 }
278
279 int InputState_get_width(lua_State* L)
280 {
281         assert(lua_gettop(L) == 2);
282         InputState *input_state = (InputState *)lua_touserdata(L, 1);
283         Theme *theme = get_theme_updata(L);
284         int signal_num = theme->map_signal(luaL_checknumber(L, 2));
285         BufferedFrame frame = input_state->buffered_frames[signal_num][0];
286         const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
287         lua_pushnumber(L, userdata->last_width[frame.field_number]);
288         return 1;
289 }
290
291 int InputState_get_height(lua_State* L)
292 {
293         assert(lua_gettop(L) == 2);
294         InputState *input_state = (InputState *)lua_touserdata(L, 1);
295         Theme *theme = get_theme_updata(L);
296         int signal_num = theme->map_signal(luaL_checknumber(L, 2));
297         BufferedFrame frame = input_state->buffered_frames[signal_num][0];
298         const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
299         lua_pushnumber(L, userdata->last_height[frame.field_number]);
300         return 1;
301 }
302
303 int Effect_set_float(lua_State *L)
304 {
305         assert(lua_gettop(L) == 3);
306         Effect *effect = (Effect *)get_effect(L, 1);
307         std::string key = checkstdstring(L, 2);
308         float value = luaL_checknumber(L, 3);
309         if (!effect->set_float(key, value)) {
310                 luaL_error(L, "Effect refused set_float(\"%s\", %d) (invalid key?)", key.c_str(), int(value));
311         }
312         return 0;
313 }
314
315 int Effect_set_int(lua_State *L)
316 {
317         assert(lua_gettop(L) == 3);
318         Effect *effect = (Effect *)get_effect(L, 1);
319         std::string key = checkstdstring(L, 2);
320         float value = luaL_checknumber(L, 3);
321         if (!effect->set_int(key, value)) {
322                 luaL_error(L, "Effect refused set_int(\"%s\", %d) (invalid key?)", key.c_str(), int(value));
323         }
324         return 0;
325 }
326
327 int Effect_set_vec3(lua_State *L)
328 {
329         assert(lua_gettop(L) == 5);
330         Effect *effect = (Effect *)get_effect(L, 1);
331         std::string key = checkstdstring(L, 2);
332         float v[3];
333         v[0] = luaL_checknumber(L, 3);
334         v[1] = luaL_checknumber(L, 4);
335         v[2] = luaL_checknumber(L, 5);
336         if (!effect->set_vec3(key, v)) {
337                 luaL_error(L, "Effect refused set_vec3(\"%s\", %f, %f, %f) (invalid key?)", key.c_str(),
338                         v[0], v[1], v[2]);
339         }
340         return 0;
341 }
342
343 int Effect_set_vec4(lua_State *L)
344 {
345         assert(lua_gettop(L) == 6);
346         Effect *effect = (Effect *)get_effect(L, 1);
347         std::string key = checkstdstring(L, 2);
348         float v[4];
349         v[0] = luaL_checknumber(L, 3);
350         v[1] = luaL_checknumber(L, 4);
351         v[2] = luaL_checknumber(L, 5);
352         v[3] = luaL_checknumber(L, 6);
353         if (!effect->set_vec4(key, v)) {
354                 luaL_error(L, "Effect refused set_vec4(\"%s\", %f, %f, %f, %f) (invalid key?)", key.c_str(),
355                         v[0], v[1], v[2], v[3]);
356         }
357         return 0;
358 }
359
360 const luaL_Reg EffectChain_funcs[] = {
361         { "new", EffectChain_new },
362         { "add_live_input", EffectChain_add_live_input },
363         { "add_effect", EffectChain_add_effect },
364         { "finalize", EffectChain_finalize },
365         { NULL, NULL }
366 };
367
368 const luaL_Reg LiveInputWrapper_funcs[] = {
369         { "connect_signal", LiveInputWrapper_connect_signal },
370         { NULL, NULL }
371 };
372
373 const luaL_Reg ImageInput_funcs[] = {
374         { "new", ImageInput_new },
375         { "set_float", Effect_set_float },
376         { "set_int", Effect_set_int },
377         { "set_vec3", Effect_set_vec3 },
378         { "set_vec4", Effect_set_vec4 },
379         { NULL, NULL }
380 };
381
382 const luaL_Reg WhiteBalanceEffect_funcs[] = {
383         { "new", WhiteBalanceEffect_new },
384         { "set_float", Effect_set_float },
385         { "set_int", Effect_set_int },
386         { "set_vec3", Effect_set_vec3 },
387         { "set_vec4", Effect_set_vec4 },
388         { NULL, NULL }
389 };
390
391 const luaL_Reg ResampleEffect_funcs[] = {
392         { "new", ResampleEffect_new },
393         { "set_float", Effect_set_float },
394         { "set_int", Effect_set_int },
395         { "set_vec3", Effect_set_vec3 },
396         { "set_vec4", Effect_set_vec4 },
397         { NULL, NULL }
398 };
399
400 const luaL_Reg PaddingEffect_funcs[] = {
401         { "new", PaddingEffect_new },
402         { "set_float", Effect_set_float },
403         { "set_int", Effect_set_int },
404         { "set_vec3", Effect_set_vec3 },
405         { "set_vec4", Effect_set_vec4 },
406         { NULL, NULL }
407 };
408
409 const luaL_Reg IntegralPaddingEffect_funcs[] = {
410         { "new", IntegralPaddingEffect_new },
411         { "set_float", Effect_set_float },
412         { "set_int", Effect_set_int },
413         { "set_vec3", Effect_set_vec3 },
414         { "set_vec4", Effect_set_vec4 },
415         { NULL, NULL }
416 };
417
418 const luaL_Reg OverlayEffect_funcs[] = {
419         { "new", OverlayEffect_new },
420         { "set_float", Effect_set_float },
421         { "set_int", Effect_set_int },
422         { "set_vec3", Effect_set_vec3 },
423         { "set_vec4", Effect_set_vec4 },
424         { NULL, NULL }
425 };
426
427 const luaL_Reg ResizeEffect_funcs[] = {
428         { "new", ResizeEffect_new },
429         { "set_float", Effect_set_float },
430         { "set_int", Effect_set_int },
431         { "set_vec3", Effect_set_vec3 },
432         { "set_vec4", Effect_set_vec4 },
433         { NULL, NULL }
434 };
435
436 const luaL_Reg MixEffect_funcs[] = {
437         { "new", MixEffect_new },
438         { "set_float", Effect_set_float },
439         { "set_int", Effect_set_int },
440         { "set_vec3", Effect_set_vec3 },
441         { "set_vec4", Effect_set_vec4 },
442         { NULL, NULL }
443 };
444
445 const luaL_Reg InputState_funcs[] = {
446         { "__gc", InputState_gc },
447         { "get_width", InputState_get_width },
448         { "get_height", InputState_get_height },
449         { NULL, NULL }
450 };
451
452 }  // namespace
453
454 LiveInputWrapper::LiveInputWrapper(Theme *theme, EffectChain *chain, bool override_bounce)
455         : theme(theme)
456 {
457         ImageFormat inout_format;
458         inout_format.color_space = COLORSPACE_sRGB;
459
460         // Gamma curve depends on the input signal, and we don't really get any
461         // indications. A camera would be expected to do Rec. 709, but
462         // I haven't checked if any do in practice. However, computers _do_ output
463         // in sRGB gamma (ie., they don't convert from sRGB to Rec. 709), and
464         // I wouldn't really be surprised if most non-professional cameras do, too.
465         // So we pick sRGB as the least evil here.
466         inout_format.gamma_curve = GAMMA_sRGB;
467
468         // The Blackmagic driver docs claim that the device outputs Y'CbCr
469         // according to Rec. 601, but practical testing indicates it definitely
470         // is Rec. 709 (at least up to errors attributable to rounding errors).
471         // Perhaps 601 was only to indicate the subsampling positions, not the
472         // colorspace itself? Tested with a Lenovo X1 gen 3 as input.
473         YCbCrFormat input_ycbcr_format;
474         input_ycbcr_format.chroma_subsampling_x = 2;
475         input_ycbcr_format.chroma_subsampling_y = 1;
476         input_ycbcr_format.cb_x_position = 0.0;
477         input_ycbcr_format.cr_x_position = 0.0;
478         input_ycbcr_format.cb_y_position = 0.5;
479         input_ycbcr_format.cr_y_position = 0.5;
480         input_ycbcr_format.luma_coefficients = YCBCR_REC_709;
481         input_ycbcr_format.full_range = false;
482
483         if (override_bounce) {
484                 input = new NonBouncingYCbCrInput(inout_format, input_ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
485         } else {
486                 input = new YCbCrInput(inout_format, input_ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
487         }
488         chain->add_input(input);
489 }
490
491 void LiveInputWrapper::connect_signal(int signal_num)
492 {
493         if (global_mixer == nullptr) {
494                 // No data yet.
495                 return;
496         }
497
498         signal_num = theme->map_signal(signal_num);
499
500         BufferedFrame frame = theme->input_state->buffered_frames[signal_num][0];
501         const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
502
503         input->set_texture_num(0, userdata->tex_y[frame.field_number]);
504         input->set_texture_num(1, userdata->tex_cbcr[frame.field_number]);
505         input->set_width(userdata->last_width[frame.field_number]);
506         input->set_height(userdata->last_height[frame.field_number]);
507 }
508
509 Theme::Theme(const char *filename, ResourcePool *resource_pool, unsigned num_cards)
510         : resource_pool(resource_pool), num_cards(num_cards)
511 {
512         L = luaL_newstate();
513         luaL_openlibs(L);
514
515         register_class("EffectChain", EffectChain_funcs); 
516         register_class("LiveInputWrapper", LiveInputWrapper_funcs); 
517         register_class("ImageInput", ImageInput_funcs);
518         register_class("WhiteBalanceEffect", WhiteBalanceEffect_funcs);
519         register_class("ResampleEffect", ResampleEffect_funcs);
520         register_class("PaddingEffect", PaddingEffect_funcs);
521         register_class("IntegralPaddingEffect", IntegralPaddingEffect_funcs);
522         register_class("OverlayEffect", OverlayEffect_funcs);
523         register_class("ResizeEffect", ResizeEffect_funcs);
524         register_class("MixEffect", MixEffect_funcs);
525         register_class("InputState", InputState_funcs);
526
527         // Run script.
528         lua_settop(L, 0);
529         if (luaL_dofile(L, filename)) {
530                 fprintf(stderr, "error: %s\n", lua_tostring(L, -1));
531                 lua_pop(L, 1);
532                 exit(1);
533         }
534         assert(lua_gettop(L) == 0);
535
536         // Ask it for the number of channels.
537         lua_getglobal(L, "num_channels");
538
539         if (lua_pcall(L, 0, 1, 0) != 0) {
540                 fprintf(stderr, "error running function `num_channels': %s\n", lua_tostring(L, -1));
541                 exit(1);
542         }
543
544         num_channels = luaL_checknumber(L, 1);
545         lua_pop(L, 1);
546         assert(lua_gettop(L) == 0);
547 }
548
549 void Theme::register_class(const char *class_name, const luaL_Reg *funcs)
550 {
551         assert(lua_gettop(L) == 0);
552         luaL_newmetatable(L, class_name);  // mt = {}
553         lua_pushlightuserdata(L, this);
554         luaL_setfuncs(L, funcs, 1);        // for (name,f in funcs) { mt[name] = f, with upvalue {theme} }
555         lua_pushvalue(L, -1);
556         lua_setfield(L, -2, "__index");    // mt.__index = mt
557         lua_setglobal(L, class_name);      // ClassName = mt
558         assert(lua_gettop(L) == 0);
559 }
560
561 Theme::Chain Theme::get_chain(unsigned num, float t, unsigned width, unsigned height, InputState input_state) 
562 {
563         Chain chain;
564
565         unique_lock<mutex> lock(m);
566         assert(lua_gettop(L) == 0);
567         lua_getglobal(L, "get_chain");  /* function to be called */
568         lua_pushnumber(L, num);
569         lua_pushnumber(L, t);
570         lua_pushnumber(L, width);
571         lua_pushnumber(L, height);
572         wrap_lua_object<InputState>(L, "InputState", input_state);
573
574         if (lua_pcall(L, 5, 2, 0) != 0) {
575                 fprintf(stderr, "error running function `get_chain': %s\n", lua_tostring(L, -1));
576                 exit(1);
577         }
578
579         chain.chain = (EffectChain *)luaL_checkudata(L, -2, "EffectChain");
580         if (!lua_isfunction(L, -1)) {
581                 fprintf(stderr, "Argument #-1 should be a function\n");
582                 exit(1);
583         }
584         lua_pushvalue(L, -1);
585         shared_ptr<LuaRefWithDeleter> funcref(new LuaRefWithDeleter(&m, L, luaL_ref(L, LUA_REGISTRYINDEX)));
586         lua_pop(L, 2);
587         assert(lua_gettop(L) == 0);
588
589         chain.setup_chain = [this, funcref, input_state]{
590                 unique_lock<mutex> lock(m);
591
592                 this->input_state = &input_state;
593
594                 // Set up state, including connecting signals.
595                 lua_rawgeti(L, LUA_REGISTRYINDEX, funcref->get());
596                 if (lua_pcall(L, 0, 0, 0) != 0) {
597                         fprintf(stderr, "error running chain setup callback: %s\n", lua_tostring(L, -1));
598                         exit(1);
599                 }
600                 assert(lua_gettop(L) == 0);
601         };
602
603         // TODO: Can we do better, e.g. by running setup_chain() and seeing what it references?
604         // Actually, setup_chain does maybe hold all the references we need now anyway?
605         for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
606                 for (unsigned frame_num = 0; frame_num < FRAME_HISTORY_LENGTH; ++frame_num) {
607                         chain.input_frames.push_back(input_state.buffered_frames[card_index][frame_num].frame);
608                 }
609         }
610
611         return chain;
612 }
613
614 std::string Theme::get_channel_name(unsigned channel)
615 {
616         unique_lock<mutex> lock(m);
617         lua_getglobal(L, "channel_name");
618         lua_pushnumber(L, channel);
619         if (lua_pcall(L, 1, 1, 0) != 0) {
620                 fprintf(stderr, "error running function `channel_nam': %s\n", lua_tostring(L, -1));
621                 exit(1);
622         }
623
624         std::string ret = lua_tostring(L, -1);
625         lua_pop(L, 1);
626         assert(lua_gettop(L) == 0);
627         return ret;
628 }
629
630 bool Theme::get_supports_set_wb(unsigned channel)
631 {
632         unique_lock<mutex> lock(m);
633         lua_getglobal(L, "supports_set_wb");
634         lua_pushnumber(L, channel);
635         if (lua_pcall(L, 1, 1, 0) != 0) {
636                 fprintf(stderr, "error running function `supports_set_wb': %s\n", lua_tostring(L, -1));
637                 exit(1);
638         }
639
640         bool ret = checkbool(L, -1);
641         lua_pop(L, 1);
642         assert(lua_gettop(L) == 0);
643         return ret;
644 }
645
646 void Theme::set_wb(unsigned channel, double r, double g, double b)
647 {
648         unique_lock<mutex> lock(m);
649         lua_getglobal(L, "set_wb");
650         lua_pushnumber(L, channel);
651         lua_pushnumber(L, r);
652         lua_pushnumber(L, g);
653         lua_pushnumber(L, b);
654         if (lua_pcall(L, 4, 0, 0) != 0) {
655                 fprintf(stderr, "error running function `set_wb': %s\n", lua_tostring(L, -1));
656                 exit(1);
657         }
658
659         assert(lua_gettop(L) == 0);
660 }
661
662 std::vector<std::string> Theme::get_transition_names(float t)
663 {
664         unique_lock<mutex> lock(m);
665         lua_getglobal(L, "get_transitions");
666         lua_pushnumber(L, t);
667         if (lua_pcall(L, 1, 1, 0) != 0) {
668                 fprintf(stderr, "error running function `get_transitions': %s\n", lua_tostring(L, -1));
669                 exit(1);
670         }
671
672         std::vector<std::string> ret;
673         lua_pushnil(L);
674         while (lua_next(L, -2) != 0) {
675                 ret.push_back(lua_tostring(L, -1));
676                 lua_pop(L, 1);
677         }
678         lua_pop(L, 1);
679         assert(lua_gettop(L) == 0);
680         return ret;
681 }       
682
683 int Theme::map_signal(int signal_num)
684 {
685         if (signal_num >= int(num_cards)) {
686                 if (signals_warned_about.insert(signal_num).second) {
687                         fprintf(stderr, "WARNING: Theme asked for input %d, but we only have %u card(s).\n", signal_num, num_cards);
688                         fprintf(stderr, "Mapping to card %d instead.\n", signal_num % num_cards);
689                 }
690                 signal_num %= num_cards;
691         }
692         return signal_num;
693 }
694
695 void Theme::transition_clicked(int transition_num, float t)
696 {
697         unique_lock<mutex> lock(m);
698         lua_getglobal(L, "transition_clicked");
699         lua_pushnumber(L, transition_num);
700         lua_pushnumber(L, t);
701
702         if (lua_pcall(L, 2, 0, 0) != 0) {
703                 fprintf(stderr, "error running function `transition_clicked': %s\n", lua_tostring(L, -1));
704                 exit(1);
705         }
706         assert(lua_gettop(L) == 0);
707 }
708
709 void Theme::channel_clicked(int preview_num)
710 {
711         unique_lock<mutex> lock(m);
712         lua_getglobal(L, "channel_clicked");
713         lua_pushnumber(L, preview_num);
714
715         if (lua_pcall(L, 1, 0, 0) != 0) {
716                 fprintf(stderr, "error running function `channel_clicked': %s\n", lua_tostring(L, -1));
717                 exit(1);
718         }
719         assert(lua_gettop(L) == 0);
720 }