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 /* First, just try and obtain a buffer. */
64 if (!gst_v4l2_codec_allocator_prepare_buffer (self->allocator, buf)) {
65 GstFlowReturn flow_ret = GST_FLOW_OK;
67 /* If none were available, try and allocate one. */
68 if (!gst_v4l2_codec_allocator_create_buffer (self->allocator)) {
69 /* Otherwise, wait if this is allowed. */
70 if (params && params->flags & GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT) {
71 flow_ret = GST_FLOW_EOS;
73 if (!gst_v4l2_codec_allocator_wait_for_buffer (self->allocator))
74 flow_ret = GST_FLOW_FLUSHING;
78 if (flow_ret != GST_FLOW_OK) {
79 gst_atomic_queue_push (self->queue, buf);
83 /* Finally, pop the buffer we created or waited for. */
84 gst_v4l2_codec_allocator_prepare_buffer (self->allocator, buf);
87 vmeta = gst_buffer_get_video_meta (buf);
88 vmeta->format = GST_VIDEO_INFO_FORMAT (self->vinfo);
89 vmeta->width = GST_VIDEO_INFO_WIDTH (self->vinfo);
90 vmeta->height = GST_VIDEO_INFO_HEIGHT (self->vinfo);
91 vmeta->n_planes = GST_VIDEO_INFO_N_PLANES (self->vinfo);
92 memcpy (vmeta->offset, self->vinfo->offset, sizeof (vmeta->offset));
93 memcpy (vmeta->stride, self->vinfo->stride, sizeof (vmeta->stride));
100 gst_v4l2_codec_pool_reset_buffer (GstBufferPool * pool, GstBuffer * buffer)
102 GstBufferPoolClass *klass =
103 GST_BUFFER_POOL_CLASS (gst_v4l2_codec_pool_parent_class);
105 /* Clears all the memories and only pool the GstBuffer objects */
106 gst_buffer_remove_all_memory (buffer);
107 klass->reset_buffer (pool, buffer);
108 GST_BUFFER_FLAGS (buffer) = 0;
112 gst_v4l2_codec_pool_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
114 GstV4l2CodecPool *self = GST_V4L2_CODEC_POOL (pool);
115 gst_atomic_queue_push (self->queue, buffer);
119 gst_v4l2_codec_pool_init (GstV4l2CodecPool * self)
121 self->queue = gst_atomic_queue_new (4);
125 gst_v4l2_codec_pool_finalize (GObject * object)
127 GstV4l2CodecPool *self = GST_V4L2_CODEC_POOL (object);
130 while ((buf = gst_atomic_queue_pop (self->queue)))
131 gst_buffer_unref (buf);
133 gst_atomic_queue_unref (self->queue);
134 g_object_unref (self->allocator);
137 gst_video_info_free (self->vinfo);
139 G_OBJECT_CLASS (gst_v4l2_codec_pool_parent_class)->finalize (object);
143 gst_v4l2_codec_pool_class_init (GstV4l2CodecPoolClass * klass)
145 GObjectClass *object_class = G_OBJECT_CLASS (klass);
146 GstBufferPoolClass *pool_class = GST_BUFFER_POOL_CLASS (klass);
148 object_class->finalize = gst_v4l2_codec_pool_finalize;
149 pool_class->start = NULL;
150 pool_class->acquire_buffer = gst_v4l2_codec_pool_acquire_buffer;
151 pool_class->reset_buffer = gst_v4l2_codec_pool_reset_buffer;
152 pool_class->release_buffer = gst_v4l2_codec_pool_release_buffer;
156 gst_v4l2_codec_pool_new (GstV4l2CodecAllocator * allocator,
157 const GstVideoInfo * vinfo)
159 GstV4l2CodecPool *pool = g_object_new (GST_TYPE_V4L2_CODEC_POOL, NULL);
162 pool->allocator = g_object_ref (allocator);
163 pool->vinfo = gst_video_info_copy (vinfo);
165 pool_size = gst_v4l2_codec_allocator_get_pool_size (allocator);
166 for (gsize i = 0; i < pool_size; i++) {
167 GstBuffer *buffer = gst_v4l2_codec_pool_create_empty_buffer ();
168 gst_atomic_queue_push (pool->queue, buffer);
175 gst_v4l2_codec_buffer_get_index (GstBuffer * buffer)
177 GstMemory *mem = gst_buffer_peek_memory (buffer, 0);
178 return gst_v4l2_codec_memory_get_index (mem);