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.

811 lines
27KB

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