gstvafilter.c 30.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 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 142 143 144 145 146 147 148 149 150 151 152
/* GStreamer
 * Copyright (C) 2020 Igalia, S.L.
 *     Author: Víctor Jáquez <vjaquez@igalia.com>
 *
 * 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 St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

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

#include "gstvafilter.h"

#include <gst/video/video.h>

#include <va/va_vpp.h>
#include <va/va_drmcommon.h>

#include "gstvacaps.h"
#include "gstvavideoformat.h"

struct _GstVaFilter
{
  GstObject parent;

  GstVaDisplay *display;
  VAConfigID config;
  VAContextID context;

  VAProcPipelineCaps pipeline_caps;

  guint32 mem_types;
  gint min_width;
  gint max_width;
  gint min_height;
  gint max_height;

  guint mirror;
  guint rotation;
  GstVideoOrientationMethod orientation;

  GArray *surface_formats;
  GArray *image_formats;

  GArray *available_filters;

  GArray *filters;
};

GST_DEBUG_CATEGORY_STATIC (gst_va_filter_debug);
#define GST_CAT_DEFAULT gst_va_filter_debug

#define gst_va_filter_parent_class parent_class
G_DEFINE_TYPE_WITH_CODE (GstVaFilter, gst_va_filter, GST_TYPE_OBJECT,
    GST_DEBUG_CATEGORY_INIT (gst_va_filter_debug, "vafilter", 0, "VA Filter"));

enum
{
  PROP_DISPLAY = 1,
  N_PROPERTIES
};

static GParamSpec *g_properties[N_PROPERTIES];

static void
gst_va_filter_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstVaFilter *self = GST_VA_FILTER (object);

  switch (prop_id) {
    case PROP_DISPLAY:{
      g_assert (!self->display);
      self->display = g_value_dup_object (value);
      break;
    }
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_va_filter_get_property (GObject * object, guint prop_id, GValue * value,
    GParamSpec * pspec)
{
  GstVaFilter *self = GST_VA_FILTER (object);

  switch (prop_id) {
    case PROP_DISPLAY:
      g_value_set_object (value, self->display);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_va_filter_dispose (GObject * object)
{
  GstVaFilter *self = GST_VA_FILTER (object);

  gst_va_filter_close (self);

  g_clear_pointer (&self->available_filters, g_array_unref);
  g_clear_pointer (&self->image_formats, g_array_unref);
  g_clear_pointer (&self->surface_formats, g_array_unref);
  gst_clear_object (&self->display);

  G_OBJECT_CLASS (parent_class)->dispose (object);
}

static void
gst_va_filter_class_init (GstVaFilterClass * klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->set_property = gst_va_filter_set_property;
  gobject_class->get_property = gst_va_filter_get_property;
  gobject_class->dispose = gst_va_filter_dispose;

  g_properties[PROP_DISPLAY] =
      g_param_spec_object ("display", "GstVaDisplay", "GstVADisplay object",
      GST_TYPE_VA_DISPLAY,
      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);

  g_object_class_install_properties (gobject_class, N_PROPERTIES, g_properties);
}

static void
gst_va_filter_init (GstVaFilter * self)
{
  self->config = VA_INVALID_ID;
  self->context = VA_INVALID_ID;

  self->min_height = 1;
  self->max_height = G_MAXINT;
  self->min_width = 1;
153
  self->max_width = G_MAXINT;
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
}

GstVaFilter *
gst_va_filter_new (GstVaDisplay * display)
{
  g_return_val_if_fail (GST_IS_VA_DISPLAY (display), NULL);

  return g_object_new (GST_TYPE_VA_FILTER, "display", display, NULL);
}

gboolean
gst_va_filter_is_open (GstVaFilter * self)
{
  gboolean ret;

  g_return_val_if_fail (GST_IS_VA_FILTER (self), FALSE);

  GST_OBJECT_LOCK (self);
  ret = (self->config != VA_INVALID_ID && self->context != VA_INVALID_ID);
  GST_OBJECT_UNLOCK (self);
  return ret;
}

static gboolean
gst_va_filter_ensure_config_attributes (GstVaFilter * self,
    guint32 * rt_formats_ptr)
{
  VAConfigAttrib attribs[] = {
    {.type = VAConfigAttribMaxPictureWidth,},
    {.type = VAConfigAttribMaxPictureHeight,},
    {.type = VAConfigAttribRTFormat,},
  };
  VADisplay dpy;
  VAStatus status;
  guint i, value, rt_formats = 0, max_width = 0, max_height = 0;

  dpy = gst_va_display_get_va_dpy (self->display);

  gst_va_display_lock (self->display);
  status = vaGetConfigAttributes (dpy, VAProfileNone, VAEntrypointVideoProc,
      attribs, G_N_ELEMENTS (attribs));
  gst_va_display_unlock (self->display);
  if (status != VA_STATUS_SUCCESS) {
    GST_ERROR_OBJECT (self, "vaGetConfigAttributes: %s", vaErrorStr (status));
    return FALSE;
  }

  for (i = 0; i < G_N_ELEMENTS (attribs); i++) {
    value = attribs[i].value;
    if (value == VA_ATTRIB_NOT_SUPPORTED)
      continue;
    switch (attribs[i].type) {
      case VAConfigAttribMaxPictureHeight:
        max_height = value;
        break;
      case VAConfigAttribMaxPictureWidth:
        max_width = value;
        break;
      case VAConfigAttribRTFormat:
        rt_formats = value;
        break;
      default:
        break;
    }
  }

  if (rt_formats_ptr && rt_formats != 0)
    *rt_formats_ptr = rt_formats;
  if (max_width > 0 && max_width < G_MAXINT)
    self->max_width = max_width;
  if (max_height > 0 && max_height < G_MAXINT)
    self->max_height = max_height;

  return TRUE;
}

static gboolean
gst_va_filter_ensure_surface_attributes (GstVaFilter * self)
{
  GArray *surface_formats;
  GstVideoFormat format;
  VASurfaceAttrib *attribs;
  guint i, attrib_count;

  attribs =
      gst_va_get_surface_attribs (self->display, self->config, &attrib_count);
  if (!attribs)
    return FALSE;
  surface_formats = g_array_new (FALSE, FALSE, sizeof (GstVideoFormat));

  for (i = 0; i < attrib_count; i++) {
    if (attribs[i].value.type != VAGenericValueTypeInteger)
      continue;
    switch (attribs[i].type) {
      case VASurfaceAttribPixelFormat:
        format = gst_va_video_format_from_va_fourcc (attribs[i].value.value.i);
        if (format != GST_VIDEO_FORMAT_UNKNOWN)
          g_array_append_val (surface_formats, format);
        break;
      case VASurfaceAttribMinWidth:
        self->min_width = MAX (self->min_width, attribs[i].value.value.i);
        break;
      case VASurfaceAttribMaxWidth:
        if (self->max_width > 0)
          self->max_width = MIN (self->max_width, attribs[i].value.value.i);
        else
          self->max_width = attribs[i].value.value.i;
        break;
      case VASurfaceAttribMinHeight:
        self->min_height = MAX (self->min_height, attribs[i].value.value.i);
        break;
      case VASurfaceAttribMaxHeight:
        if (self->max_height > 0)
          self->max_height = MIN (self->max_height, attribs[i].value.value.i);
        else
          self->max_height = attribs[i].value.value.i;
        break;
      case VASurfaceAttribMemoryType:
        self->mem_types = attribs[i].value.value.i;
        break;
      default:
        break;
    }
  }

  if (surface_formats->len == 0)
    g_clear_pointer (&surface_formats, g_array_unref);

  self->surface_formats = surface_formats;

  g_free (attribs);

  return TRUE;
}

static gboolean
gst_va_filter_ensure_pipeline_caps (GstVaFilter * self)
{
  VADisplay dpy;
  VAStatus status;

  dpy = gst_va_display_get_va_dpy (self->display);

  gst_va_display_lock (self->display);
  status = vaQueryVideoProcPipelineCaps (dpy, self->context, NULL, 0,
      &self->pipeline_caps);
  gst_va_display_unlock (self->display);
  if (status != VA_STATUS_SUCCESS) {
    GST_ERROR_OBJECT (self, "vaQueryVideoProcPipelineCaps: %s",
        vaErrorStr (status));
    return FALSE;
  }

  return TRUE;
}

310
/* Not thread-safe API */
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 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
gboolean
gst_va_filter_open (GstVaFilter * self)
{
  VAConfigAttrib attrib = {
    .type = VAConfigAttribRTFormat,
  };
  VADisplay dpy;
  VAStatus status;

  g_return_val_if_fail (GST_IS_VA_FILTER (self), FALSE);

  if (gst_va_filter_is_open (self))
    return TRUE;

  if (!gst_va_filter_ensure_config_attributes (self, &attrib.value))
    return FALSE;

  if (!gst_va_filter_ensure_pipeline_caps (self))
    return FALSE;

  self->image_formats = gst_va_display_get_image_formats (self->display);
  if (!self->image_formats)
    return FALSE;

  dpy = gst_va_display_get_va_dpy (self->display);

  gst_va_display_lock (self->display);
  status = vaCreateConfig (dpy, VAProfileNone, VAEntrypointVideoProc, &attrib,
      1, &self->config);
  gst_va_display_unlock (self->display);
  if (status != VA_STATUS_SUCCESS) {
    GST_ERROR_OBJECT (self, "vaCreateConfig: %s", vaErrorStr (status));
    return FALSE;
  }

  if (!gst_va_filter_ensure_surface_attributes (self))
    goto bail;

  gst_va_display_lock (self->display);
  status = vaCreateContext (dpy, self->config, 0, 0, 0, NULL, 0,
      &self->context);
  gst_va_display_unlock (self->display);
  if (status != VA_STATUS_SUCCESS) {
    GST_ERROR_OBJECT (self, "vaCreateContext: %s", vaErrorStr (status));
    goto bail;
  }

  return TRUE;

bail:
  {
    gst_va_display_lock (self->display);
    status = vaDestroyConfig (dpy, self->config);
    gst_va_display_unlock (self->display);

    return FALSE;
  }
}

