]> git.sesse.net Git - nageru/blobdiff - decklink_capture.cpp
Make ImageInput shutdown immediate, instead of waiting for the sleep to time out.
[nageru] / decklink_capture.cpp
index 2519caa5ef05034b7ef136d6d769f50b10ba2aca..eee46c4454f528db07fd2593129b25949c3cd19d 100644 (file)
@@ -1,25 +1,32 @@
 #include "decklink_capture.h"
 
+#include <DeckLinkAPI.h>
+#include <DeckLinkAPIConfiguration.h>
+#include <DeckLinkAPIDiscovery.h>
+#include <DeckLinkAPIModes.h>
 #include <assert.h>
+#ifdef __SSE2__
+#include <immintrin.h>
+#endif
+#include <pthread.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <cstddef>
-#ifdef __SSE2__
-#include <immintrin.h>
-#endif
+#include <chrono>
+#include <cstdint>
+#include <utility>
+#include <vector>
 
-#include <DeckLinkAPI.h>
-#include <DeckLinkAPIConfiguration.h>
-#include <DeckLinkAPIDiscovery.h>
-#include <DeckLinkAPIModes.h>
 #include "bmusb/bmusb.h"
+#include "decklink_util.h"
 
 #define FRAME_SIZE (8 << 20)  // 8 MB.
 
 using namespace std;
+using namespace std::chrono;
 using namespace std::placeholders;
+using namespace bmusb;
 
 namespace {
 
@@ -58,7 +65,7 @@ size_t memcpy_interleaved_fastpath(uint8_t *dest1, uint8_t *dest2, const uint8_t
                memcpy_interleaved(dest1, dest2, src, n2);
                dest1 += n2 / 2;
                dest2 += n2 / 2;
-               if (n2 % 1) {
+               if (n2 % 2) {
                        swap(dest1, dest2);
                }
                src = aligned_src;
@@ -199,6 +206,22 @@ DeckLinkCapture::DeckLinkCapture(IDeckLink *card, int card_index)
                }
        }
 
+       // Check if we the card supports input autodetection.
+       if (attr->GetFlag(BMDDeckLinkSupportsInputFormatDetection, &supports_autodetect) != S_OK) {
+               fprintf(stderr, "Warning: Failed to ask card %d whether it supports input format autodetection\n", card_index);
+               supports_autodetect = false;
+       }
+
+       // If there's more than one subdevice on this card, label them.
+       int64_t num_subdevices, subdevice_idx;
+       if (attr->GetInt(BMDDeckLinkNumberOfSubDevices, &num_subdevices) == S_OK && num_subdevices > 1) {
+               if (attr->GetInt(BMDDeckLinkSubDeviceIndex, &subdevice_idx) == S_OK) {
+                       char buf[256];
+                       snprintf(buf, sizeof(buf), " (subdevice %d)", int(subdevice_idx));
+                       description += buf;
+               }
+       }
+
        attr->Release();
 
        /* Set up the video and audio sources. */
@@ -207,7 +230,9 @@ DeckLinkCapture::DeckLinkCapture(IDeckLink *card, int card_index)
                exit(1);
        }
 
-       set_video_input(bmdVideoConnectionHDMI);
+       BMDVideoConnection connection = pick_default_video_connection(card, BMDDeckLinkVideoInputConnections, card_index);
+
+       set_video_input(connection);
        set_audio_input(bmdAudioConnectionEmbedded);
 
        IDeckLinkDisplayModeIterator *mode_it;
@@ -216,44 +241,11 @@ DeckLinkCapture::DeckLinkCapture(IDeckLink *card, int card_index)
                exit(1);
        }
 
-       for (IDeckLinkDisplayMode *mode_ptr; mode_it->Next(&mode_ptr) == S_OK; mode_ptr->Release()) {
-               VideoMode mode;
-
-               const char *mode_name;
-               if (mode_ptr->GetName(&mode_name) != S_OK) {
-                       mode.name = "Unknown mode";
-               } else {
-                       mode.name = mode_name;
-               }
-
-               mode.autodetect = false;
-
-               mode.width = mode_ptr->GetWidth();
-               mode.height = mode_ptr->GetHeight();
-
-               BMDTimeScale frame_rate_num;
-               BMDTimeValue frame_rate_den;
-               if (mode_ptr->GetFrameRate(&frame_rate_den, &frame_rate_num) != S_OK) {
-                       fprintf(stderr, "Could not get frame rate for mode '%s' on card %d\n", mode.name.c_str(), card_index);
-                       exit(1);
-               }
-               mode.frame_rate_num = frame_rate_num;
-               mode.frame_rate_den = frame_rate_den;
-
-               // TODO: Respect the TFF/BFF flag.
-               mode.interlaced = (mode_ptr->GetFieldDominance() == bmdLowerFieldFirst || mode_ptr->GetFieldDominance() == bmdUpperFieldFirst);
-
-               uint32_t id = mode_ptr->GetDisplayMode();
-               video_modes.insert(make_pair(id, mode));
-       }
+       video_modes = summarize_video_modes(mode_it, card_index);
+       mode_it->Release();
 
        set_video_mode_no_restart(bmdModeHD720p5994);
 
-       if (input->EnableAudioInput(48000, bmdAudioSampleType32bitInteger, 2) != S_OK) {
-               fprintf(stderr, "Failed to enable audio input for card %d\n", card_index);
-               exit(1);
-       }
-
        input->SetCallback(this);
 }
 
