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.

1191 lines
40KB

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