gstdecklink.cpp 33.2 KB
Newer Older
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>
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>
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
35

36 37 38
GType
gst_decklink_mode_get_type (void)
{
39 40
  static gsize id = 0;
  static const GEnumValue modes[] = {
41 42
    {GST_DECKLINK_MODE_AUTO, "auto", "Automatic detection"},

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    {GST_DECKLINK_MODE_NTSC, "ntsc", "NTSC SD 60i"},
    {GST_DECKLINK_MODE_NTSC2398, "ntsc2398", "NTSC SD 60i (24 fps)"},
    {GST_DECKLINK_MODE_PAL, "pal", "PAL SD 50i"},
    {GST_DECKLINK_MODE_NTSC_P, "ntsc-p", "NTSC SD 60p"},
    {GST_DECKLINK_MODE_PAL_P, "pal-p", "PAL SD 50p"},

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

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

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

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

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

71 72 73 74 75 76 77 78
    {GST_DECKLINK_MODE_2160p2398, "2160p2398", "4k 23.98p"},
    {GST_DECKLINK_MODE_2160p24, "2160p24", "4k 24p"},
    {GST_DECKLINK_MODE_2160p25, "2160p25", "4k 25p"},
    {GST_DECKLINK_MODE_2160p2997, "2160p2997", "4k 29.97p"},
    {GST_DECKLINK_MODE_2160p30, "2160p30", "4k 30p"},
    {GST_DECKLINK_MODE_2160p50, "2160p50", "4k 50p"},
    {GST_DECKLINK_MODE_2160p5994, "2160p5994", "4k 59.94p"},
    {GST_DECKLINK_MODE_2160p60, "2160p60", "4k 60p"},
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);
86
  }
87 88

  return (GType) id;
89 90
}