370
/* Not thread-safe API */
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
gboolean
gst_va_filter_close (GstVaFilter * self)
{
  VADisplay dpy;
  VAStatus status;

  g_return_val_if_fail (GST_IS_VA_FILTER (self), FALSE);

  if (!gst_va_filter_is_open (self))
    return TRUE;

  dpy = gst_va_display_get_va_dpy (self->display);

  if (self->context != VA_INVALID_ID) {
    gst_va_display_lock (self->display);
    status = vaDestroyContext (dpy, self->context);
    gst_va_display_unlock (self->display);
    if (status != VA_STATUS_SUCCESS)
      GST_ERROR_OBJECT (self, "vaDestroyContext: %s", vaErrorStr (status));
  }

  gst_va_display_lock (self->display);
  status = vaDestroyConfig (dpy, self->config);
  gst_va_display_unlock (self->display);
  if (status != VA_STATUS_SUCCESS) {
    GST_ERROR_OBJECT (self, "vaDestroyConfig: %s", vaErrorStr (status));
    return FALSE;
  }

  g_clear_pointer (&self->available_filters, g_array_unref);
401
  g_clear_pointer (&self->filters, g_array_unref);
402

403 404 405 406 407 408 409 410 411 412 413 414 415 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 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
  gst_va_filter_init (self);

  return TRUE;
}

