decklink-device-instance.cpp 18 KB

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