Tizen 2.1 base
[framework/multimedia/gstreamer-vaapi.git] / gst-libs / gst / vaapi / gstvaapisurfacepool.c
1 /*
2  *  gstvaapisurfacepool.c - Gst VA surface pool
3  *
4  *  Copyright (C) 2010-2011 Splitted-Desktop Systems
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public License
8  *  as published by the Free Software Foundation; either version 2.1
9  *  of the License, or (at your option) any later version.
10  *
11  *  This library 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 GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free
18  *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  *  Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * SECTION:gstvaapisurfacepool
24  * @short_description: VA surface pool
25  */
26
27 #include "sysdeps.h"
28 #include "gstvaapisurfacepool.h"
29 #include "gstvaapisurface_userptr.h"
30
31 #define DEBUG 1
32 #include "gstvaapidebug.h"
33
34 G_DEFINE_TYPE(
35     GstVaapiSurfacePool,
36     gst_vaapi_surface_pool,
37     GST_VAAPI_TYPE_VIDEO_POOL);
38
39 #define GST_VAAPI_SURFACE_POOL_GET_PRIVATE(obj)                 \
40     (G_TYPE_INSTANCE_GET_PRIVATE((obj),                         \
41                                  GST_VAAPI_TYPE_SURFACE_POOL,   \
42                                  GstVaapiSurfacePoolPrivate))
43
44 struct _GstVaapiSurfacePoolPrivate {
45     GstVaapiChromaType  chroma_type;
46     guint               width;
47     guint               height;
48     gboolean            is_shared_buffer;
49 };
50
51 static void
52 gst_vaapi_surface_pool_set_caps(GstVaapiVideoPool *pool, GstCaps *caps)
53 {
54     GstVaapiSurfacePoolPrivate *priv = GST_VAAPI_SURFACE_POOL(pool)->priv;
55     GstStructure *structure;
56     gint width, height;
57
58     structure = gst_caps_get_structure(caps, 0);
59     gst_structure_get_int(structure, "width", &width);
60     gst_structure_get_int(structure, "height", &height);
61
62     if (gst_structure_has_name(structure, GST_VAAPI_BUFFER_SHARING_CAPS_NAME))
63         priv->is_shared_buffer = TRUE;
64     else
65         priv->is_shared_buffer = FALSE;
66
67     priv->chroma_type   = GST_VAAPI_CHROMA_TYPE_YUV420;
68     priv->width         = width;
69     priv->height        = height;
70 }
71
72 gpointer
73 gst_vaapi_surface_pool_alloc_object(
74     GstVaapiVideoPool *pool,
75     GstVaapiDisplay   *display
76 )
77 {
78     GstVaapiSurfacePoolPrivate *priv = GST_VAAPI_SURFACE_POOL(pool)->priv;
79
80     if (priv->is_shared_buffer)
81         return gst_vaapi_surface_userptr_new(display,
82                                              priv->chroma_type,
83                                              GST_VAAPI_IMAGE_NV12,
84                                              priv->width,
85                                              priv->height);
86     else
87         return gst_vaapi_surface_new(display,
88                                      priv->chroma_type,
89                                      priv->width,
90                                      priv->height);
91 }
92
93 static void
94 gst_vaapi_surface_pool_finalize(GObject *object)
95 {
96     G_OBJECT_CLASS(gst_vaapi_surface_pool_parent_class)->finalize(object);
97 }
98
99 static void
100 gst_vaapi_surface_pool_class_init(GstVaapiSurfacePoolClass *klass)
101 {
102     GObjectClass * const object_class = G_OBJECT_CLASS(klass);
103     GstVaapiVideoPoolClass * const pool_class = GST_VAAPI_VIDEO_POOL_CLASS(klass);
104
105     g_type_class_add_private(klass, sizeof(GstVaapiSurfacePoolPrivate));
106
107     object_class->finalize      = gst_vaapi_surface_pool_finalize;
108
109     pool_class->set_caps        = gst_vaapi_surface_pool_set_caps;
110     pool_class->alloc_object    = gst_vaapi_surface_pool_alloc_object;
111 }
112
113 static void
114 gst_vaapi_surface_pool_init(GstVaapiSurfacePool *pool)
115 {
116     GstVaapiSurfacePoolPrivate *priv = GST_VAAPI_SURFACE_POOL_GET_PRIVATE(pool);
117
118     pool->priv          = priv;
119     priv->chroma_type   = 0;
120     priv->width         = 0;
121     priv->height        = 0;
122     priv->is_shared_buffer = FALSE;
123 }
124
125 /**
126  * gst_vaapi_surface_pool_new:
127  * @display: a #GstVaapiDisplay
128  * @caps: a #GstCaps
129  *
130  * Creates a new #GstVaapiVideoPool of #GstVaapiSurface with the
131  * specified dimensions in @caps.
132  *
133  * Return value: the newly allocated #GstVaapiVideoPool
134  */
135 GstVaapiVideoPool *
136 gst_vaapi_surface_pool_new(GstVaapiDisplay *display, GstCaps *caps)
137 {
138     return g_object_new(GST_VAAPI_TYPE_SURFACE_POOL,
139                         "display", display,
140                         "caps",    caps,
141                         NULL);
142 }