gstreamer/gst-libs/gst/vaapi/gstvaapisurface.c

416 lines
11 KiB
C
Raw Normal View History

/*
* gstvaapisurface.c - VA surface abstraction
*
* gstreamer-vaapi (C) 2010 Splitted-Desktop Systems
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "config.h"
2010-03-16 09:15:48 +00:00
#include "gstvaapiutils.h"
#include "gstvaapisurface.h"
#include <va/va_backend.h>
#define DEBUG 1
#include "vaapi_debug.h"
G_DEFINE_TYPE(GstVaapiSurface, gst_vaapi_surface, G_TYPE_OBJECT);
#define GST_VAAPI_SURFACE_GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE((obj), \
GST_VAAPI_TYPE_SURFACE, \
GstVaapiSurfacePrivate))
struct _GstVaapiSurfacePrivate {
GstVaapiDisplay *display;
VASurfaceID surface_id;
guint width;
guint height;
2010-03-11 13:58:32 +00:00
GstVaapiChromaType chroma_type;
};
enum {
PROP_0,
PROP_DISPLAY,
PROP_SURFACE_ID,
PROP_WIDTH,
PROP_HEIGHT,
2010-03-11 13:58:32 +00:00
PROP_CHROMA_TYPE
};
static void
gst_vaapi_surface_destroy(GstVaapiSurface *surface)
{
GstVaapiSurfacePrivate * const priv = surface->priv;
2010-03-15 15:55:20 +00:00
VADisplay dpy = GST_VAAPI_DISPLAY_VADISPLAY(priv->display);
VAStatus status;
if (priv->surface_id != VA_INVALID_SURFACE) {
status = vaDestroySurfaces(dpy, &priv->surface_id, 1);
if (!vaapi_check_status(status, "vaDestroySurfaces()"))
g_warning("failed to destroy surface 0x%08x\n", priv->surface_id);
priv->surface_id = VA_INVALID_SURFACE;
}
if (priv->display) {
g_object_unref(priv->display);
priv->display = NULL;
}
}
static gboolean
gst_vaapi_surface_create(GstVaapiSurface *surface)
{
GstVaapiSurfacePrivate * const priv = surface->priv;
VASurfaceID surface_id;
VAStatus status;
2010-03-11 13:58:32 +00:00
guint format;
switch (priv->chroma_type) {
case GST_VAAPI_CHROMA_TYPE_YUV420:
format = VA_RT_FORMAT_YUV420;
break;
case GST_VAAPI_CHROMA_TYPE_YUV422:
format = VA_RT_FORMAT_YUV422;
break;
case GST_VAAPI_CHROMA_TYPE_YUV444:
format = VA_RT_FORMAT_YUV444;
break;
default:
GST_DEBUG("unsupported chroma-type %u\n", priv->chroma_type);
return FALSE;
}
status = vaCreateSurfaces(
2010-03-15 15:55:20 +00:00
GST_VAAPI_DISPLAY_VADISPLAY(priv->display),
priv->width,
priv->height,
2010-03-11 13:58:32 +00:00
format,
1, &surface_id
);
if (!vaapi_check_status(status, "vaCreateSurfaces()"))
return FALSE;
priv->surface_id = surface_id;
return TRUE;
}
static void
gst_vaapi_surface_finalize(GObject *object)
{
gst_vaapi_surface_destroy(GST_VAAPI_SURFACE(object));
G_OBJECT_CLASS(gst_vaapi_surface_parent_class)->finalize(object);
}
static void
2010-03-11 13:58:32 +00:00
gst_vaapi_surface_set_property(
GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec
)
{
GstVaapiSurface * const surface = GST_VAAPI_SURFACE(object);
GstVaapiSurfacePrivate * const priv = surface->priv;
switch (prop_id) {
case PROP_DISPLAY:
priv->display = g_object_ref(g_value_get_object(value));
break;
case PROP_WIDTH:
priv->width = g_value_get_uint(value);
break;
case PROP_HEIGHT:
priv->height = g_value_get_uint(value);
break;
2010-03-11 13:58:32 +00:00
case PROP_CHROMA_TYPE:
priv->chroma_type = g_value_get_uint(value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
2010-03-11 13:58:32 +00:00
gst_vaapi_surface_get_property(
GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec
)
{
2010-03-09 12:00:32 +00:00
GstVaapiSurface * const surface = GST_VAAPI_SURFACE(object);
switch (prop_id) {
case PROP_DISPLAY:
g_value_set_object(value, gst_vaapi_surface_get_display(surface));
break;
case PROP_SURFACE_ID:
g_value_set_uint(value, gst_vaapi_surface_get_id(surface));
break;
case PROP_WIDTH:
g_value_set_uint(value, gst_vaapi_surface_get_width(surface));
break;
case PROP_HEIGHT:
g_value_set_uint(value, gst_vaapi_surface_get_height(surface));
break;
2010-03-11 13:58:32 +00:00
case PROP_CHROMA_TYPE:
g_value_set_uint(value, gst_vaapi_surface_get_chroma_type(surface));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
gst_vaapi_surface_constructed(GObject *object)
{
GstVaapiSurface * const surface = GST_VAAPI_SURFACE(object);
GObjectClass *parent_class;
gst_vaapi_surface_create(surface);
parent_class = G_OBJECT_CLASS(gst_vaapi_surface_parent_class);
if (parent_class->constructed)
parent_class->constructed(object);
}
static void
gst_vaapi_surface_class_init(GstVaapiSurfaceClass *klass)
{
GObjectClass * const object_class = G_OBJECT_CLASS(klass);
g_type_class_add_private(klass, sizeof(GstVaapiSurfacePrivate));
object_class->finalize = gst_vaapi_surface_finalize;
object_class->set_property = gst_vaapi_surface_set_property;
object_class->get_property = gst_vaapi_surface_get_property;
object_class->constructed = gst_vaapi_surface_constructed;
g_object_class_install_property
(object_class,
PROP_DISPLAY,
g_param_spec_object("display",
"display",
"GStreamer Va display",
GST_VAAPI_TYPE_DISPLAY,
G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property
(object_class,
PROP_SURFACE_ID,
g_param_spec_uint("id",
"VA surface id",
"VA surface id",
0, G_MAXUINT32, VA_INVALID_SURFACE,
G_PARAM_READABLE));
g_object_class_install_property
(object_class,
PROP_WIDTH,
g_param_spec_uint("width",
"width",
"VA surface width",
0, G_MAXINT32, 0,
G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property
(object_class,
PROP_HEIGHT,
g_param_spec_uint("height",
"height",
"VA surface height",
0, G_MAXINT32, 0,
G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property
(object_class,
2010-03-11 13:58:32 +00:00
PROP_CHROMA_TYPE,
g_param_spec_uint("chroma-type",
"chroma-type",
"VA surface chroma type",
0, G_MAXUINT32, 0,
G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
}
static void
gst_vaapi_surface_init(GstVaapiSurface *surface)
{
GstVaapiSurfacePrivate *priv = GST_VAAPI_SURFACE_GET_PRIVATE(surface);
surface->priv = priv;
priv->display = NULL;
priv->surface_id = VA_INVALID_SURFACE;
priv->width = 0;
priv->height = 0;
2010-03-11 13:58:32 +00:00
priv->chroma_type = 0;
}
GstVaapiSurface *
2010-03-11 13:58:32 +00:00
gst_vaapi_surface_new(
GstVaapiDisplay *display,
GstVaapiChromaType chroma_type,
guint width,
guint height
)
{
2010-03-11 13:58:32 +00:00
GST_DEBUG("size %ux%u, chroma type 0x%x", width, height, chroma_type);
return g_object_new(GST_VAAPI_TYPE_SURFACE,
2010-03-11 13:58:32 +00:00
"display", display,
"width", width,
"height", height,
"chroma-type", chroma_type,
NULL);
}
VASurfaceID
gst_vaapi_surface_get_id(GstVaapiSurface *surface)
{
g_return_val_if_fail(GST_VAAPI_IS_SURFACE(surface), VA_INVALID_SURFACE);
2010-03-05 10:07:22 +00:00
return surface->priv->surface_id;
}
GstVaapiDisplay *
gst_vaapi_surface_get_display(GstVaapiSurface *surface)
{
g_return_val_if_fail(GST_VAAPI_IS_SURFACE(surface), NULL);
return surface->priv->display;
}
2010-03-11 13:58:32 +00:00
GstVaapiChromaType
gst_vaapi_surface_get_chroma_type(GstVaapiSurface *surface)
{
g_return_val_if_fail(GST_VAAPI_IS_SURFACE(surface), 0);
2010-03-11 13:58:32 +00:00
return surface->priv->chroma_type;
}
guint
2010-03-11 13:58:32 +00:00
gst_vaapi_surface_get_width(GstVaapiSurface *surface)
{
g_return_val_if_fail(GST_VAAPI_IS_SURFACE(surface), 0);
2010-03-11 13:58:32 +00:00
return surface->priv->width;
}
guint
2010-03-11 13:58:32 +00:00
gst_vaapi_surface_get_height(GstVaapiSurface *surface)
{
g_return_val_if_fail(GST_VAAPI_IS_SURFACE(surface), 0);
2010-03-11 13:58:32 +00:00
return surface->priv->height;
}
void
gst_vaapi_surface_get_size(
GstVaapiSurface *surface,
guint *pwidth,
guint *pheight
)
{
g_return_if_fail(GST_VAAPI_IS_SURFACE(surface));
if (pwidth)
*pwidth = gst_vaapi_surface_get_width(surface);
if (pheight)
*pheight = gst_vaapi_surface_get_height(surface);
}
2010-03-15 11:49:03 +00:00
gboolean
gst_vaapi_surface_get_image(GstVaapiSurface *surface, GstVaapiImage *image)
{
VAImageID image_id;
VAStatus status;
guint width, height;
g_return_val_if_fail(GST_VAAPI_IS_SURFACE(surface), FALSE);
g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), FALSE);
gst_vaapi_image_get_size(image, &width, &height);
if (width != surface->priv->width || height != surface->priv->height)
return FALSE;
image_id = gst_vaapi_image_get_id(image);
if (image_id == VA_INVALID_ID)
return FALSE;
status = vaGetImage(
2010-03-15 15:55:20 +00:00
GST_VAAPI_DISPLAY_VADISPLAY(surface->priv->display),
2010-03-15 11:49:03 +00:00
surface->priv->surface_id,
0, 0, width, height,
image_id
);
if (!vaapi_check_status(status, "vaGetImage()"))
return FALSE;
return TRUE;
}
gboolean
gst_vaapi_surface_put_image(GstVaapiSurface *surface, GstVaapiImage *image)
{
VAImageID image_id;
VAStatus status;
guint width, height;
g_return_val_if_fail(GST_VAAPI_IS_SURFACE(surface), FALSE);
g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), FALSE);
gst_vaapi_image_get_size(image, &width, &height);
if (width != surface->priv->width || height != surface->priv->height)
return FALSE;
image_id = gst_vaapi_image_get_id(image);
if (image_id == VA_INVALID_ID)
return FALSE;
status = vaPutImage(
2010-03-15 15:55:20 +00:00
GST_VAAPI_DISPLAY_VADISPLAY(surface->priv->display),
2010-03-15 11:49:03 +00:00
surface->priv->surface_id,
image_id,
0, 0, width, height,
0, 0, width, height
);
if (!vaapi_check_status(status, "vaPutImage()"))
return FALSE;
return TRUE;
}
2010-03-15 16:13:51 +00:00
gboolean
gst_vaapi_surface_sync(GstVaapiSurface *surface)
{
VAStatus status;
g_return_val_if_fail(GST_VAAPI_IS_SURFACE(surface), FALSE);
status = vaSyncSurface(
GST_VAAPI_DISPLAY_VADISPLAY(surface->priv->display),
surface->priv->surface_id
);
if (!vaapi_check_status(status, "vaSyncSurface()"))
return FALSE;
return TRUE;
}