New refcounting policy. All getters return a reference, not a copy. So the user shall...
[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     VAImage             image;
40     guchar             *image_data;
41     guint               width;
42     guint               height;
43     GstVaapiImageFormat format;
44 };
45
46 enum {
47     PROP_0,
48
49     PROP_DISPLAY,
50     PROP_IMAGE_ID,
51     PROP_WIDTH,
52     PROP_HEIGHT,
53     PROP_FORMAT
54 };
55
56 static void
57 gst_vaapi_image_destroy(GstVaapiImage *image)
58 {
59     GstVaapiImagePrivate * const priv = image->priv;
60     VADisplay dpy = gst_vaapi_display_get_display(priv->display);
61     VAStatus status;
62
63     gst_vaapi_image_unmap(image);
64
65     if (priv->image.image_id != VA_INVALID_ID) {
66         status = vaDestroyImage(dpy, priv->image.image_id);
67         if (!vaapi_check_status(status, "vaDestroyImage()"))
68             g_warning("failed to destroy image 0x%08x\n", priv->image.image_id);
69         priv->image.image_id = VA_INVALID_ID;
70     }
71
72     if (priv->display) {
73         g_object_unref(priv->display);
74         priv->display = NULL;
75     }
76 }
77
78 static gboolean
79 gst_vaapi_image_create(GstVaapiImage *image)
80 {
81     GstVaapiImagePrivate * const priv = image->priv;
82     const VAImageFormat *format;
83     VAStatus status;
84
85     if (!gst_vaapi_display_has_image_format(priv->display, priv->format))
86         return FALSE;
87
88     format = gst_vaapi_image_format_get_va_format(priv->format);
89
90     g_return_val_if_fail(format, FALSE);
91
92     status = vaCreateImage(
93         gst_vaapi_display_get_display(priv->display),
94         (VAImageFormat *)format,
95         priv->width,
96         priv->height,
97         &priv->image
98     );
99     if (!vaapi_check_status(status, "vaCreateImage()"))
100         return FALSE;
101
102     return TRUE;
103 }
104
105 static void
106 gst_vaapi_image_finalize(GObject *object)
107 {
108     gst_vaapi_image_destroy(GST_VAAPI_IMAGE(object));
109
110     G_OBJECT_CLASS(gst_vaapi_image_parent_class)->finalize(object);
111 }
112
113 static void
114 gst_vaapi_image_set_property(
115     GObject      *object,
116     guint         prop_id,
117     const GValue *value,
118     GParamSpec   *pspec
119 )
120 {
121     GstVaapiImage        * const image = GST_VAAPI_IMAGE(object);
122     GstVaapiImagePrivate * const priv  = image->priv;
123
124     switch (prop_id) {
125     case PROP_DISPLAY:
126         priv->display = g_object_ref(g_value_get_object(value));
127         break;
128     case PROP_WIDTH:
129         priv->width = g_value_get_uint(value);
130         break;
131     case PROP_HEIGHT:
132         priv->height = g_value_get_uint(value);
133         break;
134     case PROP_FORMAT:
135         priv->format = g_value_get_uint(value);
136         break;
137     default:
138         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
139         break;
140     }
141 }
142
143 static void
144 gst_vaapi_image_get_property(
145     GObject    *object,
146     guint       prop_id,
147     GValue     *value,
148     GParamSpec *pspec
149 )
150 {
151     GstVaapiImage * const image = GST_VAAPI_IMAGE(object);
152
153     switch (prop_id) {
154     case PROP_DISPLAY:
155         g_value_set_pointer(value, gst_vaapi_image_get_display(image));
156         break;
157     case PROP_IMAGE_ID:
158         g_value_set_uint(value, gst_vaapi_image_get_id(image));
159         break;
160     case PROP_WIDTH:
161         g_value_set_uint(value, gst_vaapi_image_get_width(image));
162         break;
163     case PROP_HEIGHT:
164         g_value_set_uint(value, gst_vaapi_image_get_height(image));
165         break;
166     case PROP_FORMAT:
167         g_value_set_uint(value, gst_vaapi_image_get_format(image));
168         break;
169     default:
170         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
171         break;
172     }
173 }
174
175 static void
176 gst_vaapi_image_constructed(GObject *object)
177 {
178     GstVaapiImage * const image = GST_VAAPI_IMAGE(object);
179     GObjectClass *parent_class;
180
181     gst_vaapi_image_create(image);
182
183     parent_class = G_OBJECT_CLASS(gst_vaapi_image_parent_class);
184     if (parent_class->constructed)
185         parent_class->constructed(object);
186 }
187
188 static void
189 gst_vaapi_image_class_init(GstVaapiImageClass *klass)
190 {
191     GObjectClass * const object_class = G_OBJECT_CLASS(klass);
192
193     g_type_class_add_private(klass, sizeof(GstVaapiImagePrivate));
194
195     object_class->finalize     = gst_vaapi_image_finalize;
196     object_class->set_property = gst_vaapi_image_set_property;
197     object_class->get_property = gst_vaapi_image_get_property;
198     object_class->constructed  = gst_vaapi_image_constructed;
199
200     g_object_class_install_property
201         (object_class,
202          PROP_DISPLAY,
203          g_param_spec_object("display",
204                              "display",
205                              "GStreamer Va display",
206                              GST_VAAPI_TYPE_DISPLAY,
207                              G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
208
209     g_object_class_install_property
210         (object_class,
211          PROP_IMAGE_ID,
212          g_param_spec_uint("id",
213                            "VA image id",
214                            "VA image id",
215                            0, G_MAXUINT32, VA_INVALID_ID,
216                            G_PARAM_READABLE));
217
218     g_object_class_install_property
219         (object_class,
220          PROP_WIDTH,
221          g_param_spec_uint("width",
222                            "width",
223                            "Image width",
224                            0, G_MAXUINT32, 0,
225                            G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
226
227     g_object_class_install_property
228         (object_class,
229          PROP_HEIGHT,
230          g_param_spec_uint("height",
231                            "height",
232                            "Image height",
233                            0, G_MAXUINT32, 0,
234                            G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
235
236     g_object_class_install_property
237         (object_class,
238          PROP_FORMAT,
239          g_param_spec_uint("format",
240                            "format",
241                            "Image format",
242                            0, G_MAXUINT32, 0,
243                            G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
244 }
245
246 static void
247 gst_vaapi_image_init(GstVaapiImage *image)
248 {
249     GstVaapiImagePrivate *priv = GST_VAAPI_IMAGE_GET_PRIVATE(image);
250
251     image->priv          = priv;
252     priv->display        = NULL;
253     priv->image_data     = NULL;
254     priv->width          = 0;
255     priv->height         = 0;
256     priv->format         = 0;
257
258     memset(&priv->image, 0, sizeof(priv->image));
259     priv->image.image_id = VA_INVALID_ID;
260     priv->image.buf      = VA_INVALID_ID;
261 }
262
263 GstVaapiImage *
264 gst_vaapi_image_new(
265     GstVaapiDisplay    *display,
266     guint               width,
267     guint               height,
268     GstVaapiImageFormat format
269 )
270 {
271     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
272     g_return_val_if_fail(width > 0, NULL);
273     g_return_val_if_fail(height > 0, NULL);
274
275     GST_DEBUG("size %ux%u, format 0x%x", width, height, format);
276
277     return g_object_new(GST_VAAPI_TYPE_IMAGE,
278                         "display", display,
279                         "width",   width,
280                         "height",  height,
281                         "format",  format,
282                         NULL);
283 }
284
285 VAImageID
286 gst_vaapi_image_get_id(GstVaapiImage *image)
287 {
288     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), VA_INVALID_ID);
289
290     return image->priv->image.image_id;
291 }
292
293 GstVaapiDisplay *
294 gst_vaapi_image_get_display(GstVaapiImage *image)
295 {
296     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), NULL);
297
298     return image->priv->display;
299 }
300
301 guint
302 gst_vaapi_image_get_width(GstVaapiImage *image)
303 {
304     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), 0);
305
306     return image->priv->width;
307 }
308
309 guint
310 gst_vaapi_image_get_height(GstVaapiImage *image)
311 {
312     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), 0);
313
314     return image->priv->height;
315 }
316
317 guint
318 gst_vaapi_image_get_format(GstVaapiImage *image)
319 {
320     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), 0);
321
322     return image->priv->format;
323 }
324
325 static inline gboolean
326 _gst_vaapi_image_is_mapped(GstVaapiImage *image)
327 {
328     return image->priv->image_data != NULL;
329 }
330
331 gboolean
332 gst_vaapi_image_is_mapped(GstVaapiImage *image)
333 {
334     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), FALSE);
335
336     return _gst_vaapi_image_is_mapped(image);
337 }
338
339 gboolean
340 gst_vaapi_image_map(GstVaapiImage *image)
341 {
342     void *image_data;
343     VAStatus status;
344
345     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), FALSE);
346
347     if (_gst_vaapi_image_is_mapped(image))
348         return TRUE;
349
350     status = vaMapBuffer(
351         gst_vaapi_display_get_display(image->priv->display),
352         image->priv->image.buf,
353         &image_data
354     );
355     if (!vaapi_check_status(status, "vaMapBuffer()"))
356         return FALSE;
357
358     image->priv->image_data = image_data;
359     return TRUE;
360 }
361
362 gboolean
363 gst_vaapi_image_unmap(GstVaapiImage *image)
364 {
365     VAStatus status;
366
367     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), FALSE);
368
369     if (!_gst_vaapi_image_is_mapped(image))
370         return FALSE;
371
372     status = vaUnmapBuffer(
373         gst_vaapi_display_get_display(image->priv->display),
374         image->priv->image.buf
375     );
376     if (!vaapi_check_status(status, "vaUnmapBuffer()"))
377         return FALSE;
378
379     image->priv->image_data = NULL;
380     return TRUE;
381 }
382
383 guint
384 gst_vaapi_image_get_plane_count(GstVaapiImage *image)
385 {
386     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), 0);
387     g_return_val_if_fail(_gst_vaapi_image_is_mapped(image), 0);
388
389     return image->priv->image.num_planes;
390 }
391
392 guchar *
393 gst_vaapi_image_get_plane(GstVaapiImage *image, guint plane)
394 {
395     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), NULL);
396     g_return_val_if_fail(_gst_vaapi_image_is_mapped(image), NULL);
397     g_return_val_if_fail(plane < image->priv->image.num_planes, NULL);
398
399     return image->priv->image_data + image->priv->image.offsets[plane];
400 }
401
402 guint
403 gst_vaapi_image_get_pitch(GstVaapiImage *image, guint plane)
404 {
405     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), 0);
406     g_return_val_if_fail(_gst_vaapi_image_is_mapped(image), 0);
407     g_return_val_if_fail(plane < image->priv->image.num_planes, 0);
408
409     return image->priv->image.pitches[plane];
410 }