/* *INDENT-OFF* */
static const struct VaFilterCapMap {
  VAProcFilterType type;
  guint count;
} filter_cap_map[] = {
  { VAProcFilterNoiseReduction, 1 },
  { VAProcFilterDeinterlacing, VAProcDeinterlacingCount },
  { VAProcFilterSharpening, 1 },
  { VAProcFilterColorBalance, VAProcColorBalanceCount },
  { VAProcFilterSkinToneEnhancement, 1 },
  { VAProcFilterTotalColorCorrection, VAProcTotalColorCorrectionCount },
  { VAProcFilterHVSNoiseReduction, 0 },
  { VAProcFilterHighDynamicRangeToneMapping, 1 },
};
/* *INDENT-ON* */

static const struct VaFilterCapMap *
gst_va_filter_get_filter_cap (VAProcFilterType type)
{
  guint i;

  for (i = 0; i < G_N_ELEMENTS (filter_cap_map); i++) {
    if (filter_cap_map[i].type == type)
      return &filter_cap_map[i];
  }

  return NULL;
}

static guint
gst_va_filter_get_filter_cap_count (VAProcFilterType type)
{
  const struct VaFilterCapMap *map = gst_va_filter_get_filter_cap (type);
  return map ? map->count : 0;
}

struct VaFilter
{
  VAProcFilterType type;
  guint num_caps;
  union
  {
    VAProcFilterCap simple;
    VAProcFilterCapDeinterlacing deint[VAProcDeinterlacingCount];
    VAProcFilterCapColorBalance cb[VAProcColorBalanceCount];
    VAProcFilterCapTotalColorCorrection cc[VAProcTotalColorCorrectionCount];
    VAProcFilterCapHighDynamicRange hdr;
  } caps;
};

