vaapidecode: add support for VideoAlignment bufferpool option.
[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 #if GST_CHECK_VERSION(1,1,0) && USE_GLX
28 #include "gstvaapivideometa_texture.h"
29 #endif
30
31 GST_DEBUG_CATEGORY_STATIC(gst_debug_vaapivideopool);
32 #define GST_CAT_DEFAULT gst_debug_vaapivideopool
33
34 G_DEFINE_TYPE(GstVaapiVideoBufferPool,
35               gst_vaapi_video_buffer_pool,
36               GST_TYPE_BUFFER_POOL)
37
38 enum {
39     PROP_0,
40
41     PROP_DISPLAY,
42 };
43
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;
52 };
53
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))
58
59 static void
60 gst_vaapi_video_buffer_pool_finalize(GObject *object)
61 {
62     GstVaapiVideoBufferPoolPrivate * const priv =
63         GST_VAAPI_VIDEO_BUFFER_POOL(object)->priv;
64
65     G_OBJECT_CLASS(gst_vaapi_video_buffer_pool_parent_class)->finalize(object);
66
67     gst_vaapi_display_replace(&priv->display, NULL);
68     g_clear_object(&priv->allocator);
69 }
70
71 static void
72 gst_vaapi_video_buffer_pool_set_property(GObject *object, guint prop_id,
73     const GValue *value, GParamSpec *pspec)
74 {
75     GstVaapiVideoBufferPoolPrivate * const priv =
76         GST_VAAPI_VIDEO_BUFFER_POOL(object)->priv;
77
78     switch (prop_id) {
79     case PROP_DISPLAY:
80         priv->display = gst_vaapi_display_ref(g_value_get_pointer(value));
81         break;
82     default:
83         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
84         break;
85     }
86 }
87
88 static void
89 gst_vaapi_video_buffer_pool_get_property(GObject *object, guint prop_id,
90     GValue *value, GParamSpec *pspec)
91 {
92     GstVaapiVideoBufferPoolPrivate * const priv =
93         GST_VAAPI_VIDEO_BUFFER_POOL(object)->priv;
94
95     switch (prop_id) {
96     case PROP_DISPLAY:
97         g_value_set_pointer(value, priv->display);
98         break;
99     default:
100         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
101         break;
102     }
103 }
104
105 static void
106 fill_video_alignment(GstVaapiVideoBufferPool *pool, GstVideoAlignment *align)
107 {
108     GstVideoInfo * const vip =
109         &GST_VAAPI_VIDEO_ALLOCATOR_CAST(pool->priv->allocator)->image_info;
110     guint i;
111
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;
116 }
117
118 static const gchar **
119 gst_vaapi_video_buffer_pool_get_options(GstBufferPool *pool)
120 {
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,
126         NULL,
127     };
128     return g_options;
129 }
130
131 static gboolean
132 gst_vaapi_video_buffer_pool_set_config(GstBufferPool *pool,
133     GstStructure *config)
134 {
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;
143
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))
147         goto error_no_caps;
148
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);
153
154     if (changed_caps) {
155         allocator = gst_vaapi_video_allocator_new(priv->display, new_vip);
156         if (!allocator)
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;
162     }
163
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;
167
168     priv->has_video_meta = gst_buffer_pool_config_has_option(config,
169         GST_BUFFER_POOL_OPTION_VIDEO_META);
170
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);
176     }
177
178     priv->has_texture_upload_meta = gst_buffer_pool_config_has_option(config,
179         GST_BUFFER_POOL_OPTION_VIDEO_GL_TEXTURE_UPLOAD_META);
180
181     return GST_BUFFER_POOL_CLASS(gst_vaapi_video_buffer_pool_parent_class)->
182         set_config(pool, config);
183
184     /* ERRORS */
185 error_invalid_config:
186     {
187         GST_ERROR("invalid config");
188         return FALSE;
189     }
190 error_no_caps:
191     {
192         GST_ERROR("no valid caps in config");
193         return FALSE;
194     }
195 error_create_allocator:
196     {
197         GST_ERROR("failed to create GstVaapiVideoAllocator object");
198         return FALSE;
199     }
200 error_no_vaapi_video_meta_option:
201     {
202         GST_ERROR("no GstVaapiVideoMeta option");
203         return FALSE;
204     }
205 }
206
207 static GstFlowReturn
208 gst_vaapi_video_buffer_pool_alloc_buffer(GstBufferPool *pool,
209     GstBuffer **out_buffer_ptr, GstBufferPoolAcquireParams *params)
210 {
211     GstVaapiVideoBufferPoolPrivate * const priv =
212         GST_VAAPI_VIDEO_BUFFER_POOL(pool)->priv;
213     GstVaapiVideoMeta *meta;
214     GstMemory *mem;
215     GstBuffer *buffer;
216
217     if (!priv->allocator)
218         goto error_no_allocator;
219
220     meta = gst_vaapi_video_meta_new(priv->display);
221     if (!meta)
222         goto error_create_meta;
223
224     buffer = gst_vaapi_video_buffer_new(meta);
225     if (!buffer)
226         goto error_create_buffer;
227
228     mem = gst_vaapi_video_memory_new(priv->allocator, meta);
229     if (!mem)
230         goto error_create_memory;
231     gst_vaapi_video_meta_unref(meta);
232     gst_buffer_append_memory(buffer, mem);
233
234     if (priv->has_video_meta) {
235         GstVideoInfo * const vip =
236             &GST_VAAPI_VIDEO_ALLOCATOR_CAST(priv->allocator)->image_info;
237         GstVideoMeta *vmeta;
238
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;
246     }
247
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);
251 #endif
252
253     *out_buffer_ptr = buffer;
254     return GST_FLOW_OK;
255
256     /* ERRORS */
257 error_no_allocator:
258     {
259         GST_ERROR("no GstAllocator in buffer pool");
260         return GST_FLOW_ERROR;
261     }
262 error_create_meta:
263     {
264         GST_ERROR("failed to allocate vaapi video meta");
265         return GST_FLOW_ERROR;
266     }
267 error_create_buffer:
268     {
269         GST_ERROR("failed to create video buffer");
270         gst_vaapi_video_meta_unref(meta);
271         return GST_FLOW_ERROR;
272     }
273 error_create_memory:
274     {
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;
279     }
280 }
281
282 static void
283 gst_vaapi_video_buffer_pool_reset_buffer(GstBufferPool *pool, GstBuffer *buffer)
284 {
285     GstMemory * const mem = gst_buffer_peek_memory(buffer, 0);
286
287     /* Release the underlying surface proxy */
288     gst_vaapi_video_memory_reset_surface(GST_VAAPI_VIDEO_MEMORY_CAST(mem));
289
290     GST_BUFFER_POOL_CLASS(gst_vaapi_video_buffer_pool_parent_class)->
291         reset_buffer(pool, buffer);
292 }
293
294 static void
295 gst_vaapi_video_buffer_pool_class_init(GstVaapiVideoBufferPoolClass *klass)
296 {
297     GObjectClass * const object_class = G_OBJECT_CLASS(klass);
298     GstBufferPoolClass * const pool_class = GST_BUFFER_POOL_CLASS(klass);
299
300     GST_DEBUG_CATEGORY_INIT(gst_debug_vaapivideopool,
301         "vaapivideopool", 0, "VA-API video pool");
302
303     g_type_class_add_private(klass, sizeof(GstVaapiVideoBufferPoolPrivate));
304
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;
312
313     /**
314      * GstVaapiVideoBufferPool:display:
315      *
316      * The #GstVaapiDisplay this object is bound to.
317      */
318     g_object_class_install_property
319         (object_class,
320          PROP_DISPLAY,
321          g_param_spec_pointer("display",
322                              "Display",
323                              "The GstVaapiDisplay to use for this video pool",
324                              G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
325 }
326
327 static void
328 gst_vaapi_video_buffer_pool_init(GstVaapiVideoBufferPool *pool)
329 {
330     GstVaapiVideoBufferPoolPrivate * const priv =
331         GST_VAAPI_VIDEO_BUFFER_POOL_GET_PRIVATE(pool);
332
333     pool->priv = priv;
334
335     gst_video_info_init(&priv->video_info[0]);
336     gst_video_info_init(&priv->video_info[1]);
337 }
338
339 GstBufferPool *
340 gst_vaapi_video_buffer_pool_new(GstVaapiDisplay *display)
341 {
342     return g_object_new(GST_VAAPI_TYPE_VIDEO_BUFFER_POOL,
343         "display", display, NULL);
344 }