decklink-device-instance.cpp 18 KB

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