static gboolean
gst_va_filter_ensure_filters (GstVaFilter * self)
{
  GArray *filters;
  VADisplay dpy;
  VAProcFilterType *filter_types;
  VAStatus status;
  guint i, num = VAProcFilterCount;
  gboolean ret = FALSE;

  GST_OBJECT_LOCK (self);
  if (self->available_filters) {
    GST_OBJECT_UNLOCK (self);
    return TRUE;
  }
  GST_OBJECT_UNLOCK (self);

  filter_types = g_malloc_n (num, sizeof (*filter_types));

  dpy = gst_va_display_get_va_dpy (self->display);

  gst_va_display_lock (self->display);
  status = vaQueryVideoProcFilters (dpy, self->context, filter_types, &num);
  gst_va_display_unlock (self->display);
  if (status == VA_STATUS_ERROR_MAX_NUM_EXCEEDED) {
    filter_types = g_try_realloc_n (filter_types, num, sizeof (*filter_types));
    gst_va_display_lock (self->display);
    status = vaQueryVideoProcFilters (dpy, self->context, filter_types, &num);
    gst_va_display_unlock (self->display);
  }
  if (status != VA_STATUS_SUCCESS) {
    GST_ERROR_OBJECT (self, "vaQueryVideoProcFilters: %s", vaErrorStr (status));
    goto bail;
  }

  if (num == 0)
    goto bail;

  filters = g_array_sized_new (FALSE, FALSE, sizeof (struct VaFilter), num);

  for (i = 0; i < num; i++) {
    guint num_caps = gst_va_filter_get_filter_cap_count (filter_types[i]);
    struct VaFilter filter = { filter_types[i], num_caps, {{{0,}}} };

    if (num_caps > 0) {
      gst_va_display_lock (self->display);
      status = vaQueryVideoProcFilterCaps (dpy, self->context, filter.type,
          &filter.caps, &filter.num_caps);
      gst_va_display_unlock (self->display);
      if (status != VA_STATUS_SUCCESS) {
        GST_WARNING_OBJECT (self, "vaQueryVideoProcFiltersCaps: %s",
            vaErrorStr (status));
        continue;
      }
    }

    g_array_append_val (filters, filter);
  }

  GST_OBJECT_LOCK (self);
  g_clear_pointer (&self->available_filters, g_array_unref);
  self->available_filters = filters;
  GST_OBJECT_UNLOCK (self);

  ret = TRUE;

bail:
  g_free (filter_types);

  return ret;
}

530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
/* *INDENT-OFF* */
static const struct _CBDesc {
  const char *name;
  const char *nick;
  const char *blurb;
  guint prop_id;
} cb_desc[VAProcColorBalanceCount] = {
  [VAProcColorBalanceHue] =
      { "hue", "Hue", "Color hue value", GST_VA_FILTER_PROP_HUE },
  [VAProcColorBalanceSaturation] =
      { "saturation", "Saturation", "Color saturation value",
        GST_VA_FILTER_PROP_SATURATION },
  [VAProcColorBalanceBrightness] =
      { "brightness", "Brightness", "Color brightness value",
        GST_VA_FILTER_PROP_BRIGHTNESS },
  [VAProcColorBalanceContrast] =
      { "contrast", "Contrast", "Color contrast value",
        GST_VA_FILTER_PROP_CONTRAST },
  [VAProcColorBalanceAutoSaturation] =
      { "auto-saturation",   "Auto-Saturation", "Enable auto saturation",
        GST_VA_FILTER_PROP_AUTO_SATURATION },
  [VAProcColorBalanceAutoBrightness] =
      { "auto-brightness", "Auto-Brightness", "Enable auto brightness",
        GST_VA_FILTER_PROP_AUTO_BRIGHTNESS    },
  [VAProcColorBalanceAutoContrast] =
      { "auto-contrast", "Auto-Contrast", "Enable auto contrast",
        GST_VA_FILTER_PROP_AUTO_CONTRAST },
};
/* *INDENT-ON* */

