]> git.sesse.net Git - ffmpeg/blob - libavdevice/decklink_common.cpp
Merge commit '4fef648d10bf3bcfd4b8fa5755c1128966a2427c'
[ffmpeg] / libavdevice / decklink_common.cpp
1 /*
2  * Blackmagic DeckLink output
3  * Copyright (c) 2013-2014 Ramiro Polla, Luca Barbato, Deti Fliegl
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include <DeckLinkAPI.h>
23 #ifdef _WIN32
24 #include <DeckLinkAPI_i.c>
25 #else
26 #include <DeckLinkAPIDispatch.cpp>
27 #endif
28
29 #include <pthread.h>
30 #include <semaphore.h>
31
32 extern "C" {
33 #include "libavformat/avformat.h"
34 #include "libavformat/internal.h"
35 #include "libavutil/imgutils.h"
36 }
37
38 #include "decklink_common.h"
39
40 #ifdef _WIN32
41 IDeckLinkIterator *CreateDeckLinkIteratorInstance(void)
42 {
43     IDeckLinkIterator *iter;
44
45     if (CoInitialize(NULL) < 0) {
46         av_log(NULL, AV_LOG_ERROR, "COM initialization failed.\n");
47         return NULL;
48     }
49
50     if (CoCreateInstance(CLSID_CDeckLinkIterator, NULL, CLSCTX_ALL,
51                          IID_IDeckLinkIterator, (void**) &iter) != S_OK) {
52         av_log(NULL, AV_LOG_ERROR, "DeckLink drivers not installed.\n");
53         return NULL;
54     }
55
56     return iter;
57 }
58 #endif
59
60 #ifdef _WIN32
61 static char *dup_wchar_to_utf8(wchar_t *w)
62 {
63     char *s = NULL;
64     int l = WideCharToMultiByte(CP_UTF8, 0, w, -1, 0, 0, 0, 0);
65     s = (char *) av_malloc(l);
66     if (s)
67         WideCharToMultiByte(CP_UTF8, 0, w, -1, s, l, 0, 0);
68     return s;
69 }
70 #define DECKLINK_STR    OLECHAR *
71 #define DECKLINK_STRDUP dup_wchar_to_utf8
72 #define DECKLINK_FREE(s) SysFreeString(s)
73 #define DECKLINK_BOOL BOOL
74 #elif defined(__APPLE__)
75 static char *dup_cfstring_to_utf8(CFStringRef w)
76 {
77     char s[256];
78     CFStringGetCString(w, s, 255, kCFStringEncodingUTF8);
79     return av_strdup(s);
80 }
81 #define DECKLINK_STR    const __CFString *
82 #define DECKLINK_STRDUP dup_cfstring_to_utf8
83 #define DECKLINK_FREE(s) free((void *) s)
84 #define DECKLINK_BOOL bool
85 #else
86 #define DECKLINK_STR    const char *
87 #define DECKLINK_STRDUP av_strdup
88 /* free() is needed for a string returned by the DeckLink SDL. */
89 #define DECKLINK_FREE(s) free((void *) s)
90 #define DECKLINK_BOOL bool
91 #endif
92
93 HRESULT ff_decklink_get_display_name(IDeckLink *This, const char **displayName)
94 {
95     DECKLINK_STR tmpDisplayName;
96     HRESULT hr = This->GetDisplayName(&tmpDisplayName);
97     if (hr != S_OK)
98         return hr;
99     *displayName = DECKLINK_STRDUP(tmpDisplayName);
100     DECKLINK_FREE(tmpDisplayName);
101     return hr;
102 }
103
104 static int decklink_select_input(AVFormatContext *avctx, BMDDeckLinkConfigurationID cfg_id)
105 {
106     struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
107     struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
108     BMDDeckLinkAttributeID attr_id = (cfg_id == bmdDeckLinkConfigAudioInputConnection) ? BMDDeckLinkAudioInputConnections : BMDDeckLinkVideoInputConnections;
109     int64_t bmd_input              = (cfg_id == bmdDeckLinkConfigAudioInputConnection) ? (int64_t)ctx->audio_input : (int64_t)ctx->video_input;
110     const char *type_name          = (cfg_id == bmdDeckLinkConfigAudioInputConnection) ? "audio" : "video";
111     int64_t supported_connections = 0;
112     HRESULT res;
113
114     if (bmd_input) {
115         res = ctx->attr->GetInt(attr_id, &supported_connections);
116         if (res != S_OK) {
117             av_log(avctx, AV_LOG_ERROR, "Failed to query supported %s inputs.\n", type_name);
118             return AVERROR_EXTERNAL;
119         }
120         if ((supported_connections & bmd_input) != bmd_input) {
121             av_log(avctx, AV_LOG_ERROR, "Device does not support selected %s input.\n", type_name);
122             return AVERROR(ENOSYS);
123         }
124         res = ctx->cfg->SetInt(cfg_id, bmd_input);
125         if (res != S_OK) {
126             av_log(avctx, AV_LOG_ERROR, "Failed to select %s input.\n", type_name);
127             return AVERROR_EXTERNAL;
128         }
129     }
130     return 0;
131 }
132
133 static DECKLINK_BOOL field_order_eq(enum AVFieldOrder field_order, BMDFieldDominance bmd_field_order)
134 {
135     if (field_order == AV_FIELD_UNKNOWN)
136         return true;
137     if ((field_order == AV_FIELD_TT || field_order == AV_FIELD_TB) && bmd_field_order == bmdUpperFieldFirst)
138         return true;
139     if ((field_order == AV_FIELD_BB || field_order == AV_FIELD_BT) && bmd_field_order == bmdLowerFieldFirst)
140         return true;
141     if (field_order == AV_FIELD_PROGRESSIVE && (bmd_field_order == bmdProgressiveFrame || bmd_field_order == bmdProgressiveSegmentedFrame))
142         return true;
143     return false;
144 }
145
146 int ff_decklink_set_format(AVFormatContext *avctx,
147                                int width, int height,
148                                int tb_num, int tb_den,
149                                enum AVFieldOrder field_order,
150                                decklink_direction_t direction, int num)
151 {
152     struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
153     struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
154     BMDDisplayModeSupport support;
155     IDeckLinkDisplayModeIterator *itermode;
156     IDeckLinkDisplayMode *mode;
157     int i = 1;
158     HRESULT res;
159
160     av_log(avctx, AV_LOG_DEBUG, "Trying to find mode for frame size %dx%d, frame timing %d/%d, field order %d, direction %d, mode number %d\n",
161         width, height, tb_num, tb_den, field_order, direction, num);
162
163     if (ctx->duplex_mode) {
164         DECKLINK_BOOL duplex_supported = false;
165
166         if (ctx->attr->GetFlag(BMDDeckLinkSupportsDuplexModeConfiguration, &duplex_supported) != S_OK)
167             duplex_supported = false;
168
169         if (duplex_supported) {
170             res = ctx->cfg->SetInt(bmdDeckLinkConfigDuplexMode, ctx->duplex_mode == 2 ? bmdDuplexModeFull : bmdDuplexModeHalf);
171             if (res != S_OK)
172                 av_log(avctx, AV_LOG_WARNING, "Setting duplex mode failed.\n");
173             else
174                 av_log(avctx, AV_LOG_VERBOSE, "Successfully set duplex mode to %s duplex.\n", ctx->duplex_mode == 2 ? "full" : "half");
175         } else {
176             av_log(avctx, AV_LOG_WARNING, "Unable to set duplex mode, because it is not supported.\n");
177         }
178     }
179
180     if (direction == DIRECTION_IN) {
181         int ret;
182         ret = decklink_select_input(avctx, bmdDeckLinkConfigAudioInputConnection);
183         if (ret < 0)
184             return ret;
185         ret = decklink_select_input(avctx, bmdDeckLinkConfigVideoInputConnection);
186         if (ret < 0)
187             return ret;
188         res = ctx->dli->GetDisplayModeIterator (&itermode);
189     } else {
190         res = ctx->dlo->GetDisplayModeIterator (&itermode);
191     }
192
193     if (res!= S_OK) {
194             av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
195             return AVERROR(EIO);
196     }
197
198     AVRational target_tb = av_make_q(tb_num, tb_den);
199     ctx->bmd_mode = bmdModeUnknown;
200     while ((ctx->bmd_mode == bmdModeUnknown) && itermode->Next(&mode) == S_OK) {
201         BMDTimeValue bmd_tb_num, bmd_tb_den;
202         int bmd_width  = mode->GetWidth();
203         int bmd_height = mode->GetHeight();
204         BMDFieldDominance bmd_field_dominance = mode->GetFieldDominance();
205
206         mode->GetFrameRate(&bmd_tb_num, &bmd_tb_den);
207         AVRational mode_tb = av_make_q(bmd_tb_num, bmd_tb_den);
208
209         if ((bmd_width == width &&
210              bmd_height == height &&
211              !av_cmp_q(mode_tb, target_tb) &&
212              field_order_eq(field_order, bmd_field_dominance)) || i == num) {
213             ctx->bmd_mode   = mode->GetDisplayMode();
214             ctx->bmd_width  = bmd_width;
215             ctx->bmd_height = bmd_height;
216             ctx->bmd_tb_den = bmd_tb_den;
217             ctx->bmd_tb_num = bmd_tb_num;
218             ctx->bmd_field_dominance = bmd_field_dominance;
219             av_log(avctx, AV_LOG_INFO, "Found Decklink mode %d x %d with rate %.2f%s\n",
220                 bmd_width, bmd_height, 1/av_q2d(mode_tb),
221                 (ctx->bmd_field_dominance==bmdLowerFieldFirst || ctx->bmd_field_dominance==bmdUpperFieldFirst)?"(i)":"");
222         }
223
224         mode->Release();
225         i++;
226     }
227
228     itermode->Release();
229
230     if (ctx->bmd_mode == bmdModeUnknown)
231         return -1;
232     if (direction == DIRECTION_IN) {
233         if (ctx->dli->DoesSupportVideoMode(ctx->bmd_mode, bmdFormat8BitYUV,
234                                            bmdVideoOutputFlagDefault,
235                                            &support, NULL) != S_OK)
236             return -1;
237     } else {
238         if (ctx->dlo->DoesSupportVideoMode(ctx->bmd_mode, bmdFormat8BitYUV,
239                                            bmdVideoOutputFlagDefault,
240                                            &support, NULL) != S_OK)
241         return -1;
242     }
243     if (support == bmdDisplayModeSupported)
244         return 0;
245
246     return -1;
247 }
248
249 int ff_decklink_set_format(AVFormatContext *avctx, decklink_direction_t direction, int num) {
250     return ff_decklink_set_format(avctx, 0, 0, 0, 0, AV_FIELD_UNKNOWN, direction, num);
251 }
252
253 int ff_decklink_list_devices(AVFormatContext *avctx)
254 {
255     IDeckLink *dl = NULL;
256     IDeckLinkIterator *iter = CreateDeckLinkIteratorInstance();
257     if (!iter) {
258         av_log(avctx, AV_LOG_ERROR, "Could not create DeckLink iterator\n");
259         return AVERROR(EIO);
260     }
261     av_log(avctx, AV_LOG_INFO, "Blackmagic DeckLink devices:\n");
262     while (iter->Next(&dl) == S_OK) {
263         const char *displayName;
264         ff_decklink_get_display_name(dl, &displayName);
265         av_log(avctx, AV_LOG_INFO, "\t'%s'\n", displayName);
266         av_free((void *) displayName);
267         dl->Release();
268     }
269     iter->Release();
270     return 0;
271 }
272
273 int ff_decklink_list_formats(AVFormatContext *avctx, decklink_direction_t direction)
274 {
275     struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
276     struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
277     IDeckLinkDisplayModeIterator *itermode;
278     IDeckLinkDisplayMode *mode;
279     int i=0;
280     HRESULT res;
281
282     if (direction == DIRECTION_IN) {
283         int ret;
284         ret = decklink_select_input(avctx, bmdDeckLinkConfigAudioInputConnection);
285         if (ret < 0)
286             return ret;
287         ret = decklink_select_input(avctx, bmdDeckLinkConfigVideoInputConnection);
288         if (ret < 0)
289             return ret;
290         res = ctx->dli->GetDisplayModeIterator (&itermode);
291     } else {
292         res = ctx->dlo->GetDisplayModeIterator (&itermode);
293     }
294
295     if (res!= S_OK) {
296             av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
297             return AVERROR(EIO);
298     }
299
300     av_log(avctx, AV_LOG_INFO, "Supported formats for '%s':\n",
301                avctx->filename);
302     while (itermode->Next(&mode) == S_OK) {
303         BMDTimeValue tb_num, tb_den;
304         mode->GetFrameRate(&tb_num, &tb_den);
305         av_log(avctx, AV_LOG_INFO, "\t%d\t%ldx%ld at %d/%d fps",
306                 ++i,mode->GetWidth(), mode->GetHeight(),
307                 (int) tb_den, (int) tb_num);
308         switch (mode->GetFieldDominance()) {
309         case bmdLowerFieldFirst:
310         av_log(avctx, AV_LOG_INFO, " (interlaced, lower field first)"); break;
311         case bmdUpperFieldFirst:
312         av_log(avctx, AV_LOG_INFO, " (interlaced, upper field first)"); break;
313         }
314         av_log(avctx, AV_LOG_INFO, "\n");
315         mode->Release();
316     }
317
318     itermode->Release();
319
320     return 0;
321 }
322
323 void ff_decklink_cleanup(AVFormatContext *avctx)
324 {
325     struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
326     struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
327
328     if (ctx->dli)
329         ctx->dli->Release();
330     if (ctx->dlo)
331         ctx->dlo->Release();
332     if (ctx->attr)
333         ctx->attr->Release();
334     if (ctx->cfg)
335         ctx->cfg->Release();
336     if (ctx->dl)
337         ctx->dl->Release();
338 }
339
340 int ff_decklink_init_device(AVFormatContext *avctx, const char* name)
341 {
342     struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
343     struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
344     IDeckLink *dl = NULL;
345     IDeckLinkIterator *iter = CreateDeckLinkIteratorInstance();
346     if (!iter) {
347         av_log(avctx, AV_LOG_ERROR, "Could not create DeckLink iterator\n");
348         return AVERROR_EXTERNAL;
349     }
350
351     while (iter->Next(&dl) == S_OK) {
352         const char *displayName;
353         ff_decklink_get_display_name(dl, &displayName);
354         if (!strcmp(name, displayName)) {
355             av_free((void *)displayName);
356             ctx->dl = dl;
357             break;
358         }
359         av_free((void *)displayName);
360         dl->Release();
361     }
362     iter->Release();
363     if (!ctx->dl)
364         return AVERROR(ENXIO);
365
366     if (ctx->dl->QueryInterface(IID_IDeckLinkConfiguration, (void **)&ctx->cfg) != S_OK) {
367         av_log(avctx, AV_LOG_ERROR, "Could not get configuration interface for '%s'\n", name);
368         ff_decklink_cleanup(avctx);
369         return AVERROR_EXTERNAL;
370     }
371
372     if (ctx->dl->QueryInterface(IID_IDeckLinkAttributes, (void **)&ctx->attr) != S_OK) {
373         av_log(avctx, AV_LOG_ERROR, "Could not get attributes interface for '%s'\n", name);
374         ff_decklink_cleanup(avctx);
375         return AVERROR_EXTERNAL;
376     }
377
378     return 0;
379 }