]> git.sesse.net Git - nageru/blobdiff - decklink_capture.cpp
Make it possible to load/save input mappings.
[nageru] / decklink_capture.cpp
index 57d364d0a24404a0aa94830c5166b2787de4f5d6..81566cb1a5ad20d85a3be25a8b01e2c24a62906e 100644 (file)
@@ -20,6 +20,7 @@
 
 using namespace std;
 using namespace std::placeholders;
+using namespace bmusb;
 
 namespace {
 
@@ -58,7 +59,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;
@@ -72,9 +73,9 @@ size_t memcpy_interleaved_fastpath(uint8_t *dest1, uint8_t *dest2, const uint8_t
        assert(((limit - src) % 64) == 0);
 
 #if __AVX2__
-       const __restrict __m256i *in = (const __m256i *)src;
-       __restrict __m256i *out1 = (__m256i *)dest1;
-       __restrict __m256i *out2 = (__m256i *)dest2;
+       const __m256i * __restrict in = (const __m256i *)src;
+       __m256i * __restrict out1 = (__m256i *)dest1;
+       __m256i * __restrict out2 = (__m256i *)dest2;
 
        __m256i shuffle_cw = _mm256_set_epi8(
                15, 13, 11, 9, 7, 5, 3, 1, 14, 12, 10, 8, 6, 4, 2, 0,
@@ -102,9 +103,9 @@ size_t memcpy_interleaved_fastpath(uint8_t *dest1, uint8_t *dest2, const uint8_t
                consumed += 64;
        }
 #else
-       const __restrict __m128i *in = (const __m128i *)src;
-       __restrict __m128i *out1 = (__m128i *)dest1;
-       __restrict __m128i *out2 = (__m128i *)dest2;
+       const __m128i * __restrict in = (const __m128i *)src;
+       __m128i * __restrict out1 = (__m128i *)dest1;
+       __m128i * __restrict out2 = (__m128i *)dest2;
 
        __m128i mask_lower_byte = _mm_set1_epi16(0x00ff);
        while (in < (const __m128i *)limit) {
@@ -134,15 +135,15 @@ size_t memcpy_interleaved_fastpath(uint8_t *dest1, uint8_t *dest2, const uint8_t
 }  // namespace
 
 DeckLinkCapture::DeckLinkCapture(IDeckLink *card, int card_index)
-       : card_index(card_index)
+       : card_index(card_index), card(card)
 {
        {
                const char *model_name;
                char buf[256];
                if (card->GetModelName(&model_name) == S_OK) {
-                       snprintf(buf, sizeof(buf), "Card %d: %s", card_index, model_name);
+                       snprintf(buf, sizeof(buf), "PCI card %d: %s", card_index, model_name);
                } else {
-                       snprintf(buf, sizeof(buf), "Card %d: Unknown DeckLink card", card_index);
+                       snprintf(buf, sizeof(buf), "PCI card %d: Unknown DeckLink card", card_index);
                }
                description = buf;
        }
@@ -152,23 +153,64 @@ DeckLinkCapture::DeckLinkCapture(IDeckLink *card, int card_index)
                exit(1);
        }
 
-       /* Set up the video and audio sources. */
-       IDeckLinkConfiguration *config;
-       if (card->QueryInterface(IID_IDeckLinkConfiguration, (void**)&config) != S_OK) {
-               fprintf(stderr, "Failed to get configuration interface for card %d\n", card_index);
+       IDeckLinkAttributes *attr;
+       if (card->QueryInterface(IID_IDeckLinkAttributes, (void**)&attr) != S_OK) {
+               fprintf(stderr, "Card %d has no attributes\n", card_index);
                exit(1);
        }
 
-       if (config->SetInt(bmdDeckLinkConfigVideoInputConnection, bmdVideoConnectionHDMI) != S_OK) {
-               fprintf(stderr, "Failed to set video input connection for card %d\n", card_index);
+       // Get the list of available video inputs.
+       int64_t video_input_mask;
+       if (attr->GetInt(BMDDeckLinkVideoInputConnections, &video_input_mask) != S_OK) {
+               fprintf(stderr, "Failed to enumerate video inputs for card %d\n", card_index);
                exit(1);
        }
+       const vector<pair<BMDVideoConnection, string>> video_input_types = {
+               { bmdVideoConnectionSDI, "SDI" },
+               { bmdVideoConnectionHDMI, "HDMI" },
+               { bmdVideoConnectionOpticalSDI, "Optical SDI" },
+               { bmdVideoConnectionComponent, "Component" },
+               { bmdVideoConnectionComposite, "Composite" },
+               { bmdVideoConnectionSVideo, "S-Video" }
+       };
+       for (const auto &video_input : video_input_types) {
+               if (video_input_mask & video_input.first) {
+                       video_inputs.emplace(video_input.first, video_input.second);
+               }
+       }
 
-       if (config->SetInt(bmdDeckLinkConfigAudioInputConnection, bmdAudioConnectionEmbedded) != S_OK) {
-               fprintf(stderr, "Failed to set video input connection for card %d\n", card_index);
+       // And then the available audio inputs.
+       int64_t audio_input_mask;
+       if (attr->GetInt(BMDDeckLinkAudioInputConnections, &audio_input_mask) != S_OK) {
+               fprintf(stderr, "Failed to enumerate audio inputs for card %d\n", card_index);
+               exit(1);
+       }
+       const vector<pair<BMDAudioConnection, string>> audio_input_types = {
+               { bmdAudioConnectionEmbedded, "Embedded" },
+               { bmdAudioConnectionAESEBU, "AES/EBU" },
+               { bmdAudioConnectionAnalog, "Analog" },
+               { bmdAudioConnectionAnalogXLR, "Analog XLR" },
+               { bmdAudioConnectionAnalogRCA, "Analog RCA" },
+               { bmdAudioConnectionMicrophone, "Microphone" },
+               { bmdAudioConnectionHeadphones, "Headphones" }
+       };
+       for (const auto &audio_input : audio_input_types) {
+               if (audio_input_mask & audio_input.first) {
+                       audio_inputs.emplace(audio_input.first, audio_input.second);
+               }
+       }
+
+       attr->Release();
+
+       /* Set up the video and audio sources. */
+       if (card->QueryInterface(IID_IDeckLinkConfiguration, (void**)&config) != S_OK) {
+               fprintf(stderr, "Failed to get configuration interface for card %d\n", card_index);
                exit(1);
        }
 
+       set_video_input(bmdVideoConnectionHDMI);
+       set_audio_input(bmdAudioConnectionEmbedded);
+
        IDeckLinkDisplayModeIterator *mode_it;
        if (input->GetDisplayModeIterator(&mode_it) != S_OK) {
                fprintf(stderr, "Failed to enumerate display modes for card %d\n", card_index);
@@ -177,7 +219,6 @@ DeckLinkCapture::DeckLinkCapture(IDeckLink *card, int card_index)
 
        for (IDeckLinkDisplayMode *mode_ptr; mode_it->Next(&mode_ptr) == S_OK; mode_ptr->Release()) {
                VideoMode mode;
-               mode.id = mode_ptr->GetDisplayMode();
 
                const char *mode_name;
                if (mode_ptr->GetName(&mode_name) != S_OK) {
@@ -203,11 +244,11 @@ DeckLinkCapture::DeckLinkCapture(IDeckLink *card, int card_index)
                // TODO: Respect the TFF/BFF flag.
                mode.interlaced = (mode_ptr->GetFieldDominance() == bmdLowerFieldFirst || mode_ptr->GetFieldDominance() == bmdUpperFieldFirst);
 
-               video_modes.push_back(mode);
+               uint32_t id = mode_ptr->GetDisplayMode();
+               video_modes.insert(make_pair(id, mode));
        }
 
-       // TODO: Make the user mode selectable.
-       set_video_mode(bmdModeHD720p5994);
+       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);
@@ -222,6 +263,9 @@ DeckLinkCapture::~DeckLinkCapture()
        if (has_dequeue_callbacks) {
                dequeue_cleanup_callback();
        }
+       input->Release();
+       config->Release();
+       card->Release();
 }
 
 HRESULT STDMETHODCALLTYPE DeckLinkCapture::QueryInterface(REFIID, LPVOID *)
@@ -341,10 +385,12 @@ HRESULT STDMETHODCALLTYPE DeckLinkCapture::VideoInputFrameArrived(
 void DeckLinkCapture::configure_card()
 {
        if (video_frame_allocator == nullptr) {
-               set_video_frame_allocator(new MallocFrameAllocator(FRAME_SIZE, NUM_QUEUED_VIDEO_FRAMES));  // FIXME: leak.
+               owned_video_frame_allocator.reset(new MallocFrameAllocator(FRAME_SIZE, NUM_QUEUED_VIDEO_FRAMES));
+               set_video_frame_allocator(owned_video_frame_allocator.get());
        }
        if (audio_frame_allocator == nullptr) {
-               set_audio_frame_allocator(new MallocFrameAllocator(65536, NUM_QUEUED_AUDIO_FRAMES));  // FIXME: leak.
+               owned_audio_frame_allocator.reset(new MallocFrameAllocator(65536, NUM_QUEUED_AUDIO_FRAMES));
+               set_audio_frame_allocator(owned_audio_frame_allocator.get());
        }
 }
 
@@ -365,6 +411,21 @@ void DeckLinkCapture::stop_dequeue_thread()
 }
 
 void DeckLinkCapture::set_video_mode(uint32_t video_mode_id)
+{
+       if (input->StopStreams() != S_OK) {
+               fprintf(stderr, "StopStreams failed\n");
+               exit(1);
+       }
+
+       set_video_mode_no_restart(video_mode_id);
+
+       if (input->StartStreams() != S_OK) {
+               fprintf(stderr, "StartStreams failed\n");
+               exit(1);
+       }
+}
+
+void DeckLinkCapture::set_video_mode_no_restart(uint32_t video_mode_id)
 {
        BMDDisplayModeSupport support;
        IDeckLinkDisplayMode *display_mode;
@@ -384,9 +445,29 @@ void DeckLinkCapture::set_video_mode(uint32_t video_mode_id)
        }
 
        if (input->EnableVideoInput(video_mode_id, bmdFormat8BitYUV, 0) != S_OK) {
-               fprintf(stderr, "Failed to set 720p59.94 connection for card %d\n", card_index);
+               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;
 }
+
+void DeckLinkCapture::set_video_input(uint32_t video_input_id)
+{
+       if (config->SetInt(bmdDeckLinkConfigVideoInputConnection, video_input_id) != S_OK) {
+               fprintf(stderr, "Failed to set video input connection for card %d\n", card_index);
+               exit(1);
+       }
+
+       current_video_input = video_input_id;
+}
+
+void DeckLinkCapture::set_audio_input(uint32_t audio_input_id)
+{
+       if (config->SetInt(bmdDeckLinkConfigAudioInputConnection, audio_input_id) != S_OK) {
+               fprintf(stderr, "Failed to set audio input connection for card %d\n", card_index);
+               exit(1);
+       }
+
+       current_audio_input = audio_input_id;
+}