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.

230 lines
7.2KB

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