decklink_common.h 7.3 KB

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