decklink-device-instance.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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. const void *data;
  161. uint32_t size;
  162. packet->GetBytes(bmdAncillaryPacketFormatUInt8, &data, &size);
  163. auto anc = (uint8_t *)data;
  164. struct bitstream_reader reader;
  165. bitstream_reader_init(&reader, anc, size);
  166. // header1
  167. bitstream_reader_r8(&reader);
  168. // header2
  169. bitstream_reader_r8(&reader);
  170. // length
  171. bitstream_reader_r8(&reader);
  172. // frameRate
  173. bitstream_reader_read_bits(&reader, 4);
  174. //reserved
  175. bitstream_reader_read_bits(&reader, 4);
  176. auto cdp_timecode_added = bitstream_reader_read_bits(&reader, 1);
  177. // cdp_data_block_added
  178. bitstream_reader_read_bits(&reader, 1);
  179. // cdp_service_info_added
  180. bitstream_reader_read_bits(&reader, 1);
  181. // cdp_service_info_start
  182. bitstream_reader_read_bits(&reader, 1);
  183. // cdp_service_info_changed
  184. bitstream_reader_read_bits(&reader, 1);
  185. // cdp_service_info_end
  186. bitstream_reader_read_bits(&reader, 1);
  187. auto cdp_contains_captions = bitstream_reader_read_bits(&reader, 1);
  188. //reserved
  189. bitstream_reader_read_bits(&reader, 1);
  190. // cdp_counter
  191. bitstream_reader_r8(&reader);
  192. // cdp_counter2
  193. bitstream_reader_r8(&reader);
  194. if (cdp_timecode_added) {
  195. // timecodeSectionID
  196. bitstream_reader_r8(&reader);
  197. //reserved
  198. bitstream_reader_read_bits(&reader, 2);
  199. bitstream_reader_read_bits(&reader, 2);
  200. bitstream_reader_read_bits(&reader, 4);
  201. // reserved
  202. bitstream_reader_read_bits(&reader, 1);
  203. bitstream_reader_read_bits(&reader, 3);
  204. bitstream_reader_read_bits(&reader, 4);
  205. bitstream_reader_read_bits(&reader, 1);
  206. bitstream_reader_read_bits(&reader, 3);
  207. bitstream_reader_read_bits(&reader, 4);
  208. bitstream_reader_read_bits(&reader, 1);
  209. bitstream_reader_read_bits(&reader, 1);
  210. bitstream_reader_read_bits(&reader, 3);
  211. bitstream_reader_read_bits(&reader, 4);
  212. }
  213. if (cdp_contains_captions) {
  214. // cdp_data_section
  215. bitstream_reader_r8(&reader);
  216. //process_em_data_flag
  217. bitstream_reader_read_bits(&reader, 1);
  218. // process_cc_data_flag
  219. bitstream_reader_read_bits(&reader, 1);
  220. //additional_data_flag
  221. bitstream_reader_read_bits(&reader, 1);
  222. auto cc_count = bitstream_reader_read_bits(&reader, 5);
  223. auto *outData =
  224. (uint8_t *)bzalloc(sizeof(uint8_t) * cc_count * 3);
  225. memcpy(outData, anc + reader.pos, cc_count * 3);
  226. currentCaptions.data = outData;
  227. currentCaptions.timestamp = timestamp;
  228. currentCaptions.packets = cc_count;
  229. obs_source_output_cea708(
  230. static_cast<DeckLinkInput *>(decklink)->GetSource(),
  231. &currentCaptions);
  232. bfree(outData);
  233. }
  234. }
  235. void DeckLinkDeviceInstance::FinalizeStream()
  236. {
  237. input->SetCallback(nullptr);
  238. input->DisableVideoInput();
  239. if (channelFormat != SPEAKERS_UNKNOWN)
  240. input->DisableAudioInput();
  241. if (audioRepacker != nullptr) {
  242. delete audioRepacker;
  243. audioRepacker = nullptr;
  244. }
  245. mode = nullptr;
  246. }
  247. //#define LOG_SETUP_VIDEO_FORMAT 1
  248. void DeckLinkDeviceInstance::SetupVideoFormat(DeckLinkDeviceMode *mode_)
  249. {
  250. if (mode_ == nullptr)
  251. return;
  252. currentFrame.format = ConvertPixelFormat(pixelFormat);
  253. colorSpace = static_cast<DeckLinkInput *>(decklink)->GetColorSpace();
  254. if (colorSpace == VIDEO_CS_DEFAULT) {
  255. const BMDDisplayModeFlags flags = mode_->GetDisplayModeFlags();
  256. if (flags & bmdDisplayModeColorspaceRec709)
  257. activeColorSpace = VIDEO_CS_709;
  258. else if (flags & bmdDisplayModeColorspaceRec601)
  259. activeColorSpace = VIDEO_CS_601;
  260. else
  261. activeColorSpace = VIDEO_CS_DEFAULT;
  262. } else {
  263. activeColorSpace = colorSpace;
  264. }
  265. colorRange = static_cast<DeckLinkInput *>(decklink)->GetColorRange();
  266. currentFrame.range = colorRange;
  267. video_format_get_parameters(activeColorSpace, colorRange,
  268. currentFrame.color_matrix,
  269. currentFrame.color_range_min,
  270. currentFrame.color_range_max);
  271. if (convertFrame) {
  272. delete convertFrame;
  273. }
  274. convertFrame = new OBSVideoFrame(mode_->GetWidth(), mode_->GetHeight());
  275. #ifdef LOG_SETUP_VIDEO_FORMAT
  276. LOG(LOG_INFO, "Setup video format: %s, %s, %s",
  277. pixelFormat == bmdFormat8BitYUV ? "YUV" : "RGB",
  278. activeColorSpace == VIDEO_CS_601 ? "BT.601" : "BT.709",
  279. colorRange == VIDEO_RANGE_FULL ? "full" : "limited");
  280. #endif
  281. }
  282. bool DeckLinkDeviceInstance::StartCapture(DeckLinkDeviceMode *mode_,
  283. BMDVideoConnection bmdVideoConnection,
  284. BMDAudioConnection bmdAudioConnection)
  285. {
  286. if (mode != nullptr)
  287. return false;
  288. if (mode_ == nullptr)
  289. return false;
  290. LOG(LOG_INFO, "Starting capture...");
  291. if (!device->GetInput(&input))
  292. return false;
  293. IDeckLinkConfiguration *deckLinkConfiguration = NULL;
  294. HRESULT result = input->QueryInterface(IID_IDeckLinkConfiguration,
  295. (void **)&deckLinkConfiguration);
  296. if (result != S_OK) {
  297. LOG(LOG_ERROR,
  298. "Could not obtain the IDeckLinkConfiguration interface: %08x\n",
  299. result);
  300. } else {
  301. if (bmdVideoConnection > 0) {
  302. result = deckLinkConfiguration->SetInt(
  303. bmdDeckLinkConfigVideoInputConnection,
  304. bmdVideoConnection);
  305. if (result != S_OK) {
  306. LOG(LOG_ERROR,
  307. "Couldn't set input video port to %d\n\n",
  308. bmdVideoConnection);
  309. }
  310. }
  311. if (bmdAudioConnection > 0) {
  312. result = deckLinkConfiguration->SetInt(
  313. bmdDeckLinkConfigAudioInputConnection,
  314. bmdAudioConnection);
  315. if (result != S_OK) {
  316. LOG(LOG_ERROR,
  317. "Couldn't set input audio port to %d\n\n",
  318. bmdVideoConnection);
  319. }
  320. }
  321. }
  322. videoConnection = bmdVideoConnection;
  323. audioConnection = bmdAudioConnection;
  324. BMDVideoInputFlags flags;
  325. bool isauto = mode_->GetName() == "Auto";
  326. if (isauto) {
  327. displayMode = bmdModeNTSC;
  328. pixelFormat = bmdFormat10BitYUV;
  329. flags = bmdVideoInputEnableFormatDetection;
  330. } else {
  331. displayMode = mode_->GetDisplayMode();
  332. pixelFormat =
  333. static_cast<DeckLinkInput *>(decklink)->GetPixelFormat();
  334. flags = bmdVideoInputFlagDefault;
  335. }
  336. const HRESULT videoResult =
  337. input->EnableVideoInput(displayMode, pixelFormat, flags);
  338. if (videoResult != S_OK) {
  339. LOG(LOG_ERROR, "Failed to enable video input");
  340. return false;
  341. }
  342. SetupVideoFormat(mode_);
  343. channelFormat =
  344. static_cast<DeckLinkInput *>(decklink)->GetChannelFormat();
  345. currentPacket.speakers = channelFormat;
  346. swap = static_cast<DeckLinkInput *>(decklink)->swap;
  347. int maxdevicechannel = device->GetMaxChannel();
  348. if (channelFormat != SPEAKERS_UNKNOWN) {
  349. const int channel = ConvertChannelFormat(channelFormat);
  350. const HRESULT audioResult = input->EnableAudioInput(
  351. bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger,
  352. channel);
  353. if (audioResult != S_OK)
  354. LOG(LOG_WARNING,
  355. "Failed to enable audio input; continuing...");
  356. if (channelFormat != SPEAKERS_UNKNOWN &&
  357. channelFormat != SPEAKERS_MONO &&
  358. channelFormat != SPEAKERS_STEREO &&
  359. (channelFormat != SPEAKERS_7POINT1 || swap) &&
  360. maxdevicechannel >= 8) {
  361. const audio_repack_mode_t repack_mode =
  362. ConvertRepackFormat(channelFormat, swap);
  363. audioRepacker = new AudioRepacker(repack_mode);
  364. }
  365. }
  366. if (input->SetCallback(this) != S_OK) {
  367. LOG(LOG_ERROR, "Failed to set callback");
  368. FinalizeStream();
  369. return false;
  370. }
  371. if (input->StartStreams() != S_OK) {
  372. LOG(LOG_ERROR, "Failed to start streams");
  373. FinalizeStream();
  374. return false;
  375. }
  376. mode = mode_;
  377. return true;
  378. }
  379. bool DeckLinkDeviceInstance::StopCapture(void)
  380. {
  381. if (mode == nullptr || input == nullptr)
  382. return false;
  383. LOG(LOG_INFO, "Stopping capture of '%s'...",
  384. GetDevice()->GetDisplayName().c_str());
  385. input->StopStreams();
  386. FinalizeStream();
  387. return true;
  388. }
  389. bool DeckLinkDeviceInstance::StartOutput(DeckLinkDeviceMode *mode_)
  390. {
  391. if (mode != nullptr)
  392. return false;
  393. if (mode_ == nullptr)
  394. return false;
  395. LOG(LOG_INFO, "Starting output...");
  396. if (!device->GetOutput(&output))
  397. return false;
  398. const HRESULT videoResult = output->EnableVideoOutput(
  399. mode_->GetDisplayMode(), bmdVideoOutputFlagDefault);
  400. if (videoResult != S_OK) {
  401. LOG(LOG_ERROR, "Failed to enable video output");
  402. return false;
  403. }
  404. const HRESULT audioResult = output->EnableAudioOutput(
  405. bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger, 2,
  406. bmdAudioOutputStreamTimestamped);
  407. if (audioResult != S_OK) {
  408. LOG(LOG_ERROR, "Failed to enable audio output");
  409. return false;
  410. }
  411. mode = mode_;
  412. int keyerMode = device->GetKeyerMode();
  413. IDeckLinkKeyer *deckLinkKeyer = nullptr;
  414. if (device->GetKeyer(&deckLinkKeyer)) {
  415. if (keyerMode) {
  416. deckLinkKeyer->Enable(keyerMode == 1);
  417. deckLinkKeyer->SetLevel(255);
  418. } else {
  419. deckLinkKeyer->Disable();
  420. }
  421. }
  422. auto decklinkOutput = dynamic_cast<DeckLinkOutput *>(decklink);
  423. if (decklinkOutput == nullptr)
  424. return false;
  425. int rowBytes = decklinkOutput->GetWidth() * 2;
  426. if (decklinkOutput->keyerMode != 0) {
  427. rowBytes = decklinkOutput->GetWidth() * 4;
  428. }
  429. BMDPixelFormat pixelFormat = bmdFormat8BitYUV;
  430. if (keyerMode != 0) {
  431. pixelFormat = bmdFormat8BitBGRA;
  432. }
  433. HRESULT result;
  434. result = output->CreateVideoFrame(decklinkOutput->GetWidth(),
  435. decklinkOutput->GetHeight(), rowBytes,
  436. pixelFormat, bmdFrameFlagDefault,
  437. &decklinkOutputFrame);
  438. if (result != S_OK) {
  439. blog(LOG_ERROR, "failed to make frame 0x%X", result);
  440. return false;
  441. }
  442. return true;
  443. }
  444. bool DeckLinkDeviceInstance::StopOutput()
  445. {
  446. if (mode == nullptr || output == nullptr)
  447. return false;
  448. LOG(LOG_INFO, "Stopping output of '%s'...",
  449. GetDevice()->GetDisplayName().c_str());
  450. output->DisableVideoOutput();
  451. output->DisableAudioOutput();
  452. if (decklinkOutputFrame != nullptr) {
  453. decklinkOutputFrame->Release();
  454. decklinkOutputFrame = nullptr;
  455. }
  456. return true;
  457. }
  458. void DeckLinkDeviceInstance::DisplayVideoFrame(video_data *frame)
  459. {
  460. auto decklinkOutput = dynamic_cast<DeckLinkOutput *>(decklink);
  461. if (decklinkOutput == nullptr)
  462. return;
  463. uint8_t *destData;
  464. decklinkOutputFrame->GetBytes((void **)&destData);
  465. uint8_t *outData = frame->data[0];
  466. int rowBytes = decklinkOutput->GetWidth() * 2;
  467. if (device->GetKeyerMode()) {
  468. rowBytes = decklinkOutput->GetWidth() * 4;
  469. }
  470. std::copy(outData, outData + (decklinkOutput->GetHeight() * rowBytes),
  471. destData);
  472. output->DisplayVideoFrameSync(decklinkOutputFrame);
  473. }
  474. void DeckLinkDeviceInstance::WriteAudio(audio_data *frames)
  475. {
  476. uint32_t sampleFramesWritten;
  477. output->WriteAudioSamplesSync(frames->data[0], frames->frames,
  478. &sampleFramesWritten);
  479. }
  480. #define TIME_BASE 1000000000
  481. HRESULT STDMETHODCALLTYPE DeckLinkDeviceInstance::VideoInputFrameArrived(
  482. IDeckLinkVideoInputFrame *videoFrame,
  483. IDeckLinkAudioInputPacket *audioPacket)
  484. {
  485. BMDTimeValue videoTS = 0;
  486. BMDTimeValue videoDur = 0;
  487. BMDTimeValue audioTS = 0;
  488. if (videoFrame) {
  489. videoFrame->GetStreamTime(&videoTS, &videoDur, TIME_BASE);
  490. lastVideoTS = (uint64_t)videoTS;
  491. }
  492. if (audioPacket) {
  493. BMDTimeValue newAudioTS = 0;
  494. int64_t diff;
  495. audioPacket->GetPacketTime(&newAudioTS, TIME_BASE);
  496. audioTS = newAudioTS + audioOffset;
  497. diff = (int64_t)audioTS - (int64_t)nextAudioTS;
  498. if (diff > 10000000LL) {
  499. audioOffset -= diff;
  500. audioTS = newAudioTS + audioOffset;
  501. } else if (diff < -1000000) {
  502. audioOffset = 0;
  503. audioTS = newAudioTS;
  504. }
  505. }
  506. if (videoFrame && videoTS >= 0)
  507. HandleVideoFrame(videoFrame, (uint64_t)videoTS);
  508. if (audioPacket && audioTS >= 0)
  509. HandleAudioPacket(audioPacket, (uint64_t)audioTS);
  510. return S_OK;
  511. }
  512. HRESULT STDMETHODCALLTYPE DeckLinkDeviceInstance::VideoInputFormatChanged(
  513. BMDVideoInputFormatChangedEvents events, IDeckLinkDisplayMode *newMode,
  514. BMDDetectedVideoInputFormatFlags detectedSignalFlags)
  515. {
  516. if (events & bmdVideoInputColorspaceChanged) {
  517. switch (detectedSignalFlags) {
  518. case bmdDetectedVideoInputRGB444:
  519. pixelFormat = bmdFormat8BitBGRA;
  520. break;
  521. default:
  522. case bmdDetectedVideoInputYCbCr422:
  523. pixelFormat = bmdFormat10BitYUV;
  524. break;
  525. }
  526. }
  527. if (events & bmdVideoInputDisplayModeChanged) {
  528. input->PauseStreams();
  529. mode->SetMode(newMode);
  530. displayMode = mode->GetDisplayMode();
  531. const HRESULT videoResult = input->EnableVideoInput(
  532. displayMode, pixelFormat,
  533. bmdVideoInputEnableFormatDetection);
  534. if (videoResult != S_OK) {
  535. LOG(LOG_ERROR, "Failed to enable video input");
  536. input->StopStreams();
  537. FinalizeStream();
  538. return E_FAIL;
  539. }
  540. SetupVideoFormat(mode);
  541. input->FlushStreams();
  542. input->StartStreams();
  543. }
  544. return S_OK;
  545. }
  546. ULONG STDMETHODCALLTYPE DeckLinkDeviceInstance::AddRef(void)
  547. {
  548. return os_atomic_inc_long(&refCount);
  549. }
  550. HRESULT STDMETHODCALLTYPE DeckLinkDeviceInstance::QueryInterface(REFIID iid,
  551. LPVOID *ppv)
  552. {
  553. HRESULT result = E_NOINTERFACE;
  554. *ppv = nullptr;
  555. CFUUIDBytes unknown = CFUUIDGetUUIDBytes(IUnknownUUID);
  556. if (memcmp(&iid, &unknown, sizeof(REFIID)) == 0) {
  557. *ppv = this;
  558. AddRef();
  559. result = S_OK;
  560. } else if (memcmp(&iid, &IID_IDeckLinkNotificationCallback,
  561. sizeof(REFIID)) == 0) {
  562. *ppv = (IDeckLinkNotificationCallback *)this;
  563. AddRef();
  564. result = S_OK;
  565. }
  566. return result;
  567. }
  568. ULONG STDMETHODCALLTYPE DeckLinkDeviceInstance::Release(void)
  569. {
  570. const long newRefCount = os_atomic_dec_long(&refCount);
  571. if (newRefCount == 0) {
  572. delete this;
  573. return 0;
  574. }
  575. return newRefCount;
  576. }