gstdecklink.cpp 43.1 KB
Newer Older
David Schleef's avatar
David Schleef committed
1
2
/* GStreamer
 * 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>
David Schleef's avatar
David Schleef committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Suite 500,
 * Boston, MA 02110-1335, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gst/gst.h>
David Schleef's avatar
David Schleef committed
27
#include "gstdecklink.h"
28
29
#include "gstdecklinkaudiosink.h"
#include "gstdecklinkvideosink.h"
30
31
#include "gstdecklinkaudiosrc.h"
#include "gstdecklinkvideosrc.h"
32
33
34

GST_DEBUG_CATEGORY_STATIC (gst_decklink_debug);
#define GST_CAT_DEFAULT gst_decklink_debug
David Schleef's avatar
David Schleef committed
35

David Schleef's avatar
David Schleef committed
36
37
38
GType
gst_decklink_mode_get_type (void)
{
39
40
  static gsize id = 0;
  static const GEnumValue modes[] = {
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
    {GST_DECKLINK_MODE_AUTO, "Automatic detection", "auto"},

    {GST_DECKLINK_MODE_NTSC, "NTSC SD 60i", "ntsc"},
    {GST_DECKLINK_MODE_NTSC2398, "NTSC SD 60i (24 fps)", "ntsc2398"},
    {GST_DECKLINK_MODE_PAL, "PAL SD 50i", "pal"},
    {GST_DECKLINK_MODE_NTSC_P, "NTSC SD 60p", "ntsc-p"},
    {GST_DECKLINK_MODE_PAL_P, "PAL SD 50p", "pal-p"},

    {GST_DECKLINK_MODE_1080p2398, "HD1080 23.98p", "1080p2398"},
    {GST_DECKLINK_MODE_1080p24, "HD1080 24p", "1080p24"},
    {GST_DECKLINK_MODE_1080p25, "HD1080 25p", "1080p25"},
    {GST_DECKLINK_MODE_1080p2997, "HD1080 29.97p", "1080p2997"},
    {GST_DECKLINK_MODE_1080p30, "HD1080 30p", "1080p30"},

    {GST_DECKLINK_MODE_1080i50, "HD1080 50i", "1080i50"},
    {GST_DECKLINK_MODE_1080i5994, "HD1080 59.94i", "1080i5994"},
    {GST_DECKLINK_MODE_1080i60, "HD1080 60i", "1080i60"},

    {GST_DECKLINK_MODE_1080p50, "HD1080 50p", "1080p50"},
    {GST_DECKLINK_MODE_1080p5994, "HD1080 59.94p", "1080p5994"},
    {GST_DECKLINK_MODE_1080p60, "HD1080 60p", "1080p60"},

    {GST_DECKLINK_MODE_720p50, "HD720 50p", "720p50"},
    {GST_DECKLINK_MODE_720p5994, "HD720 59.94p", "720p5994"},
    {GST_DECKLINK_MODE_720p60, "HD720 60p", "720p60"},

    {GST_DECKLINK_MODE_1556p2398, "2k 23.98p", "1556p2398"},
    {GST_DECKLINK_MODE_1556p24, "2k 24p", "1556p24"},
    {GST_DECKLINK_MODE_1556p25, "2k 25p", "1556p25"},

    {GST_DECKLINK_MODE_2160p2398, "4k 23.98p", "2160p2398"},
    {GST_DECKLINK_MODE_2160p24, "4k 24p", "2160p24"},
    {GST_DECKLINK_MODE_2160p25, "4k 25p", "2160p25"},
    {GST_DECKLINK_MODE_2160p2997, "4k 29.97p", "2160p2997"},
    {GST_DECKLINK_MODE_2160p30, "4k 30p", "2160p30"},
    {GST_DECKLINK_MODE_2160p50, "4k 50p", "2160p50"},
    {GST_DECKLINK_MODE_2160p5994, "4k 59.94p", "2160p5994"},
    {GST_DECKLINK_MODE_2160p60, "4k 60p", "2160p60"},
79

80
81
82
83
84
85
    {0, NULL, NULL}
  };

  if (g_once_init_enter (&id)) {
    GType tmp = g_enum_register_static ("GstDecklinkModes", modes);
    g_once_init_leave (&id, tmp);
David Schleef's avatar
David Schleef committed
86
  }
87
88

  return (GType) id;
David Schleef's avatar
David Schleef committed
89
90
}

David Schleef's avatar
David Schleef committed
91
92
93
GType
gst_decklink_connection_get_type (void)
{
94
95
  static gsize id = 0;
  static const GEnumValue connections[] = {
96
97
98
99
100
101
102
    {GST_DECKLINK_CONNECTION_AUTO, "Auto", "auto"},
    {GST_DECKLINK_CONNECTION_SDI, "SDI", "sdi"},
    {GST_DECKLINK_CONNECTION_HDMI, "HDMI", "hdmi"},
    {GST_DECKLINK_CONNECTION_OPTICAL_SDI, "Optical SDI", "optical-sdi"},
    {GST_DECKLINK_CONNECTION_COMPONENT, "Component", "component"},
    {GST_DECKLINK_CONNECTION_COMPOSITE, "Composite", "composite"},
    {GST_DECKLINK_CONNECTION_SVIDEO, "S-Video", "svideo"},
103
104
105
106
107
108
    {0, NULL, NULL}
  };

  if (g_once_init_enter (&id)) {
    GType tmp = g_enum_register_static ("GstDecklinkConnection", connections);
    g_once_init_leave (&id, tmp);
David Schleef's avatar
David Schleef committed
109
  }
110
111

  return (GType) id;
David Schleef's avatar
David Schleef committed
112
113
}

114
115
116
117
118
GType
gst_decklink_video_format_get_type (void)
{
  static gsize id = 0;
  static const GEnumValue types[] = {
119
120
121
122
123
124
125
126
127
128
129
130
    {GST_DECKLINK_VIDEO_FORMAT_AUTO, "Auto", "auto"},
    {GST_DECKLINK_VIDEO_FORMAT_8BIT_YUV, "bmdFormat8BitYUV", "8bit-yuv"},
    {GST_DECKLINK_VIDEO_FORMAT_10BIT_YUV, "bmdFormat10BitYUV", "10bit-yuv"},
    {GST_DECKLINK_VIDEO_FORMAT_8BIT_ARGB, "bmdFormat8BitARGB", "8bit-argb"},
    {GST_DECKLINK_VIDEO_FORMAT_8BIT_BGRA, "bmdFormat8BitBGRA", "8bit-bgra"},
    /* Not yet supported:
       {GST_DECKLINK_VIDEO_FORMAT_10BIT_RGB, "bmdFormat10BitRGB", "10bit-rgb"},
       {GST_DECKLINK_VIDEO_FORMAT_12BIT_RGB, "bmdFormat12BitRGB", "12bit-rgb"},
       {GST_DECKLINK_VIDEO_FORMAT_12BIT_RGBLE, "bmdFormat12BitRGBLE", "12bit-rgble"},
       {GST_DECKLINK_VIDEO_FORMAT_10BIT_RGBXLE, "bmdFormat10BitRGBXLE", "10bit-rgbxle"},
       {GST_DECKLINK_VIDEO_FORMAT_10BIT_RGBX, "bmdFormat10BitRGBX", "10bit-rgbx"},
     */
