Add gst_vaapi_image_update_from_buffer() helper.
[profile/ivi/gstreamer-vaapi.git] / gst-libs / gst / vaapi / gstvaapiimage.c
1 /*
2  *  gstvaapiimage.c - VA image abstraction
3  *
4  *  gstreamer-vaapi (C) 2010 Splitted-Desktop Systems
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include <string.h>
23 #include "vaapi_utils.h"
24 #include "gstvaapiimage.h"
25 #include <va/va_backend.h>
26
27 #define DEBUG 1
28 #include "vaapi_debug.h"
29
30 G_DEFINE_TYPE(GstVaapiImage, gst_vaapi_image, G_TYPE_OBJECT);
31
32 #define GST_VAAPI_IMAGE_GET_PRIVATE(obj)                \
33     (G_TYPE_INSTANCE_GET_PRIVATE((obj),                 \
34                                  GST_VAAPI_TYPE_IMAGE,  \
35                                  GstVaapiImagePrivate))
36
37 struct _GstVaapiImagePrivate {
38     GstVaapiDisplay    *display;
39     gboolean            is_constructed;
40     VAImage             image;
41     guchar             *image_data;
42     GstVaapiImageFormat format;
43     guint               width;
44     guint               height;
45 };
46
47 enum {
48     PROP_0,
49
50     PROP_DISPLAY,
51     PROP_IMAGE_ID,
52     PROP_FORMAT,
53     PROP_WIDTH,
54     PROP_HEIGHT
55 };
56
57 static void
58 gst_vaapi_image_destroy(GstVaapiImage *image)
59 {
60     GstVaapiImagePrivate * const priv = image->priv;
61     VADisplay dpy = gst_vaapi_display_get_display(priv->display);
62     VAStatus status;
63
64     gst_vaapi_image_unmap(image);
65
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;
71     }
72
73     if (priv->display) {
74         g_object_unref(priv->display);
75         priv->display = NULL;
76     }
77 }
78
79 static gboolean
80 gst_vaapi_image_create(GstVaapiImage *image)
81 {
82     GstVaapiImagePrivate * const priv = image->priv;
83     const VAImageFormat *format;
84     VAStatus status;
85
86     if (!gst_vaapi_display_has_image_format(priv->display, priv->format))
87         return FALSE;
88
89     format = gst_vaapi_image_format_get_va_format(priv->format);
90
91     g_return_val_if_fail(format, FALSE);
92
93     status = vaCreateImage(
94         gst_vaapi_display_get_display(priv->display),
95         (VAImageFormat *)format,
96         priv->width,
97         priv->height,
98         &priv->image
99     );
100     if (!vaapi_check_status(status, "vaCreateImage()"))
101         return FALSE;
102
103     return TRUE;
104 }
105
106 static void
107 gst_vaapi_image_finalize(GObject *object)
108 {
109     gst_vaapi_image_destroy(GST_VAAPI_IMAGE(object));
110
111     G_OBJECT_CLASS(gst_vaapi_image_parent_class)->finalize(object);
112 }
113
114 static void
115 gst_vaapi_image_set_property(
116     GObject      *object,
117     guint         prop_id,
118     const GValue *value,
119     GParamSpec   *pspec
120 )
121 {
122     GstVaapiImage        * const image = GST_VAAPI_IMAGE(object);
123     GstVaapiImagePrivate * const priv  = image->priv;
124
125     switch (prop_id) {
126     case PROP_DISPLAY:
127         priv->display = g_object_ref(g_value_get_object(value));
128         break;
129     case PROP_FORMAT:
130         priv->format = g_value_get_uint(value);
131         break;
132     case PROP_WIDTH:
133         priv->width = g_value_get_uint(value);
134         break;
135     case PROP_HEIGHT:
136         priv->height = g_value_get_uint(value);
137         break;
138     default:
139         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
140         break;
141     }
142 }
143
144 static void
145 gst_vaapi_image_get_property(
146     GObject    *object,
147     guint       prop_id,
148     GValue     *value,
149     GParamSpec *pspec
150 )
151 {
152     GstVaapiImage * const image = GST_VAAPI_IMAGE(object);
153
154     switch (prop_id) {
155     case PROP_DISPLAY:
156         g_value_set_pointer(value, gst_vaapi_image_get_display(image));
157         break;
158     case PROP_IMAGE_ID:
159         g_value_set_uint(value, gst_vaapi_image_get_id(image));
160         break;
161     case PROP_FORMAT:
162         g_value_set_uint(value, gst_vaapi_image_get_format(image));
163         break;
164     case PROP_WIDTH:
165         g_value_set_uint(value, gst_vaapi_image_get_width(image));
166         break;
167     case PROP_HEIGHT:
168         g_value_set_uint(value, gst_vaapi_image_get_height(image));
169         break;
170     default:
171         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
172         break;
173     }
174 }
175
176 static void
177 gst_vaapi_image_constructed(GObject *object)
178 {
179     GstVaapiImage * const image = GST_VAAPI_IMAGE(object);
180     GObjectClass *parent_class;
181
182     image->priv->is_constructed = gst_vaapi_image_create(image);
183
184     parent_class = G_OBJECT_CLASS(gst_vaapi_image_parent_class);
185     if (parent_class->constructed)
186         parent_class->constructed(object);
187 }
188
189 static void
190 gst_vaapi_image_class_init(GstVaapiImageClass *klass)
191 {
192     GObjectClass * const object_class = G_OBJECT_CLASS(klass);
193
194     g_type_class_add_private(klass, sizeof(GstVaapiImagePrivate));
195
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;
200
201     g_object_class_install_property
202         (object_class,
203          PROP_DISPLAY,
204          g_param_spec_object("display",
205                              "display",
206                              "GStreamer Va display",
207                              GST_VAAPI_TYPE_DISPLAY,
208                              G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
209
210     g_object_class_install_property
211         (object_class,
212          PROP_IMAGE_ID,
213          g_param_spec_uint("id",
214                            "VA image id",
215                            "VA image id",
216                            0, G_MAXUINT32, VA_INVALID_ID,
217                            G_PARAM_READABLE));
218
219     g_object_class_install_property
220         (object_class,
221          PROP_WIDTH,
222          g_param_spec_uint("width",
223                            "width",
224                            "Image width",
225                            0, G_MAXUINT32, 0,
226                            G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
227
228     g_object_class_install_property
229         (object_class,
230          PROP_HEIGHT,
231          g_param_spec_uint("height",
232                            "height",
233                            "Image height",
234                            0, G_MAXUINT32, 0,
235                            G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
236
237     g_object_class_install_property
238         (object_class,
239          PROP_FORMAT,
240          g_param_spec_uint("format",
241                            "format",
242                            "Image format",
243                            0, G_MAXUINT32, 0,
244                            G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
245 }
246
247 static void
248 gst_vaapi_image_init(GstVaapiImage *image)
249 {
250     GstVaapiImagePrivate *priv = GST_VAAPI_IMAGE_GET_PRIVATE(image);
251
252     image->priv          = priv;
253     priv->display        = NULL;
254     priv->image_data     = NULL;
255     priv->width          = 0;
256     priv->height         = 0;
257     priv->format         = 0;
258
259     memset(&priv->image, 0, sizeof(priv->image));
260     priv->image.image_id = VA_INVALID_ID;
261     priv->image.buf      = VA_INVALID_ID;
262 }
263
264 GstVaapiImage *
265 gst_vaapi_image_new(
266     GstVaapiDisplay    *display,
267     GstVaapiImageFormat format,
268     guint               width,
269     guint               height
270 )
271 {
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);
275
276     GST_DEBUG("size %ux%u, format 0x%x", width, height, format);
277
278     return g_object_new(GST_VAAPI_TYPE_IMAGE,
279                         "display", display,
280                         "format",  format,
281                         "width",   width,
282                         "height",  height,
283                         NULL);
284 }
285
286 VAImageID
287 gst_vaapi_image_get_id(GstVaapiImage *image)
288 {
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);
291
292     return image->priv->image.image_id;
293 }
294
295 GstVaapiDisplay *
296 gst_vaapi_image_get_display(GstVaapiImage *image)
297 {
298     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), NULL);
299     g_return_val_if_fail(image->priv->is_constructed, FALSE);
300
301     return image->priv->display;
302 }
303
304 guint
305 gst_vaapi_image_get_width(GstVaapiImage *image)
306 {
307     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), 0);
308     g_return_val_if_fail(image->priv->is_constructed, FALSE);
309
310     return image->priv->width;
311 }
312
313 guint
314 gst_vaapi_image_get_height(GstVaapiImage *image)
315 {
316     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), 0);
317     g_return_val_if_fail(image->priv->is_constructed, FALSE);
318
319     return image->priv->height;
320 }
321
322 guint
323 gst_vaapi_image_get_format(GstVaapiImage *image)
324 {
325     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), 0);
326     g_return_val_if_fail(image->priv->is_constructed, FALSE);
327
328     return image->priv->format;
329 }
330
331 static inline gboolean
332 _gst_vaapi_image_is_mapped(GstVaapiImage *image)
333 {
334     return image->priv->image_data != NULL;
335 }
336
337 gboolean
338 gst_vaapi_image_is_mapped(GstVaapiImage *image)
339 {
340     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), FALSE);
341     g_return_val_if_fail(image->priv->is_constructed, FALSE);
342
343     return _gst_vaapi_image_is_mapped(image);
344 }
345
346 gboolean
347 gst_vaapi_image_map(GstVaapiImage *image)
348 {
349     void *image_data;
350     VAStatus status;
351
352     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), FALSE);
353     g_return_val_if_fail(image->priv->is_constructed, FALSE);
354
355     if (_gst_vaapi_image_is_mapped(image))
356         return TRUE;
357
358     status = vaMapBuffer(
359         gst_vaapi_display_get_display(image->priv->display),
360         image->priv->image.buf,
361         &image_data
362     );
363     if (!vaapi_check_status(status, "vaMapBuffer()"))
364         return FALSE;
365
366     image->priv->image_data = image_data;
367     return TRUE;
368 }
369
370 gboolean
371 gst_vaapi_image_unmap(GstVaapiImage *image)
372 {
373     VAStatus status;
374
375     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), FALSE);
376     g_return_val_if_fail(image->priv->is_constructed, FALSE);
377
378     if (!_gst_vaapi_image_is_mapped(image))
379         return FALSE;
380
381     status = vaUnmapBuffer(
382         gst_vaapi_display_get_display(image->priv->display),
383         image->priv->image.buf
384     );
385     if (!vaapi_check_status(status, "vaUnmapBuffer()"))
386         return FALSE;
387
388     image->priv->image_data = NULL;
389     return TRUE;
390 }
391
392 guint
393 gst_vaapi_image_get_plane_count(GstVaapiImage *image)
394 {
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);
398
399     return image->priv->image.num_planes;
400 }
401
402 guchar *
403 gst_vaapi_image_get_plane(GstVaapiImage *image, guint plane)
404 {
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);
409
410     return image->priv->image_data + image->priv->image.offsets[plane];
411 }
412
413 guint
414 gst_vaapi_image_get_pitch(GstVaapiImage *image, guint plane)
415 {
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);
420
421     return image->priv->image.pitches[plane];
422 }
423
424 gboolean
425 gst_vaapi_image_update_from_buffer(GstVaapiImage *image, GstBuffer *buffer)
426 {
427     GstVaapiImagePrivate *priv;
428     GstStructure *structure;
429     GstCaps *caps;
430     GstVaapiImageFormat format;
431     gint width, height;
432     guint offsets[3], pitches[3], widths[3], heights[3];
433     guint i, j;
434     guchar *data;
435     guint32 data_size;
436
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);
440
441     priv      = image->priv;
442     data      = GST_BUFFER_DATA(buffer);
443     data_size = GST_BUFFER_SIZE(buffer);
444     caps      = GST_BUFFER_CAPS(buffer);
445
446     if (!caps)
447         return FALSE;
448
449     format = gst_vaapi_image_format_from_caps(caps);
450     if (format != priv->format)
451         return FALSE;
452
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)
457         return FALSE;
458
459     if (!gst_vaapi_image_map(image))
460         return FALSE;
461
462     if (data_size == priv->image.data_size)
463         memcpy(priv->image_data, data, data_size);
464     else {
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;
468         guint size2;
469         switch (format) {
470         case GST_VAAPI_IMAGE_NV12:
471             offsets[0] = 0;
472             pitches[0] = GST_ROUND_UP_4(width);
473             widths [0] = width;
474             heights[0] = height;
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];
480             break;
481         case GST_VAAPI_IMAGE_YV12:
482         case GST_VAAPI_IMAGE_I420:
483             offsets[0] = 0;
484             pitches[0] = GST_ROUND_UP_4(width);
485             widths [0] = width;
486             heights[0] = height;
487             offsets[1] = offsets[0] + height * pitches[0];
488             pitches[1] = GST_ROUND_UP_4(GST_ROUND_UP_2(width) / 2);
489             widths [1] = width2;
490             heights[1] = height2;
491             offsets[2] = offsets[1] + height2 * pitches[1];
492             pitches[2] = pitches[1];
493             widths [2] = width2;
494             heights[2] = height2;
495             size2      = offsets[2] + height2 * pitches[2];
496             break;
497         case GST_VAAPI_IMAGE_ARGB:
498         case GST_VAAPI_IMAGE_RGBA:
499         case GST_VAAPI_IMAGE_ABGR:
500         case GST_VAAPI_IMAGE_BGRA:
501             offsets[0] = 0;
502             pitches[0] = width * 4;
503             widths [0] = width * 4;
504             heights[0] = height;
505             size2      = offsets[0] + height * pitches[0];
506             break;
507         default:
508             g_error("could not compute row-stride for %" GST_FOURCC_FORMAT,
509                     GST_FOURCC_ARGS(format));
510             break;
511         }
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]);
519                 src += pitches[i];
520                 dst += priv->image.pitches[i];
521             }
522         }
523     }
524
525     if (!gst_vaapi_image_unmap(image))
526         return FALSE;
527
528     return TRUE;
529 }