decklink-device-instance.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. #include "decklink-device-instance.hpp"
  2. #include "audio-repack.hpp"
  3. #include "DecklinkInput.hpp"
  4. #include "DecklinkOutput.hpp"
  5. #include <util/platform.h>
  6. #include <util/threading.h>
  7. #include <util/util_uint64.h>
  8. #include <sstream>
  9. #include <iomanip>
  10. #include <algorithm>
  11. #include "OBSVideoFrame.h"
  12. #include <caption/caption.h>
  13. #include <util/bitstream.h>
  14. static inline enum video_format ConvertPixelFormat(BMDPixelFormat format)
  15. {
  16. switch (format) {
  17. case bmdFormat8BitBGRA:
  18. return VIDEO_FORMAT_BGRX;
  19. default:
  20. case bmdFormat8BitYUV:;
  21. }
  22. return VIDEO_FORMAT_UYVY;
  23. }
  24. static inline int ConvertChannelFormat(speaker_layout format)
  25. {
  26. switch (format) {
  27. case SPEAKERS_2POINT1:
  28. case SPEAKERS_4POINT0:
  29. case SPEAKERS_4POINT1:
  30. case SPEAKERS_5POINT1:
  31. case SPEAKERS_7POINT1:
  32. return 8;
  33. default:
  34. case SPEAKERS_STEREO:
  35. return 2;
  36. }
  37. }
  38. static inline audio_repack_mode_t ConvertRepackFormat(speaker_layout format,
  39. bool swap)
  40. {
  41. switch (format) {
  42. case SPEAKERS_2POINT1:
  43. return repack_mode_8to3ch;
  44. case SPEAKERS_4POINT0:
  45. return repack_mode_8to4ch;
  46. case SPEAKERS_4POINT1:
  47. return swap ? repack_mode_8to5ch_swap : repack_mode_8to5ch;
  48. case SPEAKERS_5POINT1:
  49. return swap ? repack_mode_8to6ch_swap : repack_mode_8to6ch;
  50. case SPEAKERS_7POINT1:
  51. return swap ? repack_mode_8ch_swap : repack_mode_8ch;
  52. default:
  53. assert(false && "No repack requested");
  54. return (audio_repack_mode_t)-1;
  55. }
  56. }
  57. DeckLinkDeviceInstance::DeckLinkDeviceInstance(DecklinkBase *decklink_,
  58. DeckLinkDevice *device_)
  59. : currentFrame(),
  60. currentPacket(),
  61. currentCaptions(),
  62. decklink(decklink_),
  63. device(device_)
  64. {
  65. currentPacket.samples_per_sec = 48000;
  66. currentPacket.speakers = SPEAKERS_STEREO;
  67. currentPacket.format = AUDIO_FORMAT_16BIT;
  68. }
  69. DeckLinkDeviceInstance::~DeckLinkDeviceInstance()
  70. {
  71. if (convertFrame) {
  72. delete convertFrame;
  73. }
  74. }
  75. void DeckLinkDeviceInstance::HandleAudioPacket(
  76. IDeckLinkAudioInputPacket *audioPacket, const uint64_t timestamp)
  77. {
  78. if (audioPacket == nullptr)
  79. return;
  80. void *bytes;
  81. if (audioPacket->GetBytes(&bytes) != S_OK) {
  82. LOG(LOG_WARNING, "Failed to get audio packet data");
  83. return;
  84. }
  85. const uint32_t frameCount =
  86. (uint32_t)audioPacket->GetSampleFrameCount();
  87. currentPacket.frames = frameCount;
  88. currentPacket.timestamp = timestamp;
  89. if (decklink && !static_cast<DeckLinkInput *>(decklink)->buffering) {
  90. currentPacket.timestamp = os_gettime_ns();
  91. currentPacket.timestamp -=
  92. util_mul_div64(frameCount, 1000000000ULL,
  93. currentPacket.samples_per_sec);
  94. }
  95. int maxdevicechannel = device->GetMaxChannel();
  96. if (channelFormat != SPEAKERS_UNKNOWN &&
  97. channelFormat != SPEAKERS_MONO &&
  98. channelFormat != SPEAKERS_STEREO &&
  99. (channelFormat != SPEAKERS_7POINT1 ||
  100. static_cast<DeckLinkInput *>(decklink)->swap) &&
  101. maxdevicechannel >= 8) {
  102. if (audioRepacker->repack((uint8_t *)bytes, frameCount) < 0) {
  103. LOG(LOG_ERROR, "Failed to convert audio packet data");
  104. return;
  105. }
  106. currentPacket.data[0] = (*audioRepacker)->packet_buffer;
  107. } else {
  108. currentPacket.data[0] = (uint8_t *)bytes;
  109. }
  110. nextAudioTS = timestamp +
  111. util_mul_div64(frameCount, 1000000000ULL, 48000ULL) + 1;
  112. obs_source_output_audio(
  113. static_cast<DeckLinkInput *>(decklink)->GetSource(),
  114. &currentPacket);
  115. }
  116. void DeckLinkDeviceInstance::HandleVideoFrame(
  117. IDeckLinkVideoInputFrame *videoFrame, const uint64_t timestamp)
  118. {
  119. if (videoFrame == nullptr)
  120. return;
  121. IDeckLinkVideoFrameAncillaryPackets *packets;
  122. if (videoFrame->QueryInterface(IID_IDeckLinkVideoFrameAncillaryPackets,
  123. (void **)&packets) == S_OK) {
  124. IDeckLinkAncillaryPacketIterator *iterator;
  125. packets->GetPacketIterator(&iterator);
  126. IDeckLinkAncillaryPacket *packet;
  127. iterator->Next(&packet);
  128. if (packet) {
  129. auto did = packet->GetDID();
  130. auto sdid = packet->GetSDID();
  131. // Caption data
  132. if (did == 0x61 & sdid == 0x01) {
  133. this->HandleCaptionPacket(packet, timestamp);
  134. }
  135. packet->Release();
  136. }
  137. iterator->Release();
  138. packets->Release();
  139. }
  140. IDeckLinkVideoConversion *frameConverter =
  141. CreateVideoConversionInstance();
  142. frameConverter->ConvertFrame(videoFrame, convertFrame);
  143. void *bytes;
  144. if (convertFrame->GetBytes(&bytes) != S_OK) {
  145. LOG(LOG_WARNING, "Failed to get video frame data");
  146. return;
  147. }
  148. currentFrame.data[0] = (uint8_t *)bytes;
  149. currentFrame.linesize[0] = (uint32_t)convertFrame->GetRowBytes();
  150. currentFrame.width = (uint32_t)convertFrame->GetWidth();
  151. currentFrame.height = (uint32_t)convertFrame->GetHeight();
  152. currentFrame.timestamp = timestamp;
  153. obs_source_output_video2(
  154. static_cast<DeckLinkInput *>(decklink)->GetSource(),
  155. &currentFrame);
  156. }
  157. void DeckLinkDeviceInstance::HandleCaptionPacket(
  158. IDeckLinkAncillaryPacket *packet, const uint64_t timestamp)
  159. {
  160. auto line = packet->GetLineNumber();
  161. const void *data;
  162. uint32_t size;
  163. packet->GetBytes(bmdAncillaryPacketFormatUInt8, &data, &size);
  164. auto anc = (uint8_t *)data;
  165. struct bitstream_reader reader;
  166. bitstream_reader_init(&reader, anc, size);
  167. auto header1 = bitstream_reader_r8(&reader);
  168. auto header2 = bitstream_reader_r8(&reader);
  169. uint8_t length = bitstream_reader_r8(&reader);
  170. uint8_t frameRate = bitstream_reader_read_bits(&reader, 4);
  171. //reserved
  172. bitstream_reader_read_bits(&reader, 4);
  173. auto cdp_timecode_added = bitstream_reader_read_bits(&reader, 1);
  174. auto cdp_data_block_added = bitstream_reader_read_bits(&reader, 1);
  175. auto cdp_service_info_added = bitstream_reader_read_bits(&reader, 1);
  176. auto cdp_service_info_start = bitstream_reader_read_bits(&reader, 1);
  177. auto cdp_service_info_changed = bitstream_reader_read_bits(&reader, 1);
  178. auto cdp_service_info_end = bitstream_reader_read_bits(&reader, 1);
  179. auto cdp_contains_captions = bitstream_reader_read_bits(&reader, 1);
  180. //reserved
  181. bitstream_reader_read_bits(&reader, 1);
  182. auto cdp_counter = bitstream_reader_r8(&reader);
  183. auto cdp_counter2 = bitstream_reader_r8(&reader);
  184. if (cdp_timecode_added) {
  185. auto timecodeSectionID = bitstream_reader_r8(&reader);
  186. //reserved
  187. bitstream_reader_read_bits(&reader, 2);
  188. bitstream_reader_read_bits(&reader, 2);
  189. bitstream_reader_read_bits(&reader, 4);
  190. // reserved
  191. bitstream_reader_read_bits(&reader, 1);
  192. bitstream_reader_read_bits(&reader, 3);
  193. bitstream_reader_read_bits(&reader, 4);
  194. bitstream_reader_read_bits(&reader, 1);
  195. bitstream_reader_read_bits(&reader, 3);
  196. bitstream_reader_read_bits(&reader, 4);
  197. bitstream_reader_read_bits(&reader, 1);
  198. bitstream_reader_read_bits(&reader, 1);
  199. bitstream_reader_read_bits(&reader, 3);
  200. bitstream_reader_read_bits(&reader, 4);
  201. }
  202. if (cdp_contains_captions) {
  203. auto cdp_data_section = bitstream_reader_r8(&reader);
  204. auto process_em_data_flag =
  205. bitstream_reader_read_bits(&reader, 1);
  206. auto process_cc_data_flag =
  207. bitstream_reader_read_bits(&reader, 1);
  208. auto additional_data_flag =
  209. bitstream_reader_read_bits(&reader, 1);
  210. auto cc_count = bitstream_reader_read_bits(&reader, 5);
  211. auto *outData =
  212. (uint8_t *)bzalloc(sizeof(uint8_t) * cc_count * 3);
  213. memcpy(outData, anc + reader.pos, cc_count * 3);
  214. currentCaptions.data = outData;
  215. currentCaptions.timestamp = timestamp;
  216. currentCaptions.packets = cc_count;
  217. obs_source_output_cea708(
  218. static_cast<DeckLinkInput *>(decklink)->GetSource(),
  219. &currentCaptions);
  220. bfree(outData);
  221. }
  222. }
  223. void DeckLinkDeviceInstance::FinalizeStream()
  224. {
  225. input->SetCallback(nullptr);
  226. input->DisableVideoInput();
  227. if (channelFormat != SPEAKERS_UNKNOWN)
  228. input->DisableAudioInput();
  229. if (audioRepacker != nullptr) {
  230. delete audioRepacker;
  231. audioRepacker = nullptr;
  232. }
  233. mode = nullptr;
  234. }
  235. //#define LOG_SETUP_VIDEO_FORMAT 1
  236. void DeckLinkDeviceInstance::SetupVideoFormat(DeckLinkDeviceMode *mode_)
  237. {
  238. if (mode_ == nullptr)
  239. return;
  240. currentFrame.format = ConvertPixelFormat(pixelFormat);
  241. colorSpace = static_cast<DeckLinkInput *>(decklink)->GetColorSpace();
  242. if (colorSpace == VIDEO_CS_DEFAULT) {
  243. const BMDDisplayModeFlags flags = mode_->GetDisplayModeFlags();
  244. if (flags & bmdDisplayModeColorspaceRec709)
  245. activeColorSpace = VIDEO_CS_709;
  246. else if (flags & bmdDisplayModeColorspaceRec601)
  247. activeColorSpace = VIDEO_CS_601;
  248. else
  249. activeColorSpace = VIDEO_CS_DEFAULT;
  250. } else {
  251. activeColorSpace = colorSpace;
  252. }
  253. colorRange = static_cast<DeckLinkInput *>(decklink)->GetColorRange();
  254. currentFrame.range = colorRange;
  255. video_format_get_parameters(activeColorSpace, colorRange,
  256. currentFrame.color_matrix,
  257. currentFrame.color_range_min,
  258. currentFrame.color_range_max);
  259. if (convertFrame) {
  260. delete convertFrame;
  261. }
  262. convertFrame = new OBSVideoFrame(mode_->GetWidth(), mode_->GetHeight());
  263. #ifdef LOG_SETUP_VIDEO_FORMAT
  264. LOG(LOG_INFO, "Setup video format: %s, %s, %s",
  265. pixelFormat == bmdFormat8BitYUV ? "YUV" : "RGB",
  266. activeColorSpace == VIDEO_CS_601 ? "BT.601" : "BT.709",
  267. colorRange == VIDEO_RANGE_FULL ? "full" : "limited");
  268. #endif
  269. }
  270. bool DeckLinkDeviceInstance::StartCapture(DeckLinkDeviceMode *mode_,
  271. BMDVideoConnection bmdVideoConnection,
  272. BMDAudioConnection bmdAudioConnection)
  273. {
  274. if (mode != nullptr)
  275. return false;
  276. if (mode_ == nullptr)
  277. return false;
  278. LOG(LOG_INFO, "Starting capture...");
  279. if (!device->GetInput(&input))
  280. return false;
  281. IDeckLinkConfiguration *deckLinkConfiguration = NULL;
  282. HRESULT result = input->QueryInterface(IID_IDeckLinkConfiguration,
  283. (void **)&deckLinkConfiguration);
  284. if (result != S_OK) {
  285. LOG(LOG_ERROR,
  286. "Could not obtain the IDeckLinkConfiguration interface: %08x\n",
  287. result);
  288. } else {
  289. if (bmdVideoConnection > 0) {
  290. result = deckLinkConfiguration->SetInt(
  291. bmdDeckLinkConfigVideoInputConnection,
  292. bmdVideoConnection);
  293. if (result != S_OK) {
  294. LOG(LOG_ERROR,
  295. "Couldn't set input video port to %d\n\n",
  296. bmdVideoConnection);
  297. }
  298. }
  299. if (bmdAudioConnection > 0) {
  300. result = deckLinkConfiguration->SetInt(
  301. bmdDeckLinkConfigAudioInputConnection,
  302. bmdAudioConnection);
  303. if (result != S_OK) {
  304. LOG(LOG_ERROR,
  305. "Couldn't set input audio port to %d\n\n",
  306. bmdVideoConnection);
  307. }
  308. }
  309. }
  310. videoConnection = bmdVideoConnection;
  311. audioConnection = bmdAudioConnection;
  312. BMDVideoInputFlags flags;
  313. bool isauto = mode_->GetName() == "Auto";
  314. if (isauto) {
  315. displayMode = bmdModeNTSC;
  316. pixelFormat = bmdFormat10BitYUV;
  317. flags = bmdVideoInputEnableFormatDetection;
  318. } else {
  319. displayMode = mode_->GetDisplayMode();
  320. pixelFormat =
  321. static_cast<DeckLinkInput *>(decklink)->GetPixelFormat();
  322. flags = bmdVideoInputFlagDefault;
  323. }
  324. const HRESULT videoResult =
  325. input->EnableVideoInput(displayMode, pixelFormat, flags);
  326. if (videoResult != S_OK) {
  327. LOG(LOG_ERROR, "Failed to enable video input");
  328. return false;
  329. }
  330. SetupVideoFormat(mode_);
  331. channelFormat =
  332. static_cast<DeckLinkInput *>(decklink)->GetChannelFormat();
  333. currentPacket.speakers = channelFormat;
  334. swap = static_cast<DeckLinkInput *>(decklink)->swap;
  335. int maxdevicechannel = device->GetMaxChannel();
  336. if (channelFormat != SPEAKERS_UNKNOWN) {
  337. const int channel = ConvertChannelFormat(channelFormat);
  338. const HRESULT audioResult = input->EnableAudioInput(
  339. bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger,
  340. channel);
  341. if (audioResult != S_OK)
  342. LOG(LOG_WARNING,
  343. "Failed to enable audio input; continuing...");
  344. if (channelFormat != SPEAKERS_UNKNOWN &&
  345. channelFormat != SPEAKERS_MONO &&
  346. channelFormat != SPEAKERS_STEREO &&
  347. (channelFormat != SPEAKERS_7POINT1 || swap) &&
  348. maxdevicechannel >= 8) {
  349. const audio_repack_mode_t repack_mode =
  350. ConvertRepackFormat(channelFormat, swap);
  351. audioRepacker = new AudioRepacker(repack_mode);
  352. }
  353. }
  354. if (input->SetCallback(this) != S_OK) {
  355. LOG(LOG_ERROR, "Failed to set callback");
  356. FinalizeStream();
  357. return false;
  358. }
  359. if (input->StartStreams() != S_OK) {
  360. LOG(LOG_ERROR, "Failed to start streams");
  361. FinalizeStream();
  362. return false;
  363. }
  364. mode = mode_;
  365. return true;
  366. }
  367. bool DeckLinkDeviceInstance::StopCapture(void)
  368. {
  369. if (mode == nullptr || input == nullptr)
  370. return false;
  371. LOG(LOG_INFO, "Stopping capture of '%s'...",
  372. GetDevice()->GetDisplayName().c_str());
  373. input->StopStreams();
  374. FinalizeStream();
  375. return true;
  376. }
  377. bool DeckLinkDeviceInstance::StartOutput(DeckLinkDeviceMode *mode_)
  378. {
  379. if (mode != nullptr)
  380. return false;
  381. if (mode_ == nullptr)
  382. return false;
  383. LOG(LOG_INFO, "Starting output...");
  384. if (!device->GetOutput(&output))
  385. return false;
  386. const HRESULT videoResult = output->EnableVideoOutput(
  387. mode_->GetDisplayMode(), bmdVideoOutputFlagDefault);
  388. if (videoResult != S_OK) {
  389. LOG(LOG_ERROR, "Failed to enable video output");
  390. return false;
  391. }
  392. const HRESULT audioResult = output->EnableAudioOutput(
  393. bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger, 2,
  394. bmdAudioOutputStreamTimestamped);
  395. if (audioResult != S_OK) {
  396. LOG(LOG_ERROR, "Failed to enable audio output");
  397. return false;
  398. }
  399. mode = mode_;
  400. int keyerMode = device->GetKeyerMode();
  401. IDeckLinkKeyer *deckLinkKeyer = nullptr;
  402. if (device->GetKeyer(&deckLinkKeyer)) {
  403. if (keyerMode) {
  404. deckLinkKeyer->Enable(keyerMode == 1);
  405. deckLinkKeyer->SetLevel(255);
  406. } else {
  407. deckLinkKeyer->Disable();
  408. }
  409. }
  410. auto decklinkOutput = dynamic_cast<DeckLinkOutput *>(decklink);
  411. if (decklinkOutput == nullptr)
  412. return false;
  413. int rowBytes = decklinkOutput->GetWidth() * 2;
  414. if (decklinkOutput->keyerMode != 0) {
  415. rowBytes = decklinkOutput->GetWidth() * 4;
  416. }
  417. BMDPixelFormat pixelFormat = bmdFormat8BitYUV;
  418. if (keyerMode != 0) {
  419. pixelFormat = bmdFormat8BitBGRA;
  420. }
  421. HRESULT result;
  422. result = output->CreateVideoFrame(decklinkOutput->GetWidth(),
  423. decklinkOutput->GetHeight(), rowBytes,
  424. pixelFormat, bmdFrameFlagDefault,
  425. &decklinkOutputFrame);
  426. if (result != S_OK) {
  427. blog(LOG_ERROR, "failed to make frame 0x%X", result);
  428. return false;
  429. }
  430. return true;
  431. }
  432. bool DeckLinkDeviceInstance::StopOutput()
  433. {
  434. if (mode == nullptr || output == nullptr)
  435. return false;
  436. LOG(LOG_INFO, "Stopping output of '%s'...",
  437. GetDevice()->GetDisplayName().c_str());
  438. output->DisableVideoOutput();
  439. output->DisableAudioOutput();
  440. if (decklinkOutputFrame != nullptr) {
  441. decklinkOutputFrame->Release();
  442. decklinkOutputFrame = nullptr;
  443. }
  444. return true;
  445. }
  446. void DeckLinkDeviceInstance::DisplayVideoFrame(video_data *frame)
  447. {
  448. auto decklinkOutput = dynamic_cast<DeckLinkOutput *>(decklink);
  449. if (decklinkOutput == nullptr)
  450. return;
  451. uint8_t *destData;
  452. decklinkOutputFrame->GetBytes((void **)&destData);
  453. uint8_t *outData = frame->data[0];
  454. int rowBytes = decklinkOutput->GetWidth() * 2;
  455. if (device->GetKeyerMode()) {
  456. rowBytes = decklinkOutput->GetWidth() * 4;
  457. }
  458. std::copy(outData, outData + (decklinkOutput->GetHeight() * rowBytes),
  459. destData);
  460. output->DisplayVideoFrameSync(decklinkOutputFrame);
  461. }
  462. void DeckLinkDeviceInstance::WriteAudio(audio_data *frames)
  463. {
  464. uint32_t sampleFramesWritten;
  465. output->WriteAudioSamplesSync(frames->data[0], frames->frames,
  466. &sampleFramesWritten);
  467. }
  468. #define TIME_BASE 1000000000
  469. HRESULT STDMETHODCALLTYPE DeckLinkDeviceInstance::VideoInputFrameArrived(
  470. IDeckLinkVideoInputFrame *videoFrame,
  471. IDeckLinkAudioInputPacket *audioPacket)
  472. {
  473. BMDTimeValue videoTS = 0;
  474. BMDTimeValue videoDur = 0;
  475. BMDTimeValue audioTS = 0;
  476. if (videoFrame) {
  477. videoFrame->GetStreamTime(&videoTS, &videoDur, TIME_BASE);
  478. lastVideoTS = (uint64_t)videoTS;
  479. }
  480. if (audioPacket) {
  481. BMDTimeValue newAudioTS = 0;
  482. int64_t diff;
  483. audioPacket->GetPacketTime(&newAudioTS, TIME_BASE);
  484. audioTS = newAudioTS + audioOffset;
  485. diff = (int64_t)audioTS - (int64_t)nextAudioTS;
  486. if (diff > 10000000LL) {
  487. audioOffset -= diff;
  488. audioTS = newAudioTS + audioOffset;
  489. } else if (diff < -1000000) {
  490. audioOffset = 0;
  491. audioTS = newAudioTS;
  492. }
  493. }
  494. if (videoFrame && videoTS >= 0)
  495. HandleVideoFrame(videoFrame, (uint64_t)videoTS);
  496. if (audioPacket && audioTS >= 0)
  497. HandleAudioPacket(audioPacket, (uint64_t)audioTS);
  498. return S_OK;
  499. }
  500. HRESULT STDMETHODCALLTYPE DeckLinkDeviceInstance::VideoInputFormatChanged(
  501. BMDVideoInputFormatChangedEvents events, IDeckLinkDisplayMode *newMode,
  502. BMDDetectedVideoInputFormatFlags detectedSignalFlags)
  503. {
  504. if (events & bmdVideoInputColorspaceChanged) {
  505. switch (detectedSignalFlags) {
  506. case bmdDetectedVideoInputRGB444:
  507. pixelFormat = bmdFormat8BitBGRA;
  508. break;
  509. default:
  510. case bmdDetectedVideoInputYCbCr422:
  511. pixelFormat = bmdFormat10BitYUV;
  512. break;
  513. }
  514. }
  515. if (events & bmdVideoInputDisplayModeChanged) {
  516. input->PauseStreams();
  517. mode->SetMode(newMode);
  518. displayMode = mode->GetDisplayMode();
  519. const HRESULT videoResult = input->EnableVideoInput(
  520. displayMode, pixelFormat,
  521. bmdVideoInputEnableFormatDetection);
  522. if (videoResult != S_OK) {
  523. LOG(LOG_ERROR, "Failed to enable video input");
  524. input->StopStreams();
  525. FinalizeStream();
  526. return E_FAIL;
  527. }
  528. SetupVideoFormat(mode);
  529. input->FlushStreams();
  530. input->StartStreams();
  531. }
  532. return S_OK;
  533. }
  534. ULONG STDMETHODCALLTYPE DeckLinkDeviceInstance::AddRef(void)
  535. {
  536. return os_atomic_inc_long(&refCount);
  537. }
  538. HRESULT STDMETHODCALLTYPE DeckLinkDeviceInstance::QueryInterface(REFIID iid,
  539. LPVOID *ppv)
  540. {
  541. HRESULT result = E_NOINTERFACE;
  542. *ppv = nullptr;
  543. CFUUIDBytes unknown = CFUUIDGetUUIDBytes(IUnknownUUID);
  544. if (memcmp(&iid, &unknown, sizeof(REFIID)) == 0) {
  545. *ppv = this;
  546. AddRef();
  547. result = S_OK;
  548. } else if (memcmp(&iid, &IID_IDeckLinkNotificationCallback,
  549. sizeof(REFIID)) == 0) {
  550. *ppv = (IDeckLinkNotificationCallback *)this;
  551. AddRef();
  552. result = S_OK;
  553. }
  554. return result;
  555. }
  556. ULONG STDMETHODCALLTYPE DeckLinkDeviceInstance::Release(void)
  557. {
  558. const long newRefCount = os_atomic_dec_long(&refCount);
  559. if (newRefCount == 0) {
  560. delete this;
  561. return 0;
  562. }
  563. return newRefCount;
  564. }