131
132
133
134
135
136
137
138
139
140
141
    {0, NULL, NULL}
  };

  if (g_once_init_enter (&id)) {
    GType tmp = g_enum_register_static ("GstDecklinkVideoFormat", types);
    g_once_init_leave (&id, tmp);
  }

  return (GType) id;
}

142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
GType
gst_decklink_timecode_format_get_type (void)
{
  static gsize id = 0;
  static const GEnumValue timecodeformats[] = {
    {GST_DECKLINK_TIMECODE_FORMAT_RP188VITC1, "bmdTimecodeRP188VITC1",
        "rp188vitc1"},
    {GST_DECKLINK_TIMECODE_FORMAT_RP188VITC2, "bmdTimecodeRP188VITC2",
        "rp188vitc2"},
    {GST_DECKLINK_TIMECODE_FORMAT_RP188LTC, "bmdTimecodeRP188LTC", "rp188ltc"},
    {GST_DECKLINK_TIMECODE_FORMAT_RP188ANY, "bmdTimecodeRP188Any", "rp188any"},
    {GST_DECKLINK_TIMECODE_FORMAT_VITC, "bmdTimecodeVITC", "vitc"},
    {GST_DECKLINK_TIMECODE_FORMAT_VITCFIELD2, "bmdTimecodeVITCField2",
        "vitcfield2"},
    {GST_DECKLINK_TIMECODE_FORMAT_SERIAL, "bmdTimecodeSerial", "serial"},
    {0, NULL, NULL}
  };

  if (g_once_init_enter (&id)) {
    GType tmp =
        g_enum_register_static ("GstDecklinkTimecodeFormat", timecodeformats);
    g_once_init_leave (&id, tmp);
  }

  return (GType) id;
}

169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
GType
gst_decklink_keyer_mode_get_type (void)
{
  static gsize id = 0;
  static const GEnumValue keyermodes[] = {
    {GST_DECKLINK_KEYER_MODE_OFF, "Off", "off"},
    {GST_DECKLINK_KEYER_MODE_INTERNAL, "Internal", "internal"},
    {GST_DECKLINK_KEYER_MODE_EXTERNAL, "External", "external"},
    {0, NULL, NULL}
  };

  if (g_once_init_enter (&id)) {
    GType tmp = g_enum_register_static ("GstDecklinkKeyerMode", keyermodes);
    g_once_init_leave (&id, tmp);
  }

  return (GType) id;
}

188
189
190
GType
gst_decklink_audio_connection_get_type (void)
{
191
192
  static gsize id = 0;
  static const GEnumValue connections[] = {
193
194
195
196
197
198
199
200
201
    {GST_DECKLINK_AUDIO_CONNECTION_AUTO, "Automatic", "auto"},
    {GST_DECKLINK_AUDIO_CONNECTION_EMBEDDED, "SDI/HDMI embedded audio",
        "embedded"},
    {GST_DECKLINK_AUDIO_CONNECTION_AES_EBU, "AES/EBU input", "aes"},
    {GST_DECKLINK_AUDIO_CONNECTION_ANALOG, "Analog input", "analog"},
    {GST_DECKLINK_AUDIO_CONNECTION_ANALOG_XLR, "Analog input (XLR)",
        "analog-xlr"},
    {GST_DECKLINK_AUDIO_CONNECTION_ANALOG_RCA, "Analog input (RCA)",
        "analog-rca"},
202
203
204
205
206
207
208
    {0, NULL, NULL}
  };

  if (g_once_init_enter (&id)) {
    GType tmp =
        g_enum_register_static ("GstDecklinkAudioConnection", connections);
    g_once_init_leave (&id, tmp);
209
  }
210
211

  return (GType) id;
212
213
}

