test-display.c 14.8 KB
Newer Older
gb's avatar
gb committed
1 2 3
/*
 *  test-display.c - Test GstVaapiDisplayX11
 *
4
 *  Copyright (C) 2010-2011 Splitted-Desktop Systems
5
 *    Author: Gwenole Beauchesne <gwenole.beauchesne@splitted-desktop.com>
6
 *  Copyright (C) 2012-2013 Intel Corporation
7
 *    Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
gb's avatar
gb committed
8
 *
warly's avatar
warly committed
9 10 11 12
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2.1
 *  of the License, or (at your option) any later version.
gb's avatar
gb committed
13
 *
warly's avatar
warly committed
14
 *  This library is distributed in the hope that it will be useful,
gb's avatar
gb committed
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
warly's avatar
warly committed
16 17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
gb's avatar
gb committed
18
 *
warly's avatar
warly committed
19 20 21 22
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301 USA
23
 */
gb's avatar
gb committed
24

25
#include "gst/vaapi/sysdeps.h"
gb's avatar
gb committed
26
#include <gst/video/video.h>
27 28 29 30 31 32 33 34 35
#if USE_DRM
# include <gst/vaapi/gstvaapidisplay_drm.h>
# include <va/va_drm.h>
# include <fcntl.h>
# include <unistd.h>
# ifndef DRM_DEVICE_PATH
# define DRM_DEVICE_PATH "/dev/dri/card0"
# endif
#endif
36
#if USE_X11
37
# include <gst/vaapi/gstvaapidisplay_x11.h>
gb's avatar
gb committed
38
#endif
39
#if USE_GLX
40 41 42 43
# include <gst/vaapi/gstvaapidisplay_glx.h>
#endif
#if USE_WAYLAND
# include <gst/vaapi/gstvaapidisplay_wayland.h>
gb's avatar
gb committed
44
#endif
45 46 47
#if USE_EGL
# include <gst/vaapi/gstvaapidisplay_egl.h>
#endif
gb's avatar
gb committed
48

49
#ifdef HAVE_VA_VA_GLX_H
50 51 52
# include <va/va_glx.h>
#endif

53 54 55
/* Set to 1 to check display cache works (shared VA display) */
#define CHECK_DISPLAY_CACHE 1

56
static void
57
print_value (const GValue * value, const gchar * name)
58
{
59
  gchar *value_string;
60

61 62 63 64 65
  value_string = g_strdup_value_contents (value);
  if (!value_string)
    return;
  g_print ("  %s: %s\n", name, value_string);
  g_free (value_string);
66 67
}

gb's avatar
gb committed
68
static void
69
print_profiles (GArray * profiles, const gchar * name)
gb's avatar
gb committed
70
{
71 72 73
  GstVaapiCodec codec;
  const gchar *codec_name, *profile_name;
  guint i;
74

75
  g_print ("%u %s caps\n", profiles->len, name);
76

77 78 79
  for (i = 0; i < profiles->len; i++) {
    const GstVaapiProfile profile =
        g_array_index (profiles, GstVaapiProfile, i);
80

81 82 83
    codec = gst_vaapi_profile_get_codec (profile);
    if (!codec)
      continue;
84

85 86 87
    codec_name = gst_vaapi_codec_get_name (codec);
    if (!codec_name)
      continue;
88

89 90 91
    profile_name = gst_vaapi_profile_get_name (profile);
    if (!profile_name)
      continue;
92

93 94
    g_print ("  %s: %s profile\n", codec_name, profile_name);
  }
gb's avatar
gb committed
95 96
}

97
static void
98
print_format_yuv (const VAImageFormat * va_format)
99
{
100
  const guint32 fourcc = va_format->fourcc;
101

102 103 104
  g_print (" fourcc '%c%c%c%c'",
      fourcc & 0xff,
      (fourcc >> 8) & 0xff, (fourcc >> 16) & 0xff, (fourcc >> 24) & 0xff);
105 106 107
}

