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.

653 lines
19KB

  1. /*
  2. * Blackmagic DeckLink output
  3. * Copyright (c) 2013-2014 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 <DeckLinkAPI.h>
  22. #include <pthread.h>
  23. #include <semaphore.h>
  24. extern "C" {
  25. #include "config.h"
  26. #include "libavformat/avformat.h"
  27. #include "libavformat/internal.h"
  28. #include "libavutil/imgutils.h"
  29. #if CONFIG_LIBZVBI
  30. #include <libzvbi.h>
  31. #endif
  32. }
  33. #include "decklink_common.h"
  34. #include "decklink_dec.h"
  35. #if CONFIG_LIBZVBI
  36. static uint8_t calc_parity_and_line_offset(int line)
  37. {
  38. uint8_t ret = (line < 313) << 5;
  39. if (line >= 7 && line <= 22)
  40. ret += line;
  41. if (line >= 320 && line <= 335)
  42. ret += (line - 313);
  43. return ret;
  44. }
  45. int teletext_data_unit_from_vbi_data(int line, uint8_t *src, uint8_t *tgt)
  46. {
  47. vbi_bit_slicer slicer;
  48. vbi_bit_slicer_init(&slicer, 720, 13500000, 6937500, 6937500, 0x00aaaae4, 0xffff, 18, 6, 42 * 8, VBI_MODULATION_NRZ_MSB, VBI_PIXFMT_UYVY);
  49. if (vbi_bit_slice(&slicer, src, tgt + 4) == FALSE)
  50. return -1;
  51. tgt[0] = 0x02; // data_unit_id
  52. tgt[1] = 0x2c; // data_unit_length
  53. tgt[2] = calc_parity_and_line_offset(line); // field_parity, line_offset
  54. tgt[3] = 0xe4; // framing code
  55. return 0;
  56. }
  57. #endif
  58. static void avpacket_queue_init(AVFormatContext *avctx, AVPacketQueue *q)
  59. {
  60. memset(q, 0, sizeof(AVPacketQueue));
  61. pthread_mutex_init(&q->mutex, NULL);
  62. pthread_cond_init(&q->cond, NULL);
  63. q->avctx = avctx;
  64. }
  65. static void avpacket_queue_flush(AVPacketQueue *q)
  66. {
  67. AVPacketList *pkt, *pkt1;
  68. pthread_mutex_lock(&q->mutex);
  69. for (pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
  70. pkt1 = pkt->next;
  71. av_packet_unref(&pkt->pkt);
  72. av_freep(&pkt);
  73. }
  74. q->last_pkt = NULL;
  75. q->first_pkt = NULL;
  76. q->nb_packets = 0;
  77. q->size = 0;
  78. pthread_mutex_unlock(&q->mutex);
  79. }
  80. static void avpacket_queue_end(AVPacketQueue *q)
  81. {
  82. avpacket_queue_flush(q);
  83. pthread_mutex_destroy(&q->mutex);
  84. pthread_cond_destroy(&q->cond);
  85. }
  86. static unsigned long long avpacket_queue_size(AVPacketQueue *q)
  87. {
  88. unsigned long long size;
  89. pthread_mutex_lock(&q->mutex);
  90. size = q->size;
  91. pthread_mutex_unlock(&q->mutex);
  92. return size;
  93. }
  94. static int avpacket_queue_put(AVPacketQueue *q, AVPacket *pkt)
  95. {
  96. AVPacketList *pkt1;
  97. // Drop Packet if queue size is > 1GB
  98. if (avpacket_queue_size(q) > 1024 * 1024 * 1024 ) {
  99. av_log(q->avctx, AV_LOG_WARNING, "Decklink input buffer overrun!\n");
  100. return -1;
  101. }
  102. /* duplicate the packet */
  103. if (av_dup_packet(pkt) < 0) {
  104. return -1;
  105. }
  106. pkt1 = (AVPacketList *)av_malloc(sizeof(AVPacketList));
  107. if (!pkt1) {
  108. return -1;
  109. }
  110. pkt1->pkt = *pkt;
  111. pkt1->next = NULL;
  112. pthread_mutex_lock(&q->mutex);
  113. if (!q->last_pkt) {
  114. q->first_pkt = pkt1;
  115. } else {
  116. q->last_pkt->next = pkt1;
  117. }
  118. q->last_pkt = pkt1;
  119. q->nb_packets++;
  120. q->size += pkt1->pkt.size + sizeof(*pkt1);
  121. pthread_cond_signal(&q->cond);
  122. pthread_mutex_unlock(&q->mutex);
  123. return 0;
  124. }
  125. static int avpacket_queue_get(AVPacketQueue *q, AVPacket *pkt, int block)
  126. {
  127. AVPacketList *pkt1;
  128. int ret;
  129. pthread_mutex_lock(&q->mutex);
  130. for (;; ) {
  131. pkt1 = q->first_pkt;
  132. if (pkt1) {
  133. q->first_pkt = pkt1->next;
  134. if (!q->first_pkt) {
  135. q->last_pkt = NULL;
  136. }
  137. q->nb_packets--;
  138. q->size -= pkt1->pkt.size + sizeof(*pkt1);
  139. *pkt = pkt1->pkt;
  140. av_free(pkt1);
  141. ret = 1;
  142. break;
  143. } else if (!block) {
  144. ret = 0;
  145. break;
  146. } else {
  147. pthread_cond_wait(&q->cond, &q->mutex);
  148. }
  149. }
  150. pthread_mutex_unlock(&q->mutex);
  151. return ret;
  152. }
  153. class decklink_input_callback : public IDeckLinkInputCallback
  154. {
  155. public:
  156. decklink_input_callback(AVFormatContext *_avctx);
  157. ~decklink_input_callback();
  158. virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; }
  159. virtual ULONG STDMETHODCALLTYPE AddRef(void);
  160. virtual ULONG STDMETHODCALLTYPE Release(void);
  161. virtual HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(BMDVideoInputFormatChangedEvents, IDeckLinkDisplayMode*, BMDDetectedVideoInputFormatFlags);
  162. virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(IDeckLinkVideoInputFrame*, IDeckLinkAudioInputPacket*);
  163. private:
  164. ULONG m_refCount;
  165. pthread_mutex_t m_mutex;
  166. AVFormatContext *avctx;
  167. decklink_ctx *ctx;
  168. int no_video;
  169. int64_t initial_video_pts;
  170. int64_t initial_audio_pts;
  171. };
  172. decklink_input_callback::decklink_input_callback(AVFormatContext *_avctx) : m_refCount(0)
  173. {
  174. avctx = _avctx;
  175. decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  176. ctx = (struct decklink_ctx *) cctx->ctx;
  177. initial_audio_pts = initial_video_pts = AV_NOPTS_VALUE;
  178. pthread_mutex_init(&m_mutex, NULL);
  179. }
  180. decklink_input_callback::~decklink_input_callback()
  181. {
  182. pthread_mutex_destroy(&m_mutex);
  183. }
  184. ULONG decklink_input_callback::AddRef(void)
  185. {
  186. pthread_mutex_lock(&m_mutex);
  187. m_refCount++;
  188. pthread_mutex_unlock(&m_mutex);
  189. return (ULONG)m_refCount;
  190. }
  191. ULONG decklink_input_callback::Release(void)
  192. {
  193. pthread_mutex_lock(&m_mutex);
  194. m_refCount--;
  195. pthread_mutex_unlock(&m_mutex);
  196. if (m_refCount == 0) {
  197. delete this;
  198. return 0;
  199. }
  200. return (ULONG)m_refCount;
  201. }
  202. HRESULT decklink_input_callback::VideoInputFrameArrived(
  203. IDeckLinkVideoInputFrame *videoFrame, IDeckLinkAudioInputPacket *audioFrame)
  204. {
  205. void *frameBytes;
  206. void *audioFrameBytes;
  207. BMDTimeValue frameTime;
  208. BMDTimeValue frameDuration;
  209. ctx->frameCount++;
  210. // Handle Video Frame
  211. if (videoFrame) {
  212. AVPacket pkt;
  213. AVCodecContext *c;
  214. av_init_packet(&pkt);
  215. c = ctx->video_st->codec;
  216. if (ctx->frameCount % 25 == 0) {
  217. unsigned long long qsize = avpacket_queue_size(&ctx->queue);
  218. av_log(avctx, AV_LOG_DEBUG,
  219. "Frame received (#%lu) - Valid (%liB) - QSize %fMB\n",
  220. ctx->frameCount,
  221. videoFrame->GetRowBytes() * videoFrame->GetHeight(),
  222. (double)qsize / 1024 / 1024);
  223. }
  224. videoFrame->GetBytes(&frameBytes);
  225. videoFrame->GetStreamTime(&frameTime, &frameDuration,
  226. ctx->video_st->time_base.den);
  227. if (videoFrame->GetFlags() & bmdFrameHasNoInputSource) {
  228. if (videoFrame->GetPixelFormat() == bmdFormat8BitYUV) {
  229. unsigned bars[8] = {
  230. 0xEA80EA80, 0xD292D210, 0xA910A9A5, 0x90229035,
  231. 0x6ADD6ACA, 0x51EF515A, 0x286D28EF, 0x10801080 };
  232. int width = videoFrame->GetWidth();
  233. int height = videoFrame->GetHeight();
  234. unsigned *p = (unsigned *)frameBytes;
  235. for (int y = 0; y < height; y++) {
  236. for (int x = 0; x < width; x += 2)
  237. *p++ = bars[(x * 8) / width];
  238. }
  239. }
  240. if (!no_video) {
  241. av_log(avctx, AV_LOG_WARNING, "Frame received (#%lu) - No input signal detected "
  242. "- Frames dropped %u\n", ctx->frameCount, ++ctx->dropped);
  243. }
  244. no_video = 1;
  245. } else {
  246. if (no_video) {
  247. av_log(avctx, AV_LOG_WARNING, "Frame received (#%lu) - Input returned "
  248. "- Frames dropped %u\n", ctx->frameCount, ++ctx->dropped);
  249. }
  250. no_video = 0;
  251. }
  252. pkt.pts = frameTime / ctx->video_st->time_base.num;
  253. if (initial_video_pts == AV_NOPTS_VALUE) {
  254. initial_video_pts = pkt.pts;
  255. }
  256. pkt.pts -= initial_video_pts;
  257. pkt.dts = pkt.pts;
  258. pkt.duration = frameDuration;
  259. //To be made sure it still applies
  260. pkt.flags |= AV_PKT_FLAG_KEY;
  261. pkt.stream_index = ctx->video_st->index;
  262. pkt.data = (uint8_t *)frameBytes;
  263. pkt.size = videoFrame->GetRowBytes() *
  264. videoFrame->GetHeight();
  265. //fprintf(stderr,"Video Frame size %d ts %d\n", pkt.size, pkt.pts);
  266. #if CONFIG_LIBZVBI
  267. if (!no_video && ctx->teletext_lines && videoFrame->GetPixelFormat() == bmdFormat8BitYUV && videoFrame->GetWidth() == 720) {
  268. IDeckLinkVideoFrameAncillary *vanc;
  269. AVPacket txt_pkt;
  270. uint8_t txt_buf0[1611]; // max 35 * 46 bytes decoded teletext lines + 1 byte data_identifier
  271. uint8_t *txt_buf = txt_buf0;
  272. if (videoFrame->GetAncillaryData(&vanc) == S_OK) {
  273. int i;
  274. int64_t line_mask = 1;
  275. txt_buf[0] = 0x10; // data_identifier - EBU_data
  276. txt_buf++;
  277. for (i = 6; i < 336; i++, line_mask <<= 1) {
  278. uint8_t *buf;
  279. if ((ctx->teletext_lines & line_mask) && vanc->GetBufferForVerticalBlankingLine(i, (void**)&buf) == S_OK) {
  280. if (teletext_data_unit_from_vbi_data(i, buf, txt_buf) >= 0)
  281. txt_buf += 46;
  282. }
  283. if (i == 22)
  284. i = 317;
  285. }
  286. vanc->Release();
  287. if (txt_buf - txt_buf0 > 1) {
  288. int stuffing_units = (4 - ((45 + txt_buf - txt_buf0) / 46) % 4) % 4;
  289. while (stuffing_units--) {
  290. memset(txt_buf, 0xff, 46);
  291. txt_buf[1] = 0x2c; // data_unit_length
  292. txt_buf += 46;
  293. }
  294. av_init_packet(&txt_pkt);
  295. txt_pkt.pts = pkt.pts;
  296. txt_pkt.dts = pkt.dts;
  297. txt_pkt.stream_index = ctx->teletext_st->index;
  298. txt_pkt.data = txt_buf0;
  299. txt_pkt.size = txt_buf - txt_buf0;
  300. if (avpacket_queue_put(&ctx->queue, &txt_pkt) < 0) {
  301. ++ctx->dropped;
  302. }
  303. }
  304. }
  305. }
  306. #endif
  307. c->frame_number++;
  308. if (avpacket_queue_put(&ctx->queue, &pkt) < 0) {
  309. ++ctx->dropped;
  310. }
  311. }
  312. // Handle Audio Frame
  313. if (audioFrame) {
  314. AVCodecContext *c;
  315. AVPacket pkt;
  316. BMDTimeValue audio_pts;
  317. av_init_packet(&pkt);
  318. c = ctx->audio_st->codec;
  319. //hack among hacks
  320. pkt.size = audioFrame->GetSampleFrameCount() * ctx->audio_st->codec->channels * (16 / 8);
  321. audioFrame->GetBytes(&audioFrameBytes);
  322. audioFrame->GetPacketTime(&audio_pts, ctx->audio_st->time_base.den);
  323. pkt.pts = audio_pts / ctx->audio_st->time_base.num;
  324. if (initial_audio_pts == AV_NOPTS_VALUE) {
  325. initial_audio_pts = pkt.pts;
  326. }
  327. pkt.pts -= initial_audio_pts;
  328. pkt.dts = pkt.pts;
  329. //fprintf(stderr,"Audio Frame size %d ts %d\n", pkt.size, pkt.pts);
  330. pkt.flags |= AV_PKT_FLAG_KEY;
  331. pkt.stream_index = ctx->audio_st->index;
  332. pkt.data = (uint8_t *)audioFrameBytes;
  333. c->frame_number++;
  334. if (avpacket_queue_put(&ctx->queue, &pkt) < 0) {
  335. ++ctx->dropped;
  336. }
  337. }
  338. return S_OK;
  339. }
  340. HRESULT decklink_input_callback::VideoInputFormatChanged(
  341. BMDVideoInputFormatChangedEvents events, IDeckLinkDisplayMode *mode,
  342. BMDDetectedVideoInputFormatFlags)
  343. {
  344. return S_OK;
  345. }
  346. static HRESULT decklink_start_input(AVFormatContext *avctx)
  347. {
  348. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  349. struct decklink_ctx *ctx = (struct decklink_ctx *) cctx->ctx;
  350. ctx->input_callback = new decklink_input_callback(avctx);
  351. ctx->dli->SetCallback(ctx->input_callback);
  352. return ctx->dli->StartStreams();
  353. }
  354. extern "C" {
  355. av_cold int ff_decklink_read_close(AVFormatContext *avctx)
  356. {
  357. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  358. struct decklink_ctx *ctx = (struct decklink_ctx *) cctx->ctx;
  359. if (ctx->capture_started) {
  360. ctx->dli->StopStreams();
  361. ctx->dli->DisableVideoInput();
  362. ctx->dli->DisableAudioInput();
  363. }
  364. if (ctx->dli)
  365. ctx->dli->Release();
  366. if (ctx->dl)
  367. ctx->dl->Release();
  368. avpacket_queue_end(&ctx->queue);
  369. av_freep(&cctx->ctx);
  370. return 0;
  371. }
  372. av_cold int ff_decklink_read_header(AVFormatContext *avctx)
  373. {
  374. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  375. struct decklink_ctx *ctx;
  376. IDeckLinkDisplayModeIterator *itermode;
  377. IDeckLinkIterator *iter;
  378. IDeckLink *dl = NULL;
  379. AVStream *st;
  380. HRESULT result;
  381. char fname[1024];
  382. char *tmp;
  383. int mode_num = 0;
  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->teletext_lines = cctx->teletext_lines;
  390. ctx->preroll = cctx->preroll;
  391. cctx->ctx = ctx;
  392. #if !CONFIG_LIBZVBI
  393. if (ctx->teletext_lines) {
  394. av_log(avctx, AV_LOG_ERROR, "Libzvbi support is needed for capturing teletext, please recompile FFmpeg.\n");
  395. return AVERROR(ENOSYS);
  396. }
  397. #endif
  398. /* Check audio channel option for valid values: 2, 8 or 16 */
  399. switch (cctx->audio_channels) {
  400. case 2:
  401. case 8:
  402. case 16:
  403. break;
  404. default:
  405. av_log(avctx, AV_LOG_ERROR, "Value of channels option must be one of 2, 8 or 16\n");
  406. return AVERROR(EINVAL);
  407. }
  408. iter = CreateDeckLinkIteratorInstance();
  409. if (!iter) {
  410. av_log(avctx, AV_LOG_ERROR, "Could not create DeckLink iterator\n");
  411. return AVERROR(EIO);
  412. }
  413. /* List available devices. */
  414. if (ctx->list_devices) {
  415. ff_decklink_list_devices(avctx);
  416. return AVERROR_EXIT;
  417. }
  418. strcpy (fname, avctx->filename);
  419. tmp=strchr (fname, '@');
  420. if (tmp != NULL) {
  421. mode_num = atoi (tmp+1);
  422. *tmp = 0;
  423. }
  424. /* Open device. */
  425. while (iter->Next(&dl) == S_OK) {
  426. const char *displayName;
  427. ff_decklink_get_display_name(dl, &displayName);
  428. if (!strcmp(fname, displayName)) {
  429. av_free((void *) displayName);
  430. ctx->dl = dl;
  431. break;
  432. }
  433. av_free((void *) displayName);
  434. dl->Release();
  435. }
  436. iter->Release();
  437. if (!ctx->dl) {
  438. av_log(avctx, AV_LOG_ERROR, "Could not open '%s'\n", fname);
  439. return AVERROR(EIO);
  440. }
  441. /* Get input device. */
  442. if (ctx->dl->QueryInterface(IID_IDeckLinkInput, (void **) &ctx->dli) != S_OK) {
  443. av_log(avctx, AV_LOG_ERROR, "Could not open output device from '%s'\n",
  444. avctx->filename);
  445. ctx->dl->Release();
  446. return AVERROR(EIO);
  447. }
  448. /* List supported formats. */
  449. if (ctx->list_formats) {
  450. ff_decklink_list_formats(avctx, DIRECTION_IN);
  451. ctx->dli->Release();
  452. ctx->dl->Release();
  453. return AVERROR_EXIT;
  454. }
  455. if (ctx->dli->GetDisplayModeIterator(&itermode) != S_OK) {
  456. av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
  457. ctx->dl->Release();
  458. return AVERROR(EIO);
  459. }
  460. if (mode_num > 0) {
  461. if (ff_decklink_set_format(avctx, DIRECTION_IN, mode_num) < 0) {
  462. av_log(avctx, AV_LOG_ERROR, "Could not set mode %d for %s\n", mode_num, fname);
  463. goto error;
  464. }
  465. }
  466. itermode->Release();
  467. /* Setup streams. */
  468. st = avformat_new_stream(avctx, NULL);
  469. if (!st) {
  470. av_log(avctx, AV_LOG_ERROR, "Cannot add stream\n");
  471. goto error;
  472. }
  473. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  474. st->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
  475. st->codec->sample_rate = bmdAudioSampleRate48kHz;
  476. st->codec->channels = cctx->audio_channels;
  477. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  478. ctx->audio_st=st;
  479. st = avformat_new_stream(avctx, NULL);
  480. if (!st) {
  481. av_log(avctx, AV_LOG_ERROR, "Cannot add stream\n");
  482. goto error;
  483. }
  484. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  485. st->codec->width = ctx->bmd_width;
  486. st->codec->height = ctx->bmd_height;
  487. st->codec->time_base.den = ctx->bmd_tb_den;
  488. st->codec->time_base.num = ctx->bmd_tb_num;
  489. st->codec->bit_rate = av_image_get_buffer_size(st->codec->pix_fmt, ctx->bmd_width, ctx->bmd_height, 1) * 1/av_q2d(st->codec->time_base) * 8;
  490. if (cctx->v210) {
  491. st->codec->codec_id = AV_CODEC_ID_V210;
  492. st->codec->codec_tag = MKTAG('V', '2', '1', '0');
  493. } else {
  494. st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
  495. st->codec->pix_fmt = AV_PIX_FMT_UYVY422;
  496. st->codec->codec_tag = MKTAG('U', 'Y', 'V', 'Y');
  497. }
  498. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  499. ctx->video_st=st;
  500. if (ctx->teletext_lines) {
  501. st = avformat_new_stream(avctx, NULL);
  502. if (!st) {
  503. av_log(avctx, AV_LOG_ERROR, "Cannot add stream\n");
  504. goto error;
  505. }
  506. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  507. st->codec->time_base.den = ctx->bmd_tb_den;
  508. st->codec->time_base.num = ctx->bmd_tb_num;
  509. st->codec->codec_id = AV_CODEC_ID_DVB_TELETEXT;
  510. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  511. ctx->teletext_st = st;
  512. }
  513. av_log(avctx, AV_LOG_VERBOSE, "Using %d input audio channels\n", ctx->audio_st->codec->channels);
  514. result = ctx->dli->EnableAudioInput(bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger, ctx->audio_st->codec->channels);
  515. if (result != S_OK) {
  516. av_log(avctx, AV_LOG_ERROR, "Cannot enable audio input\n");
  517. goto error;
  518. }
  519. result = ctx->dli->EnableVideoInput(ctx->bmd_mode,
  520. cctx->v210 ? bmdFormat10BitYUV : bmdFormat8BitYUV,
  521. bmdVideoInputFlagDefault);
  522. if (result != S_OK) {
  523. av_log(avctx, AV_LOG_ERROR, "Cannot enable video input\n");
  524. goto error;
  525. }
  526. avpacket_queue_init (avctx, &ctx->queue);
  527. if (decklink_start_input (avctx) != S_OK) {
  528. av_log(avctx, AV_LOG_ERROR, "Cannot start input stream\n");
  529. goto error;
  530. }
  531. return 0;
  532. error:
  533. ctx->dli->Release();
  534. ctx->dl->Release();
  535. return AVERROR(EIO);
  536. }
  537. int ff_decklink_read_packet(AVFormatContext *avctx, AVPacket *pkt)
  538. {
  539. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  540. struct decklink_ctx *ctx = (struct decklink_ctx *) cctx->ctx;
  541. AVFrame *frame = ctx->video_st->codec->coded_frame;
  542. avpacket_queue_get(&ctx->queue, pkt, 1);
  543. if (frame && (ctx->bmd_field_dominance == bmdUpperFieldFirst || ctx->bmd_field_dominance == bmdLowerFieldFirst)) {
  544. frame->interlaced_frame = 1;
  545. if (ctx->bmd_field_dominance == bmdUpperFieldFirst) {
  546. frame->top_field_first = 1;
  547. }
  548. }
  549. return 0;
  550. }
  551. } /* extern "C" */