• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) 2020 Collabora Ltd.
3  *   Author: Nicolas Dufresne <nicolas.dufresne@collabora.com>
4  *
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.
9  *
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.
14  *
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.
19  */
20 
21 #include "gstv4l2codecpool.h"
22 
23 #include <string.h>
24 
25 struct _GstV4l2CodecPool
26 {
27   GstBufferPool parent;
28   GstAtomicQueue *queue;
29   GstV4l2CodecAllocator *allocator;
30   /* Used to set GstVideoMeta */
31   GstVideoInfo *vinfo;
32 };
33 
34 G_DEFINE_TYPE (GstV4l2CodecPool, gst_v4l2_codec_pool, GST_TYPE_BUFFER_POOL);
35 
36 static GstBuffer *
gst_v4l2_codec_pool_create_empty_buffer(void)37 gst_v4l2_codec_pool_create_empty_buffer (void)
38 {
39   GstVideoMeta *vmeta;
40   GstBuffer *buffer = gst_buffer_new ();
41 
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);
44 
45   return buffer;
46 }
47 
48 static GstFlowReturn
gst_v4l2_codec_pool_acquire_buffer(GstBufferPool * pool,GstBuffer ** buffer,GstBufferPoolAcquireParams * params)49 gst_v4l2_codec_pool_acquire_buffer (GstBufferPool * pool, GstBuffer ** buffer,
50     GstBufferPoolAcquireParams * params)
51 {
52   GstV4l2CodecPool *self = GST_V4L2_CODEC_POOL (pool);
53   GstBuffer *buf;
54   GstVideoMeta *vmeta;
55 
56   /* A GstVideoInfo must be set before buffer can be acquired */
57   g_return_val_if_fail (self->vinfo, GST_FLOW_ERROR);
58 
59   buf = gst_atomic_queue_pop (self->queue);
60   if (!buf)
61     buf = gst_v4l2_codec_pool_create_empty_buffer ();
62 
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;
66 
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;
72       } else {
73         if (!gst_v4l2_codec_allocator_wait_for_buffer (self->allocator))
74           flow_ret = GST_FLOW_FLUSHING;
75       }
76     }
77 
78     if (flow_ret != GST_FLOW_OK) {
79       gst_atomic_queue_push (self->queue, buf);
80       return flow_ret;
81     }
82 
83     /* Finally, pop the buffer we created or waited for. */
84     gst_v4l2_codec_allocator_prepare_buffer (self->allocator, buf);
85   }
86 
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));
94 
95   *buffer = buf;
96   return GST_FLOW_OK;
97 }
98 
99 static void
gst_v4l2_codec_pool_reset_buffer(GstBufferPool * pool,GstBuffer * buffer)100 gst_v4l2_codec_pool_reset_buffer (GstBufferPool * pool, GstBuffer * buffer)
101 {
102   GstBufferPoolClass *klass =
103       GST_BUFFER_POOL_CLASS (gst_v4l2_codec_pool_parent_class);
104 
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;
109 }
110 
111 static void
gst_v4l2_codec_pool_release_buffer(GstBufferPool * pool,GstBuffer * buffer)112 gst_v4l2_codec_pool_release_buffer (GstBufferPool * pool, GstBuffer * buffer)
113 {
114   GstV4l2CodecPool *self = GST_V4L2_CODEC_POOL (pool);
115   gst_atomic_queue_push (self->queue, buffer);
116 }
117 
118 static void
gst_v4l2_codec_pool_init(GstV4l2CodecPool * self)119 gst_v4l2_codec_pool_init (GstV4l2CodecPool * self)
120 {
121   self->queue = gst_atomic_queue_new (4);
122 }
123 
124 static void
gst_v4l2_codec_pool_finalize(GObject * object)125 gst_v4l2_codec_pool_finalize (GObject * object)
126 {
127   GstV4l2CodecPool *self = GST_V4L2_CODEC_POOL (object);
128   GstBuffer *buf;
129 
130   while ((buf = gst_atomic_queue_pop (self->queue)))
131     gst_buffer_unref (buf);
132 
133   gst_atomic_queue_unref (self->queue);
134   g_object_unref (self->allocator);
135 
136   if (self->vinfo)
137     gst_video_info_free (self->vinfo);
138 
139   G_OBJECT_CLASS (gst_v4l2_codec_pool_parent_class)->finalize (object);
140 }
141 
142 static void
gst_v4l2_codec_pool_class_init(GstV4l2CodecPoolClass * klass)143 gst_v4l2_codec_pool_class_init (GstV4l2CodecPoolClass * klass)
144 {
145   GObjectClass *object_class = G_OBJECT_CLASS (klass);
146   GstBufferPoolClass *pool_class = GST_BUFFER_POOL_CLASS (klass);
147 
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;
153 }
154 
155 GstV4l2CodecPool *
gst_v4l2_codec_pool_new(GstV4l2CodecAllocator * allocator,const GstVideoInfo * vinfo)156 gst_v4l2_codec_pool_new (GstV4l2CodecAllocator * allocator,
157     const GstVideoInfo * vinfo)
158 {
159   GstV4l2CodecPool *pool = g_object_new (GST_TYPE_V4L2_CODEC_POOL, NULL);
160   gsize pool_size;
161 
162   pool->allocator = g_object_ref (allocator);
163   pool->vinfo = gst_video_info_copy (vinfo);
164 
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);
169   }
170 
171   return pool;
172 }
173 
174 guint32
gst_v4l2_codec_buffer_get_index(GstBuffer * buffer)175 gst_v4l2_codec_buffer_get_index (GstBuffer * buffer)
176 {
177   GstMemory *mem = gst_buffer_peek_memory (buffer, 0);
178   return gst_v4l2_codec_memory_get_index (mem);
179 }
180