2
* gstvaapivideobuffer.c - Gst VA video buffer
4
* Copyright (C) 2010-2011 Splitted-Desktop Systems
5
* Copyright (C) 2011 Intel Corporation
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
24
* SECTION:gstvaapivideobuffer
25
* @short_description: VA video buffer for GStreamer
29
#include "gstvaapivideobuffer.h"
30
#include "gstvaapivideobuffer_priv.h"
31
#include "gstvaapiimagepool.h"
32
#include "gstvaapisurfacepool.h"
33
#include "gstvaapiobject_priv.h"
36
#include "gstvaapidebug.h"
38
G_DEFINE_TYPE(GstVaapiVideoBuffer, gst_vaapi_video_buffer, GST_TYPE_SURFACE_BUFFER);
40
#define GST_VAAPI_VIDEO_BUFFER_GET_PRIVATE(obj) \
41
(G_TYPE_INSTANCE_GET_PRIVATE((obj), \
42
GST_VAAPI_TYPE_VIDEO_BUFFER, \
43
GstVaapiVideoBufferPrivate))
45
struct _GstVaapiVideoBufferPrivate {
46
GstVaapiDisplay *display;
47
GstVaapiVideoPool *image_pool;
49
GstVaapiVideoPool *surface_pool;
50
GstVaapiSurface *surface;
51
GstVaapiSurfaceProxy *proxy;
56
set_display(GstVaapiVideoBuffer *buffer, GstVaapiDisplay *display)
58
GstVaapiVideoBufferPrivate * const priv = buffer->priv;
61
g_object_unref(priv->display);
66
priv->display = g_object_ref(display);
70
set_image(GstVaapiVideoBuffer *buffer, GstVaapiImage *image)
72
buffer->priv->image = g_object_ref(image);
73
set_display(buffer, GST_VAAPI_OBJECT_DISPLAY(image));
77
set_surface(GstVaapiVideoBuffer *buffer, GstVaapiSurface *surface)
79
buffer->priv->surface = g_object_ref(surface);
80
set_display(buffer, GST_VAAPI_OBJECT_DISPLAY(surface));
84
gst_vaapi_video_buffer_destroy_image(GstVaapiVideoBuffer *buffer)
86
GstVaapiVideoBufferPrivate * const priv = buffer->priv;
90
gst_vaapi_video_pool_put_object(priv->image_pool, priv->image);
92
g_object_unref(priv->image);
96
if (priv->image_pool) {
97
g_object_unref(priv->image_pool);
98
priv->image_pool = NULL;
103
gst_vaapi_video_buffer_destroy_surface(GstVaapiVideoBuffer *buffer)
105
GstVaapiVideoBufferPrivate * const priv = buffer->priv;
108
g_object_unref(priv->proxy);
113
if (priv->surface_pool)
114
gst_vaapi_video_pool_put_object(priv->surface_pool, priv->surface);
116
g_object_unref(priv->surface);
117
priv->surface = NULL;
120
if (priv->surface_pool) {
121
g_object_unref(priv->surface_pool);
122
priv->surface_pool = NULL;
126
gst_buffer_unref(priv->buffer);
132
gst_vaapi_video_buffer_finalize(GstMiniObject *object)
134
GstVaapiVideoBuffer * const buffer = GST_VAAPI_VIDEO_BUFFER(object);
135
GstMiniObjectClass *parent_class;
137
gst_vaapi_video_buffer_destroy_image(buffer);
138
gst_vaapi_video_buffer_destroy_surface(buffer);
140
set_display(buffer, NULL);
142
parent_class = GST_MINI_OBJECT_CLASS(gst_vaapi_video_buffer_parent_class);
143
if (parent_class->finalize)
144
parent_class->finalize(object);
148
gst_vaapi_video_buffer_class_init(GstVaapiVideoBufferClass *klass)
150
GstMiniObjectClass * const object_class = GST_MINI_OBJECT_CLASS(klass);
152
g_type_class_add_private(klass, sizeof(GstVaapiVideoBufferPrivate));
154
object_class->finalize = gst_vaapi_video_buffer_finalize;
158
gst_vaapi_video_buffer_init(GstVaapiVideoBuffer *buffer)
160
GstVaapiVideoBufferPrivate *priv;
162
priv = GST_VAAPI_VIDEO_BUFFER_GET_PRIVATE(buffer);
164
priv->display = NULL;
165
priv->image_pool = NULL;
167
priv->surface_pool = NULL;
168
priv->surface = NULL;
174
* gst_vaapi_video_buffer_new:
175
* @display: a #GstVaapiDisplay
177
* Creates an empty #GstBuffer. The caller is responsible for completing
178
* the initialization of the buffer with the gst_vaapi_video_buffer_set_*()
181
* Return value: the newly allocated #GstBuffer, or %NULL or error
183
static inline gpointer
184
_gst_vaapi_video_buffer_new(void)
186
return gst_mini_object_new(GST_VAAPI_TYPE_VIDEO_BUFFER);
190
gst_vaapi_video_buffer_new(GstVaapiDisplay *display)
194
g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
196
buffer = _gst_vaapi_video_buffer_new();
200
set_display(GST_VAAPI_VIDEO_BUFFER(buffer), display);
205
* gst_vaapi_video_buffer_new_from_pool:
206
* @pool: a #GstVaapiVideoPool
208
* Creates a #GstBuffer with a video object allocated from a @pool.
209
* Only #GstVaapiSurfacePool and #GstVaapiImagePool pools are supported.
211
* The buffer is destroyed through the last call to gst_buffer_unref()
212
* and the video objects are pushed back to their respective pools.
214
* Return value: the newly allocated #GstBuffer, or %NULL on error
217
gst_vaapi_video_buffer_new_from_pool(GstVaapiVideoPool *pool)
219
GstVaapiVideoBuffer *buffer;
220
gboolean is_image_pool, is_surface_pool;
222
g_return_val_if_fail(GST_VAAPI_IS_VIDEO_POOL(pool), NULL);
224
is_image_pool = GST_VAAPI_IS_IMAGE_POOL(pool);
225
is_surface_pool = GST_VAAPI_IS_SURFACE_POOL(pool);
227
if (!is_image_pool && !is_surface_pool)
230
buffer = _gst_vaapi_video_buffer_new();
233
gst_vaapi_video_buffer_set_image_from_pool(buffer, pool)) ||
235
gst_vaapi_video_buffer_set_surface_from_pool(buffer, pool)))) {
236
set_display(buffer, gst_vaapi_video_pool_get_display(pool));
237
return GST_BUFFER(buffer);
240
gst_mini_object_unref(GST_MINI_OBJECT(buffer));
245
* gst_vaapi_video_buffer_new_from_buffer:
246
* @buffer: a #GstBuffer
248
* Creates a #GstBuffer with video objects bound to @buffer video
251
* Return value: the newly allocated #GstBuffer, or %NULL on error
254
gst_vaapi_video_buffer_new_from_buffer(GstBuffer *buffer)
256
GstVaapiVideoBuffer *inbuf, *outbuf;
258
if (!GST_VAAPI_IS_VIDEO_BUFFER(buffer)) {
259
if (!buffer->parent || !GST_VAAPI_IS_VIDEO_BUFFER(buffer->parent))
261
buffer = buffer->parent;
263
inbuf = GST_VAAPI_VIDEO_BUFFER(buffer);
265
outbuf = _gst_vaapi_video_buffer_new();
269
if (inbuf->priv->image)
270
gst_vaapi_video_buffer_set_image(outbuf, inbuf->priv->image);
271
if (inbuf->priv->surface)
272
gst_vaapi_video_buffer_set_surface(outbuf, inbuf->priv->surface);
273
if (inbuf->priv->proxy)
274
gst_vaapi_video_buffer_set_surface_proxy(outbuf, inbuf->priv->proxy);
276
outbuf->priv->buffer = gst_buffer_ref(buffer);
277
return GST_BUFFER(outbuf);
281
* gst_vaapi_video_buffer_new_with_image:
282
* @image: a #GstVaapiImage
284
* Creates a #GstBuffer with the specified @image. The resulting
285
* buffer holds an additional reference to the @image.
287
* Return value: the newly allocated #GstBuffer, or %NULL on error
290
gst_vaapi_video_buffer_new_with_image(GstVaapiImage *image)
292
GstVaapiVideoBuffer *buffer;
294
g_return_val_if_fail(GST_VAAPI_IS_IMAGE(image), NULL);
296
buffer = _gst_vaapi_video_buffer_new();
298
gst_vaapi_video_buffer_set_image(buffer, image);
299
return GST_BUFFER(buffer);
303
* gst_vaapi_video_buffer_new_with_surface:
304
* @surface: a #GstVaapiSurface
306
* Creates a #GstBuffer with the specified @surface. The resulting
307
* buffer holds an additional reference to the @surface.
309
* Return value: the newly allocated #GstBuffer, or %NULL on error
312
gst_vaapi_video_buffer_new_with_surface(GstVaapiSurface *surface)
314
GstVaapiVideoBuffer *buffer;
316
g_return_val_if_fail(GST_VAAPI_IS_SURFACE(surface), NULL);
318
buffer = _gst_vaapi_video_buffer_new();
320
gst_vaapi_video_buffer_set_surface(buffer, surface);
321
return GST_BUFFER(buffer);
325
* gst_vaapi_video_buffer_new_with_surface_proxy:
326
* @proxy: a #GstVaapiSurfaceProxy
328
* Creates a #GstBuffer with the specified surface @proxy. The
329
* resulting buffer holds an additional reference to the @proxy.
331
* Return value: the newly allocated #GstBuffer, or %NULL on error
334
gst_vaapi_video_buffer_new_with_surface_proxy(GstVaapiSurfaceProxy *proxy)
336
GstVaapiVideoBuffer *buffer;
338
g_return_val_if_fail(GST_VAAPI_IS_SURFACE_PROXY(proxy), NULL);
340
buffer = _gst_vaapi_video_buffer_new();
342
gst_vaapi_video_buffer_set_surface_proxy(buffer, proxy);
343
return GST_BUFFER(buffer);
347
* gst_vaapi_video_buffer_get_display:
348
* @buffer: a #GstVaapiVideoBuffer
350
* Retrieves the #GstVaapiDisplay the @buffer is bound to. The @buffer
351
* owns the returned #GstVaapiDisplay object so the caller is
352
* responsible for calling g_object_ref() when needed.
354
* Return value: the #GstVaapiDisplay the @buffer is bound to
357
gst_vaapi_video_buffer_get_display(GstVaapiVideoBuffer *buffer)
359
g_return_val_if_fail(GST_VAAPI_IS_VIDEO_BUFFER(buffer), NULL);
361
return buffer->priv->display;
365
* gst_vaapi_video_buffer_get_image:
366
* @buffer: a #GstVaapiVideoBuffer
368
* Retrieves the #GstVaapiImage bound to the @buffer. The @buffer owns
369
* the #GstVaapiImage so the caller is responsible for calling
370
* g_object_ref() when needed.
372
* Return value: the #GstVaapiImage bound to the @buffer, or %NULL if
376
gst_vaapi_video_buffer_get_image(GstVaapiVideoBuffer *buffer)
378
g_return_val_if_fail(GST_VAAPI_IS_VIDEO_BUFFER(buffer), NULL);
380
return buffer->priv->image;
384
* gst_vaapi_video_buffer_set_image:
385
* @buffer: a #GstVaapiVideoBuffer
386
* @image: a #GstVaapiImage
388
* Binds @image to the @buffer. If the @buffer contains another image
389
* previously allocated from a pool, it's pushed back to its parent
390
* pool and the pool is also released.
393
gst_vaapi_video_buffer_set_image(
394
GstVaapiVideoBuffer *buffer,
398
g_return_if_fail(GST_VAAPI_IS_VIDEO_BUFFER(buffer));
399
g_return_if_fail(GST_VAAPI_IS_IMAGE(image));
401
gst_vaapi_video_buffer_destroy_image(buffer);
404
set_image(buffer, image);
408
* gst_vaapi_video_buffer_set_image_from_pool
409
* @buffer: a #GstVaapiVideoBuffer
410
* @pool: a #GstVaapiVideoPool
412
* Binds a newly allocated video object from the @pool. The @pool
413
* shall be of type #GstVaapiImagePool. Previously allocated objects
414
* are released and returned to their parent pools, if any.
416
* Return value: %TRUE on success
419
gst_vaapi_video_buffer_set_image_from_pool(
420
GstVaapiVideoBuffer *buffer,
421
GstVaapiVideoPool *pool
424
GstVaapiImage *image;
426
g_return_val_if_fail(GST_VAAPI_IS_VIDEO_BUFFER(buffer), FALSE);
427
g_return_val_if_fail(GST_VAAPI_IS_IMAGE_POOL(pool), FALSE);
429
gst_vaapi_video_buffer_destroy_image(buffer);
432
image = gst_vaapi_video_pool_get_object(pool);
435
set_image(buffer, image);
436
buffer->priv->image_pool = g_object_ref(pool);
442
* gst_vaapi_video_buffer_get_surface:
443
* @buffer: a #GstVaapiVideoBuffer
445
* Retrieves the #GstVaapiSurface bound to the @buffer. The @buffer
446
* owns the #GstVaapiSurface so the caller is responsible for calling
447
* g_object_ref() when needed.
449
* Return value: the #GstVaapiSurface bound to the @buffer, or %NULL if
453
gst_vaapi_video_buffer_get_surface(GstVaapiVideoBuffer *buffer)
455
g_return_val_if_fail(GST_VAAPI_IS_VIDEO_BUFFER(buffer), NULL);
457
return buffer->priv->surface;
461
* gst_vaapi_video_buffer_set_surface:
462
* @buffer: a #GstVaapiVideoBuffer
463
* @surface: a #GstVaapiSurface
465
* Binds @surface to the @buffer. If the @buffer contains another
466
* surface previously allocated from a pool, it's pushed back to its
467
* parent pool and the pool is also released.
470
gst_vaapi_video_buffer_set_surface(
471
GstVaapiVideoBuffer *buffer,
472
GstVaapiSurface *surface
475
g_return_if_fail(GST_VAAPI_IS_VIDEO_BUFFER(buffer));
476
g_return_if_fail(GST_VAAPI_IS_SURFACE(surface));
478
gst_vaapi_video_buffer_destroy_surface(buffer);
481
set_surface(buffer, surface);
485
* gst_vaapi_video_buffer_set_surface_from_pool
486
* @buffer: a #GstVaapiVideoBuffer
487
* @pool: a #GstVaapiVideoPool
489
* Binds a newly allocated video object from the @pool. The @pool
490
* shall be of type #GstVaapiSurfacePool. Previously allocated objects
491
* are released and returned to their parent pools, if any.
493
* Return value: %TRUE on success
496
gst_vaapi_video_buffer_set_surface_from_pool(
497
GstVaapiVideoBuffer *buffer,
498
GstVaapiVideoPool *pool
501
GstVaapiSurface *surface;
503
g_return_val_if_fail(GST_VAAPI_IS_VIDEO_BUFFER(buffer), FALSE);
504
g_return_val_if_fail(GST_VAAPI_IS_SURFACE_POOL(pool), FALSE);
506
gst_vaapi_video_buffer_destroy_surface(buffer);
509
surface = gst_vaapi_video_pool_get_object(pool);
512
set_surface(buffer, surface);
513
buffer->priv->surface_pool = g_object_ref(pool);
519
* gst_vaapi_video_buffer_get_surface_proxy:
520
* @buffer: a #GstVaapiVideoBuffer
522
* Retrieves the #GstVaapiSurfaceProxy bound to the @buffer. The @buffer
523
* owns the #GstVaapiSurfaceProxy so the caller is responsible for calling
524
* g_object_ref() when needed.
526
* Return value: the #GstVaapiSurfaceProxy bound to the @buffer, or
527
* %NULL if there is none
529
GstVaapiSurfaceProxy *
530
gst_vaapi_video_buffer_get_surface_proxy(GstVaapiVideoBuffer *buffer)
532
g_return_val_if_fail(GST_VAAPI_IS_VIDEO_BUFFER(buffer), NULL);
534
return buffer->priv->proxy;
538
* gst_vaapi_video_buffer_set_surface_proxy:
539
* @buffer: a #GstVaapiVideoBuffer
540
* @proxy: a #GstVaapiSurfaceProxy
542
* Binds surface @proxy to the @buffer. If the @buffer contains another
543
* surface previously allocated from a pool, it's pushed back to its
544
* parent pool and the pool is also released.
547
gst_vaapi_video_buffer_set_surface_proxy(
548
GstVaapiVideoBuffer *buffer,
549
GstVaapiSurfaceProxy *proxy
552
GstVaapiSurface *surface;
554
g_return_if_fail(GST_VAAPI_IS_VIDEO_BUFFER(buffer));
555
g_return_if_fail(GST_VAAPI_IS_SURFACE_PROXY(proxy));
557
gst_vaapi_video_buffer_destroy_surface(buffer);
560
surface = GST_VAAPI_SURFACE_PROXY_SURFACE(proxy);
563
set_surface(buffer, surface);
564
buffer->priv->proxy = g_object_ref(proxy);
569
* gst_vaapi_video_buffer_set_display:
570
* @buffer: a #GstVaapiVideoBuffer
571
* @display a #GstVaapiDisplay
573
* For subclass only, don't use.
576
gst_vaapi_video_buffer_set_display(
577
GstVaapiVideoBuffer *buffer,
578
GstVaapiDisplay *display
581
set_display(buffer, display);
585
* gst_vaapi_video_buffer_set_display:
586
* @buffer: a #GstVaapiVideoBuffer
587
* @other_buffer: a #GstBuffer
589
* For subclass only, don't use.
592
gst_vaapi_video_buffer_set_buffer(
593
GstVaapiVideoBuffer *buffer,
594
GstBuffer *other_buffer
597
g_return_if_fail (buffer->priv->buffer == NULL);
598
buffer->priv->buffer = gst_buffer_ref (other_buffer);