decklink_common.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. int frames_preroll;
  136. int frames_buffer;
  137. pthread_mutex_t mutex;
  138. pthread_cond_t cond;
  139. int frames_buffer_available_spots;
  140. int autodetect;
  141. #if CONFIG_LIBKLVANC
  142. struct klvanc_context_s *vanc_ctx;
  143. #endif
  144. int channels;
  145. int audio_depth;
  146. unsigned long tc_seen; // used with option wait_for_tc
  147. };
  148. typedef enum { DIRECTION_IN, DIRECTION_OUT} decklink_direction_t;
  149. static const BMDPixelFormat decklink_raw_format_map[] = {
  150. (BMDPixelFormat)0,
  151. bmdFormat8BitYUV,
  152. bmdFormat10BitYUV,
  153. bmdFormat8BitARGB,
  154. bmdFormat8BitBGRA,
  155. bmdFormat10BitRGB,
  156. };
  157. static const BMDAudioConnection decklink_audio_connection_map[] = {
  158. (BMDAudioConnection)0,
  159. bmdAudioConnectionEmbedded,
  160. bmdAudioConnectionAESEBU,
  161. bmdAudioConnectionAnalog,
  162. bmdAudioConnectionAnalogXLR,
  163. bmdAudioConnectionAnalogRCA,
  164. bmdAudioConnectionMicrophone,
  165. };
  166. static const BMDVideoConnection decklink_video_connection_map[] = {
  167. (BMDVideoConnection)0,
  168. bmdVideoConnectionSDI,
  169. bmdVideoConnectionHDMI,
  170. bmdVideoConnectionOpticalSDI,
  171. bmdVideoConnectionComponent,
  172. bmdVideoConnectionComposite,
  173. bmdVideoConnectionSVideo,
  174. };
  175. static const BMDTimecodeFormat decklink_timecode_format_map[] = {
  176. (BMDTimecodeFormat)0,
  177. bmdTimecodeRP188VITC1,
  178. bmdTimecodeRP188VITC2,
  179. bmdTimecodeRP188LTC,
  180. bmdTimecodeRP188Any,
  181. bmdTimecodeVITC,
  182. bmdTimecodeVITCField2,
  183. bmdTimecodeSerial,
  184. #if BLACKMAGIC_DECKLINK_API_VERSION >= 0x0b000000
  185. bmdTimecodeRP188HighFrameRate,
  186. #else
  187. (BMDTimecodeFormat)0,
  188. #endif
  189. };
  190. static const BMDLinkConfiguration decklink_link_conf_map[] = {
  191. (BMDLinkConfiguration)0,
  192. bmdLinkConfigurationSingleLink,
  193. bmdLinkConfigurationDualLink,
  194. bmdLinkConfigurationQuadLink
  195. };
  196. #if BLACKMAGIC_DECKLINK_API_VERSION >= 0x0b000000
  197. static const BMDProfileID decklink_profile_id_map[] = {
  198. (BMDProfileID)0,
  199. bmdProfileTwoSubDevicesHalfDuplex,
  200. bmdProfileOneSubDeviceFullDuplex,
  201. bmdProfileOneSubDeviceHalfDuplex,
  202. bmdProfileTwoSubDevicesFullDuplex,
  203. bmdProfileFourSubDevicesHalfDuplex,
  204. };
  205. #endif
  206. int ff_decklink_set_configs(AVFormatContext *avctx, decklink_direction_t direction);
  207. 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);
  208. int ff_decklink_set_format(AVFormatContext *avctx, decklink_direction_t direction);
  209. int ff_decklink_list_devices(AVFormatContext *avctx, struct AVDeviceInfoList *device_list, int show_inputs, int show_outputs);
  210. void ff_decklink_list_devices_legacy(AVFormatContext *avctx, int show_inputs, int show_outputs);
  211. int ff_decklink_list_formats(AVFormatContext *avctx, decklink_direction_t direction = DIRECTION_OUT);
  212. void ff_decklink_cleanup(AVFormatContext *avctx);
  213. int ff_decklink_init_device(AVFormatContext *avctx, const char* name);
  214. void ff_decklink_packet_queue_init(AVFormatContext *avctx, DecklinkPacketQueue *q, int64_t queue_size);
  215. void ff_decklink_packet_queue_flush(DecklinkPacketQueue *q);
  216. void ff_decklink_packet_queue_end(DecklinkPacketQueue *q);
  217. unsigned long long ff_decklink_packet_queue_size(DecklinkPacketQueue *q);
  218. int ff_decklink_packet_queue_put(DecklinkPacketQueue *q, AVPacket *pkt);
  219. int ff_decklink_packet_queue_get(DecklinkPacketQueue *q, AVPacket *pkt, int block);
  220. int64_t ff_decklink_packet_queue_peekpts(DecklinkPacketQueue *q);
  221. #endif /* AVDEVICE_DECKLINK_COMMON_H */