decklink_dec.cpp 21 KB

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