/* * 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 */ /** * SECTION:gst-vaapi-surface * @short_description: */ #include "config.h" #include "gstvaapiutils.h" #include "gstvaapisurface.h" #include "gstvaapiimage.h" #include #define DEBUG 1 #include "gstvaapidebug.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; GstVaapiChromaType chroma_type; }; enum { PROP_0, PROP_DISPLAY, PROP_SURFACE_ID, PROP_WIDTH, PROP_HEIGHT, PROP_CHROMA_TYPE }; static void gst_vaapi_surface_destroy(GstVaapiSurface *surface) { GstVaapiSurfacePrivate * const priv = surface->priv; VAStatus status; if (priv->surface_id != VA_INVALID_SURFACE) { GST_VAAPI_DISPLAY_LOCK(priv->display); status = vaDestroySurfaces( GST_VAAPI_DISPLAY_VADISPLAY(priv->display), &priv->surface_id, 1 ); GST_VAAPI_DISPLAY_UNLOCK(priv->display); 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; 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; } GST_VAAPI_DISPLAY_LOCK(priv->display); status = vaCreateSurfaces( GST_VAAPI_DISPLAY_VADISPLAY(priv->display), priv->width, priv->height, format, 1, &surface_id ); GST_VAAPI_DISPLAY_UNLOCK(priv->display); 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 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; 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 gst_vaapi_surface_get_property( GObject *object, guint prop_id, GValue *value, GParamSpec *pspec ) { 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; 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; /** * GstVaapiSurface:display: * * The #GstVaapiDisplay this surface is bound to. */ g_object_class_install_property (object_class, PROP_DISPLAY, g_param_spec_object("display", "Display", "The GstVaapiDisplay this surface is bound to", GST_VAAPI_TYPE_DISPLAY, G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY)); /** * GstVaapiSurface:id: * * The underlying #VASurfaceID of the surface. */ g_object_class_install_property (object_class, PROP_SURFACE_ID, g_param_spec_uint("id", "VA surface id", "The underlying 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", "The width of the surface", 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", "The height of the surface", 0, G_MAXINT32, 0, G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY)); g_object_class_install_property (object_class, PROP_CHROMA_TYPE, g_param_spec_uint("chroma-type", "Chroma type", "The chroma type of the surface", 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; priv->chroma_type = 0; } /** * gst_vaapi_surface_new: * @display: a #GstVaapiDisplay * @chroma_type: the surface chroma format * @width: the requested surface width * @height: the requested surface height * * Creates a new #GstVaapiSurface with the specified chroma format and * dimensions. * * Return value: the newly allocated #GstVaapiSurface object */ GstVaapiSurface * gst_vaapi_surface_new( GstVaapiDisplay *display, GstVaapiChromaType chroma_type, guint width, guint height ) { GST_DEBUG("size %ux%u, chroma type 0x%x", width, height, chroma_type); return g_object_new(GST_VAAPI_TYPE_SURFACE, "display", display, "width", width, "height", height, "chroma-type", chroma_type, NULL); } /** * gst_vaapi_surface_get_id: * @surface: a #GstVaapiSurface * * Returns the underlying VASurfaceID of the @surface. * * Return value: the underlying VA surface id */ VASurfaceID gst_vaapi_surface_get_id(GstVaapiSurface *surface) { g_return_val_if_fail(GST_VAAPI_IS_SURFACE(surface), VA_INVALID_SURFACE); return surface->priv->surface_id; } /** * gst_vaapi_surface_get_display: * @surface: a #GstVaapiSurface * * Returns the #GstVaapiDisplay this @surface is bound to. * * Return value: the parent #GstVaapiDisplay object */ GstVaapiDisplay * gst_vaapi_surface_get_display(GstVaapiSurface *surface) { g_return_val_if_fail(GST_VAAPI_IS_SURFACE(surface), NULL); return surface->priv->display; } /** * gst_vaapi_surface_get_chroma_type: * @surface: a #GstVaapiSurface * * Returns the #GstVaapiChromaType the @surface was created with. * * Return value: the #GstVaapiChromaType */ GstVaapiChromaType gst_vaapi_surface_get_chroma_type(GstVaapiSurface *surface) { g_return_val_if_fail(GST_VAAPI_IS_SURFACE(surface), 0); return surface->priv->chroma_type; } /** * gst_vaapi_surface_get_width: * @surface: a #GstVaapiSurface * * Returns the @surface width. * * Return value: the surface width, in pixels */ guint gst_vaapi_surface_get_width(GstVaapiSurface *surface) { g_return_val_if_fail(GST_VAAPI_IS_SURFACE(surface), 0); return surface->priv->width; } /** * gst_vaapi_surface_get_height: * @surface: a #GstVaapiSurface * * Returns the @surface height. * * Return value: the surface height, in pixels. */ guint gst_vaapi_surface_get_height(GstVaapiSurface *surface) { g_return_val_if_fail(GST_VAAPI_IS_SURFACE(surface), 0); return surface->priv->height; } /** * gst_vaapi_surface_get_size: * @surface: a #GstVaapiSurface * @pwidth: (out) (allow-none): return location for the width, or %NULL * @pheight: (out) (allow-none): return location for the height, or %NULL * * Retrieves the dimensions of a #GstVaapiSurface. */ 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); } /** * gst_vaapi_surface_derive_image: * @surface: a #GstVaapiSurface * * Derives a #GstVaapiImage from the @surface. This image buffer can * then be mapped/unmapped for direct CPU access. This operation is * only possible if the underlying implementation supports direct * rendering capabilities and internal surface formats that can be * represented with a #GstVaapiImage. * * When the operation is not possible, the function returns %NULL and * the user should then fallback to using gst_vaapi_surface_get_image() * or gst_vaapi_surface_put_image() to accomplish the same task in an * indirect manner (additional copy). * * An image created with gst_vaapi_surface_derive_image() should be * unreferenced when it's no longer needed. The image and image buffer * data structures will be destroyed. However, the surface contents * will remain unchanged until destroyed through the last call to * g_object_unref(). * * Return value: the newly allocated #GstVaapiImage object, or %NULL * on failure */ GstVaapiImage * gst_vaapi_surface_derive_image(GstVaapiSurface *surface) { VAImage va_image; VAStatus status; g_return_val_if_fail(GST_VAAPI_IS_SURFACE(surface), NULL); va_image.image_id = VA_INVALID_ID; va_image.buf = VA_INVALID_ID; GST_VAAPI_DISPLAY_LOCK(surface->priv->display); status = vaDeriveImage( GST_VAAPI_DISPLAY_VADISPLAY(surface->priv->display), surface->priv->surface_id, &va_image ); GST_VAAPI_DISPLAY_UNLOCK(surface->priv->display); if (!vaapi_check_status(status, "vaDeriveImage()")) return NULL; if (va_image.image_id == VA_INVALID_ID || va_image.buf == VA_INVALID_ID) return NULL; return gst_vaapi_image_new_with_image(surface->priv->display, &va_image); } /** * gst_vaapi_surface_get_image * @surface: a #GstVaapiSurface * @image: a #GstVaapiImage * * Retrieves surface data into a #GstVaapiImage. The @image must have * a format supported by the @surface. * * Return value: %TRUE on success */ 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; GST_VAAPI_DISPLAY_LOCK(surface->priv->display); status = vaGetImage( GST_VAAPI_DISPLAY_VADISPLAY(surface->priv->display), surface->priv->surface_id, 0, 0, width, height, image_id ); GST_VAAPI_DISPLAY_UNLOCK(surface->priv->display); if (!vaapi_check_status(status, "vaGetImage()")) return FALSE; return TRUE; } /** * gst_vaapi_surface_put_image: * @surface: a #GstVaapiSurface * @image: a #GstVaapiImage * * Copies data from a #GstVaapiImage into a @surface. The @image must * have a format supported by the @surface. * * Return value: %TRUE on success */ 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; GST_VAAPI_DISPLAY_LOCK(surface->priv->display); status = vaPutImage( GST_VAAPI_DISPLAY_VADISPLAY(surface->priv->display), surface->priv->surface_id, image_id, 0, 0, width, height, 0, 0, width, height ); GST_VAAPI_DISPLAY_UNLOCK(surface->priv->display); if (!vaapi_check_status(status, "vaPutImage()")) return FALSE; return TRUE; } /** * gst_vaapi_surface_sync: * @surface: a #GstVaapiSurface * * Blocks until all pending operations on the @surface have been * completed. * * Return value: %TRUE on success */ gboolean gst_vaapi_surface_sync(GstVaapiSurface *surface) { VAStatus status; g_return_val_if_fail(GST_VAAPI_IS_SURFACE(surface), FALSE); GST_VAAPI_DISPLAY_LOCK(surface->priv->display); status = vaSyncSurface( GST_VAAPI_DISPLAY_VADISPLAY(surface->priv->display), surface->priv->surface_id ); GST_VAAPI_DISPLAY_UNLOCK(surface->priv->display); if (!vaapi_check_status(status, "vaSyncSurface()")) return FALSE; return TRUE; }