Fix GstVaapiImage and GstVaapiSubpicture initialization.
[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     D(bug("gst_vaapi_image_constructed()\n"));
183
184     gst_vaapi_image_create(image);
185
186     parent_class = G_OBJECT_CLASS(gst_vaapi_image_parent_class);
187     if (parent_class->constructed)
188         parent_class->constructed(object);
189 }
190
191 static void
192 gst_vaapi_image_class_init(GstVaapiImageClass *klass)
193 {
194     GObjectClass * const object_class = G_OBJECT_CLASS(klass);
195
196     g_type_class_add_private(klass, sizeof(GstVaapiImagePrivate));
197
198     object_class->finalize     = gst_vaapi_image_finalize;
199     object_class->set_property = gst_vaapi_image_set_property;
200     object_class->get_property = gst_vaapi_image_get_property;
201     object_class->constructed  = gst_vaapi_image_constructed;
202
203     g_object_class_install_property
204         (object_class,
205          PROP_DISPLAY,
206          g_param_spec_object("display",
207                              "display",
208                              "GStreamer Va display",
209                              GST_VAAPI_TYPE_DISPLAY,
210                              G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
211
212     g_object_class_install_property
213         (object_class,
214          PROP_IMAGE_ID,
215          g_param_spec_uint("id",
216                            "VA image id",
217                            "VA image id",
218                            0, G_MAXUINT32, VA_INVALID_ID,
219                            G_PARAM_READABLE));
220
221     g_object_class_install_property
222         (object_class,
223          PROP_WIDTH,
224          g_param_spec_uint("width",
225                            "width",
226                            "Image width",
227                            0, G_MAXUINT32, 0,
228                            G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
229
230     g_object_class_install_property
231         (object_class,
232          PROP_HEIGHT,
233          g_param_spec_uint("height",
234                            "height",
235                            "Image height",
236                            0, G_MAXUINT32, 0,
237                            G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
238
239     g_object_class_install_property
240         (object_class,
241          PROP_FORMAT,
242          g_param_spec_uint("format",
243                            "format",
244                            "Image format",
245                            0, G_MAXUINT32, 0,
246                            G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
247 }
248
249 static void
250 gst_vaapi_image_init(GstVaapiImage *image)
251 {
252     GstVaapiImagePrivate *priv = GST_VAAPI_IMAGE_GET_PRIVATE(image);
253
254     D(bug("gst_vaapi_image_init()\n"));
255
256     image->priv          = priv;
257     priv->display        = NULL;
258     priv->image_data     = NULL;
259     priv->width          = 0;
260     priv->height         = 0;
261     priv->format         = 0;
262
263     memset(&priv->image, 0, sizeof(priv->image));
264     priv->image.image_id = VA_INVALID_ID;
265     priv->image.buf      = VA_INVALID_ID;
266 }
267
268 GstVaapiImage *
269 gst_vaapi_image_new(
270     GstVaapiDisplay    *display,
271     guint               width,
272     guint               height,
273     GstVaapiImageFormat format
274 )
275 {
276     D(bug("gst_vaapi_image_new(): size %ux%u, format 0x%x\n",
277           width, height, format));
278
279     return g_object_new(GST_VAAPI_TYPE_IMAGE,
280                         "display", display,
281                         "width",   width,
282                         "height",  height,
283                         "format",  format,
284                         NULL);
285 }
286
287 VAImageID
288 gst_vaapi_image_get_id(GstVaapiImage *image)
289 {
290     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), VA_INVALID_ID);
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
300     return g_object_ref(image->priv->display);
301 }
302
303 guint
304 gst_vaapi_image_get_width(GstVaapiImage *image)
305 {
306     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), 0);
307
308     return image->priv->width;
309 }
310
311 guint
312 gst_vaapi_image_get_height(GstVaapiImage *image)
313 {
314     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), 0);
315
316     return image->priv->height;
317 }
318
319 guint
320 gst_vaapi_image_get_format(GstVaapiImage *image)
321 {
322     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), 0);
323
324     return image->priv->format;
325 }
326
327 static inline gboolean
328 _gst_vaapi_image_is_mapped(GstVaapiImage *image)
329 {
330     return image->priv->image_data != NULL;
331 }
332
333 gboolean
334 gst_vaapi_image_is_mapped(GstVaapiImage *image)
335 {
336     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), FALSE);
337
338     return _gst_vaapi_image_is_mapped(image);
339 }
340
341 gboolean
342 gst_vaapi_image_map(GstVaapiImage *image)
343 {
344     void *image_data;
345     VAStatus status;
346
347     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), FALSE);
348
349     if (_gst_vaapi_image_is_mapped(image))
350         return TRUE;
351
352     status = vaMapBuffer(
353         gst_vaapi_display_get_display(image->priv->display),
354         image->priv->image.buf,
355         &image_data
356     );
357     if (!vaapi_check_status(status, "vaMapBuffer()"))
358         return FALSE;
359
360     image->priv->image_data = image_data;
361     return TRUE;
362 }
363
364 gboolean
365 gst_vaapi_image_unmap(GstVaapiImage *image)
366 {
367     VAStatus status;
368
369     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), FALSE);
370
371     if (!_gst_vaapi_image_is_mapped(image))
372         return FALSE;
373
374     status = vaUnmapBuffer(
375         gst_vaapi_display_get_display(image->priv->display),
376         image->priv->image.buf
377     );
378     if (!vaapi_check_status(status, "vaUnmapBuffer()"))
379         return FALSE;
380
381     image->priv->image_data = NULL;
382     return TRUE;
383 }
384
385 guint
386 gst_vaapi_image_get_plane_count(GstVaapiImage *image)
387 {
388     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), 0);
389     g_return_val_if_fail(_gst_vaapi_image_is_mapped(image), 0);
390
391     return image->priv->image.num_planes;
392 }
393
394 guchar *
395 gst_vaapi_image_get_plane(GstVaapiImage *image, guint plane)
396 {
397     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), NULL);
398     g_return_val_if_fail(_gst_vaapi_image_is_mapped(image), NULL);
399     g_return_val_if_fail(plane < image->priv->image.num_planes, NULL);
400
401     return image->priv->image_data + image->priv->image.offsets[plane];
402 }
403
404 guint
405 gst_vaapi_image_get_pitch(GstVaapiImage *image, guint plane)
406 {
407     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), 0);
408     g_return_val_if_fail(_gst_vaapi_image_is_mapped(image), 0);
409     g_return_val_if_fail(plane < image->priv->image.num_planes, 0);
410
411     return image->priv->image.pitches[plane];
412 }