214
215
216
217
218
219
220
221
GType
gst_decklink_audio_channels_get_type (void)
{
  static gsize id = 0;
  static const GEnumValue connections[] = {
    {GST_DECKLINK_AUDIO_CHANNELS_2, "2 Channels", "2"},
    {GST_DECKLINK_AUDIO_CHANNELS_8, "8 Channels", "8"},
    {GST_DECKLINK_AUDIO_CHANNELS_16, "16 Channels", "16"},
222
    {GST_DECKLINK_AUDIO_CHANNELS_MAX, "Maximum channels supported", "max"},
223
224
225
226
227
228
229
230
231
232
233
234
    {0, NULL, NULL}
  };

  if (g_once_init_enter (&id)) {
    GType tmp =
        g_enum_register_static ("GstDecklinkAudioChannels", connections);
    g_once_init_leave (&id, tmp);
  }

  return (GType) id;
}

235
236
#define NTSC 10, 11, false, "bt601"
#define PAL 12, 11, true, "bt601"
237
238
#define HD 1, 1, true, "bt709"
#define UHD 1, 1, true, "bt2020"
David Schleef's avatar
David Schleef committed
239

David Schleef's avatar
David Schleef committed
240
static const GstDecklinkMode modes[] = {
241
242
  {bmdModeNTSC, 720, 486, 30000, 1001, true, NTSC},     // default is ntsc

David Schleef's avatar
David Schleef committed
243
244
245
246
247
248
249
250
251
252
253
254
  {bmdModeNTSC, 720, 486, 30000, 1001, true, NTSC},
  {bmdModeNTSC2398, 720, 486, 24000, 1001, true, NTSC},
  {bmdModePAL, 720, 576, 25, 1, true, PAL},
  {bmdModeNTSCp, 720, 486, 30000, 1001, false, NTSC},
  {bmdModePALp, 720, 576, 25, 1, false, PAL},

  {bmdModeHD1080p2398, 1920, 1080, 24000, 1001, false, HD},
  {bmdModeHD1080p24, 1920, 1080, 24, 1, false, HD},
  {bmdModeHD1080p25, 1920, 1080, 25, 1, false, HD},
  {bmdModeHD1080p2997, 1920, 1080, 30000, 1001, false, HD},
  {bmdModeHD1080p30, 1920, 1080, 30, 1, false, HD},

255
256
257
  {bmdModeHD1080i50, 1920, 1080, 25, 1, true, HD},
  {bmdModeHD1080i5994, 1920, 1080, 30000, 1001, true, HD},
  {bmdModeHD1080i6000, 1920, 1080, 30, 1, true, HD},
David Schleef's avatar
David Schleef committed
258
259

  {bmdModeHD1080p50, 1920, 1080, 50, 1, false, HD},
260
  {bmdModeHD1080p5994, 1920, 1080, 60000, 1001, false, HD},
David Schleef's avatar
David Schleef committed
261
262
263
264
  {bmdModeHD1080p6000, 1920, 1080, 60, 1, false, HD},

  {bmdModeHD720p50, 1280, 720, 50, 1, false, HD},
  {bmdModeHD720p5994, 1280, 720, 60000, 1001, false, HD},
265
266
267
268
  {bmdModeHD720p60, 1280, 720, 60, 1, false, HD},

  {bmdMode2k2398, 2048, 1556, 24000, 1001, false, HD},
  {bmdMode2k24, 2048, 1556, 24, 1, false, HD},
269
270
  {bmdMode2k25, 2048, 1556, 25, 1, false, HD},

271
272
273
274
275
  {bmdMode4K2160p2398, 3840, 2160, 24000, 1001, false, UHD},
  {bmdMode4K2160p24, 3840, 2160, 24, 1, false, UHD},
  {bmdMode4K2160p25, 3840, 2160, 25, 1, false, UHD},
  {bmdMode4K2160p2997, 3840, 2160, 30000, 1001, false, UHD},
  {bmdMode4K2160p30, 3840, 2160, 30, 1, false, UHD},
276
  {bmdMode4K2160p50, 3840, 2160, 50, 1, false, UHD},
277
278
  {bmdMode4K2160p5994, 3840, 2160, 60000, 1001, false, UHD},
  {bmdMode4K2160p60, 3840, 2160, 60, 1, false, UHD}
David Schleef's avatar
David Schleef committed
279
};
David Schleef's avatar
David Schleef committed
280

281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
static const struct
{
  BMDPixelFormat format;
  gint bpp;
  GstVideoFormat vformat;
} formats[] = {
  /* *INDENT-OFF* */
  {bmdFormat8BitYUV, 2, GST_VIDEO_FORMAT_UYVY},  /* auto */
  {bmdFormat8BitYUV, 2, GST_VIDEO_FORMAT_UYVY},
  {bmdFormat10BitYUV, 4, GST_VIDEO_FORMAT_v210},
  {bmdFormat8BitARGB, 4, GST_VIDEO_FORMAT_ARGB},
  {bmdFormat8BitBGRA, 4, GST_VIDEO_FORMAT_BGRA},
/* Not yet supported
  {bmdFormat10BitRGB, FIXME, FIXME},
  {bmdFormat12BitRGB, FIXME, FIXME},
  {bmdFormat12BitRGBLE, FIXME, FIXME},
  {bmdFormat10BitRGBXLE, FIXME, FIXME},
  {bmdFormat10BitRGBX, FIXME, FIXME} */
  /* *INDENT-ON* */
};

302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
static const struct
{
  BMDTimecodeFormat format;
  GstDecklinkTimecodeFormat gstformat;
} tcformats[] = {
  /* *INDENT-OFF* */
  {bmdTimecodeRP188VITC1, GST_DECKLINK_TIMECODE_FORMAT_RP188VITC1},
  {bmdTimecodeRP188VITC2, GST_DECKLINK_TIMECODE_FORMAT_RP188VITC2},
  {bmdTimecodeRP188LTC, GST_DECKLINK_TIMECODE_FORMAT_RP188LTC},
  {bmdTimecodeRP188Any, GST_DECKLINK_TIMECODE_FORMAT_RP188ANY},
  {bmdTimecodeVITC, GST_DECKLINK_TIMECODE_FORMAT_VITC},
  {bmdTimecodeVITCField2, GST_DECKLINK_TIMECODE_FORMAT_VITCFIELD2},
  {bmdTimecodeSerial, GST_DECKLINK_TIMECODE_FORMAT_SERIAL}
  /* *INDENT-ON* */
};

