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