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