560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
gboolean
gst_va_filter_install_properties (GstVaFilter * self, GObjectClass * klass)
{
  guint i;

  g_return_val_if_fail (GST_IS_VA_FILTER (self), FALSE);

  if (!gst_va_filter_is_open (self))
    return FALSE;

  if (!gst_va_filter_ensure_filters (self))
    return FALSE;

  for (i = 0; i < self->available_filters->len; i++) {
    const struct VaFilter *filter =
        &g_array_index (self->available_filters, struct VaFilter, i);
576 577 578
    const GParamFlags common_flags = G_PARAM_READWRITE
        | GST_PARAM_CONDITIONALLY_AVAILABLE | G_PARAM_STATIC_STRINGS
        | GST_PARAM_DOC_SHOW_DEFAULT;
579 580 581 582 583 584 585 586 587

    switch (filter->type) {
      case VAProcFilterNoiseReduction:{
        const VAProcFilterCap *caps = &filter->caps.simple;

        g_object_class_install_property (klass, GST_VA_FILTER_PROP_DENOISE,
            g_param_spec_float ("denoise", "Noise reduction",
                "Noise reduction factor", caps->range.min_value,
                caps->range.max_value, caps->range.default_value,
588
                common_flags));
589 590 591 592 593 594 595 596 597
        break;
      }
      case VAProcFilterSharpening:{
        const VAProcFilterCap *caps = &filter->caps.simple;

        g_object_class_install_property (klass, GST_VA_FILTER_PROP_SHARPEN,
            g_param_spec_float ("sharpen", "Sharpening Level",
                "Sharpening/blurring filter", caps->range.min_value,
                caps->range.max_value, caps->range.default_value,
598
                common_flags));
599 600 601 602 603 604 605 606 607
        break;
      }
      case VAProcFilterSkinToneEnhancement:{
        const VAProcFilterCap *caps = &filter->caps.simple;
        GParamSpec *pspec;

        /* i965 filter */
        if (filter->num_caps == 0) {
          pspec = g_param_spec_boolean ("skin-tone", "Skin Tone Enhancenment",
608
              "Skin Tone Enhancenment filter", FALSE, common_flags);
609 610 611
        } else {
          pspec = g_param_spec_float ("skin-tone", "Skin Tone Enhancenment",
              "Skin Tone Enhancenment filter", caps->range.min_value,
612
              caps->range.max_value, caps->range.default_value, common_flags);
613 614 615 616 617 618 619 620 621
        }

        g_object_class_install_property (klass, GST_VA_FILTER_PROP_SKINTONE,
            pspec);
        break;
      }
      case VAProcFilterColorBalance:{
        const VAProcFilterCapColorBalance *caps = filter->caps.cb;
        GParamSpec *pspec;
622 623 624 625 626 627 628
        guint j, k;

        for (j = 0; j < filter->num_caps; j++) {
          k = caps[j].type;
          if (caps[j].range.min_value < caps[j].range.max_value) {
            pspec = g_param_spec_float (cb_desc[k].name, cb_desc[k].nick,
                cb_desc[k].blurb, caps[j].range.min_value,
629 630
                caps[j].range.max_value, caps[j].range.default_value,
                common_flags);
631
          } else {
632
            pspec = g_param_spec_boolean (cb_desc[k].name, cb_desc[k].nick,
633
                cb_desc[k].blurb, FALSE, common_flags);
634 635
          }

636
          g_object_class_install_property (klass, cb_desc[k].prop_id, pspec);
637
        }
638 639

        break;
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
      }
      default:
        break;
    }
  }

  if (self->pipeline_caps.mirror_flags != VA_MIRROR_NONE
      || self->pipeline_caps.rotation_flags != VA_ROTATION_NONE) {
    g_object_class_install_property (klass, GST_VA_FILTER_PROP_VIDEO_DIR,
        g_param_spec_enum ("video-direction", "Video Direction",
            "Video direction: rotation and flipping",
            GST_TYPE_VIDEO_ORIENTATION_METHOD, GST_VIDEO_ORIENTATION_IDENTITY,
            G_PARAM_READWRITE | GST_PARAM_CONDITIONALLY_AVAILABLE
            | G_PARAM_STATIC_STRINGS | GST_PARAM_DOC_SHOW_DEFAULT));
  }

  return TRUE;
}

const gpointer
gst_va_filter_get_filter_caps (GstVaFilter * self, VAProcFilterType type,
    guint * num_caps)
{
  struct VaFilter *filter = NULL;
  /* *INDENT-OFF* */
  static const VAProcFilterCap i965_ste_caps = {
    .range = {
      .min_value = 0.0,
      .max_value = 1.0,
      .default_value = 0.0,
      .step = 1.0,
    },
  };
  /* *INDENT-ON* */
  gpointer ret = NULL;
  guint i;

  if (!gst_va_filter_is_open (self))
    return FALSE;

  if (!gst_va_filter_ensure_filters (self))
    return FALSE;

683
  GST_OBJECT_LOCK (self);
684 685
  for (i = 0; i < self->available_filters->len; i++) {
    filter = &g_array_index (self->available_filters, struct VaFilter, i);
686

687 688 689 690 691 692 693 694 695 696 697
    if (filter->type == type) {
      if (filter->num_caps > 0)
        ret = &filter->caps;
      else if (type == VAProcFilterSkinToneEnhancement && filter->num_caps == 0)
        ret = (gpointer) & i965_ste_caps;
      break;
    }
  }

  if (ret && filter && num_caps)
    *num_caps = filter->num_caps;
698
  GST_OBJECT_UNLOCK (self);
699 700 701 702 703 704 705

  return ret;
}

guint32
gst_va_filter_get_mem_types (GstVaFilter * self)
{
706 707
  guint32 ret;

708 709
  g_return_val_if_fail (GST_IS_VA_FILTER (self), 0);

710 711 712 713 714
  GST_OBJECT_LOCK (self);
  ret = self->mem_types;
  GST_OBJECT_UNLOCK (self);

  return ret;
715 716 717 718 719
}

GArray *
gst_va_filter_get_surface_formats (GstVaFilter * self)
{
720 721
  GArray *ret;

722 723
  g_return_val_if_fail (GST_IS_VA_FILTER (self), NULL);

724 725 726 727 728
  GST_OBJECT_LOCK (self);
  ret = self->surface_formats ? g_array_ref (self->surface_formats) : NULL;
  GST_OBJECT_UNLOCK (self);

  return ret;
729 730 731 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
}

