]> git.sesse.net Git - nageru/blobdiff - decklink_capture.cpp
Support interlaced inputs with the official DeckLink driver.
[nageru] / decklink_capture.cpp
index 2519caa5ef05034b7ef136d6d769f50b10ba2aca..1093ab11aa0f64f388a6e348520ac8cb57f023a6 100644 (file)
@@ -1,25 +1,30 @@
 #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 <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"
 
 #define FRAME_SIZE (8 << 20)  // 8 MB.
 
 using namespace std;
+using namespace std::chrono;
 using namespace std::placeholders;
+using namespace bmusb;
 
 namespace {
 
@@ -58,7 +63,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;
@@ -249,11 +254,6 @@ DeckLinkCapture::DeckLinkCapture(IDeckLink *card, int card_index)
 
        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);
 }
 
@@ -294,6 +294,7 @@ HRESULT STDMETHODCALLTYPE DeckLinkCapture::VideoInputFormatChanged(
                fprintf(stderr, "Could not get new frame rate\n");
                exit(1);
        }
+       field_dominance = display_mode->GetFieldDominance();
        return S_OK;
 }
 
@@ -314,6 +315,9 @@ HRESULT STDMETHODCALLTYPE DeckLinkCapture::VideoInputFrameArrived(
 
        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);
@@ -350,6 +354,8 @@ HRESULT STDMETHODCALLTYPE DeckLinkCapture::VideoInputFrameArrived(
 
                        video_format.width = width;
                        video_format.height = height;
+
+                       current_video_frame.received_timestamp = steady_clock::now();
                }
        }
 
@@ -366,6 +372,8 @@ HRESULT STDMETHODCALLTYPE DeckLinkCapture::VideoInputFrameArrived(
 
                        audio_format.bits_per_sample = 32;
                        audio_format.num_channels = 2;
+
+                       current_audio_frame.received_timestamp = steady_clock::now();
                }
        }
 
@@ -395,18 +403,44 @@ void DeckLinkCapture::configure_card()
 
 void DeckLinkCapture::start_bm_capture()
 {
+       if (running) {
+               return;
+       }
+       if (input->EnableVideoInput(current_video_mode, bmdFormat8BitYUV, 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)
@@ -443,9 +477,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, 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;