You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

392 lines
14KB

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