gstvaapidisplay.c 28.3 KB
Newer Older
gb's avatar
gb committed
1 2 3
/*
 *  gstvaapidisplay.c - VA display abstraction
 *
4
 *  Copyright (C) 2010-2011 Splitted-Desktop Systems
gb's avatar
gb committed
5
 *
6 7 8 9
 *  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
10
 *
11
 *  This library is distributed in the hope that it will be useful,
gb's avatar
gb committed
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
gb's avatar
gb committed
15
 *
16 17 18 19
 *  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
gb's avatar
gb committed
20 21
 */

gb's avatar
gb committed
22
/**
gb's avatar
gb committed
23 24
 * SECTION:gstvaapidisplay
 * @short_description: VA display abstraction
gb's avatar
gb committed
25 26
 */

gb's avatar
gb committed
27
#include "config.h"
28
#include <string.h>
29
#include "gstvaapiutils.h"
gb's avatar
gb committed
30
#include "gstvaapidisplay.h"
31
#include "gstvaapidisplay_priv.h"
gb's avatar
gb committed
32 33

#define DEBUG 1
34
#include "gstvaapidebug.h"
gb's avatar
gb committed
35

gb's avatar
gb committed
36 37
GST_DEBUG_CATEGORY(gst_debug_vaapi);

gb's avatar
gb committed
38 39
G_DEFINE_TYPE(GstVaapiDisplay, gst_vaapi_display, G_TYPE_OBJECT);

40 41 42 43 44 45
typedef struct _GstVaapiConfig GstVaapiConfig;
struct _GstVaapiConfig {
    GstVaapiProfile     profile;
    GstVaapiEntrypoint  entrypoint;
};

gb's avatar
gb committed
46 47 48
enum {
    PROP_0,

gb's avatar
gb committed
49 50 51
    PROP_DISPLAY,
    PROP_WIDTH,
    PROP_HEIGHT
gb's avatar
gb committed
52 53
};

54 55
static GstVaapiDisplayCache *g_display_cache = NULL;

56 57 58 59 60 61 62 63 64 65 66 67 68 69
static inline GstVaapiDisplayCache *
get_display_cache(void)
{
    if (!g_display_cache)
        g_display_cache = gst_vaapi_display_cache_new();
    return g_display_cache;
}

GstVaapiDisplayCache *
gst_vaapi_display_get_cache(void)
{
    return get_display_cache();
}

70 71 72 73 74 75 76 77 78 79 80
static void
free_display_cache(void)
{
    if (!g_display_cache)
        return;
    if (gst_vaapi_display_cache_get_size(g_display_cache) > 0)
        return;
    gst_vaapi_display_cache_free(g_display_cache);
    g_display_cache = NULL;
}

gb's avatar
gb committed
81 82 83 84 85 86 87 88
/* Append GstVaapiImageFormat to formats array */
static inline void
append_format(GArray *formats, GstVaapiImageFormat format)
{
    g_array_append_val(formats, format);
}

/* Append VAImageFormats to formats array */
89
static void
gb's avatar
gb committed
90
append_formats(GArray *formats, const VAImageFormat *va_formats, guint n)
91
{
92
    GstVaapiImageFormat format;
93 94
    gboolean has_YV12 = FALSE;
    gboolean has_I420 = FALSE;
95 96 97 98
    guint i;

    for (i = 0; i < n; i++) {
        const VAImageFormat * const va_format = &va_formats[i];
99

100 101 102 103 104
        format = gst_vaapi_image_format(va_format);
        if (!format) {
            GST_DEBUG("unsupported format %" GST_FOURCC_FORMAT,
                      GST_FOURCC_ARGS(va_format->fourcc));
            continue;
105
        }
106 107 108 109 110 111 112 113 114 115

        switch (format) {
        case GST_VAAPI_IMAGE_YV12:
            has_YV12 = TRUE;
            break;
        case GST_VAAPI_IMAGE_I420:
            has_I420 = TRUE;
            break;
        default:
            break;
116
        }
gb's avatar
gb committed
117
        append_format(formats, format);
118
    }
119 120 121

    /* Append I420 (resp. YV12) format if YV12 (resp. I420) is not
       supported by the underlying driver */
gb's avatar
gb committed
122 123 124 125
    if (has_YV12 && !has_I420)
        append_format(formats, GST_VAAPI_IMAGE_I420);
    else if (has_I420 && !has_YV12)
        append_format(formats, GST_VAAPI_IMAGE_YV12);
126 127 128
}

