2010-03-04 17:39:01 +00:00
|
|
|
/*
|
|
|
|
* gstvaapiimage.c - VA image 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"
|
|
|
|
#include <string.h>
|
2010-03-16 09:15:48 +00:00
|
|
|
#include "gstvaapiutils.h"
|
2010-03-04 17:39:01 +00:00
|
|
|
#include "gstvaapiimage.h"
|
|
|
|
#include <va/va_backend.h>
|
|
|
|
|
|
|
|
#define DEBUG 1
|
2010-03-16 09:17:41 +00:00
|
|
|
#include "gstvaapidebug.h"
|
2010-03-04 17:39:01 +00:00
|
|
|
|
|
|
|
G_DEFINE_TYPE(GstVaapiImage, gst_vaapi_image, G_TYPE_OBJECT);
|
|
|
|
|
|
|
|
#define GST_VAAPI_IMAGE_GET_PRIVATE(obj) \
|
|
|
|
(G_TYPE_INSTANCE_GET_PRIVATE((obj), \
|
|
|
|
GST_VAAPI_TYPE_IMAGE, \
|
|
|
|
GstVaapiImagePrivate))
|
|
|
|
|
|
|
|
struct _GstVaapiImagePrivate {
|
|
|
|
GstVaapiDisplay *display;
|
2010-03-17 10:43:02 +00:00
|
|
|
VAImage internal_image;
|
2010-03-04 17:39:01 +00:00
|
|
|
VAImage image;
|
|
|
|
guchar *image_data;
|
2010-03-16 08:43:16 +00:00
|
|
|
GstVaapiImageFormat internal_format;
|
2010-03-15 10:27:10 +00:00
|
|
|
GstVaapiImageFormat format;
|
2010-03-04 17:39:01 +00:00
|
|
|
guint width;
|
|
|
|
guint height;
|
2010-03-17 10:43:02 +00:00
|
|
|
guint is_constructed : 1;
|
|
|
|
guint is_linear : 1;
|
2010-03-04 17:39:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
PROP_0,
|
|
|
|
|
|
|
|
PROP_DISPLAY,
|
|
|
|
PROP_IMAGE_ID,
|
2010-03-15 10:27:10 +00:00
|
|
|
PROP_FORMAT,
|
2010-03-04 17:39:01 +00:00
|
|
|
PROP_WIDTH,
|
2010-03-15 10:27:10 +00:00
|
|
|
PROP_HEIGHT
|
2010-03-04 17:39:01 +00:00
|
|
|
};
|
|
|
|
|
2010-03-16 17:57:23 +00:00
|
|
|
#define SWAP_UINT(a, b) do { \
|
2010-03-18 08:02:25 +00:00
|
|
|
guint v = a; \
|
2010-03-16 17:57:23 +00:00
|
|
|
a = b; \
|
|
|
|
b = v; \
|
|
|
|
} while (0)
|
|
|
|
|
2010-03-18 12:59:55 +00:00
|
|
|
static gboolean
|
|
|
|
_gst_vaapi_image_map(GstVaapiImage *image);
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
_gst_vaapi_image_unmap(GstVaapiImage *image);
|
|
|
|
|
2010-03-04 17:39:01 +00:00
|
|
|
static void
|
|
|
|
gst_vaapi_image_destroy(GstVaapiImage *image)
|
|
|
|
{
|
|
|
|
GstVaapiImagePrivate * const priv = image->priv;
|
|
|
|
VAStatus status;
|
|
|
|
|
2010-03-18 12:59:55 +00:00
|
|
|
_gst_vaapi_image_unmap(image);
|
2010-03-04 17:39:01 +00:00
|
|
|
|
2010-03-17 10:43:02 +00:00
|
|
|
if (priv->internal_image.image_id != VA_INVALID_ID) {
|
2010-03-17 07:59:31 +00:00
|
|
|
GST_VAAPI_DISPLAY_LOCK(priv->display);
|
|
|
|
status = vaDestroyImage(
|
|
|
|
GST_VAAPI_DISPLAY_VADISPLAY(priv->display),
|
2010-03-17 10:43:02 +00:00
|
|
|
priv->internal_image.image_id
|
2010-03-17 07:59:31 +00:00
|
|
|
);
|
|
|
|
GST_VAAPI_DISPLAY_UNLOCK(priv->display);
|
2010-03-04 17:39:01 +00:00
|
|
|
if (!vaapi_check_status(status, "vaDestroyImage()"))
|
2010-03-17 10:43:02 +00:00
|
|
|
g_warning("failed to destroy image 0x%08x\n",
|
|
|
|
priv->internal_image.image_id);
|
|
|
|
priv->internal_image.image_id = VA_INVALID_ID;
|
2010-03-04 17:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->display) {
|
|
|
|
g_object_unref(priv->display);
|
|
|
|
priv->display = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2010-03-16 08:43:16 +00:00
|
|
|
_gst_vaapi_image_create(GstVaapiImage *image, GstVaapiImageFormat format)
|
2010-03-04 17:39:01 +00:00
|
|
|
{
|
|
|
|
GstVaapiImagePrivate * const priv = image->priv;
|
2010-03-16 08:43:16 +00:00
|
|
|
const VAImageFormat *va_format;
|
2010-03-04 17:39:01 +00:00
|
|
|
VAStatus status;
|
|
|
|
|
2010-03-16 08:43:16 +00:00
|
|
|
if (!gst_vaapi_display_has_image_format(priv->display, format))
|
2010-03-04 17:39:01 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2010-03-16 08:43:16 +00:00
|
|
|
va_format = gst_vaapi_image_format_get_va_format(format);
|
|
|
|
if (!va_format)
|
|
|
|
return FALSE;
|
2010-03-04 17:39:01 +00:00
|
|
|
|
2010-03-17 07:59:31 +00:00
|
|
|
GST_VAAPI_DISPLAY_LOCK(priv->display);
|
2010-03-04 17:39:01 +00:00
|
|
|
status = vaCreateImage(
|
2010-03-17 07:59:31 +00:00
|
|
|
GST_VAAPI_DISPLAY_VADISPLAY(priv->display),
|
2010-03-16 08:43:16 +00:00
|
|
|
(VAImageFormat *)va_format,
|
2010-03-04 17:39:01 +00:00
|
|
|
priv->width,
|
|
|
|
priv->height,
|
2010-03-17 10:43:02 +00:00
|
|
|
&priv->internal_image
|
2010-03-04 17:39:01 +00:00
|
|
|
);
|
2010-03-17 07:59:31 +00:00
|
|
|
GST_VAAPI_DISPLAY_UNLOCK(priv->display);
|
2010-03-17 10:43:02 +00:00
|
|
|
if (status != VA_STATUS_SUCCESS ||
|
|
|
|
priv->internal_image.format.fourcc != va_format->fourcc)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
priv->internal_format = format;
|
|
|
|
return TRUE;
|
2010-03-16 08:43:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2010-03-17 10:43:02 +00:00
|
|
|
_gst_vaapi_image_is_linear(GstVaapiImage *image)
|
2010-03-16 08:43:16 +00:00
|
|
|
{
|
|
|
|
GstVaapiImagePrivate * const priv = image->priv;
|
2010-03-17 10:43:02 +00:00
|
|
|
guint i, width, height, width2, height2, data_size;
|
2010-03-16 08:43:16 +00:00
|
|
|
|
2010-03-17 10:43:02 +00:00
|
|
|
for (i = 1; i < priv->image.num_planes; i++)
|
|
|
|
if (priv->image.offsets[i] < priv->image.offsets[i - 1])
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
width = priv->width;
|
|
|
|
height = priv->height;
|
|
|
|
width2 = (width + 1) / 2;
|
|
|
|
height2 = (height + 1) / 2;
|
2010-03-16 08:43:16 +00:00
|
|
|
|
2010-03-17 10:43:02 +00:00
|
|
|
switch (priv->internal_format) {
|
|
|
|
case GST_VAAPI_IMAGE_NV12:
|
|
|
|
case GST_VAAPI_IMAGE_YV12:
|
2010-03-16 08:43:16 +00:00
|
|
|
case GST_VAAPI_IMAGE_I420:
|
2010-03-17 10:43:02 +00:00
|
|
|
data_size = width * height + 2 * width2 * height2;
|
2010-03-16 08:43:16 +00:00
|
|
|
break;
|
2010-03-17 10:43:02 +00:00
|
|
|
case GST_VAAPI_IMAGE_ARGB:
|
|
|
|
case GST_VAAPI_IMAGE_RGBA:
|
|
|
|
case GST_VAAPI_IMAGE_ABGR:
|
|
|
|
case GST_VAAPI_IMAGE_BGRA:
|
|
|
|
data_size = 4 * width * height;
|
2010-03-16 08:43:16 +00:00
|
|
|
break;
|
|
|
|
default:
|
2010-03-17 10:43:02 +00:00
|
|
|
g_error("FIXME: incomplete formats");
|
2010-03-16 08:43:16 +00:00
|
|
|
break;
|
|
|
|
}
|
2010-03-17 10:43:02 +00:00
|
|
|
return priv->image.data_size == data_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_vaapi_image_create(GstVaapiImage *image)
|
|
|
|
{
|
|
|
|
GstVaapiImagePrivate * const priv = image->priv;
|
|
|
|
GstVaapiImageFormat format = priv->format;
|
|
|
|
const VAImageFormat *va_format;
|
|
|
|
|
|
|
|
if (!_gst_vaapi_image_create(image, format)) {
|
|
|
|
switch (format) {
|
|
|
|
case GST_VAAPI_IMAGE_I420:
|
|
|
|
format = GST_VAAPI_IMAGE_YV12;
|
|
|
|
break;
|
|
|
|
case GST_VAAPI_IMAGE_YV12:
|
|
|
|
format = GST_VAAPI_IMAGE_I420;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
format = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!format || !_gst_vaapi_image_create(image, format))
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
priv->image = priv->internal_image;
|
|
|
|
|
|
|
|
if (priv->format != priv->internal_format) {
|
|
|
|
switch (priv->format) {
|
|
|
|
case GST_VAAPI_IMAGE_YV12:
|
|
|
|
case GST_VAAPI_IMAGE_I420:
|
|
|
|
va_format = gst_vaapi_image_format_get_va_format(priv->format);
|
|
|
|
if (!va_format)
|
|
|
|
return FALSE;
|
|
|
|
priv->image.format = *va_format;
|
|
|
|
SWAP_UINT(priv->image.offsets[1], priv->image.offsets[2]);
|
|
|
|
SWAP_UINT(priv->image.pitches[1], priv->image.pitches[2]);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-03-04 17:39:01 +00:00
|
|
|
|
2010-03-16 08:43:16 +00:00
|
|
|
GST_DEBUG("image 0x%08x", priv->image.image_id);
|
2010-03-17 10:43:02 +00:00
|
|
|
priv->is_linear = _gst_vaapi_image_is_linear(image);
|
2010-03-04 17:39:01 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vaapi_image_finalize(GObject *object)
|
|
|
|
{
|
|
|
|
gst_vaapi_image_destroy(GST_VAAPI_IMAGE(object));
|
|
|
|
|
|
|
|
G_OBJECT_CLASS(gst_vaapi_image_parent_class)->finalize(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vaapi_image_set_property(
|
|
|
|
GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec
|
|
|
|
)
|
|
|
|
{
|
|
|
|
GstVaapiImage * const image = GST_VAAPI_IMAGE(object);
|
|
|
|
GstVaapiImagePrivate * const priv = image->priv;
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_DISPLAY:
|
2010-03-11 12:30:12 +00:00
|
|
|
priv->display = g_object_ref(g_value_get_object(value));
|
2010-03-04 17:39:01 +00:00
|
|
|
break;
|
2010-03-15 10:27:10 +00:00
|
|
|
case PROP_FORMAT:
|
|
|
|
priv->format = g_value_get_uint(value);
|
|
|
|
break;
|
2010-03-04 17:39:01 +00:00
|
|
|
case PROP_WIDTH:
|
|
|
|
priv->width = g_value_get_uint(value);
|
|
|
|
break;
|
|
|
|
case PROP_HEIGHT:
|
|
|
|
priv->height = g_value_get_uint(value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vaapi_image_get_property(
|
|
|
|
GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec
|
|
|
|
)
|
|
|
|
{
|
2010-03-11 12:30:12 +00:00
|
|
|
GstVaapiImage * const image = GST_VAAPI_IMAGE(object);
|
2010-03-04 17:39:01 +00:00
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_DISPLAY:
|
2010-03-11 12:30:12 +00:00
|
|
|
g_value_set_pointer(value, gst_vaapi_image_get_display(image));
|
2010-03-04 17:39:01 +00:00
|
|
|
break;
|
|
|
|
case PROP_IMAGE_ID:
|
|
|
|
g_value_set_uint(value, gst_vaapi_image_get_id(image));
|
|
|
|
break;
|
2010-03-15 10:27:10 +00:00
|
|
|
case PROP_FORMAT:
|
|
|
|
g_value_set_uint(value, gst_vaapi_image_get_format(image));
|
|
|
|
break;
|
2010-03-04 17:39:01 +00:00
|
|
|
case PROP_WIDTH:
|
|
|
|
g_value_set_uint(value, gst_vaapi_image_get_width(image));
|
|
|
|
break;
|
|
|
|
case PROP_HEIGHT:
|
|
|
|
g_value_set_uint(value, gst_vaapi_image_get_height(image));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-10 13:10:59 +00:00
|
|
|
static void
|
|
|
|
gst_vaapi_image_constructed(GObject *object)
|
2010-03-04 17:39:01 +00:00
|
|
|
{
|
2010-03-10 13:10:59 +00:00
|
|
|
GstVaapiImage * const image = GST_VAAPI_IMAGE(object);
|
2010-03-04 17:39:01 +00:00
|
|
|
GObjectClass *parent_class;
|
|
|
|
|
2010-03-15 10:27:10 +00:00
|
|
|
image->priv->is_constructed = gst_vaapi_image_create(image);
|
2010-03-04 17:39:01 +00:00
|
|
|
|
|
|
|
parent_class = G_OBJECT_CLASS(gst_vaapi_image_parent_class);
|
2010-03-10 13:10:59 +00:00
|
|
|
if (parent_class->constructed)
|
|
|
|
parent_class->constructed(object);
|
2010-03-04 17:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vaapi_image_class_init(GstVaapiImageClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass * const object_class = G_OBJECT_CLASS(klass);
|
|
|
|
|
|
|
|
g_type_class_add_private(klass, sizeof(GstVaapiImagePrivate));
|
|
|
|
|
|
|
|
object_class->finalize = gst_vaapi_image_finalize;
|
|
|
|
object_class->set_property = gst_vaapi_image_set_property;
|
|
|
|
object_class->get_property = gst_vaapi_image_get_property;
|
2010-03-10 13:10:59 +00:00
|
|
|
object_class->constructed = gst_vaapi_image_constructed;
|
2010-03-04 17:39:01 +00:00
|
|
|
|
|
|
|
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_IMAGE_ID,
|
|
|
|
g_param_spec_uint("id",
|
|
|
|
"VA image id",
|
|
|
|
"VA image id",
|
|
|
|
0, G_MAXUINT32, VA_INVALID_ID,
|
|
|
|
G_PARAM_READABLE));
|
|
|
|
|
|
|
|
g_object_class_install_property
|
|
|
|
(object_class,
|
|
|
|
PROP_WIDTH,
|
|
|
|
g_param_spec_uint("width",
|
|
|
|
"width",
|
|
|
|
"Image width",
|
|
|
|
0, G_MAXUINT32, 0,
|
|
|
|
G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
|
|
|
|
|
|
|
|
g_object_class_install_property
|
|
|
|
(object_class,
|
|
|
|
PROP_HEIGHT,
|
|
|
|
g_param_spec_uint("height",
|
|
|
|
"height",
|
|
|
|
"Image height",
|
|
|
|
0, G_MAXUINT32, 0,
|
|
|
|
G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
|
|
|
|
|
|
|
|
g_object_class_install_property
|
|
|
|
(object_class,
|
|
|
|
PROP_FORMAT,
|
|
|
|
g_param_spec_uint("format",
|
|
|
|
"format",
|
|
|
|
"Image format",
|
|
|
|
0, G_MAXUINT32, 0,
|
|
|
|
G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vaapi_image_init(GstVaapiImage *image)
|
|
|
|
{
|
|
|
|
GstVaapiImagePrivate *priv = GST_VAAPI_IMAGE_GET_PRIVATE(image);
|
|
|
|
|
2010-03-17 10:43:02 +00:00
|
|
|
image->priv = priv;
|
|
|
|
priv->display = NULL;
|
|
|
|
priv->image_data = NULL;
|
|
|
|
priv->width = 0;
|
|
|
|
priv->height = 0;
|
|
|
|
priv->internal_format = 0;
|
|
|
|
priv->format = 0;
|
|
|
|
priv->is_constructed = FALSE;
|
|
|
|
priv->is_linear = FALSE;
|
|
|
|
|
|
|
|
memset(&priv->internal_image, 0, sizeof(priv->internal_image));
|
|
|
|
priv->internal_image.image_id = VA_INVALID_ID;
|
|
|
|
priv->internal_image.buf = VA_INVALID_ID;
|
2010-03-04 17:39:01 +00:00
|
|
|
|
|
|
|
memset(&priv->image, 0, sizeof(priv->image));
|
2010-03-17 10:43:02 +00:00
|
|
|
priv->image.image_id = VA_INVALID_ID;
|
|
|
|
priv->image.buf = VA_INVALID_ID;
|
2010-03-04 17:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GstVaapiImage *
|
|
|
|
gst_vaapi_image_new(
|
|
|
|
GstVaapiDisplay *display,
|
2010-03-15 10:27:10 +00:00
|
|
|
GstVaapiImageFormat format,
|
2010-03-04 17:39:01 +00:00
|
|
|
guint width,
|
2010-03-15 10:27:10 +00:00
|
|
|
guint height
|
2010-03-04 17:39:01 +00:00
|
|
|
)
|
|
|
|
{
|
2010-03-18 12:56:53 +00:00
|
|
|
GstVaapiImage *image;
|
|
|
|
|
2010-03-10 13:13:51 +00:00
|
|
|
g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
|
|
|
|
g_return_val_if_fail(width > 0, NULL);
|
|
|
|
g_return_val_if_fail(height > 0, NULL);
|
|
|
|
|
2010-03-15 16:13:37 +00:00
|
|
|
GST_DEBUG("format %" GST_FOURCC_FORMAT ", size %ux%u",
|
|
|
|
GST_FOURCC_ARGS(format), width, height);
|
2010-03-04 17:39:01 +00:00
|
|
|
|
2010-03-18 12:56:53 +00:00
|
|
|
image = g_object_new(
|
|
|
|
GST_VAAPI_TYPE_IMAGE,
|
|
|
|
"display", display,
|
|
|
|
"format", format,
|
|
|
|
"width", width,
|
|
|
|
"height", height,
|
|
|
|
NULL
|
|
|
|
);
|
|
|
|
if (!image)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!image->priv->is_constructed) {
|
|
|
|
g_object_unref(image);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return image;
|
2010-03-04 17:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VAImageID
|
|
|
|
gst_vaapi_image_get_id(GstVaapiImage *image)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), VA_INVALID_ID);
|
2010-03-16 17:57:23 +00:00
|
|
|
g_return_val_if_fail(image->priv->is_constructed, VA_INVALID_ID);
|
2010-03-04 17:39:01 +00:00
|
|
|
|
|
|
|
return image->priv->image.image_id;
|
|
|
|
}
|
|
|
|
|
2010-03-16 17:57:23 +00:00
|
|
|
gboolean
|
|
|
|
gst_vaapi_image_get_image(GstVaapiImage *image, VAImage *va_image)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), FALSE);
|
|
|
|
g_return_val_if_fail(image->priv->is_constructed, FALSE);
|
|
|
|
|
2010-03-17 10:43:02 +00:00
|
|
|
if (va_image)
|
|
|
|
*va_image = image->priv->image;
|
2010-03-16 17:57:23 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-03-04 17:39:01 +00:00
|
|
|
GstVaapiDisplay *
|
|
|
|
gst_vaapi_image_get_display(GstVaapiImage *image)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), NULL);
|
2010-03-15 10:27:10 +00:00
|
|
|
g_return_val_if_fail(image->priv->is_constructed, FALSE);
|
2010-03-04 17:39:01 +00:00
|
|
|
|
2010-03-11 12:30:12 +00:00
|
|
|
return image->priv->display;
|
2010-03-04 17:39:01 +00:00
|
|
|
}
|
|
|
|
|
2010-03-15 15:55:20 +00:00
|
|
|
GstVaapiImageFormat
|
|
|
|
gst_vaapi_image_get_format(GstVaapiImage *image)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), 0);
|
|
|
|
g_return_val_if_fail(image->priv->is_constructed, FALSE);
|
|
|
|
|
|
|
|
return image->priv->format;
|
|
|
|
}
|
|
|
|
|
2010-03-04 17:39:01 +00:00
|
|
|
guint
|
|
|
|
gst_vaapi_image_get_width(GstVaapiImage *image)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), 0);
|
2010-03-15 10:27:10 +00:00
|
|
|
g_return_val_if_fail(image->priv->is_constructed, FALSE);
|
2010-03-04 17:39:01 +00:00
|
|
|
|
|
|
|
return image->priv->width;
|
|
|
|
}
|
|
|
|
|
|
|
|
guint
|
|
|
|
gst_vaapi_image_get_height(GstVaapiImage *image)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), 0);
|
2010-03-15 10:27:10 +00:00
|
|
|
g_return_val_if_fail(image->priv->is_constructed, FALSE);
|
2010-03-04 17:39:01 +00:00
|
|
|
|
|
|
|
return image->priv->height;
|
|
|
|
}
|
|
|
|
|
2010-03-15 11:49:03 +00:00
|
|
|
void
|
|
|
|
gst_vaapi_image_get_size(GstVaapiImage *image, guint *pwidth, guint *pheight)
|
|
|
|
{
|
2010-03-15 14:57:30 +00:00
|
|
|
g_return_if_fail(GST_VAAPI_IS_IMAGE(image));
|
|
|
|
g_return_if_fail(image->priv->is_constructed);
|
2010-03-15 11:49:03 +00:00
|
|
|
|
|
|
|
if (pwidth)
|
|
|
|
*pwidth = image->priv->width;
|
|
|
|
|
|
|
|
if (pheight)
|
|
|
|
*pheight = image->priv->height;
|
|
|
|
}
|
|
|
|
|
2010-03-16 17:57:23 +00:00
|
|
|
gboolean
|
|
|
|
gst_vaapi_image_is_linear(GstVaapiImage *image)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), FALSE);
|
|
|
|
g_return_val_if_fail(image->priv->is_constructed, FALSE);
|
|
|
|
|
2010-03-17 10:43:02 +00:00
|
|
|
return image->priv->is_linear;
|
2010-03-16 17:57:23 +00:00
|
|
|
}
|
|
|
|
|
2010-03-04 17:39:01 +00:00
|
|
|
static inline gboolean
|
|
|
|
_gst_vaapi_image_is_mapped(GstVaapiImage *image)
|
|
|
|
{
|
|
|
|
return image->priv->image_data != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_vaapi_image_is_mapped(GstVaapiImage *image)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), FALSE);
|
2010-03-15 10:27:10 +00:00
|
|
|
g_return_val_if_fail(image->priv->is_constructed, FALSE);
|
2010-03-04 17:39:01 +00:00
|
|
|
|
|
|
|
return _gst_vaapi_image_is_mapped(image);
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_vaapi_image_map(GstVaapiImage *image)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), FALSE);
|
2010-03-15 10:27:10 +00:00
|
|
|
g_return_val_if_fail(image->priv->is_constructed, FALSE);
|
2010-03-04 17:39:01 +00:00
|
|
|
|
2010-03-18 12:59:55 +00:00
|
|
|
return _gst_vaapi_image_map(image);
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
_gst_vaapi_image_map(GstVaapiImage *image)
|
|
|
|
{
|
|
|
|
void *image_data;
|
|
|
|
VAStatus status;
|
|
|
|
|
2010-03-04 17:39:01 +00:00
|
|
|
if (_gst_vaapi_image_is_mapped(image))
|
|
|
|
return TRUE;
|
|
|
|
|
2010-03-17 07:59:31 +00:00
|
|
|
GST_VAAPI_DISPLAY_LOCK(image->priv->display);
|
2010-03-04 17:39:01 +00:00
|
|
|
status = vaMapBuffer(
|
2010-03-17 07:59:31 +00:00
|
|
|
GST_VAAPI_DISPLAY_VADISPLAY(image->priv->display),
|
2010-03-04 17:39:01 +00:00
|
|
|
image->priv->image.buf,
|
|
|
|
&image_data
|
|
|
|
);
|
2010-03-17 07:59:31 +00:00
|
|
|
GST_VAAPI_DISPLAY_UNLOCK(image->priv->display);
|
2010-03-04 17:39:01 +00:00
|
|
|
if (!vaapi_check_status(status, "vaMapBuffer()"))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
image->priv->image_data = image_data;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_vaapi_image_unmap(GstVaapiImage *image)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), FALSE);
|
2010-03-15 10:27:10 +00:00
|
|
|
g_return_val_if_fail(image->priv->is_constructed, FALSE);
|
2010-03-04 17:39:01 +00:00
|
|
|
|
2010-03-18 12:59:55 +00:00
|
|
|
return _gst_vaapi_image_unmap(image);
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
_gst_vaapi_image_unmap(GstVaapiImage *image)
|
|
|
|
{
|
|
|
|
VAStatus status;
|
|
|
|
|
2010-03-04 17:39:01 +00:00
|
|
|
if (!_gst_vaapi_image_is_mapped(image))
|
|
|
|
return FALSE;
|
|
|
|
|
2010-03-17 07:59:31 +00:00
|
|
|
GST_VAAPI_DISPLAY_LOCK(image->priv->display);
|
2010-03-04 17:39:01 +00:00
|
|
|
status = vaUnmapBuffer(
|
2010-03-17 07:59:31 +00:00
|
|
|
GST_VAAPI_DISPLAY_VADISPLAY(image->priv->display),
|
2010-03-04 17:39:01 +00:00
|
|
|
image->priv->image.buf
|
|
|
|
);
|
2010-03-17 07:59:31 +00:00
|
|
|
GST_VAAPI_DISPLAY_UNLOCK(image->priv->display);
|
2010-03-04 17:39:01 +00:00
|
|
|
if (!vaapi_check_status(status, "vaUnmapBuffer()"))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
image->priv->image_data = NULL;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
guint
|
|
|
|
gst_vaapi_image_get_plane_count(GstVaapiImage *image)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), 0);
|
2010-03-15 10:27:10 +00:00
|
|
|
g_return_val_if_fail(image->priv->is_constructed, FALSE);
|
2010-03-04 17:39:01 +00:00
|
|
|
g_return_val_if_fail(_gst_vaapi_image_is_mapped(image), 0);
|
|
|
|
|
|
|
|
return image->priv->image.num_planes;
|
|
|
|
}
|
|
|
|
|
|
|
|
guchar *
|
|
|
|
gst_vaapi_image_get_plane(GstVaapiImage *image, guint plane)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), NULL);
|
2010-03-15 10:27:10 +00:00
|
|
|
g_return_val_if_fail(image->priv->is_constructed, FALSE);
|
2010-03-04 17:39:01 +00:00
|
|
|
g_return_val_if_fail(_gst_vaapi_image_is_mapped(image), NULL);
|
|
|
|
g_return_val_if_fail(plane < image->priv->image.num_planes, NULL);
|
|
|
|
|
|
|
|
return image->priv->image_data + image->priv->image.offsets[plane];
|
|
|
|
}
|
|
|
|
|
|
|
|
guint
|
|
|
|
gst_vaapi_image_get_pitch(GstVaapiImage *image, guint plane)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), 0);
|
2010-03-15 10:27:10 +00:00
|
|
|
g_return_val_if_fail(image->priv->is_constructed, FALSE);
|
2010-03-04 17:39:01 +00:00
|
|
|
g_return_val_if_fail(_gst_vaapi_image_is_mapped(image), 0);
|
|
|
|
g_return_val_if_fail(plane < image->priv->image.num_planes, 0);
|
|
|
|
|
|
|
|
return image->priv->image.pitches[plane];
|
|
|
|
}
|
2010-03-15 10:27:10 +00:00
|
|
|
|
2010-03-16 17:57:23 +00:00
|
|
|
guint
|
|
|
|
gst_vaapi_image_get_data_size(GstVaapiImage *image)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), 0);
|
|
|
|
g_return_val_if_fail(image->priv->is_constructed, FALSE);
|
|
|
|
|
|
|
|
return image->priv->image.data_size;
|
|
|
|
}
|
|
|
|
|
2010-03-15 10:27:10 +00:00
|
|
|
gboolean
|
|
|
|
gst_vaapi_image_update_from_buffer(GstVaapiImage *image, GstBuffer *buffer)
|
|
|
|
{
|
|
|
|
GstVaapiImagePrivate *priv;
|
|
|
|
GstStructure *structure;
|
|
|
|
GstCaps *caps;
|
|
|
|
GstVaapiImageFormat format;
|
|
|
|
gint width, height;
|
|
|
|
guint offsets[3], pitches[3], widths[3], heights[3];
|
|
|
|
guint i, j;
|
|
|
|
guchar *data;
|
|
|
|
guint32 data_size;
|
|
|
|
|
|
|
|
g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), FALSE);
|
|
|
|
g_return_val_if_fail(image->priv->is_constructed, FALSE);
|
|
|
|
g_return_val_if_fail(GST_IS_BUFFER(buffer), FALSE);
|
|
|
|
|
|
|
|
priv = image->priv;
|
|
|
|
data = GST_BUFFER_DATA(buffer);
|
|
|
|
data_size = GST_BUFFER_SIZE(buffer);
|
|
|
|
caps = GST_BUFFER_CAPS(buffer);
|
|
|
|
|
|
|
|
if (!caps)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
format = gst_vaapi_image_format_from_caps(caps);
|
2010-03-16 08:43:16 +00:00
|
|
|
if (format != priv->format)
|
2010-03-15 10:27:10 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
structure = gst_caps_get_structure(caps, 0);
|
|
|
|
gst_structure_get_int(structure, "width", &width);
|
|
|
|
gst_structure_get_int(structure, "height", &height);
|
|
|
|
if (width != priv->width || height != priv->height)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!gst_vaapi_image_map(image))
|
|
|
|
return FALSE;
|
|
|
|
|
2010-03-17 10:43:02 +00:00
|
|
|
if (priv->is_linear && data_size == priv->image.data_size)
|
2010-03-15 10:27:10 +00:00
|
|
|
memcpy(priv->image_data, data, data_size);
|
|
|
|
else {
|
|
|
|
/* XXX: copied from gst_video_format_get_row_stride() -- no NV12? */
|
|
|
|
const guint width2 = (width + 1) / 2;
|
|
|
|
const guint height2 = (height + 1) / 2;
|
|
|
|
guint size2;
|
|
|
|
switch (format) {
|
|
|
|
case GST_VAAPI_IMAGE_NV12:
|
|
|
|
offsets[0] = 0;
|
|
|
|
pitches[0] = GST_ROUND_UP_4(width);
|
|
|
|
widths [0] = width;
|
|
|
|
heights[0] = height;
|
|
|
|
offsets[1] = offsets[0] + height * pitches[0];
|
|
|
|
pitches[1] = pitches[0];
|
|
|
|
widths [1] = width2 * 2;
|
|
|
|
heights[1] = height2;
|
|
|
|
size2 = offsets[1] + height2 * pitches[1];
|
|
|
|
break;
|
|
|
|
case GST_VAAPI_IMAGE_YV12:
|
|
|
|
case GST_VAAPI_IMAGE_I420:
|
|
|
|
offsets[0] = 0;
|
|
|
|
pitches[0] = GST_ROUND_UP_4(width);
|
|
|
|
widths [0] = width;
|
|
|
|
heights[0] = height;
|
|
|
|
offsets[1] = offsets[0] + height * pitches[0];
|
|
|
|
pitches[1] = GST_ROUND_UP_4(GST_ROUND_UP_2(width) / 2);
|
|
|
|
widths [1] = width2;
|
|
|
|
heights[1] = height2;
|
|
|
|
offsets[2] = offsets[1] + height2 * pitches[1];
|
|
|
|
pitches[2] = pitches[1];
|
|
|
|
widths [2] = width2;
|
|
|
|
heights[2] = height2;
|
|
|
|
size2 = offsets[2] + height2 * pitches[2];
|
|
|
|
break;
|
|
|
|
case GST_VAAPI_IMAGE_ARGB:
|
|
|
|
case GST_VAAPI_IMAGE_RGBA:
|
|
|
|
case GST_VAAPI_IMAGE_ABGR:
|
|
|
|
case GST_VAAPI_IMAGE_BGRA:
|
|
|
|
offsets[0] = 0;
|
|
|
|
pitches[0] = width * 4;
|
|
|
|
widths [0] = width * 4;
|
|
|
|
heights[0] = height;
|
|
|
|
size2 = offsets[0] + height * pitches[0];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_error("could not compute row-stride for %" GST_FOURCC_FORMAT,
|
|
|
|
GST_FOURCC_ARGS(format));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (size2 != data_size)
|
|
|
|
g_error("data_size mismatch %d / %u", size2, data_size);
|
|
|
|
for (i = 0; i < priv->image.num_planes; i++) {
|
|
|
|
guchar *src = data + offsets[i];
|
|
|
|
guchar *dst = priv->image_data + priv->image.offsets[i];
|
|
|
|
for (j = 0; j < heights[i]; j++) {
|
|
|
|
memcpy(dst, src, widths[i]);
|
|
|
|
src += pitches[i];
|
|
|
|
dst += priv->image.pitches[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gst_vaapi_image_unmap(image))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|