318
319
320
321
322
323
324
325
326
327
328
329
static const struct
{
  BMDKeyerMode keymode;
  GstDecklinkKeyerMode gstkeymode;
} kmodes[] = {
  /* *INDENT-OFF* */
  {bmdKeyerModeOff, GST_DECKLINK_KEYER_MODE_OFF},
  {bmdKeyerModeInternal, GST_DECKLINK_KEYER_MODE_INTERNAL},
  {bmdKeyerModeExternal, GST_DECKLINK_KEYER_MODE_EXTERNAL}
  /* *INDENT-ON* */
};

David Schleef's avatar
David Schleef committed
330
331
332
const GstDecklinkMode *
gst_decklink_get_mode (GstDecklinkModeEnum e)
{
333
  if (e < GST_DECKLINK_MODE_AUTO || e > GST_DECKLINK_MODE_2160p60)
334
    return NULL;
David Schleef's avatar
David Schleef committed
335
336
337
  return &modes[e];
}

338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
const GstDecklinkModeEnum
gst_decklink_get_mode_enum_from_bmd (BMDDisplayMode mode)
{
  GstDecklinkModeEnum displayMode = GST_DECKLINK_MODE_NTSC;
  switch (mode) {
    case bmdModeNTSC:
      displayMode = GST_DECKLINK_MODE_NTSC;
      break;
    case bmdModeNTSC2398:
      displayMode = GST_DECKLINK_MODE_NTSC2398;
      break;
    case bmdModePAL:
      displayMode = GST_DECKLINK_MODE_PAL;
      break;
    case bmdModeNTSCp:
      displayMode = GST_DECKLINK_MODE_NTSC_P;
      break;
    case bmdModePALp:
      displayMode = GST_DECKLINK_MODE_PAL_P;
      break;
    case bmdModeHD1080p2398:
      displayMode = GST_DECKLINK_MODE_1080p2398;
      break;
    case bmdModeHD1080p24:
      displayMode = GST_DECKLINK_MODE_1080p24;
      break;
    case bmdModeHD1080p25:
      displayMode = GST_DECKLINK_MODE_1080p25;
      break;
    case bmdModeHD1080p2997:
      displayMode = GST_DECKLINK_MODE_1080p2997;
      break;
    case bmdModeHD1080p30:
      displayMode = GST_DECKLINK_MODE_1080p30;
      break;
    case bmdModeHD1080i50:
      displayMode = GST_DECKLINK_MODE_1080i50;
      break;
    case bmdModeHD1080i5994:
      displayMode = GST_DECKLINK_MODE_1080i5994;
      break;
    case bmdModeHD1080i6000:
      displayMode = GST_DECKLINK_MODE_1080i60;
      break;
    case bmdModeHD1080p50:
      displayMode = GST_DECKLINK_MODE_1080p50;
      break;
    case bmdModeHD1080p5994:
      displayMode = GST_DECKLINK_MODE_1080p5994;
      break;
    case bmdModeHD1080p6000:
      displayMode = GST_DECKLINK_MODE_1080p60;
      break;
    case bmdModeHD720p50:
      displayMode = GST_DECKLINK_MODE_720p50;
      break;
    case bmdModeHD720p5994:
      displayMode = GST_DECKLINK_MODE_720p5994;
      break;
    case bmdModeHD720p60:
      displayMode = GST_DECKLINK_MODE_720p60;
      break;
    case bmdMode2k2398:
401
      displayMode = GST_DECKLINK_MODE_1556p2398;
402
403
      break;
    case bmdMode2k24:
404
      displayMode = GST_DECKLINK_MODE_1556p24;
405
406
      break;
    case bmdMode2k25:
407
      displayMode = GST_DECKLINK_MODE_1556p25;
408
409
      break;
    case bmdMode4K2160p2398:
410
      displayMode = GST_DECKLINK_MODE_2160p2398;
411
412
      break;
    case bmdMode4K2160p24:
413
      displayMode = GST_DECKLINK_MODE_2160p24;
414
415
      break;
    case bmdMode4K2160p25:
416
      displayMode = GST_DECKLINK_MODE_2160p25;
417
418
      break;
    case bmdMode4K2160p2997:
419
      displayMode = GST_DECKLINK_MODE_2160p2997;
420
421
      break;
    case bmdMode4K2160p30:
422
      displayMode = GST_DECKLINK_MODE_2160p30;
423
424
      break;
    case bmdMode4K2160p50:
425
      displayMode = GST_DECKLINK_MODE_2160p50;
426
427
      break;
    case bmdMode4K2160p5994:
428
      displayMode = GST_DECKLINK_MODE_2160p5994;
429
430
      break;
    case bmdMode4K2160p60:
431
      displayMode = GST_DECKLINK_MODE_2160p60;
432
433
434
435
436
437
438
439
      break;
    default:
      g_assert_not_reached ();
      break;
  }
  return displayMode;
}

440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
const BMDPixelFormat
gst_decklink_pixel_format_from_type (GstDecklinkVideoFormat t)
{
  return formats[t].format;
}

const gint
gst_decklink_bpp_from_type (GstDecklinkVideoFormat t)
{
  return formats[t].bpp;
}

