]> git.sesse.net Git - nageru/blobdiff - nageru/decklink_capture.cpp
Fix a Clang 19 warning.
[nageru] / nageru / decklink_capture.cpp
index f7016dce7c07f607455481c9d00a34e2d379ac99..90d86b15dd580f0fe73d96148fa4f728ff280c9f 100644 (file)
@@ -1,10 +1,16 @@
 #include "decklink_capture.h"
+#include "defs.h"
 
 #include <DeckLinkAPI.h>
 #include <DeckLinkAPIConfiguration.h>
 #include <DeckLinkAPIDiscovery.h>
 #include <DeckLinkAPIModes.h>
+#include <DeckLinkAPITypes.h>
+#include <LinuxCOM.h>
 #include <assert.h>
+#include <errno.h>
+#include <sched.h>
+#include <string>
 #ifdef __SSE2__
 #include <immintrin.h>
 #endif
@@ -24,8 +30,6 @@
 #include "shared/memcpy_interleaved.h"
 #include "v210_converter.h"
 
-#define FRAME_SIZE (8 << 20)  // 8 MB.
-
 using namespace std;
 using namespace std::chrono;
 using namespace std::placeholders;
@@ -63,20 +67,20 @@ DeckLinkCapture::DeckLinkCapture(IDeckLink *card, int card_index)
 
        if (card->QueryInterface(IID_IDeckLinkInput, (void**)&input) != S_OK) {
                fprintf(stderr, "Card %d has no inputs\n", card_index);
-               exit(1);
+               abort();
        }
 
        IDeckLinkAttributes *attr;
        if (card->QueryInterface(IID_IDeckLinkAttributes, (void**)&attr) != S_OK) {
                fprintf(stderr, "Card %d has no attributes\n", card_index);
-               exit(1);
+               abort();
        }
 
        // 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);
+               abort();
        }
        const vector<pair<BMDVideoConnection, string>> video_input_types = {
                { bmdVideoConnectionSDI, "SDI" },
@@ -96,7 +100,7 @@ DeckLinkCapture::DeckLinkCapture(IDeckLink *card, int card_index)
        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);