static void
108
print_format_rgb (const VAImageFormat * va_format)
109
{
110 111 112 113 114 115 116 117
  g_print (" %d bits per pixel, %s endian,",
      va_format->bits_per_pixel,
      va_format->byte_order == VA_MSB_FIRST ? "big" : "little");
  g_print (" %s masks", va_format->alpha_mask ? "rgba" : "rgb");
  g_print (" 0x%08x 0x%08x 0x%08x",
      va_format->red_mask, va_format->green_mask, va_format->blue_mask);
  if (va_format->alpha_mask)
    g_print (" 0x%08x", va_format->alpha_mask);
118 119
}

gb's avatar
gb committed
120
static void
121
print_formats (GArray * formats, const gchar * name)
gb's avatar
gb committed
122
{
123
  guint i;
gb's avatar
gb committed
124

125
  g_print ("%u %s caps\n", formats->len, name);
gb's avatar
gb committed
126

127 128 129
  for (i = 0; i < formats->len; i++) {
    const GstVideoFormat format = g_array_index (formats, GstVideoFormat, i);
    const VAImageFormat *va_format;
130

131
    g_print ("  %s:", gst_vaapi_video_format_to_string (format));
132

133 134 135
    va_format = gst_vaapi_video_format_to_va_format (format);
    if (!va_format)
      g_error ("could not determine VA format");
136

137 138 139 140 141 142
    if (gst_vaapi_video_format_is_yuv (format))
      print_format_yuv (va_format);
    else
      print_format_rgb (va_format);
    g_print ("\n");
  }
gb's avatar
gb committed
143 144
}

145
typedef struct _GstVaapiDisplayProperty GstVaapiDisplayProperty;
146 147 148 149
struct _GstVaapiDisplayProperty
{
  const gchar *name;
  GValue value;
150 151 152
};

static void
153
gst_vaapi_display_property_free (GstVaapiDisplayProperty * prop)
154
{
155 156 157 158
  if (!prop)
    return;
  g_value_unset (&prop->value);
  g_slice_free (GstVaapiDisplayProperty, prop);
159 160 161
}

static GstVaapiDisplayProperty *
162
gst_vaapi_display_property_new (const gchar * name)
163
{
164
  GstVaapiDisplayProperty *prop;
165

166 167 168 169 170
  prop = g_slice_new0 (GstVaapiDisplayProperty);
  if (!prop)
    return NULL;
  prop->name = name;
  return prop;
171 172 173
}

static void
174
free_property_cb (gpointer data, gpointer user_data)
175
{
176
  gst_vaapi_display_property_free (data);
177 178 179
}

static void
180
dump_properties (GstVaapiDisplay * display)
181
{
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
  GstVaapiDisplayProperty *prop;
  GPtrArray *properties;
  guint i;

  static const gchar *g_properties[] = {
    GST_VAAPI_DISPLAY_PROP_RENDER_MODE,
    GST_VAAPI_DISPLAY_PROP_ROTATION,
    GST_VAAPI_DISPLAY_PROP_HUE,
    GST_VAAPI_DISPLAY_PROP_SATURATION,
    GST_VAAPI_DISPLAY_PROP_BRIGHTNESS,
    GST_VAAPI_DISPLAY_PROP_CONTRAST,
    NULL
  };

  properties = g_ptr_array_new ();
  if (!properties)
    return;

  for (i = 0; g_properties[i] != NULL; i++) {
    const gchar *const name = g_properties[i];

    if (!gst_vaapi_display_has_property (display, name))
      continue;

    prop = gst_vaapi_display_property_new (name);
    if (!prop) {
      GST_ERROR ("failed to allocate GstVaapiDisplayProperty");
      goto end;
210 211
    }

212 213 214
    if (!gst_vaapi_display_get_property (display, name, &prop->value)) {
      GST_ERROR ("failed to get property '%s'", name);
      goto end;
215
    }
216 217 218 219 220 221 222 223
    g_ptr_array_add (properties, prop);
  }

  g_print ("%u properties\n", properties->len);
  for (i = 0; i < properties->len; i++) {
    prop = g_ptr_array_index (properties, i);
    print_value (&prop->value, prop->name);
  }
224 225

end:
226 227 228 229
  if (properties) {
    g_ptr_array_foreach (properties, free_property_cb, NULL);
    g_ptr_array_free (properties, TRUE);
  }
230 231
}

