]> git.sesse.net Git - nageru/blob - decklink_util.cpp
Fix an issue where the mixer lagging too much behind CEF would cause us to display...
[nageru] / decklink_util.cpp
1 #include <DeckLinkAPI.h>
2 #include <DeckLinkAPIModes.h>
3
4 #include <assert.h>
5
6 #include "decklink_util.h"
7
8 using namespace bmusb;
9 using namespace std;
10
11 map<uint32_t, VideoMode> summarize_video_modes(IDeckLinkDisplayModeIterator *mode_it, unsigned card_index)
12 {
13         map<uint32_t, VideoMode> video_modes;
14
15         for (IDeckLinkDisplayMode *mode_ptr; mode_it->Next(&mode_ptr) == S_OK; mode_ptr->Release()) {
16                 VideoMode mode;
17
18                 const char *mode_name;
19                 if (mode_ptr->GetName(&mode_name) != S_OK) {
20                         mode.name = "Unknown mode";
21                 } else {
22                         mode.name = mode_name;
23                         free((char *)mode_name);
24                 }
25
26                 mode.autodetect = false;
27
28                 mode.width = mode_ptr->GetWidth();
29                 mode.height = mode_ptr->GetHeight();
30
31                 BMDTimeScale frame_rate_num;
32                 BMDTimeValue frame_rate_den;
33                 if (mode_ptr->GetFrameRate(&frame_rate_den, &frame_rate_num) != S_OK) {
34                         fprintf(stderr, "Could not get frame rate for mode '%s' on card %d\n", mode.name.c_str(), card_index);
35                         exit(1);
36                 }
37                 mode.frame_rate_num = frame_rate_num;
38                 mode.frame_rate_den = frame_rate_den;
39
40                 // TODO: Respect the TFF/BFF flag.
41                 mode.interlaced = (mode_ptr->GetFieldDominance() == bmdLowerFieldFirst || mode_ptr->GetFieldDominance() == bmdUpperFieldFirst);
42
43                 uint32_t id = mode_ptr->GetDisplayMode();
44                 video_modes.insert(make_pair(id, mode));
45         }
46
47         return video_modes;
48 }
49
50 BMDVideoConnection pick_default_video_connection(IDeckLink *card, BMDDeckLinkAttributeID attribute_id, unsigned card_index)
51 {
52         assert(attribute_id == BMDDeckLinkVideoInputConnections ||
53                attribute_id == BMDDeckLinkVideoOutputConnections);
54
55         IDeckLinkAttributes *attr;
56         if (card->QueryInterface(IID_IDeckLinkAttributes, (void**)&attr) != S_OK) {
57                 fprintf(stderr, "Card %u has no attributes\n", card_index);
58                 exit(1);
59         }
60
61         int64_t connection_mask;
62         if (attr->GetInt(attribute_id, &connection_mask) != S_OK) {
63                 if (attribute_id == BMDDeckLinkVideoInputConnections) {
64                         fprintf(stderr, "Failed to enumerate video inputs for card %u\n", card_index);
65                 } else {
66                         fprintf(stderr, "Failed to enumerate video outputs for card %u\n", card_index);
67                 }
68                 exit(1);
69         }
70         attr->Release();
71         if (connection_mask == 0) {
72                 if (attribute_id == BMDDeckLinkVideoInputConnections) {
73                         fprintf(stderr, "Card %u has no input connections\n", card_index);
74                 } else {
75                         fprintf(stderr, "Card %u has no outpu connectionss\n", card_index);
76                 }
77                 exit(1);
78         }
79
80         if (connection_mask & bmdVideoConnectionHDMI) {
81                 return bmdVideoConnectionHDMI;
82         } else if (connection_mask & bmdVideoConnectionSDI) {
83                 return bmdVideoConnectionSDI;
84         } else {
85                 // Fallback: Return lowest-set bit, whatever that might be.
86                 return connection_mask & (-connection_mask);
87         }
88 }