/* Sort image formats. Prefer YUV formats first */
129 130
static gint
compare_yuv_formats(gconstpointer a, gconstpointer b)
131
{
132 133
    const GstVaapiImageFormat fmt1 = *(GstVaapiImageFormat *)a;
    const GstVaapiImageFormat fmt2 = *(GstVaapiImageFormat *)b;
134 135 136 137 138 139 140

    const gboolean is_fmt1_yuv = gst_vaapi_image_format_is_yuv(fmt1);
    const gboolean is_fmt2_yuv = gst_vaapi_image_format_is_yuv(fmt2);

    if (is_fmt1_yuv != is_fmt2_yuv)
        return is_fmt1_yuv ? -1 : 1;

141 142
    return ((gint)gst_vaapi_image_format_get_score(fmt1) -
            (gint)gst_vaapi_image_format_get_score(fmt2));
143 144 145
}

/* Sort subpicture formats. Prefer RGB formats first */
146 147
static gint
compare_rgb_formats(gconstpointer a, gconstpointer b)
148
{
149 150
    const GstVaapiImageFormat fmt1 = *(GstVaapiImageFormat *)a;
    const GstVaapiImageFormat fmt2 = *(GstVaapiImageFormat *)b;
151 152 153 154 155 156 157

    const gboolean is_fmt1_rgb = gst_vaapi_image_format_is_rgb(fmt1);
    const gboolean is_fmt2_rgb = gst_vaapi_image_format_is_rgb(fmt2);

    if (is_fmt1_rgb != is_fmt2_rgb)
        return is_fmt1_rgb ? -1 : 1;

158 159 160 161
    return ((gint)gst_vaapi_image_format_get_score(fmt1) -
            (gint)gst_vaapi_image_format_get_score(fmt2));
}

162
/* Check if configs array contains profile at entrypoint */
163
static inline gboolean
164 165 166 167 168
find_config(
    GArray             *configs,
    GstVaapiProfile     profile,
    GstVaapiEntrypoint  entrypoint
)
169
{
170
    GstVaapiConfig *config;
171 172
    guint i;

173 174 175
    if (!configs)
        return FALSE;

176 177 178
    for (i = 0; i < configs->len; i++) {
        config = &g_array_index(configs, GstVaapiConfig, i);
        if (config->profile == profile && config->entrypoint == entrypoint)
179
            return TRUE;
180
    }
181 182 183
    return FALSE;
}

184
/* Convert configs array to profiles as GstCaps */
gb's avatar
gb committed
185
static GstCaps *
186
get_profile_caps(GArray *configs)
gb's avatar
gb committed
187
{
188
    GstVaapiConfig *config;
gb's avatar
gb committed
189 190 191
    GstCaps *out_caps, *caps;
    guint i;

192 193 194
    if (!configs)
        return NULL;

gb's avatar
gb committed
195 196 197 198
    out_caps = gst_caps_new_empty();
    if (!out_caps)
        return NULL;

199 200 201
    for (i = 0; i < configs->len; i++) {
        config = &g_array_index(configs, GstVaapiConfig, i);
        caps   = gst_vaapi_profile_get_caps(config->profile);
gb's avatar
gb committed
202
        if (caps)
203
            gst_caps_merge(out_caps, caps);
gb's avatar
gb committed
204 205 206 207
    }
    return out_caps;
}

208 209 210 211 212 213 214 215 216 217 218 219 220 221
/* Check if formats array contains format */
static inline gboolean
find_format(GArray *formats, GstVaapiImageFormat format)
{
    guint i;

    for (i = 0; i < formats->len; i++)
        if (g_array_index(formats, GstVaapiImageFormat, i) == format)
            return TRUE;
    return FALSE;
}

/* Convert formats array to GstCaps */
static GstCaps *
gb's avatar
gb committed
222
get_format_caps(GArray *formats)
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
{
    GstVaapiImageFormat format;
    GstCaps *out_caps, *caps;
    guint i;

    out_caps = gst_caps_new_empty();
    if (!out_caps)
        return NULL;

    for (i = 0; i < formats->len; i++) {
        format = g_array_index(formats, GstVaapiImageFormat, i);
        caps   = gst_vaapi_image_format_get_caps(format);
        if (caps)
            gst_caps_append(out_caps, caps);
    }
    return out_caps;
239 240
}

