decklink_common.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 "libavcodec/packet_internal.h"
  31. }
  32. #include "libavutil/thread.h"
  33. #include "decklink_common_c.h"
  34. #if CONFIG_LIBKLVANC
  35. #include "libklvanc/vanc.h"
  36. #endif
  37. #ifdef _WIN32
  38. #define DECKLINK_BOOL BOOL
  39. #else
  40. #define DECKLINK_BOOL bool
  41. #endif
  42. #ifdef _WIN32
  43. static char *dup_wchar_to_utf8(wchar_t *w)
  44. {
  45. char *s = NULL;
  46. int l = WideCharToMultiByte(CP_UTF8, 0, w, -1, 0, 0, 0, 0);
  47. s = (char *) av_malloc(l);
  48. if (s)
  49. WideCharToMultiByte(CP_UTF8, 0, w, -1, s, l, 0, 0);
  50. return s;
  51. }
  52. #define DECKLINK_STR OLECHAR *
  53. #define DECKLINK_STRDUP dup_wchar_to_utf8
  54. #define DECKLINK_FREE(s) SysFreeString(s)
  55. #elif defined(__APPLE__)
  56. static char *dup_cfstring_to_utf8(CFStringRef w)
  57. {
  58. char s[256];
  59. CFStringGetCString(w, s, 255, kCFStringEncodingUTF8);
  60. return av_strdup(s);
  61. }
  62. #define DECKLINK_STR const __CFString *
  63. #define DECKLINK_STRDUP dup_cfstring_to_utf8
  64. #define DECKLINK_FREE(s) CFRelease(s)
  65. #else
  66. #define DECKLINK_STR const char *
  67. #define DECKLINK_STRDUP av_strdup
  68. /* free() is needed for a string returned by the DeckLink SDL. */
  69. #define DECKLINK_FREE(s) free((void *) s)
  70. #endif
  71. class decklink_output_callback;
  72. class decklink_input_callback;
  73. typedef struct AVPacketQueue {
  74. PacketList pkt_list;
  75. int nb_packets;
  76. unsigned long long size;
  77. int abort_request;
  78. pthread_mutex_t mutex;
  79. pthread_cond_t cond;
  80. AVFormatContext *avctx;
  81. int64_t max_q_size;
  82. } AVPacketQueue;
  83. struct decklink_ctx {
  84. /* DeckLink SDK interfaces */
  85. IDeckLink *dl;
  86. IDeckLinkOutput *dlo;
  87. IDeckLinkInput *dli;
  88. IDeckLinkConfiguration *cfg;
  89. IDeckLinkProfileAttributes *attr;
  90. decklink_output_callback *output_callback;
  91. /* DeckLink mode information */
  92. BMDTimeValue bmd_tb_den;
  93. BMDTimeValue bmd_tb_num;
  94. BMDDisplayMode bmd_mode;
  95. BMDVideoConnection video_input;
  96. BMDAudioConnection audio_input;
  97. BMDTimecodeFormat tc_format;
  98. int bmd_width;
  99. int bmd_height;
  100. int bmd_field_dominance;
  101. int supports_vanc;
  102. /* Capture buffer queue */
  103. AVPacketQueue queue;
  104. /* Streams present */
  105. int audio;
  106. int video;
  107. /* Status */
  108. int playback_started;
  109. int64_t last_pts;
  110. unsigned long frameCount;
  111. unsigned int dropped;
  112. AVStream *audio_st;
  113. AVStream *video_st;
  114. AVStream *klv_st;
  115. AVStream *teletext_st;
  116. uint16_t cdp_sequence_num;
  117. /* Options */
  118. int list_devices;
  119. int list_formats;
  120. int enable_klv;
  121. int64_t teletext_lines;
  122. double preroll;
  123. int duplex_mode;
  124. BMDLinkConfiguration link;
  125. DecklinkPtsSource audio_pts_source;
  126. DecklinkPtsSource video_pts_source;
  127. int draw_bars;
  128. BMDPixelFormat raw_format;
  129. int frames_preroll;
  130. int frames_buffer;
  131. pthread_mutex_t mutex;
  132. pthread_cond_t cond;
  133. int frames_buffer_available_spots;
  134. int autodetect;
  135. #if CONFIG_LIBKLVANC
  136. struct klvanc_context_s *vanc_ctx;
  137. #endif
  138. int channels;
  139. int audio_depth;
  140. unsigned long tc_seen; // used with option wait_for_tc
  141. };
  142. typedef enum { DIRECTION_IN, DIRECTION_OUT} decklink_direction_t;
  143. static const BMDPixelFormat decklink_raw_format_map[] = {
  144. (BMDPixelFormat)0,
  145. bmdFormat8BitYUV,
  146. bmdFormat10BitYUV,
  147. bmdFormat8BitARGB,
  148. bmdFormat8BitBGRA,
  149. bmdFormat10BitRGB,
  150. };
  151. static const BMDAudioConnection decklink_audio_connection_map[] = {
  152. (BMDAudioConnection)0,
  153. bmdAudioConnectionEmbedded,
  154. bmdAudioConnectionAESEBU,
  155. bmdAudioConnectionAnalog,
  156. bmdAudioConnectionAnalogXLR,
  157. bmdAudioConnectionAnalogRCA,
  158. bmdAudioConnectionMicrophone,
  159. };
  160. static const BMDVideoConnection decklink_video_connection_map[] = {
  161. (BMDVideoConnection)0,
  162. bmdVideoConnectionSDI,
  163. bmdVideoConnectionHDMI,
  164. bmdVideoConnectionOpticalSDI,
  165. bmdVideoConnectionComponent,
  166. bmdVideoConnectionComposite,
  167. bmdVideoConnectionSVideo,
  168. };
  169. static const BMDTimecodeFormat decklink_timecode_format_map[] = {
  170. (BMDTimecodeFormat)0,
  171. bmdTimecodeRP188VITC1,
  172. bmdTimecodeRP188VITC2,
  173. bmdTimecodeRP188LTC,
  174. bmdTimecodeRP188Any,
  175. bmdTimecodeVITC,
  176. bmdTimecodeVITCField2,
  177. bmdTimecodeSerial,
  178. #if BLACKMAGIC_DECKLINK_API_VERSION >= 0x0b000000
  179. bmdTimecodeRP188HighFrameRate,
  180. #else
  181. (BMDTimecodeFormat)0,
  182. #endif
  183. };
  184. static const BMDLinkConfiguration decklink_link_conf_map[] = {
  185. (BMDLinkConfiguration)0,
  186. bmdLinkConfigurationSingleLink,
  187. bmdLinkConfigurationDualLink,
  188. bmdLinkConfigurationQuadLink
  189. };
  190. #if BLACKMAGIC_DECKLINK_API_VERSION >= 0x0b000000
  191. static const BMDProfileID decklink_profile_id_map[] = {
  192. (BMDProfileID)0,
  193. bmdProfileTwoSubDevicesHalfDuplex,
  194. bmdProfileOneSubDeviceFullDuplex,
  195. bmdProfileOneSubDeviceHalfDuplex,
  196. bmdProfileTwoSubDevicesFullDuplex,
  197. bmdProfileFourSubDevicesHalfDuplex,
  198. };
  199. #endif
  200. int ff_decklink_set_configs(AVFormatContext *avctx, decklink_direction_t direction);
  201. 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);
  202. int ff_decklink_set_format(AVFormatContext *avctx, decklink_direction_t direction);
  203. int ff_decklink_list_devices(AVFormatContext *avctx, struct AVDeviceInfoList *device_list, int show_inputs, int show_outputs);
  204. void ff_decklink_list_devices_legacy(AVFormatContext *avctx, int show_inputs, int show_outputs);
  205. int ff_decklink_list_formats(AVFormatContext *avctx, decklink_direction_t direction = DIRECTION_OUT);
  206. void ff_decklink_cleanup(AVFormatContext *avctx);
  207. int ff_decklink_init_device(AVFormatContext *avctx, const char* name);
  208. #endif /* AVDEVICE_DECKLINK_COMMON_H */