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.

360 lines
12KB

  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. #elif defined(__APPLE__)
  65. static char *dup_cfstring_to_utf8(CFStringRef w)
  66. {
  67. char s[256];
  68. CFStringGetCString(w, s, 255, kCFStringEncodingUTF8);
  69. return av_strdup(s);
  70. }
  71. #define DECKLINK_STR const __CFString *
  72. #define DECKLINK_STRDUP dup_cfstring_to_utf8
  73. #define DECKLINK_FREE(s) free((void *) s)
  74. #else
  75. #define DECKLINK_STR const char *
  76. #define DECKLINK_STRDUP av_strdup
  77. /* free() is needed for a string returned by the DeckLink SDL. */
  78. #define DECKLINK_FREE(s) free((void *) s)
  79. #endif
  80. HRESULT ff_decklink_get_display_name(IDeckLink *This, const char **displayName)
  81. {
  82. DECKLINK_STR tmpDisplayName;
  83. HRESULT hr = This->GetDisplayName(&tmpDisplayName);
  84. if (hr != S_OK)
  85. return hr;
  86. *displayName = DECKLINK_STRDUP(tmpDisplayName);
  87. DECKLINK_FREE(tmpDisplayName);
  88. return hr;
  89. }
  90. static int decklink_select_input(AVFormatContext *avctx, BMDDeckLinkConfigurationID cfg_id)
  91. {
  92. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  93. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  94. BMDDeckLinkAttributeID attr_id = (cfg_id == bmdDeckLinkConfigAudioInputConnection) ? BMDDeckLinkAudioInputConnections : BMDDeckLinkVideoInputConnections;
  95. int64_t bmd_input = (cfg_id == bmdDeckLinkConfigAudioInputConnection) ? ctx->audio_input : ctx->video_input;
  96. const char *type_name = (cfg_id == bmdDeckLinkConfigAudioInputConnection) ? "audio" : "video";
  97. int64_t supported_connections = 0;
  98. HRESULT res;
  99. if (bmd_input) {
  100. res = ctx->attr->GetInt(attr_id, &supported_connections);
  101. if (res != S_OK) {
  102. av_log(avctx, AV_LOG_ERROR, "Failed to query supported %s inputs.\n", type_name);
  103. return AVERROR_EXTERNAL;
  104. }
  105. if ((supported_connections & bmd_input) != bmd_input) {
  106. av_log(avctx, AV_LOG_ERROR, "Device does not support selected %s input.\n", type_name);
  107. return AVERROR(ENOSYS);
  108. }
  109. res = ctx->cfg->SetInt(cfg_id, bmd_input);
  110. if (res != S_OK) {
  111. av_log(avctx, AV_LOG_ERROR, "Failed to select %s input.\n", type_name);
  112. return AVERROR_EXTERNAL;
  113. }
  114. }
  115. return 0;
  116. }
  117. int ff_decklink_set_format(AVFormatContext *avctx,
  118. int width, int height,
  119. int tb_num, int tb_den,
  120. decklink_direction_t direction, int num)
  121. {
  122. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  123. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  124. BMDDisplayModeSupport support;
  125. IDeckLinkDisplayModeIterator *itermode;
  126. IDeckLinkDisplayMode *mode;
  127. int i = 1;
  128. HRESULT res;
  129. if (ctx->duplex_mode) {
  130. bool duplex_supported = false;
  131. if (ctx->attr->GetFlag(BMDDeckLinkSupportsDuplexModeConfiguration, &duplex_supported) != S_OK)
  132. duplex_supported = false;
  133. if (duplex_supported) {
  134. res = ctx->cfg->SetInt(bmdDeckLinkConfigDuplexMode, ctx->duplex_mode == 2 ? bmdDuplexModeFull : bmdDuplexModeHalf);
  135. if (res != S_OK)
  136. av_log(avctx, AV_LOG_WARNING, "Setting duplex mode failed.\n");
  137. else
  138. av_log(avctx, AV_LOG_VERBOSE, "Succesfully set duplex mode to %s duplex.\n", ctx->duplex_mode == 2 ? "full" : "half");
  139. } else {
  140. av_log(avctx, AV_LOG_WARNING, "Unable to set duplex mode, because it is not supported.\n");
  141. }
  142. }
  143. if (direction == DIRECTION_IN) {
  144. int ret;
  145. ret = decklink_select_input(avctx, bmdDeckLinkConfigAudioInputConnection);
  146. if (ret < 0)
  147. return ret;
  148. ret = decklink_select_input(avctx, bmdDeckLinkConfigVideoInputConnection);
  149. if (ret < 0)
  150. return ret;
  151. res = ctx->dli->GetDisplayModeIterator (&itermode);
  152. } else {
  153. res = ctx->dlo->GetDisplayModeIterator (&itermode);
  154. }
  155. if (res!= S_OK) {
  156. av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
  157. return AVERROR(EIO);
  158. }
  159. if (tb_num == 1) {
  160. tb_num *= 1000;
  161. tb_den *= 1000;
  162. }
  163. ctx->bmd_mode = bmdModeUnknown;
  164. while ((ctx->bmd_mode == bmdModeUnknown) && itermode->Next(&mode) == S_OK) {
  165. BMDTimeValue bmd_tb_num, bmd_tb_den;
  166. int bmd_width = mode->GetWidth();
  167. int bmd_height = mode->GetHeight();
  168. mode->GetFrameRate(&bmd_tb_num, &bmd_tb_den);
  169. if ((bmd_width == width && bmd_height == height &&
  170. bmd_tb_num == tb_num && bmd_tb_den == tb_den) || i == num) {
  171. ctx->bmd_mode = mode->GetDisplayMode();
  172. ctx->bmd_width = bmd_width;
  173. ctx->bmd_height = bmd_height;
  174. ctx->bmd_tb_den = bmd_tb_den;
  175. ctx->bmd_tb_num = bmd_tb_num;
  176. ctx->bmd_field_dominance = mode->GetFieldDominance();
  177. av_log(avctx, AV_LOG_INFO, "Found Decklink mode %d x %d with rate %.2f%s\n",
  178. bmd_width, bmd_height, (float)bmd_tb_den/(float)bmd_tb_num,
  179. (ctx->bmd_field_dominance==bmdLowerFieldFirst || ctx->bmd_field_dominance==bmdUpperFieldFirst)?"(i)":"");
  180. }
  181. mode->Release();
  182. i++;
  183. }
  184. itermode->Release();
  185. if (ctx->bmd_mode == bmdModeUnknown)
  186. return -1;
  187. if (direction == DIRECTION_IN) {
  188. if (ctx->dli->DoesSupportVideoMode(ctx->bmd_mode, bmdFormat8BitYUV,
  189. bmdVideoOutputFlagDefault,
  190. &support, NULL) != S_OK)
  191. return -1;
  192. } else {
  193. if (ctx->dlo->DoesSupportVideoMode(ctx->bmd_mode, bmdFormat8BitYUV,
  194. bmdVideoOutputFlagDefault,
  195. &support, NULL) != S_OK)
  196. return -1;
  197. }
  198. if (support == bmdDisplayModeSupported)
  199. return 0;
  200. return -1;
  201. }
  202. int ff_decklink_set_format(AVFormatContext *avctx, decklink_direction_t direction, int num) {
  203. return ff_decklink_set_format(avctx, 0, 0, 0, 0, direction, num);
  204. }
  205. int ff_decklink_list_devices(AVFormatContext *avctx)
  206. {
  207. IDeckLink *dl = NULL;
  208. IDeckLinkIterator *iter = CreateDeckLinkIteratorInstance();
  209. if (!iter) {
  210. av_log(avctx, AV_LOG_ERROR, "Could not create DeckLink iterator\n");
  211. return AVERROR(EIO);
  212. }
  213. av_log(avctx, AV_LOG_INFO, "Blackmagic DeckLink devices:\n");
  214. while (iter->Next(&dl) == S_OK) {
  215. const char *displayName;
  216. ff_decklink_get_display_name(dl, &displayName);
  217. av_log(avctx, AV_LOG_INFO, "\t'%s'\n", displayName);
  218. av_free((void *) displayName);
  219. dl->Release();
  220. }
  221. iter->Release();
  222. return 0;
  223. }
  224. int ff_decklink_list_formats(AVFormatContext *avctx, decklink_direction_t direction)
  225. {
  226. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  227. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  228. IDeckLinkDisplayModeIterator *itermode;
  229. IDeckLinkDisplayMode *mode;
  230. int i=0;
  231. HRESULT res;
  232. if (direction == DIRECTION_IN) {
  233. int ret;
  234. ret = decklink_select_input(avctx, bmdDeckLinkConfigAudioInputConnection);
  235. if (ret < 0)
  236. return ret;
  237. ret = decklink_select_input(avctx, bmdDeckLinkConfigVideoInputConnection);
  238. if (ret < 0)
  239. return ret;
  240. res = ctx->dli->GetDisplayModeIterator (&itermode);
  241. } else {
  242. res = ctx->dlo->GetDisplayModeIterator (&itermode);
  243. }
  244. if (res!= S_OK) {
  245. av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
  246. return AVERROR(EIO);
  247. }
  248. av_log(avctx, AV_LOG_INFO, "Supported formats for '%s':\n",
  249. avctx->filename);
  250. while (itermode->Next(&mode) == S_OK) {
  251. BMDTimeValue tb_num, tb_den;
  252. mode->GetFrameRate(&tb_num, &tb_den);
  253. av_log(avctx, AV_LOG_INFO, "\t%d\t%ldx%ld at %d/%d fps",
  254. ++i,mode->GetWidth(), mode->GetHeight(),
  255. (int) tb_den, (int) tb_num);
  256. switch (mode->GetFieldDominance()) {
  257. case bmdLowerFieldFirst:
  258. av_log(avctx, AV_LOG_INFO, " (interlaced, lower field first)"); break;
  259. case bmdUpperFieldFirst:
  260. av_log(avctx, AV_LOG_INFO, " (interlaced, upper field first)"); break;
  261. }
  262. av_log(avctx, AV_LOG_INFO, "\n");
  263. mode->Release();
  264. }
  265. itermode->Release();
  266. return 0;
  267. }
  268. void ff_decklink_cleanup(AVFormatContext *avctx)
  269. {
  270. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  271. struct decklink_ctx *ctx = (struct decklink_ctx *) cctx->ctx;
  272. if (ctx->dli)
  273. ctx->dli->Release();
  274. if (ctx->dlo)
  275. ctx->dlo->Release();
  276. if (ctx->attr)
  277. ctx->attr->Release();
  278. if (ctx->cfg)
  279. ctx->cfg->Release();
  280. if (ctx->dl)
  281. ctx->dl->Release();
  282. }
  283. int ff_decklink_init_device(AVFormatContext *avctx, const char* name)
  284. {
  285. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  286. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  287. IDeckLink *dl = NULL;
  288. IDeckLinkIterator *iter = CreateDeckLinkIteratorInstance();
  289. if (!iter) {
  290. av_log(avctx, AV_LOG_ERROR, "Could not create DeckLink iterator\n");
  291. return AVERROR_EXTERNAL;
  292. }
  293. while (iter->Next(&dl) == S_OK) {
  294. const char *displayName;
  295. ff_decklink_get_display_name(dl, &displayName);
  296. if (!strcmp(name, displayName)) {
  297. av_free((void *)displayName);
  298. ctx->dl = dl;
  299. break;
  300. }
  301. av_free((void *)displayName);
  302. dl->Release();
  303. }
  304. iter->Release();
  305. if (!ctx->dl)
  306. return AVERROR(ENXIO);
  307. if (ctx->dl->QueryInterface(IID_IDeckLinkConfiguration, (void **)&ctx->cfg) != S_OK) {
  308. av_log(avctx, AV_LOG_ERROR, "Could not get configuration interface for '%s'\n", name);
  309. ff_decklink_cleanup(avctx);
  310. return AVERROR_EXTERNAL;
  311. }
  312. if (ctx->dl->QueryInterface(IID_IDeckLinkAttributes, (void **)&ctx->attr) != S_OK) {
  313. av_log(avctx, AV_LOG_ERROR, "Could not get attributes interface for '%s'\n", name);
  314. ff_decklink_cleanup(avctx);
  315. return AVERROR_EXTERNAL;
  316. }
  317. return 0;
  318. }