decklink_common.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 *teletext_st;
  112. uint16_t cdp_sequence_num;
  113. /* Options */
  114. int list_devices;
  115. int list_formats;
  116. int64_t teletext_lines;
  117. double preroll;
  118. int duplex_mode;
  119. DecklinkPtsSource audio_pts_source;
  120. DecklinkPtsSource video_pts_source;
  121. int draw_bars;
  122. BMDPixelFormat raw_format;
  123. int frames_preroll;
  124. int frames_buffer;
  125. pthread_mutex_t mutex;
  126. pthread_cond_t cond;
  127. int frames_buffer_available_spots;
  128. int autodetect;
  129. #if CONFIG_LIBKLVANC
  130. struct klvanc_context_s *vanc_ctx;
  131. #endif
  132. int channels;
  133. int audio_depth;
  134. unsigned long tc_seen; // used with option wait_for_tc
  135. };
  136. typedef enum { DIRECTION_IN, DIRECTION_OUT} decklink_direction_t;
  137. #ifdef _WIN32
  138. #if BLACKMAGIC_DECKLINK_API_VERSION < 0x0a040000
  139. typedef unsigned long buffercount_type;
  140. #else
  141. typedef unsigned int buffercount_type;
  142. #endif
  143. IDeckLinkIterator *CreateDeckLinkIteratorInstance(void);
  144. #else
  145. typedef uint32_t buffercount_type;
  146. #endif
  147. static const BMDAudioConnection decklink_audio_connection_map[] = {
  148. (BMDAudioConnection)0,
  149. bmdAudioConnectionEmbedded,
  150. bmdAudioConnectionAESEBU,
  151. bmdAudioConnectionAnalog,
  152. bmdAudioConnectionAnalogXLR,
  153. bmdAudioConnectionAnalogRCA,
  154. bmdAudioConnectionMicrophone,
  155. };
  156. static const BMDVideoConnection decklink_video_connection_map[] = {
  157. (BMDVideoConnection)0,
  158. bmdVideoConnectionSDI,
  159. bmdVideoConnectionHDMI,
  160. bmdVideoConnectionOpticalSDI,
  161. bmdVideoConnectionComponent,
  162. bmdVideoConnectionComposite,
  163. bmdVideoConnectionSVideo,
  164. };
  165. static const BMDTimecodeFormat decklink_timecode_format_map[] = {
  166. (BMDTimecodeFormat)0,
  167. bmdTimecodeRP188VITC1,
  168. bmdTimecodeRP188VITC2,
  169. bmdTimecodeRP188LTC,
  170. bmdTimecodeRP188Any,
  171. bmdTimecodeVITC,
  172. bmdTimecodeVITCField2,
  173. bmdTimecodeSerial,
  174. };
  175. int ff_decklink_set_configs(AVFormatContext *avctx, decklink_direction_t direction);
  176. 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);
  177. int ff_decklink_set_format(AVFormatContext *avctx, decklink_direction_t direction);
  178. int ff_decklink_list_devices(AVFormatContext *avctx, struct AVDeviceInfoList *device_list, int show_inputs, int show_outputs);
  179. void ff_decklink_list_devices_legacy(AVFormatContext *avctx, int show_inputs, int show_outputs);
  180. int ff_decklink_list_formats(AVFormatContext *avctx, decklink_direction_t direction = DIRECTION_OUT);
  181. void ff_decklink_cleanup(AVFormatContext *avctx);
  182. int ff_decklink_init_device(AVFormatContext *avctx, const char* name);
  183. #endif /* AVDEVICE_DECKLINK_COMMON_H */