91 92 93
GType
gst_decklink_connection_get_type (void)
{
94 95
  static gsize id = 0;
  static const GEnumValue connections[] = {
96
    {GST_DECKLINK_CONNECTION_AUTO, "auto", "Auto"},
97 98 99 100 101 102 103 104 105 106 107 108
    {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, "svideo", "S-Video"},
    {0, NULL, NULL}
  };

  if (g_once_init_enter (&id)) {
    GType tmp = g_enum_register_static ("GstDecklinkConnection", connections);
    g_once_init_leave (&id, tmp);
109
  }
110 111

  return (GType) id;
112 113
}

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
GType
gst_decklink_video_format_get_type (void)
{
  static gsize id = 0;
  static const GEnumValue types[] = {
    {GST_DECKLINK_VIDEO_FORMAT_AUTO, "auto", "Auto"},
    {GST_DECKLINK_VIDEO_FORMAT_8BIT_YUV, "8bit-yuv", "bmdFormat8BitYUV"},
    {GST_DECKLINK_VIDEO_FORMAT_10BIT_YUV, "10bit-yuv", "bmdFormat10BitYUV"},
    {GST_DECKLINK_VIDEO_FORMAT_8BIT_ARGB, "8bit-argb", "bmdFormat8BitARGB"},
    {GST_DECKLINK_VIDEO_FORMAT_8BIT_BGRA, "8bit-bgra", "bmdFormat8BitBGRA"},
    /*
    {GST_DECKLINK_VIDEO_FORMAT_10BIT_RGB, "10bit-rgb", "bmdFormat10BitRGB"},
    {GST_DECKLINK_VIDEO_FORMAT_12BIT_RGB, "12bit-rgb", "bmdFormat12BitRGB"},
    {GST_DECKLINK_VIDEO_FORMAT_12BIT_RGBLE, "12bit-rgble", "bmdFormat12BitRGBLE"},
    {GST_DECKLINK_VIDEO_FORMAT_10BIT_RGBXLE, "10bit-rgbxle", "bmdFormat10BitRGBXLE"},
    {GST_DECKLINK_VIDEO_FORMAT_10BIT_RGBX, "10bit-rgbx", "bmdFormat10BitRGBX"},
    */
    {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
GType
gst_decklink_audio_connection_get_type (void)
{
145 146 147 148 149 150 151
  static gsize id = 0;
  static const GEnumValue connections[] = {
    {GST_DECKLINK_AUDIO_CONNECTION_AUTO, "auto", "Automatic"},
    {GST_DECKLINK_AUDIO_CONNECTION_EMBEDDED, "embedded",
        "SDI/HDMI embedded audio"},
    {GST_DECKLINK_AUDIO_CONNECTION_AES_EBU, "aes", "AES/EBU input"},
    {GST_DECKLINK_AUDIO_CONNECTION_ANALOG, "analog", "Analog input"},
152 153 154 155
    {GST_DECKLINK_AUDIO_CONNECTION_ANALOG_XLR, "analog-xlr",
        "Analog input (XLR)"},
    {GST_DECKLINK_AUDIO_CONNECTION_ANALOG_RCA, "analog-rca",
        "Analog input (RCA)"},
156 157 158 159 160 161 162
    {0, NULL, NULL}
  };

  if (g_once_init_enter (&id)) {
    GType tmp =
        g_enum_register_static ("GstDecklinkAudioConnection", connections);
    g_once_init_leave (&id, tmp);
163
  }
164 165

  return (GType) id;
166 167
}

168 169 170 171
#define NTSC 10, 11, false, "bt601"
#define PAL 12, 11, true, "bt601"
#define HD 1, 1, false, "bt709"
#define UHD 1, 1, false, "bt2020"
David Schleef's avatar
David Schleef committed
172

173
static const GstDecklinkMode modes[] = {
174 175
  {bmdModeNTSC, 720, 486, 30000, 1001, true, NTSC},     // default is ntsc

David Schleef's avatar
David Schleef committed
176 177 178 179 180 181 182 183 184 185 186 187
  {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},

188 189 190
  {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
191 192

  {bmdModeHD1080p50, 1920, 1080, 50, 1, false, HD},
193
  {bmdModeHD1080p5994, 1920, 1080, 60000, 1001, false, HD},
David Schleef's avatar
David Schleef committed
194 195 196 197
  {bmdModeHD1080p6000, 1920, 1080, 60, 1, false, HD},

  {bmdModeHD720p50, 1280, 720, 50, 1, false, HD},
  {bmdModeHD720p5994, 1280, 720, 60000, 1001, false, HD},
198 199 200 201
  {bmdModeHD720p60, 1280, 720, 60, 1, false, HD},

  {bmdMode2k2398, 2048, 1556, 24000, 1001, false, HD},
  {bmdMode2k24, 2048, 1556, 24, 1, false, HD},
202 203
  {bmdMode2k25, 2048, 1556, 25, 1, false, HD},

204 205 206 207 208
  {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},
209
  {bmdMode4K2160p50, 3840, 2160, 50, 1, false, UHD},
210 211
  {bmdMode4K2160p5994, 3840, 2160, 60000, 1001, false, UHD},
  {bmdMode4K2160p60, 3840, 2160, 60, 1, false, UHD}
David Schleef's avatar
David Schleef committed
212
};
213

214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
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* */
};

235 236 237
const GstDecklinkMode *
gst_decklink_get_mode (GstDecklinkModeEnum e)
{
238
  if (e < GST_DECKLINK_MODE_AUTO || e > GST_DECKLINK_MODE_2160p60)
239
    return NULL;
240 241 242
  return &modes[e];
}

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
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:
306
      displayMode = GST_DECKLINK_MODE_1556p2398;
307 308
      break;
    case bmdMode2k24:
309
      displayMode = GST_DECKLINK_MODE_1556p24;
310 311
      break;
    case bmdMode2k25:
312
      displayMode = GST_DECKLINK_MODE_1556p25;
313 314
      break;
    case bmdMode4K2160p2398:
315
      displayMode = GST_DECKLINK_MODE_2160p2398;
316 317
      break;
    case bmdMode4K2160p24:
318
      displayMode = GST_DECKLINK_MODE_2160p24;
319 320
      break;
    case bmdMode4K2160p25:
321
      displayMode = GST_DECKLINK_MODE_2160p25;
322 323
      break;
    case bmdMode4K2160p2997:
324
      displayMode = GST_DECKLINK_MODE_2160p2997;
325 326
      break;
    case bmdMode4K2160p30:
327
      displayMode = GST_DECKLINK_MODE_2160p30;
328 329
      break;
    case bmdMode4K2160p50:
330
      displayMode = GST_DECKLINK_MODE_2160p50;
331 332
      break;
    case bmdMode4K2160p5994:
333
      displayMode = GST_DECKLINK_MODE_2160p5994;
334 335
      break;
    case bmdMode4K2160p60:
336
      displayMode = GST_DECKLINK_MODE_2160p60;
337 338 339 340 341 342 343 344
      break;
    default:
      g_assert_not_reached ();
      break;
  }
  return displayMode;
}

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
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;
}