const GstDecklinkVideoFormat
gst_decklink_type_from_video_format (GstVideoFormat f)
{
  guint i;

  for (i = 1; i < G_N_ELEMENTS (formats); i++) {
    if (formats[i].vformat == f)
      return (GstDecklinkVideoFormat) i;
  }
  g_assert_not_reached ();
  return GST_DECKLINK_VIDEO_FORMAT_AUTO;
}

465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
const BMDTimecodeFormat
gst_decklink_timecode_format_from_enum (GstDecklinkTimecodeFormat f)
{
  return tcformats[f].format;
}

const GstDecklinkTimecodeFormat
gst_decklink_timecode_format_to_enum (BMDTimecodeFormat f)
{
  guint i;

  for (i = 0; i < G_N_ELEMENTS (tcformats); i++) {
    if (tcformats[i].format == f)
      return (GstDecklinkTimecodeFormat) i;
  }
  g_assert_not_reached ();
  return GST_DECKLINK_TIMECODE_FORMAT_RP188ANY;
}

484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
const BMDKeyerMode
gst_decklink_keyer_mode_from_enum (GstDecklinkKeyerMode m)
{
  return kmodes[m].keymode;
}

const GstDecklinkKeyerMode
gst_decklink_keyer_mode_to_enum (BMDKeyerMode m)
{
  guint i;

  for (i = 0; i < G_N_ELEMENTS (kmodes); i++) {
    if (kmodes[i].keymode == m)
      return (GstDecklinkKeyerMode) i;
  }
  g_assert_not_reached ();
  return GST_DECKLINK_KEYER_MODE_OFF;
}

503
static const BMDVideoConnection connections[] = {
Sebastian Dröge's avatar
Sebastian Dröge committed
504
  (BMDVideoConnection) 0,       /* auto */
505
506
507
508
509
510
511
512
513
514
515
  bmdVideoConnectionSDI,
  bmdVideoConnectionHDMI,
  bmdVideoConnectionOpticalSDI,
  bmdVideoConnectionComponent,
  bmdVideoConnectionComposite,
  bmdVideoConnectionSVideo
};

const BMDVideoConnection
gst_decklink_get_connection (GstDecklinkConnectionEnum e)
{
516
517
518
519
  g_return_val_if_fail (e != GST_DECKLINK_CONNECTION_AUTO,
      bmdVideoConnectionSDI);

  if (e <= GST_DECKLINK_CONNECTION_AUTO || e > GST_DECKLINK_CONNECTION_SVIDEO)
520
    e = GST_DECKLINK_CONNECTION_SDI;
521

522
523
524
  return connections[e];
}

525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
static gboolean
gst_decklink_caps_get_pixel_format (GstCaps * caps, BMDPixelFormat * format)
{
  GstVideoInfo vinfo;
  GstVideoFormat f;

  if (gst_video_info_from_caps (&vinfo, caps) == FALSE) {
    GST_ERROR ("Could not get video info from caps: %" GST_PTR_FORMAT, caps);
    return FALSE;
  }

  f = vinfo.finfo->format;
  return gst_decklink_type_from_video_format (f);
}

David Schleef's avatar
David Schleef committed
540
static GstStructure *
Sebastian Dröge's avatar
Sebastian Dröge committed
541
542
gst_decklink_mode_get_structure (GstDecklinkModeEnum e, BMDPixelFormat f,
    gboolean input)
David Schleef's avatar
David Schleef committed
543
544
{
  const GstDecklinkMode *mode = &modes[e];
545
  GstStructure *s = gst_structure_new ("video/x-raw",
David Schleef's avatar
David Schleef committed
546
547
      "width", G_TYPE_INT, mode->width,
      "height", G_TYPE_INT, mode->height,
548
      "pixel-aspect-ratio", GST_TYPE_FRACTION, mode->par_n, mode->par_d,
Sebastian Dröge's avatar
Sebastian Dröge committed
549
550
      "interlace-mode", G_TYPE_STRING,
      mode->interlaced ? "interleaved" : "progressive",
551
552
      "framerate", GST_TYPE_FRACTION, mode->fps_n, mode->fps_d, NULL);

553
554
  if (input && mode->interlaced) {
    if (mode->tff)
Sebastian Dröge's avatar
Sebastian Dröge committed
555
556
      gst_structure_set (s, "field-order", G_TYPE_STRING, "top-field-first",
          NULL);
557
    else
Sebastian Dröge's avatar
Sebastian Dröge committed
558
559
      gst_structure_set (s, "field-order", G_TYPE_STRING, "bottom-field-first",
          NULL);
560
561
  }

562
  switch (f) {
Sebastian Dröge's avatar
Sebastian Dröge committed
563
    case bmdFormat8BitYUV:     /* '2vuy' */
564
565
566
567
      gst_structure_set (s, "format", G_TYPE_STRING, "UYVY",
          "colorimetry", G_TYPE_STRING, mode->colorimetry,
          "chroma-site", G_TYPE_STRING, "mpeg2", NULL);
      break;
Sebastian Dröge's avatar
Sebastian Dröge committed
568
    case bmdFormat10BitYUV:    /* 'v210' */
569
570
      gst_structure_set (s, "format", G_TYPE_STRING, "v210", NULL);
      break;
Sebastian Dröge's avatar
Sebastian Dröge committed
571
    case bmdFormat8BitARGB:    /* 'ARGB' */
572
573
      gst_structure_set (s, "format", G_TYPE_STRING, "ARGB", NULL);
      break;
Sebastian Dröge's avatar
Sebastian Dröge committed
574
    case bmdFormat8BitBGRA:    /* 'BGRA' */
575
576
      gst_structure_set (s, "format", G_TYPE_STRING, "BGRA", NULL);
      break;
Sebastian Dröge's avatar
Sebastian Dröge committed
577
578
579
    case bmdFormat10BitRGB:    /* 'r210' Big-endian RGB 10-bit per component with SMPTE video levels (64-960). Packed as 2:10:10:10 */
    case bmdFormat12BitRGB:    /* 'R12B' Big-endian RGB 12-bit per component with full range (0-4095). Packed as 12-bit per component */
    case bmdFormat12BitRGBLE:  /* 'R12L' Little-endian RGB 12-bit per component with full range (0-4095). Packed as 12-bit per component */
580
    case bmdFormat10BitRGBXLE: /* 'R10l' Little-endian 10-bit RGB with SMPTE video levels (64-940) */
Sebastian Dröge's avatar
Sebastian Dröge committed
581
    case bmdFormat10BitRGBX:   /* 'R10b' Big-endian 10-bit RGB with SMPTE video levels (64-940) */
582
583
584
585
586
587
588
589
    default:
      GST_WARNING ("format not supported %d", f);
      gst_structure_free (s);
      s = NULL;
      break;
  }

  return s;
David Schleef's avatar
David Schleef committed
590
591
}

