decklink_common.h 6.0 KB

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