X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=decklink_output.cpp;h=f2ac40b8a9600f417399dd64efeedf92ff2d84fc;hb=cc48fb94d8215eaf529825f76c18e8332352175d;hp=21baf618f7aba57a5bab928ee334c33cff40c6db;hpb=71480f2d1cd96c1ca5b2888f82bf33d290d60228;p=nageru diff --git a/decklink_output.cpp b/decklink_output.cpp index 21baf61..f2ac40b 100644 --- a/decklink_output.cpp +++ b/decklink_output.cpp @@ -12,6 +12,7 @@ #include "print_latency.h" #include "resource_pool.h" #include "timebase.h" +#include "v210_converter.h" using namespace movit; using namespace std; @@ -66,7 +67,12 @@ void DeckLinkOutput::start_output(uint32_t mode, int64_t base_pts) assert(output); assert(!playback_initiated); - should_quit = false; + if (video_modes.empty()) { + fprintf(stderr, "ERROR: No matching output modes for %dx%d found\n", width, height); + exit(1); + } + + should_quit.unquit(); playback_initiated = true; playback_started = false; this->base_pts = base_pts; @@ -88,10 +94,16 @@ void DeckLinkOutput::start_output(uint32_t mode, int64_t base_pts) fprintf(stderr, "Failed to set PsF flag for card\n"); exit(1); } + if (config->SetFlag(bmdDeckLinkConfigSMPTELevelAOutput, true) != S_OK) { + // This affects at least some no-name SDI->HDMI converters. + // Warn, but don't die. + fprintf(stderr, "WARNING: Failed to enable SMTPE Level A; resolutions like 1080p60 might have issues.\n"); + } BMDDisplayModeSupport support; IDeckLinkDisplayMode *display_mode; - if (output->DoesSupportVideoMode(mode, bmdFormat8BitYUV, bmdVideoOutputFlagDefault, + BMDPixelFormat pixel_format = global_flags.ten_bit_output ? bmdFormat10BitYUV : bmdFormat8BitYUV; + if (output->DoesSupportVideoMode(mode, pixel_format, bmdVideoOutputFlagDefault, &support, &display_mode) != S_OK) { fprintf(stderr, "Couldn't ask for format support\n"); exit(1); @@ -102,14 +114,7 @@ void DeckLinkOutput::start_output(uint32_t mode, int64_t base_pts) exit(1); } - BMDDisplayModeFlags flags = display_mode->GetFlags(); - if ((flags & bmdDisplayModeColorspaceRec601) && global_flags.ycbcr_rec709_coefficients) { - fprintf(stderr, "WARNING: Chosen output mode expects Rec. 601 Y'CbCr coefficients.\n"); - fprintf(stderr, " Consider --output-ycbcr-coefficients=rec601 (or =auto).\n"); - } else if ((flags & bmdDisplayModeColorspaceRec709) && !global_flags.ycbcr_rec709_coefficients) { - fprintf(stderr, "WARNING: Chosen output mode expects Rec. 709 Y'CbCr coefficients.\n"); - fprintf(stderr, " Consider --output-ycbcr-coefficients=rec709 (or =auto).\n"); - } + current_mode_flags = display_mode->GetFlags(); BMDTimeValue time_value; BMDTimeScale time_scale; @@ -160,7 +165,7 @@ void DeckLinkOutput::end_output() return; } - should_quit = true; + should_quit.quit(); frame_queues_changed.notify_all(); present_thread.join(); playback_initiated = false; @@ -179,10 +184,32 @@ void DeckLinkOutput::end_output() } } -void DeckLinkOutput::send_frame(GLuint y_tex, GLuint cbcr_tex, const vector &input_frames, int64_t pts, int64_t duration) +void DeckLinkOutput::send_frame(GLuint y_tex, GLuint cbcr_tex, YCbCrLumaCoefficients output_ycbcr_coefficients, const vector &input_frames, int64_t pts, int64_t duration) { + assert(!should_quit.should_quit()); + + if ((current_mode_flags & bmdDisplayModeColorspaceRec601) && output_ycbcr_coefficients == YCBCR_REC_709) { + if (!last_frame_had_mode_mismatch) { + fprintf(stderr, "WARNING: Chosen output mode expects Rec. 601 Y'CbCr coefficients.\n"); + fprintf(stderr, " Consider --output-ycbcr-coefficients=rec601 (or =auto).\n"); + } + last_frame_had_mode_mismatch = true; + } else if ((current_mode_flags & bmdDisplayModeColorspaceRec709) && output_ycbcr_coefficients == YCBCR_REC_601) { + if (!last_frame_had_mode_mismatch) { + fprintf(stderr, "WARNING: Chosen output mode expects Rec. 709 Y'CbCr coefficients.\n"); + fprintf(stderr, " Consider --output-ycbcr-coefficients=rec709 (or =auto).\n"); + } + last_frame_had_mode_mismatch = true; + } else { + last_frame_had_mode_mismatch = false; + } + unique_ptr frame = move(get_frame()); - chroma_subsampler->create_uyvy(y_tex, cbcr_tex, width, height, frame->uyvy_tex); + if (global_flags.ten_bit_output) { + chroma_subsampler->create_v210(y_tex, cbcr_tex, width, height, frame->uyvy_tex); + } else { + chroma_subsampler->create_uyvy(y_tex, cbcr_tex, width, height, frame->uyvy_tex); + } // Download the UYVY texture to the PBO. glPixelStorei(GL_PACK_ROW_LENGTH, 0); @@ -191,10 +218,17 @@ void DeckLinkOutput::send_frame(GLuint y_tex, GLuint cbcr_tex, const vectorpbo); check_error(); - glBindTexture(GL_TEXTURE_2D, frame->uyvy_tex); - check_error(); - glGetTexImage(GL_TEXTURE_2D, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0)); - check_error(); + if (global_flags.ten_bit_output) { + glBindTexture(GL_TEXTURE_2D, frame->uyvy_tex); + check_error(); + glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, BUFFER_OFFSET(0)); + check_error(); + } else { + glBindTexture(GL_TEXTURE_2D, frame->uyvy_tex); + check_error(); + glGetTexImage(GL_TEXTURE_2D, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0)); + check_error(); + } glBindTexture(GL_TEXTURE_2D, 0); check_error(); @@ -240,8 +274,10 @@ void DeckLinkOutput::send_audio(int64_t pts, const std::vector &samples) } } -void DeckLinkOutput::wait_for_frame(int64_t pts, int *dropped_frames, int64_t *frame_duration) +void DeckLinkOutput::wait_for_frame(int64_t pts, int *dropped_frames, int64_t *frame_duration, bool *is_preroll, steady_clock::time_point *frame_timestamp) { + assert(!should_quit.should_quit()); + *dropped_frames = 0; *frame_duration = this->frame_duration; @@ -251,9 +287,12 @@ void DeckLinkOutput::wait_for_frame(int64_t pts, int *dropped_frames, int64_t *f // While prerolling, we send out frames as quickly as we can. if (target_time < base_pts) { + *is_preroll = true; return; } + *is_preroll = !playback_started; + if (!playback_started) { if (output->EndAudioPreroll() != S_OK) { fprintf(stderr, "Could not end audio preroll\n"); @@ -270,11 +309,12 @@ void DeckLinkOutput::wait_for_frame(int64_t pts, int *dropped_frames, int64_t *f double playback_speed; output->GetScheduledStreamTime(TIMEBASE, &stream_frame_time, &playback_speed); + *frame_timestamp = steady_clock::now() + + nanoseconds((target_time - stream_frame_time) * 1000000000 / TIMEBASE); + // If we're ahead of time, wait for the frame to (approximately) start. if (stream_frame_time < target_time) { - steady_clock::time_point t = steady_clock::now() + - nanoseconds((target_time - stream_frame_time) * 1000000000 / TIMEBASE); - this_thread::sleep_until(t); + should_quit.sleep_until(*frame_timestamp); return; } @@ -289,9 +329,49 @@ void DeckLinkOutput::wait_for_frame(int64_t pts, int *dropped_frames, int64_t *f // Oops, we missed by more than one frame. Return immediately, // but drop so that we catch up. *dropped_frames = (stream_frame_time - target_time + *frame_duration - 1) / *frame_duration; + const int64_t ns_per_frame = this->frame_duration * 1000000000 / TIMEBASE; + *frame_timestamp += nanoseconds(*dropped_frames * ns_per_frame); fprintf(stderr, "Dropped %d output frames; skipping.\n", *dropped_frames); } +uint32_t DeckLinkOutput::pick_video_mode(uint32_t mode) const +{ + if (video_modes.count(mode)) { + return mode; + } + + // Prioritize 59.94 > 60 > 29.97. If none of those are found, then pick the highest one. + for (const pair &desired : vector>{ { 60000, 1001 }, { 60, 0 }, { 30000, 1001 } }) { + for (const auto &it : video_modes) { + if (it.second.frame_rate_num * desired.second == desired.first * it.second.frame_rate_den) { + return it.first; + } + } + } + + uint32_t best_mode = 0; + double best_fps = 0.0; + for (const auto &it : video_modes) { + double fps = double(it.second.frame_rate_num) / it.second.frame_rate_den; + if (fps > best_fps) { + best_mode = it.first; + best_fps = fps; + } + } + return best_mode; +} + +YCbCrLumaCoefficients DeckLinkOutput::preferred_ycbcr_coefficients() const +{ + if (current_mode_flags & bmdDisplayModeColorspaceRec601) { + return YCBCR_REC_601; + } else { + // Don't bother checking bmdDisplayModeColorspaceRec709; + // if none is set, 709 is a good default anyway. + return YCBCR_REC_709; + } +} + HRESULT DeckLinkOutput::ScheduledFrameCompleted(/* in */ IDeckLinkVideoFrame *completedFrame, /* in */ BMDOutputFrameCompletionResult result) { Frame *frame = static_cast(completedFrame); @@ -314,8 +394,8 @@ HRESULT DeckLinkOutput::ScheduledFrameCompleted(/* in */ IDeckLinkVideoFrame *co break; } - static int hei = 0; - print_latency("DeckLink output latency (frame received → output on HDMI):", frame->received_ts, false, &hei); + static int frameno = 0; + print_latency("DeckLink output latency (frame received → output on HDMI):", frame->received_ts, false, &frameno); { lock_guard lock(frame_queue_mutex); @@ -344,17 +424,31 @@ unique_ptr DeckLinkOutput::get_frame() unique_ptr frame(new Frame); - frame->uyvy_tex = resource_pool->create_2d_texture(GL_RGBA8, width / 2, height); + size_t stride; + if (global_flags.ten_bit_output) { + stride = v210Converter::get_v210_stride(width); + GLint v210_width = stride / sizeof(uint32_t); + frame->uyvy_tex = resource_pool->create_2d_texture(GL_RGB10_A2, v210_width, height); + + // We need valid texture state, or NVIDIA won't allow us to write to the texture. + glBindTexture(GL_TEXTURE_2D, frame->uyvy_tex); + check_error(); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + check_error(); + } else { + stride = width * 2; + frame->uyvy_tex = resource_pool->create_2d_texture(GL_RGBA8, width / 2, height); + } glGenBuffers(1, &frame->pbo); check_error(); glBindBuffer(GL_PIXEL_PACK_BUFFER, frame->pbo); check_error(); - glBufferStorage(GL_PIXEL_PACK_BUFFER, width * height * 2, NULL, GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT); + glBufferStorage(GL_PIXEL_PACK_BUFFER, stride * height, NULL, GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT); check_error(); - frame->uyvy_ptr = (uint8_t *)glMapBufferRange(GL_PIXEL_PACK_BUFFER, 0, width * height * 2, GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT); + frame->uyvy_ptr = (uint8_t *)glMapBufferRange(GL_PIXEL_PACK_BUFFER, 0, stride * height, GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT); check_error(); - frame->uyvy_ptr_local.reset(new uint8_t[width * height * 2]); + frame->uyvy_ptr_local.reset(new uint8_t[stride * height]); frame->resource_pool = resource_pool; return frame; @@ -368,9 +462,9 @@ void DeckLinkOutput::present_thread_func() { unique_lock lock(frame_queue_mutex); frame_queues_changed.wait(lock, [this]{ - return should_quit || !pending_video_frames.empty(); + return should_quit.should_quit() || !pending_video_frames.empty(); }); - if (should_quit) { + if (should_quit.should_quit()) { return; } frame = move(pending_video_frames.front()); @@ -378,11 +472,15 @@ void DeckLinkOutput::present_thread_func() ++num_frames_in_flight; } - glWaitSync(frame->fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED); + glClientWaitSync(frame->fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED); check_error(); frame->fence.reset(); - memcpy(frame->uyvy_ptr_local.get(), frame->uyvy_ptr, width * height * 2); + if (global_flags.ten_bit_output) { + memcpy(frame->uyvy_ptr_local.get(), frame->uyvy_ptr, v210Converter::get_v210_stride(width) * height); + } else { + memcpy(frame->uyvy_ptr_local.get(), frame->uyvy_ptr, width * height * 2); + } // Release any input frames we needed to render this frame. frame->input_frames.clear(); @@ -464,12 +562,20 @@ long DeckLinkOutput::Frame::GetHeight() long DeckLinkOutput::Frame::GetRowBytes() { - return global_flags.width * 2; + if (global_flags.ten_bit_output) { + return v210Converter::get_v210_stride(global_flags.width); + } else { + return global_flags.width * 2; + } } BMDPixelFormat DeckLinkOutput::Frame::GetPixelFormat() { - return bmdFormat8BitYUV; + if (global_flags.ten_bit_output) { + return bmdFormat10BitYUV; + } else { + return bmdFormat8BitYUV; + } } BMDFrameFlags DeckLinkOutput::Frame::GetFlags()