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.

1188 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_log(q->avctx, AV_LOG_WARNING, "Decklink input buffer overrun!\n");
  417. return -1;
  418. }
  419. /* ensure the packet is reference counted */
  420. if (av_packet_make_refcounted(pkt) < 0) {
  421. return -1;
  422. }
  423. pkt1 = (AVPacketList *)av_malloc(sizeof(AVPacketList));
  424. if (!pkt1) {
  425. return -1;
  426. }
  427. av_packet_move_ref(&pkt1->pkt, pkt);
  428. pkt1->next = NULL;
  429. pthread_mutex_lock(&q->mutex);
  430. if (!q->last_pkt) {
  431. q->first_pkt = pkt1;
  432. } else {
  433. q->last_pkt->next = pkt1;
  434. }
  435. q->last_pkt = pkt1;
  436. q->nb_packets++;
  437. q->size += pkt1->pkt.size + sizeof(*pkt1);
  438. pthread_cond_signal(&q->cond);
  439. pthread_mutex_unlock(&q->mutex);
  440. return 0;
  441. }
  442. static int avpacket_queue_get(AVPacketQueue *q, AVPacket *pkt, int block)
  443. {
  444. AVPacketList *pkt1;
  445. int ret;
  446. pthread_mutex_lock(&q->mutex);
  447. for (;; ) {
  448. pkt1 = q->first_pkt;
  449. if (pkt1) {
  450. q->first_pkt = pkt1->next;
  451. if (!q->first_pkt) {
  452. q->last_pkt = NULL;
  453. }
  454. q->nb_packets--;
  455. q->size -= pkt1->pkt.size + sizeof(*pkt1);
  456. *pkt = pkt1->pkt;
  457. av_free(pkt1);
  458. ret = 1;
  459. break;
  460. } else if (!block) {
  461. ret = 0;
  462. break;
  463. } else {
  464. pthread_cond_wait(&q->cond, &q->mutex);
  465. }
  466. }
  467. pthread_mutex_unlock(&q->mutex);
  468. return ret;
  469. }
  470. class decklink_input_callback : public IDeckLinkInputCallback
  471. {
  472. public:
  473. decklink_input_callback(AVFormatContext *_avctx);
  474. ~decklink_input_callback();
  475. virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; }
  476. virtual ULONG STDMETHODCALLTYPE AddRef(void);
  477. virtual ULONG STDMETHODCALLTYPE Release(void);
  478. virtual HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(BMDVideoInputFormatChangedEvents, IDeckLinkDisplayMode*, BMDDetectedVideoInputFormatFlags);
  479. virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(IDeckLinkVideoInputFrame*, IDeckLinkAudioInputPacket*);
  480. private:
  481. ULONG m_refCount;
  482. pthread_mutex_t m_mutex;
  483. AVFormatContext *avctx;
  484. decklink_ctx *ctx;
  485. int no_video;
  486. int64_t initial_video_pts;
  487. int64_t initial_audio_pts;
  488. };
  489. decklink_input_callback::decklink_input_callback(AVFormatContext *_avctx) : m_refCount(0)
  490. {
  491. avctx = _avctx;
  492. decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  493. ctx = (struct decklink_ctx *)cctx->ctx;
  494. no_video = 0;
  495. initial_audio_pts = initial_video_pts = AV_NOPTS_VALUE;
  496. pthread_mutex_init(&m_mutex, NULL);
  497. }
  498. decklink_input_callback::~decklink_input_callback()
  499. {
  500. pthread_mutex_destroy(&m_mutex);
  501. }
  502. ULONG decklink_input_callback::AddRef(void)
  503. {
  504. pthread_mutex_lock(&m_mutex);
  505. m_refCount++;
  506. pthread_mutex_unlock(&m_mutex);
  507. return (ULONG)m_refCount;
  508. }
  509. ULONG decklink_input_callback::Release(void)
  510. {
  511. pthread_mutex_lock(&m_mutex);
  512. m_refCount--;
  513. pthread_mutex_unlock(&m_mutex);
  514. if (m_refCount == 0) {
  515. delete this;
  516. return 0;
  517. }
  518. return (ULONG)m_refCount;
  519. }
  520. static int64_t get_pkt_pts(IDeckLinkVideoInputFrame *videoFrame,
  521. IDeckLinkAudioInputPacket *audioFrame,
  522. int64_t wallclock,
  523. int64_t abs_wallclock,
  524. DecklinkPtsSource pts_src,
  525. AVRational time_base, int64_t *initial_pts,
  526. int copyts)
  527. {
  528. int64_t pts = AV_NOPTS_VALUE;
  529. BMDTimeValue bmd_pts;
  530. BMDTimeValue bmd_duration;
  531. HRESULT res = E_INVALIDARG;
  532. switch (pts_src) {
  533. case PTS_SRC_AUDIO:
  534. if (audioFrame)
  535. res = audioFrame->GetPacketTime(&bmd_pts, time_base.den);
  536. break;
  537. case PTS_SRC_VIDEO:
  538. if (videoFrame)
  539. res = videoFrame->GetStreamTime(&bmd_pts, &bmd_duration, time_base.den);
  540. break;
  541. case PTS_SRC_REFERENCE:
  542. if (videoFrame)
  543. res = videoFrame->GetHardwareReferenceTimestamp(time_base.den, &bmd_pts, &bmd_duration);
  544. break;
  545. case PTS_SRC_WALLCLOCK:
  546. /* fall through */
  547. case PTS_SRC_ABS_WALLCLOCK:
  548. {
  549. /* MSVC does not support compound literals like AV_TIME_BASE_Q
  550. * in C++ code (compiler error C4576) */
  551. AVRational timebase;
  552. timebase.num = 1;
  553. timebase.den = AV_TIME_BASE;
  554. if (pts_src == PTS_SRC_WALLCLOCK)
  555. pts = av_rescale_q(wallclock, timebase, time_base);
  556. else
  557. pts = av_rescale_q(abs_wallclock, timebase, time_base);
  558. break;
  559. }
  560. }
  561. if (res == S_OK)
  562. pts = bmd_pts / time_base.num;
  563. if (!copyts) {
  564. if (pts != AV_NOPTS_VALUE && *initial_pts == AV_NOPTS_VALUE)
  565. *initial_pts = pts;
  566. if (*initial_pts != AV_NOPTS_VALUE)
  567. pts -= *initial_pts;
  568. }
  569. return pts;
  570. }
  571. HRESULT decklink_input_callback::VideoInputFrameArrived(
  572. IDeckLinkVideoInputFrame *videoFrame, IDeckLinkAudioInputPacket *audioFrame)
  573. {
  574. void *frameBytes;
  575. void *audioFrameBytes;
  576. BMDTimeValue frameTime;
  577. BMDTimeValue frameDuration;
  578. int64_t wallclock = 0, abs_wallclock = 0;
  579. struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
  580. if (ctx->autodetect) {
  581. if (videoFrame && !(videoFrame->GetFlags() & bmdFrameHasNoInputSource) &&
  582. ctx->bmd_mode == bmdModeUnknown)
  583. {
  584. ctx->bmd_mode = AUTODETECT_DEFAULT_MODE;
  585. }
  586. return S_OK;
  587. }
  588. ctx->frameCount++;
  589. if (ctx->audio_pts_source == PTS_SRC_WALLCLOCK || ctx->video_pts_source == PTS_SRC_WALLCLOCK)
  590. wallclock = av_gettime_relative();
  591. if (ctx->audio_pts_source == PTS_SRC_ABS_WALLCLOCK || ctx->video_pts_source == PTS_SRC_ABS_WALLCLOCK)
  592. abs_wallclock = av_gettime();
  593. // Handle Video Frame
  594. if (videoFrame) {
  595. AVPacket pkt;
  596. av_init_packet(&pkt);
  597. if (ctx->frameCount % 25 == 0) {
  598. unsigned long long qsize = avpacket_queue_size(&ctx->queue);
  599. av_log(avctx, AV_LOG_DEBUG,
  600. "Frame received (#%lu) - Valid (%liB) - QSize %fMB\n",
  601. ctx->frameCount,
  602. videoFrame->GetRowBytes() * videoFrame->GetHeight(),
  603. (double)qsize / 1024 / 1024);
  604. }
  605. videoFrame->GetBytes(&frameBytes);
  606. videoFrame->GetStreamTime(&frameTime, &frameDuration,
  607. ctx->video_st->time_base.den);
  608. if (videoFrame->GetFlags() & bmdFrameHasNoInputSource) {
  609. if (ctx->draw_bars && videoFrame->GetPixelFormat() == bmdFormat8BitYUV) {
  610. unsigned bars[8] = {
  611. 0xEA80EA80, 0xD292D210, 0xA910A9A5, 0x90229035,
  612. 0x6ADD6ACA, 0x51EF515A, 0x286D28EF, 0x10801080 };
  613. int width = videoFrame->GetWidth();
  614. int height = videoFrame->GetHeight();
  615. unsigned *p = (unsigned *)frameBytes;
  616. for (int y = 0; y < height; y++) {
  617. for (int x = 0; x < width; x += 2)
  618. *p++ = bars[(x * 8) / width];
  619. }
  620. }
  621. if (!no_video) {
  622. av_log(avctx, AV_LOG_WARNING, "Frame received (#%lu) - No input signal detected "
  623. "- Frames dropped %u\n", ctx->frameCount, ++ctx->dropped);
  624. }
  625. no_video = 1;
  626. } else {
  627. if (no_video) {
  628. av_log(avctx, AV_LOG_WARNING, "Frame received (#%lu) - Input returned "
  629. "- Frames dropped %u\n", ctx->frameCount, ++ctx->dropped);
  630. }
  631. no_video = 0;
  632. }
  633. pkt.pts = get_pkt_pts(videoFrame, audioFrame, wallclock, abs_wallclock, ctx->video_pts_source, ctx->video_st->time_base, &initial_video_pts, cctx->copyts);
  634. pkt.dts = pkt.pts;
  635. pkt.duration = frameDuration;
  636. //To be made sure it still applies
  637. pkt.flags |= AV_PKT_FLAG_KEY;
  638. pkt.stream_index = ctx->video_st->index;
  639. pkt.data = (uint8_t *)frameBytes;
  640. pkt.size = videoFrame->GetRowBytes() *
  641. videoFrame->GetHeight();
  642. //fprintf(stderr,"Video Frame size %d ts %d\n", pkt.size, pkt.pts);
  643. if (!no_video) {
  644. IDeckLinkVideoFrameAncillary *vanc;
  645. AVPacket txt_pkt;
  646. uint8_t txt_buf0[3531]; // 35 * 46 bytes decoded teletext lines + 1 byte data_identifier + 1920 bytes OP47 decode buffer
  647. uint8_t *txt_buf = txt_buf0;
  648. if (videoFrame->GetAncillaryData(&vanc) == S_OK) {
  649. int i;
  650. int64_t line_mask = 1;
  651. BMDPixelFormat vanc_format = vanc->GetPixelFormat();
  652. txt_buf[0] = 0x10; // data_identifier - EBU_data
  653. txt_buf++;
  654. #if CONFIG_LIBZVBI
  655. if (ctx->bmd_mode == bmdModePAL && ctx->teletext_lines &&
  656. (vanc_format == bmdFormat8BitYUV || vanc_format == bmdFormat10BitYUV)) {
  657. av_assert0(videoFrame->GetWidth() == 720);
  658. for (i = 6; i < 336; i++, line_mask <<= 1) {
  659. uint8_t *buf;
  660. if ((ctx->teletext_lines & line_mask) && vanc->GetBufferForVerticalBlankingLine(i, (void**)&buf) == S_OK) {
  661. if (vanc_format == bmdFormat8BitYUV)
  662. txt_buf = teletext_data_unit_from_vbi_data(i, buf, txt_buf, VBI_PIXFMT_UYVY);
  663. else
  664. txt_buf = teletext_data_unit_from_vbi_data_10bit(i, buf, txt_buf);
  665. }
  666. if (i == 22)
  667. i = 317;
  668. }
  669. }
  670. #endif
  671. if (vanc_format == bmdFormat10BitYUV && videoFrame->GetWidth() <= MAX_WIDTH_VANC) {
  672. int idx = get_vanc_line_idx(ctx->bmd_mode);
  673. for (i = vanc_line_numbers[idx].vanc_start; i <= vanc_line_numbers[idx].vanc_end; i++) {
  674. uint8_t *buf;
  675. if (vanc->GetBufferForVerticalBlankingLine(i, (void**)&buf) == S_OK) {
  676. uint16_t vanc[MAX_WIDTH_VANC];
  677. size_t vanc_size = videoFrame->GetWidth();
  678. if (ctx->bmd_mode == bmdModeNTSC && videoFrame->GetWidth() * 2 <= MAX_WIDTH_VANC) {
  679. vanc_size = vanc_size * 2;
  680. unpack_v210(vanc, buf, videoFrame->GetWidth());
  681. } else {
  682. extract_luma_from_v210(vanc, buf, videoFrame->GetWidth());
  683. }
  684. txt_buf = get_metadata(avctx, vanc, vanc_size,
  685. txt_buf, sizeof(txt_buf0) - (txt_buf - txt_buf0), &pkt);
  686. }
  687. if (i == vanc_line_numbers[idx].field0_vanc_end)
  688. i = vanc_line_numbers[idx].field1_vanc_start - 1;
  689. }
  690. }
  691. vanc->Release();
  692. if (txt_buf - txt_buf0 > 1) {
  693. int stuffing_units = (4 - ((45 + txt_buf - txt_buf0) / 46) % 4) % 4;
  694. while (stuffing_units--) {
  695. memset(txt_buf, 0xff, 46);
  696. txt_buf[1] = 0x2c; // data_unit_length
  697. txt_buf += 46;
  698. }
  699. av_init_packet(&txt_pkt);
  700. txt_pkt.pts = pkt.pts;
  701. txt_pkt.dts = pkt.dts;
  702. txt_pkt.stream_index = ctx->teletext_st->index;
  703. txt_pkt.data = txt_buf0;
  704. txt_pkt.size = txt_buf - txt_buf0;
  705. if (avpacket_queue_put(&ctx->queue, &txt_pkt) < 0) {
  706. ++ctx->dropped;
  707. }
  708. }
  709. }
  710. }
  711. pkt.buf = av_buffer_create(pkt.data, pkt.size, decklink_object_free, videoFrame, 0);
  712. if (pkt.buf)
  713. videoFrame->AddRef();
  714. if (avpacket_queue_put(&ctx->queue, &pkt) < 0) {
  715. ++ctx->dropped;
  716. }
  717. }
  718. // Handle Audio Frame
  719. if (audioFrame) {
  720. AVPacket pkt;
  721. BMDTimeValue audio_pts;
  722. av_init_packet(&pkt);
  723. //hack among hacks
  724. pkt.size = audioFrame->GetSampleFrameCount() * ctx->audio_st->codecpar->channels * (ctx->audio_depth / 8);
  725. audioFrame->GetBytes(&audioFrameBytes);
  726. audioFrame->GetPacketTime(&audio_pts, ctx->audio_st->time_base.den);
  727. pkt.pts = get_pkt_pts(videoFrame, audioFrame, wallclock, abs_wallclock, ctx->audio_pts_source, ctx->audio_st->time_base, &initial_audio_pts, cctx->copyts);
  728. pkt.dts = pkt.pts;
  729. //fprintf(stderr,"Audio Frame size %d ts %d\n", pkt.size, pkt.pts);
  730. pkt.flags |= AV_PKT_FLAG_KEY;
  731. pkt.stream_index = ctx->audio_st->index;
  732. pkt.data = (uint8_t *)audioFrameBytes;
  733. if (avpacket_queue_put(&ctx->queue, &pkt) < 0) {
  734. ++ctx->dropped;
  735. }
  736. }
  737. return S_OK;
  738. }
  739. HRESULT decklink_input_callback::VideoInputFormatChanged(
  740. BMDVideoInputFormatChangedEvents events, IDeckLinkDisplayMode *mode,
  741. BMDDetectedVideoInputFormatFlags)
  742. {
  743. ctx->bmd_mode = mode->GetDisplayMode();
  744. return S_OK;
  745. }
  746. static int decklink_autodetect(struct decklink_cctx *cctx) {
  747. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  748. DECKLINK_BOOL autodetect_supported = false;
  749. int i;
  750. if (ctx->attr->GetFlag(BMDDeckLinkSupportsInputFormatDetection, &autodetect_supported) != S_OK)
  751. return -1;
  752. if (autodetect_supported == false)
  753. return -1;
  754. ctx->autodetect = 1;
  755. ctx->bmd_mode = bmdModeUnknown;
  756. if (ctx->dli->EnableVideoInput(AUTODETECT_DEFAULT_MODE,
  757. bmdFormat8BitYUV,
  758. bmdVideoInputEnableFormatDetection) != S_OK) {
  759. return -1;
  760. }
  761. if (ctx->dli->StartStreams() != S_OK) {
  762. return -1;
  763. }
  764. // 1 second timeout
  765. for (i = 0; i < 10; i++) {
  766. av_usleep(100000);
  767. /* Sometimes VideoInputFrameArrived is called without the
  768. * bmdFrameHasNoInputSource flag before VideoInputFormatChanged.
  769. * So don't break for bmd_mode == AUTODETECT_DEFAULT_MODE. */
  770. if (ctx->bmd_mode != bmdModeUnknown &&
  771. ctx->bmd_mode != AUTODETECT_DEFAULT_MODE)
  772. break;
  773. }
  774. ctx->dli->PauseStreams();
  775. ctx->dli->FlushStreams();
  776. ctx->autodetect = 0;
  777. if (ctx->bmd_mode != bmdModeUnknown) {
  778. cctx->format_code = (char *)av_mallocz(5);
  779. if (!cctx->format_code)
  780. return -1;
  781. AV_WB32(cctx->format_code, ctx->bmd_mode);
  782. return 0;
  783. } else {
  784. return -1;
  785. }
  786. }
  787. extern "C" {
  788. av_cold int ff_decklink_read_close(AVFormatContext *avctx)
  789. {
  790. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  791. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  792. if (ctx->capture_started) {
  793. ctx->dli->StopStreams();
  794. ctx->dli->DisableVideoInput();
  795. ctx->dli->DisableAudioInput();
  796. }
  797. ff_decklink_cleanup(avctx);
  798. avpacket_queue_end(&ctx->queue);
  799. av_freep(&cctx->ctx);
  800. return 0;
  801. }
  802. av_cold int ff_decklink_read_header(AVFormatContext *avctx)
  803. {
  804. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  805. struct decklink_ctx *ctx;
  806. AVStream *st;
  807. HRESULT result;
  808. char fname[1024];
  809. char *tmp;
  810. int mode_num = 0;
  811. int ret;
  812. ctx = (struct decklink_ctx *) av_mallocz(sizeof(struct decklink_ctx));
  813. if (!ctx)
  814. return AVERROR(ENOMEM);
  815. ctx->list_devices = cctx->list_devices;
  816. ctx->list_formats = cctx->list_formats;
  817. ctx->teletext_lines = cctx->teletext_lines;
  818. ctx->preroll = cctx->preroll;
  819. ctx->duplex_mode = cctx->duplex_mode;
  820. if (cctx->video_input > 0 && (unsigned int)cctx->video_input < FF_ARRAY_ELEMS(decklink_video_connection_map))
  821. ctx->video_input = decklink_video_connection_map[cctx->video_input];
  822. if (cctx->audio_input > 0 && (unsigned int)cctx->audio_input < FF_ARRAY_ELEMS(decklink_audio_connection_map))
  823. ctx->audio_input = decklink_audio_connection_map[cctx->audio_input];
  824. ctx->audio_pts_source = cctx->audio_pts_source;
  825. ctx->video_pts_source = cctx->video_pts_source;
  826. ctx->draw_bars = cctx->draw_bars;
  827. ctx->audio_depth = cctx->audio_depth;
  828. cctx->ctx = ctx;
  829. /* Check audio channel option for valid values: 2, 8 or 16 */
  830. switch (cctx->audio_channels) {
  831. case 2:
  832. case 8:
  833. case 16:
  834. break;
  835. default:
  836. av_log(avctx, AV_LOG_ERROR, "Value of channels option must be one of 2, 8 or 16\n");
  837. return AVERROR(EINVAL);
  838. }
  839. /* Check audio bit depth option for valid values: 16 or 32 */
  840. switch (cctx->audio_depth) {
  841. case 16:
  842. case 32:
  843. break;
  844. default:
  845. av_log(avctx, AV_LOG_ERROR, "Value for audio bit depth option must be either 16 or 32\n");
  846. return AVERROR(EINVAL);
  847. }
  848. /* List available devices. */
  849. if (ctx->list_devices) {
  850. ff_decklink_list_devices_legacy(avctx, 1, 0);
  851. return AVERROR_EXIT;
  852. }
  853. if (cctx->v210) {
  854. av_log(avctx, AV_LOG_WARNING, "The bm_v210 option is deprecated and will be removed. Please use the -raw_format yuv422p10.\n");
  855. cctx->raw_format = MKBETAG('v','2','1','0');
  856. }
  857. av_strlcpy(fname, avctx->url, sizeof(fname));
  858. tmp=strchr (fname, '@');
  859. if (tmp != NULL) {
  860. av_log(avctx, AV_LOG_WARNING, "The @mode syntax is deprecated and will be removed. Please use the -format_code option.\n");
  861. mode_num = atoi (tmp+1);
  862. *tmp = 0;
  863. }
  864. ret = ff_decklink_init_device(avctx, fname);
  865. if (ret < 0)
  866. return ret;
  867. /* Get input device. */
  868. if (ctx->dl->QueryInterface(IID_IDeckLinkInput, (void **) &ctx->dli) != S_OK) {
  869. av_log(avctx, AV_LOG_ERROR, "Could not open input device from '%s'\n",
  870. avctx->url);
  871. ret = AVERROR(EIO);
  872. goto error;
  873. }
  874. /* List supported formats. */
  875. if (ctx->list_formats) {
  876. ff_decklink_list_formats(avctx, DIRECTION_IN);
  877. ret = AVERROR_EXIT;
  878. goto error;
  879. }
  880. if (ff_decklink_set_configs(avctx, DIRECTION_IN) < 0) {
  881. av_log(avctx, AV_LOG_ERROR, "Could not set input configuration\n");
  882. ret = AVERROR(EIO);
  883. goto error;
  884. }
  885. ctx->input_callback = new decklink_input_callback(avctx);
  886. ctx->dli->SetCallback(ctx->input_callback);
  887. if (mode_num == 0 && !cctx->format_code) {
  888. if (decklink_autodetect(cctx) < 0) {
  889. av_log(avctx, AV_LOG_ERROR, "Cannot Autodetect input stream or No signal\n");
  890. ret = AVERROR(EIO);
  891. goto error;
  892. }
  893. av_log(avctx, AV_LOG_INFO, "Autodetected the input mode\n");
  894. }
  895. if (ff_decklink_set_format(avctx, DIRECTION_IN, mode_num) < 0) {
  896. av_log(avctx, AV_LOG_ERROR, "Could not set mode number %d or format code %s for %s\n",
  897. mode_num, (cctx->format_code) ? cctx->format_code : "(unset)", fname);
  898. ret = AVERROR(EIO);
  899. goto error;
  900. }
  901. #if !CONFIG_LIBZVBI
  902. if (ctx->teletext_lines && ctx->bmd_mode == bmdModePAL) {
  903. av_log(avctx, AV_LOG_ERROR, "Libzvbi support is needed for capturing SD PAL teletext, please recompile FFmpeg.\n");
  904. ret = AVERROR(ENOSYS);
  905. goto error;
  906. }
  907. #endif
  908. /* Setup streams. */
  909. st = avformat_new_stream(avctx, NULL);
  910. if (!st) {
  911. av_log(avctx, AV_LOG_ERROR, "Cannot add stream\n");
  912. ret = AVERROR(ENOMEM);
  913. goto error;
  914. }
  915. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  916. st->codecpar->codec_id = cctx->audio_depth == 32 ? AV_CODEC_ID_PCM_S32LE : AV_CODEC_ID_PCM_S16LE;
  917. st->codecpar->sample_rate = bmdAudioSampleRate48kHz;
  918. st->codecpar->channels = cctx->audio_channels;
  919. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  920. ctx->audio_st=st;
  921. st = avformat_new_stream(avctx, NULL);
  922. if (!st) {
  923. av_log(avctx, AV_LOG_ERROR, "Cannot add stream\n");
  924. ret = AVERROR(ENOMEM);
  925. goto error;
  926. }
  927. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  928. st->codecpar->width = ctx->bmd_width;
  929. st->codecpar->height = ctx->bmd_height;
  930. st->time_base.den = ctx->bmd_tb_den;
  931. st->time_base.num = ctx->bmd_tb_num;
  932. st->r_frame_rate = av_make_q(st->time_base.den, st->time_base.num);
  933. switch((BMDPixelFormat)cctx->raw_format) {
  934. case bmdFormat8BitYUV:
  935. st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
  936. st->codecpar->codec_tag = MKTAG('U', 'Y', 'V', 'Y');
  937. st->codecpar->format = AV_PIX_FMT_UYVY422;
  938. st->codecpar->bit_rate = av_rescale(ctx->bmd_width * ctx->bmd_height * 16, st->time_base.den, st->time_base.num);
  939. break;
  940. case bmdFormat10BitYUV:
  941. st->codecpar->codec_id = AV_CODEC_ID_V210;
  942. st->codecpar->codec_tag = MKTAG('V','2','1','0');
  943. st->codecpar->bit_rate = av_rescale(ctx->bmd_width * ctx->bmd_height * 64, st->time_base.den, st->time_base.num * 3);
  944. st->codecpar->bits_per_coded_sample = 10;
  945. break;
  946. case bmdFormat8BitARGB:
  947. st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
  948. st->codecpar->codec_tag = avcodec_pix_fmt_to_codec_tag((enum AVPixelFormat)st->codecpar->format);
  949. st->codecpar->format = AV_PIX_FMT_0RGB;
  950. st->codecpar->bit_rate = av_rescale(ctx->bmd_width * ctx->bmd_height * 32, st->time_base.den, st->time_base.num);
  951. break;
  952. case bmdFormat8BitBGRA:
  953. st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
  954. st->codecpar->codec_tag = avcodec_pix_fmt_to_codec_tag((enum AVPixelFormat)st->codecpar->format);
  955. st->codecpar->format = AV_PIX_FMT_BGR0;
  956. st->codecpar->bit_rate = av_rescale(ctx->bmd_width * ctx->bmd_height * 32, st->time_base.den, st->time_base.num);
  957. break;
  958. case bmdFormat10BitRGB:
  959. st->codecpar->codec_id = AV_CODEC_ID_R210;
  960. st->codecpar->codec_tag = MKTAG('R','2','1','0');
  961. st->codecpar->format = AV_PIX_FMT_RGB48LE;
  962. st->codecpar->bit_rate = av_rescale(ctx->bmd_width * ctx->bmd_height * 30, st->time_base.den, st->time_base.num);
  963. st->codecpar->bits_per_coded_sample = 10;
  964. break;
  965. default:
  966. av_log(avctx, AV_LOG_ERROR, "Raw Format %.4s not supported\n", (char*) &cctx->raw_format);
  967. ret = AVERROR(EINVAL);
  968. goto error;
  969. }
  970. switch (ctx->bmd_field_dominance) {
  971. case bmdUpperFieldFirst:
  972. st->codecpar->field_order = AV_FIELD_TT;
  973. break;
  974. case bmdLowerFieldFirst:
  975. st->codecpar->field_order = AV_FIELD_BB;
  976. break;
  977. case bmdProgressiveFrame:
  978. case bmdProgressiveSegmentedFrame:
  979. st->codecpar->field_order = AV_FIELD_PROGRESSIVE;
  980. break;
  981. }
  982. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  983. ctx->video_st=st;
  984. if (ctx->teletext_lines) {
  985. st = avformat_new_stream(avctx, NULL);
  986. if (!st) {
  987. av_log(avctx, AV_LOG_ERROR, "Cannot add stream\n");
  988. ret = AVERROR(ENOMEM);
  989. goto error;
  990. }
  991. st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
  992. st->time_base.den = ctx->bmd_tb_den;
  993. st->time_base.num = ctx->bmd_tb_num;
  994. st->codecpar->codec_id = AV_CODEC_ID_DVB_TELETEXT;
  995. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  996. ctx->teletext_st = st;
  997. }
  998. av_log(avctx, AV_LOG_VERBOSE, "Using %d input audio channels\n", ctx->audio_st->codecpar->channels);
  999. result = ctx->dli->EnableAudioInput(bmdAudioSampleRate48kHz, cctx->audio_depth == 32 ? bmdAudioSampleType32bitInteger : bmdAudioSampleType16bitInteger, ctx->audio_st->codecpar->channels);
  1000. if (result != S_OK) {
  1001. av_log(avctx, AV_LOG_ERROR, "Cannot enable audio input\n");
  1002. ret = AVERROR(EIO);
  1003. goto error;
  1004. }
  1005. result = ctx->dli->EnableVideoInput(ctx->bmd_mode,
  1006. (BMDPixelFormat) cctx->raw_format,
  1007. bmdVideoInputFlagDefault);
  1008. if (result != S_OK) {
  1009. av_log(avctx, AV_LOG_ERROR, "Cannot enable video input\n");
  1010. ret = AVERROR(EIO);
  1011. goto error;
  1012. }
  1013. avpacket_queue_init (avctx, &ctx->queue);
  1014. if (ctx->dli->StartStreams() != S_OK) {
  1015. av_log(avctx, AV_LOG_ERROR, "Cannot start input stream\n");
  1016. ret = AVERROR(EIO);
  1017. goto error;
  1018. }
  1019. return 0;
  1020. error:
  1021. ff_decklink_cleanup(avctx);
  1022. return ret;
  1023. }
  1024. int ff_decklink_read_packet(AVFormatContext *avctx, AVPacket *pkt)
  1025. {
  1026. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  1027. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  1028. avpacket_queue_get(&ctx->queue, pkt, 1);
  1029. return 0;
  1030. }
  1031. int ff_decklink_list_input_devices(AVFormatContext *avctx, struct AVDeviceInfoList *device_list)
  1032. {
  1033. return ff_decklink_list_devices(avctx, device_list, 1, 0);
  1034. }
  1035. } /* extern "C" */