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.

814 lines
27KB

  1. /*
  2. * Blackmagic DeckLink input
  3. * Copyright (c) 2013-2014 Luca Barbato, Deti Fliegl
  4. * Copyright (c) 2017 Akamai Technologies, Inc.
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /* Include internal.h first to avoid conflict between winsock.h (used by
  23. * DeckLink headers) and winsock2.h (used by libavformat) in MSVC++ builds */
  24. extern "C" {
  25. #include "libavformat/internal.h"
  26. }
  27. #include <DeckLinkAPI.h>
  28. extern "C" {
  29. #include "config.h"
  30. #include "libavformat/avformat.h"
  31. #include "libavutil/avassert.h"
  32. #include "libavutil/avutil.h"
  33. #include "libavutil/common.h"
  34. #include "libavutil/imgutils.h"
  35. #include "libavutil/time.h"
  36. #include "libavutil/mathematics.h"
  37. #include "libavutil/reverse.h"
  38. #if CONFIG_LIBZVBI
  39. #include <libzvbi.h>
  40. #endif
  41. }
  42. #include "decklink_common.h"
  43. #include "decklink_dec.h"
  44. static uint8_t calc_parity_and_line_offset(int line)
  45. {
  46. uint8_t ret = (line < 313) << 5;
  47. if (line >= 7 && line <= 22)
  48. ret += line;
  49. if (line >= 320 && line <= 335)
  50. ret += (line - 313);
  51. return ret;
  52. }
  53. static void fill_data_unit_head(int line, uint8_t *tgt)
  54. {
  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. }
  60. #if CONFIG_LIBZVBI
  61. static uint8_t* teletext_data_unit_from_vbi_data(int line, uint8_t *src, uint8_t *tgt, vbi_pixfmt fmt)
  62. {
  63. vbi_bit_slicer slicer;
  64. vbi_bit_slicer_init(&slicer, 720, 13500000, 6937500, 6937500, 0x00aaaae4, 0xffff, 18, 6, 42 * 8, VBI_MODULATION_NRZ_MSB, fmt);
  65. if (vbi_bit_slice(&slicer, src, tgt + 4) == FALSE)
  66. return tgt;
  67. fill_data_unit_head(line, tgt);
  68. return tgt + 46;
  69. }
  70. static uint8_t* teletext_data_unit_from_vbi_data_10bit(int line, uint8_t *src, uint8_t *tgt)
  71. {
  72. uint8_t y[720];
  73. uint8_t *py = y;
  74. uint8_t *pend = y + 720;
  75. /* The 10-bit VBI data is packed in V210, but libzvbi only supports 8-bit,
  76. * so we extract the 8 MSBs of the luma component, that is enough for
  77. * teletext bit slicing. */
  78. while (py < pend) {
  79. *py++ = (src[1] >> 4) + ((src[2] & 15) << 4);
  80. *py++ = (src[4] >> 2) + ((src[5] & 3 ) << 6);
  81. *py++ = (src[6] >> 6) + ((src[7] & 63) << 2);
  82. src += 8;
  83. }
  84. return teletext_data_unit_from_vbi_data(line, y, tgt, VBI_PIXFMT_YUV420);
  85. }
  86. #endif
  87. static uint8_t* teletext_data_unit_from_op47_vbi_packet(int line, uint16_t *py, uint8_t *tgt)
  88. {
  89. int i;
  90. if (py[0] != 0x255 || py[1] != 0x255 || py[2] != 0x227)
  91. return tgt;
  92. fill_data_unit_head(line, tgt);
  93. py += 3;
  94. tgt += 4;
  95. for (i = 0; i < 42; i++)
  96. *tgt++ = ff_reverse[py[i] & 255];
  97. return tgt;
  98. }
  99. static int linemask_matches(int line, int64_t mask)
  100. {
  101. int shift = -1;
  102. if (line >= 6 && line <= 22)
  103. shift = line - 6;
  104. if (line >= 318 && line <= 335)
  105. shift = line - 318 + 17;
  106. return shift >= 0 && ((1ULL << shift) & mask);
  107. }
  108. static uint8_t* teletext_data_unit_from_op47_data(uint16_t *py, uint16_t *pend, uint8_t *tgt, int64_t wanted_lines)
  109. {
  110. if (py < pend - 9) {
  111. if (py[0] == 0x151 && py[1] == 0x115 && py[3] == 0x102) { // identifier, identifier, format code for WST teletext
  112. uint16_t *descriptors = py + 4;
  113. int i;
  114. py += 9;
  115. for (i = 0; i < 5 && py < pend - 45; i++, py += 45) {
  116. int line = (descriptors[i] & 31) + (!(descriptors[i] & 128)) * 313;
  117. if (line && linemask_matches(line, wanted_lines))
  118. tgt = teletext_data_unit_from_op47_vbi_packet(line, py, tgt);
  119. }
  120. }
  121. }
  122. return tgt;
  123. }
  124. static uint8_t* teletext_data_unit_from_ancillary_packet(uint16_t *py, uint16_t *pend, uint8_t *tgt, int64_t wanted_lines, int allow_multipacket)
  125. {
  126. uint16_t did = py[0]; // data id
  127. uint16_t sdid = py[1]; // secondary data id
  128. uint16_t dc = py[2] & 255; // data count
  129. py += 3;
  130. pend = FFMIN(pend, py + dc);
  131. if (did == 0x143 && sdid == 0x102) { // subtitle distribution packet
  132. tgt = teletext_data_unit_from_op47_data(py, pend, tgt, wanted_lines);
  133. } else if (allow_multipacket && did == 0x143 && sdid == 0x203) { // VANC multipacket
  134. py += 2; // priority, line/field
  135. while (py < pend - 3) {
  136. tgt = teletext_data_unit_from_ancillary_packet(py, pend, tgt, wanted_lines, 0);
  137. py += 4 + (py[2] & 255); // ndid, nsdid, ndc, line/field
  138. }
  139. }
  140. return tgt;
  141. }
  142. static uint8_t* teletext_data_unit_from_vanc_data(uint8_t *src, uint8_t *tgt, int64_t wanted_lines)
  143. {
  144. uint16_t y[1920];
  145. uint16_t *py = y;
  146. uint16_t *pend = y + 1920;
  147. /* The 10-bit VANC data is packed in V210, we only need the luma component. */
  148. while (py < pend) {
  149. *py++ = (src[1] >> 2) + ((src[2] & 15) << 6);
  150. *py++ = src[4] + ((src[5] & 3) << 8);
  151. *py++ = (src[6] >> 4) + ((src[7] & 63) << 4);
  152. src += 8;
  153. }
  154. py = y;
  155. while (py < pend - 6) {
  156. if (py[0] == 0 && py[1] == 0x3ff && py[2] == 0x3ff) { // ancillary data flag
  157. py += 3;
  158. tgt = teletext_data_unit_from_ancillary_packet(py, pend, tgt, wanted_lines, 0);
  159. py += py[2] & 255;
  160. } else {
  161. py++;
  162. }
  163. }
  164. return tgt;
  165. }
  166. static void avpacket_queue_init(AVFormatContext *avctx, AVPacketQueue *q)
  167. {
  168. struct decklink_cctx *ctx = (struct decklink_cctx *)avctx->priv_data;
  169. memset(q, 0, sizeof(AVPacketQueue));
  170. pthread_mutex_init(&q->mutex, NULL);
  171. pthread_cond_init(&q->cond, NULL);
  172. q->avctx = avctx;
  173. q->max_q_size = ctx->queue_size;
  174. }
  175. static void avpacket_queue_flush(AVPacketQueue *q)
  176. {
  177. AVPacketList *pkt, *pkt1;
  178. pthread_mutex_lock(&q->mutex);
  179. for (pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
  180. pkt1 = pkt->next;
  181. av_packet_unref(&pkt->pkt);
  182. av_freep(&pkt);
  183. }
  184. q->last_pkt = NULL;
  185. q->first_pkt = NULL;
  186. q->nb_packets = 0;
  187. q->size = 0;
  188. pthread_mutex_unlock(&q->mutex);
  189. }
  190. static void avpacket_queue_end(AVPacketQueue *q)
  191. {
  192. avpacket_queue_flush(q);
  193. pthread_mutex_destroy(&q->mutex);
  194. pthread_cond_destroy(&q->cond);
  195. }
  196. static unsigned long long avpacket_queue_size(AVPacketQueue *q)
  197. {
  198. unsigned long long size;
  199. pthread_mutex_lock(&q->mutex);
  200. size = q->size;
  201. pthread_mutex_unlock(&q->mutex);
  202. return size;
  203. }
  204. static int avpacket_queue_put(AVPacketQueue *q, AVPacket *pkt)
  205. {
  206. AVPacketList *pkt1;
  207. // Drop Packet if queue size is > maximum queue size
  208. if (avpacket_queue_size(q) > (uint64_t)q->max_q_size) {
  209. av_log(q->avctx, AV_LOG_WARNING, "Decklink input buffer overrun!\n");
  210. return -1;
  211. }
  212. /* duplicate the packet */
  213. if (av_dup_packet(pkt) < 0) {
  214. return -1;
  215. }
  216. pkt1 = (AVPacketList *)av_malloc(sizeof(AVPacketList));
  217. if (!pkt1) {
  218. return -1;
  219. }
  220. pkt1->pkt = *pkt;
  221. pkt1->next = NULL;
  222. pthread_mutex_lock(&q->mutex);
  223. if (!q->last_pkt) {
  224. q->first_pkt = pkt1;
  225. } else {
  226. q->last_pkt->next = pkt1;
  227. }
  228. q->last_pkt = pkt1;
  229. q->nb_packets++;
  230. q->size += pkt1->pkt.size + sizeof(*pkt1);
  231. pthread_cond_signal(&q->cond);
  232. pthread_mutex_unlock(&q->mutex);
  233. return 0;
  234. }
  235. static int avpacket_queue_get(AVPacketQueue *q, AVPacket *pkt, int block)
  236. {
  237. AVPacketList *pkt1;
  238. int ret;
  239. pthread_mutex_lock(&q->mutex);
  240. for (;; ) {
  241. pkt1 = q->first_pkt;
  242. if (pkt1) {
  243. q->first_pkt = pkt1->next;
  244. if (!q->first_pkt) {
  245. q->last_pkt = NULL;
  246. }
  247. q->nb_packets--;
  248. q->size -= pkt1->pkt.size + sizeof(*pkt1);
  249. *pkt = pkt1->pkt;
  250. av_free(pkt1);
  251. ret = 1;
  252. break;
  253. } else if (!block) {
  254. ret = 0;
  255. break;
  256. } else {
  257. pthread_cond_wait(&q->cond, &q->mutex);
  258. }
  259. }
  260. pthread_mutex_unlock(&q->mutex);
  261. return ret;
  262. }
  263. class decklink_input_callback : public IDeckLinkInputCallback
  264. {
  265. public:
  266. decklink_input_callback(AVFormatContext *_avctx);
  267. ~decklink_input_callback();
  268. virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; }
  269. virtual ULONG STDMETHODCALLTYPE AddRef(void);
  270. virtual ULONG STDMETHODCALLTYPE Release(void);
  271. virtual HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(BMDVideoInputFormatChangedEvents, IDeckLinkDisplayMode*, BMDDetectedVideoInputFormatFlags);
  272. virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(IDeckLinkVideoInputFrame*, IDeckLinkAudioInputPacket*);
  273. private:
  274. ULONG m_refCount;
  275. pthread_mutex_t m_mutex;
  276. AVFormatContext *avctx;
  277. decklink_ctx *ctx;
  278. int no_video;
  279. int64_t initial_video_pts;
  280. int64_t initial_audio_pts;
  281. };
  282. decklink_input_callback::decklink_input_callback(AVFormatContext *_avctx) : m_refCount(0)
  283. {
  284. avctx = _avctx;
  285. decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  286. ctx = (struct decklink_ctx *)cctx->ctx;
  287. no_video = 0;
  288. initial_audio_pts = initial_video_pts = AV_NOPTS_VALUE;
  289. pthread_mutex_init(&m_mutex, NULL);
  290. }
  291. decklink_input_callback::~decklink_input_callback()
  292. {
  293. pthread_mutex_destroy(&m_mutex);
  294. }
  295. ULONG decklink_input_callback::AddRef(void)
  296. {
  297. pthread_mutex_lock(&m_mutex);
  298. m_refCount++;
  299. pthread_mutex_unlock(&m_mutex);
  300. return (ULONG)m_refCount;
  301. }
  302. ULONG decklink_input_callback::Release(void)
  303. {
  304. pthread_mutex_lock(&m_mutex);
  305. m_refCount--;
  306. pthread_mutex_unlock(&m_mutex);
  307. if (m_refCount == 0) {
  308. delete this;
  309. return 0;
  310. }
  311. return (ULONG)m_refCount;
  312. }
  313. static int64_t get_pkt_pts(IDeckLinkVideoInputFrame *videoFrame,
  314. IDeckLinkAudioInputPacket *audioFrame,
  315. int64_t wallclock,
  316. DecklinkPtsSource pts_src,
  317. AVRational time_base, int64_t *initial_pts)
  318. {
  319. int64_t pts = AV_NOPTS_VALUE;
  320. BMDTimeValue bmd_pts;
  321. BMDTimeValue bmd_duration;
  322. HRESULT res = E_INVALIDARG;
  323. switch (pts_src) {
  324. case PTS_SRC_AUDIO:
  325. if (audioFrame)
  326. res = audioFrame->GetPacketTime(&bmd_pts, time_base.den);
  327. break;
  328. case PTS_SRC_VIDEO:
  329. if (videoFrame)
  330. res = videoFrame->GetStreamTime(&bmd_pts, &bmd_duration, time_base.den);
  331. break;
  332. case PTS_SRC_REFERENCE:
  333. if (videoFrame)
  334. res = videoFrame->GetHardwareReferenceTimestamp(time_base.den, &bmd_pts, &bmd_duration);
  335. break;
  336. case PTS_SRC_WALLCLOCK:
  337. {
  338. /* MSVC does not support compound literals like AV_TIME_BASE_Q
  339. * in C++ code (compiler error C4576) */
  340. AVRational timebase;
  341. timebase.num = 1;
  342. timebase.den = AV_TIME_BASE;
  343. pts = av_rescale_q(wallclock, timebase, time_base);
  344. break;
  345. }
  346. }
  347. if (res == S_OK)
  348. pts = bmd_pts / time_base.num;
  349. if (pts != AV_NOPTS_VALUE && *initial_pts == AV_NOPTS_VALUE)
  350. *initial_pts = pts;
  351. if (*initial_pts != AV_NOPTS_VALUE)
  352. pts -= *initial_pts;
  353. return pts;
  354. }
  355. HRESULT decklink_input_callback::VideoInputFrameArrived(
  356. IDeckLinkVideoInputFrame *videoFrame, IDeckLinkAudioInputPacket *audioFrame)
  357. {
  358. void *frameBytes;
  359. void *audioFrameBytes;
  360. BMDTimeValue frameTime;
  361. BMDTimeValue frameDuration;
  362. int64_t wallclock = 0;
  363. ctx->frameCount++;
  364. if (ctx->audio_pts_source == PTS_SRC_WALLCLOCK || ctx->video_pts_source == PTS_SRC_WALLCLOCK)
  365. wallclock = av_gettime_relative();
  366. // Handle Video Frame
  367. if (videoFrame) {
  368. AVPacket pkt;
  369. av_init_packet(&pkt);
  370. if (ctx->frameCount % 25 == 0) {
  371. unsigned long long qsize = avpacket_queue_size(&ctx->queue);
  372. av_log(avctx, AV_LOG_DEBUG,
  373. "Frame received (#%lu) - Valid (%liB) - QSize %fMB\n",
  374. ctx->frameCount,
  375. videoFrame->GetRowBytes() * videoFrame->GetHeight(),
  376. (double)qsize / 1024 / 1024);
  377. }
  378. videoFrame->GetBytes(&frameBytes);
  379. videoFrame->GetStreamTime(&frameTime, &frameDuration,
  380. ctx->video_st->time_base.den);
  381. if (videoFrame->GetFlags() & bmdFrameHasNoInputSource) {
  382. if (ctx->draw_bars && videoFrame->GetPixelFormat() == bmdFormat8BitYUV) {
  383. unsigned bars[8] = {
  384. 0xEA80EA80, 0xD292D210, 0xA910A9A5, 0x90229035,
  385. 0x6ADD6ACA, 0x51EF515A, 0x286D28EF, 0x10801080 };
  386. int width = videoFrame->GetWidth();
  387. int height = videoFrame->GetHeight();
  388. unsigned *p = (unsigned *)frameBytes;
  389. for (int y = 0; y < height; y++) {
  390. for (int x = 0; x < width; x += 2)
  391. *p++ = bars[(x * 8) / width];
  392. }
  393. }
  394. if (!no_video) {
  395. av_log(avctx, AV_LOG_WARNING, "Frame received (#%lu) - No input signal detected "
  396. "- Frames dropped %u\n", ctx->frameCount, ++ctx->dropped);
  397. }
  398. no_video = 1;
  399. } else {
  400. if (no_video) {
  401. av_log(avctx, AV_LOG_WARNING, "Frame received (#%lu) - Input returned "
  402. "- Frames dropped %u\n", ctx->frameCount, ++ctx->dropped);
  403. }
  404. no_video = 0;
  405. }
  406. pkt.pts = get_pkt_pts(videoFrame, audioFrame, wallclock, ctx->video_pts_source, ctx->video_st->time_base, &initial_video_pts);
  407. pkt.dts = pkt.pts;
  408. pkt.duration = frameDuration;
  409. //To be made sure it still applies
  410. pkt.flags |= AV_PKT_FLAG_KEY;
  411. pkt.stream_index = ctx->video_st->index;
  412. pkt.data = (uint8_t *)frameBytes;
  413. pkt.size = videoFrame->GetRowBytes() *
  414. videoFrame->GetHeight();
  415. //fprintf(stderr,"Video Frame size %d ts %d\n", pkt.size, pkt.pts);
  416. if (!no_video && ctx->teletext_lines) {
  417. IDeckLinkVideoFrameAncillary *vanc;
  418. AVPacket txt_pkt;
  419. uint8_t txt_buf0[3531]; // 35 * 46 bytes decoded teletext lines + 1 byte data_identifier + 1920 bytes OP47 decode buffer
  420. uint8_t *txt_buf = txt_buf0;
  421. if (videoFrame->GetAncillaryData(&vanc) == S_OK) {
  422. int i;
  423. int64_t line_mask = 1;
  424. BMDPixelFormat vanc_format = vanc->GetPixelFormat();
  425. txt_buf[0] = 0x10; // data_identifier - EBU_data
  426. txt_buf++;
  427. #if CONFIG_LIBZVBI
  428. if (ctx->bmd_mode == bmdModePAL && (vanc_format == bmdFormat8BitYUV || vanc_format == bmdFormat10BitYUV)) {
  429. av_assert0(videoFrame->GetWidth() == 720);
  430. for (i = 6; i < 336; i++, line_mask <<= 1) {
  431. uint8_t *buf;
  432. if ((ctx->teletext_lines & line_mask) && vanc->GetBufferForVerticalBlankingLine(i, (void**)&buf) == S_OK) {
  433. if (vanc_format == bmdFormat8BitYUV)
  434. txt_buf = teletext_data_unit_from_vbi_data(i, buf, txt_buf, VBI_PIXFMT_UYVY);
  435. else
  436. txt_buf = teletext_data_unit_from_vbi_data_10bit(i, buf, txt_buf);
  437. }
  438. if (i == 22)
  439. i = 317;
  440. }
  441. }
  442. #endif
  443. if (videoFrame->GetWidth() == 1920 && vanc_format == bmdFormat10BitYUV) {
  444. int first_active_line = ctx->bmd_field_dominance == bmdProgressiveFrame ? 42 : 584;
  445. for (i = 8; i < first_active_line; i++) {
  446. uint8_t *buf;
  447. if (vanc->GetBufferForVerticalBlankingLine(i, (void**)&buf) == S_OK)
  448. txt_buf = teletext_data_unit_from_vanc_data(buf, txt_buf, ctx->teletext_lines);
  449. if (ctx->bmd_field_dominance != bmdProgressiveFrame && i == 20) // skip field1 active lines
  450. i = 569;
  451. if (txt_buf - txt_buf0 > 1611) { // ensure we still have at least 1920 bytes free in the buffer
  452. av_log(avctx, AV_LOG_ERROR, "Too many OP47 teletext packets.\n");
  453. break;
  454. }
  455. }
  456. }
  457. vanc->Release();
  458. if (txt_buf - txt_buf0 > 1) {
  459. int stuffing_units = (4 - ((45 + txt_buf - txt_buf0) / 46) % 4) % 4;
  460. while (stuffing_units--) {
  461. memset(txt_buf, 0xff, 46);
  462. txt_buf[1] = 0x2c; // data_unit_length
  463. txt_buf += 46;
  464. }
  465. av_init_packet(&txt_pkt);
  466. txt_pkt.pts = pkt.pts;
  467. txt_pkt.dts = pkt.dts;
  468. txt_pkt.stream_index = ctx->teletext_st->index;
  469. txt_pkt.data = txt_buf0;
  470. txt_pkt.size = txt_buf - txt_buf0;
  471. if (avpacket_queue_put(&ctx->queue, &txt_pkt) < 0) {
  472. ++ctx->dropped;
  473. }
  474. }
  475. }
  476. }
  477. if (avpacket_queue_put(&ctx->queue, &pkt) < 0) {
  478. ++ctx->dropped;
  479. }
  480. }
  481. // Handle Audio Frame
  482. if (audioFrame) {
  483. AVPacket pkt;
  484. BMDTimeValue audio_pts;
  485. av_init_packet(&pkt);
  486. //hack among hacks
  487. pkt.size = audioFrame->GetSampleFrameCount() * ctx->audio_st->codecpar->channels * (16 / 8);
  488. audioFrame->GetBytes(&audioFrameBytes);
  489. audioFrame->GetPacketTime(&audio_pts, ctx->audio_st->time_base.den);
  490. pkt.pts = get_pkt_pts(videoFrame, audioFrame, wallclock, ctx->audio_pts_source, ctx->audio_st->time_base, &initial_audio_pts);
  491. pkt.dts = pkt.pts;
  492. //fprintf(stderr,"Audio Frame size %d ts %d\n", pkt.size, pkt.pts);
  493. pkt.flags |= AV_PKT_FLAG_KEY;
  494. pkt.stream_index = ctx->audio_st->index;
  495. pkt.data = (uint8_t *)audioFrameBytes;
  496. if (avpacket_queue_put(&ctx->queue, &pkt) < 0) {
  497. ++ctx->dropped;
  498. }
  499. }
  500. return S_OK;
  501. }
  502. HRESULT decklink_input_callback::VideoInputFormatChanged(
  503. BMDVideoInputFormatChangedEvents events, IDeckLinkDisplayMode *mode,
  504. BMDDetectedVideoInputFormatFlags)
  505. {
  506. return S_OK;
  507. }
  508. static HRESULT decklink_start_input(AVFormatContext *avctx)
  509. {
  510. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  511. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  512. ctx->input_callback = new decklink_input_callback(avctx);
  513. ctx->dli->SetCallback(ctx->input_callback);
  514. return ctx->dli->StartStreams();
  515. }
  516. extern "C" {
  517. av_cold int ff_decklink_read_close(AVFormatContext *avctx)
  518. {
  519. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  520. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  521. if (ctx->capture_started) {
  522. ctx->dli->StopStreams();
  523. ctx->dli->DisableVideoInput();
  524. ctx->dli->DisableAudioInput();
  525. }
  526. ff_decklink_cleanup(avctx);
  527. avpacket_queue_end(&ctx->queue);
  528. av_freep(&cctx->ctx);
  529. return 0;
  530. }
  531. av_cold int ff_decklink_read_header(AVFormatContext *avctx)
  532. {
  533. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  534. struct decklink_ctx *ctx;
  535. AVStream *st;
  536. HRESULT result;
  537. char fname[1024];
  538. char *tmp;
  539. int mode_num = 0;
  540. int ret;
  541. ctx = (struct decklink_ctx *) av_mallocz(sizeof(struct decklink_ctx));
  542. if (!ctx)
  543. return AVERROR(ENOMEM);
  544. ctx->list_devices = cctx->list_devices;
  545. ctx->list_formats = cctx->list_formats;
  546. ctx->teletext_lines = cctx->teletext_lines;
  547. ctx->preroll = cctx->preroll;
  548. ctx->duplex_mode = cctx->duplex_mode;
  549. if (cctx->video_input > 0 && (unsigned int)cctx->video_input < FF_ARRAY_ELEMS(decklink_video_connection_map))
  550. ctx->video_input = decklink_video_connection_map[cctx->video_input];
  551. if (cctx->audio_input > 0 && (unsigned int)cctx->audio_input < FF_ARRAY_ELEMS(decklink_audio_connection_map))
  552. ctx->audio_input = decklink_audio_connection_map[cctx->audio_input];
  553. ctx->audio_pts_source = cctx->audio_pts_source;
  554. ctx->video_pts_source = cctx->video_pts_source;
  555. ctx->draw_bars = cctx->draw_bars;
  556. cctx->ctx = ctx;
  557. /* Check audio channel option for valid values: 2, 8 or 16 */
  558. switch (cctx->audio_channels) {
  559. case 2:
  560. case 8:
  561. case 16:
  562. break;
  563. default:
  564. av_log(avctx, AV_LOG_ERROR, "Value of channels option must be one of 2, 8 or 16\n");
  565. return AVERROR(EINVAL);
  566. }
  567. /* List available devices. */
  568. if (ctx->list_devices) {
  569. ff_decklink_list_devices(avctx);
  570. return AVERROR_EXIT;
  571. }
  572. strcpy (fname, avctx->filename);
  573. tmp=strchr (fname, '@');
  574. if (tmp != NULL) {
  575. av_log(avctx, AV_LOG_WARNING, "The @mode syntax is deprecated and will be removed. Please use the -format_code option.\n");
  576. mode_num = atoi (tmp+1);
  577. *tmp = 0;
  578. }
  579. ret = ff_decklink_init_device(avctx, fname);
  580. if (ret < 0)
  581. return ret;
  582. /* Get input device. */
  583. if (ctx->dl->QueryInterface(IID_IDeckLinkInput, (void **) &ctx->dli) != S_OK) {
  584. av_log(avctx, AV_LOG_ERROR, "Could not open input device from '%s'\n",
  585. avctx->filename);
  586. ret = AVERROR(EIO);
  587. goto error;
  588. }
  589. /* List supported formats. */
  590. if (ctx->list_formats) {
  591. ff_decklink_list_formats(avctx, DIRECTION_IN);
  592. ret = AVERROR_EXIT;
  593. goto error;
  594. }
  595. if (mode_num > 0 || cctx->format_code) {
  596. if (ff_decklink_set_format(avctx, DIRECTION_IN, mode_num) < 0) {
  597. av_log(avctx, AV_LOG_ERROR, "Could not set mode number %d or format code %s for %s\n",
  598. mode_num, (cctx->format_code) ? cctx->format_code : "(unset)", fname);
  599. ret = AVERROR(EIO);
  600. goto error;
  601. }
  602. }
  603. #if !CONFIG_LIBZVBI
  604. if (ctx->teletext_lines && ctx->bmd_mode == bmdModePAL) {
  605. av_log(avctx, AV_LOG_ERROR, "Libzvbi support is needed for capturing SD PAL teletext, please recompile FFmpeg.\n");
  606. ret = AVERROR(ENOSYS);
  607. goto error;
  608. }
  609. #endif
  610. /* Setup streams. */
  611. st = avformat_new_stream(avctx, NULL);
  612. if (!st) {
  613. av_log(avctx, AV_LOG_ERROR, "Cannot add stream\n");
  614. ret = AVERROR(ENOMEM);
  615. goto error;
  616. }
  617. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  618. st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
  619. st->codecpar->sample_rate = bmdAudioSampleRate48kHz;
  620. st->codecpar->channels = cctx->audio_channels;
  621. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  622. ctx->audio_st=st;
  623. st = avformat_new_stream(avctx, NULL);
  624. if (!st) {
  625. av_log(avctx, AV_LOG_ERROR, "Cannot add stream\n");
  626. ret = AVERROR(ENOMEM);
  627. goto error;
  628. }
  629. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  630. st->codecpar->width = ctx->bmd_width;
  631. st->codecpar->height = ctx->bmd_height;
  632. st->time_base.den = ctx->bmd_tb_den;
  633. st->time_base.num = ctx->bmd_tb_num;
  634. av_stream_set_r_frame_rate(st, av_make_q(st->time_base.den, st->time_base.num));
  635. if (cctx->v210) {
  636. st->codecpar->codec_id = AV_CODEC_ID_V210;
  637. st->codecpar->codec_tag = MKTAG('V', '2', '1', '0');
  638. st->codecpar->bit_rate = av_rescale(ctx->bmd_width * ctx->bmd_height * 64, st->time_base.den, st->time_base.num * 3);
  639. } else {
  640. st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
  641. st->codecpar->format = AV_PIX_FMT_UYVY422;
  642. st->codecpar->codec_tag = MKTAG('U', 'Y', 'V', 'Y');
  643. st->codecpar->bit_rate = av_rescale(ctx->bmd_width * ctx->bmd_height * 16, st->time_base.den, st->time_base.num);
  644. }
  645. switch (ctx->bmd_field_dominance) {
  646. case bmdUpperFieldFirst:
  647. st->codecpar->field_order = AV_FIELD_TT;
  648. break;
  649. case bmdLowerFieldFirst:
  650. st->codecpar->field_order = AV_FIELD_BB;
  651. break;
  652. case bmdProgressiveFrame:
  653. case bmdProgressiveSegmentedFrame:
  654. st->codecpar->field_order = AV_FIELD_PROGRESSIVE;
  655. break;
  656. }
  657. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  658. ctx->video_st=st;
  659. if (ctx->teletext_lines) {
  660. st = avformat_new_stream(avctx, NULL);
  661. if (!st) {
  662. av_log(avctx, AV_LOG_ERROR, "Cannot add stream\n");
  663. ret = AVERROR(ENOMEM);
  664. goto error;
  665. }
  666. st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
  667. st->time_base.den = ctx->bmd_tb_den;
  668. st->time_base.num = ctx->bmd_tb_num;
  669. st->codecpar->codec_id = AV_CODEC_ID_DVB_TELETEXT;
  670. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  671. ctx->teletext_st = st;
  672. }
  673. av_log(avctx, AV_LOG_VERBOSE, "Using %d input audio channels\n", ctx->audio_st->codecpar->channels);
  674. result = ctx->dli->EnableAudioInput(bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger, ctx->audio_st->codecpar->channels);
  675. if (result != S_OK) {
  676. av_log(avctx, AV_LOG_ERROR, "Cannot enable audio input\n");
  677. ret = AVERROR(EIO);
  678. goto error;
  679. }
  680. result = ctx->dli->EnableVideoInput(ctx->bmd_mode,
  681. cctx->v210 ? bmdFormat10BitYUV : bmdFormat8BitYUV,
  682. bmdVideoInputFlagDefault);
  683. if (result != S_OK) {
  684. av_log(avctx, AV_LOG_ERROR, "Cannot enable video input\n");
  685. ret = AVERROR(EIO);
  686. goto error;
  687. }
  688. avpacket_queue_init (avctx, &ctx->queue);
  689. if (decklink_start_input (avctx) != S_OK) {
  690. av_log(avctx, AV_LOG_ERROR, "Cannot start input stream\n");
  691. ret = AVERROR(EIO);
  692. goto error;
  693. }
  694. return 0;
  695. error:
  696. ff_decklink_cleanup(avctx);
  697. return ret;
  698. }
  699. int ff_decklink_read_packet(AVFormatContext *avctx, AVPacket *pkt)
  700. {
  701. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  702. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  703. avpacket_queue_get(&ctx->queue, pkt, 1);
  704. return 0;
  705. }
  706. } /* extern "C" */