decklink_common.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 internal.h first to avoid conflict between winsock.h (used by
  22. * DeckLink headers) and winsock2.h (used by libavformat) in MSVC++ builds */
  23. extern "C" {
  24. #include "libavformat/internal.h"
  25. }
  26. #include <DeckLinkAPI.h>
  27. #ifdef _WIN32
  28. #include <DeckLinkAPI_i.c>
  29. #else
  30. #include <DeckLinkAPIDispatch.cpp>
  31. #endif
  32. extern "C" {
  33. #include "libavformat/avformat.h"
  34. #include "libavutil/imgutils.h"
  35. #include "libavutil/intreadwrite.h"
  36. #include "libavutil/bswap.h"
  37. #include "avdevice.h"
  38. }
  39. #include "decklink_common.h"
  40. #ifdef _WIN32
  41. IDeckLinkIterator *CreateDeckLinkIteratorInstance(void)
  42. {
  43. IDeckLinkIterator *iter;
  44. if (CoInitialize(NULL) < 0) {
  45. av_log(NULL, AV_LOG_ERROR, "COM initialization failed.\n");
  46. return NULL;
  47. }
  48. if (CoCreateInstance(CLSID_CDeckLinkIterator, NULL, CLSCTX_ALL,
  49. IID_IDeckLinkIterator, (void**) &iter) != S_OK) {
  50. av_log(NULL, AV_LOG_ERROR, "DeckLink drivers not installed.\n");
  51. return NULL;
  52. }
  53. return iter;
  54. }
  55. #endif
  56. #ifdef _WIN32
  57. static char *dup_wchar_to_utf8(wchar_t *w)
  58. {
  59. char *s = NULL;
  60. int l = WideCharToMultiByte(CP_UTF8, 0, w, -1, 0, 0, 0, 0);
  61. s = (char *) av_malloc(l);
  62. if (s)
  63. WideCharToMultiByte(CP_UTF8, 0, w, -1, s, l, 0, 0);
  64. return s;
  65. }
  66. #define DECKLINK_STR OLECHAR *
  67. #define DECKLINK_STRDUP dup_wchar_to_utf8
  68. #define DECKLINK_FREE(s) SysFreeString(s)
  69. #define DECKLINK_BOOL BOOL
  70. #elif defined(__APPLE__)
  71. static char *dup_cfstring_to_utf8(CFStringRef w)
  72. {
  73. char s[256];
  74. CFStringGetCString(w, s, 255, kCFStringEncodingUTF8);
  75. return av_strdup(s);
  76. }
  77. #define DECKLINK_STR const __CFString *
  78. #define DECKLINK_STRDUP dup_cfstring_to_utf8
  79. #define DECKLINK_FREE(s) CFRelease(s)
  80. #define DECKLINK_BOOL bool
  81. #else
  82. #define DECKLINK_STR const char *
  83. #define DECKLINK_STRDUP av_strdup
  84. /* free() is needed for a string returned by the DeckLink SDL. */
  85. #define DECKLINK_FREE(s) free((void *) s)
  86. #define DECKLINK_BOOL bool
  87. #endif
  88. HRESULT ff_decklink_get_display_name(IDeckLink *This, const char **displayName)
  89. {
  90. DECKLINK_STR tmpDisplayName;
  91. HRESULT hr = This->GetDisplayName(&tmpDisplayName);
  92. if (hr != S_OK)
  93. return hr;
  94. *displayName = DECKLINK_STRDUP(tmpDisplayName);
  95. DECKLINK_FREE(tmpDisplayName);
  96. return hr;
  97. }
  98. static int decklink_select_input(AVFormatContext *avctx, BMDDeckLinkConfigurationID cfg_id)
  99. {
  100. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  101. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  102. BMDDeckLinkAttributeID attr_id = (cfg_id == bmdDeckLinkConfigAudioInputConnection) ? BMDDeckLinkAudioInputConnections : BMDDeckLinkVideoInputConnections;
  103. int64_t bmd_input = (cfg_id == bmdDeckLinkConfigAudioInputConnection) ? (int64_t)ctx->audio_input : (int64_t)ctx->video_input;
  104. const char *type_name = (cfg_id == bmdDeckLinkConfigAudioInputConnection) ? "audio" : "video";
  105. int64_t supported_connections = 0;
  106. HRESULT res;
  107. if (bmd_input) {
  108. res = ctx->attr->GetInt(attr_id, &supported_connections);
  109. if (res != S_OK) {
  110. av_log(avctx, AV_LOG_ERROR, "Failed to query supported %s inputs.\n", type_name);
  111. return AVERROR_EXTERNAL;
  112. }
  113. if ((supported_connections & bmd_input) != bmd_input) {
  114. av_log(avctx, AV_LOG_ERROR, "Device does not support selected %s input.\n", type_name);
  115. return AVERROR(ENOSYS);
  116. }
  117. res = ctx->cfg->SetInt(cfg_id, bmd_input);
  118. if (res != S_OK) {
  119. av_log(avctx, AV_LOG_ERROR, "Failed to select %s input.\n", type_name);
  120. return AVERROR_EXTERNAL;
  121. }
  122. }
  123. return 0;
  124. }
  125. static DECKLINK_BOOL field_order_eq(enum AVFieldOrder field_order, BMDFieldDominance bmd_field_order)
  126. {
  127. if (field_order == AV_FIELD_UNKNOWN)
  128. return true;
  129. if ((field_order == AV_FIELD_TT || field_order == AV_FIELD_TB) && bmd_field_order == bmdUpperFieldFirst)
  130. return true;
  131. if ((field_order == AV_FIELD_BB || field_order == AV_FIELD_BT) && bmd_field_order == bmdLowerFieldFirst)
  132. return true;
  133. if (field_order == AV_FIELD_PROGRESSIVE && (bmd_field_order == bmdProgressiveFrame || bmd_field_order == bmdProgressiveSegmentedFrame))
  134. return true;
  135. return false;
  136. }
  137. int ff_decklink_set_format(AVFormatContext *avctx,
  138. int width, int height,
  139. int tb_num, int tb_den,
  140. enum AVFieldOrder field_order,
  141. decklink_direction_t direction, int num)
  142. {
  143. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  144. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  145. BMDDisplayModeSupport support;
  146. IDeckLinkDisplayModeIterator *itermode;
  147. IDeckLinkDisplayMode *mode;
  148. int i = 1;
  149. HRESULT res;
  150. av_log(avctx, AV_LOG_DEBUG, "Trying to find mode for frame size %dx%d, frame timing %d/%d, field order %d, direction %d, mode number %d, format code %s\n",
  151. width, height, tb_num, tb_den, field_order, direction, num, (cctx->format_code) ? cctx->format_code : "(unset)");
  152. if (ctx->duplex_mode) {
  153. DECKLINK_BOOL duplex_supported = false;
  154. if (ctx->attr->GetFlag(BMDDeckLinkSupportsDuplexModeConfiguration, &duplex_supported) != S_OK)
  155. duplex_supported = false;
  156. if (duplex_supported) {
  157. res = ctx->cfg->SetInt(bmdDeckLinkConfigDuplexMode, ctx->duplex_mode == 2 ? bmdDuplexModeFull : bmdDuplexModeHalf);
  158. if (res != S_OK)
  159. av_log(avctx, AV_LOG_WARNING, "Setting duplex mode failed.\n");
  160. else
  161. av_log(avctx, AV_LOG_VERBOSE, "Successfully set duplex mode to %s duplex.\n", ctx->duplex_mode == 2 ? "full" : "half");
  162. } else {
  163. av_log(avctx, AV_LOG_WARNING, "Unable to set duplex mode, because it is not supported.\n");
  164. }
  165. }
  166. if (direction == DIRECTION_IN) {
  167. int ret;
  168. ret = decklink_select_input(avctx, bmdDeckLinkConfigAudioInputConnection);
  169. if (ret < 0)
  170. return ret;
  171. ret = decklink_select_input(avctx, bmdDeckLinkConfigVideoInputConnection);
  172. if (ret < 0)
  173. return ret;
  174. res = ctx->dli->GetDisplayModeIterator (&itermode);
  175. } else {
  176. res = ctx->dlo->GetDisplayModeIterator (&itermode);
  177. }
  178. if (res!= S_OK) {
  179. av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
  180. return AVERROR(EIO);
  181. }
  182. char format_buf[] = " ";
  183. if (cctx->format_code)
  184. memcpy(format_buf, cctx->format_code, FFMIN(strlen(cctx->format_code), sizeof(format_buf)));
  185. BMDDisplayMode target_mode = (BMDDisplayMode)AV_RB32(format_buf);
  186. AVRational target_tb = av_make_q(tb_num, tb_den);
  187. ctx->bmd_mode = bmdModeUnknown;
  188. while ((ctx->bmd_mode == bmdModeUnknown) && itermode->Next(&mode) == S_OK) {
  189. BMDTimeValue bmd_tb_num, bmd_tb_den;
  190. int bmd_width = mode->GetWidth();
  191. int bmd_height = mode->GetHeight();
  192. BMDDisplayMode bmd_mode = mode->GetDisplayMode();
  193. BMDFieldDominance bmd_field_dominance = mode->GetFieldDominance();
  194. mode->GetFrameRate(&bmd_tb_num, &bmd_tb_den);
  195. AVRational mode_tb = av_make_q(bmd_tb_num, bmd_tb_den);
  196. if ((bmd_width == width &&
  197. bmd_height == height &&
  198. !av_cmp_q(mode_tb, target_tb) &&
  199. field_order_eq(field_order, bmd_field_dominance))
  200. || i == num
  201. || target_mode == bmd_mode) {
  202. ctx->bmd_mode = bmd_mode;
  203. ctx->bmd_width = bmd_width;
  204. ctx->bmd_height = bmd_height;
  205. ctx->bmd_tb_den = bmd_tb_den;
  206. ctx->bmd_tb_num = bmd_tb_num;
  207. ctx->bmd_field_dominance = bmd_field_dominance;
  208. av_log(avctx, AV_LOG_INFO, "Found Decklink mode %d x %d with rate %.2f%s\n",
  209. bmd_width, bmd_height, 1/av_q2d(mode_tb),
  210. (ctx->bmd_field_dominance==bmdLowerFieldFirst || ctx->bmd_field_dominance==bmdUpperFieldFirst)?"(i)":"");
  211. }
  212. mode->Release();
  213. i++;
  214. }
  215. itermode->Release();
  216. if (ctx->bmd_mode == bmdModeUnknown)
  217. return -1;
  218. if (direction == DIRECTION_IN) {
  219. if (ctx->dli->DoesSupportVideoMode(ctx->bmd_mode, (BMDPixelFormat) cctx->raw_format,
  220. bmdVideoOutputFlagDefault,
  221. &support, NULL) != S_OK)
  222. return -1;
  223. } else {
  224. if (ctx->dlo->DoesSupportVideoMode(ctx->bmd_mode, bmdFormat8BitYUV,
  225. bmdVideoOutputFlagDefault,
  226. &support, NULL) != S_OK)
  227. return -1;
  228. }
  229. if (support == bmdDisplayModeSupported)
  230. return 0;
  231. return -1;
  232. }
  233. int ff_decklink_set_format(AVFormatContext *avctx, decklink_direction_t direction, int num) {
  234. return ff_decklink_set_format(avctx, 0, 0, 0, 0, AV_FIELD_UNKNOWN, direction, num);
  235. }
  236. int ff_decklink_list_devices(AVFormatContext *avctx,
  237. struct AVDeviceInfoList *device_list,
  238. int show_inputs, int show_outputs)
  239. {
  240. IDeckLink *dl = NULL;
  241. IDeckLinkIterator *iter = CreateDeckLinkIteratorInstance();
  242. int ret = 0;
  243. if (!iter) {
  244. av_log(avctx, AV_LOG_ERROR, "Could not create DeckLink iterator\n");
  245. return AVERROR(EIO);
  246. }
  247. while (ret == 0 && iter->Next(&dl) == S_OK) {
  248. IDeckLinkOutput *output_config;
  249. IDeckLinkInput *input_config;
  250. const char *displayName;
  251. AVDeviceInfo *new_device = NULL;
  252. int add = 0;
  253. ff_decklink_get_display_name(dl, &displayName);
  254. if (show_outputs) {
  255. if (dl->QueryInterface(IID_IDeckLinkOutput, (void **)&output_config) == S_OK) {
  256. output_config->Release();
  257. add = 1;
  258. }
  259. }
  260. if (show_inputs) {
  261. if (dl->QueryInterface(IID_IDeckLinkInput, (void **)&input_config) == S_OK) {
  262. input_config->Release();
  263. add = 1;
  264. }
  265. }
  266. if (add == 1) {
  267. new_device = (AVDeviceInfo *) av_mallocz(sizeof(AVDeviceInfo));
  268. if (!new_device) {
  269. ret = AVERROR(ENOMEM);
  270. goto next;
  271. }
  272. new_device->device_name = av_strdup(displayName);
  273. if (!new_device->device_name) {
  274. ret = AVERROR(ENOMEM);
  275. goto next;
  276. }
  277. new_device->device_description = av_strdup(displayName);
  278. if (!new_device->device_description) {
  279. av_freep(&new_device->device_name);
  280. ret = AVERROR(ENOMEM);
  281. goto next;
  282. }
  283. if ((ret = av_dynarray_add_nofree(&device_list->devices,
  284. &device_list->nb_devices, new_device)) < 0) {
  285. av_freep(&new_device->device_name);
  286. av_freep(&new_device->device_description);
  287. av_freep(&new_device);
  288. goto next;
  289. }
  290. }
  291. next:
  292. av_freep(&displayName);
  293. dl->Release();
  294. }
  295. iter->Release();
  296. return ret;
  297. }
  298. /* This is a wrapper around the ff_decklink_list_devices() which dumps the
  299. output to av_log() and exits (for backward compatibility with the
  300. "-list_devices" argument). */
  301. void ff_decklink_list_devices_legacy(AVFormatContext *avctx,
  302. int show_inputs, int show_outputs)
  303. {
  304. struct AVDeviceInfoList *device_list = NULL;
  305. int ret;
  306. device_list = (struct AVDeviceInfoList *) av_mallocz(sizeof(AVDeviceInfoList));
  307. if (!device_list)
  308. return;
  309. ret = ff_decklink_list_devices(avctx, device_list, show_inputs, show_outputs);
  310. if (ret == 0) {
  311. av_log(avctx, AV_LOG_INFO, "Blackmagic DeckLink %s devices:\n",
  312. show_inputs ? "input" : "output");
  313. for (int i = 0; i < device_list->nb_devices; i++) {
  314. av_log(avctx, AV_LOG_INFO, "\t'%s'\n", device_list->devices[i]->device_name);
  315. }
  316. }
  317. avdevice_free_list_devices(&device_list);
  318. }
  319. int ff_decklink_list_formats(AVFormatContext *avctx, decklink_direction_t direction)
  320. {
  321. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  322. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  323. IDeckLinkDisplayModeIterator *itermode;
  324. IDeckLinkDisplayMode *mode;
  325. uint32_t format_code;
  326. HRESULT res;
  327. if (direction == DIRECTION_IN) {
  328. int ret;
  329. ret = decklink_select_input(avctx, bmdDeckLinkConfigAudioInputConnection);
  330. if (ret < 0)
  331. return ret;
  332. ret = decklink_select_input(avctx, bmdDeckLinkConfigVideoInputConnection);
  333. if (ret < 0)
  334. return ret;
  335. res = ctx->dli->GetDisplayModeIterator (&itermode);
  336. } else {
  337. res = ctx->dlo->GetDisplayModeIterator (&itermode);
  338. }
  339. if (res!= S_OK) {
  340. av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
  341. return AVERROR(EIO);
  342. }
  343. av_log(avctx, AV_LOG_INFO, "Supported formats for '%s':\n\tformat_code\tdescription",
  344. avctx->filename);
  345. while (itermode->Next(&mode) == S_OK) {
  346. BMDTimeValue tb_num, tb_den;
  347. mode->GetFrameRate(&tb_num, &tb_den);
  348. format_code = av_bswap32(mode->GetDisplayMode());
  349. av_log(avctx, AV_LOG_INFO, "\n\t%.4s\t\t%ldx%ld at %d/%d fps",
  350. (char*) &format_code, mode->GetWidth(), mode->GetHeight(),
  351. (int) tb_den, (int) tb_num);
  352. switch (mode->GetFieldDominance()) {
  353. case bmdLowerFieldFirst:
  354. av_log(avctx, AV_LOG_INFO, " (interlaced, lower field first)"); break;
  355. case bmdUpperFieldFirst:
  356. av_log(avctx, AV_LOG_INFO, " (interlaced, upper field first)"); break;
  357. }
  358. mode->Release();
  359. }
  360. av_log(avctx, AV_LOG_INFO, "\n");
  361. itermode->Release();
  362. return 0;
  363. }
  364. void ff_decklink_cleanup(AVFormatContext *avctx)
  365. {
  366. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  367. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  368. if (ctx->dli)
  369. ctx->dli->Release();
  370. if (ctx->dlo)
  371. ctx->dlo->Release();
  372. if (ctx->attr)
  373. ctx->attr->Release();
  374. if (ctx->cfg)
  375. ctx->cfg->Release();
  376. if (ctx->dl)
  377. ctx->dl->Release();
  378. }
  379. int ff_decklink_init_device(AVFormatContext *avctx, const char* name)
  380. {
  381. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  382. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  383. IDeckLink *dl = NULL;
  384. IDeckLinkIterator *iter = CreateDeckLinkIteratorInstance();
  385. if (!iter) {
  386. av_log(avctx, AV_LOG_ERROR, "Could not create DeckLink iterator\n");
  387. return AVERROR_EXTERNAL;
  388. }
  389. while (iter->Next(&dl) == S_OK) {
  390. const char *displayName;
  391. ff_decklink_get_display_name(dl, &displayName);
  392. if (!strcmp(name, displayName)) {
  393. av_free((void *)displayName);
  394. ctx->dl = dl;
  395. break;
  396. }
  397. av_free((void *)displayName);
  398. dl->Release();
  399. }
  400. iter->Release();
  401. if (!ctx->dl)
  402. return AVERROR(ENXIO);
  403. if (ctx->dl->QueryInterface(IID_IDeckLinkConfiguration, (void **)&ctx->cfg) != S_OK) {
  404. av_log(avctx, AV_LOG_ERROR, "Could not get configuration interface for '%s'\n", name);
  405. ff_decklink_cleanup(avctx);
  406. return AVERROR_EXTERNAL;
  407. }
  408. if (ctx->dl->QueryInterface(IID_IDeckLinkAttributes, (void **)&ctx->attr) != S_OK) {
  409. av_log(avctx, AV_LOG_ERROR, "Could not get attributes interface for '%s'\n", name);
  410. ff_decklink_cleanup(avctx);
  411. return AVERROR_EXTERNAL;
  412. }
  413. return 0;
  414. }