+               abort();
        }
        const vector<pair<BMDAudioConnection, string>> audio_input_types = {
                { bmdAudioConnectionEmbedded, "Embedded" },
@@ -134,7 +138,7 @@ DeckLinkCapture::DeckLinkCapture(IDeckLink *card, int card_index)
        /* 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);
+               abort();
        }
 
        BMDVideoConnection connection = pick_default_video_connection(card, BMDDeckLinkVideoInputConnections, card_index);
@@ -145,7 +149,7 @@ DeckLinkCapture::DeckLinkCapture(IDeckLink *card, int card_index)
        IDeckLinkDisplayModeIterator *mode_it;
        if (input->GetDisplayModeIterator(&mode_it) != S_OK) {
                fprintf(stderr, "Failed to enumerate display modes for card %d\n", card_index);
-               exit(1);
+               abort();
        }
 
        video_modes = summarize_video_modes(mode_it, card_index);
@@ -198,7 +202,7 @@ HRESULT STDMETHODCALLTYPE DeckLinkCapture::VideoInputFormatChanged(
        }
        if (display_mode->GetFrameRate(&frame_duration, &time_scale) != S_OK) {
                fprintf(stderr, "Could not get new frame rate\n");
-               exit(1);
+               abort();
        }
        field_dominance = display_mode->GetFieldDominance();
        return S_OK;
@@ -246,13 +250,20 @@ HRESULT STDMETHODCALLTYPE DeckLinkCapture::VideoInputFrameArrived(
                const int stride = video_frame->GetRowBytes();
                const BMDPixelFormat format = video_frame->GetPixelFormat();
                assert(format == pixel_format_to_bmd(current_pixel_format));
-               if (global_flags.ten_bit_input) {
+               if (global_flags.bit_depth > 8) {
                        assert(stride == int(v210Converter::get_v210_stride(width)));
                } else {
                        assert(stride == width * 2);
                }
 
-               current_video_frame = video_frame_allocator->create_frame(width, height, stride);
+               if (width * stride > FRAME_SIZE) {
+                       // TODO: If we had an OpenGL context here, calling create_frame()
+                       // would be completely fine.
+                       fprintf(stderr, "Card %u: Captured frame %d x %d (stride %d) would be larger than supported frame size (%d > %d), skipping.\n",
+                               card_index, width, height, stride, width * stride, FRAME_SIZE);
+               } else {
+                       current_video_frame = video_frame_allocator->create_frame(width, height, stride);
+               }
                if (current_video_frame.data != nullptr) {
                        const uint8_t *src;
                        video_frame->GetBytes((void **)&src);
@@ -283,12 +294,12 @@ HRESULT STDMETHODCALLTYPE DeckLinkCapture::VideoInputFrameArrived(
                if (current_audio_frame.data != nullptr) {
                        const uint8_t *src;
                        audio_frame->GetBytes((void **)&src);
-                       current_audio_frame.len = sizeof(int32_t) * 2 * num_samples;
+                       current_audio_frame.len = sizeof(int32_t) * 8 * num_samples;
 
                        memcpy(current_audio_frame.data, src, current_audio_frame.len);
 
                        audio_format.bits_per_sample = 32;
-                       audio_format.num_channels = 2;
+                       audio_format.num_channels = 8;
                }
        }
 
@@ -326,16 +337,16 @@ void DeckLinkCapture::start_bm_capture()
        }
        if (input->EnableVideoInput(current_video_mode, pixel_format_to_bmd(current_pixel_format), 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);
+               abort();
        }
-       if (input->EnableAudioInput(48000, bmdAudioSampleType32bitInteger, 2) != S_OK) {
+       if (input->EnableAudioInput(48000, bmdAudioSampleType32bitInteger, 8) != S_OK) {
                fprintf(stderr, "Failed to enable audio input for card %d\n", card_index);
-               exit(1);
+               abort();
        }
 
        if (input->StartStreams() != S_OK) {
                fprintf(stderr, "StartStreams failed\n");
-               exit(1);
+               abort();
        }
        running = true;
 }
@@ -348,12 +359,13 @@ void DeckLinkCapture::stop_dequeue_thread()
        HRESULT result = input->StopStreams();
        if (result != S_OK) {
                fprintf(stderr, "StopStreams failed with error 0x%x\n", result);
-               exit(1);
+               abort();
        }
 
        // We could call DisableVideoInput() and DisableAudioInput() here,
        // but they seem to be taking a really long time, and we only do this
-       // during shutdown anyway, so StopStreams() will suffice.
+       // during shutdown anyway (except when switching to output mode,
+       // where DeckLinkOutput does the disabling), so StopStreams() will suffice.
 
        running = false;
 }
@@ -363,11 +375,11 @@ void DeckLinkCapture::set_video_mode(uint32_t video_mode_id)
        if (running) {
                if (input->PauseStreams() != S_OK) {
                        fprintf(stderr, "PauseStreams failed\n");
-                       exit(1);
+                       abort();
                }
                if (input->FlushStreams() != S_OK) {
                        fprintf(stderr, "FlushStreams failed\n");
-                       exit(1);
+                       abort();
                }
        }
 
@@ -376,7 +388,7 @@ void DeckLinkCapture::set_video_mode(uint32_t video_mode_id)
        if (running) {
                if (input->StartStreams() != S_OK) {
                        fprintf(stderr, "StartStreams failed\n");
-                       exit(1);
+                       abort();
                }
        }
 }
@@ -393,17 +405,17 @@ void DeckLinkCapture::set_video_mode_no_restart(uint32_t video_mode_id)
        IDeckLinkDisplayMode *display_mode;
        if (input->DoesSupportVideoMode(video_mode_id, pixel_format_to_bmd(current_pixel_format), /*flags=*/0, &support, &display_mode)) {
                fprintf(stderr, "Failed to query display mode for card %d\n", card_index);
-               exit(1);
+               abort();
        }
 
        if (support == bmdDisplayModeNotSupported) {
                fprintf(stderr, "Card %d does not support display mode\n", card_index);
-               exit(1);
+               abort();
        }
 
        if (display_mode->GetFrameRate(&frame_duration, &time_scale) != S_OK) {
                fprintf(stderr, "Could not get frame rate for card %d\n", card_index);
-               exit(1);
+               abort();
        }
 
        field_dominance = display_mode->GetFieldDominance();
@@ -411,7 +423,7 @@ void DeckLinkCapture::set_video_mode_no_restart(uint32_t video_mode_id)
        if (running) {
                if (input->EnableVideoInput(video_mode_id, pixel_format_to_bmd(current_pixel_format), 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);
+                       abort();
                }
        }
 
@@ -422,7 +434,7 @@ 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);
+               abort();
        }
 
        current_video_input = video_input_id;
@@ -432,7 +444,7 @@ 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);
+               abort();
        }
 
        current_audio_input = audio_input_id;