241 242 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
static void
gst_vaapi_display_calculate_pixel_aspect_ratio(GstVaapiDisplay *display)
{
    GstVaapiDisplayPrivate * const priv = display->priv;
    gdouble ratio, delta;
    gint i, index;

    static const gint par[][2] = {
        {1, 1},         /* regular screen            */
        {16, 15},       /* PAL TV                    */
        {11, 10},       /* 525 line Rec.601 video    */
        {54, 59},       /* 625 line Rec.601 video    */
        {64, 45},       /* 1280x1024 on 16:9 display */
        {5, 3},         /* 1280x1024 on  4:3 display */
        {4, 3}          /*  800x600  on 16:9 display */
    };

    /* First, calculate the "real" ratio based on the X values;
     * which is the "physical" w/h divided by the w/h in pixels of the
     * display */
    if (!priv->width || !priv->height || !priv->width_mm || !priv->height_mm)
        ratio = 1.0;
    else
        ratio = (gdouble)(priv->width_mm * priv->height) /
            (priv->height_mm * priv->width);
    GST_DEBUG("calculated pixel aspect ratio: %f", ratio);

    /* Now, find the one from par[][2] with the lowest delta to the real one */
#define DELTA(idx) (ABS(ratio - ((gdouble)par[idx][0] / par[idx][1])))
    delta = DELTA(0);
    index = 0;

    for (i = 1; i < G_N_ELEMENTS(par); i++) {
        const gdouble this_delta = DELTA(i);
        if (this_delta < delta) {
            index = i;
            delta = this_delta;
        }
    }
#undef DELTA

    priv->par_n = par[index][0];
    priv->par_d = par[index][1];
}

gb's avatar
gb committed
286 287 288 289 290
static void
gst_vaapi_display_destroy(GstVaapiDisplay *display)
{
    GstVaapiDisplayPrivate * const priv = display->priv;

gb's avatar
gb committed
291 292 293 294 295 296 297 298
    if (priv->decoders) {
        g_array_free(priv->decoders, TRUE);
        priv->decoders = NULL;
    }

    if (priv->encoders) {
        g_array_free(priv->encoders, TRUE);
        priv->encoders = NULL;
gb's avatar
gb committed
299 300 301
    }

    if (priv->image_formats) {
302
        g_array_free(priv->image_formats, TRUE);
gb's avatar
gb committed
303 304 305 306
        priv->image_formats = NULL;
    }

    if (priv->subpicture_formats) {
307
        g_array_free(priv->subpicture_formats, TRUE);
gb's avatar
gb committed
308 309 310 311
        priv->subpicture_formats = NULL;
    }

    if (priv->display) {
312 313
        if (!priv->parent)
            vaTerminate(priv->display);
gb's avatar
gb committed
314 315
        priv->display = NULL;
    }
316 317 318 319 320 321

    if (priv->create_display) {
        GstVaapiDisplayClass *klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
        if (klass->close_display)
            klass->close_display(display);
    }
322 323 324 325 326 327

    if (priv->parent) {
        g_object_unref(priv->parent);
        priv->parent = NULL;
    }

328 329 330 331
    if (g_display_cache) {
        gst_vaapi_display_cache_remove(get_display_cache(), display);
        free_display_cache();
    }
gb's avatar
gb committed
332 333 334 335 336 337
}

