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.

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