232
static void
233
dump_info (GstVaapiDisplay * display)
gb's avatar
gb committed
234
{
235
  GArray *profiles, *formats;
gb's avatar
gb committed
236

237 238 239
  profiles = gst_vaapi_display_get_decode_profiles (display);
  if (!profiles)
    g_error ("could not get VA decode profiles");
gb's avatar
gb committed
240

241 242
  print_profiles (profiles, "decoders");
  g_array_unref (profiles);
gb's avatar
gb committed
243

244 245 246
  profiles = gst_vaapi_display_get_encode_profiles (display);
  if (!profiles)
    g_error ("could not get VA encode profiles");
gb's avatar
gb committed
247

248 249
  print_profiles (profiles, "encoders");
  g_array_unref (profiles);
gb's avatar
gb committed
250

251 252 253
  formats = gst_vaapi_display_get_image_formats (display);
  if (!formats)
    g_error ("could not get VA image formats");
gb's avatar
gb committed
254

255 256
  print_formats (formats, "image");
  g_array_unref (formats);
gb's avatar
gb committed
257

258 259 260
  formats = gst_vaapi_display_get_subpicture_formats (display);
  if (!formats)
    g_error ("could not get VA subpicture formats");
gb's avatar
gb committed
261

262 263
  print_formats (formats, "subpicture");
  g_array_unref (formats);
264

265
  dump_properties (display);
266 267 268
}

