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.

387 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 <DeckLinkAPI.h>
  22. #ifdef _WIN32
  23. #include <DeckLinkAPI_i.c>
  24. #else
  25. #include <DeckLinkAPIDispatch.cpp>
  26. #endif
  27. extern "C" {
  28. #include "libavformat/avformat.h"
  29. #include "libavformat/internal.h"
  30. #include "libavutil/imgutils.h"
  31. #include "libavutil/intreadwrite.h"
  32. #include "libavutil/bswap.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. static DECKLINK_BOOL field_order_eq(enum AVFieldOrder field_order, BMDFieldDominance bmd_field_order)
  121. {
  122. if (field_order == AV_FIELD_UNKNOWN)
  123. return true;
  124. if ((field_order == AV_FIELD_TT || field_order == AV_FIELD_TB) && bmd_field_order == bmdUpperFieldFirst)
  125. return true;
  126. if ((field_order == AV_FIELD_BB || field_order == AV_FIELD_BT) && bmd_field_order == bmdLowerFieldFirst)
  127. return true;
  128. if (field_order == AV_FIELD_PROGRESSIVE && (bmd_field_order == bmdProgressiveFrame || bmd_field_order == bmdProgressiveSegmentedFrame))
  129. return true;
  130. return false;
  131. }
  132. int ff_decklink_set_format(AVFormatContext *avctx,
  133. int width, int height,
  134. int tb_num, int tb_den,
  135. enum AVFieldOrder field_order,
  136. decklink_direction_t direction, int num)
  137. {
  138. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  139. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  140. BMDDisplayModeSupport support;
  141. IDeckLinkDisplayModeIterator *itermode;
  142. IDeckLinkDisplayMode *mode;
  143. int i = 1;
  144. HRESULT res;
  145. 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",
  146. width, height, tb_num, tb_den, field_order, direction, num, (cctx->format_code) ? cctx->format_code : "(unset)");
  147. if (ctx->duplex_mode) {
  148. DECKLINK_BOOL duplex_supported = false;
  149. if (ctx->attr->GetFlag(BMDDeckLinkSupportsDuplexModeConfiguration, &duplex_supported) != S_OK)
  150. duplex_supported = false;
  151. if (duplex_supported) {
  152. res = ctx->cfg->SetInt(bmdDeckLinkConfigDuplexMode, ctx->duplex_mode == 2 ? bmdDuplexModeFull : bmdDuplexModeHalf);
  153. if (res != S_OK)
  154. av_log(avctx, AV_LOG_WARNING, "Setting duplex mode failed.\n");
  155. else
  156. av_log(avctx, AV_LOG_VERBOSE, "Successfully set duplex mode to %s duplex.\n", ctx->duplex_mode == 2 ? "full" : "half");
  157. } else {
  158. av_log(avctx, AV_LOG_WARNING, "Unable to set duplex mode, because it is not supported.\n");
  159. }
  160. }
  161. if (direction == DIRECTION_IN) {
  162. int ret;
  163. ret = decklink_select_input(avctx, bmdDeckLinkConfigAudioInputConnection);
  164. if (ret < 0)
  165. return ret;
  166. ret = decklink_select_input(avctx, bmdDeckLinkConfigVideoInputConnection);
  167. if (ret < 0)
  168. return ret;
  169. res = ctx->dli->GetDisplayModeIterator (&itermode);
  170. } else {
  171. res = ctx->dlo->GetDisplayModeIterator (&itermode);
  172. }
  173. if (res!= S_OK) {
  174. av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
  175. return AVERROR(EIO);
  176. }
  177. char format_buf[] = " ";
  178. if (cctx->format_code)
  179. memcpy(format_buf, cctx->format_code, FFMIN(strlen(cctx->format_code), sizeof(format_buf)));
  180. BMDDisplayMode target_mode = (BMDDisplayMode)AV_RB32(format_buf);
  181. AVRational target_tb = av_make_q(tb_num, tb_den);
  182. ctx->bmd_mode = bmdModeUnknown;
  183. while ((ctx->bmd_mode == bmdModeUnknown) && itermode->Next(&mode) == S_OK) {
  184. BMDTimeValue bmd_tb_num, bmd_tb_den;
  185. int bmd_width = mode->GetWidth();
  186. int bmd_height = mode->GetHeight();
  187. BMDDisplayMode bmd_mode = mode->GetDisplayMode();
  188. BMDFieldDominance bmd_field_dominance = mode->GetFieldDominance();
  189. mode->GetFrameRate(&bmd_tb_num, &bmd_tb_den);
  190. AVRational mode_tb = av_make_q(bmd_tb_num, bmd_tb_den);
  191. if ((bmd_width == width &&
  192. bmd_height == height &&
  193. !av_cmp_q(mode_tb, target_tb) &&
  194. field_order_eq(field_order, bmd_field_dominance))
  195. || i == num
  196. || target_mode == bmd_mode) {
  197. ctx->bmd_mode = bmd_mode;
  198. ctx->bmd_width = bmd_width;
  199. ctx->bmd_height = bmd_height;
  200. ctx->bmd_tb_den = bmd_tb_den;
  201. ctx->bmd_tb_num = bmd_tb_num;
  202. ctx->bmd_field_dominance = bmd_field_dominance;
  203. av_log(avctx, AV_LOG_INFO, "Found Decklink mode %d x %d with rate %.2f%s\n",
  204. bmd_width, bmd_height, 1/av_q2d(mode_tb),
  205. (ctx->bmd_field_dominance==bmdLowerFieldFirst || ctx->bmd_field_dominance==bmdUpperFieldFirst)?"(i)":"");
  206. }
  207. mode->Release();
  208. i++;
  209. }
  210. itermode->Release();
  211. if (ctx->bmd_mode == bmdModeUnknown)
  212. return -1;
  213. if (direction == DIRECTION_IN) {
  214. if (ctx->dli->DoesSupportVideoMode(ctx->bmd_mode, bmdFormat8BitYUV,
  215. bmdVideoOutputFlagDefault,
  216. &support, NULL) != S_OK)
  217. return -1;
  218. } else {
  219. if (ctx->dlo->DoesSupportVideoMode(ctx->bmd_mode, bmdFormat8BitYUV,
  220. bmdVideoOutputFlagDefault,
  221. &support, NULL) != S_OK)
  222. return -1;
  223. }
  224. if (support == bmdDisplayModeSupported)
  225. return 0;
  226. return -1;
  227. }
  228. int ff_decklink_set_format(AVFormatContext *avctx, decklink_direction_t direction, int num) {
  229. return ff_decklink_set_format(avctx, 0, 0, 0, 0, AV_FIELD_UNKNOWN, direction, num);
  230. }
  231. int ff_decklink_list_devices(AVFormatContext *avctx)
  232. {
  233. IDeckLink *dl = NULL;
  234. IDeckLinkIterator *iter = CreateDeckLinkIteratorInstance();
  235. if (!iter) {
  236. av_log(avctx, AV_LOG_ERROR, "Could not create DeckLink iterator\n");
  237. return AVERROR(EIO);
  238. }
  239. av_log(avctx, AV_LOG_INFO, "Blackmagic DeckLink devices:\n");
  240. while (iter->Next(&dl) == S_OK) {
  241. const char *displayName;
  242. ff_decklink_get_display_name(dl, &displayName);
  243. av_log(avctx, AV_LOG_INFO, "\t'%s'\n", displayName);
  244. av_free((void *) displayName);
  245. dl->Release();
  246. }
  247. iter->Release();
  248. return 0;
  249. }
  250. int ff_decklink_list_formats(AVFormatContext *avctx, decklink_direction_t direction)
  251. {
  252. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  253. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  254. IDeckLinkDisplayModeIterator *itermode;
  255. IDeckLinkDisplayMode *mode;
  256. uint32_t format_code;
  257. HRESULT res;
  258. if (direction == DIRECTION_IN) {
  259. int ret;
  260. ret = decklink_select_input(avctx, bmdDeckLinkConfigAudioInputConnection);
  261. if (ret < 0)
  262. return ret;
  263. ret = decklink_select_input(avctx, bmdDeckLinkConfigVideoInputConnection);
  264. if (ret < 0)
  265. return ret;
  266. res = ctx->dli->GetDisplayModeIterator (&itermode);
  267. } else {
  268. res = ctx->dlo->GetDisplayModeIterator (&itermode);
  269. }
  270. if (res!= S_OK) {
  271. av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
  272. return AVERROR(EIO);
  273. }
  274. av_log(avctx, AV_LOG_INFO, "Supported formats for '%s':\n\tformat_code\tdescription",
  275. avctx->filename);
  276. while (itermode->Next(&mode) == S_OK) {
  277. BMDTimeValue tb_num, tb_den;
  278. mode->GetFrameRate(&tb_num, &tb_den);
  279. format_code = av_bswap32(mode->GetDisplayMode());
  280. av_log(avctx, AV_LOG_INFO, "\n\t%.4s\t\t%ldx%ld at %d/%d fps",
  281. (char*) &format_code, mode->GetWidth(), mode->GetHeight(),
  282. (int) tb_den, (int) tb_num);
  283. switch (mode->GetFieldDominance()) {
  284. case bmdLowerFieldFirst:
  285. av_log(avctx, AV_LOG_INFO, " (interlaced, lower field first)"); break;
  286. case bmdUpperFieldFirst:
  287. av_log(avctx, AV_LOG_INFO, " (interlaced, upper field first)"); break;
  288. }
  289. mode->Release();
  290. }
  291. av_log(avctx, AV_LOG_INFO, "\n");
  292. itermode->Release();
  293. return 0;
  294. }
  295. void ff_decklink_cleanup(AVFormatContext *avctx)
  296. {
  297. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  298. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  299. if (ctx->dli)
  300. ctx->dli->Release();
  301. if (ctx->dlo)
  302. ctx->dlo->Release();
  303. if (ctx->attr)
  304. ctx->attr->Release();
  305. if (ctx->cfg)
  306. ctx->cfg->Release();
  307. if (ctx->dl)
  308. ctx->dl->Release();
  309. }
  310. int ff_decklink_init_device(AVFormatContext *avctx, const char* name)
  311. {
  312. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  313. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  314. IDeckLink *dl = NULL;
  315. IDeckLinkIterator *iter = CreateDeckLinkIteratorInstance();
  316. if (!iter) {
  317. av_log(avctx, AV_LOG_ERROR, "Could not create DeckLink iterator\n");
  318. return AVERROR_EXTERNAL;
  319. }
  320. while (iter->Next(&dl) == S_OK) {
  321. const char *displayName;
  322. ff_decklink_get_display_name(dl, &displayName);
  323. if (!strcmp(name, displayName)) {
  324. av_free((void *)displayName);
  325. ctx->dl = dl;
  326. break;
  327. }
  328. av_free((void *)displayName);
  329. dl->Release();
  330. }
  331. iter->Release();
  332. if (!ctx->dl)
  333. return AVERROR(ENXIO);
  334. if (ctx->dl->QueryInterface(IID_IDeckLinkConfiguration, (void **)&ctx->cfg) != S_OK) {
  335. av_log(avctx, AV_LOG_ERROR, "Could not get configuration interface for '%s'\n", name);
  336. ff_decklink_cleanup(avctx);
  337. return AVERROR_EXTERNAL;
  338. }
  339. if (ctx->dl->QueryInterface(IID_IDeckLinkAttributes, (void **)&ctx->attr) != S_OK) {
  340. av_log(avctx, AV_LOG_ERROR, "Could not get attributes interface for '%s'\n", name);
  341. ff_decklink_cleanup(avctx);
  342. return AVERROR_EXTERNAL;
  343. }
  344. return 0;
  345. }