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