decklink-device-instance.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. #include "decklink-device-instance.hpp"
  2. #include "audio-repack.hpp"
  3. #include <util/platform.h>
  4. #include <util/threading.h>
  5. #include <sstream>
  6. #define LOG(level, message, ...) blog(level, "%s: " message, \
  7. obs_source_get_name(this->decklink->GetSource()), ##__VA_ARGS__)
  8. #define ISSTEREO(flag) ((flag) == SPEAKERS_STEREO)
  9. static inline enum video_format ConvertPixelFormat(BMDPixelFormat format)
  10. {
  11. switch (format) {
  12. case bmdFormat8BitBGRA: return VIDEO_FORMAT_BGRX;
  13. default:
  14. case bmdFormat8BitYUV:;
  15. }
  16. return VIDEO_FORMAT_UYVY;
  17. }
  18. static inline int ConvertChannelFormat(speaker_layout format)
  19. {
  20. switch (format) {
  21. case SPEAKERS_5POINT1:
  22. case SPEAKERS_5POINT1_SURROUND:
  23. case SPEAKERS_7POINT1:
  24. return 8;
  25. default:
  26. case SPEAKERS_STEREO:
  27. return 2;
  28. }
  29. }
  30. static inline audio_repack_mode_t ConvertRepackFormat(speaker_layout format)
  31. {
  32. switch (format) {
  33. case SPEAKERS_5POINT1:
  34. case SPEAKERS_5POINT1_SURROUND:
  35. return repack_mode_8to6ch_swap23;
  36. case SPEAKERS_7POINT1:
  37. return repack_mode_8ch_swap23;
  38. default:
  39. assert(false && "No repack requested");
  40. return (audio_repack_mode_t)-1;
  41. }
  42. }
  43. DeckLinkDeviceInstance::DeckLinkDeviceInstance(DeckLink *decklink_,
  44. DeckLinkDevice *device_) :
  45. currentFrame(), currentPacket(), decklink(decklink_), device(device_)
  46. {
  47. currentPacket.samples_per_sec = 48000;
  48. currentPacket.speakers = SPEAKERS_STEREO;
  49. currentPacket.format = AUDIO_FORMAT_16BIT;
  50. }
  51. DeckLinkDeviceInstance::~DeckLinkDeviceInstance()
  52. {
  53. }
  54. void DeckLinkDeviceInstance::HandleAudioPacket(
  55. IDeckLinkAudioInputPacket *audioPacket,
  56. const uint64_t timestamp)
  57. {
  58. if (audioPacket == nullptr)
  59. return;
  60. void *bytes;
  61. if (audioPacket->GetBytes(&bytes) != S_OK) {
  62. LOG(LOG_WARNING, "Failed to get audio packet data");
  63. return;
  64. }
  65. const uint32_t frameCount = (uint32_t)audioPacket->GetSampleFrameCount();
  66. currentPacket.frames = frameCount;
  67. currentPacket.timestamp = timestamp;
  68. if (!ISSTEREO(channelFormat)) {
  69. if (audioRepacker->repack((uint8_t *)bytes, frameCount) < 0) {
  70. LOG(LOG_ERROR, "Failed to convert audio packet data");
  71. return;
  72. }
  73. currentPacket.data[0] = (*audioRepacker)->packet_buffer;
  74. } else {
  75. currentPacket.data[0] = (uint8_t *)bytes;
  76. }
  77. nextAudioTS = timestamp +
  78. ((uint64_t)frameCount * 1000000000ULL / 48000ULL) + 1;
  79. obs_source_output_audio(decklink->GetSource(), &currentPacket);
  80. }
  81. void DeckLinkDeviceInstance::HandleVideoFrame(
  82. IDeckLinkVideoInputFrame *videoFrame, const uint64_t timestamp)
  83. {
  84. if (videoFrame == nullptr)
  85. return;
  86. void *bytes;
  87. if (videoFrame->GetBytes(&bytes) != S_OK) {
  88. LOG(LOG_WARNING, "Failed to get video frame data");
  89. return;
  90. }
  91. currentFrame.data[0] = (uint8_t *)bytes;
  92. currentFrame.linesize[0] = (uint32_t)videoFrame->GetRowBytes();
  93. currentFrame.width = (uint32_t)videoFrame->GetWidth();
  94. currentFrame.height = (uint32_t)videoFrame->GetHeight();
  95. currentFrame.timestamp = timestamp;
  96. obs_source_output_video(decklink->GetSource(), &currentFrame);
  97. }
  98. void DeckLinkDeviceInstance::FinalizeStream()
  99. {
  100. input->SetCallback(nullptr);
  101. input->DisableVideoInput();
  102. if (channelFormat != SPEAKERS_UNKNOWN)
  103. input->DisableAudioInput();
  104. if (audioRepacker != nullptr)
  105. {
  106. delete audioRepacker;
  107. audioRepacker = nullptr;
  108. }
  109. mode = nullptr;
  110. }
  111. //#define LOG_SETUP_VIDEO_FORMAT 1
  112. void DeckLinkDeviceInstance::SetupVideoFormat(DeckLinkDeviceMode *mode_)
  113. {
  114. if (mode_ == nullptr)
  115. return;
  116. currentFrame.format = ConvertPixelFormat(pixelFormat);
  117. colorSpace = decklink->GetColorSpace();
  118. if (colorSpace == VIDEO_CS_DEFAULT) {
  119. const BMDDisplayModeFlags flags = mode_->GetDisplayModeFlags();
  120. if (flags & bmdDisplayModeColorspaceRec709)
  121. activeColorSpace = VIDEO_CS_709;
  122. else if (flags & bmdDisplayModeColorspaceRec601)
  123. activeColorSpace = VIDEO_CS_601;
  124. else
  125. activeColorSpace = VIDEO_CS_DEFAULT;
  126. } else {
  127. activeColorSpace = colorSpace;
  128. }
  129. colorRange = decklink->GetColorRange();
  130. currentFrame.full_range = colorRange == VIDEO_RANGE_FULL;
  131. video_format_get_parameters(activeColorSpace, colorRange,
  132. currentFrame.color_matrix, currentFrame.color_range_min,
  133. currentFrame.color_range_max);
  134. #ifdef LOG_SETUP_VIDEO_FORMAT
  135. LOG(LOG_INFO, "Setup video format: %s, %s, %s",
  136. pixelFormat == bmdFormat8BitYUV ? "YUV" : "RGB",
  137. activeColorSpace == VIDEO_CS_709 ? "BT.709" : "BT.601",
  138. colorRange == VIDEO_RANGE_FULL ? "full" : "limited");
  139. #endif
  140. }
  141. bool DeckLinkDeviceInstance::StartCapture(DeckLinkDeviceMode *mode_)
  142. {
  143. if (mode != nullptr)
  144. return false;
  145. if (mode_ == nullptr)
  146. return false;
  147. LOG(LOG_INFO, "Starting capture...");
  148. if (!device->GetInput(&input))
  149. return false;
  150. BMDVideoInputFlags flags;
  151. bool isauto = mode_->GetName() == "Auto";
  152. if (isauto) {
  153. displayMode = bmdModeNTSC;
  154. pixelFormat = bmdFormat8BitYUV;
  155. flags = bmdVideoInputEnableFormatDetection;
  156. } else {
  157. displayMode = mode_->GetDisplayMode();
  158. pixelFormat = decklink->GetPixelFormat();
  159. flags = bmdVideoInputFlagDefault;
  160. }
  161. const HRESULT videoResult = input->EnableVideoInput(displayMode,
  162. pixelFormat, flags);
  163. if (videoResult != S_OK) {
  164. LOG(LOG_ERROR, "Failed to enable video input");
  165. return false;
  166. }
  167. SetupVideoFormat(mode_);
  168. channelFormat = decklink->GetChannelFormat();
  169. currentPacket.speakers = channelFormat;
  170. if (channelFormat != SPEAKERS_UNKNOWN) {
  171. const int channel = ConvertChannelFormat(channelFormat);
  172. const HRESULT audioResult = input->EnableAudioInput(
  173. bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger,
  174. channel);
  175. if (audioResult != S_OK)
  176. LOG(LOG_WARNING, "Failed to enable audio input; continuing...");
  177. if (!ISSTEREO(channelFormat)) {
  178. const audio_repack_mode_t repack_mode = ConvertRepackFormat(channelFormat);
  179. audioRepacker = new AudioRepacker(repack_mode);
  180. }
  181. }
  182. if (input->SetCallback(this) != S_OK) {
  183. LOG(LOG_ERROR, "Failed to set callback");
  184. FinalizeStream();
  185. return false;
  186. }
  187. if (input->StartStreams() != S_OK) {
  188. LOG(LOG_ERROR, "Failed to start streams");
  189. FinalizeStream();
  190. return false;
  191. }
  192. mode = mode_;
  193. return true;
  194. }
  195. bool DeckLinkDeviceInstance::StopCapture(void)
  196. {
  197. if (mode == nullptr || input == nullptr)
  198. return false;
  199. LOG(LOG_INFO, "Stopping capture of '%s'...",
  200. GetDevice()->GetDisplayName().c_str());
  201. input->StopStreams();
  202. FinalizeStream();
  203. return true;
  204. }
  205. #define TIME_BASE 1000000000
  206. HRESULT STDMETHODCALLTYPE DeckLinkDeviceInstance::VideoInputFrameArrived(
  207. IDeckLinkVideoInputFrame *videoFrame,
  208. IDeckLinkAudioInputPacket *audioPacket)
  209. {
  210. BMDTimeValue videoTS = 0;
  211. BMDTimeValue videoDur = 0;
  212. BMDTimeValue audioTS = 0;
  213. if (videoFrame) {
  214. videoFrame->GetStreamTime(&videoTS, &videoDur, TIME_BASE);
  215. lastVideoTS = (uint64_t)videoTS;
  216. }
  217. if (audioPacket) {
  218. BMDTimeValue newAudioTS = 0;
  219. int64_t diff;
  220. audioPacket->GetPacketTime(&newAudioTS, TIME_BASE);
  221. audioTS = newAudioTS + audioOffset;
  222. diff = (int64_t)audioTS - (int64_t)nextAudioTS;
  223. if (diff > 10000000LL) {
  224. audioOffset -= diff;
  225. audioTS = newAudioTS + audioOffset;
  226. } else if (diff < -1000000) {
  227. audioOffset = 0;
  228. audioTS = newAudioTS;
  229. }
  230. }
  231. if (videoFrame && videoTS >= 0)
  232. HandleVideoFrame(videoFrame, (uint64_t)videoTS);
  233. if (audioPacket && audioTS >= 0)
  234. HandleAudioPacket(audioPacket, (uint64_t)audioTS);
  235. return S_OK;
  236. }
  237. HRESULT STDMETHODCALLTYPE DeckLinkDeviceInstance::VideoInputFormatChanged(
  238. BMDVideoInputFormatChangedEvents events,
  239. IDeckLinkDisplayMode *newMode,
  240. BMDDetectedVideoInputFormatFlags detectedSignalFlags)
  241. {
  242. input->PauseStreams();
  243. mode->SetMode(newMode);
  244. if (events & bmdVideoInputDisplayModeChanged) {
  245. displayMode = mode->GetDisplayMode();
  246. }
  247. if (events & bmdVideoInputColorspaceChanged) {
  248. switch (detectedSignalFlags) {
  249. case bmdDetectedVideoInputRGB444:
  250. pixelFormat = bmdFormat8BitBGRA;
  251. break;
  252. default:
  253. case bmdDetectedVideoInputYCbCr422:
  254. pixelFormat = bmdFormat8BitYUV;
  255. break;
  256. }
  257. }
  258. const HRESULT videoResult = input->EnableVideoInput(displayMode,
  259. pixelFormat, bmdVideoInputEnableFormatDetection);
  260. if (videoResult != S_OK) {
  261. LOG(LOG_ERROR, "Failed to enable video input");
  262. input->StopStreams();
  263. FinalizeStream();
  264. return E_FAIL;
  265. }
  266. SetupVideoFormat(mode);
  267. input->FlushStreams();
  268. input->StartStreams();
  269. return S_OK;
  270. }
  271. ULONG STDMETHODCALLTYPE DeckLinkDeviceInstance::AddRef(void)
  272. {
  273. return os_atomic_inc_long(&refCount);
  274. }
  275. HRESULT STDMETHODCALLTYPE DeckLinkDeviceInstance::QueryInterface(REFIID iid,
  276. LPVOID *ppv)
  277. {
  278. HRESULT result = E_NOINTERFACE;
  279. *ppv = nullptr;
  280. CFUUIDBytes unknown = CFUUIDGetUUIDBytes(IUnknownUUID);
  281. if (memcmp(&iid, &unknown, sizeof(REFIID)) == 0) {
  282. *ppv = this;
  283. AddRef();
  284. result = S_OK;
  285. } else if (memcmp(&iid, &IID_IDeckLinkNotificationCallback,
  286. sizeof(REFIID)) == 0) {
  287. *ppv = (IDeckLinkNotificationCallback *)this;
  288. AddRef();
  289. result = S_OK;
  290. }
  291. return result;
  292. }
  293. ULONG STDMETHODCALLTYPE DeckLinkDeviceInstance::Release(void)
  294. {
  295. const long newRefCount = os_atomic_dec_long(&refCount);
  296. if (newRefCount == 0) {
  297. delete this;
  298. return 0;
  299. }
  300. return newRefCount;
  301. }