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.

637 lines
21KB

  1. /*
  2. * Blackmagic DeckLink output
  3. * Copyright (c) 2013-2014 Ramiro Polla
  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 <atomic>
  22. using std::atomic;
  23. /* Include internal.h first to avoid conflict between winsock.h (used by
  24. * DeckLink headers) and winsock2.h (used by libavformat) in MSVC++ builds */
  25. extern "C" {
  26. #include "libavformat/internal.h"
  27. }
  28. #include <DeckLinkAPI.h>
  29. extern "C" {
  30. #include "libavformat/avformat.h"
  31. #include "libavutil/imgutils.h"
  32. #include "avdevice.h"
  33. }
  34. #include "decklink_common.h"
  35. #include "decklink_enc.h"
  36. #if CONFIG_LIBKLVANC
  37. #include "libklvanc/vanc.h"
  38. #include "libklvanc/vanc-lines.h"
  39. #include "libklvanc/pixels.h"
  40. #endif
  41. /* DeckLink callback class declaration */
  42. class decklink_frame : public IDeckLinkVideoFrame
  43. {
  44. public:
  45. decklink_frame(struct decklink_ctx *ctx, AVFrame *avframe, AVCodecID codec_id, int height, int width) :
  46. _ctx(ctx), _avframe(avframe), _avpacket(NULL), _codec_id(codec_id), _ancillary(NULL), _height(height), _width(width), _refs(1) { }
  47. decklink_frame(struct decklink_ctx *ctx, AVPacket *avpacket, AVCodecID codec_id, int height, int width) :
  48. _ctx(ctx), _avframe(NULL), _avpacket(avpacket), _codec_id(codec_id), _ancillary(NULL), _height(height), _width(width), _refs(1) { }
  49. virtual long STDMETHODCALLTYPE GetWidth (void) { return _width; }
  50. virtual long STDMETHODCALLTYPE GetHeight (void) { return _height; }
  51. virtual long STDMETHODCALLTYPE GetRowBytes (void)
  52. {
  53. if (_codec_id == AV_CODEC_ID_WRAPPED_AVFRAME)
  54. return _avframe->linesize[0] < 0 ? -_avframe->linesize[0] : _avframe->linesize[0];
  55. else
  56. return ((GetWidth() + 47) / 48) * 128;
  57. }
  58. virtual BMDPixelFormat STDMETHODCALLTYPE GetPixelFormat(void)
  59. {
  60. if (_codec_id == AV_CODEC_ID_WRAPPED_AVFRAME)
  61. return bmdFormat8BitYUV;
  62. else
  63. return bmdFormat10BitYUV;
  64. }
  65. virtual BMDFrameFlags STDMETHODCALLTYPE GetFlags (void)
  66. {
  67. if (_codec_id == AV_CODEC_ID_WRAPPED_AVFRAME)
  68. return _avframe->linesize[0] < 0 ? bmdFrameFlagFlipVertical : bmdFrameFlagDefault;
  69. else
  70. return bmdFrameFlagDefault;
  71. }
  72. virtual HRESULT STDMETHODCALLTYPE GetBytes (void **buffer)
  73. {
  74. if (_codec_id == AV_CODEC_ID_WRAPPED_AVFRAME) {
  75. if (_avframe->linesize[0] < 0)
  76. *buffer = (void *)(_avframe->data[0] + _avframe->linesize[0] * (_avframe->height - 1));
  77. else
  78. *buffer = (void *)(_avframe->data[0]);
  79. } else {
  80. *buffer = (void *)(_avpacket->data);
  81. }
  82. return S_OK;
  83. }
  84. virtual HRESULT STDMETHODCALLTYPE GetTimecode (BMDTimecodeFormat format, IDeckLinkTimecode **timecode) { return S_FALSE; }
  85. virtual HRESULT STDMETHODCALLTYPE GetAncillaryData(IDeckLinkVideoFrameAncillary **ancillary)
  86. {
  87. *ancillary = _ancillary;
  88. if (_ancillary) {
  89. _ancillary->AddRef();
  90. return S_OK;
  91. } else {
  92. return S_FALSE;
  93. }
  94. }
  95. virtual HRESULT STDMETHODCALLTYPE SetAncillaryData(IDeckLinkVideoFrameAncillary *ancillary)
  96. {
  97. if (_ancillary)
  98. _ancillary->Release();
  99. _ancillary = ancillary;
  100. _ancillary->AddRef();
  101. return S_OK;
  102. }
  103. virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; }
  104. virtual ULONG STDMETHODCALLTYPE AddRef(void) { return ++_refs; }
  105. virtual ULONG STDMETHODCALLTYPE Release(void)
  106. {
  107. int ret = --_refs;
  108. if (!ret) {
  109. av_frame_free(&_avframe);
  110. av_packet_free(&_avpacket);
  111. if (_ancillary)
  112. _ancillary->Release();
  113. delete this;
  114. }
  115. return ret;
  116. }
  117. struct decklink_ctx *_ctx;
  118. AVFrame *_avframe;
  119. AVPacket *_avpacket;
  120. AVCodecID _codec_id;
  121. IDeckLinkVideoFrameAncillary *_ancillary;
  122. int _height;
  123. int _width;
  124. private:
  125. std::atomic<int> _refs;
  126. };
  127. class decklink_output_callback : public IDeckLinkVideoOutputCallback
  128. {
  129. public:
  130. virtual HRESULT STDMETHODCALLTYPE ScheduledFrameCompleted(IDeckLinkVideoFrame *_frame, BMDOutputFrameCompletionResult result)
  131. {
  132. decklink_frame *frame = static_cast<decklink_frame *>(_frame);
  133. struct decklink_ctx *ctx = frame->_ctx;
  134. if (frame->_avframe)
  135. av_frame_unref(frame->_avframe);
  136. if (frame->_avpacket)
  137. av_packet_unref(frame->_avpacket);
  138. pthread_mutex_lock(&ctx->mutex);
  139. ctx->frames_buffer_available_spots++;
  140. pthread_cond_broadcast(&ctx->cond);
  141. pthread_mutex_unlock(&ctx->mutex);
  142. return S_OK;
  143. }
  144. virtual HRESULT STDMETHODCALLTYPE ScheduledPlaybackHasStopped(void) { return S_OK; }
  145. virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; }
  146. virtual ULONG STDMETHODCALLTYPE AddRef(void) { return 1; }
  147. virtual ULONG STDMETHODCALLTYPE Release(void) { return 1; }
  148. };
  149. static int decklink_setup_video(AVFormatContext *avctx, AVStream *st)
  150. {
  151. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  152. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  153. AVCodecParameters *c = st->codecpar;
  154. if (ctx->video) {
  155. av_log(avctx, AV_LOG_ERROR, "Only one video stream is supported!\n");
  156. return -1;
  157. }
  158. if (c->codec_id == AV_CODEC_ID_WRAPPED_AVFRAME) {
  159. if (c->format != AV_PIX_FMT_UYVY422) {
  160. av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format!"
  161. " Only AV_PIX_FMT_UYVY422 is supported.\n");
  162. return -1;
  163. }
  164. ctx->raw_format = bmdFormat8BitYUV;
  165. } else if (c->codec_id != AV_CODEC_ID_V210) {
  166. av_log(avctx, AV_LOG_ERROR, "Unsupported codec type!"
  167. " Only V210 and wrapped frame with AV_PIX_FMT_UYVY422 are supported.\n");
  168. return -1;
  169. } else {
  170. ctx->raw_format = bmdFormat10BitYUV;
  171. }
  172. if (ff_decklink_set_configs(avctx, DIRECTION_OUT) < 0) {
  173. av_log(avctx, AV_LOG_ERROR, "Could not set output configuration\n");
  174. return -1;
  175. }
  176. if (ff_decklink_set_format(avctx, c->width, c->height,
  177. st->time_base.num, st->time_base.den, c->field_order)) {
  178. av_log(avctx, AV_LOG_ERROR, "Unsupported video size, framerate or field order!"
  179. " Check available formats with -list_formats 1.\n");
  180. return -1;
  181. }
  182. if (ctx->dlo->EnableVideoOutput(ctx->bmd_mode,
  183. ctx->supports_vanc ? bmdVideoOutputVANC : bmdVideoOutputFlagDefault) != S_OK) {
  184. av_log(avctx, AV_LOG_ERROR, "Could not enable video output!\n");
  185. return -1;
  186. }
  187. /* Set callback. */
  188. ctx->output_callback = new decklink_output_callback();
  189. ctx->dlo->SetScheduledFrameCompletionCallback(ctx->output_callback);
  190. ctx->frames_preroll = st->time_base.den * ctx->preroll;
  191. if (st->time_base.den > 1000)
  192. ctx->frames_preroll /= 1000;
  193. /* Buffer twice as many frames as the preroll. */
  194. ctx->frames_buffer = ctx->frames_preroll * 2;
  195. ctx->frames_buffer = FFMIN(ctx->frames_buffer, 60);
  196. pthread_mutex_init(&ctx->mutex, NULL);
  197. pthread_cond_init(&ctx->cond, NULL);
  198. ctx->frames_buffer_available_spots = ctx->frames_buffer;
  199. av_log(avctx, AV_LOG_DEBUG, "output: %s, preroll: %d, frames buffer size: %d\n",
  200. avctx->url, ctx->frames_preroll, ctx->frames_buffer);
  201. /* The device expects the framerate to be fixed. */
  202. avpriv_set_pts_info(st, 64, st->time_base.num, st->time_base.den);
  203. ctx->video = 1;
  204. return 0;
  205. }
  206. static int decklink_setup_audio(AVFormatContext *avctx, AVStream *st)
  207. {
  208. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  209. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  210. AVCodecParameters *c = st->codecpar;
  211. if (ctx->audio) {
  212. av_log(avctx, AV_LOG_ERROR, "Only one audio stream is supported!\n");
  213. return -1;
  214. }
  215. if (c->sample_rate != 48000) {
  216. av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate!"
  217. " Only 48kHz is supported.\n");
  218. return -1;
  219. }
  220. if (c->channels != 2 && c->channels != 8 && c->channels != 16) {
  221. av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels!"
  222. " Only 2, 8 or 16 channels are supported.\n");
  223. return -1;
  224. }
  225. if (ctx->dlo->EnableAudioOutput(bmdAudioSampleRate48kHz,
  226. bmdAudioSampleType16bitInteger,
  227. c->channels,
  228. bmdAudioOutputStreamTimestamped) != S_OK) {
  229. av_log(avctx, AV_LOG_ERROR, "Could not enable audio output!\n");
  230. return -1;
  231. }
  232. if (ctx->dlo->BeginAudioPreroll() != S_OK) {
  233. av_log(avctx, AV_LOG_ERROR, "Could not begin audio preroll!\n");
  234. return -1;
  235. }
  236. /* The device expects the sample rate to be fixed. */
  237. avpriv_set_pts_info(st, 64, 1, c->sample_rate);
  238. ctx->channels = c->channels;
  239. ctx->audio = 1;
  240. return 0;
  241. }
  242. av_cold int ff_decklink_write_trailer(AVFormatContext *avctx)
  243. {
  244. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  245. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  246. if (ctx->playback_started) {
  247. BMDTimeValue actual;
  248. ctx->dlo->StopScheduledPlayback(ctx->last_pts * ctx->bmd_tb_num,
  249. &actual, ctx->bmd_tb_den);
  250. ctx->dlo->DisableVideoOutput();
  251. if (ctx->audio)
  252. ctx->dlo->DisableAudioOutput();
  253. }
  254. ff_decklink_cleanup(avctx);
  255. if (ctx->output_callback)
  256. delete ctx->output_callback;
  257. pthread_mutex_destroy(&ctx->mutex);
  258. pthread_cond_destroy(&ctx->cond);
  259. #if CONFIG_LIBKLVANC
  260. klvanc_context_destroy(ctx->vanc_ctx);
  261. #endif
  262. av_freep(&cctx->ctx);
  263. return 0;
  264. }
  265. #if CONFIG_LIBKLVANC
  266. static void construct_cc(AVFormatContext *avctx, struct decklink_ctx *ctx,
  267. AVPacket *pkt, struct klvanc_line_set_s *vanc_lines)
  268. {
  269. struct klvanc_packet_eia_708b_s *cdp;
  270. uint16_t *cdp_words;
  271. uint16_t len;
  272. uint8_t cc_count;
  273. int size, ret, i;
  274. const uint8_t *data = av_packet_get_side_data(pkt, AV_PKT_DATA_A53_CC, &size);
  275. if (!data)
  276. return;
  277. cc_count = size / 3;
  278. ret = klvanc_create_eia708_cdp(&cdp);
  279. if (ret)
  280. return;
  281. ret = klvanc_set_framerate_EIA_708B(cdp, ctx->bmd_tb_num, ctx->bmd_tb_den);
  282. if (ret) {
  283. av_log(avctx, AV_LOG_ERROR, "Invalid framerate specified: %lld/%lld\n",
  284. ctx->bmd_tb_num, ctx->bmd_tb_den);
  285. klvanc_destroy_eia708_cdp(cdp);
  286. return;
  287. }
  288. if (cc_count > KLVANC_MAX_CC_COUNT) {
  289. av_log(avctx, AV_LOG_ERROR, "Illegal cc_count received: %d\n", cc_count);
  290. cc_count = KLVANC_MAX_CC_COUNT;
  291. }
  292. /* CC data */
  293. cdp->header.ccdata_present = 1;
  294. cdp->header.caption_service_active = 1;
  295. cdp->ccdata.cc_count = cc_count;
  296. for (i = 0; i < cc_count; i++) {
  297. if (data [3*i] & 0x04)
  298. cdp->ccdata.cc[i].cc_valid = 1;
  299. cdp->ccdata.cc[i].cc_type = data[3*i] & 0x03;
  300. cdp->ccdata.cc[i].cc_data[0] = data[3*i+1];
  301. cdp->ccdata.cc[i].cc_data[1] = data[3*i+2];
  302. }
  303. klvanc_finalize_EIA_708B(cdp, ctx->cdp_sequence_num++);
  304. ret = klvanc_convert_EIA_708B_to_words(cdp, &cdp_words, &len);
  305. klvanc_destroy_eia708_cdp(cdp);
  306. if (ret != 0) {
  307. av_log(avctx, AV_LOG_ERROR, "Failed converting 708 packet to words\n");
  308. return;
  309. }
  310. ret = klvanc_line_insert(ctx->vanc_ctx, vanc_lines, cdp_words, len, 11, 0);
  311. free(cdp_words);
  312. if (ret != 0) {
  313. av_log(avctx, AV_LOG_ERROR, "VANC line insertion failed\n");
  314. return;
  315. }
  316. }
  317. static int decklink_construct_vanc(AVFormatContext *avctx, struct decklink_ctx *ctx,
  318. AVPacket *pkt, decklink_frame *frame)
  319. {
  320. struct klvanc_line_set_s vanc_lines = { 0 };
  321. int ret = 0, i;
  322. if (!ctx->supports_vanc)
  323. return 0;
  324. construct_cc(avctx, ctx, pkt, &vanc_lines);
  325. IDeckLinkVideoFrameAncillary *vanc;
  326. int result = ctx->dlo->CreateAncillaryData(bmdFormat10BitYUV, &vanc);
  327. if (result != S_OK) {
  328. av_log(avctx, AV_LOG_ERROR, "Failed to create vanc\n");
  329. ret = AVERROR(EIO);
  330. goto done;
  331. }
  332. /* Now that we've got all the VANC lines in a nice orderly manner, generate the
  333. final VANC sections for the Decklink output */
  334. for (i = 0; i < vanc_lines.num_lines; i++) {
  335. struct klvanc_line_s *line = vanc_lines.lines[i];
  336. int real_line;
  337. void *buf;
  338. if (!line)
  339. break;
  340. /* FIXME: include hack for certain Decklink cards which mis-represent
  341. line numbers for pSF frames */
  342. real_line = line->line_number;
  343. result = vanc->GetBufferForVerticalBlankingLine(real_line, &buf);
  344. if (result != S_OK) {
  345. av_log(avctx, AV_LOG_ERROR, "Failed to get VANC line %d: %d", real_line, result);
  346. continue;
  347. }
  348. /* Generate the full line taking into account all VANC packets on that line */
  349. result = klvanc_generate_vanc_line_v210(ctx->vanc_ctx, line, (uint8_t *) buf,
  350. ctx->bmd_width);
  351. if (result) {
  352. av_log(avctx, AV_LOG_ERROR, "Failed to generate VANC line\n");
  353. continue;
  354. }
  355. }
  356. result = frame->SetAncillaryData(vanc);
  357. vanc->Release();
  358. if (result != S_OK) {
  359. av_log(avctx, AV_LOG_ERROR, "Failed to set vanc: %d", result);
  360. ret = AVERROR(EIO);
  361. }
  362. done:
  363. for (i = 0; i < vanc_lines.num_lines; i++)
  364. klvanc_line_free(vanc_lines.lines[i]);
  365. return ret;
  366. }
  367. #endif
  368. static int decklink_write_video_packet(AVFormatContext *avctx, AVPacket *pkt)
  369. {
  370. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  371. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  372. AVStream *st = avctx->streams[pkt->stream_index];
  373. AVFrame *avframe = NULL, *tmp = (AVFrame *)pkt->data;
  374. AVPacket *avpacket = NULL;
  375. decklink_frame *frame;
  376. buffercount_type buffered;
  377. HRESULT hr;
  378. if (st->codecpar->codec_id == AV_CODEC_ID_WRAPPED_AVFRAME) {
  379. if (tmp->format != AV_PIX_FMT_UYVY422 ||
  380. tmp->width != ctx->bmd_width ||
  381. tmp->height != ctx->bmd_height) {
  382. av_log(avctx, AV_LOG_ERROR, "Got a frame with invalid pixel format or dimension.\n");
  383. return AVERROR(EINVAL);
  384. }
  385. avframe = av_frame_clone(tmp);
  386. if (!avframe) {
  387. av_log(avctx, AV_LOG_ERROR, "Could not clone video frame.\n");
  388. return AVERROR(EIO);
  389. }
  390. frame = new decklink_frame(ctx, avframe, st->codecpar->codec_id, avframe->height, avframe->width);
  391. } else {
  392. avpacket = av_packet_clone(pkt);
  393. if (!avpacket) {
  394. av_log(avctx, AV_LOG_ERROR, "Could not clone video frame.\n");
  395. return AVERROR(EIO);
  396. }
  397. frame = new decklink_frame(ctx, avpacket, st->codecpar->codec_id, ctx->bmd_height, ctx->bmd_width);
  398. #if CONFIG_LIBKLVANC
  399. if (decklink_construct_vanc(avctx, ctx, pkt, frame))
  400. av_log(avctx, AV_LOG_ERROR, "Failed to construct VANC\n");
  401. #endif
  402. }
  403. if (!frame) {
  404. av_log(avctx, AV_LOG_ERROR, "Could not create new frame.\n");
  405. av_frame_free(&avframe);
  406. av_packet_free(&avpacket);
  407. return AVERROR(EIO);
  408. }
  409. /* Always keep at most one second of frames buffered. */
  410. pthread_mutex_lock(&ctx->mutex);
  411. while (ctx->frames_buffer_available_spots == 0) {
  412. pthread_cond_wait(&ctx->cond, &ctx->mutex);
  413. }
  414. ctx->frames_buffer_available_spots--;
  415. pthread_mutex_unlock(&ctx->mutex);
  416. /* Schedule frame for playback. */
  417. hr = ctx->dlo->ScheduleVideoFrame((class IDeckLinkVideoFrame *) frame,
  418. pkt->pts * ctx->bmd_tb_num,
  419. ctx->bmd_tb_num, ctx->bmd_tb_den);
  420. /* Pass ownership to DeckLink, or release on failure */
  421. frame->Release();
  422. if (hr != S_OK) {
  423. av_log(avctx, AV_LOG_ERROR, "Could not schedule video frame."
  424. " error %08x.\n", (uint32_t) hr);
  425. return AVERROR(EIO);
  426. }
  427. ctx->dlo->GetBufferedVideoFrameCount(&buffered);
  428. av_log(avctx, AV_LOG_DEBUG, "Buffered video frames: %d.\n", (int) buffered);
  429. if (pkt->pts > 2 && buffered <= 2)
  430. av_log(avctx, AV_LOG_WARNING, "There are not enough buffered video frames."
  431. " Video may misbehave!\n");
  432. /* Preroll video frames. */
  433. if (!ctx->playback_started && pkt->pts > ctx->frames_preroll) {
  434. av_log(avctx, AV_LOG_DEBUG, "Ending audio preroll.\n");
  435. if (ctx->audio && ctx->dlo->EndAudioPreroll() != S_OK) {
  436. av_log(avctx, AV_LOG_ERROR, "Could not end audio preroll!\n");
  437. return AVERROR(EIO);
  438. }
  439. av_log(avctx, AV_LOG_DEBUG, "Starting scheduled playback.\n");
  440. if (ctx->dlo->StartScheduledPlayback(0, ctx->bmd_tb_den, 1.0) != S_OK) {
  441. av_log(avctx, AV_LOG_ERROR, "Could not start scheduled playback!\n");
  442. return AVERROR(EIO);
  443. }
  444. ctx->playback_started = 1;
  445. }
  446. return 0;
  447. }
  448. static int decklink_write_audio_packet(AVFormatContext *avctx, AVPacket *pkt)
  449. {
  450. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  451. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  452. int sample_count = pkt->size / (ctx->channels << 1);
  453. buffercount_type buffered;
  454. ctx->dlo->GetBufferedAudioSampleFrameCount(&buffered);
  455. if (pkt->pts > 1 && !buffered)
  456. av_log(avctx, AV_LOG_WARNING, "There's no buffered audio."
  457. " Audio will misbehave!\n");
  458. if (ctx->dlo->ScheduleAudioSamples(pkt->data, sample_count, pkt->pts,
  459. bmdAudioSampleRate48kHz, NULL) != S_OK) {
  460. av_log(avctx, AV_LOG_ERROR, "Could not schedule audio samples.\n");
  461. return AVERROR(EIO);
  462. }
  463. return 0;
  464. }
  465. extern "C" {
  466. av_cold int ff_decklink_write_header(AVFormatContext *avctx)
  467. {
  468. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  469. struct decklink_ctx *ctx;
  470. unsigned int n;
  471. int ret;
  472. ctx = (struct decklink_ctx *) av_mallocz(sizeof(struct decklink_ctx));
  473. if (!ctx)
  474. return AVERROR(ENOMEM);
  475. ctx->list_devices = cctx->list_devices;
  476. ctx->list_formats = cctx->list_formats;
  477. ctx->preroll = cctx->preroll;
  478. ctx->duplex_mode = cctx->duplex_mode;
  479. cctx->ctx = ctx;
  480. #if CONFIG_LIBKLVANC
  481. if (klvanc_context_create(&ctx->vanc_ctx) < 0) {
  482. av_log(avctx, AV_LOG_ERROR, "Cannot create VANC library context\n");
  483. return AVERROR(ENOMEM);
  484. }
  485. ctx->supports_vanc = 1;
  486. #endif
  487. /* List available devices and exit. */
  488. if (ctx->list_devices) {
  489. ff_decklink_list_devices_legacy(avctx, 0, 1);
  490. return AVERROR_EXIT;
  491. }
  492. ret = ff_decklink_init_device(avctx, avctx->url);
  493. if (ret < 0)
  494. return ret;
  495. /* Get output device. */
  496. if (ctx->dl->QueryInterface(IID_IDeckLinkOutput, (void **) &ctx->dlo) != S_OK) {
  497. av_log(avctx, AV_LOG_ERROR, "Could not open output device from '%s'\n",
  498. avctx->url);
  499. ret = AVERROR(EIO);
  500. goto error;
  501. }
  502. /* List supported formats. */
  503. if (ctx->list_formats) {
  504. ff_decklink_list_formats(avctx);
  505. ret = AVERROR_EXIT;
  506. goto error;
  507. }
  508. /* Setup streams. */
  509. ret = AVERROR(EIO);
  510. for (n = 0; n < avctx->nb_streams; n++) {
  511. AVStream *st = avctx->streams[n];
  512. AVCodecParameters *c = st->codecpar;
  513. if (c->codec_type == AVMEDIA_TYPE_AUDIO) {
  514. if (decklink_setup_audio(avctx, st))
  515. goto error;
  516. } else if (c->codec_type == AVMEDIA_TYPE_VIDEO) {
  517. if (decklink_setup_video(avctx, st))
  518. goto error;
  519. } else {
  520. av_log(avctx, AV_LOG_ERROR, "Unsupported stream type.\n");
  521. goto error;
  522. }
  523. }
  524. return 0;
  525. error:
  526. ff_decklink_cleanup(avctx);
  527. return ret;
  528. }
  529. int ff_decklink_write_packet(AVFormatContext *avctx, AVPacket *pkt)
  530. {
  531. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  532. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  533. AVStream *st = avctx->streams[pkt->stream_index];
  534. ctx->last_pts = FFMAX(ctx->last_pts, pkt->pts);
  535. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
  536. return decklink_write_video_packet(avctx, pkt);
  537. else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
  538. return decklink_write_audio_packet(avctx, pkt);
  539. return AVERROR(EIO);
  540. }
  541. int ff_decklink_list_output_devices(AVFormatContext *avctx, struct AVDeviceInfoList *device_list)
  542. {
  543. return ff_decklink_list_devices(avctx, device_list, 0, 1);
  544. }
  545. } /* extern "C" */