decklink_common.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 (direction == DIRECTION_IN) {
  103. res = ctx->dli->GetDisplayModeIterator (&itermode);
  104. } else {
  105. res = ctx->dlo->GetDisplayModeIterator (&itermode);
  106. }
  107. if (res!= S_OK) {
  108. av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
  109. return AVERROR(EIO);
  110. }
  111. if (tb_num == 1) {
  112. tb_num *= 1000;
  113. tb_den *= 1000;
  114. }
  115. ctx->bmd_mode = bmdModeUnknown;
  116. while ((ctx->bmd_mode == bmdModeUnknown) && itermode->Next(&mode) == S_OK) {
  117. BMDTimeValue bmd_tb_num, bmd_tb_den;
  118. int bmd_width = mode->GetWidth();
  119. int bmd_height = mode->GetHeight();
  120. mode->GetFrameRate(&bmd_tb_num, &bmd_tb_den);
  121. if ((bmd_width == width && bmd_height == height &&
  122. bmd_tb_num == tb_num && bmd_tb_den == tb_den) || i == num) {
  123. ctx->bmd_mode = mode->GetDisplayMode();
  124. ctx->bmd_width = bmd_width;
  125. ctx->bmd_height = bmd_height;
  126. ctx->bmd_tb_den = bmd_tb_den;
  127. ctx->bmd_tb_num = bmd_tb_num;
  128. ctx->bmd_field_dominance = mode->GetFieldDominance();
  129. av_log(avctx, AV_LOG_INFO, "Found Decklink mode %d x %d with rate %.2f%s\n",
  130. bmd_width, bmd_height, (float)bmd_tb_den/(float)bmd_tb_num,
  131. (ctx->bmd_field_dominance==bmdLowerFieldFirst || ctx->bmd_field_dominance==bmdUpperFieldFirst)?"(i)":"");
  132. }
  133. mode->Release();
  134. i++;
  135. }
  136. itermode->Release();
  137. if (ctx->bmd_mode == bmdModeUnknown)
  138. return -1;
  139. if (direction == DIRECTION_IN) {
  140. if (ctx->dli->DoesSupportVideoMode(ctx->bmd_mode, bmdFormat8BitYUV,
  141. bmdVideoOutputFlagDefault,
  142. &support, NULL) != S_OK)
  143. return -1;
  144. } else {
  145. if (ctx->dlo->DoesSupportVideoMode(ctx->bmd_mode, bmdFormat8BitYUV,
  146. bmdVideoOutputFlagDefault,
  147. &support, NULL) != S_OK)
  148. return -1;
  149. }
  150. if (support == bmdDisplayModeSupported)
  151. return 0;
  152. return -1;
  153. }
  154. int ff_decklink_set_format(AVFormatContext *avctx, decklink_direction_t direction, int num) {
  155. return ff_decklink_set_format(avctx, 0, 0, 0, 0, direction, num);
  156. }
  157. int ff_decklink_list_devices(AVFormatContext *avctx)
  158. {
  159. IDeckLink *dl = NULL;
  160. IDeckLinkIterator *iter = CreateDeckLinkIteratorInstance();
  161. if (!iter) {
  162. av_log(avctx, AV_LOG_ERROR, "Could not create DeckLink iterator\n");
  163. return AVERROR(EIO);
  164. }
  165. av_log(avctx, AV_LOG_INFO, "Blackmagic DeckLink devices:\n");
  166. while (iter->Next(&dl) == S_OK) {
  167. const char *displayName;
  168. ff_decklink_get_display_name(dl, &displayName);
  169. av_log(avctx, AV_LOG_INFO, "\t'%s'\n", displayName);
  170. av_free((void *) displayName);
  171. dl->Release();
  172. }
  173. iter->Release();
  174. return 0;
  175. }
  176. int ff_decklink_list_formats(AVFormatContext *avctx, decklink_direction_t direction)
  177. {
  178. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  179. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  180. IDeckLinkDisplayModeIterator *itermode;
  181. IDeckLinkDisplayMode *mode;
  182. int i=0;
  183. HRESULT res;
  184. if (direction == DIRECTION_IN) {
  185. res = ctx->dli->GetDisplayModeIterator (&itermode);
  186. } else {
  187. res = ctx->dlo->GetDisplayModeIterator (&itermode);
  188. }
  189. if (res!= S_OK) {
  190. av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
  191. return AVERROR(EIO);
  192. }
  193. av_log(avctx, AV_LOG_INFO, "Supported formats for '%s':\n",
  194. avctx->filename);
  195. while (itermode->Next(&mode) == S_OK) {
  196. BMDTimeValue tb_num, tb_den;
  197. mode->GetFrameRate(&tb_num, &tb_den);
  198. av_log(avctx, AV_LOG_INFO, "\t%d\t%ldx%ld at %d/%d fps",
  199. ++i,mode->GetWidth(), mode->GetHeight(),
  200. (int) tb_den, (int) tb_num);
  201. switch (mode->GetFieldDominance()) {
  202. case bmdLowerFieldFirst:
  203. av_log(avctx, AV_LOG_INFO, " (interlaced, lower field first)"); break;
  204. case bmdUpperFieldFirst:
  205. av_log(avctx, AV_LOG_INFO, " (interlaced, upper field first)"); break;
  206. }
  207. av_log(avctx, AV_LOG_INFO, "\n");
  208. mode->Release();
  209. }
  210. itermode->Release();
  211. return 0;
  212. }