static gboolean
gst_vaapi_display_create(GstVaapiDisplay *display)
{
    GstVaapiDisplayPrivate * const priv = display->priv;
338
    GstVaapiDisplayCache *cache;
339 340
    gboolean            has_errors      = TRUE;
    VAProfile          *profiles        = NULL;
gb's avatar
gb committed
341
    VAEntrypoint       *entrypoints     = NULL;
342 343
    VAImageFormat      *formats         = NULL;
    unsigned int       *flags           = NULL;
gb's avatar
gb committed
344
    gint                i, j, n, num_entrypoints, major_version, minor_version;
345
    VAStatus            status;
346 347 348 349 350
    GstVaapiDisplayInfo info;
    const GstVaapiDisplayInfo *cached_info = NULL;

    memset(&info, 0, sizeof(info));
    info.display = display;
gb's avatar
gb committed
351

352 353 354
    if (priv->display)
        info.va_display = priv->display;
    else if (priv->create_display) {
355 356 357
        GstVaapiDisplayClass *klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
        if (klass->open_display && !klass->open_display(display))
            return FALSE;
358 359 360
        if (!klass->get_display || !klass->get_display(display, &info))
            return FALSE;
        priv->display = info.va_display;
gb's avatar
gb committed
361 362
        if (klass->get_size)
            klass->get_size(display, &priv->width, &priv->height);
363 364 365
        if (klass->get_size_mm)
            klass->get_size_mm(display, &priv->width_mm, &priv->height_mm);
        gst_vaapi_display_calculate_pixel_aspect_ratio(display);
366 367 368 369
    }
    if (!priv->display)
        return FALSE;

370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
    cache = get_display_cache();
    if (!cache)
        return FALSE;
    cached_info = gst_vaapi_display_cache_lookup_by_va_display(
        cache,
        info.va_display
    );
    if (cached_info) {
        if (priv->parent)
            g_object_unref(priv->parent);
        priv->parent = g_object_ref(cached_info->display);
    }

    if (!priv->parent) {
        status = vaInitialize(priv->display, &major_version, &minor_version);
        if (!vaapi_check_status(status, "vaInitialize()"))
            goto end;
        GST_DEBUG("VA-API version %d.%d", major_version, minor_version);
    }
gb's avatar
gb committed
389 390

    /* VA profiles */
391 392 393
    profiles = g_new(VAProfile, vaMaxNumProfiles(priv->display));
    if (!profiles)
        goto end;
gb's avatar
gb committed
394 395 396
    entrypoints = g_new(VAEntrypoint, vaMaxNumEntrypoints(priv->display));
    if (!entrypoints)
        goto end;
397
    status = vaQueryConfigProfiles(priv->display, profiles, &n);
gb's avatar
gb committed
398
    if (!vaapi_check_status(status, "vaQueryConfigProfiles()"))
399
        goto end;
gb's avatar
gb committed
400

401 402 403 404
    GST_DEBUG("%d profiles", n);
    for (i = 0; i < n; i++)
        GST_DEBUG("  %s", string_of_VAProfile(profiles[i]));

405
    priv->decoders = g_array_new(FALSE, FALSE, sizeof(GstVaapiConfig));
gb's avatar
gb committed
406 407
    if (!priv->decoders)
        goto end;
408
    priv->encoders = g_array_new(FALSE, FALSE, sizeof(GstVaapiConfig));
gb's avatar
gb committed
409
    if (!priv->encoders)
410
        goto end;
gb's avatar
gb committed
411 412

    for (i = 0; i < n; i++) {
413
        GstVaapiConfig config;
gb's avatar
gb committed
414

415 416
        config.profile = gst_vaapi_profile(profiles[i]);
        if (!config.profile)
gb's avatar
gb committed
417 418 419 420 421 422 423 424 425 426 427
            continue;

        status = vaQueryConfigEntrypoints(
            priv->display,
            profiles[i],
            entrypoints, &num_entrypoints
        );
        if (!vaapi_check_status(status, "vaQueryConfigEntrypoints()"))
            goto end;

        for (j = 0; j < num_entrypoints; j++) {
428 429 430 431 432 433
            config.entrypoint = gst_vaapi_entrypoint(entrypoints[j]);
            switch (config.entrypoint) {
            case GST_VAAPI_ENTRYPOINT_VLD:
            case GST_VAAPI_ENTRYPOINT_IDCT:
            case GST_VAAPI_ENTRYPOINT_MOCO:
                g_array_append_val(priv->decoders, config);
gb's avatar
gb committed
434
                break;
435 436
            case GST_VAAPI_ENTRYPOINT_SLICE_ENCODE:
                g_array_append_val(priv->encoders, config);
gb's avatar
gb committed
437 438 439 440
                break;
            }
        }
    }
gb's avatar
gb committed
441 442

    /* VA image formats */
443 444 445 446
    formats = g_new(VAImageFormat, vaMaxNumImageFormats(priv->display));
    if (!formats)
        goto end;
    status = vaQueryImageFormats(priv->display, formats, &n);
gb's avatar
gb committed
447
    if (!vaapi_check_status(status, "vaQueryImageFormats()"))
448
        goto end;
gb's avatar
gb committed
449

450 451
    GST_DEBUG("%d image formats", n);
    for (i = 0; i < n; i++)
452
        GST_DEBUG("  %" GST_FOURCC_FORMAT, GST_FOURCC_ARGS(formats[i].fourcc));
gb's avatar
gb committed
453

454 455 456 457 458 459
    priv->image_formats =
        g_array_new(FALSE, FALSE, sizeof(GstVaapiImageFormat));
    if (!priv->image_formats)
        goto end;
    append_formats(priv->image_formats, formats, n);
    g_array_sort(priv->image_formats, compare_yuv_formats);
460

gb's avatar
gb committed
461
    /* VA subpicture formats */
462 463 464 465 466 467
    n = vaMaxNumSubpictureFormats(priv->display);
    formats = g_renew(VAImageFormat, formats, n);
    flags   = g_new(guint, n);
    if (!formats || !flags)
        goto end;
    status = vaQuerySubpictureFormats(priv->display, formats, flags, &n);
gb's avatar
gb committed
468
    if (!vaapi_check_status(status, "vaQuerySubpictureFormats()"))
469
        goto end;
470

471 472
    GST_DEBUG("%d subpicture formats", n);
    for (i = 0; i < n; i++)
473
        GST_DEBUG("  %" GST_FOURCC_FORMAT, GST_FOURCC_ARGS(formats[i].fourcc));
gb's avatar
gb committed
474

475 476 477 478 479 480 481
    priv->subpicture_formats =
        g_array_new(FALSE, FALSE, sizeof(GstVaapiImageFormat));
    if (!priv->subpicture_formats)
        goto end;
    append_formats(priv->subpicture_formats, formats, n);
    g_array_sort(priv->subpicture_formats, compare_rgb_formats);

482 483 484 485 486
    if (!cached_info) {
        if (!gst_vaapi_display_cache_add(cache, &info))
            goto end;
    }

487 488 489
    has_errors = FALSE;
end:
    g_free(profiles);
gb's avatar
gb committed
490
    g_free(entrypoints);
491 492 493
    g_free(formats);
    g_free(flags);
    return !has_errors;
gb's avatar
gb committed
494 495
}

