decklink-device-instance.cpp 18 KB

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