@@ -288,12 +280,20 @@ ULONG STDMETHODCALLTYPE DeckLinkCapture::Release(void)
 HRESULT STDMETHODCALLTYPE DeckLinkCapture::VideoInputFormatChanged(
        BMDVideoInputFormatChangedEvents,
        IDeckLinkDisplayMode* display_mode,
-       BMDDetectedVideoInputFormatFlags)
+       BMDDetectedVideoInputFormatFlags format_flags)
 {
+       if (format_flags & bmdDetectedVideoInputRGB444) {
+               fprintf(stderr, "WARNING: Input detected as 4:4:4 RGB, but Nageru can't consume that yet.\n");
+               fprintf(stderr, "Doing hardware conversion to 4:2:2 Y'CbCr.\n");
+       }
+       if (supports_autodetect && display_mode->GetDisplayMode() != current_video_mode) {
+               set_video_mode(display_mode->GetDisplayMode());
+       }
        if (display_mode->GetFrameRate(&frame_duration, &time_scale) != S_OK) {
                fprintf(stderr, "Could not get new frame rate\n");
                exit(1);
        }
+       field_dominance = display_mode->GetFieldDominance();
        return S_OK;
 }
 
@@ -302,18 +302,26 @@ HRESULT STDMETHODCALLTYPE DeckLinkCapture::VideoInputFrameArrived(
        IDeckLinkAudioInputPacket *audio_frame)
 {
        if (!done_init) {
+               char thread_name[16];
+               snprintf(thread_name, sizeof(thread_name), "DeckLink_C_%d", card_index);
+               pthread_setname_np(pthread_self(), thread_name);
                if (has_dequeue_callbacks) {
                        dequeue_init_callback();
                }
                done_init = true;
        }
 
+       steady_clock::time_point now = steady_clock::now();
+
        FrameAllocator::Frame current_video_frame, current_audio_frame;
        VideoFormat video_format;
        AudioFormat audio_format;
 
        video_format.frame_rate_nom = time_scale;
        video_format.frame_rate_den = frame_duration;
+       // TODO: Respect the TFF/BFF flag.
+       video_format.interlaced = (field_dominance == bmdLowerFieldFirst || field_dominance == bmdUpperFieldFirst);
+       video_format.second_field_start = 1;
 
        if (video_frame) {
                video_format.has_signal = !(video_frame->GetFlags() & bmdFrameHasNoInputSource);
@@ -369,6 +377,9 @@ HRESULT STDMETHODCALLTYPE DeckLinkCapture::VideoInputFrameArrived(
                }
        }
 
+       current_video_frame.received_timestamp = now;
+       current_audio_frame.received_timestamp = now;
+
        if (current_video_frame.data != nullptr || current_audio_frame.data != nullptr) {
                // TODO: Put into a queue and put into a dequeue thread, if the
                // BlackMagic drivers don't already do that for us?
@@ -395,24 +406,54 @@ void DeckLinkCapture::configure_card()
 
 void DeckLinkCapture::start_bm_capture()
 {
+       if (running) {
+               return;
+       }
+       if (input->EnableVideoInput(current_video_mode, bmdFormat8BitYUV, supports_autodetect ? bmdVideoInputEnableFormatDetection : 0) != S_OK) {
+               fprintf(stderr, "Failed to set video mode 0x%04x for card %d\n", current_video_mode, card_index);
+               exit(1);
+       }
+       if (input->EnableAudioInput(48000, bmdAudioSampleType32bitInteger, 2) != S_OK) {
+               fprintf(stderr, "Failed to enable audio input for card %d\n", card_index);
+               exit(1);
+       }
+
        if (input->StartStreams() != S_OK) {
                fprintf(stderr, "StartStreams failed\n");
                exit(1);
        }
+       running = true;
 }
 
 void DeckLinkCapture::stop_dequeue_thread()
 {
-       if (input->StopStreams() != S_OK) {
-               fprintf(stderr, "StopStreams failed\n");
+       if (!running) {
+               return;
+       }
+       HRESULT result = input->StopStreams();
+       if (result != S_OK) {
+               fprintf(stderr, "StopStreams failed with error 0x%x\n", result);
+               exit(1);
+       }
+       if (input->DisableVideoInput() != S_OK) {
+               fprintf(stderr, "Failed to disable video input for card %d\n", card_index);
+               exit(1);
+       }
+       if (input->DisableAudioInput() != S_OK) {
+               fprintf(stderr, "Failed to disable audio input for card %d\n", card_index);
                exit(1);
        }
+       running = false;
 }
 
 void DeckLinkCapture::set_video_mode(uint32_t video_mode_id)
 {
-       if (input->StopStreams() != S_OK) {
-               fprintf(stderr, "StopStreams failed\n");
+       if (input->PauseStreams() != S_OK) {
+               fprintf(stderr, "PauseStreams failed\n");
+               exit(1);
+       }
+       if (input->FlushStreams() != S_OK) {
+               fprintf(stderr, "FlushStreams failed\n");
                exit(1);
        }
 
@@ -443,9 +484,13 @@ void DeckLinkCapture::set_video_mode_no_restart(uint32_t video_mode_id)
                exit(1);
        }
 
-       if (input->EnableVideoInput(video_mode_id, bmdFormat8BitYUV, 0) != S_OK) {
-               fprintf(stderr, "Failed to set video mode 0x%04x for card %d\n", video_mode_id, card_index);
-               exit(1);
+       field_dominance = display_mode->GetFieldDominance();
+
+       if (running) {
+               if (input->EnableVideoInput(video_mode_id, bmdFormat8BitYUV, supports_autodetect ? bmdVideoInputEnableFormatDetection : 0) != S_OK) {
+                       fprintf(stderr, "Failed to set video mode 0x%04x for card %d\n", video_mode_id, card_index);
+                       exit(1);
+               }
        }
 
        current_video_mode = video_mode_id;