static gboolean
_from_video_orientation_method (GstVideoOrientationMethod orientation,
    guint * mirror, guint * rotation)
{
  switch (orientation) {
    case GST_VIDEO_ORIENTATION_IDENTITY:
      *mirror = VA_MIRROR_NONE;
      *rotation = VA_ROTATION_NONE;
      break;
    case GST_VIDEO_ORIENTATION_HORIZ:
      *mirror = VA_MIRROR_HORIZONTAL;
      *rotation = VA_ROTATION_NONE;
      break;
    case GST_VIDEO_ORIENTATION_VERT:
      *mirror = VA_MIRROR_VERTICAL;
      *rotation = VA_ROTATION_NONE;
      break;
    case GST_VIDEO_ORIENTATION_90R:
      *mirror = VA_MIRROR_NONE;
      *rotation = VA_ROTATION_90;
      break;
    case GST_VIDEO_ORIENTATION_180:
      *mirror = VA_MIRROR_NONE;
      *rotation = VA_ROTATION_180;
      break;
    case GST_VIDEO_ORIENTATION_90L:
      *mirror = VA_MIRROR_NONE;
      *rotation = VA_ROTATION_270;
      break;
    case GST_VIDEO_ORIENTATION_UL_LR:
      *mirror = VA_MIRROR_HORIZONTAL;
      *rotation = VA_ROTATION_90;
      break;
    case GST_VIDEO_ORIENTATION_UR_LL:
      *mirror = VA_MIRROR_VERTICAL;
      *rotation = VA_ROTATION_90;
      break;
    default:
      return FALSE;
      break;
  }

  return TRUE;
}

gboolean
gst_va_filter_set_orientation (GstVaFilter * self,
    GstVideoOrientationMethod orientation)
{
  guint32 mirror = VA_MIRROR_NONE, rotation = VA_ROTATION_NONE;
781
  guint32 mirror_flags, rotation_flags;
782 783 784 785

  if (!_from_video_orientation_method (orientation, &mirror, &rotation))
    return FALSE;

786 787 788 789 790
  GST_OBJECT_LOCK (self);
  mirror_flags = self->pipeline_caps.mirror_flags;
  GST_OBJECT_UNLOCK (self);

  if (mirror != VA_MIRROR_NONE && !(mirror_flags & mirror))
791 792
    return FALSE;

793 794 795 796 797
  GST_OBJECT_LOCK (self);
  rotation_flags = self->pipeline_caps.rotation_flags;
  GST_OBJECT_UNLOCK (self);

  if (rotation != VA_ROTATION_NONE && !(rotation_flags & (1 << rotation)))
798 799 800 801 802 803 804 805 806 807 808 809 810 811
    return FALSE;

  GST_OBJECT_LOCK (self);
  self->orientation = orientation;
  self->mirror = mirror;
  self->rotation = rotation;
  GST_OBJECT_UNLOCK (self);

  return TRUE;
}

GstVideoOrientationMethod
gst_va_filter_get_orientation (GstVaFilter * self)
{
812 813 814 815 816 817 818
  GstVideoOrientationMethod ret;

  GST_OBJECT_LOCK (self);
  ret = self->orientation;
  GST_OBJECT_UNLOCK (self);

  return ret;
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
}

static inline GstCaps *
_create_base_caps (GstVaFilter * self)
{
  /* XXX(victor): remove interlace-mode when deinterlacing is
   * supported */
  return gst_caps_new_simple ("video/x-raw", "width", GST_TYPE_INT_RANGE,
      self->min_width, self->max_width, "height", GST_TYPE_INT_RANGE,
      self->min_height, self->max_height, "interlace-mode", G_TYPE_STRING,
      "progressive", NULL);
}

GstCaps *
gst_va_filter_get_caps (GstVaFilter * self)
{
835
  GArray *surface_formats = NULL, *image_formats = NULL;
836 837
  GstCaps *caps, *base_caps, *feature_caps;
  GstCapsFeatures *features;
838
  guint32 mem_types;
839 840 841 842 843 844

  g_return_val_if_fail (GST_IS_VA_FILTER (self), NULL);

  if (!gst_va_filter_is_open (self))
    return NULL;

845 846 847 848
  surface_formats = gst_va_filter_get_surface_formats (self);
  if (!surface_formats)
    return NULL;

849 850
  base_caps = _create_base_caps (self);

851 852 853 854
  if (!gst_caps_set_format_array (base_caps, surface_formats))
    goto fail;

  g_array_unref (surface_formats);
855 856 857

  caps = gst_caps_new_empty ();

858 859 860
  mem_types = gst_va_filter_get_mem_types (self);

  if (mem_types & VA_SURFACE_ATTRIB_MEM_TYPE_VA) {
861 862 863 864 865
    feature_caps = gst_caps_copy (base_caps);
    features = gst_caps_features_from_string ("memory:VAMemory");
    gst_caps_set_features_simple (feature_caps, features);
    caps = gst_caps_merge (caps, feature_caps);
  }
866 867
  if (mem_types & VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME
      || mem_types & VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2) {
868 869 870 871 872 873 874 875 876 877
    feature_caps = gst_caps_copy (base_caps);
    features = gst_caps_features_from_string ("memory:DMABuf");
    gst_caps_set_features_simple (feature_caps, features);
    caps = gst_caps_merge (caps, feature_caps);
  }

  gst_caps_unref (base_caps);

  base_caps = _create_base_caps (self);

878 879 880 881 882 883 884 885 886
  GST_OBJECT_LOCK (self);
  image_formats =
      self->image_formats ? g_array_ref (self->image_formats) : NULL;
  GST_OBJECT_UNLOCK (self);

  if (image_formats) {
    if (!gst_caps_set_format_array (base_caps, image_formats))
      goto fail;
    g_array_unref (image_formats);
887 888 889
  }

  return gst_caps_merge (caps, base_caps);
890 891 892 893 894 895 896 897

fail:
  {
    g_clear_pointer (&surface_formats, g_array_unref);
    g_clear_pointer (&image_formats, g_array_unref);
    gst_caps_unref (base_caps);
    return NULL;
  }
898 899 900
}

