decklink_dec.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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. initial_audio_pts = initial_video_pts = AV_NOPTS_VALUE;
  182. pthread_mutex_init(&m_mutex, NULL);
  183. }
  184. decklink_input_callback::~decklink_input_callback()
  185. {
  186. pthread_mutex_destroy(&m_mutex);
  187. }
  188. ULONG decklink_input_callback::AddRef(void)
  189. {
  190. pthread_mutex_lock(&m_mutex);
  191. m_refCount++;
  192. pthread_mutex_unlock(&m_mutex);
  193. return (ULONG)m_refCount;
  194. }
  195. ULONG decklink_input_callback::Release(void)
  196. {
  197. pthread_mutex_lock(&m_mutex);
  198. m_refCount--;
  199. pthread_mutex_unlock(&m_mutex);
  200. if (m_refCount == 0) {
  201. delete this;
  202. return 0;
  203. }
  204. return (ULONG)m_refCount;
  205. }
  206. static int64_t get_pkt_pts(IDeckLinkVideoInputFrame *videoFrame,
  207. IDeckLinkAudioInputPacket *audioFrame,
  208. int64_t wallclock,
  209. DecklinkPtsSource pts_src,
  210. AVRational time_base, int64_t *initial_pts)
  211. {
  212. int64_t pts = AV_NOPTS_VALUE;
  213. BMDTimeValue bmd_pts;
  214. BMDTimeValue bmd_duration;
  215. HRESULT res = E_INVALIDARG;
  216. switch (pts_src) {
  217. case PTS_SRC_AUDIO:
  218. if (audioFrame)
  219. res = audioFrame->GetPacketTime(&bmd_pts, time_base.den);
  220. break;
  221. case PTS_SRC_VIDEO:
  222. if (videoFrame)
  223. res = videoFrame->GetStreamTime(&bmd_pts, &bmd_duration, time_base.den);
  224. break;
  225. case PTS_SRC_REFERENCE:
  226. if (videoFrame)
  227. res = videoFrame->GetHardwareReferenceTimestamp(time_base.den, &bmd_pts, &bmd_duration);
  228. break;
  229. case PTS_SRC_WALLCLOCK:
  230. pts = av_rescale_q(wallclock, AV_TIME_BASE_Q, time_base);
  231. break;
  232. }
  233. if (res == S_OK)
  234. pts = bmd_pts / time_base.num;
  235. if (pts != AV_NOPTS_VALUE && *initial_pts == AV_NOPTS_VALUE)
  236. *initial_pts = pts;
  237. if (*initial_pts != AV_NOPTS_VALUE)
  238. pts -= *initial_pts;
  239. return pts;
  240. }
  241. HRESULT decklink_input_callback::VideoInputFrameArrived(
  242. IDeckLinkVideoInputFrame *videoFrame, IDeckLinkAudioInputPacket *audioFrame)
  243. {
  244. void *frameBytes;
  245. void *audioFrameBytes;
  246. BMDTimeValue frameTime;
  247. BMDTimeValue frameDuration;
  248. int64_t wallclock = 0;
  249. ctx->frameCount++;
  250. if (ctx->audio_pts_source == PTS_SRC_WALLCLOCK || ctx->video_pts_source == PTS_SRC_WALLCLOCK)
  251. wallclock = av_gettime_relative();
  252. // Handle Video Frame
  253. if (videoFrame) {
  254. AVPacket pkt;
  255. av_init_packet(&pkt);
  256. if (ctx->frameCount % 25 == 0) {
  257. unsigned long long qsize = avpacket_queue_size(&ctx->queue);
  258. av_log(avctx, AV_LOG_DEBUG,
  259. "Frame received (#%lu) - Valid (%liB) - QSize %fMB\n",
  260. ctx->frameCount,
  261. videoFrame->GetRowBytes() * videoFrame->GetHeight(),
  262. (double)qsize / 1024 / 1024);
  263. }
  264. videoFrame->GetBytes(&frameBytes);
  265. videoFrame->GetStreamTime(&frameTime, &frameDuration,
  266. ctx->video_st->time_base.den);
  267. if (videoFrame->GetFlags() & bmdFrameHasNoInputSource) {
  268. if (ctx->draw_bars && videoFrame->GetPixelFormat() == bmdFormat8BitYUV) {
  269. unsigned bars[8] = {
  270. 0xEA80EA80, 0xD292D210, 0xA910A9A5, 0x90229035,
  271. 0x6ADD6ACA, 0x51EF515A, 0x286D28EF, 0x10801080 };
  272. int width = videoFrame->GetWidth();
  273. int height = videoFrame->GetHeight();
  274. unsigned *p = (unsigned *)frameBytes;
  275. for (int y = 0; y < height; y++) {
  276. for (int x = 0; x < width; x += 2)
  277. *p++ = bars[(x * 8) / width];
  278. }
  279. }
  280. if (!no_video) {
  281. av_log(avctx, AV_LOG_WARNING, "Frame received (#%lu) - No input signal detected "
  282. "- Frames dropped %u\n", ctx->frameCount, ++ctx->dropped);
  283. }
  284. no_video = 1;
  285. } else {
  286. if (no_video) {
  287. av_log(avctx, AV_LOG_WARNING, "Frame received (#%lu) - Input returned "
  288. "- Frames dropped %u\n", ctx->frameCount, ++ctx->dropped);
  289. }
  290. no_video = 0;
  291. }
  292. pkt.pts = get_pkt_pts(videoFrame, audioFrame, wallclock, ctx->video_pts_source, ctx->video_st->time_base, &initial_video_pts);
  293. pkt.dts = pkt.pts;
  294. pkt.duration = frameDuration;
  295. //To be made sure it still applies
  296. pkt.flags |= AV_PKT_FLAG_KEY;
  297. pkt.stream_index = ctx->video_st->index;
  298. pkt.data = (uint8_t *)frameBytes;
  299. pkt.size = videoFrame->GetRowBytes() *
  300. videoFrame->GetHeight();
  301. //fprintf(stderr,"Video Frame size %d ts %d\n", pkt.size, pkt.pts);
  302. #if CONFIG_LIBZVBI
  303. if (!no_video && ctx->teletext_lines && videoFrame->GetPixelFormat() == bmdFormat8BitYUV && videoFrame->GetWidth() == 720) {
  304. IDeckLinkVideoFrameAncillary *vanc;
  305. AVPacket txt_pkt;
  306. uint8_t txt_buf0[1611]; // max 35 * 46 bytes decoded teletext lines + 1 byte data_identifier
  307. uint8_t *txt_buf = txt_buf0;
  308. if (videoFrame->GetAncillaryData(&vanc) == S_OK) {
  309. int i;
  310. int64_t line_mask = 1;
  311. txt_buf[0] = 0x10; // data_identifier - EBU_data
  312. txt_buf++;
  313. for (i = 6; i < 336; i++, line_mask <<= 1) {
  314. uint8_t *buf;
  315. if ((ctx->teletext_lines & line_mask) && vanc->GetBufferForVerticalBlankingLine(i, (void**)&buf) == S_OK) {
  316. if (teletext_data_unit_from_vbi_data(i, buf, txt_buf) >= 0)
  317. txt_buf += 46;
  318. }
  319. if (i == 22)
  320. i = 317;
  321. }
  322. vanc->Release();
  323. if (txt_buf - txt_buf0 > 1) {
  324. int stuffing_units = (4 - ((45 + txt_buf - txt_buf0) / 46) % 4) % 4;
  325. while (stuffing_units--) {
  326. memset(txt_buf, 0xff, 46);
  327. txt_buf[1] = 0x2c; // data_unit_length
  328. txt_buf += 46;
  329. }
  330. av_init_packet(&txt_pkt);
  331. txt_pkt.pts = pkt.pts;
  332. txt_pkt.dts = pkt.dts;
  333. txt_pkt.stream_index = ctx->teletext_st->index;
  334. txt_pkt.data = txt_buf0;
  335. txt_pkt.size = txt_buf - txt_buf0;
  336. if (avpacket_queue_put(&ctx->queue, &txt_pkt) < 0) {
  337. ++ctx->dropped;
  338. }
  339. }
  340. }
  341. }
  342. #endif
  343. if (avpacket_queue_put(&ctx->queue, &pkt) < 0) {
  344. ++ctx->dropped;
  345. }
  346. }
  347. // Handle Audio Frame
  348. if (audioFrame) {
  349. AVPacket pkt;
  350. BMDTimeValue audio_pts;
  351. av_init_packet(&pkt);
  352. //hack among hacks
  353. pkt.size = audioFrame->GetSampleFrameCount() * ctx->audio_st->codecpar->channels * (16 / 8);
  354. audioFrame->GetBytes(&audioFrameBytes);
  355. audioFrame->GetPacketTime(&audio_pts, ctx->audio_st->time_base.den);
  356. pkt.pts = get_pkt_pts(videoFrame, audioFrame, wallclock, ctx->audio_pts_source, ctx->audio_st->time_base, &initial_audio_pts);
  357. pkt.dts = pkt.pts;
  358. //fprintf(stderr,"Audio Frame size %d ts %d\n", pkt.size, pkt.pts);
  359. pkt.flags |= AV_PKT_FLAG_KEY;
  360. pkt.stream_index = ctx->audio_st->index;
  361. pkt.data = (uint8_t *)audioFrameBytes;
  362. if (avpacket_queue_put(&ctx->queue, &pkt) < 0) {
  363. ++ctx->dropped;
  364. }
  365. }
  366. return S_OK;
  367. }
  368. HRESULT decklink_input_callback::VideoInputFormatChanged(
  369. BMDVideoInputFormatChangedEvents events, IDeckLinkDisplayMode *mode,
  370. BMDDetectedVideoInputFormatFlags)
  371. {
  372. return S_OK;
  373. }
  374. static HRESULT decklink_start_input(AVFormatContext *avctx)
  375. {
  376. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  377. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  378. ctx->input_callback = new decklink_input_callback(avctx);
  379. ctx->dli->SetCallback(ctx->input_callback);
  380. return ctx->dli->StartStreams();
  381. }
  382. extern "C" {
  383. av_cold int ff_decklink_read_close(AVFormatContext *avctx)
  384. {
  385. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  386. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  387. if (ctx->capture_started) {
  388. ctx->dli->StopStreams();
  389. ctx->dli->DisableVideoInput();
  390. ctx->dli->DisableAudioInput();
  391. }
  392. ff_decklink_cleanup(avctx);
  393. avpacket_queue_end(&ctx->queue);
  394. av_freep(&cctx->ctx);
  395. return 0;
  396. }
  397. av_cold int ff_decklink_read_header(AVFormatContext *avctx)
  398. {
  399. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  400. struct decklink_ctx *ctx;
  401. AVStream *st;
  402. HRESULT result;
  403. char fname[1024];
  404. char *tmp;
  405. int mode_num = 0;
  406. int ret;
  407. ctx = (struct decklink_ctx *) av_mallocz(sizeof(struct decklink_ctx));
  408. if (!ctx)
  409. return AVERROR(ENOMEM);
  410. ctx->list_devices = cctx->list_devices;
  411. ctx->list_formats = cctx->list_formats;
  412. ctx->teletext_lines = cctx->teletext_lines;
  413. ctx->preroll = cctx->preroll;
  414. ctx->duplex_mode = cctx->duplex_mode;
  415. if (cctx->video_input > 0 && (unsigned int)cctx->video_input < FF_ARRAY_ELEMS(decklink_video_connection_map))
  416. ctx->video_input = decklink_video_connection_map[cctx->video_input];
  417. if (cctx->audio_input > 0 && (unsigned int)cctx->audio_input < FF_ARRAY_ELEMS(decklink_audio_connection_map))
  418. ctx->audio_input = decklink_audio_connection_map[cctx->audio_input];
  419. ctx->audio_pts_source = cctx->audio_pts_source;
  420. ctx->video_pts_source = cctx->video_pts_source;
  421. ctx->draw_bars = cctx->draw_bars;
  422. cctx->ctx = ctx;
  423. #if !CONFIG_LIBZVBI
  424. if (ctx->teletext_lines) {
  425. av_log(avctx, AV_LOG_ERROR, "Libzvbi support is needed for capturing teletext, please recompile FFmpeg.\n");
  426. return AVERROR(ENOSYS);
  427. }
  428. #endif
  429. /* Check audio channel option for valid values: 2, 8 or 16 */
  430. switch (cctx->audio_channels) {
  431. case 2:
  432. case 8:
  433. case 16:
  434. break;
  435. default:
  436. av_log(avctx, AV_LOG_ERROR, "Value of channels option must be one of 2, 8 or 16\n");
  437. return AVERROR(EINVAL);
  438. }
  439. /* List available devices. */
  440. if (ctx->list_devices) {
  441. ff_decklink_list_devices(avctx);
  442. return AVERROR_EXIT;
  443. }
  444. strcpy (fname, avctx->filename);
  445. tmp=strchr (fname, '@');
  446. if (tmp != NULL) {
  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) {
  467. if (ff_decklink_set_format(avctx, DIRECTION_IN, mode_num) < 0) {
  468. av_log(avctx, AV_LOG_ERROR, "Could not set mode %d for %s\n", mode_num, fname);
  469. ret = AVERROR(EIO);
  470. goto error;
  471. }
  472. }
  473. /* Setup streams. */
  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_AUDIO;
  481. st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
  482. st->codecpar->sample_rate = bmdAudioSampleRate48kHz;
  483. st->codecpar->channels = cctx->audio_channels;
  484. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  485. ctx->audio_st=st;
  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_VIDEO;
  493. st->codecpar->width = ctx->bmd_width;
  494. st->codecpar->height = ctx->bmd_height;
  495. st->time_base.den = ctx->bmd_tb_den;
  496. st->time_base.num = ctx->bmd_tb_num;
  497. av_stream_set_r_frame_rate(st, av_make_q(st->time_base.den, st->time_base.num));
  498. if (cctx->v210) {
  499. st->codecpar->codec_id = AV_CODEC_ID_V210;
  500. st->codecpar->codec_tag = MKTAG('V', '2', '1', '0');
  501. st->codecpar->bit_rate = av_rescale(ctx->bmd_width * ctx->bmd_height * 64, st->time_base.den, st->time_base.num * 3);
  502. } else {
  503. st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
  504. st->codecpar->format = AV_PIX_FMT_UYVY422;
  505. st->codecpar->codec_tag = MKTAG('U', 'Y', 'V', 'Y');
  506. st->codecpar->bit_rate = av_rescale(ctx->bmd_width * ctx->bmd_height * 16, st->time_base.den, st->time_base.num);
  507. }
  508. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  509. ctx->video_st=st;
  510. if (ctx->teletext_lines) {
  511. st = avformat_new_stream(avctx, NULL);
  512. if (!st) {
  513. av_log(avctx, AV_LOG_ERROR, "Cannot add stream\n");
  514. ret = AVERROR(ENOMEM);
  515. goto error;
  516. }
  517. st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
  518. st->time_base.den = ctx->bmd_tb_den;
  519. st->time_base.num = ctx->bmd_tb_num;
  520. st->codecpar->codec_id = AV_CODEC_ID_DVB_TELETEXT;
  521. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  522. ctx->teletext_st = st;
  523. }
  524. av_log(avctx, AV_LOG_VERBOSE, "Using %d input audio channels\n", ctx->audio_st->codecpar->channels);
  525. result = ctx->dli->EnableAudioInput(bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger, ctx->audio_st->codecpar->channels);
  526. if (result != S_OK) {
  527. av_log(avctx, AV_LOG_ERROR, "Cannot enable audio input\n");
  528. ret = AVERROR(EIO);
  529. goto error;
  530. }
  531. result = ctx->dli->EnableVideoInput(ctx->bmd_mode,
  532. cctx->v210 ? bmdFormat10BitYUV : bmdFormat8BitYUV,
  533. bmdVideoInputFlagDefault);
  534. if (result != S_OK) {
  535. av_log(avctx, AV_LOG_ERROR, "Cannot enable video input\n");
  536. ret = AVERROR(EIO);
  537. goto error;
  538. }
  539. avpacket_queue_init (avctx, &ctx->queue);
  540. if (decklink_start_input (avctx) != S_OK) {
  541. av_log(avctx, AV_LOG_ERROR, "Cannot start input stream\n");
  542. ret = AVERROR(EIO);
  543. goto error;
  544. }
  545. return 0;
  546. error:
  547. ff_decklink_cleanup(avctx);
  548. return ret;
  549. }
  550. int ff_decklink_read_packet(AVFormatContext *avctx, AVPacket *pkt)
  551. {
  552. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  553. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  554. AVFrame *frame = ctx->video_st->codec->coded_frame;
  555. avpacket_queue_get(&ctx->queue, pkt, 1);
  556. if (frame && (ctx->bmd_field_dominance == bmdUpperFieldFirst || ctx->bmd_field_dominance == bmdLowerFieldFirst)) {
  557. frame->interlaced_frame = 1;
  558. if (ctx->bmd_field_dominance == bmdUpperFieldFirst) {
  559. frame->top_field_first = 1;
  560. }
  561. }
  562. return 0;
  563. }
  564. } /* extern "C" */