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.

1161 lines
39KB

  1. /*
  2. * Blackmagic DeckLink input
  3. * Copyright (c) 2013-2014 Luca Barbato, Deti Fliegl
  4. * Copyright (c) 2014 Rafaël Carré
  5. * Copyright (c) 2017 Akamai Technologies, Inc.
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* Include internal.h first to avoid conflict between winsock.h (used by
  24. * DeckLink headers) and winsock2.h (used by libavformat) in MSVC++ builds */
  25. extern "C" {
  26. #include "libavformat/internal.h"
  27. }
  28. #include <DeckLinkAPI.h>
  29. extern "C" {
  30. #include "config.h"
  31. #include "libavformat/avformat.h"
  32. #include "libavutil/avassert.h"
  33. #include "libavutil/avutil.h"
  34. #include "libavutil/common.h"
  35. #include "libavutil/imgutils.h"
  36. #include "libavutil/intreadwrite.h"
  37. #include "libavutil/time.h"
  38. #include "libavutil/mathematics.h"
  39. #include "libavutil/reverse.h"
  40. #include "avdevice.h"
  41. #if CONFIG_LIBZVBI
  42. #include <libzvbi.h>
  43. #endif
  44. }
  45. #include "decklink_common.h"
  46. #include "decklink_dec.h"
  47. #define MAX_WIDTH_VANC 1920
  48. const BMDDisplayMode AUTODETECT_DEFAULT_MODE = bmdModeNTSC;
  49. typedef struct VANCLineNumber {
  50. BMDDisplayMode mode;
  51. int vanc_start;
  52. int field0_vanc_end;
  53. int field1_vanc_start;
  54. int vanc_end;
  55. } VANCLineNumber;
  56. /* These VANC line numbers need not be very accurate. In any case
  57. * GetBufferForVerticalBlankingLine() will return an error when invalid
  58. * ancillary line number was requested. We just need to make sure that the
  59. * entire VANC region is covered, while making sure we don't decode VANC of
  60. * another source during switching*/
  61. static VANCLineNumber vanc_line_numbers[] = {
  62. /* SD Modes */
  63. {bmdModeNTSC, 11, 19, 274, 282},
  64. {bmdModeNTSC2398, 11, 19, 274, 282},
  65. {bmdModePAL, 7, 22, 320, 335},
  66. {bmdModeNTSCp, 11, -1, -1, 39},
  67. {bmdModePALp, 7, -1, -1, 45},
  68. /* HD 1080 Modes */
  69. {bmdModeHD1080p2398, 8, -1, -1, 42},
  70. {bmdModeHD1080p24, 8, -1, -1, 42},
  71. {bmdModeHD1080p25, 8, -1, -1, 42},
  72. {bmdModeHD1080p2997, 8, -1, -1, 42},
  73. {bmdModeHD1080p30, 8, -1, -1, 42},
  74. {bmdModeHD1080i50, 8, 20, 570, 585},
  75. {bmdModeHD1080i5994, 8, 20, 570, 585},
  76. {bmdModeHD1080i6000, 8, 20, 570, 585},
  77. {bmdModeHD1080p50, 8, -1, -1, 42},
  78. {bmdModeHD1080p5994, 8, -1, -1, 42},
  79. {bmdModeHD1080p6000, 8, -1, -1, 42},
  80. /* HD 720 Modes */
  81. {bmdModeHD720p50, 8, -1, -1, 26},
  82. {bmdModeHD720p5994, 8, -1, -1, 26},
  83. {bmdModeHD720p60, 8, -1, -1, 26},
  84. /* For all other modes, for which we don't support VANC */
  85. {bmdModeUnknown, 0, -1, -1, -1}
  86. };
  87. static int get_vanc_line_idx(BMDDisplayMode mode)
  88. {
  89. unsigned int i;
  90. for (i = 0; i < FF_ARRAY_ELEMS(vanc_line_numbers); i++) {
  91. if (mode == vanc_line_numbers[i].mode)
  92. return i;
  93. }
  94. /* Return the VANC idx for Unknown mode */
  95. return i - 1;
  96. }
  97. static inline void clear_parity_bits(uint16_t *buf, int len) {
  98. int i;
  99. for (i = 0; i < len; i++)
  100. buf[i] &= 0xff;
  101. }
  102. static int check_vanc_parity_checksum(uint16_t *buf, int len, uint16_t checksum) {
  103. int i;
  104. uint16_t vanc_sum = 0;
  105. for (i = 3; i < len - 1; i++) {
  106. uint16_t v = buf[i];
  107. int np = v >> 8;
  108. int p = av_parity(v & 0xff);
  109. if ((!!p ^ !!(v & 0x100)) || (np != 1 && np != 2)) {
  110. // Parity check failed
  111. return -1;
  112. }
  113. vanc_sum += v;
  114. }
  115. vanc_sum &= 0x1ff;
  116. vanc_sum |= ((~vanc_sum & 0x100) << 1);
  117. if (checksum != vanc_sum) {
  118. // Checksum verification failed
  119. return -1;
  120. }
  121. return 0;
  122. }
  123. /* The 10-bit VANC data is packed in V210, we only need the luma component. */
  124. static void extract_luma_from_v210(uint16_t *dst, const uint8_t *src, int width)
  125. {
  126. int i;
  127. for (i = 0; i < width / 3; i++) {
  128. *dst++ = (src[1] >> 2) + ((src[2] & 15) << 6);
  129. *dst++ = src[4] + ((src[5] & 3) << 8);
  130. *dst++ = (src[6] >> 4) + ((src[7] & 63) << 4);
  131. src += 8;
  132. }
  133. }
  134. static uint8_t calc_parity_and_line_offset(int line)
  135. {
  136. uint8_t ret = (line < 313) << 5;
  137. if (line >= 7 && line <= 22)
  138. ret += line;
  139. if (line >= 320 && line <= 335)
  140. ret += (line - 313);
  141. return ret;
  142. }
  143. static void fill_data_unit_head(int line, uint8_t *tgt)
  144. {
  145. tgt[0] = 0x02; // data_unit_id
  146. tgt[1] = 0x2c; // data_unit_length
  147. tgt[2] = calc_parity_and_line_offset(line); // field_parity, line_offset
  148. tgt[3] = 0xe4; // framing code
  149. }
  150. #if CONFIG_LIBZVBI
  151. static uint8_t* teletext_data_unit_from_vbi_data(int line, uint8_t *src, uint8_t *tgt, vbi_pixfmt fmt)
  152. {
  153. vbi_bit_slicer slicer;
  154. vbi_bit_slicer_init(&slicer, 720, 13500000, 6937500, 6937500, 0x00aaaae4, 0xffff, 18, 6, 42 * 8, VBI_MODULATION_NRZ_MSB, fmt);
  155. if (vbi_bit_slice(&slicer, src, tgt + 4) == FALSE)
  156. return tgt;
  157. fill_data_unit_head(line, tgt);
  158. return tgt + 46;
  159. }
  160. static uint8_t* teletext_data_unit_from_vbi_data_10bit(int line, uint8_t *src, uint8_t *tgt)
  161. {
  162. uint8_t y[720];
  163. uint8_t *py = y;
  164. uint8_t *pend = y + 720;
  165. /* The 10-bit VBI data is packed in V210, but libzvbi only supports 8-bit,
  166. * so we extract the 8 MSBs of the luma component, that is enough for
  167. * teletext bit slicing. */
  168. while (py < pend) {
  169. *py++ = (src[1] >> 4) + ((src[2] & 15) << 4);
  170. *py++ = (src[4] >> 2) + ((src[5] & 3 ) << 6);
  171. *py++ = (src[6] >> 6) + ((src[7] & 63) << 2);
  172. src += 8;
  173. }
  174. return teletext_data_unit_from_vbi_data(line, y, tgt, VBI_PIXFMT_YUV420);
  175. }
  176. #endif
  177. static uint8_t* teletext_data_unit_from_op47_vbi_packet(int line, uint16_t *py, uint8_t *tgt)
  178. {
  179. int i;
  180. if (py[0] != 0x255 || py[1] != 0x255 || py[2] != 0x227)
  181. return tgt;
  182. fill_data_unit_head(line, tgt);
  183. py += 3;
  184. tgt += 4;
  185. for (i = 0; i < 42; i++)
  186. *tgt++ = ff_reverse[py[i] & 255];
  187. return tgt;
  188. }
  189. static int linemask_matches(int line, int64_t mask)
  190. {
  191. int shift = -1;
  192. if (line >= 6 && line <= 22)
  193. shift = line - 6;
  194. if (line >= 318 && line <= 335)
  195. shift = line - 318 + 17;
  196. return shift >= 0 && ((1ULL << shift) & mask);
  197. }
  198. static uint8_t* teletext_data_unit_from_op47_data(uint16_t *py, uint16_t *pend, uint8_t *tgt, int64_t wanted_lines)
  199. {
  200. if (py < pend - 9) {
  201. if (py[0] == 0x151 && py[1] == 0x115 && py[3] == 0x102) { // identifier, identifier, format code for WST teletext
  202. uint16_t *descriptors = py + 4;
  203. int i;
  204. py += 9;
  205. for (i = 0; i < 5 && py < pend - 45; i++, py += 45) {
  206. int line = (descriptors[i] & 31) + (!(descriptors[i] & 128)) * 313;
  207. if (line && linemask_matches(line, wanted_lines))
  208. tgt = teletext_data_unit_from_op47_vbi_packet(line, py, tgt);
  209. }
  210. }
  211. }
  212. return tgt;
  213. }
  214. 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)
  215. {
  216. uint16_t did = py[0]; // data id
  217. uint16_t sdid = py[1]; // secondary data id
  218. uint16_t dc = py[2] & 255; // data count
  219. py += 3;
  220. pend = FFMIN(pend, py + dc);
  221. if (did == 0x143 && sdid == 0x102) { // subtitle distribution packet
  222. tgt = teletext_data_unit_from_op47_data(py, pend, tgt, wanted_lines);
  223. } else if (allow_multipacket && did == 0x143 && sdid == 0x203) { // VANC multipacket
  224. py += 2; // priority, line/field
  225. while (py < pend - 3) {
  226. tgt = teletext_data_unit_from_ancillary_packet(py, pend, tgt, wanted_lines, 0);
  227. py += 4 + (py[2] & 255); // ndid, nsdid, ndc, line/field
  228. }
  229. }
  230. return tgt;
  231. }
  232. static uint8_t *vanc_to_cc(AVFormatContext *avctx, uint16_t *buf, size_t words,
  233. unsigned &cc_count)
  234. {
  235. size_t i, len = (buf[5] & 0xff) + 6 + 1;
  236. uint8_t cdp_sum, rate;
  237. uint16_t hdr, ftr;
  238. uint8_t *cc;
  239. uint16_t *cdp = &buf[6]; // CDP follows
  240. if (cdp[0] != 0x96 || cdp[1] != 0x69) {
  241. av_log(avctx, AV_LOG_WARNING, "Invalid CDP header 0x%.2x 0x%.2x\n", cdp[0], cdp[1]);
  242. return NULL;
  243. }
  244. len -= 7; // remove VANC header and checksum
  245. if (cdp[2] != len) {
  246. av_log(avctx, AV_LOG_WARNING, "CDP len %d != %zu\n", cdp[2], len);
  247. return NULL;
  248. }
  249. cdp_sum = 0;
  250. for (i = 0; i < len - 1; i++)
  251. cdp_sum += cdp[i];
  252. cdp_sum = cdp_sum ? 256 - cdp_sum : 0;
  253. if (cdp[len - 1] != cdp_sum) {
  254. av_log(avctx, AV_LOG_WARNING, "CDP checksum invalid 0x%.4x != 0x%.4x\n", cdp_sum, cdp[len-1]);
  255. return NULL;
  256. }
  257. rate = cdp[3];
  258. if (!(rate & 0x0f)) {
  259. av_log(avctx, AV_LOG_WARNING, "CDP frame rate invalid (0x%.2x)\n", rate);
  260. return NULL;
  261. }
  262. rate >>= 4;
  263. if (rate > 8) {
  264. av_log(avctx, AV_LOG_WARNING, "CDP frame rate invalid (0x%.2x)\n", rate);
  265. return NULL;
  266. }
  267. if (!(cdp[4] & 0x43)) /* ccdata_present | caption_service_active | reserved */ {
  268. av_log(avctx, AV_LOG_WARNING, "CDP flags invalid (0x%.2x)\n", cdp[4]);
  269. return NULL;
  270. }
  271. hdr = (cdp[5] << 8) | cdp[6];
  272. if (cdp[7] != 0x72) /* ccdata_id */ {
  273. av_log(avctx, AV_LOG_WARNING, "Invalid ccdata_id 0x%.2x\n", cdp[7]);
  274. return NULL;
  275. }
  276. cc_count = cdp[8];
  277. if (!(cc_count & 0xe0)) {
  278. av_log(avctx, AV_LOG_WARNING, "Invalid cc_count 0x%.2x\n", cc_count);
  279. return NULL;
  280. }
  281. cc_count &= 0x1f;
  282. if ((len - 13) < cc_count * 3) {
  283. av_log(avctx, AV_LOG_WARNING, "Invalid cc_count %d (> %zu)\n", cc_count * 3, len - 13);
  284. return NULL;
  285. }
  286. if (cdp[len - 4] != 0x74) /* footer id */ {
  287. av_log(avctx, AV_LOG_WARNING, "Invalid footer id 0x%.2x\n", cdp[len-4]);
  288. return NULL;
  289. }
  290. ftr = (cdp[len - 3] << 8) | cdp[len - 2];
  291. if (ftr != hdr) {
  292. av_log(avctx, AV_LOG_WARNING, "Header 0x%.4x != Footer 0x%.4x\n", hdr, ftr);
  293. return NULL;
  294. }
  295. cc = (uint8_t *)av_malloc(cc_count * 3);
  296. if (cc == NULL) {
  297. av_log(avctx, AV_LOG_WARNING, "CC - av_malloc failed for cc_count = %d\n", cc_count);
  298. return NULL;
  299. }
  300. for (size_t i = 0; i < cc_count; i++) {
  301. cc[3*i + 0] = cdp[9 + 3*i+0] /* & 3 */;
  302. cc[3*i + 1] = cdp[9 + 3*i+1];
  303. cc[3*i + 2] = cdp[9 + 3*i+2];
  304. }
  305. cc_count *= 3;
  306. return cc;
  307. }
  308. static uint8_t *get_metadata(AVFormatContext *avctx, uint16_t *buf, size_t width,
  309. uint8_t *tgt, size_t tgt_size, AVPacket *pkt)
  310. {
  311. decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  312. uint16_t *max_buf = buf + width;
  313. while (buf < max_buf - 6) {
  314. int len;
  315. uint16_t did = buf[3] & 0xFF; // data id
  316. uint16_t sdid = buf[4] & 0xFF; // secondary data id
  317. /* Check for VANC header */
  318. if (buf[0] != 0 || buf[1] != 0x3ff || buf[2] != 0x3ff) {
  319. return tgt;
  320. }
  321. len = (buf[5] & 0xff) + 6 + 1;
  322. if (len > max_buf - buf) {
  323. av_log(avctx, AV_LOG_WARNING, "Data Count (%d) > data left (%zu)\n",
  324. len, max_buf - buf);
  325. return tgt;
  326. }
  327. if (did == 0x43 && (sdid == 0x02 || sdid == 0x03) && cctx->teletext_lines &&
  328. width == 1920 && tgt_size >= 1920) {
  329. if (check_vanc_parity_checksum(buf, len, buf[len - 1]) < 0) {
  330. av_log(avctx, AV_LOG_WARNING, "VANC parity or checksum incorrect\n");
  331. goto skip_packet;
  332. }
  333. tgt = teletext_data_unit_from_ancillary_packet(buf + 3, buf + len, tgt, cctx->teletext_lines, 1);
  334. } else if (did == 0x61 && sdid == 0x01) {
  335. unsigned int data_len;
  336. uint8_t *data;
  337. if (check_vanc_parity_checksum(buf, len, buf[len - 1]) < 0) {
  338. av_log(avctx, AV_LOG_WARNING, "VANC parity or checksum incorrect\n");
  339. goto skip_packet;
  340. }
  341. clear_parity_bits(buf, len);
  342. data = vanc_to_cc(avctx, buf, width, data_len);
  343. if (data) {
  344. if (av_packet_add_side_data(pkt, AV_PKT_DATA_A53_CC, data, data_len) < 0)
  345. av_free(data);
  346. }
  347. } else {
  348. av_log(avctx, AV_LOG_DEBUG, "Unknown meta data DID = 0x%.2x SDID = 0x%.2x\n",
  349. did, sdid);
  350. }
  351. skip_packet:
  352. buf += len;
  353. }
  354. return tgt;
  355. }
  356. static void avpacket_queue_init(AVFormatContext *avctx, AVPacketQueue *q)
  357. {
  358. struct decklink_cctx *ctx = (struct decklink_cctx *)avctx->priv_data;
  359. memset(q, 0, sizeof(AVPacketQueue));
  360. pthread_mutex_init(&q->mutex, NULL);
  361. pthread_cond_init(&q->cond, NULL);
  362. q->avctx = avctx;
  363. q->max_q_size = ctx->queue_size;
  364. }
  365. static void avpacket_queue_flush(AVPacketQueue *q)
  366. {
  367. AVPacketList *pkt, *pkt1;
  368. pthread_mutex_lock(&q->mutex);
  369. for (pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
  370. pkt1 = pkt->next;
  371. av_packet_unref(&pkt->pkt);
  372. av_freep(&pkt);
  373. }
  374. q->last_pkt = NULL;
  375. q->first_pkt = NULL;
  376. q->nb_packets = 0;
  377. q->size = 0;
  378. pthread_mutex_unlock(&q->mutex);
  379. }
  380. static void avpacket_queue_end(AVPacketQueue *q)
  381. {
  382. avpacket_queue_flush(q);
  383. pthread_mutex_destroy(&q->mutex);
  384. pthread_cond_destroy(&q->cond);
  385. }
  386. static unsigned long long avpacket_queue_size(AVPacketQueue *q)
  387. {
  388. unsigned long long size;
  389. pthread_mutex_lock(&q->mutex);
  390. size = q->size;
  391. pthread_mutex_unlock(&q->mutex);
  392. return size;
  393. }
  394. static int avpacket_queue_put(AVPacketQueue *q, AVPacket *pkt)
  395. {
  396. AVPacketList *pkt1;
  397. int ret;
  398. // Drop Packet if queue size is > maximum queue size
  399. if (avpacket_queue_size(q) > (uint64_t)q->max_q_size) {
  400. av_log(q->avctx, AV_LOG_WARNING, "Decklink input buffer overrun!\n");
  401. return -1;
  402. }
  403. pkt1 = (AVPacketList *)av_mallocz(sizeof(AVPacketList));
  404. if (!pkt1) {
  405. return -1;
  406. }
  407. ret = av_packet_ref(&pkt1->pkt, pkt);
  408. av_packet_unref(pkt);
  409. if (ret < 0) {
  410. av_free(pkt1);
  411. return -1;
  412. }
  413. pkt1->next = NULL;
  414. pthread_mutex_lock(&q->mutex);
  415. if (!q->last_pkt) {
  416. q->first_pkt = pkt1;
  417. } else {
  418. q->last_pkt->next = pkt1;
  419. }
  420. q->last_pkt = pkt1;
  421. q->nb_packets++;
  422. q->size += pkt1->pkt.size + sizeof(*pkt1);
  423. pthread_cond_signal(&q->cond);
  424. pthread_mutex_unlock(&q->mutex);
  425. return 0;
  426. }
  427. static int avpacket_queue_get(AVPacketQueue *q, AVPacket *pkt, int block)
  428. {
  429. AVPacketList *pkt1;
  430. int ret;
  431. pthread_mutex_lock(&q->mutex);
  432. for (;; ) {
  433. pkt1 = q->first_pkt;
  434. if (pkt1) {
  435. q->first_pkt = pkt1->next;
  436. if (!q->first_pkt) {
  437. q->last_pkt = NULL;
  438. }
  439. q->nb_packets--;
  440. q->size -= pkt1->pkt.size + sizeof(*pkt1);
  441. *pkt = pkt1->pkt;
  442. av_free(pkt1);
  443. ret = 1;
  444. break;
  445. } else if (!block) {
  446. ret = 0;
  447. break;
  448. } else {
  449. pthread_cond_wait(&q->cond, &q->mutex);
  450. }
  451. }
  452. pthread_mutex_unlock(&q->mutex);
  453. return ret;
  454. }
  455. class decklink_input_callback : public IDeckLinkInputCallback
  456. {
  457. public:
  458. decklink_input_callback(AVFormatContext *_avctx);
  459. ~decklink_input_callback();
  460. virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; }
  461. virtual ULONG STDMETHODCALLTYPE AddRef(void);
  462. virtual ULONG STDMETHODCALLTYPE Release(void);
  463. virtual HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(BMDVideoInputFormatChangedEvents, IDeckLinkDisplayMode*, BMDDetectedVideoInputFormatFlags);
  464. virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(IDeckLinkVideoInputFrame*, IDeckLinkAudioInputPacket*);
  465. private:
  466. ULONG m_refCount;
  467. pthread_mutex_t m_mutex;
  468. AVFormatContext *avctx;
  469. decklink_ctx *ctx;
  470. int no_video;
  471. int64_t initial_video_pts;
  472. int64_t initial_audio_pts;
  473. };
  474. decklink_input_callback::decklink_input_callback(AVFormatContext *_avctx) : m_refCount(0)
  475. {
  476. avctx = _avctx;
  477. decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  478. ctx = (struct decklink_ctx *)cctx->ctx;
  479. no_video = 0;
  480. initial_audio_pts = initial_video_pts = AV_NOPTS_VALUE;
  481. pthread_mutex_init(&m_mutex, NULL);
  482. }
  483. decklink_input_callback::~decklink_input_callback()
  484. {
  485. pthread_mutex_destroy(&m_mutex);
  486. }
  487. ULONG decklink_input_callback::AddRef(void)
  488. {
  489. pthread_mutex_lock(&m_mutex);
  490. m_refCount++;
  491. pthread_mutex_unlock(&m_mutex);
  492. return (ULONG)m_refCount;
  493. }
  494. ULONG decklink_input_callback::Release(void)
  495. {
  496. pthread_mutex_lock(&m_mutex);
  497. m_refCount--;
  498. pthread_mutex_unlock(&m_mutex);
  499. if (m_refCount == 0) {
  500. delete this;
  501. return 0;
  502. }
  503. return (ULONG)m_refCount;
  504. }
  505. static int64_t get_pkt_pts(IDeckLinkVideoInputFrame *videoFrame,
  506. IDeckLinkAudioInputPacket *audioFrame,
  507. int64_t wallclock,
  508. int64_t abs_wallclock,
  509. DecklinkPtsSource pts_src,
  510. AVRational time_base, int64_t *initial_pts,
  511. int copyts)
  512. {
  513. int64_t pts = AV_NOPTS_VALUE;
  514. BMDTimeValue bmd_pts;
  515. BMDTimeValue bmd_duration;
  516. HRESULT res = E_INVALIDARG;
  517. switch (pts_src) {
  518. case PTS_SRC_AUDIO:
  519. if (audioFrame)
  520. res = audioFrame->GetPacketTime(&bmd_pts, time_base.den);
  521. break;
  522. case PTS_SRC_VIDEO:
  523. if (videoFrame)
  524. res = videoFrame->GetStreamTime(&bmd_pts, &bmd_duration, time_base.den);
  525. break;
  526. case PTS_SRC_REFERENCE:
  527. if (videoFrame)
  528. res = videoFrame->GetHardwareReferenceTimestamp(time_base.den, &bmd_pts, &bmd_duration);
  529. break;
  530. case PTS_SRC_WALLCLOCK:
  531. /* fall through */
  532. case PTS_SRC_ABS_WALLCLOCK:
  533. {
  534. /* MSVC does not support compound literals like AV_TIME_BASE_Q
  535. * in C++ code (compiler error C4576) */
  536. AVRational timebase;
  537. timebase.num = 1;
  538. timebase.den = AV_TIME_BASE;
  539. if (pts_src == PTS_SRC_WALLCLOCK)
  540. pts = av_rescale_q(wallclock, timebase, time_base);
  541. else
  542. pts = av_rescale_q(abs_wallclock, timebase, time_base);
  543. break;
  544. }
  545. }
  546. if (res == S_OK)
  547. pts = bmd_pts / time_base.num;
  548. if (!copyts) {
  549. if (pts != AV_NOPTS_VALUE && *initial_pts == AV_NOPTS_VALUE)
  550. *initial_pts = pts;
  551. if (*initial_pts != AV_NOPTS_VALUE)
  552. pts -= *initial_pts;
  553. }
  554. return pts;
  555. }
  556. HRESULT decklink_input_callback::VideoInputFrameArrived(
  557. IDeckLinkVideoInputFrame *videoFrame, IDeckLinkAudioInputPacket *audioFrame)
  558. {
  559. void *frameBytes;
  560. void *audioFrameBytes;
  561. BMDTimeValue frameTime;
  562. BMDTimeValue frameDuration;
  563. int64_t wallclock = 0, abs_wallclock = 0;
  564. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  565. if (ctx->autodetect) {
  566. if (videoFrame && !(videoFrame->GetFlags() & bmdFrameHasNoInputSource) &&
  567. ctx->bmd_mode == bmdModeUnknown)
  568. {
  569. ctx->bmd_mode = AUTODETECT_DEFAULT_MODE;
  570. }
  571. return S_OK;
  572. }
  573. ctx->frameCount++;
  574. if (ctx->audio_pts_source == PTS_SRC_WALLCLOCK || ctx->video_pts_source == PTS_SRC_WALLCLOCK)
  575. wallclock = av_gettime_relative();
  576. if (ctx->audio_pts_source == PTS_SRC_ABS_WALLCLOCK || ctx->video_pts_source == PTS_SRC_ABS_WALLCLOCK)
  577. abs_wallclock = av_gettime();
  578. // Handle Video Frame
  579. if (videoFrame) {
  580. AVPacket pkt;
  581. av_init_packet(&pkt);
  582. if (ctx->frameCount % 25 == 0) {
  583. unsigned long long qsize = avpacket_queue_size(&ctx->queue);
  584. av_log(avctx, AV_LOG_DEBUG,
  585. "Frame received (#%lu) - Valid (%liB) - QSize %fMB\n",
  586. ctx->frameCount,
  587. videoFrame->GetRowBytes() * videoFrame->GetHeight(),
  588. (double)qsize / 1024 / 1024);
  589. }
  590. videoFrame->GetBytes(&frameBytes);
  591. videoFrame->GetStreamTime(&frameTime, &frameDuration,
  592. ctx->video_st->time_base.den);
  593. if (videoFrame->GetFlags() & bmdFrameHasNoInputSource) {
  594. if (ctx->draw_bars && videoFrame->GetPixelFormat() == bmdFormat8BitYUV) {
  595. unsigned bars[8] = {
  596. 0xEA80EA80, 0xD292D210, 0xA910A9A5, 0x90229035,
  597. 0x6ADD6ACA, 0x51EF515A, 0x286D28EF, 0x10801080 };
  598. int width = videoFrame->GetWidth();
  599. int height = videoFrame->GetHeight();
  600. unsigned *p = (unsigned *)frameBytes;
  601. for (int y = 0; y < height; y++) {
  602. for (int x = 0; x < width; x += 2)
  603. *p++ = bars[(x * 8) / width];
  604. }
  605. }
  606. if (!no_video) {
  607. av_log(avctx, AV_LOG_WARNING, "Frame received (#%lu) - No input signal detected "
  608. "- Frames dropped %u\n", ctx->frameCount, ++ctx->dropped);
  609. }
  610. no_video = 1;
  611. } else {
  612. if (no_video) {
  613. av_log(avctx, AV_LOG_WARNING, "Frame received (#%lu) - Input returned "
  614. "- Frames dropped %u\n", ctx->frameCount, ++ctx->dropped);
  615. }
  616. no_video = 0;
  617. }
  618. pkt.pts = get_pkt_pts(videoFrame, audioFrame, wallclock, abs_wallclock, ctx->video_pts_source, ctx->video_st->time_base, &initial_video_pts, cctx->copyts);
  619. pkt.dts = pkt.pts;
  620. pkt.duration = frameDuration;
  621. //To be made sure it still applies
  622. pkt.flags |= AV_PKT_FLAG_KEY;
  623. pkt.stream_index = ctx->video_st->index;
  624. pkt.data = (uint8_t *)frameBytes;
  625. pkt.size = videoFrame->GetRowBytes() *
  626. videoFrame->GetHeight();
  627. //fprintf(stderr,"Video Frame size %d ts %d\n", pkt.size, pkt.pts);
  628. if (!no_video) {
  629. IDeckLinkVideoFrameAncillary *vanc;
  630. AVPacket txt_pkt;
  631. uint8_t txt_buf0[3531]; // 35 * 46 bytes decoded teletext lines + 1 byte data_identifier + 1920 bytes OP47 decode buffer
  632. uint8_t *txt_buf = txt_buf0;
  633. if (videoFrame->GetAncillaryData(&vanc) == S_OK) {
  634. int i;
  635. int64_t line_mask = 1;
  636. BMDPixelFormat vanc_format = vanc->GetPixelFormat();
  637. txt_buf[0] = 0x10; // data_identifier - EBU_data
  638. txt_buf++;
  639. #if CONFIG_LIBZVBI
  640. if (ctx->bmd_mode == bmdModePAL && ctx->teletext_lines &&
  641. (vanc_format == bmdFormat8BitYUV || vanc_format == bmdFormat10BitYUV)) {
  642. av_assert0(videoFrame->GetWidth() == 720);
  643. for (i = 6; i < 336; i++, line_mask <<= 1) {
  644. uint8_t *buf;
  645. if ((ctx->teletext_lines & line_mask) && vanc->GetBufferForVerticalBlankingLine(i, (void**)&buf) == S_OK) {
  646. if (vanc_format == bmdFormat8BitYUV)
  647. txt_buf = teletext_data_unit_from_vbi_data(i, buf, txt_buf, VBI_PIXFMT_UYVY);
  648. else
  649. txt_buf = teletext_data_unit_from_vbi_data_10bit(i, buf, txt_buf);
  650. }
  651. if (i == 22)
  652. i = 317;
  653. }
  654. }
  655. #endif
  656. if (vanc_format == bmdFormat10BitYUV && videoFrame->GetWidth() <= MAX_WIDTH_VANC) {
  657. int idx = get_vanc_line_idx(ctx->bmd_mode);
  658. for (i = vanc_line_numbers[idx].vanc_start; i <= vanc_line_numbers[idx].vanc_end; i++) {
  659. uint8_t *buf;
  660. if (vanc->GetBufferForVerticalBlankingLine(i, (void**)&buf) == S_OK) {
  661. uint16_t luma_vanc[MAX_WIDTH_VANC];
  662. extract_luma_from_v210(luma_vanc, buf, videoFrame->GetWidth());
  663. txt_buf = get_metadata(avctx, luma_vanc, videoFrame->GetWidth(),
  664. txt_buf, sizeof(txt_buf0) - (txt_buf - txt_buf0), &pkt);
  665. }
  666. if (i == vanc_line_numbers[idx].field0_vanc_end)
  667. i = vanc_line_numbers[idx].field1_vanc_start - 1;
  668. }
  669. }
  670. vanc->Release();
  671. if (txt_buf - txt_buf0 > 1) {
  672. int stuffing_units = (4 - ((45 + txt_buf - txt_buf0) / 46) % 4) % 4;
  673. while (stuffing_units--) {
  674. memset(txt_buf, 0xff, 46);
  675. txt_buf[1] = 0x2c; // data_unit_length
  676. txt_buf += 46;
  677. }
  678. av_init_packet(&txt_pkt);
  679. txt_pkt.pts = pkt.pts;
  680. txt_pkt.dts = pkt.dts;
  681. txt_pkt.stream_index = ctx->teletext_st->index;
  682. txt_pkt.data = txt_buf0;
  683. txt_pkt.size = txt_buf - txt_buf0;
  684. if (avpacket_queue_put(&ctx->queue, &txt_pkt) < 0) {
  685. ++ctx->dropped;
  686. }
  687. }
  688. }
  689. }
  690. if (avpacket_queue_put(&ctx->queue, &pkt) < 0) {
  691. ++ctx->dropped;
  692. }
  693. }
  694. // Handle Audio Frame
  695. if (audioFrame) {
  696. AVPacket pkt;
  697. BMDTimeValue audio_pts;
  698. av_init_packet(&pkt);
  699. //hack among hacks
  700. pkt.size = audioFrame->GetSampleFrameCount() * ctx->audio_st->codecpar->channels * (ctx->audio_depth / 8);
  701. audioFrame->GetBytes(&audioFrameBytes);
  702. audioFrame->GetPacketTime(&audio_pts, ctx->audio_st->time_base.den);
  703. pkt.pts = get_pkt_pts(videoFrame, audioFrame, wallclock, abs_wallclock, ctx->audio_pts_source, ctx->audio_st->time_base, &initial_audio_pts, cctx->copyts);
  704. pkt.dts = pkt.pts;
  705. //fprintf(stderr,"Audio Frame size %d ts %d\n", pkt.size, pkt.pts);
  706. pkt.flags |= AV_PKT_FLAG_KEY;
  707. pkt.stream_index = ctx->audio_st->index;
  708. pkt.data = (uint8_t *)audioFrameBytes;
  709. if (avpacket_queue_put(&ctx->queue, &pkt) < 0) {
  710. ++ctx->dropped;
  711. }
  712. }
  713. return S_OK;
  714. }
  715. HRESULT decklink_input_callback::VideoInputFormatChanged(
  716. BMDVideoInputFormatChangedEvents events, IDeckLinkDisplayMode *mode,
  717. BMDDetectedVideoInputFormatFlags)
  718. {
  719. ctx->bmd_mode = mode->GetDisplayMode();
  720. return S_OK;
  721. }
  722. static int decklink_autodetect(struct decklink_cctx *cctx) {
  723. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  724. DECKLINK_BOOL autodetect_supported = false;
  725. int i;
  726. if (ctx->attr->GetFlag(BMDDeckLinkSupportsInputFormatDetection, &autodetect_supported) != S_OK)
  727. return -1;
  728. if (autodetect_supported == false)
  729. return -1;
  730. ctx->autodetect = 1;
  731. ctx->bmd_mode = bmdModeUnknown;
  732. if (ctx->dli->EnableVideoInput(AUTODETECT_DEFAULT_MODE,
  733. bmdFormat8BitYUV,
  734. bmdVideoInputEnableFormatDetection) != S_OK) {
  735. return -1;
  736. }
  737. if (ctx->dli->StartStreams() != S_OK) {
  738. return -1;
  739. }
  740. // 1 second timeout
  741. for (i = 0; i < 10; i++) {
  742. av_usleep(100000);
  743. /* Sometimes VideoInputFrameArrived is called without the
  744. * bmdFrameHasNoInputSource flag before VideoInputFormatChanged.
  745. * So don't break for bmd_mode == AUTODETECT_DEFAULT_MODE. */
  746. if (ctx->bmd_mode != bmdModeUnknown &&
  747. ctx->bmd_mode != AUTODETECT_DEFAULT_MODE)
  748. break;
  749. }
  750. ctx->dli->PauseStreams();
  751. ctx->dli->FlushStreams();
  752. ctx->autodetect = 0;
  753. if (ctx->bmd_mode != bmdModeUnknown) {
  754. cctx->format_code = (char *)av_mallocz(5);
  755. if (!cctx->format_code)
  756. return -1;
  757. AV_WB32(cctx->format_code, ctx->bmd_mode);
  758. return 0;
  759. } else {
  760. return -1;
  761. }
  762. }
  763. extern "C" {
  764. av_cold int ff_decklink_read_close(AVFormatContext *avctx)
  765. {
  766. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  767. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  768. if (ctx->capture_started) {
  769. ctx->dli->StopStreams();
  770. ctx->dli->DisableVideoInput();
  771. ctx->dli->DisableAudioInput();
  772. }
  773. ff_decklink_cleanup(avctx);
  774. avpacket_queue_end(&ctx->queue);
  775. av_freep(&cctx->ctx);
  776. return 0;
  777. }
  778. av_cold int ff_decklink_read_header(AVFormatContext *avctx)
  779. {
  780. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  781. struct decklink_ctx *ctx;
  782. AVStream *st;
  783. HRESULT result;
  784. char fname[1024];
  785. char *tmp;
  786. int mode_num = 0;
  787. int ret;
  788. ctx = (struct decklink_ctx *) av_mallocz(sizeof(struct decklink_ctx));
  789. if (!ctx)
  790. return AVERROR(ENOMEM);
  791. ctx->list_devices = cctx->list_devices;
  792. ctx->list_formats = cctx->list_formats;
  793. ctx->teletext_lines = cctx->teletext_lines;
  794. ctx->preroll = cctx->preroll;
  795. ctx->duplex_mode = cctx->duplex_mode;
  796. if (cctx->video_input > 0 && (unsigned int)cctx->video_input < FF_ARRAY_ELEMS(decklink_video_connection_map))
  797. ctx->video_input = decklink_video_connection_map[cctx->video_input];
  798. if (cctx->audio_input > 0 && (unsigned int)cctx->audio_input < FF_ARRAY_ELEMS(decklink_audio_connection_map))
  799. ctx->audio_input = decklink_audio_connection_map[cctx->audio_input];
  800. ctx->audio_pts_source = cctx->audio_pts_source;
  801. ctx->video_pts_source = cctx->video_pts_source;
  802. ctx->draw_bars = cctx->draw_bars;
  803. ctx->audio_depth = cctx->audio_depth;
  804. cctx->ctx = ctx;
  805. /* Check audio channel option for valid values: 2, 8 or 16 */
  806. switch (cctx->audio_channels) {
  807. case 2:
  808. case 8:
  809. case 16:
  810. break;
  811. default:
  812. av_log(avctx, AV_LOG_ERROR, "Value of channels option must be one of 2, 8 or 16\n");
  813. return AVERROR(EINVAL);
  814. }
  815. /* Check audio bit depth option for valid values: 16 or 32 */
  816. switch (cctx->audio_depth) {
  817. case 16:
  818. case 32:
  819. break;
  820. default:
  821. av_log(avctx, AV_LOG_ERROR, "Value for audio bit depth option must be either 16 or 32\n");
  822. return AVERROR(EINVAL);
  823. }
  824. /* List available devices. */
  825. if (ctx->list_devices) {
  826. ff_decklink_list_devices_legacy(avctx, 1, 0);
  827. return AVERROR_EXIT;
  828. }
  829. if (cctx->v210) {
  830. av_log(avctx, AV_LOG_WARNING, "The bm_v210 option is deprecated and will be removed. Please use the -raw_format yuv422p10.\n");
  831. cctx->raw_format = MKBETAG('v','2','1','0');
  832. }
  833. av_strlcpy(fname, avctx->url, sizeof(fname));
  834. tmp=strchr (fname, '@');
  835. if (tmp != NULL) {
  836. av_log(avctx, AV_LOG_WARNING, "The @mode syntax is deprecated and will be removed. Please use the -format_code option.\n");
  837. mode_num = atoi (tmp+1);
  838. *tmp = 0;
  839. }
  840. ret = ff_decklink_init_device(avctx, fname);
  841. if (ret < 0)
  842. return ret;
  843. /* Get input device. */
  844. if (ctx->dl->QueryInterface(IID_IDeckLinkInput, (void **) &ctx->dli) != S_OK) {
  845. av_log(avctx, AV_LOG_ERROR, "Could not open input device from '%s'\n",
  846. avctx->url);
  847. ret = AVERROR(EIO);
  848. goto error;
  849. }
  850. /* List supported formats. */
  851. if (ctx->list_formats) {
  852. ff_decklink_list_formats(avctx, DIRECTION_IN);
  853. ret = AVERROR_EXIT;
  854. goto error;
  855. }
  856. if (ff_decklink_set_configs(avctx, DIRECTION_IN) < 0) {
  857. av_log(avctx, AV_LOG_ERROR, "Could not set input configuration\n");
  858. ret = AVERROR(EIO);
  859. goto error;
  860. }
  861. ctx->input_callback = new decklink_input_callback(avctx);
  862. ctx->dli->SetCallback(ctx->input_callback);
  863. if (mode_num == 0 && !cctx->format_code) {
  864. if (decklink_autodetect(cctx) < 0) {
  865. av_log(avctx, AV_LOG_ERROR, "Cannot Autodetect input stream or No signal\n");
  866. ret = AVERROR(EIO);
  867. goto error;
  868. }
  869. av_log(avctx, AV_LOG_INFO, "Autodetected the input mode\n");
  870. }
  871. if (ff_decklink_set_format(avctx, DIRECTION_IN, mode_num) < 0) {
  872. av_log(avctx, AV_LOG_ERROR, "Could not set mode number %d or format code %s for %s\n",
  873. mode_num, (cctx->format_code) ? cctx->format_code : "(unset)", fname);
  874. ret = AVERROR(EIO);
  875. goto error;
  876. }
  877. #if !CONFIG_LIBZVBI
  878. if (ctx->teletext_lines && ctx->bmd_mode == bmdModePAL) {
  879. av_log(avctx, AV_LOG_ERROR, "Libzvbi support is needed for capturing SD PAL teletext, please recompile FFmpeg.\n");
  880. ret = AVERROR(ENOSYS);
  881. goto error;
  882. }
  883. #endif
  884. /* Setup streams. */
  885. st = avformat_new_stream(avctx, NULL);
  886. if (!st) {
  887. av_log(avctx, AV_LOG_ERROR, "Cannot add stream\n");
  888. ret = AVERROR(ENOMEM);
  889. goto error;
  890. }
  891. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  892. st->codecpar->codec_id = cctx->audio_depth == 32 ? AV_CODEC_ID_PCM_S32LE : AV_CODEC_ID_PCM_S16LE;
  893. st->codecpar->sample_rate = bmdAudioSampleRate48kHz;
  894. st->codecpar->channels = cctx->audio_channels;
  895. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  896. ctx->audio_st=st;
  897. st = avformat_new_stream(avctx, NULL);
  898. if (!st) {
  899. av_log(avctx, AV_LOG_ERROR, "Cannot add stream\n");
  900. ret = AVERROR(ENOMEM);
  901. goto error;
  902. }
  903. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  904. st->codecpar->width = ctx->bmd_width;
  905. st->codecpar->height = ctx->bmd_height;
  906. st->time_base.den = ctx->bmd_tb_den;
  907. st->time_base.num = ctx->bmd_tb_num;
  908. st->r_frame_rate = av_make_q(st->time_base.den, st->time_base.num);
  909. switch((BMDPixelFormat)cctx->raw_format) {
  910. case bmdFormat8BitYUV:
  911. st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
  912. st->codecpar->codec_tag = MKTAG('U', 'Y', 'V', 'Y');
  913. st->codecpar->format = AV_PIX_FMT_UYVY422;
  914. st->codecpar->bit_rate = av_rescale(ctx->bmd_width * ctx->bmd_height * 16, st->time_base.den, st->time_base.num);
  915. break;
  916. case bmdFormat10BitYUV:
  917. st->codecpar->codec_id = AV_CODEC_ID_V210;
  918. st->codecpar->codec_tag = MKTAG('V','2','1','0');
  919. st->codecpar->bit_rate = av_rescale(ctx->bmd_width * ctx->bmd_height * 64, st->time_base.den, st->time_base.num * 3);
  920. st->codecpar->bits_per_coded_sample = 10;
  921. break;
  922. case bmdFormat8BitARGB:
  923. st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
  924. st->codecpar->codec_tag = avcodec_pix_fmt_to_codec_tag((enum AVPixelFormat)st->codecpar->format);;
  925. st->codecpar->format = AV_PIX_FMT_0RGB;
  926. st->codecpar->bit_rate = av_rescale(ctx->bmd_width * ctx->bmd_height * 32, st->time_base.den, st->time_base.num);
  927. break;
  928. case bmdFormat8BitBGRA:
  929. st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
  930. st->codecpar->codec_tag = avcodec_pix_fmt_to_codec_tag((enum AVPixelFormat)st->codecpar->format);
  931. st->codecpar->format = AV_PIX_FMT_BGR0;
  932. st->codecpar->bit_rate = av_rescale(ctx->bmd_width * ctx->bmd_height * 32, st->time_base.den, st->time_base.num);
  933. break;
  934. case bmdFormat10BitRGB:
  935. st->codecpar->codec_id = AV_CODEC_ID_R210;
  936. st->codecpar->codec_tag = MKTAG('R','2','1','0');
  937. st->codecpar->format = AV_PIX_FMT_RGB48LE;
  938. st->codecpar->bit_rate = av_rescale(ctx->bmd_width * ctx->bmd_height * 30, st->time_base.den, st->time_base.num);
  939. st->codecpar->bits_per_coded_sample = 10;
  940. break;
  941. default:
  942. av_log(avctx, AV_LOG_ERROR, "Raw Format %.4s not supported\n", (char*) &cctx->raw_format);
  943. ret = AVERROR(EINVAL);
  944. goto error;
  945. }
  946. switch (ctx->bmd_field_dominance) {
  947. case bmdUpperFieldFirst:
  948. st->codecpar->field_order = AV_FIELD_TT;
  949. break;
  950. case bmdLowerFieldFirst:
  951. st->codecpar->field_order = AV_FIELD_BB;
  952. break;
  953. case bmdProgressiveFrame:
  954. case bmdProgressiveSegmentedFrame:
  955. st->codecpar->field_order = AV_FIELD_PROGRESSIVE;
  956. break;
  957. }
  958. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  959. ctx->video_st=st;
  960. if (ctx->teletext_lines) {
  961. st = avformat_new_stream(avctx, NULL);
  962. if (!st) {
  963. av_log(avctx, AV_LOG_ERROR, "Cannot add stream\n");
  964. ret = AVERROR(ENOMEM);
  965. goto error;
  966. }
  967. st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
  968. st->time_base.den = ctx->bmd_tb_den;
  969. st->time_base.num = ctx->bmd_tb_num;
  970. st->codecpar->codec_id = AV_CODEC_ID_DVB_TELETEXT;
  971. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  972. ctx->teletext_st = st;
  973. }
  974. av_log(avctx, AV_LOG_VERBOSE, "Using %d input audio channels\n", ctx->audio_st->codecpar->channels);
  975. result = ctx->dli->EnableAudioInput(bmdAudioSampleRate48kHz, cctx->audio_depth == 32 ? bmdAudioSampleType32bitInteger : bmdAudioSampleType16bitInteger, ctx->audio_st->codecpar->channels);
  976. if (result != S_OK) {
  977. av_log(avctx, AV_LOG_ERROR, "Cannot enable audio input\n");
  978. ret = AVERROR(EIO);
  979. goto error;
  980. }
  981. result = ctx->dli->EnableVideoInput(ctx->bmd_mode,
  982. (BMDPixelFormat) cctx->raw_format,
  983. bmdVideoInputFlagDefault);
  984. if (result != S_OK) {
  985. av_log(avctx, AV_LOG_ERROR, "Cannot enable video input\n");
  986. ret = AVERROR(EIO);
  987. goto error;
  988. }
  989. avpacket_queue_init (avctx, &ctx->queue);
  990. if (ctx->dli->StartStreams() != S_OK) {
  991. av_log(avctx, AV_LOG_ERROR, "Cannot start input stream\n");
  992. ret = AVERROR(EIO);
  993. goto error;
  994. }
  995. return 0;
  996. error:
  997. ff_decklink_cleanup(avctx);
  998. return ret;
  999. }
  1000. int ff_decklink_read_packet(AVFormatContext *avctx, AVPacket *pkt)
  1001. {
  1002. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  1003. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  1004. avpacket_queue_get(&ctx->queue, pkt, 1);
  1005. return 0;
  1006. }
  1007. int ff_decklink_list_input_devices(AVFormatContext *avctx, struct AVDeviceInfoList *device_list)
  1008. {
  1009. return ff_decklink_list_devices(avctx, device_list, 1, 0);
  1010. }
  1011. } /* extern "C" */