decklink_enc.cpp 22 KB

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