--- /dev/null
+#ifndef _INPUT_STATE_H
+#define _INPUT_STATE_H 1
+
+#include "defs.h"
+#include "ref_counted_frame.h"
+
+struct BufferedFrame {
+ RefCountedFrame frame;
+ unsigned field_number;
+};
+
+// Encapsulates the state of all inputs at any given instant.
+// In particular, this is captured by Theme::get_chain(),
+// so that it can hold on to all the frames it needs for rendering.
+struct InputState {
+ // For each card, the last three frames (or fields), with 0 being the
+ // most recent one. Note that we only need the actual history if we have
+ // interlaced output (for deinterlacing), so if we detect progressive input,
+ // we immediately clear out all history and all entries will point to the same
+ // frame.
+ BufferedFrame buffered_frames[MAX_CARDS][FRAME_HISTORY_LENGTH];
+};
+
+#endif // !defined(_INPUT_STATE_H)
}
}
+void insert_new_frame(RefCountedFrame frame, unsigned field_num, bool interlaced, unsigned card_index, InputState *input_state)
+{
+ if (interlaced) {
+ for (unsigned frame_num = FRAME_HISTORY_LENGTH; frame_num --> 1; ) { // :-)
+ input_state->buffered_frames[card_index][frame_num] =
+ input_state->buffered_frames[card_index][frame_num - 1];
+ }
+ input_state->buffered_frames[card_index][0] = { frame, field_num };
+ } else {
+ for (unsigned frame_num = 0; frame_num < FRAME_HISTORY_LENGTH; ++frame_num) {
+ input_state->buffered_frames[card_index][frame_num] = { frame, field_num };
+ }
+ }
+}
+
+
} // namespace
Mixer::Mixer(const QSurfaceFormat &format, unsigned num_cards)
continue;
assert(card->new_frame != nullptr);
- if (card->new_frame_interlaced) {
- for (unsigned frame_num = FRAME_HISTORY_LENGTH; frame_num --> 1; ) { // :-)
- buffered_frames[card_index][frame_num] = buffered_frames[card_index][frame_num - 1];
- }
- buffered_frames[card_index][0] = { card->new_frame, card->new_frame_field };
- } else {
- for (unsigned frame_num = 0; frame_num < FRAME_HISTORY_LENGTH; ++frame_num) {
- buffered_frames[card_index][frame_num] = { card->new_frame, card->new_frame_field };
- }
- }
+ insert_new_frame(card->new_frame, card->new_frame_field, card->new_frame_interlaced, card_index, &input_state);
check_error();
// The new texture might still be uploaded,
}
// Get the main chain from the theme, and set its state immediately.
- Theme::Chain theme_main_chain = theme->get_chain(0, pts(), WIDTH, HEIGHT);
+ Theme::Chain theme_main_chain = theme->get_chain(0, pts(), WIDTH, HEIGHT, input_state);
EffectChain *chain = theme_main_chain.chain;
theme_main_chain.setup_chain();
// Set up preview and any additional channels.
for (int i = 1; i < theme->get_num_channels() + 2; ++i) {
DisplayFrame display_frame;
- Theme::Chain chain = theme->get_chain(i, pts(), WIDTH, HEIGHT); // FIXME: dimensions
+ Theme::Chain chain = theme->get_chain(i, pts(), WIDTH, HEIGHT, input_state); // FIXME: dimensions
display_frame.chain = chain.chain;
display_frame.setup_chain = chain.setup_chain;
display_frame.ready_fence = fence;
#include "timebase.h"
#include "stereocompressor.h"
#include "filter.h"
+#include "input_state.h"
class H264Encoder;
class QSurface;
void reset_meters();
- struct BufferedFrame {
- RefCountedFrame frame;
- unsigned field_number;
- };
-
- BufferedFrame get_buffered_frame(int card, int history_pos)
- {
- return buffered_frames[card][history_pos];
- }
-
private:
void bm_frame(unsigned card_index, uint16_t timecode,
FrameAllocator::Frame video_frame, size_t video_offset, uint16_t video_format,
};
CaptureCard cards[MAX_CARDS]; // protected by <bmusb_mutex>
- // For each card, the last three frames (or fields), with 0 being the
- // most recent one. Note that we only need the actual history if we have
- // interlaced output (for deinterlacing), so if we detect progressive input,
- // we immediately clear out all history and all entries will point to the same
- // frame.
- BufferedFrame buffered_frames[MAX_CARDS][FRAME_HISTORY_LENGTH];
+ InputState input_state;
class OutputChannel {
public:
signal_num = theme->map_signal(signal_num);
- Mixer::BufferedFrame frame = global_mixer->get_buffered_frame(signal_num, 0);
+ BufferedFrame frame = theme->input_state->buffered_frames[signal_num][0];
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]);
-
- // Hold on to the refcount so that we don't release the input frame
- // until we're done rendering from it.
- if (theme->used_input_frames_collector != nullptr) {
- theme->used_input_frames_collector->push_back(frame.frame);
- }
}
Theme::Theme(const char *filename, ResourcePool *resource_pool, unsigned num_cards)
assert(lua_gettop(L) == 0);
}
-Theme::Chain Theme::get_chain(unsigned num, float t, unsigned width, unsigned height)
+Theme::Chain Theme::get_chain(unsigned num, float t, unsigned width, unsigned height, InputState input_state)
{
Chain chain;
lua_pushnumber(L, width);
lua_pushnumber(L, height);
- this->used_input_frames_collector = &chain.input_frames;
if (lua_pcall(L, 4, 2, 0) != 0) {
fprintf(stderr, "error running function `get_chain': %s\n", lua_tostring(L, -1));
exit(1);
}
- this->used_input_frames_collector = nullptr;
chain.chain = (EffectChain *)luaL_checkudata(L, -2, "EffectChain");
if (!lua_isfunction(L, -1)) {
lua_pop(L, 2);
assert(lua_gettop(L) == 0);
- chain.setup_chain = [this, funcref]{
+ chain.setup_chain = [this, funcref, input_state]{
unique_lock<mutex> lock(m);
+ this->input_state = &input_state;
+
// Set up state, including connecting signals.
lua_rawgeti(L, LUA_REGISTRYINDEX, funcref->get());
if (lua_pcall(L, 0, 0, 0) != 0) {
assert(lua_gettop(L) == 0);
};
+ // TODO: Can we do better, e.g. by running setup_chain() and seeing what it references?
+ // Actually, setup_chain does maybe hold all the references we need now anyway?
+ for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
+ for (unsigned frame_num = 0; frame_num < FRAME_HISTORY_LENGTH; ++frame_num) {
+ chain.input_frames.push_back(input_state.buffered_frames[card_index][frame_num].frame);
+ }
+ }
+
return chain;
}
#include <vector>
#include "defs.h"
+#include "input_state.h"
#include "ref_counted_frame.h"
namespace movit {
std::vector<RefCountedFrame> input_frames;
};
- Chain get_chain(unsigned num, float t, unsigned width, unsigned height);
+ Chain get_chain(unsigned num, float t, unsigned width, unsigned height, InputState input_state);
int get_num_channels() const { return num_channels; }
int map_signal(int signal_num);
void register_class(const char *class_name, const luaL_Reg *funcs);
std::mutex m;
- lua_State *L;
+ lua_State *L; // Protected by <m>.
+ const InputState *input_state; // Protected by <m>. Only set temporarily, during chain setup.
movit::ResourcePool *resource_pool;
int num_channels;
unsigned num_cards;
std::set<int> signals_warned_about;
- // All input frames needed for the current chain. Filled during call to get_chain(),
- // as inputs get connected.
- std::vector<RefCountedFrame> *used_input_frames_collector;
friend class LiveInputWrapper;
};