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"
27 #if GST_CHECK_VERSION(1,1,0) && USE_GLX
28 #include "gstvaapivideometa_texture.h"
31 GST_DEBUG_CATEGORY_STATIC(gst_debug_vaapivideopool);
32 #define GST_CAT_DEFAULT gst_debug_vaapivideopool
34 G_DEFINE_TYPE(GstVaapiVideoBufferPool,
35 gst_vaapi_video_buffer_pool,
44 struct _GstVaapiVideoBufferPoolPrivate {
45 GstVideoInfo video_info[2];
46 guint video_info_index;
47 GstAllocator *allocator;
48 GstVaapiDisplay *display;
49 guint has_video_meta : 1;
50 guint has_video_alignment : 1;
51 guint has_texture_upload_meta : 1;
54 #define GST_VAAPI_VIDEO_BUFFER_POOL_GET_PRIVATE(obj) \
55 (G_TYPE_INSTANCE_GET_PRIVATE((obj), \
56 GST_VAAPI_TYPE_VIDEO_BUFFER_POOL, \
57 GstVaapiVideoBufferPoolPrivate))
60 gst_vaapi_video_buffer_pool_finalize(GObject *object)
62 GstVaapiVideoBufferPoolPrivate * const priv =
63 GST_VAAPI_VIDEO_BUFFER_POOL(object)->priv;
65 G_OBJECT_CLASS(gst_vaapi_video_buffer_pool_parent_class)->finalize(object);
67 gst_vaapi_display_replace(&priv->display, NULL);
68 g_clear_object(&priv->allocator);
72 gst_vaapi_video_buffer_pool_set_property(GObject *object, guint prop_id,
73 const GValue *value, GParamSpec *pspec)
75 GstVaapiVideoBufferPoolPrivate * const priv =
76 GST_VAAPI_VIDEO_BUFFER_POOL(object)->priv;
80 priv->display = gst_vaapi_display_ref(g_value_get_pointer(value));
83 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
89 gst_vaapi_video_buffer_pool_get_property(GObject *object, guint prop_id,
90 GValue *value, GParamSpec *pspec)
92 GstVaapiVideoBufferPoolPrivate * const priv =
93 GST_VAAPI_VIDEO_BUFFER_POOL(object)->priv;
97 g_value_set_pointer(value, priv->display);
100 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
106 fill_video_alignment(GstVaapiVideoBufferPool *pool, GstVideoAlignment *align)
108 GstVideoInfo * const vip =
109 &GST_VAAPI_VIDEO_ALLOCATOR_CAST(pool->priv->allocator)->image_info;
112 gst_video_alignment_reset(align);
113 for (i = 0; i < GST_VIDEO_INFO_N_PLANES(vip); i++)
114 align->stride_align[i] =
115 (1U << g_bit_nth_lsf(GST_VIDEO_INFO_PLANE_STRIDE(vip, i), 0)) - 1;
118 static const gchar **
119 gst_vaapi_video_buffer_pool_get_options(GstBufferPool *pool)
121 static const gchar *g_options[] = {
122 GST_BUFFER_POOL_OPTION_VIDEO_META,
123 GST_BUFFER_POOL_OPTION_VAAPI_VIDEO_META,
124 GST_BUFFER_POOL_OPTION_VIDEO_GL_TEXTURE_UPLOAD_META,
125 GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT,
132 gst_vaapi_video_buffer_pool_set_config(GstBufferPool *pool,
133 GstStructure *config)
135 GstVaapiVideoBufferPoolPrivate * const priv =
136 GST_VAAPI_VIDEO_BUFFER_POOL(pool)->priv;
137 GstCaps *caps = NULL;
138 GstVideoInfo * const cur_vip = &priv->video_info[priv->video_info_index];
139 GstVideoInfo * const new_vip = &priv->video_info[!priv->video_info_index];
140 GstVideoAlignment align;
141 GstAllocator *allocator;
142 gboolean changed_caps;
144 if (!gst_buffer_pool_config_get_params(config, &caps, NULL, NULL, NULL))
145 goto error_invalid_config;
146 if (!caps || !gst_video_info_from_caps(new_vip, caps))
149 changed_caps = !priv->allocator ||
150 GST_VIDEO_INFO_FORMAT(cur_vip) != GST_VIDEO_INFO_FORMAT(new_vip) ||
151 GST_VIDEO_INFO_WIDTH(cur_vip) != GST_VIDEO_INFO_WIDTH(new_vip) ||
152 GST_VIDEO_INFO_HEIGHT(cur_vip) != GST_VIDEO_INFO_HEIGHT(new_vip);
155 allocator = gst_vaapi_video_allocator_new(priv->display, new_vip);
157 goto error_create_allocator;
158 gst_object_replace((GstObject **)&priv->allocator,
159 GST_OBJECT_CAST(allocator));
160 gst_object_unref(allocator);
161 priv->video_info_index ^= 1;
164 if (!gst_buffer_pool_config_has_option(config,
165 GST_BUFFER_POOL_OPTION_VAAPI_VIDEO_META))
166 goto error_no_vaapi_video_meta_option;
168 priv->has_video_meta = gst_buffer_pool_config_has_option(config,
169 GST_BUFFER_POOL_OPTION_VIDEO_META);
171 priv->has_video_alignment = gst_buffer_pool_config_has_option(config,
172 GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
173 if (priv->has_video_alignment) {
174 fill_video_alignment(GST_VAAPI_VIDEO_BUFFER_POOL(pool), &align);
175 gst_buffer_pool_config_set_video_alignment(config, &align);
178 priv->has_texture_upload_meta = gst_buffer_pool_config_has_option(config,
179 GST_BUFFER_POOL_OPTION_VIDEO_GL_TEXTURE_UPLOAD_META);
181 return GST_BUFFER_POOL_CLASS(gst_vaapi_video_buffer_pool_parent_class)->
182 set_config(pool, config);
185 error_invalid_config:
187 GST_ERROR("invalid config");
192 GST_ERROR("no valid caps in config");
195 error_create_allocator:
197 GST_ERROR("failed to create GstVaapiVideoAllocator object");
200 error_no_vaapi_video_meta_option:
202 GST_ERROR("no GstVaapiVideoMeta option");
208 gst_vaapi_video_buffer_pool_alloc_buffer(GstBufferPool *pool,
209 GstBuffer **out_buffer_ptr, GstBufferPoolAcquireParams *params)
211 GstVaapiVideoBufferPoolPrivate * const priv =
212 GST_VAAPI_VIDEO_BUFFER_POOL(pool)->priv;
213 GstVaapiVideoMeta *meta;
217 if (!priv->allocator)
218 goto error_no_allocator;
220 meta = gst_vaapi_video_meta_new(priv->display);
222 goto error_create_meta;
224 buffer = gst_vaapi_video_buffer_new(meta);
226 goto error_create_buffer;
228 mem = gst_vaapi_video_memory_new(priv->allocator, meta);
230 goto error_create_memory;
231 gst_vaapi_video_meta_unref(meta);
232 gst_buffer_append_memory(buffer, mem);
234 if (priv->has_video_meta) {
235 GstVideoInfo * const vip =
236 &GST_VAAPI_VIDEO_ALLOCATOR_CAST(priv->allocator)->image_info;
239 vmeta = gst_buffer_add_video_meta_full(buffer, 0,
240 GST_VIDEO_INFO_FORMAT(vip), GST_VIDEO_INFO_WIDTH(vip),
241 GST_VIDEO_INFO_HEIGHT(vip), GST_VIDEO_INFO_N_PLANES(vip),
242 &GST_VIDEO_INFO_PLANE_OFFSET(vip, 0),
243 &GST_VIDEO_INFO_PLANE_STRIDE(vip, 0));
244 vmeta->map = gst_video_meta_map_vaapi_memory;
245 vmeta->unmap = gst_video_meta_unmap_vaapi_memory;
248 #if GST_CHECK_VERSION(1,1,0) && USE_GLX
249 if (priv->has_texture_upload_meta)
250 gst_buffer_add_texture_upload_meta(buffer);
253 *out_buffer_ptr = buffer;
259 GST_ERROR("no GstAllocator in buffer pool");
260 return GST_FLOW_ERROR;
264 GST_ERROR("failed to allocate vaapi video meta");
265 return GST_FLOW_ERROR;
269 GST_ERROR("failed to create video buffer");
270 gst_vaapi_video_meta_unref(meta);
271 return GST_FLOW_ERROR;
275 GST_ERROR("failed to create video memory");
276 gst_buffer_unref(buffer);
277 gst_vaapi_video_meta_unref(meta);
278 return GST_FLOW_ERROR;
283 gst_vaapi_video_buffer_pool_reset_buffer(GstBufferPool *pool, GstBuffer *buffer)
285 GstMemory * const mem = gst_buffer_peek_memory(buffer, 0);
287 /* Release the underlying surface proxy */
288 gst_vaapi_video_memory_reset_surface(GST_VAAPI_VIDEO_MEMORY_CAST(mem));
290 GST_BUFFER_POOL_CLASS(gst_vaapi_video_buffer_pool_parent_class)->
291 reset_buffer(pool, buffer);
295 gst_vaapi_video_buffer_pool_class_init(GstVaapiVideoBufferPoolClass *klass)
297 GObjectClass * const object_class = G_OBJECT_CLASS(klass);
298 GstBufferPoolClass * const pool_class = GST_BUFFER_POOL_CLASS(klass);
300 GST_DEBUG_CATEGORY_INIT(gst_debug_vaapivideopool,
301 "vaapivideopool", 0, "VA-API video pool");
303 g_type_class_add_private(klass, sizeof(GstVaapiVideoBufferPoolPrivate));
305 object_class->finalize = gst_vaapi_video_buffer_pool_finalize;
306 object_class->set_property = gst_vaapi_video_buffer_pool_set_property;
307 object_class->get_property = gst_vaapi_video_buffer_pool_get_property;
308 pool_class->get_options = gst_vaapi_video_buffer_pool_get_options;
309 pool_class->set_config = gst_vaapi_video_buffer_pool_set_config;
310 pool_class->alloc_buffer = gst_vaapi_video_buffer_pool_alloc_buffer;
311 pool_class->reset_buffer = gst_vaapi_video_buffer_pool_reset_buffer;
314 * GstVaapiVideoBufferPool:display:
316 * The #GstVaapiDisplay this object is bound to.
318 g_object_class_install_property
321 g_param_spec_pointer("display",
323 "The GstVaapiDisplay to use for this video pool",
324 G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
328 gst_vaapi_video_buffer_pool_init(GstVaapiVideoBufferPool *pool)
330 GstVaapiVideoBufferPoolPrivate * const priv =
331 GST_VAAPI_VIDEO_BUFFER_POOL_GET_PRIVATE(pool);
335 gst_video_info_init(&priv->video_info[0]);
336 gst_video_info_init(&priv->video_info[1]);
340 gst_vaapi_video_buffer_pool_new(GstVaapiDisplay *display)
342 return g_object_new(GST_VAAPI_TYPE_VIDEO_BUFFER_POOL,
343 "display", display, NULL);