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.

232 lines
7.3KB

  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. #else
  65. #define DECKLINK_STR const char *
  66. #define DECKLINK_STRDUP av_strdup
  67. /* free() is needed for a string returned by the DeckLink SDL. */
  68. #define DECKLINK_FREE(s) free((void *) s)
  69. #endif
  70. HRESULT ff_decklink_get_display_name(IDeckLink *This, const char **displayName)
  71. {
  72. DECKLINK_STR tmpDisplayName;
  73. HRESULT hr = This->GetDisplayName(&tmpDisplayName);
  74. if (hr != S_OK)
  75. return hr;
  76. *displayName = DECKLINK_STRDUP(tmpDisplayName);
  77. DECKLINK_FREE(tmpDisplayName);
  78. return hr;
  79. }
  80. int ff_decklink_set_format(AVFormatContext *avctx,
  81. int width, int height,
  82. int tb_num, int tb_den,
  83. decklink_direction_t direction, int num)
  84. {
  85. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  86. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  87. BMDDisplayModeSupport support;
  88. IDeckLinkDisplayModeIterator *itermode;
  89. IDeckLinkDisplayMode *mode;
  90. int i = 1;
  91. HRESULT res;
  92. if (direction == DIRECTION_IN) {
  93. res = ctx->dli->GetDisplayModeIterator (&itermode);
  94. } else {
  95. res = ctx->dlo->GetDisplayModeIterator (&itermode);
  96. }
  97. if (res!= S_OK) {
  98. av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
  99. return AVERROR(EIO);
  100. }
  101. if (tb_num == 1) {
  102. tb_num *= 1000;
  103. tb_den *= 1000;
  104. }
  105. ctx->bmd_mode = bmdModeUnknown;
  106. while ((ctx->bmd_mode == bmdModeUnknown) && itermode->Next(&mode) == S_OK) {
  107. BMDTimeValue bmd_tb_num, bmd_tb_den;
  108. int bmd_width = mode->GetWidth();
  109. int bmd_height = mode->GetHeight();
  110. mode->GetFrameRate(&bmd_tb_num, &bmd_tb_den);
  111. if ((bmd_width == width && bmd_height == height &&
  112. bmd_tb_num == tb_num && bmd_tb_den == tb_den) || i == num) {
  113. ctx->bmd_mode = mode->GetDisplayMode();
  114. ctx->bmd_width = bmd_width;
  115. ctx->bmd_height = bmd_height;
  116. ctx->bmd_tb_den = bmd_tb_den;
  117. ctx->bmd_tb_num = bmd_tb_num;
  118. ctx->bmd_field_dominance = mode->GetFieldDominance();
  119. av_log(avctx, AV_LOG_INFO, "Found Decklink mode %d x %d with rate %.2f%s\n",
  120. bmd_width, bmd_height, (float)bmd_tb_den/(float)bmd_tb_num,
  121. (ctx->bmd_field_dominance==bmdLowerFieldFirst || ctx->bmd_field_dominance==bmdUpperFieldFirst)?"(i)":"");
  122. }
  123. mode->Release();
  124. i++;
  125. }
  126. itermode->Release();
  127. if (ctx->bmd_mode == bmdModeUnknown)
  128. return -1;
  129. if (direction == DIRECTION_IN) {
  130. if (ctx->dli->DoesSupportVideoMode(ctx->bmd_mode, bmdFormat8BitYUV,
  131. bmdVideoOutputFlagDefault,
  132. &support, NULL) != S_OK)
  133. return -1;
  134. } else {
  135. if (ctx->dlo->DoesSupportVideoMode(ctx->bmd_mode, bmdFormat8BitYUV,
  136. bmdVideoOutputFlagDefault,
  137. &support, NULL) != S_OK)
  138. return -1;
  139. }
  140. if (support == bmdDisplayModeSupported)
  141. return 0;
  142. return -1;
  143. }
  144. int ff_decklink_set_format(AVFormatContext *avctx, decklink_direction_t direction, int num) {
  145. return ff_decklink_set_format(avctx, 0, 0, 0, 0, direction, num);
  146. }
  147. int ff_decklink_list_devices(AVFormatContext *avctx)
  148. {
  149. IDeckLink *dl = NULL;
  150. IDeckLinkIterator *iter = CreateDeckLinkIteratorInstance();
  151. if (!iter) {
  152. av_log(avctx, AV_LOG_ERROR, "Could not create DeckLink iterator\n");
  153. return AVERROR(EIO);
  154. }
  155. av_log(avctx, AV_LOG_INFO, "Blackmagic DeckLink devices:\n");
  156. while (iter->Next(&dl) == S_OK) {
  157. const char *displayName;
  158. ff_decklink_get_display_name(dl, &displayName);
  159. av_log(avctx, AV_LOG_INFO, "\t'%s'\n", displayName);
  160. av_free((void *) displayName);
  161. dl->Release();
  162. }
  163. iter->Release();
  164. return 0;
  165. }
  166. int ff_decklink_list_formats(AVFormatContext *avctx, decklink_direction_t direction)
  167. {
  168. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  169. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  170. IDeckLinkDisplayModeIterator *itermode;
  171. IDeckLinkDisplayMode *mode;
  172. int i=0;
  173. HRESULT res;
  174. if (direction == DIRECTION_IN) {
  175. res = ctx->dli->GetDisplayModeIterator (&itermode);
  176. } else {
  177. res = ctx->dlo->GetDisplayModeIterator (&itermode);
  178. }
  179. if (res!= S_OK) {
  180. av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
  181. return AVERROR(EIO);
  182. }
  183. av_log(avctx, AV_LOG_INFO, "Supported formats for '%s':\n",
  184. avctx->filename);
  185. while (itermode->Next(&mode) == S_OK) {
  186. BMDTimeValue tb_num, tb_den;
  187. mode->GetFrameRate(&tb_num, &tb_den);
  188. av_log(avctx, AV_LOG_INFO, "\t%d\t%ldx%ld at %d/%d fps",
  189. ++i,mode->GetWidth(), mode->GetHeight(),
  190. (int) tb_den, (int) tb_num);
  191. switch (mode->GetFieldDominance()) {
  192. case bmdLowerFieldFirst:
  193. av_log(avctx, AV_LOG_INFO, " (interlaced, lower field first)"); break;
  194. case bmdUpperFieldFirst:
  195. av_log(avctx, AV_LOG_INFO, " (interlaced, upper field first)"); break;
  196. }
  197. av_log(avctx, AV_LOG_INFO, "\n");
  198. mode->Release();
  199. }
  200. itermode->Release();
  201. return 0;
  202. }