gb's avatar
gb committed
496 497 498
static void
gst_vaapi_display_lock_default(GstVaapiDisplay *display)
{
499 500 501 502 503
    GstVaapiDisplayPrivate *priv = display->priv;

    if (priv->parent)
        priv = priv->parent->priv;
    g_static_rec_mutex_lock(&priv->mutex);
gb's avatar
gb committed
504 505 506 507 508
}

static void
gst_vaapi_display_unlock_default(GstVaapiDisplay *display)
{
509 510 511 512 513
    GstVaapiDisplayPrivate *priv = display->priv;

    if (priv->parent)
        priv = priv->parent->priv;
    g_static_rec_mutex_unlock(&priv->mutex);
gb's avatar
gb committed
514 515
}

gb's avatar
gb committed
516 517 518
static void
gst_vaapi_display_finalize(GObject *object)
{
gb's avatar
gb committed
519
    GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
gb's avatar
gb committed
520

gb's avatar
gb committed
521
    gst_vaapi_display_destroy(display);
gb's avatar
gb committed
522 523 524 525 526

    G_OBJECT_CLASS(gst_vaapi_display_parent_class)->finalize(object);
}

static void
527 528 529 530
gst_vaapi_display_set_property(
    GObject      *object,
    guint         prop_id,
    const GValue *value,
gb's avatar
gb committed
531 532
    GParamSpec   *pspec
)
gb's avatar
gb committed
533
{
gb's avatar
gb committed
534
    GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
gb's avatar
gb committed
535 536 537

    switch (prop_id) {
    case PROP_DISPLAY:
538
        display->priv->display = g_value_get_pointer(value);
gb's avatar
gb committed
539 540 541 542 543 544 545 546
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
        break;
    }
}

static void
547 548 549 550 551 552
gst_vaapi_display_get_property(
    GObject    *object,
    guint       prop_id,
    GValue     *value,
    GParamSpec *pspec
)
gb's avatar
gb committed
553
{
gb's avatar
gb committed
554
    GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
gb's avatar
gb committed
555 556 557 558 559

    switch (prop_id) {
    case PROP_DISPLAY:
        g_value_set_pointer(value, gst_vaapi_display_get_display(display));
        break;
gb's avatar
gb committed
560 561 562 563 564 565
    case PROP_WIDTH:
        g_value_set_uint(value, gst_vaapi_display_get_width(display));
        break;
    case PROP_HEIGHT:
        g_value_set_uint(value, gst_vaapi_display_get_height(display));
        break;
gb's avatar
gb committed
566 567 568 569 570 571
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
        break;
    }
}

572 573 574 575 576 577 578
static void
gst_vaapi_display_constructed(GObject *object)
{
    GstVaapiDisplay * const display = GST_VAAPI_DISPLAY(object);
    GObjectClass *parent_class;

    display->priv->create_display = display->priv->display == NULL;
579 580
    if (!gst_vaapi_display_create(display))
        gst_vaapi_display_destroy(display);
581 582 583 584 585 586

    parent_class = G_OBJECT_CLASS(gst_vaapi_display_parent_class);
    if (parent_class->constructed)
        parent_class->constructed(object);
}

