]> git.sesse.net Git - ffmpeg/blob - libavdevice/decklink_common.cpp
Merge commit '88b32673db39440422a73ec3047d3326c96b4fb2'
[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) != S_OK) {
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 #else
73 #define DECKLINK_STR    const char *
74 #define DECKLINK_STRDUP av_strdup
75 #endif
76
77 HRESULT ff_decklink_get_display_name(IDeckLink *This, const char **displayName)
78 {
79     DECKLINK_STR tmpDisplayName;
80     HRESULT hr = This->GetDisplayName(&tmpDisplayName);
81     if (hr != S_OK)
82         return hr;
83     *displayName = DECKLINK_STRDUP(tmpDisplayName);
84     /* free() is needed for a string returned by the DeckLink SDL. */
85     free((void *) tmpDisplayName);
86     return hr;
87 }
88
89 int ff_decklink_set_format(AVFormatContext *avctx,
90                                int width, int height,
91                                int tb_num, int tb_den,
92                                decklink_direction_t direction, int num)
93 {
94     struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
95     struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
96     BMDDisplayModeSupport support;
97     IDeckLinkDisplayModeIterator *itermode;
98     IDeckLinkDisplayMode *mode;
99     int i = 1;
100     HRESULT res;
101
102     if (direction == DIRECTION_IN) {
103         res = ctx->dli->GetDisplayModeIterator (&itermode);
104     } else {
105         res = ctx->dlo->GetDisplayModeIterator (&itermode);
106     }
107
108     if (res!= S_OK) {
109             av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
110             return AVERROR(EIO);
111     }
112
113
114     if (tb_num == 1) {
115         tb_num *= 1000;
116         tb_den *= 1000;
117     }
118     ctx->bmd_mode = bmdModeUnknown;
119     while ((ctx->bmd_mode == bmdModeUnknown) && itermode->Next(&mode) == S_OK) {
120         BMDTimeValue bmd_tb_num, bmd_tb_den;
121         int bmd_width  = mode->GetWidth();
122         int bmd_height = mode->GetHeight();
123
124         mode->GetFrameRate(&bmd_tb_num, &bmd_tb_den);
125
126         if ((bmd_width == width && bmd_height == height &&
127             bmd_tb_num == tb_num && bmd_tb_den == tb_den) || i == num) {
128             ctx->bmd_mode   = mode->GetDisplayMode();
129             ctx->bmd_width  = bmd_width;
130             ctx->bmd_height = bmd_height;
131             ctx->bmd_tb_den = bmd_tb_den;
132             ctx->bmd_tb_num = bmd_tb_num;
133             ctx->bmd_field_dominance = mode->GetFieldDominance();
134             av_log(avctx, AV_LOG_INFO, "Found Decklink mode %d x %d with rate %.2f%s\n",
135                 bmd_width, bmd_height, (float)bmd_tb_den/(float)bmd_tb_num,
136                 (ctx->bmd_field_dominance==bmdLowerFieldFirst || ctx->bmd_field_dominance==bmdUpperFieldFirst)?"(i)":"");
137         }
138
139         mode->Release();
140         i++;
141     }
142
143     itermode->Release();
144
145     if (ctx->bmd_mode == bmdModeUnknown)
146         return -1;
147     if (direction == DIRECTION_IN) {
148         if (ctx->dli->DoesSupportVideoMode(ctx->bmd_mode, bmdFormat8BitYUV,
149                                            bmdVideoOutputFlagDefault,
150                                            &support, NULL) != S_OK)
151             return -1;
152     } else {
153         if (ctx->dlo->DoesSupportVideoMode(ctx->bmd_mode, bmdFormat8BitYUV,
154                                            bmdVideoOutputFlagDefault,
155                                            &support, NULL) != S_OK)
156         return -1;
157     }
158     if (support == bmdDisplayModeSupported)
159         return 0;
160
161     return -1;
162 }
163
164 int ff_decklink_set_format(AVFormatContext *avctx, decklink_direction_t direction, int num) {
165     return ff_decklink_set_format(avctx, 0, 0, 0, 0, direction, num);
166 }
167
168 int ff_decklink_list_devices(AVFormatContext *avctx)
169 {
170     IDeckLink *dl = NULL;
171     IDeckLinkIterator *iter = CreateDeckLinkIteratorInstance();
172     if (!iter) {
173         av_log(avctx, AV_LOG_ERROR, "Could not create DeckLink iterator\n");
174         return AVERROR(EIO);
175     }
176     av_log(avctx, AV_LOG_INFO, "Blackmagic DeckLink devices:\n");
177     while (iter->Next(&dl) == S_OK) {
178         const char *displayName;
179         ff_decklink_get_display_name(dl, &displayName);
180         av_log(avctx, AV_LOG_INFO, "\t'%s'\n", displayName);
181         av_free((void *) displayName);
182         dl->Release();
183     }
184     iter->Release();
185     return 0;
186 }
187
188 int ff_decklink_list_formats(AVFormatContext *avctx, decklink_direction_t direction)
189 {
190     struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
191     struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
192     IDeckLinkDisplayModeIterator *itermode;
193     IDeckLinkDisplayMode *mode;
194     int i=0;
195     HRESULT res;
196
197     if (direction == DIRECTION_IN) {
198         res = ctx->dli->GetDisplayModeIterator (&itermode);
199     } else {
200         res = ctx->dlo->GetDisplayModeIterator (&itermode);
201     }
202
203     if (res!= S_OK) {
204             av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
205             return AVERROR(EIO);
206     }
207
208     av_log(avctx, AV_LOG_INFO, "Supported formats for '%s':\n",
209                avctx->filename);
210     while (itermode->Next(&mode) == S_OK) {
211         BMDTimeValue tb_num, tb_den;
212         mode->GetFrameRate(&tb_num, &tb_den);
213         av_log(avctx, AV_LOG_INFO, "\t%d\t%ldx%ld at %d/%d fps",
214                 ++i,mode->GetWidth(), mode->GetHeight(),
215                 (int) tb_den, (int) tb_num);
216         switch (mode->GetFieldDominance()) {
217         case bmdLowerFieldFirst:
218         av_log(avctx, AV_LOG_INFO, " (interlaced, lower field first)"); break;
219         case bmdUpperFieldFirst:
220         av_log(avctx, AV_LOG_INFO, " (interlaced, upper field first)"); break;
221         }
222         av_log(avctx, AV_LOG_INFO, "\n");
223         mode->Release();
224     }
225
226     itermode->Release();
227
228     return 0;
229 }