Use GST_DEBUG.
[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_pointer(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     GstVaapiImagePrivate * const priv  = image->priv;
153
154     switch (prop_id) {
155     case PROP_DISPLAY:
156         g_value_set_pointer(value, g_object_ref(priv->display));
157         break;
158     case PROP_IMAGE_ID:
159         g_value_set_uint(value, gst_vaapi_image_get_id(image));
160         break;
161     case PROP_WIDTH:
162         g_value_set_uint(value, gst_vaapi_image_get_width(image));
163         break;
164     case PROP_HEIGHT:
165         g_value_set_uint(value, gst_vaapi_image_get_height(image));
166         break;
167     case PROP_FORMAT:
168         g_value_set_uint(value, gst_vaapi_image_get_format(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     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     guint               width,
268     guint               height,
269     GstVaapiImageFormat format
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                         "width",   width,
281                         "height",  height,
282                         "format",  format,
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
291     return image->priv->image.image_id;
292 }
293
294 GstVaapiDisplay *
295 gst_vaapi_image_get_display(GstVaapiImage *image)
296 {
297     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), NULL);
298
299     return g_object_ref(image->priv->display);
300 }
301
302 guint
303 gst_vaapi_image_get_width(GstVaapiImage *image)
304 {
305     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), 0);
306
307     return image->priv->width;
308 }
309
310 guint
311 gst_vaapi_image_get_height(GstVaapiImage *image)
312 {
313     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), 0);
314
315     return image->priv->height;
316 }
317
318 guint
319 gst_vaapi_image_get_format(GstVaapiImage *image)
320 {
321     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), 0);
322
323     return image->priv->format;
324 }
325
326 static inline gboolean
327 _gst_vaapi_image_is_mapped(GstVaapiImage *image)
328 {
329     return image->priv->image_data != NULL;
330 }
331
332 gboolean
333 gst_vaapi_image_is_mapped(GstVaapiImage *image)
334 {
335     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), FALSE);
336
337     return _gst_vaapi_image_is_mapped(image);
338 }
339
340 gboolean
341 gst_vaapi_image_map(GstVaapiImage *image)
342 {
343     void *image_data;
344     VAStatus status;
345
346     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), FALSE);
347
348     if (_gst_vaapi_image_is_mapped(image))
349         return TRUE;
350
351     status = vaMapBuffer(
352         gst_vaapi_display_get_display(image->priv->display),
353         image->priv->image.buf,
354         &image_data
355     );
356     if (!vaapi_check_status(status, "vaMapBuffer()"))
357         return FALSE;
358
359     image->priv->image_data = image_data;
360     return TRUE;
361 }
362
363 gboolean
364 gst_vaapi_image_unmap(GstVaapiImage *image)
365 {
366     VAStatus status;
367
368     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), FALSE);
369
370     if (!_gst_vaapi_image_is_mapped(image))
371         return FALSE;
372
373     status = vaUnmapBuffer(
374         gst_vaapi_display_get_display(image->priv->display),
375         image->priv->image.buf
376     );
377     if (!vaapi_check_status(status, "vaUnmapBuffer()"))
378         return FALSE;
379
380     image->priv->image_data = NULL;
381     return TRUE;
382 }
383
384 guint
385 gst_vaapi_image_get_plane_count(GstVaapiImage *image)
386 {
387     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), 0);
388     g_return_val_if_fail(_gst_vaapi_image_is_mapped(image), 0);
389
390     return image->priv->image.num_planes;
391 }
392
393 guchar *
394 gst_vaapi_image_get_plane(GstVaapiImage *image, guint plane)
395 {
396     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), NULL);
397     g_return_val_if_fail(_gst_vaapi_image_is_mapped(image), NULL);
398     g_return_val_if_fail(plane < image->priv->image.num_planes, NULL);
399
400     return image->priv->image_data + image->priv->image.offsets[plane];
401 }
402
403 guint
404 gst_vaapi_image_get_pitch(GstVaapiImage *image, guint plane)
405 {
406     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), 0);
407     g_return_val_if_fail(_gst_vaapi_image_is_mapped(image), 0);
408     g_return_val_if_fail(plane < image->priv->image.num_planes, 0);
409
410     return image->priv->image.pitches[plane];
411 }