gb's avatar
gb committed
587 588 589 590
static void
gst_vaapi_display_class_init(GstVaapiDisplayClass *klass)
{
    GObjectClass * const object_class = G_OBJECT_CLASS(klass);
gb's avatar
gb committed
591
    GstVaapiDisplayClass * const dpy_class = GST_VAAPI_DISPLAY_CLASS(klass);
gb's avatar
gb committed
592

gb's avatar
gb committed
593 594
    GST_DEBUG_CATEGORY_INIT(gst_debug_vaapi, "vaapi", 0, "VA-API helper");

gb's avatar
gb committed
595 596
    g_type_class_add_private(klass, sizeof(GstVaapiDisplayPrivate));

gb's avatar
gb committed
597 598 599 600 601
    object_class->finalize      = gst_vaapi_display_finalize;
    object_class->set_property  = gst_vaapi_display_set_property;
    object_class->get_property  = gst_vaapi_display_get_property;
    object_class->constructed   = gst_vaapi_display_constructed;

602 603
    dpy_class->lock             = gst_vaapi_display_lock_default;
    dpy_class->unlock           = gst_vaapi_display_unlock_default;
gb's avatar
gb committed
604 605 606 607 608 609 610 611

    g_object_class_install_property
        (object_class,
         PROP_DISPLAY,
         g_param_spec_pointer("display",
                              "VA display",
                              "VA display",
                              G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
gb's avatar
gb committed
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629

    g_object_class_install_property
        (object_class,
         PROP_WIDTH,
         g_param_spec_uint("width",
                           "Width",
                           "The display width",
                           1, G_MAXUINT32, 1,
                           G_PARAM_READABLE));

    g_object_class_install_property
        (object_class,
         PROP_HEIGHT,
         g_param_spec_uint("height",
                           "height",
                           "The display height",
                           1, G_MAXUINT32, 1,
                           G_PARAM_READABLE));
gb's avatar
gb committed
630 631 632 633 634 635 636
}

static void
gst_vaapi_display_init(GstVaapiDisplay *display)
{
    GstVaapiDisplayPrivate *priv = GST_VAAPI_DISPLAY_GET_PRIVATE(display);

637
    display->priv               = priv;
638
    priv->parent                = NULL;
639
    priv->display               = NULL;
gb's avatar
gb committed
640 641
    priv->width                 = 0;
    priv->height                = 0;
642 643 644 645
    priv->width_mm              = 0;
    priv->height_mm             = 0;
    priv->par_n                 = 1;
    priv->par_d                 = 1;
gb's avatar
gb committed
646 647
    priv->decoders              = NULL;
    priv->encoders              = NULL;
648 649
    priv->image_formats         = NULL;
    priv->subpicture_formats    = NULL;
650
    priv->create_display        = TRUE;
gb's avatar
gb committed
651

652
    g_static_rec_mutex_init(&priv->mutex);
gb's avatar
gb committed
653 654
}

gb's avatar
gb committed
655 656 657 658 659 660 661 662 663
/**
 * gst_vaapi_display_new_with_display:
 * @va_display: a #VADisplay
 *
 * Creates a new #GstVaapiDisplay, using @va_display as the VA
 * display.
 *
 * Return value: the newly created #GstVaapiDisplay object
 */
gb's avatar
gb committed
664 665
GstVaapiDisplay *
gst_vaapi_display_new_with_display(VADisplay va_display)
gb's avatar
gb committed
666
{
667 668 669 670 671 672 673 674 675 676
    GstVaapiDisplayCache * const cache = get_display_cache();
    const GstVaapiDisplayInfo *info;

    g_return_val_if_fail(va_display != NULL, NULL);
    g_return_val_if_fail(cache != NULL, NULL);

    info = gst_vaapi_display_cache_lookup_by_va_display(cache, va_display);
    if (info)
        return g_object_ref(info->display);

gb's avatar
gb committed
677 678 679
    return g_object_new(GST_VAAPI_TYPE_DISPLAY,
                        "display", va_display,
                        NULL);
gb's avatar
gb committed
680 681
}

gb's avatar
gb committed
682 683 684 685 686 687 688 689
/**
 * gst_vaapi_display_lock:
 * @display: a #GstVaapiDisplay
 *
 * Locks @display. If @display is already locked by another thread,
 * the current thread will block until @display is unlocked by the
 * other thread.
 */
gb's avatar
gb committed
690 691 692 693 694 695 696 697
void
gst_vaapi_display_lock(GstVaapiDisplay *display)
{
    GstVaapiDisplayClass *klass;

    g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));

    klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
698 699
    if (klass->lock)
        klass->lock(display);
gb's avatar
gb committed
700 701
}

gb's avatar
gb committed
702 703 704 705 706 707 708 709
/**
 * gst_vaapi_display_unlock:
 * @display: a #GstVaapiDisplay
 *
 * Unlocks @display. If another thread is blocked in a
 * gst_vaapi_display_lock() call for @display, it will be woken and
 * can lock @display itself.
 */
gb's avatar
gb committed
710 711 712 713 714 715 716 717
void
gst_vaapi_display_unlock(GstVaapiDisplay *display)
{
    GstVaapiDisplayClass *klass;

    g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));

    klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
718 719
    if (klass->unlock)
        klass->unlock(display);
gb's avatar
gb committed
720 721
}

722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
/**
 * gst_vaapi_display_sync:
 * @display: a #GstVaapiDisplay
 *
 * Flushes any requests queued for the windowing system and waits until
 * all requests have been handled. This is often used for making sure
 * that the display is synchronized with the current state of the program.
 *
 * This is most useful for X11. On windowing systems where requests are
 * handled synchronously, this function will do nothing.
 */
void
gst_vaapi_display_sync(GstVaapiDisplay *display)
{
    GstVaapiDisplayClass *klass;

    g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));

    klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
    if (klass->sync)
        klass->sync(display);
    else if (klass->flush)
        klass->flush(display);
}

