decklink_dec.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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. av_init_packet(&pkt);
  214. if (ctx->frameCount % 25 == 0) {
  215. unsigned long long qsize = avpacket_queue_size(&ctx->queue);
  216. av_log(avctx, AV_LOG_DEBUG,
  217. "Frame received (#%lu) - Valid (%liB) - QSize %fMB\n",
  218. ctx->frameCount,
  219. videoFrame->GetRowBytes() * videoFrame->GetHeight(),
  220. (double)qsize / 1024 / 1024);
  221. }
  222. videoFrame->GetBytes(&frameBytes);
  223. videoFrame->GetStreamTime(&frameTime, &frameDuration,
  224. ctx->video_st->time_base.den);
  225. if (videoFrame->GetFlags() & bmdFrameHasNoInputSource) {
  226. if (videoFrame->GetPixelFormat() == bmdFormat8BitYUV) {
  227. unsigned bars[8] = {
  228. 0xEA80EA80, 0xD292D210, 0xA910A9A5, 0x90229035,
  229. 0x6ADD6ACA, 0x51EF515A, 0x286D28EF, 0x10801080 };
  230. int width = videoFrame->GetWidth();
  231. int height = videoFrame->GetHeight();
  232. unsigned *p = (unsigned *)frameBytes;
  233. for (int y = 0; y < height; y++) {
  234. for (int x = 0; x < width; x += 2)
  235. *p++ = bars[(x * 8) / width];
  236. }
  237. }
  238. if (!no_video) {
  239. av_log(avctx, AV_LOG_WARNING, "Frame received (#%lu) - No input signal detected "
  240. "- Frames dropped %u\n", ctx->frameCount, ++ctx->dropped);
  241. }
  242. no_video = 1;
  243. } else {
  244. if (no_video) {
  245. av_log(avctx, AV_LOG_WARNING, "Frame received (#%lu) - Input returned "
  246. "- Frames dropped %u\n", ctx->frameCount, ++ctx->dropped);
  247. }
  248. no_video = 0;
  249. }
  250. pkt.pts = frameTime / ctx->video_st->time_base.num;
  251. if (initial_video_pts == AV_NOPTS_VALUE) {
  252. initial_video_pts = pkt.pts;
  253. }
  254. pkt.pts -= initial_video_pts;
  255. pkt.dts = pkt.pts;
  256. pkt.duration = frameDuration;
  257. //To be made sure it still applies
  258. pkt.flags |= AV_PKT_FLAG_KEY;
  259. pkt.stream_index = ctx->video_st->index;
  260. pkt.data = (uint8_t *)frameBytes;
  261. pkt.size = videoFrame->GetRowBytes() *
  262. videoFrame->GetHeight();
  263. //fprintf(stderr,"Video Frame size %d ts %d\n", pkt.size, pkt.pts);
  264. #if CONFIG_LIBZVBI
  265. if (!no_video && ctx->teletext_lines && videoFrame->GetPixelFormat() == bmdFormat8BitYUV && videoFrame->GetWidth() == 720) {
  266. IDeckLinkVideoFrameAncillary *vanc;
  267. AVPacket txt_pkt;
  268. uint8_t txt_buf0[1611]; // max 35 * 46 bytes decoded teletext lines + 1 byte data_identifier
  269. uint8_t *txt_buf = txt_buf0;
  270. if (videoFrame->GetAncillaryData(&vanc) == S_OK) {
  271. int i;
  272. int64_t line_mask = 1;
  273. txt_buf[0] = 0x10; // data_identifier - EBU_data
  274. txt_buf++;
  275. for (i = 6; i < 336; i++, line_mask <<= 1) {
  276. uint8_t *buf;
  277. if ((ctx->teletext_lines & line_mask) && vanc->GetBufferForVerticalBlankingLine(i, (void**)&buf) == S_OK) {
  278. if (teletext_data_unit_from_vbi_data(i, buf, txt_buf) >= 0)
  279. txt_buf += 46;
  280. }
  281. if (i == 22)
  282. i = 317;
  283. }
  284. vanc->Release();
  285. if (txt_buf - txt_buf0 > 1) {
  286. int stuffing_units = (4 - ((45 + txt_buf - txt_buf0) / 46) % 4) % 4;
  287. while (stuffing_units--) {
  288. memset(txt_buf, 0xff, 46);
  289. txt_buf[1] = 0x2c; // data_unit_length
  290. txt_buf += 46;
  291. }
  292. av_init_packet(&txt_pkt);
  293. txt_pkt.pts = pkt.pts;
  294. txt_pkt.dts = pkt.dts;
  295. txt_pkt.stream_index = ctx->teletext_st->index;
  296. txt_pkt.data = txt_buf0;
  297. txt_pkt.size = txt_buf - txt_buf0;
  298. if (avpacket_queue_put(&ctx->queue, &txt_pkt) < 0) {
  299. ++ctx->dropped;
  300. }
  301. }
  302. }
  303. }
  304. #endif
  305. if (avpacket_queue_put(&ctx->queue, &pkt) < 0) {
  306. ++ctx->dropped;
  307. }
  308. }
  309. // Handle Audio Frame
  310. if (audioFrame) {
  311. AVPacket pkt;
  312. BMDTimeValue audio_pts;
  313. av_init_packet(&pkt);
  314. //hack among hacks
  315. pkt.size = audioFrame->GetSampleFrameCount() * ctx->audio_st->codecpar->channels * (16 / 8);
  316. audioFrame->GetBytes(&audioFrameBytes);
  317. audioFrame->GetPacketTime(&audio_pts, ctx->audio_st->time_base.den);
  318. pkt.pts = audio_pts / ctx->audio_st->time_base.num;
  319. if (initial_audio_pts == AV_NOPTS_VALUE) {
  320. initial_audio_pts = pkt.pts;
  321. }
  322. pkt.pts -= initial_audio_pts;
  323. pkt.dts = pkt.pts;
  324. //fprintf(stderr,"Audio Frame size %d ts %d\n", pkt.size, pkt.pts);
  325. pkt.flags |= AV_PKT_FLAG_KEY;
  326. pkt.stream_index = ctx->audio_st->index;
  327. pkt.data = (uint8_t *)audioFrameBytes;
  328. if (avpacket_queue_put(&ctx->queue, &pkt) < 0) {
  329. ++ctx->dropped;
  330. }
  331. }
  332. return S_OK;
  333. }
  334. HRESULT decklink_input_callback::VideoInputFormatChanged(
  335. BMDVideoInputFormatChangedEvents events, IDeckLinkDisplayMode *mode,
  336. BMDDetectedVideoInputFormatFlags)
  337. {
  338. return S_OK;
  339. }
  340. static HRESULT decklink_start_input(AVFormatContext *avctx)
  341. {
  342. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  343. struct decklink_ctx *ctx = (struct decklink_ctx *) cctx->ctx;
  344. ctx->input_callback = new decklink_input_callback(avctx);
  345. ctx->dli->SetCallback(ctx->input_callback);
  346. return ctx->dli->StartStreams();
  347. }
  348. extern "C" {
  349. av_cold int ff_decklink_read_close(AVFormatContext *avctx)
  350. {
  351. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  352. struct decklink_ctx *ctx = (struct decklink_ctx *) cctx->ctx;
  353. if (ctx->capture_started) {
  354. ctx->dli->StopStreams();
  355. ctx->dli->DisableVideoInput();
  356. ctx->dli->DisableAudioInput();
  357. }
  358. if (ctx->dli)
  359. ctx->dli->Release();
  360. if (ctx->dl)
  361. ctx->dl->Release();
  362. avpacket_queue_end(&ctx->queue);
  363. av_freep(&cctx->ctx);
  364. return 0;
  365. }
  366. av_cold int ff_decklink_read_header(AVFormatContext *avctx)
  367. {
  368. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  369. struct decklink_ctx *ctx;
  370. IDeckLinkDisplayModeIterator *itermode;
  371. IDeckLinkIterator *iter;
  372. IDeckLink *dl = NULL;
  373. AVStream *st;
  374. HRESULT result;
  375. char fname[1024];
  376. char *tmp;
  377. int mode_num = 0;
  378. ctx = (struct decklink_ctx *) av_mallocz(sizeof(struct decklink_ctx));
  379. if (!ctx)
  380. return AVERROR(ENOMEM);
  381. ctx->list_devices = cctx->list_devices;
  382. ctx->list_formats = cctx->list_formats;
  383. ctx->teletext_lines = cctx->teletext_lines;
  384. ctx->preroll = cctx->preroll;
  385. cctx->ctx = ctx;
  386. #if !CONFIG_LIBZVBI
  387. if (ctx->teletext_lines) {
  388. av_log(avctx, AV_LOG_ERROR, "Libzvbi support is needed for capturing teletext, please recompile FFmpeg.\n");
  389. return AVERROR(ENOSYS);
  390. }
  391. #endif
  392. /* Check audio channel option for valid values: 2, 8 or 16 */
  393. switch (cctx->audio_channels) {
  394. case 2:
  395. case 8:
  396. case 16:
  397. break;
  398. default:
  399. av_log(avctx, AV_LOG_ERROR, "Value of channels option must be one of 2, 8 or 16\n");
  400. return AVERROR(EINVAL);
  401. }
  402. iter = CreateDeckLinkIteratorInstance();
  403. if (!iter) {
  404. av_log(avctx, AV_LOG_ERROR, "Could not create DeckLink iterator\n");
  405. return AVERROR(EIO);
  406. }
  407. /* List available devices. */
  408. if (ctx->list_devices) {
  409. ff_decklink_list_devices(avctx);
  410. return AVERROR_EXIT;
  411. }
  412. strcpy (fname, avctx->filename);
  413. tmp=strchr (fname, '@');
  414. if (tmp != NULL) {
  415. mode_num = atoi (tmp+1);
  416. *tmp = 0;
  417. }
  418. /* Open device. */
  419. while (iter->Next(&dl) == S_OK) {
  420. const char *displayName;
  421. ff_decklink_get_display_name(dl, &displayName);
  422. if (!strcmp(fname, displayName)) {
  423. av_free((void *) displayName);
  424. ctx->dl = dl;
  425. break;
  426. }
  427. av_free((void *) displayName);
  428. dl->Release();
  429. }
  430. iter->Release();
  431. if (!ctx->dl) {
  432. av_log(avctx, AV_LOG_ERROR, "Could not open '%s'\n", fname);
  433. return AVERROR(EIO);
  434. }
  435. /* Get input device. */
  436. if (ctx->dl->QueryInterface(IID_IDeckLinkInput, (void **) &ctx->dli) != S_OK) {
  437. av_log(avctx, AV_LOG_ERROR, "Could not open output device from '%s'\n",
  438. avctx->filename);
  439. ctx->dl->Release();
  440. return AVERROR(EIO);
  441. }
  442. /* List supported formats. */
  443. if (ctx->list_formats) {
  444. ff_decklink_list_formats(avctx, DIRECTION_IN);
  445. ctx->dli->Release();
  446. ctx->dl->Release();
  447. return AVERROR_EXIT;
  448. }
  449. if (ctx->dli->GetDisplayModeIterator(&itermode) != S_OK) {
  450. av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
  451. ctx->dl->Release();
  452. return AVERROR(EIO);
  453. }
  454. if (mode_num > 0) {
  455. if (ff_decklink_set_format(avctx, DIRECTION_IN, mode_num) < 0) {
  456. av_log(avctx, AV_LOG_ERROR, "Could not set mode %d for %s\n", mode_num, fname);
  457. goto error;
  458. }
  459. }
  460. itermode->Release();
  461. /* Setup streams. */
  462. st = avformat_new_stream(avctx, NULL);
  463. if (!st) {
  464. av_log(avctx, AV_LOG_ERROR, "Cannot add stream\n");
  465. goto error;
  466. }
  467. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  468. st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
  469. st->codecpar->sample_rate = bmdAudioSampleRate48kHz;
  470. st->codecpar->channels = cctx->audio_channels;
  471. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  472. ctx->audio_st=st;
  473. st = avformat_new_stream(avctx, NULL);
  474. if (!st) {
  475. av_log(avctx, AV_LOG_ERROR, "Cannot add stream\n");
  476. goto error;
  477. }
  478. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  479. st->codecpar->width = ctx->bmd_width;
  480. st->codecpar->height = ctx->bmd_height;
  481. st->time_base.den = ctx->bmd_tb_den;
  482. st->time_base.num = ctx->bmd_tb_num;
  483. st->codecpar->bit_rate = av_image_get_buffer_size((AVPixelFormat)st->codecpar->format, ctx->bmd_width, ctx->bmd_height, 1) * 1/av_q2d(st->time_base) * 8;
  484. if (cctx->v210) {
  485. st->codecpar->codec_id = AV_CODEC_ID_V210;
  486. st->codecpar->codec_tag = MKTAG('V', '2', '1', '0');
  487. } else {
  488. st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
  489. st->codecpar->format = AV_PIX_FMT_UYVY422;
  490. st->codecpar->codec_tag = MKTAG('U', 'Y', 'V', 'Y');
  491. }
  492. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  493. ctx->video_st=st;
  494. if (ctx->teletext_lines) {
  495. st = avformat_new_stream(avctx, NULL);
  496. if (!st) {
  497. av_log(avctx, AV_LOG_ERROR, "Cannot add stream\n");
  498. goto error;
  499. }
  500. st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
  501. st->time_base.den = ctx->bmd_tb_den;
  502. st->time_base.num = ctx->bmd_tb_num;
  503. st->codecpar->codec_id = AV_CODEC_ID_DVB_TELETEXT;
  504. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  505. ctx->teletext_st = st;
  506. }
  507. av_log(avctx, AV_LOG_VERBOSE, "Using %d input audio channels\n", ctx->audio_st->codecpar->channels);
  508. result = ctx->dli->EnableAudioInput(bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger, ctx->audio_st->codecpar->channels);
  509. if (result != S_OK) {
  510. av_log(avctx, AV_LOG_ERROR, "Cannot enable audio input\n");
  511. goto error;
  512. }
  513. result = ctx->dli->EnableVideoInput(ctx->bmd_mode,
  514. cctx->v210 ? bmdFormat10BitYUV : bmdFormat8BitYUV,
  515. bmdVideoInputFlagDefault);
  516. if (result != S_OK) {
  517. av_log(avctx, AV_LOG_ERROR, "Cannot enable video input\n");
  518. goto error;
  519. }
  520. avpacket_queue_init (avctx, &ctx->queue);
  521. if (decklink_start_input (avctx) != S_OK) {
  522. av_log(avctx, AV_LOG_ERROR, "Cannot start input stream\n");
  523. goto error;
  524. }
  525. return 0;
  526. error:
  527. ctx->dli->Release();
  528. ctx->dl->Release();
  529. return AVERROR(EIO);
  530. }
  531. int ff_decklink_read_packet(AVFormatContext *avctx, AVPacket *pkt)
  532. {
  533. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  534. struct decklink_ctx *ctx = (struct decklink_ctx *) cctx->ctx;
  535. AVFrame *frame = ctx->video_st->codec->coded_frame;
  536. avpacket_queue_get(&ctx->queue, pkt, 1);
  537. if (frame && (ctx->bmd_field_dominance == bmdUpperFieldFirst || ctx->bmd_field_dominance == bmdLowerFieldFirst)) {
  538. frame->interlaced_frame = 1;
  539. if (ctx->bmd_field_dominance == bmdUpperFieldFirst) {
  540. frame->top_field_first = 1;
  541. }
  542. }
  543. return 0;
  544. }
  545. } /* extern "C" */