Add basic GstVaapiVideoBuffer.
[vaapi:gstreamer-vaapi.git] / gst-libs / gst / vaapi / gstvaapivideobuffer.c
1 /*
2  *  gstvaapivideobuffer.c - Gst VA video buffer
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 "gstvaapivideobuffer.h"
23
24 #define DEBUG 1
25 #include "vaapi_debug.h"
26
27 G_DEFINE_TYPE(GstVaapiVideoBuffer, gst_vaapi_video_buffer, GST_TYPE_BUFFER);
28
29 #define GST_VAAPI_VIDEO_BUFFER_GET_PRIVATE(obj)                 \
30     (G_TYPE_INSTANCE_GET_PRIVATE((obj),                         \
31                                  GST_VAAPI_TYPE_VIDEO_BUFFER,   \
32                                  GstVaapiVideoBufferPrivate))
33
34 struct _GstVaapiVideoBufferPrivate {
35     GstVaapiVideoPool  *pool;
36     guint               pool_type;
37     GstVaapiImage      *image;
38     GstVaapiSurface    *surface;
39     guint               flags;
40 };
41
42 enum {
43     POOL_TYPE_NONE,
44     POOL_TYPE_IMAGE,
45     POOL_TYPE_SURFACE
46 };
47
48 static void
49 gst_vaapi_video_buffer_finalize(GstMiniObject *object)
50 {
51     GstVaapiVideoBufferPrivate *priv = GST_VAAPI_VIDEO_BUFFER(object)->priv;
52     GstMiniObjectClass *parent_class;
53
54     if (priv->image) {
55         if (priv->pool_type == POOL_TYPE_IMAGE)
56             gst_vaapi_video_pool_put_object(priv->pool, priv->image);
57         else
58             g_object_unref(priv->image);
59         priv->image = NULL;
60     }
61
62     if (priv->surface) {
63         if (priv->pool_type == POOL_TYPE_SURFACE)
64             gst_vaapi_video_pool_put_object(priv->pool, priv->surface);
65         else
66             g_object_unref(priv->surface);
67         priv->surface = NULL;
68     }
69
70     if (priv->pool) {
71         g_object_unref(priv->pool);
72         priv->pool = NULL;
73     }
74
75     parent_class = GST_MINI_OBJECT_CLASS(gst_vaapi_video_buffer_parent_class);
76     if (parent_class->finalize)
77         parent_class->finalize(object);
78 }
79
80 static void
81 gst_vaapi_video_buffer_class_init(GstVaapiVideoBufferClass *klass)
82 {
83     GstMiniObjectClass * const object_class = GST_MINI_OBJECT_CLASS(klass);
84
85     g_type_class_add_private(klass, sizeof(GstVaapiVideoBufferPrivate));
86
87     object_class->finalize = gst_vaapi_video_buffer_finalize;
88 }
89
90 static void
91 gst_vaapi_video_buffer_init(GstVaapiVideoBuffer *buffer)
92 {
93     GstVaapiVideoBufferPrivate *priv;
94
95     priv                = GST_VAAPI_VIDEO_BUFFER_GET_PRIVATE(buffer);
96     buffer->priv        = priv;
97     priv->pool          = NULL;
98     priv->image         = NULL;
99     priv->surface       = NULL;
100 }
101
102 static GstBuffer *
103 gst_vaapi_video_buffer_new(
104     GstVaapiVideoPool  *pool,
105     GstVaapiImage      *image,
106     GstVaapiSurface    *surface
107 )
108 {
109     gpointer vobject = NULL;
110     GstMiniObject *object;
111     GstVaapiVideoBuffer *buffer;
112     GstVaapiVideoBufferPrivate *priv;
113
114     object = gst_mini_object_new(GST_VAAPI_TYPE_VIDEO_BUFFER);
115     if (!object)
116         return NULL;
117
118     buffer              = GST_VAAPI_VIDEO_BUFFER(object);
119     priv                = buffer->priv;
120     priv->pool          = pool;
121     priv->pool_type     = POOL_TYPE_NONE;
122     priv->image         = image;
123     priv->surface       = surface;
124
125     if (pool) {
126         vobject = gst_vaapi_video_pool_get_object(pool);
127         if (!vobject)
128             goto error;
129
130         if (GST_VAAPI_IS_IMAGE(vobject)) {
131             priv->pool_type = POOL_TYPE_IMAGE;
132             priv->image     = vobject;
133         }
134         else if (GST_VAAPI_IS_SURFACE(vobject)) {
135             priv->pool_type = POOL_TYPE_SURFACE;
136             priv->surface   = vobject;
137         }
138         else
139             goto error;
140     }
141     return GST_BUFFER(buffer);
142
143 error:
144     if (vobject)
145         gst_vaapi_video_pool_put_object(pool, vobject);
146     gst_mini_object_unref(object);
147     return NULL;
148 }
149
150 GstBuffer *
151 gst_vaapi_video_buffer_new_from_pool(GstVaapiVideoPool *pool)
152 {
153     g_return_val_if_fail(GST_VAAPI_IS_VIDEO_POOL(pool), NULL);
154
155     return gst_vaapi_video_buffer_new(g_object_ref(pool), NULL, NULL);
156 }
157
158 GstBuffer *
159 gst_vaapi_video_buffer_new_with_image(GstVaapiImage *image)
160 {
161     g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), NULL);
162
163     return gst_vaapi_video_buffer_new(NULL, g_object_ref(image), NULL);
164 }
165
166 GstBuffer *
167 gst_vaapi_video_buffer_new_with_surface(GstVaapiSurface *surface)
168 {
169     g_return_val_if_fail(GST_VAAPI_IS_SURFACE(surface), NULL);
170
171     return gst_vaapi_video_buffer_new(NULL, NULL, g_object_ref(surface));
172 }
173
174 GstVaapiImage *
175 gst_vaapi_video_buffer_get_image(GstVaapiVideoBuffer *buffer)
176 {
177     g_return_val_if_fail(GST_VAAPI_IS_VIDEO_BUFFER(buffer), NULL);
178
179     return buffer->priv->image;
180 }
181
182 GstVaapiSurface *
183 gst_vaapi_video_buffer_get_surface(GstVaapiVideoBuffer *buffer)
184 {
185     g_return_val_if_fail(GST_VAAPI_IS_VIDEO_BUFFER(buffer), NULL);
186
187     return buffer->priv->surface;
188 }