decklink_common.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Blackmagic DeckLink common code
  3. * Copyright (c) 2013-2014 Ramiro Polla, Luca Barbato, Deti Fliegl
  4. * Copyright (c) 2017 Akamai Technologies, Inc.
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #ifndef AVDEVICE_DECKLINK_COMMON_H
  23. #define AVDEVICE_DECKLINK_COMMON_H
  24. #include <DeckLinkAPIVersion.h>
  25. #if BLACKMAGIC_DECKLINK_API_VERSION < 0x0b000000
  26. #define IID_IDeckLinkProfileAttributes IID_IDeckLinkAttributes
  27. #define IDeckLinkProfileAttributes IDeckLinkAttributes
  28. #endif
  29. extern "C" {
  30. #include "libavutil/mem.h"
  31. #include "libavcodec/packet_internal.h"
  32. #include "libavfilter/ccfifo.h"
  33. }
  34. #include "libavutil/thread.h"
  35. #include "decklink_common_c.h"
  36. #if CONFIG_LIBKLVANC
  37. #include "libklvanc/vanc.h"
  38. #endif
  39. #ifdef _WIN32
  40. #define DECKLINK_BOOL BOOL
  41. #else
  42. #define DECKLINK_BOOL bool
  43. #endif
  44. #ifdef _WIN32
  45. static char *dup_wchar_to_utf8(wchar_t *w)
  46. {
  47. char *s = NULL;
  48. int l = WideCharToMultiByte(CP_UTF8, 0, w, -1, 0, 0, 0, 0);
  49. s = (char *) av_malloc(l);
  50. if (s)
  51. WideCharToMultiByte(CP_UTF8, 0, w, -1, s, l, 0, 0);
  52. return s;
  53. }
  54. #define DECKLINK_STR OLECHAR *
  55. #define DECKLINK_STRDUP dup_wchar_to_utf8
  56. #define DECKLINK_FREE(s) SysFreeString(s)
  57. #elif defined(__APPLE__)
  58. static char *dup_cfstring_to_utf8(CFStringRef w)
  59. {
  60. char s[256];
  61. CFStringGetCString(w, s, 255, kCFStringEncodingUTF8);
  62. return av_strdup(s);
  63. }
  64. #define DECKLINK_STR const __CFString *
  65. #define DECKLINK_STRDUP dup_cfstring_to_utf8
  66. #define DECKLINK_FREE(s) CFRelease(s)
  67. #else
  68. #define DECKLINK_STR const char *
  69. #define DECKLINK_STRDUP av_strdup
  70. /* free() is needed for a string returned by the DeckLink SDL. */
  71. #define DECKLINK_FREE(s) free((void *) s)
  72. #endif
  73. class decklink_output_callback;
  74. class decklink_input_callback;
  75. typedef struct DecklinkPacketQueue {
  76. PacketList pkt_list;
  77. int nb_packets;
  78. unsigned long long size;
  79. int abort_request;
  80. pthread_mutex_t mutex;
  81. pthread_cond_t cond;
  82. AVFormatContext *avctx;
  83. int64_t max_q_size;
  84. } DecklinkPacketQueue;
  85. struct decklink_ctx {
  86. /* DeckLink SDK interfaces */
  87. IDeckLink *dl;
  88. IDeckLinkOutput *dlo;
  89. IDeckLinkInput *dli;
  90. IDeckLinkConfiguration *cfg;
  91. IDeckLinkProfileAttributes *attr;
  92. decklink_output_callback *output_callback;
  93. /* DeckLink mode information */
  94. BMDTimeValue bmd_tb_den;
  95. BMDTimeValue bmd_tb_num;
  96. BMDDisplayMode bmd_mode;
  97. BMDVideoConnection video_input;
  98. BMDAudioConnection audio_input;
  99. BMDTimecodeFormat tc_format;
  100. int bmd_width;
  101. int bmd_height;
  102. int bmd_field_dominance;
  103. int supports_vanc;
  104. /* Capture buffer queue */
  105. DecklinkPacketQueue queue;
  106. CCFifo cc_fifo; ///< closed captions
  107. /* Output VANC queue */
  108. DecklinkPacketQueue vanc_queue;
  109. /* Streams present */
  110. int audio;
  111. int video;
  112. /* Status */
  113. int playback_started;
  114. int64_t first_pts;
  115. int64_t last_pts;
  116. unsigned long frameCount;
  117. unsigned int dropped;
  118. AVStream *audio_st;
  119. AVStream *video_st;
  120. AVStream *klv_st;
  121. AVStream *teletext_st;
  122. uint16_t cdp_sequence_num;
  123. /* Options */
  124. int list_devices;
  125. int list_formats;
  126. int enable_klv;
  127. int64_t teletext_lines;
  128. double preroll;
  129. int duplex_mode;
  130. BMDLinkConfiguration link;
  131. DecklinkPtsSource audio_pts_source;
  132. DecklinkPtsSource video_pts_source;
  133. int draw_bars;
  134. BMDPixelFormat raw_format;
  135. DecklinkSignalLossAction signal_loss_action;
  136. int frames_preroll;
  137. int frames_buffer;
  138. pthread_mutex_t mutex;
  139. pthread_cond_t cond;
  140. int frames_buffer_available_spots;
  141. int autodetect;
  142. #if CONFIG_LIBKLVANC
  143. struct klvanc_context_s *vanc_ctx;
  144. #endif
  145. int channels;
  146. int audio_depth;
  147. unsigned long tc_seen; // used with option wait_for_tc
  148. };
  149. typedef enum { DIRECTION_IN, DIRECTION_OUT} decklink_direction_t;
  150. static const BMDPixelFormat decklink_raw_format_map[] = {
  151. (BMDPixelFormat)0,
  152. bmdFormat8BitYUV,
  153. bmdFormat10BitYUV,
  154. bmdFormat8BitARGB,
  155. bmdFormat8BitBGRA,
  156. bmdFormat10BitRGB,
  157. };
  158. static const BMDAudioConnection decklink_audio_connection_map[] = {
  159. (BMDAudioConnection)0,
  160. bmdAudioConnectionEmbedded,
  161. bmdAudioConnectionAESEBU,
  162. bmdAudioConnectionAnalog,
  163. bmdAudioConnectionAnalogXLR,
  164. bmdAudioConnectionAnalogRCA,
  165. bmdAudioConnectionMicrophone,
  166. };
  167. static const BMDVideoConnection decklink_video_connection_map[] = {
  168. (BMDVideoConnection)0,
  169. bmdVideoConnectionSDI,
  170. bmdVideoConnectionHDMI,
  171. bmdVideoConnectionOpticalSDI,
  172. bmdVideoConnectionComponent,
  173. bmdVideoConnectionComposite,
  174. bmdVideoConnectionSVideo,
  175. };
  176. static const BMDTimecodeFormat decklink_timecode_format_map[] = {
  177. (BMDTimecodeFormat)0,
  178. bmdTimecodeRP188VITC1,
  179. bmdTimecodeRP188VITC2,
  180. bmdTimecodeRP188LTC,
  181. bmdTimecodeRP188Any,
  182. bmdTimecodeVITC,
  183. bmdTimecodeVITCField2,
  184. bmdTimecodeSerial,
  185. #if BLACKMAGIC_DECKLINK_API_VERSION >= 0x0b000000
  186. bmdTimecodeRP188HighFrameRate,
  187. #else
  188. (BMDTimecodeFormat)0,
  189. #endif
  190. };
  191. static const BMDLinkConfiguration decklink_link_conf_map[] = {
  192. (BMDLinkConfiguration)0,
  193. bmdLinkConfigurationSingleLink,
  194. bmdLinkConfigurationDualLink,
  195. bmdLinkConfigurationQuadLink
  196. };
  197. #if BLACKMAGIC_DECKLINK_API_VERSION >= 0x0b000000
  198. static const BMDProfileID decklink_profile_id_map[] = {
  199. (BMDProfileID)0,
  200. bmdProfileTwoSubDevicesHalfDuplex,
  201. bmdProfileOneSubDeviceFullDuplex,
  202. bmdProfileOneSubDeviceHalfDuplex,
  203. bmdProfileTwoSubDevicesFullDuplex,
  204. bmdProfileFourSubDevicesHalfDuplex,
  205. };
  206. #endif
  207. int ff_decklink_set_configs(AVFormatContext *avctx, decklink_direction_t direction);
  208. int ff_decklink_set_format(AVFormatContext *avctx, int width, int height, int tb_num, int tb_den, enum AVFieldOrder field_order, decklink_direction_t direction = DIRECTION_OUT);
  209. int ff_decklink_set_format(AVFormatContext *avctx, decklink_direction_t direction);
  210. int ff_decklink_list_devices(AVFormatContext *avctx, struct AVDeviceInfoList *device_list, int show_inputs, int show_outputs);
  211. void ff_decklink_list_devices_legacy(AVFormatContext *avctx, int show_inputs, int show_outputs);
  212. int ff_decklink_list_formats(AVFormatContext *avctx, decklink_direction_t direction = DIRECTION_OUT);
  213. void ff_decklink_cleanup(AVFormatContext *avctx);
  214. int ff_decklink_init_device(AVFormatContext *avctx, const char* name);
  215. void ff_decklink_packet_queue_init(AVFormatContext *avctx, DecklinkPacketQueue *q, int64_t queue_size);
  216. void ff_decklink_packet_queue_flush(DecklinkPacketQueue *q);
  217. void ff_decklink_packet_queue_end(DecklinkPacketQueue *q);
  218. unsigned long long ff_decklink_packet_queue_size(DecklinkPacketQueue *q);
  219. int ff_decklink_packet_queue_put(DecklinkPacketQueue *q, AVPacket *pkt);
  220. int ff_decklink_packet_queue_get(DecklinkPacketQueue *q, AVPacket *pkt, int block);
  221. int64_t ff_decklink_packet_queue_peekpts(DecklinkPacketQueue *q);
  222. #endif /* AVDEVICE_DECKLINK_COMMON_H */