/**
gb's avatar
gb committed
748
 * gst_vaapi_display_flush:
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
 * @display: a #GstVaapiDisplay
 *
 * Flushes any requests queued for the windowing system.
 *
 * This is most useful for X11. On windowing systems where requests
 * are handled synchronously, this function will do nothing.
 */
void
gst_vaapi_display_flush(GstVaapiDisplay *display)
{
    GstVaapiDisplayClass *klass;

    g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));

    klass = GST_VAAPI_DISPLAY_GET_CLASS(display);
    if (klass->flush)
        klass->flush(display);
}

gb's avatar
gb committed
768 769 770 771 772 773 774 775
/**
 * gst_vaapi_display_get_display:
 * @display: a #GstVaapiDisplay
 *
 * Returns the #VADisplay bound to @display.
 *
 * Return value: the #VADisplay
 */
gb's avatar
gb committed
776 777
VADisplay
gst_vaapi_display_get_display(GstVaapiDisplay *display)
gb's avatar
gb committed
778
{
gb's avatar
gb committed
779
    g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
gb's avatar
gb committed
780

gb's avatar
gb committed
781
    return display->priv->display;
gb's avatar
gb committed
782 783
}

gb's avatar
gb committed
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
/**
 * gst_vaapi_display_get_width:
 * @display: a #GstVaapiDisplay
 *
 * Retrieves the width of a #GstVaapiDisplay.
 *
 * Return value: the width of the @display, in pixels
 */
guint
gst_vaapi_display_get_width(GstVaapiDisplay *display)
{
    g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), 0);

    return display->priv->width;
}

/**
 * gst_vaapi_display_get_height:
 * @display: a #GstVaapiDisplay
 *
 * Retrieves the height of a #GstVaapiDisplay
 *
 * Return value: the height of the @display, in pixels
 */
guint
gst_vaapi_display_get_height(GstVaapiDisplay *display)
{
    g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), 0);

    return display->priv->height;
}

/**
 * gst_vaapi_display_get_size:
 * @display: a #GstVaapiDisplay
819 820
 * @pwidth: return location for the width, or %NULL
 * @pheight: return location for the height, or %NULL
gb's avatar
gb committed
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
 *
 * Retrieves the dimensions of a #GstVaapiDisplay.
 */
void
gst_vaapi_display_get_size(GstVaapiDisplay *display, guint *pwidth, guint *pheight)
{
    g_return_if_fail(GST_VAAPI_DISPLAY(display));

    if (pwidth)
        *pwidth = display->priv->width;

    if (pheight)
        *pheight = display->priv->height;
}

836 837 838
/**
 * gst_vaapi_display_get_pixel_aspect_ratio:
 * @display: a #GstVaapiDisplay
839 840
 * @par_n: return location for the numerator of pixel aspect ratio, or %NULL
 * @par_d: return location for the denominator of pixel aspect ratio, or %NULL
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
 *
 * Retrieves the pixel aspect ratio of a #GstVaapiDisplay.
 */
void
gst_vaapi_display_get_pixel_aspect_ratio(
    GstVaapiDisplay *display,
    guint           *par_n,
    guint           *par_d
)
{
    g_return_if_fail(GST_VAAPI_IS_DISPLAY(display));

    if (par_n)
        *par_n = display->priv->par_n;

    if (par_d)
        *par_d = display->priv->par_d;
}

gb's avatar
gb committed
860
/**
gb's avatar
gb committed
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
 * gst_vaapi_display_get_decode_caps:
 * @display: a #GstVaapiDisplay
 *
 * Gets the supported profiles for decoding as #GstCaps capabilities.
 *
 * Return value: a newly allocated #GstCaps object, possibly empty
 */
GstCaps *
gst_vaapi_display_get_decode_caps(GstVaapiDisplay *display)
{
    g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);

    return get_profile_caps(display->priv->decoders);
}

/**
 * gst_vaapi_display_has_decoder:
gb's avatar
gb committed
878 879
 * @display: a #GstVaapiDisplay
 * @profile: a #VAProfile
880
 * @entrypoint: a #GstVaaiEntrypoint
gb's avatar
gb committed
881
 *
882 883
 * Returns whether VA @display supports @profile for decoding at the
 * specified @entrypoint.
gb's avatar
gb committed
884
 *
gb's avatar
gb committed
885
 * Return value: %TRUE if VA @display supports @profile for decoding.
gb's avatar
gb committed
886
 */
887
gboolean
gb's avatar
gb committed
888
gst_vaapi_display_has_decoder(
889 890 891
    GstVaapiDisplay    *display,
    GstVaapiProfile     profile,
    GstVaapiEntrypoint  entrypoint
gb's avatar
gb committed
892 893 894 895
)
{
    g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);

896
    return find_config(display->priv->decoders, profile, entrypoint);
gb's avatar
gb committed
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
}

