decklink_enc.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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 <DeckLinkAPI.h>
  22. #ifdef _WIN32
  23. #include <DeckLinkAPI_i.c>
  24. typedef unsigned long buffercount_type;
  25. #else
  26. #include <DeckLinkAPIDispatch.cpp>
  27. typedef uint32_t buffercount_type;
  28. #endif
  29. #include <pthread.h>
  30. #include <semaphore.h>
  31. extern "C" {
  32. #include "libavformat/avformat.h"
  33. #include "libavformat/internal.h"
  34. #include "libavutil/imgutils.h"
  35. }
  36. #include "decklink_enc.h"
  37. class decklink_callback;
  38. struct decklink_ctx {
  39. /* DeckLink SDK interfaces */
  40. IDeckLink *dl;
  41. IDeckLinkOutput *dlo;
  42. decklink_callback *callback;
  43. /* DeckLink mode information */
  44. IDeckLinkDisplayModeIterator *itermode;
  45. BMDTimeValue bmd_tb_den;
  46. BMDTimeValue bmd_tb_num;
  47. BMDDisplayMode bmd_mode;
  48. int bmd_width;
  49. int bmd_height;
  50. /* Streams present */
  51. int audio;
  52. int video;
  53. /* Status */
  54. int playback_started;
  55. int64_t last_pts;
  56. /* Options */
  57. int list_devices;
  58. int list_formats;
  59. double preroll;
  60. int frames_preroll;
  61. int frames_buffer;
  62. sem_t semaphore;
  63. int channels;
  64. };
  65. /* DeckLink callback class declaration */
  66. class decklink_frame : public IDeckLinkVideoFrame
  67. {
  68. public:
  69. decklink_frame(struct decklink_ctx *ctx, AVFrame *avframe, long width,
  70. long height, void *buffer) :
  71. _ctx(ctx), _avframe(avframe), _width(width),
  72. _height(height), _buffer(buffer), _refs(0) { }
  73. virtual long STDMETHODCALLTYPE GetWidth (void) { return _width; }
  74. virtual long STDMETHODCALLTYPE GetHeight (void) { return _height; }
  75. virtual long STDMETHODCALLTYPE GetRowBytes (void) { return _width<<1; }
  76. virtual BMDPixelFormat STDMETHODCALLTYPE GetPixelFormat(void) { return bmdFormat8BitYUV; }
  77. virtual BMDFrameFlags STDMETHODCALLTYPE GetFlags (void) { return bmdVideoOutputFlagDefault; }
  78. virtual HRESULT STDMETHODCALLTYPE GetBytes (void **buffer) { *buffer = _buffer; return S_OK; }
  79. virtual HRESULT STDMETHODCALLTYPE GetTimecode (BMDTimecodeFormat format, IDeckLinkTimecode **timecode) { return S_FALSE; }
  80. virtual HRESULT STDMETHODCALLTYPE GetAncillaryData(IDeckLinkVideoFrameAncillary **ancillary) { return S_FALSE; }
  81. virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; }
  82. virtual ULONG STDMETHODCALLTYPE AddRef(void) { return ++_refs; }
  83. virtual ULONG STDMETHODCALLTYPE Release(void) { if (!--_refs) delete this; return _refs; }
  84. struct decklink_ctx *_ctx;
  85. AVFrame *_avframe;
  86. private:
  87. long _width;
  88. long _height;
  89. void *_buffer;
  90. int _refs;
  91. };
  92. class decklink_callback : public IDeckLinkVideoOutputCallback
  93. {
  94. public:
  95. virtual HRESULT STDMETHODCALLTYPE ScheduledFrameCompleted(IDeckLinkVideoFrame *_frame, BMDOutputFrameCompletionResult result)
  96. {
  97. decklink_frame *frame = static_cast<decklink_frame *>(_frame);
  98. struct decklink_ctx *ctx = frame->_ctx;
  99. AVFrame *avframe = frame->_avframe;
  100. av_frame_free(&avframe);
  101. sem_post(&ctx->semaphore);
  102. return S_OK;
  103. }
  104. virtual HRESULT STDMETHODCALLTYPE ScheduledPlaybackHasStopped(void) { return S_OK; }
  105. virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; }
  106. virtual ULONG STDMETHODCALLTYPE AddRef(void) { return 1; }
  107. virtual ULONG STDMETHODCALLTYPE Release(void) { return 1; }
  108. };
  109. #ifdef _WIN32
  110. static IDeckLinkIterator *CreateDeckLinkIteratorInstance(void)
  111. {
  112. IDeckLinkIterator *iter;
  113. if (CoInitialize(NULL) != S_OK) {
  114. av_log(NULL, AV_LOG_ERROR, "COM initialization failed.\n");
  115. return NULL;
  116. }
  117. if (CoCreateInstance(CLSID_CDeckLinkIterator, NULL, CLSCTX_ALL,
  118. IID_IDeckLinkIterator, (void**) &iter) != S_OK) {
  119. av_log(NULL, AV_LOG_ERROR, "DeckLink drivers not installed.\n");
  120. return NULL;
  121. }
  122. return iter;
  123. }
  124. #endif
  125. /* free() is needed for a string returned by the DeckLink SDL. */
  126. #undef free
  127. #ifdef _WIN32
  128. static char *dup_wchar_to_utf8(wchar_t *w)
  129. {
  130. char *s = NULL;
  131. int l = WideCharToMultiByte(CP_UTF8, 0, w, -1, 0, 0, 0, 0);
  132. s = (char *) av_malloc(l);
  133. if (s)
  134. WideCharToMultiByte(CP_UTF8, 0, w, -1, s, l, 0, 0);
  135. return s;
  136. }
  137. #define DECKLINK_STR OLECHAR *
  138. #define DECKLINK_STRDUP dup_wchar_to_utf8
  139. #else
  140. #define DECKLINK_STR const char *
  141. #define DECKLINK_STRDUP av_strdup
  142. #endif
  143. static HRESULT IDeckLink_GetDisplayName(IDeckLink *This, const char **displayName)
  144. {
  145. DECKLINK_STR tmpDisplayName;
  146. HRESULT hr = This->GetDisplayName(&tmpDisplayName);
  147. if (hr != S_OK)
  148. return hr;
  149. *displayName = DECKLINK_STRDUP(tmpDisplayName);
  150. free((void *) tmpDisplayName);
  151. return hr;
  152. }
  153. static int decklink_set_format(struct decklink_ctx *ctx,
  154. int width, int height,
  155. int tb_num, int tb_den)
  156. {
  157. BMDDisplayModeSupport support;
  158. IDeckLinkDisplayMode *mode;
  159. if (tb_num == 1) {
  160. tb_num *= 1000;
  161. tb_den *= 1000;
  162. }
  163. ctx->bmd_mode = bmdModeUnknown;
  164. while ((ctx->bmd_mode == bmdModeUnknown) && ctx->itermode->Next(&mode) == S_OK) {
  165. BMDTimeValue bmd_tb_num, bmd_tb_den;
  166. int bmd_width = mode->GetWidth();
  167. int bmd_height = mode->GetHeight();
  168. mode->GetFrameRate(&bmd_tb_num, &bmd_tb_den);
  169. if (bmd_width == width && bmd_height == height &&
  170. bmd_tb_num == tb_num && bmd_tb_den == tb_den) {
  171. ctx->bmd_mode = mode->GetDisplayMode();
  172. ctx->bmd_width = bmd_width;
  173. ctx->bmd_height = bmd_height;
  174. ctx->bmd_tb_den = bmd_tb_den;
  175. ctx->bmd_tb_num = bmd_tb_num;
  176. }
  177. mode->Release();
  178. }
  179. if (ctx->bmd_mode == bmdModeUnknown)
  180. return -1;
  181. if (ctx->dlo->DoesSupportVideoMode(ctx->bmd_mode, bmdFormat8BitYUV,
  182. bmdVideoOutputFlagDefault,
  183. &support, NULL) != S_OK)
  184. return -1;
  185. if (support == bmdDisplayModeSupported)
  186. return 0;
  187. return -1;
  188. }
  189. static int decklink_setup_video(AVFormatContext *avctx, AVStream *st)
  190. {
  191. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  192. struct decklink_ctx *ctx = (struct decklink_ctx *) cctx->ctx;
  193. AVCodecContext *c = st->codec;
  194. if (ctx->video) {
  195. av_log(avctx, AV_LOG_ERROR, "Only one video stream is supported!\n");
  196. return -1;
  197. }
  198. if (c->pix_fmt != AV_PIX_FMT_UYVY422) {
  199. av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format!"
  200. " Only AV_PIX_FMT_UYVY422 is supported.\n");
  201. return -1;
  202. }
  203. if (decklink_set_format(ctx, c->width, c->height,
  204. c->time_base.num, c->time_base.den)) {
  205. av_log(avctx, AV_LOG_ERROR, "Unsupported video size or framerate!"
  206. " Check available formats with -list_formats 1.\n");
  207. return -1;
  208. }
  209. if (ctx->dlo->EnableVideoOutput(ctx->bmd_mode,
  210. bmdVideoOutputFlagDefault) != S_OK) {
  211. av_log(avctx, AV_LOG_ERROR, "Could not enable video output!\n");
  212. return -1;
  213. }
  214. /* Set callback. */
  215. ctx->callback = new decklink_callback();
  216. ctx->dlo->SetScheduledFrameCompletionCallback(ctx->callback);
  217. /* Start video semaphore. */
  218. ctx->frames_preroll = c->time_base.den * ctx->preroll;
  219. if (c->time_base.den > 1000)
  220. ctx->frames_preroll /= 1000;
  221. /* Buffer twice as many frames as the preroll. */
  222. ctx->frames_buffer = ctx->frames_preroll * 2;
  223. ctx->frames_buffer = FFMIN(ctx->frames_buffer, 60);
  224. sem_init(&ctx->semaphore, 0, ctx->frames_buffer);
  225. /* The device expects the framerate to be fixed. */
  226. avpriv_set_pts_info(st, 64, c->time_base.num, c->time_base.den);
  227. ctx->video = 1;
  228. return 0;
  229. }
  230. static int decklink_setup_audio(AVFormatContext *avctx, AVStream *st)
  231. {
  232. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  233. struct decklink_ctx *ctx = (struct decklink_ctx *) cctx->ctx;
  234. AVCodecContext *c = st->codec;
  235. if (ctx->audio) {
  236. av_log(avctx, AV_LOG_ERROR, "Only one audio stream is supported!\n");
  237. return -1;
  238. }
  239. if (c->sample_rate != 48000) {
  240. av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate!"
  241. " Only 48kHz is supported.\n");
  242. return -1;
  243. }
  244. if (c->channels != 2 && c->channels != 8) {
  245. av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels!"
  246. " Only stereo and 7.1 are supported.\n");
  247. return -1;
  248. }
  249. if (ctx->dlo->EnableAudioOutput(bmdAudioSampleRate48kHz,
  250. bmdAudioSampleType16bitInteger,
  251. c->channels,
  252. bmdAudioOutputStreamTimestamped) != S_OK) {
  253. av_log(avctx, AV_LOG_ERROR, "Could not enable audio output!\n");
  254. return -1;
  255. }
  256. if (ctx->dlo->BeginAudioPreroll() != S_OK) {
  257. av_log(avctx, AV_LOG_ERROR, "Could not begin audio preroll!\n");
  258. return -1;
  259. }
  260. /* The device expects the sample rate to be fixed. */
  261. avpriv_set_pts_info(st, 64, 1, c->sample_rate);
  262. ctx->channels = c->channels;
  263. ctx->audio = 1;
  264. return 0;
  265. }
  266. av_cold int ff_decklink_write_trailer(AVFormatContext *avctx)
  267. {
  268. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  269. struct decklink_ctx *ctx = (struct decklink_ctx *) cctx->ctx;
  270. if (ctx->playback_started) {
  271. BMDTimeValue actual;
  272. ctx->dlo->StopScheduledPlayback(ctx->last_pts * ctx->bmd_tb_num,
  273. &actual, ctx->bmd_tb_den);
  274. ctx->dlo->DisableVideoOutput();
  275. if (ctx->audio)
  276. ctx->dlo->DisableAudioOutput();
  277. }
  278. if (ctx->dlo)
  279. ctx->dlo->Release();
  280. if (ctx->dl)
  281. ctx->dl->Release();
  282. if (ctx->callback)
  283. delete ctx->callback;
  284. sem_destroy(&ctx->semaphore);
  285. av_freep(&cctx->ctx);
  286. return 0;
  287. }
  288. static int decklink_write_video_packet(AVFormatContext *avctx, AVPacket *pkt)
  289. {
  290. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  291. struct decklink_ctx *ctx = (struct decklink_ctx *) cctx->ctx;
  292. AVPicture *avpicture = (AVPicture *) pkt->data;
  293. AVFrame *avframe, *tmp;
  294. decklink_frame *frame;
  295. buffercount_type buffered;
  296. HRESULT hr;
  297. /* HACK while av_uncoded_frame() isn't implemented */
  298. int ret;
  299. tmp = av_frame_alloc();
  300. if (!tmp)
  301. return AVERROR(ENOMEM);
  302. tmp->format = AV_PIX_FMT_UYVY422;
  303. tmp->width = ctx->bmd_width;
  304. tmp->height = ctx->bmd_height;
  305. ret = av_frame_get_buffer(tmp, 32);
  306. if (ret < 0) {
  307. av_frame_free(&tmp);
  308. return ret;
  309. }
  310. av_image_copy(tmp->data, tmp->linesize, (const uint8_t **) avpicture->data,
  311. avpicture->linesize, (AVPixelFormat) tmp->format, tmp->width,
  312. tmp->height);
  313. avframe = av_frame_clone(tmp);
  314. av_frame_free(&tmp);
  315. if (!avframe) {
  316. av_log(avctx, AV_LOG_ERROR, "Could not clone video frame.\n");
  317. return AVERROR(EIO);
  318. }
  319. /* end HACK */
  320. frame = new decklink_frame(ctx, avframe, ctx->bmd_width, ctx->bmd_height,
  321. (void *) avframe->data[0]);
  322. if (!frame) {
  323. av_log(avctx, AV_LOG_ERROR, "Could not create new frame.\n");
  324. return AVERROR(EIO);
  325. }
  326. /* Always keep at most one second of frames buffered. */
  327. sem_wait(&ctx->semaphore);
  328. /* Schedule frame for playback. */
  329. hr = ctx->dlo->ScheduleVideoFrame((struct IDeckLinkVideoFrame *) frame,
  330. pkt->pts * ctx->bmd_tb_num,
  331. ctx->bmd_tb_num, ctx->bmd_tb_den);
  332. if (hr != S_OK) {
  333. av_log(avctx, AV_LOG_ERROR, "Could not schedule video frame."
  334. " error %08x.\n", (uint32_t) hr);
  335. frame->Release();
  336. return AVERROR(EIO);
  337. }
  338. ctx->dlo->GetBufferedVideoFrameCount(&buffered);
  339. av_log(avctx, AV_LOG_DEBUG, "Buffered video frames: %d.\n", (int) buffered);
  340. if (pkt->pts > 2 && buffered <= 2)
  341. av_log(avctx, AV_LOG_WARNING, "There are not enough buffered video frames."
  342. " Video may misbehave!\n");
  343. /* Preroll video frames. */
  344. if (!ctx->playback_started && pkt->pts > ctx->frames_preroll) {
  345. av_log(avctx, AV_LOG_DEBUG, "Ending audio preroll.\n");
  346. if (ctx->audio && ctx->dlo->EndAudioPreroll() != S_OK) {
  347. av_log(avctx, AV_LOG_ERROR, "Could not end audio preroll!\n");
  348. return AVERROR(EIO);
  349. }
  350. av_log(avctx, AV_LOG_DEBUG, "Starting scheduled playback.\n");
  351. if (ctx->dlo->StartScheduledPlayback(0, ctx->bmd_tb_den, 1.0) != S_OK) {
  352. av_log(avctx, AV_LOG_ERROR, "Could not start scheduled playback!\n");
  353. return AVERROR(EIO);
  354. }
  355. ctx->playback_started = 1;
  356. }
  357. return 0;
  358. }
  359. static int decklink_write_audio_packet(AVFormatContext *avctx, AVPacket *pkt)
  360. {
  361. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  362. struct decklink_ctx *ctx = (struct decklink_ctx *) cctx->ctx;
  363. int sample_count = pkt->size / (ctx->channels << 1);
  364. buffercount_type buffered;
  365. ctx->dlo->GetBufferedAudioSampleFrameCount(&buffered);
  366. if (pkt->pts > 1 && !buffered)
  367. av_log(avctx, AV_LOG_WARNING, "There's no buffered audio."
  368. " Audio will misbehave!\n");
  369. if (ctx->dlo->ScheduleAudioSamples(pkt->data, sample_count, pkt->pts,
  370. bmdAudioSampleRate48kHz, NULL) != S_OK) {
  371. av_log(avctx, AV_LOG_ERROR, "Could not schedule audio samples.\n");
  372. return AVERROR(EIO);
  373. }
  374. return 0;
  375. }
  376. extern "C" {
  377. av_cold int ff_decklink_write_header(AVFormatContext *avctx)
  378. {
  379. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  380. struct decklink_ctx *ctx;
  381. IDeckLinkIterator *iter;
  382. IDeckLink *dl = NULL;
  383. unsigned int n;
  384. ctx = (struct decklink_ctx *) av_mallocz(sizeof(struct decklink_ctx));
  385. if (!ctx)
  386. return AVERROR(ENOMEM);
  387. ctx->list_devices = cctx->list_devices;
  388. ctx->list_formats = cctx->list_formats;
  389. ctx->preroll = cctx->preroll;
  390. cctx->ctx = ctx;
  391. iter = CreateDeckLinkIteratorInstance();
  392. if (!iter) {
  393. av_log(avctx, AV_LOG_ERROR, "Could not create DeckLink iterator\n");
  394. return AVERROR(EIO);
  395. }
  396. /* List available devices. */
  397. if (ctx->list_devices) {
  398. av_log(avctx, AV_LOG_INFO, "Blackmagic DeckLink devices:\n");
  399. while (iter->Next(&dl) == S_OK) {
  400. const char *displayName;
  401. IDeckLink_GetDisplayName(dl, &displayName);
  402. av_log(avctx, AV_LOG_INFO, "\t'%s'\n", displayName);
  403. av_free((void *) displayName);
  404. dl->Release();
  405. }
  406. iter->Release();
  407. return AVERROR_EXIT;
  408. }
  409. /* Open device. */
  410. while (iter->Next(&dl) == S_OK) {
  411. const char *displayName;
  412. IDeckLink_GetDisplayName(dl, &displayName);
  413. if (!strcmp(avctx->filename, displayName)) {
  414. av_free((void *) displayName);
  415. ctx->dl = dl;
  416. break;
  417. }
  418. av_free((void *) displayName);
  419. dl->Release();
  420. }
  421. iter->Release();
  422. if (!ctx->dl) {
  423. av_log(avctx, AV_LOG_ERROR, "Could not open '%s'\n", avctx->filename);
  424. return AVERROR(EIO);
  425. }
  426. /* Get output device. */
  427. if (ctx->dl->QueryInterface(IID_IDeckLinkOutput, (void **) &ctx->dlo) != S_OK) {
  428. av_log(avctx, AV_LOG_ERROR, "Could not open output device from '%s'\n",
  429. avctx->filename);
  430. ctx->dl->Release();
  431. return AVERROR(EIO);
  432. }
  433. if (ctx->dlo->GetDisplayModeIterator(&ctx->itermode) != S_OK) {
  434. av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
  435. ctx->dl->Release();
  436. return AVERROR(EIO);
  437. }
  438. /* List supported formats. */
  439. if (ctx->list_formats) {
  440. IDeckLinkDisplayMode *mode;
  441. av_log(avctx, AV_LOG_INFO, "Supported formats for '%s':\n",
  442. avctx->filename);
  443. while (ctx->itermode->Next(&mode) == S_OK) {
  444. BMDTimeValue tb_num, tb_den;
  445. mode->GetFrameRate(&tb_num, &tb_den);
  446. av_log(avctx, AV_LOG_INFO, "\t%ldx%ld at %d/%d fps",
  447. mode->GetWidth(), mode->GetHeight(),
  448. (int) tb_den, (int) tb_num);
  449. switch (mode->GetFieldDominance()) {
  450. case bmdLowerFieldFirst:
  451. av_log(avctx, AV_LOG_INFO, " (interlaced, lower field first)"); break;
  452. case bmdUpperFieldFirst:
  453. av_log(avctx, AV_LOG_INFO, " (interlaced, upper field first)"); break;
  454. }
  455. av_log(avctx, AV_LOG_INFO, "\n");
  456. mode->Release();
  457. }
  458. ctx->itermode->Release();
  459. ctx->dlo->Release();
  460. ctx->dl->Release();
  461. return AVERROR_EXIT;
  462. }
  463. /* Setup streams. */
  464. for (n = 0; n < avctx->nb_streams; n++) {
  465. AVStream *st = avctx->streams[n];
  466. AVCodecContext *c = st->codec;
  467. if (c->codec_type == AVMEDIA_TYPE_AUDIO) {
  468. if (decklink_setup_audio(avctx, st))
  469. goto error;
  470. } else if (c->codec_type == AVMEDIA_TYPE_VIDEO) {
  471. if (decklink_setup_video(avctx, st))
  472. goto error;
  473. } else {
  474. av_log(avctx, AV_LOG_ERROR, "Unsupported stream type.\n");
  475. goto error;
  476. }
  477. }
  478. ctx->itermode->Release();
  479. return 0;
  480. error:
  481. ctx->dlo->Release();
  482. ctx->dl->Release();
  483. return AVERROR(EIO);
  484. }
  485. int ff_decklink_write_packet(AVFormatContext *avctx, AVPacket *pkt)
  486. {
  487. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  488. struct decklink_ctx *ctx = (struct decklink_ctx *) cctx->ctx;
  489. AVStream *st = avctx->streams[pkt->stream_index];
  490. ctx->last_pts = FFMAX(ctx->last_pts, pkt->pts);
  491. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
  492. return decklink_write_video_packet(avctx, pkt);
  493. else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
  494. return decklink_write_audio_packet(avctx, pkt);
  495. return AVERROR(EIO);
  496. }
  497. } /* extern "C" */