decklink_common.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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. /* The file provided by the SDK is known to be missing prototypes, which doesn't
  31. cause issues with GCC since the warning doesn't apply to C++ files. However
  32. Clang does complain (and warnings are treated as errors), so suppress the
  33. warning just for this one file */
  34. #ifdef __clang__
  35. #pragma clang diagnostic push
  36. #pragma clang diagnostic ignored "-Wmissing-prototypes"
  37. #endif
  38. #include <DeckLinkAPIDispatch.cpp>
  39. #ifdef __clang__
  40. #pragma clang diagnostic pop
  41. #endif
  42. #endif
  43. extern "C" {
  44. #include "libavformat/avformat.h"
  45. #include "libavutil/imgutils.h"
  46. #include "libavutil/intreadwrite.h"
  47. #include "libavutil/bswap.h"
  48. #include "avdevice.h"
  49. }
  50. #include "decklink_common.h"
  51. static IDeckLinkIterator *decklink_create_iterator(AVFormatContext *avctx)
  52. {
  53. IDeckLinkIterator *iter;
  54. #ifdef _WIN32
  55. if (CoInitialize(NULL) < 0) {
  56. av_log(avctx, AV_LOG_ERROR, "COM initialization failed.\n");
  57. return NULL;
  58. }
  59. if (CoCreateInstance(CLSID_CDeckLinkIterator, NULL, CLSCTX_ALL,
  60. IID_IDeckLinkIterator, (void**) &iter) != S_OK) {
  61. iter = NULL;
  62. }
  63. #else
  64. iter = CreateDeckLinkIteratorInstance();
  65. #endif
  66. if (!iter) {
  67. av_log(avctx, AV_LOG_ERROR, "Could not create DeckLink iterator. "
  68. "Make sure you have DeckLink drivers " BLACKMAGIC_DECKLINK_API_VERSION_STRING " or newer installed.\n");
  69. } else {
  70. IDeckLinkAPIInformation *api;
  71. int64_t version;
  72. #ifdef _WIN32
  73. if (CoCreateInstance(CLSID_CDeckLinkAPIInformation, NULL, CLSCTX_ALL,
  74. IID_IDeckLinkAPIInformation, (void**) &api) != S_OK) {
  75. api = NULL;
  76. }
  77. #else
  78. api = CreateDeckLinkAPIInformationInstance();
  79. #endif
  80. if (api && api->GetInt(BMDDeckLinkAPIVersion, &version) == S_OK) {
  81. if (version < BLACKMAGIC_DECKLINK_API_VERSION)
  82. av_log(avctx, AV_LOG_WARNING, "Installed DeckLink drivers are too old and may be incompatible with the SDK this module was built against. "
  83. "Make sure you have DeckLink drivers " BLACKMAGIC_DECKLINK_API_VERSION_STRING " or newer installed.\n");
  84. } else {
  85. av_log(avctx, AV_LOG_ERROR, "Failed to check installed DeckLink API version.\n");
  86. }
  87. if (api)
  88. api->Release();
  89. }
  90. return iter;
  91. }
  92. static int decklink_get_attr_string(IDeckLink *dl, BMDDeckLinkAttributeID cfg_id, const char **s)
  93. {
  94. DECKLINK_STR tmp;
  95. HRESULT hr;
  96. IDeckLinkProfileAttributes *attr;
  97. *s = NULL;
  98. if (dl->QueryInterface(IID_IDeckLinkProfileAttributes, (void **)&attr) != S_OK)
  99. return AVERROR_EXTERNAL;
  100. hr = attr->GetString(cfg_id, &tmp);
  101. attr->Release();
  102. if (hr == S_OK) {
  103. *s = DECKLINK_STRDUP(tmp);
  104. DECKLINK_FREE(tmp);
  105. if (!*s)
  106. return AVERROR(ENOMEM);
  107. } else if (hr == E_FAIL) {
  108. return AVERROR_EXTERNAL;
  109. }
  110. return 0;
  111. }
  112. static int decklink_select_input(AVFormatContext *avctx, BMDDeckLinkConfigurationID cfg_id)
  113. {
  114. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  115. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  116. BMDDeckLinkAttributeID attr_id = (cfg_id == bmdDeckLinkConfigAudioInputConnection) ? BMDDeckLinkAudioInputConnections : BMDDeckLinkVideoInputConnections;
  117. int64_t bmd_input = (cfg_id == bmdDeckLinkConfigAudioInputConnection) ? (int64_t)ctx->audio_input : (int64_t)ctx->video_input;
  118. const char *type_name = (cfg_id == bmdDeckLinkConfigAudioInputConnection) ? "audio" : "video";
  119. int64_t supported_connections = 0;
  120. HRESULT res;
  121. if (bmd_input) {
  122. res = ctx->attr->GetInt(attr_id, &supported_connections);
  123. if (res != S_OK) {
  124. av_log(avctx, AV_LOG_ERROR, "Failed to query supported %s inputs.\n", type_name);
  125. return AVERROR_EXTERNAL;
  126. }
  127. if ((supported_connections & bmd_input) != bmd_input) {
  128. av_log(avctx, AV_LOG_ERROR, "Device does not support selected %s input.\n", type_name);
  129. return AVERROR(ENOSYS);
  130. }
  131. res = ctx->cfg->SetInt(cfg_id, bmd_input);
  132. if (res != S_OK) {
  133. av_log(avctx, AV_LOG_ERROR, "Failed to select %s input.\n", type_name);
  134. return AVERROR_EXTERNAL;
  135. }
  136. }
  137. return 0;
  138. }
  139. static DECKLINK_BOOL field_order_eq(enum AVFieldOrder field_order, BMDFieldDominance bmd_field_order)
  140. {
  141. if (field_order == AV_FIELD_UNKNOWN)
  142. return true;
  143. if ((field_order == AV_FIELD_TT || field_order == AV_FIELD_TB) && bmd_field_order == bmdUpperFieldFirst)
  144. return true;
  145. if ((field_order == AV_FIELD_BB || field_order == AV_FIELD_BT) && bmd_field_order == bmdLowerFieldFirst)
  146. return true;
  147. if (field_order == AV_FIELD_PROGRESSIVE && (bmd_field_order == bmdProgressiveFrame || bmd_field_order == bmdProgressiveSegmentedFrame))
  148. return true;
  149. return false;
  150. }
  151. int ff_decklink_set_configs(AVFormatContext *avctx,
  152. decklink_direction_t direction) {
  153. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  154. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  155. HRESULT res;
  156. if (ctx->duplex_mode) {
  157. DECKLINK_BOOL duplex_supported = false;
  158. #if BLACKMAGIC_DECKLINK_API_VERSION >= 0x0b000000
  159. IDeckLinkProfileManager *manager = NULL;
  160. if (ctx->dl->QueryInterface(IID_IDeckLinkProfileManager, (void **)&manager) == S_OK)
  161. duplex_supported = true;
  162. #else
  163. if (ctx->attr->GetFlag(BMDDeckLinkSupportsDuplexModeConfiguration, &duplex_supported) != S_OK)
  164. duplex_supported = false;
  165. #endif
  166. if (duplex_supported) {
  167. #if BLACKMAGIC_DECKLINK_API_VERSION >= 0x0b000000
  168. IDeckLinkProfile *profile = NULL;
  169. BMDProfileID bmd_profile_id;
  170. if (ctx->duplex_mode < 0 || ctx->duplex_mode >= FF_ARRAY_ELEMS(decklink_profile_id_map))
  171. return EINVAL;
  172. bmd_profile_id = decklink_profile_id_map[ctx->duplex_mode];
  173. res = manager->GetProfile(bmd_profile_id, &profile);
  174. if (res == S_OK) {
  175. res = profile->SetActive();
  176. profile->Release();
  177. }
  178. manager->Release();
  179. #else
  180. res = ctx->cfg->SetInt(bmdDeckLinkConfigDuplexMode, ctx->duplex_mode == 2 ? bmdDuplexModeFull : bmdDuplexModeHalf);
  181. #endif
  182. if (res != S_OK)
  183. av_log(avctx, AV_LOG_WARNING, "Setting duplex mode failed.\n");
  184. else
  185. av_log(avctx, AV_LOG_VERBOSE, "Successfully set duplex mode to %s duplex.\n", ctx->duplex_mode == 2 || ctx->duplex_mode == 4 ? "full" : "half");
  186. } else {
  187. av_log(avctx, AV_LOG_WARNING, "Unable to set duplex mode, because it is not supported.\n");
  188. }
  189. }
  190. if (direction == DIRECTION_IN) {
  191. int ret;
  192. ret = decklink_select_input(avctx, bmdDeckLinkConfigAudioInputConnection);
  193. if (ret < 0)
  194. return ret;
  195. ret = decklink_select_input(avctx, bmdDeckLinkConfigVideoInputConnection);
  196. if (ret < 0)
  197. return ret;
  198. }
  199. if (direction == DIRECTION_OUT && cctx->timing_offset != INT_MIN) {
  200. res = ctx->cfg->SetInt(bmdDeckLinkConfigReferenceInputTimingOffset, cctx->timing_offset);
  201. if (res != S_OK)
  202. av_log(avctx, AV_LOG_WARNING, "Setting timing offset failed.\n");
  203. }
  204. if (direction == DIRECTION_OUT && ctx->link > 0) {
  205. res = ctx->cfg->SetInt(bmdDeckLinkConfigSDIOutputLinkConfiguration, ctx->link);
  206. if (res != S_OK)
  207. av_log(avctx, AV_LOG_WARNING, "Setting link configuration failed.\n");
  208. else
  209. av_log(avctx, AV_LOG_VERBOSE, "Successfully set link configuration: 0x%x.\n", ctx->link);
  210. if (ctx->link == bmdLinkConfigurationQuadLink && cctx->sqd >= 0) {
  211. res = ctx->cfg->SetFlag(bmdDeckLinkConfigQuadLinkSDIVideoOutputSquareDivisionSplit, cctx->sqd);
  212. if (res != S_OK)
  213. av_log(avctx, AV_LOG_WARNING, "Setting SquareDivisionSplit failed.\n");
  214. else
  215. av_log(avctx, AV_LOG_VERBOSE, "Successfully set SquareDivisionSplit.\n");
  216. }
  217. }
  218. if (direction == DIRECTION_OUT && cctx->level_a >= 0) {
  219. DECKLINK_BOOL level_a_supported = false;
  220. if (ctx->attr->GetFlag(BMDDeckLinkSupportsSMPTELevelAOutput, &level_a_supported) != S_OK)
  221. level_a_supported = false;
  222. if (level_a_supported) {
  223. res = ctx->cfg->SetFlag(bmdDeckLinkConfigSMPTELevelAOutput, cctx->level_a);
  224. if (res != S_OK)
  225. av_log(avctx, AV_LOG_WARNING, "Setting SMPTE levelA failed.\n");
  226. else
  227. av_log(avctx, AV_LOG_VERBOSE, "Successfully set SMPTE levelA.\n");
  228. } else {
  229. av_log(avctx, AV_LOG_WARNING, "Unable to set SMPTE levelA mode, because it is not supported.\n");
  230. }
  231. }
  232. return 0;
  233. }
  234. int ff_decklink_set_format(AVFormatContext *avctx,
  235. int width, int height,
  236. int tb_num, int tb_den,
  237. enum AVFieldOrder field_order,
  238. decklink_direction_t direction)
  239. {
  240. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  241. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  242. #if BLACKMAGIC_DECKLINK_API_VERSION >= 0x0b000000
  243. DECKLINK_BOOL support;
  244. #else
  245. BMDDisplayModeSupport support;
  246. #endif
  247. IDeckLinkDisplayModeIterator *itermode;
  248. IDeckLinkDisplayMode *mode;
  249. int i = 1;
  250. HRESULT res;
  251. av_log(avctx, AV_LOG_DEBUG, "Trying to find mode for frame size %dx%d, frame timing %d/%d, field order %d, direction %d, format code %s\n",
  252. width, height, tb_num, tb_den, field_order, direction, cctx->format_code ? cctx->format_code : "(unset)");
  253. if (direction == DIRECTION_IN) {
  254. res = ctx->dli->GetDisplayModeIterator (&itermode);
  255. } else {
  256. res = ctx->dlo->GetDisplayModeIterator (&itermode);
  257. }
  258. if (res!= S_OK) {
  259. av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
  260. return AVERROR(EIO);
  261. }
  262. char format_buf[] = " ";
  263. if (cctx->format_code)
  264. memcpy(format_buf, cctx->format_code, FFMIN(strlen(cctx->format_code), sizeof(format_buf)));
  265. BMDDisplayMode target_mode = (BMDDisplayMode)AV_RB32(format_buf);
  266. AVRational target_tb = av_make_q(tb_num, tb_den);
  267. ctx->bmd_mode = bmdModeUnknown;
  268. while ((ctx->bmd_mode == bmdModeUnknown) && itermode->Next(&mode) == S_OK) {
  269. BMDTimeValue bmd_tb_num, bmd_tb_den;
  270. int bmd_width = mode->GetWidth();
  271. int bmd_height = mode->GetHeight();
  272. BMDDisplayMode bmd_mode = mode->GetDisplayMode();
  273. BMDFieldDominance bmd_field_dominance = mode->GetFieldDominance();
  274. mode->GetFrameRate(&bmd_tb_num, &bmd_tb_den);
  275. AVRational mode_tb = av_make_q(bmd_tb_num, bmd_tb_den);
  276. if ((bmd_width == width &&
  277. bmd_height == height &&
  278. !av_cmp_q(mode_tb, target_tb) &&
  279. field_order_eq(field_order, bmd_field_dominance))
  280. || target_mode == bmd_mode) {
  281. ctx->bmd_mode = bmd_mode;
  282. ctx->bmd_width = bmd_width;
  283. ctx->bmd_height = bmd_height;
  284. ctx->bmd_tb_den = bmd_tb_den;
  285. ctx->bmd_tb_num = bmd_tb_num;
  286. ctx->bmd_field_dominance = bmd_field_dominance;
  287. av_log(avctx, AV_LOG_INFO, "Found Decklink mode %d x %d with rate %.2f%s\n",
  288. bmd_width, bmd_height, 1/av_q2d(mode_tb),
  289. (ctx->bmd_field_dominance==bmdLowerFieldFirst || ctx->bmd_field_dominance==bmdUpperFieldFirst)?"(i)":"");
  290. }
  291. mode->Release();
  292. i++;
  293. }
  294. itermode->Release();
  295. if (ctx->bmd_mode == bmdModeUnknown)
  296. return -1;
  297. #if BLACKMAGIC_DECKLINK_API_VERSION >= 0x0b050000
  298. if (direction == DIRECTION_IN) {
  299. BMDDisplayMode actualMode = ctx->bmd_mode;
  300. if (ctx->dli->DoesSupportVideoMode(ctx->video_input, ctx->bmd_mode, ctx->raw_format,
  301. bmdNoVideoInputConversion, bmdSupportedVideoModeDefault,
  302. &actualMode, &support) != S_OK || !support || ctx->bmd_mode != actualMode)
  303. return -1;
  304. } else {
  305. BMDDisplayMode actualMode = ctx->bmd_mode;
  306. if (ctx->dlo->DoesSupportVideoMode(bmdVideoConnectionUnspecified, ctx->bmd_mode, ctx->raw_format,
  307. bmdNoVideoOutputConversion, bmdSupportedVideoModeDefault,
  308. &actualMode, &support) != S_OK || !support || ctx->bmd_mode != actualMode)
  309. return -1;
  310. }
  311. return 0;
  312. #elif BLACKMAGIC_DECKLINK_API_VERSION >= 0x0b000000
  313. if (direction == DIRECTION_IN) {
  314. if (ctx->dli->DoesSupportVideoMode(ctx->video_input, ctx->bmd_mode, ctx->raw_format,
  315. bmdSupportedVideoModeDefault,
  316. &support) != S_OK)
  317. return -1;
  318. } else {
  319. BMDDisplayMode actualMode = ctx->bmd_mode;
  320. if (ctx->dlo->DoesSupportVideoMode(bmdVideoConnectionUnspecified, ctx->bmd_mode, ctx->raw_format,
  321. bmdSupportedVideoModeDefault,
  322. &actualMode, &support) != S_OK || !support || ctx->bmd_mode != actualMode) {
  323. return -1;
  324. }
  325. }
  326. if (support)
  327. return 0;
  328. #else
  329. if (direction == DIRECTION_IN) {
  330. if (ctx->dli->DoesSupportVideoMode(ctx->bmd_mode, ctx->raw_format,
  331. bmdVideoOutputFlagDefault,
  332. &support, NULL) != S_OK)
  333. return -1;
  334. } else {
  335. if (!ctx->supports_vanc || ctx->dlo->DoesSupportVideoMode(ctx->bmd_mode, ctx->raw_format,
  336. bmdVideoOutputVANC,
  337. &support, NULL) != S_OK || support != bmdDisplayModeSupported) {
  338. /* Try without VANC enabled */
  339. if (ctx->dlo->DoesSupportVideoMode(ctx->bmd_mode, ctx->raw_format,
  340. bmdVideoOutputFlagDefault,
  341. &support, NULL) != S_OK) {
  342. return -1;
  343. }
  344. ctx->supports_vanc = 0;
  345. }
  346. }
  347. if (support == bmdDisplayModeSupported)
  348. return 0;
  349. #endif
  350. return -1;
  351. }
  352. int ff_decklink_set_format(AVFormatContext *avctx, decklink_direction_t direction) {
  353. return ff_decklink_set_format(avctx, 0, 0, 0, 0, AV_FIELD_UNKNOWN, direction);
  354. }
  355. void ff_decklink_packet_queue_init(AVFormatContext *avctx, DecklinkPacketQueue *q, int64_t queue_size)
  356. {
  357. memset(q, 0, sizeof(DecklinkPacketQueue));
  358. pthread_mutex_init(&q->mutex, NULL);
  359. pthread_cond_init(&q->cond, NULL);
  360. q->avctx = avctx;
  361. q->max_q_size = queue_size;
  362. }
  363. void ff_decklink_packet_queue_flush(DecklinkPacketQueue *q)
  364. {
  365. AVPacket pkt;
  366. pthread_mutex_lock(&q->mutex);
  367. while (avpriv_packet_list_get(&q->pkt_list, &pkt) == 0) {
  368. av_packet_unref(&pkt);
  369. }
  370. q->nb_packets = 0;
  371. q->size = 0;
  372. pthread_mutex_unlock(&q->mutex);
  373. }
  374. void ff_decklink_packet_queue_end(DecklinkPacketQueue *q)
  375. {
  376. ff_decklink_packet_queue_flush(q);
  377. pthread_mutex_destroy(&q->mutex);
  378. pthread_cond_destroy(&q->cond);
  379. }
  380. unsigned long long ff_decklink_packet_queue_size(DecklinkPacketQueue *q)
  381. {
  382. unsigned long long size;
  383. pthread_mutex_lock(&q->mutex);
  384. size = q->size;
  385. pthread_mutex_unlock(&q->mutex);
  386. return size;
  387. }
  388. int ff_decklink_packet_queue_put(DecklinkPacketQueue *q, AVPacket *pkt)
  389. {
  390. int pkt_size = pkt->size;
  391. int ret;
  392. // Drop Packet if queue size is > maximum queue size
  393. if (ff_decklink_packet_queue_size(q) > (uint64_t)q->max_q_size) {
  394. av_packet_unref(pkt);
  395. av_log(q->avctx, AV_LOG_WARNING, "Decklink input buffer overrun!\n");
  396. return -1;
  397. }
  398. /* ensure the packet is reference counted */
  399. if (av_packet_make_refcounted(pkt) < 0) {
  400. av_packet_unref(pkt);
  401. return -1;
  402. }
  403. pthread_mutex_lock(&q->mutex);
  404. ret = avpriv_packet_list_put(&q->pkt_list, pkt, NULL, 0);
  405. if (ret == 0) {
  406. q->nb_packets++;
  407. q->size += pkt_size + sizeof(AVPacket);
  408. pthread_cond_signal(&q->cond);
  409. } else {
  410. av_packet_unref(pkt);
  411. }
  412. pthread_mutex_unlock(&q->mutex);
  413. return ret;
  414. }
  415. int ff_decklink_packet_queue_get(DecklinkPacketQueue *q, AVPacket *pkt, int block)
  416. {
  417. int ret;
  418. pthread_mutex_lock(&q->mutex);
  419. for (;; ) {
  420. ret = avpriv_packet_list_get(&q->pkt_list, pkt);
  421. if (ret == 0) {
  422. q->nb_packets--;
  423. q->size -= pkt->size + sizeof(AVPacket);
  424. ret = 1;
  425. break;
  426. } else if (!block) {
  427. ret = 0;
  428. break;
  429. } else {
  430. pthread_cond_wait(&q->cond, &q->mutex);
  431. }
  432. }
  433. pthread_mutex_unlock(&q->mutex);
  434. return ret;
  435. }
  436. int64_t ff_decklink_packet_queue_peekpts(DecklinkPacketQueue *q)
  437. {
  438. PacketListEntry *pkt1;
  439. int64_t pts = -1;
  440. pthread_mutex_lock(&q->mutex);
  441. pkt1 = q->pkt_list.head;
  442. if (pkt1) {
  443. pts = pkt1->pkt.pts;
  444. }
  445. pthread_mutex_unlock(&q->mutex);
  446. return pts;
  447. }
  448. int ff_decklink_list_devices(AVFormatContext *avctx,
  449. struct AVDeviceInfoList *device_list,
  450. int show_inputs, int show_outputs)
  451. {
  452. IDeckLink *dl = NULL;
  453. IDeckLinkIterator *iter = decklink_create_iterator(avctx);
  454. int ret = 0;
  455. if (!iter)
  456. return AVERROR(EIO);
  457. while (ret == 0 && iter->Next(&dl) == S_OK) {
  458. IDeckLinkOutput *output_config;
  459. IDeckLinkInput *input_config;
  460. const char *display_name = NULL;
  461. const char *unique_name = NULL;
  462. AVDeviceInfo *new_device = NULL;
  463. int add = 0;
  464. ret = decklink_get_attr_string(dl, BMDDeckLinkDisplayName, &display_name);
  465. if (ret < 0)
  466. goto next;
  467. ret = decklink_get_attr_string(dl, BMDDeckLinkDeviceHandle, &unique_name);
  468. if (ret < 0)
  469. goto next;
  470. if (show_outputs) {
  471. if (dl->QueryInterface(IID_IDeckLinkOutput, (void **)&output_config) == S_OK) {
  472. output_config->Release();
  473. add = 1;
  474. }
  475. }
  476. if (show_inputs) {
  477. if (dl->QueryInterface(IID_IDeckLinkInput, (void **)&input_config) == S_OK) {
  478. input_config->Release();
  479. add = 1;
  480. }
  481. }
  482. if (add == 1) {
  483. new_device = (AVDeviceInfo *) av_mallocz(sizeof(AVDeviceInfo));
  484. if (!new_device) {
  485. ret = AVERROR(ENOMEM);
  486. goto next;
  487. }
  488. new_device->device_name = av_strdup(unique_name ? unique_name : display_name);
  489. new_device->device_description = av_strdup(display_name);
  490. if (!new_device->device_name ||
  491. !new_device->device_description ||
  492. av_dynarray_add_nofree(&device_list->devices, &device_list->nb_devices, new_device) < 0) {
  493. ret = AVERROR(ENOMEM);
  494. av_freep(&new_device->device_name);
  495. av_freep(&new_device->device_description);
  496. av_freep(&new_device);
  497. goto next;
  498. }
  499. }
  500. next:
  501. av_freep(&display_name);
  502. av_freep(&unique_name);
  503. dl->Release();
  504. }
  505. iter->Release();
  506. return ret;
  507. }
  508. /* This is a wrapper around the ff_decklink_list_devices() which dumps the
  509. output to av_log() and exits (for backward compatibility with the
  510. "-list_devices" argument). */
  511. void ff_decklink_list_devices_legacy(AVFormatContext *avctx,
  512. int show_inputs, int show_outputs)
  513. {
  514. struct AVDeviceInfoList *device_list = NULL;
  515. int ret;
  516. device_list = (struct AVDeviceInfoList *) av_mallocz(sizeof(AVDeviceInfoList));
  517. if (!device_list)
  518. return;
  519. ret = ff_decklink_list_devices(avctx, device_list, show_inputs, show_outputs);
  520. if (ret == 0) {
  521. av_log(avctx, AV_LOG_INFO, "Blackmagic DeckLink %s devices:\n",
  522. show_inputs ? "input" : "output");
  523. for (int i = 0; i < device_list->nb_devices; i++) {
  524. av_log(avctx, AV_LOG_INFO, "\t'%s'\n", device_list->devices[i]->device_description);
  525. }
  526. }
  527. avdevice_free_list_devices(&device_list);
  528. }
  529. int ff_decklink_list_formats(AVFormatContext *avctx, decklink_direction_t direction)
  530. {
  531. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  532. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  533. IDeckLinkDisplayModeIterator *itermode;
  534. IDeckLinkDisplayMode *mode;
  535. uint32_t format_code;
  536. HRESULT res;
  537. if (direction == DIRECTION_IN) {
  538. int ret;
  539. ret = decklink_select_input(avctx, bmdDeckLinkConfigAudioInputConnection);
  540. if (ret < 0)
  541. return ret;
  542. ret = decklink_select_input(avctx, bmdDeckLinkConfigVideoInputConnection);
  543. if (ret < 0)
  544. return ret;
  545. res = ctx->dli->GetDisplayModeIterator (&itermode);
  546. } else {
  547. res = ctx->dlo->GetDisplayModeIterator (&itermode);
  548. }
  549. if (res!= S_OK) {
  550. av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
  551. return AVERROR(EIO);
  552. }
  553. av_log(avctx, AV_LOG_INFO, "Supported formats for '%s':\n\tformat_code\tdescription",
  554. avctx->url);
  555. while (itermode->Next(&mode) == S_OK) {
  556. BMDTimeValue tb_num, tb_den;
  557. mode->GetFrameRate(&tb_num, &tb_den);
  558. format_code = av_bswap32(mode->GetDisplayMode());
  559. av_log(avctx, AV_LOG_INFO, "\n\t%.4s\t\t%ldx%ld at %d/%d fps",
  560. (char*) &format_code, mode->GetWidth(), mode->GetHeight(),
  561. (int) tb_den, (int) tb_num);
  562. switch (mode->GetFieldDominance()) {
  563. case bmdLowerFieldFirst:
  564. av_log(avctx, AV_LOG_INFO, " (interlaced, lower field first)"); break;
  565. case bmdUpperFieldFirst:
  566. av_log(avctx, AV_LOG_INFO, " (interlaced, upper field first)"); break;
  567. }
  568. mode->Release();
  569. }
  570. av_log(avctx, AV_LOG_INFO, "\n");
  571. itermode->Release();
  572. return 0;
  573. }
  574. void ff_decklink_cleanup(AVFormatContext *avctx)
  575. {
  576. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  577. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  578. if (ctx->dli)
  579. ctx->dli->Release();
  580. if (ctx->dlo)
  581. ctx->dlo->Release();
  582. if (ctx->attr)
  583. ctx->attr->Release();
  584. if (ctx->cfg)
  585. ctx->cfg->Release();
  586. if (ctx->dl)
  587. ctx->dl->Release();
  588. }
  589. int ff_decklink_init_device(AVFormatContext *avctx, const char* name)
  590. {
  591. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  592. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  593. IDeckLink *dl = NULL;
  594. IDeckLinkIterator *iter = decklink_create_iterator(avctx);
  595. if (!iter)
  596. return AVERROR_EXTERNAL;
  597. while (iter->Next(&dl) == S_OK) {
  598. const char *display_name = NULL;
  599. const char *unique_name = NULL;
  600. decklink_get_attr_string(dl, BMDDeckLinkDisplayName, &display_name);
  601. decklink_get_attr_string(dl, BMDDeckLinkDeviceHandle, &unique_name);
  602. if (display_name && !strcmp(name, display_name) || unique_name && !strcmp(name, unique_name)) {
  603. av_free((void *)unique_name);
  604. av_free((void *)display_name);
  605. ctx->dl = dl;
  606. break;
  607. }
  608. av_free((void *)display_name);
  609. av_free((void *)unique_name);
  610. dl->Release();
  611. }
  612. iter->Release();
  613. if (!ctx->dl)
  614. return AVERROR(ENXIO);
  615. if (ctx->dl->QueryInterface(IID_IDeckLinkConfiguration, (void **)&ctx->cfg) != S_OK) {
  616. av_log(avctx, AV_LOG_ERROR, "Could not get configuration interface for '%s'\n", name);
  617. ff_decklink_cleanup(avctx);
  618. return AVERROR_EXTERNAL;
  619. }
  620. if (ctx->dl->QueryInterface(IID_IDeckLinkProfileAttributes, (void **)&ctx->attr) != S_OK) {
  621. av_log(avctx, AV_LOG_ERROR, "Could not get attributes interface for '%s'\n", name);
  622. ff_decklink_cleanup(avctx);
  623. return AVERROR_EXTERNAL;
  624. }
  625. return 0;
  626. }