2 * Copyright (C) 2020 Collabora Ltd.
3 * Author: Nicolas Dufresne <nicolas.dufresne@collabora.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
21 #include "gstv4l2codecpool.h"
25 struct _GstV4l2CodecPool
28 GstAtomicQueue *queue;
29 GstV4l2CodecAllocator *allocator;
30 /* Used to set GstVideoMeta */
34 G_DEFINE_TYPE (GstV4l2CodecPool, gst_v4l2_codec_pool, GST_TYPE_BUFFER_POOL);
37 gst_v4l2_codec_pool_create_empty_buffer (void)
40 GstBuffer *buffer = gst_buffer_new ();
42 vmeta = gst_buffer_add_video_meta (buffer, 0, GST_VIDEO_FORMAT_NV12, 1, 1);
43 GST_META_FLAG_SET (vmeta, GST_META_FLAG_POOLED);
49 gst_v4l2_codec_pool_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
50 GstBufferPoolAcquireParams * params)
52 GstV4l2CodecPool *self = GST_V4L2_CODEC_POOL (pool);
56 /* A GstVideoInfo must be set before buffer can be acquired */
57 g_return_val_if_fail (self->vinfo, GST_FLOW_ERROR);
59 buf = gst_atomic_queue_pop (self->queue);
61 buf = gst_v4l2_codec_pool_create_empty_buffer ();
63 if (!gst_v4l2_codec_allocator_prepare_buffer (self->allocator, buf)) {
64 gst_atomic_queue_push (self->queue, buf);
65 return GST_FLOW_ERROR;
68 vmeta = gst_buffer_get_video_meta (buf);
69 vmeta->format = GST_VIDEO_INFO_FORMAT (self->vinfo);
70 vmeta->width = GST_VIDEO_INFO_WIDTH (self->vinfo);
71 vmeta->height = GST_VIDEO_INFO_HEIGHT (self->vinfo);
72 vmeta->n_planes = GST_VIDEO_INFO_N_PLANES (self->vinfo);
73 memcpy (vmeta->offset, self->vinfo->offset, sizeof (vmeta->offset));
74 memcpy (vmeta->stride, self->vinfo->stride, sizeof (vmeta->stride));
81 gst_v4l2_codec_pool_reset_buffer (GstBufferPool * pool, GstBuffer * buffer)
83 GstBufferPoolClass *klass =
84 GST_BUFFER_POOL_CLASS (gst_v4l2_codec_pool_parent_class);
86 /* Clears all the memories and only pool the GstBuffer objects */
87 gst_buffer_remove_all_memory (buffer);
88 klass->reset_buffer (pool, buffer);
89 GST_BUFFER_FLAGS (buffer) = 0;
93 gst_v4l2_codec_pool_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
95 GstV4l2CodecPool *self = GST_V4L2_CODEC_POOL (pool);
96 gst_atomic_queue_push (self->queue, buffer);
100 gst_v4l2_codec_pool_init (GstV4l2CodecPool * self)
102 self->queue = gst_atomic_queue_new (4);
106 gst_v4l2_codec_pool_finalize (GObject * object)
108 GstV4l2CodecPool *self = GST_V4L2_CODEC_POOL (object);
111 while ((buf = gst_atomic_queue_pop (self->queue)))
112 gst_buffer_unref (buf);
114 gst_atomic_queue_unref (self->queue);
115 g_object_unref (self->allocator);
118 gst_video_info_free (self->vinfo);
120 G_OBJECT_CLASS (gst_v4l2_codec_pool_parent_class)->finalize (object);
124 gst_v4l2_codec_pool_class_init (GstV4l2CodecPoolClass * klass)
126 GObjectClass *object_class = G_OBJECT_CLASS (klass);
127 GstBufferPoolClass *pool_class = GST_BUFFER_POOL_CLASS (klass);
129 object_class->finalize = gst_v4l2_codec_pool_finalize;
130 pool_class->start = NULL;
131 pool_class->acquire_buffer = gst_v4l2_codec_pool_acquire_buffer;
132 pool_class->reset_buffer = gst_v4l2_codec_pool_reset_buffer;
133 pool_class->release_buffer = gst_v4l2_codec_pool_release_buffer;
137 gst_v4l2_codec_pool_new (GstV4l2CodecAllocator * allocator,
138 const GstVideoInfo * vinfo)
140 GstV4l2CodecPool *pool = g_object_new (GST_TYPE_V4L2_CODEC_POOL, NULL);
143 pool->allocator = g_object_ref (allocator);
144 pool->vinfo = gst_video_info_copy (vinfo);
146 pool_size = gst_v4l2_codec_allocator_get_pool_size (allocator);
147 for (gsize i = 0; i < pool_size; i++) {
148 GstBuffer *buffer = gst_v4l2_codec_pool_create_empty_buffer ();
149 gst_atomic_queue_push (pool->queue, buffer);
156 gst_v4l2_codec_buffer_get_index (GstBuffer * buffer)
158 GstMemory *mem = gst_buffer_peek_memory (buffer, 0);
159 return gst_v4l2_codec_memory_get_index (mem);