]> git.sesse.net Git - nageru/blob - theme.cpp
Redo get_width/get_height to not work on a LiveInputWrapper, since we do not really...
[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         int signal_num = luaL_checknumber(L, 2);
284         BufferedFrame frame = input_state->buffered_frames[signal_num][0];
285         const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
286         lua_pushnumber(L, userdata->last_width[frame.field_number]);
287         return 1;
288 }
289
290 int InputState_get_height(lua_State* L)
291 {
292         assert(lua_gettop(L) == 2);
293         InputState *input_state = (InputState *)lua_touserdata(L, 1);
294         int signal_num = luaL_checknumber(L, 2);
295         BufferedFrame frame = input_state->buffered_frames[signal_num][0];
296         const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
297         lua_pushnumber(L, userdata->last_height[frame.field_number]);
298         return 1;
299 }
300
301 int Effect_set_float(lua_State *L)
302 {
303         assert(lua_gettop(L) == 3);
304         Effect *effect = (Effect *)get_effect(L, 1);
305         std::string key = checkstdstring(L, 2);
306         float value = luaL_checknumber(L, 3);
307         if (!effect->set_float(key, value)) {
308                 luaL_error(L, "Effect refused set_float(\"%s\", %d) (invalid key?)", key.c_str(), int(value));
309         }
310         return 0;
311 }
312
313 int Effect_set_int(lua_State *L)
314 {
315         assert(lua_gettop(L) == 3);
316         Effect *effect = (Effect *)get_effect(L, 1);
317         std::string key = checkstdstring(L, 2);
318         float value = luaL_checknumber(L, 3);
319         if (!effect->set_int(key, value)) {
320                 luaL_error(L, "Effect refused set_int(\"%s\", %d) (invalid key?)", key.c_str(), int(value));
321         }
322         return 0;
323 }
324
325 int Effect_set_vec3(lua_State *L)
326 {
327         assert(lua_gettop(L) == 5);
328         Effect *effect = (Effect *)get_effect(L, 1);
329         std::string key = checkstdstring(L, 2);
330         float v[3];
331         v[0] = luaL_checknumber(L, 3);
332         v[1] = luaL_checknumber(L, 4);
333         v[2] = luaL_checknumber(L, 5);
334         if (!effect->set_vec3(key, v)) {
335                 luaL_error(L, "Effect refused set_vec3(\"%s\", %f, %f, %f) (invalid key?)", key.c_str(),
336                         v[0], v[1], v[2]);
337         }
338         return 0;
339 }
340
341 int Effect_set_vec4(lua_State *L)
342 {
343         assert(lua_gettop(L) == 6);
344         Effect *effect = (Effect *)get_effect(L, 1);
345         std::string key = checkstdstring(L, 2);
346         float v[4];
347         v[0] = luaL_checknumber(L, 3);
348         v[1] = luaL_checknumber(L, 4);
349         v[2] = luaL_checknumber(L, 5);
350         v[3] = luaL_checknumber(L, 6);
351         if (!effect->set_vec4(key, v)) {
352                 luaL_error(L, "Effect refused set_vec4(\"%s\", %f, %f, %f, %f) (invalid key?)", key.c_str(),
353                         v[0], v[1], v[2], v[3]);
354         }
355         return 0;
356 }
357
358 const luaL_Reg EffectChain_funcs[] = {
359         { "new", EffectChain_new },
360         { "add_live_input", EffectChain_add_live_input },
361         { "add_effect", EffectChain_add_effect },
362         { "finalize", EffectChain_finalize },
363         { NULL, NULL }
364 };
365
366 const luaL_Reg LiveInputWrapper_funcs[] = {
367         { "connect_signal", LiveInputWrapper_connect_signal },
368         { NULL, NULL }
369 };
370
371 const luaL_Reg ImageInput_funcs[] = {
372         { "new", ImageInput_new },
373         { "set_float", Effect_set_float },
374         { "set_int", Effect_set_int },
375         { "set_vec3", Effect_set_vec3 },
376         { "set_vec4", Effect_set_vec4 },
377         { NULL, NULL }
378 };
379
380 const luaL_Reg WhiteBalanceEffect_funcs[] = {
381         { "new", WhiteBalanceEffect_new },
382         { "set_float", Effect_set_float },
383         { "set_int", Effect_set_int },
384         { "set_vec3", Effect_set_vec3 },
385         { "set_vec4", Effect_set_vec4 },
386         { NULL, NULL }
387 };
388
389 const luaL_Reg ResampleEffect_funcs[] = {
390         { "new", ResampleEffect_new },
391         { "set_float", Effect_set_float },
392         { "set_int", Effect_set_int },
393         { "set_vec3", Effect_set_vec3 },
394         { "set_vec4", Effect_set_vec4 },
395         { NULL, NULL }
396 };
397
398 const luaL_Reg PaddingEffect_funcs[] = {
399         { "new", PaddingEffect_new },
400         { "set_float", Effect_set_float },
401         { "set_int", Effect_set_int },
402         { "set_vec3", Effect_set_vec3 },
403         { "set_vec4", Effect_set_vec4 },
404         { NULL, NULL }
405 };
406
407 const luaL_Reg IntegralPaddingEffect_funcs[] = {
408         { "new", IntegralPaddingEffect_new },
409         { "set_float", Effect_set_float },
410         { "set_int", Effect_set_int },
411         { "set_vec3", Effect_set_vec3 },
412         { "set_vec4", Effect_set_vec4 },
413         { NULL, NULL }
414 };
415
416 const luaL_Reg OverlayEffect_funcs[] = {
417         { "new", OverlayEffect_new },
418         { "set_float", Effect_set_float },
419         { "set_int", Effect_set_int },
420         { "set_vec3", Effect_set_vec3 },
421         { "set_vec4", Effect_set_vec4 },
422         { NULL, NULL }
423 };
424
425 const luaL_Reg ResizeEffect_funcs[] = {
426         { "new", ResizeEffect_new },
427         { "set_float", Effect_set_float },
428         { "set_int", Effect_set_int },
429         { "set_vec3", Effect_set_vec3 },
430         { "set_vec4", Effect_set_vec4 },
431         { NULL, NULL }
432 };
433
434 const luaL_Reg MixEffect_funcs[] = {
435         { "new", MixEffect_new },
436         { "set_float", Effect_set_float },
437         { "set_int", Effect_set_int },
438         { "set_vec3", Effect_set_vec3 },
439         { "set_vec4", Effect_set_vec4 },
440         { NULL, NULL }
441 };
442
443 const luaL_Reg InputState_funcs[] = {
444         { "__gc", InputState_gc },
445         { "get_width", InputState_get_width },
446         { "get_height", InputState_get_height },
447         { NULL, NULL }
448 };
449
450 }  // namespace
451
452 LiveInputWrapper::LiveInputWrapper(Theme *theme, EffectChain *chain, bool override_bounce)
453         : theme(theme)
454 {
455         ImageFormat inout_format;
456         inout_format.color_space = COLORSPACE_sRGB;
457
458         // Gamma curve depends on the input signal, and we don't really get any
459         // indications. A camera would be expected to do Rec. 709, but
460         // I haven't checked if any do in practice. However, computers _do_ output
461         // in sRGB gamma (ie., they don't convert from sRGB to Rec. 709), and
462         // I wouldn't really be surprised if most non-professional cameras do, too.
463         // So we pick sRGB as the least evil here.
464         inout_format.gamma_curve = GAMMA_sRGB;
465
466         // The Blackmagic driver docs claim that the device outputs Y'CbCr
467         // according to Rec. 601, but practical testing indicates it definitely
468         // is Rec. 709 (at least up to errors attributable to rounding errors).
469         // Perhaps 601 was only to indicate the subsampling positions, not the
470         // colorspace itself? Tested with a Lenovo X1 gen 3 as input.
471         YCbCrFormat input_ycbcr_format;
472         input_ycbcr_format.chroma_subsampling_x = 2;
473         input_ycbcr_format.chroma_subsampling_y = 1;
474         input_ycbcr_format.cb_x_position = 0.0;
475         input_ycbcr_format.cr_x_position = 0.0;
476         input_ycbcr_format.cb_y_position = 0.5;
477         input_ycbcr_format.cr_y_position = 0.5;
478         input_ycbcr_format.luma_coefficients = YCBCR_REC_709;
479         input_ycbcr_format.full_range = false;
480
481         if (override_bounce) {
482                 input = new NonBouncingYCbCrInput(inout_format, input_ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
483         } else {
484                 input = new YCbCrInput(inout_format, input_ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
485         }
486         chain->add_input(input);
487 }
488
489 void LiveInputWrapper::connect_signal(int signal_num)
490 {
491         if (global_mixer == nullptr) {
492                 // No data yet.
493                 return;
494         }
495
496         signal_num = theme->map_signal(signal_num);
497
498         BufferedFrame frame = theme->input_state->buffered_frames[signal_num][0];
499         const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
500
501         input->set_texture_num(0, userdata->tex_y[frame.field_number]);
502         input->set_texture_num(1, userdata->tex_cbcr[frame.field_number]);
503         input->set_width(userdata->last_width[frame.field_number]);
504         input->set_height(userdata->last_height[frame.field_number]);
505 }
506
507 Theme::Theme(const char *filename, ResourcePool *resource_pool, unsigned num_cards)
508         : resource_pool(resource_pool), num_cards(num_cards)
509 {
510         L = luaL_newstate();
511         luaL_openlibs(L);
512
513         register_class("EffectChain", EffectChain_funcs); 
514         register_class("LiveInputWrapper", LiveInputWrapper_funcs); 
515         register_class("ImageInput", ImageInput_funcs);
516         register_class("WhiteBalanceEffect", WhiteBalanceEffect_funcs);
517         register_class("ResampleEffect", ResampleEffect_funcs);
518         register_class("PaddingEffect", PaddingEffect_funcs);
519         register_class("IntegralPaddingEffect", IntegralPaddingEffect_funcs);
520         register_class("OverlayEffect", OverlayEffect_funcs);
521         register_class("ResizeEffect", ResizeEffect_funcs);
522         register_class("MixEffect", MixEffect_funcs);
523         register_class("InputState", InputState_funcs);
524
525         // Run script.
526         lua_settop(L, 0);
527         if (luaL_dofile(L, filename)) {
528                 fprintf(stderr, "error: %s\n", lua_tostring(L, -1));
529                 lua_pop(L, 1);
530                 exit(1);
531         }
532         assert(lua_gettop(L) == 0);
533
534         // Ask it for the number of channels.
535         lua_getglobal(L, "num_channels");
536
537         if (lua_pcall(L, 0, 1, 0) != 0) {
538                 fprintf(stderr, "error running function `num_channels': %s\n", lua_tostring(L, -1));
539                 exit(1);
540         }
541
542         num_channels = luaL_checknumber(L, 1);
543         lua_pop(L, 1);
544         assert(lua_gettop(L) == 0);
545 }
546
547 void Theme::register_class(const char *class_name, const luaL_Reg *funcs)
548 {
549         assert(lua_gettop(L) == 0);
550         luaL_newmetatable(L, class_name);  // mt = {}
551         lua_pushlightuserdata(L, this);
552         luaL_setfuncs(L, funcs, 1);        // for (name,f in funcs) { mt[name] = f, with upvalue {theme} }
553         lua_pushvalue(L, -1);
554         lua_setfield(L, -2, "__index");    // mt.__index = mt
555         lua_setglobal(L, class_name);      // ClassName = mt
556         assert(lua_gettop(L) == 0);
557 }
558
559 Theme::Chain Theme::get_chain(unsigned num, float t, unsigned width, unsigned height, InputState input_state) 
560 {
561         Chain chain;
562
563         unique_lock<mutex> lock(m);
564         assert(lua_gettop(L) == 0);
565         lua_getglobal(L, "get_chain");  /* function to be called */
566         lua_pushnumber(L, num);
567         lua_pushnumber(L, t);
568         lua_pushnumber(L, width);
569         lua_pushnumber(L, height);
570         wrap_lua_object<InputState>(L, "InputState", input_state);
571
572         if (lua_pcall(L, 5, 2, 0) != 0) {
573                 fprintf(stderr, "error running function `get_chain': %s\n", lua_tostring(L, -1));
574                 exit(1);
575         }
576
577         chain.chain = (EffectChain *)luaL_checkudata(L, -2, "EffectChain");
578         if (!lua_isfunction(L, -1)) {
579                 fprintf(stderr, "Argument #-1 should be a function\n");
580                 exit(1);
581         }
582         lua_pushvalue(L, -1);
583         shared_ptr<LuaRefWithDeleter> funcref(new LuaRefWithDeleter(&m, L, luaL_ref(L, LUA_REGISTRYINDEX)));
584         lua_pop(L, 2);
585         assert(lua_gettop(L) == 0);
586
587         chain.setup_chain = [this, funcref, input_state]{
588                 unique_lock<mutex> lock(m);
589
590                 this->input_state = &input_state;
591
592                 // Set up state, including connecting signals.
593                 lua_rawgeti(L, LUA_REGISTRYINDEX, funcref->get());
594                 if (lua_pcall(L, 0, 0, 0) != 0) {
595                         fprintf(stderr, "error running chain setup callback: %s\n", lua_tostring(L, -1));
596                         exit(1);
597                 }
598                 assert(lua_gettop(L) == 0);
599         };
600
601         // TODO: Can we do better, e.g. by running setup_chain() and seeing what it references?
602         // Actually, setup_chain does maybe hold all the references we need now anyway?
603         for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
604                 for (unsigned frame_num = 0; frame_num < FRAME_HISTORY_LENGTH; ++frame_num) {
605                         chain.input_frames.push_back(input_state.buffered_frames[card_index][frame_num].frame);
606                 }
607         }
608
609         return chain;
610 }
611
612 std::string Theme::get_channel_name(unsigned channel)
613 {
614         unique_lock<mutex> lock(m);
615         lua_getglobal(L, "channel_name");
616         lua_pushnumber(L, channel);
617         if (lua_pcall(L, 1, 1, 0) != 0) {
618                 fprintf(stderr, "error running function `channel_nam': %s\n", lua_tostring(L, -1));
619                 exit(1);
620         }
621
622         std::string ret = lua_tostring(L, -1);
623         lua_pop(L, 1);
624         assert(lua_gettop(L) == 0);
625         return ret;
626 }
627
628 bool Theme::get_supports_set_wb(unsigned channel)
629 {
630         unique_lock<mutex> lock(m);
631         lua_getglobal(L, "supports_set_wb");
632         lua_pushnumber(L, channel);
633         if (lua_pcall(L, 1, 1, 0) != 0) {
634                 fprintf(stderr, "error running function `supports_set_wb': %s\n", lua_tostring(L, -1));
635                 exit(1);
636         }
637
638         bool ret = checkbool(L, -1);
639         lua_pop(L, 1);
640         assert(lua_gettop(L) == 0);
641         return ret;
642 }
643
644 void Theme::set_wb(unsigned channel, double r, double g, double b)
645 {
646         unique_lock<mutex> lock(m);
647         lua_getglobal(L, "set_wb");
648         lua_pushnumber(L, channel);
649         lua_pushnumber(L, r);
650         lua_pushnumber(L, g);
651         lua_pushnumber(L, b);
652         if (lua_pcall(L, 4, 0, 0) != 0) {
653                 fprintf(stderr, "error running function `set_wb': %s\n", lua_tostring(L, -1));
654                 exit(1);
655         }
656
657         assert(lua_gettop(L) == 0);
658 }
659
660 std::vector<std::string> Theme::get_transition_names(float t)
661 {
662         unique_lock<mutex> lock(m);
663         lua_getglobal(L, "get_transitions");
664         lua_pushnumber(L, t);
665         if (lua_pcall(L, 1, 1, 0) != 0) {
666                 fprintf(stderr, "error running function `get_transitions': %s\n", lua_tostring(L, -1));
667                 exit(1);
668         }
669
670         std::vector<std::string> ret;
671         lua_pushnil(L);
672         while (lua_next(L, -2) != 0) {
673                 ret.push_back(lua_tostring(L, -1));
674                 lua_pop(L, 1);
675         }
676         lua_pop(L, 1);
677         assert(lua_gettop(L) == 0);
678         return ret;
679 }       
680
681 int Theme::map_signal(int signal_num)
682 {
683         if (signal_num >= int(num_cards)) {
684                 if (signals_warned_about.insert(signal_num).second) {
685                         fprintf(stderr, "WARNING: Theme asked for input %d, but we only have %u card(s).\n", signal_num, num_cards);
686                         fprintf(stderr, "Mapping to card %d instead.\n", signal_num % num_cards);
687                 }
688                 signal_num %= num_cards;
689         }
690         return signal_num;
691 }
692
693 void Theme::transition_clicked(int transition_num, float t)
694 {
695         unique_lock<mutex> lock(m);
696         lua_getglobal(L, "transition_clicked");
697         lua_pushnumber(L, transition_num);
698         lua_pushnumber(L, t);
699
700         if (lua_pcall(L, 2, 0, 0) != 0) {
701                 fprintf(stderr, "error running function `transition_clicked': %s\n", lua_tostring(L, -1));
702                 exit(1);
703         }
704         assert(lua_gettop(L) == 0);
705 }
706
707 void Theme::channel_clicked(int preview_num)
708 {
709         unique_lock<mutex> lock(m);
710         lua_getglobal(L, "channel_clicked");
711         lua_pushnumber(L, preview_num);
712
713         if (lua_pcall(L, 1, 0, 0) != 0) {
714                 fprintf(stderr, "error running function `channel_clicked': %s\n", lua_tostring(L, -1));
715                 exit(1);
716         }
717         assert(lua_gettop(L) == 0);
718 }