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.

659 lines
21KB

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