David Schleef's avatar
David Schleef committed
592
GstCaps *
Sebastian Dröge's avatar
Sebastian Dröge committed
593
594
gst_decklink_mode_get_caps (GstDecklinkModeEnum e, BMDPixelFormat f,
    gboolean input)
David Schleef's avatar
David Schleef committed
595
596
597
598
{
  GstCaps *caps;

  caps = gst_caps_new_empty ();
599
  caps =
Sebastian Dröge's avatar
Sebastian Dröge committed
600
601
      gst_caps_merge_structure (caps, gst_decklink_mode_get_structure (e, f,
          input));
David Schleef's avatar
David Schleef committed
602
603
604
605
606

  return caps;
}

GstCaps *
607
gst_decklink_mode_get_caps_all_formats (GstDecklinkModeEnum e, gboolean input)
608
609
610
611
612
613
614
615
{
  GstCaps *caps;
  guint i;

  caps = gst_caps_new_empty ();
  for (i = 1; i < G_N_ELEMENTS (formats); i++)
    caps =
        gst_caps_merge_structure (caps, gst_decklink_mode_get_structure (e,
616
            formats[i].format, input));
617
618
619
620
621

  return caps;
}

GstCaps *
622
gst_decklink_pixel_format_get_caps (BMDPixelFormat f, gboolean input)
David Schleef's avatar
David Schleef committed
623
624
625
626
627
628
{
  int i;
  GstCaps *caps;
  GstStructure *s;

  caps = gst_caps_new_empty ();
629
  for (i = 1; i < (int) G_N_ELEMENTS (modes); i++) {
630
    s = gst_decklink_mode_get_structure ((GstDecklinkModeEnum) i, f, input);
631
    caps = gst_caps_merge_structure (caps, s);
David Schleef's avatar
David Schleef committed
632
633
634
635
  }

  return caps;
}
David Schleef's avatar
David Schleef committed
636

637
GstCaps *
638
gst_decklink_mode_get_template_caps (gboolean input)
639
640
641
642
643
644
645
646
{
  int i;
  GstCaps *caps;

  caps = gst_caps_new_empty ();
  for (i = 1; i < (int) G_N_ELEMENTS (modes); i++)
    caps =
        gst_caps_merge (caps,
Sebastian Dröge's avatar
Sebastian Dröge committed
647
648
        gst_decklink_mode_get_caps_all_formats ((GstDecklinkModeEnum) i,
            input));
649
650
651
652

  return caps;
}

653
const GstDecklinkMode *
654
655
gst_decklink_find_mode_and_format_for_caps (GstCaps * caps,
    BMDPixelFormat * format)
656
657
658
659
{
  int i;
  GstCaps *mode_caps;

660
661
662
663
  g_return_val_if_fail (gst_caps_is_fixed (caps), NULL);
  if (!gst_decklink_caps_get_pixel_format (caps, format))
    return NULL;

664
  for (i = 1; i < (int) G_N_ELEMENTS (modes); i++) {
Sebastian Dröge's avatar
Sebastian Dröge committed
665
666
    mode_caps =
        gst_decklink_mode_get_caps ((GstDecklinkModeEnum) i, *format, FALSE);
667
668
669
670
671
672
673
674
675
676
    if (gst_caps_can_intersect (caps, mode_caps)) {
      gst_caps_unref (mode_caps);
      return gst_decklink_get_mode ((GstDecklinkModeEnum) i);
    }
    gst_caps_unref (mode_caps);
  }

  return NULL;
}

677
678
679
680
681
682
683
684
const GstDecklinkMode *
gst_decklink_find_mode_for_caps (GstCaps * caps)
{
  BMDPixelFormat format;

  return gst_decklink_find_mode_and_format_for_caps (caps, &format);
}

685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
#define GST_TYPE_DECKLINK_CLOCK \
  (gst_decklink_clock_get_type())
#define GST_DECKLINK_CLOCK(obj) \
  (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_DECKLINK_CLOCK,GstDecklinkClock))
#define GST_DECKLINK_CLOCK_CLASS(klass) \
  (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_DECKLINK_CLOCK,GstDecklinkClockClass))
#define GST_IS_Decklink_CLOCK(obj) \
  (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_DECKLINK_CLOCK))
#define GST_IS_Decklink_CLOCK_CLASS(klass) \
  (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_DECKLINK_CLOCK))
#define GST_DECKLINK_CLOCK_CAST(obj) \
  ((GstDecklinkClock*)(obj))

typedef struct _GstDecklinkClock GstDecklinkClock;
typedef struct _GstDecklinkClockClass GstDecklinkClockClass;

struct _GstDecklinkClock
Sebastian Dröge's avatar
Sebastian Dröge committed
702
{
703
704
  GstSystemClock clock;

705
  GstDecklinkOutput *output;
706
707
};