370
static const BMDVideoConnection connections[] = {
371
  0,                            /* auto */
372 373 374 375 376 377 378 379 380 381 382
  bmdVideoConnectionSDI,
  bmdVideoConnectionHDMI,
  bmdVideoConnectionOpticalSDI,
  bmdVideoConnectionComponent,
  bmdVideoConnectionComposite,
  bmdVideoConnectionSVideo
};

const BMDVideoConnection
gst_decklink_get_connection (GstDecklinkConnectionEnum e)
{
383 384 385 386
  g_return_val_if_fail (e != GST_DECKLINK_CONNECTION_AUTO,
      bmdVideoConnectionSDI);

  if (e <= GST_DECKLINK_CONNECTION_AUTO || e > GST_DECKLINK_CONNECTION_SVIDEO)
387
    e = GST_DECKLINK_CONNECTION_SDI;
388

389 390 391
  return connections[e];
}

392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
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
407
static GstStructure *
408
gst_decklink_mode_get_structure (GstDecklinkModeEnum e, BMDPixelFormat f)
409 410
{
  const GstDecklinkMode *mode = &modes[e];
411
  GstStructure *s = gst_structure_new ("video/x-raw",
412 413
      "width", G_TYPE_INT, mode->width,
      "height", G_TYPE_INT, mode->height,
414
      "pixel-aspect-ratio", GST_TYPE_FRACTION, mode->par_n, mode->par_d,
415 416
      "interlace-mode", G_TYPE_STRING,
      mode->interlaced ? "interleaved" : "progressive",
417 418 419
      "framerate", GST_TYPE_FRACTION, mode->fps_n, mode->fps_d, NULL);

  switch (f) {
420
    case bmdFormat8BitYUV:     /* '2vuy' */
421 422 423 424
      gst_structure_set (s, "format", G_TYPE_STRING, "UYVY",
          "colorimetry", G_TYPE_STRING, mode->colorimetry,
          "chroma-site", G_TYPE_STRING, "mpeg2", NULL);
      break;
425
    case bmdFormat10BitYUV:    /* 'v210' */
426 427
      gst_structure_set (s, "format", G_TYPE_STRING, "v210", NULL);
      break;
428
    case bmdFormat8BitARGB:    /* 'ARGB' */
429 430
      gst_structure_set (s, "format", G_TYPE_STRING, "ARGB", NULL);
      break;
431
    case bmdFormat8BitBGRA:    /* 'BGRA' */
432 433
      gst_structure_set (s, "format", G_TYPE_STRING, "BGRA", NULL);
      break;
434 435 436
    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 */
437
    case bmdFormat10BitRGBXLE: /* 'R10l' Little-endian 10-bit RGB with SMPTE video levels (64-940) */
438
    case bmdFormat10BitRGBX:   /* 'R10b' Big-endian 10-bit RGB with SMPTE video levels (64-940) */
439 440 441 442 443 444 445 446
    default:
      GST_WARNING ("format not supported %d", f);
      gst_structure_free (s);
      s = NULL;
      break;
  }

  return s;
447 448
}

David Schleef's avatar
David Schleef committed
449
GstCaps *
450
gst_decklink_mode_get_caps (GstDecklinkModeEnum e, BMDPixelFormat f)
David Schleef's avatar
David Schleef committed
451 452 453 454
{
  GstCaps *caps;

  caps = gst_caps_new_empty ();
455 456
  caps =
      gst_caps_merge_structure (caps, gst_decklink_mode_get_structure (e, f));
David Schleef's avatar
David Schleef committed
457 458 459 460 461

  return caps;
}

GstCaps *
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
gst_decklink_mode_get_caps_all_formats (GstDecklinkModeEnum e)
{
  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,
            formats[i].format));

  return caps;
}

