xvimagesink: use xvcontext for allocation
[platform/upstream/gstreamer.git] / sys / xvimage / xvimagepool.c
1 /* GStreamer
2  * Copyright (C) <2005> Julien Moutte <julien@moutte.net>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 /* Object header */
25 #include "xvimagepool.h"
26 #include "xvimageallocator.h"
27
28 /* Debugging category */
29 #include <gst/gstinfo.h>
30
31 /* Helper functions */
32 #include <gst/video/video.h>
33 #include <gst/video/gstvideometa.h>
34 #include <gst/video/gstvideopool.h>
35
36
37 GST_DEBUG_CATEGORY_EXTERN (gst_debug_xvimagepool);
38 #define GST_CAT_DEFAULT gst_debug_xvimagepool
39
40
41 struct _GstXvImageBufferPoolPrivate
42 {
43   GstXvImageAllocator *allocator;
44
45   GstCaps *caps;
46   gint im_format;
47   GstVideoRectangle crop;
48   GstVideoInfo info;
49   GstVideoAlignment align;
50   guint padded_width;
51   guint padded_height;
52   gboolean add_metavideo;
53   gboolean need_alignment;
54 };
55
56 /* bufferpool */
57 static void gst_xvimage_buffer_pool_finalize (GObject * object);
58
59 #define GST_XVIMAGE_BUFFER_POOL_GET_PRIVATE(obj)  \
60    (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_XVIMAGE_BUFFER_POOL, GstXvImageBufferPoolPrivate))
61
62 #define gst_xvimage_buffer_pool_parent_class parent_class
63 G_DEFINE_TYPE (GstXvImageBufferPool, gst_xvimage_buffer_pool,
64     GST_TYPE_BUFFER_POOL);
65
66 static const gchar **
67 xvimage_buffer_pool_get_options (GstBufferPool * pool)
68 {
69   static const gchar *options[] = { GST_BUFFER_POOL_OPTION_VIDEO_META,
70     GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT, NULL
71   };
72
73   return options;
74 }
75
76 static gboolean
77 xvimage_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
78 {
79   GstXvImageBufferPool *xvpool = GST_XVIMAGE_BUFFER_POOL_CAST (pool);
80   GstXvImageBufferPoolPrivate *priv = xvpool->priv;
81   GstVideoInfo info;
82   GstCaps *caps;
83   GstXvContext *context;
84
85   if (!gst_buffer_pool_config_get_params (config, &caps, NULL, NULL, NULL))
86     goto wrong_config;
87
88   if (caps == NULL)
89     goto no_caps;
90
91   /* now parse the caps from the config */
92   if (!gst_video_info_from_caps (&info, caps))
93     goto wrong_caps;
94
95   GST_LOG_OBJECT (pool, "%dx%d, caps %" GST_PTR_FORMAT, info.width, info.height,
96       caps);
97
98   context = gst_xvimage_allocator_peek_context (priv->allocator);
99
100   priv->im_format = gst_xvcontext_get_format_from_info (context, &info);
101   if (priv->im_format == -1)
102     goto unknown_format;
103
104   if (priv->caps)
105     gst_caps_unref (priv->caps);
106   priv->caps = gst_caps_ref (caps);
107
108   /* enable metadata based on config of the pool */
109   priv->add_metavideo =
110       gst_buffer_pool_config_has_option (config,
111       GST_BUFFER_POOL_OPTION_VIDEO_META);
112
113   /* parse extra alignment info */
114   priv->need_alignment = gst_buffer_pool_config_has_option (config,
115       GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
116
117   if (priv->need_alignment) {
118     gst_buffer_pool_config_get_video_alignment (config, &priv->align);
119
120     GST_LOG_OBJECT (pool, "padding %u-%ux%u-%u", priv->align.padding_top,
121         priv->align.padding_left, priv->align.padding_left,
122         priv->align.padding_bottom);
123
124     /* do padding and alignment */
125     gst_video_info_align (&info, &priv->align);
126
127     /* we need the video metadata too now */
128     priv->add_metavideo = TRUE;
129   } else {
130     gst_video_alignment_reset (&priv->align);
131   }
132
133   /* add the padding */
134   priv->padded_width =
135       GST_VIDEO_INFO_WIDTH (&info) + priv->align.padding_left +
136       priv->align.padding_right;
137   priv->padded_height =
138       GST_VIDEO_INFO_HEIGHT (&info) + priv->align.padding_top +
139       priv->align.padding_bottom;
140
141   priv->info = info;
142   priv->crop.x = priv->align.padding_left;
143   priv->crop.y = priv->align.padding_top;
144   priv->crop.w = priv->info.width;
145   priv->crop.h = priv->info.height;
146
147   return GST_BUFFER_POOL_CLASS (parent_class)->set_config (pool, config);
148
149   /* ERRORS */
150 wrong_config:
151   {
152     GST_WARNING_OBJECT (pool, "invalid config");
153     return FALSE;
154   }
155 no_caps:
156   {
157     GST_WARNING_OBJECT (pool, "no caps in config");
158     return FALSE;
159   }
160 wrong_caps:
161   {
162     GST_WARNING_OBJECT (pool,
163         "failed getting geometry from caps %" GST_PTR_FORMAT, caps);
164     return FALSE;
165   }
166 unknown_format:
167   {
168     GST_WARNING_OBJECT (pool, "failed to get format from caps %"
169         GST_PTR_FORMAT, caps);
170     return FALSE;;
171   }
172 }
173
174 /* This function handles GstXImageBuffer creation depending on XShm availability */
175 static GstFlowReturn
176 xvimage_buffer_pool_alloc (GstBufferPool * pool, GstBuffer ** buffer,
177     GstBufferPoolAcquireParams * params)
178 {
179   GstXvImageBufferPool *xvpool = GST_XVIMAGE_BUFFER_POOL_CAST (pool);
180   GstXvImageBufferPoolPrivate *priv = xvpool->priv;
181   GstVideoInfo *info;
182   GstBuffer *xvimage;
183   GstMemory *mem;
184
185   info = &priv->info;
186
187   xvimage = gst_buffer_new ();
188
189   mem = gst_xvimage_allocator_alloc (priv->allocator, priv->im_format,
190       priv->padded_width, priv->padded_height, &priv->crop, NULL);
191
192   if (mem == NULL) {
193     gst_buffer_unref (xvimage);
194     goto no_buffer;
195   }
196   gst_buffer_append_memory (xvimage, mem);
197
198   if (priv->add_metavideo) {
199     GST_DEBUG_OBJECT (pool, "adding GstVideoMeta");
200     gst_buffer_add_video_meta_full (xvimage, GST_VIDEO_FRAME_FLAG_NONE,
201         GST_VIDEO_INFO_FORMAT (info), GST_VIDEO_INFO_WIDTH (info),
202         GST_VIDEO_INFO_HEIGHT (info), GST_VIDEO_INFO_N_PLANES (info),
203         info->offset, info->stride);
204   }
205
206   *buffer = xvimage;
207
208   return GST_FLOW_OK;
209
210   /* ERROR */
211 no_buffer:
212   {
213     GST_WARNING_OBJECT (pool, "can't create image");
214     return GST_FLOW_ERROR;
215   }
216 }
217
218 GstBufferPool *
219 gst_xvimage_buffer_pool_new (GstXvImageAllocator * allocator)
220 {
221   GstXvImageBufferPool *pool;
222
223   pool = g_object_new (GST_TYPE_XVIMAGE_BUFFER_POOL, NULL);
224   pool->priv->allocator = gst_object_ref (allocator);
225
226   GST_LOG_OBJECT (pool, "new XvImage buffer pool %p", pool);
227
228   return GST_BUFFER_POOL_CAST (pool);
229 }
230
231 static void
232 gst_xvimage_buffer_pool_class_init (GstXvImageBufferPoolClass * klass)
233 {
234   GObjectClass *gobject_class = (GObjectClass *) klass;
235   GstBufferPoolClass *gstbufferpool_class = (GstBufferPoolClass *) klass;
236
237   g_type_class_add_private (klass, sizeof (GstXvImageBufferPoolPrivate));
238
239   gobject_class->finalize = gst_xvimage_buffer_pool_finalize;
240
241   gstbufferpool_class->get_options = xvimage_buffer_pool_get_options;
242   gstbufferpool_class->set_config = xvimage_buffer_pool_set_config;
243   gstbufferpool_class->alloc_buffer = xvimage_buffer_pool_alloc;
244 }
245
246 static void
247 gst_xvimage_buffer_pool_init (GstXvImageBufferPool * pool)
248 {
249   pool->priv = GST_XVIMAGE_BUFFER_POOL_GET_PRIVATE (pool);
250 }
251
252 static void
253 gst_xvimage_buffer_pool_finalize (GObject * object)
254 {
255   GstXvImageBufferPool *pool = GST_XVIMAGE_BUFFER_POOL_CAST (object);
256   GstXvImageBufferPoolPrivate *priv = pool->priv;
257
258   GST_LOG_OBJECT (pool, "finalize XvImage buffer pool %p", pool);
259
260   if (priv->caps)
261     gst_caps_unref (priv->caps);
262   if (priv->allocator)
263     gst_object_unref (priv->allocator);
264
265   G_OBJECT_CLASS (gst_xvimage_buffer_pool_parent_class)->finalize (object);
266 }