decklink_common.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Blackmagic DeckLink output
  3. * Copyright (c) 2013-2014 Ramiro Polla, Luca Barbato, Deti Fliegl
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <DeckLinkAPI.h>
  22. #ifdef _WIN32
  23. #include <DeckLinkAPI_i.c>
  24. #else
  25. #include <DeckLinkAPIDispatch.cpp>
  26. #endif
  27. #include <pthread.h>
  28. #include <semaphore.h>
  29. extern "C" {
  30. #include "libavformat/avformat.h"
  31. #include "libavformat/internal.h"
  32. #include "libavutil/imgutils.h"
  33. }
  34. #include "decklink_common.h"
  35. #ifdef _WIN32
  36. IDeckLinkIterator *CreateDeckLinkIteratorInstance(void)
  37. {
  38. IDeckLinkIterator *iter;
  39. if (CoInitialize(NULL) < 0) {
  40. av_log(NULL, AV_LOG_ERROR, "COM initialization failed.\n");
  41. return NULL;
  42. }
  43. if (CoCreateInstance(CLSID_CDeckLinkIterator, NULL, CLSCTX_ALL,
  44. IID_IDeckLinkIterator, (void**) &iter) != S_OK) {
  45. av_log(NULL, AV_LOG_ERROR, "DeckLink drivers not installed.\n");
  46. return NULL;
  47. }
  48. return iter;
  49. }
  50. #endif
  51. #ifdef _WIN32
  52. static char *dup_wchar_to_utf8(wchar_t *w)
  53. {
  54. char *s = NULL;
  55. int l = WideCharToMultiByte(CP_UTF8, 0, w, -1, 0, 0, 0, 0);
  56. s = (char *) av_malloc(l);
  57. if (s)
  58. WideCharToMultiByte(CP_UTF8, 0, w, -1, s, l, 0, 0);
  59. return s;
  60. }
  61. #define DECKLINK_STR OLECHAR *
  62. #define DECKLINK_STRDUP dup_wchar_to_utf8
  63. #define DECKLINK_FREE(s) SysFreeString(s)
  64. #define DECKLINK_BOOL BOOL
  65. #elif defined(__APPLE__)
  66. static char *dup_cfstring_to_utf8(CFStringRef w)
  67. {
  68. char s[256];
  69. CFStringGetCString(w, s, 255, kCFStringEncodingUTF8);
  70. return av_strdup(s);
  71. }
  72. #define DECKLINK_STR const __CFString *
  73. #define DECKLINK_STRDUP dup_cfstring_to_utf8
  74. #define DECKLINK_FREE(s) free((void *) s)
  75. #define DECKLINK_BOOL bool
  76. #else
  77. #define DECKLINK_STR const char *
  78. #define DECKLINK_STRDUP av_strdup
  79. /* free() is needed for a string returned by the DeckLink SDL. */
  80. #define DECKLINK_FREE(s) free((void *) s)
  81. #define DECKLINK_BOOL bool
  82. #endif
  83. HRESULT ff_decklink_get_display_name(IDeckLink *This, const char **displayName)
  84. {
  85. DECKLINK_STR tmpDisplayName;
  86. HRESULT hr = This->GetDisplayName(&tmpDisplayName);
  87. if (hr != S_OK)
  88. return hr;
  89. *displayName = DECKLINK_STRDUP(tmpDisplayName);
  90. DECKLINK_FREE(tmpDisplayName);
  91. return hr;
  92. }
  93. static int decklink_select_input(AVFormatContext *avctx, BMDDeckLinkConfigurationID cfg_id)
  94. {
  95. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  96. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  97. BMDDeckLinkAttributeID attr_id = (cfg_id == bmdDeckLinkConfigAudioInputConnection) ? BMDDeckLinkAudioInputConnections : BMDDeckLinkVideoInputConnections;
  98. int64_t bmd_input = (cfg_id == bmdDeckLinkConfigAudioInputConnection) ? (int64_t)ctx->audio_input : (int64_t)ctx->video_input;
  99. const char *type_name = (cfg_id == bmdDeckLinkConfigAudioInputConnection) ? "audio" : "video";
  100. int64_t supported_connections = 0;
  101. HRESULT res;
  102. if (bmd_input) {
  103. res = ctx->attr->GetInt(attr_id, &supported_connections);
  104. if (res != S_OK) {
  105. av_log(avctx, AV_LOG_ERROR, "Failed to query supported %s inputs.\n", type_name);
  106. return AVERROR_EXTERNAL;
  107. }
  108. if ((supported_connections & bmd_input) != bmd_input) {
  109. av_log(avctx, AV_LOG_ERROR, "Device does not support selected %s input.\n", type_name);
  110. return AVERROR(ENOSYS);
  111. }
  112. res = ctx->cfg->SetInt(cfg_id, bmd_input);
  113. if (res != S_OK) {
  114. av_log(avctx, AV_LOG_ERROR, "Failed to select %s input.\n", type_name);
  115. return AVERROR_EXTERNAL;
  116. }
  117. }
  118. return 0;
  119. }
  120. int ff_decklink_set_format(AVFormatContext *avctx,
  121. int width, int height,
  122. int tb_num, int tb_den,
  123. decklink_direction_t direction, int num)
  124. {
  125. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  126. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  127. BMDDisplayModeSupport support;
  128. IDeckLinkDisplayModeIterator *itermode;
  129. IDeckLinkDisplayMode *mode;
  130. int i = 1;
  131. HRESULT res;
  132. av_log(avctx, AV_LOG_DEBUG, "Trying to find mode for frame size %dx%d, frame timing %d/%d, direction %d, mode number %d\n",
  133. width, height, tb_num, tb_den, direction, num);
  134. if (ctx->duplex_mode) {
  135. DECKLINK_BOOL duplex_supported = false;
  136. if (ctx->attr->GetFlag(BMDDeckLinkSupportsDuplexModeConfiguration, &duplex_supported) != S_OK)
  137. duplex_supported = false;
  138. if (duplex_supported) {
  139. res = ctx->cfg->SetInt(bmdDeckLinkConfigDuplexMode, ctx->duplex_mode == 2 ? bmdDuplexModeFull : bmdDuplexModeHalf);
  140. if (res != S_OK)
  141. av_log(avctx, AV_LOG_WARNING, "Setting duplex mode failed.\n");
  142. else
  143. av_log(avctx, AV_LOG_VERBOSE, "Successfully set duplex mode to %s duplex.\n", ctx->duplex_mode == 2 ? "full" : "half");
  144. } else {
  145. av_log(avctx, AV_LOG_WARNING, "Unable to set duplex mode, because it is not supported.\n");
  146. }
  147. }
  148. if (direction == DIRECTION_IN) {
  149. int ret;
  150. ret = decklink_select_input(avctx, bmdDeckLinkConfigAudioInputConnection);
  151. if (ret < 0)
  152. return ret;
  153. ret = decklink_select_input(avctx, bmdDeckLinkConfigVideoInputConnection);
  154. if (ret < 0)
  155. return ret;
  156. res = ctx->dli->GetDisplayModeIterator (&itermode);
  157. } else {
  158. res = ctx->dlo->GetDisplayModeIterator (&itermode);
  159. }
  160. if (res!= S_OK) {
  161. av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
  162. return AVERROR(EIO);
  163. }
  164. AVRational target_tb = av_make_q(tb_num, tb_den);
  165. ctx->bmd_mode = bmdModeUnknown;
  166. while ((ctx->bmd_mode == bmdModeUnknown) && itermode->Next(&mode) == S_OK) {
  167. BMDTimeValue bmd_tb_num, bmd_tb_den;
  168. int bmd_width = mode->GetWidth();
  169. int bmd_height = mode->GetHeight();
  170. mode->GetFrameRate(&bmd_tb_num, &bmd_tb_den);
  171. AVRational mode_tb = av_make_q(bmd_tb_num, bmd_tb_den);
  172. if ((bmd_width == width && bmd_height == height &&
  173. !av_cmp_q(mode_tb, target_tb)) || i == num) {
  174. ctx->bmd_mode = mode->GetDisplayMode();
  175. ctx->bmd_width = bmd_width;
  176. ctx->bmd_height = bmd_height;
  177. ctx->bmd_tb_den = bmd_tb_den;
  178. ctx->bmd_tb_num = bmd_tb_num;
  179. ctx->bmd_field_dominance = mode->GetFieldDominance();
  180. av_log(avctx, AV_LOG_INFO, "Found Decklink mode %d x %d with rate %.2f%s\n",
  181. bmd_width, bmd_height, 1/av_q2d(mode_tb),
  182. (ctx->bmd_field_dominance==bmdLowerFieldFirst || ctx->bmd_field_dominance==bmdUpperFieldFirst)?"(i)":"");
  183. }
  184. mode->Release();
  185. i++;
  186. }
  187. itermode->Release();
  188. if (ctx->bmd_mode == bmdModeUnknown)
  189. return -1;
  190. if (direction == DIRECTION_IN) {
  191. if (ctx->dli->DoesSupportVideoMode(ctx->bmd_mode, bmdFormat8BitYUV,
  192. bmdVideoOutputFlagDefault,
  193. &support, NULL) != S_OK)
  194. return -1;
  195. } else {
  196. if (ctx->dlo->DoesSupportVideoMode(ctx->bmd_mode, bmdFormat8BitYUV,
  197. bmdVideoOutputFlagDefault,
  198. &support, NULL) != S_OK)
  199. return -1;
  200. }
  201. if (support == bmdDisplayModeSupported)
  202. return 0;
  203. return -1;
  204. }
  205. int ff_decklink_set_format(AVFormatContext *avctx, decklink_direction_t direction, int num) {
  206. return ff_decklink_set_format(avctx, 0, 0, 0, 0, direction, num);
  207. }
  208. int ff_decklink_list_devices(AVFormatContext *avctx)
  209. {
  210. IDeckLink *dl = NULL;
  211. IDeckLinkIterator *iter = CreateDeckLinkIteratorInstance();
  212. if (!iter) {
  213. av_log(avctx, AV_LOG_ERROR, "Could not create DeckLink iterator\n");
  214. return AVERROR(EIO);
  215. }
  216. av_log(avctx, AV_LOG_INFO, "Blackmagic DeckLink devices:\n");
  217. while (iter->Next(&dl) == S_OK) {
  218. const char *displayName;
  219. ff_decklink_get_display_name(dl, &displayName);
  220. av_log(avctx, AV_LOG_INFO, "\t'%s'\n", displayName);
  221. av_free((void *) displayName);
  222. dl->Release();
  223. }
  224. iter->Release();
  225. return 0;
  226. }
  227. int ff_decklink_list_formats(AVFormatContext *avctx, decklink_direction_t direction)
  228. {
  229. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  230. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  231. IDeckLinkDisplayModeIterator *itermode;
  232. IDeckLinkDisplayMode *mode;
  233. int i=0;
  234. HRESULT res;
  235. if (direction == DIRECTION_IN) {
  236. int ret;
  237. ret = decklink_select_input(avctx, bmdDeckLinkConfigAudioInputConnection);
  238. if (ret < 0)
  239. return ret;
  240. ret = decklink_select_input(avctx, bmdDeckLinkConfigVideoInputConnection);
  241. if (ret < 0)
  242. return ret;
  243. res = ctx->dli->GetDisplayModeIterator (&itermode);
  244. } else {
  245. res = ctx->dlo->GetDisplayModeIterator (&itermode);
  246. }
  247. if (res!= S_OK) {
  248. av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
  249. return AVERROR(EIO);
  250. }
  251. av_log(avctx, AV_LOG_INFO, "Supported formats for '%s':\n",
  252. avctx->filename);
  253. while (itermode->Next(&mode) == S_OK) {
  254. BMDTimeValue tb_num, tb_den;
  255. mode->GetFrameRate(&tb_num, &tb_den);
  256. av_log(avctx, AV_LOG_INFO, "\t%d\t%ldx%ld at %d/%d fps",
  257. ++i,mode->GetWidth(), mode->GetHeight(),
  258. (int) tb_den, (int) tb_num);
  259. switch (mode->GetFieldDominance()) {
  260. case bmdLowerFieldFirst:
  261. av_log(avctx, AV_LOG_INFO, " (interlaced, lower field first)"); break;
  262. case bmdUpperFieldFirst:
  263. av_log(avctx, AV_LOG_INFO, " (interlaced, upper field first)"); break;
  264. }
  265. av_log(avctx, AV_LOG_INFO, "\n");
  266. mode->Release();
  267. }
  268. itermode->Release();
  269. return 0;
  270. }
  271. void ff_decklink_cleanup(AVFormatContext *avctx)
  272. {
  273. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  274. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  275. if (ctx->dli)
  276. ctx->dli->Release();
  277. if (ctx->dlo)
  278. ctx->dlo->Release();
  279. if (ctx->attr)
  280. ctx->attr->Release();
  281. if (ctx->cfg)
  282. ctx->cfg->Release();
  283. if (ctx->dl)
  284. ctx->dl->Release();
  285. }
  286. int ff_decklink_init_device(AVFormatContext *avctx, const char* name)
  287. {
  288. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  289. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  290. IDeckLink *dl = NULL;
  291. IDeckLinkIterator *iter = CreateDeckLinkIteratorInstance();
  292. if (!iter) {
  293. av_log(avctx, AV_LOG_ERROR, "Could not create DeckLink iterator\n");
  294. return AVERROR_EXTERNAL;
  295. }
  296. while (iter->Next(&dl) == S_OK) {
  297. const char *displayName;
  298. ff_decklink_get_display_name(dl, &displayName);
  299. if (!strcmp(name, displayName)) {
  300. av_free((void *)displayName);
  301. ctx->dl = dl;
  302. break;
  303. }
  304. av_free((void *)displayName);
  305. dl->Release();
  306. }
  307. iter->Release();
  308. if (!ctx->dl)
  309. return AVERROR(ENXIO);
  310. if (ctx->dl->QueryInterface(IID_IDeckLinkConfiguration, (void **)&ctx->cfg) != S_OK) {
  311. av_log(avctx, AV_LOG_ERROR, "Could not get configuration interface for '%s'\n", name);
  312. ff_decklink_cleanup(avctx);
  313. return AVERROR_EXTERNAL;
  314. }
  315. if (ctx->dl->QueryInterface(IID_IDeckLinkAttributes, (void **)&ctx->attr) != S_OK) {
  316. av_log(avctx, AV_LOG_ERROR, "Could not get attributes interface for '%s'\n", name);
  317. ff_decklink_cleanup(avctx);
  318. return AVERROR_EXTERNAL;
  319. }
  320. return 0;
  321. }