2 * gstvaapivideobufferpool.c - Gstreamer/VA video buffer pool
4 * Copyright (C) 2013 Intel Corporation
5 * Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * as published by the Free Software Foundation; either version 2.1
10 * of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301 USA
23 #include "gst/vaapi/sysdeps.h"
24 #include "gstvaapivideobufferpool.h"
25 #include "gstvaapivideobuffer.h"
26 #include "gstvaapivideomemory.h"
28 GST_DEBUG_CATEGORY_STATIC(gst_debug_vaapivideopool);
29 #define GST_CAT_DEFAULT gst_debug_vaapivideopool
31 G_DEFINE_TYPE(GstVaapiVideoBufferPool,
32 gst_vaapi_video_buffer_pool,
41 struct _GstVaapiVideoBufferPoolPrivate {
42 GstVideoInfo video_info[2];
43 guint video_info_index;
44 GstAllocator *allocator;
45 GstVaapiDisplay *display;
46 guint has_video_meta : 1;
49 #define GST_VAAPI_VIDEO_BUFFER_POOL_GET_PRIVATE(obj) \
50 (G_TYPE_INSTANCE_GET_PRIVATE((obj), \
51 GST_VAAPI_TYPE_VIDEO_BUFFER_POOL, \
52 GstVaapiVideoBufferPoolPrivate))
55 gst_vaapi_video_buffer_pool_finalize(GObject *object)
57 GstVaapiVideoBufferPoolPrivate * const priv =
58 GST_VAAPI_VIDEO_BUFFER_POOL(object)->priv;
60 G_OBJECT_CLASS(gst_vaapi_video_buffer_pool_parent_class)->finalize(object);
62 gst_vaapi_display_replace(&priv->display, NULL);
63 g_clear_object(&priv->allocator);
67 gst_vaapi_video_buffer_pool_set_property(GObject *object, guint prop_id,
68 const GValue *value, GParamSpec *pspec)
70 GstVaapiVideoBufferPoolPrivate * const priv =
71 GST_VAAPI_VIDEO_BUFFER_POOL(object)->priv;
75 priv->display = gst_vaapi_display_ref(g_value_get_pointer(value));
78 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
84 gst_vaapi_video_buffer_pool_get_property(GObject *object, guint prop_id,
85 GValue *value, GParamSpec *pspec)
87 GstVaapiVideoBufferPoolPrivate * const priv =
88 GST_VAAPI_VIDEO_BUFFER_POOL(object)->priv;
92 g_value_set_pointer(value, priv->display);
95 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
100 static const gchar **
101 gst_vaapi_video_buffer_pool_get_options(GstBufferPool *pool)
103 static const gchar *g_options[] = {
104 GST_BUFFER_POOL_OPTION_VIDEO_META,
105 GST_BUFFER_POOL_OPTION_VAAPI_VIDEO_META,
112 gst_vaapi_video_buffer_pool_set_config(GstBufferPool *pool,
113 GstStructure *config)
115 GstVaapiVideoBufferPoolPrivate * const priv =
116 GST_VAAPI_VIDEO_BUFFER_POOL(pool)->priv;
117 GstCaps *caps = NULL;
118 GstVideoInfo * const cur_vip = &priv->video_info[priv->video_info_index];
119 GstVideoInfo * const new_vip = &priv->video_info[!priv->video_info_index];
120 GstAllocator *allocator;
121 gboolean changed_caps;
123 if (!gst_buffer_pool_config_get_params(config, &caps, NULL, NULL, NULL))
124 goto error_invalid_config;
125 if (!caps || !gst_video_info_from_caps(new_vip, caps))
128 changed_caps = !priv->allocator ||
129 GST_VIDEO_INFO_FORMAT(cur_vip) != GST_VIDEO_INFO_FORMAT(new_vip) ||
130 GST_VIDEO_INFO_WIDTH(cur_vip) != GST_VIDEO_INFO_WIDTH(new_vip) ||
131 GST_VIDEO_INFO_HEIGHT(cur_vip) != GST_VIDEO_INFO_HEIGHT(new_vip);
134 allocator = gst_vaapi_video_allocator_new(priv->display, new_vip);
136 goto error_create_allocator;
137 gst_object_replace((GstObject **)&priv->allocator,
138 GST_OBJECT_CAST(allocator));
139 gst_object_unref(allocator);
140 priv->video_info_index ^= 1;
143 if (!gst_buffer_pool_config_has_option(config,
144 GST_BUFFER_POOL_OPTION_VAAPI_VIDEO_META))
145 goto error_no_vaapi_video_meta_option;
147 priv->has_video_meta = gst_buffer_pool_config_has_option(config,
148 GST_BUFFER_POOL_OPTION_VIDEO_META);
150 return GST_BUFFER_POOL_CLASS(gst_vaapi_video_buffer_pool_parent_class)->
151 set_config(pool, config);
154 error_invalid_config:
156 GST_ERROR("invalid config");
161 GST_ERROR("no valid caps in config");
164 error_create_allocator:
166 GST_ERROR("failed to create GstVaapiVideoAllocator object");
169 error_no_vaapi_video_meta_option:
171 GST_ERROR("no GstVaapiVideoMeta option");
177 gst_vaapi_video_buffer_pool_alloc_buffer(GstBufferPool *pool,
178 GstBuffer **out_buffer_ptr, GstBufferPoolAcquireParams *params)
180 GstVaapiVideoBufferPoolPrivate * const priv =
181 GST_VAAPI_VIDEO_BUFFER_POOL(pool)->priv;
182 GstVaapiVideoMeta *meta;
186 if (!priv->allocator)
187 goto error_no_allocator;
189 meta = gst_vaapi_video_meta_new(priv->display);
191 goto error_create_meta;
193 buffer = gst_vaapi_video_buffer_new(meta);
195 goto error_create_buffer;
197 mem = gst_vaapi_video_memory_new(priv->allocator, meta);
199 goto error_create_memory;
200 gst_vaapi_video_meta_unref(meta);
201 gst_buffer_append_memory(buffer, mem);
203 if (priv->has_video_meta) {
204 GstVideoInfo * const vip =
205 &GST_VAAPI_VIDEO_ALLOCATOR_CAST(priv->allocator)->image_info;
208 vmeta = gst_buffer_add_video_meta_full(buffer, 0,
209 GST_VIDEO_INFO_FORMAT(vip), GST_VIDEO_INFO_WIDTH(vip),
210 GST_VIDEO_INFO_HEIGHT(vip), GST_VIDEO_INFO_N_PLANES(vip),
211 &GST_VIDEO_INFO_PLANE_OFFSET(vip, 0),
212 &GST_VIDEO_INFO_PLANE_STRIDE(vip, 0));
213 vmeta->map = gst_video_meta_map_vaapi_memory;
214 vmeta->unmap = gst_video_meta_unmap_vaapi_memory;
217 *out_buffer_ptr = buffer;
223 GST_ERROR("no GstAllocator in buffer pool");
224 return GST_FLOW_ERROR;
228 GST_ERROR("failed to allocate vaapi video meta");
229 return GST_FLOW_ERROR;
233 GST_ERROR("failed to create video buffer");
234 gst_vaapi_video_meta_unref(meta);
235 return GST_FLOW_ERROR;
239 GST_ERROR("failed to create video memory");
240 gst_buffer_unref(buffer);
241 gst_vaapi_video_meta_unref(meta);
242 return GST_FLOW_ERROR;
247 gst_vaapi_video_buffer_pool_reset_buffer(GstBufferPool *pool, GstBuffer *buffer)
249 GstMemory * const mem = gst_buffer_peek_memory(buffer, 0);
251 /* Release the underlying surface proxy */
252 gst_vaapi_video_memory_reset_surface(GST_VAAPI_VIDEO_MEMORY_CAST(mem));
254 GST_BUFFER_POOL_CLASS(gst_vaapi_video_buffer_pool_parent_class)->
255 reset_buffer(pool, buffer);
259 gst_vaapi_video_buffer_pool_class_init(GstVaapiVideoBufferPoolClass *klass)
261 GObjectClass * const object_class = G_OBJECT_CLASS(klass);
262 GstBufferPoolClass * const pool_class = GST_BUFFER_POOL_CLASS(klass);
264 GST_DEBUG_CATEGORY_INIT(gst_debug_vaapivideopool,
265 "vaapivideopool", 0, "VA-API video pool");
267 g_type_class_add_private(klass, sizeof(GstVaapiVideoBufferPoolPrivate));
269 object_class->finalize = gst_vaapi_video_buffer_pool_finalize;
270 object_class->set_property = gst_vaapi_video_buffer_pool_set_property;
271 object_class->get_property = gst_vaapi_video_buffer_pool_get_property;
272 pool_class->get_options = gst_vaapi_video_buffer_pool_get_options;
273 pool_class->set_config = gst_vaapi_video_buffer_pool_set_config;
274 pool_class->alloc_buffer = gst_vaapi_video_buffer_pool_alloc_buffer;
275 pool_class->reset_buffer = gst_vaapi_video_buffer_pool_reset_buffer;
278 * GstVaapiVideoBufferPool:display:
280 * The #GstVaapiDisplay this object is bound to.
282 g_object_class_install_property
285 g_param_spec_pointer("display",
287 "The GstVaapiDisplay to use for this video pool",
288 G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
292 gst_vaapi_video_buffer_pool_init(GstVaapiVideoBufferPool *pool)
294 GstVaapiVideoBufferPoolPrivate * const priv =
295 GST_VAAPI_VIDEO_BUFFER_POOL_GET_PRIVATE(pool);
299 gst_video_info_init(&priv->video_info[0]);
300 gst_video_info_init(&priv->video_info[1]);
304 gst_vaapi_video_buffer_pool_new(GstVaapiDisplay *display)
306 return g_object_new(GST_VAAPI_TYPE_VIDEO_BUFFER_POOL,
307 "display", display, NULL);