]> git.sesse.net Git - nageru/blobdiff - theme.cpp
Also show the frame rate.
[nageru] / theme.cpp
index caedbf4b1899a0bee3d26497142376c81d884440..7bfe6cf57f555cece42fe9d34a1337a9bae11d38 100644 (file)
--- a/theme.cpp
+++ b/theme.cpp
@@ -12,6 +12,7 @@
 #include <movit/padding_effect.h>
 #include <movit/resample_effect.h>
 #include <movit/resize_effect.h>
+#include <movit/util.h>
 #include <movit/white_balance_effect.h>
 #include <movit/ycbcr.h>
 #include <movit/ycbcr_input.h>
@@ -37,6 +38,35 @@ extern Mixer *global_mixer;
 
 namespace {
 
+// Contains basically the same data as InputState, but does not hold on to
+// a reference to the frames. This is important so that we can release them
+// without having to wait for Lua's GC.
+struct InputStateInfo {
+       InputStateInfo(const InputState& input_state);
+
+       unsigned last_width[MAX_CARDS], last_height[MAX_CARDS];
+       bool last_interlaced[MAX_CARDS];
+       unsigned last_frame_rate_nom[MAX_CARDS], last_frame_rate_den[MAX_CARDS];
+};
+
+InputStateInfo::InputStateInfo(const InputState &input_state)
+{
+       for (unsigned signal_num = 0; signal_num < MAX_CARDS; ++signal_num) {
+               BufferedFrame frame = input_state.buffered_frames[signal_num][0];
+               if (frame.frame == nullptr) {
+                       last_width[signal_num] = last_height[signal_num] = 0;
+                       last_interlaced[signal_num] = false;
+                       continue;
+               }
+               const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
+               last_width[signal_num] = userdata->last_width[frame.field_number];
+               last_height[signal_num] = userdata->last_height[frame.field_number];
+               last_interlaced[signal_num] = userdata->last_interlaced;
+               last_frame_rate_nom[signal_num] = userdata->last_frame_rate_nom;
+               last_frame_rate_den[signal_num] = userdata->last_frame_rate_den;
+       }
+}
+
 class LuaRefWithDeleter {
 public:
        LuaRefWithDeleter(mutex *m, lua_State *L, int ref) : m(m), L(L), ref(ref) {}
@@ -90,6 +120,15 @@ Effect *get_effect(lua_State *L, int idx)
        return nullptr;
 }
 
+InputStateInfo *get_input_state_info(lua_State *L, int idx)
+{
+       if (luaL_testudata(L, idx, "InputStateInfo")) {
+               return (InputStateInfo *)lua_touserdata(L, idx);
+       }
+       luaL_error(L, "Error: Index #%d was not InputStateInfo\n", idx);
+       return nullptr;
+}
+
 bool checkbool(lua_State* L, int idx)
 {
        luaL_checktype(L, idx, LUA_TBOOLEAN);
@@ -106,19 +145,21 @@ std::string checkstdstring(lua_State *L, int index)
 int EffectChain_new(lua_State* L)
 {
        assert(lua_gettop(L) == 2);
+       Theme *theme = get_theme_updata(L);
        int aspect_w = luaL_checknumber(L, 1);
        int aspect_h = luaL_checknumber(L, 2);
 
-       return wrap_lua_object<EffectChain>(L, "EffectChain", aspect_w, aspect_h);
+       return wrap_lua_object<EffectChain>(L, "EffectChain", aspect_w, aspect_h, theme->get_resource_pool());
 }
 
 int EffectChain_add_live_input(lua_State* L)
 {
-       assert(lua_gettop(L) == 2);
+       assert(lua_gettop(L) == 3);
        Theme *theme = get_theme_updata(L);
        EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
        bool override_bounce = checkbool(L, 2);
-       return wrap_lua_object<LiveInputWrapper>(L, "LiveInputWrapper", theme, chain, override_bounce);
+       bool deinterlace = checkbool(L, 3);
+       return wrap_lua_object<LiveInputWrapper>(L, "LiveInputWrapper", theme, chain, override_bounce, deinterlace);
 }
 
 int EffectChain_add_effect(lua_State* L)
@@ -139,7 +180,7 @@ int EffectChain_add_effect(lua_State* L)
                for (int idx = 3; idx <= lua_gettop(L); ++idx) {
                        if (luaL_testudata(L, idx, "LiveInputWrapper")) {
                                LiveInputWrapper *input = (LiveInputWrapper *)lua_touserdata(L, idx);
-                               inputs.push_back(input->get_input());
+                               inputs.push_back(input->get_effect());
                        } else {
                                inputs.push_back(get_effect(L, idx));
                        }
@@ -210,22 +251,6 @@ int LiveInputWrapper_connect_signal(lua_State* L)
        return 0;
 }
 
-int LiveInputWrapper_get_width(lua_State* L)
-{
-       assert(lua_gettop(L) == 1);
-       LiveInputWrapper *input = (LiveInputWrapper *)luaL_checkudata(L, 1, "LiveInputWrapper");
-       lua_pushnumber(L, input->get_width());
-       return 1;
-}
-
-int LiveInputWrapper_get_height(lua_State* L)
-{
-       assert(lua_gettop(L) == 1);
-       LiveInputWrapper *input = (LiveInputWrapper *)luaL_checkudata(L, 1, "LiveInputWrapper");
-       lua_pushnumber(L, input->get_height());
-       return 1;
-}
-
 int ImageInput_new(lua_State* L)
 {
        assert(lua_gettop(L) == 1);
@@ -275,6 +300,56 @@ int MixEffect_new(lua_State* L)
        return wrap_lua_object<MixEffect>(L, "MixEffect");
 }
 
+int InputStateInfo_get_width(lua_State* L)
+{
+       assert(lua_gettop(L) == 2);
+       InputStateInfo *input_state_info = get_input_state_info(L, 1);
+       Theme *theme = get_theme_updata(L);
+       int signal_num = theme->map_signal(luaL_checknumber(L, 2));
+       lua_pushnumber(L, input_state_info->last_width[signal_num]);
+       return 1;
+}
+
+int InputStateInfo_get_height(lua_State* L)
+{
+       assert(lua_gettop(L) == 2);
+       InputStateInfo *input_state_info = get_input_state_info(L, 1);
+       Theme *theme = get_theme_updata(L);
+       int signal_num = theme->map_signal(luaL_checknumber(L, 2));
+       lua_pushnumber(L, input_state_info->last_height[signal_num]);
+       return 1;
+}
+
+int InputStateInfo_get_interlaced(lua_State* L)
+{
+       assert(lua_gettop(L) == 2);
+       InputStateInfo *input_state_info = get_input_state_info(L, 1);
+       Theme *theme = get_theme_updata(L);
+       int signal_num = theme->map_signal(luaL_checknumber(L, 2));
+       lua_pushboolean(L, input_state_info->last_interlaced[signal_num]);
+       return 1;
+}
+
+int InputStateInfo_get_frame_rate_nom(lua_State* L)
+{
+       assert(lua_gettop(L) == 2);
+       InputStateInfo *input_state_info = get_input_state_info(L, 1);
+       Theme *theme = get_theme_updata(L);
+       int signal_num = theme->map_signal(luaL_checknumber(L, 2));
+       lua_pushnumber(L, input_state_info->last_frame_rate_nom[signal_num]);
+       return 1;
+}
+
+int InputStateInfo_get_frame_rate_den(lua_State* L)
+{
+       assert(lua_gettop(L) == 2);
+       InputStateInfo *input_state_info = get_input_state_info(L, 1);
+       Theme *theme = get_theme_updata(L);
+       int signal_num = theme->map_signal(luaL_checknumber(L, 2));
+       lua_pushnumber(L, input_state_info->last_frame_rate_den[signal_num]);
+       return 1;
+}
+
 int Effect_set_float(lua_State *L)
 {
        assert(lua_gettop(L) == 3);
@@ -342,9 +417,6 @@ const luaL_Reg EffectChain_funcs[] = {
 
 const luaL_Reg LiveInputWrapper_funcs[] = {
        { "connect_signal", LiveInputWrapper_connect_signal },
-       // These are only valid during calls to get_chain and the setup chain callback.
-       { "get_width", LiveInputWrapper_get_width },
-       { "get_height", LiveInputWrapper_get_height },
        { NULL, NULL }
 };
 
@@ -420,10 +492,20 @@ const luaL_Reg MixEffect_funcs[] = {
        { NULL, NULL }
 };
 
+const luaL_Reg InputStateInfo_funcs[] = {
+       { "get_width", InputStateInfo_get_width },
+       { "get_height", InputStateInfo_get_height },
+       { "get_interlaced", InputStateInfo_get_interlaced },
+       { "get_frame_rate_nom", InputStateInfo_get_frame_rate_nom },
+       { "get_frame_rate_den", InputStateInfo_get_frame_rate_den },
+       { NULL, NULL }
+};
+
 }  // namespace
 
-LiveInputWrapper::LiveInputWrapper(Theme *theme, EffectChain *chain, bool override_bounce)
-       : theme(theme)
+LiveInputWrapper::LiveInputWrapper(Theme *theme, EffectChain *chain, bool override_bounce, bool deinterlace)
+       : theme(theme),
+         deinterlace(deinterlace)
 {
        ImageFormat inout_format;
        inout_format.color_space = COLORSPACE_sRGB;
@@ -451,12 +533,34 @@ LiveInputWrapper::LiveInputWrapper(Theme *theme, EffectChain *chain, bool overri
        input_ycbcr_format.luma_coefficients = YCBCR_REC_709;
        input_ycbcr_format.full_range = false;
 
-       if (override_bounce) {
-               input = new NonBouncingYCbCrInput(inout_format, input_ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
+       unsigned num_inputs;
+       if (deinterlace) {
+               deinterlace_effect = new movit::DeinterlaceEffect();
+
+               // As per the comments in deinterlace_effect.h, we turn this off.
+               // The most likely interlaced input for us is either a camera
+               // (where it's fine to turn it off) or a laptop (where it _should_
+               // be turned off).
+               CHECK(deinterlace_effect->set_int("enable_spatial_interlacing_check", 0));
+
+               num_inputs = deinterlace_effect->num_inputs();
+               assert(num_inputs == FRAME_HISTORY_LENGTH);
        } else {
-               input = new YCbCrInput(inout_format, input_ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
+               num_inputs = 1;
+       }
+       for (unsigned i = 0; i < num_inputs; ++i) {
+               if (override_bounce) {
+                       inputs.push_back(new NonBouncingYCbCrInput(inout_format, input_ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR));
+               } else {
+                       inputs.push_back(new YCbCrInput(inout_format, input_ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR));
+               }
+               chain->add_input(inputs.back());
+       }
+
+       if (deinterlace) {
+               vector<Effect *> reverse_inputs(inputs.rbegin(), inputs.rend());
+               chain->add_effect(deinterlace_effect, reverse_inputs);
        }
-       chain->add_input(input);
 }
 
 void LiveInputWrapper::connect_signal(int signal_num)
@@ -468,33 +572,47 @@ void LiveInputWrapper::connect_signal(int signal_num)
 
        signal_num = theme->map_signal(signal_num);
 
-       BufferedFrame frame = theme->input_state->buffered_frames[signal_num][0];
-       const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
+       BufferedFrame first_frame = theme->input_state->buffered_frames[signal_num][0];
+       if (first_frame.frame == nullptr) {
+               // No data yet.
+               return;
+       }
+       unsigned width, height;
+       {
+               const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)first_frame.frame->userdata;
+               width = userdata->last_width[first_frame.field_number];
+               height = userdata->last_height[first_frame.field_number];
+       }
+
+       BufferedFrame last_good_frame = first_frame;
+       for (unsigned i = 0; i < inputs.size(); ++i) {
+               BufferedFrame frame = theme->input_state->buffered_frames[signal_num][i];
+               if (frame.frame == nullptr) {
+                       // Not enough data; reuse last frame (well, field).
+                       // This is suboptimal, but we have nothing better.
+                       frame = last_good_frame;
+               }
+               const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
 
-       input->set_texture_num(0, userdata->tex_y[frame.field_number]);
-       input->set_texture_num(1, userdata->tex_cbcr[frame.field_number]);
-       input->set_width(userdata->last_width[frame.field_number]);
-       input->set_height(userdata->last_height[frame.field_number]);
-}
+               if (userdata->last_width[frame.field_number] != width ||
+                   userdata->last_height[frame.field_number] != height) {
+                       // Resolution changed; reuse last frame/field.
+                       frame = last_good_frame;
+                       userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
+               }
 
-unsigned LiveInputWrapper::get_width() const
-{
-       if (last_connected_signal_num == -1) {
-               return 0;
+               inputs[i]->set_texture_num(0, userdata->tex_y[frame.field_number]);
+               inputs[i]->set_texture_num(1, userdata->tex_cbcr[frame.field_number]);
+               inputs[i]->set_width(userdata->last_width[frame.field_number]);
+               inputs[i]->set_height(userdata->last_height[frame.field_number]);
+
+               last_good_frame = frame;
        }
-       BufferedFrame frame = theme->input_state->buffered_frames[last_connected_signal_num][0];
-       const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
-       return userdata->last_width[frame.field_number];
-}
 
-unsigned LiveInputWrapper::get_height() const
-{
-       if (last_connected_signal_num == -1) {
-               return 0;
+       if (deinterlace) {
+               BufferedFrame frame = theme->input_state->buffered_frames[signal_num][0];
+               CHECK(deinterlace_effect->set_int("current_field_position", frame.field_number));
        }
-       BufferedFrame frame = theme->input_state->buffered_frames[last_connected_signal_num][0];
-       const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
-       return userdata->last_height[frame.field_number];
 }
 
 Theme::Theme(const char *filename, ResourcePool *resource_pool, unsigned num_cards)
@@ -513,6 +631,7 @@ Theme::Theme(const char *filename, ResourcePool *resource_pool, unsigned num_car
        register_class("OverlayEffect", OverlayEffect_funcs);
        register_class("ResizeEffect", ResizeEffect_funcs);
        register_class("MixEffect", MixEffect_funcs);
+       register_class("InputStateInfo", InputStateInfo_funcs);
 
        // Run script.
        lua_settop(L, 0);
@@ -559,9 +678,9 @@ Theme::Chain Theme::get_chain(unsigned num, float t, unsigned width, unsigned he
        lua_pushnumber(L, t);
        lua_pushnumber(L, width);
        lua_pushnumber(L, height);
+       wrap_lua_object<InputStateInfo>(L, "InputStateInfo", input_state);
 
-       this->input_state = &input_state;
-       if (lua_pcall(L, 4, 2, 0) != 0) {
+       if (lua_pcall(L, 5, 2, 0) != 0) {
                fprintf(stderr, "error running function `get_chain': %s\n", lua_tostring(L, -1));
                exit(1);
        }
@@ -607,7 +726,7 @@ std::string Theme::get_channel_name(unsigned channel)
        lua_getglobal(L, "channel_name");
        lua_pushnumber(L, channel);
        if (lua_pcall(L, 1, 1, 0) != 0) {
-               fprintf(stderr, "error running function `channel_nam': %s\n", lua_tostring(L, -1));
+               fprintf(stderr, "error running function `channel_name': %s\n", lua_tostring(L, -1));
                exit(1);
        }