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.

238 lines
7.5KB

  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/internal.h"
  29. #include "libavutil/imgutils.h"
  30. }
  31. #include "decklink_common.h"
  32. #ifdef _WIN32
  33. IDeckLinkIterator *CreateDeckLinkIteratorInstance(void)
  34. {
  35. IDeckLinkIterator *iter;
  36. if (CoInitialize(NULL) < 0) {
  37. av_log(NULL, AV_LOG_ERROR, "COM initialization failed.\n");
  38. return NULL;
  39. }
  40. if (CoCreateInstance(CLSID_CDeckLinkIterator, NULL, CLSCTX_ALL,
  41. IID_IDeckLinkIterator, (void**) &iter) != S_OK) {
  42. av_log(NULL, AV_LOG_ERROR, "DeckLink drivers not installed.\n");
  43. return NULL;
  44. }
  45. return iter;
  46. }
  47. #endif
  48. #ifdef _WIN32
  49. static char *dup_wchar_to_utf8(wchar_t *w)
  50. {
  51. char *s = NULL;
  52. int l = WideCharToMultiByte(CP_UTF8, 0, w, -1, 0, 0, 0, 0);
  53. s = (char *) av_malloc(l);
  54. if (s)
  55. WideCharToMultiByte(CP_UTF8, 0, w, -1, s, l, 0, 0);
  56. return s;
  57. }
  58. #define DECKLINK_STR OLECHAR *
  59. #define DECKLINK_STRDUP dup_wchar_to_utf8
  60. #define DECKLINK_FREE(s) SysFreeString(s)
  61. #elif defined(__APPLE__)
  62. static char *dup_cfstring_to_utf8(CFStringRef w)
  63. {
  64. char s[256];
  65. CFStringGetCString(w, s, 255, kCFStringEncodingUTF8);
  66. return av_strdup(s);
  67. }
  68. #define DECKLINK_STR const __CFString *
  69. #define DECKLINK_STRDUP dup_cfstring_to_utf8
  70. #define DECKLINK_FREE(s) free((void *) s)
  71. #else
  72. #define DECKLINK_STR const char *
  73. #define DECKLINK_STRDUP av_strdup
  74. /* free() is needed for a string returned by the DeckLink SDL. */
  75. #define DECKLINK_FREE(s) free((void *) s)
  76. #endif
  77. HRESULT ff_decklink_get_display_name(IDeckLink *This, const char **displayName)
  78. {
  79. DECKLINK_STR tmpDisplayName;
  80. HRESULT hr = This->GetDisplayName(&tmpDisplayName);
  81. if (hr != S_OK)
  82. return hr;
  83. *displayName = DECKLINK_STRDUP(tmpDisplayName);
  84. DECKLINK_FREE(tmpDisplayName);
  85. return hr;
  86. }
  87. int ff_decklink_set_format(AVFormatContext *avctx,
  88. int width, int height,
  89. int tb_num, int tb_den,
  90. decklink_direction_t direction, int num)
  91. {
  92. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  93. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  94. BMDDisplayModeSupport support;
  95. IDeckLinkDisplayModeIterator *itermode;
  96. IDeckLinkDisplayMode *mode;
  97. int i = 1;
  98. HRESULT res;
  99. if (direction == DIRECTION_IN) {
  100. res = ctx->dli->GetDisplayModeIterator (&itermode);
  101. } else {
  102. res = ctx->dlo->GetDisplayModeIterator (&itermode);
  103. }
  104. if (res!= S_OK) {
  105. av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
  106. return AVERROR(EIO);
  107. }
  108. if (tb_num == 1) {
  109. tb_num *= 1000;
  110. tb_den *= 1000;
  111. }
  112. ctx->bmd_mode = bmdModeUnknown;
  113. while ((ctx->bmd_mode == bmdModeUnknown) && itermode->Next(&mode) == S_OK) {
  114. BMDTimeValue bmd_tb_num, bmd_tb_den;
  115. int bmd_width = mode->GetWidth();
  116. int bmd_height = mode->GetHeight();
  117. mode->GetFrameRate(&bmd_tb_num, &bmd_tb_den);
  118. if ((bmd_width == width && bmd_height == height &&
  119. bmd_tb_num == tb_num && bmd_tb_den == tb_den) || i == num) {
  120. ctx->bmd_mode = mode->GetDisplayMode();
  121. ctx->bmd_width = bmd_width;
  122. ctx->bmd_height = bmd_height;
  123. ctx->bmd_tb_den = bmd_tb_den;
  124. ctx->bmd_tb_num = bmd_tb_num;
  125. ctx->bmd_field_dominance = mode->GetFieldDominance();
  126. av_log(avctx, AV_LOG_INFO, "Found Decklink mode %d x %d with rate %.2f%s\n",
  127. bmd_width, bmd_height, (float)bmd_tb_den/(float)bmd_tb_num,
  128. (ctx->bmd_field_dominance==bmdLowerFieldFirst || ctx->bmd_field_dominance==bmdUpperFieldFirst)?"(i)":"");
  129. }
  130. mode->Release();
  131. i++;
  132. }
  133. itermode->Release();
  134. if (ctx->bmd_mode == bmdModeUnknown)
  135. return -1;
  136. if (direction == DIRECTION_IN) {
  137. if (ctx->dli->DoesSupportVideoMode(ctx->bmd_mode, bmdFormat8BitYUV,
  138. bmdVideoOutputFlagDefault,
  139. &support, NULL) != S_OK)
  140. return -1;
  141. } else {
  142. if (ctx->dlo->DoesSupportVideoMode(ctx->bmd_mode, bmdFormat8BitYUV,
  143. bmdVideoOutputFlagDefault,
  144. &support, NULL) != S_OK)
  145. return -1;
  146. }
  147. if (support == bmdDisplayModeSupported)
  148. return 0;
  149. return -1;
  150. }
  151. int ff_decklink_set_format(AVFormatContext *avctx, decklink_direction_t direction, int num) {
  152. return ff_decklink_set_format(avctx, 0, 0, 0, 0, direction, num);
  153. }
  154. int ff_decklink_list_devices(AVFormatContext *avctx)
  155. {
  156. IDeckLink *dl = NULL;
  157. IDeckLinkIterator *iter = CreateDeckLinkIteratorInstance();
  158. if (!iter) {
  159. av_log(avctx, AV_LOG_ERROR, "Could not create DeckLink iterator\n");
  160. return AVERROR(EIO);
  161. }
  162. av_log(avctx, AV_LOG_INFO, "Blackmagic DeckLink devices:\n");
  163. while (iter->Next(&dl) == S_OK) {
  164. const char *displayName;
  165. ff_decklink_get_display_name(dl, &displayName);
  166. av_log(avctx, AV_LOG_INFO, "\t'%s'\n", displayName);
  167. av_free((void *) displayName);
  168. dl->Release();
  169. }
  170. iter->Release();
  171. return 0;
  172. }
  173. int ff_decklink_list_formats(AVFormatContext *avctx, decklink_direction_t direction)
  174. {
  175. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  176. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  177. IDeckLinkDisplayModeIterator *itermode;
  178. IDeckLinkDisplayMode *mode;
  179. int i=0;
  180. HRESULT res;
  181. if (direction == DIRECTION_IN) {
  182. res = ctx->dli->GetDisplayModeIterator (&itermode);
  183. } else {
  184. res = ctx->dlo->GetDisplayModeIterator (&itermode);
  185. }
  186. if (res!= S_OK) {
  187. av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
  188. return AVERROR(EIO);
  189. }
  190. av_log(avctx, AV_LOG_INFO, "Supported formats for '%s':\n",
  191. avctx->filename);
  192. while (itermode->Next(&mode) == S_OK) {
  193. BMDTimeValue tb_num, tb_den;
  194. mode->GetFrameRate(&tb_num, &tb_den);
  195. av_log(avctx, AV_LOG_INFO, "\t%d\t%ldx%ld at %d/%d fps",
  196. ++i,mode->GetWidth(), mode->GetHeight(),
  197. (int) tb_den, (int) tb_num);
  198. switch (mode->GetFieldDominance()) {
  199. case bmdLowerFieldFirst:
  200. av_log(avctx, AV_LOG_INFO, " (interlaced, lower field first)"); break;
  201. case bmdUpperFieldFirst:
  202. av_log(avctx, AV_LOG_INFO, " (interlaced, upper field first)"); break;
  203. }
  204. av_log(avctx, AV_LOG_INFO, "\n");
  205. mode->Release();
  206. }
  207. itermode->Release();
  208. return 0;
  209. }