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.

317 lines
10KB

  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. int ff_decklink_set_format(AVFormatContext *avctx,
  91. int width, int height,
  92. int tb_num, int tb_den,
  93. decklink_direction_t direction, int num)
  94. {
  95. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  96. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  97. BMDDisplayModeSupport support;
  98. IDeckLinkDisplayModeIterator *itermode;
  99. IDeckLinkDisplayMode *mode;
  100. int i = 1;
  101. HRESULT res;
  102. if (ctx->duplex_mode) {
  103. bool duplex_supported = false;
  104. if (ctx->attr->GetFlag(BMDDeckLinkSupportsDuplexModeConfiguration, &duplex_supported) != S_OK)
  105. duplex_supported = false;
  106. if (duplex_supported) {
  107. res = ctx->cfg->SetInt(bmdDeckLinkConfigDuplexMode, ctx->duplex_mode == 2 ? bmdDuplexModeFull : bmdDuplexModeHalf);
  108. if (res != S_OK)
  109. av_log(avctx, AV_LOG_WARNING, "Setting duplex mode failed.\n");
  110. else
  111. av_log(avctx, AV_LOG_VERBOSE, "Succesfully set duplex mode to %s duplex.\n", ctx->duplex_mode == 2 ? "full" : "half");
  112. } else {
  113. av_log(avctx, AV_LOG_WARNING, "Unable to set duplex mode, because it is not supported.\n");
  114. }
  115. }
  116. if (direction == DIRECTION_IN) {
  117. res = ctx->dli->GetDisplayModeIterator (&itermode);
  118. } else {
  119. res = ctx->dlo->GetDisplayModeIterator (&itermode);
  120. }
  121. if (res!= S_OK) {
  122. av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
  123. return AVERROR(EIO);
  124. }
  125. if (tb_num == 1) {
  126. tb_num *= 1000;
  127. tb_den *= 1000;
  128. }
  129. ctx->bmd_mode = bmdModeUnknown;
  130. while ((ctx->bmd_mode == bmdModeUnknown) && itermode->Next(&mode) == S_OK) {
  131. BMDTimeValue bmd_tb_num, bmd_tb_den;
  132. int bmd_width = mode->GetWidth();
  133. int bmd_height = mode->GetHeight();
  134. mode->GetFrameRate(&bmd_tb_num, &bmd_tb_den);
  135. if ((bmd_width == width && bmd_height == height &&
  136. bmd_tb_num == tb_num && bmd_tb_den == tb_den) || i == num) {
  137. ctx->bmd_mode = mode->GetDisplayMode();
  138. ctx->bmd_width = bmd_width;
  139. ctx->bmd_height = bmd_height;
  140. ctx->bmd_tb_den = bmd_tb_den;
  141. ctx->bmd_tb_num = bmd_tb_num;
  142. ctx->bmd_field_dominance = mode->GetFieldDominance();
  143. av_log(avctx, AV_LOG_INFO, "Found Decklink mode %d x %d with rate %.2f%s\n",
  144. bmd_width, bmd_height, (float)bmd_tb_den/(float)bmd_tb_num,
  145. (ctx->bmd_field_dominance==bmdLowerFieldFirst || ctx->bmd_field_dominance==bmdUpperFieldFirst)?"(i)":"");
  146. }
  147. mode->Release();
  148. i++;
  149. }
  150. itermode->Release();
  151. if (ctx->bmd_mode == bmdModeUnknown)
  152. return -1;
  153. if (direction == DIRECTION_IN) {
  154. if (ctx->dli->DoesSupportVideoMode(ctx->bmd_mode, bmdFormat8BitYUV,
  155. bmdVideoOutputFlagDefault,
  156. &support, NULL) != S_OK)
  157. return -1;
  158. } else {
  159. if (ctx->dlo->DoesSupportVideoMode(ctx->bmd_mode, bmdFormat8BitYUV,
  160. bmdVideoOutputFlagDefault,
  161. &support, NULL) != S_OK)
  162. return -1;
  163. }
  164. if (support == bmdDisplayModeSupported)
  165. return 0;
  166. return -1;
  167. }
  168. int ff_decklink_set_format(AVFormatContext *avctx, decklink_direction_t direction, int num) {
  169. return ff_decklink_set_format(avctx, 0, 0, 0, 0, direction, num);
  170. }
  171. int ff_decklink_list_devices(AVFormatContext *avctx)
  172. {
  173. IDeckLink *dl = NULL;
  174. IDeckLinkIterator *iter = CreateDeckLinkIteratorInstance();
  175. if (!iter) {
  176. av_log(avctx, AV_LOG_ERROR, "Could not create DeckLink iterator\n");
  177. return AVERROR(EIO);
  178. }
  179. av_log(avctx, AV_LOG_INFO, "Blackmagic DeckLink devices:\n");
  180. while (iter->Next(&dl) == S_OK) {
  181. const char *displayName;
  182. ff_decklink_get_display_name(dl, &displayName);
  183. av_log(avctx, AV_LOG_INFO, "\t'%s'\n", displayName);
  184. av_free((void *) displayName);
  185. dl->Release();
  186. }
  187. iter->Release();
  188. return 0;
  189. }
  190. int ff_decklink_list_formats(AVFormatContext *avctx, decklink_direction_t direction)
  191. {
  192. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  193. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  194. IDeckLinkDisplayModeIterator *itermode;
  195. IDeckLinkDisplayMode *mode;
  196. int i=0;
  197. HRESULT res;
  198. if (direction == DIRECTION_IN) {
  199. res = ctx->dli->GetDisplayModeIterator (&itermode);
  200. } else {
  201. res = ctx->dlo->GetDisplayModeIterator (&itermode);
  202. }
  203. if (res!= S_OK) {
  204. av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
  205. return AVERROR(EIO);
  206. }
  207. av_log(avctx, AV_LOG_INFO, "Supported formats for '%s':\n",
  208. avctx->filename);
  209. while (itermode->Next(&mode) == S_OK) {
  210. BMDTimeValue tb_num, tb_den;
  211. mode->GetFrameRate(&tb_num, &tb_den);
  212. av_log(avctx, AV_LOG_INFO, "\t%d\t%ldx%ld at %d/%d fps",
  213. ++i,mode->GetWidth(), mode->GetHeight(),
  214. (int) tb_den, (int) tb_num);
  215. switch (mode->GetFieldDominance()) {
  216. case bmdLowerFieldFirst:
  217. av_log(avctx, AV_LOG_INFO, " (interlaced, lower field first)"); break;
  218. case bmdUpperFieldFirst:
  219. av_log(avctx, AV_LOG_INFO, " (interlaced, upper field first)"); break;
  220. }
  221. av_log(avctx, AV_LOG_INFO, "\n");
  222. mode->Release();
  223. }
  224. itermode->Release();
  225. return 0;
  226. }
  227. void ff_decklink_cleanup(AVFormatContext *avctx)
  228. {
  229. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  230. struct decklink_ctx *ctx = (struct decklink_ctx *) cctx->ctx;
  231. if (ctx->dli)
  232. ctx->dli->Release();
  233. if (ctx->dlo)
  234. ctx->dlo->Release();
  235. if (ctx->attr)
  236. ctx->attr->Release();
  237. if (ctx->cfg)
  238. ctx->cfg->Release();
  239. if (ctx->dl)
  240. ctx->dl->Release();
  241. }
  242. int ff_decklink_init_device(AVFormatContext *avctx, const char* name)
  243. {
  244. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  245. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  246. IDeckLink *dl = NULL;
  247. IDeckLinkIterator *iter = CreateDeckLinkIteratorInstance();
  248. if (!iter) {
  249. av_log(avctx, AV_LOG_ERROR, "Could not create DeckLink iterator\n");
  250. return AVERROR_EXTERNAL;
  251. }
  252. while (iter->Next(&dl) == S_OK) {
  253. const char *displayName;
  254. ff_decklink_get_display_name(dl, &displayName);
  255. if (!strcmp(name, displayName)) {
  256. av_free((void *)displayName);
  257. ctx->dl = dl;
  258. break;
  259. }
  260. av_free((void *)displayName);
  261. dl->Release();
  262. }
  263. iter->Release();
  264. if (!ctx->dl)
  265. return AVERROR(ENXIO);
  266. if (ctx->dl->QueryInterface(IID_IDeckLinkConfiguration, (void **)&ctx->cfg) != S_OK) {
  267. av_log(avctx, AV_LOG_ERROR, "Could not get configuration interface for '%s'\n", name);
  268. ff_decklink_cleanup(avctx);
  269. return AVERROR_EXTERNAL;
  270. }
  271. if (ctx->dl->QueryInterface(IID_IDeckLinkAttributes, (void **)&ctx->attr) != S_OK) {
  272. av_log(avctx, AV_LOG_ERROR, "Could not get attributes interface for '%s'\n", name);
  273. ff_decklink_cleanup(avctx);
  274. return AVERROR_EXTERNAL;
  275. }
  276. return 0;
  277. }