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.

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