bde949c9c0ad91bcb5ecb2cf9e846c31cafabe9e
[platform/upstream/gst-plugins-base.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_xv_image_pool);
38 #define GST_CAT_DEFAULT gst_debug_xv_image_pool
39
40 /* bufferpool */
41 static void gst_xvimage_buffer_pool_finalize (GObject * object);
42
43 #define gst_xvimage_buffer_pool_parent_class parent_class
44 G_DEFINE_TYPE (GstXvImageBufferPool, gst_xvimage_buffer_pool,
45     GST_TYPE_BUFFER_POOL);
46
47 static const gchar **
48 xvimage_buffer_pool_get_options (GstBufferPool * pool)
49 {
50   static const gchar *options[] = { GST_BUFFER_POOL_OPTION_VIDEO_META,
51     GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT, NULL
52   };
53
54   return options;
55 }
56
57 static gboolean
58 xvimage_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
59 {
60   GstXvImageBufferPool *xvpool = GST_XVIMAGE_BUFFER_POOL_CAST (pool);
61   GstVideoInfo info;
62   GstCaps *caps;
63   guint size, min_buffers, max_buffers;
64   GstXvContext *context;
65
66   if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
67           &max_buffers))
68     goto wrong_config;
69
70   if (caps == NULL)
71     goto no_caps;
72
73   /* now parse the caps from the config */
74   if (!gst_video_info_from_caps (&info, caps))
75     goto wrong_caps;
76
77   GST_LOG_OBJECT (pool, "%dx%d, caps %" GST_PTR_FORMAT, info.width, info.height,
78       caps);
79
80   context = gst_xvimage_allocator_peek_context (xvpool->allocator);
81
82   xvpool->im_format = gst_xvcontext_get_format_from_info (context, &info);
83   if (xvpool->im_format == -1)
84     goto unknown_format;
85
86   if (xvpool->caps)
87     gst_caps_unref (xvpool->caps);
88   xvpool->caps = gst_caps_ref (caps);
89
90   /* enable metadata based on config of the pool */
91   xvpool->add_metavideo =
92       gst_buffer_pool_config_has_option (config,
93       GST_BUFFER_POOL_OPTION_VIDEO_META);
94
95   /* parse extra alignment info */
96   xvpool->need_alignment = gst_buffer_pool_config_has_option (config,
97       GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
98
99   if (xvpool->need_alignment) {
100     gst_buffer_pool_config_get_video_alignment (config, &xvpool->align);
101
102     GST_LOG_OBJECT (pool, "padding %u-%ux%u-%u", xvpool->align.padding_top,
103         xvpool->align.padding_left, xvpool->align.padding_left,
104         xvpool->align.padding_bottom);
105
106     /* do padding and alignment */
107     gst_video_info_align (&info, &xvpool->align);
108
109     gst_buffer_pool_config_set_video_alignment (config, &xvpool->align);
110
111     /* we need the video metadata too now */
112     xvpool->add_metavideo = TRUE;
113   } else {
114     gst_video_alignment_reset (&xvpool->align);
115   }
116
117   /* add the padding */
118   xvpool->padded_width =
119       GST_VIDEO_INFO_WIDTH (&info) + xvpool->align.padding_left +
120       xvpool->align.padding_right;
121   xvpool->padded_height =
122       GST_VIDEO_INFO_HEIGHT (&info) + xvpool->align.padding_top +
123       xvpool->align.padding_bottom;
124
125   xvpool->info = info;
126   xvpool->crop.x = xvpool->align.padding_left;
127   xvpool->crop.y = xvpool->align.padding_top;
128   xvpool->crop.w = xvpool->info.width;
129   xvpool->crop.h = xvpool->info.height;
130
131   gst_buffer_pool_config_set_params (config, caps, info.size, min_buffers,
132       max_buffers);
133
134   return GST_BUFFER_POOL_CLASS (parent_class)->set_config (pool, config);
135
136   /* ERRORS */
137 wrong_config:
138   {
139     GST_WARNING_OBJECT (pool, "invalid config");
140     return FALSE;
141   }
142 no_caps:
143   {
144     GST_WARNING_OBJECT (pool, "no caps in config");
145     return FALSE;
146   }
147 wrong_caps:
148   {
149     GST_WARNING_OBJECT (pool,
150         "failed getting geometry from caps %" GST_PTR_FORMAT, caps);
151     return FALSE;
152   }
153 unknown_format:
154   {
155     GST_WARNING_OBJECT (pool, "failed to get format from caps %"
156         GST_PTR_FORMAT, caps);
157     return FALSE;
158   }
159 }
160
161 /* This function handles GstXImageBuffer creation depending on XShm availability */
162 static GstFlowReturn
163 xvimage_buffer_pool_alloc (GstBufferPool * pool, GstBuffer ** buffer,
164     GstBufferPoolAcquireParams * params)
165 {
166   GstXvImageBufferPool *xvpool = GST_XVIMAGE_BUFFER_POOL_CAST (pool);
167   GstVideoInfo *info;
168   GstBuffer *xvimage;
169   GstMemory *mem;
170
171   info = &xvpool->info;
172
173   xvimage = gst_buffer_new ();
174
175   mem = gst_xvimage_allocator_alloc (xvpool->allocator, xvpool->im_format,
176       xvpool->padded_width, xvpool->padded_height, &xvpool->crop, NULL);
177
178   if (mem == NULL) {
179     gst_buffer_unref (xvimage);
180     goto no_buffer;
181   }
182   gst_buffer_append_memory (xvimage, mem);
183
184   if (xvpool->add_metavideo) {
185     GST_DEBUG_OBJECT (pool, "adding GstVideoMeta");
186     gst_buffer_add_video_meta_full (xvimage, GST_VIDEO_FRAME_FLAG_NONE,
187         GST_VIDEO_INFO_FORMAT (info), GST_VIDEO_INFO_WIDTH (info),
188         GST_VIDEO_INFO_HEIGHT (info), GST_VIDEO_INFO_N_PLANES (info),
189         info->offset, info->stride);
190   }
191
192   *buffer = xvimage;
193
194   return GST_FLOW_OK;
195
196   /* ERROR */
197 no_buffer:
198   {
199     GST_WARNING_OBJECT (pool, "can't create image");
200     return GST_FLOW_ERROR;
201   }
202 }
203
204 GstBufferPool *
205 gst_xvimage_buffer_pool_new (GstXvImageAllocator * allocator)
206 {
207   GstXvImageBufferPool *pool;
208
209   pool = g_object_new (GST_TYPE_XVIMAGE_BUFFER_POOL, NULL);
210   pool->allocator = gst_object_ref (allocator);
211
212   GST_LOG_OBJECT (pool, "new XvImage buffer pool %p", pool);
213
214   return GST_BUFFER_POOL_CAST (pool);
215 }
216
217 static void
218 gst_xvimage_buffer_pool_class_init (GstXvImageBufferPoolClass * klass)
219 {
220   GObjectClass *gobject_class = (GObjectClass *) klass;
221   GstBufferPoolClass *gstbufferpool_class = (GstBufferPoolClass *) klass;
222
223   gobject_class->finalize = gst_xvimage_buffer_pool_finalize;
224
225   gstbufferpool_class->get_options = xvimage_buffer_pool_get_options;
226   gstbufferpool_class->set_config = xvimage_buffer_pool_set_config;
227   gstbufferpool_class->alloc_buffer = xvimage_buffer_pool_alloc;
228 }
229
230 static void
231 gst_xvimage_buffer_pool_init (GstXvImageBufferPool * pool)
232 {
233   /* nothing to do here */
234 }
235
236 static void
237 gst_xvimage_buffer_pool_finalize (GObject * object)
238 {
239   GstXvImageBufferPool *pool = GST_XVIMAGE_BUFFER_POOL_CAST (object);
240
241   GST_LOG_OBJECT (pool, "finalize XvImage buffer pool %p", pool);
242
243   if (pool->caps)
244     gst_caps_unref (pool->caps);
245   if (pool->allocator)
246     gst_object_unref (pool->allocator);
247
248   G_OBJECT_CLASS (gst_xvimage_buffer_pool_parent_class)->finalize (object);
249 }