GstCaps *
gst_decklink_pixel_format_get_caps (BMDPixelFormat f)
David Schleef's avatar
David Schleef committed
478 479 480 481 482 483
{
  int i;
  GstCaps *caps;
  GstStructure *s;

  caps = gst_caps_new_empty ();
484
  for (i = 1; i < (int) G_N_ELEMENTS (modes); i++) {
485 486
    s = gst_decklink_mode_get_structure ((GstDecklinkModeEnum) i, f);
    caps = gst_caps_merge_structure (caps, s);
David Schleef's avatar
David Schleef committed
487 488 489 490
  }

  return caps;
}
491

492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
GstCaps *
gst_decklink_mode_get_template_caps (void)
{
  int i;
  GstCaps *caps;

  caps = gst_caps_new_empty ();
  for (i = 1; i < (int) G_N_ELEMENTS (modes); i++)
    caps =
        gst_caps_merge (caps,
        gst_decklink_mode_get_caps_all_formats ((GstDecklinkModeEnum) i));

  return caps;
}

507
const GstDecklinkMode *
508 509
gst_decklink_find_mode_and_format_for_caps (GstCaps * caps,
    BMDPixelFormat * format)
510 511 512 513
{
  int i;
  GstCaps *mode_caps;

514 515 516 517
  g_return_val_if_fail (gst_caps_is_fixed (caps), NULL);
  if (!gst_decklink_caps_get_pixel_format (caps, format))
    return NULL;

518
  for (i = 1; i < (int) G_N_ELEMENTS (modes); i++) {
519
    mode_caps = gst_decklink_mode_get_caps ((GstDecklinkModeEnum) i, *format);
520 521 522 523 524 525 526 527 528 529
    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;
}

530 531 532 533 534 535 536 537
const GstDecklinkMode *
gst_decklink_find_mode_for_caps (GstCaps * caps)
{
  BMDPixelFormat format;

  return gst_decklink_find_mode_and_format_for_caps (caps, &format);
}

538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
#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
555
{
556 557
  GstSystemClock clock;

558 559
  GstDecklinkInput *input;
  GstDecklinkOutput *output;
560 561
};

562 563 564 565 566 567
struct _GstDecklinkClockClass
{
  GstSystemClockClass parent_class;
};

GType gst_decklink_clock_get_type (void);
568
static GstClock *gst_decklink_clock_new (const gchar * name);
569 570 571 572 573 574 575 576

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

577 578 579 580 581 582 583 584
class GStreamerDecklinkInputCallback:public IDeckLinkInputCallback
{
private:
  GstDecklinkInput * m_input;
  GMutex m_mutex;
  gint m_refcount;
public:
    GStreamerDecklinkInputCallback (GstDecklinkInput * input)
585
  : IDeckLinkInputCallback (), m_refcount (1)
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
  {
    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,
632
      IDeckLinkDisplayMode * mode, BMDDetectedVideoInputFormatFlags formatFlags)
633
  {
634 635
    BMDPixelFormat pixelFormat = bmdFormat8BitYUV;

636 637
    GST_INFO ("Video input format changed");

638 639 640
    if (formatFlags & bmdDetectedVideoInputRGB444)
      pixelFormat = bmdFormat8BitARGB;

641 642 643
    g_mutex_lock (&m_input->lock);
    m_input->input->PauseStreams ();
    m_input->input->EnableVideoInput (mode->GetDisplayMode (),
644
        pixelFormat, bmdVideoInputEnableFormatDetection);
645 646 647 648 649
    m_input->input->FlushStreams ();
    m_input->input->StartStreams ();
    m_input->mode =
        gst_decklink_get_mode (gst_decklink_get_mode_enum_from_bmd
        (mode->GetDisplayMode ()));
650
    m_input->format = pixelFormat;
651 652
    g_mutex_unlock (&m_input->lock);

653 654 655 656 657 658 659
    return S_OK;
  }

  virtual HRESULT STDMETHODCALLTYPE
      VideoInputFrameArrived (IDeckLinkVideoInputFrame * video_frame,
      IDeckLinkAudioInputPacket * audio_packet)
  {
660 661
    GstElement *videosrc = NULL, *audiosrc = NULL;
    void (*got_video_frame) (GstElement * videosrc,
662 663
        IDeckLinkVideoInputFrame * frame, GstDecklinkModeEnum mode,
        GstClockTime capture_time, GstClockTime capture_duration) = NULL;
664
    void (*got_audio_packet) (GstElement * videosrc,
665 666
        IDeckLinkAudioInputPacket * packet, GstClockTime capture_time,
        gboolean discont) = NULL;
667
    GstDecklinkModeEnum mode;
668 669
    BMDTimeValue capture_time = GST_CLOCK_TIME_NONE, capture_duration =
        GST_CLOCK_TIME_NONE;
670 671
    HRESULT res;

672 673 674
    if (video_frame == NULL)
      goto no_video_frame;

675 676 677 678 679 680 681 682
    res =
        video_frame->GetHardwareReferenceTimestamp (GST_SECOND, &capture_time,
        &capture_duration);
    if (res != S_OK) {
      GST_ERROR ("Failed to get capture time: 0x%08x", res);
      capture_time = GST_CLOCK_TIME_NONE;
      capture_duration = GST_CLOCK_TIME_NONE;
    }
683 684

    g_mutex_lock (&m_input->lock);
685

686
    if (capture_time > (BMDTimeValue) m_input->clock_start_time)
687 688 689 690
      capture_time -= m_input->clock_start_time;
    else
      capture_time = 0;

691
    if (capture_time > (BMDTimeValue) m_input->clock_offset)
692 693 694 695
      capture_time -= m_input->clock_offset;
    else
      capture_time = 0;

696 697 698
    if (m_input->videosrc) {
      videosrc = GST_ELEMENT_CAST (gst_object_ref (m_input->videosrc));
      got_video_frame = m_input->got_video_frame;
699
    }
700
    mode = gst_decklink_get_mode_enum_from_bmd (m_input->mode->mode);
701

702 703 704
    if (m_input->audiosrc) {
      audiosrc = GST_ELEMENT_CAST (gst_object_ref (m_input->audiosrc));
      got_audio_packet = m_input->got_audio_packet;
705 706
    }
    g_mutex_unlock (&m_input->lock);
707 708

    if (got_video_frame && videosrc) {
709 710
      got_video_frame (videosrc, video_frame, mode, capture_time,
          capture_duration);
711 712
    }

713
  no_video_frame:
714
    if (got_audio_packet && audiosrc && audio_packet) {
715 716
      m_input->got_audio_packet (audiosrc, audio_packet, capture_time,
          m_input->audio_discont);
717 718 719 720
      m_input->audio_discont = FALSE;
    } else {
      m_input->audio_discont = TRUE;
      if (!audio_packet)
721 722
        GST_DEBUG ("Received no audio packet at %" GST_TIME_FORMAT,
            GST_TIME_ARGS (capture_time));
723 724 725 726 727
    }

    gst_object_replace ((GstObject **) & videosrc, NULL);
    gst_object_replace ((GstObject **) & audiosrc, NULL);

728 729 730 731
    return S_OK;
  }
};

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 778 779 780 781 782
#ifdef _MSC_VER
/* FIXME: We currently never deinit this */

static GMutex com_init_lock;
static GMutex com_deinit_lock;
static GCond com_init_cond;
static GCond com_deinit_cond;
static GCond com_deinited_cond;
static gboolean com_initialized = FALSE;

/* COM initialization/uninitialization thread */
static gpointer
gst_decklink_com_thread (gpointer data)
{
  HRESULT res;

  g_mutex_lock (&com_init_lock);

  /* Initialize COM with a MTA for this process. This thread will
   * be the first one to enter the apartement and the last one to leave
   * it, unitializing COM properly */

  res = CoInitializeEx (0, COINIT_MULTITHREADED);
  if (res == S_FALSE)
    GST_WARNING ("COM has been already initialized in the same process");
  else if (res == RPC_E_CHANGED_MODE)
    GST_WARNING ("The concurrency model of COM has changed.");
  else
    GST_INFO ("COM intialized succesfully");

  com_initialized = TRUE;

  /* Signal other threads waiting on this condition that COM was initialized */
  g_cond_signal (&com_init_cond);

  g_mutex_unlock (&com_init_lock);

  /* Wait until the unitialize condition is met to leave the COM apartement */
  g_mutex_lock (&com_deinit_lock);
  g_cond_wait (&com_deinit_cond, &com_deinit_lock);

  CoUninitialize ();
  GST_INFO ("COM unintialized succesfully");
  com_initialized = FALSE;
  g_cond_signal (&com_deinited_cond);
  g_mutex_unlock (&com_deinit_lock);

  return NULL;
}
#endif /* _MSC_VER */

783
static GOnce devices_once = G_ONCE_INIT;
784 785 786
static int n_devices;
static Device devices[10];

787 788
static gpointer
init_devices (gpointer data)
789 790 791 792 793 794
{
  IDeckLinkIterator *iterator;
  IDeckLink *decklink = NULL;
  HRESULT ret;
  int i;

795 796 797 798 799 800 801 802 803 804 805 806 807
#ifdef _MSC_VER
  // Start COM thread for Windows

  g_mutex_lock (&com_init_lock);

  /* create the COM initialization thread */
  g_thread_create ((GThreadFunc) gst_decklink_com_thread, NULL, FALSE, NULL);

  /* wait until the COM thread signals that COM has been initialized */
  g_cond_wait (&com_init_cond, &com_init_lock);
  g_mutex_unlock (&com_init_lock);
#endif /* _MSC_VER */

808 809 810
  iterator = CreateDeckLinkIteratorInstance ();
  if (iterator == NULL) {
    GST_ERROR ("no driver");
811
    return NULL;
812 813
  }

814
  i = 0;
815
  ret = iterator->Next (&decklink);
816 817
  while (ret == S_OK) {
    ret = decklink->QueryInterface (IID_IDeckLinkInput,
818
        (void **) &devices[i].input.input);
819 820
    if (ret != S_OK) {
      GST_WARNING ("selected device does not have input interface");
821 822
    } else {
      devices[i].input.device = decklink;
823
      devices[i].input.clock = gst_decklink_clock_new ("GstDecklinkInputClock");
824
      GST_DECKLINK_CLOCK_CAST (devices[i].input.clock)->input =
825
          &devices[i].input;
826 827 828
      devices[i].input.
          input->SetCallback (new GStreamerDecklinkInputCallback (&devices[i].
              input));
829 830 831
    }

    ret = decklink->QueryInterface (IID_IDeckLinkOutput,
832
        (void **) &devices[i].output.output);
833
    if (ret != S_OK) {
834
      GST_WARNING ("selected device does not have output interface");
835 836
    } else {
      devices[i].output.device = decklink;
837 838
      devices[i].output.clock =
          gst_decklink_clock_new ("GstDecklinkOutputClock");
839
      GST_DECKLINK_CLOCK_CAST (devices[i].output.clock)->output =
840
          &devices[i].output;
841
    }
842 843

    ret = decklink->QueryInterface (IID_IDeckLinkConfiguration,
844
        (void **) &devices[i].input.config);
845 846 847 848
    if (ret != S_OK) {
      GST_WARNING ("selected device does not have config interface");
    }

849 850 851 852 853 854
    ret = decklink->QueryInterface (IID_IDeckLinkAttributes,
        (void **) &devices[i].input.attributes);
    if (ret != S_OK) {
      GST_WARNING ("selected device does not have attributes interface");
    }

855 856
    ret = iterator->Next (&decklink);
    i++;
857 858 859 860 861

    if (i == 10) {
      GST_WARNING ("this hardware has more then 10 devices");
      break;
    }
862 863
  }

864 865
  n_devices = i;

866
  iterator->Release ();
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900

  return NULL;
}

GstDecklinkOutput *
gst_decklink_acquire_nth_output (gint n, GstElement * sink, gboolean is_audio)
{
  GstDecklinkOutput *output;

  g_once (&devices_once, init_devices, NULL);

  if (n >= n_devices)
    return NULL;

  output = &devices[n].output;
  if (!output->output) {
    GST_ERROR ("Device %d has no output", n);
    return NULL;
  }

  g_mutex_lock (&output->lock);
  if (is_audio && !output->audiosink) {
    output->audiosink = GST_ELEMENT_CAST (gst_object_ref (sink));
    g_mutex_unlock (&output->lock);
    return output;
  } else if (!output->videosink) {
    output->videosink = GST_ELEMENT_CAST (gst_object_ref (sink));
    g_mutex_unlock (&output->lock);
    return output;
  }
  g_mutex_unlock (&output->lock);

  GST_ERROR ("Output device %d (audio: %d) in use already", n, is_audio);
  return NULL;
901 902
}

903 904
void
gst_decklink_release_nth_output (gint n, GstElement * sink, gboolean is_audio)
905
{
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
  GstDecklinkOutput *output;

  if (n >= n_devices)
    return;

  output = &devices[n].output;
  g_assert (output->output);

  g_mutex_lock (&output->lock);
  if (is_audio) {
    g_assert (output->audiosink == sink);
    gst_object_unref (sink);
    output->audiosink = NULL;
  } else {
    g_assert (output->videosink == sink);
    gst_object_unref (sink);
    output->videosink = NULL;
  }
  g_mutex_unlock (&output->lock);
925 926
}

927 928 929
void
gst_decklink_output_set_audio_clock (GstDecklinkOutput * output,
    GstClock * clock)
930
{
931 932 933 934 935 936 937
  g_mutex_lock (&output->lock);
  if (output->audio_clock)
    gst_object_unref (output->audio_clock);
  output->audio_clock = clock;
  if (clock)
    gst_object_ref (clock);
  g_mutex_unlock (&output->lock);
938 939
}

940 941 942

GstClock *
gst_decklink_output_get_audio_clock (GstDecklinkOutput * output)
943
{
944 945 946 947 948 949 950 951
  GstClock *ret = NULL;

  g_mutex_lock (&output->lock);
  if (output->audio_clock)
    ret = GST_CLOCK_CAST (gst_object_ref (output->audio_clock));
  g_mutex_unlock (&output->lock);

  return ret;
952 953
}

954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
GstDecklinkInput *
gst_decklink_acquire_nth_input (gint n, GstElement * src, gboolean is_audio)
{
  GstDecklinkInput *input;

  g_once (&devices_once, init_devices, NULL);

  if (n >= n_devices)
    return NULL;

  input = &devices[n].input;
  if (!input->input) {
    GST_ERROR ("Device %d has no input", n);
    return NULL;
  }

  g_mutex_lock (&input->lock);
  if (is_audio && !input->audiosrc) {
    input->audiosrc = GST_ELEMENT_CAST (gst_object_ref (src));
973
    input->audio_discont = TRUE;
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 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
    g_mutex_unlock (&input->lock);
    return input;
  } else if (!input->videosrc) {
    input->videosrc = GST_ELEMENT_CAST (gst_object_ref (src));
    g_mutex_unlock (&input->lock);
    return input;
  }
  g_mutex_unlock (&input->lock);

  GST_ERROR ("Input device %d (audio: %d) in use already", n, is_audio);
  return NULL;
}

void
gst_decklink_release_nth_input (gint n, GstElement * src, gboolean is_audio)
{
  GstDecklinkInput *input;

  if (n >= n_devices)
    return;

  input = &devices[n].input;
  g_assert (input->input);

  g_mutex_lock (&input->lock);
  if (is_audio) {
    g_assert (input->audiosrc == src);
    gst_object_unref (src);
    input->audiosrc = NULL;
  } else {
    g_assert (input->videosrc == src);
    gst_object_unref (src);
    input->videosrc = NULL;
  }
  g_mutex_unlock (&input->lock);
}

1011 1012 1013 1014 1015 1016
G_DEFINE_TYPE (GstDecklinkClock, gst_decklink_clock, GST_TYPE_SYSTEM_CLOCK);

static GstClockTime gst_decklink_clock_get_internal_time (GstClock * clock);

static void
gst_decklink_clock_class_init (GstDecklinkClockClass * klass)
1017
{
1018 1019 1020
  GstClockClass *clock_class = (GstClockClass *) klass;

  clock_class->get_internal_time = gst_decklink_clock_get_internal_time;
1021 1022
}

1023 1024
static void
gst_decklink_clock_init (GstDecklinkClock * clock)
1025
{
1026 1027 1028
  GST_OBJECT_FLAG_SET (clock, GST_CLOCK_FLAG_CAN_SET_MASTER);
}

1029
static GstClock *
1030 1031 1032 1033 1034 1035
gst_decklink_clock_new (const gchar * name)
{
  GstDecklinkClock *self =
      GST_DECKLINK_CLOCK (g_object_new (GST_TYPE_DECKLINK_CLOCK, "name", name,
          "clock-type", GST_CLOCK_TYPE_OTHER, NULL));

1036
  return GST_CLOCK_CAST (self);
1037 1038 1039 1040 1041 1042
}

static GstClockTime
gst_decklink_clock_get_internal_time (GstClock * clock)
{
  GstDecklinkClock *self = GST_DECKLINK_CLOCK (clock);
1043 1044
  GstClockTime result, start_time, last_time;
  GstClockTimeDiff offset;
1045 1046 1047 1048
  BMDTimeValue time;
  HRESULT ret;

  if (self->input != NULL) {
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
    g_mutex_lock (&self->input->lock);
    start_time = self->input->clock_start_time;
    offset = self->input->clock_offset;
    last_time = self->input->clock_last_time;
    time = -1;
    if (!self->input->started) {
      result = last_time;
      ret = -1;
    } else {
      ret =
          self->input->input->GetHardwareReferenceClock (GST_SECOND, &time,
          NULL, NULL);
      if (ret == S_OK && time >= 0) {
        result = time;
        if (start_time == GST_CLOCK_TIME_NONE)
          start_time = self->input->clock_start_time = result;

        if (result > start_time)
          result -= start_time;
        else
          result = 0;

        if (self->input->clock_restart) {
          self->input->clock_offset = result - last_time;
          offset = self->input->clock_offset;
          self->input->clock_restart = FALSE;
        }
        result = MAX (last_time, result);
        result -= offset;
        result = MAX (last_time, result);
      } else {
        result = last_time;
      }

      self->input->clock_last_time = result;
    }
1085
    result += self->input->clock_epoch;
1086
    g_mutex_unlock (&self->input->lock);
1087
  } else if (self->output != NULL) {
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
    g_mutex_lock (&self->output->lock);
    start_time = self->output->clock_start_time;
    offset = self->output->clock_offset;
    last_time = self->output->clock_last_time;
    time = -1;
    if (!self->output->started) {
      result = last_time;
      ret = -1;
    } else {
      ret =
          self->output->output->GetHardwareReferenceClock (GST_SECOND, &time,
          NULL, NULL);
      if (ret == S_OK && time >= 0) {
        result = time;

        if (start_time == GST_CLOCK_TIME_NONE)
          start_time = self->output->clock_start_time = result;

        if (result > start_time)
          result -= start_time;
        else
          result = 0;

        if (self->output->clock_restart) {
          self->output->clock_offset = result - last_time;
          offset = self->output->clock_offset;
          self->output->clock_restart = FALSE;
        }
        result = MAX (last_time, result);
        result -= offset;
        result = MAX (last_time, result);
      } else {
        result = last_time;
      }

      self->output->clock_last_time = result;
    }
1125
    result += self->output->clock_epoch;
1126
    g_mutex_unlock (&self->output->lock);
1127
  } else {
1128
    g_assert_not_reached ();
1129
  }
1130 1131 1132 1133 1134 1135
  GST_LOG_OBJECT (clock,
      "result %" GST_TIME_FORMAT " time %" GST_TIME_FORMAT " last time %"
      GST_TIME_FORMAT " offset %" GST_TIME_FORMAT " start time %"
      GST_TIME_FORMAT " (ret: 0x%08x)", GST_TIME_ARGS (result),
      GST_TIME_ARGS (time), GST_TIME_ARGS (last_time), GST_TIME_ARGS (offset),
      GST_TIME_ARGS (start_time), ret);
1136

1137 1138
  return result;
}
1139

1140 1141 1142 1143 1144
static gboolean
plugin_init (GstPlugin * plugin)
{
  GST_DEBUG_CATEGORY_INIT (gst_decklink_debug, "decklink", 0,
      "debug category for decklink plugin");
1145

1146 1147 1148 1149
  gst_element_register (plugin, "decklinkaudiosink", GST_RANK_NONE,
      GST_TYPE_DECKLINK_AUDIO_SINK);
  gst_element_register (plugin, "decklinkvideosink", GST_RANK_NONE,
      GST_TYPE_DECKLINK_VIDEO_SINK);
1150 1151 1152 1153
  gst_element_register (plugin, "decklinkaudiosrc", GST_RANK_NONE,
      GST_TYPE_DECKLINK_AUDIO_SRC);
  gst_element_register (plugin, "decklinkvideosrc", GST_RANK_NONE,
      GST_TYPE_DECKLINK_VIDEO_SRC);
1154 1155 1156 1157 1158
  return TRUE;
}

GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
    GST_VERSION_MINOR,
1159
    decklink,
1160 1161
    "Blackmagic Decklink plugin",
    plugin_init, VERSION, "LGPL", PACKAGE_NAME, GST_PACKAGE_ORIGIN)