708
709
710
711
712
713
struct _GstDecklinkClockClass
{
  GstSystemClockClass parent_class;
};

GType gst_decklink_clock_get_type (void);
Sebastian Dröge's avatar
Sebastian Dröge committed
714
static GstClock *gst_decklink_clock_new (const gchar * name);
715
716
717
718
719
720
721
722

typedef struct _Device Device;
struct _Device
{
  GstDecklinkOutput output;
  GstDecklinkInput input;
};

723
724
725
726
727
728
729
730
class GStreamerDecklinkInputCallback:public IDeckLinkInputCallback
{
private:
  GstDecklinkInput * m_input;
  GMutex m_mutex;
  gint m_refcount;
public:
    GStreamerDecklinkInputCallback (GstDecklinkInput * input)
731
  : IDeckLinkInputCallback (), m_refcount (1)
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
  {
    m_input = input;
    g_mutex_init (&m_mutex);
  }

  virtual ~ GStreamerDecklinkInputCallback ()
  {
    g_mutex_clear (&m_mutex);
  }

  virtual HRESULT STDMETHODCALLTYPE QueryInterface (REFIID, LPVOID *)
  {
    return E_NOINTERFACE;
  }

  virtual ULONG STDMETHODCALLTYPE AddRef (void)
  {
    ULONG ret;

    g_mutex_lock (&m_mutex);
    m_refcount++;
    ret = m_refcount;
    g_mutex_unlock (&m_mutex);

    return ret;
  }

  virtual ULONG STDMETHODCALLTYPE Release (void)
  {
    ULONG ret;

    g_mutex_lock (&m_mutex);
    m_refcount--;
    ret = m_refcount;
    g_mutex_unlock (&m_mutex);


    if (ret == 0) {
      delete this;
    }

    return ret;
  }

  virtual HRESULT STDMETHODCALLTYPE
      VideoInputFormatChanged (BMDVideoInputFormatChangedEvents,
778
      IDeckLinkDisplayMode * mode, BMDDetectedVideoInputFormatFlags formatFlags)
779
  {
780
781
    BMDPixelFormat pixelFormat = bmdFormat8BitYUV;

782
783
    GST_INFO ("Video input format changed");

784
785
786
    if (formatFlags & bmdDetectedVideoInputRGB444)
      pixelFormat = bmdFormat8BitARGB;

787
788
789
    g_mutex_lock (&m_input->lock);
    m_input->input->PauseStreams ();
    m_input->input->EnableVideoInput (mode->GetDisplayMode (),
790
        pixelFormat, bmdVideoInputEnableFormatDetection);
791
792
793
794
795
    m_input->input->FlushStreams ();
    m_input->input->StartStreams ();
    m_input->mode =
        gst_decklink_get_mode (gst_decklink_get_mode_enum_from_bmd
        (mode->GetDisplayMode ()));
796
    m_input->format = pixelFormat;
797
798
    g_mutex_unlock (&m_input->lock);

799
800
801
802
803
804
805
    return S_OK;
  }

