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.

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