1 /* GStreamer
2  * Copyright (C) 2011 David Schleef <ds@schleef.org>
3  * Copyright (C) 2014 Sebastian Dröge <sebastian@centricular.com>
4  * Copyright (C) 2015 Florian Langlois <florian.langlois@fr.thalesgroup.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin Street, Suite 500,
19  * Boston, MA 02110-1335, USA.
20  */
21 
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 
26 #include <gst/gst.h>
27 #include "gstdecklink.h"
28 #include "gstdecklinkaudiosink.h"
29 #include "gstdecklinkvideosink.h"
30 #include "gstdecklinkaudiosrc.h"
31 #include "gstdecklinkvideosrc.h"
32 
33 GST_DEBUG_CATEGORY_STATIC (gst_decklink_debug);
34 #define GST_CAT_DEFAULT gst_decklink_debug
35 
36 GType
gst_decklink_mode_get_type(void)37 gst_decklink_mode_get_type (void)
38 {
39   static gsize id = 0;
40   static const GEnumValue modes[] = {
41     {GST_DECKLINK_MODE_AUTO, "Automatic detection", "auto"},
42 
43     {GST_DECKLINK_MODE_NTSC, "NTSC SD 60i", "ntsc"},
44     {GST_DECKLINK_MODE_NTSC2398, "NTSC SD 60i (24 fps)", "ntsc2398"},
45     {GST_DECKLINK_MODE_PAL, "PAL SD 50i", "pal"},
46     {GST_DECKLINK_MODE_NTSC_P, "NTSC SD 60p", "ntsc-p"},
47     {GST_DECKLINK_MODE_PAL_P, "PAL SD 50p", "pal-p"},
48 
49     {GST_DECKLINK_MODE_1080p2398, "HD1080 23.98p", "1080p2398"},
50     {GST_DECKLINK_MODE_1080p24, "HD1080 24p", "1080p24"},
51     {GST_DECKLINK_MODE_1080p25, "HD1080 25p", "1080p25"},
52     {GST_DECKLINK_MODE_1080p2997, "HD1080 29.97p", "1080p2997"},
53     {GST_DECKLINK_MODE_1080p30, "HD1080 30p", "1080p30"},
54 
55     {GST_DECKLINK_MODE_1080i50, "HD1080 50i", "1080i50"},
56     {GST_DECKLINK_MODE_1080i5994, "HD1080 59.94i", "1080i5994"},
57     {GST_DECKLINK_MODE_1080i60, "HD1080 60i", "1080i60"},
58 
59     {GST_DECKLINK_MODE_1080p50, "HD1080 50p", "1080p50"},
60     {GST_DECKLINK_MODE_1080p5994, "HD1080 59.94p", "1080p5994"},
61     {GST_DECKLINK_MODE_1080p60, "HD1080 60p", "1080p60"},
62 
63     {GST_DECKLINK_MODE_720p50, "HD720 50p", "720p50"},
64     {GST_DECKLINK_MODE_720p5994, "HD720 59.94p", "720p5994"},
65     {GST_DECKLINK_MODE_720p60, "HD720 60p", "720p60"},
66 
67     {GST_DECKLINK_MODE_1556p2398, "2k 23.98p", "1556p2398"},
68     {GST_DECKLINK_MODE_1556p24, "2k 24p", "1556p24"},
69     {GST_DECKLINK_MODE_1556p25, "2k 25p", "1556p25"},
70 
71     {GST_DECKLINK_MODE_2160p2398, "4k 23.98p", "2160p2398"},
72     {GST_DECKLINK_MODE_2160p24, "4k 24p", "2160p24"},
73     {GST_DECKLINK_MODE_2160p25, "4k 25p", "2160p25"},
74     {GST_DECKLINK_MODE_2160p2997, "4k 29.97p", "2160p2997"},
75     {GST_DECKLINK_MODE_2160p30, "4k 30p", "2160p30"},
76     {GST_DECKLINK_MODE_2160p50, "4k 50p", "2160p50"},
77     {GST_DECKLINK_MODE_2160p5994, "4k 59.94p", "2160p5994"},
78     {GST_DECKLINK_MODE_2160p60, "4k 60p", "2160p60"},
79 
80     {0, NULL, NULL}
81   };
82 
83   if (g_once_init_enter (&id)) {
84     GType tmp = g_enum_register_static ("GstDecklinkModes", modes);
85     g_once_init_leave (&id, tmp);
86   }
87 
88   return (GType) id;
89 }
90 
91 GType
gst_decklink_connection_get_type(void)92 gst_decklink_connection_get_type (void)
93 {
94   static gsize id = 0;
95   static const GEnumValue connections[] = {
96     {GST_DECKLINK_CONNECTION_AUTO, "Auto", "auto"},
97     {GST_DECKLINK_CONNECTION_SDI, "SDI", "sdi"},
98     {GST_DECKLINK_CONNECTION_HDMI, "HDMI", "hdmi"},
99     {GST_DECKLINK_CONNECTION_OPTICAL_SDI, "Optical SDI", "optical-sdi"},
100     {GST_DECKLINK_CONNECTION_COMPONENT, "Component", "component"},
101     {GST_DECKLINK_CONNECTION_COMPOSITE, "Composite", "composite"},
102     {GST_DECKLINK_CONNECTION_SVIDEO, "S-Video", "svideo"},
103     {0, NULL, NULL}
104   };
105 
106   if (g_once_init_enter (&id)) {
107     GType tmp = g_enum_register_static ("GstDecklinkConnection", connections);
108     g_once_init_leave (&id, tmp);
109   }
110 
111   return (GType) id;
112 }
113 
114 GType
gst_decklink_video_format_get_type(void)115 gst_decklink_video_format_get_type (void)
116 {
117   static gsize id = 0;
118   static const GEnumValue types[] = {
119     {GST_DECKLINK_VIDEO_FORMAT_AUTO, "Auto", "auto"},
120     {GST_DECKLINK_VIDEO_FORMAT_8BIT_YUV, "bmdFormat8BitYUV", "8bit-yuv"},
121     {GST_DECKLINK_VIDEO_FORMAT_10BIT_YUV, "bmdFormat10BitYUV", "10bit-yuv"},
122     {GST_DECKLINK_VIDEO_FORMAT_8BIT_ARGB, "bmdFormat8BitARGB", "8bit-argb"},
123     {GST_DECKLINK_VIDEO_FORMAT_8BIT_BGRA, "bmdFormat8BitBGRA", "8bit-bgra"},
124     /* Not yet supported:
125        {GST_DECKLINK_VIDEO_FORMAT_10BIT_RGB, "bmdFormat10BitRGB", "10bit-rgb"},
126        {GST_DECKLINK_VIDEO_FORMAT_12BIT_RGB, "bmdFormat12BitRGB", "12bit-rgb"},
127        {GST_DECKLINK_VIDEO_FORMAT_12BIT_RGBLE, "bmdFormat12BitRGBLE", "12bit-rgble"},
128        {GST_DECKLINK_VIDEO_FORMAT_10BIT_RGBXLE, "bmdFormat10BitRGBXLE", "10bit-rgbxle"},
129        {GST_DECKLINK_VIDEO_FORMAT_10BIT_RGBX, "bmdFormat10BitRGBX", "10bit-rgbx"},
130      */
131     {0, NULL, NULL}
132   };
133 
134   if (g_once_init_enter (&id)) {
135     GType tmp = g_enum_register_static ("GstDecklinkVideoFormat", types);
136     g_once_init_leave (&id, tmp);
137   }
138 
139   return (GType) id;
140 }
141 
142 GType
gst_decklink_duplex_mode_get_type(void)143 gst_decklink_duplex_mode_get_type (void)
144 {
145   static gsize id = 0;
146   static const GEnumValue types[] = {
147     {GST_DECKLINK_DUPLEX_MODE_HALF, "Half-Duplex", "half"},
148     {GST_DECKLINK_DUPLEX_MODE_FULL, "Full-Duplex", "full"},
149     {0, NULL, NULL}
150   };
151 
152   if (g_once_init_enter (&id)) {
153     GType tmp = g_enum_register_static ("GstDecklinkDuplexMode", types);
154     g_once_init_leave (&id, tmp);
155   }
156 
157   return (GType) id;
158 }
159 
160 GType
gst_decklink_timecode_format_get_type(void)161 gst_decklink_timecode_format_get_type (void)
162 {
163   static gsize id = 0;
164   static const GEnumValue timecodeformats[] = {
165     {GST_DECKLINK_TIMECODE_FORMAT_RP188VITC1, "bmdTimecodeRP188VITC1",
166         "rp188vitc1"},
167     {GST_DECKLINK_TIMECODE_FORMAT_RP188VITC2, "bmdTimecodeRP188VITC2",
168         "rp188vitc2"},
169     {GST_DECKLINK_TIMECODE_FORMAT_RP188LTC, "bmdTimecodeRP188LTC", "rp188ltc"},
170     {GST_DECKLINK_TIMECODE_FORMAT_RP188ANY, "bmdTimecodeRP188Any", "rp188any"},
171     {GST_DECKLINK_TIMECODE_FORMAT_VITC, "bmdTimecodeVITC", "vitc"},
172     {GST_DECKLINK_TIMECODE_FORMAT_VITCFIELD2, "bmdTimecodeVITCField2",
173         "vitcfield2"},
174     {GST_DECKLINK_TIMECODE_FORMAT_SERIAL, "bmdTimecodeSerial", "serial"},
175     {0, NULL, NULL}
176   };
177 
178   if (g_once_init_enter (&id)) {
179     GType tmp =
180         g_enum_register_static ("GstDecklinkTimecodeFormat", timecodeformats);
181     g_once_init_leave (&id, tmp);
182   }
183 
184   return (GType) id;
185 }
186 
187 GType
gst_decklink_keyer_mode_get_type(void)188 gst_decklink_keyer_mode_get_type (void)
189 {
190   static gsize id = 0;
191   static const GEnumValue keyermodes[] = {
192     {GST_DECKLINK_KEYER_MODE_OFF, "Off", "off"},
193     {GST_DECKLINK_KEYER_MODE_INTERNAL, "Internal", "internal"},
194     {GST_DECKLINK_KEYER_MODE_EXTERNAL, "External", "external"},
195     {0, NULL, NULL}
196   };
197 
198   if (g_once_init_enter (&id)) {
199     GType tmp = g_enum_register_static ("GstDecklinkKeyerMode", keyermodes);
200     g_once_init_leave (&id, tmp);
201   }
202 
203   return (GType) id;
204 }
205 
206 GType
gst_decklink_audio_connection_get_type(void)207 gst_decklink_audio_connection_get_type (void)
208 {
209   static gsize id = 0;
210   static const GEnumValue connections[] = {
211     {GST_DECKLINK_AUDIO_CONNECTION_AUTO, "Automatic", "auto"},
212     {GST_DECKLINK_AUDIO_CONNECTION_EMBEDDED, "SDI/HDMI embedded audio",
213         "embedded"},
214     {GST_DECKLINK_AUDIO_CONNECTION_AES_EBU, "AES/EBU input", "aes"},
215     {GST_DECKLINK_AUDIO_CONNECTION_ANALOG, "Analog input", "analog"},
216     {GST_DECKLINK_AUDIO_CONNECTION_ANALOG_XLR, "Analog input (XLR)",
217         "analog-xlr"},
218     {GST_DECKLINK_AUDIO_CONNECTION_ANALOG_RCA, "Analog input (RCA)",
219         "analog-rca"},
220     {0, NULL, NULL}
221   };
222 
223   if (g_once_init_enter (&id)) {
224     GType tmp =
225         g_enum_register_static ("GstDecklinkAudioConnection", connections);
226     g_once_init_leave (&id, tmp);
227   }
228 
229   return (GType) id;
230 }
231 
232 GType
gst_decklink_audio_channels_get_type(void)233 gst_decklink_audio_channels_get_type (void)
234 {
235   static gsize id = 0;
236   static const GEnumValue connections[] = {
237     {GST_DECKLINK_AUDIO_CHANNELS_2, "2 Channels", "2"},
238     {GST_DECKLINK_AUDIO_CHANNELS_8, "8 Channels", "8"},
239     {GST_DECKLINK_AUDIO_CHANNELS_16, "16 Channels", "16"},
240     {GST_DECKLINK_AUDIO_CHANNELS_MAX, "Maximum channels supported", "max"},
241     {0, NULL, NULL}
242   };
243 
244   if (g_once_init_enter (&id)) {
245     GType tmp =
246         g_enum_register_static ("GstDecklinkAudioChannels", connections);
247     g_once_init_leave (&id, tmp);
248   }
249 
250   return (GType) id;
251 }
252 
253 #define NTSC 10, 11, false, "bt601"
254 #define PAL 12, 11, true, "bt601"
255 #define HD 1, 1, true, "bt709"
256 #define UHD 1, 1, true, "bt2020"
257 
258 static const GstDecklinkMode modes[] = {
259   {bmdModeNTSC, 720, 486, 30000, 1001, true, NTSC},     // default is ntsc
260 
261   {bmdModeNTSC, 720, 486, 30000, 1001, true, NTSC},
262   {bmdModeNTSC2398, 720, 486, 24000, 1001, true, NTSC},
263   {bmdModePAL, 720, 576, 25, 1, true, PAL},
264   {bmdModeNTSCp, 720, 486, 30000, 1001, false, NTSC},
265   {bmdModePALp, 720, 576, 25, 1, false, PAL},
266 
267   {bmdModeHD1080p2398, 1920, 1080, 24000, 1001, false, HD},
268   {bmdModeHD1080p24, 1920, 1080, 24, 1, false, HD},
269   {bmdModeHD1080p25, 1920, 1080, 25, 1, false, HD},
270   {bmdModeHD1080p2997, 1920, 1080, 30000, 1001, false, HD},
271   {bmdModeHD1080p30, 1920, 1080, 30, 1, false, HD},
272 
273   {bmdModeHD1080i50, 1920, 1080, 25, 1, true, HD},
274   {bmdModeHD1080i5994, 1920, 1080, 30000, 1001, true, HD},
275   {bmdModeHD1080i6000, 1920, 1080, 30, 1, true, HD},
276 
277   {bmdModeHD1080p50, 1920, 1080, 50, 1, false, HD},
278   {bmdModeHD1080p5994, 1920, 1080, 60000, 1001, false, HD},
279   {bmdModeHD1080p6000, 1920, 1080, 60, 1, false, HD},
280 
281   {bmdModeHD720p50, 1280, 720, 50, 1, false, HD},
282   {bmdModeHD720p5994, 1280, 720, 60000, 1001, false, HD},
283   {bmdModeHD720p60, 1280, 720, 60, 1, false, HD},
284 
285   {bmdMode2k2398, 2048, 1556, 24000, 1001, false, HD},
286   {bmdMode2k24, 2048, 1556, 24, 1, false, HD},
287   {bmdMode2k25, 2048, 1556, 25, 1, false, HD},
288 
289   {bmdMode4K2160p2398, 3840, 2160, 24000, 1001, false, UHD},
290   {bmdMode4K2160p24, 3840, 2160, 24, 1, false, UHD},
291   {bmdMode4K2160p25, 3840, 2160, 25, 1, false, UHD},
292   {bmdMode4K2160p2997, 3840, 2160, 30000, 1001, false, UHD},
293   {bmdMode4K2160p30, 3840, 2160, 30, 1, false, UHD},
294   {bmdMode4K2160p50, 3840, 2160, 50, 1, false, UHD},
295   {bmdMode4K2160p5994, 3840, 2160, 60000, 1001, false, UHD},
296   {bmdMode4K2160p60, 3840, 2160, 60, 1, false, UHD}
297 };
298 
299 static const struct
300 {
301   BMDPixelFormat format;
302   gint bpp;
303   GstVideoFormat vformat;
304 } formats[] = {
305   /* *INDENT-OFF* */
306   {bmdFormat8BitYUV, 2, GST_VIDEO_FORMAT_UYVY},  /* auto */
307   {bmdFormat8BitYUV, 2, GST_VIDEO_FORMAT_UYVY},
308   {bmdFormat10BitYUV, 4, GST_VIDEO_FORMAT_v210},
309   {bmdFormat8BitARGB, 4, GST_VIDEO_FORMAT_ARGB},
310   {bmdFormat8BitBGRA, 4, GST_VIDEO_FORMAT_BGRA},
311 /* Not yet supported
312   {bmdFormat10BitRGB, FIXME, FIXME},
313   {bmdFormat12BitRGB, FIXME, FIXME},
314   {bmdFormat12BitRGBLE, FIXME, FIXME},
315   {bmdFormat10BitRGBXLE, FIXME, FIXME},
316   {bmdFormat10BitRGBX, FIXME, FIXME} */
317   /* *INDENT-ON* */
318 };
319 
320 static const struct
321 {
322   BMDDuplexMode mode;
323   GstDecklinkDuplexMode gstmode;
324 } duplex_modes[] = {
325   /* *INDENT-OFF* */
326   {bmdDuplexModeHalf, GST_DECKLINK_DUPLEX_MODE_HALF},
327   {bmdDuplexModeFull, GST_DECKLINK_DUPLEX_MODE_FULL},
328   /* *INDENT-ON* */
329 };
330 
331 enum DuplexModeSetOperationResult
332 {
333   DUPLEX_MODE_SET_UNSUPPORTED,
334   DUPLEX_MODE_SET_SUCCESS,
335   DUPLEX_MODE_SET_FAILURE
336 };
337 
338 static const struct
339 {
340   BMDTimecodeFormat format;
341   GstDecklinkTimecodeFormat gstformat;
342 } tcformats[] = {
343   /* *INDENT-OFF* */
344   {bmdTimecodeRP188VITC1, GST_DECKLINK_TIMECODE_FORMAT_RP188VITC1},
345   {bmdTimecodeRP188VITC2, GST_DECKLINK_TIMECODE_FORMAT_RP188VITC2},
346   {bmdTimecodeRP188LTC, GST_DECKLINK_TIMECODE_FORMAT_RP188LTC},
347   {bmdTimecodeRP188Any, GST_DECKLINK_TIMECODE_FORMAT_RP188ANY},
348   {bmdTimecodeVITC, GST_DECKLINK_TIMECODE_FORMAT_VITC},
349   {bmdTimecodeVITCField2, GST_DECKLINK_TIMECODE_FORMAT_VITCFIELD2},
350   {bmdTimecodeSerial, GST_DECKLINK_TIMECODE_FORMAT_SERIAL}
351   /* *INDENT-ON* */
352 };
353 
354 static const struct
355 {
356   BMDKeyerMode keymode;
357   GstDecklinkKeyerMode gstkeymode;
358 } kmodes[] = {
359   /* *INDENT-OFF* */
360   {bmdKeyerModeOff, GST_DECKLINK_KEYER_MODE_OFF},
361   {bmdKeyerModeInternal, GST_DECKLINK_KEYER_MODE_INTERNAL},
362   {bmdKeyerModeExternal, GST_DECKLINK_KEYER_MODE_EXTERNAL}
363   /* *INDENT-ON* */
364 };
365 
366 const GstDecklinkMode *
gst_decklink_get_mode(GstDecklinkModeEnum e)367 gst_decklink_get_mode (GstDecklinkModeEnum e)
368 {
369   if (e < GST_DECKLINK_MODE_AUTO || e > GST_DECKLINK_MODE_2160p60)
370     return NULL;
371   return &modes[e];
372 }
373 
374 const GstDecklinkModeEnum
gst_decklink_get_mode_enum_from_bmd(BMDDisplayMode mode)375 gst_decklink_get_mode_enum_from_bmd (BMDDisplayMode mode)
376 {
377   GstDecklinkModeEnum displayMode = GST_DECKLINK_MODE_NTSC;
378   switch (mode) {
379     case bmdModeNTSC:
380       displayMode = GST_DECKLINK_MODE_NTSC;
381       break;
382     case bmdModeNTSC2398:
383       displayMode = GST_DECKLINK_MODE_NTSC2398;
384       break;
385     case bmdModePAL:
386       displayMode = GST_DECKLINK_MODE_PAL;
387       break;
388     case bmdModeNTSCp:
389       displayMode = GST_DECKLINK_MODE_NTSC_P;
390       break;
391     case bmdModePALp:
392       displayMode = GST_DECKLINK_MODE_PAL_P;
393       break;
394     case bmdModeHD1080p2398:
395       displayMode = GST_DECKLINK_MODE_1080p2398;
396       break;
397     case bmdModeHD1080p24:
398       displayMode = GST_DECKLINK_MODE_1080p24;
399       break;
400     case bmdModeHD1080p25:
401       displayMode = GST_DECKLINK_MODE_1080p25;
402       break;
403     case bmdModeHD1080p2997:
404       displayMode = GST_DECKLINK_MODE_1080p2997;
405       break;
406     case bmdModeHD1080p30:
407       displayMode = GST_DECKLINK_MODE_1080p30;
408       break;
409     case bmdModeHD1080i50:
410       displayMode = GST_DECKLINK_MODE_1080i50;
411       break;
412     case bmdModeHD1080i5994:
413       displayMode = GST_DECKLINK_MODE_1080i5994;
414       break;
415     case bmdModeHD1080i6000:
416       displayMode = GST_DECKLINK_MODE_1080i60;
417       break;
418     case bmdModeHD1080p50:
419       displayMode = GST_DECKLINK_MODE_1080p50;
420       break;
421     case bmdModeHD1080p5994:
422       displayMode = GST_DECKLINK_MODE_1080p5994;
423       break;
424     case bmdModeHD1080p6000:
425       displayMode = GST_DECKLINK_MODE_1080p60;
426       break;
427     case bmdModeHD720p50:
428       displayMode = GST_DECKLINK_MODE_720p50;
429       break;
430     case bmdModeHD720p5994:
431       displayMode = GST_DECKLINK_MODE_720p5994;
432       break;
433     case bmdModeHD720p60:
434       displayMode = GST_DECKLINK_MODE_720p60;
435       break;
436     case bmdMode2k2398:
437       displayMode = GST_DECKLINK_MODE_1556p2398;
438       break;
439     case bmdMode2k24:
440       displayMode = GST_DECKLINK_MODE_1556p24;
441       break;
442     case bmdMode2k25:
443       displayMode = GST_DECKLINK_MODE_1556p25;
444       break;
445     case bmdMode4K2160p2398:
446       displayMode = GST_DECKLINK_MODE_2160p2398;
447       break;
448     case bmdMode4K2160p24:
449       displayMode = GST_DECKLINK_MODE_2160p24;
450       break;
451     case bmdMode4K2160p25:
452       displayMode = GST_DECKLINK_MODE_2160p25;
453       break;
454     case bmdMode4K2160p2997:
455       displayMode = GST_DECKLINK_MODE_2160p2997;
456       break;
457     case bmdMode4K2160p30:
458       displayMode = GST_DECKLINK_MODE_2160p30;
459       break;
460     case bmdMode4K2160p50:
461       displayMode = GST_DECKLINK_MODE_2160p50;
462       break;
463     case bmdMode4K2160p5994:
464       displayMode = GST_DECKLINK_MODE_2160p5994;
465       break;
466     case bmdMode4K2160p60:
467       displayMode = GST_DECKLINK_MODE_2160p60;
468       break;
469     default:
470       g_assert_not_reached ();
471       break;
472   }
473   return displayMode;
474 }
475 
476 const BMDPixelFormat
gst_decklink_pixel_format_from_type(GstDecklinkVideoFormat t)477 gst_decklink_pixel_format_from_type (GstDecklinkVideoFormat t)
478 {
479   return formats[t].format;
480 }
481 
482 const gint
gst_decklink_bpp_from_type(GstDecklinkVideoFormat t)483 gst_decklink_bpp_from_type (GstDecklinkVideoFormat t)
484 {
485   return formats[t].bpp;
486 }
487 
488 const GstDecklinkVideoFormat
gst_decklink_type_from_video_format(GstVideoFormat f)489 gst_decklink_type_from_video_format (GstVideoFormat f)
490 {
491   guint i;
492 
493   for (i = 1; i < G_N_ELEMENTS (formats); i++) {
494     if (formats[i].vformat == f)
495       return (GstDecklinkVideoFormat) i;
496   }
497   g_assert_not_reached ();
498   return GST_DECKLINK_VIDEO_FORMAT_AUTO;
499 }
500 
501 GstVideoFormat
gst_decklink_video_format_from_type(BMDPixelFormat pf)502 gst_decklink_video_format_from_type (BMDPixelFormat pf)
503 {
504   guint i;
505 
506   for (i = 1; i < G_N_ELEMENTS (formats); i++) {
507     if (formats[i].format == pf)
508       return formats[i].vformat;
509   }
510   GST_WARNING ("Unknown pixel format 0x%x", pf);
511   return GST_VIDEO_FORMAT_UNKNOWN;
512 }
513 
514 
515 const BMDTimecodeFormat
gst_decklink_timecode_format_from_enum(GstDecklinkTimecodeFormat f)516 gst_decklink_timecode_format_from_enum (GstDecklinkTimecodeFormat f)
517 {
518   return tcformats[f].format;
519 }
520 
521 const GstDecklinkTimecodeFormat
gst_decklink_timecode_format_to_enum(BMDTimecodeFormat f)522 gst_decklink_timecode_format_to_enum (BMDTimecodeFormat f)
523 {
524   guint i;
525 
526   for (i = 0; i < G_N_ELEMENTS (tcformats); i++) {
527     if (tcformats[i].format == f)
528       return (GstDecklinkTimecodeFormat) i;
529   }
530   g_assert_not_reached ();
531   return GST_DECKLINK_TIMECODE_FORMAT_RP188ANY;
532 }
533 
534 const BMDDuplexMode
gst_decklink_duplex_mode_from_enum(GstDecklinkDuplexMode m)535 gst_decklink_duplex_mode_from_enum (GstDecklinkDuplexMode m)
536 {
537   return duplex_modes[m].mode;
538 }
539 
540 const GstDecklinkDuplexMode
gst_decklink_duplex_mode_to_enum(BMDDuplexMode m)541 gst_decklink_duplex_mode_to_enum (BMDDuplexMode m)
542 {
543   guint i;
544 
545   for (i = 0; i < G_N_ELEMENTS (duplex_modes); i++) {
546     if (duplex_modes[i].mode == m)
547       return duplex_modes[i].gstmode;
548   }
549   g_assert_not_reached ();
550   return GST_DECKLINK_DUPLEX_MODE_HALF;
551 }
552 
553 const BMDKeyerMode
gst_decklink_keyer_mode_from_enum(GstDecklinkKeyerMode m)554 gst_decklink_keyer_mode_from_enum (GstDecklinkKeyerMode m)
555 {
556   return kmodes[m].keymode;
557 }
558 
559 const GstDecklinkKeyerMode
gst_decklink_keyer_mode_to_enum(BMDKeyerMode m)560 gst_decklink_keyer_mode_to_enum (BMDKeyerMode m)
561 {
562   guint i;
563 
564   for (i = 0; i < G_N_ELEMENTS (kmodes); i++) {
565     if (kmodes[i].keymode == m)
566       return (GstDecklinkKeyerMode) i;
567   }
568   g_assert_not_reached ();
569   return GST_DECKLINK_KEYER_MODE_OFF;
570 }
571 
572 static const BMDVideoConnection connections[] = {
573   (BMDVideoConnection) 0,       /* auto */
574   bmdVideoConnectionSDI,
575   bmdVideoConnectionHDMI,
576   bmdVideoConnectionOpticalSDI,
577   bmdVideoConnectionComponent,
578   bmdVideoConnectionComposite,
579   bmdVideoConnectionSVideo
580 };
581 
582 const BMDVideoConnection
gst_decklink_get_connection(GstDecklinkConnectionEnum e)583 gst_decklink_get_connection (GstDecklinkConnectionEnum e)
584 {
585   g_return_val_if_fail (e != GST_DECKLINK_CONNECTION_AUTO,
586       bmdVideoConnectionSDI);
587 
588   if (e <= GST_DECKLINK_CONNECTION_AUTO || e > GST_DECKLINK_CONNECTION_SVIDEO)
589     e = GST_DECKLINK_CONNECTION_SDI;
590 
591   return connections[e];
592 }
593 
594 static gboolean
gst_decklink_caps_get_pixel_format(GstCaps * caps,BMDPixelFormat * format)595 gst_decklink_caps_get_pixel_format (GstCaps * caps, BMDPixelFormat * format)
596 {
597   GstVideoInfo vinfo;
598   GstVideoFormat f;
599 
600   if (gst_video_info_from_caps (&vinfo, caps) == FALSE) {
601     GST_ERROR ("Could not get video info from caps: %" GST_PTR_FORMAT, caps);
602     return FALSE;
603   }
604 
605   f = vinfo.finfo->format;
606   return gst_decklink_type_from_video_format (f);
607 }
608 
609 static GstStructure *
gst_decklink_mode_get_structure(GstDecklinkModeEnum e,BMDPixelFormat f,gboolean input)610 gst_decklink_mode_get_structure (GstDecklinkModeEnum e, BMDPixelFormat f,
611     gboolean input)
612 {
613   const GstDecklinkMode *mode = &modes[e];
614   GstStructure *s = gst_structure_new ("video/x-raw",
615       "width", G_TYPE_INT, mode->width,
616       "height", G_TYPE_INT, mode->height,
617       "pixel-aspect-ratio", GST_TYPE_FRACTION, mode->par_n, mode->par_d,
618       "interlace-mode", G_TYPE_STRING,
619       mode->interlaced ? "interleaved" : "progressive",
620       "framerate", GST_TYPE_FRACTION, mode->fps_n, mode->fps_d, NULL);
621 
622   if (input && mode->interlaced) {
623     if (mode->tff)
624       gst_structure_set (s, "field-order", G_TYPE_STRING, "top-field-first",
625           NULL);
626     else
627       gst_structure_set (s, "field-order", G_TYPE_STRING, "bottom-field-first",
628           NULL);
629   }
630 
631   switch (f) {
632     case bmdFormat8BitYUV:     /* '2vuy' */
633       gst_structure_set (s, "format", G_TYPE_STRING, "UYVY",
634           "colorimetry", G_TYPE_STRING, mode->colorimetry,
635           "chroma-site", G_TYPE_STRING, "mpeg2", NULL);
636       break;
637     case bmdFormat10BitYUV:    /* 'v210' */
638       gst_structure_set (s, "format", G_TYPE_STRING, "v210", NULL);
639       break;
640     case bmdFormat8BitARGB:    /* 'ARGB' */
641       gst_structure_set (s, "format", G_TYPE_STRING, "ARGB", NULL);
642       break;
643     case bmdFormat8BitBGRA:    /* 'BGRA' */
644       gst_structure_set (s, "format", G_TYPE_STRING, "BGRA", NULL);
645       break;
646     case bmdFormat10BitRGB:    /* 'r210' Big-endian RGB 10-bit per component with SMPTE video levels (64-960). Packed as 2:10:10:10 */
647     case bmdFormat12BitRGB:    /* 'R12B' Big-endian RGB 12-bit per component with full range (0-4095). Packed as 12-bit per component */
648     case bmdFormat12BitRGBLE:  /* 'R12L' Little-endian RGB 12-bit per component with full range (0-4095). Packed as 12-bit per component */
649     case bmdFormat10BitRGBXLE: /* 'R10l' Little-endian 10-bit RGB with SMPTE video levels (64-940) */
650     case bmdFormat10BitRGBX:   /* 'R10b' Big-endian 10-bit RGB with SMPTE video levels (64-940) */
651     default:
652       GST_WARNING ("format not supported %d", f);
653       gst_structure_free (s);
654       s = NULL;
655       break;
656   }
657 
658   return s;
659 }
660 
661 GstCaps *
gst_decklink_mode_get_caps(GstDecklinkModeEnum e,BMDPixelFormat f,gboolean input)662 gst_decklink_mode_get_caps (GstDecklinkModeEnum e, BMDPixelFormat f,
663     gboolean input)
664 {
665   GstCaps *caps;
666 
667   caps = gst_caps_new_empty ();
668   caps =
669       gst_caps_merge_structure (caps, gst_decklink_mode_get_structure (e, f,
670           input));
671 
672   return caps;
673 }
674 
675 GstCaps *
gst_decklink_mode_get_caps_all_formats(GstDecklinkModeEnum e,gboolean input)676 gst_decklink_mode_get_caps_all_formats (GstDecklinkModeEnum e, gboolean input)
677 {
678   GstCaps *caps;
679   guint i;
680 
681   caps = gst_caps_new_empty ();
682   for (i = 1; i < G_N_ELEMENTS (formats); i++)
683     caps =
684         gst_caps_merge_structure (caps, gst_decklink_mode_get_structure (e,
685             formats[i].format, input));
686 
687   return caps;
688 }
689 
690 GstCaps *
gst_decklink_pixel_format_get_caps(BMDPixelFormat f,gboolean input)691 gst_decklink_pixel_format_get_caps (BMDPixelFormat f, gboolean input)
692 {
693   int i;
694   GstCaps *caps;
695   GstStructure *s;
696 
697   caps = gst_caps_new_empty ();
698   for (i = 1; i < (int) G_N_ELEMENTS (modes); i++) {
699     s = gst_decklink_mode_get_structure ((GstDecklinkModeEnum) i, f, input);
700     caps = gst_caps_merge_structure (caps, s);
701   }
702 
703   return caps;
704 }
705 
706 GstCaps *
gst_decklink_mode_get_template_caps(gboolean input)707 gst_decklink_mode_get_template_caps (gboolean input)
708 {
709   int i;
710   GstCaps *caps;
711 
712   caps = gst_caps_new_empty ();
713   for (i = 1; i < (int) G_N_ELEMENTS (modes); i++)
714     caps =
715         gst_caps_merge (caps,
716         gst_decklink_mode_get_caps_all_formats ((GstDecklinkModeEnum) i,
717             input));
718 
719   return caps;
720 }
721 
722 const GstDecklinkMode *
gst_decklink_find_mode_and_format_for_caps(GstCaps * caps,BMDPixelFormat * format)723 gst_decklink_find_mode_and_format_for_caps (GstCaps * caps,
724     BMDPixelFormat * format)
725 {
726   int i;
727   GstCaps *mode_caps;
728 
729   g_return_val_if_fail (gst_caps_is_fixed (caps), NULL);
730   if (!gst_decklink_caps_get_pixel_format (caps, format))
731     return NULL;
732 
733   for (i = 1; i < (int) G_N_ELEMENTS (modes); i++) {
734     mode_caps =
735         gst_decklink_mode_get_caps ((GstDecklinkModeEnum) i, *format, FALSE);
736     if (gst_caps_can_intersect (caps, mode_caps)) {
737       gst_caps_unref (mode_caps);
738       return gst_decklink_get_mode ((GstDecklinkModeEnum) i);
739     }
740     gst_caps_unref (mode_caps);
741   }
742 
743   return NULL;
744 }
745 
746 const GstDecklinkMode *
gst_decklink_find_mode_for_caps(GstCaps * caps)747 gst_decklink_find_mode_for_caps (GstCaps * caps)
748 {
749   BMDPixelFormat format;
750 
751   return gst_decklink_find_mode_and_format_for_caps (caps, &format);
752 }
753 
754 #define GST_TYPE_DECKLINK_CLOCK \
755   (gst_decklink_clock_get_type())
756 #define GST_DECKLINK_CLOCK(obj) \
757   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_DECKLINK_CLOCK,GstDecklinkClock))
758 #define GST_DECKLINK_CLOCK_CLASS(klass) \
759   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_DECKLINK_CLOCK,GstDecklinkClockClass))
760 #define GST_IS_Decklink_CLOCK(obj) \
761   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_DECKLINK_CLOCK))
762 #define GST_IS_Decklink_CLOCK_CLASS(klass) \
763   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_DECKLINK_CLOCK))
764 #define GST_DECKLINK_CLOCK_CAST(obj) \
765   ((GstDecklinkClock*)(obj))
766 
767 typedef struct _GstDecklinkClock GstDecklinkClock;
768 typedef struct _GstDecklinkClockClass GstDecklinkClockClass;
769 
770 struct _GstDecklinkClock
771 {
772   GstSystemClock clock;
773 
774   GstDecklinkOutput *output;
775 };
776 
777 struct _GstDecklinkClockClass
778 {
779   GstSystemClockClass parent_class;
780 };
781 
782 GType gst_decklink_clock_get_type (void);
783 static GstClock *gst_decklink_clock_new (const gchar * name);
784 
785 typedef struct _Device Device;
786 struct _Device
787 {
788   GstDecklinkOutput output;
789   GstDecklinkInput input;
790 };
791 
792 DuplexModeSetOperationResult gst_decklink_configure_duplex_mode (Device *
793     device, BMDDuplexMode duplex_mode);
794 DuplexModeSetOperationResult
795 gst_decklink_configure_duplex_mode_pair_device (Device * device,
796     BMDDuplexMode duplex_mode);
797 Device *gst_decklink_find_device_by_persistent_id (int64_t persistent_id);
798 gboolean gst_decklink_device_has_persistent_id (Device * device,
799     int64_t persistent_id);
800 
801 class GStreamerDecklinkInputCallback:public IDeckLinkInputCallback
802 {
803 private:
804   GstDecklinkInput * m_input;
805   GMutex m_mutex;
806   gint m_refcount;
807 public:
GStreamerDecklinkInputCallback(GstDecklinkInput * input)808     GStreamerDecklinkInputCallback (GstDecklinkInput * input)
809   : IDeckLinkInputCallback (), m_refcount (1)
810   {
811     m_input = input;
812     g_mutex_init (&m_mutex);
813   }
814 
~GStreamerDecklinkInputCallback()815   virtual ~ GStreamerDecklinkInputCallback ()
816   {
817     g_mutex_clear (&m_mutex);
818   }
819 
QueryInterface(REFIID,LPVOID *)820   virtual HRESULT STDMETHODCALLTYPE QueryInterface (REFIID, LPVOID *)
821   {
822     return E_NOINTERFACE;
823   }
824 
AddRef(void)825   virtual ULONG STDMETHODCALLTYPE AddRef (void)
826   {
827     ULONG ret;
828 
829     g_mutex_lock (&m_mutex);
830     m_refcount++;
831     ret = m_refcount;
832     g_mutex_unlock (&m_mutex);
833 
834     return ret;
835   }
836 
Release(void)837   virtual ULONG STDMETHODCALLTYPE Release (void)
838   {
839     ULONG ret;
840 
841     g_mutex_lock (&m_mutex);
842     m_refcount--;
843     ret = m_refcount;
844     g_mutex_unlock (&m_mutex);
845 
846 
847     if (ret == 0) {
848       delete this;
849     }
850 
851     return ret;
852   }
853 
854   virtual HRESULT STDMETHODCALLTYPE
VideoInputFormatChanged(BMDVideoInputFormatChangedEvents,IDeckLinkDisplayMode * mode,BMDDetectedVideoInputFormatFlags formatFlags)855       VideoInputFormatChanged (BMDVideoInputFormatChangedEvents,
856       IDeckLinkDisplayMode * mode, BMDDetectedVideoInputFormatFlags formatFlags)
857   {
858     BMDPixelFormat pixelFormat;
859 
860     GST_INFO ("Video input format changed");
861 
862     if ((formatFlags & bmdDetectedVideoInputRGB444)
863         && m_input->format == bmdFormat8BitYUV) {
864       /* user-set format was auto or 8BitYUV, change to RGB */
865       pixelFormat = bmdFormat8BitARGB;
866     } else {
867       /* use the user-set format, defaulting to 8BitYUV */
868       pixelFormat = m_input->format;
869     }
870 
871     g_mutex_lock (&m_input->lock);
872     m_input->input->PauseStreams ();
873     m_input->input->EnableVideoInput (mode->GetDisplayMode (),
874         pixelFormat, bmdVideoInputEnableFormatDetection);
875     m_input->input->FlushStreams ();
876     m_input->input->StartStreams ();
877     m_input->mode =
878         gst_decklink_get_mode (gst_decklink_get_mode_enum_from_bmd
879         (mode->GetDisplayMode ()));
880     m_input->format = pixelFormat;
881     g_mutex_unlock (&m_input->lock);
882 
883     return S_OK;
884   }
885 
886   virtual HRESULT STDMETHODCALLTYPE
VideoInputFrameArrived(IDeckLinkVideoInputFrame * video_frame,IDeckLinkAudioInputPacket * audio_packet)887       VideoInputFrameArrived (IDeckLinkVideoInputFrame * video_frame,
888       IDeckLinkAudioInputPacket * audio_packet)
889   {
890     GstElement *videosrc = NULL, *audiosrc = NULL;
891     void (*got_video_frame) (GstElement * videosrc,
892         IDeckLinkVideoInputFrame * frame, GstDecklinkModeEnum mode,
893         GstClockTime capture_time, GstClockTime stream_time,
894         GstClockTime stream_duration, GstClockTime hardware_time,
895         GstClockTime hardware_duration, IDeckLinkTimecode * dtc,
896         gboolean no_signal) = NULL;
897     void (*got_audio_packet) (GstElement * videosrc,
898         IDeckLinkAudioInputPacket * packet, GstClockTime capture_time,
899         GstClockTime stream_time, GstClockTime stream_duration,
900         GstClockTime hardware_time, GstClockTime hardware_duration,
901         gboolean no_signal) = NULL;
902     GstDecklinkModeEnum mode;
903     GstClockTime capture_time = GST_CLOCK_TIME_NONE;
904     GstClockTime base_time = 0;
905     gboolean no_signal = FALSE;
906     GstClock *clock = NULL;
907     HRESULT res;
908     BMDTimeValue stream_time = GST_CLOCK_TIME_NONE;
909     BMDTimeValue stream_duration = GST_CLOCK_TIME_NONE;
910     BMDTimeValue hardware_time = GST_CLOCK_TIME_NONE;
911     BMDTimeValue hardware_duration = GST_CLOCK_TIME_NONE;
912 
913     g_mutex_lock (&m_input->lock);
914     if (m_input->videosrc) {
915       videosrc = GST_ELEMENT_CAST (gst_object_ref (m_input->videosrc));
916       clock = gst_element_get_clock (videosrc);
917       base_time = gst_element_get_base_time (videosrc);
918       got_video_frame = m_input->got_video_frame;
919     }
920     mode = gst_decklink_get_mode_enum_from_bmd (m_input->mode->mode);
921 
922     if (m_input->audiosrc) {
923       audiosrc = GST_ELEMENT_CAST (gst_object_ref (m_input->audiosrc));
924       if (!clock) {
925         clock = gst_element_get_clock (GST_ELEMENT_CAST (audiosrc));
926         base_time = gst_element_get_base_time (audiosrc);
927       }
928       got_audio_packet = m_input->got_audio_packet;
929     }
930     g_mutex_unlock (&m_input->lock);
931 
932     if (clock) {
933       capture_time = gst_clock_get_time (clock);
934       if (capture_time > base_time)
935         capture_time -= base_time;
936       else
937         capture_time = 0;
938     }
939 
940     if (video_frame) {
941       BMDFrameFlags flags;
942 
943       flags = video_frame->GetFlags ();
944       if (flags & bmdFrameHasNoInputSource) {
945         no_signal = TRUE;
946       }
947     }
948 
949     if (got_video_frame && videosrc && video_frame) {
950       IDeckLinkTimecode *dtc = 0;
951 
952       res =
953           video_frame->GetStreamTime (&stream_time, &stream_duration,
954           GST_SECOND);
955       if (res != S_OK) {
956         GST_ERROR ("Failed to get stream time: 0x%08lx", (unsigned long) res);
957         stream_time = GST_CLOCK_TIME_NONE;
958         stream_duration = GST_CLOCK_TIME_NONE;
959       }
960 
961       res =
962           video_frame->GetHardwareReferenceTimestamp (GST_SECOND,
963           &hardware_time, &hardware_duration);
964       if (res != S_OK) {
965         GST_ERROR ("Failed to get hardware time: 0x%08lx", (unsigned long) res);
966         hardware_time = GST_CLOCK_TIME_NONE;
967         hardware_duration = GST_CLOCK_TIME_NONE;
968       }
969 
970       if (m_input->videosrc) {
971         /* FIXME: Avoid circularity between gstdecklink.cpp and
972          * gstdecklinkvideosrc.cpp */
973         res =
974             video_frame->
975             GetTimecode (GST_DECKLINK_VIDEO_SRC (videosrc)->timecode_format,
976             &dtc);
977 
978         if (res != S_OK) {
979           GST_DEBUG_OBJECT (videosrc, "Failed to get timecode: 0x%08lx",
980               (unsigned long) res);
981           dtc = NULL;
982         }
983       }
984 
985       /* passing dtc reference */
986       got_video_frame (videosrc, video_frame, mode, capture_time,
987           stream_time, stream_duration, hardware_time, hardware_duration, dtc,
988           no_signal);
989     }
990 
991     if (got_audio_packet && audiosrc && audio_packet) {
992       m_input->got_audio_packet (audiosrc, audio_packet, capture_time,
993           stream_time, stream_duration, hardware_time, hardware_duration,
994           no_signal);
995     } else {
996       if (!audio_packet)
997         GST_DEBUG ("Received no audio packet at %" GST_TIME_FORMAT,
998             GST_TIME_ARGS (capture_time));
999     }
1000 
1001     gst_object_replace ((GstObject **) & videosrc, NULL);
1002     gst_object_replace ((GstObject **) & audiosrc, NULL);
1003     gst_object_replace ((GstObject **) & clock, NULL);
1004 
1005     return S_OK;
1006   }
1007 };
1008 
1009 class GStreamerDecklinkMemoryAllocator:public IDeckLinkMemoryAllocator
1010 {
1011 private:
1012   GMutex m_mutex;
1013   uint32_t m_lastBufferSize;
1014   uint32_t m_nonEmptyCalls;
1015   GstQueueArray *m_buffers;
1016   gint m_refcount;
1017 
_clearBufferPool()1018   void _clearBufferPool ()
1019   {
1020     uint8_t *buf;
1021 
1022     if (!m_buffers)
1023         return;
1024 
1025     while ((buf = (uint8_t *) gst_queue_array_pop_head (m_buffers))) {
1026       uint8_t offset = *(buf - 1);
1027       void *alloc_buf = buf - 128 + offset;
1028       g_free (alloc_buf);
1029     }
1030   }
1031 
1032 public:
GStreamerDecklinkMemoryAllocator()1033     GStreamerDecklinkMemoryAllocator ()
1034   : IDeckLinkMemoryAllocator (),
1035       m_lastBufferSize (0),
1036       m_nonEmptyCalls (0), m_buffers (NULL), m_refcount (1)
1037   {
1038     g_mutex_init (&m_mutex);
1039 
1040     m_buffers = gst_queue_array_new (60);
1041   }
1042 
~GStreamerDecklinkMemoryAllocator()1043   virtual ~ GStreamerDecklinkMemoryAllocator () {
1044     Decommit ();
1045 
1046     gst_queue_array_free (m_buffers);
1047 
1048     g_mutex_clear (&m_mutex);
1049   }
1050 
QueryInterface(REFIID,LPVOID *)1051   virtual HRESULT STDMETHODCALLTYPE QueryInterface (REFIID, LPVOID *)
1052   {
1053     return E_NOINTERFACE;
1054   }
1055 
AddRef(void)1056   virtual ULONG STDMETHODCALLTYPE AddRef (void)
1057   {
1058     ULONG ret;
1059 
1060     g_mutex_lock (&m_mutex);
1061     m_refcount++;
1062     ret = m_refcount;
1063     g_mutex_unlock (&m_mutex);
1064 
1065     return ret;
1066   }
1067 
Release(void)1068   virtual ULONG STDMETHODCALLTYPE Release (void)
1069   {
1070     ULONG ret;
1071 
1072     g_mutex_lock (&m_mutex);
1073     m_refcount--;
1074     ret = m_refcount;
1075     g_mutex_unlock (&m_mutex);
1076 
1077 
1078     if (ret == 0) {
1079       delete this;
1080     }
1081 
1082     return ret;
1083   }
1084 
1085   virtual HRESULT STDMETHODCALLTYPE
AllocateBuffer(uint32_t bufferSize,void ** allocatedBuffer)1086       AllocateBuffer (uint32_t bufferSize, void **allocatedBuffer)
1087   {
1088     uint8_t *buf;
1089     uint8_t offset = 0;
1090 
1091     g_mutex_lock (&m_mutex);
1092 
1093     /* If buffer size changed since last call, empty buffer pool */
1094     if (bufferSize != m_lastBufferSize) {
1095       _clearBufferPool ();
1096       m_lastBufferSize = bufferSize;
1097     }
1098 
1099     /* Look if there is a free buffer in the pool */
1100     if (!(buf = (uint8_t *) gst_queue_array_pop_head (m_buffers))) {
1101       /* If not, alloc a new one */
1102       buf = (uint8_t *) g_malloc (bufferSize + 128);
1103 
1104       /* The Decklink SDK requires 16 byte aligned memory at least but for us
1105        * to work nicely let's align to 64 bytes (512 bits) as this allows
1106        * aligned AVX2 operations for example */
1107       if (((guintptr) buf) % 64 != 0) {
1108         offset = ((guintptr) buf) % 64;
1109       }
1110 
1111       /* Write the allocation size at the very beginning. It's guaranteed by
1112        * malloc() to be allocated aligned enough for doing this. */
1113       *((uint32_t *) buf) = bufferSize;
1114 
1115       /* Align our buffer */
1116       buf += 128 - offset;
1117 
1118       /* And write the alignment offset right before the buffer */
1119       *(buf - 1) = offset;
1120     }
1121     *allocatedBuffer = (void *) buf;
1122 
1123     /* If there are still unused buffers in the pool
1124      * remove one of them every fifth call */
1125     if (gst_queue_array_get_length (m_buffers) > 0) {
1126       if (++m_nonEmptyCalls >= 5) {
1127         buf = (uint8_t *) gst_queue_array_pop_head (m_buffers);
1128         uint8_t offset = *(buf - 1);
1129         void *alloc_buf = buf - 128 + offset;
1130         g_free (alloc_buf);
1131         m_nonEmptyCalls = 0;
1132       }
1133     } else {
1134       m_nonEmptyCalls = 0;
1135     }
1136 
1137     g_mutex_unlock (&m_mutex);
1138 
1139     return S_OK;
1140   }
1141 
ReleaseBuffer(void * buffer)1142   virtual HRESULT STDMETHODCALLTYPE ReleaseBuffer (void *buffer)
1143   {
1144     g_mutex_lock (&m_mutex);
1145 
1146     /* Put the buffer back to the pool if size matches with current pool */
1147     uint8_t offset = *(((uint8_t *) buffer) - 1);
1148     uint8_t *alloc_buffer = ((uint8_t *) buffer) - 128 + offset;
1149     uint32_t size = *(uint32_t *) alloc_buffer;
1150     if (size == m_lastBufferSize) {
1151       gst_queue_array_push_tail (m_buffers, buffer);
1152     } else {
1153       g_free (alloc_buffer);
1154     }
1155 
1156     g_mutex_unlock (&m_mutex);
1157 
1158     return S_OK;
1159   }
1160 
Commit()1161   virtual HRESULT STDMETHODCALLTYPE Commit ()
1162   {
1163     return S_OK;
1164   }
1165 
Decommit()1166   virtual HRESULT STDMETHODCALLTYPE Decommit ()
1167   {
1168     /* Clear all remaining pools */
1169     _clearBufferPool ();
1170 
1171     return S_OK;
1172   }
1173 };
1174 
1175 #ifdef G_OS_WIN32
1176 /* FIXME: We currently never deinit this */
1177 
1178 static GMutex com_init_lock;
1179 static GMutex com_deinit_lock;
1180 static GCond com_init_cond;
1181 static GCond com_deinit_cond;
1182 static GCond com_deinited_cond;
1183 static gboolean com_initialized = FALSE;
1184 
1185 /* COM initialization/uninitialization thread */
1186 static gpointer
gst_decklink_com_thread(gpointer data)1187 gst_decklink_com_thread (gpointer data)
1188 {
1189   HRESULT res;
1190 
1191   g_mutex_lock (&com_init_lock);
1192 
1193   /* Initialize COM with a MTA for this process. This thread will
1194    * be the first one to enter the apartement and the last one to leave
1195    * it, unitializing COM properly */
1196 
1197   res = CoInitializeEx (0, COINIT_MULTITHREADED);
1198   if (res == S_FALSE)
1199     GST_WARNING ("COM has been already initialized in the same process");
1200   else if (res == RPC_E_CHANGED_MODE)
1201     GST_WARNING ("The concurrency model of COM has changed.");
1202   else
1203     GST_INFO ("COM intialized succesfully");
1204 
1205   com_initialized = TRUE;
1206 
1207   /* Signal other threads waiting on this condition that COM was initialized */
1208   g_cond_signal (&com_init_cond);
1209 
1210   g_mutex_unlock (&com_init_lock);
1211 
1212   /* Wait until the unitialize condition is met to leave the COM apartement */
1213   g_mutex_lock (&com_deinit_lock);
1214   g_cond_wait (&com_deinit_cond, &com_deinit_lock);
1215 
1216   CoUninitialize ();
1217   GST_INFO ("COM unintialized succesfully");
1218   com_initialized = FALSE;
1219   g_cond_signal (&com_deinited_cond);
1220   g_mutex_unlock (&com_deinit_lock);
1221 
1222   return NULL;
1223 }
1224 #endif /* G_OS_WIN32 */
1225 
1226 static GOnce devices_once = G_ONCE_INIT;
1227 static GPtrArray *devices;      /* array of Device */
1228 
1229 static gpointer
init_devices(gpointer data)1230 init_devices (gpointer data)
1231 {
1232   IDeckLinkIterator *iterator;
1233   IDeckLink *decklink = NULL;
1234   HRESULT ret;
1235   int i;
1236 
1237 #ifdef G_OS_WIN32
1238   // Start COM thread for Windows
1239 
1240   g_mutex_lock (&com_init_lock);
1241 
1242   /* create the COM initialization thread */
1243   g_thread_new ("COM init thread", (GThreadFunc) gst_decklink_com_thread, NULL);
1244 
1245   /* wait until the COM thread signals that COM has been initialized */
1246   g_cond_wait (&com_init_cond, &com_init_lock);
1247   g_mutex_unlock (&com_init_lock);
1248 #endif /* G_OS_WIN32 */
1249 
1250   iterator = CreateDeckLinkIteratorInstance ();
1251   if (iterator == NULL) {
1252     GST_ERROR ("no driver");
1253     return NULL;
1254   }
1255 
1256   devices = g_ptr_array_new ();
1257 
1258   i = 0;
1259   ret = iterator->Next (&decklink);
1260   while (ret == S_OK) {
1261     Device *dev;
1262 
1263     dev = g_new0 (Device, 1);
1264 
1265     g_mutex_init (&dev->input.lock);
1266     g_mutex_init (&dev->output.lock);
1267     g_cond_init (&dev->output.cond);
1268 
1269     ret = decklink->QueryInterface (IID_IDeckLinkInput,
1270         (void **) &dev->input.input);
1271     if (ret != S_OK) {
1272       GST_WARNING ("selected device does not have input interface: 0x%08lx",
1273           (unsigned long) ret);
1274     } else {
1275       IDeckLinkDisplayModeIterator *mode_iter;
1276 
1277       dev->input.device = decklink;
1278       dev->input.input->
1279           SetCallback (new GStreamerDecklinkInputCallback (&dev->input));
1280 
1281       if ((ret = dev->input.input->GetDisplayModeIterator (&mode_iter)) == S_OK) {
1282         IDeckLinkDisplayMode *mode;
1283 
1284         GST_DEBUG ("Input %d supports:", i);
1285         while ((ret = mode_iter->Next (&mode)) == S_OK) {
1286           char *name;
1287 
1288           mode->GetName ((COMSTR_T *) & name);
1289           CONVERT_COM_STRING (name);
1290           GST_DEBUG ("    %s mode: 0x%08x width: %ld height: %ld"
1291               " fields: 0x%08x flags: 0x%08x", name,
1292               (int) mode->GetDisplayMode (), mode->GetWidth (),
1293               mode->GetHeight (), (int) mode->GetFieldDominance (),
1294               (int) mode->GetFlags ());
1295           FREE_COM_STRING (name);
1296           mode->Release ();
1297         }
1298         mode_iter->Release ();
1299       }
1300       ret = S_OK;
1301     }
1302 
1303     ret = decklink->QueryInterface (IID_IDeckLinkOutput,
1304         (void **) &dev->output.output);
1305     if (ret != S_OK) {
1306       GST_WARNING ("selected device does not have output interface: 0x%08lx",
1307           (unsigned long) ret);
1308     } else {
1309       IDeckLinkDisplayModeIterator *mode_iter;
1310 
1311       dev->output.device = decklink;
1312       dev->output.clock = gst_decklink_clock_new ("GstDecklinkOutputClock");
1313       GST_DECKLINK_CLOCK_CAST (dev->output.clock)->output = &dev->output;
1314 
1315       if ((ret =
1316               dev->output.output->GetDisplayModeIterator (&mode_iter)) ==
1317           S_OK) {
1318         IDeckLinkDisplayMode *mode;
1319 
1320         GST_DEBUG ("Output %d supports:", i);
1321         while ((ret = mode_iter->Next (&mode)) == S_OK) {
1322           char *name;
1323 
1324           mode->GetName ((COMSTR_T *) & name);
1325           CONVERT_COM_STRING (name);
1326           GST_DEBUG ("    %s mode: 0x%08x width: %ld height: %ld"
1327               " fields: 0x%08x flags: 0x%08x", name,
1328               (int) mode->GetDisplayMode (), mode->GetWidth (),
1329               mode->GetHeight (), (int) mode->GetFieldDominance (),
1330               (int) mode->GetFlags ());
1331           FREE_COM_STRING (name);
1332           mode->Release ();
1333         }
1334         mode_iter->Release ();
1335       }
1336       ret = S_OK;
1337     }
1338 
1339     ret = decklink->QueryInterface (IID_IDeckLinkConfiguration,
1340         (void **) &dev->input.config);
1341     if (ret != S_OK) {
1342       GST_WARNING ("selected device does not have config interface: 0x%08lx",
1343           (unsigned long) ret);
1344     } else {
1345       char *serial_number;
1346 
1347       ret =
1348           dev->input.
1349           config->GetString (bmdDeckLinkConfigDeviceInformationSerialNumber,
1350           (COMSTR_T *) & serial_number);
1351       if (ret == S_OK) {
1352         CONVERT_COM_STRING (serial_number);
1353         dev->output.hw_serial_number = g_strdup (serial_number);
1354         dev->input.hw_serial_number = g_strdup (serial_number);
1355         GST_DEBUG ("device %d has serial number %s", i, serial_number);
1356         FREE_COM_STRING (serial_number);
1357       }
1358     }
1359 
1360     ret = decklink->QueryInterface (IID_IDeckLinkAttributes,
1361         (void **) &dev->input.attributes);
1362     dev->output.attributes = dev->input.attributes;
1363     if (ret != S_OK) {
1364       GST_WARNING ("selected device does not have attributes interface: "
1365           "0x%08lx", (unsigned long) ret);
1366     }
1367 
1368     ret = decklink->QueryInterface (IID_IDeckLinkKeyer,
1369         (void **) &dev->output.keyer);
1370 
1371     g_ptr_array_add (devices, dev);
1372 
1373     /* We only warn of failure to obtain the keyer interface if the keyer
1374      * is enabled by keyer_mode
1375      */
1376 
1377     ret = iterator->Next (&decklink);
1378     i++;
1379   }
1380 
1381   GST_INFO ("Detected %u devices", devices->len);
1382 
1383   iterator->Release ();
1384 
1385   return NULL;
1386 }
1387 
1388 GstDecklinkOutput *
gst_decklink_acquire_nth_output(gint n,GstElement * sink,gboolean is_audio)1389 gst_decklink_acquire_nth_output (gint n, GstElement * sink, gboolean is_audio)
1390 {
1391   GstDecklinkOutput *output;
1392   Device *device;
1393 
1394   g_once (&devices_once, init_devices, NULL);
1395 
1396   if (devices == NULL)
1397     return NULL;
1398 
1399   if (n < 0 || (guint) n >= devices->len)
1400     return NULL;
1401 
1402   device = (Device *) g_ptr_array_index (devices, n);
1403   output = &device->output;
1404   if (!output->output) {
1405     GST_ERROR ("Device %d has no output", n);
1406     return NULL;
1407   }
1408 
1409   if (!is_audio) {
1410     GstDecklinkVideoSink *videosink = (GstDecklinkVideoSink *) (sink);
1411     if (gst_decklink_configure_duplex_mode (device,
1412             videosink->duplex_mode) == DUPLEX_MODE_SET_FAILURE) {
1413       return NULL;
1414     }
1415   }
1416 
1417   g_mutex_lock (&output->lock);
1418   if (is_audio && !output->audiosink) {
1419     output->audiosink = GST_ELEMENT_CAST (gst_object_ref (sink));
1420     g_mutex_unlock (&output->lock);
1421     return output;
1422   } else if (!output->videosink) {
1423     output->videosink = GST_ELEMENT_CAST (gst_object_ref (sink));
1424     g_mutex_unlock (&output->lock);
1425     return output;
1426   }
1427   g_mutex_unlock (&output->lock);
1428 
1429   GST_ERROR ("Output device %d (audio: %d) in use already", n, is_audio);
1430   return NULL;
1431 }
1432 
1433 void
gst_decklink_release_nth_output(gint n,GstElement * sink,gboolean is_audio)1434 gst_decklink_release_nth_output (gint n, GstElement * sink, gboolean is_audio)
1435 {
1436   GstDecklinkOutput *output;
1437   Device *device;
1438 
1439   if (devices == NULL)
1440     return;
1441 
1442   if (n < 0 || (guint) n >= devices->len)
1443     return;
1444 
1445   device = (Device *) g_ptr_array_index (devices, n);
1446   output = &device->output;
1447   g_assert (output->output);
1448 
1449   g_mutex_lock (&output->lock);
1450   if (is_audio) {
1451     g_assert (output->audiosink == sink);
1452     gst_object_unref (sink);
1453     output->audiosink = NULL;
1454   } else {
1455     g_assert (output->videosink == sink);
1456     gst_object_unref (sink);
1457     output->videosink = NULL;
1458   }
1459   g_mutex_unlock (&output->lock);
1460 }
1461 
1462 GstDecklinkInput *
gst_decklink_acquire_nth_input(gint n,GstElement * src,gboolean is_audio)1463 gst_decklink_acquire_nth_input (gint n, GstElement * src, gboolean is_audio)
1464 {
1465   GstDecklinkInput *input;
1466   Device *device;
1467 
1468   g_once (&devices_once, init_devices, NULL);
1469 
1470   if (devices == NULL)
1471     return NULL;
1472 
1473   if (n < 0 || (guint) n >= devices->len)
1474     return NULL;
1475 
1476   device = (Device *) g_ptr_array_index (devices, n);
1477   input = &device->input;
1478   if (!input->input) {
1479     GST_ERROR ("Device %d has no input", n);
1480     return NULL;
1481   }
1482 
1483   if (!is_audio) {
1484     GstDecklinkVideoSrc *videosrc = (GstDecklinkVideoSrc *) (src);
1485     if (gst_decklink_configure_duplex_mode (device,
1486             videosrc->duplex_mode) == DUPLEX_MODE_SET_FAILURE) {
1487       return NULL;
1488     }
1489   }
1490   g_mutex_lock (&input->lock);
1491   input->input->SetVideoInputFrameMemoryAllocator (new
1492       GStreamerDecklinkMemoryAllocator);
1493   if (is_audio && !input->audiosrc) {
1494     input->audiosrc = GST_ELEMENT_CAST (gst_object_ref (src));
1495     g_mutex_unlock (&input->lock);
1496     return input;
1497   } else if (!input->videosrc) {
1498     input->videosrc = GST_ELEMENT_CAST (gst_object_ref (src));
1499     g_mutex_unlock (&input->lock);
1500     return input;
1501   }
1502   g_mutex_unlock (&input->lock);
1503 
1504   GST_ERROR ("Input device %d (audio: %d) in use already", n, is_audio);
1505   return NULL;
1506 }
1507 
1508 void
gst_decklink_release_nth_input(gint n,GstElement * src,gboolean is_audio)1509 gst_decklink_release_nth_input (gint n, GstElement * src, gboolean is_audio)
1510 {
1511   GstDecklinkInput *input;
1512   Device *device;
1513 
1514   if (devices == NULL)
1515     return;
1516 
1517   if (n < 0 || (guint) n >= devices->len)
1518     return;
1519 
1520   device = (Device *) g_ptr_array_index (devices, n);
1521 
1522   input = &device->input;
1523   g_assert (input->input);
1524 
1525   g_mutex_lock (&input->lock);
1526   if (is_audio) {
1527     g_assert (input->audiosrc == src);
1528     gst_object_unref (src);
1529     input->audiosrc = NULL;
1530   } else {
1531     g_assert (input->videosrc == src);
1532     gst_object_unref (src);
1533     input->videosrc = NULL;
1534   }
1535   g_mutex_unlock (&input->lock);
1536 }
1537 
1538 /**
1539  * Probes if duplex-mode is supported and sets it accordingly. I duplex-mode is not supported
1540  * but this device is part of a pair (Duo2- and Quad2-Cards) and Half-Dupley-Mode is requested,
1541  * the parent device is also checked and configured accordingly.
1542  *
1543  * If
1544  *  - full-duplex-mode is requsted and the device does not support it *or*
1545  *  - half-duplex-mode is requested and there is not parent-device *or*
1546  *  - half-duplex-mode is requested and neither the device nor the parent device does support setting
1547  *    the duplex-mode, DUPLEX_MODE_SET_UNSUPPORTED is returnded.
1548  * If the device does support duplex-mode and setting it succeeded, DUPLEX_MODE_SET_SUCCESS is rerturned.
1549  * If
1550  *  - the device does support duplex-mode and setting it failed *or*
1551  *  - the Device reported a pair-device that does not exist in the system,
1552  *    DUPLEX_MODE_SET_FAILURE is returned.
1553  */
1554 DuplexModeSetOperationResult
gst_decklink_configure_duplex_mode(Device * device,BMDDuplexMode duplex_mode)1555 gst_decklink_configure_duplex_mode (Device * device, BMDDuplexMode duplex_mode)
1556 {
1557   HRESULT result;
1558   bool duplex_supported;
1559   int64_t paired_device_id;
1560 
1561   GstDecklinkInput *input = &device->input;
1562 
1563   result =
1564       input->attributes->GetFlag (BMDDeckLinkSupportsDuplexModeConfiguration,
1565       &duplex_supported);
1566   if (result != S_OK) {
1567     duplex_supported = false;
1568   }
1569 
1570   if (!duplex_supported) {
1571     if (duplex_mode == bmdDuplexModeFull) {
1572       GST_DEBUG ("Device does not support Full-Duplex-Mode");
1573       return DUPLEX_MODE_SET_UNSUPPORTED;
1574     } else if (duplex_mode == bmdDuplexModeHalf) {
1575       result =
1576           input->attributes->GetInt (BMDDeckLinkPairedDevicePersistentID,
1577           &paired_device_id);
1578 
1579       if (result == S_OK) {
1580         GST_DEBUG ("Device does not support Half-Duplex-Mode but the Device is "
1581             "a Part of a Device-Pair, trying to set Half-Duplex-Mode "
1582             "on the Parent-Device");
1583 
1584         Device *pair_device =
1585             gst_decklink_find_device_by_persistent_id (paired_device_id);
1586         if (pair_device == NULL) {
1587           GST_ERROR ("Device reported as Pair-Device does not exist");
1588           return DUPLEX_MODE_SET_FAILURE;
1589         }
1590         return gst_decklink_configure_duplex_mode_pair_device (pair_device,
1591             duplex_mode);
1592       } else {
1593         GST_DEBUG ("Device does not support Half-Duplex-Mode");
1594         return DUPLEX_MODE_SET_SUCCESS;
1595       }
1596     } else {
1597       GST_ERROR ("duplex_mode=%d", duplex_mode);
1598       g_assert_not_reached ();
1599     }
1600   } else {
1601     GST_DEBUG ("Setting duplex-mode of Device");
1602     result = input->config->SetInt (bmdDeckLinkConfigDuplexMode, duplex_mode);
1603 
1604     if (result == S_OK) {
1605       GST_DEBUG ("Duplex mode set successful");
1606       return DUPLEX_MODE_SET_SUCCESS;
1607     } else {
1608       GST_ERROR ("Setting duplex mode failed");
1609       return DUPLEX_MODE_SET_FAILURE;
1610     }
1611   }
1612 }
1613 
1614 DuplexModeSetOperationResult
gst_decklink_configure_duplex_mode_pair_device(Device * device,BMDDuplexMode duplex_mode)1615 gst_decklink_configure_duplex_mode_pair_device (Device * device,
1616     BMDDuplexMode duplex_mode)
1617 {
1618   HRESULT result;
1619   bool duplex_supported;
1620 
1621   GstDecklinkInput *input = &device->input;
1622 
1623   result =
1624       input->attributes->GetFlag (BMDDeckLinkSupportsDuplexModeConfiguration,
1625       &duplex_supported);
1626   if (result != S_OK) {
1627     duplex_supported = false;
1628   }
1629 
1630   if (!duplex_supported) {
1631     GST_DEBUG ("Pair-Device does not support Duplex-Mode");
1632     return DUPLEX_MODE_SET_UNSUPPORTED;
1633   }
1634 
1635   GST_DEBUG ("Setting duplex-mode of Pair-Device");
1636   result = input->config->SetInt (bmdDeckLinkConfigDuplexMode, duplex_mode);
1637 
1638   if (result == S_OK) {
1639     GST_DEBUG ("Duplex mode set successful");
1640     return DUPLEX_MODE_SET_SUCCESS;
1641   } else {
1642     GST_ERROR ("Setting duplex mode failed");
1643     return DUPLEX_MODE_SET_FAILURE;
1644   }
1645 }
1646 
1647 gboolean
gst_decklink_device_has_persistent_id(Device * device,int64_t persistent_id)1648 gst_decklink_device_has_persistent_id (Device * device, int64_t persistent_id)
1649 {
1650   HRESULT result;
1651   int64_t this_device_persistent_id;
1652 
1653   GstDecklinkInput *input = &device->input;
1654 
1655   result =
1656       input->attributes->GetInt (BMDDeckLinkPersistentID,
1657       &this_device_persistent_id);
1658   return (result == S_OK) && (this_device_persistent_id == persistent_id);
1659 }
1660 
1661 Device *
gst_decklink_find_device_by_persistent_id(int64_t persistent_id)1662 gst_decklink_find_device_by_persistent_id (int64_t persistent_id)
1663 {
1664   GST_DEBUG ("Searching Device by persistent ID %" G_GINT64_FORMAT,
1665       persistent_id);
1666 
1667   for (guint index = 0; index < devices->len; index++) {
1668     Device *device = (Device *) g_ptr_array_index (devices, index);
1669 
1670     if (gst_decklink_device_has_persistent_id (device, persistent_id)) {
1671       GST_DEBUG ("Found matching Device %u", index);
1672       return device;
1673     }
1674   }
1675 
1676   return NULL;
1677 }
1678 
1679 G_DEFINE_TYPE (GstDecklinkClock, gst_decklink_clock, GST_TYPE_SYSTEM_CLOCK);
1680 
1681 static GstClockTime gst_decklink_clock_get_internal_time (GstClock * clock);
1682 
1683 static void
gst_decklink_clock_class_init(GstDecklinkClockClass * klass)1684 gst_decklink_clock_class_init (GstDecklinkClockClass * klass)
1685 {
1686   GstClockClass *clock_class = (GstClockClass *) klass;
1687 
1688   clock_class->get_internal_time = gst_decklink_clock_get_internal_time;
1689 }
1690 
1691 static void
gst_decklink_clock_init(GstDecklinkClock * clock)1692 gst_decklink_clock_init (GstDecklinkClock * clock)
1693 {
1694   GST_OBJECT_FLAG_SET (clock, GST_CLOCK_FLAG_CAN_SET_MASTER);
1695 }
1696 
1697 static GstClock *
gst_decklink_clock_new(const gchar * name)1698 gst_decklink_clock_new (const gchar * name)
1699 {
1700   GstDecklinkClock *self =
1701       GST_DECKLINK_CLOCK (g_object_new (GST_TYPE_DECKLINK_CLOCK, "name", name,
1702           "clock-type", GST_CLOCK_TYPE_OTHER, NULL));
1703 
1704   gst_object_ref_sink (self);
1705 
1706   return GST_CLOCK_CAST (self);
1707 }
1708 
1709 static GstClockTime
gst_decklink_clock_get_internal_time(GstClock * clock)1710 gst_decklink_clock_get_internal_time (GstClock * clock)
1711 {
1712   GstDecklinkClock *self = GST_DECKLINK_CLOCK (clock);
1713   GstClockTime result, start_time, last_time;
1714   GstClockTimeDiff offset;
1715   BMDTimeValue time;
1716   HRESULT ret;
1717 
1718   g_mutex_lock (&self->output->lock);
1719   start_time = self->output->clock_start_time;
1720   offset = self->output->clock_offset;
1721   last_time = self->output->clock_last_time;
1722   time = -1;
1723   if (!self->output->started) {
1724     result = last_time;
1725     ret = -1;
1726   } else {
1727     ret =
1728         self->output->output->GetHardwareReferenceClock (GST_SECOND, &time,
1729         NULL, NULL);
1730     if (ret == S_OK && time >= 0) {
1731       result = time;
1732 
1733       if (start_time == GST_CLOCK_TIME_NONE)
1734         start_time = self->output->clock_start_time = result;
1735 
1736       if (result > start_time)
1737         result -= start_time;
1738       else
1739         result = 0;
1740 
1741       if (self->output->clock_restart) {
1742         self->output->clock_offset = result - last_time;
1743         offset = self->output->clock_offset;
1744         self->output->clock_restart = FALSE;
1745       }
1746       result = MAX (last_time, result);
1747       result -= offset;
1748       result = MAX (last_time, result);
1749     } else {
1750       result = last_time;
1751     }
1752 
1753     self->output->clock_last_time = result;
1754   }
1755   result += self->output->clock_epoch;
1756   g_mutex_unlock (&self->output->lock);
1757 
1758   GST_LOG_OBJECT (clock,
1759       "result %" GST_TIME_FORMAT " time %" GST_TIME_FORMAT " last time %"
1760       GST_TIME_FORMAT " offset %" GST_TIME_FORMAT " start time %"
1761       GST_TIME_FORMAT " (ret: 0x%08lx)", GST_TIME_ARGS (result),
1762       GST_TIME_ARGS (time), GST_TIME_ARGS (last_time), GST_TIME_ARGS (offset),
1763       GST_TIME_ARGS (start_time), (unsigned long) ret);
1764 
1765   return result;
1766 }
1767 
1768 static gboolean
plugin_init(GstPlugin * plugin)1769 plugin_init (GstPlugin * plugin)
1770 {
1771   GST_DEBUG_CATEGORY_INIT (gst_decklink_debug, "decklink", 0,
1772       "debug category for decklink plugin");
1773 
1774   gst_element_register (plugin, "decklinkaudiosink", GST_RANK_NONE,
1775       GST_TYPE_DECKLINK_AUDIO_SINK);
1776   gst_element_register (plugin, "decklinkvideosink", GST_RANK_NONE,
1777       GST_TYPE_DECKLINK_VIDEO_SINK);
1778   gst_element_register (plugin, "decklinkaudiosrc", GST_RANK_NONE,
1779       GST_TYPE_DECKLINK_AUDIO_SRC);
1780   gst_element_register (plugin, "decklinkvideosrc", GST_RANK_NONE,
1781       GST_TYPE_DECKLINK_VIDEO_SRC);
1782   return TRUE;
1783 }
1784 
1785 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1786     GST_VERSION_MINOR,
1787     decklink,
1788     "Blackmagic Decklink plugin",
1789     plugin_init, VERSION, "LGPL", PACKAGE_NAME, GST_PACKAGE_ORIGIN)
1790