static gboolean
901
_destroy_filters_unlocked (GstVaFilter * self)
902 903 904 905 906 907 908
{
  VABufferID buffer;
  VADisplay dpy;
  VAStatus status;
  gboolean ret = TRUE;
  guint i;

909
  GST_TRACE_OBJECT (self, "Destroying %u filter buffers", self->filters->len);
910 911 912 913 914

  dpy = gst_va_display_get_va_dpy (self->display);

  for (i = 0; i < self->filters->len; i++) {
    buffer = g_array_index (self->filters, VABufferID, i);
915

916 917 918 919 920
    gst_va_display_lock (self->display);
    status = vaDestroyBuffer (dpy, buffer);
    gst_va_display_unlock (self->display);
    if (status != VA_STATUS_SUCCESS) {
      ret = FALSE;
921 922
      GST_WARNING_OBJECT (self, "Failed to destroy filter buffer: %s",
          vaErrorStr (status));
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
    }
  }

  self->filters = g_array_set_size (self->filters, 0);

  return ret;
}

gboolean
gst_va_filter_add_filter_buffer (GstVaFilter * self, gpointer data, gsize size,
    guint num)
{
  VABufferID buffer;
  VADisplay dpy;
  VAStatus status;

  g_return_val_if_fail (GST_IS_VA_FILTER (self), FALSE);
  g_return_val_if_fail (data && size > 0, FALSE);

  if (!gst_va_filter_is_open (self))
    return FALSE;

  dpy = gst_va_display_get_va_dpy (self->display);
  gst_va_display_lock (self->display);
  status = vaCreateBuffer (dpy, self->context, VAProcFilterParameterBufferType,
      size, num, data, &buffer);
  gst_va_display_unlock (self->display);
  if (status != VA_STATUS_SUCCESS) {
    GST_ERROR_OBJECT (self, "vaCreateBuffer: %s", vaErrorStr (status));
    return FALSE;
  }

955 956
  /* lazy creation */
  GST_OBJECT_LOCK (self);
957 958 959 960
  if (!self->filters)
    self->filters = g_array_sized_new (FALSE, FALSE, sizeof (VABufferID), 16);

  g_array_append_val (self->filters, buffer);
961
  GST_OBJECT_UNLOCK (self);
962 963 964 965 966 967 968 969 970 971

  return TRUE;
}

static gboolean
_create_pipeline_buffer (GstVaFilter * self, VASurfaceID surface,
    VARectangle * src_rect, VARectangle * dst_rect, VABufferID * buffer)
{
  VADisplay dpy;
  VAStatus status;
972 973 974 975 976 977 978 979 980 981
  VABufferID *filters = NULL;
  VAProcPipelineParameterBuffer params;
  guint32 num_filters = 0;

  GST_OBJECT_LOCK (self);
  if (self->filters) {
    num_filters = self->filters->len;
    filters = (num_filters > 0) ? (VABufferID *) self->filters->data : NULL;
  }
  GST_OBJECT_UNLOCK (self);
982 983

  /* *INDENT-OFF* */
984
  params = (VAProcPipelineParameterBuffer) {
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
    .surface = surface,
    .surface_region = src_rect,
    .surface_color_standard = VAProcColorStandardNone,
    .output_region = dst_rect,
    .output_background_color = 0xff000000,
    .output_color_standard = VAProcColorStandardNone,
    .filters = filters,
    .num_filters = num_filters,
    .rotation_state = self->rotation,
    .mirror_state = self->mirror,
  };
  /* *INDENT-ON* */

  dpy = gst_va_display_get_va_dpy (self->display);
  gst_va_display_lock (self->display);
  status = vaCreateBuffer (dpy, self->context,
      VAProcPipelineParameterBufferType, sizeof (params), 1, &params, buffer);
  gst_va_display_unlock (self->display);
  if (status != VA_STATUS_SUCCESS) {
    GST_ERROR_OBJECT (self, "vaCreateBuffer: %s", vaErrorStr (status));
    return FALSE;
  }

1008 1009 1010
  GST_TRACE_OBJECT (self, "Created VABufferID %#x with %u filters", *buffer,
      num_filters);

1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
  return TRUE;
}