  virtual HRESULT STDMETHODCALLTYPE
      VideoInputFrameArrived (IDeckLinkVideoInputFrame * video_frame,
      IDeckLinkAudioInputPacket * audio_packet)
  {
806
807
    GstElement *videosrc = NULL, *audiosrc = NULL;
    void (*got_video_frame) (GstElement * videosrc,
808
        IDeckLinkVideoInputFrame * frame, GstDecklinkModeEnum mode,
809
        GstClockTime capture_time, GstClockTime stream_time,
810
811
812
        GstClockTime stream_duration, GstClockTime hardware_time,
        GstClockTime hardware_duration, IDeckLinkTimecode * dtc,
        gboolean no_signal) = NULL;
813
    void (*got_audio_packet) (GstElement * videosrc,
Sebastian Dröge's avatar
Sebastian Dröge committed
814
        IDeckLinkAudioInputPacket * packet, GstClockTime capture_time,
815
        GstClockTime stream_time, GstClockTime stream_duration,
816
        GstClockTime hardware_time, GstClockTime hardware_duration,
817
        gboolean no_signal) = NULL;
818
    GstDecklinkModeEnum mode;
819
    GstClockTime capture_time = GST_CLOCK_TIME_NONE;
820
    GstClockTime base_time = 0;
821
    gboolean no_signal = FALSE;
822
    GstClock *clock = NULL;
823
    HRESULT res;
824
    BMDTimeValue stream_time = GST_CLOCK_TIME_NONE;
825
    BMDTimeValue stream_duration = GST_CLOCK_TIME_NONE;
826
827
    BMDTimeValue hardware_time = GST_CLOCK_TIME_NONE;
    BMDTimeValue hardware_duration = GST_CLOCK_TIME_NONE;
828

829
    g_mutex_lock (&m_input->lock);
830
    if (m_input->videosrc) {
831
832
833
      videosrc = GST_ELEMENT_CAST (gst_object_ref (m_input->videosrc));
      clock = gst_element_get_clock (videosrc);
      base_time = gst_element_get_base_time (videosrc);
834
      got_video_frame = m_input->got_video_frame;
835
    }
836
    mode = gst_decklink_get_mode_enum_from_bmd (m_input->mode->mode);
837

838
839
    if (m_input->audiosrc) {
      audiosrc = GST_ELEMENT_CAST (gst_object_ref (m_input->audiosrc));
840
841
842
843
      if (!clock) {
        clock = gst_element_get_clock (GST_ELEMENT_CAST (audiosrc));
        base_time = gst_element_get_base_time (audiosrc);
      }
844
      got_audio_packet = m_input->got_audio_packet;
845
846
    }
    g_mutex_unlock (&m_input->lock);
847

848
849
850
851
852
853
854
855
    if (clock) {
      capture_time = gst_clock_get_time (clock);
      if (capture_time > base_time)
        capture_time -= base_time;
      else
        capture_time = 0;
    }

856
857
858
859
860
861
862
863
864
    if (video_frame) {
      BMDFrameFlags flags;

      flags = video_frame->GetFlags ();
      if (flags & bmdFrameHasNoInputSource) {
        no_signal = TRUE;
      }
    }

865
    if (got_video_frame && videosrc && video_frame) {
866
      IDeckLinkTimecode *dtc = 0;
867
868
869
870
871

      res =
          video_frame->GetStreamTime (&stream_time, &stream_duration,
          GST_SECOND);
      if (res != S_OK) {
872
        GST_ERROR ("Failed to get stream time: 0x%08lx", (unsigned long) res);
873
874
875
876
        stream_time = GST_CLOCK_TIME_NONE;
        stream_duration = GST_CLOCK_TIME_NONE;
      }

877
878
879
880
881
882
883
884
885
      res =
          video_frame->GetHardwareReferenceTimestamp (GST_SECOND,
          &hardware_time, &hardware_duration);
      if (res != S_OK) {
        GST_ERROR ("Failed to get hardware time: 0x%08lx", (unsigned long) res);
        hardware_time = GST_CLOCK_TIME_NONE;
        hardware_duration = GST_CLOCK_TIME_NONE;
      }

886
887
888
889
890
891
892
893
894
      if (m_input->videosrc) {
        /* FIXME: Avoid circularity between gstdecklink.cpp and
         * gstdecklinkvideosrc.cpp */
        res =
            video_frame->
            GetTimecode (GST_DECKLINK_VIDEO_SRC (videosrc)->timecode_format,
            &dtc);

        if (res != S_OK) {
895
896
          GST_DEBUG_OBJECT (videosrc, "Failed to get timecode: 0x%08lx",
              (unsigned long) res);
897
898
899
900
          dtc = NULL;
        }
      }

901
      /* passing dtc reference */
902
      got_video_frame (videosrc, video_frame, mode, capture_time,
903
904
          stream_time, stream_duration, hardware_time, hardware_duration, dtc,
          no_signal);
905
906
    }

907
    if (got_audio_packet && audiosrc && audio_packet) {
Sebastian Dröge's avatar
Sebastian Dröge committed
908
      m_input->got_audio_packet (audiosrc, audio_packet, capture_time,
909
910
          stream_time, stream_duration, hardware_time, hardware_duration,
          no_signal);
911
912
    } else {
      if (!audio_packet)
Sebastian Dröge's avatar
Sebastian Dröge committed
913
914
        GST_DEBUG ("Received no audio packet at %" GST_TIME_FORMAT,
            GST_TIME_ARGS (capture_time));
915
916
917
918
    }

    gst_object_replace ((GstObject **) & videosrc, NULL);
    gst_object_replace ((GstObject **) & audiosrc, NULL);
919
    gst_object_replace ((GstObject **) & clock, NULL);
920

921
922
923
924
    return S_OK;
  }
};

925
926
927
928
929
930
class GStreamerDecklinkMemoryAllocator:public IDeckLinkMemoryAllocator
{
private:
  GMutex m_mutex;
  uint32_t m_lastBufferSize;
  uint32_t m_nonEmptyCalls;
Sebastian Dröge's avatar
Sebastian Dröge committed
931
  GstQueueArray *m_buffers;
932
933
  gint m_refcount;

Sebastian Dröge's avatar
Sebastian Dröge committed
934
  void _clearBufferPool ()
935
  {
Sebastian Dröge's avatar
Sebastian Dröge committed
936
    uint8_t *buf;
937
938

    if (!m_buffers)
Sebastian Dröge's avatar
Sebastian Dröge committed
939
        return;
940

Sebastian Dröge's avatar
Sebastian Dröge committed
941
942
    while ((buf = (uint8_t *) gst_queue_array_pop_head (m_buffers)))
      g_free (buf - 128);
943
944
945
946
  }

public:
    GStreamerDecklinkMemoryAllocator ()
Sebastian Dröge's avatar
Sebastian Dröge committed
947
948
949
  : IDeckLinkMemoryAllocator (),
      m_lastBufferSize (0),
      m_nonEmptyCalls (0), m_buffers (NULL), m_refcount (1)
950
951
952
953
954
955
  {
    g_mutex_init (&m_mutex);

    m_buffers = gst_queue_array_new (60);
  }

Sebastian Dröge's avatar
Sebastian Dröge committed
956
957
  virtual ~ GStreamerDecklinkMemoryAllocator () {
    Decommit ();
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998

    gst_queue_array_free (m_buffers);

    g_mutex_clear (&m_mutex);
  }

  virtual HRESULT STDMETHODCALLTYPE QueryInterface (REFIID, LPVOID *)
  {
    return E_NOINTERFACE;
  }

  virtual ULONG STDMETHODCALLTYPE AddRef (void)
  {
    ULONG ret;

    g_mutex_lock (&m_mutex);
    m_refcount++;
    ret = m_refcount;
    g_mutex_unlock (&m_mutex);

    return ret;
  }

  virtual ULONG STDMETHODCALLTYPE Release (void)
  {
    ULONG ret;

    g_mutex_lock (&m_mutex);
    m_refcount--;
    ret = m_refcount;
    g_mutex_unlock (&m_mutex);


    if (ret == 0) {
      delete this;
    }

    return ret;
  }

  virtual HRESULT STDMETHODCALLTYPE
Sebastian Dröge's avatar
Sebastian Dröge committed
999
      AllocateBuffer (uint32_t bufferSize, void **allocatedBuffer)
1000
  {