legal: add per-file authorship information.
[platform/upstream/gstreamer-vaapi.git] / gst / vaapi / gstvaapivideobufferpool.c
1 /*
2  *  gstvaapivideobufferpool.c - Gstreamer/VA video buffer pool
3  *
4  *  Copyright (C) 2013 Intel Corporation
5  *    Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
6  *
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.
11  *
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.
16  *
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
21  */
22
23 #include "gst/vaapi/sysdeps.h"
24 #include "gstvaapivideobufferpool.h"
25 #include "gstvaapivideobuffer.h"
26 #include "gstvaapivideomemory.h"
27
28 GST_DEBUG_CATEGORY_STATIC(gst_debug_vaapivideopool);
29 #define GST_CAT_DEFAULT gst_debug_vaapivideopool
30
31 G_DEFINE_TYPE(GstVaapiVideoBufferPool,
32               gst_vaapi_video_buffer_pool,
33               GST_TYPE_BUFFER_POOL)
34
35 enum {
36     PROP_0,
37
38     PROP_DISPLAY,
39 };
40
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;
47 };
48
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))
53
54 static void
55 gst_vaapi_video_buffer_pool_finalize(GObject *object)
56 {
57     GstVaapiVideoBufferPoolPrivate * const priv =
58         GST_VAAPI_VIDEO_BUFFER_POOL(object)->priv;
59
60     G_OBJECT_CLASS(gst_vaapi_video_buffer_pool_parent_class)->finalize(object);
61
62     gst_vaapi_display_replace(&priv->display, NULL);
63     g_clear_object(&priv->allocator);
64 }
65
66 static void
67 gst_vaapi_video_buffer_pool_set_property(GObject *object, guint prop_id,
68     const GValue *value, GParamSpec *pspec)
69 {
70     GstVaapiVideoBufferPoolPrivate * const priv =
71         GST_VAAPI_VIDEO_BUFFER_POOL(object)->priv;
72
73     switch (prop_id) {
74     case PROP_DISPLAY:
75         priv->display = gst_vaapi_display_ref(g_value_get_pointer(value));
76         break;
77     default:
78         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
79         break;
80     }
81 }
82
83 static void
84 gst_vaapi_video_buffer_pool_get_property(GObject *object, guint prop_id,
85     GValue *value, GParamSpec *pspec)
86 {
87     GstVaapiVideoBufferPoolPrivate * const priv =
88         GST_VAAPI_VIDEO_BUFFER_POOL(object)->priv;
89
90     switch (prop_id) {
91     case PROP_DISPLAY:
92         g_value_set_pointer(value, priv->display);
93         break;
94     default:
95         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
96         break;
97     }
98 }
99
100 static const gchar **
101 gst_vaapi_video_buffer_pool_get_options(GstBufferPool *pool)
102 {
103     static const gchar *g_options[] = {
104         GST_BUFFER_POOL_OPTION_VIDEO_META,
105         GST_BUFFER_POOL_OPTION_VAAPI_VIDEO_META,
106         NULL,
107     };
108     return g_options;
109 }
110
111 static gboolean
112 gst_vaapi_video_buffer_pool_set_config(GstBufferPool *pool,
113     GstStructure *config)
114 {
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;
122
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))
126         goto error_no_caps;
127
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);
132
133     if (changed_caps) {
134         allocator = gst_vaapi_video_allocator_new(priv->display, new_vip);
135         if (!allocator)
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;
141     }
142
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;
146
147     priv->has_video_meta = gst_buffer_pool_config_has_option(config,
148         GST_BUFFER_POOL_OPTION_VIDEO_META);
149
150     return GST_BUFFER_POOL_CLASS(gst_vaapi_video_buffer_pool_parent_class)->
151         set_config(pool, config);
152
153     /* ERRORS */
154 error_invalid_config:
155     {
156         GST_ERROR("invalid config");
157         return FALSE;
158     }
159 error_no_caps:
160     {
161         GST_ERROR("no valid caps in config");
162         return FALSE;
163     }
164 error_create_allocator:
165     {
166         GST_ERROR("failed to create GstVaapiVideoAllocator object");
167         return FALSE;
168     }
169 error_no_vaapi_video_meta_option:
170     {
171         GST_ERROR("no GstVaapiVideoMeta option");
172         return FALSE;
173     }
174 }
175
176 static GstFlowReturn
177 gst_vaapi_video_buffer_pool_alloc_buffer(GstBufferPool *pool,
178     GstBuffer **out_buffer_ptr, GstBufferPoolAcquireParams *params)
179 {
180     GstVaapiVideoBufferPoolPrivate * const priv =
181         GST_VAAPI_VIDEO_BUFFER_POOL(pool)->priv;
182     GstVaapiVideoMeta *meta;
183     GstMemory *mem;
184     GstBuffer *buffer;
185
186     if (!priv->allocator)
187         goto error_no_allocator;
188
189     meta = gst_vaapi_video_meta_new(priv->display);
190     if (!meta)
191         goto error_create_meta;
192
193     buffer = gst_vaapi_video_buffer_new(meta);
194     if (!buffer)
195         goto error_create_buffer;
196
197     mem = gst_vaapi_video_memory_new(priv->allocator, meta);
198     if (!mem)
199         goto error_create_memory;
200     gst_vaapi_video_meta_unref(meta);
201     gst_buffer_append_memory(buffer, mem);
202
203     if (priv->has_video_meta) {
204         GstVideoInfo * const vip =
205             &GST_VAAPI_VIDEO_ALLOCATOR_CAST(priv->allocator)->image_info;
206         GstVideoMeta *vmeta;
207
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;
215     }
216
217     *out_buffer_ptr = buffer;
218     return GST_FLOW_OK;
219
220     /* ERRORS */
221 error_no_allocator:
222     {
223         GST_ERROR("no GstAllocator in buffer pool");
224         return GST_FLOW_ERROR;
225     }
226 error_create_meta:
227     {
228         GST_ERROR("failed to allocate vaapi video meta");
229         return GST_FLOW_ERROR;
230     }
231 error_create_buffer:
232     {
233         GST_ERROR("failed to create video buffer");
234         gst_vaapi_video_meta_unref(meta);
235         return GST_FLOW_ERROR;
236     }
237 error_create_memory:
238     {
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;
243     }
244 }
245
246 static void
247 gst_vaapi_video_buffer_pool_reset_buffer(GstBufferPool *pool, GstBuffer *buffer)
248 {
249     GstMemory * const mem = gst_buffer_peek_memory(buffer, 0);
250
251     /* Release the underlying surface proxy */
252     gst_vaapi_video_memory_reset_surface(GST_VAAPI_VIDEO_MEMORY_CAST(mem));
253
254     GST_BUFFER_POOL_CLASS(gst_vaapi_video_buffer_pool_parent_class)->
255         reset_buffer(pool, buffer);
256 }
257
258 static void
259 gst_vaapi_video_buffer_pool_class_init(GstVaapiVideoBufferPoolClass *klass)
260 {
261     GObjectClass * const object_class = G_OBJECT_CLASS(klass);
262     GstBufferPoolClass * const pool_class = GST_BUFFER_POOL_CLASS(klass);
263
264     GST_DEBUG_CATEGORY_INIT(gst_debug_vaapivideopool,
265         "vaapivideopool", 0, "VA-API video pool");
266
267     g_type_class_add_private(klass, sizeof(GstVaapiVideoBufferPoolPrivate));
268
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;
276
277     /**
278      * GstVaapiVideoBufferPool:display:
279      *
280      * The #GstVaapiDisplay this object is bound to.
281      */
282     g_object_class_install_property
283         (object_class,
284          PROP_DISPLAY,
285          g_param_spec_pointer("display",
286                              "Display",
287                              "The GstVaapiDisplay to use for this video pool",
288                              G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
289 }
290
291 static void
292 gst_vaapi_video_buffer_pool_init(GstVaapiVideoBufferPool *pool)
293 {
294     GstVaapiVideoBufferPoolPrivate * const priv =
295         GST_VAAPI_VIDEO_BUFFER_POOL_GET_PRIVATE(pool);
296
297     pool->priv = priv;
298
299     gst_video_info_init(&priv->video_info[0]);
300     gst_video_info_init(&priv->video_info[1]);
301 }
302
303 GstBufferPool *
304 gst_vaapi_video_buffer_pool_new(GstVaapiDisplay *display)
305 {
306     return g_object_new(GST_VAAPI_TYPE_VIDEO_BUFFER_POOL,
307         "display", display, NULL);
308 }