gboolean
gst_va_filter_convert_surface (GstVaFilter * self, VASurfaceID in_surface,
    GstVideoInfo * in_info, VASurfaceID out_surface, GstVideoInfo * out_info)
{
  VABufferID buffer;
  VADisplay dpy;
  VAProcPipelineCaps pipeline_caps = { 0, };
  VARectangle src_rect;
  VARectangle dst_rect;
  VAStatus status;
1024 1025
  VABufferID *filters = NULL;
  guint32 num_filters = 0;
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
  gboolean ret = FALSE;

  g_return_val_if_fail (GST_IS_VA_FILTER (self), FALSE);
  g_return_val_if_fail (in_surface != VA_INVALID_ID
      && out_surface != VA_INVALID_ID, FALSE);
  g_return_val_if_fail (out_info && in_info, FALSE);

  if (!gst_va_filter_is_open (self))
    return FALSE;

  GST_TRACE_OBJECT (self, "Processing %#x", in_surface);

  /* *INDENT-OFF* */
  src_rect = (VARectangle) {
    .width = GST_VIDEO_INFO_WIDTH (in_info),
    .height = GST_VIDEO_INFO_HEIGHT (in_info),
  };
  dst_rect = (VARectangle) {
    .width = GST_VIDEO_INFO_WIDTH (out_info),
    .height = GST_VIDEO_INFO_HEIGHT (out_info),
  };
  /* *INDENT-ON* */

1049 1050 1051 1052 1053 1054 1055
  GST_OBJECT_LOCK (self);
  if (self->filters) {
    g_array_ref (self->filters);
    num_filters = self->filters->len;
    filters = (num_filters > 0) ? (VABufferID *) self->filters->data : NULL;
  }
  GST_OBJECT_UNLOCK (self);
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077

  dpy = gst_va_display_get_va_dpy (self->display);

  gst_va_display_lock (self->display);
  status = vaQueryVideoProcPipelineCaps (dpy, self->context, filters,
      num_filters, &pipeline_caps);
  gst_va_display_unlock (self->display);
  if (status != VA_STATUS_SUCCESS) {
    GST_ERROR_OBJECT (self, "vaQueryVideoProcPipelineCaps: %s",
        vaErrorStr (status));
    return FALSE;
  }

  if (!_create_pipeline_buffer (self, in_surface, &src_rect, &dst_rect,
          &buffer))
    return FALSE;

  gst_va_display_lock (self->display);
  status = vaBeginPicture (dpy, self->context, out_surface);
  gst_va_display_unlock (self->display);
  if (status != VA_STATUS_SUCCESS) {
    GST_ERROR_OBJECT (self, "vaBeginPicture: %s", vaErrorStr (status));
1078
    return FALSE;
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
  }

  gst_va_display_lock (self->display);
  status = vaRenderPicture (dpy, self->context, &buffer, 1);
  gst_va_display_unlock (self->display);
  if (status != VA_STATUS_SUCCESS) {
    GST_ERROR_OBJECT (self, "vaRenderPicture: %s", vaErrorStr (status));
    goto fail_end_pic;
  }

  gst_va_display_lock (self->display);
  status = vaEndPicture (dpy, self->context);
  gst_va_display_unlock (self->display);
  if (status != VA_STATUS_SUCCESS) {
    GST_ERROR_OBJECT (self, "vaEndPicture: %s", vaErrorStr (status));
    goto bail;
  }

  ret = TRUE;

bail:
1100
  GST_OBJECT_LOCK (self);
1101
  if (self->filters) {
1102
    g_array_unref (self->filters);
1103 1104
    _destroy_filters_unlocked (self);
  }
1105 1106 1107 1108 1109 1110 1111 1112

  gst_va_display_lock (self->display);
  status = vaDestroyBuffer (dpy, buffer);
  gst_va_display_unlock (self->display);
  if (status != VA_STATUS_SUCCESS) {
    GST_WARNING_OBJECT (self, "Failed to destroy pipeline buffer: %s",
        vaErrorStr (status));
  }
1113 1114
  GST_OBJECT_UNLOCK (self);

1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
  return ret;

fail_end_pic:
  {
    gst_va_display_lock (self->display);
    status = vaEndPicture (dpy, self->context);
    gst_va_display_unlock (self->display);
    if (status != VA_STATUS_SUCCESS)
      GST_ERROR_OBJECT (self, "vaEndPicture: %s", vaErrorStr (status));
    goto bail;
  }
}