Merge remote-tracking branch 'origin/0.10'
[platform/upstream/gstreamer.git] / sys / decklink / gstdecklink.cpp
1 /* GStreamer
2  * Copyright (C) 2011 David Schleef <ds@schleef.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Suite 500,
17  * Boston, MA 02110-1335, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <gst/gst.h>
25 #include "gstdecklink.h"
26 #include "gstdecklinksrc.h"
27 #include "gstdecklinksink.h"
28
29 GType
30 gst_decklink_mode_get_type (void)
31 {
32   static GType type;
33
34   if (!type) {
35     static const GEnumValue modes[] = {
36       {GST_DECKLINK_MODE_NTSC, "ntsc", "NTSC SD 60i"},
37       {GST_DECKLINK_MODE_NTSC2398, "ntsc2398", "NTSC SD 60i (24 fps)"},
38       {GST_DECKLINK_MODE_PAL, "pal", "PAL SD 50i"},
39       {GST_DECKLINK_MODE_NTSC_P, "ntsc-p", "NTSC SD 60p"},
40       {GST_DECKLINK_MODE_PAL_P, "pal-p", "PAL SD 50p"},
41
42       {GST_DECKLINK_MODE_1080p2398, "1080p2398", "HD1080 23.98p"},
43       {GST_DECKLINK_MODE_1080p24, "1080p24", "HD1080 24p"},
44       {GST_DECKLINK_MODE_1080p25, "1080p25", "HD1080 25p"},
45       {GST_DECKLINK_MODE_1080p2997, "1080p2997", "HD1080 29.97p"},
46       {GST_DECKLINK_MODE_1080p30, "1080p30", "HD1080 30p"},
47
48       {GST_DECKLINK_MODE_1080i50, "1080i50", "HD1080 50i"},
49       {GST_DECKLINK_MODE_1080i5994, "1080i5994", "HD1080 59.94i"},
50       {GST_DECKLINK_MODE_1080i60, "1080i60", "HD1080 60i"},
51
52       {GST_DECKLINK_MODE_1080p50, "1080p50", "HD1080 50p"},
53       {GST_DECKLINK_MODE_1080p5994, "1080p5994", "HD1080 59.94p"},
54       {GST_DECKLINK_MODE_1080p60, "1080p60", "HD1080 60p"},
55
56       {GST_DECKLINK_MODE_720p50, "720p50", "HD720 50p"},
57       {GST_DECKLINK_MODE_720p5994, "720p5994", "HD720 59.94p"},
58       {GST_DECKLINK_MODE_720p60, "720p60", "HD720 60p"},
59
60       {0, NULL, NULL}
61     };
62
63     type = g_enum_register_static ("GstDecklinkModes", modes);
64   }
65   return type;
66 }
67
68 GType
69 gst_decklink_connection_get_type (void)
70 {
71   static GType type;
72
73   if (!type) {
74     static const GEnumValue connections[] = {
75       {GST_DECKLINK_CONNECTION_SDI, "sdi", "SDI"},
76       {GST_DECKLINK_CONNECTION_HDMI, "hdmi", "HDMI"},
77       {GST_DECKLINK_CONNECTION_OPTICAL_SDI, "optical-sdi", "Optical SDI"},
78       {GST_DECKLINK_CONNECTION_COMPONENT, "component", "Component"},
79       {GST_DECKLINK_CONNECTION_COMPOSITE, "composite", "Composite"},
80       {GST_DECKLINK_CONNECTION_SVIDEO, "svideo", "S-Video"},
81       {0, NULL, NULL}
82     };
83
84     type = g_enum_register_static ("GstDecklinkConnection", connections);
85   }
86   return type;
87 }
88
89 GType
90 gst_decklink_audio_connection_get_type (void)
91 {
92   static GType type;
93
94   if (!type) {
95     static const GEnumValue connections[] = {
96       {GST_DECKLINK_AUDIO_CONNECTION_AUTO, "auto", "Automatic"},
97       {GST_DECKLINK_AUDIO_CONNECTION_EMBEDDED, "embedded",
98           "SDI/HDMI embedded audio"},
99       {GST_DECKLINK_AUDIO_CONNECTION_AES_EBU, "aes", "AES/EBU input"},
100       {GST_DECKLINK_AUDIO_CONNECTION_ANALOG, "analog", "Analog input"},
101       {0, NULL, NULL}
102     };
103
104     type = g_enum_register_static ("GstDecklinkAudioConnection", connections);
105   }
106   return type;
107 }
108
109 #define NTSC 10, 11, false, false
110 #define PAL 12, 11, true, false
111 #define HD 1, 1, false, true
112
113 static const GstDecklinkMode modes[] = {
114   {bmdModeNTSC, 720, 486, 30000, 1001, true, NTSC},
115   {bmdModeNTSC2398, 720, 486, 24000, 1001, true, NTSC},
116   {bmdModePAL, 720, 576, 25, 1, true, PAL},
117   {bmdModeNTSCp, 720, 486, 30000, 1001, false, NTSC},
118   {bmdModePALp, 720, 576, 25, 1, false, PAL},
119
120   {bmdModeHD1080p2398, 1920, 1080, 24000, 1001, false, HD},
121   {bmdModeHD1080p24, 1920, 1080, 24, 1, false, HD},
122   {bmdModeHD1080p25, 1920, 1080, 25, 1, false, HD},
123   {bmdModeHD1080p2997, 1920, 1080, 30000, 1001, false, HD},
124   {bmdModeHD1080p30, 1920, 1080, 30, 1, false, HD},
125
126   {bmdModeHD1080i50, 1920, 1080, 25, 1, true, HD},
127   {bmdModeHD1080i5994, 1920, 1080, 30000, 1001, true, HD},
128   {bmdModeHD1080i6000, 1920, 1080, 30, 1, true, HD},
129
130   {bmdModeHD1080p50, 1920, 1080, 50, 1, false, HD},
131   {bmdModeHD1080p5994, 1920, 1080, 30000, 1001, false, HD},
132   {bmdModeHD1080p6000, 1920, 1080, 60, 1, false, HD},
133
134   {bmdModeHD720p50, 1280, 720, 50, 1, false, HD},
135   {bmdModeHD720p5994, 1280, 720, 60000, 1001, false, HD},
136   {bmdModeHD720p60, 1280, 720, 60, 1, false, HD}
137
138 };
139
140 const GstDecklinkMode *
141 gst_decklink_get_mode (GstDecklinkModeEnum e)
142 {
143   return &modes[e];
144 }
145
146 static GstStructure *
147 gst_decklink_mode_get_structure (GstDecklinkModeEnum e)
148 {
149   const GstDecklinkMode *mode = &modes[e];
150
151   return gst_structure_new ("video/x-raw-yuv",
152       "format", GST_TYPE_FOURCC, GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'),
153       "width", G_TYPE_INT, mode->width,
154       "height", G_TYPE_INT, mode->height,
155       "framerate", GST_TYPE_FRACTION, mode->fps_n, mode->fps_d,
156       "interlaced", G_TYPE_BOOLEAN, mode->interlaced,
157       "pixel-aspect-ratio", GST_TYPE_FRACTION, mode->par_n, mode->par_d,
158       "color-matrix", G_TYPE_STRING, mode->is_hdtv ? "hdtv" : "sdtv",
159       "chroma-site", G_TYPE_STRING, "mpeg2", NULL);
160 }
161
162 GstCaps *
163 gst_decklink_mode_get_caps (GstDecklinkModeEnum e)
164 {
165   GstCaps *caps;
166
167   caps = gst_caps_new_empty ();
168   gst_caps_append_structure (caps, gst_decklink_mode_get_structure (e));
169
170   return caps;
171 }
172
173 GstCaps *
174 gst_decklink_mode_get_template_caps (void)
175 {
176   int i;
177   GstCaps *caps;
178   GstStructure *s;
179
180   caps = gst_caps_new_empty ();
181   for (i = 0; i < (int) G_N_ELEMENTS (modes); i++) {
182     s = gst_decklink_mode_get_structure ((GstDecklinkModeEnum) i);
183     gst_caps_append_structure (caps, s);
184   }
185
186   return caps;
187 }
188
189 IDeckLink *
190 gst_decklink_get_nth_device (int n)
191 {
192   IDeckLinkIterator *iterator;
193   IDeckLink *decklink = NULL;
194   HRESULT ret;
195   int i;
196
197   iterator = CreateDeckLinkIteratorInstance ();
198   if (iterator == NULL) {
199     GST_ERROR ("no driver");
200     return NULL;
201   }
202
203   ret = iterator->Next (&decklink);
204   if (ret != S_OK) {
205     GST_ERROR ("no card");
206     return NULL;
207   }
208   for (i = 0; i < n; i++) {
209     ret = iterator->Next (&decklink);
210     if (ret != S_OK) {
211       GST_ERROR ("no card");
212       return NULL;
213     }
214   }
215
216   return decklink;
217 }
218
219 static gboolean
220 plugin_init (GstPlugin * plugin)
221 {
222
223   gst_element_register (plugin, "decklinksrc", GST_RANK_NONE,
224       gst_decklink_src_get_type ());
225   gst_element_register (plugin, "decklinksink", GST_RANK_NONE,
226       gst_decklink_sink_get_type ());
227
228   return TRUE;
229 }
230
231 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
232     GST_VERSION_MINOR,
233     decklink,
234     "Blackmagic Decklink plugin",
235     plugin_init, VERSION, "LGPL", PACKAGE_NAME, GST_PACKAGE_ORIGIN)