2 * gstvaapiimage.c - VA image abstraction
4 * gstreamer-vaapi (C) 2010 Splitted-Desktop Systems
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "vaapi_utils.h"
24 #include "gstvaapiimage.h"
25 #include <va/va_backend.h>
28 #include "vaapi_debug.h"
30 G_DEFINE_TYPE(GstVaapiImage, gst_vaapi_image, G_TYPE_OBJECT);
32 #define GST_VAAPI_IMAGE_GET_PRIVATE(obj) \
33 (G_TYPE_INSTANCE_GET_PRIVATE((obj), \
34 GST_VAAPI_TYPE_IMAGE, \
35 GstVaapiImagePrivate))
37 struct _GstVaapiImagePrivate {
38 GstVaapiDisplay *display;
39 gboolean is_constructed;
42 GstVaapiImageFormat format;
58 gst_vaapi_image_destroy(GstVaapiImage *image)
60 GstVaapiImagePrivate * const priv = image->priv;
61 VADisplay dpy = gst_vaapi_display_get_display(priv->display);
64 gst_vaapi_image_unmap(image);
66 if (priv->image.image_id != VA_INVALID_ID) {
67 status = vaDestroyImage(dpy, priv->image.image_id);
68 if (!vaapi_check_status(status, "vaDestroyImage()"))
69 g_warning("failed to destroy image 0x%08x\n", priv->image.image_id);
70 priv->image.image_id = VA_INVALID_ID;
74 g_object_unref(priv->display);
80 gst_vaapi_image_create(GstVaapiImage *image)
82 GstVaapiImagePrivate * const priv = image->priv;
83 const VAImageFormat *format;
86 if (!gst_vaapi_display_has_image_format(priv->display, priv->format))
89 format = gst_vaapi_image_format_get_va_format(priv->format);
91 g_return_val_if_fail(format, FALSE);
93 status = vaCreateImage(
94 gst_vaapi_display_get_display(priv->display),
95 (VAImageFormat *)format,
100 if (!vaapi_check_status(status, "vaCreateImage()"))
107 gst_vaapi_image_finalize(GObject *object)
109 gst_vaapi_image_destroy(GST_VAAPI_IMAGE(object));
111 G_OBJECT_CLASS(gst_vaapi_image_parent_class)->finalize(object);
115 gst_vaapi_image_set_property(
122 GstVaapiImage * const image = GST_VAAPI_IMAGE(object);
123 GstVaapiImagePrivate * const priv = image->priv;
127 priv->display = g_object_ref(g_value_get_object(value));
130 priv->format = g_value_get_uint(value);
133 priv->width = g_value_get_uint(value);
136 priv->height = g_value_get_uint(value);
139 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
145 gst_vaapi_image_get_property(
152 GstVaapiImage * const image = GST_VAAPI_IMAGE(object);
156 g_value_set_pointer(value, gst_vaapi_image_get_display(image));
159 g_value_set_uint(value, gst_vaapi_image_get_id(image));
162 g_value_set_uint(value, gst_vaapi_image_get_format(image));
165 g_value_set_uint(value, gst_vaapi_image_get_width(image));
168 g_value_set_uint(value, gst_vaapi_image_get_height(image));
171 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
177 gst_vaapi_image_constructed(GObject *object)
179 GstVaapiImage * const image = GST_VAAPI_IMAGE(object);
180 GObjectClass *parent_class;
182 image->priv->is_constructed = gst_vaapi_image_create(image);
184 parent_class = G_OBJECT_CLASS(gst_vaapi_image_parent_class);
185 if (parent_class->constructed)
186 parent_class->constructed(object);
190 gst_vaapi_image_class_init(GstVaapiImageClass *klass)
192 GObjectClass * const object_class = G_OBJECT_CLASS(klass);
194 g_type_class_add_private(klass, sizeof(GstVaapiImagePrivate));
196 object_class->finalize = gst_vaapi_image_finalize;
197 object_class->set_property = gst_vaapi_image_set_property;
198 object_class->get_property = gst_vaapi_image_get_property;
199 object_class->constructed = gst_vaapi_image_constructed;
201 g_object_class_install_property
204 g_param_spec_object("display",
206 "GStreamer Va display",
207 GST_VAAPI_TYPE_DISPLAY,
208 G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
210 g_object_class_install_property
213 g_param_spec_uint("id",
216 0, G_MAXUINT32, VA_INVALID_ID,
219 g_object_class_install_property
222 g_param_spec_uint("width",
226 G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
228 g_object_class_install_property
231 g_param_spec_uint("height",
235 G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
237 g_object_class_install_property
240 g_param_spec_uint("format",
244 G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
248 gst_vaapi_image_init(GstVaapiImage *image)
250 GstVaapiImagePrivate *priv = GST_VAAPI_IMAGE_GET_PRIVATE(image);
253 priv->display = NULL;
254 priv->image_data = NULL;
259 memset(&priv->image, 0, sizeof(priv->image));
260 priv->image.image_id = VA_INVALID_ID;
261 priv->image.buf = VA_INVALID_ID;
266 GstVaapiDisplay *display,
267 GstVaapiImageFormat format,
272 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
273 g_return_val_if_fail(width > 0, NULL);
274 g_return_val_if_fail(height > 0, NULL);
276 GST_DEBUG("size %ux%u, format 0x%x", width, height, format);
278 return g_object_new(GST_VAAPI_TYPE_IMAGE,
287 gst_vaapi_image_get_id(GstVaapiImage *image)
289 g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), VA_INVALID_ID);
290 g_return_val_if_fail(image->priv->is_constructed, FALSE);
292 return image->priv->image.image_id;
296 gst_vaapi_image_get_display(GstVaapiImage *image)
298 g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), NULL);
299 g_return_val_if_fail(image->priv->is_constructed, FALSE);
301 return image->priv->display;
305 gst_vaapi_image_get_width(GstVaapiImage *image)
307 g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), 0);
308 g_return_val_if_fail(image->priv->is_constructed, FALSE);
310 return image->priv->width;
314 gst_vaapi_image_get_height(GstVaapiImage *image)
316 g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), 0);
317 g_return_val_if_fail(image->priv->is_constructed, FALSE);
319 return image->priv->height;
323 gst_vaapi_image_get_format(GstVaapiImage *image)
325 g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), 0);
326 g_return_val_if_fail(image->priv->is_constructed, FALSE);
328 return image->priv->format;
331 static inline gboolean
332 _gst_vaapi_image_is_mapped(GstVaapiImage *image)
334 return image->priv->image_data != NULL;
338 gst_vaapi_image_is_mapped(GstVaapiImage *image)
340 g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), FALSE);
341 g_return_val_if_fail(image->priv->is_constructed, FALSE);
343 return _gst_vaapi_image_is_mapped(image);
347 gst_vaapi_image_map(GstVaapiImage *image)
352 g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), FALSE);
353 g_return_val_if_fail(image->priv->is_constructed, FALSE);
355 if (_gst_vaapi_image_is_mapped(image))
358 status = vaMapBuffer(
359 gst_vaapi_display_get_display(image->priv->display),
360 image->priv->image.buf,
363 if (!vaapi_check_status(status, "vaMapBuffer()"))
366 image->priv->image_data = image_data;
371 gst_vaapi_image_unmap(GstVaapiImage *image)
375 g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), FALSE);
376 g_return_val_if_fail(image->priv->is_constructed, FALSE);
378 if (!_gst_vaapi_image_is_mapped(image))
381 status = vaUnmapBuffer(
382 gst_vaapi_display_get_display(image->priv->display),
383 image->priv->image.buf
385 if (!vaapi_check_status(status, "vaUnmapBuffer()"))
388 image->priv->image_data = NULL;
393 gst_vaapi_image_get_plane_count(GstVaapiImage *image)
395 g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), 0);
396 g_return_val_if_fail(image->priv->is_constructed, FALSE);
397 g_return_val_if_fail(_gst_vaapi_image_is_mapped(image), 0);
399 return image->priv->image.num_planes;
403 gst_vaapi_image_get_plane(GstVaapiImage *image, guint plane)
405 g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), NULL);
406 g_return_val_if_fail(image->priv->is_constructed, FALSE);
407 g_return_val_if_fail(_gst_vaapi_image_is_mapped(image), NULL);
408 g_return_val_if_fail(plane < image->priv->image.num_planes, NULL);
410 return image->priv->image_data + image->priv->image.offsets[plane];
414 gst_vaapi_image_get_pitch(GstVaapiImage *image, guint plane)
416 g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), 0);
417 g_return_val_if_fail(image->priv->is_constructed, FALSE);
418 g_return_val_if_fail(_gst_vaapi_image_is_mapped(image), 0);
419 g_return_val_if_fail(plane < image->priv->image.num_planes, 0);
421 return image->priv->image.pitches[plane];
425 gst_vaapi_image_update_from_buffer(GstVaapiImage *image, GstBuffer *buffer)
427 GstVaapiImagePrivate *priv;
428 GstStructure *structure;
430 GstVaapiImageFormat format;
432 guint offsets[3], pitches[3], widths[3], heights[3];
437 g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), FALSE);
438 g_return_val_if_fail(image->priv->is_constructed, FALSE);
439 g_return_val_if_fail(GST_IS_BUFFER(buffer), FALSE);
442 data = GST_BUFFER_DATA(buffer);
443 data_size = GST_BUFFER_SIZE(buffer);
444 caps = GST_BUFFER_CAPS(buffer);
449 format = gst_vaapi_image_format_from_caps(caps);
450 if (format != priv->format)
453 structure = gst_caps_get_structure(caps, 0);
454 gst_structure_get_int(structure, "width", &width);
455 gst_structure_get_int(structure, "height", &height);
456 if (width != priv->width || height != priv->height)
459 if (!gst_vaapi_image_map(image))
462 if (data_size == priv->image.data_size)
463 memcpy(priv->image_data, data, data_size);
465 /* XXX: copied from gst_video_format_get_row_stride() -- no NV12? */
466 const guint width2 = (width + 1) / 2;
467 const guint height2 = (height + 1) / 2;
470 case GST_VAAPI_IMAGE_NV12:
472 pitches[0] = GST_ROUND_UP_4(width);
475 offsets[1] = offsets[0] + height * pitches[0];
476 pitches[1] = pitches[0];
477 widths [1] = width2 * 2;
478 heights[1] = height2;
479 size2 = offsets[1] + height2 * pitches[1];
481 case GST_VAAPI_IMAGE_YV12:
482 case GST_VAAPI_IMAGE_I420:
484 pitches[0] = GST_ROUND_UP_4(width);
487 offsets[1] = offsets[0] + height * pitches[0];
488 pitches[1] = GST_ROUND_UP_4(GST_ROUND_UP_2(width) / 2);
490 heights[1] = height2;
491 offsets[2] = offsets[1] + height2 * pitches[1];
492 pitches[2] = pitches[1];
494 heights[2] = height2;
495 size2 = offsets[2] + height2 * pitches[2];
497 case GST_VAAPI_IMAGE_ARGB:
498 case GST_VAAPI_IMAGE_RGBA:
499 case GST_VAAPI_IMAGE_ABGR:
500 case GST_VAAPI_IMAGE_BGRA:
502 pitches[0] = width * 4;
503 widths [0] = width * 4;
505 size2 = offsets[0] + height * pitches[0];
508 g_error("could not compute row-stride for %" GST_FOURCC_FORMAT,
509 GST_FOURCC_ARGS(format));
512 if (size2 != data_size)
513 g_error("data_size mismatch %d / %u", size2, data_size);
514 for (i = 0; i < priv->image.num_planes; i++) {
515 guchar *src = data + offsets[i];
516 guchar *dst = priv->image_data + priv->image.offsets[i];
517 for (j = 0; j < heights[i]; j++) {
518 memcpy(dst, src, widths[i]);
520 dst += priv->image.pitches[i];
525 if (!gst_vaapi_image_unmap(image))