decklink_common.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. #include "libavutil/intreadwrite.h"
  34. #include "libavutil/bswap.h"
  35. }
  36. #include "decklink_common.h"
  37. #ifdef _WIN32
  38. IDeckLinkIterator *CreateDeckLinkIteratorInstance(void)
  39. {
  40. IDeckLinkIterator *iter;
  41. if (CoInitialize(NULL) < 0) {
  42. av_log(NULL, AV_LOG_ERROR, "COM initialization failed.\n");
  43. return NULL;
  44. }
  45. if (CoCreateInstance(CLSID_CDeckLinkIterator, NULL, CLSCTX_ALL,
  46. IID_IDeckLinkIterator, (void**) &iter) != S_OK) {
  47. av_log(NULL, AV_LOG_ERROR, "DeckLink drivers not installed.\n");
  48. return NULL;
  49. }
  50. return iter;
  51. }
  52. #endif
  53. #ifdef _WIN32
  54. static char *dup_wchar_to_utf8(wchar_t *w)
  55. {
  56. char *s = NULL;
  57. int l = WideCharToMultiByte(CP_UTF8, 0, w, -1, 0, 0, 0, 0);
  58. s = (char *) av_malloc(l);
  59. if (s)
  60. WideCharToMultiByte(CP_UTF8, 0, w, -1, s, l, 0, 0);
  61. return s;
  62. }
  63. #define DECKLINK_STR OLECHAR *
  64. #define DECKLINK_STRDUP dup_wchar_to_utf8
  65. #define DECKLINK_FREE(s) SysFreeString(s)
  66. #define DECKLINK_BOOL BOOL
  67. #elif defined(__APPLE__)
  68. static char *dup_cfstring_to_utf8(CFStringRef w)
  69. {
  70. char s[256];
  71. CFStringGetCString(w, s, 255, kCFStringEncodingUTF8);
  72. return av_strdup(s);
  73. }
  74. #define DECKLINK_STR const __CFString *
  75. #define DECKLINK_STRDUP dup_cfstring_to_utf8
  76. #define DECKLINK_FREE(s) free((void *) s)
  77. #define DECKLINK_BOOL bool
  78. #else
  79. #define DECKLINK_STR const char *
  80. #define DECKLINK_STRDUP av_strdup
  81. /* free() is needed for a string returned by the DeckLink SDL. */
  82. #define DECKLINK_FREE(s) free((void *) s)
  83. #define DECKLINK_BOOL bool
  84. #endif
  85. HRESULT ff_decklink_get_display_name(IDeckLink *This, const char **displayName)
  86. {
  87. DECKLINK_STR tmpDisplayName;
  88. HRESULT hr = This->GetDisplayName(&tmpDisplayName);
  89. if (hr != S_OK)
  90. return hr;
  91. *displayName = DECKLINK_STRDUP(tmpDisplayName);
  92. DECKLINK_FREE(tmpDisplayName);
  93. return hr;
  94. }
  95. static int decklink_select_input(AVFormatContext *avctx, BMDDeckLinkConfigurationID cfg_id)
  96. {
  97. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  98. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  99. BMDDeckLinkAttributeID attr_id = (cfg_id == bmdDeckLinkConfigAudioInputConnection) ? BMDDeckLinkAudioInputConnections : BMDDeckLinkVideoInputConnections;
  100. int64_t bmd_input = (cfg_id == bmdDeckLinkConfigAudioInputConnection) ? (int64_t)ctx->audio_input : (int64_t)ctx->video_input;
  101. const char *type_name = (cfg_id == bmdDeckLinkConfigAudioInputConnection) ? "audio" : "video";
  102. int64_t supported_connections = 0;
  103. HRESULT res;
  104. if (bmd_input) {
  105. res = ctx->attr->GetInt(attr_id, &supported_connections);
  106. if (res != S_OK) {
  107. av_log(avctx, AV_LOG_ERROR, "Failed to query supported %s inputs.\n", type_name);
  108. return AVERROR_EXTERNAL;
  109. }
  110. if ((supported_connections & bmd_input) != bmd_input) {
  111. av_log(avctx, AV_LOG_ERROR, "Device does not support selected %s input.\n", type_name);
  112. return AVERROR(ENOSYS);
  113. }
  114. res = ctx->cfg->SetInt(cfg_id, bmd_input);
  115. if (res != S_OK) {
  116. av_log(avctx, AV_LOG_ERROR, "Failed to select %s input.\n", type_name);
  117. return AVERROR_EXTERNAL;
  118. }
  119. }
  120. return 0;
  121. }
  122. static DECKLINK_BOOL field_order_eq(enum AVFieldOrder field_order, BMDFieldDominance bmd_field_order)
  123. {
  124. if (field_order == AV_FIELD_UNKNOWN)
  125. return true;
  126. if ((field_order == AV_FIELD_TT || field_order == AV_FIELD_TB) && bmd_field_order == bmdUpperFieldFirst)
  127. return true;
  128. if ((field_order == AV_FIELD_BB || field_order == AV_FIELD_BT) && bmd_field_order == bmdLowerFieldFirst)
  129. return true;
  130. if (field_order == AV_FIELD_PROGRESSIVE && (bmd_field_order == bmdProgressiveFrame || bmd_field_order == bmdProgressiveSegmentedFrame))
  131. return true;
  132. return false;
  133. }
  134. int ff_decklink_set_format(AVFormatContext *avctx,
  135. int width, int height,
  136. int tb_num, int tb_den,
  137. enum AVFieldOrder field_order,
  138. decklink_direction_t direction, int num)
  139. {
  140. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  141. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  142. BMDDisplayModeSupport support;
  143. IDeckLinkDisplayModeIterator *itermode;
  144. IDeckLinkDisplayMode *mode;
  145. int i = 1;
  146. HRESULT res;
  147. av_log(avctx, AV_LOG_DEBUG, "Trying to find mode for frame size %dx%d, frame timing %d/%d, field order %d, direction %d, mode number %d, format code %s\n",
  148. width, height, tb_num, tb_den, field_order, direction, num, (cctx->format_code) ? cctx->format_code : "(unset)");
  149. if (ctx->duplex_mode) {
  150. DECKLINK_BOOL duplex_supported = false;
  151. if (ctx->attr->GetFlag(BMDDeckLinkSupportsDuplexModeConfiguration, &duplex_supported) != S_OK)
  152. duplex_supported = false;
  153. if (duplex_supported) {
  154. res = ctx->cfg->SetInt(bmdDeckLinkConfigDuplexMode, ctx->duplex_mode == 2 ? bmdDuplexModeFull : bmdDuplexModeHalf);
  155. if (res != S_OK)
  156. av_log(avctx, AV_LOG_WARNING, "Setting duplex mode failed.\n");
  157. else
  158. av_log(avctx, AV_LOG_VERBOSE, "Successfully set duplex mode to %s duplex.\n", ctx->duplex_mode == 2 ? "full" : "half");
  159. } else {
  160. av_log(avctx, AV_LOG_WARNING, "Unable to set duplex mode, because it is not supported.\n");
  161. }
  162. }
  163. if (direction == DIRECTION_IN) {
  164. int ret;
  165. ret = decklink_select_input(avctx, bmdDeckLinkConfigAudioInputConnection);
  166. if (ret < 0)
  167. return ret;
  168. ret = decklink_select_input(avctx, bmdDeckLinkConfigVideoInputConnection);
  169. if (ret < 0)
  170. return ret;
  171. res = ctx->dli->GetDisplayModeIterator (&itermode);
  172. } else {
  173. res = ctx->dlo->GetDisplayModeIterator (&itermode);
  174. }
  175. if (res!= S_OK) {
  176. av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
  177. return AVERROR(EIO);
  178. }
  179. char format_buf[] = " ";
  180. if (cctx->format_code)
  181. memcpy(format_buf, cctx->format_code, FFMIN(strlen(cctx->format_code), sizeof(format_buf)));
  182. BMDDisplayMode target_mode = (BMDDisplayMode)AV_RB32(format_buf);
  183. AVRational target_tb = av_make_q(tb_num, tb_den);
  184. ctx->bmd_mode = bmdModeUnknown;
  185. while ((ctx->bmd_mode == bmdModeUnknown) && itermode->Next(&mode) == S_OK) {
  186. BMDTimeValue bmd_tb_num, bmd_tb_den;
  187. int bmd_width = mode->GetWidth();
  188. int bmd_height = mode->GetHeight();
  189. BMDDisplayMode bmd_mode = mode->GetDisplayMode();
  190. BMDFieldDominance bmd_field_dominance = mode->GetFieldDominance();
  191. mode->GetFrameRate(&bmd_tb_num, &bmd_tb_den);
  192. AVRational mode_tb = av_make_q(bmd_tb_num, bmd_tb_den);
  193. if ((bmd_width == width &&
  194. bmd_height == height &&
  195. !av_cmp_q(mode_tb, target_tb) &&
  196. field_order_eq(field_order, bmd_field_dominance))
  197. || i == num
  198. || target_mode == bmd_mode) {
  199. ctx->bmd_mode = bmd_mode;
  200. ctx->bmd_width = bmd_width;
  201. ctx->bmd_height = bmd_height;
  202. ctx->bmd_tb_den = bmd_tb_den;
  203. ctx->bmd_tb_num = bmd_tb_num;
  204. ctx->bmd_field_dominance = bmd_field_dominance;
  205. av_log(avctx, AV_LOG_INFO, "Found Decklink mode %d x %d with rate %.2f%s\n",
  206. bmd_width, bmd_height, 1/av_q2d(mode_tb),
  207. (ctx->bmd_field_dominance==bmdLowerFieldFirst || ctx->bmd_field_dominance==bmdUpperFieldFirst)?"(i)":"");
  208. }
  209. mode->Release();
  210. i++;
  211. }
  212. itermode->Release();
  213. if (ctx->bmd_mode == bmdModeUnknown)
  214. return -1;
  215. if (direction == DIRECTION_IN) {
  216. if (ctx->dli->DoesSupportVideoMode(ctx->bmd_mode, bmdFormat8BitYUV,
  217. bmdVideoOutputFlagDefault,
  218. &support, NULL) != S_OK)
  219. return -1;
  220. } else {
  221. if (ctx->dlo->DoesSupportVideoMode(ctx->bmd_mode, bmdFormat8BitYUV,
  222. bmdVideoOutputFlagDefault,
  223. &support, NULL) != S_OK)
  224. return -1;
  225. }
  226. if (support == bmdDisplayModeSupported)
  227. return 0;
  228. return -1;
  229. }
  230. int ff_decklink_set_format(AVFormatContext *avctx, decklink_direction_t direction, int num) {
  231. return ff_decklink_set_format(avctx, 0, 0, 0, 0, AV_FIELD_UNKNOWN, direction, num);
  232. }
  233. int ff_decklink_list_devices(AVFormatContext *avctx)
  234. {
  235. IDeckLink *dl = NULL;
  236. IDeckLinkIterator *iter = CreateDeckLinkIteratorInstance();
  237. if (!iter) {
  238. av_log(avctx, AV_LOG_ERROR, "Could not create DeckLink iterator\n");
  239. return AVERROR(EIO);
  240. }
  241. av_log(avctx, AV_LOG_INFO, "Blackmagic DeckLink devices:\n");
  242. while (iter->Next(&dl) == S_OK) {
  243. const char *displayName;
  244. ff_decklink_get_display_name(dl, &displayName);
  245. av_log(avctx, AV_LOG_INFO, "\t'%s'\n", displayName);
  246. av_free((void *) displayName);
  247. dl->Release();
  248. }
  249. iter->Release();
  250. return 0;
  251. }
  252. int ff_decklink_list_formats(AVFormatContext *avctx, decklink_direction_t direction)
  253. {
  254. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  255. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  256. IDeckLinkDisplayModeIterator *itermode;
  257. IDeckLinkDisplayMode *mode;
  258. uint32_t format_code;
  259. HRESULT res;
  260. if (direction == DIRECTION_IN) {
  261. int ret;
  262. ret = decklink_select_input(avctx, bmdDeckLinkConfigAudioInputConnection);
  263. if (ret < 0)
  264. return ret;
  265. ret = decklink_select_input(avctx, bmdDeckLinkConfigVideoInputConnection);
  266. if (ret < 0)
  267. return ret;
  268. res = ctx->dli->GetDisplayModeIterator (&itermode);
  269. } else {
  270. res = ctx->dlo->GetDisplayModeIterator (&itermode);
  271. }
  272. if (res!= S_OK) {
  273. av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
  274. return AVERROR(EIO);
  275. }
  276. av_log(avctx, AV_LOG_INFO, "Supported formats for '%s':\n\tformat_code\tdescription",
  277. avctx->filename);
  278. while (itermode->Next(&mode) == S_OK) {
  279. BMDTimeValue tb_num, tb_den;
  280. mode->GetFrameRate(&tb_num, &tb_den);
  281. format_code = av_bswap32(mode->GetDisplayMode());
  282. av_log(avctx, AV_LOG_INFO, "\n\t%.4s\t\t%ldx%ld at %d/%d fps",
  283. (char*) &format_code, mode->GetWidth(), mode->GetHeight(),
  284. (int) tb_den, (int) tb_num);
  285. switch (mode->GetFieldDominance()) {
  286. case bmdLowerFieldFirst:
  287. av_log(avctx, AV_LOG_INFO, " (interlaced, lower field first)"); break;
  288. case bmdUpperFieldFirst:
  289. av_log(avctx, AV_LOG_INFO, " (interlaced, upper field first)"); break;
  290. }
  291. mode->Release();
  292. }
  293. av_log(avctx, AV_LOG_INFO, "\n");
  294. itermode->Release();
  295. return 0;
  296. }
  297. void ff_decklink_cleanup(AVFormatContext *avctx)
  298. {
  299. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  300. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  301. if (ctx->dli)
  302. ctx->dli->Release();
  303. if (ctx->dlo)
  304. ctx->dlo->Release();
  305. if (ctx->attr)
  306. ctx->attr->Release();
  307. if (ctx->cfg)
  308. ctx->cfg->Release();
  309. if (ctx->dl)
  310. ctx->dl->Release();
  311. }
  312. int ff_decklink_init_device(AVFormatContext *avctx, const char* name)
  313. {
  314. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  315. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  316. IDeckLink *dl = NULL;
  317. IDeckLinkIterator *iter = CreateDeckLinkIteratorInstance();
  318. if (!iter) {
  319. av_log(avctx, AV_LOG_ERROR, "Could not create DeckLink iterator\n");
  320. return AVERROR_EXTERNAL;
  321. }
  322. while (iter->Next(&dl) == S_OK) {
  323. const char *displayName;
  324. ff_decklink_get_display_name(dl, &displayName);
  325. if (!strcmp(name, displayName)) {
  326. av_free((void *)displayName);
  327. ctx->dl = dl;
  328. break;
  329. }
  330. av_free((void *)displayName);
  331. dl->Release();
  332. }
  333. iter->Release();
  334. if (!ctx->dl)
  335. return AVERROR(ENXIO);
  336. if (ctx->dl->QueryInterface(IID_IDeckLinkConfiguration, (void **)&ctx->cfg) != S_OK) {
  337. av_log(avctx, AV_LOG_ERROR, "Could not get configuration interface for '%s'\n", name);
  338. ff_decklink_cleanup(avctx);
  339. return AVERROR_EXTERNAL;
  340. }
  341. if (ctx->dl->QueryInterface(IID_IDeckLinkAttributes, (void **)&ctx->attr) != S_OK) {
  342. av_log(avctx, AV_LOG_ERROR, "Could not get attributes interface for '%s'\n", name);
  343. ff_decklink_cleanup(avctx);
  344. return AVERROR_EXTERNAL;
  345. }
  346. return 0;
  347. }