+ if (global_mixer == nullptr) {
+ // No data yet.
+ return;
+ }
+
+ signal_num = theme->map_signal(signal_num);
+ connect_signal_raw(signal_num, *theme->input_state);
+}
+
+void LiveInputWrapper::connect_signal_raw(int signal_num, const InputState &input_state)
+{
+ BufferedFrame first_frame = 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];
+ }
+
+ movit::YCbCrLumaCoefficients ycbcr_coefficients = input_state.ycbcr_coefficients[signal_num];
+ bool full_range = input_state.full_range[signal_num];
+
+ if (input_state.ycbcr_coefficients_auto[signal_num]) {
+ full_range = false;
+
+ // The Blackmagic driver docs claim that the device outputs Y'CbCr
+ // according to Rec. 601, but this seems to indicate the subsampling
+ // positions only, as they publish Y'CbCr → RGB formulas that are
+ // different for HD and SD (corresponding to Rec. 709 and 601, respectively),
+ // and a Lenovo X1 gen 3 I used to test definitely outputs Rec. 709
+ // (at least up to rounding error). Other devices seem to use Rec. 601
+ // even on HD resolutions. Nevertheless, Rec. 709 _is_ the right choice
+ // for HD, so we default to that if the user hasn't set anything.
+ if (height >= 720) {
+ ycbcr_coefficients = YCBCR_REC_709;
+ } else {
+ ycbcr_coefficients = YCBCR_REC_601;
+ }
+ }
+
+ // This is a global, but it doesn't really matter.
+ input_ycbcr_format.luma_coefficients = ycbcr_coefficients;
+ input_ycbcr_format.full_range = full_range;
+
+ BufferedFrame last_good_frame = first_frame;
+ for (unsigned i = 0; i < max(ycbcr_inputs.size(), rgba_inputs.size()); ++i) {
+ BufferedFrame frame = 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;
+
+ unsigned this_width = userdata->last_width[frame.field_number];
+ unsigned this_height = userdata->last_height[frame.field_number];
+ if (this_width != width || this_height != height) {
+ // Resolution changed; reuse last frame/field.
+ frame = last_good_frame;
+ userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
+ }
+
+ assert(userdata->pixel_format == pixel_format);
+ switch (pixel_format) {
+ case bmusb::PixelFormat_8BitYCbCr:
+ ycbcr_inputs[i]->set_texture_num(0, userdata->tex_y[frame.field_number]);
+ ycbcr_inputs[i]->set_texture_num(1, userdata->tex_cbcr[frame.field_number]);
+ ycbcr_inputs[i]->change_ycbcr_format(input_ycbcr_format);
+ ycbcr_inputs[i]->set_width(width);
+ ycbcr_inputs[i]->set_height(height);
+ break;
+ case bmusb::PixelFormat_8BitYCbCrPlanar:
+ ycbcr_inputs[i]->set_texture_num(0, userdata->tex_y[frame.field_number]);
+ ycbcr_inputs[i]->set_texture_num(1, userdata->tex_cb[frame.field_number]);
+ ycbcr_inputs[i]->set_texture_num(2, userdata->tex_cr[frame.field_number]);
+ ycbcr_inputs[i]->change_ycbcr_format(userdata->ycbcr_format);
+ ycbcr_inputs[i]->set_width(width);
+ ycbcr_inputs[i]->set_height(height);
+ break;
+ case bmusb::PixelFormat_10BitYCbCr:
+ ycbcr_inputs[i]->set_texture_num(0, userdata->tex_444[frame.field_number]);
+ ycbcr_inputs[i]->change_ycbcr_format(input_ycbcr_format);
+ ycbcr_inputs[i]->set_width(width);
+ ycbcr_inputs[i]->set_height(height);
+ break;
+ case bmusb::PixelFormat_8BitBGRA:
+ rgba_inputs[i]->set_texture_num(userdata->tex_rgba[frame.field_number]);
+ rgba_inputs[i]->set_width(width);
+ rgba_inputs[i]->set_height(height);
+ break;
+ default:
+ assert(false);
+ }
+
+ last_good_frame = frame;
+ }
+
+ if (deinterlace) {
+ BufferedFrame frame = input_state.buffered_frames[signal_num][0];
+ CHECK(deinterlace_effect->set_int("current_field_position", frame.field_number));
+ }
+}
+
+namespace {
+
+int call_num_channels(lua_State *L)
+{
+ lua_getglobal(L, "num_channels");
+
+ if (lua_pcall(L, 0, 1, 0) != 0) {
+ fprintf(stderr, "error running function `num_channels': %s\n", lua_tostring(L, -1));
+ exit(1);
+ }
+
+ int num_channels = luaL_checknumber(L, 1);
+ lua_pop(L, 1);
+ assert(lua_gettop(L) == 0);
+ return num_channels;