int
269
main (int argc, char *argv[])
270
{
271 272 273 274 275 276 277 278
  GstVaapiDisplay *display;
#if USE_X11
  GstVaapiDisplay *display2;
#endif
#if USE_X11 || USE_WAYLAND
  guint width, height;
  guint par_n, par_d;
#endif
279

280
  gst_init (&argc, &argv);
281

282
#if USE_DRM
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
  g_print ("#\n");
  g_print ("# Create display with gst_vaapi_display_drm_new()\n");
  g_print ("#\n");
  {
    display = gst_vaapi_display_drm_new (NULL);
    if (!display)
      g_error ("could not create Gst/VA display");

    dump_info (display);
    gst_vaapi_display_unref (display);
  }
  g_print ("\n");

  g_print ("#\n");
  g_print ("# Create display with gst_vaapi_display_drm_new_with_device()\n");
  g_print ("#\n");
  {
    int drm_device;

    drm_device = open (DRM_DEVICE_PATH, O_RDWR | O_CLOEXEC);
    if (drm_device < 0)
      g_error ("could not open DRM device");

    display = gst_vaapi_display_drm_new_with_device (drm_device);
    if (!display)
      g_error ("could not create Gst/VA display");

    dump_info (display);
    gst_vaapi_display_unref (display);
    close (drm_device);
  }
  g_print ("\n");

  g_print ("#\n");
  g_print
      ("# Create display with gst_vaapi_display_new_with_display() [vaGetDisplayDRM()]\n");
  g_print ("#\n");
  {
    int drm_device;
    VADisplay va_display;

    drm_device = open (DRM_DEVICE_PATH, O_RDWR | O_CLOEXEC);
    if (drm_device < 0)
      g_error ("could not open DRM device");

    va_display = vaGetDisplayDRM (drm_device);
    if (!va_display)
      g_error ("could not create VA display");

    display = gst_vaapi_display_new_with_display (va_display);
    if (!display)
      g_error ("could not create Gst/VA display");

    dump_info (display);
    gst_vaapi_display_unref (display);
    close (drm_device);
  }
  g_print ("\n");
341 342
#endif

343
#if USE_X11
344 345 346 347 348 349 350
  g_print ("#\n");
  g_print ("# Create display with gst_vaapi_display_x11_new()\n");
  g_print ("#\n");
  {
    display = gst_vaapi_display_x11_new (NULL);
    if (!display)
      g_error ("could not create Gst/VA display");
351

352 353
    if (CHECK_DISPLAY_CACHE) {
      display2 = gst_vaapi_display_x11_new (NULL);
354

355 356 357 358
      /* Check for the same X11 display */
      g_assert (gst_vaapi_display_x11_get_display (GST_VAAPI_DISPLAY_X11
              (display)) ==
          gst_vaapi_display_x11_get_display (GST_VAAPI_DISPLAY_X11 (display2)));
359

360 361 362
      /* Check for the same VA display */
      g_assert (gst_vaapi_display_get_display (display) ==
          gst_vaapi_display_get_display (display2));
363

364
      gst_vaapi_display_unref (display2);
365 366

#if USE_GLX
367
      display2 = gst_vaapi_display_glx_new (NULL);
368

369 370 371 372 373
      /* Check for the different X11 display */
      /* XXX: it is also desired to cache underlying X11 displays */
      g_assert (gst_vaapi_display_x11_get_display (GST_VAAPI_DISPLAY_X11
              (display)) !=
          gst_vaapi_display_x11_get_display (GST_VAAPI_DISPLAY_X11 (display2)));
374

375 376 377
      /* Check for different VA display */
      g_assert (gst_vaapi_display_get_display (display) !=
          gst_vaapi_display_get_display (display2));
378

379
      gst_vaapi_display_unref (display2);
380
#endif
381
    }
gb's avatar
gb committed
382

383 384
    gst_vaapi_display_get_size (display, &width, &height);
    g_print ("Display size: %ux%u\n", width, height);
385

386 387
    gst_vaapi_display_get_pixel_aspect_ratio (display, &par_n, &par_d);
    g_print ("Pixel aspect ratio: %u/%u\n", par_n, par_d);
388

389 390 391 392
    dump_info (display);
    gst_vaapi_display_unref (display);
  }
  g_print ("\n");
393

394 395 396 397 398
  g_print ("#\n");
  g_print ("# Create display with gst_vaapi_display_x11_new_with_display()\n");
  g_print ("#\n");
  {
    Display *x11_display;
399

400 401 402
    x11_display = XOpenDisplay (NULL);
    if (!x11_display)
      g_error ("could not create X11 display");
403

404 405 406
    display = gst_vaapi_display_x11_new_with_display (x11_display);
    if (!display)
      g_error ("could not create Gst/VA display");
407

408 409
    if (CHECK_DISPLAY_CACHE) {
      display2 = gst_vaapi_display_x11_new_with_display (x11_display);
410

411 412 413
      /* Check for the same VA display */
      g_assert (gst_vaapi_display_get_display (display) ==
          gst_vaapi_display_get_display (display2));
414

415
      gst_vaapi_display_unref (display2);
416
    }
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448

    dump_info (display);
    gst_vaapi_display_unref (display);
    XCloseDisplay (x11_display);
  }
  g_print ("\n");

  g_print ("#\n");
  g_print
      ("# Create display with gst_vaapi_display_new_with_display() [vaGetDisplay()]\n");
  g_print ("#\n");
  {
    Display *x11_display;
    VADisplay va_display;

    x11_display = XOpenDisplay (NULL);
    if (!x11_display)
      g_error ("could not create X11 display");

    va_display = vaGetDisplay (x11_display);
    if (!va_display)
      g_error ("could not create VA display");

    display = gst_vaapi_display_new_with_display (va_display);
    if (!display)
      g_error ("could not create Gst/VA display");

    dump_info (display);
    gst_vaapi_display_unref (display);
    XCloseDisplay (x11_display);
  }
  g_print ("\n");
gb's avatar
gb committed
449 450
#endif

451
#if USE_GLX
452 453 454 455 456 457 458
  g_print ("#\n");
  g_print ("# Create display with gst_vaapi_display_glx_new()\n");
  g_print ("#\n");
  {
    display = gst_vaapi_display_glx_new (NULL);
    if (!display)
      g_error ("could not create Gst/VA display");
gb's avatar
gb committed
459

460 461
    if (CHECK_DISPLAY_CACHE) {
      display2 = gst_vaapi_display_glx_new (NULL);
462

463 464 465 466
      /* Check for the same X11 display */
      g_assert (gst_vaapi_display_x11_get_display (GST_VAAPI_DISPLAY_X11
              (display)) ==
          gst_vaapi_display_x11_get_display (GST_VAAPI_DISPLAY_X11 (display2)));
467

468 469 470
      /* Check for the same VA display */
      g_assert (gst_vaapi_display_get_display (display) ==
          gst_vaapi_display_get_display (display2));
471

472
      gst_vaapi_display_unref (display2);
473

474
      display2 = gst_vaapi_display_x11_new (NULL);
475

476 477 478 479
      /* Check for the same X11 display */
      g_assert (gst_vaapi_display_x11_get_display (GST_VAAPI_DISPLAY_X11
              (display)) ==
          gst_vaapi_display_x11_get_display (GST_VAAPI_DISPLAY_X11 (display2)));
480

481 482 483
      /* Check for the same VA display */
      g_assert (gst_vaapi_display_get_display (display) ==
          gst_vaapi_display_get_display (display2));
484

485 486
      gst_vaapi_display_unref (display2);
    }
487

488 489
    gst_vaapi_display_get_size (display, &width, &height);
    g_print ("Display size: %ux%u\n", width, height);
gb's avatar
gb committed
490

491 492
    gst_vaapi_display_get_pixel_aspect_ratio (display, &par_n, &par_d);
    g_print ("Pixel aspect ratio: %u/%u\n", par_n, par_d);
gb's avatar
gb committed
493

494 495 496 497
    dump_info (display);
    gst_vaapi_display_unref (display);
  }
  g_print ("\n");
gb's avatar
gb committed
498

499 500 501 502 503
  g_print ("#\n");
  g_print ("# Create display with gst_vaapi_display_glx_new_with_display()\n");
  g_print ("#\n");
  {
    Display *x11_display;
504

505 506 507
    x11_display = XOpenDisplay (NULL);
    if (!x11_display)
      g_error ("could not create X11 display");
gb's avatar
gb committed
508

509 510 511
    display = gst_vaapi_display_glx_new_with_display (x11_display);
    if (!display)
      g_error ("could not create Gst/VA display");
gb's avatar
gb committed
512

513 514 515 516 517
    dump_info (display);
    gst_vaapi_display_unref (display);
    XCloseDisplay (x11_display);
  }
  g_print ("\n");
gb's avatar
gb committed
518

519
#ifdef HAVE_VA_VA_GLX_H
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
  g_print ("#\n");
  g_print
      ("# Create display with gst_vaapi_display_new_with_display() [vaGetDisplayGLX()]\n");
  g_print ("#\n");
  {
    Display *x11_display;
    VADisplay va_display;

    x11_display = XOpenDisplay (NULL);
    if (!x11_display)
      g_error ("could not create X11 display");

    va_display = vaGetDisplayGLX (x11_display);
    if (!va_display)
      g_error ("could not create VA display");

    display = gst_vaapi_display_new_with_display (va_display);
    if (!display)
      g_error ("could not create Gst/VA display");

    dump_info (display);
    gst_vaapi_display_unref (display);
    XCloseDisplay (x11_display);
  }
  g_print ("\n");
545
#endif
gb's avatar
gb committed
546
#endif
gb's avatar
gb committed
547

548
#if USE_WAYLAND
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
  g_print ("#\n");
  g_print ("# Create display with gst_vaapi_display_wayland_new()\n");
  g_print ("#\n");
  {
    display = gst_vaapi_display_wayland_new (NULL);
    if (!display)
      g_error ("could not create Gst/VA display");

    gst_vaapi_display_get_size (display, &width, &height);
    g_print ("Display size: %ux%u\n", width, height);

    gst_vaapi_display_get_pixel_aspect_ratio (display, &par_n, &par_d);
    g_print ("Pixel aspect ratio: %u/%u\n", par_n, par_d);

    dump_info (display);
    gst_vaapi_display_unref (display);
  }
  g_print ("\n");
567 568
#endif

569 570
  gst_deinit ();
  return 0;
gb's avatar
gb committed
571
}