-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
DeckLinkInputDevice.cpp
556 lines (453 loc) · 17.7 KB
/
DeckLinkInputDevice.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/* -LICENSE-START-
** Copyright (c) 2018 Blackmagic Design
**
** Permission is hereby granted, free of charge, to any person or organization
** obtaining a copy of the software and accompanying documentation covered by
** this license (the "Software") to use, reproduce, display, distribute,
** execute, and transmit the Software, and to prepare derivative works of the
** Software, and to permit third-parties to whom the Software is furnished to
** do so, all subject to the following:
**
** The copyright notices in the Software and this entire statement, including
** the above license grant, this restriction and the following disclaimer,
** must be included in all copies of the Software, in whole or in part, and
** all derivative works of the Software, unless such copies or derivative
** works are solely in the form of machine-executable object code generated by
** a source language processor.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
** SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
** FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
** ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
** DEALINGS IN THE SOFTWARE.
** -LICENSE-END-
*/
#include "AncillaryDataTable.h"
#include "DeckLinkCapture.h"
#include "DeckLinkInputDevice.h"
#include <QCoreApplication>
#include <QDebug>
#include <QMessageBox>
#include <QTextStream>
#include "common.h"
#include <omp.h>
struct DeckLinkInputDevice::Private {
QAtomicInt refcount = 1;
DeckLinkCapture *capture = nullptr;
QString device_name;
IDeckLink *decklink = nullptr;
IDeckLinkInput *decklink_input = nullptr;
IDeckLinkConfiguration *decklink_config = nullptr;
IDeckLinkHDMIInputEDID *decklink_hdmi_input_edid = nullptr;
IDeckLinkProfileManager *decklink_profile_manager = nullptr;
BOOL supports_format_detection = false;
bool currently_capturing = false;
bool apply_detected_input_mode = false;
int64_t supported_input_connections = 0;
};
DeckLinkInputDevice::DeckLinkInputDevice(DeckLinkCapture *capture, IDeckLink *device)
: m(new Private)
{
m->capture = capture;
m->decklink = device;
m->decklink->AddRef();
}
DeckLinkInputDevice::~DeckLinkInputDevice()
{
if (m->decklink_hdmi_input_edid) {
m->decklink_hdmi_input_edid->Release();
m->decklink_hdmi_input_edid = nullptr;
}
if (m->decklink_profile_manager) {
m->decklink_profile_manager->Release();
m->decklink_profile_manager = nullptr;
}
if (m->decklink_input) {
m->decklink_input->Release();
m->decklink_input = nullptr;
}
if (m->decklink_config) {
m->decklink_config->Release();
m->decklink_config = nullptr;
}
if (m->decklink) {
m->decklink->Release();
m->decklink = nullptr;
}
delete m;
}
HRESULT DeckLinkInputDevice::QueryInterface(REFIID iid, void **ppv)
{
CFUUIDBytes iunknown;
HRESULT result = E_NOINTERFACE;
if (!ppv)
return E_INVALIDARG;
// Initialise the return result
*ppv = nullptr;
// Obtain the IUnknown interface and compare it the provided REFIID
#ifdef Q_OS_WIN
iunknown = IID_IUnknown;
#else
iunknown = CFUUIDGetUUIDBytes(IUnknownUUID);
#endif
if (memcmp(&iid, &iunknown, sizeof(REFIID)) == 0) {
*ppv = this;
AddRef();
result = S_OK;
} else if (memcmp(&iid, &IID_IDeckLinkInputCallback, sizeof(REFIID)) == 0) {
*ppv = (IDeckLinkInputCallback*)this;
AddRef();
result = S_OK;
}
return result;
}
ULONG DeckLinkInputDevice::AddRef(void)
{
return (ULONG)m->refcount.fetchAndAddAcquire(1);
}
ULONG DeckLinkInputDevice::Release(void)
{
ULONG newRefValue = m->refcount.fetchAndAddAcquire(-1);
if (newRefValue == 0) {
delete this;
return 0;
}
return newRefValue;
}
bool DeckLinkInputDevice::init()
{
HRESULT result;
IDeckLinkProfileAttributes *deckLinkAttributes = nullptr;
DLString deviceNameStr;
// Get input interface
result = m->decklink->QueryInterface(IID_IDeckLinkInput, (void**)&m->decklink_input);
if (result != S_OK) {
// This may occur if device does not have input interface, for instance DeckLink Mini Monitor.
return false;
}
// Get configuration interface so we can change input connector
// We hold onto IDeckLinkConfiguration until destructor to retain input connector setting
result = m->decklink->QueryInterface(IID_IDeckLinkConfiguration, (void**)&m->decklink_config);
if (result != S_OK) {
m->capture->criticalError("DeckLink Input initialization error", "Unable to query IDeckLinkConfiguration object interface");
return false;
}
// Get attributes interface
result = m->decklink->QueryInterface(IID_IDeckLinkProfileAttributes, (void**) &deckLinkAttributes);
if (result != S_OK) {
m->capture->criticalError("DeckLink Input initialization error", "Unable to query IDeckLinkProfileAttributes object interface");
return false;
}
// Check if input mode detection is supported.
if (deckLinkAttributes->GetFlag(BMDDeckLinkSupportsInputFormatDetection, &m->supports_format_detection) != S_OK) {
m->supports_format_detection = false;
}
// Get the supported input connections for the device
if (deckLinkAttributes->GetInt(BMDDeckLinkVideoInputConnections, &m->supported_input_connections) != S_OK) {
m->supported_input_connections = 0;
}
deckLinkAttributes->Release();
// Enable all EDID functionality if possible
if (m->decklink->QueryInterface(IID_IDeckLinkHDMIInputEDID, (void **)&m->decklink_hdmi_input_edid) == S_OK && m->decklink_hdmi_input_edid) {
int64_t allKnownRanges = bmdDynamicRangeSDR | bmdDynamicRangeHDRStaticPQ | bmdDynamicRangeHDRStaticHLG;
m->decklink_hdmi_input_edid->SetInt(bmdDeckLinkHDMIInputEDIDDynamicRange, allKnownRanges);
m->decklink_hdmi_input_edid->WriteToEDID();
}
// Get device name
result = m->decklink->GetDisplayName(&deviceNameStr);
if (result == S_OK) {
m->device_name = deviceNameStr;
} else {
m->device_name = "DeckLink";
}
// Get the profile manager interface
// Will return S_OK when the device has > 1 profiles
if (m->decklink->QueryInterface(IID_IDeckLinkProfileManager, (void**) &m->decklink_profile_manager) != S_OK) {
m->decklink_profile_manager = nullptr;
}
return true;
}
const QString &DeckLinkInputDevice::getDeviceName() const
{
return m->device_name;
}
bool DeckLinkInputDevice::isCapturing() const
{
return m->currently_capturing;
}
bool DeckLinkInputDevice::supportsFormatDetection() const
{
return m->supports_format_detection;
}
BMDVideoConnection DeckLinkInputDevice::getVideoConnections() const
{
return (BMDVideoConnection)m->supported_input_connections;
}
bool DeckLinkInputDevice::startCapture(BMDDisplayMode displayMode, IDeckLinkScreenPreviewCallback *screenPreviewCallback, bool applyDetectedInputMode, bool input_audio)
{
m->capture->clearCriticalError();
HRESULT result;
BMDVideoInputFlags video_input_flags = bmdVideoInputFlagDefault;
m->apply_detected_input_mode = applyDetectedInputMode;
// Enable input video mode detection if the device supports it
if (m->supports_format_detection) {
video_input_flags |= bmdVideoInputEnableFormatDetection;
}
// Set the screen preview
m->decklink_input->SetScreenPreviewCallback(screenPreviewCallback);
// Set capture callback
m->decklink_input->SetCallback(this);
// Set the video input mode
result = m->decklink_input->EnableVideoInput(displayMode, bmdFormat8BitYUV, video_input_flags);
if (result != S_OK) {
m->capture->criticalError("Error starting the capture", "This application was unable to select the chosen video mode. Perhaps, the selected device is currently in-use.");
return false;
}
if (input_audio) {
result = m->decklink_input->EnableAudioInput(bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger, 2);
} else {
result = m->decklink_input->DisableAudioInput();
}
// Start the capture
result = m->decklink_input->StartStreams();
if (result != S_OK) {
m->capture->criticalError("Error starting the capture", "This application was unable to start the capture. Perhaps, the selected device is currently in-use.");
return false;
}
m->currently_capturing = true;
return true;
}
void DeckLinkInputDevice::stopCapture()
{
if (m->decklink_input) {
// Stop the capture
m->decklink_input->StopStreams();
//
m->decklink_input->SetScreenPreviewCallback(nullptr);
// Delete capture callback
m->decklink_input->SetCallback(nullptr);
}
m->currently_capturing = false;
}
IDeckLink *DeckLinkInputDevice::getDeckLinkInstance()
{
return m->decklink;
}
IDeckLinkInput *DeckLinkInputDevice::getDeckLinkInput()
{
return m->decklink_input;
}
IDeckLinkConfiguration *DeckLinkInputDevice::getDeckLinkConfiguration()
{
return m->decklink_config;
}
IDeckLinkProfileManager *DeckLinkInputDevice::getProfileManager()
{
return m->decklink_profile_manager;
}
Rational DeckLinkInputDevice::frameRate(IDeckLinkDisplayMode *mode)
{
BMDTimeValue duration = 0;
BMDTimeScale scale = 0;
mode->GetFrameRate(&duration, &scale);
return {scale, duration};
}
HRESULT DeckLinkInputDevice::VideoInputFormatChanged(BMDVideoInputFormatChangedEvents notificationEvents, IDeckLinkDisplayMode *newMode, BMDDetectedVideoInputFormatFlags detectedSignalFlags)
{
HRESULT result;
BMDPixelFormat pixel_format = bmdFormat8BitYUV;
// Unexpected callback when auto-detect mode not enabled
if (!m->apply_detected_input_mode) {
return E_FAIL;
}
if (detectedSignalFlags & bmdDetectedVideoInputRGB444) {
pixel_format = bmdFormat10BitRGB;
}
m->capture->setPixelFormat(pixel_format);
// Stop the capture
m->decklink_input->StopStreams();
// Set the video input mode
result = m->decklink_input->EnableVideoInput(newMode->GetDisplayMode(), pixel_format, bmdVideoInputEnableFormatDetection);
if (result != S_OK) {
m->capture->criticalError("Error restarting the capture", "This application was unable to set new display mode");
return result;
}
Rational fps = frameRate(newMode);
// Start the capture
result = m->decklink_input->StartStreams();
if (result != S_OK) {
m->capture->criticalError("Error restarting the capture", "This application was unable to restart capture");
return result;
}
// Notify UI of new display mode
if (notificationEvents & bmdVideoInputDisplayModeChanged) {
QCoreApplication::postEvent(m->capture, new DeckLinkInputFormatChangedEvent(newMode->GetDisplayMode(), fps));
}
return S_OK;
}
HRESULT DeckLinkInputDevice::VideoInputFrameArrived(IDeckLinkVideoInputFrame *videoFrame, IDeckLinkAudioInputPacket *audioPacket)
{
if (!videoFrame) return S_OK;
bool signal_valid = (videoFrame->GetFlags() & bmdFrameHasNoInputSource) == 0;
VideoFrameData t;
if (m->capture) {
t.d->signal_valid = signal_valid;
getAncillaryDataFromFrame(videoFrame, bmdTimecodeVITC, &t.d->ancillary_data.vitcF1Timecode, &t.d->ancillary_data.vitcF1UserBits);
getAncillaryDataFromFrame(videoFrame, bmdTimecodeVITCField2, &t.d->ancillary_data.vitcF2Timecode, &t.d->ancillary_data.vitcF2UserBits);
getAncillaryDataFromFrame(videoFrame, bmdTimecodeRP188VITC1, &t.d->ancillary_data.rp188vitc1Timecode, &t.d->ancillary_data.rp188vitc1UserBits);
getAncillaryDataFromFrame(videoFrame, bmdTimecodeRP188VITC2, &t.d->ancillary_data.rp188vitc2Timecode, &t.d->ancillary_data.rp188vitc2UserBits);
getAncillaryDataFromFrame(videoFrame, bmdTimecodeRP188LTC, &t.d->ancillary_data.rp188ltcTimecode, &t.d->ancillary_data.rp188ltcUserBits);
getAncillaryDataFromFrame(videoFrame, bmdTimecodeRP188HighFrameRate, &t.d->ancillary_data.rp188hfrtcTimecode, &t.d->ancillary_data.rp188hfrtcUserBits);
getHDRMetadataFromFrame(videoFrame, &t.d->hdr_metadata);
if (audioPacket) {
const int channels = 2;
const int frames = audioPacket->GetSampleFrameCount();
const int bytes = frames * sizeof(int16_t) * channels;
void *data = nullptr;
audioPacket->GetBytes(&data);
if (data && bytes > 0) {
t.d->audio = QByteArray((char const *)data, bytes);
}
}
if (videoFrame) {
t.d->pixfmt = videoFrame->GetPixelFormat();
int w = videoFrame->GetWidth();
int h = videoFrame->GetHeight();
int stride = videoFrame->GetRowBytes();
uint8_t const *bytes = nullptr;
if (w > 0 && h > 0 && videoFrame->GetBytes((void **)&bytes) == S_OK && bytes) {
t.d->image = DeckLinkCapture::createImage(w, h, t.d->pixfmt, bytes, stride * h);
}
}
emit m->capture->newFrame(t);
}
return S_OK;
}
void DeckLinkInputDevice::getAncillaryDataFromFrame(IDeckLinkVideoInputFrame *videoFrame, BMDTimecodeFormat timecodeFormat, QString *timecodeString, QString *userBitsString)
{
IDeckLinkTimecode *timecode = nullptr;
DLString timecodeStr;
BMDTimecodeUserBits userBits = 0;
if (videoFrame && timecodeString && userBitsString && videoFrame->GetTimecode(timecodeFormat, &timecode) == S_OK) {
if (timecode->GetString(&timecodeStr) == S_OK) {
*timecodeString = timecodeStr;
} else {
*timecodeString = "";
}
timecode->GetTimecodeUserBits(&userBits);
*userBitsString = QString("0x%1").arg(userBits, 8, 16, QChar('0'));
timecode->Release();
} else {
*timecodeString = "";
*userBitsString = "";
}
}
void DeckLinkInputDevice::getHDRMetadataFromFrame(IDeckLinkVideoInputFrame* videoFrame, HDRMetadataStruct* hdrMetadata)
{
hdrMetadata->electroOpticalTransferFunction = "";
hdrMetadata->displayPrimariesRedX = "";
hdrMetadata->displayPrimariesRedY = "";
hdrMetadata->displayPrimariesGreenX = "";
hdrMetadata->displayPrimariesGreenY = "";
hdrMetadata->displayPrimariesBlueX = "";
hdrMetadata->displayPrimariesBlueY = "";
hdrMetadata->whitePointX = "";
hdrMetadata->whitePointY = "";
hdrMetadata->maxDisplayMasteringLuminance = "";
hdrMetadata->minDisplayMasteringLuminance = "";
hdrMetadata->maximumContentLightLevel = "";
hdrMetadata->maximumFrameAverageLightLevel = "";
hdrMetadata->colorspace = "";
if (videoFrame->GetFlags() & bmdFrameContainsHDRMetadata) {
IDeckLinkVideoFrameMetadataExtensions* metadataExtensions = nullptr;
if (videoFrame->QueryInterface(IID_IDeckLinkVideoFrameMetadataExtensions, (void**)&metadataExtensions) == S_OK) {
double doubleValue = 0.0;
int64_t intValue = 0;
if (metadataExtensions->GetInt(bmdDeckLinkFrameMetadataHDRElectroOpticalTransferFunc, &intValue) == S_OK) {
switch (intValue) {
case 0:
hdrMetadata->electroOpticalTransferFunction = "SDR";
break;
case 1:
hdrMetadata->electroOpticalTransferFunction = "HDR";
break;
case 2:
hdrMetadata->electroOpticalTransferFunction = "PQ (ST2084)";
break;
case 3:
hdrMetadata->electroOpticalTransferFunction = "HLG";
break;
default:
hdrMetadata->electroOpticalTransferFunction = QString("Unknown EOTF: %1").arg((int32_t)intValue);
break;
}
}
if (metadataExtensions->GetFloat(bmdDeckLinkFrameMetadataHDRDisplayPrimariesRedX, &doubleValue) == S_OK) {
hdrMetadata->displayPrimariesRedX = QString::number(doubleValue, 'f', 4);
}
if (metadataExtensions->GetFloat(bmdDeckLinkFrameMetadataHDRDisplayPrimariesRedY, &doubleValue) == S_OK) {
hdrMetadata->displayPrimariesRedY = QString::number(doubleValue, 'f', 4);
}
if (metadataExtensions->GetFloat(bmdDeckLinkFrameMetadataHDRDisplayPrimariesGreenX, &doubleValue) == S_OK) {
hdrMetadata->displayPrimariesGreenX = QString::number(doubleValue, 'f', 4);
}
if (metadataExtensions->GetFloat(bmdDeckLinkFrameMetadataHDRDisplayPrimariesGreenY, &doubleValue) == S_OK) {
hdrMetadata->displayPrimariesGreenY = QString::number(doubleValue, 'f', 4);
}
if (metadataExtensions->GetFloat(bmdDeckLinkFrameMetadataHDRDisplayPrimariesBlueX, &doubleValue) == S_OK) {
hdrMetadata->displayPrimariesBlueX = QString::number(doubleValue, 'f', 4);
}
if (metadataExtensions->GetFloat(bmdDeckLinkFrameMetadataHDRDisplayPrimariesBlueY, &doubleValue) == S_OK) {
hdrMetadata->displayPrimariesBlueY = QString::number(doubleValue, 'f', 4);
}
if (metadataExtensions->GetFloat(bmdDeckLinkFrameMetadataHDRWhitePointX, &doubleValue) == S_OK) {
hdrMetadata->whitePointX = QString::number(doubleValue, 'f', 4);
}
if (metadataExtensions->GetFloat(bmdDeckLinkFrameMetadataHDRWhitePointY, &doubleValue) == S_OK) {
hdrMetadata->whitePointY = QString::number(doubleValue, 'f', 4);
}
if (metadataExtensions->GetFloat(bmdDeckLinkFrameMetadataHDRMaxDisplayMasteringLuminance, &doubleValue) == S_OK) {
hdrMetadata->maxDisplayMasteringLuminance = QString::number(doubleValue, 'f', 4);
}
if (metadataExtensions->GetFloat(bmdDeckLinkFrameMetadataHDRMinDisplayMasteringLuminance, &doubleValue) == S_OK) {
hdrMetadata->minDisplayMasteringLuminance = QString::number(doubleValue, 'f', 4);
}
if (metadataExtensions->GetFloat(bmdDeckLinkFrameMetadataHDRMaximumContentLightLevel, &doubleValue) == S_OK) {
hdrMetadata->maximumContentLightLevel = QString::number(doubleValue, 'f', 4);
}
if (metadataExtensions->GetFloat(bmdDeckLinkFrameMetadataHDRMaximumFrameAverageLightLevel, &doubleValue) == S_OK) {
hdrMetadata->maximumFrameAverageLightLevel = QString::number(doubleValue, 'f', 4);
}
if (metadataExtensions->GetInt(bmdDeckLinkFrameMetadataColorspace, &intValue) == S_OK) {
switch (intValue) {
case bmdColorspaceRec601:
hdrMetadata->colorspace = "Rec.601";
break;
case bmdColorspaceRec709:
hdrMetadata->colorspace = "Rec.709";
break;
case bmdColorspaceRec2020:
hdrMetadata->colorspace = "Rec.2020";
break;
default:
hdrMetadata->colorspace = QString("Unknown Colorspace: %1").arg((int32_t)intValue);
break;
}
}
metadataExtensions->Release();
}
}
}
DeckLinkInputFormatChangedEvent::DeckLinkInputFormatChangedEvent(BMDDisplayMode displayMode, Rational const &fps)
: QEvent(kVideoFormatChangedEvent)
, display_mode_(displayMode)
, fps_(fps)
{
}
//DeckLinkInputFrameArrivedEvent::DeckLinkInputFrameArrivedEvent(bool signalValid)
// : QEvent(kVideoFrameArrivedEvent)
// , signal_valid_(signalValid)
//{
//}