/**
 * gst_vaapi_display_get_encode_caps:
 * @display: a #GstVaapiDisplay
 *
 * Gets the supported profiles for decoding as #GstCaps capabilities.
 *
 * Return value: a newly allocated #GstCaps object, possibly empty
 */
GstCaps *
gst_vaapi_display_get_encode_caps(GstVaapiDisplay *display)
{
    g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);

    return get_profile_caps(display->priv->encoders);
}

/**
 * gst_vaapi_display_has_encoder:
 * @display: a #GstVaapiDisplay
 * @profile: a #VAProfile
919
 * @entrypoint: a #GstVaapiEntrypoint
gb's avatar
gb committed
920
 *
921 922
 * Returns whether VA @display supports @profile for encoding at the
 * specified @entrypoint.
gb's avatar
gb committed
923 924 925 926 927
 *
 * Return value: %TRUE if VA @display supports @profile for encoding.
 */
gboolean
gst_vaapi_display_has_encoder(
928 929 930
    GstVaapiDisplay    *display,
    GstVaapiProfile     profile,
    GstVaapiEntrypoint  entrypoint
gb's avatar
gb committed
931
)
932 933 934
{
    g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);

935
    return find_config(display->priv->encoders, profile, entrypoint);
936 937
}

gb's avatar
gb committed
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
/**
 * gst_vaapi_display_get_image_caps:
 * @display: a #GstVaapiDisplay
 *
 * Gets the supported image formats for gst_vaapi_surface_get_image()
 * or gst_vaapi_surface_put_image() as #GstCaps capabilities.
 *
 * Note that this method does not necessarily map image formats
 * returned by vaQueryImageFormats(). The set of capabilities can be
 * stripped down, if gstreamer-vaapi does not support the format, or
 * expanded to cover compatible formats not exposed by the underlying
 * driver. e.g. I420 can be supported even if the driver only exposes
 * YV12.
 *
 * Return value: a newly allocated #GstCaps object, possibly empty
 */
954 955 956 957 958
GstCaps *
gst_vaapi_display_get_image_caps(GstVaapiDisplay *display)
{
    g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);

gb's avatar
gb committed
959
    return get_format_caps(display->priv->image_formats);
960 961
}

gb's avatar
gb committed
962 963 964 965 966 967 968 969 970
/**
 * gst_vaapi_display_has_image_format:
 * @display: a #GstVaapiDisplay
 * @format: a #GstVaapiFormat
 *
 * Returns whether VA @display supports @format image format.
 *
 * Return value: %TRUE if VA @display supports @format image format
 */
971 972 973 974 975 976 977
gboolean
gst_vaapi_display_has_image_format(
    GstVaapiDisplay    *display,
    GstVaapiImageFormat format
)
{
    g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
978
    g_return_val_if_fail(format, FALSE);
979

980 981 982 983 984 985 986 987
    if (find_format(display->priv->image_formats, format))
        return TRUE;

    /* XXX: try subpicture formats since some drivers could report a
     * set of VA image formats that is not a superset of the set of VA
     * subpicture formats
     */
    return find_format(display->priv->subpicture_formats, format);
988 989
}

gb's avatar
gb committed
990 991 992 993 994 995 996 997 998 999 1000 1001 1002
/**
 * gst_vaapi_display_get_subpicture_caps:
 * @display: a #GstVaapiDisplay
 *
 * Gets the supported subpicture formats as #GstCaps capabilities.
 *
 * Note that this method does not necessarily map subpicture formats
 * returned by vaQuerySubpictureFormats(). The set of capabilities can
 * be stripped down if gstreamer-vaapi does not support the
 * format. e.g. this is the case for paletted formats like IA44.
 *
 * Return value: a newly allocated #GstCaps object, possibly empty
 */
1003 1004 1005 1006 1007
GstCaps *
gst_vaapi_display_get_subpicture_caps(GstVaapiDisplay *display)
{
    g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);

gb's avatar
gb committed
1008
    return get_format_caps(display->priv->subpicture_formats);
1009 1010
}

gb's avatar
gb committed
1011 1012 1013 1014 1015 1016 1017 1018 1019
/**
 * gst_vaapi_display_has_subpicture_format:
 * @display: a #GstVaapiDisplay
 * @format: a #GstVaapiFormat
 *
 * Returns whether VA @display supports @format subpicture format.
 *
 * Return value: %TRUE if VA @display supports @format subpicture format
 */
1020 1021 1022 1023 1024 1025 1026
gboolean
gst_vaapi_display_has_subpicture_format(
    GstVaapiDisplay    *display,
    GstVaapiImageFormat format
)
{
    g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), FALSE);
1027
    g_return_val_if_fail(format, FALSE);
1028

1029
    return find_format(display->priv->subpicture_formats, format);
1030
}